Roll src/third_party/WebKit d67e6d4:0cefbbd (svn 194847:194848)
[chromium-blink-merge.git] / ui / chromeos / network / network_state_notifier.cc
blob3bcbb6707d13f0262997eac5523a9ed0172dfa3f
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"
7 #include "base/bind.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;
33 namespace {
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)
40 return true;
41 return false;
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));
85 } // namespace
87 namespace ui {
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())
106 return;
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())
115 return;
116 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
117 FROM_HERE);
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) {
141 return;
143 ShowNetworkConnectError(error_name, service_path);
146 void NetworkStateNotifier::DiconnectRequested(const std::string& service_path) {
147 const NetworkState* network =
148 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
149 service_path);
150 if (network && network->type() == shill::kTypeVPN)
151 need_vpn_disconnection_notify_ = false;
154 void NetworkStateNotifier::DefaultNetworkChanged(const NetworkState* network) {
155 if (!UpdateDefaultNetwork(network))
156 return;
157 // If the default network changes to another network, allow the out of
158 // credits notification to be shown again. A delay prevents the notification
159 // from being shown too frequently (see below).
160 if (network)
161 did_show_out_of_credits_ = false;
164 void NetworkStateNotifier::NetworkConnectionStateChanged(
165 const NetworkState* network) {
166 if (network->type() == shill::kTypeVPN)
167 UpdateVpnConnectionState(network);
170 void NetworkStateNotifier::NetworkPropertiesUpdated(
171 const NetworkState* network) {
172 if (network->type() != shill::kTypeCellular)
173 return;
174 UpdateCellularOutOfCredits(network);
175 UpdateCellularActivating(network);
178 bool NetworkStateNotifier::UpdateDefaultNetwork(const NetworkState* network) {
179 std::string default_network_path;
180 if (network)
181 default_network_path = network->path();
182 if (default_network_path != last_default_network_) {
183 last_default_network_ = default_network_path;
184 return true;
186 return false;
189 void NetworkStateNotifier::UpdateVpnConnectionState(const NetworkState* vpn) {
190 if (!vpn->IsConnectedState() && need_vpn_disconnection_notify_)
191 ShowVpnDisconnectedNotification(vpn);
192 need_vpn_disconnection_notify_ = vpn->IsConnectedState();
195 void NetworkStateNotifier::UpdateCellularOutOfCredits(
196 const NetworkState* cellular) {
197 // Only display a notification if we are out of credits and have not already
198 // shown a notification (or have since connected to another network type).
199 if (!cellular->cellular_out_of_credits() || did_show_out_of_credits_)
200 return;
202 // Only display a notification if not connected, connecting, or waiting to
203 // connect to another network.
204 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
205 const NetworkState* default_network = handler->DefaultNetwork();
206 if (default_network && default_network != cellular)
207 return;
208 if (handler->ConnectingNetworkByType(NetworkTypePattern::NonVirtual()) ||
209 NetworkHandler::Get()
210 ->network_connection_handler()
211 ->HasPendingConnectRequest())
212 return;
214 did_show_out_of_credits_ = true;
215 base::TimeDelta dtime = base::Time::Now() - out_of_credits_notify_time_;
216 if (dtime.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds) {
217 out_of_credits_notify_time_ = base::Time::Now();
218 base::string16 error_msg = l10n_util::GetStringFUTF16(
219 IDS_NETWORK_OUT_OF_CREDITS_BODY, base::UTF8ToUTF16(cellular->name()));
220 ShowErrorNotification(
221 kNetworkOutOfCreditsNotificationId, cellular->type(),
222 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_CREDITS_TITLE), error_msg,
223 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
224 weak_ptr_factory_.GetWeakPtr(), cellular->path()));
228 void NetworkStateNotifier::UpdateCellularActivating(
229 const NetworkState* cellular) {
230 // Keep track of any activating cellular network.
231 std::string activation_state = cellular->activation_state();
232 if (activation_state == shill::kActivationStateActivating) {
233 cellular_activating_.insert(cellular->path());
234 return;
236 // Only display a notification if this network was activating and is now
237 // activated.
238 if (!cellular_activating_.count(cellular->path()) ||
239 activation_state != shill::kActivationStateActivated)
240 return;
242 cellular_activating_.erase(cellular->path());
243 int icon_id;
244 if (cellular->network_technology() == shill::kNetworkTechnologyLte)
245 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE;
246 else
247 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G;
248 const gfx::Image& icon =
249 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id);
250 message_center::MessageCenter::Get()->AddNotification(
251 message_center::Notification::CreateSystemNotification(
252 kNetworkActivateNotificationId,
253 l10n_util::GetStringUTF16(IDS_NETWORK_CELLULAR_ACTIVATED_TITLE),
254 l10n_util::GetStringFUTF16(IDS_NETWORK_CELLULAR_ACTIVATED,
255 base::UTF8ToUTF16((cellular->name()))),
256 icon, kNotifierNetwork,
257 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
258 weak_ptr_factory_.GetWeakPtr(), cellular->path())));
261 void NetworkStateNotifier::ShowNetworkConnectError(
262 const std::string& error_name,
263 const std::string& service_path) {
264 if (service_path.empty()) {
265 base::DictionaryValue shill_properties;
266 ShowConnectErrorNotification(error_name, service_path, shill_properties);
267 return;
269 // Get the up-to-date properties for the network and display the error.
270 NetworkHandler::Get()->network_configuration_handler()->GetShillProperties(
271 service_path,
272 base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesSucceeded,
273 weak_ptr_factory_.GetWeakPtr(), error_name),
274 base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesFailed,
275 weak_ptr_factory_.GetWeakPtr(), error_name, service_path));
278 void NetworkStateNotifier::ShowMobileActivationError(
279 const std::string& service_path) {
280 const NetworkState* cellular =
281 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
282 service_path);
283 if (!cellular || cellular->type() != shill::kTypeCellular) {
284 NET_LOG_ERROR("ShowMobileActivationError without Cellular network",
285 service_path);
286 return;
288 message_center::MessageCenter::Get()->AddNotification(
289 message_center::Notification::CreateSystemNotification(
290 kNetworkActivateNotificationId,
291 l10n_util::GetStringUTF16(IDS_NETWORK_ACTIVATION_ERROR_TITLE),
292 l10n_util::GetStringFUTF16(IDS_NETWORK_ACTIVATION_NEEDS_CONNECTION,
293 base::UTF8ToUTF16(cellular->name())),
294 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
295 IDR_AURA_UBER_TRAY_NETWORK_FAILED_CELLULAR),
296 kNotifierNetworkError,
297 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
298 weak_ptr_factory_.GetWeakPtr(), service_path)));
301 void NetworkStateNotifier::RemoveConnectNotification() {
302 message_center::MessageCenter* message_center =
303 message_center::MessageCenter::Get();
304 if (message_center) {
305 message_center->RemoveNotification(kNetworkConnectNotificationId,
306 false /* not by user */);
310 void NetworkStateNotifier::ConnectErrorPropertiesSucceeded(
311 const std::string& error_name,
312 const std::string& service_path,
313 const base::DictionaryValue& shill_properties) {
314 std::string state;
315 shill_properties.GetStringWithoutPathExpansion(shill::kStateProperty, &state);
316 if (chromeos::NetworkState::StateIsConnected(state) ||
317 chromeos::NetworkState::StateIsConnecting(state)) {
318 // Network is no longer in an error state. This can happen if an
319 // unexpected idle state transition occurs, see crbug.com/333955.
320 return;
322 ShowConnectErrorNotification(error_name, service_path, shill_properties);
325 void NetworkStateNotifier::ConnectErrorPropertiesFailed(
326 const std::string& error_name,
327 const std::string& service_path,
328 const std::string& shill_connect_error,
329 scoped_ptr<base::DictionaryValue> shill_error_data) {
330 base::DictionaryValue shill_properties;
331 ShowConnectErrorNotification(error_name, service_path, shill_properties);
334 void NetworkStateNotifier::ShowConnectErrorNotification(
335 const std::string& error_name,
336 const std::string& service_path,
337 const base::DictionaryValue& shill_properties) {
338 base::string16 error = GetConnectErrorString(error_name);
339 if (error.empty()) {
340 std::string shill_error;
341 shill_properties.GetStringWithoutPathExpansion(shill::kErrorProperty,
342 &shill_error);
343 if (!chromeos::NetworkState::ErrorIsValid(shill_error)) {
344 shill_properties.GetStringWithoutPathExpansion(
345 shill::kPreviousErrorProperty, &shill_error);
346 NET_LOG_DEBUG("Notify Service.PreviousError: " + shill_error,
347 service_path);
348 if (!chromeos::NetworkState::ErrorIsValid(shill_error))
349 shill_error.clear();
350 } else {
351 NET_LOG_DEBUG("Notify Service.Error: " + shill_error, service_path);
354 const NetworkState* network =
355 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
356 service_path);
357 if (network) {
358 // Always log last_error, but only use it if shill_error is empty.
359 // TODO(stevenjb): This shouldn't ever be necessary, but is kept here as
360 // a failsafe since more information is better than less when debugging
361 // and we have encountered some strange edge cases before.
362 NET_LOG_DEBUG("Notify Network.last_error: " + network->last_error(),
363 service_path);
364 if (shill_error.empty())
365 shill_error = network->last_error();
368 if (ShillErrorIsIgnored(shill_error)) {
369 NET_LOG_DEBUG("Notify Ignoring error: " + error_name, service_path);
370 return;
373 error = network_connect_->GetShillErrorString(shill_error, service_path);
374 if (error.empty())
375 error = l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN);
377 NET_LOG_ERROR("Notify connect error: " + base::UTF16ToUTF8(error),
378 service_path);
380 std::string network_name =
381 chromeos::shill_property_util::GetNameFromProperties(service_path,
382 shill_properties);
383 std::string network_error_details;
384 shill_properties.GetStringWithoutPathExpansion(shill::kErrorDetailsProperty,
385 &network_error_details);
387 base::string16 error_msg;
388 if (!network_error_details.empty()) {
389 // network_name should't be empty if network_error_details is set.
390 error_msg = l10n_util::GetStringFUTF16(
391 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_SERVER_MESSAGE,
392 base::UTF8ToUTF16(network_name), error,
393 base::UTF8ToUTF16(network_error_details));
394 } else if (network_name.empty()) {
395 error_msg = l10n_util::GetStringFUTF16(
396 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_NO_NAME, error);
397 } else {
398 error_msg =
399 l10n_util::GetStringFUTF16(IDS_NETWORK_CONNECTION_ERROR_MESSAGE,
400 base::UTF8ToUTF16(network_name), error);
403 std::string network_type;
404 shill_properties.GetStringWithoutPathExpansion(shill::kTypeProperty,
405 &network_type);
407 ShowErrorNotification(
408 kNetworkConnectNotificationId, network_type,
409 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE), error_msg,
410 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
411 weak_ptr_factory_.GetWeakPtr(), service_path));
414 void NetworkStateNotifier::ShowVpnDisconnectedNotification(
415 const NetworkState* vpn) {
416 base::string16 error_msg = l10n_util::GetStringFUTF16(
417 IDS_NETWORK_VPN_CONNECTION_LOST_BODY, base::UTF8ToUTF16(vpn->name()));
418 ShowErrorNotification(
419 kNetworkConnectNotificationId, shill::kTypeVPN,
420 l10n_util::GetStringUTF16(IDS_NETWORK_VPN_CONNECTION_LOST_TITLE),
421 error_msg, base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
422 weak_ptr_factory_.GetWeakPtr(), vpn->path()));
425 void NetworkStateNotifier::ShowNetworkSettingsForPath(
426 const std::string& service_path) {
427 network_connect_->ShowNetworkSettingsForPath(service_path);
430 } // namespace ui