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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_
6 #define CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_
13 #include "base/callback.h"
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"
27 class ExtensionAction
;
29 namespace extensions
{
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
{
39 explicit ActiveScriptController(content::WebContents
* web_contents
);
40 virtual ~ActiveScriptController();
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
);
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
);
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
,
57 const base::Closure
& callback
);
59 // Notifies the ActiveScriptController that an extension has been granted
60 // active tab permissions. This will run any pending injections for that
62 void OnActiveTabPermissionGranted(const Extension
* extension
);
64 // Notifies the ActiveScriptController of detected ad injection.
65 void OnAdInjectionDetected(const std::set
<std::string
> ad_injectors
);
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
;
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
);
82 base::Closure closure
;
85 typedef std::vector
<PendingRequest
> PendingRequestList
;
86 typedef std::map
<std::string
, PendingRequestList
> PendingRequestMap
;
88 // Runs any pending injections for the corresponding extension.
89 void RunPendingForExtension(const Extension
* extension
);
91 // Handles the NotifyExtensionScriptExecution message.
92 void OnNotifyExtensionScriptExecution(const std::string
& extension_id
,
95 // content::WebContentsObserver implementation.
96 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
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.
106 // The map of extension_id:pending_request of all pending requests.
107 PendingRequestMap pending_requests_
;
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_
;
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_
;
121 DISALLOW_COPY_AND_ASSIGN(ActiveScriptController
);
124 } // namespace extensions
126 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_