Chromium Code Reviews| 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/chromeos/status/data_promo_notification.h" | 5 #include "chrome/browser/chromeos/status/data_promo_notification.h" |
| 6 | 6 |
| 7 #include "ash/system/system_notifier.h" | 7 #include "ash/system/system_notifier.h" |
| 8 #include "base/command_line.h" | |
| 8 #include "base/prefs/pref_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
| 9 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/chromeos/login/helper.h" | 13 #include "chrome/browser/chromeos/login/helper.h" |
| 13 #include "chrome/browser/chromeos/mobile_config.h" | 14 #include "chrome/browser/chromeos/mobile_config.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_list.h" | 18 #include "chrome/browser/ui/browser_list.h" |
| 18 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 19 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
| 19 #include "chrome/browser/ui/singleton_tabs.h" | 20 #include "chrome/browser/ui/singleton_tabs.h" |
| 21 #include "chrome/common/extensions/extension_constants.h" | |
| 20 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 21 #include "chrome/grit/generated_resources.h" | 23 #include "chrome/grit/generated_resources.h" |
| 24 #include "chromeos/chromeos_switches.h" | |
| 22 #include "chromeos/login/login_state.h" | 25 #include "chromeos/login/login_state.h" |
| 23 #include "chromeos/network/device_state.h" | 26 #include "chromeos/network/device_state.h" |
| 24 #include "chromeos/network/network_connection_handler.h" | 27 #include "chromeos/network/network_connection_handler.h" |
| 25 #include "chromeos/network/network_event_log.h" | 28 #include "chromeos/network/network_event_log.h" |
| 26 #include "chromeos/network/network_state.h" | 29 #include "chromeos/network/network_state.h" |
| 27 #include "chromeos/network/network_state_handler.h" | 30 #include "chromeos/network/network_state_handler.h" |
| 31 #include "extensions/browser/extension_registry.h" | |
| 28 #include "grit/ui_chromeos_resources.h" | 32 #include "grit/ui_chromeos_resources.h" |
| 29 #include "third_party/cros_system_api/dbus/service_constants.h" | 33 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 30 #include "ui/base/l10n/l10n_util.h" | 34 #include "ui/base/l10n/l10n_util.h" |
| 31 #include "ui/base/resource/resource_bundle.h" | 35 #include "ui/base/resource/resource_bundle.h" |
| 32 #include "ui/chromeos/network/network_connect.h" | 36 #include "ui/chromeos/network/network_connect.h" |
| 33 #include "ui/chromeos/network/network_state_notifier.h" | 37 #include "ui/chromeos/network/network_state_notifier.h" |
| 34 #include "ui/message_center/message_center.h" | 38 #include "ui/message_center/message_center.h" |
| 35 #include "ui/message_center/notification.h" | 39 #include "ui/message_center/notification.h" |
| 36 #include "ui/views/view.h" | 40 #include "ui/views/view.h" |
| 37 #include "ui/views/widget/widget.h" | 41 #include "ui/views/widget/widget.h" |
| 38 | 42 |
| 39 namespace chromeos { | 43 namespace chromeos { |
| 40 | 44 |
| 41 namespace { | 45 namespace { |
| 42 | 46 |
| 43 const char kDataPromoNotificationId[] = "chrome://settings/internet/data_promo"; | 47 const char kDataPromoNotificationId[] = "chrome://settings/internet/data_promo"; |
| 48 const char kDataSaverExtensionUrl[] = | |
| 49 "https://chrome.google.com/webstore/detail/" | |
| 50 "pfmgfdlgomnbgkofeojodiodmgpgmkac"; | |
| 44 | 51 |
| 45 const int kNotificationCountPrefDefault = -1; | 52 const int kNotificationCountPrefDefault = -1; |
| 53 const int kTimesToShowDataSaverPrompt = 2; | |
| 46 | 54 |
| 47 bool GetBooleanPref(const char* pref_name) { | 55 bool GetBooleanPref(const char* pref_name) { |
| 48 Profile* profile = ProfileManager::GetPrimaryUserProfile(); | 56 Profile* profile = ProfileManager::GetPrimaryUserProfile(); |
| 49 PrefService* prefs = profile->GetPrefs(); | 57 PrefService* prefs = profile->GetPrefs(); |
| 50 return prefs->GetBoolean(pref_name); | 58 return prefs->GetBoolean(pref_name); |
| 51 } | 59 } |
| 52 | 60 |
| 53 int GetIntegerLocalPref(const char* pref_name) { | 61 int GetIntegerLocalPref(const char* pref_name) { |
| 54 PrefService* prefs = g_browser_process->local_state(); | 62 PrefService* prefs = g_browser_process->local_state(); |
| 55 return prefs->GetInteger(pref_name); | 63 return prefs->GetInteger(pref_name); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 78 // Returns prefs::kCarrierDealPromoShown which is number of times | 86 // Returns prefs::kCarrierDealPromoShown which is number of times |
| 79 // carrier deal notification has been shown to users on this machine. | 87 // carrier deal notification has been shown to users on this machine. |
| 80 int GetCarrierDealPromoShown() { | 88 int GetCarrierDealPromoShown() { |
| 81 return GetIntegerLocalPref(prefs::kCarrierDealPromoShown); | 89 return GetIntegerLocalPref(prefs::kCarrierDealPromoShown); |
| 82 } | 90 } |
| 83 | 91 |
| 84 void SetCarrierDealPromoShown(int value) { | 92 void SetCarrierDealPromoShown(int value) { |
| 85 SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value); | 93 SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value); |
| 86 } | 94 } |
| 87 | 95 |
| 96 // Returns number of times the Data Saver prompt has been displayed. | |
| 97 int GetDataSaverPromptsShown() { | |
| 98 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
| 99 PrefService* prefs = profile->GetPrefs(); | |
| 100 return prefs->GetInteger(prefs::kDataSaverPromptsShown); | |
| 101 } | |
| 102 | |
| 103 // Updates number of times the Data Saver prompt has been displayed. | |
| 104 void SetDataSaverPromptsShown(int times_shown) { | |
| 105 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
| 106 PrefService* prefs = profile->GetPrefs(); | |
| 107 prefs->SetInteger(prefs::kDataSaverPromptsShown, times_shown); | |
| 108 } | |
| 109 | |
| 88 const chromeos::MobileConfig::Carrier* GetCarrier( | 110 const chromeos::MobileConfig::Carrier* GetCarrier( |
| 89 const NetworkState* cellular) { | 111 const NetworkState* cellular) { |
| 90 const DeviceState* device = NetworkHandler::Get()->network_state_handler()-> | 112 const DeviceState* device = NetworkHandler::Get()->network_state_handler()-> |
| 91 GetDeviceState(cellular->device_path()); | 113 GetDeviceState(cellular->device_path()); |
| 92 std::string carrier_id = device ? device->home_provider_id() : ""; | 114 std::string carrier_id = device ? device->home_provider_id() : ""; |
| 93 if (carrier_id.empty()) { | 115 if (carrier_id.empty()) { |
| 94 NET_LOG_ERROR("Empty carrier ID for cellular network", | 116 NET_LOG_ERROR("Empty carrier ID for cellular network", |
| 95 device ? device->path(): "No device"); | 117 device ? device->path(): "No device"); |
| 96 return NULL; | 118 return NULL; |
| 97 } | 119 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 | 174 |
| 153 void DataPromoNotification::RegisterPrefs(PrefRegistrySimple* registry) { | 175 void DataPromoNotification::RegisterPrefs(PrefRegistrySimple* registry) { |
| 154 // Carrier deal notification shown count defaults to 0. | 176 // Carrier deal notification shown count defaults to 0. |
| 155 registry->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0); | 177 registry->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0); |
| 156 } | 178 } |
| 157 | 179 |
| 158 void DataPromoNotification::NetworkPropertiesUpdated( | 180 void DataPromoNotification::NetworkPropertiesUpdated( |
| 159 const NetworkState* network) { | 181 const NetworkState* network) { |
| 160 if (!network || network->type() != shill::kTypeCellular) | 182 if (!network || network->type() != shill::kTypeCellular) |
| 161 return; | 183 return; |
| 162 ShowOptionalMobileDataPromoNotification(); | 184 |
| 185 bool hotkey_debug = network->path() == kHotkeyDebugDataSaver; | |
| 186 ShowOptionalMobileDataPromoNotification(hotkey_debug); | |
| 163 } | 187 } |
| 164 | 188 |
| 165 void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) { | 189 void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) { |
| 166 // Call NetworkPropertiesUpdated in case the Cellular network became the | 190 // Call NetworkPropertiesUpdated in case the Cellular network became the |
| 167 // default network. | 191 // default network. |
| 168 NetworkPropertiesUpdated(network); | 192 NetworkPropertiesUpdated(network); |
| 169 } | 193 } |
| 170 | 194 |
| 171 void DataPromoNotification::ShowOptionalMobileDataPromoNotification() { | 195 void DataPromoNotification::ShowOptionalMobileDataPromoNotification( |
| 172 // Display a one-time notification for authenticated users on first use | 196 bool hotkey_debug) { |
| 173 // of Mobile Data connection or if there is a carrier deal defined | |
| 174 // show that even if user has already seen generic promo. | |
| 175 if (!check_for_promo_ || !LoginState::Get()->IsUserAuthenticated()) | |
| 176 return; | |
| 177 const NetworkState* default_network = | 197 const NetworkState* default_network = |
| 178 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); | 198 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); |
| 179 if (!default_network || default_network->type() != shill::kTypeCellular) | 199 // Do not show notifications to unauthenticated users, or when requesting a |
| 200 // network connection, or if no default_network. | |
| 201 if (!default_network || !LoginState::Get()->IsUserAuthenticated() || | |
| 202 NetworkHandler::Get() | |
| 203 ->network_connection_handler() | |
| 204 ->HasPendingConnectRequest()) | |
| 180 return; | 205 return; |
| 181 // When requesting a network connection, do not show the notification. | 206 // |hotkey_debug| is temporary code that lets us invoke this notification |
| 182 if (NetworkHandler::Get()->network_connection_handler()-> | 207 // via CTRL+ALT+SHIFT+3. |
| 183 HasPendingConnectRequest()) | 208 if (!hotkey_debug && default_network->type() != shill::kTypeCellular) |
| 184 return; | 209 return; |
| 185 | 210 |
| 211 bool data_saver_prompt_shown = ShowDataSaverNotification(hotkey_debug); | |
| 212 | |
| 213 if (!check_for_promo_) | |
| 214 return; | |
| 215 | |
| 216 // Display a one-time notification on first use of Mobile Data connection or | |
| 217 // if there is a carrier deal defined show that even if user has already seen | |
| 218 // generic promo. | |
| 186 int carrier_deal_promo_pref = kNotificationCountPrefDefault; | 219 int carrier_deal_promo_pref = kNotificationCountPrefDefault; |
| 187 const MobileConfig::CarrierDeal* deal = NULL; | 220 const MobileConfig::CarrierDeal* deal = NULL; |
| 188 const MobileConfig::Carrier* carrier = GetCarrier(default_network); | 221 const MobileConfig::Carrier* carrier = GetCarrier(default_network); |
| 189 if (carrier) | 222 if (carrier) |
| 190 deal = GetCarrierDeal(carrier); | 223 deal = GetCarrierDeal(carrier); |
| 191 | 224 |
| 192 base::string16 message = | 225 base::string16 message = |
| 193 l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE); | 226 l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE); |
| 194 std::string info_url; | 227 std::string info_url; |
| 195 if (deal) { | 228 if (deal) { |
| 196 carrier_deal_promo_pref = GetCarrierDealPromoShown(); | 229 carrier_deal_promo_pref = GetCarrierDealPromoShown(); |
| 197 const std::string locale = g_browser_process->GetApplicationLocale(); | 230 const std::string locale = g_browser_process->GetApplicationLocale(); |
| 198 std::string deal_text = | 231 std::string deal_text = |
| 199 deal->GetLocalizedString(locale, "notification_text"); | 232 deal->GetLocalizedString(locale, "notification_text"); |
| 200 message = base::UTF8ToUTF16(deal_text + "\n\n") + message; | 233 message = base::UTF8ToUTF16(deal_text + "\n\n") + message; |
| 201 info_url = deal->info_url(); | 234 info_url = deal->info_url(); |
| 202 if (info_url.empty() && carrier) | 235 if (info_url.empty() && carrier) |
| 203 info_url = carrier->top_up_url(); | 236 info_url = carrier->top_up_url(); |
| 204 } else if (!ShouldShow3gPromoNotification()) { | 237 } else if (data_saver_prompt_shown || !ShouldShow3gPromoNotification()) { |
| 205 check_for_promo_ = false; | 238 check_for_promo_ = false; |
| 206 return; | 239 return; |
| 207 } | 240 } |
| 208 | 241 |
| 209 int icon_id; | 242 int icon_id; |
| 210 if (default_network->network_technology() == shill::kNetworkTechnologyLte) | 243 if (default_network->network_technology() == shill::kNetworkTechnologyLte) |
| 211 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE; | 244 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE; |
| 212 else | 245 else |
| 213 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G; | 246 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G; |
| 214 const gfx::Image& icon = | 247 const gfx::Image& icon = |
| 215 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id); | 248 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id); |
| 216 | 249 |
| 217 message_center::MessageCenter::Get()->AddNotification( | 250 message_center::MessageCenter::Get()->AddNotification( |
| 218 message_center::Notification::CreateSystemNotification( | 251 message_center::Notification::CreateSystemNotification( |
| 219 kDataPromoNotificationId, base::string16() /* title */, message, icon, | 252 kDataPromoNotificationId, base::string16() /* title */, message, icon, |
| 220 ui::NetworkStateNotifier::kNotifierNetwork, | 253 ui::NetworkStateNotifier::kNotifierNetwork, |
| 221 base::Bind(&NotificationClicked, default_network->path(), info_url))); | 254 base::Bind(&NotificationClicked, default_network->path(), info_url))); |
| 222 | 255 |
| 223 check_for_promo_ = false; | 256 check_for_promo_ = false; |
| 224 SetShow3gPromoNotification(false); | 257 SetShow3gPromoNotification(false); |
| 225 if (carrier_deal_promo_pref != kNotificationCountPrefDefault) | 258 if (carrier_deal_promo_pref != kNotificationCountPrefDefault) |
| 226 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1); | 259 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1); |
| 227 } | 260 } |
| 228 | 261 |
| 262 bool DataPromoNotification::ShowDataSaverNotification(bool hotkey_debug) { | |
| 263 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 264 chromeos::switches::kEnableDataSaverPrompt)) | |
| 265 return false; | |
| 266 | |
| 267 int times_shown = GetDataSaverPromptsShown(); | |
| 268 if (!hotkey_debug && times_shown >= kTimesToShowDataSaverPrompt) | |
| 269 return false; | |
| 270 | |
| 271 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
| 272 if (extensions::ExtensionRegistry::Get(profile) | |
| 273 ->GetExtensionById(extension_misc::kDataSaverExtensionId, | |
| 274 extensions::ExtensionRegistry::EVERYTHING)) { | |
| 275 // If extension is installed, disable future prompts. | |
| 276 SetDataSaverPromptsShown(kTimesToShowDataSaverPrompt); | |
| 277 return false; | |
| 278 } | |
| 279 | |
| 280 const NetworkState* default_network = | |
| 281 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); | |
| 282 | |
| 283 base::string16 title = l10n_util::GetStringUTF16(IDS_3G_DATASAVER_TITLE); | |
| 284 base::string16 message = l10n_util::GetStringUTF16(IDS_3G_DATASAVER_MESSAGE); | |
| 285 const gfx::Image& icon = | |
| 286 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 287 IDR_AURA_UBER_TRAY_NOTIFICATION_DS); | |
| 288 | |
| 289 // Clears the cached notification; otherwise, future calls from this session | |
| 290 // won't display it. | |
| 291 if (hotkey_debug) | |
|
Daniel Erat
2015/03/23 03:49:21
style guide requires curly brackets for multiline
Greg Levin
2015/03/24 20:35:32
Done.
| |
| 292 message_center::MessageCenter::Get()->RemoveNotification( | |
| 293 kDataPromoNotificationId, false); | |
| 294 | |
| 295 message_center::MessageCenter::Get()->AddNotification( | |
| 296 message_center::Notification::CreateSystemNotification( | |
| 297 kDataPromoNotificationId, title, message, icon, | |
| 298 ui::NetworkStateNotifier::kNotifierNetwork, | |
| 299 base::Bind(&NotificationClicked, default_network->path(), | |
| 300 kDataSaverExtensionUrl))); | |
| 301 | |
| 302 // Hotkey resets times shown count. | |
| 303 SetDataSaverPromptsShown(hotkey_debug ? 0 : times_shown + 1); | |
| 304 return true; | |
| 305 } | |
| 306 | |
| 229 } // namespace chromeos | 307 } // namespace chromeos |
| OLD | NEW |