[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be | ||||
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 78ce302 | 2012-09-24 01:48:48 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVE_TAB_PERMISSION_GRANTER_H_ |
6 | #define CHROME_BROWSER_EXTENSIONS_ACTIVE_TAB_PERMISSION_GRANTER_H_ | ||||
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 7 | |
Ivan Sandrk | e2b20c6 | 2018-09-10 16:23:53 | [diff] [blame] | 8 | #include <memory> |
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 9 | #include <set> |
10 | #include <string> | ||||
11 | |||||
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 01d3a96 | 2014-04-11 17:54:07 | [diff] [blame] | 13 | #include "base/scoped_observer.h" |
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 14 | #include "content/public/browser/web_contents_observer.h" |
Evan Stade | 75872a6 | 2019-09-06 21:17:38 | [diff] [blame] | 15 | #include "extensions/browser/extension_registry.h" |
[email protected] | 01d3a96 | 2014-04-11 17:54:07 | [diff] [blame] | 16 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 17 | #include "extensions/common/extension_set.h" |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 18 | #include "extensions/common/url_pattern_set.h" |
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 19 | |
[email protected] | 6c4f0a99 | 2012-07-18 07:41:06 | [diff] [blame] | 20 | class Profile; |
21 | |||||
22 | namespace content { | ||||
23 | class WebContents; | ||||
24 | } | ||||
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 25 | |
26 | namespace extensions { | ||||
27 | |||||
28 | class Extension; | ||||
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 29 | |
30 | // Responsible for granting and revoking tab-specific permissions to extensions | ||||
[email protected] | 70b84c6 | 2013-04-18 10:51:59 | [diff] [blame] | 31 | // with the activeTab or tabCapture permission. |
[email protected] | 01d3a96 | 2014-04-11 17:54:07 | [diff] [blame] | 32 | class ActiveTabPermissionGranter |
33 | : public content::WebContentsObserver, | ||||
34 | public extensions::ExtensionRegistryObserver { | ||||
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 35 | public: |
isandrk | 98a3e4a1 | 2017-05-26 21:14:50 | [diff] [blame] | 36 | // Platform specific delegate. |
37 | class Delegate { | ||||
38 | public: | ||||
39 | virtual ~Delegate() {} | ||||
40 | // Platform specific check whether the activeTab permission is allowed. | ||||
Ivan Sandrk | cca3f2d9c | 2018-06-19 10:53:44 | [diff] [blame] | 41 | virtual bool ShouldGrantActiveTabOrPrompt( |
42 | const Extension* extension, | ||||
43 | content::WebContents* web_contents) = 0; | ||||
isandrk | 98a3e4a1 | 2017-05-26 21:14:50 | [diff] [blame] | 44 | }; |
45 | |||||
[email protected] | 78ce302 | 2012-09-24 01:48:48 | [diff] [blame] | 46 | ActiveTabPermissionGranter(content::WebContents* web_contents, |
[email protected] | 6c4f0a99 | 2012-07-18 07:41:06 | [diff] [blame] | 47 | int tab_id, |
48 | Profile* profile); | ||||
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 49 | ~ActiveTabPermissionGranter() override; |
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 50 | |
Ivan Sandrk | e2b20c6 | 2018-09-10 16:23:53 | [diff] [blame] | 51 | // Platform specific delegate should be set during startup. |
52 | static void SetPlatformDelegate(std::unique_ptr<Delegate> delegate); | ||||
isandrk | 98a3e4a1 | 2017-05-26 21:14:50 | [diff] [blame] | 53 | |
[email protected] | 70b84c6 | 2013-04-18 10:51:59 | [diff] [blame] | 54 | // If |extension| has the activeTab or tabCapture permission, grants |
55 | // tab-specific permissions to it until the next page navigation or refresh. | ||||
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 56 | void GrantIfRequested(const Extension* extension); |
57 | |||||
rdevlin.cronin | 9a62870f | 2016-02-11 23:25:58 | [diff] [blame] | 58 | // Clears tab-specific permissions for all extensions. Used only for testing. |
59 | void RevokeForTesting(); | ||||
60 | |||||
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 61 | private: |
[email protected] | 053ac71 | 2013-10-18 19:52:42 | [diff] [blame] | 62 | // content::WebContentsObserver implementation. |
jam | ca0cadf0 | 2017-02-07 05:16:48 | [diff] [blame] | 63 | void DidFinishNavigation( |
64 | content::NavigationHandle* navigation_handle) override; | ||||
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 65 | void WebContentsDestroyed() override; |
[email protected] | 053ac71 | 2013-10-18 19:52:42 | [diff] [blame] | 66 | |
[email protected] | 01d3a96 | 2014-04-11 17:54:07 | [diff] [blame] | 67 | // extensions::ExtensionRegistryObserver implementation. |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 68 | void OnExtensionUnloaded(content::BrowserContext* browser_context, |
69 | const Extension* extension, | ||||
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 70 | UnloadedExtensionReason reason) override; |
[email protected] | 053ac71 | 2013-10-18 19:52:42 | [diff] [blame] | 71 | |
72 | // Clears any tab-specific permissions for all extensions on |tab_id_| and | ||||
73 | // notifies renderers. | ||||
74 | void ClearActiveExtensionsAndNotify(); | ||||
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 75 | |
[email protected] | 6c4f0a99 | 2012-07-18 07:41:06 | [diff] [blame] | 76 | // The tab ID for this tab. |
77 | int tab_id_; | ||||
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 78 | |
[email protected] | 053ac71 | 2013-10-18 19:52:42 | [diff] [blame] | 79 | // Extensions with the activeTab permission that have been granted |
80 | // tab-specific permissions until the next navigation/refresh. | ||||
81 | ExtensionSet granted_extensions_; | ||||
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 82 | |
[email protected] | 053ac71 | 2013-10-18 19:52:42 | [diff] [blame] | 83 | // Listen to extension unloaded notifications. |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 84 | ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
Evan Stade | 75872a6 | 2019-09-06 21:17:38 | [diff] [blame] | 85 | extension_registry_observer_{this}; |
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 86 | |
[email protected] | 78ce302 | 2012-09-24 01:48:48 | [diff] [blame] | 87 | DISALLOW_COPY_AND_ASSIGN(ActiveTabPermissionGranter); |
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 88 | }; |
89 | |||||
90 | } // namespace extensions | ||||
91 | |||||
[email protected] | 78ce302 | 2012-09-24 01:48:48 | [diff] [blame] | 92 | #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_TAB_PERMISSION_GRANTER_H_ |