[email protected] | 39ef0a7c5 | 2014-05-11 01:40:00 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <string> |
[email protected] | ac02ac5 | 2014-05-20 01:11:26 | [diff] [blame] | 11 | #include <vector> |
[email protected] | 39ef0a7c5 | 2014-05-11 01:40:00 | [diff] [blame] | 12 | |
[email protected] | ac02ac5 | 2014-05-20 01:11:26 | [diff] [blame] | 13 | #include "base/callback.h" |
[email protected] | 39ef0a7c5 | 2014-05-11 01:40:00 | [diff] [blame] | 14 | #include "base/compiler_specific.h" |
| 15 | #include "base/memory/linked_ptr.h" |
| 16 | #include "chrome/browser/extensions/location_bar_controller.h" |
| 17 | #include "content/public/browser/web_contents_observer.h" |
| 18 | |
| 19 | namespace content { |
| 20 | class WebContents; |
| 21 | } |
| 22 | |
| 23 | namespace IPC { |
| 24 | class Message; |
| 25 | } |
| 26 | |
| 27 | class ExtensionAction; |
| 28 | |
| 29 | namespace extensions { |
| 30 | class Extension; |
| 31 | |
| 32 | // The provider for ExtensionActions corresponding to scripts which are actively |
| 33 | // running or need permission. |
| 34 | // TODO(rdevlin.cronin): This isn't really a controller, but it has good parity |
| 35 | // with PageAction"Controller". |
| 36 | class ActiveScriptController : public LocationBarController::ActionProvider, |
| 37 | public content::WebContentsObserver { |
| 38 | public: |
| 39 | explicit ActiveScriptController(content::WebContents* web_contents); |
| 40 | virtual ~ActiveScriptController(); |
| 41 | |
[email protected] | eac223a | 2014-05-13 17:39:57 | [diff] [blame] | 42 | // Returns the ActiveScriptController for the given |web_contents|, or NULL |
| 43 | // if one does not exist. |
| 44 | static ActiveScriptController* GetForWebContents( |
| 45 | content::WebContents* web_contents); |
| 46 | |
[email protected] | ac02ac5 | 2014-05-20 01:11:26 | [diff] [blame] | 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); |
[email protected] | 39ef0a7c5 | 2014-05-11 01:40:00 | [diff] [blame] | 58 | |
[email protected] | 11814f5 | 2014-05-23 06:50:35 | [diff] [blame^] | 59 | // Notifies the ActiveScriptController that an extension has been granted |
| 60 | // active tab permissions. This will run any pending injections for that |
| 61 | // extension. |
| 62 | void OnActiveTabPermissionGranted(const Extension* extension); |
| 63 | |
[email protected] | 39ef0a7c5 | 2014-05-11 01:40:00 | [diff] [blame] | 64 | // Notifies the ActiveScriptController of detected ad injection. |
| 65 | void OnAdInjectionDetected(const std::vector<std::string> ad_injectors); |
| 66 | |
| 67 | // LocationBarControllerProvider implementation. |
| 68 | virtual ExtensionAction* GetActionForExtension( |
| 69 | const Extension* extension) OVERRIDE; |
| 70 | virtual LocationBarController::Action OnClicked( |
| 71 | const Extension* extension) OVERRIDE; |
| 72 | virtual void OnNavigated() OVERRIDE; |
| 73 | |
| 74 | private: |
[email protected] | ac02ac5 | 2014-05-20 01:11:26 | [diff] [blame] | 75 | // 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". |
| 77 | struct PendingRequest { |
| 78 | PendingRequest(); // For STL. |
| 79 | PendingRequest(const base::Closure& closure, int page_id); |
| 80 | ~PendingRequest(); |
[email protected] | 39ef0a7c5 | 2014-05-11 01:40:00 | [diff] [blame] | 81 | |
[email protected] | ac02ac5 | 2014-05-20 01:11:26 | [diff] [blame] | 82 | base::Closure closure; |
| 83 | int page_id; |
| 84 | }; |
| 85 | typedef std::vector<PendingRequest> PendingRequestList; |
| 86 | typedef std::map<std::string, PendingRequestList> PendingRequestMap; |
| 87 | |
[email protected] | 11814f5 | 2014-05-23 06:50:35 | [diff] [blame^] | 88 | // Runs any pending injections for the corresponding extension. |
| 89 | void RunPendingForExtension(const Extension* extension); |
| 90 | |
[email protected] | ac02ac5 | 2014-05-20 01:11:26 | [diff] [blame] | 91 | // Handles the NotifyExtensionScriptExecution message. |
[email protected] | 39ef0a7c5 | 2014-05-11 01:40:00 | [diff] [blame] | 92 | void OnNotifyExtensionScriptExecution(const std::string& extension_id, |
| 93 | int page_id); |
| 94 | |
[email protected] | ac02ac5 | 2014-05-20 01:11:26 | [diff] [blame] | 95 | // content::WebContentsObserver implementation. |
| 96 | virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 97 | |
[email protected] | 39ef0a7c5 | 2014-05-11 01:40:00 | [diff] [blame] | 98 | // Log metrics. |
| 99 | void LogUMA() const; |
| 100 | |
| 101 | // Whether or not the ActiveScriptController is enabled (corresponding to the |
| 102 | // kActiveScriptEnforcement switch). If it is not, it acts as an empty shell, |
| 103 | // always allowing scripts to run and never displaying actions. |
| 104 | bool enabled_; |
| 105 | |
[email protected] | ac02ac5 | 2014-05-20 01:11:26 | [diff] [blame] | 106 | // The map of extension_id:pending_request of all pending requests. |
| 107 | PendingRequestMap pending_requests_; |
[email protected] | 39ef0a7c5 | 2014-05-11 01:40:00 | [diff] [blame] | 108 | |
[email protected] | ac02ac5 | 2014-05-20 01:11:26 | [diff] [blame] | 109 | // 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 |
| 111 | // have been permitted to run on the page via this interface. Instead, it |
| 112 | // should incorporate more fully with ActiveTab. |
| 113 | std::set<std::string> permitted_extensions_; |
[email protected] | 39ef0a7c5 | 2014-05-11 01:40:00 | [diff] [blame] | 114 | |
| 115 | // Script badges that have been generated for extensions. This is both those |
| 116 | // with actions already declared that are copied and normalised, and actions |
| 117 | // that get generated for extensions that haven't declared anything. |
| 118 | typedef std::map<std::string, linked_ptr<ExtensionAction> > ActiveScriptMap; |
| 119 | ActiveScriptMap active_script_actions_; |
| 120 | |
| 121 | DISALLOW_COPY_AND_ASSIGN(ActiveScriptController); |
| 122 | }; |
| 123 | |
| 124 | } // namespace extensions |
| 125 | |
| 126 | #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ |