| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 explicit TimedScriptInjectionCallback( | 84 explicit TimedScriptInjectionCallback( |
| 85 base::WeakPtr<ScriptInjection> injection) | 85 base::WeakPtr<ScriptInjection> injection) |
| 86 : ScriptInjectionCallback( | 86 : ScriptInjectionCallback( |
| 87 base::Bind(&TimedScriptInjectionCallback::OnCompleted, | 87 base::Bind(&TimedScriptInjectionCallback::OnCompleted, |
| 88 base::Unretained(this))), | 88 base::Unretained(this))), |
| 89 injection_(injection) {} | 89 injection_(injection) {} |
| 90 ~TimedScriptInjectionCallback() override {} | 90 ~TimedScriptInjectionCallback() override {} |
| 91 | 91 |
| 92 void OnCompleted(const std::vector<v8::Local<v8::Value>>& result) { | 92 void OnCompleted(const std::vector<v8::Local<v8::Value>>& result) { |
| 93 if (injection_) { | 93 if (injection_) { |
| 94 base::TimeDelta elapsed = base::TimeTicks::Now() - start_time_; | 94 base::Optional<base::TimeDelta> elapsed; |
| 95 // If the script will never execute (such as if the context is destroyed), |
| 96 // willExecute() will not be called, but OnCompleted() will. Only log a |
| 97 // time for execution if the script, in fact, executed. |
| 98 if (!start_time_.is_null()) |
| 99 elapsed = base::TimeTicks::Now() - start_time_; |
| 95 injection_->OnJsInjectionCompleted(result, elapsed); | 100 injection_->OnJsInjectionCompleted(result, elapsed); |
| 96 } | 101 } |
| 97 } | 102 } |
| 98 | 103 |
| 99 void willExecute() override { start_time_ = base::TimeTicks::Now(); } | 104 void willExecute() override { start_time_ = base::TimeTicks::Now(); } |
| 100 | 105 |
| 101 private: | 106 private: |
| 102 base::WeakPtr<ScriptInjection> injection_; | 107 base::WeakPtr<ScriptInjection> injection_; |
| 103 base::TimeTicks start_time_; | 108 base::TimeTicks start_time_; |
| 104 }; | 109 }; |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 option = blink::WebLocalFrame::Synchronous; | 318 option = blink::WebLocalFrame::Synchronous; |
| 314 } | 319 } |
| 315 web_frame->requestExecuteScriptInIsolatedWorld( | 320 web_frame->requestExecuteScriptInIsolatedWorld( |
| 316 world_id, &sources.front(), sources.size(), is_user_gesture, option, | 321 world_id, &sources.front(), sources.size(), is_user_gesture, option, |
| 317 callback.release()); | 322 callback.release()); |
| 318 } | 323 } |
| 319 } | 324 } |
| 320 | 325 |
| 321 void ScriptInjection::OnJsInjectionCompleted( | 326 void ScriptInjection::OnJsInjectionCompleted( |
| 322 const std::vector<v8::Local<v8::Value>>& results, | 327 const std::vector<v8::Local<v8::Value>>& results, |
| 323 base::TimeDelta elapsed) { | 328 base::Optional<base::TimeDelta> elapsed) { |
| 324 DCHECK(!did_inject_js_); | 329 DCHECK(!did_inject_js_); |
| 325 | 330 |
| 326 if (injection_host_->id().type() == HostID::EXTENSIONS) { | 331 if (injection_host_->id().type() == HostID::EXTENSIONS && elapsed) { |
| 327 UMA_HISTOGRAM_TIMES("Extensions.InjectedScriptExecutionTime", elapsed); | 332 UMA_HISTOGRAM_TIMES("Extensions.InjectedScriptExecutionTime", *elapsed); |
| 328 switch (run_location_) { | 333 switch (run_location_) { |
| 329 case UserScript::DOCUMENT_START: | 334 case UserScript::DOCUMENT_START: |
| 330 UMA_HISTOGRAM_TIMES( | 335 UMA_HISTOGRAM_TIMES( |
| 331 "Extensions.InjectedScriptExecutionTime.DocumentStart", elapsed); | 336 "Extensions.InjectedScriptExecutionTime.DocumentStart", *elapsed); |
| 332 break; | 337 break; |
| 333 case UserScript::DOCUMENT_END: | 338 case UserScript::DOCUMENT_END: |
| 334 UMA_HISTOGRAM_TIMES( | 339 UMA_HISTOGRAM_TIMES( |
| 335 "Extensions.InjectedScriptExecutionTime.DocumentEnd", elapsed); | 340 "Extensions.InjectedScriptExecutionTime.DocumentEnd", *elapsed); |
| 336 break; | 341 break; |
| 337 case UserScript::DOCUMENT_IDLE: | 342 case UserScript::DOCUMENT_IDLE: |
| 338 UMA_HISTOGRAM_TIMES( | 343 UMA_HISTOGRAM_TIMES( |
| 339 "Extensions.InjectedScriptExecutionTime.DocumentIdle", elapsed); | 344 "Extensions.InjectedScriptExecutionTime.DocumentIdle", *elapsed); |
| 340 break; | 345 break; |
| 341 default: | 346 default: |
| 342 break; | 347 break; |
| 343 } | 348 } |
| 344 } | 349 } |
| 345 | 350 |
| 346 bool expects_results = injector_->ExpectsResults(); | 351 bool expects_results = injector_->ExpectsResults(); |
| 347 if (expects_results) { | 352 if (expects_results) { |
| 348 if (!results.empty() && !results[0].IsEmpty()) { | 353 if (!results.empty() && !results[0].IsEmpty()) { |
| 349 // Right now, we only support returning single results (per frame). | 354 // Right now, we only support returning single results (per frame). |
| (...skipping 26 matching lines...) Expand all Loading... |
| 376 void ScriptInjection::InjectCss(std::set<std::string>* injected_stylesheets, | 381 void ScriptInjection::InjectCss(std::set<std::string>* injected_stylesheets, |
| 377 size_t* num_injected_stylesheets) { | 382 size_t* num_injected_stylesheets) { |
| 378 std::vector<blink::WebString> css_sources = injector_->GetCssSources( | 383 std::vector<blink::WebString> css_sources = injector_->GetCssSources( |
| 379 run_location_, injected_stylesheets, num_injected_stylesheets); | 384 run_location_, injected_stylesheets, num_injected_stylesheets); |
| 380 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); | 385 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); |
| 381 for (const blink::WebString& css : css_sources) | 386 for (const blink::WebString& css : css_sources) |
| 382 web_frame->document().insertStyleSheet(css); | 387 web_frame->document().insertStyleSheet(css); |
| 383 } | 388 } |
| 384 | 389 |
| 385 } // namespace extensions | 390 } // namespace extensions |
| OLD | NEW |