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