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

Side by Side Diff: extensions/renderer/script_injection.cc

Issue 594043002: Change ScriptInjection to work with WebLocalFrames. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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
« no previous file with comments | « extensions/renderer/script_injection.h ('k') | extensions/renderer/script_injection_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #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_groups.h" 20 #include "extensions/renderer/extension_groups.h"
21 #include "extensions/renderer/extensions_renderer_client.h" 21 #include "extensions/renderer/extensions_renderer_client.h"
22 #include "third_party/WebKit/public/platform/WebString.h" 22 #include "third_party/WebKit/public/platform/WebString.h"
23 #include "third_party/WebKit/public/web/WebDocument.h" 23 #include "third_party/WebKit/public/web/WebDocument.h"
24 #include "third_party/WebKit/public/web/WebFrame.h" 24 #include "third_party/WebKit/public/web/WebLocalFrame.h"
25 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" 25 #include "third_party/WebKit/public/web/WebScopedUserGesture.h"
26 #include "third_party/WebKit/public/web/WebScriptSource.h" 26 #include "third_party/WebKit/public/web/WebScriptSource.h"
27 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" 27 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
28 #include "url/gurl.h" 28 #include "url/gurl.h"
29 29
30 namespace extensions { 30 namespace extensions {
31 31
32 namespace { 32 namespace {
33 33
34 typedef std::map<std::string, int> IsolatedWorldMap; 34 typedef std::map<std::string, int> IsolatedWorldMap;
(...skipping 17 matching lines...) Expand all
52 child_frame = child_frame->nextSibling()) { 52 child_frame = child_frame->nextSibling()) {
53 frames_vector->push_back(child_frame); 53 frames_vector->push_back(child_frame);
54 AppendAllChildFrames(child_frame, frames_vector); 54 AppendAllChildFrames(child_frame, frames_vector);
55 } 55 }
56 } 56 }
57 57
58 // Gets the isolated world ID to use for the given |extension| in the given 58 // Gets the isolated world ID to use for the given |extension| in the given
59 // |frame|. If no isolated world has been created for that extension, 59 // |frame|. If no isolated world has been created for that extension,
60 // one will be created and initialized. 60 // one will be created and initialized.
61 int GetIsolatedWorldIdForExtension(const Extension* extension, 61 int GetIsolatedWorldIdForExtension(const Extension* extension,
62 blink::WebFrame* frame) { 62 blink::WebLocalFrame* frame) {
63 static int g_next_isolated_world_id = 63 static int g_next_isolated_world_id =
64 ExtensionsRendererClient::Get()->GetLowestIsolatedWorldId(); 64 ExtensionsRendererClient::Get()->GetLowestIsolatedWorldId();
65 65
66 IsolatedWorldMap& isolated_worlds = g_isolated_worlds.Get(); 66 IsolatedWorldMap& isolated_worlds = g_isolated_worlds.Get();
67 67
68 int id = 0; 68 int id = 0;
69 IsolatedWorldMap::iterator iter = isolated_worlds.find(extension->id()); 69 IsolatedWorldMap::iterator iter = isolated_worlds.find(extension->id());
70 if (iter != isolated_worlds.end()) { 70 if (iter != isolated_worlds.end()) {
71 id = iter->second; 71 id = iter->second;
72 } else { 72 } else {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 return std::string(); 107 return std::string();
108 } 108 }
109 109
110 // static 110 // static
111 void ScriptInjection::RemoveIsolatedWorld(const std::string& extension_id) { 111 void ScriptInjection::RemoveIsolatedWorld(const std::string& extension_id) {
112 g_isolated_worlds.Get().erase(extension_id); 112 g_isolated_worlds.Get().erase(extension_id);
113 } 113 }
114 114
115 ScriptInjection::ScriptInjection( 115 ScriptInjection::ScriptInjection(
116 scoped_ptr<ScriptInjector> injector, 116 scoped_ptr<ScriptInjector> injector,
117 blink::WebFrame* web_frame, 117 blink::WebLocalFrame* web_frame,
118 const std::string& extension_id, 118 const std::string& extension_id,
119 UserScript::RunLocation run_location, 119 UserScript::RunLocation run_location,
120 int tab_id) 120 int tab_id)
121 : injector_(injector.Pass()), 121 : injector_(injector.Pass()),
122 web_frame_(web_frame), 122 web_frame_(web_frame),
123 extension_id_(extension_id), 123 extension_id_(extension_id),
124 run_location_(run_location), 124 run_location_(run_location),
125 tab_id_(tab_id), 125 tab_id_(tab_id),
126 request_id_(kInvalidRequestId), 126 request_id_(kInvalidRequestId),
127 complete_(false) { 127 complete_(false) {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 216
217 bool inject_js = injector_->ShouldInjectJs(run_location_); 217 bool inject_js = injector_->ShouldInjectJs(run_location_);
218 bool inject_css = injector_->ShouldInjectCss(run_location_); 218 bool inject_css = injector_->ShouldInjectCss(run_location_);
219 DCHECK(inject_js || inject_css); 219 DCHECK(inject_js || inject_css);
220 220
221 scoped_ptr<base::ListValue> execution_results(new base::ListValue()); 221 scoped_ptr<base::ListValue> execution_results(new base::ListValue());
222 GURL top_url = web_frame_->top()->document().url(); 222 GURL top_url = web_frame_->top()->document().url();
223 for (std::vector<blink::WebFrame*>::iterator iter = frame_vector.begin(); 223 for (std::vector<blink::WebFrame*>::iterator iter = frame_vector.begin();
224 iter != frame_vector.end(); 224 iter != frame_vector.end();
225 ++iter) { 225 ++iter) {
226 blink::WebFrame* frame = *iter; 226 // 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.
228 blink::WebLocalFrame* frame = (*iter)->toWebLocalFrame();
227 229
228 // We recheck access here in the renderer for extra safety against races 230 // We recheck access here in the renderer for extra safety against races
229 // with navigation, but different frames can have different URLs, and the 231 // with navigation, but different frames can have different URLs, and the
230 // extension might only have access to a subset of them. 232 // extension might only have access to a subset of them.
231 // For child frames, we just skip ones the extension doesn't have access 233 // For child frames, we just skip ones the extension doesn't have access
232 // to and carry on. 234 // to and carry on.
233 // Note: we don't consider ACCESS_WITHHELD because there is nowhere to 235 // Note: we don't consider ACCESS_WITHHELD because there is nowhere to
234 // surface a request for a child frame. 236 // surface a request for a child frame.
235 // TODO(rdevlin.cronin): We should ask for permission somehow. 237 // TODO(rdevlin.cronin): We should ask for permission somehow.
236 if (injector_->CanExecuteOnFrame(extension, frame, tab_id_, top_url) == 238 if (injector_->CanExecuteOnFrame(extension, frame, tab_id_, top_url) ==
237 PermissionsData::ACCESS_DENIED) { 239 PermissionsData::ACCESS_DENIED) {
238 DCHECK(frame->parent()); 240 DCHECK(frame->parent());
239 continue; 241 continue;
240 } 242 }
241 if (inject_js) 243 if (inject_js)
242 InjectJs(extension, frame, execution_results.get()); 244 InjectJs(extension, frame, execution_results.get());
243 if (inject_css) 245 if (inject_css)
244 InjectCss(frame); 246 InjectCss(frame);
245 } 247 }
246 248
247 complete_ = true; 249 complete_ = true;
248 injector_->OnInjectionComplete(execution_results.Pass(), 250 injector_->OnInjectionComplete(execution_results.Pass(),
249 scripts_run_info, 251 scripts_run_info,
250 run_location_); 252 run_location_);
251 } 253 }
252 254
253 void ScriptInjection::InjectJs(const Extension* extension, 255 void ScriptInjection::InjectJs(const Extension* extension,
254 blink::WebFrame* frame, 256 blink::WebLocalFrame* frame,
255 base::ListValue* execution_results) { 257 base::ListValue* execution_results) {
256 std::vector<blink::WebScriptSource> sources = 258 std::vector<blink::WebScriptSource> sources =
257 injector_->GetJsSources(run_location_); 259 injector_->GetJsSources(run_location_);
258 bool in_main_world = injector_->ShouldExecuteInMainWorld(); 260 bool in_main_world = injector_->ShouldExecuteInMainWorld();
259 int world_id = in_main_world 261 int world_id = in_main_world
260 ? DOMActivityLogger::kMainWorldId 262 ? DOMActivityLogger::kMainWorldId
261 : GetIsolatedWorldIdForExtension(extension, frame); 263 : GetIsolatedWorldIdForExtension(extension, frame);
262 bool expects_results = injector_->ExpectsResults(); 264 bool expects_results = injector_->ExpectsResults();
263 265
264 base::ElapsedTimer exec_timer; 266 base::ElapsedTimer exec_timer;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); 302 v8::Local<v8::Context> context = frame->mainWorldScriptContext();
301 scoped_ptr<base::Value> result( 303 scoped_ptr<base::Value> result(
302 v8_converter->FromV8Value(script_value, context)); 304 v8_converter->FromV8Value(script_value, context));
303 // Always append an execution result (i.e. no result == null result) 305 // Always append an execution result (i.e. no result == null result)
304 // so that |execution_results| lines up with the frames. 306 // so that |execution_results| lines up with the frames.
305 execution_results->Append(result.get() ? result.release() 307 execution_results->Append(result.get() ? result.release()
306 : base::Value::CreateNullValue()); 308 : base::Value::CreateNullValue());
307 } 309 }
308 } 310 }
309 311
310 void ScriptInjection::InjectCss(blink::WebFrame* frame) { 312 void ScriptInjection::InjectCss(blink::WebLocalFrame* frame) {
311 std::vector<std::string> css_sources = 313 std::vector<std::string> css_sources =
312 injector_->GetCssSources(run_location_); 314 injector_->GetCssSources(run_location_);
313 for (std::vector<std::string>::const_iterator iter = css_sources.begin(); 315 for (std::vector<std::string>::const_iterator iter = css_sources.begin();
314 iter != css_sources.end(); 316 iter != css_sources.end();
315 ++iter) { 317 ++iter) {
316 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter)); 318 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter));
317 } 319 }
318 } 320 }
319 321
320 } // namespace extensions 322 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/script_injection.h ('k') | extensions/renderer/script_injection_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698