Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/browser/extensions/active_script_controller.h

Issue 286003004: Block tabs.executeScript() from executing until user grants permission (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
14 #include "chrome/browser/extensions/location_bar_controller.h" 16 #include "chrome/browser/extensions/location_bar_controller.h"
15 #include "content/public/browser/web_contents_observer.h" 17 #include "content/public/browser/web_contents_observer.h"
16 18
17 namespace content { 19 namespace content {
18 class WebContents; 20 class WebContents;
19 } 21 }
20 22
21 namespace IPC { 23 namespace IPC {
(...skipping 13 matching lines...) Expand all
35 public content::WebContentsObserver { 37 public content::WebContentsObserver {
36 public: 38 public:
37 explicit ActiveScriptController(content::WebContents* web_contents); 39 explicit ActiveScriptController(content::WebContents* web_contents);
38 virtual ~ActiveScriptController(); 40 virtual ~ActiveScriptController();
39 41
40 // Returns the ActiveScriptController for the given |web_contents|, or NULL 42 // Returns the ActiveScriptController for the given |web_contents|, or NULL
41 // if one does not exist. 43 // if one does not exist.
42 static ActiveScriptController* GetForWebContents( 44 static ActiveScriptController* GetForWebContents(
43 content::WebContents* web_contents); 45 content::WebContents* web_contents);
44 46
45 // Notify the ActiveScriptController that an extension is running a script. 47 // Injects the script (by running |callback|) if the extension has permission
46 // TODO(rdevlin.cronin): Soon, this should be ask the user for permission, 48 // to do. This can be determined by the extension's explicit permissions, or
47 // rather than simply notifying them. 49 // by asking the user for permission to run. If the extension does not have
48 void NotifyScriptExecuting(const std::string& extension_id, int page_id); 50 // permission and it is never given, |callback| is never run (and will
51 // destruct).
52 // NOTE: Callers *cannot* make any assumptions as to when (or if) |callback|
53 // will be run. It may be run immediately (if the extension already has the
54 // required permissions), after a period of time (if the user consents), or
55 // never.
56 void InjectScriptIfHasPermission(const std::string& extension_id,
57 int page_id,
58 const base::Closure& callback);
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<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 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 been granted permission to run on the given page.
80 std::set<std::string> ad_injectors_; 103 std::set<std::string> permitted_extensions_;
81 104
82 // Script badges that have been generated for extensions. This is both those 105 // Script badges that have been generated for extensions. This is both those
83 // with actions already declared that are copied and normalised, and actions 106 // with actions already declared that are copied and normalised, and actions
84 // that get generated for extensions that haven't declared anything. 107 // that get generated for extensions that haven't declared anything.
85 typedef std::map<std::string, linked_ptr<ExtensionAction> > ActiveScriptMap; 108 typedef std::map<std::string, linked_ptr<ExtensionAction> > ActiveScriptMap;
86 ActiveScriptMap active_script_actions_; 109 ActiveScriptMap active_script_actions_;
87 110
88 DISALLOW_COPY_AND_ASSIGN(ActiveScriptController); 111 DISALLOW_COPY_AND_ASSIGN(ActiveScriptController);
89 }; 112 };
90 113
91 } // namespace extensions 114 } // namespace extensions
92 115
93 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_ 116 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVE_SCRIPT_CONTROLLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698