Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: chrome/browser/web_resource/promo_resource_service.cc

Issue 7655008: promo_resource_service fixes/cleanup for promos. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: round of fixing nits Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 ALL_BUILDS,
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
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 UnpackNotificationSignal(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
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::UnpackNotificationSignal(
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(
259 DictionaryValue* a_dic,
260 std::string* promo_start_string,
261 std::string* promo_end_string) {
262 std::string promo_signal;
263 if (a_dic->GetString("name", &promo_signal)) {
264 if (promo_signal == "promo_start") {
265 SetNotificationParams(a_dic);
266 SetNotificationLine(a_dic);
267
268 a_dic->GetString("inproduct", promo_start_string);
269 } else if (promo_signal == "promo_end") {
270 a_dic->GetString("inproduct", promo_end_string);
271 }
272 }
273 }
274
275 void PromoResourceService::SetNotificationParams(DictionaryValue* a_dic) {
276 std::string question;
277 a_dic->GetString("question", &question);
278
279 size_t index(0);
280 bool err(false);
281 int promo_build = GetNextQuestionValue(question, &index, &err);
282 int time_slice = GetNextQuestionValue(question, &index, &err);
283 int max_group = GetNextQuestionValue(question, &index, &err);
284
285 if (err ||
286 promo_build < 0 ||
287 promo_build > ALL_BUILDS ||
288 time_slice < 0 ||
289 time_slice > kMaxTimeSliceHours ||
290 max_group < 0 ||
291 max_group >= kNTPPromoGroupSize) {
292 // If values are not valid, do not show promo.
293 NOTREACHED() << "Invalid server data, question=" << question <<
294 ", build=" << promo_build <<
295 ", time_slice=" << time_slice <<
296 ", max_group=" << max_group;
297 promo_build = NO_BUILD;
298 time_slice = 0;
299 max_group = 0;
300 }
301
302 prefs_->SetInteger(prefs::kNTPPromoBuild, promo_build);
303 prefs_->SetInteger(prefs::kNTPPromoGroupTimeSlice, time_slice);
304 prefs_->SetInteger(prefs::kNTPPromoGroupMax, max_group);
305 }
306
307 void PromoResourceService::SetNotificationLine(DictionaryValue* a_dic) {
308 std::string promo_line;
309 a_dic->GetString("tooltip", &promo_line);
310 if (!promo_line.empty())
311 prefs_->SetString(prefs::kNTPPromoLine, promo_line);
312 }
313
314 void PromoResourceService::CheckForNewNotification(
315 const std::string& promo_start_string,
316 const std::string& promo_end_string) {
317 double promo_start = 0.0, promo_end = 0.0;
318 ParseNewNotificationTimes(promo_start_string, promo_end_string,
319 &promo_start, &promo_end);
320
321 double old_promo_start = 0.0, old_promo_end = 0.0;
322 GetCurrentNotificationTimes(&old_promo_start, &old_promo_end);
323
298 // If start or end times have changed, trigger a new web resource 324 // 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 325 // 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 326 // outside the reading of the web resource data, because the absence of
301 // dates counts as a triggering change if there were dates before. 327 // dates counts as a triggering change if there were dates before.
302 // Also reset the promo closed preference, to signal a new promo. 328 // Also create new promo groups, and reset the promo closed preference,
303 if (!(old_promo_start == promo_start) || 329 // to signal a new promo.
304 !(old_promo_end == promo_end)) { 330 if (old_promo_start != promo_start || old_promo_end != promo_end)
305 prefs_->SetDouble(prefs::kNTPPromoStart, promo_start); 331 OnNewNotification(promo_start, promo_end);
306 prefs_->SetDouble(prefs::kNTPPromoEnd, promo_end); 332 }
jstritar 2011/09/19 22:20:54 nit: new line
achuithb 2011/09/19 22:57:56 Done.
307 prefs_->SetBoolean(prefs::kNTPPromoClosed, false); 333 void PromoResourceService::ParseNewNotificationTimes(
308 ScheduleNotification(promo_start, promo_end); 334 const std::string& promo_start_string,
309 } 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) {
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 static const double kSecondsInHour = 60.0 * 60.0;
384 return promo_start + promo_group * time_slice * kSecondsInHour;
385 }
386
387 void PromoResourceService::OnNewNotification(double promo_start,
388 double promo_end) {
389 ResetNotificationGroup();
390
391 prefs_->SetBoolean(prefs::kNTPPromoClosed, false);
392
393 prefs_->SetDouble(prefs::kNTPPromoStart, promo_start);
394 prefs_->SetDouble(prefs::kNTPPromoEnd, promo_end);
395
396 ScheduleNotification(GetNotificationStartTime(prefs_), promo_end);
397 }
398
399 bool PromoResourceService::CanShowNotificationPromo(Profile* profile) {
400 PrefService* prefs = profile->GetPrefs();
401
402 // Check if promo has been closed by the user.
403 if (prefs->HasPrefPath(prefs::kNTPPromoClosed) &&
404 prefs->GetBoolean(prefs::kNTPPromoClosed))
405 return false;
406
407 // Check if our build is appropriate for this promo.
408 if (!prefs->HasPrefPath(prefs::kNTPPromoBuild) ||
409 !IsBuildTargeted(GetChannel(), prefs->GetInteger(prefs::kNTPPromoBuild)))
410 return false;
411
412 // Check if we are in the right group for this promo.
413 if (!prefs->FindPreference(prefs::kNTPPromoGroup) ||
414 !prefs->FindPreference(prefs::kNTPPromoGroupMax) ||
415 (prefs->GetInteger(prefs::kNTPPromoGroup) >=
416 prefs->GetInteger(prefs::kNTPPromoGroupMax)))
417 return false;
418
419 // Check if we are in the right time window for this promo.
420 if (!prefs->FindPreference(prefs::kNTPPromoStart) ||
421 !prefs->FindPreference(prefs::kNTPPromoEnd) ||
422 base::Time::FromDoubleT(GetNotificationStartTime(prefs)) >
423 base::Time::Now() ||
424 base::Time::FromDoubleT(prefs->GetDouble(prefs::kNTPPromoEnd)) <
425 base::Time::Now())
426 return false;
427
428 return prefs->HasPrefPath(prefs::kNTPPromoLine);
310 } 429 }
311 430
312 void PromoResourceService::UnpackWebStoreSignal( 431 void PromoResourceService::UnpackWebStoreSignal(
313 const DictionaryValue& parsed_json) { 432 const DictionaryValue& parsed_json) {
314 DictionaryValue* topic_dict; 433 DictionaryValue* topic_dict;
315 ListValue* answer_list; 434 ListValue* answer_list;
316 435
317 bool is_webstore_active = false; 436 bool is_webstore_active = false;
318 bool signal_found = false; 437 bool signal_found = false;
319 AppsPromo::PromoData promo_data; 438 AppsPromo::PromoData promo_data;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 if (!(old_logo_start == logo_start) || 570 if (!(old_logo_start == logo_start) ||
452 !(old_logo_end == logo_end)) { 571 !(old_logo_end == logo_end)) {
453 prefs_->SetDouble(prefs::kNTPCustomLogoStart, logo_start); 572 prefs_->SetDouble(prefs::kNTPCustomLogoStart, logo_start);
454 prefs_->SetDouble(prefs::kNTPCustomLogoEnd, logo_end); 573 prefs_->SetDouble(prefs::kNTPCustomLogoEnd, logo_end);
455 NotificationService* service = NotificationService::current(); 574 NotificationService* service = NotificationService::current();
456 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, 575 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED,
457 Source<WebResourceService>(this), 576 Source<WebResourceService>(this),
458 NotificationService::NoDetails()); 577 NotificationService::NoDetails());
459 } 578 }
460 } 579 }
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698