Move action_runner.py out of actions folder prior to moving actions to internal.
[chromium-blink-merge.git] / ui / chromeos / network / network_state_notifier.cc
blobfbb02d1484577f0f434e3d477903ccd8a96a9fff
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::ConnectFailed(const std::string& service_path,
122 const std::string& error_name) {
123 // Only show a notification for certain errors. Other failures are expected
124 // to be handled by the UI that initiated the connect request.
125 // Note: kErrorConnectFailed may also cause the configure dialog to be
126 // displayed, but we rely on the notification system to show additional
127 // details if available.
128 if (error_name != NetworkConnectionHandler::kErrorConnectFailed &&
129 error_name != NetworkConnectionHandler::kErrorNotFound &&
130 error_name != NetworkConnectionHandler::kErrorConfigureFailed &&
131 error_name != NetworkConnectionHandler::kErrorCertLoadTimeout) {
132 return;
134 ShowNetworkConnectError(error_name, service_path);
137 void NetworkStateNotifier::DiconnectRequested(const std::string& service_path) {
138 const NetworkState* network =
139 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
140 service_path);
141 if (network && network->type() == shill::kTypeVPN)
142 need_vpn_disconnection_notify_ = false;
145 void NetworkStateNotifier::DefaultNetworkChanged(const NetworkState* network) {
146 if (!UpdateDefaultNetwork(network))
147 return;
148 // If the default network changes to another network, allow the out of
149 // credits notification to be shown again. A delay prevents the notification
150 // from being shown too frequently (see below).
151 if (network)
152 did_show_out_of_credits_ = false;
155 void NetworkStateNotifier::NetworkConnectionStateChanged(
156 const NetworkState* network) {
157 if (network->type() == shill::kTypeVPN)
158 UpdateVpnConnectionState(network);
161 void NetworkStateNotifier::NetworkPropertiesUpdated(
162 const NetworkState* network) {
163 if (network->type() != shill::kTypeCellular)
164 return;
165 UpdateCellularOutOfCredits(network);
166 UpdateCellularActivating(network);
169 bool NetworkStateNotifier::UpdateDefaultNetwork(const NetworkState* network) {
170 std::string default_network_path;
171 if (network)
172 default_network_path = network->path();
173 if (default_network_path != last_default_network_) {
174 last_default_network_ = default_network_path;
175 return true;
177 return false;
180 void NetworkStateNotifier::UpdateVpnConnectionState(const NetworkState* vpn) {
181 if (!vpn->IsConnectedState() && need_vpn_disconnection_notify_)
182 ShowVpnDisconnectedNotification(vpn);
183 need_vpn_disconnection_notify_ = vpn->IsConnectedState();
186 void NetworkStateNotifier::UpdateCellularOutOfCredits(
187 const NetworkState* cellular) {
188 // Only display a notification if we are out of credits and have not already
189 // shown a notification (or have since connected to another network type).
190 if (!cellular->cellular_out_of_credits() || did_show_out_of_credits_)
191 return;
193 // Only display a notification if not connected, connecting, or waiting to
194 // connect to another network.
195 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
196 const NetworkState* default_network = handler->DefaultNetwork();
197 if (default_network && default_network != cellular)
198 return;
199 if (handler->ConnectingNetworkByType(NetworkTypePattern::NonVirtual()) ||
200 NetworkHandler::Get()
201 ->network_connection_handler()
202 ->HasPendingConnectRequest())
203 return;
205 did_show_out_of_credits_ = true;
206 base::TimeDelta dtime = base::Time::Now() - out_of_credits_notify_time_;
207 if (dtime.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds) {
208 out_of_credits_notify_time_ = base::Time::Now();
209 base::string16 error_msg = l10n_util::GetStringFUTF16(
210 IDS_NETWORK_OUT_OF_CREDITS_BODY, base::UTF8ToUTF16(cellular->name()));
211 ShowErrorNotification(
212 kNetworkOutOfCreditsNotificationId, cellular->type(),
213 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_CREDITS_TITLE), error_msg,
214 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
215 weak_ptr_factory_.GetWeakPtr(), cellular->path()));
219 void NetworkStateNotifier::UpdateCellularActivating(
220 const NetworkState* cellular) {
221 // Keep track of any activating cellular network.
222 std::string activation_state = cellular->activation_state();
223 if (activation_state == shill::kActivationStateActivating) {
224 cellular_activating_.insert(cellular->path());
225 return;
227 // Only display a notification if this network was activating and is now
228 // activated.
229 if (!cellular_activating_.count(cellular->path()) ||
230 activation_state != shill::kActivationStateActivated)
231 return;
233 cellular_activating_.erase(cellular->path());
234 int icon_id;
235 if (cellular->network_technology() == shill::kNetworkTechnologyLte)
236 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE;
237 else
238 icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G;
239 const gfx::Image& icon =
240 ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id);
241 message_center::MessageCenter::Get()->AddNotification(
242 message_center::Notification::CreateSystemNotification(
243 kNetworkActivateNotificationId,
244 l10n_util::GetStringUTF16(IDS_NETWORK_CELLULAR_ACTIVATED_TITLE),
245 l10n_util::GetStringFUTF16(IDS_NETWORK_CELLULAR_ACTIVATED,
246 base::UTF8ToUTF16((cellular->name()))),
247 icon, kNotifierNetwork,
248 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
249 weak_ptr_factory_.GetWeakPtr(), cellular->path())));
252 void NetworkStateNotifier::ShowNetworkConnectError(
253 const std::string& error_name,
254 const std::string& service_path) {
255 if (service_path.empty()) {
256 base::DictionaryValue shill_properties;
257 ShowConnectErrorNotification(error_name, service_path, shill_properties);
258 return;
260 // Get the up-to-date properties for the network and display the error.
261 NetworkHandler::Get()->network_configuration_handler()->GetShillProperties(
262 service_path,
263 base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesSucceeded,
264 weak_ptr_factory_.GetWeakPtr(), error_name),
265 base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesFailed,
266 weak_ptr_factory_.GetWeakPtr(), error_name, service_path));
269 void NetworkStateNotifier::ShowMobileActivationError(
270 const std::string& service_path) {
271 const NetworkState* cellular =
272 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
273 service_path);
274 if (!cellular || cellular->type() != shill::kTypeCellular) {
275 NET_LOG_ERROR("ShowMobileActivationError without Cellular network",
276 service_path);
277 return;
279 message_center::MessageCenter::Get()->AddNotification(
280 message_center::Notification::CreateSystemNotification(
281 kNetworkActivateNotificationId,
282 l10n_util::GetStringUTF16(IDS_NETWORK_ACTIVATION_ERROR_TITLE),
283 l10n_util::GetStringFUTF16(IDS_NETWORK_ACTIVATION_NEEDS_CONNECTION,
284 base::UTF8ToUTF16(cellular->name())),
285 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
286 IDR_AURA_UBER_TRAY_NETWORK_FAILED_CELLULAR),
287 kNotifierNetworkError,
288 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
289 weak_ptr_factory_.GetWeakPtr(), service_path)));
292 void NetworkStateNotifier::RemoveConnectNotification() {
293 message_center::MessageCenter* message_center =
294 message_center::MessageCenter::Get();
295 if (message_center) {
296 message_center->RemoveNotification(kNetworkConnectNotificationId,
297 false /* not by user */);
301 void NetworkStateNotifier::ConnectErrorPropertiesSucceeded(
302 const std::string& error_name,
303 const std::string& service_path,
304 const base::DictionaryValue& shill_properties) {
305 std::string state;
306 shill_properties.GetStringWithoutPathExpansion(shill::kStateProperty, &state);
307 if (chromeos::NetworkState::StateIsConnected(state) ||
308 chromeos::NetworkState::StateIsConnecting(state)) {
309 // Network is no longer in an error state. This can happen if an
310 // unexpected idle state transition occurs, see crbug.com/333955.
311 return;
313 ShowConnectErrorNotification(error_name, service_path, shill_properties);
316 void NetworkStateNotifier::ConnectErrorPropertiesFailed(
317 const std::string& error_name,
318 const std::string& service_path,
319 const std::string& shill_connect_error,
320 scoped_ptr<base::DictionaryValue> shill_error_data) {
321 base::DictionaryValue shill_properties;
322 ShowConnectErrorNotification(error_name, service_path, shill_properties);
325 void NetworkStateNotifier::ShowConnectErrorNotification(
326 const std::string& error_name,
327 const std::string& service_path,
328 const base::DictionaryValue& shill_properties) {
329 base::string16 error = GetConnectErrorString(error_name);
330 if (error.empty()) {
331 std::string shill_error;
332 shill_properties.GetStringWithoutPathExpansion(shill::kErrorProperty,
333 &shill_error);
334 if (!chromeos::NetworkState::ErrorIsValid(shill_error)) {
335 shill_properties.GetStringWithoutPathExpansion(
336 shill::kPreviousErrorProperty, &shill_error);
337 NET_LOG_DEBUG("Notify Service.PreviousError: " + shill_error,
338 service_path);
339 if (!chromeos::NetworkState::ErrorIsValid(shill_error))
340 shill_error.clear();
341 } else {
342 NET_LOG_DEBUG("Notify Service.Error: " + shill_error, service_path);
345 const NetworkState* network =
346 NetworkHandler::Get()->network_state_handler()->GetNetworkState(
347 service_path);
348 if (network) {
349 // Always log last_error, but only use it if shill_error is empty.
350 // TODO(stevenjb): This shouldn't ever be necessary, but is kept here as
351 // a failsafe since more information is better than less when debugging
352 // and we have encountered some strange edge cases before.
353 NET_LOG_DEBUG("Notify Network.last_error: " + network->last_error(),
354 service_path);
355 if (shill_error.empty())
356 shill_error = network->last_error();
359 if (ShillErrorIsIgnored(shill_error)) {
360 NET_LOG_DEBUG("Notify Ignoring error: " + error_name, service_path);
361 return;
364 error = network_connect_->GetShillErrorString(shill_error, service_path);
365 if (error.empty())
366 error = l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN);
368 NET_LOG_ERROR("Notify connect error: " + base::UTF16ToUTF8(error),
369 service_path);
371 std::string network_name =
372 chromeos::shill_property_util::GetNameFromProperties(service_path,
373 shill_properties);
374 std::string network_error_details;
375 shill_properties.GetStringWithoutPathExpansion(shill::kErrorDetailsProperty,
376 &network_error_details);
378 base::string16 error_msg;
379 if (!network_error_details.empty()) {
380 // network_name should't be empty if network_error_details is set.
381 error_msg = l10n_util::GetStringFUTF16(
382 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_SERVER_MESSAGE,
383 base::UTF8ToUTF16(network_name), error,
384 base::UTF8ToUTF16(network_error_details));
385 } else if (network_name.empty()) {
386 error_msg = l10n_util::GetStringFUTF16(
387 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_NO_NAME, error);
388 } else {
389 error_msg =
390 l10n_util::GetStringFUTF16(IDS_NETWORK_CONNECTION_ERROR_MESSAGE,
391 base::UTF8ToUTF16(network_name), error);
394 std::string network_type;
395 shill_properties.GetStringWithoutPathExpansion(shill::kTypeProperty,
396 &network_type);
398 ShowErrorNotification(
399 kNetworkConnectNotificationId, network_type,
400 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE), error_msg,
401 base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
402 weak_ptr_factory_.GetWeakPtr(), service_path));
405 void NetworkStateNotifier::ShowVpnDisconnectedNotification(
406 const NetworkState* vpn) {
407 base::string16 error_msg = l10n_util::GetStringFUTF16(
408 IDS_NETWORK_VPN_CONNECTION_LOST_BODY, base::UTF8ToUTF16(vpn->name()));
409 ShowErrorNotification(
410 kNetworkConnectNotificationId, shill::kTypeVPN,
411 l10n_util::GetStringUTF16(IDS_NETWORK_VPN_CONNECTION_LOST_TITLE),
412 error_msg, base::Bind(&NetworkStateNotifier::ShowNetworkSettingsForPath,
413 weak_ptr_factory_.GetWeakPtr(), vpn->path()));
416 void NetworkStateNotifier::ShowNetworkSettingsForPath(
417 const std::string& service_path) {
418 network_connect_->ShowNetworkSettingsForPath(service_path);
421 } // namespace ui