| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 static const char kWebStoreExpireProperty[] = "tooltip"; | 49 static const char kWebStoreExpireProperty[] = "tooltip"; |
| 50 | 50 |
| 51 const char* GetPromoResourceURL() { | 51 const char* GetPromoResourceURL() { |
| 52 const char* kCaitPromoResourceServer = | 52 const char* kCaitPromoResourceServer = |
| 53 "http://caitkp-desktop.mon.corp.google.com/promo.js?"; | 53 "http://caitkp-desktop.mon.corp.google.com/promo.js?"; |
| 54 std::string promo_server_url = CommandLine::ForCurrentProcess()-> | 54 std::string promo_server_url = CommandLine::ForCurrentProcess()-> |
| 55 GetSwitchValueASCII(switches::kPromoServerURL); | 55 GetSwitchValueASCII(switches::kPromoServerURL); |
| 56 return promo_server_url.empty() ? | 56 return promo_server_url.empty() ? |
| 57 PromoResourceService::kDefaultPromoResourceServer : | 57 PromoResourceService::kDefaultPromoResourceServer : |
| 58 kCaitPromoResourceServer; | 58 kCaitPromoResourceServer; |
| 59 //promo_server_url.c_str(); | |
| 60 } | 59 } |
| 61 | 60 |
| 62 bool IsTest() { | 61 bool IsTest() { |
| 63 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL); | 62 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL); |
| 64 } | 63 } |
| 65 | 64 |
| 66 int GetCacheUpdateDelay() { | 65 int GetCacheUpdateDelay() { |
| 67 return IsTest() ? kTestCacheUpdateDelay : kCacheUpdateDelay; | 66 return IsTest() ? kTestCacheUpdateDelay : kCacheUpdateDelay; |
| 68 } | 67 } |
| 69 | 68 |
| 70 } // namespace | 69 } // namespace |
| 71 | 70 |
| 72 // Server for dynamically loaded NTP HTML elements. | 71 // Server for dynamically loaded NTP HTML elements. |
| 73 const char* PromoResourceService::kDefaultPromoResourceServer = | 72 const char* PromoResourceService::kDefaultPromoResourceServer = |
| 74 "https://www.google.com/support/chrome/bin/topic/1142433/inproduct?hl="; | 73 "https://www.google.com/support/chrome/bin/topic/1142433/inproduct?hl="; |
| 75 | 74 |
| 76 | 75 |
| 77 | 76 |
| 78 // static | 77 // static |
| 79 void PromoResourceService::RegisterPrefs(PrefService* local_state) { | 78 void PromoResourceService::RegisterPrefs(PrefService* local_state) { |
| 80 local_state->RegisterIntegerPref(prefs::kNTPPromoVersion, 0); | 79 local_state->RegisterIntegerPref(prefs::kNTPPromoVersion, 0); |
| 81 local_state->RegisterStringPref(prefs::kNTPPromoLocale, std::string()); | 80 local_state->RegisterStringPref(prefs::kNTPPromoLocale, std::string()); |
| 82 } | 81 } |
| 83 | 82 |
| 84 // static | 83 // static |
| 85 void PromoResourceService::RegisterUserPrefs(PrefService* prefs) { | 84 void PromoResourceService::RegisterUserPrefs(PrefService* prefs) { |
| 85 RegisterLastUpdateTimePref(prefs, prefs::kNTPPromoResourceCacheUpdate); |
| 86 prefs->RegisterDoublePref(prefs::kNTPCustomLogoStart, | 86 prefs->RegisterDoublePref(prefs::kNTPCustomLogoStart, |
| 87 0, | 87 0, |
| 88 PrefService::UNSYNCABLE_PREF); | 88 PrefService::UNSYNCABLE_PREF); |
| 89 prefs->RegisterDoublePref(prefs::kNTPCustomLogoEnd, | 89 prefs->RegisterDoublePref(prefs::kNTPCustomLogoEnd, |
| 90 0, | 90 0, |
| 91 PrefService::UNSYNCABLE_PREF); | 91 PrefService::UNSYNCABLE_PREF); |
| 92 prefs->RegisterIntegerPref(prefs::kNTPSignInPromoGroup, | 92 prefs->RegisterIntegerPref(prefs::kNTPSignInPromoGroup, |
| 93 0, | 93 0, |
| 94 PrefService::UNSYNCABLE_PREF); | 94 PrefService::UNSYNCABLE_PREF); |
| 95 prefs->RegisterIntegerPref(prefs::kNTPSignInPromoGroupMax, | 95 prefs->RegisterIntegerPref(prefs::kNTPSignInPromoGroupMax, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 126 // Show promos for local builds when using a custom promo URL. | 126 // Show promos for local builds when using a custom promo URL. |
| 127 return CommandLine::ForCurrentProcess()->HasSwitch( | 127 return CommandLine::ForCurrentProcess()->HasSwitch( |
| 128 switches::kPromoServerURL); | 128 switches::kPromoServerURL); |
| 129 } | 129 } |
| 130 } | 130 } |
| 131 | 131 |
| 132 PromoResourceService::PromoResourceService(Profile* profile) | 132 PromoResourceService::PromoResourceService(Profile* profile) |
| 133 : WebResourceService(profile->GetPrefs(), | 133 : WebResourceService(profile->GetPrefs(), |
| 134 GetPromoResourceURL(), | 134 GetPromoResourceURL(), |
| 135 false,//true, // append locale to URL | 135 false,//true, // append locale to URL |
| 136 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, | |
| 137 prefs::kNTPPromoResourceCacheUpdate, | 136 prefs::kNTPPromoResourceCacheUpdate, |
| 138 kStartResourceFetchDelay, | 137 kStartResourceFetchDelay, |
| 139 GetCacheUpdateDelay()), | 138 GetCacheUpdateDelay()), |
| 140 profile_(profile), | 139 profile_(profile), |
| 141 channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) { | 140 channel_(chrome::VersionInfo::CHANNEL_UNKNOWN), |
| 142 Init(); | 141 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
| 142 web_resource_update_scheduled_(false) { |
| 143 ScheduleNotificationOnInit(); |
| 143 } | 144 } |
| 144 | 145 |
| 145 PromoResourceService::~PromoResourceService() { } | 146 PromoResourceService::~PromoResourceService() { } |
| 146 | 147 |
| 147 void PromoResourceService::Init() { | |
| 148 ScheduleNotificationOnInit(); | |
| 149 } | |
| 150 | |
| 151 bool PromoResourceService::IsBuildTargeted(int builds_targeted) { | 148 bool PromoResourceService::IsBuildTargeted(int builds_targeted) { |
| 152 if (channel_ == chrome::VersionInfo::CHANNEL_UNKNOWN) | 149 if (channel_ == chrome::VersionInfo::CHANNEL_UNKNOWN) |
| 153 channel_ = GetChannel(); | 150 channel_ = GetChannel(); |
| 154 | 151 |
| 155 return IsBuildTargeted(channel_, builds_targeted); | 152 return IsBuildTargeted(channel_, builds_targeted); |
| 156 } | 153 } |
| 157 | 154 |
| 158 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { | 155 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { |
| 159 UnpackLogoSignal(parsed_json); | 156 UnpackLogoSignal(parsed_json); |
| 160 UnpackNotificationSignal(parsed_json); | 157 UnpackNotificationSignal(parsed_json); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 PostNotification(0); | 201 PostNotification(0); |
| 205 } else { | 202 } else { |
| 206 // If the promo start is in the future, set a notification task to | 203 // If the promo start is in the future, set a notification task to |
| 207 // invalidate the NTP cache at the time of the promo start. | 204 // invalidate the NTP cache at the time of the promo start. |
| 208 double promo_start = prefs_->GetDouble(prefs::kNTPPromoStart); | 205 double promo_start = prefs_->GetDouble(prefs::kNTPPromoStart); |
| 209 double promo_end = prefs_->GetDouble(prefs::kNTPPromoEnd); | 206 double promo_end = prefs_->GetDouble(prefs::kNTPPromoEnd); |
| 210 ScheduleNotification(promo_start, promo_end); | 207 ScheduleNotification(promo_start, promo_end); |
| 211 } | 208 } |
| 212 } | 209 } |
| 213 | 210 |
| 211 void PromoResourceService::PostNotification(int64 delay_ms) { |
| 212 if (web_resource_update_scheduled_) |
| 213 return; |
| 214 if (delay_ms > 0) { |
| 215 web_resource_update_scheduled_ = true; |
| 216 MessageLoop::current()->PostDelayedTask( |
| 217 FROM_HERE, |
| 218 base::Bind(&PromoResourceService::PromoResourceStateChange, |
| 219 weak_ptr_factory_.GetWeakPtr()), |
| 220 delay_ms); |
| 221 } else if (delay_ms == 0) { |
| 222 PromoResourceStateChange(); |
| 223 } |
| 224 } |
| 225 |
| 226 void PromoResourceService::PromoResourceStateChange() { |
| 227 web_resource_update_scheduled_ = false; |
| 228 content::NotificationService* service = |
| 229 content::NotificationService::current(); |
| 230 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, |
| 231 content::Source<WebResourceService>(this), |
| 232 content::NotificationService::NoDetails()); |
| 233 } |
| 234 |
| 214 int PromoResourceService::GetPromoServiceVersion() { | 235 int PromoResourceService::GetPromoServiceVersion() { |
| 215 PrefService* local_state = g_browser_process->local_state(); | 236 PrefService* local_state = g_browser_process->local_state(); |
| 216 return local_state->GetInteger(prefs::kNTPPromoVersion); | 237 return local_state->GetInteger(prefs::kNTPPromoVersion); |
| 217 } | 238 } |
| 218 | 239 |
| 219 std::string PromoResourceService::GetPromoLocale() { | 240 std::string PromoResourceService::GetPromoLocale() { |
| 220 PrefService* local_state = g_browser_process->local_state(); | 241 PrefService* local_state = g_browser_process->local_state(); |
| 221 return local_state->GetString(prefs::kNTPPromoLocale); | 242 return local_state->GetString(prefs::kNTPPromoLocale); |
| 222 } | 243 } |
| 223 | 244 |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 if (!prefs->HasPrefPath(prefs::kNTPSignInPromoGroup)) { | 479 if (!prefs->HasPrefPath(prefs::kNTPSignInPromoGroup)) { |
| 459 prefs->SetInteger(prefs::kNTPSignInPromoGroup, | 480 prefs->SetInteger(prefs::kNTPSignInPromoGroup, |
| 460 base::RandInt(1, kNTPSignInPromoNumberOfGroups)); | 481 base::RandInt(1, kNTPSignInPromoNumberOfGroups)); |
| 461 } | 482 } |
| 462 | 483 |
| 463 // A response is not kept if the build wasn't targeted, so the only thing | 484 // A response is not kept if the build wasn't targeted, so the only thing |
| 464 // required to check is the group this client has been tagged in. | 485 // required to check is the group this client has been tagged in. |
| 465 return prefs->GetInteger(prefs::kNTPSignInPromoGroupMax) >= | 486 return prefs->GetInteger(prefs::kNTPSignInPromoGroupMax) >= |
| 466 prefs->GetInteger(prefs::kNTPSignInPromoGroup); | 487 prefs->GetInteger(prefs::kNTPSignInPromoGroup); |
| 467 } | 488 } |
| OLD | NEW |