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/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/system/chromeos/network/network_observer.h" | 8 #include "ash/system/chromeos/network/network_observer.h" |
9 #include "ash/system/chromeos/network/network_state_notifier.h" | 9 #include "ash/system/chromeos/network/network_state_notifier.h" |
10 #include "ash/system/tray/system_tray_delegate.h" | 10 #include "ash/system/tray/system_tray_delegate.h" |
11 #include "ash/system/tray/system_tray_notifier.h" | 11 #include "ash/system/tray/system_tray_notifier.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chromeos/login/login_state.h" | |
17 #include "chromeos/network/device_state.h" | |
18 #include "chromeos/network/network_configuration_handler.h" | |
16 #include "chromeos/network/network_connection_handler.h" | 19 #include "chromeos/network/network_connection_handler.h" |
20 #include "chromeos/network/network_event_log.h" | |
21 #include "chromeos/network/network_profile.h" | |
22 #include "chromeos/network/network_profile_handler.h" | |
17 #include "chromeos/network/network_state.h" | 23 #include "chromeos/network/network_state.h" |
18 #include "chromeos/network/network_state_handler.h" | 24 #include "chromeos/network/network_state_handler.h" |
19 #include "grit/ash_strings.h" | 25 #include "grit/ash_strings.h" |
20 #include "third_party/cros_system_api/dbus/service_constants.h" | 26 #include "third_party/cros_system_api/dbus/service_constants.h" |
21 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
22 | 28 |
29 using chromeos::DeviceState; | |
30 using chromeos::NetworkConfigurationHandler; | |
23 using chromeos::NetworkConnectionHandler; | 31 using chromeos::NetworkConnectionHandler; |
24 using chromeos::NetworkHandler; | 32 using chromeos::NetworkHandler; |
33 using chromeos::NetworkProfile; | |
34 using chromeos::NetworkProfileHandler; | |
25 using chromeos::NetworkState; | 35 using chromeos::NetworkState; |
26 | 36 |
27 namespace ash { | 37 namespace ash { |
28 | 38 |
29 namespace { | 39 namespace { |
30 | 40 |
41 // Returns true for carriers that can be activated through Shill instead of | |
42 // through a WebUI dialog. | |
43 bool IsDirectActivatedCarrier(const std::string& carrier) { | |
44 if (carrier == shill::kCarrierSprint) | |
45 return true; | |
46 return false; | |
47 } | |
48 | |
31 void OnConnectFailed(const std::string& service_path, | 49 void OnConnectFailed(const std::string& service_path, |
50 gfx::NativeWindow owning_window, | |
32 const std::string& error_name, | 51 const std::string& error_name, |
33 scoped_ptr<base::DictionaryValue> error_data) { | 52 scoped_ptr<base::DictionaryValue> error_data) { |
34 VLOG(1) << "Connect Failed for " << service_path << ": " << error_name; | 53 NET_LOG_ERROR("Connect Failed: " + error_name, service_path); |
35 if (error_name == NetworkConnectionHandler::kErrorPassphraseRequired) { | 54 |
36 // TODO(stevenjb): Possibly add inline UI to handle passphrase entry here. | 55 if (error_name == NetworkConnectionHandler::kErrorPassphraseRequired || |
37 ash::Shell::GetInstance()->system_tray_delegate()->ConfigureNetwork( | |
38 service_path); | |
39 return; | |
40 } | |
41 if (error_name == NetworkConnectionHandler::kErrorActivationRequired || | |
42 error_name == NetworkConnectionHandler::kErrorCertificateRequired || | |
43 error_name == NetworkConnectionHandler::kErrorConfigurationRequired || | 56 error_name == NetworkConnectionHandler::kErrorConfigurationRequired || |
44 error_name == NetworkConnectionHandler::kErrorAuthenticationRequired) { | 57 error_name == NetworkConnectionHandler::kErrorAuthenticationRequired) { |
45 ash::Shell::GetInstance()->system_tray_delegate()->ConfigureNetwork( | 58 ash::Shell::GetInstance()->system_tray_delegate()->ConfigureNetwork( |
46 service_path); | 59 service_path); |
47 return; | 60 return; |
48 } | 61 } |
62 | |
63 if (error_name == NetworkConnectionHandler::kErrorCertificateRequired) { | |
64 ash::Shell::GetInstance()->system_tray_delegate()->EnrollOrConfigureNetwork( | |
65 service_path, owning_window); | |
66 return; | |
67 } | |
68 | |
69 if (error_name == NetworkConnectionHandler::kErrorActivationRequired) { | |
70 network_connect::ActivateCellular(service_path); | |
71 return; | |
72 } | |
73 | |
49 if (error_name == NetworkConnectionHandler::kErrorConnected || | 74 if (error_name == NetworkConnectionHandler::kErrorConnected || |
50 error_name == NetworkConnectionHandler::kErrorConnecting) { | 75 error_name == NetworkConnectionHandler::kErrorConnecting) { |
51 ash::Shell::GetInstance()->system_tray_delegate()->ShowNetworkSettings( | 76 ash::Shell::GetInstance()->system_tray_delegate()->ShowNetworkSettings( |
52 service_path); | 77 service_path); |
53 return; | 78 return; |
54 } | 79 } |
80 | |
55 // Shill does not always provide a helpful error. In this case, show the | 81 // Shill does not always provide a helpful error. In this case, show the |
56 // configuration UI and a notification. See crbug.com/217033 for an example. | 82 // configuration UI and a notification. See crbug.com/217033 for an example. |
57 if (error_name == NetworkConnectionHandler::kErrorConnectFailed) { | 83 if (error_name == NetworkConnectionHandler::kErrorConnectFailed) { |
58 ash::Shell::GetInstance()->system_tray_delegate()->ConfigureNetwork( | 84 ash::Shell::GetInstance()->system_tray_delegate()->ConfigureNetwork( |
59 service_path); | 85 service_path); |
60 } | 86 } |
61 ash::Shell::GetInstance()->system_tray_notifier()->network_state_notifier()-> | 87 ash::Shell::GetInstance()->system_tray_notifier()->network_state_notifier()-> |
62 ShowNetworkConnectError(error_name, service_path); | 88 ShowNetworkConnectError(error_name, service_path); |
63 } | 89 } |
64 | 90 |
65 void OnConnectSucceeded(const std::string& service_path) { | 91 void OnConnectSucceeded(const std::string& service_path) { |
66 VLOG(1) << "Connect Succeeded for " << service_path; | 92 NET_LOG_USER("Connect Succeeded", service_path); |
67 ash::Shell::GetInstance()->system_tray_notifier()->NotifyClearNetworkMessage( | 93 ash::Shell::GetInstance()->system_tray_notifier()->NotifyClearNetworkMessage( |
68 NetworkObserver::ERROR_CONNECT_FAILED); | 94 NetworkObserver::ERROR_CONNECT_FAILED); |
69 } | 95 } |
70 | 96 |
97 // If |check_error_state| is true, error state for the network is checked, | |
98 // otherwise any current error state is ignored (e.g. for recently configured | |
99 // networks or repeat connect attempts). |owning_window| will be used to parent | |
100 // any configuration UI on failure and may be NULL (in which case the default | |
101 // window will be used). | |
102 void CallConnectToNetwork(const std::string& service_path, | |
103 bool check_error_state, | |
104 gfx::NativeWindow owning_window) { | |
105 NET_LOG_USER("ConnectToNetwork", service_path); | |
106 | |
107 ash::Shell::GetInstance()->system_tray_notifier()->NotifyClearNetworkMessage( | |
108 NetworkObserver::ERROR_CONNECT_FAILED); | |
109 | |
110 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork( | |
111 service_path, | |
112 base::Bind(&OnConnectSucceeded, service_path), | |
113 base::Bind(&OnConnectFailed, service_path, owning_window), | |
114 check_error_state); | |
115 } | |
116 | |
117 void OnActivateFailed(const std::string& service_path, | |
118 const std::string& error_name, | |
119 scoped_ptr<base::DictionaryValue> error_data) { | |
120 NET_LOG_ERROR("Unable to activate network", service_path); | |
121 } | |
122 | |
123 void OnActivateSucceeded(const std::string& service_path) { | |
124 NET_LOG_USER("Activation Succeeded", service_path); | |
125 } | |
126 | |
127 void OnConfigureFailed(const std::string& error_name, | |
128 scoped_ptr<base::DictionaryValue> error_data) { | |
129 NET_LOG_ERROR("Unable to configure network", ""); | |
130 } | |
131 | |
132 void OnConfigureSucceeded(const std::string& service_path) { | |
133 NET_LOG_USER("Configure Succeeded", service_path); | |
134 // After configuring a network, ignore any (possibly stale) error state. | |
135 const bool check_error_state = false; | |
136 const gfx::NativeWindow owning_window = NULL; | |
137 CallConnectToNetwork(service_path, check_error_state, owning_window); | |
138 } | |
139 | |
140 void SetPropertiesFailed(const std::string& desc, | |
141 const std::string& service_path, | |
142 const std::string& config_error_name, | |
143 scoped_ptr<base::DictionaryValue> error_data) { | |
144 NET_LOG_ERROR(desc + ": Failed: " + config_error_name, service_path); | |
145 } | |
146 | |
147 void SetPropertiesToClear(base::DictionaryValue* properties_to_set, | |
148 std::vector<std::string>* properties_to_clear) { | |
149 // Move empty string properties to properties_to_clear. | |
150 for (base::DictionaryValue::Iterator iter(*properties_to_set); | |
151 !iter.IsAtEnd(); iter.Advance()) { | |
152 std::string value_str; | |
153 if (iter.value().GetAsString(&value_str) && value_str.empty()) | |
154 properties_to_clear->push_back(iter.key()); | |
155 } | |
156 // Remove cleared properties from properties_to_set. | |
157 for (std::vector<std::string>::iterator iter = properties_to_clear->begin(); | |
158 iter != properties_to_clear->end(); ++iter) { | |
159 properties_to_set->RemoveWithoutPathExpansion(*iter, NULL); | |
160 } | |
161 } | |
162 | |
163 void ClearPropertiesAndConnect( | |
164 const std::string& service_path, | |
165 const std::vector<std::string>& properties_to_clear) { | |
166 NET_LOG_USER("ClearPropertiesAndConnect", service_path); | |
167 // After configuring a network, ignore any (possibly stale) error state. | |
168 const bool check_error_state = false; | |
169 const gfx::NativeWindow owning_window = NULL; | |
170 NetworkHandler::Get()->network_configuration_handler()->ClearProperties( | |
171 service_path, | |
172 properties_to_clear, | |
173 base::Bind(&CallConnectToNetwork, | |
174 service_path, check_error_state, | |
175 owning_window), | |
176 base::Bind(&SetPropertiesFailed, "ClearProperties", service_path)); | |
177 } | |
178 | |
179 std::string GetNetworkProfilePath(bool shared) { | |
180 // No need to specify a profile if not logged in and authenticated. | |
181 if (!chromeos::LoginState::Get()->IsUserAuthenticated()) | |
pneubeck (no reviews)
2013/08/08 11:10:00
I'm surprised by this special return value "".
Why
stevenjb
2013/08/08 19:00:56
Fair enough. Done.
| |
182 return std::string(); | |
183 | |
184 if (shared) | |
185 return NetworkProfileHandler::kSharedProfilePath; | |
186 | |
187 const NetworkProfile* profile = | |
188 NetworkHandler::Get()->network_profile_handler()-> | |
189 GetDefaultUserProfile(); | |
190 if (profile) | |
191 return profile->path; | |
192 | |
193 NET_LOG_ERROR("No user profile for unshared network configuration", ""); | |
194 return std::string(); | |
195 } | |
196 | |
71 } // namespace | 197 } // namespace |
72 | 198 |
73 namespace network_connect { | 199 namespace network_connect { |
74 | 200 |
75 void ConnectToNetwork(const std::string& service_path) { | 201 void ConnectToNetwork(const std::string& service_path, |
76 const NetworkState* network = NetworkHandler::Get()->network_state_handler()-> | 202 gfx::NativeWindow owning_window) { |
203 const bool check_error_state = true; | |
204 CallConnectToNetwork(service_path, check_error_state, owning_window); | |
205 } | |
206 | |
207 void ActivateCellular(const std::string& service_path) { | |
208 NET_LOG_USER("ActivateCellular", service_path); | |
209 const DeviceState* cellular_device = | |
210 NetworkHandler::Get()->network_state_handler()-> | |
211 GetDeviceStateByType(flimflam::kTypeCellular); | |
212 if (!cellular_device) { | |
213 NET_LOG_ERROR("ActivateCellular with no Device", service_path); | |
214 return; | |
215 } | |
216 if (!IsDirectActivatedCarrier(cellular_device->carrier())) { | |
217 // For non direct activation, show the mobile setup dialog which can be | |
218 // used to activate the network. | |
219 ash::Shell::GetInstance()->system_tray_delegate()->ShowMobileSetup( | |
220 service_path); | |
221 return; | |
222 } | |
223 const NetworkState* cellular = | |
224 NetworkHandler::Get()->network_state_handler()-> | |
77 GetNetworkState(service_path); | 225 GetNetworkState(service_path); |
78 if (!network) | 226 if (!cellular || cellular->type() != flimflam::kTypeCellular) { |
227 NET_LOG_ERROR("ActivateCellular with no Service", service_path); | |
79 return; | 228 return; |
80 ash::Shell::GetInstance()->system_tray_notifier()->NotifyClearNetworkMessage( | 229 } |
81 NetworkObserver::ERROR_CONNECT_FAILED); | 230 if (cellular->activation_state() == flimflam::kActivationStateActivated) { |
82 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork( | 231 NET_LOG_ERROR("ActivateCellular for activated service", service_path); |
232 return; | |
233 } | |
234 | |
235 NetworkHandler::Get()->network_connection_handler()->ActivateNetwork( | |
83 service_path, | 236 service_path, |
84 base::Bind(&OnConnectSucceeded, service_path), | 237 "", // carrier |
85 base::Bind(&OnConnectFailed, service_path), | 238 base::Bind(&OnActivateSucceeded, service_path), |
86 true /* check_error_state */); | 239 base::Bind(&OnActivateFailed, service_path)); |
240 } | |
241 | |
242 void ConfigureNetworkAndConnect(const std::string& service_path, | |
243 const base::DictionaryValue& properties, | |
244 bool shared) { | |
245 NET_LOG_USER("ConfigureNetworkAndConnect", service_path); | |
246 std::string profile_path = GetNetworkProfilePath(shared); | |
247 | |
248 if (!profile_path.empty()) { | |
249 NetworkHandler::Get()->network_configuration_handler()->SetNetworkProfile( | |
250 service_path, profile_path, | |
251 base::Bind(base::DoNothing), | |
252 base::Bind(&SetPropertiesFailed, | |
pneubeck (no reviews)
2013/08/08 11:10:00
If this fails, you might end up silently writing t
stevenjb
2013/08/08 19:00:56
If it fails... there are probably bigger problems.
| |
253 "SetProfile: " + profile_path, service_path)); | |
254 } | |
255 | |
256 scoped_ptr<base::DictionaryValue> properties_to_set(properties.DeepCopy()); | |
257 std::vector<std::string> properties_to_clear; | |
258 SetPropertiesToClear(properties_to_set.get(), &properties_to_clear); | |
259 NetworkHandler::Get()->network_configuration_handler()->SetProperties( | |
260 service_path, | |
261 *properties_to_set, | |
262 base::Bind(&ClearPropertiesAndConnect, | |
263 service_path, | |
264 properties_to_clear), | |
265 base::Bind(&SetPropertiesFailed, "SetProperties", service_path)); | |
266 } | |
267 | |
268 void CreateConfigurationAndConnect(base::DictionaryValue* properties, | |
269 bool shared) { | |
270 NET_LOG_USER("CreateConfigurationAndConnect", ""); | |
271 std::string profile_path = GetNetworkProfilePath(shared); | |
272 properties->SetStringWithoutPathExpansion( | |
273 flimflam::kProfileProperty, profile_path); | |
274 NetworkHandler::Get()->network_configuration_handler()->CreateConfiguration( | |
275 *properties, | |
276 base::Bind(&OnConfigureSucceeded), | |
277 base::Bind(&OnConfigureFailed)); | |
87 } | 278 } |
88 | 279 |
89 string16 ErrorString(const std::string& error) { | 280 string16 ErrorString(const std::string& error) { |
90 if (error.empty()) | 281 if (error.empty()) |
91 return string16(); | 282 return string16(); |
92 if (error == flimflam::kErrorOutOfRange) | 283 if (error == flimflam::kErrorOutOfRange) |
93 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_OUT_OF_RANGE); | 284 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_OUT_OF_RANGE); |
94 if (error == flimflam::kErrorPinMissing) | 285 if (error == flimflam::kErrorPinMissing) |
95 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_PIN_MISSING); | 286 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_PIN_MISSING); |
96 if (error == flimflam::kErrorDhcpFailed) | 287 if (error == flimflam::kErrorDhcpFailed) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 if (StringToLowerASCII(error) == | 341 if (StringToLowerASCII(error) == |
151 StringToLowerASCII(std::string(flimflam::kUnknownString))) { | 342 StringToLowerASCII(std::string(flimflam::kUnknownString))) { |
152 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN); | 343 return l10n_util::GetStringUTF16(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN); |
153 } | 344 } |
154 return l10n_util::GetStringFUTF16(IDS_NETWORK_UNRECOGNIZED_ERROR, | 345 return l10n_util::GetStringFUTF16(IDS_NETWORK_UNRECOGNIZED_ERROR, |
155 UTF8ToUTF16(error)); | 346 UTF8ToUTF16(error)); |
156 } | 347 } |
157 | 348 |
158 } // network_connect | 349 } // network_connect |
159 } // ash | 350 } // ash |
OLD | NEW |