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