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/chromeos/extensions/networking_private_api_factory.h" | |
9 #include "chrome/browser/extensions/extension_function_registry.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 namespace chromeos { | |
21 | |
22 namespace { | |
23 | |
24 // An error returned when no valid services were found. | |
25 const char kInvalidResponseError[] = "Error.invalidResponse"; | |
26 | |
27 // This creates a new ONC dictionary that only contains the information we're | |
28 // interested in passing on to JavaScript. | |
29 base::DictionaryValue* CreateFilteredResult( | |
30 const base::DictionaryValue& properties) { | |
31 scoped_ptr<base::DictionaryValue> onc_properties( | |
32 onc::TranslateShillServiceToONCPart( | |
33 properties, | |
34 &onc::kNetworkConfigurationSignature)); | |
35 | |
36 // Now we filter it so we only include properties that we care about for this | |
37 // interface. | |
38 static const char* const desired_fields[] = { | |
39 onc::network_config::kWiFi, | |
40 onc::network_config::kName, | |
41 onc::network_config::kGUID, | |
42 onc::network_config::kType, | |
43 onc::network_config::kState, | |
44 }; | |
45 | |
46 scoped_ptr<base::DictionaryValue> filtered_result(new base::DictionaryValue); | |
47 for (size_t i = 0; i < arraysize(desired_fields); ++i) { | |
48 base::Value* value; | |
49 if (onc_properties->Get(desired_fields[i], &value)) | |
50 filtered_result->Set(desired_fields[i], value->DeepCopy()); | |
51 } | |
52 return filtered_result.release(); | |
53 } | |
54 | |
55 } // namespace | |
56 | |
57 //////////////////////////////////////////////////////////////////////////////// | |
58 // NetworkingGetPropertiesFunction | |
59 | |
60 NetworkingGetPropertiesFunction::~NetworkingGetPropertiesFunction() { | |
61 } | |
62 | |
63 bool NetworkingGetPropertiesFunction::RunImpl() { | |
64 std::string service_path; | |
65 if (!args_->GetString(0, &service_path)) | |
66 return false; | |
67 | |
68 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | |
69 dbus::ObjectPath(service_path), base::Bind( | |
70 &NetworkingGetPropertiesFunction::ResultCallback, this)); | |
71 return true; | |
72 } | |
73 | |
74 void NetworkingGetPropertiesFunction::ResultCallback( | |
75 DBusMethodCallStatus call_status, | |
76 const base::DictionaryValue& result) { | |
77 scoped_ptr<base::DictionaryValue> filtered_result( | |
78 CreateFilteredResult(result)); | |
79 | |
80 SetResult(filtered_result.release()); | |
81 SendResponse(true); | |
82 } | |
83 | |
84 //////////////////////////////////////////////////////////////////////////////// | |
85 // NetworkingGetVisibleNetworksFunction::ResultList | |
86 class NetworkingGetVisibleNetworksFunction::ResultList | |
87 : public base::RefCounted<ResultList> { | |
88 public: | |
89 explicit ResultList(int count); | |
90 | |
91 int count() const { return count_; } | |
92 void decrement() { --count_; } | |
93 base::ListValue* release() { return list_.release(); } | |
94 void Append(base::Value* value); | |
95 | |
96 private: | |
97 friend class base::RefCounted<ResultList>; | |
98 ~ResultList(); | |
99 | |
100 scoped_ptr<base::ListValue> list_; | |
101 int count_; | |
102 | |
103 DISALLOW_COPY_AND_ASSIGN(ResultList); | |
104 }; | |
105 | |
106 NetworkingGetVisibleNetworksFunction::ResultList::ResultList(int count) | |
107 : list_(new base::ListValue), count_(count) { | |
108 } | |
109 | |
110 NetworkingGetVisibleNetworksFunction::ResultList::~ResultList() { | |
111 } | |
112 | |
113 void NetworkingGetVisibleNetworksFunction::ResultList::Append( | |
114 base::Value* value) { | |
115 list_->Append(value); | |
116 } | |
117 | |
118 //////////////////////////////////////////////////////////////////////////////// | |
119 // NetworkingGetVisibleNetworksFunction | |
120 | |
121 NetworkingGetVisibleNetworksFunction::~NetworkingGetVisibleNetworksFunction() { | |
122 } | |
123 | |
124 bool NetworkingGetVisibleNetworksFunction::RunImpl() { | |
125 std::string network_type; | |
126 if (!args_->GetString(0, &network_type)) | |
127 return false; | |
128 | |
129 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties( | |
130 base::Bind( | |
131 &NetworkingGetVisibleNetworksFunction::ManagerPropertiesCallback, | |
132 this, | |
133 network_type)); | |
134 return true; | |
135 } | |
136 | |
137 // For each of the available services, fire off a request for its properties. | |
138 void NetworkingGetVisibleNetworksFunction::ManagerPropertiesCallback( | |
139 const std::string& network_type, | |
140 DBusMethodCallStatus call_status, | |
141 const base::DictionaryValue& result) { | |
142 const base::ListValue* available_services; | |
143 if (!result.GetList(flimflam::kServicesProperty, &available_services)) { | |
144 LOG(ERROR) | |
145 << "ShillManagerClient::GetProperties returned malformed service list."; | |
146 error_ = kInvalidResponseError; | |
147 SendResponse(false); | |
148 return; | |
149 } | |
150 scoped_refptr<ResultList> result_list( | |
151 new ResultList(available_services->GetSize())); | |
152 // If there just are no services, return an empty list. | |
153 if (available_services->GetSize() == 0) { | |
154 SetResult(result_list->release()); | |
155 SendResponse(true); | |
156 return; | |
157 } | |
158 for (base::ListValue::const_iterator iter = available_services->begin(); | |
159 iter != available_services->end(); ++iter) { | |
160 std::string service_path; | |
161 if (!(*iter)->GetAsString(&service_path)) { | |
162 LOG(ERROR) | |
163 << "ShillManagerClient::GetProperties returned malformed service."; | |
164 result_list->decrement(); | |
165 continue; | |
166 } | |
167 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | |
168 dbus::ObjectPath(service_path), | |
169 base::Bind( | |
170 &NetworkingGetVisibleNetworksFunction::ServicePropertiesCallback, | |
171 this, | |
172 service_path, | |
173 network_type, | |
174 result_list)); | |
175 } | |
176 // All of the results were malformed, so we fail. | |
pneubeck (no reviews)
2013/01/21 09:28:51
'results' is misleading -> 'service paths'
Greg Spencer (Chromium)
2013/01/22 19:44:29
Done.
| |
177 if (result_list->count() == 0) { | |
178 error_ = kInvalidResponseError; | |
179 SendResponse(false); | |
180 } | |
181 } | |
182 | |
183 // If this network is of the appropriate type, add it to the results and | |
184 // decrement the count. If the count hits zero, then send the result. | |
185 void NetworkingGetVisibleNetworksFunction::ServicePropertiesCallback( | |
186 const std::string& service_path, | |
187 const std::string& network_type, | |
188 scoped_refptr<ResultList> result_list, | |
189 DBusMethodCallStatus call_status, | |
190 const base::DictionaryValue& result) { | |
191 if (call_status == DBUS_METHOD_CALL_SUCCESS) { | |
pneubeck (no reviews)
2013/01/21 09:28:51
why not
if ( .. && .. && .. )
instead of nestin
Greg Spencer (Chromium)
2013/01/22 19:44:29
Done.
| |
192 std::string shill_type; | |
193 if (result.GetString(flimflam::kTypeProperty, &shill_type)) { | |
194 std::string onc_type; | |
195 if (TranslateStringToONC(onc::kNetworkTypeTable, shill_type, &onc_type)) { | |
pneubeck (no reviews)
2013/01/21 09:28:51
With my suggestion about adding a OncValueSignatur
pneubeck (no reviews)
2013/01/21 12:12:09
I thought more about this and tried to implement s
Greg Spencer (Chromium)
2013/01/22 19:44:29
OK, I'll take that path. I'll remove the exports
| |
196 if (onc_type == network_type || | |
197 network_type == onc::network_type::kAllTypes) { | |
pneubeck (no reviews)
2013/01/21 09:28:51
kAllTypes should be in the list of ONC constants,
Greg Spencer (Chromium)
2013/01/22 19:44:29
Hmm. Maybe I misunderstood your other comment. F
pneubeck (no reviews)
2013/01/23 09:14:21
Sorry, misstyped. I meant "shouldn't".
But I get
| |
198 scoped_ptr<base::DictionaryValue> filtered_result( | |
199 CreateFilteredResult(result)); | |
200 | |
201 // TODO(gspencer): For now the "guid" we send back is going to look | |
202 // remarkably like the service path. Once this code starts using the | |
203 // NetworkStateHandler instead of Shill directly, we should remove | |
204 // this line so that we're sending back the actual GUID. The | |
205 // JavaScript shouldn't care: this ID is opaque to it, and it | |
206 // shouldn't store it anywhere. | |
207 filtered_result->SetString(onc::network_config::kGUID, service_path); | |
208 | |
209 result_list->Append(filtered_result.release()); | |
210 } | |
211 } | |
212 } | |
213 } | |
214 // We must decrement the count each time we get a callback, regardless of the | |
215 // outcome, or we'll never send a response. | |
216 result_list->decrement(); | |
217 if (result_list->count() == 0) { | |
218 SetResult(result_list->release()); | |
219 SendResponse(true); | |
220 } | |
221 } | |
222 | |
223 //////////////////////////////////////////////////////////////////////////////// | |
224 // NetworkingRequestConnectFunction | |
225 | |
226 NetworkingRequestConnectFunction::~NetworkingRequestConnectFunction() { | |
227 } | |
228 | |
229 void NetworkingRequestConnectFunction::ConnectRequestSuccess() { | |
230 SendResponse(true); | |
231 } | |
232 | |
233 void NetworkingRequestConnectFunction::ConnectRequestFailed( | |
234 const std::string& errorName, | |
235 const std::string& errorMessage) { | |
236 error_ = errorName; | |
237 SendResponse(false); | |
238 } | |
239 | |
240 bool NetworkingRequestConnectFunction::RunImpl() { | |
241 std::string guid; | |
242 if (!args_->GetString(0, &guid)) | |
243 return false; | |
244 | |
245 // TODO(gspencer): For now, the "guid" we receive from the JavaScript is going | |
246 // to be the service path. Fix this so it actually looks up the service path | |
247 // from the GUID once we're using the NetworkStateHandler. | |
248 std::string service_path = guid; | |
249 | |
250 DBusThreadManager::Get()->GetShillServiceClient()->Connect( | |
251 dbus::ObjectPath(service_path), | |
252 base::Bind(&NetworkingRequestConnectFunction::ConnectRequestSuccess, | |
253 this), | |
254 base::Bind(&NetworkingRequestConnectFunction::ConnectRequestFailed, | |
255 this)); | |
256 return true; | |
257 } | |
258 | |
259 //////////////////////////////////////////////////////////////////////////////// | |
260 // NetworkingRequestDisconnectFunction | |
261 | |
262 NetworkingRequestDisconnectFunction::~NetworkingRequestDisconnectFunction() { | |
263 } | |
264 | |
265 void NetworkingRequestDisconnectFunction::DisconnectRequestSuccess() { | |
266 SendResponse(true); | |
267 } | |
268 | |
269 void NetworkingRequestDisconnectFunction::DisconnectRequestFailed( | |
270 const std::string& errorName, | |
271 const std::string& errorMessage) { | |
272 error_ = errorName; | |
273 SendResponse(false); | |
274 } | |
275 | |
276 bool NetworkingRequestDisconnectFunction::RunImpl() { | |
277 std::string service_path; | |
278 if (!args_->GetString(0, &service_path)) | |
279 return false; | |
280 | |
281 DBusThreadManager::Get()->GetShillServiceClient()->Connect( | |
282 dbus::ObjectPath(service_path), | |
283 base::Bind(&NetworkingRequestDisconnectFunction::DisconnectRequestSuccess, | |
284 this), | |
285 base::Bind(&NetworkingRequestDisconnectFunction::DisconnectRequestFailed, | |
286 this)); | |
287 return true; | |
288 } | |
289 | |
290 //////////////////////////////////////////////////////////////////////////////// | |
291 // NetworkingPrivateAPI | |
292 | |
293 NetworkingPrivateAPI::NetworkingPrivateAPI(Profile* profile) { | |
294 ExtensionFunctionRegistry* registry = | |
295 ExtensionFunctionRegistry::GetInstance(); | |
296 registry->RegisterFunction<NetworkingGetPropertiesFunction>(); | |
297 registry->RegisterFunction<NetworkingGetVisibleNetworksFunction>(); | |
298 registry->RegisterFunction<NetworkingRequestConnectFunction>(); | |
299 registry->RegisterFunction<NetworkingRequestDisconnectFunction>(); | |
300 } | |
301 | |
302 NetworkingPrivateAPI::~NetworkingPrivateAPI() { | |
303 } | |
304 | |
305 void NetworkingPrivateAPI::Shutdown() { | |
306 } | |
307 | |
308 // static | |
309 NetworkingPrivateAPI* NetworkingPrivateAPI::Get(Profile* profile) { | |
310 return NetworkingPrivateAPIFactory::GetForProfile(profile); | |
311 } | |
312 | |
313 } // namespace chromeos | |
OLD | NEW |