Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: chrome/browser/chromeos/status/data_promo_notification.cc

Issue 1022333002: Initial CL for Data Saver (Flywheel) prompt when cellular network detected (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments from two reviewers Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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();
stevenjb 2015/03/25 17:57:08 nit: I think these would actually be more clear wi
Greg Levin 2015/04/03 18:28:37 Done.
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
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() ==
186 NetworkStateHandlerObserver::kDebugDataSaverNetworkPath;
187 ShowOptionalMobileDataPromoNotification(hotkey_debug);
163 } 188 }
164 189
165 void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) { 190 void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) {
166 // Call NetworkPropertiesUpdated in case the Cellular network became the 191 // Call NetworkPropertiesUpdated in case the Cellular network became the
167 // default network. 192 // default network.
168 NetworkPropertiesUpdated(network); 193 NetworkPropertiesUpdated(network);
169 } 194 }
170 195
171 void DataPromoNotification::ShowOptionalMobileDataPromoNotification() { 196 void DataPromoNotification::ShowOptionalMobileDataPromoNotification(
172 // Display a one-time notification for authenticated users on first use 197 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 = 198 const NetworkState* default_network =
178 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); 199 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
179 if (!default_network || default_network->type() != shill::kTypeCellular) 200 // Do not show notifications to unauthenticated users, or when requesting a
201 // network connection, or if no default_network.
202 if (!default_network || !LoginState::Get()->IsUserAuthenticated() ||
203 NetworkHandler::Get()
204 ->network_connection_handler()
205 ->HasPendingConnectRequest())
180 return; 206 return;
181 // When requesting a network connection, do not show the notification. 207 // |hotkey_debug| is temporary code that lets us invoke this notification
182 if (NetworkHandler::Get()->network_connection_handler()-> 208 // via CTRL+ALT+SHIFT+3.
183 HasPendingConnectRequest()) 209 if (!hotkey_debug && default_network->type() != shill::kTypeCellular)
184 return; 210 return;
185 211
212 bool data_saver_prompt_shown = ShowDataSaverNotification(hotkey_debug);
213
214 if (!check_for_promo_)
215 return;
216
217 // Display a one-time notification on first use of Mobile Data connection or
218 // if there is a carrier deal defined show that even if user has already seen
219 // generic promo.
186 int carrier_deal_promo_pref = kNotificationCountPrefDefault; 220 int carrier_deal_promo_pref = kNotificationCountPrefDefault;
187 const MobileConfig::CarrierDeal* deal = NULL; 221 const MobileConfig::CarrierDeal* deal = NULL;
188 const MobileConfig::Carrier* carrier = GetCarrier(default_network); 222 const MobileConfig::Carrier* carrier = GetCarrier(default_network);
189 if (carrier) 223 if (carrier)
190 deal = GetCarrierDeal(carrier); 224 deal = GetCarrierDeal(carrier);
191 225
192 base::string16 message = 226 base::string16 message =
193 l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE); 227 l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE);
194 std::string info_url; 228 std::string info_url;
195 if (deal) { 229 if (deal) {
196 carrier_deal_promo_pref = GetCarrierDealPromoShown(); 230 carrier_deal_promo_pref = GetCarrierDealPromoShown();
197 const std::string locale = g_browser_process->GetApplicationLocale(); 231 const std::string locale = g_browser_process->GetApplicationLocale();
198 std::string deal_text = 232 std::string deal_text =
199 deal->GetLocalizedString(locale, "notification_text"); 233 deal->GetLocalizedString(locale, "notification_text");
200 message = base::UTF8ToUTF16(deal_text + "\n\n") + message; 234 message = base::UTF8ToUTF16(deal_text + "\n\n") + message;
201 info_url = deal->info_url(); 235 info_url = deal->info_url();
202 if (info_url.empty() && carrier) 236 if (info_url.empty() && carrier)
203 info_url = carrier->top_up_url(); 237 info_url = carrier->top_up_url();
204 } else if (!ShouldShow3gPromoNotification()) { 238 } else if (data_saver_prompt_shown || !ShouldShow3gPromoNotification()) {
205 check_for_promo_ = false; 239 check_for_promo_ = false;
206 return; 240 return;
207 } 241 }
208 242
209 int icon_id; 243 int icon_id;
210 if (default_network->network_technology() == shill::kNetworkTechnologyLte) 244 if (default_network->network_technology() == shill::kNetworkTechnologyLte)
211 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE; 245 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE;
212 else 246 else
213 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G; 247 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G;
214 const gfx::Image& icon = 248 const gfx::Image& icon =
215 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id); 249 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id);
216 250
217 message_center::MessageCenter::Get()->AddNotification( 251 message_center::MessageCenter::Get()->AddNotification(
218 message_center::Notification::CreateSystemNotification( 252 message_center::Notification::CreateSystemNotification(
219 kDataPromoNotificationId, base::string16() /* title */, message, icon, 253 kDataPromoNotificationId, base::string16() /* title */, message, icon,
220 ui::NetworkStateNotifier::kNotifierNetwork, 254 ui::NetworkStateNotifier::kNotifierNetwork,
221 base::Bind(&NotificationClicked, default_network->path(), info_url))); 255 base::Bind(&NotificationClicked, default_network->path(), info_url)));
222 256
223 check_for_promo_ = false; 257 check_for_promo_ = false;
224 SetShow3gPromoNotification(false); 258 SetShow3gPromoNotification(false);
225 if (carrier_deal_promo_pref != kNotificationCountPrefDefault) 259 if (carrier_deal_promo_pref != kNotificationCountPrefDefault)
226 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1); 260 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1);
227 } 261 }
228 262
263 bool DataPromoNotification::ShowDataSaverNotification(bool hotkey_debug) {
264 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
265 chromeos::switches::kEnableDataSaverPrompt))
266 return false;
267
268 int times_shown = GetDataSaverPromptsShown();
269 if (!hotkey_debug && times_shown >= kTimesToShowDataSaverPrompt)
270 return false;
271
272 Profile* profile = ProfileManager::GetActiveUserProfile();
273 if (extensions::ExtensionRegistry::Get(profile)
274 ->GetExtensionById(extension_misc::kDataSaverExtensionId,
275 extensions::ExtensionRegistry::EVERYTHING)) {
276 // If extension is installed, disable future prompts.
277 SetDataSaverPromptsShown(kTimesToShowDataSaverPrompt);
278 return false;
279 }
280
281 const NetworkState* default_network =
282 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
283
284 base::string16 title = l10n_util::GetStringUTF16(IDS_3G_DATASAVER_TITLE);
285 base::string16 message = l10n_util::GetStringUTF16(IDS_3G_DATASAVER_MESSAGE);
286 const gfx::Image& icon =
287 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
288 IDR_AURA_UBER_TRAY_NOTIFICATION_DATASAVER);
289
290 // Clears the cached notification; otherwise, future calls from this session
291 // won't display it.
292 if (hotkey_debug) {
293 message_center::MessageCenter::Get()->RemoveNotification(
294 kDataPromoNotificationId, false);
295 }
296
297 message_center::MessageCenter::Get()->AddNotification(
298 message_center::Notification::CreateSystemNotification(
299 kDataPromoNotificationId, title, message, icon,
300 ui::NetworkStateNotifier::kNotifierNetwork,
301 base::Bind(&NotificationClicked, default_network->path(),
302 kDataSaverExtensionUrl)));
303
304 // Hotkey resets times shown count.
305 SetDataSaverPromptsShown(hotkey_debug ? 0 : times_shown + 1);
306 return true;
307 }
308
229 } // namespace chromeos 309 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698