Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / ui / chromeos / network / network_state_notifier.cc
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.
4
5 #include "ui/chromeos/network/network_state_notifier.h"
6
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"
26
27 using chromeos::NetworkConnectionHandler;
28 using chromeos::NetworkHandler;
29 using chromeos::NetworkState;
30 using chromeos::NetworkStateHandler;
31 using chromeos::NetworkTypePattern;
32
33 namespace {
34
35 const int kMinTimeBetweenOutOfCreditsNotifySeconds = 10 * 60;
36
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;
42 }
43
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);
51   }
52   if (error_name == NetworkConnectionHandler::kErrorCertLoadTimeout) {
53     return l10n_util::GetStringUTF16(
54         IDS_CHROMEOS_NETWORK_ERROR_CERTIFICATES_NOT_LOADED);
55   }
56   if (error_name == ui::NetworkConnect::kErrorActivateFailed) {
57     return l10n_util::GetStringUTF16(
58         IDS_CHROMEOS_NETWORK_ERROR_ACTIVATION_FAILED);
59   }
60   return base::string16();
61 }
62
63 void ShowErrorNotification(const std::string& notification_id,
64                            const std::string& network_type,
65                            const base::string16& title,
66                            const base::string16& message,
67                            const base::Closure& callback) {
68   int icon_id = (network_type == shill::kTypeCellular)
69                     ? IDR_AURA_UBER_TRAY_NETWORK_FAILED_CELLULAR
70                     : IDR_AURA_UBER_TRAY_NETWORK_FAILED;
71   const gfx::Image& icon =
72       ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id);
73   message_center::MessageCenter::Get()->AddNotification(
74       message_center::Notification::CreateSystemNotification(
75           notification_id, title, message, icon,
76           ui::NetworkStateNotifier::kNotifierNetworkError, callback));
77 }
78
79 }  // namespace
80
81 namespace ui {
82
83 const char NetworkStateNotifier::kNotifierNetwork[] = "ui.chromeos.network";
84 const char NetworkStateNotifier::kNotifierNetworkError[] =
85     "ui.chromeos.network.error";
86
87 const char NetworkStateNotifier::kNetworkConnectNotificationId[] =
88     "chrome://settings/internet/connect";
89 const char NetworkStateNotifier::kNetworkActivateNotificationId[] =
90     "chrome://settings/internet/activate";
91 const char NetworkStateNotifier::kNetworkOutOfCreditsNotificationId[] =
92     "chrome://settings/internet/out-of-credits";
93
94 NetworkStateNotifier::NetworkStateNotifier(NetworkConnect* network_connect)
95     : network_connect_(network_connect),
96       did_show_out_of_credits_(false),
97       weak_ptr_factory_(this) {
98   if (!NetworkHandler::IsInitialized())
99     return;
100   NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
101   handler->AddObserver(this, FROM_HERE);
102   UpdateDefaultNetwork(handler->DefaultNetwork());
103 }
104
105 NetworkStateNotifier::~NetworkStateNotifier() {
106   if (!NetworkHandler::IsInitialized())
107     return;
108   NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
109                                                                  FROM_HERE);
110 }
111
112 void NetworkStateNotifier::DefaultNetworkChanged(const NetworkState* network) {
113   if (!UpdateDefaultNetwork(network))
114     return;
115   // If the default network changes to another network, allow the out of
116   // credits notification to be shown again. A delay prevents the notification
117   // from being shown too frequently (see below).
118   if (network)
119     did_show_out_of_credits_ = false;
120 }
121
122 void NetworkStateNotifier::NetworkPropertiesUpdated(
123     const NetworkState* network) {
124   if (network->type() != shill::kTypeCellular)
125     return;
126   UpdateCellularOutOfCredits(network);
127   UpdateCellularActivating(network);
128 }
129
130 bool NetworkStateNotifier::UpdateDefaultNetwork(const NetworkState* network) {
131   std::string default_network_path;
132   if (network)
133     default_network_path = network->path();
134   if (default_network_path != last_default_network_) {
135     last_default_network_ = default_network_path;
136     return true;
137   }
138   return false;
139 }
140
141 void NetworkStateNotifier::UpdateCellularOutOfCredits(
142     const NetworkState* cellular) {
143   // Only display a notification if we are out of credits and have not already
144   // shown a notification (or have since connected to another network type).
145   if (!cellular->cellular_out_of_credits() || did_show_out_of_credits_)
146     return;
147
148   // Only display a notification if not connected, connecting, or waiting to
149   // connect to another network.
150   NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
151   const NetworkState* default_network = handler->DefaultNetwork();
152   if (default_network && default_network != cellular)
153     return;
154   if (handler->ConnectingNetworkByType(NetworkTypePattern::NonVirtual()) ||
155       NetworkHandler::Get()
156           ->network_connection_handler()
157           ->HasPendingConnectRequest())
158     return;
159
160   did_show_out_of_credits_ = true;
161   base::TimeDelta dtime = base::Time::Now() - out_of_credits_notify_time_;
162   if (dtime.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds) {
163     out_of_credits_notify_time_ = base::Time::Now();
164     base::string16 error_msg = l10n_util::GetStringFUTF16(
165         IDS_NETWORK_OUT_OF_CREDITS_BODY, base::UTF8ToUTF16(cellular->name()));
166     ShowErrorNotification(
167         kNetworkOutOfCreditsNotificationId, cellular->type(),
168         l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_CREDITS_TITLE), error_msg,
169         base::Bind(&NetworkStateNotifier::ShowNetworkSettings,
170                    weak_ptr_factory_.GetWeakPtr(), cellular->path()));
171   }
172 }
173
174 void NetworkStateNotifier::UpdateCellularActivating(
175     const NetworkState* cellular) {
176   // Keep track of any activating cellular network.
177   std::string activation_state = cellular->activation_state();
178   if (activation_state == shill::kActivationStateActivating) {
179     cellular_activating_.insert(cellular->path());
180     return;
181   }
182   // Only display a notification if this network was activating and is now
183   // activated.
184   if (!cellular_activating_.count(cellular->path()) ||
185       activation_state != shill::kActivationStateActivated)
186     return;
187
188   cellular_activating_.erase(cellular->path());
189   int icon_id;
190   if (cellular->network_technology() == shill::kNetworkTechnologyLte)
191     icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_LTE;
192   else
193     icon_id = IDR_AURA_UBER_TRAY_NOTIFICATION_3G;
194   const gfx::Image& icon =
195       ui::ResourceBundle::GetSharedInstance().GetImageNamed(icon_id);
196   message_center::MessageCenter::Get()->AddNotification(
197       message_center::Notification::CreateSystemNotification(
198           kNetworkActivateNotificationId,
199           l10n_util::GetStringUTF16(IDS_NETWORK_CELLULAR_ACTIVATED_TITLE),
200           l10n_util::GetStringFUTF16(IDS_NETWORK_CELLULAR_ACTIVATED,
201                                      base::UTF8ToUTF16((cellular->name()))),
202           icon, kNotifierNetwork,
203           base::Bind(&NetworkStateNotifier::ShowNetworkSettings,
204                      weak_ptr_factory_.GetWeakPtr(), cellular->path())));
205 }
206
207 void NetworkStateNotifier::ShowNetworkConnectError(
208     const std::string& error_name,
209     const std::string& service_path) {
210   if (service_path.empty()) {
211     base::DictionaryValue shill_properties;
212     ShowConnectErrorNotification(error_name, service_path, shill_properties);
213     return;
214   }
215   // Get the up-to-date properties for the network and display the error.
216   NetworkHandler::Get()->network_configuration_handler()->GetProperties(
217       service_path,
218       base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesSucceeded,
219                  weak_ptr_factory_.GetWeakPtr(), error_name),
220       base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesFailed,
221                  weak_ptr_factory_.GetWeakPtr(), error_name, service_path));
222 }
223
224 void NetworkStateNotifier::ShowMobileActivationError(
225     const std::string& service_path) {
226   const NetworkState* cellular =
227       NetworkHandler::Get()->network_state_handler()->GetNetworkState(
228           service_path);
229   if (!cellular || cellular->type() != shill::kTypeCellular) {
230     NET_LOG_ERROR("ShowMobileActivationError without Cellular network",
231                   service_path);
232     return;
233   }
234   message_center::MessageCenter::Get()->AddNotification(
235       message_center::Notification::CreateSystemNotification(
236           kNetworkActivateNotificationId,
237           l10n_util::GetStringUTF16(IDS_NETWORK_ACTIVATION_ERROR_TITLE),
238           l10n_util::GetStringFUTF16(IDS_NETWORK_ACTIVATION_NEEDS_CONNECTION,
239                                      base::UTF8ToUTF16(cellular->name())),
240           ui::ResourceBundle::GetSharedInstance().GetImageNamed(
241               IDR_AURA_UBER_TRAY_NETWORK_FAILED_CELLULAR),
242           kNotifierNetworkError,
243           base::Bind(&NetworkStateNotifier::ShowNetworkSettings,
244                      weak_ptr_factory_.GetWeakPtr(), service_path)));
245 }
246
247 void NetworkStateNotifier::RemoveConnectNotification() {
248   message_center::MessageCenter* message_center =
249       message_center::MessageCenter::Get();
250   if (message_center) {
251     message_center->RemoveNotification(kNetworkConnectNotificationId,
252                                        false /* not by user */);
253   }
254 }
255
256 void NetworkStateNotifier::ConnectErrorPropertiesSucceeded(
257     const std::string& error_name,
258     const std::string& service_path,
259     const base::DictionaryValue& shill_properties) {
260   std::string state;
261   shill_properties.GetStringWithoutPathExpansion(shill::kStateProperty, &state);
262   if (chromeos::NetworkState::StateIsConnected(state) ||
263       chromeos::NetworkState::StateIsConnecting(state)) {
264     // Network is no longer in an error state. This can happen if an
265     // unexpected idle state transition occurs, see crbug.com/333955.
266     return;
267   }
268   ShowConnectErrorNotification(error_name, service_path, shill_properties);
269 }
270
271 void NetworkStateNotifier::ConnectErrorPropertiesFailed(
272     const std::string& error_name,
273     const std::string& service_path,
274     const std::string& shill_connect_error,
275     scoped_ptr<base::DictionaryValue> shill_error_data) {
276   base::DictionaryValue shill_properties;
277   ShowConnectErrorNotification(error_name, service_path, shill_properties);
278 }
279
280 void NetworkStateNotifier::ShowConnectErrorNotification(
281     const std::string& error_name,
282     const std::string& service_path,
283     const base::DictionaryValue& shill_properties) {
284   base::string16 error = GetConnectErrorString(error_name);
285   if (error.empty()) {
286     std::string shill_error;
287     shill_properties.GetStringWithoutPathExpansion(shill::kErrorProperty,
288                                                    &shill_error);
289     if (!chromeos::NetworkState::ErrorIsValid(shill_error)) {
290       shill_properties.GetStringWithoutPathExpansion(
291           shill::kPreviousErrorProperty, &shill_error);
292       NET_LOG_DEBUG("Notify Service.PreviousError: " + shill_error,
293                     service_path);
294       if (!chromeos::NetworkState::ErrorIsValid(shill_error))
295         shill_error.clear();
296     } else {
297       NET_LOG_DEBUG("Notify Service.Error: " + shill_error, service_path);
298     }
299
300     const NetworkState* network =
301         NetworkHandler::Get()->network_state_handler()->GetNetworkState(
302             service_path);
303     if (network) {
304       // Always log last_error, but only use it if shill_error is empty.
305       // TODO(stevenjb): This shouldn't ever be necessary, but is kept here as
306       // a failsafe since more information is better than less when debugging
307       // and we have encountered some strange edge cases before.
308       NET_LOG_DEBUG("Notify Network.last_error: " + network->last_error(),
309                     service_path);
310       if (shill_error.empty())
311         shill_error = network->last_error();
312     }
313
314     if (ShillErrorIsIgnored(shill_error)) {
315       NET_LOG_DEBUG("Notify Ignoring error: " + error_name, service_path);
316       return;
317     }
318
319     error = network_connect_->GetErrorString(shill_error, service_path);
320     if (error.empty())
321       error = l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN);
322   }
323   NET_LOG_ERROR("Notify connect error: " + base::UTF16ToUTF8(error),
324                 service_path);
325
326   std::string network_name =
327       chromeos::shill_property_util::GetNameFromProperties(service_path,
328                                                            shill_properties);
329   std::string network_error_details;
330   shill_properties.GetStringWithoutPathExpansion(shill::kErrorDetailsProperty,
331                                                  &network_error_details);
332
333   base::string16 error_msg;
334   if (!network_error_details.empty()) {
335     // network_name should't be empty if network_error_details is set.
336     error_msg = l10n_util::GetStringFUTF16(
337         IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_SERVER_MESSAGE,
338         base::UTF8ToUTF16(network_name), error,
339         base::UTF8ToUTF16(network_error_details));
340   } else if (network_name.empty()) {
341     error_msg = l10n_util::GetStringFUTF16(
342         IDS_NETWORK_CONNECTION_ERROR_MESSAGE_NO_NAME, error);
343   } else {
344     error_msg =
345         l10n_util::GetStringFUTF16(IDS_NETWORK_CONNECTION_ERROR_MESSAGE,
346                                    base::UTF8ToUTF16(network_name), error);
347   }
348
349   std::string network_type;
350   shill_properties.GetStringWithoutPathExpansion(shill::kTypeProperty,
351                                                  &network_type);
352
353   ShowErrorNotification(
354       kNetworkConnectNotificationId, network_type,
355       l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE), error_msg,
356       base::Bind(&NetworkStateNotifier::ShowNetworkSettings,
357                  weak_ptr_factory_.GetWeakPtr(), service_path));
358 }
359
360 void NetworkStateNotifier::ShowNetworkSettings(
361     const std::string& service_path) {
362   network_connect_->ShowNetworkSettings(service_path);
363 }
364
365 }  // namespace ui