| 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 // TODO(achuith): remove this in m23. |
| 109 void ClearDeprecatedPrefs(PrefService* prefs) { |
| 110 prefs->RegisterStringPref(prefs::kNtpPromoLine, |
| 111 std::string(), |
| 112 PrefService::UNSYNCABLE_PREF); |
| 113 prefs->ClearPref(prefs::kNtpPromoLine); |
| 114 #if defined(OS_ANDROID) |
| 115 prefs->RegisterStringPref(prefs::kNtpPromoLineLong, |
| 116 std::string(), |
| 117 PrefService::UNSYNCABLE_PREF); |
| 118 prefs->RegisterStringPref(prefs::kNtpPromoActionType, |
| 119 std::string(), |
| 120 PrefService::UNSYNCABLE_PREF); |
| 121 prefs->RegisterListPref(prefs::kNtpPromoActionArgs, |
| 122 new base::ListValue, |
| 123 PrefService::UNSYNCABLE_PREF); |
| 124 prefs->ClearPref(prefs::kNtpPromoLineLong); |
| 125 prefs->ClearPref(prefs::kNtpPromoActionType); |
| 126 prefs->ClearPref(prefs::kNtpPromoActionArgs); |
| 127 #endif // defined(OS_ANDROID) |
| 128 |
| 129 prefs->RegisterDoublePref(prefs::kNtpPromoStart, |
| 130 0, |
| 131 PrefService::UNSYNCABLE_PREF); |
| 132 prefs->RegisterDoublePref(prefs::kNtpPromoEnd, |
| 133 0, |
| 134 PrefService::UNSYNCABLE_PREF); |
| 135 |
| 136 prefs->RegisterIntegerPref(prefs::kNtpPromoNumGroups, |
| 137 0, |
| 138 PrefService::UNSYNCABLE_PREF); |
| 139 prefs->RegisterIntegerPref(prefs::kNtpPromoInitialSegment, |
| 140 0, |
| 141 PrefService::UNSYNCABLE_PREF); |
| 142 prefs->RegisterIntegerPref(prefs::kNtpPromoIncrement, |
| 143 1, |
| 144 PrefService::UNSYNCABLE_PREF); |
| 145 prefs->RegisterIntegerPref(prefs::kNtpPromoGroupTimeSlice, |
| 146 0, |
| 147 PrefService::UNSYNCABLE_PREF); |
| 148 prefs->RegisterIntegerPref(prefs::kNtpPromoGroupMax, |
| 149 0, |
| 150 PrefService::UNSYNCABLE_PREF); |
| 151 |
| 152 prefs->RegisterIntegerPref(prefs::kNtpPromoViewsMax, |
| 153 0, |
| 154 PrefService::UNSYNCABLE_PREF); |
| 155 |
| 156 prefs->RegisterIntegerPref(prefs::kNtpPromoGroup, |
| 157 0, |
| 158 PrefService::UNSYNCABLE_PREF); |
| 159 prefs->RegisterIntegerPref(prefs::kNtpPromoViews, |
| 160 0, |
| 161 PrefService::UNSYNCABLE_PREF); |
| 162 prefs->RegisterBooleanPref(prefs::kNtpPromoClosed, |
| 163 false, |
| 164 PrefService::UNSYNCABLE_PREF); |
| 165 |
| 166 prefs->RegisterBooleanPref(prefs::kNtpPromoGplusRequired, |
| 167 false, |
| 168 PrefService::UNSYNCABLE_PREF); |
| 169 |
| 170 prefs->ClearPref(prefs::kNtpPromoStart); |
| 171 prefs->ClearPref(prefs::kNtpPromoEnd); |
| 172 prefs->ClearPref(prefs::kNtpPromoNumGroups); |
| 173 prefs->ClearPref(prefs::kNtpPromoInitialSegment); |
| 174 prefs->ClearPref(prefs::kNtpPromoIncrement); |
| 175 prefs->ClearPref(prefs::kNtpPromoGroupTimeSlice); |
| 176 prefs->ClearPref(prefs::kNtpPromoGroupMax); |
| 177 prefs->ClearPref(prefs::kNtpPromoViewsMax); |
| 178 prefs->ClearPref(prefs::kNtpPromoGroup); |
| 179 prefs->ClearPref(prefs::kNtpPromoViews); |
| 180 prefs->ClearPref(prefs::kNtpPromoClosed); |
| 181 prefs->ClearPref(prefs::kNtpPromoGplusRequired); |
| 182 } |
| 183 |
| 92 } // namespace | 184 } // namespace |
| 93 | 185 |
| 186 const char NotificationPromo::kNtpNotificationPromoType[] = |
| 187 "ntp_notification_promo"; |
| 188 const char NotificationPromo::kBubblePromoType[] = "bubble_promo"; |
| 189 |
| 94 NotificationPromo::NotificationPromo(Profile* profile) | 190 NotificationPromo::NotificationPromo(Profile* profile) |
| 95 : profile_(profile), | 191 : profile_(profile), |
| 96 prefs_(profile_->GetPrefs()), | 192 prefs_(profile_->GetPrefs()), |
| 193 promo_type_(kNtpNotificationPromoType), |
| 97 #if defined(OS_ANDROID) | 194 #if defined(OS_ANDROID) |
| 98 promo_action_args_(new base::ListValue), | 195 promo_action_args_(new base::ListValue), |
| 99 #endif // defined(OS_ANDROID) | 196 #endif // defined(OS_ANDROID) |
| 100 start_(0.0), | 197 start_(0.0), |
| 101 end_(0.0), | 198 end_(0.0), |
| 102 num_groups_(kDefaultGroupSize), | 199 num_groups_(kDefaultGroupSize), |
| 103 initial_segment_(0), | 200 initial_segment_(0), |
| 104 increment_(1), | 201 increment_(1), |
| 105 time_slice_(0), | 202 time_slice_(0), |
| 106 max_group_(0), | 203 max_group_(0), |
| 107 max_views_(0), | 204 max_views_(0), |
| 108 group_(0), | 205 group_(0), |
| 109 views_(0), | 206 views_(0), |
| 110 closed_(false), | 207 closed_(false), |
| 111 gplus_required_(false), | 208 gplus_required_(false), |
| 112 new_notification_(false) { | 209 new_notification_(false) { |
| 113 DCHECK(profile); | 210 DCHECK(profile); |
| 114 DCHECK(prefs_); | 211 DCHECK(prefs_); |
| 115 } | 212 } |
| 116 | 213 |
| 117 NotificationPromo::~NotificationPromo() {} | 214 NotificationPromo::~NotificationPromo() {} |
| 118 | 215 |
| 119 void NotificationPromo::InitFromJson(const DictionaryValue& json) { | 216 void NotificationPromo::InitFromJson(const DictionaryValue& json) { |
| 120 ListValue* promo_list = NULL; | 217 ListValue* promo_list = NULL; |
| 121 #if !defined(OS_ANDROID) | 218 #if !defined(OS_ANDROID) |
| 122 if (!json.GetList("ntp_notification_promo", &promo_list)) | 219 if (!json.GetList(promo_type_, &promo_list)) |
| 123 return; | 220 return; |
| 124 #else | 221 #else |
| 125 if (!json.GetList("mobile_ntp_sync_promo", &promo_list)) { | 222 if (!json.GetList("mobile_ntp_sync_promo", &promo_list)) { |
| 126 LOG(ERROR) << "Malfromed JSON: not a mobile_ntp_sync_promo"; | 223 LOG(ERROR) << "Malfromed JSON: not a mobile_ntp_sync_promo"; |
| 127 return; | 224 return; |
| 128 } | 225 } |
| 129 #endif // !defined(OS_ANDROID) | 226 #endif // !defined(OS_ANDROID) |
| 130 | 227 |
| 131 // No support for multiple promos yet. Only consider the first one. | 228 // No support for multiple promos yet. Only consider the first one. |
| 132 DictionaryValue* promo = NULL; | 229 DictionaryValue* promo = NULL; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 147 ListValue* date_list = NULL; | 244 ListValue* date_list = NULL; |
| 148 if (promo->GetList("date", &date_list)) { | 245 if (promo->GetList("date", &date_list)) { |
| 149 DictionaryValue* date; | 246 DictionaryValue* date; |
| 150 if (date_list->GetDictionary(0, &date)) { | 247 if (date_list->GetDictionary(0, &date)) { |
| 151 std::string time_str; | 248 std::string time_str; |
| 152 base::Time time; | 249 base::Time time; |
| 153 if (date->GetString("start", &time_str) && | 250 if (date->GetString("start", &time_str) && |
| 154 base::Time::FromString(time_str.c_str(), &time)) { | 251 base::Time::FromString(time_str.c_str(), &time)) { |
| 155 start_ = time.ToDoubleT(); | 252 start_ = time.ToDoubleT(); |
| 156 DVLOG(1) << "start str=" << time_str | 253 DVLOG(1) << "start str=" << time_str |
| 157 << ", start_="<< base::DoubleToString(start_); | 254 << ", start_="<< base::DoubleToString(start_); |
| 158 } | 255 } |
| 159 if (date->GetString("end", &time_str) && | 256 if (date->GetString("end", &time_str) && |
| 160 base::Time::FromString(time_str.c_str(), &time)) { | 257 base::Time::FromString(time_str.c_str(), &time)) { |
| 161 end_ = time.ToDoubleT(); | 258 end_ = time.ToDoubleT(); |
| 162 DVLOG(1) << "end str =" << time_str | 259 DVLOG(1) << "end str =" << time_str |
| 163 << ", end_=" << base::DoubleToString(end_); | 260 << ", end_=" << base::DoubleToString(end_); |
| 164 } | 261 } |
| 165 } | 262 } |
| 166 } | 263 } |
| 167 | 264 |
| 168 // Grouping. | 265 // Grouping. |
| 169 DictionaryValue* grouping = NULL; | 266 DictionaryValue* grouping = NULL; |
| 170 if (promo->GetDictionary("grouping", &grouping)) { | 267 if (promo->GetDictionary("grouping", &grouping)) { |
| 171 grouping->GetInteger("buckets", &num_groups_); | 268 grouping->GetInteger("buckets", &num_groups_); |
| 172 grouping->GetInteger("segment", &initial_segment_); | 269 grouping->GetInteger("segment", &initial_segment_); |
| 173 grouping->GetInteger("increment", &increment_); | 270 grouping->GetInteger("increment", &increment_); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 } | 335 } |
| 239 promo_action_args_->Append(base::Value::CreateStringValue(value)); | 336 promo_action_args_->Append(base::Value::CreateStringValue(value)); |
| 240 } | 337 } |
| 241 } | 338 } |
| 242 #endif // defined(OS_ANDROID) | 339 #endif // defined(OS_ANDROID) |
| 243 | 340 |
| 244 CheckForNewNotification(); | 341 CheckForNewNotification(); |
| 245 } | 342 } |
| 246 | 343 |
| 247 void NotificationPromo::CheckForNewNotification() { | 344 void NotificationPromo::CheckForNewNotification() { |
| 248 const double old_start = GetTimeFromPrefs(prefs_, prefs::kNtpPromoStart); | 345 NotificationPromo old_promo(profile_); |
| 249 const double old_end = GetTimeFromPrefs(prefs_, prefs::kNtpPromoEnd); | 346 old_promo.InitFromPrefs(); |
| 250 const std::string old_promo_text = prefs_->GetString(prefs::kNtpPromoLine); | 347 const double old_start = old_promo.start_; |
| 348 const double old_end = old_promo.end_; |
| 349 const std::string old_promo_text = old_promo.promo_text_; |
| 251 | 350 |
| 252 new_notification_ = | 351 new_notification_ = |
| 253 old_start != start_ || old_end != end_ || old_promo_text != promo_text_; | 352 old_start != start_ || old_end != end_ || old_promo_text != promo_text_; |
| 254 if (new_notification_) | 353 if (new_notification_) |
| 255 OnNewNotification(); | 354 OnNewNotification(); |
| 256 } | 355 } |
| 257 | 356 |
| 258 void NotificationPromo::OnNewNotification() { | 357 void NotificationPromo::OnNewNotification() { |
| 259 // Create a new promo group. | 358 // Create a new promo group. |
| 260 group_ = base::RandInt(0, num_groups_ - 1); | 359 group_ = base::RandInt(0, num_groups_ - 1); |
| 261 WritePrefs(); | 360 WritePrefs(); |
| 262 } | 361 } |
| 263 | 362 |
| 264 // static | 363 // static |
| 265 void NotificationPromo::RegisterUserPrefs(PrefService* prefs) { | 364 void NotificationPromo::RegisterUserPrefs(PrefService* prefs) { |
| 266 prefs->RegisterStringPref(prefs::kNtpPromoLine, | 365 ClearDeprecatedPrefs(prefs); |
| 267 std::string(), | 366 prefs->RegisterDictionaryPref("promo", |
| 268 PrefService::UNSYNCABLE_PREF); | 367 new base::DictionaryValue, |
| 269 #if defined(OS_ANDROID) | 368 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 } | 369 } |
| 332 | 370 |
| 333 void NotificationPromo::WritePrefs() { | 371 void NotificationPromo::WritePrefs() { |
| 334 prefs_->SetString(prefs::kNtpPromoLine, promo_text_); | 372 DVLOG(1) << "WritePrefs"; |
| 373 base::DictionaryValue* ntp_promo = new base::DictionaryValue; |
| 374 ntp_promo->SetString(kPrefPromoText, promo_text_); |
| 335 #if defined(OS_ANDROID) | 375 #if defined(OS_ANDROID) |
| 336 prefs_->SetString(prefs::kNtpPromoLineLong, promo_text_long_); | 376 ntp_promo->SetString(kPrefPromoTextLong, promo_text_long_); |
| 337 prefs_->SetString(prefs::kNtpPromoActionType, promo_action_type_); | 377 ntp_promo->SetString(kPrefPromoActionType, promo_action_type_); |
| 338 DCHECK(promo_action_args_.get() != NULL); | 378 DCHECK(promo_action_args_.get()); |
| 339 prefs_->Set(prefs::kNtpPromoActionArgs, *promo_action_args_.get()); | 379 ntp_promo->Set(kPrefPromoActionArgs, promo_action_args_->DeepCopy()); |
| 340 #endif // defined(OS_ANDROID) | 380 #endif // defined(OS_ANDROID) |
| 381 ntp_promo->SetDouble(kPrefPromoStart, start_); |
| 382 ntp_promo->SetDouble(kPrefPromoEnd, end_); |
| 341 | 383 |
| 342 prefs_->SetDouble(prefs::kNtpPromoStart, start_); | 384 ntp_promo->SetInteger(kPrefPromoNumGroups, num_groups_); |
| 343 prefs_->SetDouble(prefs::kNtpPromoEnd, end_); | 385 ntp_promo->SetInteger(kPrefPromoSegment, initial_segment_); |
| 386 ntp_promo->SetInteger(kPrefPromoIncrement, increment_); |
| 387 ntp_promo->SetInteger(kPrefPromoIncrementFrequency, time_slice_); |
| 388 ntp_promo->SetInteger(kPrefPromoIncrementMax, max_group_); |
| 344 | 389 |
| 345 prefs_->SetInteger(prefs::kNtpPromoNumGroups, num_groups_); | 390 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 | 391 |
| 351 prefs_->SetInteger(prefs::kNtpPromoViewsMax, max_views_); | 392 ntp_promo->SetInteger(kPrefPromoGroup, group_); |
| 393 ntp_promo->SetInteger(kPrefPromoViews, views_); |
| 394 ntp_promo->SetBoolean(kPrefPromoClosed, closed_); |
| 352 | 395 |
| 353 prefs_->SetInteger(prefs::kNtpPromoGroup, group_); | 396 ntp_promo->SetBoolean(kPrefPromoGPlusRequired, gplus_required_); |
| 354 prefs_->SetInteger(prefs::kNtpPromoViews, views_); | |
| 355 prefs_->SetBoolean(prefs::kNtpPromoClosed, closed_); | |
| 356 | 397 |
| 357 prefs_->SetBoolean(prefs::kNtpPromoGplusRequired, gplus_required_); | 398 base::ListValue* promo_list = new base::ListValue; |
| 399 promo_list->Set(0, ntp_promo); // Only support 1 promo for now. |
| 400 |
| 401 base::DictionaryValue promo_dict; |
| 402 promo_dict.Set(promo_type_, promo_list); |
| 403 prefs_->Set(kPrefPromoObject, promo_dict); |
| 358 } | 404 } |
| 359 | 405 |
| 360 void NotificationPromo::InitFromPrefs() { | 406 void NotificationPromo::InitFromPrefs() { |
| 361 promo_text_ = prefs_->GetString(prefs::kNtpPromoLine); | 407 const base::DictionaryValue* promo_dict = |
| 408 prefs_->GetDictionary(kPrefPromoObject); |
| 409 if (!promo_dict) |
| 410 return; |
| 411 |
| 412 base::ListValue* promo_list(NULL); |
| 413 promo_dict->GetList(promo_type_, &promo_list); |
| 414 if (!promo_list) |
| 415 return; |
| 416 |
| 417 base::DictionaryValue* ntp_promo(NULL); |
| 418 promo_list->GetDictionary(0, &ntp_promo); |
| 419 if (!ntp_promo) |
| 420 return; |
| 421 |
| 422 ntp_promo->GetString(kPrefPromoText, &promo_text_); |
| 362 #if defined(OS_ANDROID) | 423 #if defined(OS_ANDROID) |
| 363 promo_text_long_ = prefs_->GetString(prefs::kNtpPromoLineLong); | 424 ntp_promo->GetString(kPrefPromoTextLong, &promo_text_long_); |
| 364 promo_action_type_ = prefs_->GetString(prefs::kNtpPromoActionType); | 425 ntp_promo->GetString(kPrefPromoActionType, &promo_action_type_); |
| 365 const base::ListValue* lv = prefs_->GetList(prefs::kNtpPromoActionArgs); | 426 base::ListValue* lv(NULL); |
| 427 ntp_promo->GetList(kPrefPromoActionArgs, &lv); |
| 366 DCHECK(lv != NULL); | 428 DCHECK(lv != NULL); |
| 367 promo_action_args_.reset(lv->DeepCopy()); | 429 promo_action_args_.reset(lv->DeepCopy()); |
| 368 #endif // defined(OS_ANDROID) | 430 #endif // defined(OS_ANDROID) |
| 369 | 431 |
| 370 start_ = prefs_->GetDouble(prefs::kNtpPromoStart); | 432 ntp_promo->GetDouble(kPrefPromoStart, &start_); |
| 371 end_ = prefs_->GetDouble(prefs::kNtpPromoEnd); | 433 ntp_promo->GetDouble(kPrefPromoEnd, &end_); |
| 372 | 434 |
| 373 num_groups_ = prefs_->GetInteger(prefs::kNtpPromoNumGroups); | 435 ntp_promo->GetInteger(kPrefPromoNumGroups, &num_groups_); |
| 374 initial_segment_ = prefs_->GetInteger(prefs::kNtpPromoInitialSegment); | 436 ntp_promo->GetInteger(kPrefPromoSegment, &initial_segment_); |
| 375 increment_ = prefs_->GetInteger(prefs::kNtpPromoIncrement); | 437 ntp_promo->GetInteger(kPrefPromoIncrement, &increment_); |
| 376 time_slice_ = prefs_->GetInteger(prefs::kNtpPromoGroupTimeSlice); | 438 ntp_promo->GetInteger(kPrefPromoIncrementFrequency, &time_slice_); |
| 377 max_group_ = prefs_->GetInteger(prefs::kNtpPromoGroupMax); | 439 ntp_promo->GetInteger(kPrefPromoIncrementMax, &max_group_); |
| 378 | 440 |
| 379 max_views_ = prefs_->GetInteger(prefs::kNtpPromoViewsMax); | 441 ntp_promo->GetInteger(kPrefPromoMaxViews, &max_views_); |
| 380 | 442 |
| 381 group_ = prefs_->GetInteger(prefs::kNtpPromoGroup); | 443 ntp_promo->GetInteger(kPrefPromoGroup, &group_); |
| 382 views_ = prefs_->GetInteger(prefs::kNtpPromoViews); | 444 ntp_promo->GetInteger(kPrefPromoViews, &views_); |
| 383 closed_ = prefs_->GetBoolean(prefs::kNtpPromoClosed); | 445 ntp_promo->GetBoolean(kPrefPromoClosed, &closed_); |
| 384 | 446 |
| 385 gplus_required_ = prefs_->GetBoolean(prefs::kNtpPromoGplusRequired); | 447 ntp_promo->GetBoolean(kPrefPromoGPlusRequired, &gplus_required_); |
| 386 } | 448 } |
| 387 | 449 |
| 388 bool NotificationPromo::CanShow() const { | 450 bool NotificationPromo::CanShow() const { |
| 389 return !closed_ && | 451 return !closed_ && |
| 390 !promo_text_.empty() && | 452 !promo_text_.empty() && |
| 391 !ExceedsMaxGroup() && | 453 !ExceedsMaxGroup() && |
| 392 !ExceedsMaxViews() && | 454 !ExceedsMaxViews() && |
| 393 base::Time::FromDoubleT(StartTimeForGroup()) < base::Time::Now() && | 455 base::Time::FromDoubleT(StartTimeForGroup()) < base::Time::Now() && |
| 394 base::Time::FromDoubleT(EndTime()) > base::Time::Now() && | 456 base::Time::FromDoubleT(EndTime()) > base::Time::Now() && |
| 395 IsGPlusRequired(); | 457 IsGPlusRequired(); |
| 396 } | 458 } |
| 397 | 459 |
| 398 void NotificationPromo::HandleClosed() { | 460 void NotificationPromo::HandleClosed() { |
| 399 content::RecordAction(UserMetricsAction("NTPPromoClosed")); | 461 content::RecordAction(UserMetricsAction("NTPPromoClosed")); |
| 400 prefs_->SetBoolean(prefs::kNtpPromoClosed, true); | 462 InitFromPrefs(); |
| 463 if (!closed_) { |
| 464 closed_ = true; |
| 465 WritePrefs(); |
| 466 } |
| 401 } | 467 } |
| 402 | 468 |
| 403 bool NotificationPromo::HandleViewed() { | 469 bool NotificationPromo::HandleViewed() { |
| 404 content::RecordAction(UserMetricsAction("NTPPromoShown")); | 470 content::RecordAction(UserMetricsAction("NTPPromoShown")); |
| 405 if (prefs_->HasPrefPath(prefs::kNtpPromoViewsMax)) | 471 InitFromPrefs(); |
| 406 max_views_ = prefs_->GetInteger(prefs::kNtpPromoViewsMax); | 472 ++views_; |
| 407 | 473 WritePrefs(); |
| 408 if (prefs_->HasPrefPath(prefs::kNtpPromoViews)) | |
| 409 views_ = prefs_->GetInteger(prefs::kNtpPromoViews); | |
| 410 | |
| 411 prefs_->SetInteger(prefs::kNtpPromoViews, ++views_); | |
| 412 return ExceedsMaxViews(); | 474 return ExceedsMaxViews(); |
| 413 } | 475 } |
| 414 | 476 |
| 415 bool NotificationPromo::ExceedsMaxGroup() const { | 477 bool NotificationPromo::ExceedsMaxGroup() const { |
| 416 return (max_group_ == 0) ? false : group_ >= max_group_; | 478 return (max_group_ == 0) ? false : group_ >= max_group_; |
| 417 } | 479 } |
| 418 | 480 |
| 419 bool NotificationPromo::ExceedsMaxViews() const { | 481 bool NotificationPromo::ExceedsMaxViews() const { |
| 420 return (max_views_ == 0) ? false : views_ >= max_views_; | 482 return (max_views_ == 0) ? false : views_ >= max_views_; |
| 421 } | 483 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 442 if (group_ < initial_segment_) | 504 if (group_ < initial_segment_) |
| 443 return start_; | 505 return start_; |
| 444 return start_ + | 506 return start_ + |
| 445 std::ceil(static_cast<float>(group_ - initial_segment_ + 1) / increment_) | 507 std::ceil(static_cast<float>(group_ - initial_segment_ + 1) / increment_) |
| 446 * time_slice_; | 508 * time_slice_; |
| 447 } | 509 } |
| 448 | 510 |
| 449 double NotificationPromo::EndTime() const { | 511 double NotificationPromo::EndTime() const { |
| 450 return end_; | 512 return end_; |
| 451 } | 513 } |
| OLD | NEW |