scripts_run_info.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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
  3. // found in the LICENSE file.
  4. #include "extensions/renderer/scripts_run_info.h"
  5. #include "base/metrics/histogram_macros.h"
  6. #include "content/public/renderer/render_frame.h"
  7. #include "content/public/renderer/render_thread.h"
  8. #include "extensions/common/extension_messages.h"
  9. #include "extensions/renderer/script_context.h"
  10. #include "third_party/blink/public/web/web_local_frame.h"
  11. namespace extensions {
  12. ScriptsRunInfo::ScriptsRunInfo(content::RenderFrame* render_frame,
  13. mojom::RunLocation location)
  14. : num_css(0u),
  15. num_js(0u),
  16. num_blocking_js(0u),
  17. routing_id_(render_frame->GetRoutingID()),
  18. run_location_(location),
  19. frame_url_(ScriptContext::GetDocumentLoaderURLForFrame(
  20. render_frame->GetWebFrame())) {}
  21. ScriptsRunInfo::~ScriptsRunInfo() {
  22. }
  23. void ScriptsRunInfo::LogRun(bool send_script_activity) {
  24. // Notify the browser if any extensions are now executing scripts.
  25. if (!executing_scripts.empty() && send_script_activity) {
  26. content::RenderThread::Get()->Send(
  27. new ExtensionHostMsg_ContentScriptsExecuting(
  28. routing_id_, executing_scripts, frame_url_));
  29. }
  30. base::TimeDelta elapsed = timer.Elapsed();
  31. switch (run_location_) {
  32. case mojom::RunLocation::kDocumentStart:
  33. UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css);
  34. UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_js);
  35. if (num_blocking_js) {
  36. UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_BlockingScriptCount",
  37. num_blocking_js);
  38. } else if (num_css || num_js) {
  39. UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", elapsed);
  40. }
  41. break;
  42. case mojom::RunLocation::kDocumentEnd:
  43. UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_js);
  44. if (num_blocking_js) {
  45. UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_BlockingScriptCount",
  46. num_blocking_js);
  47. } else if (num_js) {
  48. UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", elapsed);
  49. }
  50. break;
  51. case mojom::RunLocation::kDocumentIdle:
  52. UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_js);
  53. if (num_blocking_js) {
  54. UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_BlockingScriptCount",
  55. num_blocking_js);
  56. } else if (num_js) {
  57. UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", elapsed);
  58. }
  59. break;
  60. case mojom::RunLocation::kRunDeferred:
  61. case mojom::RunLocation::kBrowserDriven:
  62. // TODO(rdevlin.cronin): Add histograms.
  63. break;
  64. case mojom::RunLocation::kUndefined:
  65. NOTREACHED();
  66. }
  67. }
  68. } // namespace extensions