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

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 review, remove hotkey Created 5 years, 8 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 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
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
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
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 = 217 const NetworkState* default_network =
178 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); 218 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
179 if (!default_network || default_network->type() != shill::kTypeCellular) 219 // Do not show notifications to unauthenticated users, or when requesting a
180 return; 220 // network connection, or if no default_network.
181 // When requesting a network connection, do not show the notification. 221 if (!default_network || !LoginState::Get()->IsUserAuthenticated() ||
182 if (NetworkHandler::Get()->network_connection_handler()-> 222 NetworkHandler::Get()
183 HasPendingConnectRequest()) 223 ->network_connection_handler()
224 ->HasPendingConnectRequest())
stevenjb 2015/04/03 20:07:26 nit: I think that keeping these separate is a litt
Greg Levin 2015/04/03 22:17:33 Done.
184 return; 225 return;
185 226
227 if (!DataSaverSwitchDemoMode() &&
228 (notifications_shown_ || default_network->type() != shill::kTypeCellular))
229 return;
230
stevenjb 2015/04/03 20:07:26 nit: It looks like we can set |notifications_shown
Greg Levin 2015/04/03 22:17:33 Done.
231 bool data_saver_prompt_shown = ShowDataSaverNotification();
232
233 // Display a one-time notification on first use of Mobile Data connection or
234 // if there is a carrier deal defined show that even if user has already seen
235 // generic promo. Show deal regardless of |data_saver_prompt_shown|.
186 int carrier_deal_promo_pref = kNotificationCountPrefDefault; 236 int carrier_deal_promo_pref = kNotificationCountPrefDefault;
187 const MobileConfig::CarrierDeal* deal = NULL; 237 const MobileConfig::CarrierDeal* deal = NULL;
188 const MobileConfig::Carrier* carrier = GetCarrier(default_network); 238 const MobileConfig::Carrier* carrier = GetCarrier(default_network);
189 if (carrier) 239 if (carrier)
190 deal = GetCarrierDeal(carrier); 240 deal = GetCarrierDeal(carrier);
191 241
192 base::string16 message = 242 base::string16 message =
193 l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE); 243 l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE);
194 std::string info_url; 244 std::string info_url;
195 if (deal) { 245 if (deal) {
196 carrier_deal_promo_pref = GetCarrierDealPromoShown(); 246 carrier_deal_promo_pref = GetCarrierDealPromoShown();
197 const std::string locale = g_browser_process->GetApplicationLocale(); 247 const std::string locale = g_browser_process->GetApplicationLocale();
198 std::string deal_text = 248 std::string deal_text =
199 deal->GetLocalizedString(locale, "notification_text"); 249 deal->GetLocalizedString(locale, "notification_text");
200 message = base::UTF8ToUTF16(deal_text + "\n\n") + message; 250 message = base::UTF8ToUTF16(deal_text + "\n\n") + message;
201 info_url = deal->info_url(); 251 info_url = deal->info_url();
202 if (info_url.empty() && carrier) 252 if (info_url.empty() && carrier)
203 info_url = carrier->top_up_url(); 253 info_url = carrier->top_up_url();
204 } else if (!ShouldShow3gPromoNotification()) { 254 } else if (data_saver_prompt_shown || !ShouldShow3gPromoNotification()) {
205 check_for_promo_ = false; 255 notifications_shown_ = true;
206 return; 256 return;
207 } 257 }
208 258
209 int icon_id; 259 int icon_id;
210 if (default_network->network_technology() == shill::kNetworkTechnologyLte) 260 if (default_network->network_technology() == shill::kNetworkTechnologyLte)
211 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE; 261 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE;
212 else 262 else
213 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G; 263 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G;
214 const gfx::Image& icon = 264 const gfx::Image& icon =
215 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id); 265 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id);
216 266
217 message_center::MessageCenter::Get()->AddNotification( 267 message_center::MessageCenter::Get()->AddNotification(
218 message_center::Notification::CreateSystemNotification( 268 message_center::Notification::CreateSystemNotification(
219 kDataPromoNotificationId, base::string16() /* title */, message, icon, 269 kDataPromoNotificationId, base::string16() /* title */, message, icon,
220 ui::NetworkStateNotifier::kNotifierNetwork, 270 ui::NetworkStateNotifier::kNotifierNetwork,
221 base::Bind(&NotificationClicked, default_network->path(), info_url))); 271 base::Bind(&NotificationClicked, default_network->path(), info_url)));
222 272
223 check_for_promo_ = false; 273 notifications_shown_ = true;
224 SetShow3gPromoNotification(false); 274 SetShow3gPromoNotification(false);
225 if (carrier_deal_promo_pref != kNotificationCountPrefDefault) 275 if (carrier_deal_promo_pref != kNotificationCountPrefDefault)
226 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1); 276 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1);
227 } 277 }
228 278
279 bool DataPromoNotification::ShowDataSaverNotification() {
280 if (!DataSaverSwitchEnabled())
281 return false;
282
283 if (message_center::MessageCenter::Get()->FindVisibleNotificationById(
284 kDataSaverNotificationId)) // already showing.
285 return true;
286
287 int times_shown = GetDataSaverPromptsShown();
288 if (!DataSaverSwitchDemoMode() && times_shown >= kTimesToShowDataSaverPrompt)
289 return false;
290
291 if (extensions::ExtensionRegistry::Get(ProfileManager::GetActiveUserProfile())
292 ->GetExtensionById(extension_misc::kDataSaverExtensionId,
293 extensions::ExtensionRegistry::EVERYTHING)) {
294 // If extension is installed, disable future prompts.
295 SetDataSaverPromptsShown(kTimesToShowDataSaverPrompt);
296 return false;
297 }
298
299 base::string16 title = l10n_util::GetStringUTF16(IDS_3G_DATASAVER_TITLE);
300 base::string16 message = l10n_util::GetStringUTF16(IDS_3G_DATASAVER_MESSAGE);
301 const gfx::Image& icon =
302 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
303 IDR_AURA_UBER_TRAY_NOTIFICATION_DATASAVER);
304 const std::string path =
305 NetworkHandler::Get()->network_state_handler()->DefaultNetwork()->path();
306
307 message_center::MessageCenter::Get()->AddNotification(
308 message_center::Notification::CreateSystemNotification(
309 kDataSaverNotificationId, title, message, icon,
310 ui::NetworkStateNotifier::kNotifierNetwork,
311 base::Bind(&NotificationClicked, path, kDataSaverExtensionUrl)));
stevenjb 2015/04/03 20:07:26 Is the intended behavior to show the settings for
Greg Levin 2015/04/03 22:17:33 You mean from |path|? The network settings were n
312
313 if (DataSaverSwitchDemoMode()) {
314 SetDataSaverPromptsShown(0); // demo mode resets times shown counts.
stevenjb 2015/04/03 20:07:26 If we are resetting the times shown here, do we al
Greg Levin 2015/04/03 22:17:33 This is done to accommodate the case where you're
stevenjb 2015/04/03 22:27:53 I see. If we have shown the notification and then
315 SetShow3gPromoNotification(true);
316 } else {
317 SetDataSaverPromptsShown(times_shown + 1);
318 }
319
320 return true;
321 }
322
229 } // namespace chromeos 323 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698