Chromium Code Reviews| Index: ios/chrome/browser/notification_promo.cc |
| diff --git a/ios/chrome/browser/notification_promo.cc b/ios/chrome/browser/notification_promo.cc |
| index 11119b2332611769418c8002d528b06b9ee67c5b..80c697687fc65e3413fae52c142da74cf147c190 100644 |
| --- a/ios/chrome/browser/notification_promo.cc |
| +++ b/ios/chrome/browser/notification_promo.cc |
| @@ -25,6 +25,7 @@ const char kNTPPromoFinchExperiment[] = "IOSNTPPromotion"; |
| const char kPrefPromoObject[] = "ios.ntppromo"; |
| // Keys in the kPrefPromoObject dictionary; used only here. |
| +const char kPrefPromoID[] = "id"; |
| const char kPrefPromoFirstViewTime[] = "first_view_time"; |
| const char kPrefPromoViews[] = "views"; |
| const char kPrefPromoClosed[] = "closed"; |
| @@ -135,14 +136,21 @@ void NotificationPromo::MigrateUserPrefs(PrefService* user_prefs) { |
| } |
| void NotificationPromo::WritePrefs() { |
| + this->WritePrefs(promo_id_, first_view_time_, views_, closed_); |
| +} |
| + |
| +void NotificationPromo::WritePrefs(int promo_id, |
| + double first_view_time, |
| + int views, |
| + bool closed) { |
| base::DictionaryValue* ntp_promo = new base::DictionaryValue; |
| - ntp_promo->SetDouble(kPrefPromoFirstViewTime, first_view_time_); |
| - ntp_promo->SetInteger(kPrefPromoViews, views_); |
| - ntp_promo->SetBoolean(kPrefPromoClosed, closed_); |
| + ntp_promo->SetDouble(kPrefPromoFirstViewTime, first_view_time); |
| + ntp_promo->SetInteger(kPrefPromoViews, views); |
| + ntp_promo->SetBoolean(kPrefPromoClosed, closed); |
| base::DictionaryValue promo_dict; |
| promo_dict.MergeDictionary(local_state_->GetDictionary(kPrefPromoObject)); |
| - promo_dict.Set(base::IntToString(promo_id_), ntp_promo); |
| + promo_dict.Set(base::IntToString(promo_id), ntp_promo); |
| local_state_->Set(kPrefPromoObject, promo_dict); |
| DVLOG(1) << "WritePrefs " << promo_dict; |
| } |
| @@ -150,6 +158,10 @@ void NotificationPromo::WritePrefs() { |
| void NotificationPromo::InitFromPrefs(PromoType promo_type) { |
| promo_type_ = promo_type; |
| + // Check if data is stored in the old prefs structure, and migrate it before |
| + // reading from prefs. |
| + this->MigrateOldPrefs(); |
|
rohitrao (ping after 24h)
2016/06/27 17:04:19
Why do you need "this->" ?
gchatz
2016/06/27 23:12:22
Done.
|
| + |
| // If |promo_id_| is not set, do nothing. |
| if (promo_id_ == -1) |
| return; |
| @@ -169,6 +181,48 @@ void NotificationPromo::InitFromPrefs(PromoType promo_type) { |
| ntp_promo->GetBoolean(kPrefPromoClosed, &closed_); |
| } |
| +void NotificationPromo::MigrateOldPrefs() { |
| + const base::DictionaryValue* promo_dict = |
| + local_state_->GetDictionary(kPrefPromoObject); |
| + if (!promo_dict) |
| + return; |
| + |
| + const base::ListValue* promo_list = NULL; |
| + promo_dict->GetList("mobile_ntp_whats_new_promo", &promo_list); |
| + if (!promo_list) |
| + return; |
| + |
| + const base::DictionaryValue* ntp_promo = NULL; |
| + promo_list->GetDictionary(0, &ntp_promo); |
| + if (!ntp_promo) { |
| + // If the list is saved but there is no promo dictionary, clear prefs to |
| + // delete the empty list. |
| + NotificationPromo::MigrateUserPrefs(local_state_); |
| + return; |
| + } |
| + |
| + int promo_id = -1; |
| + ntp_promo->GetInteger(kPrefPromoID, &promo_id); |
| + if (promo_id == -1) { |
| + // If there is no promo id saved in prefs, then data is corrupt and the |
| + // prefs can be discarded. |
| + NotificationPromo::MigrateUserPrefs(local_state_); |
| + return; |
| + } |
| + |
| + double first_view_time = 0; |
| + ntp_promo->GetDouble(kPrefPromoFirstViewTime, &first_view_time); |
| + int views = 0; |
| + ntp_promo->GetInteger(kPrefPromoViews, &views); |
| + bool closed = false; |
| + ntp_promo->GetBoolean(kPrefPromoClosed, &closed); |
| + |
| + // Clear prefs to discard the old structure before saving the data in the new |
| + // structure. |
| + NotificationPromo::MigrateUserPrefs(local_state_); |
| + this->WritePrefs(promo_id, first_view_time, views, closed); |
| +} |
| + |
| bool NotificationPromo::CanShow() const { |
| return !closed_ && !promo_text_.empty() && !ExceedsMaxViews() && |
| !ExceedsMaxSeconds() && |