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/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); |
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_, complete_, |
Devlin
2016/08/29 20:48:14
why do we pass |complete_| instead of |should_inje
catmullings
2016/09/01 17:43:51
No longer applicable with GetRunInfo rolled into G
catmullings
2016/09/01 17:43:51
Done.
| |
236 should_inject_css); | |
229 | 237 |
230 if (complete_) { | 238 if (complete_) { |
231 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, | 239 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, |
232 render_frame_); | 240 render_frame_); |
233 } else { | 241 } else { |
234 ++scripts_run_info->num_blocking_js; | 242 ++scripts_run_info->num_blocking_js; |
235 } | 243 } |
236 | 244 |
237 return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED; | 245 return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED; |
238 } | 246 } |
239 | 247 |
240 void ScriptInjection::InjectJs() { | 248 void ScriptInjection::InjectJs(ScriptsRunInfo* scripts_run_info) { |
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_, scripts_run_info->injected_scripts); |
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(ScriptsRunInfo* scripts_run_info) { |
315 std::vector<blink::WebString> css_sources = | 324 std::vector<blink::WebString> css_sources = injector_->GetCssSources( |
316 injector_->GetCssSources(run_location_); | 325 run_location_, scripts_run_info->injected_scripts); |
317 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 326 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
318 for (const blink::WebString& css : css_sources) | 327 for (const blink::WebString& css : css_sources) |
319 web_frame->document().insertStyleSheet(css); | 328 web_frame->document().insertStyleSheet(css); |
320 } | 329 } |
321 | 330 |
322 } // namespace extensions | 331 } // namespace extensions |
OLD | NEW |