| 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/browser/web_resource/notification_promo.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 20 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 // Delay on first fetch so we don't interfere with startup. | 25 // Delay on first fetch so we don't interfere with startup. |
| 25 static const int kStartResourceFetchDelay = 5000; | 26 const int kStartResourceFetchDelay = 5000; |
| 26 | 27 |
| 27 // Delay between calls to update the cache (12 hours), and 3 min in debug mode. | 28 // Delay between calls to update the cache (12 hours), and 3 min in debug mode. |
| 28 static const int kCacheUpdateDelay = 12 * 60 * 60 * 1000; | 29 const int kCacheUpdateDelay = 12 * 60 * 60 * 1000; |
| 29 static const int kTestCacheUpdateDelay = 3 * 60 * 1000; | 30 const int kTestCacheUpdateDelay = 3 * 60 * 1000; |
| 30 | 31 |
| 31 // 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 |
| 32 // to versions with different types of promos). | 33 // to versions with different types of promos). |
| 33 static const int kPromoServiceVersion = 7; | 34 const int kPromoServiceVersion = 7; |
| 34 | 35 |
| 35 // The promotion type used for Unpack() and ScheduleNotificationOnInit. | 36 // The promotion type used for Unpack() and ScheduleNotificationOnInit. |
| 36 static const NotificationPromo::PromoType kDefaultPromoType = | 37 const NotificationPromo::PromoType kValidPromoTypes[] = { |
| 37 #if defined(OS_ANDROID) || defined(OS_IOS) | 38 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 38 NotificationPromo::MOBILE_NTP_SYNC_PROMO; | 39 NotificationPromo::MOBILE_NTP_SYNC_PROMO, |
| 39 #else | 40 #else |
| 40 NotificationPromo::NTP_NOTIFICATION_PROMO; | 41 NotificationPromo::NTP_NOTIFICATION_PROMO, |
| 42 NotificationPromo::NTP_BUBBLE_PROMO, |
| 41 #endif | 43 #endif |
| 44 }; |
| 42 | 45 |
| 43 GURL GetPromoResourceURL() { | 46 GURL GetPromoResourceURL() { |
| 44 const std::string promo_server_url = CommandLine::ForCurrentProcess()-> | 47 const std::string promo_server_url = CommandLine::ForCurrentProcess()-> |
| 45 GetSwitchValueASCII(switches::kPromoServerURL); | 48 GetSwitchValueASCII(switches::kPromoServerURL); |
| 46 return promo_server_url.empty() ? | 49 return promo_server_url.empty() ? |
| 47 NotificationPromo::PromoServerURL() : GURL(promo_server_url); | 50 NotificationPromo::PromoServerURL() : GURL(promo_server_url); |
| 48 } | 51 } |
| 49 | 52 |
| 50 bool IsTest() { | 53 bool IsTest() { |
| 51 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL); | 54 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 GetCacheUpdateDelay()), | 93 GetCacheUpdateDelay()), |
| 91 profile_(profile), | 94 profile_(profile), |
| 92 ALLOW_THIS_IN_INITIALIZER_LIST( | 95 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 93 weak_ptr_factory_(this)) { | 96 weak_ptr_factory_(this)) { |
| 94 ScheduleNotificationOnInit(); | 97 ScheduleNotificationOnInit(); |
| 95 } | 98 } |
| 96 | 99 |
| 97 PromoResourceService::~PromoResourceService() { | 100 PromoResourceService::~PromoResourceService() { |
| 98 } | 101 } |
| 99 | 102 |
| 100 void PromoResourceService::ScheduleNotification(double promo_start, | 103 void PromoResourceService::ScheduleNotification( |
| 101 double promo_end) { | 104 const NotificationPromo& notification_promo) { |
| 105 const double promo_start = notification_promo.StartTimeForGroup(); |
| 106 const double promo_end = notification_promo.EndTime(); |
| 107 |
| 102 if (promo_start > 0 && promo_end > 0) { | 108 if (promo_start > 0 && promo_end > 0) { |
| 103 const int64 ms_until_start = | 109 const int64 ms_until_start = |
| 104 static_cast<int64>((base::Time::FromDoubleT( | 110 static_cast<int64>((base::Time::FromDoubleT( |
| 105 promo_start) - base::Time::Now()).InMilliseconds()); | 111 promo_start) - base::Time::Now()).InMilliseconds()); |
| 106 const int64 ms_until_end = | 112 const int64 ms_until_end = |
| 107 static_cast<int64>((base::Time::FromDoubleT( | 113 static_cast<int64>((base::Time::FromDoubleT( |
| 108 promo_end) - base::Time::Now()).InMilliseconds()); | 114 promo_end) - base::Time::Now()).InMilliseconds()); |
| 109 if (ms_until_start > 0) { | 115 if (ms_until_start > 0) { |
| 110 // Schedule the next notification to happen at the start of promotion. | 116 // Schedule the next notification to happen at the start of promotion. |
| 111 PostNotification(ms_until_start); | 117 PostNotification(ms_until_start); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 136 // multi-profile case. We should probably store version/locale in prefs_ | 142 // multi-profile case. We should probably store version/locale in prefs_ |
| 137 // as well. | 143 // as well. |
| 138 PrefService* local_state = g_browser_process->local_state(); | 144 PrefService* local_state = g_browser_process->local_state(); |
| 139 local_state->SetInteger(prefs::kNtpPromoVersion, kPromoServiceVersion); | 145 local_state->SetInteger(prefs::kNtpPromoVersion, kPromoServiceVersion); |
| 140 local_state->SetString(prefs::kNtpPromoLocale, locale); | 146 local_state->SetString(prefs::kNtpPromoLocale, locale); |
| 141 prefs_->ClearPref(prefs::kNtpPromoResourceCacheUpdate); | 147 prefs_->ClearPref(prefs::kNtpPromoResourceCacheUpdate); |
| 142 PostNotification(0); | 148 PostNotification(0); |
| 143 } else { | 149 } else { |
| 144 // If the promo start is in the future, set a notification task to | 150 // If the promo start is in the future, set a notification task to |
| 145 // invalidate the NTP cache at the time of the promo start. | 151 // invalidate the NTP cache at the time of the promo start. |
| 146 NotificationPromo notification_promo(profile_); | 152 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { |
| 147 notification_promo.InitFromPrefs(kDefaultPromoType); | 153 NotificationPromo notification_promo(profile_); |
| 148 ScheduleNotification(notification_promo.StartTimeForGroup(), | 154 notification_promo.InitFromPrefs(kValidPromoTypes[i]); |
| 149 notification_promo.EndTime()); | 155 ScheduleNotification(notification_promo); |
| 156 } |
| 150 } | 157 } |
| 151 } | 158 } |
| 152 | 159 |
| 153 void PromoResourceService::PostNotification(int64 delay_ms) { | 160 void PromoResourceService::PostNotification(int64 delay_ms) { |
| 154 // Note that this could cause re-issuing a notification every time | 161 // Note that this could cause re-issuing a notification every time |
| 155 // we receive an update from a server if something goes wrong. | 162 // we receive an update from a server if something goes wrong. |
| 156 // Given that this couldn't happen more frequently than every | 163 // Given that this couldn't happen more frequently than every |
| 157 // kCacheUpdateDelay milliseconds, we should be fine. | 164 // kCacheUpdateDelay milliseconds, we should be fine. |
| 158 // TODO(achuith): This crashes if we post delay_ms = 0 to the message loop. | 165 // TODO(achuith): This crashes if we post delay_ms = 0 to the message loop. |
| 159 // during startup. | 166 // during startup. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 180 PrefService* local_state = g_browser_process->local_state(); | 187 PrefService* local_state = g_browser_process->local_state(); |
| 181 return local_state->GetInteger(prefs::kNtpPromoVersion); | 188 return local_state->GetInteger(prefs::kNtpPromoVersion); |
| 182 } | 189 } |
| 183 | 190 |
| 184 std::string PromoResourceService::GetPromoLocale() { | 191 std::string PromoResourceService::GetPromoLocale() { |
| 185 PrefService* local_state = g_browser_process->local_state(); | 192 PrefService* local_state = g_browser_process->local_state(); |
| 186 return local_state->GetString(prefs::kNtpPromoLocale); | 193 return local_state->GetString(prefs::kNtpPromoLocale); |
| 187 } | 194 } |
| 188 | 195 |
| 189 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { | 196 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { |
| 190 NotificationPromo notification_promo(profile_); | 197 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { |
| 191 notification_promo.InitFromJson(parsed_json, kDefaultPromoType); | 198 NotificationPromo notification_promo(profile_); |
| 192 | 199 notification_promo.InitFromJson(parsed_json, kValidPromoTypes[i]); |
| 193 if (notification_promo.new_notification()) { | 200 if (notification_promo.new_notification()) |
| 194 ScheduleNotification(notification_promo.StartTimeForGroup(), | 201 ScheduleNotification(notification_promo); |
| 195 notification_promo.EndTime()); | |
| 196 } | 202 } |
| 197 } | 203 } |
| OLD | NEW |