| 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/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 // Delay on first fetch so we don't interfere with startup. | 28 // Delay on first fetch so we don't interfere with startup. |
| 29 static const int kStartResourceFetchDelay = 5000; | 29 static const int kStartResourceFetchDelay = 5000; |
| 30 | 30 |
| 31 // Delay between calls to update the cache (48 hours). | 31 // Delay between calls to update the cache (48 hours). |
| 32 static const int kCacheUpdateDelay = 48 * 60 * 60 * 1000; | 32 static const int kCacheUpdateDelay = 48 * 60 * 60 * 1000; |
| 33 | 33 |
| 34 // Users are randomly assigned to one of kNTPPromoGroupSize buckets, in order | 34 // Users are randomly assigned to one of kNTPPromoGroupSize buckets, in order |
| 35 // to be able to roll out promos slowly, or display different promos to | 35 // to be able to roll out promos slowly, or display different promos to |
| 36 // different groups. | 36 // different groups. |
| 37 static const int kNTPPromoGroupSize = 16; | 37 static const int kNTPPromoGroupSize = 100; |
| 38 | 38 |
| 39 // Maximum number of hours for each time slice (4 weeks). | 39 // Maximum number of hours for each time slice (4 weeks). |
| 40 static const int kMaxTimeSliceHours = 24 * 7 * 4; | 40 static const int kMaxTimeSliceHours = 24 * 7 * 4; |
| 41 | 41 |
| 42 // The version of the service (used to expire the cache when upgrading Chrome | 42 // The version of the service (used to expire the cache when upgrading Chrome |
| 43 // to versions with different types of promos). | 43 // to versions with different types of promos). |
| 44 static const int kPromoServiceVersion = 2; | 44 static const int kPromoServiceVersion = 2; |
| 45 | 45 |
| 46 // Properties used by the server. | 46 // Properties used by the server. |
| 47 static const char kAnswerIdProperty[] = "answer_id"; | 47 static const char kAnswerIdProperty[] = "answer_id"; |
| 48 static const char kWebStoreHeaderProperty[] = "question"; | 48 static const char kWebStoreHeaderProperty[] = "question"; |
| 49 static const char kWebStoreButtonProperty[] = "inproduct_target"; | 49 static const char kWebStoreButtonProperty[] = "inproduct_target"; |
| 50 static const char kWebStoreLinkProperty[] = "inproduct"; | 50 static const char kWebStoreLinkProperty[] = "inproduct"; |
| 51 static const char kWebStoreExpireProperty[] = "tooltip"; | 51 static const char kWebStoreExpireProperty[] = "tooltip"; |
| 52 | 52 |
| 53 chrome::VersionInfo::Channel GetChannel() { |
| 54 // GetChannel hits the registry on Windows. See http://crbug.com/70898. |
| 55 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 56 return chrome::VersionInfo::GetChannel(); |
| 57 } |
| 58 |
| 59 int GetNextQuestionValue(const std::string& question, |
| 60 size_t* index, |
| 61 bool* err) { |
| 62 if (*err) |
| 63 return 0; |
| 64 |
| 65 size_t new_index = question.find(':', *index); |
| 66 // Note that substr correctly handles npos. |
| 67 std::string fragment(question.substr(*index, new_index - *index)); |
| 68 *index = new_index + 1; |
| 69 |
| 70 int value; |
| 71 *err = !base::StringToInt(fragment, &value); |
| 72 return value; |
| 73 } |
| 74 |
| 53 const char* GetPromoResourceURL() { | 75 const char* GetPromoResourceURL() { |
| 54 std::string promo_server_url = CommandLine::ForCurrentProcess()-> | 76 std::string promo_server_url = CommandLine::ForCurrentProcess()-> |
| 55 GetSwitchValueASCII(switches::kPromoServerURL); | 77 GetSwitchValueASCII(switches::kPromoServerURL); |
| 56 return promo_server_url.empty() ? | 78 return promo_server_url.empty() ? |
| 57 PromoResourceService::kDefaultPromoResourceServer : | 79 PromoResourceService::kDefaultPromoResourceServer : |
| 58 promo_server_url.c_str(); | 80 promo_server_url.c_str(); |
| 59 } | 81 } |
| 60 | 82 |
| 61 } // namespace | 83 } // namespace |
| 62 | 84 |
| 63 // Server for dynamically loaded NTP HTML elements. TODO(mirandac): append | 85 // Server for dynamically loaded NTP HTML elements. |
| 64 // locale for future usage, when we're serving localizable strings. | |
| 65 const char* PromoResourceService::kDefaultPromoResourceServer = | 86 const char* PromoResourceService::kDefaultPromoResourceServer = |
| 66 "https://www.google.com/support/chrome/bin/topic/1142433/inproduct?hl="; | 87 "https://www.google.com/support/chrome/bin/topic/1142433/inproduct?hl="; |
| 67 | 88 |
| 68 // static | 89 // static |
| 69 void PromoResourceService::RegisterPrefs(PrefService* local_state) { | 90 void PromoResourceService::RegisterPrefs(PrefService* local_state) { |
| 70 local_state->RegisterIntegerPref(prefs::kNTPPromoVersion, 0); | 91 local_state->RegisterIntegerPref(prefs::kNTPPromoVersion, 0); |
| 71 local_state->RegisterStringPref(prefs::kNTPPromoLocale, std::string()); | 92 local_state->RegisterStringPref(prefs::kNTPPromoLocale, std::string()); |
| 72 } | 93 } |
| 73 | 94 |
| 74 // static | 95 // static |
| (...skipping 10 matching lines...) Expand all Loading... |
| 85 prefs->RegisterDoublePref(prefs::kNTPPromoEnd, | 106 prefs->RegisterDoublePref(prefs::kNTPPromoEnd, |
| 86 0, | 107 0, |
| 87 PrefService::UNSYNCABLE_PREF); | 108 PrefService::UNSYNCABLE_PREF); |
| 88 prefs->RegisterStringPref(prefs::kNTPPromoLine, | 109 prefs->RegisterStringPref(prefs::kNTPPromoLine, |
| 89 std::string(), | 110 std::string(), |
| 90 PrefService::UNSYNCABLE_PREF); | 111 PrefService::UNSYNCABLE_PREF); |
| 91 prefs->RegisterBooleanPref(prefs::kNTPPromoClosed, | 112 prefs->RegisterBooleanPref(prefs::kNTPPromoClosed, |
| 92 false, | 113 false, |
| 93 PrefService::UNSYNCABLE_PREF); | 114 PrefService::UNSYNCABLE_PREF); |
| 94 prefs->RegisterIntegerPref(prefs::kNTPPromoGroup, | 115 prefs->RegisterIntegerPref(prefs::kNTPPromoGroup, |
| 95 -1, | 116 0, |
| 96 PrefService::UNSYNCABLE_PREF); | 117 PrefService::UNSYNCABLE_PREF); |
| 97 prefs->RegisterIntegerPref( | 118 prefs->RegisterIntegerPref( |
| 98 prefs::kNTPPromoBuild, | 119 prefs::kNTPPromoBuild, |
| 99 CANARY_BUILD | DEV_BUILD | BETA_BUILD | STABLE_BUILD, | 120 CANARY_BUILD | DEV_BUILD | BETA_BUILD | STABLE_BUILD, |
| 100 PrefService::UNSYNCABLE_PREF); | 121 PrefService::UNSYNCABLE_PREF); |
| 101 prefs->RegisterIntegerPref(prefs::kNTPPromoGroupTimeSlice, | 122 prefs->RegisterIntegerPref(prefs::kNTPPromoGroupTimeSlice, |
| 102 0, | 123 0, |
| 103 PrefService::UNSYNCABLE_PREF); | 124 PrefService::UNSYNCABLE_PREF); |
| 125 prefs->RegisterIntegerPref(prefs::kNTPPromoGroupMax, |
| 126 0, |
| 127 PrefService::UNSYNCABLE_PREF); |
| 104 } | 128 } |
| 105 | 129 |
| 106 // static | 130 // static |
| 107 bool PromoResourceService::IsBuildTargeted(chrome::VersionInfo::Channel channel, | 131 bool PromoResourceService::IsBuildTargeted(chrome::VersionInfo::Channel channel, |
| 108 int builds_allowed) { | 132 int builds_allowed) { |
| 109 if (builds_allowed == NO_BUILD) | 133 if (builds_allowed == NO_BUILD) |
| 110 return false; | 134 return false; |
| 111 switch (channel) { | 135 switch (channel) { |
| 112 case chrome::VersionInfo::CHANNEL_CANARY: | 136 case chrome::VersionInfo::CHANNEL_CANARY: |
| 113 return (CANARY_BUILD & builds_allowed) != 0; | 137 return (CANARY_BUILD & builds_allowed) != 0; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 125 } | 149 } |
| 126 | 150 |
| 127 PromoResourceService::PromoResourceService(Profile* profile) | 151 PromoResourceService::PromoResourceService(Profile* profile) |
| 128 : WebResourceService(profile->GetPrefs(), | 152 : WebResourceService(profile->GetPrefs(), |
| 129 GetPromoResourceURL(), | 153 GetPromoResourceURL(), |
| 130 true, // append locale to URL | 154 true, // append locale to URL |
| 131 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, | 155 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, |
| 132 prefs::kNTPPromoResourceCacheUpdate, | 156 prefs::kNTPPromoResourceCacheUpdate, |
| 133 kStartResourceFetchDelay, | 157 kStartResourceFetchDelay, |
| 134 kCacheUpdateDelay), | 158 kCacheUpdateDelay), |
| 135 web_resource_cache_(NULL), | |
| 136 channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) { | 159 channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) { |
| 137 Init(); | 160 Init(); |
| 138 } | 161 } |
| 139 | 162 |
| 140 PromoResourceService::~PromoResourceService() { } | 163 PromoResourceService::~PromoResourceService() { } |
| 141 | 164 |
| 142 void PromoResourceService::Init() { | 165 void PromoResourceService::Init() { |
| 143 ScheduleNotificationOnInit(); | 166 ScheduleNotificationOnInit(); |
| 144 } | 167 } |
| 145 | 168 |
| 146 bool PromoResourceService::IsThisBuildTargeted(int builds_targeted) { | 169 bool PromoResourceService::IsThisBuildTargeted(int builds_targeted) { |
| 147 if (channel_ == chrome::VersionInfo::CHANNEL_UNKNOWN) { | 170 if (channel_ == chrome::VersionInfo::CHANNEL_UNKNOWN) |
| 148 // GetChannel hits the registry on Windows. See http://crbug.com/70898. | 171 channel_ = GetChannel(); |
| 149 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 150 channel_ = chrome::VersionInfo::GetChannel(); | |
| 151 } | |
| 152 | 172 |
| 153 return IsBuildTargeted(channel_, builds_targeted); | 173 return IsBuildTargeted(channel_, builds_targeted); |
| 154 } | 174 } |
| 155 | 175 |
| 156 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { | 176 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { |
| 157 UnpackLogoSignal(parsed_json); | 177 UnpackLogoSignal(parsed_json); |
| 158 UnpackPromoSignal(parsed_json); | 178 UnpackPromoSignal(parsed_json); |
| 159 UnpackWebStoreSignal(parsed_json); | 179 UnpackWebStoreSignal(parsed_json); |
| 160 } | 180 } |
| 161 | 181 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 return local_state->GetInteger(prefs::kNTPPromoVersion); | 226 return local_state->GetInteger(prefs::kNTPPromoVersion); |
| 207 } | 227 } |
| 208 | 228 |
| 209 std::string PromoResourceService::GetPromoLocale() { | 229 std::string PromoResourceService::GetPromoLocale() { |
| 210 PrefService* local_state = g_browser_process->local_state(); | 230 PrefService* local_state = g_browser_process->local_state(); |
| 211 return local_state->GetString(prefs::kNTPPromoLocale); | 231 return local_state->GetString(prefs::kNTPPromoLocale); |
| 212 } | 232 } |
| 213 | 233 |
| 214 void PromoResourceService::UnpackPromoSignal( | 234 void PromoResourceService::UnpackPromoSignal( |
| 215 const DictionaryValue& parsed_json) { | 235 const DictionaryValue& parsed_json) { |
| 236 // Check for newly received start and end values. |
| 237 std::string promo_start_string, promo_end_string; |
| 238 |
| 216 DictionaryValue* topic_dict; | 239 DictionaryValue* topic_dict; |
| 217 ListValue* answer_list; | |
| 218 double old_promo_start = 0; | |
| 219 double old_promo_end = 0; | |
| 220 double promo_start = 0; | |
| 221 double promo_end = 0; | |
| 222 | |
| 223 // Check for preexisting start and end values. | |
| 224 if (prefs_->HasPrefPath(prefs::kNTPPromoStart) && | |
| 225 prefs_->HasPrefPath(prefs::kNTPPromoEnd)) { | |
| 226 old_promo_start = prefs_->GetDouble(prefs::kNTPPromoStart); | |
| 227 old_promo_end = prefs_->GetDouble(prefs::kNTPPromoEnd); | |
| 228 } | |
| 229 | |
| 230 // Check for newly received start and end values. | |
| 231 if (parsed_json.GetDictionary("topic", &topic_dict)) { | 240 if (parsed_json.GetDictionary("topic", &topic_dict)) { |
| 241 ListValue* answer_list; |
| 232 if (topic_dict->GetList("answers", &answer_list)) { | 242 if (topic_dict->GetList("answers", &answer_list)) { |
| 233 std::string promo_start_string = ""; | |
| 234 std::string promo_end_string = ""; | |
| 235 std::string promo_string = ""; | |
| 236 std::string promo_build = ""; | |
| 237 int promo_build_type = 0; | |
| 238 int time_slice_hrs = 0; | |
| 239 for (ListValue::const_iterator answer_iter = answer_list->begin(); | 243 for (ListValue::const_iterator answer_iter = answer_list->begin(); |
| 240 answer_iter != answer_list->end(); ++answer_iter) { | 244 answer_iter != answer_list->end(); ++answer_iter) { |
| 241 if (!(*answer_iter)->IsType(Value::TYPE_DICTIONARY)) | 245 if (!(*answer_iter)->IsType(Value::TYPE_DICTIONARY)) |
| 242 continue; | 246 continue; |
| 243 DictionaryValue* a_dic = | 247 |
| 244 static_cast<DictionaryValue*>(*answer_iter); | 248 ParsePromo(static_cast<DictionaryValue*>(*answer_iter), |
| 245 std::string promo_signal; | 249 &promo_start_string, &promo_end_string); |
| 246 if (a_dic->GetString("name", &promo_signal)) { | |
| 247 if (promo_signal == "promo_start") { | |
| 248 a_dic->GetString("question", &promo_build); | |
| 249 size_t split = promo_build.find(":"); | |
| 250 if (split != std::string::npos && | |
| 251 base::StringToInt(promo_build.substr(0, split), | |
| 252 &promo_build_type) && | |
| 253 base::StringToInt(promo_build.substr(split+1), | |
| 254 &time_slice_hrs) && | |
| 255 promo_build_type >= 0 && | |
| 256 promo_build_type <= (DEV_BUILD | BETA_BUILD | STABLE_BUILD) && | |
| 257 time_slice_hrs >= 0 && | |
| 258 time_slice_hrs <= kMaxTimeSliceHours) { | |
| 259 prefs_->SetInteger(prefs::kNTPPromoBuild, promo_build_type); | |
| 260 prefs_->SetInteger(prefs::kNTPPromoGroupTimeSlice, | |
| 261 time_slice_hrs); | |
| 262 } else { | |
| 263 // If no time data or bad time data are set, do not show promo. | |
| 264 prefs_->SetInteger(prefs::kNTPPromoBuild, NO_BUILD); | |
| 265 prefs_->SetInteger(prefs::kNTPPromoGroupTimeSlice, 0); | |
| 266 } | |
| 267 a_dic->GetString("inproduct", &promo_start_string); | |
| 268 a_dic->GetString("tooltip", &promo_string); | |
| 269 prefs_->SetString(prefs::kNTPPromoLine, promo_string); | |
| 270 srand(static_cast<uint32>(time(NULL))); | |
| 271 prefs_->SetInteger(prefs::kNTPPromoGroup, | |
| 272 rand() % kNTPPromoGroupSize); | |
| 273 } else if (promo_signal == "promo_end") { | |
| 274 a_dic->GetString("inproduct", &promo_end_string); | |
| 275 } | |
| 276 } | |
| 277 } | |
| 278 if (!promo_start_string.empty() && | |
| 279 promo_start_string.length() > 0 && | |
| 280 !promo_end_string.empty() && | |
| 281 promo_end_string.length() > 0) { | |
| 282 base::Time start_time; | |
| 283 base::Time end_time; | |
| 284 if (base::Time::FromString(promo_start_string.c_str(), &start_time) && | |
| 285 base::Time::FromString(promo_end_string.c_str(), &end_time)) { | |
| 286 // Add group time slice, adjusted from hours to seconds. | |
| 287 promo_start = start_time.ToDoubleT() + | |
| 288 (prefs_->FindPreference(prefs::kNTPPromoGroup) ? | |
| 289 prefs_->GetInteger(prefs::kNTPPromoGroup) * | |
| 290 time_slice_hrs * 60 * 60 : 0); | |
| 291 promo_end = end_time.ToDoubleT(); | |
| 292 } | |
| 293 } | 250 } |
| 294 } | 251 } |
| 295 } | 252 } |
| 296 | 253 |
| 254 CheckForNewPromo(promo_start_string, promo_end_string); |
| 255 } |
| 256 |
| 257 void PromoResourceService::ParsePromo(DictionaryValue* a_dic, |
| 258 std::string* promo_start_string, |
| 259 std::string* promo_end_string) { |
| 260 std::string promo_signal; |
| 261 if (a_dic->GetString("name", &promo_signal)) { |
| 262 if (promo_signal == "promo_start") { |
| 263 SetPromoParams(a_dic); |
| 264 SetPromoLine(a_dic); |
| 265 |
| 266 a_dic->GetString("inproduct", promo_start_string); |
| 267 } else if (promo_signal == "promo_end") { |
| 268 a_dic->GetString("inproduct", promo_end_string); |
| 269 } |
| 270 } |
| 271 } |
| 272 |
| 273 void PromoResourceService::SetPromoParams(DictionaryValue* a_dic) { |
| 274 std::string question; |
| 275 a_dic->GetString("question", &question); |
| 276 |
| 277 size_t index(0); |
| 278 bool err(false); |
| 279 int promo_build = GetNextQuestionValue(question, &index, &err); |
| 280 int time_slice = GetNextQuestionValue(question, &index, &err); |
| 281 int max_group = GetNextQuestionValue(question, &index, &err); |
| 282 |
| 283 if (err || |
| 284 promo_build < 0 || |
| 285 promo_build > (DEV_BUILD | BETA_BUILD | STABLE_BUILD) || |
| 286 time_slice < 0 || |
| 287 time_slice > kMaxTimeSliceHours || |
| 288 max_group < 0 || |
| 289 max_group >= kNTPPromoGroupSize) { |
| 290 // If values are not valid, do not show promo. |
| 291 NOTREACHED() << "Invalid server data, question=" << question << |
| 292 ", build=" << promo_build << |
| 293 ", time_slice=" << time_slice << |
| 294 ", max_group=" << max_group; |
| 295 promo_build = NO_BUILD; |
| 296 time_slice = 0; |
| 297 max_group = 0; |
| 298 } |
| 299 |
| 300 prefs_->SetInteger(prefs::kNTPPromoBuild, promo_build); |
| 301 prefs_->SetInteger(prefs::kNTPPromoGroupTimeSlice, time_slice); |
| 302 prefs_->SetInteger(prefs::kNTPPromoGroupMax, max_group); |
| 303 } |
| 304 |
| 305 void PromoResourceService::SetPromoLine(DictionaryValue* a_dic) { |
| 306 std::string promo_line; |
| 307 a_dic->GetString("tooltip", &promo_line); |
| 308 if (!promo_line.empty()) |
| 309 prefs_->SetString(prefs::kNTPPromoLine, promo_line); |
| 310 } |
| 311 |
| 312 void PromoResourceService::CheckForNewPromo( |
| 313 const std::string& promo_start_string, |
| 314 const std::string& promo_end_string) { |
| 315 double promo_start = 0.0, promo_end = 0.0; |
| 316 CalculateNewPromoTimes(promo_start_string, promo_end_string, |
| 317 &promo_start, &promo_end); |
| 318 |
| 319 double old_promo_start = 0.0, old_promo_end = 0.0; |
| 320 GetCurrentPromoTimes(&old_promo_start, &old_promo_end); |
| 321 |
| 297 // If start or end times have changed, trigger a new web resource | 322 // If start or end times have changed, trigger a new web resource |
| 298 // notification, so that the logo on the NTP is updated. This check is | 323 // notification, so that the logo on the NTP is updated. This check is |
| 299 // outside the reading of the web resource data, because the absence of | 324 // outside the reading of the web resource data, because the absence of |
| 300 // dates counts as a triggering change if there were dates before. | 325 // dates counts as a triggering change if there were dates before. |
| 301 // Also reset the promo closed preference, to signal a new promo. | 326 // Also create new promo groups, and reset the promo closed preference, |
| 302 if (!(old_promo_start == promo_start) || | 327 // to signal a new promo. |
| 303 !(old_promo_end == promo_end)) { | 328 if (old_promo_start != promo_start || old_promo_end != promo_end) { |
| 304 prefs_->SetDouble(prefs::kNTPPromoStart, promo_start); | 329 OnNewPromo(promo_start, promo_end); |
| 305 prefs_->SetDouble(prefs::kNTPPromoEnd, promo_end); | |
| 306 prefs_->SetBoolean(prefs::kNTPPromoClosed, false); | |
| 307 ScheduleNotification(promo_start, promo_end); | |
| 308 } | 330 } |
| 309 } | 331 } |
| 332 void PromoResourceService::CalculateNewPromoTimes( |
| 333 const std::string& promo_start_string, |
| 334 const std::string& promo_end_string, |
| 335 double* promo_start, |
| 336 double* promo_end) { |
| 337 *promo_start = *promo_end = 0.0; |
| 338 |
| 339 if (promo_start_string.empty() && !promo_end_string.empty()) |
| 340 return; |
| 341 |
| 342 base::Time start_time, end_time; |
| 343 if (!base::Time::FromString(promo_start_string.c_str(), &start_time) || |
| 344 !base::Time::FromString(promo_end_string.c_str(), &end_time)) |
| 345 return; |
| 346 |
| 347 *promo_start = start_time.ToDoubleT(); |
| 348 *promo_end = end_time.ToDoubleT(); |
| 349 } |
| 350 |
| 351 void PromoResourceService::GetCurrentPromoTimes(double* old_promo_start, |
| 352 double* old_promo_end) { |
| 353 *old_promo_start = *old_promo_end = 0.0; |
| 354 if (prefs_->HasPrefPath(prefs::kNTPPromoStart) && |
| 355 prefs_->HasPrefPath(prefs::kNTPPromoEnd)) { |
| 356 *old_promo_start = prefs_->GetDouble(prefs::kNTPPromoStart); |
| 357 *old_promo_end = prefs_->GetDouble(prefs::kNTPPromoEnd); |
| 358 } |
| 359 } |
| 360 |
| 361 int PromoResourceService::ResetPromoGroup() { |
| 362 srand(static_cast<uint32>(time(NULL))); |
| 363 const int promo_group = rand() % kNTPPromoGroupSize; |
| 364 prefs_->SetInteger(prefs::kNTPPromoGroup, promo_group); |
| 365 return promo_group; |
| 366 } |
| 367 |
| 368 // static |
| 369 double PromoResourceService::GetPromoStartTime(PrefService* prefs) { |
| 370 if (!prefs->HasPrefPath(prefs::kNTPPromoStart)) |
| 371 return 0.0; |
| 372 |
| 373 const double promo_start = prefs->GetDouble(prefs::kNTPPromoStart); |
| 374 |
| 375 if (!prefs->HasPrefPath(prefs::kNTPPromoGroup) || |
| 376 !prefs->HasPrefPath(prefs::kNTPPromoGroupTimeSlice)) |
| 377 return promo_start; |
| 378 |
| 379 const int promo_group = prefs->GetInteger(prefs::kNTPPromoGroup); |
| 380 const int time_slice = prefs->GetInteger(prefs::kNTPPromoGroupTimeSlice); |
| 381 // Adjust promo_start using group time slice, adjusted from hours to seconds. |
| 382 return promo_start + promo_group * time_slice * 60 * 60; |
| 383 } |
| 384 |
| 385 void PromoResourceService::OnNewPromo(double promo_start, double promo_end) { |
| 386 ResetPromoGroup(); |
| 387 |
| 388 prefs_->SetBoolean(prefs::kNTPPromoClosed, false); |
| 389 |
| 390 prefs_->SetDouble(prefs::kNTPPromoStart, promo_start); |
| 391 prefs_->SetDouble(prefs::kNTPPromoEnd, promo_end); |
| 392 |
| 393 ScheduleNotification(GetPromoStartTime(prefs_), promo_end); |
| 394 } |
| 395 |
| 396 bool PromoResourceService::CanShowPromo(Profile* profile) { |
| 397 PrefService* prefs = profile->GetPrefs(); |
| 398 |
| 399 // Check if promo has been closed by the user. |
| 400 if (prefs->HasPrefPath(prefs::kNTPPromoClosed) && |
| 401 prefs->GetBoolean(prefs::kNTPPromoClosed)) |
| 402 return false; |
| 403 |
| 404 // Check if our build is appropriate for this promo. |
| 405 if (!prefs->HasPrefPath(prefs::kNTPPromoBuild) || |
| 406 !IsBuildTargeted(GetChannel(), prefs->GetInteger(prefs::kNTPPromoBuild))) |
| 407 return false; |
| 408 |
| 409 // Check if we are in the right group for this promo. |
| 410 if (!prefs->FindPreference(prefs::kNTPPromoGroup) || |
| 411 !prefs->FindPreference(prefs::kNTPPromoGroupMax) || |
| 412 (prefs->GetInteger(prefs::kNTPPromoGroup) >= |
| 413 prefs->GetInteger(prefs::kNTPPromoGroupMax))) |
| 414 return false; |
| 415 |
| 416 // Check if we are in the right time window for this promo. |
| 417 if (!prefs->FindPreference(prefs::kNTPPromoStart) || |
| 418 !prefs->FindPreference(prefs::kNTPPromoEnd) || |
| 419 base::Time::FromDoubleT(GetPromoStartTime(prefs)) > |
| 420 base::Time::Now() || |
| 421 base::Time::FromDoubleT(prefs->GetDouble(prefs::kNTPPromoEnd)) < |
| 422 base::Time::Now()) |
| 423 return false; |
| 424 |
| 425 return prefs->HasPrefPath(prefs::kNTPPromoLine); |
| 426 } |
| 310 | 427 |
| 311 void PromoResourceService::UnpackWebStoreSignal( | 428 void PromoResourceService::UnpackWebStoreSignal( |
| 312 const DictionaryValue& parsed_json) { | 429 const DictionaryValue& parsed_json) { |
| 313 DictionaryValue* topic_dict; | 430 DictionaryValue* topic_dict; |
| 314 ListValue* answer_list; | 431 ListValue* answer_list; |
| 315 | 432 |
| 316 bool is_webstore_active = false; | 433 bool is_webstore_active = false; |
| 317 bool signal_found = false; | 434 bool signal_found = false; |
| 318 std::string promo_id = ""; | 435 std::string promo_id = ""; |
| 319 std::string promo_header = ""; | 436 std::string promo_header = ""; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 if (!(old_logo_start == logo_start) || | 574 if (!(old_logo_start == logo_start) || |
| 458 !(old_logo_end == logo_end)) { | 575 !(old_logo_end == logo_end)) { |
| 459 prefs_->SetDouble(prefs::kNTPCustomLogoStart, logo_start); | 576 prefs_->SetDouble(prefs::kNTPCustomLogoStart, logo_start); |
| 460 prefs_->SetDouble(prefs::kNTPCustomLogoEnd, logo_end); | 577 prefs_->SetDouble(prefs::kNTPCustomLogoEnd, logo_end); |
| 461 NotificationService* service = NotificationService::current(); | 578 NotificationService* service = NotificationService::current(); |
| 462 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, | 579 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, |
| 463 Source<WebResourceService>(this), | 580 Source<WebResourceService>(this), |
| 464 NotificationService::NoDetails()); | 581 NotificationService::NoDetails()); |
| 465 } | 582 } |
| 466 } | 583 } |
| 467 | |
| 468 namespace PromoResourceServiceUtil { | |
| 469 | |
| 470 bool CanShowPromo(Profile* profile) { | |
| 471 bool promo_closed = false; | |
| 472 PrefService* prefs = profile->GetPrefs(); | |
| 473 if (prefs->HasPrefPath(prefs::kNTPPromoClosed)) | |
| 474 promo_closed = prefs->GetBoolean(prefs::kNTPPromoClosed); | |
| 475 | |
| 476 // Only show if not synced. | |
| 477 bool is_synced = | |
| 478 (profile->HasProfileSyncService() && | |
| 479 sync_ui_util::GetStatus( | |
| 480 profile->GetProfileSyncService()) == sync_ui_util::SYNCED); | |
| 481 | |
| 482 bool is_promo_build = false; | |
| 483 if (prefs->HasPrefPath(prefs::kNTPPromoBuild)) { | |
| 484 // GetChannel hits the registry on Windows. See http://crbug.com/70898. | |
| 485 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 486 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); | |
| 487 is_promo_build = PromoResourceService::IsBuildTargeted( | |
| 488 channel, prefs->GetInteger(prefs::kNTPPromoBuild)); | |
| 489 } | |
| 490 | |
| 491 return !promo_closed && !is_synced && is_promo_build; | |
| 492 } | |
| 493 | |
| 494 } // namespace PromoResourceServiceUtil | |
| OLD | NEW |