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 // Occurs when script is already injected. |
Devlin
2016/08/22 22:46:52
I'd be a little more explicit here - maybe somethi
catmullings
2016/08/24 01:13:19
That's clearer. I'll use yours.
Which crbug.com/nn
Devlin
2016/08/25 16:52:26
crbug.com/631247, which is the crash that resulted
catmullings
2016/08/27 00:23:08
Done.
| |
218 bool should_inject_css = injector_->ShouldInjectCss(run_location_); | 222 if (!(should_inject_js || should_inject_css)) { |
Devlin
2016/08/22 22:46:52
nit: I find this a little more readable after we D
catmullings
2016/08/24 01:13:19
Done.
| |
219 DCHECK(should_inject_js || should_inject_css); | 223 return INJECTION_FINISHED; |
224 } | |
220 | 225 |
221 if (should_inject_js) | 226 if (should_inject_js) |
222 InjectJs(); | 227 InjectJs(scripts_run_info); |
223 if (should_inject_css) | 228 if (should_inject_css) |
224 InjectCss(); | 229 InjectCss(scripts_run_info); |
225 | 230 |
226 complete_ = did_inject_js_ || !should_inject_js; | 231 complete_ = did_inject_js_ || !should_inject_js; |
227 | 232 |
228 injector_->GetRunInfo(scripts_run_info, run_location_); | 233 injector_->GetRunInfo(scripts_run_info, run_location_); |
229 | 234 |
230 if (complete_) { | 235 if (complete_) { |
231 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, | 236 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, |
232 render_frame_); | 237 render_frame_); |
233 } else { | 238 } else { |
234 ++scripts_run_info->num_blocking_js; | 239 ++scripts_run_info->num_blocking_js; |
235 } | 240 } |
236 | 241 |
237 return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED; | 242 return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED; |
238 } | 243 } |
239 | 244 |
240 void ScriptInjection::InjectJs() { | 245 void ScriptInjection::InjectJs(ScriptsRunInfo* scripts_run_info) { |
241 DCHECK(!did_inject_js_); | 246 DCHECK(!did_inject_js_); |
242 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 247 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
243 std::vector<blink::WebScriptSource> sources = | 248 std::vector<blink::WebScriptSource> sources = |
244 injector_->GetJsSources(run_location_); | 249 injector_->GetJsSources(run_location_, scripts_run_info); |
250 DCHECK(!sources.empty()); | |
245 bool in_main_world = injector_->ShouldExecuteInMainWorld(); | 251 bool in_main_world = injector_->ShouldExecuteInMainWorld(); |
246 int world_id = in_main_world | 252 int world_id = in_main_world |
247 ? DOMActivityLogger::kMainWorldId | 253 ? DOMActivityLogger::kMainWorldId |
248 : GetIsolatedWorldIdForInstance(injection_host_.get(), | 254 : GetIsolatedWorldIdForInstance(injection_host_.get(), |
249 web_frame); | 255 web_frame); |
250 bool is_user_gesture = injector_->IsUserGesture(); | 256 bool is_user_gesture = injector_->IsUserGesture(); |
251 | 257 |
252 std::unique_ptr<blink::WebScriptExecutionCallback> callback( | 258 std::unique_ptr<blink::WebScriptExecutionCallback> callback( |
253 new ScriptInjectionCallback( | 259 new ScriptInjectionCallback( |
254 base::Bind(&ScriptInjection::OnJsInjectionCompleted, | 260 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 | 310 // If |async_completion_callback_| is set, it means the script finished |
305 // asynchronously, and we should run it. | 311 // asynchronously, and we should run it. |
306 if (!async_completion_callback_.is_null()) { | 312 if (!async_completion_callback_.is_null()) { |
307 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, | 313 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, |
308 render_frame_); | 314 render_frame_); |
309 // Warning: this object can be destroyed after this line! | 315 // Warning: this object can be destroyed after this line! |
310 async_completion_callback_.Run(this); | 316 async_completion_callback_.Run(this); |
311 } | 317 } |
312 } | 318 } |
313 | 319 |
314 void ScriptInjection::InjectCss() { | 320 void ScriptInjection::InjectCss(ScriptsRunInfo* scripts_run_info) { |
315 std::vector<std::string> css_sources = | 321 std::vector<std::string> css_sources = |
316 injector_->GetCssSources(run_location_); | 322 injector_->GetCssSources(run_location_, scripts_run_info); |
317 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 323 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
318 for (const std::string& css : css_sources) | 324 for (const std::string& css : css_sources) |
319 web_frame->document().insertStyleSheet(blink::WebString::fromUTF8(css)); | 325 web_frame->document().insertStyleSheet(blink::WebString::fromUTF8(css)); |
320 } | 326 } |
321 | 327 |
322 } // namespace extensions | 328 } // namespace extensions |
OLD | NEW |