| 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 "base/scoped_observer.h" |
| 16 #include "chrome/browser/extensions/location_bar_controller.h" | 17 #include "chrome/browser/extensions/location_bar_controller.h" |
| 17 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
| 19 #include "extensions/browser/extension_registry_observer.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; |
| 33 class ExtensionRegistry; |
| 31 | 34 |
| 32 // The provider for ExtensionActions corresponding to scripts which are actively | 35 // The provider for ExtensionActions corresponding to scripts which are actively |
| 33 // running or need permission. | 36 // running or need permission. |
| 34 // TODO(rdevlin.cronin): This isn't really a controller, but it has good parity | 37 // TODO(rdevlin.cronin): This isn't really a controller, but it has good parity |
| 35 // with PageAction"Controller". | 38 // with PageAction"Controller". |
| 36 class ActiveScriptController : public LocationBarController::ActionProvider, | 39 class ActiveScriptController : public LocationBarController::ActionProvider, |
| 37 public content::WebContentsObserver { | 40 public content::WebContentsObserver, |
| 41 public ExtensionRegistryObserver { |
| 38 public: | 42 public: |
| 39 explicit ActiveScriptController(content::WebContents* web_contents); | 43 explicit ActiveScriptController(content::WebContents* web_contents); |
| 40 virtual ~ActiveScriptController(); | 44 virtual ~ActiveScriptController(); |
| 41 | 45 |
| 42 // Returns the ActiveScriptController for the given |web_contents|, or NULL | 46 // Returns the ActiveScriptController for the given |web_contents|, or NULL |
| 43 // if one does not exist. | 47 // if one does not exist. |
| 44 static ActiveScriptController* GetForWebContents( | 48 static ActiveScriptController* GetForWebContents( |
| 45 content::WebContents* web_contents); | 49 content::WebContents* web_contents); |
| 46 | 50 |
| 47 // Returns true if the extension requesting script injection requires | 51 // Returns true if the extension requesting script injection requires |
| 48 // user consent. If this is true, the caller should then register a request | 52 // user consent. If this is true, the caller should then register a request |
| 49 // via RequestScriptInjection(). | 53 // via RequestScriptInjection(). |
| 50 bool RequiresUserConsentForScriptInjection(const Extension* extension); | 54 bool RequiresUserConsentForScriptInjection(const Extension* extension); |
| 51 | 55 |
| 52 // Register a request for a script injection, to be executed by running | 56 // 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) | 57 // |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. | 58 // |callback| is run is that, if it is run, it will run on the current page. |
| 55 void RequestScriptInjection(const Extension* extension, | 59 void RequestScriptInjection(const Extension* extension, |
| 56 int page_id, | 60 int page_id, |
| 57 const base::Closure& callback); | 61 const base::Closure& callback); |
| 58 | 62 |
| 59 // Notifies the ActiveScriptController that an extension has been granted | 63 // Notifies the ActiveScriptController that an extension has been granted |
| 60 // active tab permissions. This will run any pending injections for that | 64 // active tab permissions. This will run any pending injections for that |
| 61 // extension. | 65 // extension. |
| 62 void OnActiveTabPermissionGranted(const Extension* extension); | 66 void OnActiveTabPermissionGranted(const Extension* extension); |
| 63 | 67 |
| 64 // Notifies the ActiveScriptController of detected ad injection. | 68 // Notifies the ActiveScriptController of detected ad injection. |
| 65 void OnAdInjectionDetected(const std::set<std::string> ad_injectors); | 69 void OnAdInjectionDetected(const std::set<std::string>& ad_injectors); |
| 66 | 70 |
| 67 // LocationBarControllerProvider implementation. | 71 // LocationBarControllerProvider implementation. |
| 68 virtual ExtensionAction* GetActionForExtension( | 72 virtual ExtensionAction* GetActionForExtension( |
| 69 const Extension* extension) OVERRIDE; | 73 const Extension* extension) OVERRIDE; |
| 70 virtual LocationBarController::Action OnClicked( | 74 virtual LocationBarController::Action OnClicked( |
| 71 const Extension* extension) OVERRIDE; | 75 const Extension* extension) OVERRIDE; |
| 72 virtual void OnNavigated() OVERRIDE; | 76 virtual void OnNavigated() OVERRIDE; |
| 73 | 77 |
| 74 private: | 78 private: |
| 75 // 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 |
| 76 // stl typedefs, and "request.closure" is nicer than "request.first". | 80 // stl typedefs, and "request.closure" is nicer than "request.first". |
| 77 struct PendingRequest { | 81 struct PendingRequest { |
| 78 PendingRequest(); // For STL. | 82 PendingRequest(); // For STL. |
| 79 PendingRequest(const base::Closure& closure, int page_id); | 83 PendingRequest(const base::Closure& closure, int page_id); |
| 80 ~PendingRequest(); | 84 ~PendingRequest(); |
| 81 | 85 |
| 82 base::Closure closure; | 86 base::Closure closure; |
| 83 int page_id; | 87 int page_id; |
| 84 }; | 88 }; |
| 85 typedef std::vector<PendingRequest> PendingRequestList; | 89 typedef std::vector<PendingRequest> PendingRequestList; |
| 86 typedef std::map<std::string, PendingRequestList> PendingRequestMap; | 90 typedef std::map<std::string, PendingRequestList> PendingRequestMap; |
| 87 | 91 |
| 88 // Runs any pending injections for the corresponding extension. | 92 // Runs any pending injections for the corresponding extension. |
| 89 void RunPendingForExtension(const Extension* extension); | 93 void RunPendingForExtension(const Extension* extension); |
| 90 | 94 |
| 91 // Handles the NotifyExtensionScriptExecution message. | 95 // Handle the RequestContentScriptPermission message. |
| 92 void OnNotifyExtensionScriptExecution(const std::string& extension_id, | 96 void OnRequestContentScriptPermission(const std::string& extension_id, |
| 93 int page_id); | 97 int page_id, |
| 98 int request_id); |
| 99 |
| 100 // Grants permission for the given request to run. |
| 101 void GrantContentScriptPermission(int request_id); |
| 94 | 102 |
| 95 // content::WebContentsObserver implementation. | 103 // content::WebContentsObserver implementation. |
| 96 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 104 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 97 | 105 |
| 106 // ExtensionRegistryObserver implementation. |
| 107 virtual void OnExtensionUnloaded( |
| 108 content::BrowserContext* browser_context, |
| 109 const Extension* extension, |
| 110 UnloadedExtensionInfo::Reason reason) OVERRIDE; |
| 111 |
| 98 // Log metrics. | 112 // Log metrics. |
| 99 void LogUMA() const; | 113 void LogUMA() const; |
| 100 | 114 |
| 101 // Whether or not the ActiveScriptController is enabled (corresponding to the | 115 // Whether or not the ActiveScriptController is enabled (corresponding to the |
| 102 // kActiveScriptEnforcement switch). If it is not, it acts as an empty shell, | 116 // kActiveScriptEnforcement switch). If it is not, it acts as an empty shell, |
| 103 // always allowing scripts to run and never displaying actions. | 117 // always allowing scripts to run and never displaying actions. |
| 104 bool enabled_; | 118 bool enabled_; |
| 105 | 119 |
| 106 // The map of extension_id:pending_request of all pending requests. | 120 // The map of extension_id:pending_request of all pending requests. |
| 107 PendingRequestMap pending_requests_; | 121 PendingRequestMap pending_requests_; |
| 108 | 122 |
| 109 // The extensions which have been granted permission to run on the given page. | 123 // The extensions which have been granted permission to run on the given page. |
| 110 // TODO(rdevlin.cronin): Right now, this just keeps track of extensions that | 124 // TODO(rdevlin.cronin): Right now, this just keeps track of extensions that |
| 111 // have been permitted to run on the page via this interface. Instead, it | 125 // have been permitted to run on the page via this interface. Instead, it |
| 112 // should incorporate more fully with ActiveTab. | 126 // should incorporate more fully with ActiveTab. |
| 113 std::set<std::string> permitted_extensions_; | 127 std::set<std::string> permitted_extensions_; |
| 114 | 128 |
| 115 // Script badges that have been generated for extensions. This is both those | 129 // Script badges that have been generated for extensions. This is both those |
| 116 // with actions already declared that are copied and normalised, and actions | 130 // with actions already declared that are copied and normalised, and actions |
| 117 // that get generated for extensions that haven't declared anything. | 131 // that get generated for extensions that haven't declared anything. |
| 118 typedef std::map<std::string, linked_ptr<ExtensionAction> > ActiveScriptMap; | 132 typedef std::map<std::string, linked_ptr<ExtensionAction> > ActiveScriptMap; |
| 119 ActiveScriptMap active_script_actions_; | 133 ActiveScriptMap active_script_actions_; |
| 120 | 134 |
| 135 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 136 extension_registry_observer_; |
| 137 |
| 121 DISALLOW_COPY_AND_ASSIGN(ActiveScriptController); | 138 DISALLOW_COPY_AND_ASSIGN(ActiveScriptController); |
| 122 }; | 139 }; |
| 123 | 140 |
| 124 } // namespace extensions | 141 } // namespace extensions |
| 125 | 142 |
| 126 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ | 143 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ |
| OLD | NEW |