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/notification_promo.h" | 5 #include "chrome/browser/web_resource/notification_promo.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 17 matching lines...) Expand all Loading... | |
28 #endif // defined(OS_ANDROID) | 28 #endif // defined(OS_ANDROID) |
29 | 29 |
30 using content::UserMetricsAction; | 30 using content::UserMetricsAction; |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 const int kDefaultGroupSize = 100; | 34 const int kDefaultGroupSize = 100; |
35 | 35 |
36 const char promo_server_url[] = "https://clients3.google.com/crsignal/client"; | 36 const char promo_server_url[] = "https://clients3.google.com/crsignal/client"; |
37 | 37 |
38 const char kPrefPromoObject[] = "promo"; | |
39 const char kPrefPromoText[] = "text"; | |
40 #if defined(OS_ANDROID) | |
41 const char kPrefPromoTextLong[] = "text_long"; | |
42 const char kPrefPromoActionType[] = "action_type"; | |
43 const char kPrefPromoActionArgs[] = "action_args"; | |
44 #endif | |
45 const char kPrefPromoStart[] = "start"; | |
46 const char kPrefPromoEnd[] = "end"; | |
47 const char kPrefPromoNumGroups[] = "num_groups"; | |
48 const char kPrefPromoSegment[] = "segment"; | |
49 const char kPrefPromoIncrement[] = "increment"; | |
50 const char kPrefPromoIncrementFrequency[] = "increment_frequency"; | |
51 const char kPrefPromoIncrementMax[] = "increment_max"; | |
52 const char kPrefPromoMaxViews[] = "max_views"; | |
53 const char kPrefPromoGroup[] = "group"; | |
54 const char kPrefPromoViews[] = "views"; | |
55 const char kPrefPromoClosed[] = "closed"; | |
56 const char kPrefPromoGPlusRequired[] = "gplus_required"; | |
57 | |
38 #if defined(OS_ANDROID) | 58 #if defined(OS_ANDROID) |
39 const int kCurrentMobilePayloadFormatVersion = 3; | 59 const int kCurrentMobilePayloadFormatVersion = 3; |
40 #endif // defined(OS_ANDROID) | 60 #endif // defined(OS_ANDROID) |
41 | 61 |
42 double GetTimeFromPrefs(PrefService* prefs, const char* pref) { | |
43 return prefs->HasPrefPath(pref) ? prefs->GetDouble(pref) : 0.0; | |
44 } | |
45 | |
46 // Returns a string suitable for the Promo Server URL 'osname' value. | 62 // Returns a string suitable for the Promo Server URL 'osname' value. |
47 std::string PlatformString() { | 63 std::string PlatformString() { |
48 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
49 return "win"; | 65 return "win"; |
50 #elif defined(OS_IOS) | 66 #elif defined(OS_IOS) |
51 // TODO(noyau): add iOS-specific implementation | 67 // TODO(noyau): add iOS-specific implementation |
52 const bool isTablet = false; | 68 const bool isTablet = false; |
53 return std::string("ios-") + (isTablet ? "tablet" : "phone"); | 69 return std::string("ios-") + (isTablet ? "tablet" : "phone"); |
54 #elif defined(OS_MACOSX) | 70 #elif defined(OS_MACOSX) |
55 return "mac"; | 71 return "mac"; |
(...skipping 26 matching lines...) Expand all Loading... | |
82 return "dev"; | 98 return "dev"; |
83 case chrome::VersionInfo::CHANNEL_BETA: | 99 case chrome::VersionInfo::CHANNEL_BETA: |
84 return "beta"; | 100 return "beta"; |
85 case chrome::VersionInfo::CHANNEL_STABLE: | 101 case chrome::VersionInfo::CHANNEL_STABLE: |
86 return "stable"; | 102 return "stable"; |
87 default: | 103 default: |
88 return "none"; | 104 return "none"; |
89 } | 105 } |
90 } | 106 } |
91 | 107 |
108 void ClearDeprecatedPrefs(PrefService* prefs) { | |
Robert Sesek
2012/07/23 20:38:50
Add a TODO for when to remove this.
achuithb
2012/07/23 23:12:02
Done.
| |
109 prefs->RegisterStringPref(prefs::kNtpPromoLine, | |
110 std::string(), | |
111 PrefService::UNSYNCABLE_PREF); | |
112 prefs->ClearPref(prefs::kNtpPromoLine); | |
113 #if defined(OS_ANDROID) | |
114 prefs->RegisterStringPref(prefs::kNtpPromoLineLong, | |
115 std::string(), | |
116 PrefService::UNSYNCABLE_PREF); | |
117 prefs->RegisterStringPref(prefs::kNtpPromoActionType, | |
118 std::string(), | |
119 PrefService::UNSYNCABLE_PREF); | |
120 prefs->RegisterListPref(prefs::kNtpPromoActionArgs, | |
121 new base::ListValue, | |
122 PrefService::UNSYNCABLE_PREF); | |
123 prefs->ClearPref(prefs::kNtpPromoLineLong); | |
124 prefs->ClearPref(prefs::kNtpPromoActionType); | |
125 prefs->ClearPref(prefs::kNtpPromoActionArgs); | |
126 #endif // defined(OS_ANDROID) | |
127 | |
128 prefs->RegisterDoublePref(prefs::kNtpPromoStart, | |
129 0, | |
130 PrefService::UNSYNCABLE_PREF); | |
131 prefs->RegisterDoublePref(prefs::kNtpPromoEnd, | |
132 0, | |
133 PrefService::UNSYNCABLE_PREF); | |
134 | |
135 prefs->RegisterIntegerPref(prefs::kNtpPromoNumGroups, | |
136 0, | |
137 PrefService::UNSYNCABLE_PREF); | |
138 prefs->RegisterIntegerPref(prefs::kNtpPromoInitialSegment, | |
139 0, | |
140 PrefService::UNSYNCABLE_PREF); | |
141 prefs->RegisterIntegerPref(prefs::kNtpPromoIncrement, | |
142 1, | |
143 PrefService::UNSYNCABLE_PREF); | |
144 prefs->RegisterIntegerPref(prefs::kNtpPromoGroupTimeSlice, | |
145 0, | |
146 PrefService::UNSYNCABLE_PREF); | |
147 prefs->RegisterIntegerPref(prefs::kNtpPromoGroupMax, | |
148 0, | |
149 PrefService::UNSYNCABLE_PREF); | |
150 | |
151 prefs->RegisterIntegerPref(prefs::kNtpPromoViewsMax, | |
152 0, | |
153 PrefService::UNSYNCABLE_PREF); | |
154 | |
155 prefs->RegisterIntegerPref(prefs::kNtpPromoGroup, | |
156 0, | |
157 PrefService::UNSYNCABLE_PREF); | |
158 prefs->RegisterIntegerPref(prefs::kNtpPromoViews, | |
159 0, | |
160 PrefService::UNSYNCABLE_PREF); | |
161 prefs->RegisterBooleanPref(prefs::kNtpPromoClosed, | |
162 false, | |
163 PrefService::UNSYNCABLE_PREF); | |
164 | |
165 prefs->RegisterBooleanPref(prefs::kNtpPromoGplusRequired, | |
166 false, | |
167 PrefService::UNSYNCABLE_PREF); | |
168 | |
169 prefs->ClearPref(prefs::kNtpPromoStart); | |
170 prefs->ClearPref(prefs::kNtpPromoEnd); | |
171 prefs->ClearPref(prefs::kNtpPromoNumGroups); | |
172 prefs->ClearPref(prefs::kNtpPromoInitialSegment); | |
173 prefs->ClearPref(prefs::kNtpPromoIncrement); | |
174 prefs->ClearPref(prefs::kNtpPromoGroupTimeSlice); | |
175 prefs->ClearPref(prefs::kNtpPromoGroupMax); | |
176 prefs->ClearPref(prefs::kNtpPromoViewsMax); | |
177 prefs->ClearPref(prefs::kNtpPromoGroup); | |
178 prefs->ClearPref(prefs::kNtpPromoViews); | |
179 prefs->ClearPref(prefs::kNtpPromoClosed); | |
180 prefs->ClearPref(prefs::kNtpPromoGplusRequired); | |
181 } | |
182 | |
92 } // namespace | 183 } // namespace |
93 | 184 |
185 const char NotificationPromo::kNtpNotificationPromoType[] = | |
186 "ntp_notification_promo"; | |
187 const char NotificationPromo::kBubblePromoType[] = "bubble_promo"; | |
188 | |
94 NotificationPromo::NotificationPromo(Profile* profile) | 189 NotificationPromo::NotificationPromo(Profile* profile) |
95 : profile_(profile), | 190 : profile_(profile), |
96 prefs_(profile_->GetPrefs()), | 191 prefs_(profile_->GetPrefs()), |
192 promo_type_(kNtpNotificationPromoType), | |
97 #if defined(OS_ANDROID) | 193 #if defined(OS_ANDROID) |
98 promo_action_args_(new base::ListValue), | 194 promo_action_args_(new base::ListValue), |
99 #endif // defined(OS_ANDROID) | 195 #endif // defined(OS_ANDROID) |
100 start_(0.0), | 196 start_(0.0), |
101 end_(0.0), | 197 end_(0.0), |
102 num_groups_(kDefaultGroupSize), | 198 num_groups_(kDefaultGroupSize), |
103 initial_segment_(0), | 199 initial_segment_(0), |
104 increment_(1), | 200 increment_(1), |
105 time_slice_(0), | 201 time_slice_(0), |
106 max_group_(0), | 202 max_group_(0), |
107 max_views_(0), | 203 max_views_(0), |
108 group_(0), | 204 group_(0), |
109 views_(0), | 205 views_(0), |
110 closed_(false), | 206 closed_(false), |
111 gplus_required_(false), | 207 gplus_required_(false), |
112 new_notification_(false) { | 208 new_notification_(false) { |
113 DCHECK(profile); | 209 DCHECK(profile); |
114 DCHECK(prefs_); | 210 DCHECK(prefs_); |
115 } | 211 } |
116 | 212 |
117 NotificationPromo::~NotificationPromo() {} | 213 NotificationPromo::~NotificationPromo() {} |
118 | 214 |
119 void NotificationPromo::InitFromJson(const DictionaryValue& json) { | 215 void NotificationPromo::InitFromJson(const DictionaryValue& json) { |
120 ListValue* promo_list = NULL; | 216 ListValue* promo_list = NULL; |
121 #if !defined(OS_ANDROID) | 217 #if !defined(OS_ANDROID) |
122 if (!json.GetList("ntp_notification_promo", &promo_list)) | 218 if (!json.GetList(promo_type_, &promo_list)) |
123 return; | 219 return; |
124 #else | 220 #else |
125 if (!json.GetList("mobile_ntp_sync_promo", &promo_list)) { | 221 if (!json.GetList("mobile_ntp_sync_promo", &promo_list)) { |
126 LOG(ERROR) << "Malfromed JSON: not a mobile_ntp_sync_promo"; | 222 LOG(ERROR) << "Malfromed JSON: not a mobile_ntp_sync_promo"; |
127 return; | 223 return; |
128 } | 224 } |
129 #endif // !defined(OS_ANDROID) | 225 #endif // !defined(OS_ANDROID) |
130 | 226 |
131 // No support for multiple promos yet. Only consider the first one. | 227 // No support for multiple promos yet. Only consider the first one. |
132 DictionaryValue* promo = NULL; | 228 DictionaryValue* promo = NULL; |
(...skipping 14 matching lines...) Expand all Loading... | |
147 ListValue* date_list = NULL; | 243 ListValue* date_list = NULL; |
148 if (promo->GetList("date", &date_list)) { | 244 if (promo->GetList("date", &date_list)) { |
149 DictionaryValue* date; | 245 DictionaryValue* date; |
150 if (date_list->GetDictionary(0, &date)) { | 246 if (date_list->GetDictionary(0, &date)) { |
151 std::string time_str; | 247 std::string time_str; |
152 base::Time time; | 248 base::Time time; |
153 if (date->GetString("start", &time_str) && | 249 if (date->GetString("start", &time_str) && |
154 base::Time::FromString(time_str.c_str(), &time)) { | 250 base::Time::FromString(time_str.c_str(), &time)) { |
155 start_ = time.ToDoubleT(); | 251 start_ = time.ToDoubleT(); |
156 DVLOG(1) << "start str=" << time_str | 252 DVLOG(1) << "start str=" << time_str |
157 << ", start_="<< base::DoubleToString(start_); | 253 << ", start_="<< base::DoubleToString(start_); |
158 } | 254 } |
159 if (date->GetString("end", &time_str) && | 255 if (date->GetString("end", &time_str) && |
160 base::Time::FromString(time_str.c_str(), &time)) { | 256 base::Time::FromString(time_str.c_str(), &time)) { |
161 end_ = time.ToDoubleT(); | 257 end_ = time.ToDoubleT(); |
162 DVLOG(1) << "end str =" << time_str | 258 DVLOG(1) << "end str =" << time_str |
163 << ", end_=" << base::DoubleToString(end_); | 259 << ", end_=" << base::DoubleToString(end_); |
164 } | 260 } |
165 } | 261 } |
166 } | 262 } |
167 | 263 |
168 // Grouping. | 264 // Grouping. |
169 DictionaryValue* grouping = NULL; | 265 DictionaryValue* grouping = NULL; |
170 if (promo->GetDictionary("grouping", &grouping)) { | 266 if (promo->GetDictionary("grouping", &grouping)) { |
171 grouping->GetInteger("buckets", &num_groups_); | 267 grouping->GetInteger("buckets", &num_groups_); |
172 grouping->GetInteger("segment", &initial_segment_); | 268 grouping->GetInteger("segment", &initial_segment_); |
173 grouping->GetInteger("increment", &increment_); | 269 grouping->GetInteger("increment", &increment_); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 } | 334 } |
239 promo_action_args_->Append(base::Value::CreateStringValue(value)); | 335 promo_action_args_->Append(base::Value::CreateStringValue(value)); |
240 } | 336 } |
241 } | 337 } |
242 #endif // defined(OS_ANDROID) | 338 #endif // defined(OS_ANDROID) |
243 | 339 |
244 CheckForNewNotification(); | 340 CheckForNewNotification(); |
245 } | 341 } |
246 | 342 |
247 void NotificationPromo::CheckForNewNotification() { | 343 void NotificationPromo::CheckForNewNotification() { |
248 const double old_start = GetTimeFromPrefs(prefs_, prefs::kNtpPromoStart); | 344 NotificationPromo old_promo(profile_); |
249 const double old_end = GetTimeFromPrefs(prefs_, prefs::kNtpPromoEnd); | 345 old_promo.InitFromPrefs(); |
250 const std::string old_promo_text = prefs_->GetString(prefs::kNtpPromoLine); | 346 const double old_start = old_promo.start_; |
347 const double old_end = old_promo.end_; | |
348 const std::string old_promo_text = old_promo.promo_text_; | |
251 | 349 |
252 new_notification_ = | 350 new_notification_ = |
253 old_start != start_ || old_end != end_ || old_promo_text != promo_text_; | 351 old_start != start_ || old_end != end_ || old_promo_text != promo_text_; |
254 if (new_notification_) | 352 if (new_notification_) |
255 OnNewNotification(); | 353 OnNewNotification(); |
256 } | 354 } |
257 | 355 |
258 void NotificationPromo::OnNewNotification() { | 356 void NotificationPromo::OnNewNotification() { |
259 // Create a new promo group. | 357 // Create a new promo group. |
260 group_ = base::RandInt(0, num_groups_ - 1); | 358 group_ = base::RandInt(0, num_groups_ - 1); |
261 WritePrefs(); | 359 WritePrefs(); |
262 } | 360 } |
263 | 361 |
264 // static | 362 // static |
265 void NotificationPromo::RegisterUserPrefs(PrefService* prefs) { | 363 void NotificationPromo::RegisterUserPrefs(PrefService* prefs) { |
266 prefs->RegisterStringPref(prefs::kNtpPromoLine, | 364 ClearDeprecatedPrefs(prefs); |
267 std::string(), | 365 prefs->RegisterDictionaryPref("promo", |
268 PrefService::UNSYNCABLE_PREF); | 366 new base::DictionaryValue, |
269 #if defined(OS_ANDROID) | 367 PrefService::UNSYNCABLE_PREF); |
270 prefs->RegisterStringPref(prefs::kNtpPromoLineLong, | |
271 std::string(), | |
272 PrefService::UNSYNCABLE_PREF); | |
273 prefs->RegisterStringPref(prefs::kNtpPromoActionType, | |
274 std::string(), | |
275 PrefService::UNSYNCABLE_PREF); | |
276 prefs->RegisterListPref(prefs::kNtpPromoActionArgs, | |
277 new base::ListValue, | |
278 PrefService::UNSYNCABLE_PREF); | |
279 #endif // defined(OS_ANDROID) | |
280 | |
281 prefs->RegisterDoublePref(prefs::kNtpPromoStart, | |
282 0, | |
283 PrefService::UNSYNCABLE_PREF); | |
284 prefs->RegisterDoublePref(prefs::kNtpPromoEnd, | |
285 0, | |
286 PrefService::UNSYNCABLE_PREF); | |
287 | |
288 prefs->RegisterIntegerPref(prefs::kNtpPromoNumGroups, | |
289 0, | |
290 PrefService::UNSYNCABLE_PREF); | |
291 prefs->RegisterIntegerPref(prefs::kNtpPromoInitialSegment, | |
292 0, | |
293 PrefService::UNSYNCABLE_PREF); | |
294 prefs->RegisterIntegerPref(prefs::kNtpPromoIncrement, | |
295 1, | |
296 PrefService::UNSYNCABLE_PREF); | |
297 prefs->RegisterIntegerPref(prefs::kNtpPromoGroupTimeSlice, | |
298 0, | |
299 PrefService::UNSYNCABLE_PREF); | |
300 prefs->RegisterIntegerPref(prefs::kNtpPromoGroupMax, | |
301 0, | |
302 PrefService::UNSYNCABLE_PREF); | |
303 | |
304 prefs->RegisterIntegerPref(prefs::kNtpPromoViewsMax, | |
305 0, | |
306 PrefService::UNSYNCABLE_PREF); | |
307 | |
308 prefs->RegisterIntegerPref(prefs::kNtpPromoGroup, | |
309 0, | |
310 PrefService::UNSYNCABLE_PREF); | |
311 prefs->RegisterIntegerPref(prefs::kNtpPromoViews, | |
312 0, | |
313 PrefService::UNSYNCABLE_PREF); | |
314 prefs->RegisterBooleanPref(prefs::kNtpPromoClosed, | |
315 false, | |
316 PrefService::UNSYNCABLE_PREF); | |
317 | |
318 prefs->RegisterBooleanPref(prefs::kNtpPromoGplusRequired, | |
319 false, | |
320 PrefService::UNSYNCABLE_PREF); | |
321 | |
322 // TODO(achuith): Delete this in M22. | |
323 prefs->RegisterIntegerPref(prefs::kNtpPromoBuild, | |
324 0, | |
325 PrefService::UNSYNCABLE_PREF); | |
326 prefs->RegisterIntegerPref(prefs::kNtpPromoPlatform, | |
327 0, | |
328 PrefService::UNSYNCABLE_PREF); | |
329 prefs->ClearPref(prefs::kNtpPromoBuild); | |
330 prefs->ClearPref(prefs::kNtpPromoPlatform); | |
331 } | 368 } |
332 | 369 |
333 void NotificationPromo::WritePrefs() { | 370 void NotificationPromo::WritePrefs() { |
334 prefs_->SetString(prefs::kNtpPromoLine, promo_text_); | 371 DVLOG(1) << "WritePrefs"; |
372 base::DictionaryValue* ntp_promo(new base::DictionaryValue); | |
Robert Sesek
2012/07/23 20:38:50
nit: I think it's more common to write this as an
achuithb
2012/07/23 23:12:02
Done.
| |
373 ntp_promo->SetString(kPrefPromoText, promo_text_); | |
335 #if defined(OS_ANDROID) | 374 #if defined(OS_ANDROID) |
336 prefs_->SetString(prefs::kNtpPromoLineLong, promo_text_long_); | 375 ntp_promo->SetString(kPrefPromoTextLong, promo_text_long_); |
337 prefs_->SetString(prefs::kNtpPromoActionType, promo_action_type_); | 376 ntp_promo->SetString(kPrefPromoActionType, promo_action_type_); |
338 DCHECK(promo_action_args_.get() != NULL); | 377 DCHECK(promo_action_args_.get()); |
339 prefs_->Set(prefs::kNtpPromoActionArgs, *promo_action_args_.get()); | 378 ntp_promo->Set(kPrefPromoActionArgs, promo_action_args_->DeepCopy()); |
340 #endif // defined(OS_ANDROID) | 379 #endif // defined(OS_ANDROID) |
380 ntp_promo->SetDouble(kPrefPromoStart, start_); | |
381 ntp_promo->SetDouble(kPrefPromoEnd, end_); | |
341 | 382 |
342 prefs_->SetDouble(prefs::kNtpPromoStart, start_); | 383 ntp_promo->SetInteger(kPrefPromoNumGroups, num_groups_); |
343 prefs_->SetDouble(prefs::kNtpPromoEnd, end_); | 384 ntp_promo->SetInteger(kPrefPromoSegment, initial_segment_); |
385 ntp_promo->SetInteger(kPrefPromoIncrement, increment_); | |
386 ntp_promo->SetInteger(kPrefPromoIncrementFrequency, time_slice_); | |
387 ntp_promo->SetInteger(kPrefPromoIncrementMax, max_group_); | |
344 | 388 |
345 prefs_->SetInteger(prefs::kNtpPromoNumGroups, num_groups_); | 389 ntp_promo->SetInteger(kPrefPromoMaxViews, max_views_); |
346 prefs_->SetInteger(prefs::kNtpPromoInitialSegment, initial_segment_); | |
347 prefs_->SetInteger(prefs::kNtpPromoIncrement, increment_); | |
348 prefs_->SetInteger(prefs::kNtpPromoGroupTimeSlice, time_slice_); | |
349 prefs_->SetInteger(prefs::kNtpPromoGroupMax, max_group_); | |
350 | 390 |
351 prefs_->SetInteger(prefs::kNtpPromoViewsMax, max_views_); | 391 ntp_promo->SetInteger(kPrefPromoGroup, group_); |
392 ntp_promo->SetInteger(kPrefPromoViews, views_); | |
393 ntp_promo->SetBoolean(kPrefPromoClosed, closed_); | |
352 | 394 |
353 prefs_->SetInteger(prefs::kNtpPromoGroup, group_); | 395 ntp_promo->SetBoolean(kPrefPromoGPlusRequired, gplus_required_); |
354 prefs_->SetInteger(prefs::kNtpPromoViews, views_); | |
355 prefs_->SetBoolean(prefs::kNtpPromoClosed, closed_); | |
356 | 396 |
357 prefs_->SetBoolean(prefs::kNtpPromoGplusRequired, gplus_required_); | 397 base::ListValue* promo_list(new base::ListValue); |
Robert Sesek
2012/07/23 20:38:50
nit: here too
achuithb
2012/07/23 23:12:02
Done.
| |
398 promo_list->Set(0, ntp_promo); // Only support 1 promo for now. | |
399 | |
400 base::DictionaryValue promo_dict; | |
401 promo_dict.Set(promo_type_, promo_list); | |
402 prefs_->Set(kPrefPromoObject, promo_dict); | |
358 } | 403 } |
359 | 404 |
360 void NotificationPromo::InitFromPrefs() { | 405 void NotificationPromo::InitFromPrefs() { |
361 promo_text_ = prefs_->GetString(prefs::kNtpPromoLine); | 406 const base::DictionaryValue* promo_dict = |
407 prefs_->GetDictionary(kPrefPromoObject); | |
408 if (!promo_dict) | |
409 return; | |
410 | |
411 base::ListValue* promo_list(NULL); | |
412 promo_dict->GetList(promo_type_, &promo_list); | |
413 if (!promo_list) | |
414 return; | |
415 | |
416 base::DictionaryValue* ntp_promo(NULL); | |
417 promo_list->GetDictionary(0, &ntp_promo); | |
418 if (!ntp_promo) | |
419 return; | |
420 | |
421 ntp_promo->GetString(kPrefPromoText, &promo_text_); | |
362 #if defined(OS_ANDROID) | 422 #if defined(OS_ANDROID) |
363 promo_text_long_ = prefs_->GetString(prefs::kNtpPromoLineLong); | 423 ntp_promo->GetString(kPrefPromoTextLong, &promo_text_long_); |
364 promo_action_type_ = prefs_->GetString(prefs::kNtpPromoActionType); | 424 ntp_promo->GetString(kPrefPromoActionType, &promo_action_type_); |
365 const base::ListValue* lv = prefs_->GetList(prefs::kNtpPromoActionArgs); | 425 base::ListValue* lv(NULL); |
426 ntp_promo->GetList(kPrefPromoActionArgs, &lv); | |
366 DCHECK(lv != NULL); | 427 DCHECK(lv != NULL); |
367 promo_action_args_.reset(lv->DeepCopy()); | 428 promo_action_args_.reset(lv->DeepCopy()); |
368 #endif // defined(OS_ANDROID) | 429 #endif // defined(OS_ANDROID) |
369 | 430 |
370 start_ = prefs_->GetDouble(prefs::kNtpPromoStart); | 431 ntp_promo->GetDouble(kPrefPromoStart, &start_); |
371 end_ = prefs_->GetDouble(prefs::kNtpPromoEnd); | 432 ntp_promo->GetDouble(kPrefPromoEnd, &end_); |
372 | 433 |
373 num_groups_ = prefs_->GetInteger(prefs::kNtpPromoNumGroups); | 434 ntp_promo->GetInteger(kPrefPromoNumGroups, &num_groups_); |
374 initial_segment_ = prefs_->GetInteger(prefs::kNtpPromoInitialSegment); | 435 ntp_promo->GetInteger(kPrefPromoSegment, &initial_segment_); |
375 increment_ = prefs_->GetInteger(prefs::kNtpPromoIncrement); | 436 ntp_promo->GetInteger(kPrefPromoIncrement, &increment_); |
376 time_slice_ = prefs_->GetInteger(prefs::kNtpPromoGroupTimeSlice); | 437 ntp_promo->GetInteger(kPrefPromoIncrementFrequency, &time_slice_); |
377 max_group_ = prefs_->GetInteger(prefs::kNtpPromoGroupMax); | 438 ntp_promo->GetInteger(kPrefPromoIncrementMax, &max_group_); |
378 | 439 |
379 max_views_ = prefs_->GetInteger(prefs::kNtpPromoViewsMax); | 440 ntp_promo->GetInteger(kPrefPromoMaxViews, &max_views_); |
380 | 441 |
381 group_ = prefs_->GetInteger(prefs::kNtpPromoGroup); | 442 ntp_promo->GetInteger(kPrefPromoGroup, &group_); |
382 views_ = prefs_->GetInteger(prefs::kNtpPromoViews); | 443 ntp_promo->GetInteger(kPrefPromoViews, &views_); |
383 closed_ = prefs_->GetBoolean(prefs::kNtpPromoClosed); | 444 ntp_promo->GetBoolean(kPrefPromoClosed, &closed_); |
384 | 445 |
385 gplus_required_ = prefs_->GetBoolean(prefs::kNtpPromoGplusRequired); | 446 ntp_promo->GetBoolean(kPrefPromoGPlusRequired, &gplus_required_); |
386 } | 447 } |
387 | 448 |
388 bool NotificationPromo::CanShow() const { | 449 bool NotificationPromo::CanShow() const { |
389 return !closed_ && | 450 return !closed_ && |
390 !promo_text_.empty() && | 451 !promo_text_.empty() && |
391 !ExceedsMaxGroup() && | 452 !ExceedsMaxGroup() && |
392 !ExceedsMaxViews() && | 453 !ExceedsMaxViews() && |
393 base::Time::FromDoubleT(StartTimeForGroup()) < base::Time::Now() && | 454 base::Time::FromDoubleT(StartTimeForGroup()) < base::Time::Now() && |
394 base::Time::FromDoubleT(EndTime()) > base::Time::Now() && | 455 base::Time::FromDoubleT(EndTime()) > base::Time::Now() && |
395 IsGPlusRequired(); | 456 IsGPlusRequired(); |
396 } | 457 } |
397 | 458 |
398 void NotificationPromo::HandleClosed() { | 459 void NotificationPromo::HandleClosed() { |
399 content::RecordAction(UserMetricsAction("NTPPromoClosed")); | 460 content::RecordAction(UserMetricsAction("NTPPromoClosed")); |
400 prefs_->SetBoolean(prefs::kNtpPromoClosed, true); | 461 InitFromPrefs(); |
462 if (!closed_) { | |
463 closed_ = true; | |
464 WritePrefs(); | |
465 } | |
401 } | 466 } |
402 | 467 |
403 bool NotificationPromo::HandleViewed() { | 468 bool NotificationPromo::HandleViewed() { |
404 content::RecordAction(UserMetricsAction("NTPPromoShown")); | 469 content::RecordAction(UserMetricsAction("NTPPromoShown")); |
405 if (prefs_->HasPrefPath(prefs::kNtpPromoViewsMax)) | 470 InitFromPrefs(); |
406 max_views_ = prefs_->GetInteger(prefs::kNtpPromoViewsMax); | 471 ++views_; |
407 | 472 WritePrefs(); |
408 if (prefs_->HasPrefPath(prefs::kNtpPromoViews)) | |
409 views_ = prefs_->GetInteger(prefs::kNtpPromoViews); | |
410 | |
411 prefs_->SetInteger(prefs::kNtpPromoViews, ++views_); | |
412 return ExceedsMaxViews(); | 473 return ExceedsMaxViews(); |
413 } | 474 } |
414 | 475 |
415 bool NotificationPromo::ExceedsMaxGroup() const { | 476 bool NotificationPromo::ExceedsMaxGroup() const { |
416 return (max_group_ == 0) ? false : group_ >= max_group_; | 477 return (max_group_ == 0) ? false : group_ >= max_group_; |
417 } | 478 } |
418 | 479 |
419 bool NotificationPromo::ExceedsMaxViews() const { | 480 bool NotificationPromo::ExceedsMaxViews() const { |
420 return (max_views_ == 0) ? false : views_ >= max_views_; | 481 return (max_views_ == 0) ? false : views_ >= max_views_; |
421 } | 482 } |
(...skipping 20 matching lines...) Expand all Loading... | |
442 if (group_ < initial_segment_) | 503 if (group_ < initial_segment_) |
443 return start_; | 504 return start_; |
444 return start_ + | 505 return start_ + |
445 std::ceil(static_cast<float>(group_ - initial_segment_ + 1) / increment_) | 506 std::ceil(static_cast<float>(group_ - initial_segment_ + 1) / increment_) |
446 * time_slice_; | 507 * time_slice_; |
447 } | 508 } |
448 | 509 |
449 double NotificationPromo::EndTime() const { | 510 double NotificationPromo::EndTime() const { |
450 return end_; | 511 return end_; |
451 } | 512 } |
OLD | NEW |