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

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

Issue 11689004: Move PromoResourceService from Profile to BrowserProcessImpl/local_state(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: android x 4 Created 7 years, 12 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/ntp/android/promo_handler.h" 5 #include "chrome/browser/ui/webui/ntp/android/promo_handler.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/ref_counted_memory.h" 8 #include "base/memory/ref_counted_memory.h"
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/android/intent_helper.h" 13 #include "chrome/browser/android/intent_helper.h"
14 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/prefs/pref_service.h" 15 #include "chrome/browser/prefs/pref_service.h"
15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h" 17 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/signin/signin_manager.h" 18 #include "chrome/browser/signin/signin_manager.h"
18 #include "chrome/browser/sync/glue/session_model_associator.h" 19 #include "chrome/browser/sync/glue/session_model_associator.h"
19 #include "chrome/browser/sync/glue/synced_session.h" 20 #include "chrome/browser/sync/glue/synced_session.h"
20 #include "chrome/browser/sync/profile_sync_service.h" 21 #include "chrome/browser/sync/profile_sync_service.h"
21 #include "chrome/browser/sync/profile_sync_service_factory.h" 22 #include "chrome/browser/sync/profile_sync_service_factory.h"
22 #include "chrome/browser/web_resource/notification_promo.h" 23 #include "chrome/browser/web_resource/notification_promo.h"
23 #include "chrome/browser/web_resource/notification_promo_mobile_ntp.h" 24 #include "chrome/browser/web_resource/notification_promo_mobile_ntp.h"
(...skipping 19 matching lines...) Expand all
43 PROMO_IMPRESSION_BUCKET_BOUNDARY = 5 44 PROMO_IMPRESSION_BUCKET_BOUNDARY = 5
44 }; 45 };
45 46
46 // Helper to record an impression in NewTabPage.MobilePromo histogram. 47 // Helper to record an impression in NewTabPage.MobilePromo histogram.
47 void RecordImpressionOnHistogram(PromoImpressionBuckets type) { 48 void RecordImpressionOnHistogram(PromoImpressionBuckets type) {
48 UMA_HISTOGRAM_ENUMERATION("NewTabPage.MobilePromo", type, 49 UMA_HISTOGRAM_ENUMERATION("NewTabPage.MobilePromo", type,
49 PROMO_IMPRESSION_BUCKET_BOUNDARY); 50 PROMO_IMPRESSION_BUCKET_BOUNDARY);
50 } 51 }
51 52
52 // Helper to ask whether the promo is active. 53 // Helper to ask whether the promo is active.
53 bool CanShowNotificationPromo(Profile* profile) { 54 bool CanShowNotificationPromo(PrefServiceSimple* prefs) {
54 NotificationPromo notification_promo(profile); 55 NotificationPromo notification_promo(prefs);
55 notification_promo.InitFromPrefs(NotificationPromo::MOBILE_NTP_SYNC_PROMO); 56 notification_promo.InitFromPrefs(NotificationPromo::MOBILE_NTP_SYNC_PROMO);
56 return notification_promo.CanShow(); 57 return notification_promo.CanShow();
57 } 58 }
58 59
59 // Helper to send out promo resource change notification. 60 // Helper to send out promo resource change notification.
60 void Notify(PromoHandler* ph, chrome::NotificationType notification_type) { 61 void Notify(PromoHandler* ph, chrome::NotificationType notification_type) {
61 content::NotificationService* service = 62 content::NotificationService* service =
62 content::NotificationService::current(); 63 content::NotificationService::current();
63 service->Notify(notification_type, 64 service->Notify(notification_type,
64 content::Source<PromoHandler>(ph), 65 content::Source<PromoHandler>(ph),
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 if (service && service->signin()) 169 if (service && service->signin())
169 data_email = service->signin()->GetAuthenticatedUsername(); 170 data_email = service->signin()->GetAuthenticatedUsername();
170 171
171 chrome::android::SendEmail( 172 chrome::android::SendEmail(
172 UTF8ToUTF16(data_email), data_subject, data_body, data_inv, 173 UTF8ToUTF16(data_email), data_subject, data_body, data_inv,
173 EmptyString16()); 174 EmptyString16());
174 RecordImpressionOnHistogram(PROMO_IMPRESSION_SEND_EMAIL_CLICKED); 175 RecordImpressionOnHistogram(PROMO_IMPRESSION_SEND_EMAIL_CLICKED);
175 } 176 }
176 177
177 void PromoHandler::HandlePromoActionTriggered(const base::ListValue* /*args*/) { 178 void PromoHandler::HandlePromoActionTriggered(const base::ListValue* /*args*/) {
178 Profile* profile = Profile::FromWebUI(web_ui()); 179 if (!CanShowNotificationPromo(g_browser_process->local_state()))
179 if (!profile || !CanShowNotificationPromo(profile))
180 return; 180 return;
181 181
182 NotificationPromoMobileNtp promo(profile); 182 NotificationPromoMobileNtp promo(g_browser_process->local_state());
183 if (!promo.InitFromPrefs()) 183 if (!promo.InitFromPrefs())
184 return; 184 return;
185 185
186 if (promo.action_type() == "ACTION_EMAIL") 186 if (promo.action_type() == "ACTION_EMAIL")
187 HandlePromoSendEmail(promo.action_args()); 187 HandlePromoSendEmail(promo.action_args());
188 } 188 }
189 189
190 void PromoHandler::HandlePromoDisabled(const base::ListValue* /*args*/) { 190 void PromoHandler::HandlePromoDisabled(const base::ListValue* /*args*/) {
191 Profile* profile = Profile::FromWebUI(web_ui()); 191 if (!CanShowNotificationPromo(g_browser_process->local_state()))
192 if (!profile || !CanShowNotificationPromo(profile))
193 return; 192 return;
194 193
195 NotificationPromo::HandleClosed( 194 NotificationPromo::HandleClosed(g_browser_process->local_state(),
196 profile, NotificationPromo::MOBILE_NTP_SYNC_PROMO); 195 NotificationPromo::MOBILE_NTP_SYNC_PROMO);
197 RecordImpressionOnHistogram(PROMO_IMPRESSION_CLOSE_PROMO_CLICKED); 196 RecordImpressionOnHistogram(PROMO_IMPRESSION_CLOSE_PROMO_CLICKED);
198 197
199 content::NotificationService* service = 198 content::NotificationService* service =
200 content::NotificationService::current(); 199 content::NotificationService::current();
201 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, 200 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED,
202 content::Source<PromoHandler>(this), 201 content::Source<PromoHandler>(this),
203 content::NotificationService::NoDetails()); 202 content::NotificationService::NoDetails());
204 } 203 }
205 204
206 void PromoHandler::HandleGetPromotions(const base::ListValue* /*args*/) { 205 void PromoHandler::HandleGetPromotions(const base::ListValue* /*args*/) {
(...skipping 11 matching lines...) Expand all
218 DictionaryValue result; 217 DictionaryValue result;
219 if (FetchPromotion(&result)) 218 if (FetchPromotion(&result))
220 web_ui()->CallJavascriptFunction("ntp.setPromotions", result); 219 web_ui()->CallJavascriptFunction("ntp.setPromotions", result);
221 else 220 else
222 web_ui()->CallJavascriptFunction("ntp.clearPromotions"); 221 web_ui()->CallJavascriptFunction("ntp.clearPromotions");
223 } 222 }
224 223
225 void PromoHandler::RecordPromotionImpression(const std::string& id) { 224 void PromoHandler::RecordPromotionImpression(const std::string& id) {
226 // Update number of views a promotion has received and trigger refresh 225 // Update number of views a promotion has received and trigger refresh
227 // if it exceeded max_views set for the promotion. 226 // if it exceeded max_views set for the promotion.
228 Profile* profile = Profile::FromWebUI(web_ui()); 227 if (NotificationPromo::HandleViewed(g_browser_process->local_state(),
229 if (profile &&
230 NotificationPromo::HandleViewed(profile,
231 NotificationPromo::MOBILE_NTP_SYNC_PROMO)) { 228 NotificationPromo::MOBILE_NTP_SYNC_PROMO)) {
232 Notify(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED); 229 Notify(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED);
233 } 230 }
234 231
235 if (id == "most_visited") 232 if (id == "most_visited")
236 RecordImpressionOnHistogram(PROMO_IMPRESSION_MOST_VISITED); 233 RecordImpressionOnHistogram(PROMO_IMPRESSION_MOST_VISITED);
237 else if (id == "open_tabs") 234 else if (id == "open_tabs")
238 RecordImpressionOnHistogram(PROMO_IMPRESSION_OPEN_TABS); 235 RecordImpressionOnHistogram(PROMO_IMPRESSION_OPEN_TABS);
239 else if (id == "sync_promo") 236 else if (id == "sync_promo")
240 RecordImpressionOnHistogram(PROMO_IMPRESSION_SYNC_PROMO); 237 RecordImpressionOnHistogram(PROMO_IMPRESSION_SYNC_PROMO);
241 else 238 else
242 NOTREACHED() << "Unknown promotion impression: " << id; 239 NOTREACHED() << "Unknown promotion impression: " << id;
243 } 240 }
244 241
245 bool PromoHandler::FetchPromotion(DictionaryValue* result) { 242 bool PromoHandler::FetchPromotion(DictionaryValue* result) {
246 DCHECK(result != NULL); 243 DCHECK(result != NULL);
247 Profile* profile = Profile::FromWebUI(web_ui()); 244 if (!CanShowNotificationPromo(g_browser_process->local_state()))
248 if (!profile || !CanShowNotificationPromo(profile))
249 return false; 245 return false;
250 246
251 NotificationPromoMobileNtp promo(profile); 247 NotificationPromoMobileNtp promo(g_browser_process->local_state());
252 if (!promo.InitFromPrefs()) 248 if (!promo.InitFromPrefs())
253 return false; 249 return false;
254 250
255 DCHECK(!promo.text().empty()); 251 DCHECK(!promo.text().empty());
256 if (!DoesChromePromoMatchCurrentSync( 252 if (!DoesChromePromoMatchCurrentSync(
257 promo.requires_sync(), promo.requires_mobile_only_sync())) { 253 promo.requires_sync(), promo.requires_mobile_only_sync())) {
258 return false; 254 return false;
259 } 255 }
260 256
261 result->SetBoolean("promoIsAllowed", true); 257 result->SetBoolean("promoIsAllowed", true);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 sessions[i]->device_type; 327 sessions[i]->device_type;
332 if (device_type == browser_sync::SyncedSession::TYPE_WIN || 328 if (device_type == browser_sync::SyncedSession::TYPE_WIN ||
333 device_type == browser_sync::SyncedSession::TYPE_MACOSX || 329 device_type == browser_sync::SyncedSession::TYPE_MACOSX ||
334 device_type == browser_sync::SyncedSession::TYPE_LINUX) { 330 device_type == browser_sync::SyncedSession::TYPE_LINUX) {
335 // Found a desktop session: write out the pref. 331 // Found a desktop session: write out the pref.
336 prefs->SetBoolean(prefs::kNtpPromoDesktopSessionFound, true); 332 prefs->SetBoolean(prefs::kNtpPromoDesktopSessionFound, true);
337 return; 333 return;
338 } 334 }
339 } 335 }
340 } 336 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698