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 | 12 |
13 #include "base/callback.h" | |
12 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
13 #include "base/memory/linked_ptr.h" | 15 #include "base/memory/linked_ptr.h" |
16 #include "base/memory/scoped_ptr.h" | |
14 #include "chrome/browser/extensions/location_bar_controller.h" | 17 #include "chrome/browser/extensions/location_bar_controller.h" |
15 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
16 | 19 |
17 namespace content { | 20 namespace content { |
18 class WebContents; | 21 class WebContents; |
19 } | 22 } |
20 | 23 |
21 namespace IPC { | 24 namespace IPC { |
22 class Message; | 25 class Message; |
23 } | 26 } |
24 | 27 |
25 class ExtensionAction; | 28 class ExtensionAction; |
26 | 29 |
27 namespace extensions { | 30 namespace extensions { |
28 class Extension; | 31 class Extension; |
32 class ExtensionRegistry; | |
29 | 33 |
30 // The provider for ExtensionActions corresponding to scripts which are actively | 34 // The provider for ExtensionActions corresponding to scripts which are actively |
31 // running or need permission. | 35 // running or need permission. |
32 // 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 |
33 // with PageAction"Controller". | 37 // with PageAction"Controller". |
34 class ActiveScriptController : public LocationBarController::ActionProvider, | 38 class ActiveScriptController : public LocationBarController::ActionProvider, |
35 public content::WebContentsObserver { | 39 public content::WebContentsObserver { |
36 public: | 40 public: |
37 explicit ActiveScriptController(content::WebContents* web_contents); | 41 explicit ActiveScriptController(content::WebContents* web_contents); |
38 virtual ~ActiveScriptController(); | 42 virtual ~ActiveScriptController(); |
39 | 43 |
40 // Returns the ActiveScriptController for the given |web_contents|, or NULL | 44 // Returns the ActiveScriptController for the given |web_contents|, or NULL |
41 // if one does not exist. | 45 // if one does not exist. |
42 static ActiveScriptController* GetForWebContents( | 46 static ActiveScriptController* GetForWebContents( |
43 content::WebContents* web_contents); | 47 content::WebContents* web_contents); |
44 | 48 |
45 // Notify the ActiveScriptController that an extension is running a script. | 49 // Checks whether permission is needed for the script to run. If no permission |
46 // TODO(rdevlin.cronin): Soon, this should be ask the user for permission, | 50 // is needed, runs the |callback| immediately. If permission is needed, asks |
47 // rather than simply notifying them. | 51 // the user for permission, and will run the script upon permission being |
48 void NotifyScriptExecuting(const std::string& extension_id, int page_id); | 52 // granted. |
not at google - send to devlin
2014/05/15 00:12:36
what if permission wasn't granted?
Devlin
2014/05/15 17:45:59
Done.
| |
53 // Even though base::Closure can be safely passed, we use a scoped_ptr here | |
54 // because there can be a lot of bound parameters, and we'd prefer to avoid | |
not at google - send to devlin
2014/05/15 00:12:36
"a lot of bound parameters" is an implementation o
Devlin
2014/05/15 17:45:59
Okay.
| |
55 // copying. | |
56 void GetPermissionForInjection(const std::string& extension_id, | |
57 int page_id, | |
58 scoped_ptr<const base::Closure> callback); | |
not at google - send to devlin
2014/05/15 00:12:36
pet peeve: re-entrant callbacks. Even worse, callb
Devlin
2014/05/15 17:45:59
Per offline discussion, neither of these are great
not at google - send to devlin
2014/05/15 18:19:56
All I want is for the callback to never be run re-
| |
49 | 59 |
50 // Notifies the ActiveScriptController of detected ad injection. | 60 // Notifies the ActiveScriptController of detected ad injection. |
51 void OnAdInjectionDetected(const std::vector<std::string> ad_injectors); | 61 void OnAdInjectionDetected(const std::vector<std::string> ad_injectors); |
52 | 62 |
53 // LocationBarControllerProvider implementation. | 63 // LocationBarControllerProvider implementation. |
54 virtual ExtensionAction* GetActionForExtension( | 64 virtual ExtensionAction* GetActionForExtension( |
55 const Extension* extension) OVERRIDE; | 65 const Extension* extension) OVERRIDE; |
56 virtual LocationBarController::Action OnClicked( | 66 virtual LocationBarController::Action OnClicked( |
57 const Extension* extension) OVERRIDE; | 67 const Extension* extension) OVERRIDE; |
58 virtual void OnNavigated() OVERRIDE; | 68 virtual void OnNavigated() OVERRIDE; |
59 | 69 |
60 private: | 70 private: |
71 typedef std::vector<linked_ptr<const base::Closure> > PendingRequestList; | |
72 typedef std::map<std::string, PendingRequestList> PendingRequestMap; | |
73 | |
74 // Handles the NotifyExtensionScriptExecution message. | |
75 void OnNotifyExtensionScriptExecution(const std::string& extension_id, | |
76 int page_id); | |
77 | |
78 // Returns true if the ActiveScriptController should process the request. This | |
79 // can be false if, e.g., the page id does not match, the extension cannot | |
80 // be found, etc. | |
81 bool ShouldProcessRequest(const Extension* extension, int page_id); | |
82 | |
83 // Adds the |request| to the map of pending requests, or processes it | |
84 // immediately if no permission is needed. | |
85 void AddOrProcessRequest(const Extension* extension, | |
86 scoped_ptr<const base::Closure> request); | |
87 | |
61 // content::WebContentsObserver implementation. | 88 // content::WebContentsObserver implementation. |
62 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 89 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
63 | 90 |
64 // Handle the NotifyExtensionScriptExecution message. | |
65 void OnNotifyExtensionScriptExecution(const std::string& extension_id, | |
66 int page_id); | |
67 | |
68 // Log metrics. | 91 // Log metrics. |
69 void LogUMA() const; | 92 void LogUMA() const; |
70 | 93 |
71 // Whether or not the ActiveScriptController is enabled (corresponding to the | 94 // Whether or not the ActiveScriptController is enabled (corresponding to the |
72 // kActiveScriptEnforcement switch). If it is not, it acts as an empty shell, | 95 // kActiveScriptEnforcement switch). If it is not, it acts as an empty shell, |
73 // always allowing scripts to run and never displaying actions. | 96 // always allowing scripts to run and never displaying actions. |
74 bool enabled_; | 97 bool enabled_; |
75 | 98 |
76 // The extensions that have called ExecuteScript on the current frame. | 99 // The map of extension_id:pending_request of all pending requests. |
77 std::set<std::string> extensions_executing_scripts_; | 100 PendingRequestMap pending_requests_; |
78 | 101 |
79 // The extensions which have injected ads. | 102 // The extensions which have requested permission at some point in this page. |
80 std::set<std::string> ad_injectors_; | 103 // Used for metrics. Separate from |pending_requests_|, as pending requests |
104 // are erased when the request is fulfilled. | |
105 std::set<std::string> requesting_extensions_; | |
106 | |
107 // The associated ExtensionRegistry, for convenience. | |
108 ExtensionRegistry* extension_registry_; | |
81 | 109 |
82 // Script badges that have been generated for extensions. This is both those | 110 // Script badges that have been generated for extensions. This is both those |
83 // with actions already declared that are copied and normalised, and actions | 111 // with actions already declared that are copied and normalised, and actions |
84 // that get generated for extensions that haven't declared anything. | 112 // that get generated for extensions that haven't declared anything. |
85 typedef std::map<std::string, linked_ptr<ExtensionAction> > ActiveScriptMap; | 113 typedef std::map<std::string, linked_ptr<ExtensionAction> > ActiveScriptMap; |
86 ActiveScriptMap active_script_actions_; | 114 ActiveScriptMap active_script_actions_; |
87 | 115 |
88 DISALLOW_COPY_AND_ASSIGN(ActiveScriptController); | 116 DISALLOW_COPY_AND_ASSIGN(ActiveScriptController); |
89 }; | 117 }; |
90 | 118 |
91 } // namespace extensions | 119 } // namespace extensions |
92 | 120 |
93 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ | 121 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ |
OLD | NEW |