Chromium Code Reviews| 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 profile_(profile), | 159 profile_(profile), |
| 136 web_resource_cache_(NULL), | 160 channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) { |
| 137 channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) { | |
| 138 Init(); | 161 Init(); |
| 139 } | 162 } |
| 140 | 163 |
| 141 PromoResourceService::~PromoResourceService() { } | 164 PromoResourceService::~PromoResourceService() { } |
| 142 | 165 |
| 143 void PromoResourceService::Init() { | 166 void PromoResourceService::Init() { |
| 144 ScheduleNotificationOnInit(); | 167 ScheduleNotificationOnInit(); |
| 145 } | 168 } |
| 146 | 169 |
| 147 bool PromoResourceService::IsThisBuildTargeted(int builds_targeted) { | 170 bool PromoResourceService::IsThisBuildTargeted(int builds_targeted) { |
| 148 if (channel_ == chrome::VersionInfo::CHANNEL_UNKNOWN) { | 171 if (channel_ == chrome::VersionInfo::CHANNEL_UNKNOWN) |
| 149 // GetChannel hits the registry on Windows. See http://crbug.com/70898. | 172 channel_ = GetChannel(); |
| 150 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 151 channel_ = chrome::VersionInfo::GetChannel(); | |
| 152 } | |
| 153 | 173 |
| 154 return IsBuildTargeted(channel_, builds_targeted); | 174 return IsBuildTargeted(channel_, builds_targeted); |
| 155 } | 175 } |
| 156 | 176 |
| 157 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { | 177 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { |
| 158 UnpackLogoSignal(parsed_json); | 178 UnpackLogoSignal(parsed_json); |
| 159 UnpackPromoSignal(parsed_json); | 179 UnpackAdSignal(parsed_json); |
| 160 UnpackWebStoreSignal(parsed_json); | 180 UnpackWebStoreSignal(parsed_json); |
| 161 } | 181 } |
| 162 | 182 |
| 163 void PromoResourceService::ScheduleNotification(double promo_start, | 183 void PromoResourceService::ScheduleNotification(double promo_start, |
| 164 double promo_end) { | 184 double promo_end) { |
| 165 if (promo_start > 0 && promo_end > 0) { | 185 if (promo_start > 0 && promo_end > 0) { |
| 166 int64 ms_until_start = | 186 int64 ms_until_start = |
| 167 static_cast<int64>((base::Time::FromDoubleT( | 187 static_cast<int64>((base::Time::FromDoubleT( |
| 168 promo_start) - base::Time::Now()).InMilliseconds()); | 188 promo_start) - base::Time::Now()).InMilliseconds()); |
| 169 int64 ms_until_end = | 189 int64 ms_until_end = |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 int PromoResourceService::GetPromoServiceVersion() { | 225 int PromoResourceService::GetPromoServiceVersion() { |
| 206 PrefService* local_state = g_browser_process->local_state(); | 226 PrefService* local_state = g_browser_process->local_state(); |
| 207 return local_state->GetInteger(prefs::kNTPPromoVersion); | 227 return local_state->GetInteger(prefs::kNTPPromoVersion); |
| 208 } | 228 } |
| 209 | 229 |
| 210 std::string PromoResourceService::GetPromoLocale() { | 230 std::string PromoResourceService::GetPromoLocale() { |
| 211 PrefService* local_state = g_browser_process->local_state(); | 231 PrefService* local_state = g_browser_process->local_state(); |
| 212 return local_state->GetString(prefs::kNTPPromoLocale); | 232 return local_state->GetString(prefs::kNTPPromoLocale); |
| 213 } | 233 } |
| 214 | 234 |
| 215 void PromoResourceService::UnpackPromoSignal( | 235 void PromoResourceService::UnpackAdSignal( |
|
Miranda Callahan
2011/09/15 08:21:30
nit:UnpackNotificationSignal
achuithb
2011/09/15 18:58:13
Done.
| |
| 216 const DictionaryValue& parsed_json) { | 236 const DictionaryValue& parsed_json) { |
| 237 // Check for newly received start and end values. | |
| 238 std::string promo_start_string, promo_end_string; | |
| 239 | |
| 217 DictionaryValue* topic_dict; | 240 DictionaryValue* topic_dict; |
| 218 ListValue* answer_list; | |
| 219 double old_promo_start = 0; | |
| 220 double old_promo_end = 0; | |
| 221 double promo_start = 0; | |
| 222 double promo_end = 0; | |
| 223 | |
| 224 // Check for preexisting start and end values. | |
| 225 if (prefs_->HasPrefPath(prefs::kNTPPromoStart) && | |
| 226 prefs_->HasPrefPath(prefs::kNTPPromoEnd)) { | |
| 227 old_promo_start = prefs_->GetDouble(prefs::kNTPPromoStart); | |
| 228 old_promo_end = prefs_->GetDouble(prefs::kNTPPromoEnd); | |
| 229 } | |
| 230 | |
| 231 // Check for newly received start and end values. | |
| 232 if (parsed_json.GetDictionary("topic", &topic_dict)) { | 241 if (parsed_json.GetDictionary("topic", &topic_dict)) { |
| 242 ListValue* answer_list; | |
| 233 if (topic_dict->GetList("answers", &answer_list)) { | 243 if (topic_dict->GetList("answers", &answer_list)) { |
| 234 std::string promo_start_string = ""; | |
| 235 std::string promo_end_string = ""; | |
| 236 std::string promo_string = ""; | |
| 237 std::string promo_build = ""; | |
| 238 int promo_build_type = 0; | |
| 239 int time_slice_hrs = 0; | |
| 240 for (ListValue::const_iterator answer_iter = answer_list->begin(); | 244 for (ListValue::const_iterator answer_iter = answer_list->begin(); |
| 241 answer_iter != answer_list->end(); ++answer_iter) { | 245 answer_iter != answer_list->end(); ++answer_iter) { |
| 242 if (!(*answer_iter)->IsType(Value::TYPE_DICTIONARY)) | 246 if (!(*answer_iter)->IsType(Value::TYPE_DICTIONARY)) |
| 243 continue; | 247 continue; |
| 244 DictionaryValue* a_dic = | 248 |
| 245 static_cast<DictionaryValue*>(*answer_iter); | 249 ParseNotification(static_cast<DictionaryValue*>(*answer_iter), |
| 246 std::string promo_signal; | 250 &promo_start_string, &promo_end_string); |
| 247 if (a_dic->GetString("name", &promo_signal)) { | |
| 248 if (promo_signal == "promo_start") { | |
| 249 a_dic->GetString("question", &promo_build); | |
| 250 size_t split = promo_build.find(":"); | |
| 251 if (split != std::string::npos && | |
| 252 base::StringToInt(promo_build.substr(0, split), | |
| 253 &promo_build_type) && | |
| 254 base::StringToInt(promo_build.substr(split+1), | |
| 255 &time_slice_hrs) && | |
| 256 promo_build_type >= 0 && | |
| 257 promo_build_type <= (DEV_BUILD | BETA_BUILD | STABLE_BUILD) && | |
| 258 time_slice_hrs >= 0 && | |
| 259 time_slice_hrs <= kMaxTimeSliceHours) { | |
| 260 prefs_->SetInteger(prefs::kNTPPromoBuild, promo_build_type); | |
| 261 prefs_->SetInteger(prefs::kNTPPromoGroupTimeSlice, | |
| 262 time_slice_hrs); | |
| 263 } else { | |
| 264 // If no time data or bad time data are set, do not show promo. | |
| 265 prefs_->SetInteger(prefs::kNTPPromoBuild, NO_BUILD); | |
| 266 prefs_->SetInteger(prefs::kNTPPromoGroupTimeSlice, 0); | |
| 267 } | |
| 268 a_dic->GetString("inproduct", &promo_start_string); | |
| 269 a_dic->GetString("tooltip", &promo_string); | |
| 270 prefs_->SetString(prefs::kNTPPromoLine, promo_string); | |
| 271 srand(static_cast<uint32>(time(NULL))); | |
| 272 prefs_->SetInteger(prefs::kNTPPromoGroup, | |
| 273 rand() % kNTPPromoGroupSize); | |
| 274 } else if (promo_signal == "promo_end") { | |
| 275 a_dic->GetString("inproduct", &promo_end_string); | |
| 276 } | |
| 277 } | |
| 278 } | |
| 279 if (!promo_start_string.empty() && | |
| 280 promo_start_string.length() > 0 && | |
| 281 !promo_end_string.empty() && | |
| 282 promo_end_string.length() > 0) { | |
| 283 base::Time start_time; | |
| 284 base::Time end_time; | |
| 285 if (base::Time::FromString(promo_start_string.c_str(), &start_time) && | |
| 286 base::Time::FromString(promo_end_string.c_str(), &end_time)) { | |
| 287 // Add group time slice, adjusted from hours to seconds. | |
| 288 promo_start = start_time.ToDoubleT() + | |
| 289 (prefs_->FindPreference(prefs::kNTPPromoGroup) ? | |
| 290 prefs_->GetInteger(prefs::kNTPPromoGroup) * | |
| 291 time_slice_hrs * 60 * 60 : 0); | |
| 292 promo_end = end_time.ToDoubleT(); | |
| 293 } | |
| 294 } | 251 } |
| 295 } | 252 } |
| 296 } | 253 } |
| 297 | 254 |
| 255 CheckForNewNotification(promo_start_string, promo_end_string); | |
| 256 } | |
| 257 | |
| 258 void PromoResourceService::ParseNotification(DictionaryValue* a_dic, | |
|
jstritar
2011/09/15 15:42:27
nit: DictionaryValue* a_dic should be on the next
achuithb
2011/09/15 18:58:13
Done.
| |
| 259 std::string* promo_start_string, | |
| 260 std::string* promo_end_string) { | |
| 261 std::string promo_signal; | |
| 262 if (a_dic->GetString("name", &promo_signal)) { | |
| 263 if (promo_signal == "promo_start") { | |
| 264 SetNotificationParams(a_dic); | |
| 265 SetNotificationLine(a_dic); | |
| 266 | |
| 267 a_dic->GetString("inproduct", promo_start_string); | |
| 268 } else if (promo_signal == "promo_end") { | |
| 269 a_dic->GetString("inproduct", promo_end_string); | |
| 270 } | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 void PromoResourceService::SetNotificationParams(DictionaryValue* a_dic) { | |
| 275 std::string question; | |
| 276 a_dic->GetString("question", &question); | |
| 277 | |
| 278 size_t index(0); | |
| 279 bool err(false); | |
| 280 int promo_build = GetNextQuestionValue(question, &index, &err); | |
| 281 int time_slice = GetNextQuestionValue(question, &index, &err); | |
| 282 int max_group = GetNextQuestionValue(question, &index, &err); | |
| 283 | |
| 284 if (err || | |
| 285 promo_build < 0 || | |
| 286 promo_build > (DEV_BUILD | BETA_BUILD | STABLE_BUILD) || | |
|
jstritar
2011/09/15 15:42:27
There's also a CANARY_BUILD now. How about we defi
achuithb
2011/09/15 18:58:13
Done.
| |
| 287 time_slice < 0 || | |
| 288 time_slice > kMaxTimeSliceHours || | |
| 289 max_group < 0 || | |
| 290 max_group >= kNTPPromoGroupSize) { | |
| 291 // If values are not valid, do not show promo. | |
| 292 NOTREACHED() << "Invalid server data, question=" << question << | |
| 293 ", build=" << promo_build << | |
| 294 ", time_slice=" << time_slice << | |
| 295 ", max_group=" << max_group; | |
| 296 promo_build = NO_BUILD; | |
| 297 time_slice = 0; | |
| 298 max_group = 0; | |
| 299 } | |
| 300 | |
| 301 prefs_->SetInteger(prefs::kNTPPromoBuild, promo_build); | |
| 302 prefs_->SetInteger(prefs::kNTPPromoGroupTimeSlice, time_slice); | |
| 303 prefs_->SetInteger(prefs::kNTPPromoGroupMax, max_group); | |
| 304 } | |
| 305 | |
| 306 void PromoResourceService::SetNotificationLine(DictionaryValue* a_dic) { | |
| 307 std::string promo_line; | |
| 308 a_dic->GetString("tooltip", &promo_line); | |
| 309 if (!promo_line.empty()) | |
| 310 prefs_->SetString(prefs::kNTPPromoLine, promo_line); | |
| 311 } | |
| 312 | |
| 313 void PromoResourceService::CheckForNewNotification( | |
| 314 const std::string& promo_start_string, | |
| 315 const std::string& promo_end_string) { | |
| 316 double promo_start = 0.0, promo_end = 0.0; | |
| 317 CalculateNewNotificationTimes(promo_start_string, promo_end_string, | |
| 318 &promo_start, &promo_end); | |
| 319 | |
| 320 double old_promo_start = 0.0, old_promo_end = 0.0; | |
| 321 GetCurrentNotificationTimes(&old_promo_start, &old_promo_end); | |
| 322 | |
| 298 // If start or end times have changed, trigger a new web resource | 323 // If start or end times have changed, trigger a new web resource |
| 299 // notification, so that the logo on the NTP is updated. This check is | 324 // notification, so that the logo on the NTP is updated. This check is |
| 300 // outside the reading of the web resource data, because the absence of | 325 // outside the reading of the web resource data, because the absence of |
| 301 // dates counts as a triggering change if there were dates before. | 326 // dates counts as a triggering change if there were dates before. |
| 302 // Also reset the promo closed preference, to signal a new promo. | 327 // Also create new promo groups, and reset the promo closed preference, |
| 303 if (!(old_promo_start == promo_start) || | 328 // to signal a new promo. |
| 304 !(old_promo_end == promo_end)) { | 329 if (old_promo_start != promo_start || old_promo_end != promo_end) { |
| 305 prefs_->SetDouble(prefs::kNTPPromoStart, promo_start); | 330 OnNewNotification(promo_start, promo_end); |
|
jstritar
2011/09/15 15:42:27
nit: don't need the { }
achuithb
2011/09/15 18:58:13
Done.
| |
| 306 prefs_->SetDouble(prefs::kNTPPromoEnd, promo_end); | 331 } |
| 307 prefs_->SetBoolean(prefs::kNTPPromoClosed, false); | 332 } |
| 308 ScheduleNotification(promo_start, promo_end); | 333 void PromoResourceService::CalculateNewNotificationTimes( |
|
jstritar
2011/09/15 15:42:27
Would ParseNewNotificationTimes be a better name,
achuithb
2011/09/15 18:58:13
Done.
| |
| 309 } | 334 const std::string& promo_start_string, |
| 335 const std::string& promo_end_string, | |
| 336 double* promo_start, | |
| 337 double* promo_end) { | |
| 338 *promo_start = *promo_end = 0.0; | |
| 339 | |
| 340 if (promo_start_string.empty() && !promo_end_string.empty()) | |
| 341 return; | |
| 342 | |
| 343 base::Time start_time, end_time; | |
| 344 if (!base::Time::FromString(promo_start_string.c_str(), &start_time) || | |
| 345 !base::Time::FromString(promo_end_string.c_str(), &end_time)) | |
| 346 return; | |
| 347 | |
| 348 *promo_start = start_time.ToDoubleT(); | |
| 349 *promo_end = end_time.ToDoubleT(); | |
| 350 } | |
| 351 | |
| 352 void PromoResourceService::GetCurrentNotificationTimes(double* old_promo_start, | |
| 353 double* old_promo_end) { | |
|
jstritar
2011/09/15 15:42:27
nit: indent
achuithb
2011/09/15 18:58:13
Done.
| |
| 354 *old_promo_start = *old_promo_end = 0.0; | |
| 355 if (prefs_->HasPrefPath(prefs::kNTPPromoStart) && | |
| 356 prefs_->HasPrefPath(prefs::kNTPPromoEnd)) { | |
| 357 *old_promo_start = prefs_->GetDouble(prefs::kNTPPromoStart); | |
| 358 *old_promo_end = prefs_->GetDouble(prefs::kNTPPromoEnd); | |
| 359 } | |
| 360 } | |
| 361 | |
| 362 int PromoResourceService::ResetNotificationGroup() { | |
| 363 srand(static_cast<uint32>(time(NULL))); | |
| 364 const int promo_group = rand() % kNTPPromoGroupSize; | |
| 365 prefs_->SetInteger(prefs::kNTPPromoGroup, promo_group); | |
| 366 return promo_group; | |
| 367 } | |
| 368 | |
| 369 // static | |
| 370 double PromoResourceService::GetNotificationStartTime(PrefService* prefs) { | |
| 371 if (!prefs->HasPrefPath(prefs::kNTPPromoStart)) | |
| 372 return 0.0; | |
| 373 | |
| 374 const double promo_start = prefs->GetDouble(prefs::kNTPPromoStart); | |
| 375 | |
| 376 if (!prefs->HasPrefPath(prefs::kNTPPromoGroup) || | |
| 377 !prefs->HasPrefPath(prefs::kNTPPromoGroupTimeSlice)) | |
| 378 return promo_start; | |
| 379 | |
| 380 const int promo_group = prefs->GetInteger(prefs::kNTPPromoGroup); | |
| 381 const int time_slice = prefs->GetInteger(prefs::kNTPPromoGroupTimeSlice); | |
| 382 // Adjust promo_start using group time slice, adjusted from hours to seconds. | |
| 383 return promo_start + promo_group * time_slice * 60 * 60; | |
|
jstritar
2011/09/15 15:42:27
nit: maybe put 60*60 in a constant?
achuithb
2011/09/15 18:58:13
Done.
| |
| 384 } | |
| 385 | |
| 386 void PromoResourceService::OnNewNotification(double promo_start, | |
| 387 double promo_end) { | |
| 388 ResetNotificationGroup(); | |
| 389 | |
| 390 prefs_->SetBoolean(prefs::kNTPPromoClosed, false); | |
| 391 | |
| 392 prefs_->SetDouble(prefs::kNTPPromoStart, promo_start); | |
| 393 prefs_->SetDouble(prefs::kNTPPromoEnd, promo_end); | |
| 394 | |
| 395 ScheduleNotification(GetNotificationStartTime(prefs_), promo_end); | |
| 396 } | |
| 397 | |
| 398 bool PromoResourceService::CanShowPromo(Profile* profile) { | |
| 399 PrefService* prefs = profile->GetPrefs(); | |
| 400 | |
| 401 // Check if promo has been closed by the user. | |
| 402 if (prefs->HasPrefPath(prefs::kNTPPromoClosed) && | |
| 403 prefs->GetBoolean(prefs::kNTPPromoClosed)) | |
| 404 return false; | |
| 405 | |
| 406 // Check if our build is appropriate for this promo. | |
| 407 if (!prefs->HasPrefPath(prefs::kNTPPromoBuild) || | |
| 408 !IsBuildTargeted(GetChannel(), prefs->GetInteger(prefs::kNTPPromoBuild))) | |
| 409 return false; | |
| 410 | |
| 411 // Check if we are in the right group for this promo. | |
| 412 if (!prefs->FindPreference(prefs::kNTPPromoGroup) || | |
| 413 !prefs->FindPreference(prefs::kNTPPromoGroupMax) || | |
| 414 (prefs->GetInteger(prefs::kNTPPromoGroup) >= | |
| 415 prefs->GetInteger(prefs::kNTPPromoGroupMax))) | |
| 416 return false; | |
| 417 | |
| 418 // Check if we are in the right time window for this promo. | |
| 419 if (!prefs->FindPreference(prefs::kNTPPromoStart) || | |
| 420 !prefs->FindPreference(prefs::kNTPPromoEnd) || | |
| 421 base::Time::FromDoubleT(GetNotificationStartTime(prefs)) > | |
| 422 base::Time::Now() || | |
| 423 base::Time::FromDoubleT(prefs->GetDouble(prefs::kNTPPromoEnd)) < | |
| 424 base::Time::Now()) | |
| 425 return false; | |
| 426 | |
| 427 return prefs->HasPrefPath(prefs::kNTPPromoLine); | |
| 310 } | 428 } |
| 311 | 429 |
| 312 void PromoResourceService::UnpackWebStoreSignal( | 430 void PromoResourceService::UnpackWebStoreSignal( |
| 313 const DictionaryValue& parsed_json) { | 431 const DictionaryValue& parsed_json) { |
| 314 DictionaryValue* topic_dict; | 432 DictionaryValue* topic_dict; |
| 315 ListValue* answer_list; | 433 ListValue* answer_list; |
| 316 | 434 |
| 317 bool is_webstore_active = false; | 435 bool is_webstore_active = false; |
| 318 bool signal_found = false; | 436 bool signal_found = false; |
| 319 AppsPromo::PromoData promo_data; | 437 AppsPromo::PromoData promo_data; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 451 if (!(old_logo_start == logo_start) || | 569 if (!(old_logo_start == logo_start) || |
| 452 !(old_logo_end == logo_end)) { | 570 !(old_logo_end == logo_end)) { |
| 453 prefs_->SetDouble(prefs::kNTPCustomLogoStart, logo_start); | 571 prefs_->SetDouble(prefs::kNTPCustomLogoStart, logo_start); |
| 454 prefs_->SetDouble(prefs::kNTPCustomLogoEnd, logo_end); | 572 prefs_->SetDouble(prefs::kNTPCustomLogoEnd, logo_end); |
| 455 NotificationService* service = NotificationService::current(); | 573 NotificationService* service = NotificationService::current(); |
| 456 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, | 574 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, |
| 457 Source<WebResourceService>(this), | 575 Source<WebResourceService>(this), |
| 458 NotificationService::NoDetails()); | 576 NotificationService::NoDetails()); |
| 459 } | 577 } |
| 460 } | 578 } |
| 461 | |
| 462 namespace PromoResourceServiceUtil { | |
| 463 | |
| 464 bool CanShowPromo(Profile* profile) { | |
| 465 bool promo_closed = false; | |
| 466 PrefService* prefs = profile->GetPrefs(); | |
| 467 if (prefs->HasPrefPath(prefs::kNTPPromoClosed)) | |
| 468 promo_closed = prefs->GetBoolean(prefs::kNTPPromoClosed); | |
| 469 | |
| 470 // Only show if not synced. | |
| 471 bool is_synced = | |
| 472 (profile->HasProfileSyncService() && | |
| 473 sync_ui_util::GetStatus( | |
| 474 profile->GetProfileSyncService()) == sync_ui_util::SYNCED); | |
| 475 | |
| 476 bool is_promo_build = false; | |
| 477 if (prefs->HasPrefPath(prefs::kNTPPromoBuild)) { | |
| 478 // GetChannel hits the registry on Windows. See http://crbug.com/70898. | |
| 479 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 480 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); | |
| 481 is_promo_build = PromoResourceService::IsBuildTargeted( | |
| 482 channel, prefs->GetInteger(prefs::kNTPPromoBuild)); | |
| 483 } | |
| 484 | |
| 485 return !promo_closed && !is_synced && is_promo_build; | |
| 486 } | |
| 487 | |
| 488 } // namespace PromoResourceServiceUtil | |
| OLD | NEW |