| 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 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 ScriptInjector::InjectFailureReason reason) { | 206 ScriptInjector::InjectFailureReason reason) { |
| 207 complete_ = true; | 207 complete_ = true; |
| 208 injector_->OnWillNotInject(reason, render_frame_); | 208 injector_->OnWillNotInject(reason, render_frame_); |
| 209 } | 209 } |
| 210 | 210 |
| 211 ScriptInjection::InjectionResult ScriptInjection::Inject( | 211 ScriptInjection::InjectionResult ScriptInjection::Inject( |
| 212 ScriptsRunInfo* scripts_run_info) { | 212 ScriptsRunInfo* scripts_run_info) { |
| 213 DCHECK(injection_host_); | 213 DCHECK(injection_host_); |
| 214 DCHECK(scripts_run_info); | 214 DCHECK(scripts_run_info); |
| 215 DCHECK(!complete_); | 215 DCHECK(!complete_); |
| 216 bool should_inject_js = injector_->ShouldInjectJs( |
| 217 run_location_, scripts_run_info->injected_scripts); |
| 218 bool should_inject_css = injector_->ShouldInjectCss( |
| 219 run_location_, scripts_run_info->injected_scripts); |
| 216 | 220 |
| 217 bool should_inject_js = injector_->ShouldInjectJs(run_location_); | 221 // This can happen if the extension specified a script to |
| 218 bool should_inject_css = injector_->ShouldInjectCss(run_location_); | 222 // be run in multiple rules, and the script has already run. |
| 219 DCHECK(should_inject_js || should_inject_css); | 223 // See crbug.com/nnn. |
| 224 if (!should_inject_js && !should_inject_css) { |
| 225 return INJECTION_FINISHED; |
| 226 } |
| 220 | 227 |
| 221 if (should_inject_js) | 228 if (should_inject_js) |
| 222 InjectJs(); | 229 InjectJs(scripts_run_info); |
| 223 if (should_inject_css) | 230 if (should_inject_css) |
| 224 InjectCss(); | 231 InjectCss(scripts_run_info); |
| 225 | 232 |
| 226 complete_ = did_inject_js_ || !should_inject_js; | 233 complete_ = did_inject_js_ || !should_inject_js; |
| 227 | 234 |
| 228 injector_->GetRunInfo(scripts_run_info, run_location_); | 235 injector_->GetRunInfo(scripts_run_info, run_location_); |
| 229 | 236 |
| 230 if (complete_) { | 237 if (complete_) { |
| 231 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, | 238 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, |
| 232 render_frame_); | 239 render_frame_); |
| 233 } else { | 240 } else { |
| 234 ++scripts_run_info->num_blocking_js; | 241 ++scripts_run_info->num_blocking_js; |
| 235 } | 242 } |
| 236 | 243 |
| 237 return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED; | 244 return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED; |
| 238 } | 245 } |
| 239 | 246 |
| 240 void ScriptInjection::InjectJs() { | 247 void ScriptInjection::InjectJs(ScriptsRunInfo* scripts_run_info) { |
| 241 DCHECK(!did_inject_js_); | 248 DCHECK(!did_inject_js_); |
| 242 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 249 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
| 243 std::vector<blink::WebScriptSource> sources = | 250 std::vector<blink::WebScriptSource> sources = |
| 244 injector_->GetJsSources(run_location_); | 251 injector_->GetJsSources(run_location_, scripts_run_info); |
| 252 DCHECK(!sources.empty()); |
| 245 bool in_main_world = injector_->ShouldExecuteInMainWorld(); | 253 bool in_main_world = injector_->ShouldExecuteInMainWorld(); |
| 246 int world_id = in_main_world | 254 int world_id = in_main_world |
| 247 ? DOMActivityLogger::kMainWorldId | 255 ? DOMActivityLogger::kMainWorldId |
| 248 : GetIsolatedWorldIdForInstance(injection_host_.get(), | 256 : GetIsolatedWorldIdForInstance(injection_host_.get(), |
| 249 web_frame); | 257 web_frame); |
| 250 bool is_user_gesture = injector_->IsUserGesture(); | 258 bool is_user_gesture = injector_->IsUserGesture(); |
| 251 | 259 |
| 252 std::unique_ptr<blink::WebScriptExecutionCallback> callback( | 260 std::unique_ptr<blink::WebScriptExecutionCallback> callback( |
| 253 new ScriptInjectionCallback( | 261 new ScriptInjectionCallback( |
| 254 base::Bind(&ScriptInjection::OnJsInjectionCompleted, | 262 base::Bind(&ScriptInjection::OnJsInjectionCompleted, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 // If |async_completion_callback_| is set, it means the script finished | 312 // If |async_completion_callback_| is set, it means the script finished |
| 305 // asynchronously, and we should run it. | 313 // asynchronously, and we should run it. |
| 306 if (!async_completion_callback_.is_null()) { | 314 if (!async_completion_callback_.is_null()) { |
| 307 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, | 315 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, |
| 308 render_frame_); | 316 render_frame_); |
| 309 // Warning: this object can be destroyed after this line! | 317 // Warning: this object can be destroyed after this line! |
| 310 async_completion_callback_.Run(this); | 318 async_completion_callback_.Run(this); |
| 311 } | 319 } |
| 312 } | 320 } |
| 313 | 321 |
| 314 void ScriptInjection::InjectCss() { | 322 void ScriptInjection::InjectCss(ScriptsRunInfo* scripts_run_info) { |
| 315 std::vector<blink::WebString> css_sources = | 323 std::vector<blink::WebString> css_sources = |
| 316 injector_->GetCssSources(run_location_); | 324 injector_->GetCssSources(run_location_, scripts_run_info); |
| 317 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 325 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
| 318 for (const blink::WebString& css : css_sources) | 326 for (const blink::WebString& css : css_sources) |
| 319 web_frame->document().insertStyleSheet(css); | 327 web_frame->document().insertStyleSheet(css); |
| 320 } | 328 } |
| 321 | 329 |
| 322 } // namespace extensions | 330 } // namespace extensions |
| OLD | NEW |