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"; | |
not at google - send to devlin
2013/02/01 19:09:42
These errors are printed in the extension's consol
| |
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 | |
not at google - send to devlin
2013/02/01 19:09:42
I find these big comments distracting. Of course,
| |
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 | |
93 class NetworkingPrivateGetVisibleNetworksFunction::ResultList | |
94 : public base::RefCounted<ResultList> { | |
95 public: | |
96 explicit ResultList(int count); | |
97 | |
98 int count() const { return count_; } | |
99 void decrement() { --count_; } | |
100 scoped_ptr<base::ListValue> GetResults() { | |
101 return api::GetVisibleNetworks::Results::Create(list_); | |
102 } | |
103 void Append(api::NetworkProperties* value); | |
104 | |
105 private: | |
106 friend class base::RefCounted<ResultList>; | |
107 ~ResultList(); | |
108 | |
109 std::vector<linked_ptr<api::NetworkProperties> > list_; | |
110 int count_; | |
111 | |
112 DISALLOW_COPY_AND_ASSIGN(ResultList); | |
113 }; | |
114 | |
115 NetworkingPrivateGetVisibleNetworksFunction::ResultList::ResultList(int count) | |
116 : count_(count) { | |
117 } | |
118 | |
119 NetworkingPrivateGetVisibleNetworksFunction::ResultList::~ResultList() { | |
120 } | |
121 | |
122 void NetworkingPrivateGetVisibleNetworksFunction::ResultList::Append( | |
123 api::NetworkProperties* value) { | |
124 list_.push_back(linked_ptr<api::NetworkProperties>(value)); | |
not at google - send to devlin
2013/02/01 19:09:42
implement these methods inline?
| |
125 } | |
126 | |
127 //////////////////////////////////////////////////////////////////////////////// | |
128 // NetworkingPrivateGetVisibleNetworksFunction | |
129 | |
130 NetworkingPrivateGetVisibleNetworksFunction:: | |
131 ~NetworkingPrivateGetVisibleNetworksFunction() { | |
132 } | |
133 | |
134 bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() { | |
135 scoped_ptr<api::GetVisibleNetworks::Params> params = | |
136 api::GetVisibleNetworks::Params::Create(*args_); | |
137 EXTENSION_FUNCTION_VALIDATE(params); | |
138 | |
139 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties(base::Bind( | |
140 &NetworkingPrivateGetVisibleNetworksFunction::ManagerPropertiesCallback, | |
141 this, | |
142 api::GetVisibleNetworks::Params::ToString(params->type))); | |
143 return true; | |
144 } | |
145 | |
146 // For each of the available services, fire off a request for its properties. | |
147 void NetworkingPrivateGetVisibleNetworksFunction::ManagerPropertiesCallback( | |
148 const std::string& network_type, | |
149 DBusMethodCallStatus call_status, | |
150 const base::DictionaryValue& result) { | |
151 const base::ListValue* available_services; | |
152 if (!result.GetList(flimflam::kServicesProperty, &available_services)) { | |
153 LOG(ERROR) | |
154 << "ShillManagerClient::GetProperties returned malformed service list."; | |
155 error_ = kInvalidResponseError; | |
156 SendResponse(false); | |
157 return; | |
158 } | |
159 scoped_refptr<ResultList> result_list( | |
160 new ResultList(available_services->GetSize())); | |
161 // If there just are no services, return an empty list. | |
162 if (available_services->GetSize() == 0) { | |
163 results_ = result_list->GetResults(); | |
164 SendResponse(true); | |
165 return; | |
166 } | |
167 for (base::ListValue::const_iterator iter = available_services->begin(); | |
168 iter != available_services->end(); ++iter) { | |
169 std::string service_path; | |
170 if (!(*iter)->GetAsString(&service_path)) { | |
171 LOG(ERROR) | |
172 << "ShillManagerClient::GetProperties returned malformed service."; | |
173 result_list->decrement(); | |
174 continue; | |
175 } | |
176 | |
177 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | |
178 dbus::ObjectPath(service_path), | |
179 base::Bind( | |
180 &NetworkingPrivateGetVisibleNetworksFunction:: | |
181 ServicePropertiesCallback, | |
182 this, | |
183 service_path, | |
184 network_type, | |
185 result_list)); | |
186 } | |
187 // All of the service paths were malformed, so we fail. | |
188 if (result_list->count() == 0) { | |
189 error_ = kInvalidResponseError; | |
190 SendResponse(false); | |
191 } | |
192 } | |
193 | |
194 // If this network is of the appropriate type, add it to the results and | |
195 // decrement the count. If the count hits zero, then send the result. | |
196 void NetworkingPrivateGetVisibleNetworksFunction::ServicePropertiesCallback( | |
197 const std::string& service_path, | |
198 const std::string& network_type, | |
199 scoped_refptr<ResultList> result_list, | |
200 DBusMethodCallStatus call_status, | |
201 const base::DictionaryValue& result) { | |
202 if (call_status == DBUS_METHOD_CALL_SUCCESS) { | |
203 scoped_ptr<api::NetworkProperties> filtered_result( | |
204 CreateFilteredResult(result)); | |
205 | |
206 std::string onc_type; | |
207 if (filtered_result->additional_properties.GetString( | |
208 onc::network_config::kType, &onc_type) && | |
209 (onc_type == network_type || | |
210 network_type == onc::network_type::kAllTypes)) { | |
211 // TODO(gspencer): For now the "guid" we send back is going to look | |
not at google - send to devlin
2013/02/01 19:09:42
too much indent
| |
212 // remarkably like the service path. Once this code starts using the | |
213 // NetworkStateHandler instead of Shill directly, we should remove | |
214 // this line so that we're sending back the actual GUID. The | |
215 // JavaScript shouldn't care: this ID is opaque to it, and it | |
216 // shouldn't store it anywhere. | |
217 filtered_result->additional_properties.SetString( | |
218 onc::network_config::kGUID, service_path); | |
219 | |
220 result_list->Append(filtered_result.release()); | |
221 } | |
222 } | |
223 // We must decrement the count each time we get a callback, regardless of the | |
224 // outcome, or we'll never send a response. | |
225 result_list->decrement(); | |
226 if (result_list->count() == 0) { | |
227 results_ = result_list->GetResults(); | |
228 SendResponse(true); | |
229 } | |
230 } | |
231 | |
232 //////////////////////////////////////////////////////////////////////////////// | |
233 // NetworkingPrivateRequestConnectFunction | |
234 | |
235 NetworkingPrivateRequestConnectFunction:: | |
236 ~NetworkingPrivateRequestConnectFunction() { | |
237 } | |
238 | |
239 void NetworkingPrivateRequestConnectFunction::ConnectRequestSuccess() { | |
240 SendResponse(true); | |
241 } | |
242 | |
243 void NetworkingPrivateRequestConnectFunction::ConnectRequestFailed( | |
244 const std::string& errorName, | |
245 const std::string& errorMessage) { | |
246 error_ = errorName; | |
247 SendResponse(false); | |
248 } | |
249 | |
250 bool NetworkingPrivateRequestConnectFunction::RunImpl() { | |
251 scoped_ptr<api::RequestConnect::Params> params = | |
252 api::RequestConnect::Params::Create(*args_); | |
253 EXTENSION_FUNCTION_VALIDATE(params); | |
254 | |
255 // TODO(gspencer): For now, the "guid" we receive from the JavaScript is going | |
256 // to be the service path. Fix this so it actually looks up the service path | |
257 // from the GUID once we're using the NetworkStateHandler. | |
258 std::string service_path = params->network_guid; | |
259 | |
260 DBusThreadManager::Get()->GetShillServiceClient()->Connect( | |
261 dbus::ObjectPath(service_path), | |
262 base::Bind( | |
263 &NetworkingPrivateRequestConnectFunction::ConnectRequestSuccess, | |
264 this), | |
265 base::Bind(&NetworkingPrivateRequestConnectFunction::ConnectRequestFailed, | |
266 this)); | |
267 return true; | |
268 } | |
269 | |
270 //////////////////////////////////////////////////////////////////////////////// | |
271 // NetworkingPrivateRequestDisconnectFunction | |
272 | |
273 NetworkingPrivateRequestDisconnectFunction:: | |
274 ~NetworkingPrivateRequestDisconnectFunction() { | |
275 } | |
276 | |
277 void NetworkingPrivateRequestDisconnectFunction::DisconnectRequestSuccess() { | |
278 SendResponse(true); | |
279 } | |
280 | |
281 void NetworkingPrivateRequestDisconnectFunction::DisconnectRequestFailed( | |
282 const std::string& errorName, | |
283 const std::string& errorMessage) { | |
not at google - send to devlin
2013/02/01 19:09:42
errorName/errorMessage -> error_name/error_message
| |
284 error_ = errorName; | |
285 SendResponse(false); | |
286 } | |
287 | |
288 bool NetworkingPrivateRequestDisconnectFunction::RunImpl() { | |
289 scoped_ptr<api::RequestDisconnect::Params> params = | |
290 api::RequestDisconnect::Params::Create(*args_); | |
291 EXTENSION_FUNCTION_VALIDATE(params); | |
292 | |
293 // TODO(gspencer): Currently the |network_guid| parameter is storing the | |
294 // service path. Convert to using the actual guid when we start using | |
295 // the NetworkStateHandler. | |
296 DBusThreadManager::Get()->GetShillServiceClient()->Connect( | |
297 dbus::ObjectPath(params->network_guid), | |
298 base::Bind( | |
299 &NetworkingPrivateRequestDisconnectFunction::DisconnectRequestSuccess, | |
300 this), | |
301 base::Bind( | |
302 &NetworkingPrivateRequestDisconnectFunction::DisconnectRequestFailed, | |
303 this)); | |
304 return true; | |
305 } | |
OLD | NEW |