Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / renderer / scripts_run_info.cc
blobf5a85d1d1e425c75433f0b4ff260f8b9e58b295b
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.
5 #include "extensions/renderer/scripts_run_info.h"
7 #include "base/metrics/histogram.h"
8 #include "content/public/renderer/render_view.h"
9 #include "extensions/common/extension_messages.h"
10 #include "extensions/renderer/script_context.h"
11 #include "third_party/WebKit/public/web/WebFrame.h"
13 namespace extensions {
15 ScriptsRunInfo::ScriptsRunInfo()
16 : num_css(0u), num_js(0u), num_blocking_js(0u) {
19 ScriptsRunInfo::~ScriptsRunInfo() {
22 void ScriptsRunInfo::LogRun(blink::WebFrame* frame,
23 UserScript::RunLocation location) {
24 // Notify the browser if any extensions are now executing scripts.
25 if (!executing_scripts.empty()) {
26 content::RenderView* render_view =
27 content::RenderView::FromWebView(frame->view());
28 render_view->Send(new ExtensionHostMsg_ContentScriptsExecuting(
29 render_view->GetRoutingID(),
30 executing_scripts,
31 ScriptContext::GetDataSourceURLForFrame(frame)));
34 switch (location) {
35 case UserScript::DOCUMENT_START:
36 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css);
37 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_js);
38 if (num_blocking_js) {
39 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_BlockingScriptCount",
40 num_blocking_js);
41 } else if (num_css || num_js) {
42 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed());
44 break;
45 case UserScript::DOCUMENT_END:
46 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_js);
47 if (num_blocking_js) {
48 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_BlockingScriptCount",
49 num_blocking_js);
50 } else if (num_js) {
51 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed());
53 break;
54 case UserScript::DOCUMENT_IDLE:
55 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_js);
56 if (num_blocking_js) {
57 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_BlockingScriptCount",
58 num_blocking_js);
59 } else if (num_js) {
60 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed());
62 break;
63 case UserScript::RUN_DEFERRED:
64 case UserScript::BROWSER_DRIVEN:
65 // TODO(rdevlin.cronin): Add histograms.
66 break;
67 case UserScript::UNDEFINED:
68 case UserScript::RUN_LOCATION_LAST:
69 NOTREACHED();
73 } // namespace extensions