| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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 #import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h" | 5 #import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 // A mapping from a string to a l10n message id. | 36 // A mapping from a string to a l10n message id. |
| 37 const PromoStringToIdsMapEntry kPromoStringToIdsMap[] = { | 37 const PromoStringToIdsMapEntry kPromoStringToIdsMap[] = { |
| 38 {"IDS_IOS_APP_RATING_PROMO_STRING", IDS_IOS_APP_RATING_PROMO_STRING}, | 38 {"IDS_IOS_APP_RATING_PROMO_STRING", IDS_IOS_APP_RATING_PROMO_STRING}, |
| 39 {"IDS_IOS_MOVE_TO_DOCK_FASTER_ACCESS", IDS_IOS_MOVE_TO_DOCK_FASTER_ACCESS}, | 39 {"IDS_IOS_MOVE_TO_DOCK_FASTER_ACCESS", IDS_IOS_MOVE_TO_DOCK_FASTER_ACCESS}, |
| 40 {"IDS_IOS_MOVE_TO_DOCK_LOVE_CHROME", IDS_IOS_MOVE_TO_DOCK_LOVE_CHROME}, | 40 {"IDS_IOS_MOVE_TO_DOCK_LOVE_CHROME", IDS_IOS_MOVE_TO_DOCK_LOVE_CHROME}, |
| 41 {"IDS_IOS_MOVE_TO_DOCK_TIP", IDS_IOS_MOVE_TO_DOCK_TIP}, | 41 {"IDS_IOS_MOVE_TO_DOCK_TIP", IDS_IOS_MOVE_TO_DOCK_TIP}, |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 // Returns a localized version of |promo_text| if it has an entry in the | 44 // Returns a localized version of |promo_text| if it has an entry in the |
| 45 // |kPromoStringToIdsMap|. If there is no entry, |promo_text| is returned. | 45 // |kPromoStringToIdsMap|. If there is no entry, an empty string is returned. |
| 46 std::string GetLocalizedPromoText(const std::string& promo_text) { | 46 std::string GetLocalizedPromoText(const std::string& promo_text) { |
| 47 for (size_t i = 0; i < arraysize(kPromoStringToIdsMap); ++i) { | 47 for (size_t i = 0; i < arraysize(kPromoStringToIdsMap); ++i) { |
| 48 if (kPromoStringToIdsMap[i].promo_text_str == promo_text) | 48 if (kPromoStringToIdsMap[i].promo_text_str == promo_text) |
| 49 return l10n_util::GetStringUTF8(kPromoStringToIdsMap[i].message_id); | 49 return l10n_util::GetStringUTF8(kPromoStringToIdsMap[i].message_id); |
| 50 } | 50 } |
| 51 return promo_text; | 51 return std::string(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 } // namespace | 54 } // namespace |
| 55 | 55 |
| 56 NotificationPromoWhatsNew::NotificationPromoWhatsNew(PrefService* local_state) | 56 NotificationPromoWhatsNew::NotificationPromoWhatsNew(PrefService* local_state) |
| 57 : local_state_(local_state), | 57 : local_state_(local_state), |
| 58 valid_(false), | 58 valid_(false), |
| 59 notification_promo_(local_state_) {} | 59 notification_promo_(local_state_) {} |
| 60 | 60 |
| 61 NotificationPromoWhatsNew::~NotificationPromoWhatsNew() {} | 61 NotificationPromoWhatsNew::~NotificationPromoWhatsNew() {} |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 // |max_seconds_since_install_| seconds ago. | 134 // |max_seconds_since_install_| seconds ago. |
| 135 int64_t install_date = local_state_->GetInt64(metrics::prefs::kInstallDate); | 135 int64_t install_date = local_state_->GetInt64(metrics::prefs::kInstallDate); |
| 136 const base::Time last_view_time = | 136 const base::Time last_view_time = |
| 137 base::Time::FromTimeT(install_date) + | 137 base::Time::FromTimeT(install_date) + |
| 138 base::TimeDelta::FromSeconds(max_seconds_since_install_); | 138 base::TimeDelta::FromSeconds(max_seconds_since_install_); |
| 139 if (last_view_time < base::Time::Now()) { | 139 if (last_view_time < base::Time::Now()) { |
| 140 return false; | 140 return false; |
| 141 } | 141 } |
| 142 } | 142 } |
| 143 | 143 |
| 144 if (promo_name_ == "WKWVGotFasterPromo") { | |
| 145 // Promo is not relevant anymore: It was shown during the migration to the | |
| 146 // WKWebview. | |
| 147 return false; | |
| 148 } | |
| 149 | |
| 150 return true; | 144 return true; |
| 151 } | 145 } |
| 152 | 146 |
| 153 void NotificationPromoWhatsNew::HandleViewed() { | 147 void NotificationPromoWhatsNew::HandleViewed() { |
| 154 // TODO(justincohen): metrics actions names should be inlined. Since | 148 // TODO(justincohen): metrics actions names should be inlined. Since |
| 155 // metric_name_ is retrieved from a server, it's not possible to know at build | 149 // metric_name_ is retrieved from a server, it's not possible to know at build |
| 156 // time the values. We should investigate to find a solution. In the meantime, | 150 // time the values. We should investigate to find a solution. In the meantime, |
| 157 // metrics will be reported hashed. | 151 // metrics will be reported hashed. |
| 158 // http://crbug.com/437500 | 152 // http://crbug.com/437500 |
| 159 std::string metric = std::string("WhatsNewPromoViewed_") + metric_name_; | 153 std::string metric = std::string("WhatsNewPromoViewed_") + metric_name_; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 188 return WHATS_NEW_LOGO; | 182 return WHATS_NEW_LOGO; |
| 189 } else if (icon_name == "logoWithRoundedRectangle") { | 183 } else if (icon_name == "logoWithRoundedRectangle") { |
| 190 return WHATS_NEW_LOGO_ROUNDED_RECTANGLE; | 184 return WHATS_NEW_LOGO_ROUNDED_RECTANGLE; |
| 191 } | 185 } |
| 192 return WHATS_NEW_INFO; | 186 return WHATS_NEW_INFO; |
| 193 } | 187 } |
| 194 | 188 |
| 195 bool NotificationPromoWhatsNew::InitFromNotificationPromo() { | 189 bool NotificationPromoWhatsNew::InitFromNotificationPromo() { |
| 196 valid_ = false; | 190 valid_ = false; |
| 197 | 191 |
| 192 promo_text_ = GetLocalizedPromoText(notification_promo_.promo_text()); |
| 193 if (promo_text_.empty()) |
| 194 return valid_; |
| 195 |
| 196 notification_promo_.promo_payload()->GetString("metric_name", &metric_name_); |
| 197 if (metric_name_.empty()) |
| 198 return valid_; |
| 199 |
| 198 notification_promo_.promo_payload()->GetString("promo_type", &promo_type_); | 200 notification_promo_.promo_payload()->GetString("promo_type", &promo_type_); |
| 199 notification_promo_.promo_payload()->GetString("metric_name", &metric_name_); | |
| 200 promo_text_ = GetLocalizedPromoText(notification_promo_.promo_text()); | |
| 201 | |
| 202 if (IsURLPromo()) { | 201 if (IsURLPromo()) { |
| 203 std::string url_text; | 202 std::string url_text; |
| 204 notification_promo_.promo_payload()->GetString("url", &url_text); | 203 notification_promo_.promo_payload()->GetString("url", &url_text); |
| 205 url_ = GURL(url_text); | 204 url_ = GURL(url_text); |
| 206 if (url_.is_empty() || !url_.is_valid()) { | 205 if (url_.is_empty() || !url_.is_valid()) { |
| 207 valid_ = false; | |
| 208 return valid_; | 206 return valid_; |
| 209 } | 207 } |
| 210 } | 208 } else if (IsChromeCommand()) { |
| 211 | |
| 212 if (IsChromeCommand()) { | |
| 213 std::string command; | 209 std::string command; |
| 214 notification_promo_.promo_payload()->GetString("command", &command); | 210 notification_promo_.promo_payload()->GetString("command", &command); |
| 215 if (command == "bookmark") { | 211 if (command == "bookmark") { |
| 216 command_id_ = IDC_SHOW_BOOKMARK_MANAGER; | 212 command_id_ = IDC_SHOW_BOOKMARK_MANAGER; |
| 217 } else if (command == "ratethisapp") { | 213 } else if (command == "ratethisapp") { |
| 218 command_id_ = IDC_RATE_THIS_APP; | 214 command_id_ = IDC_RATE_THIS_APP; |
| 219 } else { | 215 } else { |
| 220 valid_ = false; | |
| 221 return valid_; | 216 return valid_; |
| 222 } | 217 } |
| 218 } else { // If |promo_type_| is not set to URL or Command, return early. |
| 219 return valid_; |
| 223 } | 220 } |
| 224 | 221 |
| 225 valid_ = | 222 valid_ = true; |
| 226 !metric_name_.empty() && !promo_type_.empty() && !promo_text_.empty(); | |
| 227 | 223 |
| 228 notification_promo_.promo_payload()->GetString("promo_name", &promo_name_); | 224 // Optional values don't need validation. |
| 229 std::string icon_name; | 225 std::string icon_name; |
| 230 notification_promo_.promo_payload()->GetString("icon", &icon_name); | 226 notification_promo_.promo_payload()->GetString("icon", &icon_name); |
| 231 icon_ = ParseIconName(icon_name); | 227 icon_ = ParseIconName(icon_name); |
| 232 | 228 |
| 233 // Optional values don't need validation. | |
| 234 seconds_since_install_ = 0; | 229 seconds_since_install_ = 0; |
| 235 notification_promo_.promo_payload()->GetInteger("seconds_since_install", | 230 notification_promo_.promo_payload()->GetInteger("seconds_since_install", |
| 236 &seconds_since_install_); | 231 &seconds_since_install_); |
| 237 max_seconds_since_install_ = 0; | 232 max_seconds_since_install_ = 0; |
| 238 notification_promo_.promo_payload()->GetInteger("max_seconds_since_install", | 233 notification_promo_.promo_payload()->GetInteger("max_seconds_since_install", |
| 239 &max_seconds_since_install_); | 234 &max_seconds_since_install_); |
| 240 return valid_; | 235 return valid_; |
| 241 } | 236 } |
| 242 | 237 |
| 243 void NotificationPromoWhatsNew::InjectFakePromo(const std::string& promo_id, | 238 void NotificationPromoWhatsNew::InjectFakePromo(const std::string& promo_id, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 base::ReplaceStringPlaceholders(promo_json, replacements, NULL); | 273 base::ReplaceStringPlaceholders(promo_json, replacements, NULL); |
| 279 | 274 |
| 280 std::unique_ptr<base::Value> value( | 275 std::unique_ptr<base::Value> value( |
| 281 base::JSONReader::Read(promo_json_filled_in)); | 276 base::JSONReader::Read(promo_json_filled_in)); |
| 282 base::DictionaryValue* dict = NULL; | 277 base::DictionaryValue* dict = NULL; |
| 283 if (value->GetAsDictionary(&dict)) { | 278 if (value->GetAsDictionary(&dict)) { |
| 284 notification_promo_.InitFromJson( | 279 notification_promo_.InitFromJson( |
| 285 *dict, ios::NotificationPromo::MOBILE_NTP_WHATS_NEW_PROMO); | 280 *dict, ios::NotificationPromo::MOBILE_NTP_WHATS_NEW_PROMO); |
| 286 } | 281 } |
| 287 } | 282 } |
| OLD | NEW |