| 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" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.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 static const int kStartResourceFetchDelay = 5000; | 25 static const int kStartResourceFetchDelay = 5000; |
| 26 | 26 |
| 27 // Delay between calls to update the cache (12h), and 10s in debug mode. | 27 // Delay between calls to update the cache (12 hours), and 3 min in debug mode. |
| 28 static const int kCacheUpdateDelay = 12 * 60 * 60 * 1000; | 28 static const int kCacheUpdateDelay = 12 * 60 * 60 * 1000; |
| 29 static const int kTestCacheUpdateDelay = 10 * 1000; | 29 static const int kTestCacheUpdateDelay = 3 * 60 * 1000; |
| 30 | 30 |
| 31 // The version of the service (used to expire the cache when upgrading Chrome | 31 // The version of the service (used to expire the cache when upgrading Chrome |
| 32 // to versions with different types of promos). | 32 // to versions with different types of promos). |
| 33 static const int kPromoServiceVersion = 7; | 33 static const int kPromoServiceVersion = 7; |
| 34 | 34 |
| 35 // The promotion type used for Unpack() and ScheduleNotificationOnInit. |
| 36 static const NotificationPromo::PromoType kDefaultPromoType = |
| 37 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 38 NotificationPromo::MOBILE_NTP_SYNC_PROMO; |
| 39 #else |
| 40 NotificationPromo::NTP_NOTIFICATION_PROMO; |
| 41 #endif |
| 42 |
| 35 GURL GetPromoResourceURL() { | 43 GURL GetPromoResourceURL() { |
| 36 const std::string promo_server_url = CommandLine::ForCurrentProcess()-> | 44 const std::string promo_server_url = CommandLine::ForCurrentProcess()-> |
| 37 GetSwitchValueASCII(switches::kPromoServerURL); | 45 GetSwitchValueASCII(switches::kPromoServerURL); |
| 38 return promo_server_url.empty() ? | 46 return promo_server_url.empty() ? |
| 39 NotificationPromo::PromoServerURL() : GURL(promo_server_url); | 47 NotificationPromo::PromoServerURL() : GURL(promo_server_url); |
| 40 } | 48 } |
| 41 | 49 |
| 42 bool IsTest() { | 50 bool IsTest() { |
| 43 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL); | 51 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL); |
| 44 } | 52 } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 // multi-profile case. We should probably store version/locale in prefs_ | 136 // multi-profile case. We should probably store version/locale in prefs_ |
| 129 // as well. | 137 // as well. |
| 130 PrefService* local_state = g_browser_process->local_state(); | 138 PrefService* local_state = g_browser_process->local_state(); |
| 131 local_state->SetInteger(prefs::kNtpPromoVersion, kPromoServiceVersion); | 139 local_state->SetInteger(prefs::kNtpPromoVersion, kPromoServiceVersion); |
| 132 local_state->SetString(prefs::kNtpPromoLocale, locale); | 140 local_state->SetString(prefs::kNtpPromoLocale, locale); |
| 133 prefs_->ClearPref(prefs::kNtpPromoResourceCacheUpdate); | 141 prefs_->ClearPref(prefs::kNtpPromoResourceCacheUpdate); |
| 134 PostNotification(0); | 142 PostNotification(0); |
| 135 } else { | 143 } else { |
| 136 // If the promo start is in the future, set a notification task to | 144 // If the promo start is in the future, set a notification task to |
| 137 // invalidate the NTP cache at the time of the promo start. | 145 // invalidate the NTP cache at the time of the promo start. |
| 138 double promo_start = prefs_->GetDouble(prefs::kNtpPromoStart); | 146 NotificationPromo notification_promo(profile_); |
| 139 double promo_end = prefs_->GetDouble(prefs::kNtpPromoEnd); | 147 notification_promo.InitFromPrefs(kDefaultPromoType); |
| 140 ScheduleNotification(promo_start, promo_end); | 148 ScheduleNotification(notification_promo.StartTimeForGroup(), |
| 149 notification_promo.EndTime()); |
| 141 } | 150 } |
| 142 } | 151 } |
| 143 | 152 |
| 144 void PromoResourceService::PostNotification(int64 delay_ms) { | 153 void PromoResourceService::PostNotification(int64 delay_ms) { |
| 145 // Note that this could cause re-issuing a notification every time | 154 // Note that this could cause re-issuing a notification every time |
| 146 // we receive an update from a server if something goes wrong. | 155 // we receive an update from a server if something goes wrong. |
| 147 // Given that this couldn't happen more frequently than every | 156 // Given that this couldn't happen more frequently than every |
| 148 // kCacheUpdateDelay milliseconds, we should be fine. | 157 // kCacheUpdateDelay milliseconds, we should be fine. |
| 149 // TODO(achuith): This crashes if we post delay_ms = 0 to the message loop. | 158 // TODO(achuith): This crashes if we post delay_ms = 0 to the message loop. |
| 150 // during startup. | 159 // during startup. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 172 return local_state->GetInteger(prefs::kNtpPromoVersion); | 181 return local_state->GetInteger(prefs::kNtpPromoVersion); |
| 173 } | 182 } |
| 174 | 183 |
| 175 std::string PromoResourceService::GetPromoLocale() { | 184 std::string PromoResourceService::GetPromoLocale() { |
| 176 PrefService* local_state = g_browser_process->local_state(); | 185 PrefService* local_state = g_browser_process->local_state(); |
| 177 return local_state->GetString(prefs::kNtpPromoLocale); | 186 return local_state->GetString(prefs::kNtpPromoLocale); |
| 178 } | 187 } |
| 179 | 188 |
| 180 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { | 189 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { |
| 181 NotificationPromo notification_promo(profile_); | 190 NotificationPromo notification_promo(profile_); |
| 182 NotificationPromo::PromoType promo_type = | 191 notification_promo.InitFromJson(parsed_json, kDefaultPromoType); |
| 183 #if !defined(OS_ANDROID) | |
| 184 NotificationPromo::NTP_NOTIFICATION_PROMO; | |
| 185 #else | |
| 186 NotificationPromo::MOBILE_NTP_SYNC_PROMO; | |
| 187 #endif | |
| 188 notification_promo.InitFromJson(parsed_json, promo_type); | |
| 189 | 192 |
| 190 if (notification_promo.new_notification()) { | 193 if (notification_promo.new_notification()) { |
| 191 ScheduleNotification(notification_promo.StartTimeForGroup(), | 194 ScheduleNotification(notification_promo.StartTimeForGroup(), |
| 192 notification_promo.EndTime()); | 195 notification_promo.EndTime()); |
| 193 } | 196 } |
| 194 } | 197 } |
| OLD | NEW |