Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / ntp / new_tab_page_handler.cc
blobb914a5cc4694552e16b56606e7dd02937cd40944
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/ntp/new_tab_page_handler.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/metrics/histogram.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/sync/profile_sync_service.h"
15 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
16 #include "chrome/browser/web_resource/notification_promo_helper.h"
17 #include "chrome/common/pref_names.h"
18 #include "components/pref_registry/pref_registry_syncable.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/web_ui.h"
22 namespace {
24 const char kDefaultPageTypeHistogram[] = "NewTabPage.DefaultPageType";
26 enum PromoAction {
27 PROMO_VIEWED = 0,
28 PROMO_CLOSED,
29 PROMO_LINK_CLICKED,
30 PROMO_ACTION_MAX,
33 } // namespace
35 NewTabPageHandler::NewTabPageHandler() : page_switch_count_(0) {
38 NewTabPageHandler::~NewTabPageHandler() {
39 LOCAL_HISTOGRAM_COUNTS_100("NewTabPage.SingleSessionPageSwitches",
40 page_switch_count_);
43 void NewTabPageHandler::RegisterMessages() {
44 // Record an open of the NTP with its default page type.
45 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
46 int shown_page_type = prefs->GetInteger(prefs::kNtpShownPage) >>
47 kPageIdOffset;
48 UMA_HISTOGRAM_ENUMERATION(kDefaultPageTypeHistogram,
49 shown_page_type, kHistogramEnumerationMax);
51 web_ui()->RegisterMessageCallback("notificationPromoClosed",
52 base::Bind(&NewTabPageHandler::HandleNotificationPromoClosed,
53 base::Unretained(this)));
54 web_ui()->RegisterMessageCallback("notificationPromoViewed",
55 base::Bind(&NewTabPageHandler::HandleNotificationPromoViewed,
56 base::Unretained(this)));
57 web_ui()->RegisterMessageCallback("notificationPromoLinkClicked",
58 base::Bind(&NewTabPageHandler::HandleNotificationPromoLinkClicked,
59 base::Unretained(this)));
60 web_ui()->RegisterMessageCallback("bubblePromoClosed",
61 base::Bind(&NewTabPageHandler::HandleBubblePromoClosed,
62 base::Unretained(this)));
63 web_ui()->RegisterMessageCallback("bubblePromoViewed",
64 base::Bind(&NewTabPageHandler::HandleBubblePromoViewed,
65 base::Unretained(this)));
66 web_ui()->RegisterMessageCallback("bubblePromoLinkClicked",
67 base::Bind(&NewTabPageHandler::HandleBubblePromoLinkClicked,
68 base::Unretained(this)));
69 web_ui()->RegisterMessageCallback("pageSelected",
70 base::Bind(&NewTabPageHandler::HandlePageSelected,
71 base::Unretained(this)));
74 void NewTabPageHandler::HandleNotificationPromoClosed(
75 const base::ListValue* args) {
76 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Promo.Notification",
77 PROMO_CLOSED, PROMO_ACTION_MAX);
78 web_resource::HandleNotificationPromoClosed(
79 web_resource::NotificationPromo::NTP_NOTIFICATION_PROMO);
80 Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED);
83 void NewTabPageHandler::HandleNotificationPromoViewed(
84 const base::ListValue* args) {
85 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Promo.Notification",
86 PROMO_VIEWED, PROMO_ACTION_MAX);
87 if (web_resource::HandleNotificationPromoViewed(
88 web_resource::NotificationPromo::NTP_NOTIFICATION_PROMO)) {
89 Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED);
93 void NewTabPageHandler::HandleNotificationPromoLinkClicked(
94 const base::ListValue* args) {
95 DVLOG(1) << "HandleNotificationPromoLinkClicked";
96 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Promo.Notification",
97 PROMO_LINK_CLICKED, PROMO_ACTION_MAX);
100 void NewTabPageHandler::HandleBubblePromoClosed(const base::ListValue* args) {
101 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Promo.Bubble",
102 PROMO_CLOSED, PROMO_ACTION_MAX);
103 web_resource::HandleNotificationPromoClosed(
104 web_resource::NotificationPromo::NTP_BUBBLE_PROMO);
105 Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED);
108 void NewTabPageHandler::HandleBubblePromoViewed(const base::ListValue* args) {
109 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Promo.Bubble",
110 PROMO_VIEWED, PROMO_ACTION_MAX);
111 if (web_resource::HandleNotificationPromoViewed(
112 web_resource::NotificationPromo::NTP_BUBBLE_PROMO))
113 Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED);
116 void NewTabPageHandler::HandleBubblePromoLinkClicked(
117 const base::ListValue* args) {
118 DVLOG(1) << "HandleBubblePromoLinkClicked";
119 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Promo.Bubble",
120 PROMO_LINK_CLICKED, PROMO_ACTION_MAX);
123 void NewTabPageHandler::HandlePageSelected(const base::ListValue* args) {
124 page_switch_count_++;
126 double page_id_double;
127 CHECK(args->GetDouble(0, &page_id_double));
128 int page_id = static_cast<int>(page_id_double);
130 double index_double;
131 CHECK(args->GetDouble(1, &index_double));
132 int index = static_cast<int>(index_double);
134 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
135 int previous_shown_page =
136 prefs->GetInteger(prefs::kNtpShownPage) >> kPageIdOffset;
137 UMA_HISTOGRAM_ENUMERATION("NewTabPage.PreviousSelectedPageType",
138 previous_shown_page, kHistogramEnumerationMax);
140 prefs->SetInteger(prefs::kNtpShownPage, page_id | index);
142 int shown_page_type = page_id >> kPageIdOffset;
143 UMA_HISTOGRAM_ENUMERATION("NewTabPage.SelectedPageType",
144 shown_page_type, kHistogramEnumerationMax);
147 // static
148 void NewTabPageHandler::RegisterProfilePrefs(
149 user_prefs::PrefRegistrySyncable* registry) {
150 // TODO(estade): should be syncable.
151 registry->RegisterIntegerPref(prefs::kNtpShownPage, APPS_PAGE_ID);
154 // static
155 void NewTabPageHandler::GetLocalizedValues(Profile* profile,
156 base::DictionaryValue* values) {
157 values->SetInteger("apps_page_id", APPS_PAGE_ID);
159 PrefService* prefs = profile->GetPrefs();
160 int shown_page = prefs->GetInteger(prefs::kNtpShownPage);
161 values->SetInteger("shown_page_type", shown_page & ~INDEX_MASK);
162 values->SetInteger("shown_page_index", shown_page & INDEX_MASK);
165 void NewTabPageHandler::Notify(chrome::NotificationType notification_type) {
166 content::NotificationService* service =
167 content::NotificationService::current();
168 service->Notify(notification_type,
169 content::Source<NewTabPageHandler>(this),
170 content::NotificationService::NoDetails());