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 "chrome/browser/chromeos/extensions/networking_private_api.h" | 5 #include "chrome/browser/chromeos/extensions/networking_private_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "chrome/browser/chromeos/net/managed_network_configuration_handler.h" | 10 #include "chrome/browser/chromeos/net/managed_network_configuration_handler.h" |
11 #include "chrome/browser/extensions/extension_function_registry.h" | 11 #include "chrome/browser/extensions/extension_function_registry.h" |
12 #include "chrome/common/extensions/api/networking_private.h" | 12 #include "chrome/common/extensions/api/networking_private.h" |
13 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
14 #include "chromeos/dbus/shill_manager_client.h" | 14 #include "chromeos/dbus/shill_manager_client.h" |
15 #include "chromeos/dbus/shill_service_client.h" | 15 #include "chromeos/network/network_state.h" |
16 #include "chromeos/network/network_state_handler.h" | |
16 #include "chromeos/network/onc/onc_constants.h" | 17 #include "chromeos/network/onc/onc_constants.h" |
17 #include "chromeos/network/onc/onc_signature.h" | |
18 #include "chromeos/network/onc/onc_translation_tables.h" | |
19 #include "chromeos/network/onc/onc_translator.h" | |
20 #include "dbus/object_path.h" | |
21 #include "third_party/cros_system_api/dbus/service_constants.h" | |
22 | 18 |
23 using namespace chromeos; | 19 using namespace chromeos; |
24 namespace api = extensions::api::networking_private; | 20 namespace api = extensions::api::networking_private; |
25 | 21 |
26 namespace { | 22 namespace { |
27 | 23 |
28 // An error returned when no valid services were found. | 24 // Given the ONC translation of network properties, creates the dictionary which |
29 const char kInvalidResponseError[] = "Error.invalidResponse"; | 25 // will be passed to JavaScript. |
30 | 26 scoped_ptr<api::NetworkProperties> CreateNetworkProperties( |
31 // Filters from the given ONC dictionary the information we're interested in | 27 const std::string& service_path, |
32 // before passing it to JavaScript. | |
33 scoped_ptr<api::NetworkProperties> CreateFilteredResult( | |
34 const base::DictionaryValue& onc_dictionary) { | 28 const base::DictionaryValue& onc_dictionary) { |
35 static const char* const desired_fields[] = { | 29 scoped_ptr<api::NetworkProperties> result( |
36 onc::network_config::kWiFi, | |
37 onc::network_config::kName, | |
38 onc::network_config::kGUID, | |
39 onc::network_config::kType, | |
40 onc::network_config::kConnectionState, | |
41 }; | |
42 | |
43 scoped_ptr<api::NetworkProperties> filtered_result( | |
44 new api::NetworkProperties); | 30 new api::NetworkProperties); |
45 for (size_t i = 0; i < arraysize(desired_fields); ++i) { | 31 result->additional_properties.MergeDictionary(&onc_dictionary); |
46 const base::Value* value; | 32 result->additional_properties.SetStringWithoutPathExpansion( |
47 if (onc_dictionary.GetWithoutPathExpansion(desired_fields[i], &value)) { | 33 onc::network_config::kGUID, service_path); |
48 filtered_result->additional_properties.SetWithoutPathExpansion( | 34 return result.Pass(); |
49 desired_fields[i], | |
50 value->DeepCopy()); | |
51 } | |
52 } | |
53 | |
54 return filtered_result.Pass(); | |
55 } | |
56 | |
57 class ResultList : public base::RefCounted<ResultList> { | |
58 public: | |
59 typedef base::Callback<void(const std::string& error, | |
60 scoped_ptr<base::ListValue>)> ResultCallback; | |
61 | |
62 ResultList(const std::string& type, const ResultCallback& callback) | |
63 : callback_(callback) { | |
64 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties( | |
65 base::Bind(&ResultList::ManagerPropertiesCallback, this, type)); | |
66 } | |
67 | |
68 scoped_ptr<base::ListValue> GetResults() { | |
69 return api::GetVisibleNetworks::Results::Create(list_); | |
70 } | |
71 | |
72 private: | |
73 friend class base::RefCounted<ResultList>; | |
74 | |
75 ~ResultList() { | |
76 callback_.Run(std::string(), GetResults()); | |
77 } | |
78 | |
79 void Append(api::NetworkProperties* value) { | |
80 list_.push_back(linked_ptr<api::NetworkProperties>(value)); | |
81 } | |
82 | |
83 // Receives the result of a call to GetProperties on the Shill Manager API. | |
84 void ManagerPropertiesCallback(const std::string& network_type, | |
85 chromeos::DBusMethodCallStatus call_status, | |
86 const base::DictionaryValue& result); | |
87 | |
88 // Receives the result of a call to GetProperties on the Shill Service API. | |
89 void ServicePropertiesCallback(const std::string& service_path, | |
90 const std::string& network_type, | |
91 chromeos::DBusMethodCallStatus call_status, | |
92 const base::DictionaryValue& result); | |
93 | |
94 std::vector<linked_ptr<api::NetworkProperties> > list_; | |
95 ResultCallback callback_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(ResultList); | |
98 }; | |
99 | |
100 // For each of the available services, fire off a request for its properties. | |
101 void ResultList::ManagerPropertiesCallback( | |
102 const std::string& network_type, | |
103 DBusMethodCallStatus call_status, | |
104 const base::DictionaryValue& result) { | |
105 const base::ListValue* available_services; | |
106 if (!result.GetList(flimflam::kServicesProperty, &available_services)) { | |
107 LOG(ERROR) | |
108 << "ShillManagerClient::GetProperties returned malformed service list."; | |
109 callback_.Run(kInvalidResponseError, make_scoped_ptr(new base::ListValue)); | |
110 return; | |
111 } | |
112 // If there just are no services, return an empty list. | |
113 if (available_services->GetSize() == 0) { | |
114 callback_.Run(std::string(), make_scoped_ptr(new base::ListValue)); | |
115 return; | |
116 } | |
117 for (base::ListValue::const_iterator iter = available_services->begin(); | |
118 iter != available_services->end(); ++iter) { | |
119 std::string service_path; | |
120 if (!(*iter)->GetAsString(&service_path)) { | |
121 LOG(ERROR) | |
122 << "ShillManagerClient::GetProperties returned malformed service."; | |
123 continue; | |
124 } | |
125 | |
126 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | |
127 dbus::ObjectPath(service_path), | |
128 base::Bind( | |
129 &ResultList::ServicePropertiesCallback, | |
130 this, | |
131 service_path, | |
132 network_type)); | |
133 } | |
134 } | |
135 | |
136 void ResultList::ServicePropertiesCallback( | |
137 const std::string& service_path, | |
138 const std::string& network_type, | |
139 DBusMethodCallStatus call_status, | |
140 const base::DictionaryValue& result) { | |
141 if (call_status == DBUS_METHOD_CALL_SUCCESS) { | |
142 scoped_ptr<base::DictionaryValue> onc_properties( | |
143 onc::TranslateShillServiceToONCPart( | |
144 result, | |
145 &onc::kNetworkWithStateSignature)); | |
146 | |
147 scoped_ptr<api::NetworkProperties> filtered_result( | |
148 CreateFilteredResult(*onc_properties)); | |
149 | |
150 std::string onc_type; | |
151 if (filtered_result->additional_properties.GetString( | |
152 onc::network_config::kType, &onc_type) && | |
153 (onc_type == network_type || | |
154 network_type == onc::network_type::kAllTypes)) { | |
155 // TODO(gspencer): For now the "GUID" we send back is going to look | |
156 // remarkably like the service path. Once this code starts using the | |
157 // NetworkStateHandler instead of Shill directly, we should remove | |
158 // this line so that we're sending back the actual GUID. The | |
159 // JavaScript shouldn't care: this ID is opaque to it, and it | |
160 // shouldn't store it anywhere. | |
161 filtered_result->additional_properties.SetStringWithoutPathExpansion( | |
162 onc::network_config::kGUID, service_path); | |
163 | |
164 Append(filtered_result.release()); | |
165 } | |
166 } | |
167 } | 35 } |
168 | 36 |
169 } // namespace | 37 } // namespace |
170 | 38 |
171 //////////////////////////////////////////////////////////////////////////////// | 39 //////////////////////////////////////////////////////////////////////////////// |
172 // NetworkingPrivateGetPropertiesFunction | 40 // NetworkingPrivateGetPropertiesFunction |
173 | 41 |
174 NetworkingPrivateGetPropertiesFunction:: | 42 NetworkingPrivateGetPropertiesFunction:: |
175 ~NetworkingPrivateGetPropertiesFunction() { | 43 ~NetworkingPrivateGetPropertiesFunction() { |
176 } | 44 } |
177 | 45 |
178 bool NetworkingPrivateGetPropertiesFunction::RunImpl() { | 46 bool NetworkingPrivateGetPropertiesFunction::RunImpl() { |
179 scoped_ptr<api::GetProperties::Params> params = | 47 scoped_ptr<api::GetProperties::Params> params = |
180 api::GetProperties::Params::Create(*args_); | 48 api::GetProperties::Params::Create(*args_); |
181 EXTENSION_FUNCTION_VALIDATE(params); | 49 EXTENSION_FUNCTION_VALIDATE(params); |
182 if (ManagedNetworkConfigurationHandler::IsInitialized()) { | 50 // The |network_guid| parameter is storing the service path. |
183 ManagedNetworkConfigurationHandler::Get()->GetProperties( | 51 std::string service_path = params->network_guid; |
184 params->network_guid, | 52 |
185 base::Bind( | 53 ManagedNetworkConfigurationHandler::Get()->GetProperties( |
186 &NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess, | 54 service_path, |
187 this), | 55 base::Bind( |
188 base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed, | 56 &NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess, |
189 this)); | 57 this), |
190 } else { | 58 base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed, |
191 // TODO(gspencer): Currently we're using the service path as the | 59 this)); |
192 // |network_guid|. Eventually this should be using the real GUID. | |
193 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | |
194 dbus::ObjectPath(params->network_guid), | |
195 base::Bind(&NetworkingPrivateGetPropertiesFunction::ResultCallback, | |
196 this, | |
197 params->network_guid)); | |
198 } | |
199 return true; | 60 return true; |
200 } | 61 } |
201 | 62 |
202 void NetworkingPrivateGetPropertiesFunction::ResultCallback( | |
203 const std::string& service_path, | |
204 DBusMethodCallStatus call_status, | |
205 const base::DictionaryValue& result) { | |
206 scoped_ptr<base::DictionaryValue> onc_properties( | |
207 onc::TranslateShillServiceToONCPart( | |
208 result, | |
209 &onc::kNetworkWithStateSignature)); | |
210 GetPropertiesSuccess(service_path, | |
211 *onc_properties); | |
212 } | |
213 | |
214 void NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess( | 63 void NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess( |
215 const std::string& service_path, | 64 const std::string& service_path, |
216 const base::DictionaryValue& dictionary) { | 65 const base::DictionaryValue& dictionary) { |
217 scoped_ptr<api::NetworkProperties> filtered_result( | 66 scoped_ptr<api::NetworkProperties> result( |
218 CreateFilteredResult(dictionary)); | 67 CreateNetworkProperties(service_path, |
219 filtered_result->additional_properties.SetStringWithoutPathExpansion( | 68 dictionary)); |
220 onc::network_config::kGUID, service_path); | 69 results_ = api::GetProperties::Results::Create(*result); |
221 results_ = api::GetProperties::Results::Create(*filtered_result); | |
222 SendResponse(true); | 70 SendResponse(true); |
223 } | 71 } |
224 | 72 |
225 void NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed( | 73 void NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed( |
226 const std::string& error_name, | 74 const std::string& error_name, |
227 scoped_ptr<base::DictionaryValue> error_data) { | 75 scoped_ptr<base::DictionaryValue> error_data) { |
228 error_ = error_name; | 76 error_ = error_name; |
229 SendResponse(false); | 77 SendResponse(false); |
230 } | 78 } |
231 | 79 |
232 //////////////////////////////////////////////////////////////////////////////// | 80 //////////////////////////////////////////////////////////////////////////////// |
233 // NetworkingPrivateGetVisibleNetworksFunction | 81 // NetworkingPrivateGetVisibleNetworksFunction |
234 | 82 |
235 NetworkingPrivateGetVisibleNetworksFunction:: | 83 NetworkingPrivateGetVisibleNetworksFunction:: |
236 ~NetworkingPrivateGetVisibleNetworksFunction() { | 84 ~NetworkingPrivateGetVisibleNetworksFunction() { |
237 } | 85 } |
238 | 86 |
239 bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() { | 87 bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() { |
240 scoped_ptr<api::GetVisibleNetworks::Params> params = | 88 scoped_ptr<api::GetVisibleNetworks::Params> params = |
241 api::GetVisibleNetworks::Params::Create(*args_); | 89 api::GetVisibleNetworks::Params::Create(*args_); |
242 EXTENSION_FUNCTION_VALIDATE(params); | 90 EXTENSION_FUNCTION_VALIDATE(params); |
91 std::string type_filter = | |
92 api::GetVisibleNetworks::Params::ToString(params->type); | |
243 | 93 |
244 scoped_refptr<ResultList> result_list(new ResultList( | 94 NetworkStateHandler* state_handler = |
245 api::GetVisibleNetworks::Params::ToString(params->type), | 95 NetworkStateHandler::Get(); |
246 base::Bind( | 96 NetworkStateHandler::NetworkStateList network_states; |
247 &NetworkingPrivateGetVisibleNetworksFunction::SendResultCallback, | 97 state_handler->GetNetworkList(&network_states); |
248 this))); | 98 |
99 std::vector<linked_ptr<api::NetworkProperties> > result; | |
100 for (NetworkStateHandler::NetworkStateList::iterator it = | |
101 network_states.begin(); | |
102 it != network_states.end(); ++it) { | |
103 const std::string& service_path = (*it)->path(); | |
104 scoped_ptr<base::DictionaryValue> onc_network_part = | |
105 (*it)->TranslateToONC(); | |
106 | |
107 std::string onc_type; | |
108 onc_network_part->GetStringWithoutPathExpansion(onc::network_config::kType, | |
109 &onc_type); | |
110 if (onc_type != type_filter && | |
111 type_filter != onc::network_type::kAllTypes) | |
stevenjb
2013/03/04 18:57:58
nit: switch test order (slightly more readable) an
pneubeck (no reviews)
2013/03/05 13:11:55
Done.
| |
112 continue; | |
113 | |
114 result.push_back(linked_ptr<api::NetworkProperties>( | |
stevenjb
2013/03/04 18:57:58
Converting a scoped_ptr to a linked_ptr is confusi
pneubeck (no reviews)
2013/03/05 13:11:55
The linked_ptr is required by the call to
api::G
| |
115 CreateNetworkProperties(service_path, | |
116 *onc_network_part).release())); | |
117 } | |
118 | |
119 results_ = api::GetVisibleNetworks::Results::Create(result); | |
249 return true; | 120 return true; |
250 } | 121 } |
251 | 122 |
252 void NetworkingPrivateGetVisibleNetworksFunction::SendResultCallback( | |
253 const std::string& error, | |
254 scoped_ptr<base::ListValue> result_list) { | |
255 if (!error.empty()) { | |
256 error_ = error; | |
257 SendResponse(false); | |
258 } else { | |
259 results_.reset(result_list.release()); | |
260 SendResponse(true); | |
261 } | |
262 } | |
263 | |
264 //////////////////////////////////////////////////////////////////////////////// | 123 //////////////////////////////////////////////////////////////////////////////// |
265 // NetworkingPrivateStartConnectFunction | 124 // NetworkingPrivateStartConnectFunction |
266 | 125 |
267 NetworkingPrivateStartConnectFunction:: | 126 NetworkingPrivateStartConnectFunction:: |
268 ~NetworkingPrivateStartConnectFunction() { | 127 ~NetworkingPrivateStartConnectFunction() { |
269 } | 128 } |
270 | 129 |
271 void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() { | 130 void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() { |
272 SendResponse(true); | 131 SendResponse(true); |
273 } | 132 } |
274 | 133 |
275 void NetworkingPrivateStartConnectFunction::ConnectionStartFailed( | 134 void NetworkingPrivateStartConnectFunction::ConnectionStartFailed( |
276 const std::string& error_name, | 135 const std::string& error_name, |
277 const std::string& error_message) { | 136 const scoped_ptr<base::DictionaryValue> error_data) { |
278 error_ = error_name; | 137 error_ = error_name; |
279 SendResponse(false); | 138 SendResponse(false); |
280 } | 139 } |
281 | 140 |
282 bool NetworkingPrivateStartConnectFunction::RunImpl() { | 141 bool NetworkingPrivateStartConnectFunction::RunImpl() { |
283 scoped_ptr<api::StartConnect::Params> params = | 142 scoped_ptr<api::StartConnect::Params> params = |
284 api::StartConnect::Params::Create(*args_); | 143 api::StartConnect::Params::Create(*args_); |
285 EXTENSION_FUNCTION_VALIDATE(params); | 144 EXTENSION_FUNCTION_VALIDATE(params); |
286 | 145 |
287 // TODO(gspencer): For now, the "GUID" we receive from the JavaScript is going | 146 // The |network_guid| parameter is storing the service path. |
288 // to be the service path. Fix this so it actually looks up the service path | |
289 // from the GUID once we're using the NetworkStateHandler. | |
290 std::string service_path = params->network_guid; | 147 std::string service_path = params->network_guid; |
291 | 148 |
292 DBusThreadManager::Get()->GetShillServiceClient()->Connect( | 149 ManagedNetworkConfigurationHandler::Get()->Connect( |
293 dbus::ObjectPath(service_path), | 150 service_path, |
294 base::Bind( | 151 base::Bind( |
295 &NetworkingPrivateStartConnectFunction::ConnectionStartSuccess, | 152 &NetworkingPrivateStartConnectFunction::ConnectionStartSuccess, |
296 this), | 153 this), |
297 base::Bind(&NetworkingPrivateStartConnectFunction::ConnectionStartFailed, | 154 base::Bind( |
298 this)); | 155 &NetworkingPrivateStartConnectFunction::ConnectionStartFailed, |
156 this)); | |
299 return true; | 157 return true; |
300 } | 158 } |
301 | 159 |
302 //////////////////////////////////////////////////////////////////////////////// | 160 //////////////////////////////////////////////////////////////////////////////// |
303 // NetworkingPrivateStartDisconnectFunction | 161 // NetworkingPrivateStartDisconnectFunction |
304 | 162 |
305 NetworkingPrivateStartDisconnectFunction:: | 163 NetworkingPrivateStartDisconnectFunction:: |
306 ~NetworkingPrivateStartDisconnectFunction() { | 164 ~NetworkingPrivateStartDisconnectFunction() { |
307 } | 165 } |
308 | 166 |
309 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() { | 167 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() { |
310 SendResponse(true); | 168 SendResponse(true); |
311 } | 169 } |
312 | 170 |
313 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed( | 171 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed( |
314 const std::string& error_name, | 172 const std::string& error_name, |
315 const std::string& error_message) { | 173 const scoped_ptr<base::DictionaryValue> error_data) { |
316 error_ = error_name; | 174 error_ = error_name; |
317 SendResponse(false); | 175 SendResponse(false); |
318 } | 176 } |
319 | 177 |
320 bool NetworkingPrivateStartDisconnectFunction::RunImpl() { | 178 bool NetworkingPrivateStartDisconnectFunction::RunImpl() { |
321 scoped_ptr<api::StartDisconnect::Params> params = | 179 scoped_ptr<api::StartDisconnect::Params> params = |
322 api::StartDisconnect::Params::Create(*args_); | 180 api::StartDisconnect::Params::Create(*args_); |
323 EXTENSION_FUNCTION_VALIDATE(params); | 181 EXTENSION_FUNCTION_VALIDATE(params); |
324 | 182 |
325 // TODO(gspencer): Currently the |network_guid| parameter is storing the | 183 // The |network_guid| parameter is storing the service path. |
326 // service path. Convert to using the actual GUID when we start using | 184 std::string service_path = params->network_guid; |
327 // the NetworkStateHandler. | 185 |
328 DBusThreadManager::Get()->GetShillServiceClient()->Disconnect( | 186 ManagedNetworkConfigurationHandler::Get()->Disconnect( |
329 dbus::ObjectPath(params->network_guid), | 187 service_path, |
330 base::Bind( | 188 base::Bind( |
331 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess, | 189 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess, |
332 this), | 190 this), |
333 base::Bind( | 191 base::Bind( |
334 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed, | 192 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed, |
335 this)); | 193 this)); |
336 return true; | 194 return true; |
337 } | 195 } |
338 | 196 |
339 //////////////////////////////////////////////////////////////////////////////// | 197 //////////////////////////////////////////////////////////////////////////////// |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
448 const std::string& result) { | 306 const std::string& result) { |
449 results_ = api::VerifyAndEncryptData::Results::Create(result); | 307 results_ = api::VerifyAndEncryptData::Results::Create(result); |
450 SendResponse(true); | 308 SendResponse(true); |
451 } | 309 } |
452 | 310 |
453 void NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback( | 311 void NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback( |
454 const std::string& error_name, const std::string& error) { | 312 const std::string& error_name, const std::string& error) { |
455 error_ = error_name; | 313 error_ = error_name; |
456 SendResponse(false); | 314 SendResponse(false); |
457 } | 315 } |
OLD | NEW |