Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / extensions / active_script_controller.h
blob481ce4ec6da494cc903468d4136b68ba11710413
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_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/scoped_observer.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "extensions/browser/extension_registry_observer.h"
18 #include "extensions/common/permissions/permissions_data.h"
19 #include "extensions/common/user_script.h"
21 namespace content {
22 class BrowserContext;
23 class WebContents;
26 namespace IPC {
27 class Message;
30 class ExtensionAction;
32 namespace extensions {
33 class Extension;
34 class ExtensionRegistry;
36 // The provider for ExtensionActions corresponding to scripts which are actively
37 // running or need permission.
38 // TODO(rdevlin.cronin): This isn't really a controller, but it has good parity
39 // with LocationBar"Controller".
40 class ActiveScriptController : public content::WebContentsObserver,
41 public ExtensionRegistryObserver {
42 public:
43 explicit ActiveScriptController(content::WebContents* web_contents);
44 ~ActiveScriptController() override;
46 // Returns the ActiveScriptController for the given |web_contents|, or NULL
47 // if one does not exist.
48 static ActiveScriptController* GetForWebContents(
49 content::WebContents* web_contents);
51 // Notifies the ActiveScriptController that an extension has been granted
52 // active tab permissions. This will run any pending injections for that
53 // extension.
54 void OnActiveTabPermissionGranted(const Extension* extension);
56 // Notifies the ActiveScriptController of detected ad injection.
57 void OnAdInjectionDetected(const std::set<std::string>& ad_injectors);
59 // Adds the visible origin to |extension|'s active permissions, granting
60 // |extension| permission to always run script injections on the origin.
61 void AlwaysRunOnVisibleOrigin(const Extension* extension);
63 // Notifies the ActiveScriptController that the action for |extension| has
64 // been clicked, running any pending tasks that were previously shelved.
65 void OnClicked(const Extension* extension);
67 // Returns true if the given |extension| has a pending script that wants to
68 // run.
69 bool WantsToRun(const Extension* extension);
71 int num_page_requests() const { return num_page_requests_; }
73 #if defined(UNIT_TEST)
74 // Only used in tests.
75 PermissionsData::AccessType RequiresUserConsentForScriptInjectionForTesting(
76 const Extension* extension,
77 UserScript::InjectionType type) {
78 return RequiresUserConsentForScriptInjection(extension, type);
80 void RequestScriptInjectionForTesting(const Extension* extension,
81 const base::Closure& callback) {
82 return RequestScriptInjection(extension, callback);
84 #endif // defined(UNIT_TEST)
86 private:
87 typedef std::vector<base::Closure> PendingRequestList;
88 typedef std::map<std::string, PendingRequestList> PendingRequestMap;
90 // Returns true if the extension requesting script injection requires
91 // user consent. If this is true, the caller should then register a request
92 // via RequestScriptInjection().
93 PermissionsData::AccessType RequiresUserConsentForScriptInjection(
94 const Extension* extension,
95 UserScript::InjectionType type);
97 // |callback|. The only assumption that can be made about when (or if)
98 // |callback| is run is that, if it is run, it will run on the current page.
99 void RequestScriptInjection(const Extension* extension,
100 const base::Closure& callback);
102 // Runs any pending injections for the corresponding extension.
103 void RunPendingForExtension(const Extension* extension);
105 // Handle the RequestScriptInjectionPermission message.
106 void OnRequestScriptInjectionPermission(
107 const std::string& extension_id,
108 UserScript::InjectionType script_type,
109 int64 request_id);
111 // Grants permission for the given request to run.
112 void PermitScriptInjection(int64 request_id);
114 // Notifies the ExtensionActionAPI of a change (either that an extension now
115 // wants permission to run, or that it has been run).
116 void NotifyChange(const Extension* extension);
118 // Log metrics.
119 void LogUMA() const;
121 // content::WebContentsObserver implementation.
122 bool OnMessageReceived(const IPC::Message& message,
123 content::RenderFrameHost* render_frame_host) override;
124 void DidNavigateMainFrame(
125 const content::LoadCommittedDetails& details,
126 const content::FrameNavigateParams& params) override;
128 // ExtensionRegistryObserver:
129 void OnExtensionUnloaded(content::BrowserContext* browser_context,
130 const Extension* extension,
131 UnloadedExtensionInfo::Reason reason) override;
133 // The total number of requests from the renderer on the current page,
134 // including any that are pending or were immediately granted.
135 // Right now, used only in tests.
136 int num_page_requests_;
138 // The associated browser context.
139 content::BrowserContext* browser_context_;
141 // Whether or not the feature was used for any extensions. This may not be the
142 // case if the user never enabled the scripts-require-action flag.
143 bool was_used_on_page_;
145 // The map of extension_id:pending_request of all pending requests.
146 PendingRequestMap pending_requests_;
148 // The extensions which have been granted permission to run on the given page.
149 // TODO(rdevlin.cronin): Right now, this just keeps track of extensions that
150 // have been permitted to run on the page via this interface. Instead, it
151 // should incorporate more fully with ActiveTab.
152 std::set<std::string> permitted_extensions_;
154 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
155 extension_registry_observer_;
157 DISALLOW_COPY_AND_ASSIGN(ActiveScriptController);
160 } // namespace extensions
162 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_