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

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

Issue 23075012: Update NetworkStateNotifier to use message_center. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Track deafult_network and reset did_show_out_of_credits on any change Created 7 years, 4 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 | Annotate | Revision Log
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/shell.h" 7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h" 8 #include "ash/shell_window_ids.h"
9 #include "ash/system/chromeos/network/network_connect.h" 9 #include "ash/system/chromeos/network/network_connect.h"
10 #include "ash/system/chromeos/network/network_observer.h"
11 #include "ash/system/tray/system_tray.h" 10 #include "ash/system/tray/system_tray.h"
12 #include "ash/system/tray/system_tray_notifier.h" 11 #include "ash/system/tray/system_tray_notifier.h"
13 #include "base/prefs/pref_registry_simple.h" 12 #include "base/prefs/pref_registry_simple.h"
14 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
15 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/browser_process.h" 15 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chromeos/login/helper.h" 16 #include "chrome/browser/chromeos/login/helper.h"
18 #include "chrome/browser/chromeos/mobile_config.h" 17 #include "chrome/browser/chromeos/mobile_config.h"
19 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/profiles/profile_manager.h" 19 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/browser/ui/browser.h" 20 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/browser_finder.h" 21 #include "chrome/browser/ui/browser_finder.h"
23 #include "chrome/browser/ui/browser_list.h" 22 #include "chrome/browser/ui/browser_list.h"
24 #include "chrome/browser/ui/singleton_tabs.h" 23 #include "chrome/browser/ui/singleton_tabs.h"
25 #include "chrome/common/pref_names.h" 24 #include "chrome/common/pref_names.h"
26 #include "chromeos/login/login_state.h" 25 #include "chromeos/login/login_state.h"
27 #include "chromeos/network/device_state.h" 26 #include "chromeos/network/device_state.h"
28 #include "chromeos/network/network_connection_handler.h" 27 #include "chromeos/network/network_connection_handler.h"
29 #include "chromeos/network/network_event_log.h" 28 #include "chromeos/network/network_event_log.h"
30 #include "chromeos/network/network_state.h" 29 #include "chromeos/network/network_state.h"
31 #include "chromeos/network/network_state_handler.h" 30 #include "chromeos/network/network_state_handler.h"
32 #include "grit/ash_strings.h" 31 #include "grit/ash_resources.h"
33 #include "grit/generated_resources.h" 32 #include "grit/generated_resources.h"
34 #include "grit/theme_resources.h" 33 #include "grit/theme_resources.h"
35 #include "third_party/cros_system_api/dbus/service_constants.h" 34 #include "third_party/cros_system_api/dbus/service_constants.h"
36 #include "ui/base/l10n/l10n_util.h" 35 #include "ui/base/l10n/l10n_util.h"
37 #include "ui/base/resource/resource_bundle.h" 36 #include "ui/base/resource/resource_bundle.h"
37 #include "ui/message_center/message_center.h"
38 #include "ui/message_center/notification.h"
38 #include "ui/views/view.h" 39 #include "ui/views/view.h"
39 #include "ui/views/widget/widget.h" 40 #include "ui/views/widget/widget.h"
40 41
41 namespace chromeos { 42 namespace chromeos {
42 43
43 namespace { 44 namespace {
44 45
45 // Time in milliseconds to delay showing of promo 46 // Time in milliseconds to delay showing of promo
46 // notification when Chrome window is not on screen. 47 // notification when Chrome window is not on screen.
47 const int kPromoShowDelayMs = 10000; 48 const int kPromoShowDelayMs = 10000;
(...skipping 15 matching lines...) Expand all
63 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 64 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
64 PrefService* prefs = profile->GetPrefs(); 65 PrefService* prefs = profile->GetPrefs();
65 prefs->SetBoolean(pref_name, value); 66 prefs->SetBoolean(pref_name, value);
66 } 67 }
67 68
68 void SetIntegerLocalPref(const char* pref_name, int value) { 69 void SetIntegerLocalPref(const char* pref_name, int value) {
69 PrefService* prefs = g_browser_process->local_state(); 70 PrefService* prefs = g_browser_process->local_state();
70 prefs->SetInteger(pref_name, value); 71 prefs->SetInteger(pref_name, value);
71 } 72 }
72 73
73 // Returns prefs::kShow3gPromoNotification or false 74 // Returns prefs::kShow3gPromoNotification or false if no active browser.
74 // if there's no active browser.
75 bool ShouldShow3gPromoNotification() { 75 bool ShouldShow3gPromoNotification() {
76 return GetBooleanPref(prefs::kShow3gPromoNotification); 76 return GetBooleanPref(prefs::kShow3gPromoNotification);
77 } 77 }
78 78
79 void SetShow3gPromoNotification(bool value) { 79 void SetShow3gPromoNotification(bool value) {
80 SetBooleanPref(prefs::kShow3gPromoNotification, value); 80 SetBooleanPref(prefs::kShow3gPromoNotification, value);
81 } 81 }
82 82
83 // Returns prefs::kCarrierDealPromoShown which is number of times 83 // Returns prefs::kCarrierDealPromoShown which is number of times
84 // carrier deal notification has been shown to users on this machine. 84 // carrier deal notification has been shown to users on this machine.
85 int GetCarrierDealPromoShown() { 85 int GetCarrierDealPromoShown() {
86 return GetIntegerLocalPref(prefs::kCarrierDealPromoShown); 86 return GetIntegerLocalPref(prefs::kCarrierDealPromoShown);
87 } 87 }
88 88
89 void SetCarrierDealPromoShown(int value) { 89 void SetCarrierDealPromoShown(int value) {
90 SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value); 90 SetIntegerLocalPref(prefs::kCarrierDealPromoShown, value);
91 } 91 }
92 92
93 const chromeos::MobileConfig::Carrier* GetCarrier( 93 const chromeos::MobileConfig::Carrier* GetCarrier(
94 const NetworkState* cellular) { 94 const NetworkState* cellular) {
95 const DeviceState* device = NetworkHandler::Get()->network_state_handler()-> 95 const DeviceState* device = NetworkHandler::Get()->network_state_handler()->
96 GetDeviceState(cellular->device_path()); 96 GetDeviceState(cellular->device_path());
97 std::string carrier_id = device->home_provider_id(); 97 std::string carrier_id = device ? device->home_provider_id() : "";
98 if (carrier_id.empty()) { 98 if (carrier_id.empty()) {
99 NET_LOG_ERROR("Empty carrier ID with a cellular device", device->path()); 99 NET_LOG_ERROR("Empty carrier ID for cellular network",
100 device ? device->path(): "No device");
100 return NULL; 101 return NULL;
101 } 102 }
102 103
103 chromeos::MobileConfig* config = chromeos::MobileConfig::GetInstance(); 104 chromeos::MobileConfig* config = chromeos::MobileConfig::GetInstance();
104 if (!config->IsReady()) 105 if (!config->IsReady())
105 return NULL; 106 return NULL;
106 107
107 return config->GetCarrier(carrier_id); 108 return config->GetCarrier(carrier_id);
108 } 109 }
109 110
110 const chromeos::MobileConfig::CarrierDeal* GetCarrierDeal( 111 const chromeos::MobileConfig::CarrierDeal* GetCarrierDeal(
111 const chromeos::MobileConfig::Carrier* carrier) { 112 const chromeos::MobileConfig::Carrier* carrier) {
112 const chromeos::MobileConfig::CarrierDeal* deal = carrier->GetDefaultDeal(); 113 const chromeos::MobileConfig::CarrierDeal* deal = carrier->GetDefaultDeal();
113 if (deal) { 114 if (deal) {
114 // Check deal for validity. 115 // Check deal for validity.
115 int carrier_deal_promo_pref = GetCarrierDealPromoShown(); 116 int carrier_deal_promo_pref = GetCarrierDealPromoShown();
116 if (carrier_deal_promo_pref >= deal->notification_count()) 117 if (carrier_deal_promo_pref >= deal->notification_count())
117 return NULL; 118 return NULL;
118 const std::string locale = g_browser_process->GetApplicationLocale(); 119 const std::string locale = g_browser_process->GetApplicationLocale();
119 std::string deal_text = deal->GetLocalizedString(locale, 120 std::string deal_text = deal->GetLocalizedString(locale,
120 "notification_text"); 121 "notification_text");
121 NET_LOG_DEBUG("Carrier Deal Found", deal_text); 122 NET_LOG_DEBUG("Carrier Deal Found", deal_text);
122 if (deal_text.empty()) 123 if (deal_text.empty())
123 return NULL; 124 return NULL;
124 } 125 }
125 return deal; 126 return deal;
126 } 127 }
127 128
128 ash::NetworkObserver::NetworkType NetworkTypeForNetwork( 129 void NotificationClicked(const std::string& service_path,
129 const NetworkState* network) { 130 const std::string& info_url) {
130 DCHECK(network); 131 if (info_url.empty())
131 const std::string& technology = network->network_technology(); 132 ash::network_connect::ShowNetworkSettings(service_path);
132 return (technology == flimflam::kNetworkTechnologyLte || 133
133 technology == flimflam::kNetworkTechnologyLteAdvanced) 134 Browser* browser = chrome::FindOrCreateTabbedBrowser(
134 ? ash::NetworkObserver::NETWORK_CELLULAR_LTE 135 ProfileManager::GetDefaultProfileOrOffTheRecord(),
135 : ash::NetworkObserver::NETWORK_CELLULAR; 136 chrome::HOST_DESKTOP_TYPE_ASH);
137 if (!browser)
138 return;
139 chrome::ShowSingletonTab(browser, GURL(info_url));
136 } 140 }
137 141
138 } // namespace 142 } // namespace
139 143
140 //////////////////////////////////////////////////////////////////////////////// 144 ////////////////////////////////////////////////////////////////////////////////
141 // DataPromoNotification 145 // DataPromoNotification
142 146
143 DataPromoNotification::DataPromoNotification() 147 DataPromoNotification::DataPromoNotification()
144 : check_for_promo_(true), 148 : check_for_promo_(true),
145 cellular_activating_(false),
146 weak_ptr_factory_(this) { 149 weak_ptr_factory_(this) {
147 UpdateCellularActivating();
148 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); 150 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE);
149 } 151 }
150 152
151 DataPromoNotification::~DataPromoNotification() { 153 DataPromoNotification::~DataPromoNotification() {
152 if (NetworkHandler::IsInitialized()) { 154 if (NetworkHandler::IsInitialized()) {
153 NetworkHandler::Get()->network_state_handler()->RemoveObserver( 155 NetworkHandler::Get()->network_state_handler()->RemoveObserver(
154 this, FROM_HERE); 156 this, FROM_HERE);
155 } 157 }
156 CloseNotification();
157 } 158 }
158 159
159 void DataPromoNotification::RegisterPrefs(PrefRegistrySimple* registry) { 160 void DataPromoNotification::RegisterPrefs(PrefRegistrySimple* registry) {
160 // Carrier deal notification shown count defaults to 0. 161 // Carrier deal notification shown count defaults to 0.
161 registry->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0); 162 registry->RegisterIntegerPref(prefs::kCarrierDealPromoShown, 0);
162 } 163 }
163 164
164 void DataPromoNotification::NetworkPropertiesUpdated( 165 void DataPromoNotification::NetworkPropertiesUpdated(
165 const NetworkState* network) { 166 const NetworkState* network) {
166 if (!network || network->type() != flimflam::kTypeCellular) 167 if (!network || network->type() != flimflam::kTypeCellular)
167 return; 168 return;
168 ShowOptionalMobileDataPromoNotification(); 169 ShowOptionalMobileDataPromoNotification();
169 UpdateCellularActivating();
170 } 170 }
171 171
172 void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) { 172 void DataPromoNotification::DefaultNetworkChanged(const NetworkState* network) {
173 // Call NetworkPropertiesUpdated in case the Cellular network became the 173 // Call NetworkPropertiesUpdated in case the Cellular network became the
174 // default network. 174 // default network.
175 NetworkPropertiesUpdated(network); 175 NetworkPropertiesUpdated(network);
176 } 176 }
177 177
178 void DataPromoNotification::NotificationLinkClicked(
179 ash::NetworkObserver::MessageType message_type,
180 size_t link_index) {
181 const NetworkState* cellular =
182 NetworkHandler::Get()->network_state_handler()->
183 FirstNetworkByType(flimflam::kTypeCellular);
184 std::string service_path = cellular ? cellular->path() : "";
185 if (message_type == ash::NetworkObserver::ERROR_OUT_OF_CREDITS) {
186 ash::network_connect::ShowNetworkSettings(service_path);
187 ash::Shell::GetInstance()->system_tray_notifier()->
188 NotifyClearNetworkMessage(message_type);
189 }
190 if (message_type != ash::NetworkObserver::MESSAGE_DATA_PROMO)
191 return;
192
193 // If we have a deal info URL defined that means that there are
194 // two links in the bubble. Let the user close it manually then giving the
195 // ability to navigate to the second link.
196 if (deal_info_url_.empty())
197 CloseNotification();
198
199 std::string deal_url_to_open;
200 if (link_index == 0) {
201 if (!deal_topup_url_.empty()) {
202 deal_url_to_open = deal_topup_url_;
203 } else {
204 ash::network_connect::ShowNetworkSettings(service_path);
205 return;
206 }
207 } else if (link_index == 1) {
208 deal_url_to_open = deal_info_url_;
209 }
210
211 if (!deal_url_to_open.empty()) {
212 Browser* browser = chrome::FindOrCreateTabbedBrowser(
213 ProfileManager::GetDefaultProfileOrOffTheRecord(),
214 chrome::HOST_DESKTOP_TYPE_ASH);
215 if (!browser)
216 return;
217 chrome::ShowSingletonTab(browser, GURL(deal_url_to_open));
218 }
219 }
220
221 void DataPromoNotification::UpdateCellularActivating() {
222 // We only care about the first (default) cellular network.
223 const NetworkState* cellular =
224 NetworkHandler::Get()->network_state_handler()->
225 FirstNetworkByType(flimflam::kTypeCellular);
226 if (!cellular)
227 return;
228
229 std::string activation_state = cellular->activation_state();
230 if (activation_state == flimflam::kActivationStateActivating) {
231 cellular_activating_ = true;
232 } else if (cellular_activating_ &&
233 activation_state == flimflam::kActivationStateActivated) {
234 cellular_activating_ = false;
235 ash::Shell::GetInstance()->system_tray_notifier()->
236 NotifySetNetworkMessage(
237 NULL,
238 ash::NetworkObserver::MESSAGE_DATA_PROMO,
239 NetworkTypeForNetwork(cellular),
240 l10n_util::GetStringUTF16(IDS_NETWORK_CELLULAR_ACTIVATED_TITLE),
241 l10n_util::GetStringFUTF16(IDS_NETWORK_CELLULAR_ACTIVATED,
242 UTF8ToUTF16((cellular->name()))),
243 std::vector<string16>());
244 }
245 }
246
247 void DataPromoNotification::ShowOptionalMobileDataPromoNotification() { 178 void DataPromoNotification::ShowOptionalMobileDataPromoNotification() {
248 // Display a one-time notification for authenticated users on first use 179 // Display a one-time notification for authenticated users on first use
249 // of Mobile Data connection or if there is a carrier deal defined 180 // of Mobile Data connection or if there is a carrier deal defined
250 // show that even if user has already seen generic promo. 181 // show that even if user has already seen generic promo.
251 if (!check_for_promo_ || !LoginState::Get()->IsUserAuthenticated()) 182 if (!check_for_promo_ || !LoginState::Get()->IsUserAuthenticated())
252 return; 183 return;
253 const NetworkState* default_network = 184 const NetworkState* default_network =
254 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); 185 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
255 if (!default_network || default_network->type() != flimflam::kTypeCellular) 186 if (!default_network || default_network->type() != flimflam::kTypeCellular)
256 return; 187 return;
257 // When requesting a network connection, do not show the notification. 188 // When requesting a network connection, do not show the notification.
258 if (NetworkHandler::Get()->network_connection_handler()-> 189 if (NetworkHandler::Get()->network_connection_handler()->
259 HasPendingConnectRequest()) 190 HasPendingConnectRequest())
260 return; 191 return;
261 192
262 std::string deal_text;
263 int carrier_deal_promo_pref = kNotificationCountPrefDefault; 193 int carrier_deal_promo_pref = kNotificationCountPrefDefault;
264 const MobileConfig::CarrierDeal* deal = NULL; 194 const MobileConfig::CarrierDeal* deal = NULL;
265 const MobileConfig::Carrier* carrier = GetCarrier(default_network); 195 const MobileConfig::Carrier* carrier = GetCarrier(default_network);
266 if (carrier) 196 if (carrier)
267 deal = GetCarrierDeal(carrier); 197 deal = GetCarrierDeal(carrier);
268 deal_info_url_.clear(); 198
269 deal_topup_url_.clear(); 199 string16 message = l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE);
200 std::string info_url;
270 if (deal) { 201 if (deal) {
271 carrier_deal_promo_pref = GetCarrierDealPromoShown(); 202 carrier_deal_promo_pref = GetCarrierDealPromoShown();
272 const std::string locale = g_browser_process->GetApplicationLocale(); 203 const std::string locale = g_browser_process->GetApplicationLocale();
273 deal_text = deal->GetLocalizedString(locale, "notification_text"); 204 std::string deal_text =
274 deal_info_url_ = deal->info_url(); 205 deal->GetLocalizedString(locale, "notification_text");
275 deal_topup_url_ = carrier->top_up_url(); 206 message = UTF8ToUTF16(deal_text + "\n\n") + message;
207 info_url = deal->info_url();
208 if (info_url.empty() && carrier)
209 info_url = carrier->top_up_url();
276 } else if (!ShouldShow3gPromoNotification()) { 210 } else if (!ShouldShow3gPromoNotification()) {
277 check_for_promo_ = false; 211 check_for_promo_ = false;
278 return; 212 return;
279 } 213 }
280 214
281 string16 message = l10n_util::GetStringUTF16(IDS_3G_NOTIFICATION_MESSAGE); 215 int icon_id;
282 if (!deal_text.empty()) 216 if (default_network->network_technology() == flimflam::kNetworkTechnologyLte)
283 message = UTF8ToUTF16(deal_text + "\n\n") + message; 217 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE;
218 else
219 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G;
220 const gfx::Image& icon =
221 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id);
284 222
285 // Use deal URL if it's defined or general "Network Settings" URL. 223 message_center::MessageCenter::Get()->AddNotification(
286 int link_message_id; 224 message_center::Notification::CreateSystemNotification(
287 if (deal_topup_url_.empty()) 225 ash::network_connect::kNetworkActivateNotificationId,
288 link_message_id = IDS_OFFLINE_NETWORK_SETTINGS; 226 base::string16() /* title */,
289 else 227 message,
290 link_message_id = IDS_STATUSBAR_NETWORK_VIEW_ACCOUNT; 228 icon,
229 base::Bind(&NotificationClicked,
230 default_network->path(), info_url)));
291 231
292 ash::NetworkObserver::NetworkType type =
293 NetworkTypeForNetwork(default_network);
294
295 std::vector<string16> links;
296 links.push_back(l10n_util::GetStringUTF16(link_message_id));
297 if (!deal_info_url_.empty())
298 links.push_back(l10n_util::GetStringUTF16(IDS_LEARN_MORE));
299 ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage(
300 this, ash::NetworkObserver::MESSAGE_DATA_PROMO,
301 type, string16(), message, links);
302 check_for_promo_ = false; 232 check_for_promo_ = false;
303 SetShow3gPromoNotification(false); 233 SetShow3gPromoNotification(false);
304 if (carrier_deal_promo_pref != kNotificationCountPrefDefault) 234 if (carrier_deal_promo_pref != kNotificationCountPrefDefault)
305 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1); 235 SetCarrierDealPromoShown(carrier_deal_promo_pref + 1);
306 } 236 }
307 237
308 void DataPromoNotification::CloseNotification() {
309 ash::Shell::GetInstance()->system_tray_notifier()->NotifyClearNetworkMessage(
310 ash::NetworkObserver::MESSAGE_DATA_PROMO);
311 }
312
313 } // namespace chromeos 238 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/status/data_promo_notification.h ('k') | chrome/browser/chromeos/system/ash_system_tray_delegate.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698