| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 return iter.first; | 108 return iter.first; |
| 109 } | 109 } |
| 110 return std::string(); | 110 return std::string(); |
| 111 } | 111 } |
| 112 | 112 |
| 113 // static | 113 // static |
| 114 void ScriptInjection::RemoveIsolatedWorld(const std::string& host_id) { | 114 void ScriptInjection::RemoveIsolatedWorld(const std::string& host_id) { |
| 115 g_isolated_worlds.Get().erase(host_id); | 115 g_isolated_worlds.Get().erase(host_id); |
| 116 } | 116 } |
| 117 | 117 |
| 118 ScriptInjection::ScriptInjection(scoped_ptr<ScriptInjector> injector, | 118 ScriptInjection::ScriptInjection( |
| 119 content::RenderFrame* render_frame, | 119 std::unique_ptr<ScriptInjector> injector, |
| 120 scoped_ptr<const InjectionHost> injection_host, | 120 content::RenderFrame* render_frame, |
| 121 UserScript::RunLocation run_location) | 121 std::unique_ptr<const InjectionHost> injection_host, |
| 122 UserScript::RunLocation run_location) |
| 122 : injector_(std::move(injector)), | 123 : injector_(std::move(injector)), |
| 123 render_frame_(render_frame), | 124 render_frame_(render_frame), |
| 124 injection_host_(std::move(injection_host)), | 125 injection_host_(std::move(injection_host)), |
| 125 run_location_(run_location), | 126 run_location_(run_location), |
| 126 request_id_(kInvalidRequestId), | 127 request_id_(kInvalidRequestId), |
| 127 complete_(false), | 128 complete_(false), |
| 128 did_inject_js_(false), | 129 did_inject_js_(false), |
| 129 frame_watcher_(new FrameWatcher(render_frame, this)), | 130 frame_watcher_(new FrameWatcher(render_frame, this)), |
| 130 weak_ptr_factory_(this) { | 131 weak_ptr_factory_(this) { |
| 131 CHECK(injection_host_.get()); | 132 CHECK(injection_host_.get()); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 240 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
| 240 std::vector<blink::WebScriptSource> sources = | 241 std::vector<blink::WebScriptSource> sources = |
| 241 injector_->GetJsSources(run_location_); | 242 injector_->GetJsSources(run_location_); |
| 242 bool in_main_world = injector_->ShouldExecuteInMainWorld(); | 243 bool in_main_world = injector_->ShouldExecuteInMainWorld(); |
| 243 int world_id = in_main_world | 244 int world_id = in_main_world |
| 244 ? DOMActivityLogger::kMainWorldId | 245 ? DOMActivityLogger::kMainWorldId |
| 245 : GetIsolatedWorldIdForInstance(injection_host_.get(), | 246 : GetIsolatedWorldIdForInstance(injection_host_.get(), |
| 246 web_frame); | 247 web_frame); |
| 247 bool is_user_gesture = injector_->IsUserGesture(); | 248 bool is_user_gesture = injector_->IsUserGesture(); |
| 248 | 249 |
| 249 scoped_ptr<blink::WebScriptExecutionCallback> callback( | 250 std::unique_ptr<blink::WebScriptExecutionCallback> callback( |
| 250 new ScriptInjectionCallback( | 251 new ScriptInjectionCallback( |
| 251 base::Bind(&ScriptInjection::OnJsInjectionCompleted, | 252 base::Bind(&ScriptInjection::OnJsInjectionCompleted, |
| 252 weak_ptr_factory_.GetWeakPtr()))); | 253 weak_ptr_factory_.GetWeakPtr()))); |
| 253 | 254 |
| 254 base::ElapsedTimer exec_timer; | 255 base::ElapsedTimer exec_timer; |
| 255 if (injection_host_->id().type() == HostID::EXTENSIONS) | 256 if (injection_host_->id().type() == HostID::EXTENSIONS) |
| 256 DOMActivityLogger::AttachToWorld(world_id, injection_host_->id().id()); | 257 DOMActivityLogger::AttachToWorld(world_id, injection_host_->id().id()); |
| 257 if (in_main_world) { | 258 if (in_main_world) { |
| 258 // We only inject in the main world for javascript: urls. | 259 // We only inject in the main world for javascript: urls. |
| 259 DCHECK_EQ(1u, sources.size()); | 260 DCHECK_EQ(1u, sources.size()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 276 } | 277 } |
| 277 | 278 |
| 278 void ScriptInjection::OnJsInjectionCompleted( | 279 void ScriptInjection::OnJsInjectionCompleted( |
| 279 const blink::WebVector<v8::Local<v8::Value> >& results) { | 280 const blink::WebVector<v8::Local<v8::Value> >& results) { |
| 280 DCHECK(!did_inject_js_); | 281 DCHECK(!did_inject_js_); |
| 281 | 282 |
| 282 bool expects_results = injector_->ExpectsResults(); | 283 bool expects_results = injector_->ExpectsResults(); |
| 283 if (expects_results) { | 284 if (expects_results) { |
| 284 if (!results.isEmpty() && !results[0].IsEmpty()) { | 285 if (!results.isEmpty() && !results[0].IsEmpty()) { |
| 285 // Right now, we only support returning single results (per frame). | 286 // Right now, we only support returning single results (per frame). |
| 286 scoped_ptr<content::V8ValueConverter> v8_converter( | 287 std::unique_ptr<content::V8ValueConverter> v8_converter( |
| 287 content::V8ValueConverter::create()); | 288 content::V8ValueConverter::create()); |
| 288 // It's safe to always use the main world context when converting | 289 // It's safe to always use the main world context when converting |
| 289 // here. V8ValueConverterImpl shouldn't actually care about the | 290 // here. V8ValueConverterImpl shouldn't actually care about the |
| 290 // context scope, and it switches to v8::Object's creation context | 291 // context scope, and it switches to v8::Object's creation context |
| 291 // when encountered. | 292 // when encountered. |
| 292 v8::Local<v8::Context> context = | 293 v8::Local<v8::Context> context = |
| 293 render_frame_->GetWebFrame()->mainWorldScriptContext(); | 294 render_frame_->GetWebFrame()->mainWorldScriptContext(); |
| 294 execution_result_.reset(v8_converter->FromV8Value(results[0], context)); | 295 execution_result_.reset(v8_converter->FromV8Value(results[0], context)); |
| 295 } | 296 } |
| 296 if (!execution_result_.get()) | 297 if (!execution_result_.get()) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 310 | 311 |
| 311 void ScriptInjection::InjectCss() { | 312 void ScriptInjection::InjectCss() { |
| 312 std::vector<std::string> css_sources = | 313 std::vector<std::string> css_sources = |
| 313 injector_->GetCssSources(run_location_); | 314 injector_->GetCssSources(run_location_); |
| 314 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 315 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
| 315 for (const std::string& css : css_sources) | 316 for (const std::string& css : css_sources) |
| 316 web_frame->document().insertStyleSheet(blink::WebString::fromUTF8(css)); | 317 web_frame->document().insertStyleSheet(blink::WebString::fromUTF8(css)); |
| 317 } | 318 } |
| 318 | 319 |
| 319 } // namespace extensions | 320 } // namespace extensions |
| OLD | NEW |