Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(680)

Side by Side Diff: chrome/browser/ui/webui/ntp/new_tab_ui.cc

Issue 8052028: NTP: Clean up of MetricsHandler class and namespacing chrome.send messages. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebasing changes Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/webui/ntp/metrics_handler.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" 7 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
8 8
9 #include <set> 9 #include <set>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/i18n/rtl.h" 14 #include "base/i18n/rtl.h"
15 #include "base/memory/singleton.h" 15 #include "base/memory/singleton.h"
16 #include "base/metrics/histogram.h" 16 #include "base/metrics/histogram.h"
17 #include "base/string_number_conversions.h" 17 #include "base/string_number_conversions.h"
18 #include "base/threading/thread.h" 18 #include "base/threading/thread.h"
19 #include "base/utf_string_conversions.h" 19 #include "base/utf_string_conversions.h"
20 #include "chrome/browser/defaults.h" 20 #include "chrome/browser/defaults.h"
21 #include "chrome/browser/metrics/metric_event_duration_details.h"
22 #include "chrome/browser/prefs/pref_service.h" 21 #include "chrome/browser/prefs/pref_service.h"
23 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/sessions/session_types.h" 23 #include "chrome/browser/sessions/session_types.h"
25 #include "chrome/browser/sync/profile_sync_service.h" 24 #include "chrome/browser/sync/profile_sync_service.h"
26 #include "chrome/browser/themes/theme_service.h" 25 #include "chrome/browser/themes/theme_service.h"
27 #include "chrome/browser/themes/theme_service_factory.h" 26 #include "chrome/browser/themes/theme_service_factory.h"
28 #include "chrome/browser/ui/browser.h" 27 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" 28 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
30 #include "chrome/browser/ui/webui/ntp/bookmarks_handler.h" 29 #include "chrome/browser/ui/webui/ntp/bookmarks_handler.h"
31 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h" 30 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h"
32 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" 31 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h"
32 #include "chrome/browser/ui/webui/ntp/metrics_handler.h"
33 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h" 33 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h"
34 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" 34 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h"
35 #include "chrome/browser/ui/webui/ntp/new_tab_page_sync_handler.h" 35 #include "chrome/browser/ui/webui/ntp/new_tab_page_sync_handler.h"
36 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h" 36 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h"
37 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" 37 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
38 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" 38 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h"
39 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h" 39 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h"
40 #include "chrome/browser/ui/webui/ntp/shown_sections_handler.h" 40 #include "chrome/browser/ui/webui/ntp/shown_sections_handler.h"
41 #include "chrome/browser/ui/webui/theme_source.h" 41 #include "chrome/browser/ui/webui/theme_source.h"
42 #include "chrome/common/chrome_notification_types.h" 42 #include "chrome/common/chrome_notification_types.h"
43 #include "chrome/common/chrome_switches.h" 43 #include "chrome/common/chrome_switches.h"
44 #include "chrome/common/extensions/extension.h" 44 #include "chrome/common/extensions/extension.h"
45 #include "chrome/common/pref_names.h" 45 #include "chrome/common/pref_names.h"
46 #include "chrome/common/url_constants.h" 46 #include "chrome/common/url_constants.h"
47 #include "content/browser/browser_thread.h" 47 #include "content/browser/browser_thread.h"
48 #include "content/browser/renderer_host/render_view_host.h" 48 #include "content/browser/renderer_host/render_view_host.h"
49 #include "content/browser/tab_contents/tab_contents.h" 49 #include "content/browser/tab_contents/tab_contents.h"
50 #include "content/browser/user_metrics.h" 50 #include "content/browser/user_metrics.h"
51 #include "content/common/notification_service.h" 51 #include "content/common/notification_service.h"
52 #include "grit/generated_resources.h" 52 #include "grit/generated_resources.h"
53 #include "grit/theme_resources.h" 53 #include "grit/theme_resources.h"
54 #include "ui/base/l10n/l10n_util.h" 54 #include "ui/base/l10n/l10n_util.h"
55 55
56 namespace { 56 namespace {
57 57
58 // The number of recent bookmarks we show.
59 const int kRecentBookmarks = 9;
60
61 // The number of search URLs to show.
62 const int kSearchURLs = 3;
63
64 // The amount of time there must be no painting for us to consider painting 58 // The amount of time there must be no painting for us to consider painting
65 // finished. Observed times are in the ~1200ms range on Windows. 59 // finished. Observed times are in the ~1200ms range on Windows.
66 const int kTimeoutMs = 2000; 60 const int kTimeoutMs = 2000;
67 61
68 // Strings sent to the page via jstemplates used to set the direction of the 62 // Strings sent to the page via jstemplates used to set the direction of the
69 // HTML document based on locale. 63 // HTML document based on locale.
70 const char kRTLHtmlTextDirection[] = "rtl"; 64 const char kRTLHtmlTextDirection[] = "rtl";
71 const char kLTRHtmlTextDirection[] = "ltr"; 65 const char kLTRHtmlTextDirection[] = "ltr";
72 66
73 ///////////////////////////////////////////////////////////////////////////////
74 // MetricsHandler
75
76 // Let the page contents record UMA actions. Only use when you can't do it from
77 // C++. For example, we currently use it to let the NTP log the postion of the
78 // Most Visited or Bookmark the user clicked on, as we don't get that
79 // information through RequestOpenURL. You will need to update the metrics
80 // dashboard with the action names you use, as our processor won't catch that
81 // information (treat it as RecordComputedMetrics)
82 class MetricsHandler : public WebUIMessageHandler {
83 public:
84 MetricsHandler() {}
85 virtual ~MetricsHandler() {}
86
87 // WebUIMessageHandler implementation.
88 virtual void RegisterMessages() OVERRIDE;
89
90 // Callback which records a user action.
91 void HandleRecordAction(const ListValue* args);
92
93 // Callback which records into a histogram. |args| contains the histogram
94 // name, the value to record, and the maximum allowed value, which can be at
95 // most 4000. The histogram will use at most 100 buckets, one for each 1,
96 // 10, or 100 different values, depending on the maximum value.
97 void HandleRecordInHistogram(const ListValue* args);
98
99 // Callback for the "logEventTime" message.
100 void HandleLogEventTime(const ListValue* args);
101
102 private:
103
104 DISALLOW_COPY_AND_ASSIGN(MetricsHandler);
105 };
106
107 void MetricsHandler::RegisterMessages() {
108 web_ui_->RegisterMessageCallback("recordAction",
109 base::Bind(&MetricsHandler::HandleRecordAction,
110 base::Unretained(this)));
111 web_ui_->RegisterMessageCallback("recordInHistogram",
112 base::Bind(&MetricsHandler::HandleRecordInHistogram,
113 base::Unretained(this)));
114 web_ui_->RegisterMessageCallback("logEventTime",
115 base::Bind(&MetricsHandler::HandleLogEventTime,
116 base::Unretained(this)));
117 }
118
119 void MetricsHandler::HandleRecordAction(const ListValue* args) {
120 std::string string_action = UTF16ToUTF8(ExtractStringValue(args));
121 UserMetrics::RecordComputedAction(string_action);
122 }
123
124 void MetricsHandler::HandleRecordInHistogram(const ListValue* args) {
125 std::string histogram_name;
126 double value;
127 double boundary_value;
128 if (!args->GetString(0, &histogram_name) ||
129 !args->GetDouble(1, &value) ||
130 !args->GetDouble(2, &boundary_value)) {
131 NOTREACHED();
132 return;
133 }
134
135 int int_value = static_cast<int>(value);
136 int int_boundary_value = static_cast<int>(boundary_value);
137 if (int_boundary_value >= 4000 ||
138 int_value > int_boundary_value ||
139 int_value < 0) {
140 NOTREACHED();
141 return;
142 }
143
144 int bucket_count = int_boundary_value;
145 while (bucket_count >= 100) {
146 bucket_count /= 10;
147 }
148
149 // As |histogram_name| may change between calls, the UMA_HISTOGRAM_ENUMERATION
150 // macro cannot be used here.
151 base::Histogram* counter =
152 base::LinearHistogram::FactoryGet(
153 histogram_name, 1, int_boundary_value, bucket_count + 1,
154 base::Histogram::kUmaTargetedHistogramFlag);
155 counter->Add(int_value);
156 }
157
158 void MetricsHandler::HandleLogEventTime(const ListValue* args) {
159 std::string event_name = UTF16ToUTF8(ExtractStringValue(args));
160 TabContents* tab = web_ui_->tab_contents();
161
162 // Not all new tab pages get timed. In those cases, we don't have a
163 // new_tab_start_time_.
164 if (tab->new_tab_start_time().is_null())
165 return;
166
167 base::TimeDelta duration = base::TimeTicks::Now() - tab->new_tab_start_time();
168 MetricEventDurationDetails details(event_name,
169 static_cast<int>(duration.InMilliseconds()));
170
171 if (event_name == "Tab.NewTabScriptStart") {
172 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
173 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
174 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
175 } else if (event_name == "Tab.NewTabOnload") {
176 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
177 // The new tab page has finished loading; reset it.
178 tab->set_new_tab_start_time(base::TimeTicks());
179 } else {
180 NOTREACHED();
181 }
182 NotificationService::current()->Notify(
183 chrome::NOTIFICATION_METRIC_EVENT_DURATION,
184 Source<TabContents>(tab),
185 Details<MetricEventDurationDetails>(&details));
186 }
187
188 } // namespace 67 } // namespace
189 68
190 /////////////////////////////////////////////////////////////////////////////// 69 ///////////////////////////////////////////////////////////////////////////////
191 // NewTabUI 70 // NewTabUI
192 71
193 NewTabUI::NewTabUI(TabContents* contents) 72 NewTabUI::NewTabUI(TabContents* contents)
194 : ChromeWebUI(contents) { 73 : ChromeWebUI(contents) {
195 // Override some options on the Web UI. 74 // Override some options on the Web UI.
196 hide_favicon_ = true; 75 hide_favicon_ = true;
197 76
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 SendResponse(request_id, html_bytes); 334 SendResponse(request_id, html_bytes);
456 } 335 }
457 336
458 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const { 337 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const {
459 return "text/html"; 338 return "text/html";
460 } 339 }
461 340
462 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { 341 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const {
463 return false; 342 return false;
464 } 343 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/ntp/metrics_handler.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698