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/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/web_contents.h" |
| 29 |
| 30 using content::BrowserThread; |
| 31 |
| 32 namespace { |
| 33 |
| 34 // Promotion impression types for the NewTabPage.MobilePromo histogram. |
| 35 enum PromoImpressionBuckets { |
| 36 PROMO_IMPRESSION_MOST_VISITED, |
| 37 PROMO_IMPRESSION_OPEN_TABS, |
| 38 PROMO_IMPRESSION_SYNC_PROMO, |
| 39 PROMO_IMPRESSION_SEND_EMAIL_CLICKED, |
| 40 PROMO_IMPRESSION_CLOSE_PROMO_CLICKED, |
| 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::HandleGetPromotions, |
| 110 base::Unretained(this))); |
| 111 web_ui()->RegisterMessageCallback("recordImpression", |
| 112 base::Bind(&PromoHandler::HandleRecordImpression, |
| 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(); |
| 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 RecordImpressionOnHistogram(PROMO_IMPRESSION_SEND_EMAIL_CLICKED); |
| 172 } |
| 173 |
| 174 void PromoHandler::HandlePromoActionTriggered(const base::ListValue* /*args*/) { |
| 175 Profile* profile = Profile::FromWebUI(web_ui()); |
| 176 if (!profile || !CanShowNotificationPromo(profile)) |
| 177 return; |
| 178 |
| 179 NotificationPromoMobileNtp promo(profile); |
| 180 if (!promo.InitFromPrefs()) |
| 181 return; |
| 182 |
| 183 if (promo.action_type() == "ACTION_EMAIL") |
| 184 HandlePromoSendEmail(promo.action_args()); |
| 185 } |
| 186 |
| 187 void PromoHandler::HandlePromoDisabled(const base::ListValue* /*args*/) { |
| 188 Profile* profile = Profile::FromWebUI(web_ui()); |
| 189 if (!profile || !CanShowNotificationPromo(profile)) |
| 190 return; |
| 191 |
| 192 NotificationPromo::HandleClosed( |
| 193 profile, NotificationPromo::MOBILE_NTP_SYNC_PROMO); |
| 194 RecordImpressionOnHistogram(PROMO_IMPRESSION_CLOSE_PROMO_CLICKED); |
| 195 |
| 196 content::NotificationService* service = |
| 197 content::NotificationService::current(); |
| 198 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, |
| 199 content::Source<PromoHandler>(this), |
| 200 content::NotificationService::NoDetails()); |
| 201 } |
| 202 |
| 203 void PromoHandler::HandleGetPromotions(const base::ListValue* /*args*/) { |
| 204 CheckDesktopSessions(); |
| 205 InjectPromoDecorations(); |
| 206 } |
| 207 |
| 208 void PromoHandler::HandleRecordImpression(const base::ListValue* args) { |
| 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 210 DCHECK(args && !args->empty()); |
| 211 RecordPromotionImpression(UTF16ToASCII(ExtractStringValue(args))); |
| 212 } |
| 213 |
| 214 void PromoHandler::InjectPromoDecorations() { |
| 215 DictionaryValue result; |
| 216 if (FetchPromotion(&result)) |
| 217 web_ui()->CallJavascriptFunction("ntp.setPromotions", result); |
| 218 else |
| 219 web_ui()->CallJavascriptFunction("ntp.clearPromotions"); |
| 220 } |
| 221 |
| 222 void PromoHandler::RecordPromotionImpression(const std::string& id) { |
| 223 // Update number of views a promotion has received and trigger refresh |
| 224 // if it exceeded max_views set for the promotion. |
| 225 Profile* profile = Profile::FromWebUI(web_ui()); |
| 226 if (profile && |
| 227 NotificationPromo::HandleViewed(profile, |
| 228 NotificationPromo::MOBILE_NTP_SYNC_PROMO)) { |
| 229 Notify(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED); |
| 230 } |
| 231 |
| 232 if (id == "most_visited") |
| 233 RecordImpressionOnHistogram(PROMO_IMPRESSION_MOST_VISITED); |
| 234 else if (id == "open_tabs") |
| 235 RecordImpressionOnHistogram(PROMO_IMPRESSION_OPEN_TABS); |
| 236 else if (id == "sync_promo") |
| 237 RecordImpressionOnHistogram(PROMO_IMPRESSION_SYNC_PROMO); |
| 238 else |
| 239 NOTREACHED() << "Unknown promotion impression: " << id; |
| 240 } |
| 241 |
| 242 bool PromoHandler::FetchPromotion(DictionaryValue* result) { |
| 243 DCHECK(result != NULL); |
| 244 Profile* profile = Profile::FromWebUI(web_ui()); |
| 245 if (!profile || !CanShowNotificationPromo(profile)) |
| 246 return false; |
| 247 |
| 248 NotificationPromoMobileNtp promo(profile); |
| 249 if (!promo.InitFromPrefs()) |
| 250 return false; |
| 251 |
| 252 DCHECK(!promo.text().empty()); |
| 253 if (!DoesChromePromoMatchCurrentSync( |
| 254 promo.requires_sync(), promo.requires_mobile_only_sync())) { |
| 255 return false; |
| 256 } |
| 257 |
| 258 result->SetBoolean("promoIsAllowed", true); |
| 259 result->SetBoolean("promoIsAllowedOnMostVisited", |
| 260 promo.show_on_most_visited()); |
| 261 result->SetBoolean("promoIsAllowedOnOpenTabs", promo.show_on_open_tabs()); |
| 262 result->SetBoolean("promoIsAllowedAsVC", promo.show_as_virtual_computer()); |
| 263 result->SetString("promoVCTitle", promo.virtual_computer_title()); |
| 264 result->SetString("promoVCLastSynced", promo.virtual_computer_lastsync()); |
| 265 result->SetString("promoMessage", ReplaceSimpleMarkupWithHtml(promo.text())); |
| 266 result->SetString("promoMessageLong", |
| 267 ReplaceSimpleMarkupWithHtml(promo.text_long())); |
| 268 return true; |
| 269 } |
| 270 |
| 271 bool PromoHandler::DoesChromePromoMatchCurrentSync( |
| 272 bool promo_requires_sync, |
| 273 bool promo_requires_no_active_desktop_sync_sessions) { |
| 274 Profile* profile = Profile::FromWebUI(web_ui()); |
| 275 if (!profile) |
| 276 return false; |
| 277 |
| 278 // If the promo doesn't require any sync, the requirements are fulfilled. |
| 279 if (!promo_requires_sync) |
| 280 return true; |
| 281 |
| 282 // The promo requires the sync; check that the sync service is active. |
| 283 ProfileSyncService* service = |
| 284 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
| 285 if (!service || !service->ShouldPushChanges()) |
| 286 return false; |
| 287 |
| 288 // If the promo doesn't have specific requirements for the sync, it matches. |
| 289 if (!promo_requires_no_active_desktop_sync_sessions) |
| 290 return true; |
| 291 |
| 292 // If the promo requires mobile-only sync, |
| 293 // check that no desktop sessions are found. |
| 294 PrefService* prefs = profile->GetPrefs(); |
| 295 return !prefs || !prefs->GetBoolean(prefs::kNtpPromoDesktopSessionFound); |
| 296 } |
| 297 |
| 298 void PromoHandler::CheckDesktopSessions() { |
| 299 Profile* profile = Profile::FromWebUI(web_ui()); |
| 300 if (!profile) |
| 301 return; |
| 302 |
| 303 // Check if desktop sessions have already been found. |
| 304 PrefService* prefs = profile->GetPrefs(); |
| 305 if (!prefs || prefs->GetBoolean(prefs::kNtpPromoDesktopSessionFound)) |
| 306 return; |
| 307 |
| 308 // Check if the sync is currently active. |
| 309 ProfileSyncService* service = |
| 310 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
| 311 if (!service || !service->ShouldPushChanges()) |
| 312 return; |
| 313 |
| 314 // Check if the sync has any open sessions. |
| 315 browser_sync::SessionModelAssociator* associator = |
| 316 service->GetSessionModelAssociator(); |
| 317 if (!associator) |
| 318 return; |
| 319 |
| 320 // Let's see if there are no desktop sessions. |
| 321 std::vector<const browser_sync::SyncedSession*> sessions; |
| 322 ListValue session_list; |
| 323 if (!associator->GetAllForeignSessions(&sessions)) |
| 324 return; |
| 325 |
| 326 for (size_t i = 0; i < sessions.size(); ++i) { |
| 327 const browser_sync::SyncedSession::DeviceType device_type = |
| 328 sessions[i]->device_type; |
| 329 if (device_type == browser_sync::SyncedSession::TYPE_WIN || |
| 330 device_type == browser_sync::SyncedSession::TYPE_MACOSX || |
| 331 device_type == browser_sync::SyncedSession::TYPE_LINUX) { |
| 332 // Found a desktop session: write out the pref. |
| 333 prefs->SetBoolean(prefs::kNtpPromoDesktopSessionFound, true); |
| 334 return; |
| 335 } |
| 336 } |
| 337 } |
OLD | NEW |