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 EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 5 #ifndef EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
6 #define EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 6 #define EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <vector> |
10 #include <string> | |
11 | 10 |
12 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
13 #include "base/macros.h" | 12 #include "base/macros.h" |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/memory/scoped_vector.h" | |
16 #include "base/timer/elapsed_timer.h" | 13 #include "base/timer/elapsed_timer.h" |
17 #include "extensions/common/user_script.h" | 14 #include "extensions/common/user_script.h" |
18 | 15 |
19 class GURL; | |
20 | |
21 namespace blink { | 16 namespace blink { |
22 class WebFrame; | 17 class WebFrame; |
23 } | 18 } |
24 | 19 |
25 namespace content { | 20 namespace extensions { |
26 class RenderView; | 21 class Extension; |
27 } | |
28 | 22 |
29 namespace extensions { | 23 // An abstract script wrapper which is aware of whether or not it is allowed |
30 class UserScriptSlave; | 24 // to execute, and contains the implementation to do so. |
31 | |
32 // This class is a wrapper around a UserScript that knows how to inject itself | |
33 // into a frame. | |
34 class ScriptInjection { | 25 class ScriptInjection { |
35 public: | 26 public: |
36 // Map of extensions IDs to the executing script paths. | 27 // Map of extensions IDs to the executing script paths. |
37 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap; | 28 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap; |
38 | 29 |
39 // A struct containing information about a script run. | 30 // A struct containing information about a script run. |
40 struct ScriptsRunInfo { | 31 struct ScriptsRunInfo { |
41 ScriptsRunInfo(); | 32 ScriptsRunInfo(); |
42 ~ScriptsRunInfo(); | 33 ~ScriptsRunInfo(); |
43 | 34 |
44 // The number of CSS scripts injected. | 35 // The number of CSS scripts injected. |
45 size_t num_css; | 36 size_t num_css; |
46 // The number of JS scripts injected. | 37 // The number of JS scripts injected. |
47 size_t num_js; | 38 size_t num_js; |
48 // A map of extension ids to executing script paths. | 39 // A map of extension ids to executing script paths. |
49 ExecutingScriptsMap executing_scripts; | 40 ExecutingScriptsMap executing_scripts; |
50 // The elapsed time since the ScriptsRunInfo was constructed. | 41 // The elapsed time since the ScriptsRunInfo was constructed. |
51 base::ElapsedTimer timer; | 42 base::ElapsedTimer timer; |
52 | 43 |
53 private: | 44 private: |
54 DISALLOW_COPY_AND_ASSIGN(ScriptsRunInfo); | 45 DISALLOW_COPY_AND_ASSIGN(ScriptsRunInfo); |
55 }; | 46 }; |
56 | 47 |
57 // Return the URL to use as the document url when checking permissions for | 48 ScriptInjection(blink::WebFrame* web_frame, |
58 // script injection. | 49 const std::string& extension_id, |
59 static GURL GetDocumentUrlForFrame(blink::WebFrame* frame); | 50 UserScript::RunLocation run_location, |
| 51 int tab_id); |
| 52 virtual ~ScriptInjection(); |
60 | 53 |
61 ScriptInjection(scoped_ptr<UserScript> script, | 54 // Gets the isolated world ID to use for the given |extension| in the given |
62 UserScriptSlave* user_script_slave); | 55 // |frame|. If no isolated world has been created for that extension, |
63 ~ScriptInjection(); | 56 // one will be created and initialized. |
| 57 static int GetIsolatedWorldIdForExtension(const Extension* extension, |
| 58 blink::WebFrame* web_frame); |
64 | 59 |
65 // Inject the script into the given |frame| if the script should run on the | 60 // Return the id of the extension associated with the given world. |
66 // frame and has permission to do so. If the script requires user consent, | 61 static std::string GetExtensionIdForIsolatedWorld(int world_id); |
67 // this will register a pending request to inject at a later time. | |
68 // If the script is run immediately, |scripts_run_info| is updated with | |
69 // information about the run. | |
70 void InjectIfAllowed(blink::WebFrame* frame, | |
71 UserScript::RunLocation location, | |
72 const GURL& document_url, | |
73 ScriptsRunInfo* scripts_run_info); | |
74 | 62 |
75 // If a request with the given |request_id| exists, runs that request and | 63 // Remove the isolated world associated with the given extension. |
76 // modifies |scripts_run_info| with information about the run. Otherwise, does | 64 static void RemoveIsolatedWorld(const std::string& extension_id); |
77 // nothing. | |
78 // If |frame_out| is non-NULL and a script was run, |frame_out| will be | |
79 // populated with the frame in which the script was run. | |
80 // Returns true if the request was found *and* the script was run. | |
81 bool NotifyScriptPermitted(int64 request_id, | |
82 content::RenderView* render_view, | |
83 ScriptsRunInfo* scripts_run_info, | |
84 blink::WebFrame** frame_out); | |
85 | 65 |
86 // Notififies the Injection that the frame has been detached (i.e. is about | 66 // Called if the extension cannot be found, and the script will never execute. |
87 // to be destroyed). | 67 virtual void ExtensionNotFound() {} |
88 void FrameDetached(blink::WebFrame* frame); | |
89 | 68 |
90 void SetScript(scoped_ptr<UserScript> script); | 69 // Returns true if the script is allowed to inject. |
| 70 virtual bool Allowed(const Extension* extension) = 0; |
91 | 71 |
92 const std::string& extension_id() { return extension_id_; } | 72 // Injects the script into the associated frame. |
93 const UserScript* script() { return script_.get(); } | 73 virtual void Inject(ScriptsRunInfo* scripts_run_info, |
| 74 const Extension* extension) = 0; |
| 75 |
| 76 const blink::WebFrame* web_frame() const { return web_frame_; } |
| 77 blink::WebFrame* web_frame() { return web_frame_; } |
| 78 UserScript::RunLocation run_location() const { return run_location_; } |
| 79 const std::string& extension_id() const { return extension_id_; } |
| 80 int tab_id() const { return tab_id_; } |
| 81 int64 request_id() const { return request_id_; } |
| 82 void set_request_id(int64 request_id) { request_id_ = request_id; } |
94 | 83 |
95 private: | 84 private: |
96 struct PendingInjection; | 85 blink::WebFrame* web_frame_; |
97 | |
98 // Returns true if this ScriptInjection wants to run on the given |frame| at | |
99 // the given |run_location| (i.e., if this script would inject either JS or | |
100 // CSS). | |
101 bool WantsToRun(blink::WebFrame* frame, | |
102 UserScript::RunLocation run_location, | |
103 const GURL& document_url) const; | |
104 | |
105 // Returns true if the script will inject [css|js] at the given | |
106 // |run_location|. | |
107 bool ShouldInjectJS(UserScript::RunLocation run_location) const; | |
108 bool ShouldInjectCSS(UserScript::RunLocation run_location) const; | |
109 | |
110 // Injects the script into the given |frame|, and updates |scripts_run_info| | |
111 // information about the run. | |
112 void Inject(blink::WebFrame* frame, | |
113 UserScript::RunLocation run_location, | |
114 ScriptsRunInfo* scripts_run_info) const; | |
115 | |
116 // Injects the [css|js] scripts into the frame, and stores the results of | |
117 // the run in |scripts_run_info|. | |
118 void InjectJS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) const; | |
119 void InjectCSS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) | |
120 const; | |
121 | |
122 // The UserScript this is injecting. | |
123 scoped_ptr<UserScript> script_; | |
124 | |
125 // The associated extension's id. | |
126 std::string extension_id_; | 86 std::string extension_id_; |
127 | 87 UserScript::RunLocation run_location_; |
128 // The associated UserScriptSlave. | 88 int tab_id_; |
129 // It's unfortunate that this is needed, but we use it to get the isolated | 89 int64 request_id_; |
130 // world ids and the associated extensions. | |
131 // TODO(rdevlin.cronin): It would be nice to clean this up more. | |
132 UserScriptSlave* user_script_slave_; | |
133 | |
134 // True if the script is a standalone script or emulates greasemonkey. | |
135 bool is_standalone_or_emulate_greasemonkey_; | |
136 | |
137 ScopedVector<PendingInjection> pending_injections_; | |
138 | |
139 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); | |
140 }; | 90 }; |
141 | 91 |
142 } // namespace extensions | 92 } // namespace extensions |
143 | 93 |
144 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 94 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
OLD | NEW |