OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_connect.h" | 5 #include "ash/system/chromeos/network/network_connect.h" |
6 | 6 |
7 #include "ash/session/session_state_delegate.h" | |
8 #include "ash/shell.h" | |
9 #include "ash/system/chromeos/network/network_state_notifier.h" | 7 #include "ash/system/chromeos/network/network_state_notifier.h" |
armansito
2014/10/24 20:02:21
So I guess you left this here since NetworkStateNo
stevenjb
2014/10/24 23:01:50
Correct. They are both moving and NetworkConnect w
| |
10 #include "ash/system/system_notifier.h" | |
11 #include "ash/system/tray/system_tray_delegate.h" | |
12 #include "ash/system/tray/system_tray_notifier.h" | |
13 #include "base/bind.h" | 8 #include "base/bind.h" |
14 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
15 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
17 #include "base/values.h" | 12 #include "base/values.h" |
18 #include "chromeos/login/login_state.h" | 13 #include "chromeos/login/login_state.h" |
19 #include "chromeos/network/device_state.h" | 14 #include "chromeos/network/device_state.h" |
20 #include "chromeos/network/network_activation_handler.h" | 15 #include "chromeos/network/network_activation_handler.h" |
21 #include "chromeos/network/network_configuration_handler.h" | 16 #include "chromeos/network/network_configuration_handler.h" |
22 #include "chromeos/network/network_connection_handler.h" | 17 #include "chromeos/network/network_connection_handler.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
41 using chromeos::NetworkProfile; | 36 using chromeos::NetworkProfile; |
42 using chromeos::NetworkProfileHandler; | 37 using chromeos::NetworkProfileHandler; |
43 using chromeos::NetworkState; | 38 using chromeos::NetworkState; |
44 using chromeos::NetworkStateHandler; | 39 using chromeos::NetworkStateHandler; |
45 using chromeos::NetworkTypePattern; | 40 using chromeos::NetworkTypePattern; |
46 | 41 |
47 namespace ash { | 42 namespace ash { |
48 | 43 |
49 namespace { | 44 namespace { |
50 | 45 |
51 // TODO(stevenjb): This should be in service_constants.h | |
52 const char kErrorInProgress[] = "org.chromium.flimflam.Error.InProgress"; | |
53 | |
54 // Returns true for carriers that can be activated through Shill instead of | 46 // Returns true for carriers that can be activated through Shill instead of |
55 // through a WebUI dialog. | 47 // through a WebUI dialog. |
56 bool IsDirectActivatedCarrier(const std::string& carrier) { | 48 bool IsDirectActivatedCarrier(const std::string& carrier) { |
57 if (carrier == shill::kCarrierSprint) | 49 if (carrier == shill::kCarrierSprint) |
58 return true; | 50 return true; |
59 return false; | 51 return false; |
60 } | 52 } |
61 | 53 |
62 void ShowErrorNotification(const std::string& error_name, | 54 const NetworkState* GetNetworkState(const std::string& service_path) { |
63 const std::string& service_path) { | 55 return NetworkHandler::Get()->network_state_handler()->GetNetworkState( |
64 Shell::GetInstance()->system_tray_notifier()->network_state_notifier()-> | 56 service_path); |
65 ShowNetworkConnectError(error_name, service_path); | |
66 } | 57 } |
67 | 58 |
68 void HandleUnconfiguredNetwork(const std::string& service_path) { | 59 class NetworkConnectImpl : public NetworkConnect { |
69 const NetworkState* network = NetworkHandler::Get()->network_state_handler()-> | 60 public: |
70 GetNetworkState(service_path); | 61 explicit NetworkConnectImpl(Delegate* delegate); |
62 ~NetworkConnectImpl() override; | |
63 | |
64 // NetworkConnect | |
65 void ConnectToNetwork(const std::string& service_path) override; | |
66 void SetTechnologyEnabled(const chromeos::NetworkTypePattern& technology, | |
67 bool enabled_state) override; | |
68 void ActivateCellular(const std::string& service_path) override; | |
69 void ShowMobileSetup(const std::string& service_path) override; | |
70 void ConfigureNetworkAndConnect(const std::string& service_path, | |
71 const base::DictionaryValue& properties, | |
72 bool shared) override; | |
73 void CreateConfigurationAndConnect(base::DictionaryValue* properties, | |
74 bool shared) override; | |
75 void CreateConfiguration(base::DictionaryValue* properties, | |
76 bool shared) override; | |
77 base::string16 GetErrorString(const std::string& error, | |
78 const std::string& service_path) override; | |
79 void ShowNetworkSettings(const std::string& service_path) override; | |
80 | |
81 private: | |
82 void HandleUnconfiguredNetwork(const std::string& service_path); | |
83 void OnConnectFailed(const std::string& service_path, | |
84 const std::string& error_name, | |
85 scoped_ptr<base::DictionaryValue> error_data); | |
86 bool GetNetworkProfilePath(bool shared, std::string* profile_path); | |
87 void OnConnectSucceeded(const std::string& service_path); | |
88 void CallConnectToNetwork(const std::string& service_path, | |
89 bool check_error_state); | |
90 void OnActivateFailed(const std::string& service_path, | |
91 const std::string& error_name, | |
92 scoped_ptr<base::DictionaryValue> error_data); | |
93 void OnActivateSucceeded(const std::string& service_path); | |
94 void OnConfigureFailed(const std::string& error_name, | |
95 scoped_ptr<base::DictionaryValue> error_data); | |
96 void OnConfigureSucceeded(bool connect_on_configure, | |
97 const std::string& service_path); | |
98 void CallCreateConfiguration(base::DictionaryValue* properties, | |
99 bool shared, | |
100 bool connect_on_configure); | |
101 void SetPropertiesFailed(const std::string& desc, | |
102 const std::string& service_path, | |
103 const std::string& config_error_name, | |
104 scoped_ptr<base::DictionaryValue> error_data); | |
105 void SetPropertiesToClear(base::DictionaryValue* properties_to_set, | |
106 std::vector<std::string>* properties_to_clear); | |
107 void ClearPropertiesAndConnect( | |
108 const std::string& service_path, | |
109 const std::vector<std::string>& properties_to_clear); | |
110 void ConfigureSetProfileSucceeded( | |
111 const std::string& service_path, | |
112 scoped_ptr<base::DictionaryValue> properties_to_set); | |
113 | |
114 Delegate* delegate_; | |
115 scoped_ptr<NetworkStateNotifier> network_state_notifier_; | |
116 base::WeakPtrFactory<NetworkConnectImpl> weak_factory_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(NetworkConnectImpl); | |
119 }; | |
120 | |
121 NetworkConnectImpl::NetworkConnectImpl(Delegate* delegate) | |
122 : delegate_(delegate), weak_factory_(this) { | |
123 network_state_notifier_.reset(new NetworkStateNotifier(this)); | |
124 } | |
125 | |
126 NetworkConnectImpl::~NetworkConnectImpl() { | |
127 } | |
128 | |
129 void NetworkConnectImpl::HandleUnconfiguredNetwork( | |
130 const std::string& service_path) { | |
131 const NetworkState* network = GetNetworkState(service_path); | |
71 if (!network) { | 132 if (!network) { |
72 NET_LOG_ERROR("Configuring unknown network", service_path); | 133 NET_LOG_ERROR("Configuring unknown network", service_path); |
73 return; | 134 return; |
74 } | 135 } |
75 | 136 |
76 if (network->type() == shill::kTypeWifi) { | 137 if (network->type() == shill::kTypeWifi) { |
77 // Only show the config view for secure networks, otherwise do nothing. | 138 // Only show the config view for secure networks, otherwise do nothing. |
78 if (network->security() != shill::kSecurityNone) { | 139 if (network->security() != shill::kSecurityNone) { |
79 ash::Shell::GetInstance()->system_tray_delegate()-> | 140 delegate_->ShowNetworkConfigure(service_path); |
80 ShowNetworkConfigure(service_path); | |
81 } | 141 } |
82 return; | 142 return; |
83 } | 143 } |
84 | 144 |
85 if (network->type() == shill::kTypeWimax || | 145 if (network->type() == shill::kTypeWimax || |
86 network->type() == shill::kTypeVPN) { | 146 network->type() == shill::kTypeVPN) { |
87 ash::Shell::GetInstance()->system_tray_delegate()-> | 147 delegate_->ShowNetworkConfigure(service_path); |
88 ShowNetworkConfigure(service_path); | |
89 return; | 148 return; |
90 } | 149 } |
91 | 150 |
92 if (network->type() == shill::kTypeCellular) { | 151 if (network->type() == shill::kTypeCellular) { |
93 if (network->RequiresActivation()) { | 152 if (network->RequiresActivation()) { |
94 ash::network_connect::ActivateCellular(service_path); | 153 ActivateCellular(service_path); |
95 return; | 154 return; |
96 } | 155 } |
97 if (network->cellular_out_of_credits()) { | 156 if (network->cellular_out_of_credits()) { |
98 ash::network_connect::ShowMobileSetup(service_path); | 157 ShowMobileSetup(service_path); |
99 return; | 158 return; |
100 } | 159 } |
101 // No special configure or setup for |network|, show the settings UI. | 160 // No special configure or setup for |network|, show the settings UI. |
102 if (chromeos::LoginState::Get()->IsUserLoggedIn()) { | 161 if (chromeos::LoginState::Get()->IsUserLoggedIn()) { |
103 ash::Shell::GetInstance()->system_tray_delegate()-> | 162 delegate_->ShowNetworkSettings(service_path); |
104 ShowNetworkSettings(service_path); | |
105 } | 163 } |
106 return; | 164 return; |
107 } | 165 } |
108 NOTREACHED(); | 166 NOTREACHED(); |
109 } | 167 } |
110 | 168 |
111 // If |shared| is true, sets |profile_path| to the shared profile path. | 169 // If |shared| is true, sets |profile_path| to the shared profile path. |
112 // Otherwise sets |profile_path| to the user profile path if authenticated and | 170 // Otherwise sets |profile_path| to the user profile path if authenticated and |
113 // available. Returns 'false' if unable to set |profile_path|. | 171 // available. Returns 'false' if unable to set |profile_path|. |
114 bool GetNetworkProfilePath(bool shared, std::string* profile_path) { | 172 bool NetworkConnectImpl::GetNetworkProfilePath(bool shared, |
173 std::string* profile_path) { | |
115 if (shared) { | 174 if (shared) { |
116 *profile_path = NetworkProfileHandler::GetSharedProfilePath(); | 175 *profile_path = NetworkProfileHandler::GetSharedProfilePath(); |
117 return true; | 176 return true; |
118 } | 177 } |
119 | 178 |
120 if (!chromeos::LoginState::Get()->UserHasNetworkProfile()) { | 179 if (!chromeos::LoginState::Get()->UserHasNetworkProfile()) { |
121 NET_LOG_ERROR("User profile specified before login", ""); | 180 NET_LOG_ERROR("User profile specified before login", ""); |
122 return false; | 181 return false; |
123 } | 182 } |
124 | 183 |
125 const NetworkProfile* profile = | 184 const NetworkProfile* profile = |
126 NetworkHandler::Get()->network_profile_handler()-> | 185 NetworkHandler::Get()->network_profile_handler()->GetDefaultUserProfile(); |
127 GetDefaultUserProfile(); | |
128 if (!profile) { | 186 if (!profile) { |
129 NET_LOG_ERROR("No user profile for unshared network configuration", ""); | 187 NET_LOG_ERROR("No user profile for unshared network configuration", ""); |
130 return false; | 188 return false; |
131 } | 189 } |
132 | 190 |
133 *profile_path = profile->path; | 191 *profile_path = profile->path; |
134 return true; | 192 return true; |
135 } | 193 } |
136 | 194 |
137 void OnConnectFailed(const std::string& service_path, | 195 void NetworkConnectImpl::OnConnectFailed( |
138 const std::string& error_name, | 196 const std::string& service_path, |
139 scoped_ptr<base::DictionaryValue> error_data) { | 197 const std::string& error_name, |
198 scoped_ptr<base::DictionaryValue> error_data) { | |
140 NET_LOG_ERROR("Connect Failed: " + error_name, service_path); | 199 NET_LOG_ERROR("Connect Failed: " + error_name, service_path); |
141 | 200 |
142 if (!ash::Shell::HasInstance()) | 201 // If a new connect attempt canceled this connect, no need to notify the |
143 return; | 202 // user. |
144 | |
145 // If a new connect attempt canceled this connect, no need to notify the user. | |
146 if (error_name == NetworkConnectionHandler::kErrorConnectCanceled) | 203 if (error_name == NetworkConnectionHandler::kErrorConnectCanceled) |
147 return; | 204 return; |
148 | 205 |
149 if (error_name == shill::kErrorBadPassphrase || | 206 if (error_name == shill::kErrorBadPassphrase || |
150 error_name == NetworkConnectionHandler::kErrorPassphraseRequired || | 207 error_name == NetworkConnectionHandler::kErrorPassphraseRequired || |
151 error_name == NetworkConnectionHandler::kErrorConfigurationRequired || | 208 error_name == NetworkConnectionHandler::kErrorConfigurationRequired || |
152 error_name == NetworkConnectionHandler::kErrorAuthenticationRequired) { | 209 error_name == NetworkConnectionHandler::kErrorAuthenticationRequired) { |
153 HandleUnconfiguredNetwork(service_path); | 210 HandleUnconfiguredNetwork(service_path); |
154 return; | 211 return; |
155 } | 212 } |
156 | 213 |
157 if (error_name == NetworkConnectionHandler::kErrorCertificateRequired) { | 214 if (error_name == NetworkConnectionHandler::kErrorCertificateRequired) { |
158 if (!ash::Shell::GetInstance()->system_tray_delegate()->EnrollNetwork( | 215 if (!delegate_->ShowEnrollNetwork(service_path)) { |
159 service_path)) { | |
160 HandleUnconfiguredNetwork(service_path); | 216 HandleUnconfiguredNetwork(service_path); |
161 } | 217 } |
162 return; | 218 return; |
163 } | 219 } |
164 | 220 |
165 if (error_name == NetworkConnectionHandler::kErrorActivationRequired) { | 221 if (error_name == NetworkConnectionHandler::kErrorActivationRequired) { |
166 network_connect::ActivateCellular(service_path); | 222 ActivateCellular(service_path); |
167 return; | 223 return; |
168 } | 224 } |
169 | 225 |
170 if (error_name == NetworkConnectionHandler::kErrorConnected || | 226 if (error_name == NetworkConnectionHandler::kErrorConnected || |
171 error_name == NetworkConnectionHandler::kErrorConnecting) { | 227 error_name == NetworkConnectionHandler::kErrorConnecting) { |
172 network_connect::ShowNetworkSettings(service_path); | 228 ShowNetworkSettings(service_path); |
173 return; | 229 return; |
174 } | 230 } |
175 | 231 |
176 // ConnectFailed or unknown error; show a notification. | 232 // ConnectFailed or unknown error; show a notification. |
177 ShowErrorNotification(error_name, service_path); | 233 network_state_notifier_->ShowNetworkConnectError(error_name, service_path); |
178 | 234 |
179 // Only show a configure dialog if there was a ConnectFailed error and the | 235 // Only show a configure dialog if there was a ConnectFailed error. |
180 // screen is not locked. | 236 if (error_name != shill::kErrorConnectFailed) |
181 if (error_name != shill::kErrorConnectFailed || | |
182 Shell::GetInstance()->session_state_delegate()->IsScreenLocked()) | |
183 return; | 237 return; |
184 | 238 |
185 // If Shill reports an InProgress error, don't try to configure the network. | 239 // If Shill reports an InProgress error, don't try to configure the network. |
186 std::string dbus_error_name; | 240 std::string dbus_error_name; |
187 error_data.get()->GetString( | 241 error_data.get()->GetString(chromeos::network_handler::kDbusErrorName, |
188 chromeos::network_handler::kDbusErrorName, &dbus_error_name); | 242 &dbus_error_name); |
189 if (dbus_error_name == kErrorInProgress) | 243 if (dbus_error_name == shill::kErrorResultInProgress) |
190 return; | 244 return; |
191 | 245 |
192 HandleUnconfiguredNetwork(service_path); | 246 HandleUnconfiguredNetwork(service_path); |
193 } | 247 } |
194 | 248 |
195 void OnConnectSucceeded(const std::string& service_path) { | 249 void NetworkConnectImpl::OnConnectSucceeded(const std::string& service_path) { |
196 NET_LOG_USER("Connect Succeeded", service_path); | 250 NET_LOG_USER("Connect Succeeded", service_path); |
197 if (!ash::Shell::HasInstance()) | 251 network_state_notifier_->RemoveConnectNotification(); |
198 return; | |
199 message_center::MessageCenter::Get()->RemoveNotification( | |
200 network_connect::kNetworkConnectNotificationId, false /* not by user */); | |
201 } | 252 } |
202 | 253 |
203 // If |check_error_state| is true, error state for the network is checked, | 254 // If |check_error_state| is true, error state for the network is checked, |
204 // otherwise any current error state is ignored (e.g. for recently configured | 255 // otherwise any current error state is ignored (e.g. for recently configured |
205 // networks or repeat connect attempts). | 256 // networks or repeat connect attempts). |
206 void CallConnectToNetwork(const std::string& service_path, | 257 void NetworkConnectImpl::CallConnectToNetwork(const std::string& service_path, |
207 bool check_error_state) { | 258 bool check_error_state) { |
208 if (!ash::Shell::HasInstance()) | 259 network_state_notifier_->RemoveConnectNotification(); |
209 return; | |
210 message_center::MessageCenter::Get()->RemoveNotification( | |
211 network_connect::kNetworkConnectNotificationId, false /* not by user */); | |
212 | |
213 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork( | 260 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork( |
214 service_path, | 261 service_path, base::Bind(&NetworkConnectImpl::OnConnectSucceeded, |
215 base::Bind(&OnConnectSucceeded, service_path), | 262 weak_factory_.GetWeakPtr(), service_path), |
216 base::Bind(&OnConnectFailed, service_path), | 263 base::Bind(&NetworkConnectImpl::OnConnectFailed, |
264 weak_factory_.GetWeakPtr(), service_path), | |
217 check_error_state); | 265 check_error_state); |
218 } | 266 } |
219 | 267 |
220 void OnActivateFailed(const std::string& service_path, | 268 void NetworkConnectImpl::OnActivateFailed( |
221 const std::string& error_name, | 269 const std::string& service_path, |
222 scoped_ptr<base::DictionaryValue> error_data) { | 270 const std::string& error_name, |
271 scoped_ptr<base::DictionaryValue> error_data) { | |
223 NET_LOG_ERROR("Unable to activate network", service_path); | 272 NET_LOG_ERROR("Unable to activate network", service_path); |
224 ShowErrorNotification(network_connect::kErrorActivateFailed, service_path); | 273 network_state_notifier_->ShowNetworkConnectError(kErrorActivateFailed, |
274 service_path); | |
225 } | 275 } |
226 | 276 |
227 void OnActivateSucceeded(const std::string& service_path) { | 277 void NetworkConnectImpl::OnActivateSucceeded(const std::string& service_path) { |
228 NET_LOG_USER("Activation Succeeded", service_path); | 278 NET_LOG_USER("Activation Succeeded", service_path); |
229 } | 279 } |
230 | 280 |
231 void OnConfigureFailed(const std::string& error_name, | 281 void NetworkConnectImpl::OnConfigureFailed( |
232 scoped_ptr<base::DictionaryValue> error_data) { | 282 const std::string& error_name, |
283 scoped_ptr<base::DictionaryValue> error_data) { | |
233 NET_LOG_ERROR("Unable to configure network", ""); | 284 NET_LOG_ERROR("Unable to configure network", ""); |
234 ShowErrorNotification(NetworkConnectionHandler::kErrorConfigureFailed, ""); | 285 network_state_notifier_->ShowNetworkConnectError( |
286 NetworkConnectionHandler::kErrorConfigureFailed, ""); | |
235 } | 287 } |
236 | 288 |
237 void OnConfigureSucceeded(bool connect_on_configure, | 289 void NetworkConnectImpl::OnConfigureSucceeded(bool connect_on_configure, |
238 const std::string& service_path) { | 290 const std::string& service_path) { |
239 NET_LOG_USER("Configure Succeeded", service_path); | 291 NET_LOG_USER("Configure Succeeded", service_path); |
240 if (!connect_on_configure) | 292 if (!connect_on_configure) |
241 return; | 293 return; |
242 // After configuring a network, ignore any (possibly stale) error state. | 294 // After configuring a network, ignore any (possibly stale) error state. |
243 const bool check_error_state = false; | 295 const bool check_error_state = false; |
244 CallConnectToNetwork(service_path, check_error_state); | 296 CallConnectToNetwork(service_path, check_error_state); |
245 } | 297 } |
246 | 298 |
247 void CallCreateConfiguration(base::DictionaryValue* properties, | 299 void NetworkConnectImpl::CallCreateConfiguration( |
248 bool shared, | 300 base::DictionaryValue* properties, |
249 bool connect_on_configure) { | 301 bool shared, |
302 bool connect_on_configure) { | |
250 std::string profile_path; | 303 std::string profile_path; |
251 if (!GetNetworkProfilePath(shared, &profile_path)) { | 304 if (!GetNetworkProfilePath(shared, &profile_path)) { |
252 ShowErrorNotification(NetworkConnectionHandler::kErrorConfigureFailed, ""); | 305 network_state_notifier_->ShowNetworkConnectError( |
306 NetworkConnectionHandler::kErrorConfigureFailed, ""); | |
253 return; | 307 return; |
254 } | 308 } |
255 properties->SetStringWithoutPathExpansion( | 309 properties->SetStringWithoutPathExpansion(shill::kProfileProperty, |
256 shill::kProfileProperty, profile_path); | 310 profile_path); |
257 NetworkHandler::Get()->network_configuration_handler()->CreateConfiguration( | 311 NetworkHandler::Get()->network_configuration_handler()->CreateConfiguration( |
258 *properties, | 312 *properties, base::Bind(&NetworkConnectImpl::OnConfigureSucceeded, |
259 base::Bind(&OnConfigureSucceeded, connect_on_configure), | 313 weak_factory_.GetWeakPtr(), connect_on_configure), |
260 base::Bind(&OnConfigureFailed)); | 314 base::Bind(&NetworkConnectImpl::OnConfigureFailed, |
315 weak_factory_.GetWeakPtr())); | |
261 } | 316 } |
262 | 317 |
263 void SetPropertiesFailed(const std::string& desc, | 318 void NetworkConnectImpl::SetPropertiesFailed( |
264 const std::string& service_path, | 319 const std::string& desc, |
265 const std::string& config_error_name, | 320 const std::string& service_path, |
266 scoped_ptr<base::DictionaryValue> error_data) { | 321 const std::string& config_error_name, |
322 scoped_ptr<base::DictionaryValue> error_data) { | |
267 NET_LOG_ERROR(desc + ": Failed: " + config_error_name, service_path); | 323 NET_LOG_ERROR(desc + ": Failed: " + config_error_name, service_path); |
268 ShowErrorNotification( | 324 network_state_notifier_->ShowNetworkConnectError( |
269 NetworkConnectionHandler::kErrorConfigureFailed, service_path); | 325 NetworkConnectionHandler::kErrorConfigureFailed, service_path); |
270 } | 326 } |
271 | 327 |
272 void SetPropertiesToClear(base::DictionaryValue* properties_to_set, | 328 void NetworkConnectImpl::SetPropertiesToClear( |
273 std::vector<std::string>* properties_to_clear) { | 329 base::DictionaryValue* properties_to_set, |
330 std::vector<std::string>* properties_to_clear) { | |
274 // Move empty string properties to properties_to_clear. | 331 // Move empty string properties to properties_to_clear. |
275 for (base::DictionaryValue::Iterator iter(*properties_to_set); | 332 for (base::DictionaryValue::Iterator iter(*properties_to_set); |
276 !iter.IsAtEnd(); iter.Advance()) { | 333 !iter.IsAtEnd(); iter.Advance()) { |
277 std::string value_str; | 334 std::string value_str; |
278 if (iter.value().GetAsString(&value_str) && value_str.empty()) | 335 if (iter.value().GetAsString(&value_str) && value_str.empty()) |
279 properties_to_clear->push_back(iter.key()); | 336 properties_to_clear->push_back(iter.key()); |
280 } | 337 } |
281 // Remove cleared properties from properties_to_set. | 338 // Remove cleared properties from properties_to_set. |
282 for (std::vector<std::string>::iterator iter = properties_to_clear->begin(); | 339 for (std::vector<std::string>::iterator iter = properties_to_clear->begin(); |
283 iter != properties_to_clear->end(); ++iter) { | 340 iter != properties_to_clear->end(); ++iter) { |
284 properties_to_set->RemoveWithoutPathExpansion(*iter, NULL); | 341 properties_to_set->RemoveWithoutPathExpansion(*iter, NULL); |
285 } | 342 } |
286 } | 343 } |
287 | 344 |
288 void ClearPropertiesAndConnect( | 345 void NetworkConnectImpl::ClearPropertiesAndConnect( |
289 const std::string& service_path, | 346 const std::string& service_path, |
290 const std::vector<std::string>& properties_to_clear) { | 347 const std::vector<std::string>& properties_to_clear) { |
291 NET_LOG_USER("ClearPropertiesAndConnect", service_path); | 348 NET_LOG_USER("ClearPropertiesAndConnect", service_path); |
292 // After configuring a network, ignore any (possibly stale) error state. | 349 // After configuring a network, ignore any (possibly stale) error state. |
293 const bool check_error_state = false; | 350 const bool check_error_state = false; |
294 NetworkHandler::Get()->network_configuration_handler()->ClearProperties( | 351 NetworkHandler::Get()->network_configuration_handler()->ClearProperties( |
295 service_path, | 352 service_path, properties_to_clear, |
296 properties_to_clear, | 353 base::Bind(&NetworkConnectImpl::CallConnectToNetwork, |
297 base::Bind(&CallConnectToNetwork, | 354 weak_factory_.GetWeakPtr(), service_path, check_error_state), |
298 service_path, check_error_state), | 355 base::Bind(&NetworkConnectImpl::SetPropertiesFailed, |
299 base::Bind(&SetPropertiesFailed, "ClearProperties", service_path)); | 356 weak_factory_.GetWeakPtr(), "ClearProperties", service_path)); |
300 } | 357 } |
301 | 358 |
302 void ConfigureSetProfileSucceeded( | 359 void NetworkConnectImpl::ConfigureSetProfileSucceeded( |
303 const std::string& service_path, | 360 const std::string& service_path, |
304 scoped_ptr<base::DictionaryValue> properties_to_set) { | 361 scoped_ptr<base::DictionaryValue> properties_to_set) { |
305 std::vector<std::string> properties_to_clear; | 362 std::vector<std::string> properties_to_clear; |
306 SetPropertiesToClear(properties_to_set.get(), &properties_to_clear); | 363 SetPropertiesToClear(properties_to_set.get(), &properties_to_clear); |
307 NetworkHandler::Get()->network_configuration_handler()->SetProperties( | 364 NetworkHandler::Get()->network_configuration_handler()->SetProperties( |
308 service_path, | 365 service_path, *properties_to_set, |
309 *properties_to_set, | 366 base::Bind(&NetworkConnectImpl::ClearPropertiesAndConnect, |
310 base::Bind(&ClearPropertiesAndConnect, | 367 weak_factory_.GetWeakPtr(), service_path, properties_to_clear), |
311 service_path, | 368 base::Bind(&NetworkConnectImpl::SetPropertiesFailed, |
312 properties_to_clear), | 369 weak_factory_.GetWeakPtr(), "SetProperties", service_path)); |
313 base::Bind(&SetPropertiesFailed, "SetProperties", service_path)); | |
314 } | 370 } |
315 | 371 |
316 const NetworkState* GetNetworkState(const std::string& service_path) { | 372 // Public methods |
317 return NetworkHandler::Get()->network_state_handler()-> | |
318 GetNetworkState(service_path); | |
319 } | |
320 | 373 |
321 } // namespace | 374 void NetworkConnectImpl::ConnectToNetwork(const std::string& service_path) { |
322 | |
323 namespace network_connect { | |
324 | |
325 const char kNetworkConnectNotificationId[] = | |
326 "chrome://settings/internet/connect"; | |
327 const char kNetworkActivateNotificationId[] = | |
328 "chrome://settings/internet/activate"; | |
329 | |
330 const char kErrorActivateFailed[] = "activate-failed"; | |
331 | |
332 void ConnectToNetwork(const std::string& service_path) { | |
333 NET_LOG_USER("ConnectToNetwork", service_path); | 375 NET_LOG_USER("ConnectToNetwork", service_path); |
334 const NetworkState* network = GetNetworkState(service_path); | 376 const NetworkState* network = GetNetworkState(service_path); |
335 if (network) { | 377 if (network) { |
336 if (!network->error().empty() && !network->security().empty()) { | 378 if (!network->error().empty() && !network->security().empty()) { |
337 NET_LOG_USER("Configure: " + network->error(), service_path); | 379 NET_LOG_USER("Configure: " + network->error(), service_path); |
338 // If the network is in an error state, show the configuration UI directly | 380 // If the network is in an error state, show the configuration UI |
339 // to avoid a spurious notification. | 381 // directly to avoid a spurious notification. |
340 HandleUnconfiguredNetwork(service_path); | 382 HandleUnconfiguredNetwork(service_path); |
341 return; | 383 return; |
342 } else if (network->RequiresActivation()) { | 384 } else if (network->RequiresActivation()) { |
343 ActivateCellular(service_path); | 385 ActivateCellular(service_path); |
344 return; | 386 return; |
345 } | 387 } |
346 } | 388 } |
347 const bool check_error_state = true; | 389 const bool check_error_state = true; |
348 CallConnectToNetwork(service_path, check_error_state); | 390 CallConnectToNetwork(service_path, check_error_state); |
349 } | 391 } |
350 | 392 |
351 void SetTechnologyEnabled(const NetworkTypePattern& technology, | 393 void NetworkConnectImpl::SetTechnologyEnabled( |
352 bool enabled_state) { | 394 const NetworkTypePattern& technology, |
353 std::string log_string = | 395 bool enabled_state) { |
354 base::StringPrintf("technology %s, target state: %s", | 396 std::string log_string = base::StringPrintf( |
355 technology.ToDebugString().c_str(), | 397 "technology %s, target state: %s", technology.ToDebugString().c_str(), |
356 (enabled_state ? "ENABLED" : "DISABLED")); | 398 (enabled_state ? "ENABLED" : "DISABLED")); |
357 NET_LOG_USER("SetTechnologyEnabled", log_string); | 399 NET_LOG_USER("SetTechnologyEnabled", log_string); |
358 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | 400 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); |
359 bool enabled = handler->IsTechnologyEnabled(technology); | 401 bool enabled = handler->IsTechnologyEnabled(technology); |
360 if (enabled_state == enabled) { | 402 if (enabled_state == enabled) { |
361 NET_LOG_USER("Technology already in target state.", log_string); | 403 NET_LOG_USER("Technology already in target state.", log_string); |
362 return; | 404 return; |
363 } | 405 } |
364 if (enabled) { | 406 if (enabled) { |
365 // User requested to disable the technology. | 407 // User requested to disable the technology. |
366 handler->SetTechnologyEnabled( | 408 handler->SetTechnologyEnabled(technology, false, |
367 technology, false, chromeos::network_handler::ErrorCallback()); | 409 chromeos::network_handler::ErrorCallback()); |
368 return; | 410 return; |
369 } | 411 } |
370 // If we're dealing with a mobile network, then handle SIM lock here. | 412 // If we're dealing with a mobile network, then handle SIM lock here. |
371 // SIM locking only applies to cellular, so the code below won't execute | 413 // SIM locking only applies to cellular, so the code below won't execute |
372 // if |technology| has been explicitly set to WiMAX. | 414 // if |technology| has been explicitly set to WiMAX. |
373 if (technology.MatchesPattern(NetworkTypePattern::Mobile())) { | 415 if (technology.MatchesPattern(NetworkTypePattern::Mobile())) { |
374 const DeviceState* mobile = handler->GetDeviceStateByType(technology); | 416 const DeviceState* mobile = handler->GetDeviceStateByType(technology); |
375 if (!mobile) { | 417 if (!mobile) { |
376 NET_LOG_ERROR("SetTechnologyEnabled with no device", log_string); | 418 NET_LOG_ERROR("SetTechnologyEnabled with no device", log_string); |
377 return; | 419 return; |
378 } | 420 } |
379 // The following only applies to cellular. | 421 // The following only applies to cellular. |
380 if (mobile->type() == shill::kTypeCellular) { | 422 if (mobile->type() == shill::kTypeCellular) { |
381 if (mobile->IsSimAbsent()) { | 423 if (mobile->IsSimAbsent()) { |
382 // If this is true, then we have a cellular device with no SIM inserted. | 424 // If this is true, then we have a cellular device with no SIM |
383 // TODO(armansito): Chrome should display a notification here, prompting | 425 // inserted. TODO(armansito): Chrome should display a notification here, |
384 // the user to insert a SIM card and restart the device to enable | 426 // prompting the user to insert a SIM card and restart the device to |
385 // cellular. See crbug.com/125171. | 427 // enable cellular. See crbug.com/125171. |
386 NET_LOG_USER("Cannot enable cellular device without SIM.", log_string); | 428 NET_LOG_USER("Cannot enable cellular device without SIM.", log_string); |
387 return; | 429 return; |
388 } | 430 } |
389 if (!mobile->sim_lock_type().empty()) { | 431 if (!mobile->sim_lock_type().empty()) { |
390 // A SIM has been inserted, but it is locked. Let the user unlock it | 432 // A SIM has been inserted, but it is locked. Let the user unlock it |
391 // via the dialog. | 433 // via the dialog. |
392 ash::Shell::GetInstance()->system_tray_delegate()-> | 434 delegate_->ShowMobileSimDialog(); |
393 ShowMobileSimDialog(); | |
394 return; | 435 return; |
395 } | 436 } |
396 } | 437 } |
397 } | 438 } |
398 handler->SetTechnologyEnabled( | 439 handler->SetTechnologyEnabled(technology, true, |
399 technology, true, chromeos::network_handler::ErrorCallback()); | 440 chromeos::network_handler::ErrorCallback()); |
400 } | 441 } |
401 | 442 |
402 void ActivateCellular(const std::string& service_path) { | 443 void NetworkConnectImpl::ActivateCellular(const std::string& service_path) { |
403 NET_LOG_USER("ActivateCellular", service_path); | 444 NET_LOG_USER("ActivateCellular", service_path); |
404 const NetworkState* cellular = GetNetworkState(service_path); | 445 const NetworkState* cellular = GetNetworkState(service_path); |
405 if (!cellular || cellular->type() != shill::kTypeCellular) { | 446 if (!cellular || cellular->type() != shill::kTypeCellular) { |
406 NET_LOG_ERROR("ActivateCellular with no Service", service_path); | 447 NET_LOG_ERROR("ActivateCellular with no Service", service_path); |
407 return; | 448 return; |
408 } | 449 } |
409 const DeviceState* cellular_device = | 450 const DeviceState* cellular_device = |
410 NetworkHandler::Get()->network_state_handler()-> | 451 NetworkHandler::Get()->network_state_handler()->GetDeviceState( |
411 GetDeviceState(cellular->device_path()); | 452 cellular->device_path()); |
412 if (!cellular_device) { | 453 if (!cellular_device) { |
413 NET_LOG_ERROR("ActivateCellular with no Device", service_path); | 454 NET_LOG_ERROR("ActivateCellular with no Device", service_path); |
414 return; | 455 return; |
415 } | 456 } |
416 if (!IsDirectActivatedCarrier(cellular_device->carrier())) { | 457 if (!IsDirectActivatedCarrier(cellular_device->carrier())) { |
417 // For non direct activation, show the mobile setup dialog which can be | 458 // For non direct activation, show the mobile setup dialog which can be |
418 // used to activate the network. | 459 // used to activate the network. |
419 ShowMobileSetup(service_path); | 460 ShowMobileSetup(service_path); |
420 return; | 461 return; |
421 } | 462 } |
422 if (cellular->activation_state() == shill::kActivationStateActivated) { | 463 if (cellular->activation_state() == shill::kActivationStateActivated) { |
423 NET_LOG_ERROR("ActivateCellular for activated service", service_path); | 464 NET_LOG_ERROR("ActivateCellular for activated service", service_path); |
424 return; | 465 return; |
425 } | 466 } |
426 | 467 |
427 NetworkHandler::Get()->network_activation_handler()->Activate( | 468 NetworkHandler::Get()->network_activation_handler()->Activate( |
428 service_path, | 469 service_path, |
429 "", // carrier | 470 "", // carrier |
430 base::Bind(&OnActivateSucceeded, service_path), | 471 base::Bind(&NetworkConnectImpl::OnActivateSucceeded, |
431 base::Bind(&OnActivateFailed, service_path)); | 472 weak_factory_.GetWeakPtr(), service_path), |
473 base::Bind(&NetworkConnectImpl::OnActivateFailed, | |
474 weak_factory_.GetWeakPtr(), service_path)); | |
432 } | 475 } |
433 | 476 |
434 void ShowMobileSetup(const std::string& service_path) { | 477 void NetworkConnectImpl::ShowMobileSetup(const std::string& service_path) { |
435 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); | 478 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); |
436 const NetworkState* cellular = handler->GetNetworkState(service_path); | 479 const NetworkState* cellular = handler->GetNetworkState(service_path); |
437 if (!cellular || cellular->type() != shill::kTypeCellular) { | 480 if (!cellular || cellular->type() != shill::kTypeCellular) { |
438 NET_LOG_ERROR("ShowMobileSetup without Cellular network", service_path); | 481 NET_LOG_ERROR("ShowMobileSetup without Cellular network", service_path); |
439 return; | 482 return; |
440 } | 483 } |
441 if (cellular->activation_state() != shill::kActivationStateActivated && | 484 if (cellular->activation_state() != shill::kActivationStateActivated && |
442 cellular->activation_type() == shill::kActivationTypeNonCellular && | 485 cellular->activation_type() == shill::kActivationTypeNonCellular && |
443 !handler->DefaultNetwork()) { | 486 !handler->DefaultNetwork()) { |
444 message_center::MessageCenter::Get()->AddNotification( | 487 network_state_notifier_->ShowMobileActivationkError(service_path); |
445 message_center::Notification::CreateSystemNotification( | |
446 kNetworkActivateNotificationId, | |
447 l10n_util::GetStringUTF16(IDS_NETWORK_ACTIVATION_ERROR_TITLE), | |
448 l10n_util::GetStringFUTF16(IDS_NETWORK_ACTIVATION_NEEDS_CONNECTION, | |
449 base::UTF8ToUTF16(cellular->name())), | |
450 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
451 IDR_AURA_UBER_TRAY_CELLULAR_NETWORK_FAILED), | |
452 ash::system_notifier::kNotifierNetworkError, | |
453 base::Bind(&ash::network_connect::ShowNetworkSettings, | |
454 service_path))); | |
455 return; | 488 return; |
456 } | 489 } |
457 ash::Shell::GetInstance()->system_tray_delegate()->ShowMobileSetupDialog( | 490 delegate_->ShowMobileSetupDialog(service_path); |
458 service_path); | |
459 } | 491 } |
460 | 492 |
461 void ConfigureNetworkAndConnect(const std::string& service_path, | 493 void NetworkConnectImpl::ConfigureNetworkAndConnect( |
462 const base::DictionaryValue& properties, | 494 const std::string& service_path, |
463 bool shared) { | 495 const base::DictionaryValue& properties, |
496 bool shared) { | |
464 NET_LOG_USER("ConfigureNetworkAndConnect", service_path); | 497 NET_LOG_USER("ConfigureNetworkAndConnect", service_path); |
465 | 498 |
466 scoped_ptr<base::DictionaryValue> properties_to_set(properties.DeepCopy()); | 499 scoped_ptr<base::DictionaryValue> properties_to_set(properties.DeepCopy()); |
467 | 500 |
468 std::string profile_path; | 501 std::string profile_path; |
469 if (!GetNetworkProfilePath(shared, &profile_path)) { | 502 if (!GetNetworkProfilePath(shared, &profile_path)) { |
470 ShowErrorNotification( | 503 network_state_notifier_->ShowNetworkConnectError( |
471 NetworkConnectionHandler::kErrorConfigureFailed, service_path); | 504 NetworkConnectionHandler::kErrorConfigureFailed, service_path); |
472 return; | 505 return; |
473 } | 506 } |
474 NetworkHandler::Get()->network_configuration_handler()->SetNetworkProfile( | 507 NetworkHandler::Get()->network_configuration_handler()->SetNetworkProfile( |
475 service_path, profile_path, | 508 service_path, profile_path, |
476 base::Bind(&ConfigureSetProfileSucceeded, | 509 base::Bind(&NetworkConnectImpl::ConfigureSetProfileSucceeded, |
477 service_path, base::Passed(&properties_to_set)), | 510 weak_factory_.GetWeakPtr(), service_path, |
478 base::Bind(&SetPropertiesFailed, | 511 base::Passed(&properties_to_set)), |
479 "SetProfile: " + profile_path, service_path)); | 512 base::Bind(&NetworkConnectImpl::SetPropertiesFailed, |
513 weak_factory_.GetWeakPtr(), "SetProfile: " + profile_path, | |
514 service_path)); | |
480 } | 515 } |
481 | 516 |
482 void CreateConfigurationAndConnect(base::DictionaryValue* properties, | 517 void NetworkConnectImpl::CreateConfigurationAndConnect( |
483 bool shared) { | 518 base::DictionaryValue* properties, |
519 bool shared) { | |
484 NET_LOG_USER("CreateConfigurationAndConnect", ""); | 520 NET_LOG_USER("CreateConfigurationAndConnect", ""); |
485 CallCreateConfiguration(properties, shared, true /* connect_on_configure */); | 521 CallCreateConfiguration(properties, shared, true /* connect_on_configure */); |
486 } | 522 } |
487 | 523 |
488 void CreateConfiguration(base::DictionaryValue* properties, bool shared) { | 524 void NetworkConnectImpl::CreateConfiguration(base::DictionaryValue* properties, |
525 bool shared) { | |
489 NET_LOG_USER("CreateConfiguration", ""); | 526 NET_LOG_USER("CreateConfiguration", ""); |
490 CallCreateConfiguration(properties, shared, false /* connect_on_configure */); | 527 CallCreateConfiguration(properties, shared, false /* connect_on_configure */); |
491 } | 528 } |
492 | 529 |
493 base::string16 ErrorString(const std::string& error, | 530 base::string16 NetworkConnectImpl::GetErrorString( |
494 const std::string& service_path) { | 531 const std::string& error, |
532 const std::string& service_path) { | |
495 if (error.empty()) | 533 if (error.empty()) |
496 return base::string16(); | 534 return base::string16(); |
497 if (error == shill::kErrorOutOfRange) | 535 if (error == shill::kErrorOutOfRange) |
498 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_OUT_OF_RANGE); | 536 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_OUT_OF_RANGE); |
499 if (error == shill::kErrorPinMissing) | 537 if (error == shill::kErrorPinMissing) |
500 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_PIN_MISSING); | 538 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_PIN_MISSING); |
501 if (error == shill::kErrorDhcpFailed) | 539 if (error == shill::kErrorDhcpFailed) |
502 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_DHCP_FAILED); | 540 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_DHCP_FAILED); |
503 if (error == shill::kErrorConnectFailed) | 541 if (error == shill::kErrorConnectFailed) |
504 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_CONNECT_FAILED); | 542 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_CONNECT_FAILED); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
565 } | 603 } |
566 | 604 |
567 if (base::StringToLowerASCII(error) == | 605 if (base::StringToLowerASCII(error) == |
568 base::StringToLowerASCII(std::string(shill::kUnknownString))) { | 606 base::StringToLowerASCII(std::string(shill::kUnknownString))) { |
569 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN); | 607 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN); |
570 } | 608 } |
571 return l10n_util::GetStringFUTF16(IDS_NETWORK_UNRECOGNIZED_ERROR, | 609 return l10n_util::GetStringFUTF16(IDS_NETWORK_UNRECOGNIZED_ERROR, |
572 base::UTF8ToUTF16(error)); | 610 base::UTF8ToUTF16(error)); |
573 } | 611 } |
574 | 612 |
575 void ShowNetworkSettings(const std::string& service_path) { | 613 void NetworkConnectImpl::ShowNetworkSettings(const std::string& service_path) { |
576 if (!ash::Shell::HasInstance()) | 614 delegate_->ShowNetworkSettings(service_path); |
577 return; | |
578 ash::Shell::GetInstance()->system_tray_delegate()->ShowNetworkSettings( | |
579 service_path); | |
580 } | 615 } |
581 | 616 |
582 } // network_connect | 617 } // namespace |
618 | |
619 const char NetworkConnect::kErrorActivateFailed[] = "activate-failed"; | |
620 | |
621 static NetworkConnect* g_network_connect = NULL; | |
622 | |
623 // static | |
624 void NetworkConnect::Initialize(Delegate* delegate) { | |
625 CHECK(g_network_connect == NULL); | |
armansito
2014/10/24 20:02:21
Use DCHECK?
stevenjb
2014/10/24 23:01:50
I prefer CHECK for methods like this that don't ge
| |
626 g_network_connect = new NetworkConnectImpl(delegate); | |
627 } | |
628 | |
629 // static | |
630 void NetworkConnect::Shutdown() { | |
631 CHECK(g_network_connect); | |
632 delete g_network_connect; | |
633 g_network_connect = NULL; | |
634 } | |
635 | |
636 // static | |
637 NetworkConnect* NetworkConnect::Get() { | |
638 CHECK(g_network_connect); | |
639 return g_network_connect; | |
640 } | |
641 | |
642 NetworkConnect::NetworkConnect() { | |
643 } | |
644 | |
645 NetworkConnect::~NetworkConnect() { | |
646 } | |
647 | |
583 } // ash | 648 } // ash |
OLD | NEW |