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 #include "extensions/renderer/script_injection.h" | 5 #include "extensions/renderer/script_injection.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/timer/elapsed_timer.h" | 11 #include "base/timer/elapsed_timer.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "content/public/renderer/render_view.h" | 13 #include "content/public/renderer/render_view.h" |
14 #include "content/public/renderer/v8_value_converter.h" | 14 #include "content/public/renderer/v8_value_converter.h" |
15 #include "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
16 #include "extensions/common/extension_messages.h" | 16 #include "extensions/common/extension_messages.h" |
17 #include "extensions/common/feature_switch.h" | 17 #include "extensions/common/feature_switch.h" |
18 #include "extensions/common/manifest_handlers/csp_info.h" | 18 #include "extensions/common/manifest_handlers/csp_info.h" |
19 #include "extensions/renderer/dom_activity_logger.h" | 19 #include "extensions/renderer/dom_activity_logger.h" |
20 #include "extensions/renderer/extension_consumer.h" | |
20 #include "extensions/renderer/extension_groups.h" | 21 #include "extensions/renderer/extension_groups.h" |
21 #include "extensions/renderer/extensions_renderer_client.h" | 22 #include "extensions/renderer/extensions_renderer_client.h" |
22 #include "third_party/WebKit/public/platform/WebString.h" | 23 #include "third_party/WebKit/public/platform/WebString.h" |
23 #include "third_party/WebKit/public/web/WebDocument.h" | 24 #include "third_party/WebKit/public/web/WebDocument.h" |
24 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 25 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
25 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 26 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
26 #include "third_party/WebKit/public/web/WebScriptSource.h" | 27 #include "third_party/WebKit/public/web/WebScriptSource.h" |
27 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 28 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
28 #include "url/gurl.h" | 29 #include "url/gurl.h" |
29 | 30 |
30 namespace extensions { | 31 namespace extensions { |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 typedef std::map<std::string, int> IsolatedWorldMap; | 35 using IsolatedWorldKey = std::pair<HostID, int>; |
36 using IsolatedWorldMap = std::map<IsolatedWorldKey, int>; | |
35 base::LazyInstance<IsolatedWorldMap> g_isolated_worlds = | 37 base::LazyInstance<IsolatedWorldMap> g_isolated_worlds = |
36 LAZY_INSTANCE_INITIALIZER; | 38 LAZY_INSTANCE_INITIALIZER; |
37 | 39 |
38 const int64 kInvalidRequestId = -1; | 40 const int64 kInvalidRequestId = -1; |
39 | 41 |
40 // The id of the next pending injection. | 42 // The id of the next pending injection. |
41 int64 g_next_pending_id = 0; | 43 int64 g_next_pending_id = 0; |
42 | 44 |
43 bool ShouldNotifyBrowserOfInjections() { | 45 bool ShouldNotifyBrowserOfInjections() { |
44 return !FeatureSwitch::scripts_require_action()->IsEnabled(); | 46 return !FeatureSwitch::scripts_require_action()->IsEnabled(); |
45 } | 47 } |
46 | 48 |
47 // Append all the child frames of |parent_frame| to |frames_vector|. | 49 // Append all the child frames of |parent_frame| to |frames_vector|. |
48 void AppendAllChildFrames(blink::WebFrame* parent_frame, | 50 void AppendAllChildFrames(blink::WebFrame* parent_frame, |
49 std::vector<blink::WebFrame*>* frames_vector) { | 51 std::vector<blink::WebFrame*>* frames_vector) { |
50 DCHECK(parent_frame); | 52 DCHECK(parent_frame); |
51 for (blink::WebFrame* child_frame = parent_frame->firstChild(); child_frame; | 53 for (blink::WebFrame* child_frame = parent_frame->firstChild(); child_frame; |
52 child_frame = child_frame->nextSibling()) { | 54 child_frame = child_frame->nextSibling()) { |
53 frames_vector->push_back(child_frame); | 55 frames_vector->push_back(child_frame); |
54 AppendAllChildFrames(child_frame, frames_vector); | 56 AppendAllChildFrames(child_frame, frames_vector); |
55 } | 57 } |
56 } | 58 } |
57 | 59 |
58 // Gets the isolated world ID to use for the given |extension| in the given | 60 // Gets the isolated world ID to use for the given |host, instance_id| |
59 // |frame|. If no isolated world has been created for that extension, | 61 // in the given |frame|. If no isolated world has been created for that |
60 // one will be created and initialized. | 62 // |host, instance_id| one will be created and initialized. |
61 int GetIsolatedWorldIdForExtension(const Extension* extension, | 63 int GetIsolatedWorldIdForInstance(const Host* host, |
62 blink::WebLocalFrame* frame) { | 64 int instance_id, |
65 blink::WebLocalFrame* frame) { | |
63 static int g_next_isolated_world_id = | 66 static int g_next_isolated_world_id = |
64 ExtensionsRendererClient::Get()->GetLowestIsolatedWorldId(); | 67 ExtensionsRendererClient::Get()->GetLowestIsolatedWorldId(); |
65 | 68 |
66 IsolatedWorldMap& isolated_worlds = g_isolated_worlds.Get(); | 69 IsolatedWorldMap& isolated_worlds = g_isolated_worlds.Get(); |
67 | 70 |
68 int id = 0; | 71 int id = 0; |
69 IsolatedWorldMap::iterator iter = isolated_worlds.find(extension->id()); | 72 const HostID& host_id = host->id(); |
Devlin
2015/02/09 17:40:25
This cached variable is kinda silly. Just call ho
Xi Han
2015/02/09 23:28:11
Done.
| |
73 IsolatedWorldKey key(host_id, instance_id); | |
74 IsolatedWorldMap::iterator iter = isolated_worlds.find(key); | |
70 if (iter != isolated_worlds.end()) { | 75 if (iter != isolated_worlds.end()) { |
71 id = iter->second; | 76 id = iter->second; |
72 } else { | 77 } else { |
73 id = g_next_isolated_world_id++; | 78 id = g_next_isolated_world_id++; |
74 // This map will tend to pile up over time, but realistically, you're never | 79 // This map will tend to pile up over time, but realistically, you're never |
75 // going to have enough extensions for it to matter. | 80 // going to have enough hosts for it to matter. |
76 isolated_worlds[extension->id()] = id; | 81 isolated_worlds[key] = id; |
77 } | 82 } |
78 | 83 |
84 std::string name = host->name(); | |
85 const GURL& origin = host->url(); | |
Devlin
2015/02/09 17:40:25
Same here - just use host->url() on line 91 and ho
Xi Han
2015/02/09 23:28:11
Legacy code, forgot to clean up:(
| |
86 | |
79 // We need to set the isolated world origin and CSP even if it's not a new | 87 // We need to set the isolated world origin and CSP even if it's not a new |
80 // world since these are stored per frame, and we might not have used this | 88 // world since these are stored per frame, and we might not have used this |
81 // isolated world in this frame before. | 89 // isolated world in this frame before. |
82 frame->setIsolatedWorldSecurityOrigin( | 90 frame->setIsolatedWorldSecurityOrigin( |
83 id, blink::WebSecurityOrigin::create(extension->url())); | 91 id, blink::WebSecurityOrigin::create(origin)); |
84 frame->setIsolatedWorldContentSecurityPolicy( | 92 frame->setIsolatedWorldContentSecurityPolicy( |
85 id, | 93 id, blink::WebString::fromUTF8(host->GetContentSecurityPolicy())); |
86 blink::WebString::fromUTF8(CSPInfo::GetContentSecurityPolicy(extension))); | |
87 frame->setIsolatedWorldHumanReadableName( | 94 frame->setIsolatedWorldHumanReadableName( |
88 id, | 95 id, blink::WebString::fromUTF8(name)); |
89 blink::WebString::fromUTF8(extension->name())); | |
90 | 96 |
91 return id; | 97 return id; |
92 } | 98 } |
93 | 99 |
94 } // namespace | 100 } // namespace |
95 | 101 |
96 // static | 102 // static |
97 std::string ScriptInjection::GetExtensionIdForIsolatedWorld( | 103 HostID ScriptInjection::GetHostIdForIsolatedWorld(int isolated_world_id) { |
98 int isolated_world_id) { | 104 const IsolatedWorldMap& isolated_worlds = g_isolated_worlds.Get(); |
99 IsolatedWorldMap& isolated_worlds = g_isolated_worlds.Get(); | |
100 | 105 |
101 for (IsolatedWorldMap::iterator iter = isolated_worlds.begin(); | 106 for (const auto& kv : isolated_worlds) { |
102 iter != isolated_worlds.end(); | 107 if (kv.second == isolated_world_id) |
103 ++iter) { | 108 return kv.first.first; |
104 if (iter->second == isolated_world_id) | |
105 return iter->first; | |
106 } | 109 } |
107 return std::string(); | 110 return HostID(); |
108 } | 111 } |
109 | 112 |
110 // static | 113 // static |
111 void ScriptInjection::RemoveIsolatedWorld(const std::string& extension_id) { | 114 void ScriptInjection::RemoveIsolatedWorld(const HostID& host_id) { |
112 g_isolated_worlds.Get().erase(extension_id); | 115 std::set<IsolatedWorldKey> keys_to_delete; |
116 IsolatedWorldMap& isolated_worlds = g_isolated_worlds.Get(); | |
117 for (auto& kv: isolated_worlds) { | |
Devlin
2015/02/09 17:40:25
const auto&
Xi Han
2015/02/09 23:28:11
Done.
| |
118 const IsolatedWorldKey& key = kv.first; | |
119 if (key.first == host_id) | |
120 keys_to_delete.insert(key); | |
121 } | |
122 for (auto& key : keys_to_delete) | |
123 isolated_worlds.erase(key); | |
113 } | 124 } |
114 | 125 |
115 ScriptInjection::ScriptInjection( | 126 ScriptInjection::ScriptInjection( |
116 scoped_ptr<ScriptInjector> injector, | 127 scoped_ptr<ScriptInjector> injector, |
117 blink::WebLocalFrame* web_frame, | 128 blink::WebLocalFrame* web_frame, |
118 const std::string& extension_id, | 129 const HostID& host_id, |
130 int instance_id, | |
119 UserScript::RunLocation run_location, | 131 UserScript::RunLocation run_location, |
120 int tab_id) | 132 int tab_id) |
121 : injector_(injector.Pass()), | 133 : injector_(injector.Pass()), |
122 web_frame_(web_frame), | 134 web_frame_(web_frame), |
123 extension_id_(extension_id), | 135 host_id_(host_id), |
136 instance_id_(instance_id), | |
124 run_location_(run_location), | 137 run_location_(run_location), |
125 tab_id_(tab_id), | 138 tab_id_(tab_id), |
126 request_id_(kInvalidRequestId), | 139 request_id_(kInvalidRequestId), |
127 complete_(false) { | 140 complete_(false) { |
128 } | 141 } |
129 | 142 |
130 ScriptInjection::~ScriptInjection() { | 143 ScriptInjection::~ScriptInjection() { |
131 if (!complete_) | 144 if (!complete_) |
132 injector_->OnWillNotInject(ScriptInjector::WONT_INJECT); | 145 injector_->OnWillNotInject(ScriptInjector::WONT_INJECT); |
133 } | 146 } |
134 | 147 |
135 bool ScriptInjection::TryToInject(UserScript::RunLocation current_location, | 148 bool ScriptInjection::TryToInject(UserScript::RunLocation current_location, |
136 const Extension* extension, | 149 const Host* host, |
137 ScriptsRunInfo* scripts_run_info) { | 150 ScriptsRunInfo* scripts_run_info) { |
138 if (current_location < run_location_) | 151 if (current_location < run_location_) |
139 return false; // Wait for the right location. | 152 return false; // Wait for the right location. |
140 | 153 |
141 if (request_id_ != kInvalidRequestId) | 154 if (request_id_ != kInvalidRequestId) |
142 return false; // We're waiting for permission right now, try again later. | 155 return false; // We're waiting for permission right now, try again later. |
143 | 156 |
144 if (!extension) { | 157 if (!host) { |
145 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); | 158 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
146 return true; // We're done. | 159 return true; // We're done. |
147 } | 160 } |
148 | 161 |
149 switch (injector_->CanExecuteOnFrame( | 162 switch (injector_->CanExecuteOnFrame( |
150 extension, web_frame_, tab_id_, web_frame_->top()->document().url())) { | 163 host, web_frame_, tab_id_, web_frame_->top()->document().url())) { |
151 case PermissionsData::ACCESS_DENIED: | 164 case PermissionsData::ACCESS_DENIED: |
152 NotifyWillNotInject(ScriptInjector::NOT_ALLOWED); | 165 NotifyWillNotInject(ScriptInjector::NOT_ALLOWED); |
153 return true; // We're done. | 166 return true; // We're done. |
154 case PermissionsData::ACCESS_WITHHELD: | 167 case PermissionsData::ACCESS_WITHHELD: |
155 RequestPermission(); | 168 RequestPermission(); |
156 return false; // Wait around for permission. | 169 return false; // Wait around for permission. |
157 case PermissionsData::ACCESS_ALLOWED: | 170 case PermissionsData::ACCESS_ALLOWED: |
158 Inject(extension, scripts_run_info); | 171 Inject(host, scripts_run_info); |
159 return true; // We're done! | 172 return true; // We're done! |
160 } | 173 } |
161 | 174 |
162 // Some compilers don't realize that we always return from the switch() above. | 175 // Some compilers don't realize that we always return from the switch() above. |
163 // Make them happy. | 176 // Make them happy. |
164 return false; | 177 return false; |
165 } | 178 } |
166 | 179 |
167 bool ScriptInjection::OnPermissionGranted(const Extension* extension, | 180 bool ScriptInjection::OnPermissionGranted(const Host* host, |
168 ScriptsRunInfo* scripts_run_info) { | 181 ScriptsRunInfo* scripts_run_info) { |
169 if (!extension) { | 182 if (!host) { |
170 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); | 183 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); |
171 return false; | 184 return false; |
172 } | 185 } |
173 | 186 |
174 Inject(extension, scripts_run_info); | 187 Inject(host, scripts_run_info); |
175 return true; | 188 return true; |
176 } | 189 } |
177 | 190 |
178 void ScriptInjection::RequestPermission() { | 191 void ScriptInjection::RequestPermission() { |
179 content::RenderView* render_view = | 192 content::RenderView* render_view = |
180 content::RenderView::FromWebView(web_frame()->top()->view()); | 193 content::RenderView::FromWebView(web_frame()->top()->view()); |
181 | 194 |
182 // If we are just notifying the browser of the injection, then send an | 195 // If we are just notifying the browser of the injection, then send an |
183 // invalid request (which is treated like a notification). | 196 // invalid request (which is treated like a notification). |
184 request_id_ = ShouldNotifyBrowserOfInjections() ? kInvalidRequestId | 197 request_id_ = ShouldNotifyBrowserOfInjections() ? kInvalidRequestId |
185 : g_next_pending_id++; | 198 : g_next_pending_id++; |
186 render_view->Send(new ExtensionHostMsg_RequestScriptInjectionPermission( | 199 render_view->Send(new ExtensionHostMsg_RequestScriptInjectionPermission( |
187 render_view->GetRoutingID(), | 200 render_view->GetRoutingID(), |
188 extension_id_, | 201 host_id_.id(), |
189 injector_->script_type(), | 202 injector_->script_type(), |
190 request_id_)); | 203 request_id_)); |
191 } | 204 } |
192 | 205 |
193 void ScriptInjection::NotifyWillNotInject( | 206 void ScriptInjection::NotifyWillNotInject( |
194 ScriptInjector::InjectFailureReason reason) { | 207 ScriptInjector::InjectFailureReason reason) { |
195 complete_ = true; | 208 complete_ = true; |
196 injector_->OnWillNotInject(reason); | 209 injector_->OnWillNotInject(reason); |
197 } | 210 } |
198 | 211 |
199 void ScriptInjection::Inject(const Extension* extension, | 212 void ScriptInjection::Inject(const Host* host, |
200 ScriptsRunInfo* scripts_run_info) { | 213 ScriptsRunInfo* scripts_run_info) { |
201 DCHECK(extension); | 214 DCHECK(host); |
202 DCHECK(scripts_run_info); | 215 DCHECK(scripts_run_info); |
203 DCHECK(!complete_); | 216 DCHECK(!complete_); |
204 | 217 |
205 if (ShouldNotifyBrowserOfInjections()) | 218 if (ShouldNotifyBrowserOfInjections()) |
206 RequestPermission(); | 219 RequestPermission(); |
207 | 220 |
208 std::vector<blink::WebFrame*> frame_vector; | 221 std::vector<blink::WebFrame*> frame_vector; |
209 frame_vector.push_back(web_frame_); | 222 frame_vector.push_back(web_frame_); |
210 if (injector_->ShouldExecuteInChildFrames()) | 223 if (injector_->ShouldExecuteInChildFrames()) |
211 AppendAllChildFrames(web_frame_, &frame_vector); | 224 AppendAllChildFrames(web_frame_, &frame_vector); |
(...skipping 10 matching lines...) Expand all Loading... | |
222 GURL top_url = web_frame_->top()->document().url(); | 235 GURL top_url = web_frame_->top()->document().url(); |
223 for (std::vector<blink::WebFrame*>::iterator iter = frame_vector.begin(); | 236 for (std::vector<blink::WebFrame*>::iterator iter = frame_vector.begin(); |
224 iter != frame_vector.end(); | 237 iter != frame_vector.end(); |
225 ++iter) { | 238 ++iter) { |
226 // TODO(dcheng): Unfortunately, the code as written won't work in an OOPI | 239 // TODO(dcheng): Unfortunately, the code as written won't work in an OOPI |
227 // world. This is just a temporary hack to make things compile. | 240 // world. This is just a temporary hack to make things compile. |
228 blink::WebLocalFrame* frame = (*iter)->toWebLocalFrame(); | 241 blink::WebLocalFrame* frame = (*iter)->toWebLocalFrame(); |
229 | 242 |
230 // We recheck access here in the renderer for extra safety against races | 243 // We recheck access here in the renderer for extra safety against races |
231 // with navigation, but different frames can have different URLs, and the | 244 // with navigation, but different frames can have different URLs, and the |
232 // extension might only have access to a subset of them. | 245 // host might only have access to a subset of them. |
233 // For child frames, we just skip ones the extension doesn't have access | 246 // For child frames, we just skip ones the host doesn't have access |
234 // to and carry on. | 247 // to and carry on. |
235 // Note: we don't consider ACCESS_WITHHELD because there is nowhere to | 248 // Note: we don't consider ACCESS_WITHHELD because there is nowhere to |
236 // surface a request for a child frame. | 249 // surface a request for a child frame. |
237 // TODO(rdevlin.cronin): We should ask for permission somehow. | 250 // TODO(rdevlin.cronin): We should ask for permission somehow. |
238 if (injector_->CanExecuteOnFrame(extension, frame, tab_id_, top_url) == | 251 if (injector_->CanExecuteOnFrame(host, frame, tab_id_, top_url) == |
239 PermissionsData::ACCESS_DENIED) { | 252 PermissionsData::ACCESS_DENIED) { |
240 DCHECK(frame->parent()); | 253 DCHECK(frame->parent()); |
241 continue; | 254 continue; |
242 } | 255 } |
243 if (inject_js) | 256 if (inject_js) |
244 InjectJs(extension, frame, execution_results.get()); | 257 InjectJs(host, frame, execution_results.get()); |
245 if (inject_css) | 258 if (inject_css) |
246 InjectCss(frame); | 259 InjectCss(frame); |
247 } | 260 } |
248 | 261 |
249 complete_ = true; | 262 complete_ = true; |
250 injector_->OnInjectionComplete(execution_results.Pass(), | 263 injector_->OnInjectionComplete(execution_results.Pass(), |
251 scripts_run_info, | 264 scripts_run_info, |
252 run_location_); | 265 run_location_); |
253 } | 266 } |
254 | 267 |
255 void ScriptInjection::InjectJs(const Extension* extension, | 268 void ScriptInjection::InjectJs(const Host* host, |
256 blink::WebLocalFrame* frame, | 269 blink::WebLocalFrame* frame, |
257 base::ListValue* execution_results) { | 270 base::ListValue* execution_results) { |
258 std::vector<blink::WebScriptSource> sources = | 271 std::vector<blink::WebScriptSource> sources = |
259 injector_->GetJsSources(run_location_); | 272 injector_->GetJsSources(run_location_); |
260 bool in_main_world = injector_->ShouldExecuteInMainWorld(); | 273 bool in_main_world = injector_->ShouldExecuteInMainWorld(); |
261 int world_id = in_main_world | 274 int world_id = in_main_world |
262 ? DOMActivityLogger::kMainWorldId | 275 ? DOMActivityLogger::kMainWorldId |
263 : GetIsolatedWorldIdForExtension(extension, frame); | 276 : GetIsolatedWorldIdForInstance(host, instance_id_, frame); |
264 bool expects_results = injector_->ExpectsResults(); | 277 bool expects_results = injector_->ExpectsResults(); |
265 | 278 |
266 base::ElapsedTimer exec_timer; | 279 base::ElapsedTimer exec_timer; |
267 DOMActivityLogger::AttachToWorld(world_id, extension->id()); | 280 DOMActivityLogger::AttachToWorld(world_id, host->id().id()); |
Devlin
2015/02/09 17:40:25
We almost certainly don't want activity logger to
Xi Han
2015/02/09 23:28:11
Add a check for host_type == EXTENSIONS.
| |
268 v8::HandleScope scope(v8::Isolate::GetCurrent()); | 281 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
269 v8::Local<v8::Value> script_value; | 282 v8::Local<v8::Value> script_value; |
270 if (in_main_world) { | 283 if (in_main_world) { |
271 // We only inject in the main world for javascript: urls. | 284 // We only inject in the main world for javascript: urls. |
272 DCHECK_EQ(1u, sources.size()); | 285 DCHECK_EQ(1u, sources.size()); |
273 | 286 |
274 const blink::WebScriptSource& source = sources.front(); | 287 const blink::WebScriptSource& source = sources.front(); |
275 if (expects_results) | 288 if (expects_results) |
276 script_value = frame->executeScriptAndReturnValue(source); | 289 script_value = frame->executeScriptAndReturnValue(source); |
277 else | 290 else |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
313 std::vector<std::string> css_sources = | 326 std::vector<std::string> css_sources = |
314 injector_->GetCssSources(run_location_); | 327 injector_->GetCssSources(run_location_); |
315 for (std::vector<std::string>::const_iterator iter = css_sources.begin(); | 328 for (std::vector<std::string>::const_iterator iter = css_sources.begin(); |
316 iter != css_sources.end(); | 329 iter != css_sources.end(); |
317 ++iter) { | 330 ++iter) { |
318 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter)); | 331 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter)); |
319 } | 332 } |
320 } | 333 } |
321 | 334 |
322 } // namespace extensions | 335 } // namespace extensions |
OLD | NEW |