OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 | |
5 #include "chrome/browser/ui/webui/ntp/metrics_handler.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/metrics/histogram.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "base/values.h" | |
11 #include "chrome/browser/metrics/metric_event_duration_details.h" | |
12 #include "chrome/browser/ui/webui/chrome_web_ui.h" | |
13 #include "chrome/common/chrome_notification_types.h" | |
14 #include "content/browser/tab_contents/tab_contents.h" | |
15 #include "content/browser/user_metrics.h" | |
16 #include "content/common/notification_service.h" | |
17 | |
18 using base::ListValue; | |
19 | |
20 MetricsHandler::MetricsHandler() {} | |
21 MetricsHandler::~MetricsHandler() {} | |
22 | |
23 void MetricsHandler::RegisterMessages() { | |
24 web_ui_->RegisterMessageCallback("metricsHandler:recordAction", | |
25 NewCallback(this, &MetricsHandler::HandleRecordAction)); | |
26 web_ui_->RegisterMessageCallback("metricsHandler:recordInHistogram", | |
27 NewCallback(this, &MetricsHandler::HandleRecordInHistogram)); | |
28 web_ui_->RegisterMessageCallback("metricsHandler:logEventTime", | |
29 NewCallback(this, &MetricsHandler::HandleLogEventTime)); | |
30 } | |
31 | |
32 void MetricsHandler::HandleRecordAction(const ListValue* args) { | |
33 std::string string_action = UTF16ToUTF8(ExtractStringValue(args)); | |
34 UserMetrics::RecordComputedAction(string_action); | |
35 } | |
36 | |
37 void MetricsHandler::HandleRecordInHistogram(const ListValue* args) { | |
38 std::string histogram_name; | |
39 double value; | |
40 double boundary_value; | |
41 if (!args->GetString(0, &histogram_name) || | |
42 !args->GetDouble(1, &value) || | |
43 !args->GetDouble(2, &boundary_value)) { | |
44 NOTREACHED(); | |
45 return; | |
46 } | |
47 | |
48 int int_value = static_cast<int>(value); | |
49 int int_boundary_value = static_cast<int>(boundary_value); | |
50 if (int_boundary_value >= 4000 || | |
51 int_value > int_boundary_value || | |
52 int_value < 0) { | |
53 NOTREACHED(); | |
54 return; | |
55 } | |
56 | |
57 int bucket_count = int_boundary_value; | |
58 while (bucket_count >= 100) { | |
59 bucket_count /= 10; | |
60 } | |
61 | |
62 // As |histogram_name| may change between calls, the UMA_HISTOGRAM_ENUMERATION | |
63 // macro cannot be used here. | |
64 base::Histogram* counter = | |
65 base::LinearHistogram::FactoryGet( | |
66 histogram_name, 1, int_boundary_value, bucket_count + 1, | |
67 base::Histogram::kUmaTargetedHistogramFlag); | |
68 counter->Add(int_value); | |
69 } | |
70 | |
71 void MetricsHandler::HandleLogEventTime(const ListValue* args) { | |
72 std::string event_name = UTF16ToUTF8(ExtractStringValue(args)); | |
73 TabContents* tab = web_ui_->tab_contents(); | |
74 | |
75 // Not all new tab pages get timed. In those cases, we don't have a | |
Evan Stade
2011/10/03 21:34:22
# of spaces after .
Dan Beam
2011/10/03 22:27:03
Done. (sins of the code's forefather[s])
| |
76 // new_tab_start_time_. | |
77 if (tab->new_tab_start_time().is_null()) | |
78 return; | |
79 | |
80 base::TimeDelta duration = base::TimeTicks::Now() - tab->new_tab_start_time(); | |
81 MetricEventDurationDetails details(event_name, | |
82 static_cast<int>(duration.InMilliseconds())); | |
83 | |
84 if (event_name == "Tab.NewTabScriptStart") { | |
85 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration); | |
86 } else if (event_name == "Tab.NewTabDOMContentLoaded") { | |
87 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration); | |
88 } else if (event_name == "Tab.NewTabOnload") { | |
89 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration); | |
90 // The new tab page has finished loading; reset it. | |
91 tab->set_new_tab_start_time(base::TimeTicks()); | |
92 } else { | |
93 NOTREACHED(); | |
94 } | |
95 NotificationService::current()->Notify( | |
96 chrome::NOTIFICATION_METRIC_EVENT_DURATION, | |
97 Source<TabContents>(tab), | |
98 Details<MetricEventDurationDetails>(&details)); | |
99 } | |
OLD | NEW |