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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 | 216 |
217 bool should_inject_js = injector_->ShouldInjectJs(run_location_); | 217 bool should_inject_js = |
218 bool should_inject_css = injector_->ShouldInjectCss(run_location_); | 218 injector_->ShouldInjectJs(run_location_, scripts_run_info); |
219 bool should_inject_css = | |
220 injector_->ShouldInjectCss(run_location_, scripts_run_info); | |
219 DCHECK(should_inject_js || should_inject_css); | 221 DCHECK(should_inject_js || should_inject_css); |
220 | 222 |
221 if (should_inject_js) | 223 if (should_inject_js) |
222 InjectJs(); | 224 InjectJs(scripts_run_info); |
223 if (should_inject_css) | 225 if (should_inject_css) |
224 InjectCss(); | 226 InjectCss(scripts_run_info); |
225 | 227 |
226 complete_ = did_inject_js_ || !should_inject_js; | 228 complete_ = did_inject_js_ || !should_inject_js; |
227 | 229 |
228 injector_->GetRunInfo(scripts_run_info, run_location_); | 230 injector_->GetRunInfo(scripts_run_info, run_location_); |
229 | 231 |
230 if (complete_) { | 232 if (complete_) { |
231 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, | 233 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, |
232 render_frame_); | 234 render_frame_); |
233 } else { | 235 } else { |
234 ++scripts_run_info->num_blocking_js; | 236 ++scripts_run_info->num_blocking_js; |
235 } | 237 } |
236 | 238 |
237 return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED; | 239 return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED; |
238 } | 240 } |
239 | 241 |
240 void ScriptInjection::InjectJs() { | 242 void ScriptInjection::InjectJs(ScriptsRunInfo* scripts_run_info) { |
241 DCHECK(!did_inject_js_); | 243 DCHECK(!did_inject_js_); |
242 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 244 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
243 std::vector<blink::WebScriptSource> sources = | 245 std::vector<blink::WebScriptSource> sources = |
244 injector_->GetJsSources(run_location_); | 246 injector_->GetJsSources(run_location_, scripts_run_info); |
247 DCHECK(sources.size() != 0); | |
Devlin
2016/08/03 23:34:44
nit: prefer !sources.empty()
catmullings
2016/08/09 23:13:41
Done.
| |
245 bool in_main_world = injector_->ShouldExecuteInMainWorld(); | 248 bool in_main_world = injector_->ShouldExecuteInMainWorld(); |
246 int world_id = in_main_world | 249 int world_id = in_main_world |
247 ? DOMActivityLogger::kMainWorldId | 250 ? DOMActivityLogger::kMainWorldId |
248 : GetIsolatedWorldIdForInstance(injection_host_.get(), | 251 : GetIsolatedWorldIdForInstance(injection_host_.get(), |
249 web_frame); | 252 web_frame); |
250 bool is_user_gesture = injector_->IsUserGesture(); | 253 bool is_user_gesture = injector_->IsUserGesture(); |
251 | 254 |
252 std::unique_ptr<blink::WebScriptExecutionCallback> callback( | 255 std::unique_ptr<blink::WebScriptExecutionCallback> callback( |
253 new ScriptInjectionCallback( | 256 new ScriptInjectionCallback( |
254 base::Bind(&ScriptInjection::OnJsInjectionCompleted, | 257 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 | 307 // If |async_completion_callback_| is set, it means the script finished |
305 // asynchronously, and we should run it. | 308 // asynchronously, and we should run it. |
306 if (!async_completion_callback_.is_null()) { | 309 if (!async_completion_callback_.is_null()) { |
307 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, | 310 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, |
308 render_frame_); | 311 render_frame_); |
309 // Warning: this object can be destroyed after this line! | 312 // Warning: this object can be destroyed after this line! |
310 async_completion_callback_.Run(this); | 313 async_completion_callback_.Run(this); |
311 } | 314 } |
312 } | 315 } |
313 | 316 |
314 void ScriptInjection::InjectCss() { | 317 void ScriptInjection::InjectCss(ScriptsRunInfo* scripts_run_info) { |
315 std::vector<std::string> css_sources = | 318 std::vector<std::string> css_sources = |
316 injector_->GetCssSources(run_location_); | 319 injector_->GetCssSources(run_location_, scripts_run_info); |
317 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 320 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
318 for (const std::string& css : css_sources) | 321 for (const std::string& css : css_sources) |
319 web_frame->document().insertStyleSheet(blink::WebString::fromUTF8(css)); | 322 web_frame->document().insertStyleSheet(blink::WebString::fromUTF8(css)); |
320 } | 323 } |
321 | 324 |
322 } // namespace extensions | 325 } // namespace extensions |
OLD | NEW |