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 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 if (!json.GetList(promo_type_, &promo_list)) | 219 if (!json.GetList(promo_type_, &promo_list)) |
220 return; | 220 return; |
221 #else | 221 #else |
222 if (!json.GetList("mobile_ntp_sync_promo", &promo_list)) { | 222 if (!json.GetList("mobile_ntp_sync_promo", &promo_list)) { |
223 LOG(ERROR) << "Malfromed JSON: not a mobile_ntp_sync_promo"; | 223 LOG(ERROR) << "Malfromed JSON: not a mobile_ntp_sync_promo"; |
224 return; | 224 return; |
225 } | 225 } |
226 #endif // !defined(OS_ANDROID) | 226 #endif // !defined(OS_ANDROID) |
227 | 227 |
228 // No support for multiple promos yet. Only consider the first one. | 228 // No support for multiple promos yet. Only consider the first one. |
229 DictionaryValue* promo = NULL; | 229 const DictionaryValue* promo = NULL; |
230 if (!promo_list->GetDictionary(0, &promo)) | 230 if (!promo_list->GetDictionary(0, &promo)) |
231 return; | 231 return; |
232 | 232 |
233 // Strings. Assume the first one is the promo text. | 233 // Strings. Assume the first one is the promo text. |
234 DictionaryValue* strings = NULL; | 234 const DictionaryValue* strings = NULL; |
235 if (promo->GetDictionary("strings", &strings)) { | 235 if (promo->GetDictionary("strings", &strings)) { |
236 #if !defined(OS_ANDROID) | 236 #if !defined(OS_ANDROID) |
237 DictionaryValue::Iterator iter(*strings); | 237 DictionaryValue::Iterator iter(*strings); |
238 iter.value().GetAsString(&promo_text_); | 238 iter.value().GetAsString(&promo_text_); |
239 DVLOG(1) << "promo_text_=" << promo_text_; | 239 DVLOG(1) << "promo_text_=" << promo_text_; |
240 #endif // defined(OS_ANDROID) | 240 #endif // defined(OS_ANDROID) |
241 } | 241 } |
242 | 242 |
243 // Date. | 243 // Date. |
244 ListValue* date_list = NULL; | 244 const ListValue* date_list = NULL; |
245 if (promo->GetList("date", &date_list)) { | 245 if (promo->GetList("date", &date_list)) { |
246 DictionaryValue* date; | 246 const DictionaryValue* date; |
247 if (date_list->GetDictionary(0, &date)) { | 247 if (date_list->GetDictionary(0, &date)) { |
248 std::string time_str; | 248 std::string time_str; |
249 base::Time time; | 249 base::Time time; |
250 if (date->GetString("start", &time_str) && | 250 if (date->GetString("start", &time_str) && |
251 base::Time::FromString(time_str.c_str(), &time)) { | 251 base::Time::FromString(time_str.c_str(), &time)) { |
252 start_ = time.ToDoubleT(); | 252 start_ = time.ToDoubleT(); |
253 DVLOG(1) << "start str=" << time_str | 253 DVLOG(1) << "start str=" << time_str |
254 << ", start_="<< base::DoubleToString(start_); | 254 << ", start_="<< base::DoubleToString(start_); |
255 } | 255 } |
256 if (date->GetString("end", &time_str) && | 256 if (date->GetString("end", &time_str) && |
257 base::Time::FromString(time_str.c_str(), &time)) { | 257 base::Time::FromString(time_str.c_str(), &time)) { |
258 end_ = time.ToDoubleT(); | 258 end_ = time.ToDoubleT(); |
259 DVLOG(1) << "end str =" << time_str | 259 DVLOG(1) << "end str =" << time_str |
260 << ", end_=" << base::DoubleToString(end_); | 260 << ", end_=" << base::DoubleToString(end_); |
261 } | 261 } |
262 } | 262 } |
263 } | 263 } |
264 | 264 |
265 // Grouping. | 265 // Grouping. |
266 DictionaryValue* grouping = NULL; | 266 const DictionaryValue* grouping = NULL; |
267 if (promo->GetDictionary("grouping", &grouping)) { | 267 if (promo->GetDictionary("grouping", &grouping)) { |
268 grouping->GetInteger("buckets", &num_groups_); | 268 grouping->GetInteger("buckets", &num_groups_); |
269 grouping->GetInteger("segment", &initial_segment_); | 269 grouping->GetInteger("segment", &initial_segment_); |
270 grouping->GetInteger("increment", &increment_); | 270 grouping->GetInteger("increment", &increment_); |
271 grouping->GetInteger("increment_frequency", &time_slice_); | 271 grouping->GetInteger("increment_frequency", &time_slice_); |
272 grouping->GetInteger("increment_max", &max_group_); | 272 grouping->GetInteger("increment_max", &max_group_); |
273 | 273 |
274 DVLOG(1) << "num_groups_ = " << num_groups_ | 274 DVLOG(1) << "num_groups_ = " << num_groups_ |
275 << ", initial_segment_ = " << initial_segment_ | 275 << ", initial_segment_ = " << initial_segment_ |
276 << ", increment_ = " << increment_ | 276 << ", increment_ = " << increment_ |
277 << ", time_slice_ = " << time_slice_ | 277 << ", time_slice_ = " << time_slice_ |
278 << ", max_group_ = " << max_group_; | 278 << ", max_group_ = " << max_group_; |
279 } | 279 } |
280 | 280 |
281 // Payload. | 281 // Payload. |
282 DictionaryValue* payload = NULL; | 282 const DictionaryValue* payload = NULL; |
283 if (promo->GetDictionary("payload", &payload)) { | 283 if (promo->GetDictionary("payload", &payload)) { |
284 payload->GetBoolean("gplus_required", &gplus_required_); | 284 payload->GetBoolean("gplus_required", &gplus_required_); |
285 | 285 |
286 DVLOG(1) << "gplus_required_ = " << gplus_required_; | 286 DVLOG(1) << "gplus_required_ = " << gplus_required_; |
287 } | 287 } |
288 | 288 |
289 promo->GetInteger("max_views", &max_views_); | 289 promo->GetInteger("max_views", &max_views_); |
290 DVLOG(1) << "max_views_ " << max_views_; | 290 DVLOG(1) << "max_views_ " << max_views_; |
291 | 291 |
292 #if defined(OS_ANDROID) | 292 #if defined(OS_ANDROID) |
(...skipping 17 matching lines...) Expand all Loading... |
310 if (!payload->GetString("promo_message_short", &promo_key_short) || | 310 if (!payload->GetString("promo_message_short", &promo_key_short) || |
311 !payload->GetString("promo_message_long", &promo_key_long) || | 311 !payload->GetString("promo_message_long", &promo_key_long) || |
312 !strings->GetString(promo_key_short, &promo_text_) || | 312 !strings->GetString(promo_key_short, &promo_text_) || |
313 !strings->GetString(promo_key_long, &promo_text_long_)) { | 313 !strings->GetString(promo_key_long, &promo_text_long_)) { |
314 LOG(ERROR) << "Malformed JSON: no promo_message_short or _long"; | 314 LOG(ERROR) << "Malformed JSON: no promo_message_short or _long"; |
315 return; | 315 return; |
316 } | 316 } |
317 payload->GetString("promo_action_type", &promo_action_type_); | 317 payload->GetString("promo_action_type", &promo_action_type_); |
318 // We need to be idempotent as the tests call us more than once. | 318 // We need to be idempotent as the tests call us more than once. |
319 promo_action_args_.reset(new base::ListValue); | 319 promo_action_args_.reset(new base::ListValue); |
320 ListValue* args; | 320 const ListValue* args; |
321 if (payload->GetList("promo_action_args", &args)) { | 321 if (payload->GetList("promo_action_args", &args)) { |
322 // JSON format for args: "promo_action_args" : [ "<arg1>", "<arg2>"... ] | 322 // JSON format for args: "promo_action_args" : [ "<arg1>", "<arg2>"... ] |
323 // Every value comes from "strings" dictionary, either directly or not. | 323 // Every value comes from "strings" dictionary, either directly or not. |
324 // Every arg is either directly a key into "strings" dictionary, | 324 // Every arg is either directly a key into "strings" dictionary, |
325 // or a key into "payload" dictionary with the value that is a key into | 325 // or a key into "payload" dictionary with the value that is a key into |
326 // "strings" dictionary. | 326 // "strings" dictionary. |
327 for (std::size_t i = 0; i < args->GetSize(); ++i) { | 327 for (std::size_t i = 0; i < args->GetSize(); ++i) { |
328 std::string name, key, value; | 328 std::string name, key, value; |
329 if (!args->GetString(i, &name) || | 329 if (!args->GetString(i, &name) || |
330 !(strings->GetString(name, &value) || | 330 !(strings->GetString(name, &value) || |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 const base::DictionaryValue* promo_dict = | 408 const base::DictionaryValue* promo_dict = |
409 prefs_->GetDictionary(kPrefPromoObject); | 409 prefs_->GetDictionary(kPrefPromoObject); |
410 if (!promo_dict) | 410 if (!promo_dict) |
411 return; | 411 return; |
412 | 412 |
413 const base::ListValue* promo_list(NULL); | 413 const base::ListValue* promo_list(NULL); |
414 promo_dict->GetList(promo_type_, &promo_list); | 414 promo_dict->GetList(promo_type_, &promo_list); |
415 if (!promo_list) | 415 if (!promo_list) |
416 return; | 416 return; |
417 | 417 |
418 base::DictionaryValue* ntp_promo(NULL); | 418 const base::DictionaryValue* ntp_promo(NULL); |
419 promo_list->GetDictionary(0, &ntp_promo); | 419 promo_list->GetDictionary(0, &ntp_promo); |
420 if (!ntp_promo) | 420 if (!ntp_promo) |
421 return; | 421 return; |
422 | 422 |
423 ntp_promo->GetString(kPrefPromoText, &promo_text_); | 423 ntp_promo->GetString(kPrefPromoText, &promo_text_); |
424 #if defined(OS_ANDROID) | 424 #if defined(OS_ANDROID) |
425 ntp_promo->GetString(kPrefPromoTextLong, &promo_text_long_); | 425 ntp_promo->GetString(kPrefPromoTextLong, &promo_text_long_); |
426 ntp_promo->GetString(kPrefPromoActionType, &promo_action_type_); | 426 ntp_promo->GetString(kPrefPromoActionType, &promo_action_type_); |
427 base::ListValue* lv(NULL); | 427 const base::ListValue* lv(NULL); |
428 ntp_promo->GetList(kPrefPromoActionArgs, &lv); | 428 ntp_promo->GetList(kPrefPromoActionArgs, &lv); |
429 DCHECK(lv != NULL); | 429 DCHECK(lv != NULL); |
430 promo_action_args_.reset(lv->DeepCopy()); | 430 promo_action_args_.reset(lv->DeepCopy()); |
431 #endif // defined(OS_ANDROID) | 431 #endif // defined(OS_ANDROID) |
432 | 432 |
433 ntp_promo->GetDouble(kPrefPromoStart, &start_); | 433 ntp_promo->GetDouble(kPrefPromoStart, &start_); |
434 ntp_promo->GetDouble(kPrefPromoEnd, &end_); | 434 ntp_promo->GetDouble(kPrefPromoEnd, &end_); |
435 | 435 |
436 ntp_promo->GetInteger(kPrefPromoNumGroups, &num_groups_); | 436 ntp_promo->GetInteger(kPrefPromoNumGroups, &num_groups_); |
437 ntp_promo->GetInteger(kPrefPromoSegment, &initial_segment_); | 437 ntp_promo->GetInteger(kPrefPromoSegment, &initial_segment_); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 if (group_ < initial_segment_) | 505 if (group_ < initial_segment_) |
506 return start_; | 506 return start_; |
507 return start_ + | 507 return start_ + |
508 std::ceil(static_cast<float>(group_ - initial_segment_ + 1) / increment_) | 508 std::ceil(static_cast<float>(group_ - initial_segment_ + 1) / increment_) |
509 * time_slice_; | 509 * time_slice_; |
510 } | 510 } |
511 | 511 |
512 double NotificationPromo::EndTime() const { | 512 double NotificationPromo::EndTime() const { |
513 return end_; | 513 return end_; |
514 } | 514 } |
OLD | NEW |