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

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

Issue 10882024: Add webui handler for promotions on Android NTP. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebase. Created 8 years, 3 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
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/ui/webui/ntp/android/promo_handler.h"
6
7 #include "base/logging.h"
8 #include "base/memory/ref_counted_memory.h"
9 #include "base/metrics/histogram.h"
10 #include "base/string_number_conversions.h"
11 #include "base/string_util.h"
12 #include "chrome/browser/android/intent_helper.h"
13 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/signin/signin_manager.h"
17 #include "chrome/browser/sync/glue/session_model_associator.h"
18 #include "chrome/browser/sync/glue/synced_session.h"
19 #include "chrome/browser/sync/profile_sync_service.h"
20 #include "chrome/browser/sync/profile_sync_service_factory.h"
21 #include "chrome/browser/web_resource/notification_promo.h"
22 #include "chrome/browser/web_resource/notification_promo_mobile_ntp.h"
23 #include "chrome/browser/web_resource/promo_resource_service.h"
24 #include "chrome/common/chrome_notification_types.h"
25 #include "chrome/common/pref_names.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/notification_service.h"
28 #include "content/public/browser/user_metrics.h"
29 #include "content/public/browser/web_contents.h"
30
31 using content::BrowserThread;
32
33 namespace {
34
35 // Promotion impression types for the NewTabPage.MobilePromo histogram.
36 enum PromoImpressionBuckets {
37 PROMO_IMPRESSION_MOST_VISITED,
38 PROMO_IMPRESSION_OPEN_TABS,
39 PROMO_IMPRESSION_SYNC_PROMO,
40
41 PROMO_IMPRESSION_BUCKET_BOUNDARY
42 };
43
44 // Helper to record an impression in NewTabPage.MobilePromo histogram.
45 void RecordImpressionOnHistogram(PromoImpressionBuckets type) {
46 UMA_HISTOGRAM_ENUMERATION("NewTabPage.MobilePromo", type,
47 PROMO_IMPRESSION_BUCKET_BOUNDARY);
48 }
49
50 // Helper to ask whether the promo is active.
51 bool CanShowNotificationPromo(Profile* profile) {
52 NotificationPromo notification_promo(profile);
53 notification_promo.InitFromPrefs(NotificationPromo::MOBILE_NTP_SYNC_PROMO);
54 return notification_promo.CanShow();
55 }
56
57 // Helper to send out promo resource change notification.
58 void Notify(PromoHandler* ph, chrome::NotificationType notification_type) {
59 content::NotificationService* service =
60 content::NotificationService::current();
61 service->Notify(notification_type,
62 content::Source<PromoHandler>(ph),
63 content::NotificationService::NoDetails());
64 }
65
66 // Replaces all formatting markup in the promo with the corresponding HTML.
67 std::string ReplaceSimpleMarkupWithHtml(std::string text) {
68 const std::string LINE_BREAK = "<br/>";
69 const std::string SYNCGRAPHIC_IMAGE =
70 "<div class=\"promo-sync-graphic\"></div>";
71 const std::string BEGIN_HIGHLIGHT =
72 "<div style=\"text-align: center\"><button class=\"promo-button\">";
73 const std::string END_HIGHLIGHT = "</button></div>";
74 const std::string BEGIN_LINK =
75 "<span style=\"color: blue; text-decoration: underline;\">";
76 const std::string END_LINK = "</span>";
77 const std::string BEGIN_PROMO_AREA = "<div class=\"promo-action-target\">";
78 const std::string END_PROMO_AREA = "</div>";
79
80 ReplaceSubstringsAfterOffset(&text, 0, "LINE_BREAK", LINE_BREAK);
81 ReplaceSubstringsAfterOffset(
82 &text, 0, "SYNCGRAPHIC_IMAGE", SYNCGRAPHIC_IMAGE);
83 ReplaceSubstringsAfterOffset(&text, 0, "BEGIN_HIGHLIGHT", BEGIN_HIGHLIGHT);
84 ReplaceSubstringsAfterOffset(&text, 0, "END_HIGHLIGHT", END_HIGHLIGHT);
85 ReplaceSubstringsAfterOffset(&text, 0, "BEGIN_LINK", BEGIN_LINK);
86 ReplaceSubstringsAfterOffset(&text, 0, "END_LINK", END_LINK);
87 return BEGIN_PROMO_AREA + text + END_PROMO_AREA;
88 }
89
90 } // namespace
91
92 PromoHandler::PromoHandler() {
93 // Watch for pref changes that cause us to need to re-inject promolines.
94 registrar_.Add(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED,
95 content::NotificationService::AllSources());
96
97 // Watch for sync service updates that could cause re-injections
98 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE,
99 content::NotificationService::AllSources());
100 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED,
101 content::NotificationService::AllSources());
102 }
103
104 PromoHandler::~PromoHandler() {
105 }
106
107 void PromoHandler::RegisterMessages() {
108 web_ui()->RegisterMessageCallback("getPromotions",
109 base::Bind(&PromoHandler::InjectPromoDecorations,
110 base::Unretained(this)));
111 web_ui()->RegisterMessageCallback("recordImpression",
112 base::Bind(&PromoHandler::RecordImpression,
113 base::Unretained(this)));
114 web_ui()->RegisterMessageCallback("promoActionTriggered",
115 base::Bind(&PromoHandler::HandlePromoActionTriggered,
116 base::Unretained(this)));
117 web_ui()->RegisterMessageCallback("promoDisabled",
118 base::Bind(&PromoHandler::HandlePromoDisabled,
119 base::Unretained(this)));
120 }
121
122 // static
123 void PromoHandler::RegisterUserPrefs(PrefService* prefs) {
124 prefs->RegisterBooleanPref(prefs::kNtpPromoDesktopSessionFound,
125 false,
126 PrefService::UNSYNCABLE_PREF);
127 }
128
129 void PromoHandler::Observe(int type,
130 const content::NotificationSource& source,
131 const content::NotificationDetails& details) {
132 if (chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED == type ||
133 chrome::NOTIFICATION_SYNC_CONFIGURE_DONE == type ||
134 chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED == type) {
135 // A change occurred to one of the preferences we care about
136 CheckDesktopSessions();
137 InjectPromoDecorations(NULL);
138 } else {
139 NOTREACHED() << "Unknown pref changed.";
140 }
141 }
142
143 void PromoHandler::HandlePromoSendEmail(const base::ListValue* args) {
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
145 Profile* profile = Profile::FromBrowserContext(
146 web_ui()->GetWebContents()->GetBrowserContext());
147 if (!profile)
148 return;
149
150 string16 data_subject, data_body, data_inv;
151 if (!args || args->GetSize() < 3) {
152 DVLOG(1) << "promoSendEmail: expected three args, got "
153 << (args ? args->GetSize() : 0);
154 return;
155 }
156
157 args->GetString(0, &data_subject);
158 args->GetString(1, &data_body);
159 args->GetString(2, &data_inv);
160 if (data_inv.empty() || (data_subject.empty() && data_body.empty()))
161 return;
162
163 std::string data_email;
164 ProfileSyncService* service =
165 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
166 if (service && service->signin())
167 data_email = service->signin()->GetAuthenticatedUsername();
168
169 chrome::android::SendEmail(
170 UTF8ToUTF16(data_email), data_subject, data_body, data_inv);
171 RecordPromotionImpression("send_email");
172 }
173
174 void PromoHandler::HandlePromoActionTriggered(
175 const base::ListValue* /*args*/) {
176 Profile* profile = Profile::FromWebUI(web_ui());
177 if (!profile || !CanShowNotificationPromo(profile))
178 return;
179
180 NotificationPromoMobileNtp promo(profile);
181 if (!promo.InitFromPrefs())
182 return;
183
184 if (promo.action_type() == "ACTION_EMAIL")
185 HandlePromoSendEmail(promo.action_args());
186 }
187
188 void PromoHandler::HandlePromoDisabled(
189 const base::ListValue* /*args*/) {
190 Profile* profile = Profile::FromWebUI(web_ui());
191 if (!profile || !CanShowNotificationPromo(profile))
192 return;
193
194 NotificationPromo::HandleClosed(
195 profile, NotificationPromo::MOBILE_NTP_SYNC_PROMO);
196 RecordPromotionImpression("disable_promo");
197
198 content::NotificationService* service =
199 content::NotificationService::current();
200 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED,
201 content::Source<PromoHandler>(this),
202 content::NotificationService::NoDetails());
203 }
204
205 void PromoHandler::InjectPromoDecorations(
206 const base::ListValue* /*args*/) {
207 DictionaryValue result;
208 if (FetchPromotion(&result))
209 web_ui()->CallJavascriptFunction("ntp.setPromotions", result);
210 else
211 web_ui()->CallJavascriptFunction("ntp.clearPromotions");
212 }
213
214 void PromoHandler::RecordImpression(const base::ListValue* args) {
215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
216 DCHECK(args && !args->empty());
217 RecordPromotionImpression(UTF16ToASCII(ExtractStringValue(args)));
218 }
219
220 void PromoHandler::RecordPromotionImpression(const std::string& id) {
221 // Update number of views a promotion has received and trigger refresh
222 // if it exceeded max_views set for the promotion.
223 Profile* profile = Profile::FromWebUI(web_ui());
224 if (profile &&
225 NotificationPromo::HandleViewed(profile,
226 NotificationPromo::MOBILE_NTP_SYNC_PROMO)) {
227 Notify(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED);
228 }
229
230 if (id == "most_visited") {
231 RecordImpressionOnHistogram(PROMO_IMPRESSION_MOST_VISITED);
232 } else if (id == "open_tabs") {
233 RecordImpressionOnHistogram(PROMO_IMPRESSION_OPEN_TABS);
234 } else if (id == "sync_promo") {
235 RecordImpressionOnHistogram(PROMO_IMPRESSION_SYNC_PROMO);
236 } else if (id == "send_email") {
237 content::RecordAction(
238 content::UserMetricsAction("MobilePromoEmailLinkPressed"));
239 } else if (id == "disable_promo") {
240 content::RecordAction(
241 content::UserMetricsAction("MobilePromoClosePressedOnOpenTabs"));
Evan Stade 2012/08/29 22:17:06 we actually still use histograms for actions most
aruslan 2012/08/29 22:59:27 Done -- Ilya helped a lot; I should have had talke
242 } else {
243 NOTREACHED() << "Unknown promotion impression: " << id;
244 }
245 }
246
247 bool PromoHandler::FetchPromotion(DictionaryValue* result) {
248 DCHECK(result != NULL);
249 Profile* profile = Profile::FromWebUI(web_ui());
250 if (!profile || !CanShowNotificationPromo(profile))
251 return false;
252
253 NotificationPromoMobileNtp promo(profile);
254 if (!promo.InitFromPrefs())
255 return false;
256
257 DCHECK(!promo.text().empty());
258 if (!DoesChromePromoMatchCurrentSync(
259 promo.requires_sync(), promo.requires_mobile_only_sync())) {
260 return false;
261 }
262
263 result->SetBoolean("promoIsAllowed", true);
264 result->SetBoolean("promoIsAllowedOnMostVisited",
265 promo.show_on_most_visited());
266 result->SetBoolean("promoIsAllowedOnOpenTabs", promo.show_on_open_tabs());
267 result->SetBoolean("promoIsAllowedAsVC", promo.show_as_virtual_computer());
268 result->SetString("promoVCTitle", promo.virtual_computer_title());
269 result->SetString("promoVCLastSynced", promo.virtual_computer_lastsync());
270 result->SetString("promoMessage", ReplaceSimpleMarkupWithHtml(promo.text()));
271 result->SetString("promoMessageLong",
272 ReplaceSimpleMarkupWithHtml(promo.text_long()));
273 return true;
274 }
275
276 bool PromoHandler::DoesChromePromoMatchCurrentSync(
277 bool promo_requires_sync,
278 bool promo_requires_no_active_desktop_sync_sessions) {
279 Profile* profile = Profile::FromWebUI(web_ui());
280 if (!profile)
281 return false;
282
283 // If the promo doesn't require any sync, the requirements are fulfilled.
284 if (!promo_requires_sync)
285 return true;
286
287 // The promo requires the sync; check that the sync service is active.
288 ProfileSyncService* service =
289 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
290 if (!service || !service->ShouldPushChanges())
291 return false;
292
293 // If the promo doesn't have specific requirements for the sync, it matches.
294 if (!promo_requires_no_active_desktop_sync_sessions)
295 return true;
296
297 // If the promo requires mobile-only sync,
298 // check that no desktop sessions are found.
299 PrefService* prefs = profile->GetPrefs();
300 return !prefs || !prefs->GetBoolean(prefs::kNtpPromoDesktopSessionFound);
301 }
302
303 void PromoHandler::CheckDesktopSessions() {
304 Profile* profile = Profile::FromWebUI(web_ui());
305 if (!profile)
306 return;
307
308 // Check if desktop sessions have already been found.
309 PrefService* prefs = profile->GetPrefs();
310 if (!prefs || prefs->GetBoolean(prefs::kNtpPromoDesktopSessionFound))
311 return;
312
313 // Check if the sync is currently active.
314 ProfileSyncService* service =
315 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
316 if (!service || !service->ShouldPushChanges())
317 return;
318
319 // Check if the sync has any open sessions.
320 browser_sync::SessionModelAssociator* associator =
321 service->GetSessionModelAssociator();
322 if (!associator)
323 return;
324
325 // Let's see if there are no desktop sessions.
326 std::vector<const browser_sync::SyncedSession*> sessions;
327 ListValue session_list;
328 if (!associator->GetAllForeignSessions(&sessions))
329 return;
330
331 for (size_t i = 0; i < sessions.size(); ++i) {
332 const browser_sync::SyncedSession::DeviceType device_type =
333 sessions[i]->device_type;
334 if (device_type == browser_sync::SyncedSession::TYPE_WIN ||
335 device_type == browser_sync::SyncedSession::TYPE_MACOSX ||
336 device_type == browser_sync::SyncedSession::TYPE_LINUX) {
337 // Found a desktop session: write out the pref.
338 prefs->SetBoolean(prefs::kNtpPromoDesktopSessionFound, true);
339 return;
340 }
341 }
342 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698