OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/extensions/networking_private_api.h" | |
6 | |
7 #include "base/bind_helpers.h" | |
8 #include "chrome/browser/extensions/extension_function_registry.h" | |
9 #include "chrome/common/extensions/api/networking_private.h" | |
10 #include "chromeos/dbus/dbus_thread_manager.h" | |
11 #include "chromeos/dbus/shill_manager_client.h" | |
12 #include "chromeos/dbus/shill_service_client.h" | |
13 #include "chromeos/network/onc/onc_constants.h" | |
14 #include "chromeos/network/onc/onc_signature.h" | |
15 #include "chromeos/network/onc/onc_translation_tables.h" | |
16 #include "chromeos/network/onc/onc_translator.h" | |
17 #include "dbus/object_path.h" | |
18 #include "third_party/cros_system_api/dbus/service_constants.h" | |
19 | |
20 using namespace chromeos; | |
21 namespace api = extensions::api::networking_private; | |
22 | |
23 namespace { | |
24 | |
25 // An error returned when no valid services were found. | |
26 const char kInvalidResponseError[] = "Error.invalidResponse"; | |
27 | |
28 // This creates a new ONC dictionary that only contains the information we're | |
29 // interested in passing on to JavaScript. | |
30 scoped_ptr<api::NetworkProperties> CreateFilteredResult( | |
31 const base::DictionaryValue& properties) { | |
32 scoped_ptr<base::DictionaryValue> onc_properties( | |
33 onc::TranslateShillServiceToONCPart( | |
34 properties, | |
35 &onc::kNetworkConfigurationSignature)); | |
36 | |
37 // Now we filter it so we only include properties that we care about for this | |
38 // interface. | |
39 static const char* const desired_fields[] = { | |
40 onc::network_config::kWiFi, | |
41 onc::network_config::kName, | |
42 onc::network_config::kGUID, | |
43 onc::network_config::kType, | |
44 onc::network_config::kConnectionState, | |
45 }; | |
46 | |
47 scoped_ptr<api::NetworkProperties> filtered_result( | |
48 new api::NetworkProperties); | |
49 for (size_t i = 0; i < arraysize(desired_fields); ++i) { | |
50 base::Value* value; | |
51 if (onc_properties->Get(desired_fields[i], &value)) | |
52 filtered_result->additional_properties.Set(desired_fields[i], | |
53 value->DeepCopy()); | |
54 } | |
55 | |
56 return filtered_result.Pass(); | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 //////////////////////////////////////////////////////////////////////////////// | |
62 // NetworkingPrivateGetPropertiesFunction | |
63 | |
64 NetworkingPrivateGetPropertiesFunction:: | |
65 ~NetworkingPrivateGetPropertiesFunction() { | |
66 } | |
67 | |
68 bool NetworkingPrivateGetPropertiesFunction::RunImpl() { | |
69 scoped_ptr<api::GetProperties::Params> params = | |
70 api::GetProperties::Params::Create(*args_); | |
71 EXTENSION_FUNCTION_VALIDATE(params); | |
72 | |
73 // TODO(gspencer): Currently we're using the service path as the | |
74 // |network_guid|. Eventually this should be using the real GUID. | |
75 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | |
76 dbus::ObjectPath(params->network_guid), | |
77 base::Bind(&NetworkingPrivateGetPropertiesFunction::ResultCallback, | |
78 this)); | |
79 return true; | |
80 } | |
81 | |
82 void NetworkingPrivateGetPropertiesFunction::ResultCallback( | |
83 DBusMethodCallStatus call_status, | |
84 const base::DictionaryValue& result) { | |
85 scoped_ptr<api::NetworkProperties> filtered_result( | |
86 CreateFilteredResult(result)); | |
87 results_ = api::GetProperties::Results::Create(*filtered_result); | |
88 SendResponse(true); | |
89 } | |
90 | |
91 //////////////////////////////////////////////////////////////////////////////// | |
92 // NetworkingPrivateGetVisibleNetworksFunction::ResultList | |
not at google - send to devlin
2013/02/02 00:11:32
if you put the method bodies all within the class
Greg Spencer (Chromium)
2013/02/02 01:06:15
Done.
| |
93 class NetworkingPrivateGetVisibleNetworksFunction::ResultList | |
94 : public base::RefCounted<ResultList> { | |
95 public: | |
96 explicit ResultList( | |
97 const base::Callback<void(scoped_ptr<base::ListValue> result)>& callback); | |
98 | |
99 scoped_ptr<base::ListValue> GetResults() { | |
100 return api::GetVisibleNetworks::Results::Create(list_); | |
101 } | |
102 void Append(api::NetworkProperties* value); | |
103 | |
104 private: | |
105 friend class base::RefCounted<ResultList>; | |
106 ~ResultList(); | |
107 | |
108 std::vector<linked_ptr<api::NetworkProperties> > list_; | |
109 base::Callback<void(scoped_ptr<base::ListValue> result)> callback_; | |
110 | |
111 DISALLOW_COPY_AND_ASSIGN(ResultList); | |
112 }; | |
113 | |
114 NetworkingPrivateGetVisibleNetworksFunction::ResultList::ResultList( | |
115 const base::Callback<void(scoped_ptr<base::ListValue> result)>& callback) | |
116 : callback_(callback) { | |
117 } | |
118 | |
119 NetworkingPrivateGetVisibleNetworksFunction::ResultList::~ResultList() { | |
120 callback_.Run(GetResults()); | |
121 } | |
122 | |
123 void NetworkingPrivateGetVisibleNetworksFunction::ResultList::Append( | |
124 api::NetworkProperties* value) { | |
125 list_.push_back(linked_ptr<api::NetworkProperties>(value)); | |
126 } | |
127 | |
128 //////////////////////////////////////////////////////////////////////////////// | |
129 // NetworkingPrivateGetVisibleNetworksFunction | |
130 | |
131 NetworkingPrivateGetVisibleNetworksFunction:: | |
132 ~NetworkingPrivateGetVisibleNetworksFunction() { | |
133 } | |
134 | |
135 bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() { | |
136 scoped_ptr<api::GetVisibleNetworks::Params> params = | |
137 api::GetVisibleNetworks::Params::Create(*args_); | |
138 EXTENSION_FUNCTION_VALIDATE(params); | |
139 | |
140 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties(base::Bind( | |
141 &NetworkingPrivateGetVisibleNetworksFunction::ManagerPropertiesCallback, | |
142 this, | |
143 api::GetVisibleNetworks::Params::ToString(params->type))); | |
144 return true; | |
145 } | |
146 | |
147 // For each of the available services, fire off a request for its properties. | |
148 void NetworkingPrivateGetVisibleNetworksFunction::ManagerPropertiesCallback( | |
149 const std::string& network_type, | |
150 DBusMethodCallStatus call_status, | |
151 const base::DictionaryValue& result) { | |
152 const base::ListValue* available_services; | |
153 if (!result.GetList(flimflam::kServicesProperty, &available_services)) { | |
154 LOG(ERROR) | |
155 << "ShillManagerClient::GetProperties returned malformed service list."; | |
156 error_ = kInvalidResponseError; | |
157 SendResponse(false); | |
158 return; | |
159 } | |
160 scoped_refptr<ResultList> result_list( | |
161 new ResultList( | |
162 base::Bind( | |
163 &NetworkingPrivateGetVisibleNetworksFunction::SendResultCallback, | |
164 this))); | |
165 // If there just are no services, return an empty list. | |
166 if (available_services->GetSize() == 0) { | |
167 results_ = result_list->GetResults(); | |
168 SendResponse(true); | |
169 return; | |
170 } | |
171 for (base::ListValue::const_iterator iter = available_services->begin(); | |
172 iter != available_services->end(); ++iter) { | |
173 std::string service_path; | |
174 if (!(*iter)->GetAsString(&service_path)) { | |
175 LOG(ERROR) | |
176 << "ShillManagerClient::GetProperties returned malformed service."; | |
177 continue; | |
178 } | |
179 | |
180 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | |
181 dbus::ObjectPath(service_path), | |
182 base::Bind( | |
183 &NetworkingPrivateGetVisibleNetworksFunction:: | |
184 ServicePropertiesCallback, | |
185 this, | |
186 service_path, | |
187 network_type, | |
188 result_list)); | |
189 } | |
190 } | |
191 | |
192 // If this network is of the appropriate type, add it to the results and | |
193 // decrement the count. If the count hits zero, then send the result. | |
194 void NetworkingPrivateGetVisibleNetworksFunction::ServicePropertiesCallback( | |
195 const std::string& service_path, | |
196 const std::string& network_type, | |
197 scoped_refptr<ResultList> result_list, | |
198 DBusMethodCallStatus call_status, | |
199 const base::DictionaryValue& result) { | |
200 if (call_status == DBUS_METHOD_CALL_SUCCESS) { | |
201 scoped_ptr<api::NetworkProperties> filtered_result( | |
202 CreateFilteredResult(result)); | |
203 | |
204 std::string onc_type; | |
205 if (filtered_result->additional_properties.GetString( | |
206 onc::network_config::kType, &onc_type) && | |
207 (onc_type == network_type || | |
208 network_type == onc::network_type::kAllTypes)) { | |
209 // TODO(gspencer): For now the "GUID" we send back is going to look | |
not at google - send to devlin
2013/02/02 00:11:32
This is indented 6 spaces. It should only be inden
Greg Spencer (Chromium)
2013/02/02 01:06:15
Whoops. Fixed.
| |
210 // remarkably like the service path. Once this code starts using the | |
211 // NetworkStateHandler instead of Shill directly, we should remove | |
212 // this line so that we're sending back the actual GUID. The | |
213 // JavaScript shouldn't care: this ID is opaque to it, and it | |
214 // shouldn't store it anywhere. | |
215 filtered_result->additional_properties.SetString( | |
216 onc::network_config::kGUID, service_path); | |
217 | |
218 result_list->Append(filtered_result.release()); | |
219 } | |
220 } | |
221 } | |
222 | |
223 void NetworkingPrivateGetVisibleNetworksFunction::SendResultCallback( | |
224 scoped_ptr<base::ListValue> result_list) { | |
225 results_.reset(result_list.release()); | |
226 SendResponse(true); | |
227 } | |
228 | |
229 //////////////////////////////////////////////////////////////////////////////// | |
230 // NetworkingPrivateStartConnectFunction | |
231 | |
232 NetworkingPrivateStartConnectFunction:: | |
233 ~NetworkingPrivateStartConnectFunction() { | |
234 } | |
235 | |
236 void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() { | |
237 SendResponse(true); | |
238 } | |
239 | |
240 void NetworkingPrivateStartConnectFunction::ConnectionStartFailed( | |
241 const std::string& errorName, | |
not at google - send to devlin
2013/02/02 00:11:32
errorName -> error_name
errorMessage -> error_mess
Greg Spencer (Chromium)
2013/02/02 01:06:15
I thought I did this already?? Maybe I just did i
| |
242 const std::string& errorMessage) { | |
243 error_ = errorName; | |
244 SendResponse(false); | |
245 } | |
246 | |
247 bool NetworkingPrivateStartConnectFunction::RunImpl() { | |
248 scoped_ptr<api::StartConnect::Params> params = | |
249 api::StartConnect::Params::Create(*args_); | |
250 EXTENSION_FUNCTION_VALIDATE(params); | |
251 | |
252 // TODO(gspencer): For now, the "guid" we receive from the JavaScript is going | |
253 // to be the service path. Fix this so it actually looks up the service path | |
254 // from the GUID once we're using the NetworkStateHandler. | |
255 std::string service_path = params->network_guid; | |
256 | |
257 DBusThreadManager::Get()->GetShillServiceClient()->Connect( | |
258 dbus::ObjectPath(service_path), | |
259 base::Bind( | |
260 &NetworkingPrivateStartConnectFunction::ConnectionStartSuccess, | |
261 this), | |
262 base::Bind(&NetworkingPrivateStartConnectFunction::ConnectionStartFailed, | |
263 this)); | |
264 return true; | |
265 } | |
266 | |
267 //////////////////////////////////////////////////////////////////////////////// | |
268 // NetworkingPrivateStartDisconnectFunction | |
269 | |
270 NetworkingPrivateStartDisconnectFunction:: | |
271 ~NetworkingPrivateStartDisconnectFunction() { | |
272 } | |
273 | |
274 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() { | |
275 SendResponse(true); | |
276 } | |
277 | |
278 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed( | |
279 const std::string& errorName, | |
not at google - send to devlin
2013/02/02 00:11:32
ditto
?????????
Greg Spencer (Chromium)
2013/02/02 01:06:15
Done.
| |
280 const std::string& errorMessage) { | |
281 error_ = errorName; | |
282 SendResponse(false); | |
283 } | |
284 | |
285 bool NetworkingPrivateStartDisconnectFunction::RunImpl() { | |
286 scoped_ptr<api::StartDisconnect::Params> params = | |
287 api::StartDisconnect::Params::Create(*args_); | |
288 EXTENSION_FUNCTION_VALIDATE(params); | |
289 | |
290 // TODO(gspencer): Currently the |network_guid| parameter is storing the | |
291 // service path. Convert to using the actual guid when we start using | |
292 // the NetworkStateHandler. | |
293 DBusThreadManager::Get()->GetShillServiceClient()->Connect( | |
294 dbus::ObjectPath(params->network_guid), | |
295 base::Bind( | |
296 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess, | |
297 this), | |
298 base::Bind( | |
299 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed, | |
300 this)); | |
301 return true; | |
302 } | |
OLD | NEW |