Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / metrics_handler.cc
blob41a6c089deae476f754162e9a0ac0375d734c1bc
1 // Copyright (c) 2012 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 "chrome/browser/ui/webui/metrics_handler.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/logging.h"
10 #include "base/metrics/histogram.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
14 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h"
15 #include "chrome/common/ntp_logging_events.h"
16 #include "content/public/browser/user_metrics.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_ui.h"
20 using base::ListValue;
21 using base::UserMetricsAction;
22 using content::WebContents;
24 MetricsHandler::MetricsHandler() {}
25 MetricsHandler::~MetricsHandler() {}
27 void MetricsHandler::RegisterMessages() {
28 web_ui()->RegisterMessageCallback(
29 "metricsHandler:recordAction",
30 base::Bind(&MetricsHandler::HandleRecordAction, base::Unretained(this)));
31 web_ui()->RegisterMessageCallback(
32 "metricsHandler:recordInHistogram",
33 base::Bind(&MetricsHandler::HandleRecordInHistogram,
34 base::Unretained(this)));
35 web_ui()->RegisterMessageCallback(
36 "metricsHandler:logEventTime",
37 base::Bind(&MetricsHandler::HandleLogEventTime, base::Unretained(this)));
38 web_ui()->RegisterMessageCallback(
39 "metricsHandler:logMouseover",
40 base::Bind(&MetricsHandler::HandleLogMouseover, base::Unretained(this)));
43 void MetricsHandler::HandleRecordAction(const base::ListValue* args) {
44 std::string string_action = base::UTF16ToUTF8(ExtractStringValue(args));
45 content::RecordComputedAction(string_action);
48 void MetricsHandler::HandleRecordInHistogram(const base::ListValue* args) {
49 std::string histogram_name;
50 double value;
51 double boundary_value;
52 if (!args->GetString(0, &histogram_name) ||
53 !args->GetDouble(1, &value) ||
54 !args->GetDouble(2, &boundary_value)) {
55 NOTREACHED();
56 return;
59 int int_value = static_cast<int>(value);
60 int int_boundary_value = static_cast<int>(boundary_value);
61 if (int_boundary_value >= 4000 ||
62 int_value > int_boundary_value ||
63 int_value < 0) {
64 NOTREACHED();
65 return;
68 int bucket_count = int_boundary_value;
69 while (bucket_count >= 100) {
70 bucket_count /= 10;
73 // As |histogram_name| may change between calls, the UMA_HISTOGRAM_ENUMERATION
74 // macro cannot be used here.
75 base::HistogramBase* counter =
76 base::LinearHistogram::FactoryGet(
77 histogram_name, 1, int_boundary_value, bucket_count + 1,
78 base::HistogramBase::kUmaTargetedHistogramFlag);
79 counter->Add(int_value);
82 void MetricsHandler::HandleLogEventTime(const base::ListValue* args) {
83 std::string event_name = base::UTF16ToUTF8(ExtractStringValue(args));
84 WebContents* tab = web_ui()->GetWebContents();
86 // Not all new tab pages get timed. In those cases, we don't have a
87 // new_tab_start_time_.
88 CoreTabHelper* core_tab_helper = CoreTabHelper::FromWebContents(tab);
89 if (core_tab_helper->new_tab_start_time().is_null())
90 return;
92 base::TimeDelta duration =
93 base::TimeTicks::Now() - core_tab_helper->new_tab_start_time();
95 if (event_name == "Tab.NewTabScriptStart") {
96 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
97 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
98 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
99 } else if (event_name == "Tab.NewTabOnload") {
100 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
101 // The new tab page has finished loading; reset it.
102 CoreTabHelper* core_tab_helper = CoreTabHelper::FromWebContents(tab);
103 core_tab_helper->set_new_tab_start_time(base::TimeTicks());
104 } else {
105 NOTREACHED();
109 void MetricsHandler::HandleLogMouseover(const base::ListValue* args) {
110 #if !defined(OS_ANDROID)
111 // Android uses native UI for NTP.
112 NTPUserDataLogger::GetOrCreateFromWebContents(
113 web_ui()->GetWebContents())->LogEvent(NTP_MOUSEOVER,
114 base::TimeDelta::FromMilliseconds(0));
115 #endif // !defined(OS_ANDROID)