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