[Extensions] Handle some funny cases in script injection
[chromium-blink-merge.git] / extensions / renderer / scripts_run_info.cc
blobb31bbe07ba12ac24c56aa22a65fa791260a7d7c9
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_frame.h"
9 #include "content/public/renderer/render_thread.h"
10 #include "extensions/common/extension_messages.h"
11 #include "extensions/renderer/script_context.h"
12 #include "third_party/WebKit/public/web/WebLocalFrame.h"
14 namespace extensions {
16 ScriptsRunInfo::ScriptsRunInfo(content::RenderFrame* render_frame,
17 UserScript::RunLocation location)
18 : num_css(0u),
19 num_js(0u),
20 num_blocking_js(0u),
21 routing_id_(render_frame->GetRoutingID()),
22 run_location_(location),
23 frame_url_(ScriptContext::GetDataSourceURLForFrame(
24 render_frame->GetWebFrame())) {
27 ScriptsRunInfo::~ScriptsRunInfo() {
30 void ScriptsRunInfo::LogRun() {
31 // Notify the browser if any extensions are now executing scripts.
32 if (!executing_scripts.empty()) {
33 content::RenderThread::Get()->Send(
34 new ExtensionHostMsg_ContentScriptsExecuting(
35 routing_id_, executing_scripts, frame_url_));
38 switch (run_location_) {
39 case UserScript::DOCUMENT_START:
40 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", num_css);
41 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", num_js);
42 if (num_blocking_js) {
43 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_BlockingScriptCount",
44 num_blocking_js);
45 } else if (num_css || num_js) {
46 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", timer.Elapsed());
48 break;
49 case UserScript::DOCUMENT_END:
50 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", num_js);
51 if (num_blocking_js) {
52 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_BlockingScriptCount",
53 num_blocking_js);
54 } else if (num_js) {
55 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", timer.Elapsed());
57 break;
58 case UserScript::DOCUMENT_IDLE:
59 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", num_js);
60 if (num_blocking_js) {
61 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_BlockingScriptCount",
62 num_blocking_js);
63 } else if (num_js) {
64 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", timer.Elapsed());
66 break;
67 case UserScript::RUN_DEFERRED:
68 case UserScript::BROWSER_DRIVEN:
69 // TODO(rdevlin.cronin): Add histograms.
70 break;
71 case UserScript::UNDEFINED:
72 case UserScript::RUN_LOCATION_LAST:
73 NOTREACHED();
77 } // namespace extensions