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