| 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->executing_scripts[host_id().id()])); |
| 218 bool should_inject_css = injector_->ShouldInjectCss( |
| 219 run_location_, &(scripts_run_info->injected_stylesheets[host_id().id()])); |
| 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/631247. |
| 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->executing_scripts[host_id().id()]), |
| 230 &(scripts_run_info->num_js)); |
| 223 if (should_inject_css) | 231 if (should_inject_css) |
| 224 InjectCss(); | 232 InjectCss(&(scripts_run_info->injected_stylesheets[host_id().id()]), |
| 233 &(scripts_run_info->num_css)); |
| 225 | 234 |
| 226 complete_ = did_inject_js_ || !should_inject_js; | 235 complete_ = did_inject_js_ || !should_inject_js; |
| 227 | 236 |
| 228 injector_->GetRunInfo(scripts_run_info, run_location_); | |
| 229 | |
| 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(std::set<std::string>* executing_scripts, |
| 248 size_t* num_injected_js_script) { |
| 241 DCHECK(!did_inject_js_); | 249 DCHECK(!did_inject_js_); |
| 242 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 250 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
| 243 std::vector<blink::WebScriptSource> sources = | 251 std::vector<blink::WebScriptSource> sources = injector_->GetJsSources( |
| 244 injector_->GetJsSources(run_location_); | 252 run_location_, executing_scripts, num_injected_js_script); |
| 253 DCHECK(!sources.empty()); |
| 245 bool in_main_world = injector_->ShouldExecuteInMainWorld(); | 254 bool in_main_world = injector_->ShouldExecuteInMainWorld(); |
| 246 int world_id = in_main_world | 255 int world_id = in_main_world |
| 247 ? DOMActivityLogger::kMainWorldId | 256 ? DOMActivityLogger::kMainWorldId |
| 248 : GetIsolatedWorldIdForInstance(injection_host_.get(), | 257 : GetIsolatedWorldIdForInstance(injection_host_.get(), |
| 249 web_frame); | 258 web_frame); |
| 250 bool is_user_gesture = injector_->IsUserGesture(); | 259 bool is_user_gesture = injector_->IsUserGesture(); |
| 251 | 260 |
| 252 std::unique_ptr<blink::WebScriptExecutionCallback> callback( | 261 std::unique_ptr<blink::WebScriptExecutionCallback> callback( |
| 253 new ScriptInjectionCallback( | 262 new ScriptInjectionCallback( |
| 254 base::Bind(&ScriptInjection::OnJsInjectionCompleted, | 263 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 | 313 // If |async_completion_callback_| is set, it means the script finished |
| 305 // asynchronously, and we should run it. | 314 // asynchronously, and we should run it. |
| 306 if (!async_completion_callback_.is_null()) { | 315 if (!async_completion_callback_.is_null()) { |
| 307 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, | 316 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, |
| 308 render_frame_); | 317 render_frame_); |
| 309 // Warning: this object can be destroyed after this line! | 318 // Warning: this object can be destroyed after this line! |
| 310 async_completion_callback_.Run(this); | 319 async_completion_callback_.Run(this); |
| 311 } | 320 } |
| 312 } | 321 } |
| 313 | 322 |
| 314 void ScriptInjection::InjectCss() { | 323 void ScriptInjection::InjectCss(std::set<std::string>* injected_stylesheets, |
| 315 std::vector<blink::WebString> css_sources = | 324 size_t* num_injected_stylesheets) { |
| 316 injector_->GetCssSources(run_location_); | 325 std::vector<blink::WebString> css_sources = injector_->GetCssSources( |
| 326 run_location_, injected_stylesheets, num_injected_stylesheets); |
| 317 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 327 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
| 318 for (const blink::WebString& css : css_sources) | 328 for (const blink::WebString& css : css_sources) |
| 319 web_frame->document().insertStyleSheet(css); | 329 web_frame->document().insertStyleSheet(css); |
| 320 } | 330 } |
| 321 | 331 |
| 322 } // namespace extensions | 332 } // namespace extensions |
| OLD | NEW |