Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/system/chromeos/network/network_state_notifier.h" | 5 #include "ash/system/chromeos/network/network_state_notifier.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "ash/system/chromeos/network/network_connect.h" | 8 #include "ash/system/chromeos/network/network_connect.h" |
| 9 #include "ash/system/chromeos/network/network_observer.h" | 9 #include "ash/system/chromeos/network/network_observer.h" |
| 10 #include "ash/system/tray/system_tray_notifier.h" | 10 #include "ash/system/tray/system_tray_notifier.h" |
| 11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chromeos/network/network_configuration_handler.h" | |
| 14 #include "chromeos/network/network_connection_handler.h" | 15 #include "chromeos/network/network_connection_handler.h" |
| 15 #include "chromeos/network/network_event_log.h" | 16 #include "chromeos/network/network_event_log.h" |
| 16 #include "chromeos/network/network_state.h" | 17 #include "chromeos/network/network_state.h" |
| 17 #include "chromeos/network/network_state_handler.h" | 18 #include "chromeos/network/network_state_handler.h" |
| 18 #include "grit/ash_strings.h" | 19 #include "grit/ash_strings.h" |
| 19 #include "third_party/cros_system_api/dbus/service_constants.h" | 20 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 21 | 22 |
| 22 using chromeos::NetworkConnectionHandler; | 23 using chromeos::NetworkConnectionHandler; |
| 23 using chromeos::NetworkHandler; | 24 using chromeos::NetworkHandler; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 34 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_CONNECT_FAILED); | 35 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_CONNECT_FAILED); |
| 35 if (error_name == NetworkConnectionHandler::kErrorConfigureFailed) | 36 if (error_name == NetworkConnectionHandler::kErrorConfigureFailed) |
| 36 return l10n_util::GetStringUTF16( | 37 return l10n_util::GetStringUTF16( |
| 37 IDS_CHROMEOS_NETWORK_ERROR_CONFIGURE_FAILED); | 38 IDS_CHROMEOS_NETWORK_ERROR_CONFIGURE_FAILED); |
| 38 if (error_name == NetworkConnectionHandler::kErrorActivateFailed) | 39 if (error_name == NetworkConnectionHandler::kErrorActivateFailed) |
| 39 return l10n_util::GetStringUTF16( | 40 return l10n_util::GetStringUTF16( |
| 40 IDS_CHROMEOS_NETWORK_ERROR_ACTIVATION_FAILED); | 41 IDS_CHROMEOS_NETWORK_ERROR_ACTIVATION_FAILED); |
| 41 return string16(); | 42 return string16(); |
| 42 } | 43 } |
| 43 | 44 |
| 45 void ConfigureNetwork(const std::string& service_path) { | |
| 46 // Note: this will trigger the correct UI for out of credit notifications. | |
|
pneubeck (no reviews)
2013/08/20 08:32:26
nit:
if you depend on that behavior, it should be
stevenjb
2013/08/20 20:47:43
Removed this (unused) and updated SystemTrayDelega
| |
| 47 ash::Shell::GetInstance()->system_tray_delegate()->ConfigureNetwork( | |
| 48 service_path); | |
| 49 } | |
| 50 | |
| 44 } // namespace | 51 } // namespace |
| 45 | 52 |
| 46 namespace ash { | 53 namespace ash { |
| 47 | 54 |
| 48 NetworkStateNotifier::NetworkStateNotifier() | 55 NetworkStateNotifier::NetworkStateNotifier() |
| 49 : cellular_out_of_credits_(false) { | 56 : cellular_out_of_credits_(false), |
| 57 weak_ptr_factory_(this) { | |
| 50 if (!NetworkHandler::IsInitialized()) | 58 if (!NetworkHandler::IsInitialized()) |
| 51 return; | 59 return; |
| 52 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); | 60 NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE); |
| 53 | 61 |
| 54 // Initialize |last_active_network_|. | 62 // Initialize |last_active_network_|. |
| 55 const NetworkState* default_network = | 63 const NetworkState* default_network = |
| 56 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); | 64 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); |
| 57 if (default_network && default_network->IsConnectedState()) | 65 if (default_network && default_network->IsConnectedState()) |
| 58 last_active_network_ = default_network->path(); | 66 last_active_network_ = default_network->path(); |
| 59 } | 67 } |
| 60 | 68 |
| 61 NetworkStateNotifier::~NetworkStateNotifier() { | 69 NetworkStateNotifier::~NetworkStateNotifier() { |
| 62 if (!NetworkHandler::IsInitialized()) | 70 if (!NetworkHandler::IsInitialized()) |
| 63 return; | 71 return; |
| 64 NetworkHandler::Get()->network_state_handler()->RemoveObserver( | 72 NetworkHandler::Get()->network_state_handler()->RemoveObserver( |
| 65 this, FROM_HERE); | 73 this, FROM_HERE); |
| 66 } | 74 } |
| 67 | 75 |
| 68 void NetworkStateNotifier::NetworkListChanged() { | |
| 69 // Trigger any pending connect failed error if the network list changes | |
| 70 // (which indicates all NetworkState entries are up to date). This is in | |
| 71 // case a connect attempt fails because a network is no longer visible. | |
| 72 if (!connect_failed_network_.empty()) { | |
| 73 ShowNetworkConnectError( | |
| 74 NetworkConnectionHandler::kErrorConnectFailed, connect_failed_network_); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 void NetworkStateNotifier::DefaultNetworkChanged(const NetworkState* network) { | 76 void NetworkStateNotifier::DefaultNetworkChanged(const NetworkState* network) { |
| 79 if (!network || !network->IsConnectedState()) | 77 if (!network || !network->IsConnectedState()) |
| 80 return; | 78 return; |
| 81 if (network->path() != last_active_network_) { | 79 if (network->path() != last_active_network_) { |
| 82 last_active_network_ = network->path(); | 80 last_active_network_ = network->path(); |
| 83 // Reset state for new connected network | 81 // Reset state for new connected network |
| 84 cellular_out_of_credits_ = false; | 82 cellular_out_of_credits_ = false; |
| 83 NetworkPropertiesUpdated(network); | |
| 85 } | 84 } |
| 86 } | 85 } |
| 87 | 86 |
| 88 void NetworkStateNotifier::NetworkPropertiesUpdated( | 87 void NetworkStateNotifier::NetworkPropertiesUpdated( |
| 89 const NetworkState* network) { | 88 const NetworkState* network) { |
| 90 DCHECK(network); | |
| 91 // Trigger a pending connect failed error for |network| when the Error | |
| 92 // property has been set. | |
| 93 if (network->path() == connect_failed_network_ && !network->error().empty()) { | |
| 94 ShowNetworkConnectError( | |
| 95 NetworkConnectionHandler::kErrorConnectFailed, connect_failed_network_); | |
| 96 } | |
| 97 // Trigger "Out of credits" notification if the cellular network is the most | 89 // Trigger "Out of credits" notification if the cellular network is the most |
| 98 // recent default network (i.e. we have not switched to another network). | 90 // recent default network (i.e. we have not switched to another network). |
| 99 if (network->type() == flimflam::kTypeCellular && | 91 if (cellular_out_of_credits_ || |
| 100 network->path() == last_active_network_) { | 92 network->type() != flimflam::kTypeCellular || |
| 101 cellular_network_ = network->path(); | 93 network->path() != last_active_network_ || |
|
pneubeck (no reviews)
2013/08/20 08:32:26
if you return in this case (network->path() != las
stevenjb
2013/08/20 20:47:43
I greatly simplify this logic in the next CL, I di
| |
| 102 if (network->cellular_out_of_credits() && | 94 !network->cellular_out_of_credits()) |
| 103 !cellular_out_of_credits_) { | 95 return; |
| 104 cellular_out_of_credits_ = true; | 96 cellular_network_ = network->path(); |
| 105 base::TimeDelta dtime = base::Time::Now() - out_of_credits_notify_time_; | 97 cellular_out_of_credits_ = true; |
| 106 if (dtime.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds) { | 98 base::TimeDelta dtime = base::Time::Now() - out_of_credits_notify_time_; |
| 107 out_of_credits_notify_time_ = base::Time::Now(); | 99 if (dtime.InSeconds() > kMinTimeBetweenOutOfCreditsNotifySeconds) { |
| 108 std::vector<string16> links; | 100 out_of_credits_notify_time_ = base::Time::Now(); |
| 109 links.push_back( | 101 std::vector<string16> links; |
| 110 l10n_util::GetStringFUTF16(IDS_NETWORK_OUT_OF_CREDITS_LINK, | 102 links.push_back( |
| 111 UTF8ToUTF16(network->name()))); | 103 l10n_util::GetStringFUTF16(IDS_NETWORK_OUT_OF_CREDITS_LINK, |
| 112 ash::Shell::GetInstance()->system_tray_notifier()-> | 104 UTF8ToUTF16(network->name()))); |
| 113 NotifySetNetworkMessage( | 105 ash::Shell::GetInstance()->system_tray_notifier()-> |
| 114 this, | 106 NotifySetNetworkMessage( |
| 115 NetworkObserver::ERROR_OUT_OF_CREDITS, | 107 this, |
| 116 NetworkObserver::GetNetworkTypeForNetworkState(network), | 108 NetworkObserver::ERROR_OUT_OF_CREDITS, |
| 117 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_CREDITS_TITLE), | 109 NetworkObserver::GetNetworkTypeForNetworkState(network), |
| 118 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_CREDITS_BODY), | 110 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_CREDITS_TITLE), |
| 119 links); | 111 l10n_util::GetStringUTF16(IDS_NETWORK_OUT_OF_CREDITS_BODY), |
| 120 } | 112 links); |
| 121 } | |
| 122 } | 113 } |
| 123 } | 114 } |
| 124 | 115 |
| 125 void NetworkStateNotifier::NotificationLinkClicked( | 116 void NetworkStateNotifier::NotificationLinkClicked( |
| 126 NetworkObserver::MessageType message_type, | 117 NetworkObserver::MessageType message_type, |
| 127 size_t link_index) { | 118 size_t link_index) { |
| 128 if (message_type == NetworkObserver::ERROR_OUT_OF_CREDITS) { | 119 if (message_type == NetworkObserver::ERROR_OUT_OF_CREDITS) { |
| 129 if (!cellular_network_.empty()) { | 120 if (!cellular_network_.empty()) { |
| 130 // This will trigger the activation / portal code. | 121 // This will trigger the activation / portal code. |
| 131 Shell::GetInstance()->system_tray_delegate()->ConfigureNetwork( | 122 Shell::GetInstance()->system_tray_delegate()->ConfigureNetwork( |
| 132 cellular_network_); | 123 cellular_network_); |
| 133 } | 124 } |
| 134 ash::Shell::GetInstance()->system_tray_notifier()-> | 125 ash::Shell::GetInstance()->system_tray_notifier()-> |
| 135 NotifyClearNetworkMessage(message_type); | 126 NotifyClearNetworkMessage(message_type); |
| 136 } | 127 } |
| 137 } | 128 } |
| 138 | 129 |
| 139 void NetworkStateNotifier::ShowNetworkConnectError( | 130 void NetworkStateNotifier::ShowNetworkConnectError( |
| 140 const std::string& error_name, | 131 const std::string& error_name, |
| 141 const std::string& service_path) { | 132 const std::string& service_path) { |
| 142 const NetworkState* network = NetworkHandler::Get()->network_state_handler()-> | 133 // Get the up-to-date properties for the network and display the error. |
| 143 GetNetworkState(service_path); | 134 NetworkHandler::Get()->network_configuration_handler()->GetProperties( |
| 144 if (error_name == NetworkConnectionHandler::kErrorConnectFailed && | 135 service_path, |
| 145 service_path != connect_failed_network_) { | 136 base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesSucceeded, |
| 146 // Shill may not have set the Error property yet. First request an update | 137 weak_ptr_factory_.GetWeakPtr(), error_name), |
| 147 // and wait for either the update to complete or the network list to be | 138 base::Bind(&NetworkStateNotifier::ConnectErrorPropertiesFailed, |
| 148 // updated before displaying the error. | 139 weak_ptr_factory_.GetWeakPtr(), error_name, service_path)); |
| 149 connect_failed_network_ = service_path; | 140 } |
| 150 return; | 141 |
| 142 void NetworkStateNotifier::ConnectErrorPropertiesSucceeded( | |
| 143 const std::string& error_name, | |
| 144 const std::string& service_path, | |
| 145 const base::DictionaryValue& shill_properties) { | |
| 146 ShowConnectErrorNotification(error_name, service_path, shill_properties); | |
| 147 } | |
| 148 | |
| 149 void NetworkStateNotifier::ConnectErrorPropertiesFailed( | |
| 150 const std::string& error_name, | |
| 151 const std::string& service_path, | |
| 152 const std::string& shill_error_name, | |
| 153 scoped_ptr<base::DictionaryValue> shill_error_data) { | |
| 154 base::DictionaryValue shill_properties; | |
| 155 ShowConnectErrorNotification(error_name, service_path, shill_properties); | |
| 156 } | |
| 157 | |
| 158 void NetworkStateNotifier::ShowConnectErrorNotification( | |
| 159 const std::string& error_name, | |
| 160 const std::string& service_path, | |
| 161 const base::DictionaryValue& shill_properties) { | |
| 162 string16 error = GetConnectErrorString(error_name); | |
| 163 if (error.empty()) { | |
| 164 std::string network_error; | |
| 165 shill_properties.GetStringWithoutPathExpansion( | |
| 166 flimflam::kErrorProperty, &network_error); | |
| 167 error = network_connect::ErrorString(network_error); | |
| 168 if (error.empty()) | |
| 169 error = l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN); | |
| 151 } | 170 } |
| 152 connect_failed_network_.clear(); | |
| 153 | |
| 154 string16 error = GetConnectErrorString(error_name); | |
| 155 if (error.empty() && network) | |
| 156 error = network_connect::ErrorString(network->error()); | |
| 157 if (error.empty()) | |
| 158 error = l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN); | |
| 159 NET_LOG_ERROR("Connect error notification: " + UTF16ToUTF8(error), | 171 NET_LOG_ERROR("Connect error notification: " + UTF16ToUTF8(error), |
| 160 service_path); | 172 service_path); |
| 161 | 173 |
| 162 std::string name = network ? network->name() : ""; | 174 std::string network_name = |
| 175 NetworkState::GetNameFromProperties(service_path, shill_properties); | |
|
pneubeck (no reviews)
2013/08/20 08:32:26
GetNameFromProperties should explicitly handle shi
stevenjb
2013/08/20 20:47:43
Done.
| |
| 176 std::string network_error_details; | |
| 177 shill_properties.GetStringWithoutPathExpansion( | |
| 178 shill::kErrorDetailsProperty, &network_error_details); | |
| 179 | |
| 163 string16 error_msg; | 180 string16 error_msg; |
|
pneubeck (no reviews)
2013/08/20 08:32:26
network_name will be empty here if shill_propertie
stevenjb
2013/08/20 20:47:43
Yes, that logic hasn't changed; I will address tha
| |
| 164 if (network && !network->error_details().empty()) { | 181 if (!network_error_details.empty()) { |
| 165 error_msg = l10n_util::GetStringFUTF16( | 182 error_msg = l10n_util::GetStringFUTF16( |
| 166 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_SERVER_MESSAGE, | 183 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_SERVER_MESSAGE, |
| 167 UTF8ToUTF16(name), error, UTF8ToUTF16(network->error_details())); | 184 UTF8ToUTF16(network_name), error, |
| 185 UTF8ToUTF16(network_error_details)); | |
| 168 } else { | 186 } else { |
| 169 error_msg = l10n_util::GetStringFUTF16( | 187 error_msg = l10n_util::GetStringFUTF16( |
| 170 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS, | 188 IDS_NETWORK_CONNECTION_ERROR_MESSAGE_WITH_DETAILS, |
|
pneubeck (no reviews)
2013/08/20 08:32:26
nit:
the names of the IDS_* constants are confusin
stevenjb
2013/08/20 20:47:43
I will also address that in the next CL.
| |
| 171 UTF8ToUTF16(name), error); | 189 UTF8ToUTF16(network_name), error); |
| 190 } | |
| 191 | |
| 192 std::string network_type; | |
| 193 shill_properties.GetStringWithoutPathExpansion( | |
| 194 flimflam::kTypeProperty, &network_type); | |
| 195 | |
| 196 NetworkObserver::NetworkType type = NetworkObserver::NETWORK_UNKNOWN; | |
|
pneubeck (no reviews)
2013/08/20 08:32:26
this duplicates the type mapping.
could you instea
stevenjb
2013/08/20 20:47:43
This is temporary and going away soon.
| |
| 197 if (network_type == flimflam::kTypeCellular) { | |
| 198 std::string network_technology; | |
| 199 shill_properties.GetStringWithoutPathExpansion( | |
| 200 flimflam::kNetworkTechnologyProperty, &network_technology); | |
| 201 if (network_technology == flimflam::kNetworkTechnologyLte || | |
| 202 network_technology == flimflam::kNetworkTechnologyLteAdvanced) | |
| 203 type = NetworkObserver::NETWORK_CELLULAR_LTE; | |
| 204 else | |
| 205 type = NetworkObserver::NETWORK_CELLULAR; | |
| 206 } else if (network_type == flimflam::kTypeEthernet) { | |
| 207 type = NetworkObserver:: NETWORK_ETHERNET; | |
| 208 } else if (network_type == flimflam::kTypeWifi) { | |
| 209 type = NetworkObserver:: NETWORK_WIFI; | |
| 210 } else { | |
| 211 NOTREACHED(); | |
| 172 } | 212 } |
| 173 | 213 |
| 174 std::vector<string16> no_links; | 214 std::vector<string16> no_links; |
| 175 ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage( | 215 ash::Shell::GetInstance()->system_tray_notifier()->NotifySetNetworkMessage( |
| 176 this, | 216 this, |
| 177 NetworkObserver::ERROR_CONNECT_FAILED, | 217 NetworkObserver::ERROR_CONNECT_FAILED, |
| 178 NetworkObserver::GetNetworkTypeForNetworkState(network), | 218 type, |
| 179 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE), | 219 l10n_util::GetStringUTF16(IDS_NETWORK_CONNECTION_ERROR_TITLE), |
| 180 error_msg, | 220 error_msg, |
| 181 no_links); | 221 no_links); |
| 182 } | 222 } |
| 183 | 223 |
| 184 } // namespace ash | 224 } // namespace ash |
| OLD | NEW |