OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/memory/linked_ptr.h" | 15 #include "base/memory/linked_ptr.h" |
16 #include "chrome/browser/extensions/location_bar_controller.h" | 16 #include "chrome/browser/extensions/location_bar_controller.h" |
17 #include "content/public/browser/web_contents_observer.h" | 17 #include "content/public/browser/web_contents_observer.h" |
| 18 #include "extensions/common/constants.h" |
| 19 #include "extensions/common/permissions/permissions_data.h" |
18 | 20 |
19 namespace content { | 21 namespace content { |
20 class WebContents; | 22 class WebContents; |
21 } | 23 } |
22 | 24 |
23 namespace IPC { | 25 namespace IPC { |
24 class Message; | 26 class Message; |
25 } | 27 } |
26 | 28 |
27 class ExtensionAction; | 29 class ExtensionAction; |
28 | 30 |
29 namespace extensions { | 31 namespace extensions { |
30 class Extension; | 32 class Extension; |
31 | 33 |
32 // The provider for ExtensionActions corresponding to scripts which are actively | 34 // The provider for ExtensionActions corresponding to scripts which are actively |
33 // running or need permission. | 35 // running or need permission. |
34 // TODO(rdevlin.cronin): This isn't really a controller, but it has good parity | 36 // TODO(rdevlin.cronin): This isn't really a controller, but it has good parity |
35 // with PageAction"Controller". | 37 // with PageAction"Controller". |
36 class ActiveScriptController : public LocationBarController::ActionProvider, | 38 class ActiveScriptController : public LocationBarController::ActionProvider, |
37 public content::WebContentsObserver { | 39 public content::WebContentsObserver { |
38 public: | 40 public: |
39 explicit ActiveScriptController(content::WebContents* web_contents); | 41 explicit ActiveScriptController(content::WebContents* web_contents); |
40 virtual ~ActiveScriptController(); | 42 virtual ~ActiveScriptController(); |
41 | 43 |
42 // Returns the ActiveScriptController for the given |web_contents|, or NULL | 44 // Returns the ActiveScriptController for the given |web_contents|, or NULL |
43 // if one does not exist. | 45 // if one does not exist. |
44 static ActiveScriptController* GetForWebContents( | 46 static ActiveScriptController* GetForWebContents( |
45 content::WebContents* web_contents); | 47 content::WebContents* web_contents); |
46 | 48 |
47 // Returns true if the extension requesting script injection requires | |
48 // user consent. If this is true, the caller should then register a request | |
49 // via RequestScriptInjection(). | |
50 bool RequiresUserConsentForScriptInjection(const Extension* extension); | |
51 | |
52 // Register a request for a script injection, to be executed by running | |
53 // |callback|. The only assumption that can be made about when (or if) | |
54 // |callback| is run is that, if it is run, it will run on the current page. | |
55 void RequestScriptInjection(const Extension* extension, | |
56 int page_id, | |
57 const base::Closure& callback); | |
58 | |
59 // Notifies the ActiveScriptController that an extension has been granted | 49 // Notifies the ActiveScriptController that an extension has been granted |
60 // active tab permissions. This will run any pending injections for that | 50 // active tab permissions. This will run any pending injections for that |
61 // extension. | 51 // extension. |
62 void OnActiveTabPermissionGranted(const Extension* extension); | 52 void OnActiveTabPermissionGranted(const Extension* extension); |
63 | 53 |
64 // Notifies the ActiveScriptController of detected ad injection. | 54 // Notifies the ActiveScriptController of detected ad injection. |
65 void OnAdInjectionDetected(const std::set<std::string>& ad_injectors); | 55 void OnAdInjectionDetected(const std::set<std::string>& ad_injectors); |
66 | 56 |
67 // LocationBarControllerProvider implementation. | 57 // LocationBarControllerProvider implementation. |
68 virtual ExtensionAction* GetActionForExtension( | 58 virtual ExtensionAction* GetActionForExtension( |
69 const Extension* extension) OVERRIDE; | 59 const Extension* extension) OVERRIDE; |
70 virtual LocationBarController::Action OnClicked( | 60 virtual LocationBarController::Action OnClicked( |
71 const Extension* extension) OVERRIDE; | 61 const Extension* extension) OVERRIDE; |
72 virtual void OnNavigated() OVERRIDE; | 62 virtual void OnNavigated() OVERRIDE; |
73 virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE; | 63 virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE; |
74 | 64 |
| 65 #if defined(UNIT_TEST) |
| 66 // Only used in tests. |
| 67 PermissionsData::AccessType RequiresUserConsentForScriptInjectionForTesting( |
| 68 const Extension* extension, extension_misc::InjectedScriptType type) { |
| 69 return RequiresUserConsentForScriptInjection(extension, type); |
| 70 } |
| 71 void RequestScriptInjectionForTesting(const Extension* extension, |
| 72 int page_id, |
| 73 const base::Closure& callback) { |
| 74 return RequestScriptInjection(extension, page_id, callback); |
| 75 } |
| 76 #endif // defined(UNIT_TEST) |
| 77 |
75 private: | 78 private: |
76 // A single pending request. This could be a pair, but we'd have way too many | 79 // A single pending request. This could be a pair, but we'd have way too many |
77 // stl typedefs, and "request.closure" is nicer than "request.first". | 80 // stl typedefs, and "request.closure" is nicer than "request.first". |
78 struct PendingRequest { | 81 struct PendingRequest { |
79 PendingRequest(); // For STL. | 82 PendingRequest(); // For STL. |
80 PendingRequest(const base::Closure& closure, int page_id); | 83 PendingRequest(const base::Closure& closure, int page_id); |
81 ~PendingRequest(); | 84 ~PendingRequest(); |
82 | 85 |
83 base::Closure closure; | 86 base::Closure closure; |
84 int page_id; | 87 int page_id; |
85 }; | 88 }; |
86 typedef std::vector<PendingRequest> PendingRequestList; | 89 typedef std::vector<PendingRequest> PendingRequestList; |
87 typedef std::map<std::string, PendingRequestList> PendingRequestMap; | 90 typedef std::map<std::string, PendingRequestList> PendingRequestMap; |
88 | 91 |
| 92 // Returns true if the extension requesting script injection requires |
| 93 // user consent. If this is true, the caller should then register a request |
| 94 // via RequestScriptInjection(). |
| 95 PermissionsData::AccessType RequiresUserConsentForScriptInjection( |
| 96 const Extension* extension, extension_misc::InjectedScriptType type); |
| 97 |
| 98 // |callback|. The only assumption that can be made about when (or if) |
| 99 // |callback| is run is that, if it is run, it will run on the current page. |
| 100 void RequestScriptInjection(const Extension* extension, |
| 101 int page_id, |
| 102 const base::Closure& callback); |
| 103 |
| 104 // Register a request for a script injection, to be executed by running |
89 // Runs any pending injections for the corresponding extension. | 105 // Runs any pending injections for the corresponding extension. |
90 void RunPendingForExtension(const Extension* extension); | 106 void RunPendingForExtension(const Extension* extension); |
91 | 107 |
92 // Handle the RequestScriptInjectionPermission message. | 108 // Handle the RequestScriptInjectionPermission message. |
93 void OnRequestScriptInjectionPermission(const std::string& extension_id, | 109 void OnRequestScriptInjectionPermission( |
94 int page_id, | 110 const std::string& extension_id, |
95 int request_id); | 111 extension_misc::InjectedScriptType script_type, |
| 112 int page_id, |
| 113 int request_id); |
96 | 114 |
97 // Grants permission for the given request to run. | 115 // Grants permission for the given request to run. |
98 void PermitScriptInjection(int request_id); | 116 void PermitScriptInjection(int request_id); |
99 | 117 |
100 // content::WebContentsObserver implementation. | 118 // content::WebContentsObserver implementation. |
101 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 119 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
102 | 120 |
103 // Log metrics. | 121 // Log metrics. |
104 void LogUMA() const; | 122 void LogUMA() const; |
105 | 123 |
(...skipping 16 matching lines...) Expand all Loading... |
122 // that get generated for extensions that haven't declared anything. | 140 // that get generated for extensions that haven't declared anything. |
123 typedef std::map<std::string, linked_ptr<ExtensionAction> > ActiveScriptMap; | 141 typedef std::map<std::string, linked_ptr<ExtensionAction> > ActiveScriptMap; |
124 ActiveScriptMap active_script_actions_; | 142 ActiveScriptMap active_script_actions_; |
125 | 143 |
126 DISALLOW_COPY_AND_ASSIGN(ActiveScriptController); | 144 DISALLOW_COPY_AND_ASSIGN(ActiveScriptController); |
127 }; | 145 }; |
128 | 146 |
129 } // namespace extensions | 147 } // namespace extensions |
130 | 148 |
131 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ | 149 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ |
OLD | NEW |