Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / chromeos / network / network_state_notifier.cc
blob6147798a444aa190df391506fcfe84a4694599a3
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_state.h"
15 #include "chromeos/network/network_state_handler.h"
16 #include "chromeos/network/shill_property_util.h"
17 #include "components/device_event_log/device_event_log.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& service_path,
72 const std::string& notification_id,
73 const std::string& network_type,
74 const base::string16& title,
75 const base::string16& message,
76 const base::Closure& callback) {
77 NET_LOG(ERROR) << "ShowErrorNotification: " << service_path << ": "
78 << base::UTF16ToUTF8(title);
79 const gfx::Image& icon =
80 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
81 GetErrorNotificationIconId(network_type));
82 message_center::MessageCenter::Get()->AddNotification(
83 message_center::Notification::CreateSystemNotification(
84 notification_id, title, message, icon,
85 ui::NetworkStateNotifier::kNotifierNetworkError, callback));
88 } // namespace
90 namespace ui {
92 const char NetworkStateNotifier::kNotifierNetwork[] = "ui.chromeos.network";
93 const char NetworkStateNotifier::kNotifierNetworkError[] =
94 "ui.chromeos.network.error";
96 const char NetworkStateNotifier::kNetworkConnectNotificationId[] =
97 "chrome://settings/internet/connect";
98 const char NetworkStateNotifier::kNetworkActivateNotificationId[] =
99 "chrome://settings/internet/activate";
100 const char NetworkStateNotifier::kNetworkOutOfCreditsNotificationId[] =
101 "chrome://settings/internet/out-of-credits";
103 NetworkStateNotifier::NetworkStateNotifier(NetworkConnect* network_connect)
104 : network_connect_(network_connect),
105 did_show_out_of_credits_(false),
106 need_vpn_disconnection_notify_(false),
107 weak_ptr_factory_(this) {
108 if (!NetworkHandler::IsInitialized())
109 return;
110 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
111 handler->AddObserver(this, FROM_HERE);
112 UpdateDefaultNetwork(handler->DefaultNetwork());
113 NetworkHandler::Get()->network_connection_handler()->AddObserver(this);
116 NetworkStateNotifier::~NetworkStateNotifier() {
117 if (!NetworkHandler::IsInitialized())
118 return;
119 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
120 FROM_HERE);
121 NetworkHandler::Get()->network_connection_handler()->RemoveObserver(this);
124 void NetworkStateNotifier::ConnectToNetworkRequested(
125 const std::string& service_path) {
126 RemoveConnectNotification();
129 void NetworkStateNotifier::ConnectSucceeded(const std::string& service_path) {
130 RemoveConnectNotification();
133 void NetworkStateNotifier::ConnectFailed(const std::string& service_path,
134 const std::string& error_name) {
135 // Only show a notification for certain errors. Other failures are expected
136 // to be handled by the UI that initiated the connect request.
137 // Note: kErrorConnectFailed may also cause the configure dialog to be
138 // displayed, but we rely on the notification system to show additional
139 // details if available.
140 if (error_name != NetworkConnectionHandler::kErrorConnectFailed &&
141 error_name != NetworkConnectionHandler::kErrorNotFound &&
142 error_name != NetworkConnectionHandler::kErrorConfigureFailed &&
143 error_name != NetworkConnectionHandler::kErrorCertLoadTimeout) {
144 return;
146 ShowNetworkConnectError(error_name, service_path);
149 void NetworkStateNotifier::DisconnectRequested(
150 const std::string& service_path) {
151 const NetworkState* network =
152 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
153 service_path);
154 if (network && network->type() == shill::kTypeVPN)
155 need_vpn_disconnection_notify_ = false;
158 void NetworkStateNotifier::DefaultNetworkChanged(const NetworkState* network) {
159 if (!UpdateDefaultNetwork(network))
160 return;
161 // If the default network changes to another network, allow the out of
162 // credits notification to be shown again. A delay prevents the notification
163 // from being shown too frequently (see below).
164 if (network)
165 did_show_out_of_credits_ = false;
168 void NetworkStateNotifier::NetworkConnectionStateChanged(
169 const NetworkState* network) {
170 if (network->type() == shill::kTypeVPN)
171 UpdateVpnConnectionState(network);
174 void NetworkStateNotifier::NetworkPropertiesUpdated(
175 const NetworkState* network) {
176 if (network->type() != shill::kTypeCellular)
177 return;
178 UpdateCellularOutOfCredits(network);
179 UpdateCellularActivating(network);
182 bool NetworkStateNotifier::UpdateDefaultNetwork(const NetworkState* network) {
183 std::string default_network_path;
184 if (network)
185 default_network_path = network->path();
186 if (default_network_path != last_default_network_) {
187 last_default_network_ = default_network_path;
188 return true;
190 return false;
193 void NetworkStateNotifier::UpdateVpnConnectionState(const NetworkState* vpn) {
194 if (!vpn->IsConnectedState() && need_vpn_disconnection_notify_)
195 ShowVpnDisconnectedNotification(vpn);
196 need_vpn_disconnection_notify_ = vpn->IsConnectedState();
199 void NetworkStateNotifier::UpdateCellularOutOfCredits(
200 const NetworkState* cellular) {
201 // Only display a notification if we are out of credits and have not already
202 // shown a notification (or have since connected to another network type).
203 if (!cellular->cellular_out_of_credits() || did_show_out_of_credits_)
204 return;
206 // Only display a notification if not connected, connecting, or waiting to
207 // connect to another network.
208 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
209 const NetworkState* default_network = handler->DefaultNetwork();
210 if (default_network && default_network != cellular)
211 return;
212 if (handler->ConnectingNetworkByType(NetworkTypePattern::NonVirtual()) ||
213 NetworkHandler::Get()
214 ->network_connection_handler()
215 ->HasPendingConnectRequest())
216 return;
218 did_show_out_of_credits_ = true;
219 base::TimeDelta dtime = base::Time::Now() - out_of_credits_notify_time_;
220 if (dtime.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds) {
221 out_of_credits_notify_time_ = base::Time::Now();
222 base::string16 error_msg = l10n_util::GetStringFUTF16(
223 IDS_NETWORK_OUT_OF_CREDITS_BODY, base::UTF8ToUTF16(cellular->name()));
224 ShowErrorNotification(
225 cellular->path(), kNetworkOutOfCreditsNotificationId, cellular->type(),
226 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_CREDITS_TITLE), error_msg,
227 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
228 weak_ptr_factory_.GetWeakPtr(), cellular->path()));
232 void NetworkStateNotifier::UpdateCellularActivating(
233 const NetworkState* cellular) {
234 // Keep track of any activating cellular network.
235 std::string activation_state = cellular->activation_state();
236 if (activation_state == shill::kActivationStateActivating) {
237 cellular_activating_.insert(cellular->path());
238 return;
240 // Only display a notification if this network was activating and is now
241 // activated.
242 if (!cellular_activating_.count(cellular->path()) ||
243 activation_state != shill::kActivationStateActivated)
244 return;
246 cellular_activating_.erase(cellular->path());
247 int icon_id;
248 if (cellular->network_technology() == shill::kNetworkTechnologyLte)
249 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE;
250 else
251 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G;
252 const gfx::Image& icon =
253 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id);
254 message_center::MessageCenter::Get()->AddNotification(
255 message_center::Notification::CreateSystemNotification(
256 kNetworkActivateNotificationId,
257 l10n_util::GetStringUTF16(IDS_NETWORK_CELLULAR_ACTIVATED_TITLE),
258 l10n_util::GetStringFUTF16(IDS_NETWORK_CELLULAR_ACTIVATED,
259 base::UTF8ToUTF16((cellular->name()))),
260 icon, kNotifierNetwork,
261 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
262 weak_ptr_factory_.GetWeakPtr(), cellular->path())));
265 void NetworkStateNotifier::ShowNetworkConnectError(
266 const std::string& error_name,
267 const std::string& service_path) {
268 if (service_path.empty()) {
269 base::DictionaryValue shill_properties;
270 ShowConnectErrorNotification(error_name, service_path, shill_properties);
271 return;
273 // Get the up-to-date properties for the network and display the error.
274 NetworkHandler::Get()->network_configuration_handler()->GetShillProperties(
275 service_path,
276 base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesSucceeded,
277 weak_ptr_factory_.GetWeakPtr(), error_name),
278 base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesFailed,
279 weak_ptr_factory_.GetWeakPtr(), error_name, service_path));
282 void NetworkStateNotifier::ShowMobileActivationError(
283 const std::string& service_path) {
284 const NetworkState* cellular =
285 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
286 service_path);
287 if (!cellular || cellular->type() != shill::kTypeCellular) {
288 NET_LOG(ERROR) << "ShowMobileActivationError without Cellular network: "
289 << service_path;
290 return;
292 message_center::MessageCenter::Get()->AddNotification(
293 message_center::Notification::CreateSystemNotification(
294 kNetworkActivateNotificationId,
295 l10n_util::GetStringUTF16(IDS_NETWORK_ACTIVATION_ERROR_TITLE),
296 l10n_util::GetStringFUTF16(IDS_NETWORK_ACTIVATION_NEEDS_CONNECTION,
297 base::UTF8ToUTF16(cellular->name())),
298 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
299 IDR_AURA_UBER_TRAY_NETWORK_FAILED_CELLULAR),
300 kNotifierNetworkError,
301 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
302 weak_ptr_factory_.GetWeakPtr(), service_path)));
305 void NetworkStateNotifier::RemoveConnectNotification() {
306 message_center::MessageCenter* message_center =
307 message_center::MessageCenter::Get();
308 if (message_center) {
309 message_center->RemoveNotification(kNetworkConnectNotificationId,
310 false /* not by user */);
314 void NetworkStateNotifier::ConnectErrorPropertiesSucceeded(
315 const std::string& error_name,
316 const std::string& service_path,
317 const base::DictionaryValue& shill_properties) {
318 std::string state;
319 shill_properties.GetStringWithoutPathExpansion(shill::kStateProperty, &state);
320 if (chromeos::NetworkState::StateIsConnected(state) ||
321 chromeos::NetworkState::StateIsConnecting(state)) {
322 // Network is no longer in an error state. This can happen if an
323 // unexpected idle state transition occurs, see crbug.com/333955.
324 return;
326 ShowConnectErrorNotification(error_name, service_path, shill_properties);
329 void NetworkStateNotifier::ConnectErrorPropertiesFailed(
330 const std::string& error_name,
331 const std::string& service_path,
332 const std::string& shill_connect_error,
333 scoped_ptr<base::DictionaryValue> shill_error_data) {
334 base::DictionaryValue shill_properties;
335 ShowConnectErrorNotification(error_name, service_path, shill_properties);
338 void NetworkStateNotifier::ShowConnectErrorNotification(
339 const std::string& error_name,
340 const std::string& service_path,
341 const base::DictionaryValue& shill_properties) {
342 base::string16 error = GetConnectErrorString(error_name);
343 NET_LOG(DEBUG) << "Notify: " << service_path
344 << ": Connect error: " << error_name << ": "
345 << base::UTF16ToUTF8(error);
346 if (error.empty()) {
347 std::string shill_error;
348 shill_properties.GetStringWithoutPathExpansion(shill::kErrorProperty,
349 &shill_error);
350 if (!chromeos::NetworkState::ErrorIsValid(shill_error)) {
351 shill_properties.GetStringWithoutPathExpansion(
352 shill::kPreviousErrorProperty, &shill_error);
353 NET_LOG(DEBUG) << "Notify: " << service_path
354 << ": Service.PreviousError: " << shill_error;
355 if (!chromeos::NetworkState::ErrorIsValid(shill_error))
356 shill_error.clear();
357 } else {
358 NET_LOG(DEBUG) << "Notify: " << service_path
359 << ": Service.Error: " << shill_error;
362 const NetworkState* network =
363 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
364 service_path);
365 if (network) {
366 // Always log last_error, but only use it if shill_error is empty.
367 // TODO(stevenjb): This shouldn't ever be necessary, but is kept here as
368 // a failsafe since more information is better than less when debugging
369 // and we have encountered some strange edge cases before.
370 NET_LOG(DEBUG) << "Notify: " << service_path
371 << ": Network.last_error: " << network->last_error();
372 if (shill_error.empty())
373 shill_error = network->last_error();
376 if (ShillErrorIsIgnored(shill_error)) {
377 NET_LOG(DEBUG) << "Notify: " << service_path
378 << ": Ignoring error: " << error_name;
379 return;
382 error = network_connect_->GetShillErrorString(shill_error, service_path);
383 if (error.empty()) {
384 if (error_name == NetworkConnectionHandler::kErrorConnectFailed &&
385 network && !network->connectable()) {
386 // Connect failure on non connectable network with no additional
387 // information. We expect the UI to show configuration UI so do not
388 // show an additional (and unhelpful) notification.
389 return;
391 error = l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN);
394 NET_LOG(ERROR) << "Notify: " << service_path
395 << ": Connect error: " + base::UTF16ToUTF8(error);
397 std::string network_name =
398 chromeos::shill_property_util::GetNameFromProperties(service_path,
399 shill_properties);
400 std::string network_error_details;
401 shill_properties.GetStringWithoutPathExpansion(shill::kErrorDetailsProperty,
402 &network_error_details);
404 base::string16 error_msg;
405 if (!network_error_details.empty()) {
406 // network_name should't be empty if network_error_details is set.
407 error_msg = l10n_util::GetStringFUTF16(
408 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_SERVER_MESSAGE,
409 base::UTF8ToUTF16(network_name), error,
410 base::UTF8ToUTF16(network_error_details));
411 } else if (network_name.empty()) {
412 error_msg = l10n_util::GetStringFUTF16(
413 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_NO_NAME, error);
414 } else {
415 error_msg =
416 l10n_util::GetStringFUTF16(IDS_NETWORK_CONNECTION_ERROR_MESSAGE,
417 base::UTF8ToUTF16(network_name), error);
420 std::string network_type;
421 shill_properties.GetStringWithoutPathExpansion(shill::kTypeProperty,
422 &network_type);
424 ShowErrorNotification(
425 service_path, kNetworkConnectNotificationId, network_type,
426 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE), error_msg,
427 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
428 weak_ptr_factory_.GetWeakPtr(), service_path));
431 void NetworkStateNotifier::ShowVpnDisconnectedNotification(
432 const NetworkState* vpn) {
433 base::string16 error_msg = l10n_util::GetStringFUTF16(
434 IDS_NETWORK_VPN_CONNECTION_LOST_BODY, base::UTF8ToUTF16(vpn->name()));
435 ShowErrorNotification(
436 vpn->path(), kNetworkConnectNotificationId, shill::kTypeVPN,
437 l10n_util::GetStringUTF16(IDS_NETWORK_VPN_CONNECTION_LOST_TITLE),
438 error_msg, base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
439 weak_ptr_factory_.GetWeakPtr(), vpn->path()));
442 void NetworkStateNotifier::ShowNetworkSettingsForPath(
443 const std::string& service_path) {
444 network_connect_->ShowNetworkSettingsForPath(service_path);
447 } // namespace ui