Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/web_resource/promo_resource_service.h" | 5 #include "chrome/browser/web_resource/promo_resource_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service_simple.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 15 #include "chrome/browser/web_resource/notification_promo.h" | 15 #include "chrome/browser/web_resource/notification_promo.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 19 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
| 20 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // Delay on first fetch so we don't interfere with startup. | 24 // Delay on first fetch so we don't interfere with startup. |
| 25 const int kStartResourceFetchDelay = 5000; | 25 const int kStartResourceFetchDelay = 5000; |
| 26 | 26 |
| 27 // Delay between calls to fetch the promo json: 6 hours in production, and 3 min | 27 // Delay between calls to fetch the promo json: 6 hours in production, and 3 min |
| 28 // in debug. | 28 // in debug. |
| 29 const int kCacheUpdateDelay = 6 * 60 * 60 * 1000; | 29 const int kCacheUpdateDelay = 6 * 60 * 60 * 1000; |
| 30 const int kTestCacheUpdateDelay = 3 * 60 * 1000; | 30 const int kTestCacheUpdateDelay = 3 * 60 * 1000; |
| 31 | 31 |
| 32 // The version of the service (used to expire the cache when upgrading Chrome | 32 // The version of the service (used to expire the cache when upgrading Chrome |
| 33 // to versions with different types of promos). | 33 // to versions with different types of promos). |
| 34 const int kPromoServiceVersion = 7; | 34 const int kPromoServiceVersion = 7; |
| 35 | 35 |
| 36 // The promotion type used for Unpack() and ScheduleNotificationOnInit. | 36 // The promotion type used for Unpack() and ScheduleNotificationOnInit(). |
| 37 const NotificationPromo::PromoType kValidPromoTypes[] = { | 37 const NotificationPromo::PromoType kValidPromoTypes[] = { |
| 38 #if defined(OS_ANDROID) || defined(OS_IOS) | 38 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 39 NotificationPromo::MOBILE_NTP_SYNC_PROMO, | 39 NotificationPromo::MOBILE_NTP_SYNC_PROMO, |
| 40 #else | 40 #else |
| 41 NotificationPromo::NTP_NOTIFICATION_PROMO, | 41 NotificationPromo::NTP_NOTIFICATION_PROMO, |
| 42 NotificationPromo::NTP_BUBBLE_PROMO, | 42 NotificationPromo::NTP_BUBBLE_PROMO, |
| 43 #endif | 43 #endif |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 GURL GetPromoResourceURL() { | 46 GURL GetPromoResourceURL() { |
| 47 const std::string promo_server_url = CommandLine::ForCurrentProcess()-> | 47 const std::string promo_server_url = CommandLine::ForCurrentProcess()-> |
| 48 GetSwitchValueASCII(switches::kPromoServerURL); | 48 GetSwitchValueASCII(switches::kPromoServerURL); |
| 49 return promo_server_url.empty() ? | 49 return promo_server_url.empty() ? |
| 50 NotificationPromo::PromoServerURL() : GURL(promo_server_url); | 50 NotificationPromo::PromoServerURL() : GURL(promo_server_url); |
| 51 } | 51 } |
| 52 | 52 |
| 53 bool IsTest() { | 53 bool IsTest() { |
| 54 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL); | 54 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL); |
| 55 } | 55 } |
| 56 | 56 |
| 57 int GetCacheUpdateDelay() { | 57 int GetCacheUpdateDelay() { |
| 58 return IsTest() ? kTestCacheUpdateDelay : kCacheUpdateDelay; | 58 return IsTest() ? kTestCacheUpdateDelay : kCacheUpdateDelay; |
| 59 } | 59 } |
| 60 | 60 |
| 61 } // namespace | 61 } // namespace |
| 62 | 62 |
| 63 // static | 63 // static |
| 64 void PromoResourceService::RegisterPrefs(PrefServiceSimple* local_state) { | 64 void PromoResourceService::RegisterPrefs(PrefServiceSimple* local_state) { |
| 65 // TODO(achuith): Delete this in M26. http://crbug.com/143773 | 65 local_state->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, "0"); |
| 66 // The promo service version number, and last locale. | 66 NotificationPromo::RegisterPrefs(local_state); |
| 67 const char kNtpPromoVersion[] = "ntp.promo_version"; | |
| 68 const char kNtpPromoLocale[] = "ntp.promo_locale"; | |
| 69 local_state->RegisterIntegerPref(kNtpPromoVersion, 0); | |
| 70 local_state->RegisterStringPref(kNtpPromoLocale, std::string()); | |
| 71 local_state->ClearPref(kNtpPromoVersion); | |
| 72 local_state->ClearPref(kNtpPromoLocale); | |
| 73 } | 67 } |
| 74 | 68 |
| 75 // static | 69 // static |
| 76 void PromoResourceService::RegisterUserPrefs(PrefServiceSyncable* prefs) { | 70 void PromoResourceService::RegisterUserPrefs(PrefServiceSyncable* prefs) { |
| 71 // TODO(dbeam): remove in M28 when all prefs have been cleared. | |
|
achuithb
2013/01/08 21:57:33
Same.
| |
| 77 prefs->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, | 72 prefs->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, |
| 78 "0", | 73 "0", |
| 79 PrefServiceSyncable::UNSYNCABLE_PREF); | 74 PrefServiceSyncable::UNSYNCABLE_PREF); |
| 75 prefs->ClearPref(prefs::kNtpPromoResourceCacheUpdate); | |
| 80 NotificationPromo::RegisterUserPrefs(prefs); | 76 NotificationPromo::RegisterUserPrefs(prefs); |
| 81 } | 77 } |
| 82 | 78 |
| 83 PromoResourceService::PromoResourceService(Profile* profile) | 79 PromoResourceService::PromoResourceService() |
| 84 : WebResourceService(profile->GetPrefs(), | 80 : WebResourceService(g_browser_process->local_state(), |
| 85 GetPromoResourceURL(), | 81 GetPromoResourceURL(), |
| 86 true, // append locale to URL | 82 true, // append locale to URL |
| 87 prefs::kNtpPromoResourceCacheUpdate, | 83 prefs::kNtpPromoResourceCacheUpdate, |
| 88 kStartResourceFetchDelay, | 84 kStartResourceFetchDelay, |
| 89 GetCacheUpdateDelay()), | 85 GetCacheUpdateDelay()), |
| 90 profile_(profile), | |
| 91 ALLOW_THIS_IN_INITIALIZER_LIST( | 86 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 92 weak_ptr_factory_(this)) { | 87 weak_ptr_factory_(this)) { |
| 93 ScheduleNotificationOnInit(); | 88 ScheduleNotificationOnInit(); |
| 94 } | 89 } |
| 95 | 90 |
| 96 PromoResourceService::~PromoResourceService() { | 91 PromoResourceService::~PromoResourceService() { |
| 97 } | 92 } |
| 98 | 93 |
| 99 void PromoResourceService::ScheduleNotification( | 94 void PromoResourceService::ScheduleNotification( |
| 100 const NotificationPromo& notification_promo) { | 95 const NotificationPromo& notification_promo) { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 125 } else { | 120 } else { |
| 126 // The promo (if any) was apparently cancelled. Notify immediately. | 121 // The promo (if any) was apparently cancelled. Notify immediately. |
| 127 PostNotification(0); | 122 PostNotification(0); |
| 128 } | 123 } |
| 129 } | 124 } |
| 130 | 125 |
| 131 void PromoResourceService::ScheduleNotificationOnInit() { | 126 void PromoResourceService::ScheduleNotificationOnInit() { |
| 132 // If the promo start is in the future, set a notification task to | 127 // If the promo start is in the future, set a notification task to |
| 133 // invalidate the NTP cache at the time of the promo start. | 128 // invalidate the NTP cache at the time of the promo start. |
| 134 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { | 129 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { |
| 135 NotificationPromo notification_promo(profile_); | 130 NotificationPromo notification_promo; |
| 136 notification_promo.InitFromPrefs(kValidPromoTypes[i]); | 131 notification_promo.InitFromPrefs(kValidPromoTypes[i]); |
| 137 ScheduleNotification(notification_promo); | 132 ScheduleNotification(notification_promo); |
| 138 } | 133 } |
| 139 } | 134 } |
| 140 | 135 |
| 141 void PromoResourceService::PostNotification(int64 delay_ms) { | 136 void PromoResourceService::PostNotification(int64 delay_ms) { |
| 142 // Note that this could cause re-issuing a notification every time | 137 // Note that this could cause re-issuing a notification every time |
| 143 // we receive an update from a server if something goes wrong. | 138 // we receive an update from a server if something goes wrong. |
| 144 // Given that this couldn't happen more frequently than every | 139 // Given that this couldn't happen more frequently than every |
| 145 // kCacheUpdateDelay milliseconds, we should be fine. | 140 // kCacheUpdateDelay milliseconds, we should be fine. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 159 void PromoResourceService::PromoResourceStateChange() { | 154 void PromoResourceService::PromoResourceStateChange() { |
| 160 content::NotificationService* service = | 155 content::NotificationService* service = |
| 161 content::NotificationService::current(); | 156 content::NotificationService::current(); |
| 162 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, | 157 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, |
| 163 content::Source<WebResourceService>(this), | 158 content::Source<WebResourceService>(this), |
| 164 content::NotificationService::NoDetails()); | 159 content::NotificationService::NoDetails()); |
| 165 } | 160 } |
| 166 | 161 |
| 167 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { | 162 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { |
| 168 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { | 163 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { |
| 169 NotificationPromo notification_promo(profile_); | 164 NotificationPromo notification_promo; |
| 170 notification_promo.InitFromJson(parsed_json, kValidPromoTypes[i]); | 165 notification_promo.InitFromJson(parsed_json, kValidPromoTypes[i]); |
| 171 if (notification_promo.new_notification()) | 166 if (notification_promo.new_notification()) |
| 172 ScheduleNotification(notification_promo); | 167 ScheduleNotification(notification_promo); |
| 173 } | 168 } |
| 174 } | 169 } |
| OLD | NEW |