Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(710)

Side by Side Diff: ash/system/chromeos/network/network_connect.cc

Issue 673713003: Create a NetworkConnect class and Delegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More virtual fixes Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
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
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 (message_center::MessageCenter::Get()) { 259 network_state_notifier_->RemoveConnectNotification();
209 message_center::MessageCenter::Get()->RemoveNotification(
210 network_connect::kNetworkConnectNotificationId,
211 false /* not by user */);
212 }
213
214 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork( 260 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork(
215 service_path, 261 service_path, base::Bind(&NetworkConnectImpl::OnConnectSucceeded,
216 base::Bind(&OnConnectSucceeded, service_path), 262 weak_factory_.GetWeakPtr(), service_path),
217 base::Bind(&OnConnectFailed, service_path), 263 base::Bind(&NetworkConnectImpl::OnConnectFailed,
264 weak_factory_.GetWeakPtr(), service_path),
218 check_error_state); 265 check_error_state);
219 } 266 }
220 267
221 void OnActivateFailed(const std::string& service_path, 268 void NetworkConnectImpl::OnActivateFailed(
222 const std::string& error_name, 269 const std::string& service_path,
223 scoped_ptr<base::DictionaryValue> error_data) { 270 const std::string& error_name,
271 scoped_ptr<base::DictionaryValue> error_data) {
224 NET_LOG_ERROR("Unable to activate network", service_path); 272 NET_LOG_ERROR("Unable to activate network", service_path);
225 ShowErrorNotification(network_connect::kErrorActivateFailed, service_path); 273 network_state_notifier_->ShowNetworkConnectError(kErrorActivateFailed,
274 service_path);
226 } 275 }
227 276
228 void OnActivateSucceeded(const std::string& service_path) { 277 void NetworkConnectImpl::OnActivateSucceeded(const std::string& service_path) {
229 NET_LOG_USER("Activation Succeeded", service_path); 278 NET_LOG_USER("Activation Succeeded", service_path);
230 } 279 }
231 280
232 void OnConfigureFailed(const std::string& error_name, 281 void NetworkConnectImpl::OnConfigureFailed(
233 scoped_ptr<base::DictionaryValue> error_data) { 282 const std::string& error_name,
283 scoped_ptr<base::DictionaryValue> error_data) {
234 NET_LOG_ERROR("Unable to configure network", ""); 284 NET_LOG_ERROR("Unable to configure network", "");
235 ShowErrorNotification(NetworkConnectionHandler::kErrorConfigureFailed, ""); 285 network_state_notifier_->ShowNetworkConnectError(
286 NetworkConnectionHandler::kErrorConfigureFailed, "");
236 } 287 }
237 288
238 void OnConfigureSucceeded(bool connect_on_configure, 289 void NetworkConnectImpl::OnConfigureSucceeded(bool connect_on_configure,
239 const std::string& service_path) { 290 const std::string& service_path) {
240 NET_LOG_USER("Configure Succeeded", service_path); 291 NET_LOG_USER("Configure Succeeded", service_path);
241 if (!connect_on_configure) 292 if (!connect_on_configure)
242 return; 293 return;
243 // After configuring a network, ignore any (possibly stale) error state. 294 // After configuring a network, ignore any (possibly stale) error state.
244 const bool check_error_state = false; 295 const bool check_error_state = false;
245 CallConnectToNetwork(service_path, check_error_state); 296 CallConnectToNetwork(service_path, check_error_state);
246 } 297 }
247 298
248 void CallCreateConfiguration(base::DictionaryValue* properties, 299 void NetworkConnectImpl::CallCreateConfiguration(
249 bool shared, 300 base::DictionaryValue* properties,
250 bool connect_on_configure) { 301 bool shared,
302 bool connect_on_configure) {
251 std::string profile_path; 303 std::string profile_path;
252 if (!GetNetworkProfilePath(shared, &profile_path)) { 304 if (!GetNetworkProfilePath(shared, &profile_path)) {
253 ShowErrorNotification(NetworkConnectionHandler::kErrorConfigureFailed, ""); 305 network_state_notifier_->ShowNetworkConnectError(
306 NetworkConnectionHandler::kErrorConfigureFailed, "");
254 return; 307 return;
255 } 308 }
256 properties->SetStringWithoutPathExpansion( 309 properties->SetStringWithoutPathExpansion(shill::kProfileProperty,
257 shill::kProfileProperty, profile_path); 310 profile_path);
258 NetworkHandler::Get()->network_configuration_handler()->CreateConfiguration( 311 NetworkHandler::Get()->network_configuration_handler()->CreateConfiguration(
259 *properties, 312 *properties, base::Bind(&NetworkConnectImpl::OnConfigureSucceeded,
260 base::Bind(&OnConfigureSucceeded, connect_on_configure), 313 weak_factory_.GetWeakPtr(), connect_on_configure),
261 base::Bind(&OnConfigureFailed)); 314 base::Bind(&NetworkConnectImpl::OnConfigureFailed,
315 weak_factory_.GetWeakPtr()));
262 } 316 }
263 317
264 void SetPropertiesFailed(const std::string& desc, 318 void NetworkConnectImpl::SetPropertiesFailed(
265 const std::string& service_path, 319 const std::string& desc,
266 const std::string& config_error_name, 320 const std::string& service_path,
267 scoped_ptr<base::DictionaryValue> error_data) { 321 const std::string& config_error_name,
322 scoped_ptr<base::DictionaryValue> error_data) {
268 NET_LOG_ERROR(desc + ": Failed: " + config_error_name, service_path); 323 NET_LOG_ERROR(desc + ": Failed: " + config_error_name, service_path);
269 ShowErrorNotification( 324 network_state_notifier_->ShowNetworkConnectError(
270 NetworkConnectionHandler::kErrorConfigureFailed, service_path); 325 NetworkConnectionHandler::kErrorConfigureFailed, service_path);
271 } 326 }
272 327
273 void SetPropertiesToClear(base::DictionaryValue* properties_to_set, 328 void NetworkConnectImpl::SetPropertiesToClear(
274 std::vector<std::string>* properties_to_clear) { 329 base::DictionaryValue* properties_to_set,
330 std::vector<std::string>* properties_to_clear) {
275 // Move empty string properties to properties_to_clear. 331 // Move empty string properties to properties_to_clear.
276 for (base::DictionaryValue::Iterator iter(*properties_to_set); 332 for (base::DictionaryValue::Iterator iter(*properties_to_set);
277 !iter.IsAtEnd(); iter.Advance()) { 333 !iter.IsAtEnd(); iter.Advance()) {
278 std::string value_str; 334 std::string value_str;
279 if (iter.value().GetAsString(&value_str) && value_str.empty()) 335 if (iter.value().GetAsString(&value_str) && value_str.empty())
280 properties_to_clear->push_back(iter.key()); 336 properties_to_clear->push_back(iter.key());
281 } 337 }
282 // Remove cleared properties from properties_to_set. 338 // Remove cleared properties from properties_to_set.
283 for (std::vector<std::string>::iterator iter = properties_to_clear->begin(); 339 for (std::vector<std::string>::iterator iter = properties_to_clear->begin();
284 iter != properties_to_clear->end(); ++iter) { 340 iter != properties_to_clear->end(); ++iter) {
285 properties_to_set->RemoveWithoutPathExpansion(*iter, NULL); 341 properties_to_set->RemoveWithoutPathExpansion(*iter, NULL);
286 } 342 }
287 } 343 }
288 344
289 void ClearPropertiesAndConnect( 345 void NetworkConnectImpl::ClearPropertiesAndConnect(
290 const std::string& service_path, 346 const std::string& service_path,
291 const std::vector<std::string>& properties_to_clear) { 347 const std::vector<std::string>& properties_to_clear) {
292 NET_LOG_USER("ClearPropertiesAndConnect", service_path); 348 NET_LOG_USER("ClearPropertiesAndConnect", service_path);
293 // After configuring a network, ignore any (possibly stale) error state. 349 // After configuring a network, ignore any (possibly stale) error state.
294 const bool check_error_state = false; 350 const bool check_error_state = false;
295 NetworkHandler::Get()->network_configuration_handler()->ClearProperties( 351 NetworkHandler::Get()->network_configuration_handler()->ClearProperties(
296 service_path, 352 service_path, properties_to_clear,
297 properties_to_clear, 353 base::Bind(&NetworkConnectImpl::CallConnectToNetwork,
298 base::Bind(&CallConnectToNetwork, 354 weak_factory_.GetWeakPtr(), service_path, check_error_state),
299 service_path, check_error_state), 355 base::Bind(&NetworkConnectImpl::SetPropertiesFailed,
300 base::Bind(&SetPropertiesFailed, "ClearProperties", service_path)); 356 weak_factory_.GetWeakPtr(), "ClearProperties", service_path));
301 } 357 }
302 358
303 void ConfigureSetProfileSucceeded( 359 void NetworkConnectImpl::ConfigureSetProfileSucceeded(
304 const std::string& service_path, 360 const std::string& service_path,
305 scoped_ptr<base::DictionaryValue> properties_to_set) { 361 scoped_ptr<base::DictionaryValue> properties_to_set) {
306 std::vector<std::string> properties_to_clear; 362 std::vector<std::string> properties_to_clear;
307 SetPropertiesToClear(properties_to_set.get(), &properties_to_clear); 363 SetPropertiesToClear(properties_to_set.get(), &properties_to_clear);
308 NetworkHandler::Get()->network_configuration_handler()->SetProperties( 364 NetworkHandler::Get()->network_configuration_handler()->SetProperties(
309 service_path, 365 service_path, *properties_to_set,
310 *properties_to_set, 366 base::Bind(&NetworkConnectImpl::ClearPropertiesAndConnect,
311 base::Bind(&ClearPropertiesAndConnect, 367 weak_factory_.GetWeakPtr(), service_path, properties_to_clear),
312 service_path, 368 base::Bind(&NetworkConnectImpl::SetPropertiesFailed,
313 properties_to_clear), 369 weak_factory_.GetWeakPtr(), "SetProperties", service_path));
314 base::Bind(&SetPropertiesFailed, "SetProperties", service_path));
315 } 370 }
316 371
317 const NetworkState* GetNetworkState(const std::string& service_path) { 372 // Public methods
318 return NetworkHandler::Get()->network_state_handler()->
319 GetNetworkState(service_path);
320 }
321 373
322 } // namespace 374 void NetworkConnectImpl::ConnectToNetwork(const std::string& service_path) {
323
324 namespace network_connect {
325
326 const char kNetworkConnectNotificationId[] =
327 "chrome://settings/internet/connect";
328 const char kNetworkActivateNotificationId[] =
329 "chrome://settings/internet/activate";
330
331 const char kErrorActivateFailed[] = "activate-failed";
332
333 void ConnectToNetwork(const std::string& service_path) {
334 NET_LOG_USER("ConnectToNetwork", service_path); 375 NET_LOG_USER("ConnectToNetwork", service_path);
335 const NetworkState* network = GetNetworkState(service_path); 376 const NetworkState* network = GetNetworkState(service_path);
336 if (network) { 377 if (network) {
337 if (!network->error().empty() && !network->security().empty()) { 378 if (!network->error().empty() && !network->security().empty()) {
338 NET_LOG_USER("Configure: " + network->error(), service_path); 379 NET_LOG_USER("Configure: " + network->error(), service_path);
339 // 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
340 // to avoid a spurious notification. 381 // directly to avoid a spurious notification.
341 HandleUnconfiguredNetwork(service_path); 382 HandleUnconfiguredNetwork(service_path);
342 return; 383 return;
343 } else if (network->RequiresActivation()) { 384 } else if (network->RequiresActivation()) {
344 ActivateCellular(service_path); 385 ActivateCellular(service_path);
345 return; 386 return;
346 } 387 }
347 } 388 }
348 const bool check_error_state = true; 389 const bool check_error_state = true;
349 CallConnectToNetwork(service_path, check_error_state); 390 CallConnectToNetwork(service_path, check_error_state);
350 } 391 }
351 392
352 void SetTechnologyEnabled(const NetworkTypePattern& technology, 393 void NetworkConnectImpl::SetTechnologyEnabled(
353 bool enabled_state) { 394 const NetworkTypePattern& technology,
354 std::string log_string = 395 bool enabled_state) {
355 base::StringPrintf("technology %s, target state: %s", 396 std::string log_string = base::StringPrintf(
356 technology.ToDebugString().c_str(), 397 "technology %s, target state: %s", technology.ToDebugString().c_str(),
357 (enabled_state ? "ENABLED" : "DISABLED")); 398 (enabled_state ? "ENABLED" : "DISABLED"));
358 NET_LOG_USER("SetTechnologyEnabled", log_string); 399 NET_LOG_USER("SetTechnologyEnabled", log_string);
359 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); 400 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
360 bool enabled = handler->IsTechnologyEnabled(technology); 401 bool enabled = handler->IsTechnologyEnabled(technology);
361 if (enabled_state == enabled) { 402 if (enabled_state == enabled) {
362 NET_LOG_USER("Technology already in target state.", log_string); 403 NET_LOG_USER("Technology already in target state.", log_string);
363 return; 404 return;
364 } 405 }
365 if (enabled) { 406 if (enabled) {
366 // User requested to disable the technology. 407 // User requested to disable the technology.
367 handler->SetTechnologyEnabled( 408 handler->SetTechnologyEnabled(technology, false,
368 technology, false, chromeos::network_handler::ErrorCallback()); 409 chromeos::network_handler::ErrorCallback());
369 return; 410 return;
370 } 411 }
371 // 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.
372 // 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
373 // if |technology| has been explicitly set to WiMAX. 414 // if |technology| has been explicitly set to WiMAX.
374 if (technology.MatchesPattern(NetworkTypePattern::Mobile())) { 415 if (technology.MatchesPattern(NetworkTypePattern::Mobile())) {
375 const DeviceState* mobile = handler->GetDeviceStateByType(technology); 416 const DeviceState* mobile = handler->GetDeviceStateByType(technology);
376 if (!mobile) { 417 if (!mobile) {
377 NET_LOG_ERROR("SetTechnologyEnabled with no device", log_string); 418 NET_LOG_ERROR("SetTechnologyEnabled with no device", log_string);
378 return; 419 return;
379 } 420 }
380 // The following only applies to cellular. 421 // The following only applies to cellular.
381 if (mobile->type() == shill::kTypeCellular) { 422 if (mobile->type() == shill::kTypeCellular) {
382 if (mobile->IsSimAbsent()) { 423 if (mobile->IsSimAbsent()) {
383 // 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
384 // TODO(armansito): Chrome should display a notification here, prompting 425 // inserted. TODO(armansito): Chrome should display a notification here,
385 // 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
386 // cellular. See crbug.com/125171. 427 // enable cellular. See crbug.com/125171.
387 NET_LOG_USER("Cannot enable cellular device without SIM.", log_string); 428 NET_LOG_USER("Cannot enable cellular device without SIM.", log_string);
388 return; 429 return;
389 } 430 }
390 if (!mobile->sim_lock_type().empty()) { 431 if (!mobile->sim_lock_type().empty()) {
391 // 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
392 // via the dialog. 433 // via the dialog.
393 ash::Shell::GetInstance()->system_tray_delegate()-> 434 delegate_->ShowMobileSimDialog();
394 ShowMobileSimDialog();
395 return; 435 return;
396 } 436 }
397 } 437 }
398 } 438 }
399 handler->SetTechnologyEnabled( 439 handler->SetTechnologyEnabled(technology, true,
400 technology, true, chromeos::network_handler::ErrorCallback()); 440 chromeos::network_handler::ErrorCallback());
401 } 441 }
402 442
403 void ActivateCellular(const std::string& service_path) { 443 void NetworkConnectImpl::ActivateCellular(const std::string& service_path) {
404 NET_LOG_USER("ActivateCellular", service_path); 444 NET_LOG_USER("ActivateCellular", service_path);
405 const NetworkState* cellular = GetNetworkState(service_path); 445 const NetworkState* cellular = GetNetworkState(service_path);
406 if (!cellular || cellular->type() != shill::kTypeCellular) { 446 if (!cellular || cellular->type() != shill::kTypeCellular) {
407 NET_LOG_ERROR("ActivateCellular with no Service", service_path); 447 NET_LOG_ERROR("ActivateCellular with no Service", service_path);
408 return; 448 return;
409 } 449 }
410 const DeviceState* cellular_device = 450 const DeviceState* cellular_device =
411 NetworkHandler::Get()->network_state_handler()-> 451 NetworkHandler::Get()->network_state_handler()->GetDeviceState(
412 GetDeviceState(cellular->device_path()); 452 cellular->device_path());
413 if (!cellular_device) { 453 if (!cellular_device) {
414 NET_LOG_ERROR("ActivateCellular with no Device", service_path); 454 NET_LOG_ERROR("ActivateCellular with no Device", service_path);
415 return; 455 return;
416 } 456 }
417 if (!IsDirectActivatedCarrier(cellular_device->carrier())) { 457 if (!IsDirectActivatedCarrier(cellular_device->carrier())) {
418 // 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
419 // used to activate the network. 459 // used to activate the network.
420 ShowMobileSetup(service_path); 460 ShowMobileSetup(service_path);
421 return; 461 return;
422 } 462 }
423 if (cellular->activation_state() == shill::kActivationStateActivated) { 463 if (cellular->activation_state() == shill::kActivationStateActivated) {
424 NET_LOG_ERROR("ActivateCellular for activated service", service_path); 464 NET_LOG_ERROR("ActivateCellular for activated service", service_path);
425 return; 465 return;
426 } 466 }
427 467
428 NetworkHandler::Get()->network_activation_handler()->Activate( 468 NetworkHandler::Get()->network_activation_handler()->Activate(
429 service_path, 469 service_path,
430 "", // carrier 470 "", // carrier
431 base::Bind(&OnActivateSucceeded, service_path), 471 base::Bind(&NetworkConnectImpl::OnActivateSucceeded,
432 base::Bind(&OnActivateFailed, service_path)); 472 weak_factory_.GetWeakPtr(), service_path),
473 base::Bind(&NetworkConnectImpl::OnActivateFailed,
474 weak_factory_.GetWeakPtr(), service_path));
433 } 475 }
434 476
435 void ShowMobileSetup(const std::string& service_path) { 477 void NetworkConnectImpl::ShowMobileSetup(const std::string& service_path) {
436 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); 478 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
437 const NetworkState* cellular = handler->GetNetworkState(service_path); 479 const NetworkState* cellular = handler->GetNetworkState(service_path);
438 if (!cellular || cellular->type() != shill::kTypeCellular) { 480 if (!cellular || cellular->type() != shill::kTypeCellular) {
439 NET_LOG_ERROR("ShowMobileSetup without Cellular network", service_path); 481 NET_LOG_ERROR("ShowMobileSetup without Cellular network", service_path);
440 return; 482 return;
441 } 483 }
442 if (cellular->activation_state() != shill::kActivationStateActivated && 484 if (cellular->activation_state() != shill::kActivationStateActivated &&
443 cellular->activation_type() == shill::kActivationTypeNonCellular && 485 cellular->activation_type() == shill::kActivationTypeNonCellular &&
444 !handler->DefaultNetwork()) { 486 !handler->DefaultNetwork()) {
445 message_center::MessageCenter::Get()->AddNotification( 487 network_state_notifier_->ShowMobileActivationError(service_path);
446 message_center::Notification::CreateSystemNotification(
447 kNetworkActivateNotificationId,
448 l10n_util::GetStringUTF16(IDS_NETWORK_ACTIVATION_ERROR_TITLE),
449 l10n_util::GetStringFUTF16(IDS_NETWORK_ACTIVATION_NEEDS_CONNECTION,
450 base::UTF8ToUTF16(cellular->name())),
451 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
452 IDR_AURA_UBER_TRAY_CELLULAR_NETWORK_FAILED),
453 ash::system_notifier::kNotifierNetworkError,
454 base::Bind(&ash::network_connect::ShowNetworkSettings,
455 service_path)));
456 return; 488 return;
457 } 489 }
458 ash::Shell::GetInstance()->system_tray_delegate()->ShowMobileSetupDialog( 490 delegate_->ShowMobileSetupDialog(service_path);
459 service_path);
460 } 491 }
461 492
462 void ConfigureNetworkAndConnect(const std::string& service_path, 493 void NetworkConnectImpl::ConfigureNetworkAndConnect(
463 const base::DictionaryValue& properties, 494 const std::string& service_path,
464 bool shared) { 495 const base::DictionaryValue& properties,
496 bool shared) {
465 NET_LOG_USER("ConfigureNetworkAndConnect", service_path); 497 NET_LOG_USER("ConfigureNetworkAndConnect", service_path);
466 498
467 scoped_ptr<base::DictionaryValue> properties_to_set(properties.DeepCopy()); 499 scoped_ptr<base::DictionaryValue> properties_to_set(properties.DeepCopy());
468 500
469 std::string profile_path; 501 std::string profile_path;
470 if (!GetNetworkProfilePath(shared, &profile_path)) { 502 if (!GetNetworkProfilePath(shared, &profile_path)) {
471 ShowErrorNotification( 503 network_state_notifier_->ShowNetworkConnectError(
472 NetworkConnectionHandler::kErrorConfigureFailed, service_path); 504 NetworkConnectionHandler::kErrorConfigureFailed, service_path);
473 return; 505 return;
474 } 506 }
475 NetworkHandler::Get()->network_configuration_handler()->SetNetworkProfile( 507 NetworkHandler::Get()->network_configuration_handler()->SetNetworkProfile(
476 service_path, profile_path, 508 service_path, profile_path,
477 base::Bind(&ConfigureSetProfileSucceeded, 509 base::Bind(&NetworkConnectImpl::ConfigureSetProfileSucceeded,
478 service_path, base::Passed(&properties_to_set)), 510 weak_factory_.GetWeakPtr(), service_path,
479 base::Bind(&SetPropertiesFailed, 511 base::Passed(&properties_to_set)),
480 "SetProfile: " + profile_path, service_path)); 512 base::Bind(&NetworkConnectImpl::SetPropertiesFailed,
513 weak_factory_.GetWeakPtr(), "SetProfile: " + profile_path,
514 service_path));
481 } 515 }
482 516
483 void CreateConfigurationAndConnect(base::DictionaryValue* properties, 517 void NetworkConnectImpl::CreateConfigurationAndConnect(
484 bool shared) { 518 base::DictionaryValue* properties,
519 bool shared) {
485 NET_LOG_USER("CreateConfigurationAndConnect", ""); 520 NET_LOG_USER("CreateConfigurationAndConnect", "");
486 CallCreateConfiguration(properties, shared, true /* connect_on_configure */); 521 CallCreateConfiguration(properties, shared, true /* connect_on_configure */);
487 } 522 }
488 523
489 void CreateConfiguration(base::DictionaryValue* properties, bool shared) { 524 void NetworkConnectImpl::CreateConfiguration(base::DictionaryValue* properties,
525 bool shared) {
490 NET_LOG_USER("CreateConfiguration", ""); 526 NET_LOG_USER("CreateConfiguration", "");
491 CallCreateConfiguration(properties, shared, false /* connect_on_configure */); 527 CallCreateConfiguration(properties, shared, false /* connect_on_configure */);
492 } 528 }
493 529
494 base::string16 ErrorString(const std::string& error, 530 base::string16 NetworkConnectImpl::GetErrorString(
495 const std::string& service_path) { 531 const std::string& error,
532 const std::string& service_path) {
496 if (error.empty()) 533 if (error.empty())
497 return base::string16(); 534 return base::string16();
498 if (error == shill::kErrorOutOfRange) 535 if (error == shill::kErrorOutOfRange)
499 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_OUT_OF_RANGE); 536 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_OUT_OF_RANGE);
500 if (error == shill::kErrorPinMissing) 537 if (error == shill::kErrorPinMissing)
501 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_PIN_MISSING); 538 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_PIN_MISSING);
502 if (error == shill::kErrorDhcpFailed) 539 if (error == shill::kErrorDhcpFailed)
503 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_DHCP_FAILED); 540 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_DHCP_FAILED);
504 if (error == shill::kErrorConnectFailed) 541 if (error == shill::kErrorConnectFailed)
505 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
566 } 603 }
567 604
568 if (base::StringToLowerASCII(error) == 605 if (base::StringToLowerASCII(error) ==
569 base::StringToLowerASCII(std::string(shill::kUnknownString))) { 606 base::StringToLowerASCII(std::string(shill::kUnknownString))) {
570 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN); 607 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN);
571 } 608 }
572 return l10n_util::GetStringFUTF16(IDS_NETWORK_UNRECOGNIZED_ERROR, 609 return l10n_util::GetStringFUTF16(IDS_NETWORK_UNRECOGNIZED_ERROR,
573 base::UTF8ToUTF16(error)); 610 base::UTF8ToUTF16(error));
574 } 611 }
575 612
576 void ShowNetworkSettings(const std::string& service_path) { 613 void NetworkConnectImpl::ShowNetworkSettings(const std::string& service_path) {
577 if (!ash::Shell::HasInstance()) 614 delegate_->ShowNetworkSettings(service_path);
578 return;
579 ash::Shell::GetInstance()->system_tray_delegate()->ShowNetworkSettings(
580 service_path);
581 } 615 }
582 616
583 } // 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);
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
584 } // ash 648 } // ash
OLDNEW
« no previous file with comments | « ash/system/chromeos/network/network_connect.h ('k') | ash/system/chromeos/network/network_state_list_detailed_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698