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 kDataSaverNotificationId[] = "chrome://settings/internet/data_saver"; | |
| 49 const char kDataSaverExtensionUrl[] = | |
| 50 "https://chrome.google.com/webstore/detail/" | |
| 51 "pfmgfdlgomnbgkofeojodiodmgpgmkac"; | |
| 44 | 52 |
| 45 const int kNotificationCountPrefDefault = -1; | 53 const int kNotificationCountPrefDefault = -1; |
| 54 const int kTimesToShowDataSaverPrompt = 2; | |
| 46 | 55 |
| 47 bool GetBooleanPref(const char* pref_name) { | 56 bool GetBooleanPref(const char* pref_name) { |
| 48 Profile* profile = ProfileManager::GetPrimaryUserProfile(); | 57 Profile* profile = ProfileManager::GetPrimaryUserProfile(); |
| 49 PrefService* prefs = profile->GetPrefs(); | 58 PrefService* prefs = profile->GetPrefs(); |
| 50 return prefs->GetBoolean(pref_name); | 59 return prefs->GetBoolean(pref_name); |
| 51 } | 60 } |
| 52 | 61 |
| 53 int GetIntegerLocalPref(const char* pref_name) { | 62 int GetIntegerLocalPref(const char* pref_name) { |
| 54 PrefService* prefs = g_browser_process->local_state(); | 63 PrefService* prefs = g_browser_process->local_state(); |
| 55 return prefs->GetInteger(pref_name); | 64 return prefs->GetInteger(pref_name); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 78 // Returns prefs::kCarrierDealPromoShown which is number of times | 87 // Returns prefs::kCarrierDealPromoShown which is number of times |
| 79 // carrier deal notification has been shown to users on this machine. | 88 // carrier deal notification has been shown to users on this machine. |
| 80 int GetCarrierDealPromoShown() { | 89 int GetCarrierDealPromoShown() { |
| 81 return GetIntegerLocalPref(prefs::kCarrierDealPromoShown); | 90 return GetIntegerLocalPref(prefs::kCarrierDealPromoShown); |
| 82 } | 91 } |
| 83 | 92 |
| 84 void SetCarrierDealPromoShown(int value) { | 93 void SetCarrierDealPromoShown(int value) { |
| 85 SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value); | 94 SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value); |
| 86 } | 95 } |
| 87 | 96 |
| 97 // Returns number of times the Data Saver prompt has been displayed. | |
| 98 int GetDataSaverPromptsShown() { | |
| 99 return ProfileManager::GetActiveUserProfile()->GetPrefs()->GetInteger( | |
| 100 prefs::kDataSaverPromptsShown); | |
| 101 } | |
| 102 | |
| 103 // Updates number of times the Data Saver prompt has been displayed. | |
| 104 void SetDataSaverPromptsShown(int times_shown) { | |
| 105 ProfileManager::GetActiveUserProfile()->GetPrefs()->SetInteger( | |
| 106 prefs::kDataSaverPromptsShown, times_shown); | |
| 107 } | |
| 108 | |
| 109 // Is command line switch set for Data Saver prompt? | |
| 110 bool DataSaverSwitchEnabled() { | |
| 111 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 112 chromeos::switches::kDisableDataSaverPrompt)) | |
| 113 return false; | |
| 114 | |
| 115 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 116 chromeos::switches::kEnableDataSaverPrompt); | |
| 117 } | |
| 118 | |
| 119 // Is command line switch set for Data Saver demo mode, where we show the prompt | |
| 120 // after any change in network properties? | |
| 121 bool DataSaverSwitchDemoMode() { | |
| 122 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 123 chromeos::switches::kDisableDataSaverPrompt)) | |
| 124 return false; | |
| 125 | |
| 126 const std::string value = | |
| 127 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 128 chromeos::switches::kEnableDataSaverPrompt); | |
| 129 return value == chromeos::switches::kDataSaverPromptDemoMode; | |
| 130 } | |
| 131 | |
| 88 const chromeos::MobileConfig::Carrier* GetCarrier( | 132 const chromeos::MobileConfig::Carrier* GetCarrier( |
| 89 const NetworkState* cellular) { | 133 const NetworkState* cellular) { |
| 90 const DeviceState* device = NetworkHandler::Get()->network_state_handler()-> | 134 const DeviceState* device = NetworkHandler::Get()->network_state_handler()-> |
| 91 GetDeviceState(cellular->device_path()); | 135 GetDeviceState(cellular->device_path()); |
| 92 std::string carrier_id = device ? device->home_provider_id() : ""; | 136 std::string carrier_id = device ? device->home_provider_id() : ""; |
| 93 if (carrier_id.empty()) { | 137 if (carrier_id.empty()) { |
| 94 NET_LOG_ERROR("Empty carrier ID for cellular network", | 138 NET_LOG_ERROR("Empty carrier ID for cellular network", |
| 95 device ? device->path(): "No device"); | 139 device ? device->path(): "No device"); |
| 96 return NULL; | 140 return NULL; |
| 97 } | 141 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 chrome::HOST_DESKTOP_TYPE_ASH); | 175 chrome::HOST_DESKTOP_TYPE_ASH); |
| 132 chrome::ShowSingletonTab(displayer.browser(), GURL(info_url)); | 176 chrome::ShowSingletonTab(displayer.browser(), GURL(info_url)); |
| 133 } | 177 } |
| 134 | 178 |
| 135 } // namespace | 179 } // namespace |
| 136 | 180 |
| 137 //////////////////////////////////////////////////////////////////////////////// | 181 //////////////////////////////////////////////////////////////////////////////// |
| 138 // DataPromoNotification | 182 // DataPromoNotification |
| 139 | 183 |
| 140 DataPromoNotification::DataPromoNotification() | 184 DataPromoNotification::DataPromoNotification() |
| 141 : check_for_promo_(true), | 185 : notifications_shown_(false), |
| 142 weak_ptr_factory_(this) { | 186 weak_ptr_factory_(this) { |
| 143 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); | 187 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); |
| 144 } | 188 } |
| 145 | 189 |
| 146 DataPromoNotification::~DataPromoNotification() { | 190 DataPromoNotification::~DataPromoNotification() { |
| 147 if (NetworkHandler::IsInitialized()) { | 191 if (NetworkHandler::IsInitialized()) { |
| 148 NetworkHandler::Get()->network_state_handler()->RemoveObserver( | 192 NetworkHandler::Get()->network_state_handler()->RemoveObserver( |
| 149 this, FROM_HERE); | 193 this, FROM_HERE); |
| 150 } | 194 } |
| 151 } | 195 } |
| 152 | 196 |
| 153 void DataPromoNotification::RegisterPrefs(PrefRegistrySimple* registry) { | 197 void DataPromoNotification::RegisterPrefs(PrefRegistrySimple* registry) { |
| 154 // Carrier deal notification shown count defaults to 0. | 198 // Carrier deal notification shown count defaults to 0. |
| 155 registry->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0); | 199 registry->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0); |
| 156 } | 200 } |
| 157 | 201 |
| 158 void DataPromoNotification::NetworkPropertiesUpdated( | 202 void DataPromoNotification::NetworkPropertiesUpdated( |
| 159 const NetworkState* network) { | 203 const NetworkState* network) { |
| 160 if (!network || network->type() != shill::kTypeCellular) | 204 if (!network || |
| 205 (network->type() != shill::kTypeCellular && !DataSaverSwitchDemoMode())) | |
| 161 return; | 206 return; |
| 162 ShowOptionalMobileDataPromoNotification(); | 207 ShowOptionalMobileDataPromoNotification(); |
| 163 } | 208 } |
| 164 | 209 |
| 165 void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) { | 210 void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) { |
| 166 // Call NetworkPropertiesUpdated in case the Cellular network became the | 211 // Call NetworkPropertiesUpdated in case the Cellular network became the |
| 167 // default network. | 212 // default network. |
| 168 NetworkPropertiesUpdated(network); | 213 NetworkPropertiesUpdated(network); |
| 169 } | 214 } |
| 170 | 215 |
| 171 void DataPromoNotification::ShowOptionalMobileDataPromoNotification() { | 216 void DataPromoNotification::ShowOptionalMobileDataPromoNotification() { |
| 172 // Display a one-time notification for authenticated users on first use | 217 // Do not show notifications to unauthenticated users, or when requesting a |
| 173 // of Mobile Data connection or if there is a carrier deal defined | 218 // network connection, or if there's no default_network. |
| 174 // show that even if user has already seen generic promo. | 219 if (!LoginState::Get()->IsUserAuthenticated()) |
| 175 if (!check_for_promo_ || !LoginState::Get()->IsUserAuthenticated()) | |
| 176 return; | 220 return; |
| 177 const NetworkState* default_network = | 221 const NetworkState* default_network = |
| 178 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); | 222 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); |
| 179 if (!default_network || default_network->type() != shill::kTypeCellular) | 223 if (!default_network) |
| 180 return; | 224 return; |
| 181 // When requesting a network connection, do not show the notification. | |
| 182 if (NetworkHandler::Get()->network_connection_handler()-> | 225 if (NetworkHandler::Get()->network_connection_handler()-> |
| 183 HasPendingConnectRequest()) | 226 HasPendingConnectRequest()) |
| 184 return; | 227 return; |
| 185 | 228 |
| 229 if (!DataSaverSwitchDemoMode() && | |
| 230 (notifications_shown_ || default_network->type() != shill::kTypeCellular)) | |
| 231 return; | |
| 232 | |
| 233 notifications_shown_ = true; | |
| 234 bool data_saver_prompt_shown = ShowDataSaverNotification(); | |
| 235 | |
| 236 // Display a one-time notification on first use of Mobile Data connection, or | |
| 237 // if there is a carrier deal defined show that even if user has already seen | |
| 238 // generic promo. Show deal regardless of |data_saver_prompt_shown|. | |
| 186 int carrier_deal_promo_pref = kNotificationCountPrefDefault; | 239 int carrier_deal_promo_pref = kNotificationCountPrefDefault; |
| 187 const MobileConfig::CarrierDeal* deal = NULL; | 240 const MobileConfig::CarrierDeal* deal = NULL; |
| 188 const MobileConfig::Carrier* carrier = GetCarrier(default_network); | 241 const MobileConfig::Carrier* carrier = GetCarrier(default_network); |
| 189 if (carrier) | 242 if (carrier) |
| 190 deal = GetCarrierDeal(carrier); | 243 deal = GetCarrierDeal(carrier); |
| 191 | 244 |
| 192 base::string16 message = | 245 base::string16 message = |
| 193 l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE); | 246 l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE); |
| 194 std::string info_url; | 247 std::string info_url; |
| 195 if (deal) { | 248 if (deal) { |
| 196 carrier_deal_promo_pref = GetCarrierDealPromoShown(); | 249 carrier_deal_promo_pref = GetCarrierDealPromoShown(); |
| 197 const std::string locale = g_browser_process->GetApplicationLocale(); | 250 const std::string locale = g_browser_process->GetApplicationLocale(); |
| 198 std::string deal_text = | 251 std::string deal_text = |
| 199 deal->GetLocalizedString(locale, "notification_text"); | 252 deal->GetLocalizedString(locale, "notification_text"); |
| 200 message = base::UTF8ToUTF16(deal_text + "\n\n") + message; | 253 message = base::UTF8ToUTF16(deal_text + "\n\n") + message; |
| 201 info_url = deal->info_url(); | 254 info_url = deal->info_url(); |
| 202 if (info_url.empty() && carrier) | 255 if (info_url.empty() && carrier) |
| 203 info_url = carrier->top_up_url(); | 256 info_url = carrier->top_up_url(); |
| 204 } else if (!ShouldShow3gPromoNotification()) { | 257 } else if (data_saver_prompt_shown || !ShouldShow3gPromoNotification()) { |
| 205 check_for_promo_ = false; | |
| 206 return; | 258 return; |
| 207 } | 259 } |
| 208 | 260 |
| 209 int icon_id; | 261 int icon_id; |
| 210 if (default_network->network_technology() == shill::kNetworkTechnologyLte) | 262 if (default_network->network_technology() == shill::kNetworkTechnologyLte) |
| 211 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE; | 263 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE; |
| 212 else | 264 else |
| 213 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G; | 265 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G; |
| 214 const gfx::Image& icon = | 266 const gfx::Image& icon = |
| 215 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id); | 267 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id); |
| 216 | 268 |
| 217 message_center::MessageCenter::Get()->AddNotification( | 269 message_center::MessageCenter::Get()->AddNotification( |
| 218 message_center::Notification::CreateSystemNotification( | 270 message_center::Notification::CreateSystemNotification( |
| 219 kDataPromoNotificationId, base::string16() /* title */, message, icon, | 271 kDataPromoNotificationId, base::string16() /* title */, message, icon, |
| 220 ui::NetworkStateNotifier::kNotifierNetwork, | 272 ui::NetworkStateNotifier::kNotifierNetwork, |
| 221 base::Bind(&NotificationClicked, default_network->path(), info_url))); | 273 base::Bind(&NotificationClicked, default_network->path(), info_url))); |
| 222 | 274 |
| 223 check_for_promo_ = false; | |
| 224 SetShow3gPromoNotification(false); | 275 SetShow3gPromoNotification(false); |
| 225 if (carrier_deal_promo_pref != kNotificationCountPrefDefault) | 276 if (carrier_deal_promo_pref != kNotificationCountPrefDefault) |
| 226 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1); | 277 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1); |
| 227 } | 278 } |
| 228 | 279 |
| 280 bool DataPromoNotification::ShowDataSaverNotification() { | |
| 281 if (!DataSaverSwitchEnabled()) | |
| 282 return false; | |
| 283 | |
| 284 if (message_center::MessageCenter::Get()->FindVisibleNotificationById( | |
| 285 kDataSaverNotificationId)) // already showing. | |
| 286 return true; | |
| 287 | |
| 288 int times_shown = GetDataSaverPromptsShown(); | |
| 289 if (!DataSaverSwitchDemoMode() && times_shown >= kTimesToShowDataSaverPrompt) | |
| 290 return false; | |
| 291 | |
| 292 if (extensions::ExtensionRegistry::Get(ProfileManager::GetActiveUserProfile()) | |
| 293 ->GetExtensionById(extension_misc::kDataSaverExtensionId, | |
| 294 extensions::ExtensionRegistry::EVERYTHING)) { | |
| 295 // If extension is installed, disable future prompts. | |
| 296 SetDataSaverPromptsShown(kTimesToShowDataSaverPrompt); | |
| 297 return false; | |
| 298 } | |
| 299 | |
| 300 base::string16 title = l10n_util::GetStringUTF16(IDS_3G_DATASAVER_TITLE); | |
| 301 base::string16 message = l10n_util::GetStringUTF16(IDS_3G_DATASAVER_MESSAGE); | |
| 302 const gfx::Image& icon = | |
| 303 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 304 IDR_AURA_UBER_TRAY_NOTIFICATION_DATASAVER); | |
| 305 | |
| 306 message_center::MessageCenter::Get()->AddNotification( | |
| 307 message_center::Notification::CreateSystemNotification( | |
| 308 kDataSaverNotificationId, title, message, icon, | |
| 309 ui::NetworkStateNotifier::kNotifierNetwork, | |
| 310 base::Bind(&NotificationClicked, "", kDataSaverExtensionUrl))); | |
|
stevenjb
2015/04/03 22:27:53
Oh, I see, NotificationClicked looks at kDataSaver
Greg Levin
2015/04/04 19:27:25
I fixed it so that it won't open a blank web page
stevenjb
2015/04/06 15:34:39
This looks correct. I think that I missed that kDa
| |
| 311 | |
| 312 if (DataSaverSwitchDemoMode()) { | |
| 313 SetDataSaverPromptsShown(0); // demo mode resets times shown counts. | |
| 314 SetShow3gPromoNotification(true); | |
| 315 } else { | |
| 316 SetDataSaverPromptsShown(times_shown + 1); | |
| 317 } | |
| 318 | |
| 319 return true; | |
| 320 } | |
| 321 | |
| 229 } // namespace chromeos | 322 } // namespace chromeos |
| OLD | NEW |