| 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/permissions/permissions_data.h" |
| 19 #include "extensions/common/user_script.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 const base::Closure& callback); | |
| 57 | |
| 58 // Notifies the ActiveScriptController that an extension has been granted | 49 // Notifies the ActiveScriptController that an extension has been granted |
| 59 // active tab permissions. This will run any pending injections for that | 50 // active tab permissions. This will run any pending injections for that |
| 60 // extension. | 51 // extension. |
| 61 void OnActiveTabPermissionGranted(const Extension* extension); | 52 void OnActiveTabPermissionGranted(const Extension* extension); |
| 62 | 53 |
| 63 // Notifies the ActiveScriptController of detected ad injection. | 54 // Notifies the ActiveScriptController of detected ad injection. |
| 64 void OnAdInjectionDetected(const std::set<std::string>& ad_injectors); | 55 void OnAdInjectionDetected(const std::set<std::string>& ad_injectors); |
| 65 | 56 |
| 66 // LocationBarControllerProvider implementation. | 57 // LocationBarControllerProvider implementation. |
| 67 virtual ExtensionAction* GetActionForExtension( | 58 virtual ExtensionAction* GetActionForExtension( |
| 68 const Extension* extension) OVERRIDE; | 59 const Extension* extension) OVERRIDE; |
| 69 virtual LocationBarController::Action OnClicked( | 60 virtual LocationBarController::Action OnClicked( |
| 70 const Extension* extension) OVERRIDE; | 61 const Extension* extension) OVERRIDE; |
| 71 virtual void OnNavigated() OVERRIDE; | 62 virtual void OnNavigated() OVERRIDE; |
| 72 virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE; | 63 virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE; |
| 73 | 64 |
| 65 #if defined(UNIT_TEST) |
| 66 // Only used in tests. |
| 67 PermissionsData::AccessType RequiresUserConsentForScriptInjectionForTesting( |
| 68 const Extension* extension, |
| 69 UserScript::InjectionType type) { |
| 70 return RequiresUserConsentForScriptInjection(extension, type); |
| 71 } |
| 72 void RequestScriptInjectionForTesting(const Extension* extension, |
| 73 const base::Closure& callback) { |
| 74 return RequestScriptInjection(extension, callback); |
| 75 } |
| 76 #endif // defined(UNIT_TEST) |
| 77 |
| 74 private: | 78 private: |
| 75 typedef std::vector<base::Closure> PendingRequestList; | 79 typedef std::vector<base::Closure> PendingRequestList; |
| 76 typedef std::map<std::string, PendingRequestList> PendingRequestMap; | 80 typedef std::map<std::string, PendingRequestList> PendingRequestMap; |
| 77 | 81 |
| 82 // Returns true if the extension requesting script injection requires |
| 83 // user consent. If this is true, the caller should then register a request |
| 84 // via RequestScriptInjection(). |
| 85 PermissionsData::AccessType RequiresUserConsentForScriptInjection( |
| 86 const Extension* extension, |
| 87 UserScript::InjectionType type); |
| 88 |
| 89 // |callback|. The only assumption that can be made about when (or if) |
| 90 // |callback| is run is that, if it is run, it will run on the current page. |
| 91 void RequestScriptInjection(const Extension* extension, |
| 92 const base::Closure& callback); |
| 93 |
| 94 // Register a request for a script injection, to be executed by running |
| 78 // Runs any pending injections for the corresponding extension. | 95 // Runs any pending injections for the corresponding extension. |
| 79 void RunPendingForExtension(const Extension* extension); | 96 void RunPendingForExtension(const Extension* extension); |
| 80 | 97 |
| 81 // Handle the RequestScriptInjectionPermission message. | 98 // Handle the RequestScriptInjectionPermission message. |
| 82 void OnRequestScriptInjectionPermission(const std::string& extension_id, | 99 void OnRequestScriptInjectionPermission( |
| 83 int64 request_id); | 100 const std::string& extension_id, |
| 101 UserScript::InjectionType script_type, |
| 102 int64 request_id); |
| 84 | 103 |
| 85 // Grants permission for the given request to run. | 104 // Grants permission for the given request to run. |
| 86 void PermitScriptInjection(int64 request_id); | 105 void PermitScriptInjection(int64 request_id); |
| 87 | 106 |
| 88 // content::WebContentsObserver implementation. | 107 // content::WebContentsObserver implementation. |
| 89 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 108 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 90 | 109 |
| 91 // Log metrics. | 110 // Log metrics. |
| 92 void LogUMA() const; | 111 void LogUMA() const; |
| 93 | 112 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 110 // that get generated for extensions that haven't declared anything. | 129 // that get generated for extensions that haven't declared anything. |
| 111 typedef std::map<std::string, linked_ptr<ExtensionAction> > ActiveScriptMap; | 130 typedef std::map<std::string, linked_ptr<ExtensionAction> > ActiveScriptMap; |
| 112 ActiveScriptMap active_script_actions_; | 131 ActiveScriptMap active_script_actions_; |
| 113 | 132 |
| 114 DISALLOW_COPY_AND_ASSIGN(ActiveScriptController); | 133 DISALLOW_COPY_AND_ASSIGN(ActiveScriptController); |
| 115 }; | 134 }; |
| 116 | 135 |
| 117 } // namespace extensions | 136 } // namespace extensions |
| 118 | 137 |
| 119 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ | 138 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ |
| OLD | NEW |