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
3 // found in the LICENSE file.
5 #include "ui/chromeos/network/network_state_notifier.h"
8 #include "base/location.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chromeos/network/network_configuration_handler.h"
13 #include "chromeos/network/network_connection_handler.h"
14 #include "chromeos/network/network_event_log.h"
15 #include "chromeos/network/network_state.h"
16 #include "chromeos/network/network_state_handler.h"
17 #include "chromeos/network/shill_property_util.h"
18 #include "third_party/cros_system_api/dbus/service_constants.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/chromeos/network/network_connect.h"
22 #include "ui/chromeos/resources/grit/ui_chromeos_resources.h"
23 #include "ui/chromeos/strings/grit/ui_chromeos_strings.h"
24 #include "ui/message_center/message_center.h"
25 #include "ui/message_center/notification.h"
27 using chromeos::NetworkConnectionHandler
;
28 using chromeos::NetworkHandler
;
29 using chromeos::NetworkState
;
30 using chromeos::NetworkStateHandler
;
31 using chromeos::NetworkTypePattern
;
35 const int kMinTimeBetweenOutOfCreditsNotifySeconds
= 10 * 60;
37 // Ignore in-progress error.
38 bool ShillErrorIsIgnored(const std::string
& shill_error
) {
39 if (shill_error
== shill::kErrorResultInProgress
)
44 // Error messages based on |error_name|, not network_state->error().
45 base::string16
GetConnectErrorString(const std::string
& error_name
) {
46 if (error_name
== NetworkConnectionHandler::kErrorNotFound
)
47 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_CONNECT_FAILED
);
48 if (error_name
== NetworkConnectionHandler::kErrorConfigureFailed
) {
49 return l10n_util::GetStringUTF16(
50 IDS_CHROMEOS_NETWORK_ERROR_CONFIGURE_FAILED
);
52 if (error_name
== NetworkConnectionHandler::kErrorCertLoadTimeout
) {
53 return l10n_util::GetStringUTF16(
54 IDS_CHROMEOS_NETWORK_ERROR_CERTIFICATES_NOT_LOADED
);
56 if (error_name
== ui::NetworkConnect::kErrorActivateFailed
) {
57 return l10n_util::GetStringUTF16(
58 IDS_CHROMEOS_NETWORK_ERROR_ACTIVATION_FAILED
);
60 return base::string16();
63 int GetErrorNotificationIconId(const std::string
& network_type
) {
64 if (network_type
== shill::kTypeVPN
)
65 return IDR_AURA_UBER_TRAY_NETWORK_VPN
;
66 if (network_type
== shill::kTypeCellular
)
67 return IDR_AURA_UBER_TRAY_NETWORK_FAILED_CELLULAR
;
68 return IDR_AURA_UBER_TRAY_NETWORK_FAILED
;
71 void ShowErrorNotification(const std::string
& notification_id
,
72 const std::string
& network_type
,
73 const base::string16
& title
,
74 const base::string16
& message
,
75 const base::Closure
& callback
) {
76 const gfx::Image
& icon
=
77 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
78 GetErrorNotificationIconId(network_type
));
79 message_center::MessageCenter::Get()->AddNotification(
80 message_center::Notification::CreateSystemNotification(
81 notification_id
, title
, message
, icon
,
82 ui::NetworkStateNotifier::kNotifierNetworkError
, callback
));
89 const char NetworkStateNotifier::kNotifierNetwork
[] = "ui.chromeos.network";
90 const char NetworkStateNotifier::kNotifierNetworkError
[] =
91 "ui.chromeos.network.error";
93 const char NetworkStateNotifier::kNetworkConnectNotificationId
[] =
94 "chrome://settings/internet/connect";
95 const char NetworkStateNotifier::kNetworkActivateNotificationId
[] =
96 "chrome://settings/internet/activate";
97 const char NetworkStateNotifier::kNetworkOutOfCreditsNotificationId
[] =
98 "chrome://settings/internet/out-of-credits";
100 NetworkStateNotifier::NetworkStateNotifier(NetworkConnect
* network_connect
)
101 : network_connect_(network_connect
),
102 did_show_out_of_credits_(false),
103 need_vpn_disconnection_notify_(false),
104 weak_ptr_factory_(this) {
105 if (!NetworkHandler::IsInitialized())
107 NetworkStateHandler
* handler
= NetworkHandler::Get()->network_state_handler();
108 handler
->AddObserver(this, FROM_HERE
);
109 UpdateDefaultNetwork(handler
->DefaultNetwork());
110 NetworkHandler::Get()->network_connection_handler()->AddObserver(this);
113 NetworkStateNotifier::~NetworkStateNotifier() {
114 if (!NetworkHandler::IsInitialized())
116 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
118 NetworkHandler::Get()->network_connection_handler()->RemoveObserver(this);
121 void NetworkStateNotifier::ConnectToNetworkRequested(
122 const std::string
& service_path
) {
123 RemoveConnectNotification();
126 void NetworkStateNotifier::ConnectSucceeded(const std::string
& service_path
) {
127 RemoveConnectNotification();
130 void NetworkStateNotifier::ConnectFailed(const std::string
& service_path
,
131 const std::string
& error_name
) {
132 // Only show a notification for certain errors. Other failures are expected
133 // to be handled by the UI that initiated the connect request.
134 // Note: kErrorConnectFailed may also cause the configure dialog to be
135 // displayed, but we rely on the notification system to show additional
136 // details if available.
137 if (error_name
!= NetworkConnectionHandler::kErrorConnectFailed
&&
138 error_name
!= NetworkConnectionHandler::kErrorNotFound
&&
139 error_name
!= NetworkConnectionHandler::kErrorConfigureFailed
&&
140 error_name
!= NetworkConnectionHandler::kErrorCertLoadTimeout
) {
143 ShowNetworkConnectError(error_name
, service_path
);
146 void NetworkStateNotifier::DisconnectRequested(
147 const std::string
& service_path
) {
148 const NetworkState
* network
=
149 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
151 if (network
&& network
->type() == shill::kTypeVPN
)
152 need_vpn_disconnection_notify_
= false;
155 void NetworkStateNotifier::DefaultNetworkChanged(const NetworkState
* network
) {
156 if (!UpdateDefaultNetwork(network
))
158 // If the default network changes to another network, allow the out of
159 // credits notification to be shown again. A delay prevents the notification
160 // from being shown too frequently (see below).
162 did_show_out_of_credits_
= false;
165 void NetworkStateNotifier::NetworkConnectionStateChanged(
166 const NetworkState
* network
) {
167 if (network
->type() == shill::kTypeVPN
)
168 UpdateVpnConnectionState(network
);
171 void NetworkStateNotifier::NetworkPropertiesUpdated(
172 const NetworkState
* network
) {
173 if (network
->type() != shill::kTypeCellular
)
175 UpdateCellularOutOfCredits(network
);
176 UpdateCellularActivating(network
);
179 bool NetworkStateNotifier::UpdateDefaultNetwork(const NetworkState
* network
) {
180 std::string default_network_path
;
182 default_network_path
= network
->path();
183 if (default_network_path
!= last_default_network_
) {
184 last_default_network_
= default_network_path
;
190 void NetworkStateNotifier::UpdateVpnConnectionState(const NetworkState
* vpn
) {
191 if (!vpn
->IsConnectedState() && need_vpn_disconnection_notify_
)
192 ShowVpnDisconnectedNotification(vpn
);
193 need_vpn_disconnection_notify_
= vpn
->IsConnectedState();
196 void NetworkStateNotifier::UpdateCellularOutOfCredits(
197 const NetworkState
* cellular
) {
198 // Only display a notification if we are out of credits and have not already
199 // shown a notification (or have since connected to another network type).
200 if (!cellular
->cellular_out_of_credits() || did_show_out_of_credits_
)
203 // Only display a notification if not connected, connecting, or waiting to
204 // connect to another network.
205 NetworkStateHandler
* handler
= NetworkHandler::Get()->network_state_handler();
206 const NetworkState
* default_network
= handler
->DefaultNetwork();
207 if (default_network
&& default_network
!= cellular
)
209 if (handler
->ConnectingNetworkByType(NetworkTypePattern::NonVirtual()) ||
210 NetworkHandler::Get()
211 ->network_connection_handler()
212 ->HasPendingConnectRequest())
215 did_show_out_of_credits_
= true;
216 base::TimeDelta dtime
= base::Time::Now() - out_of_credits_notify_time_
;
217 if (dtime
.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds
) {
218 out_of_credits_notify_time_
= base::Time::Now();
219 base::string16 error_msg
= l10n_util::GetStringFUTF16(
220 IDS_NETWORK_OUT_OF_CREDITS_BODY
, base::UTF8ToUTF16(cellular
->name()));
221 ShowErrorNotification(
222 kNetworkOutOfCreditsNotificationId
, cellular
->type(),
223 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_CREDITS_TITLE
), error_msg
,
224 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath
,
225 weak_ptr_factory_
.GetWeakPtr(), cellular
->path()));
229 void NetworkStateNotifier::UpdateCellularActivating(
230 const NetworkState
* cellular
) {
231 // Keep track of any activating cellular network.
232 std::string activation_state
= cellular
->activation_state();
233 if (activation_state
== shill::kActivationStateActivating
) {
234 cellular_activating_
.insert(cellular
->path());
237 // Only display a notification if this network was activating and is now
239 if (!cellular_activating_
.count(cellular
->path()) ||
240 activation_state
!= shill::kActivationStateActivated
)
243 cellular_activating_
.erase(cellular
->path());
245 if (cellular
->network_technology() == shill::kNetworkTechnologyLte
)
246 icon_id
= IDR_AURA_UBER_TRAY_NOTIFICATION_LTE
;
248 icon_id
= IDR_AURA_UBER_TRAY_NOTIFICATION_3G
;
249 const gfx::Image
& icon
=
250 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id
);
251 message_center::MessageCenter::Get()->AddNotification(
252 message_center::Notification::CreateSystemNotification(
253 kNetworkActivateNotificationId
,
254 l10n_util::GetStringUTF16(IDS_NETWORK_CELLULAR_ACTIVATED_TITLE
),
255 l10n_util::GetStringFUTF16(IDS_NETWORK_CELLULAR_ACTIVATED
,
256 base::UTF8ToUTF16((cellular
->name()))),
257 icon
, kNotifierNetwork
,
258 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath
,
259 weak_ptr_factory_
.GetWeakPtr(), cellular
->path())));
262 void NetworkStateNotifier::ShowNetworkConnectError(
263 const std::string
& error_name
,
264 const std::string
& service_path
) {
265 if (service_path
.empty()) {
266 base::DictionaryValue shill_properties
;
267 ShowConnectErrorNotification(error_name
, service_path
, shill_properties
);
270 // Get the up-to-date properties for the network and display the error.
271 NetworkHandler::Get()->network_configuration_handler()->GetShillProperties(
273 base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesSucceeded
,
274 weak_ptr_factory_
.GetWeakPtr(), error_name
),
275 base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesFailed
,
276 weak_ptr_factory_
.GetWeakPtr(), error_name
, service_path
));
279 void NetworkStateNotifier::ShowMobileActivationError(
280 const std::string
& service_path
) {
281 const NetworkState
* cellular
=
282 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
284 if (!cellular
|| cellular
->type() != shill::kTypeCellular
) {
285 NET_LOG_ERROR("ShowMobileActivationError without Cellular network",
289 message_center::MessageCenter::Get()->AddNotification(
290 message_center::Notification::CreateSystemNotification(
291 kNetworkActivateNotificationId
,
292 l10n_util::GetStringUTF16(IDS_NETWORK_ACTIVATION_ERROR_TITLE
),
293 l10n_util::GetStringFUTF16(IDS_NETWORK_ACTIVATION_NEEDS_CONNECTION
,
294 base::UTF8ToUTF16(cellular
->name())),
295 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
296 IDR_AURA_UBER_TRAY_NETWORK_FAILED_CELLULAR
),
297 kNotifierNetworkError
,
298 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath
,
299 weak_ptr_factory_
.GetWeakPtr(), service_path
)));
302 void NetworkStateNotifier::RemoveConnectNotification() {
303 message_center::MessageCenter
* message_center
=
304 message_center::MessageCenter::Get();
305 if (message_center
) {
306 message_center
->RemoveNotification(kNetworkConnectNotificationId
,
307 false /* not by user */);
311 void NetworkStateNotifier::ConnectErrorPropertiesSucceeded(
312 const std::string
& error_name
,
313 const std::string
& service_path
,
314 const base::DictionaryValue
& shill_properties
) {
316 shill_properties
.GetStringWithoutPathExpansion(shill::kStateProperty
, &state
);
317 if (chromeos::NetworkState::StateIsConnected(state
) ||
318 chromeos::NetworkState::StateIsConnecting(state
)) {
319 // Network is no longer in an error state. This can happen if an
320 // unexpected idle state transition occurs, see crbug.com/333955.
323 ShowConnectErrorNotification(error_name
, service_path
, shill_properties
);
326 void NetworkStateNotifier::ConnectErrorPropertiesFailed(
327 const std::string
& error_name
,
328 const std::string
& service_path
,
329 const std::string
& shill_connect_error
,
330 scoped_ptr
<base::DictionaryValue
> shill_error_data
) {
331 base::DictionaryValue shill_properties
;
332 ShowConnectErrorNotification(error_name
, service_path
, shill_properties
);
335 void NetworkStateNotifier::ShowConnectErrorNotification(
336 const std::string
& error_name
,
337 const std::string
& service_path
,
338 const base::DictionaryValue
& shill_properties
) {
339 base::string16 error
= GetConnectErrorString(error_name
);
341 std::string shill_error
;
342 shill_properties
.GetStringWithoutPathExpansion(shill::kErrorProperty
,
344 if (!chromeos::NetworkState::ErrorIsValid(shill_error
)) {
345 shill_properties
.GetStringWithoutPathExpansion(
346 shill::kPreviousErrorProperty
, &shill_error
);
347 NET_LOG_DEBUG("Notify Service.PreviousError: " + shill_error
,
349 if (!chromeos::NetworkState::ErrorIsValid(shill_error
))
352 NET_LOG_DEBUG("Notify Service.Error: " + shill_error
, service_path
);
355 const NetworkState
* network
=
356 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
359 // Always log last_error, but only use it if shill_error is empty.
360 // TODO(stevenjb): This shouldn't ever be necessary, but is kept here as
361 // a failsafe since more information is better than less when debugging
362 // and we have encountered some strange edge cases before.
363 NET_LOG_DEBUG("Notify Network.last_error: " + network
->last_error(),
365 if (shill_error
.empty())
366 shill_error
= network
->last_error();
369 if (ShillErrorIsIgnored(shill_error
)) {
370 NET_LOG_DEBUG("Notify Ignoring error: " + error_name
, service_path
);
374 error
= network_connect_
->GetShillErrorString(shill_error
, service_path
);
376 error
= l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN
);
378 NET_LOG_ERROR("Notify connect error: " + base::UTF16ToUTF8(error
),
381 std::string network_name
=
382 chromeos::shill_property_util::GetNameFromProperties(service_path
,
384 std::string network_error_details
;
385 shill_properties
.GetStringWithoutPathExpansion(shill::kErrorDetailsProperty
,
386 &network_error_details
);
388 base::string16 error_msg
;
389 if (!network_error_details
.empty()) {
390 // network_name should't be empty if network_error_details is set.
391 error_msg
= l10n_util::GetStringFUTF16(
392 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_SERVER_MESSAGE
,
393 base::UTF8ToUTF16(network_name
), error
,
394 base::UTF8ToUTF16(network_error_details
));
395 } else if (network_name
.empty()) {
396 error_msg
= l10n_util::GetStringFUTF16(
397 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_NO_NAME
, error
);
400 l10n_util::GetStringFUTF16(IDS_NETWORK_CONNECTION_ERROR_MESSAGE
,
401 base::UTF8ToUTF16(network_name
), error
);
404 std::string network_type
;
405 shill_properties
.GetStringWithoutPathExpansion(shill::kTypeProperty
,
408 ShowErrorNotification(
409 kNetworkConnectNotificationId
, network_type
,
410 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE
), error_msg
,
411 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath
,
412 weak_ptr_factory_
.GetWeakPtr(), service_path
));
415 void NetworkStateNotifier::ShowVpnDisconnectedNotification(
416 const NetworkState
* vpn
) {
417 base::string16 error_msg
= l10n_util::GetStringFUTF16(
418 IDS_NETWORK_VPN_CONNECTION_LOST_BODY
, base::UTF8ToUTF16(vpn
->name()));
419 ShowErrorNotification(
420 kNetworkConnectNotificationId
, shill::kTypeVPN
,
421 l10n_util::GetStringUTF16(IDS_NETWORK_VPN_CONNECTION_LOST_TITLE
),
422 error_msg
, base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath
,
423 weak_ptr_factory_
.GetWeakPtr(), vpn
->path()));
426 void NetworkStateNotifier::ShowNetworkSettingsForPath(
427 const std::string
& service_path
) {
428 network_connect_
->ShowNetworkSettingsForPath(service_path
);