1 // Copyright 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.
5 #include "extensions/browser/api/networking_private/networking_private_service_client.h"
7 #include "base/base64.h"
9 #include "base/sequenced_task_runner.h"
10 #include "base/threading/worker_pool.h"
11 #include "components/onc/onc_constants.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "extensions/browser/api/networking_private/networking_private_api.h"
14 #include "extensions/browser/api/networking_private/networking_private_delegate_observer.h"
16 using content::BrowserThread
;
17 using wifi::WiFiService
;
19 namespace extensions
{
23 const char kNetworkingPrivateSequenceTokenName
[] = "NetworkingPrivate";
25 // Deletes WiFiService object on the worker thread.
26 void ShutdownWifiServiceOnWorkerThread(scoped_ptr
<WiFiService
> wifi_service
) {
27 DCHECK(wifi_service
.get());
32 NetworkingPrivateServiceClient::ServiceCallbacks::ServiceCallbacks() {
35 NetworkingPrivateServiceClient::ServiceCallbacks::~ServiceCallbacks() {
38 NetworkingPrivateServiceClient::NetworkingPrivateServiceClient(
39 scoped_ptr
<WiFiService
> wifi_service
,
40 scoped_ptr
<VerifyDelegate
> verify_delegate
)
41 : NetworkingPrivateDelegate(verify_delegate
.Pass()),
42 wifi_service_(wifi_service
.Pass()),
44 sequence_token_
= BrowserThread::GetBlockingPool()->GetNamedSequenceToken(
45 kNetworkingPrivateSequenceTokenName
);
47 BrowserThread::GetBlockingPool()
48 ->GetSequencedTaskRunnerWithShutdownBehavior(
49 sequence_token_
, base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN
);
50 task_runner_
->PostTask(
52 base::Bind(&WiFiService::Initialize
,
53 base::Unretained(wifi_service_
.get()), task_runner_
));
54 task_runner_
->PostTask(
57 &WiFiService::SetEventObservers
,
58 base::Unretained(wifi_service_
.get()),
59 base::MessageLoopProxy::current(),
61 &NetworkingPrivateServiceClient::OnNetworksChangedEventOnUIThread
,
62 weak_factory_
.GetWeakPtr()),
63 base::Bind(&NetworkingPrivateServiceClient::
64 OnNetworkListChangedEventOnUIThread
,
65 weak_factory_
.GetWeakPtr())));
66 net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
69 NetworkingPrivateServiceClient::~NetworkingPrivateServiceClient() {
70 // Verify that wifi_service was passed to ShutdownWifiServiceOnWorkerThread to
71 // be deleted after completion of all posted tasks.
72 DCHECK(!wifi_service_
.get());
75 void NetworkingPrivateServiceClient::Shutdown() {
76 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
77 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
78 // Clear callbacks map to release callbacks from UI thread.
79 callbacks_map_
.Clear();
80 // Post ShutdownWifiServiceOnWorkerThread task to delete services when all
81 // posted tasks are done.
82 task_runner_
->PostTask(FROM_HERE
,
83 base::Bind(&ShutdownWifiServiceOnWorkerThread
,
84 base::Passed(&wifi_service_
)));
87 void NetworkingPrivateServiceClient::AddObserver(
88 NetworkingPrivateDelegateObserver
* observer
) {
89 network_events_observers_
.AddObserver(observer
);
92 void NetworkingPrivateServiceClient::RemoveObserver(
93 NetworkingPrivateDelegateObserver
* observer
) {
94 network_events_observers_
.RemoveObserver(observer
);
97 void NetworkingPrivateServiceClient::OnNetworkChanged(
98 net::NetworkChangeNotifier::ConnectionType type
) {
99 task_runner_
->PostTask(FROM_HERE
,
100 base::Bind(&WiFiService::RequestConnectedNetworkUpdate
,
101 base::Unretained(wifi_service_
.get())));
104 NetworkingPrivateServiceClient::ServiceCallbacks
*
105 NetworkingPrivateServiceClient::AddServiceCallbacks() {
106 ServiceCallbacks
* service_callbacks
= new ServiceCallbacks();
107 service_callbacks
->id
= callbacks_map_
.Add(service_callbacks
);
108 return service_callbacks
;
111 void NetworkingPrivateServiceClient::RemoveServiceCallbacks(
112 ServiceCallbacksID callback_id
) {
113 callbacks_map_
.Remove(callback_id
);
116 // NetworkingPrivateServiceClient implementation
118 void NetworkingPrivateServiceClient::GetProperties(
119 const std::string
& guid
,
120 const DictionaryCallback
& success_callback
,
121 const FailureCallback
& failure_callback
) {
122 ServiceCallbacks
* service_callbacks
= AddServiceCallbacks();
123 service_callbacks
->failure_callback
= failure_callback
;
124 service_callbacks
->get_properties_callback
= success_callback
;
126 scoped_ptr
<base::DictionaryValue
> properties(new base::DictionaryValue
);
127 std::string
* error
= new std::string
;
129 base::DictionaryValue
* properties_ptr
= properties
.get();
130 task_runner_
->PostTaskAndReply(
131 FROM_HERE
, base::Bind(&WiFiService::GetProperties
,
132 base::Unretained(wifi_service_
.get()), guid
,
133 properties_ptr
, error
),
134 base::Bind(&NetworkingPrivateServiceClient::AfterGetProperties
,
135 weak_factory_
.GetWeakPtr(), service_callbacks
->id
, guid
,
136 base::Passed(&properties
), base::Owned(error
)));
139 void NetworkingPrivateServiceClient::GetManagedProperties(
140 const std::string
& guid
,
141 const DictionaryCallback
& success_callback
,
142 const FailureCallback
& failure_callback
) {
143 ServiceCallbacks
* service_callbacks
= AddServiceCallbacks();
144 service_callbacks
->failure_callback
= failure_callback
;
145 service_callbacks
->get_properties_callback
= success_callback
;
147 scoped_ptr
<base::DictionaryValue
> properties(new base::DictionaryValue
);
148 std::string
* error
= new std::string
;
150 base::DictionaryValue
* properties_ptr
= properties
.get();
151 task_runner_
->PostTaskAndReply(
152 FROM_HERE
, base::Bind(&WiFiService::GetManagedProperties
,
153 base::Unretained(wifi_service_
.get()), guid
,
154 properties_ptr
, error
),
155 base::Bind(&NetworkingPrivateServiceClient::AfterGetProperties
,
156 weak_factory_
.GetWeakPtr(), service_callbacks
->id
, guid
,
157 base::Passed(&properties
), base::Owned(error
)));
160 void NetworkingPrivateServiceClient::GetState(
161 const std::string
& guid
,
162 const DictionaryCallback
& success_callback
,
163 const FailureCallback
& failure_callback
) {
164 ServiceCallbacks
* service_callbacks
= AddServiceCallbacks();
165 service_callbacks
->failure_callback
= failure_callback
;
166 service_callbacks
->get_properties_callback
= success_callback
;
168 scoped_ptr
<base::DictionaryValue
> properties(new base::DictionaryValue
);
169 std::string
* error
= new std::string
;
171 base::DictionaryValue
* properties_ptr
= properties
.get();
172 task_runner_
->PostTaskAndReply(
174 base::Bind(&WiFiService::GetState
, base::Unretained(wifi_service_
.get()),
175 guid
, properties_ptr
, error
),
176 base::Bind(&NetworkingPrivateServiceClient::AfterGetProperties
,
177 weak_factory_
.GetWeakPtr(), service_callbacks
->id
, guid
,
178 base::Passed(&properties
), base::Owned(error
)));
181 void NetworkingPrivateServiceClient::SetProperties(
182 const std::string
& guid
,
183 scoped_ptr
<base::DictionaryValue
> properties
,
184 const VoidCallback
& success_callback
,
185 const FailureCallback
& failure_callback
) {
186 ServiceCallbacks
* service_callbacks
= AddServiceCallbacks();
187 service_callbacks
->failure_callback
= failure_callback
;
188 service_callbacks
->set_properties_callback
= success_callback
;
190 std::string
* error
= new std::string
;
192 task_runner_
->PostTaskAndReply(
193 FROM_HERE
, base::Bind(&WiFiService::SetProperties
,
194 base::Unretained(wifi_service_
.get()), guid
,
195 base::Passed(&properties
), error
),
196 base::Bind(&NetworkingPrivateServiceClient::AfterSetProperties
,
197 weak_factory_
.GetWeakPtr(), service_callbacks
->id
,
198 base::Owned(error
)));
201 void NetworkingPrivateServiceClient::CreateNetwork(
203 scoped_ptr
<base::DictionaryValue
> properties
,
204 const StringCallback
& success_callback
,
205 const FailureCallback
& failure_callback
) {
206 ServiceCallbacks
* service_callbacks
= AddServiceCallbacks();
207 service_callbacks
->failure_callback
= failure_callback
;
208 service_callbacks
->create_network_callback
= success_callback
;
210 std::string
* network_guid
= new std::string
;
211 std::string
* error
= new std::string
;
213 task_runner_
->PostTaskAndReply(
214 FROM_HERE
, base::Bind(&WiFiService::CreateNetwork
,
215 base::Unretained(wifi_service_
.get()), shared
,
216 base::Passed(&properties
), network_guid
, error
),
217 base::Bind(&NetworkingPrivateServiceClient::AfterCreateNetwork
,
218 weak_factory_
.GetWeakPtr(), service_callbacks
->id
,
219 base::Owned(network_guid
), base::Owned(error
)));
222 void NetworkingPrivateServiceClient::ForgetNetwork(
223 const std::string
& guid
,
224 const VoidCallback
& success_callback
,
225 const FailureCallback
& failure_callback
) {
226 // TODO(mef): Implement for Win/Mac
227 failure_callback
.Run(networking_private::kErrorNotSupported
);
230 void NetworkingPrivateServiceClient::GetNetworks(
231 const std::string
& network_type
,
232 bool configured_only
,
235 const NetworkListCallback
& success_callback
,
236 const FailureCallback
& failure_callback
) {
237 ServiceCallbacks
* service_callbacks
= AddServiceCallbacks();
238 service_callbacks
->failure_callback
= failure_callback
;
239 service_callbacks
->get_visible_networks_callback
= success_callback
;
241 scoped_ptr
<base::ListValue
> networks(new base::ListValue
);
243 // TODO(stevenjb/mef): Apply filters (configured, visible, limit).
245 base::ListValue
* networks_ptr
= networks
.get();
246 task_runner_
->PostTaskAndReply(
247 FROM_HERE
, base::Bind(&WiFiService::GetVisibleNetworks
,
248 base::Unretained(wifi_service_
.get()), network_type
,
249 networks_ptr
, false),
250 base::Bind(&NetworkingPrivateServiceClient::AfterGetVisibleNetworks
,
251 weak_factory_
.GetWeakPtr(), service_callbacks
->id
,
252 base::Passed(&networks
)));
255 void NetworkingPrivateServiceClient::StartConnect(
256 const std::string
& guid
,
257 const VoidCallback
& success_callback
,
258 const FailureCallback
& failure_callback
) {
259 ServiceCallbacks
* service_callbacks
= AddServiceCallbacks();
260 service_callbacks
->failure_callback
= failure_callback
;
261 service_callbacks
->start_connect_callback
= success_callback
;
263 std::string
* error
= new std::string
;
265 task_runner_
->PostTaskAndReply(
266 FROM_HERE
, base::Bind(&WiFiService::StartConnect
,
267 base::Unretained(wifi_service_
.get()), guid
, error
),
268 base::Bind(&NetworkingPrivateServiceClient::AfterStartConnect
,
269 weak_factory_
.GetWeakPtr(), service_callbacks
->id
,
270 base::Owned(error
)));
273 void NetworkingPrivateServiceClient::StartDisconnect(
274 const std::string
& guid
,
275 const VoidCallback
& success_callback
,
276 const FailureCallback
& failure_callback
) {
277 ServiceCallbacks
* service_callbacks
= AddServiceCallbacks();
278 service_callbacks
->failure_callback
= failure_callback
;
279 service_callbacks
->start_disconnect_callback
= success_callback
;
281 std::string
* error
= new std::string
;
283 task_runner_
->PostTaskAndReply(
284 FROM_HERE
, base::Bind(&WiFiService::StartDisconnect
,
285 base::Unretained(wifi_service_
.get()), guid
, error
),
286 base::Bind(&NetworkingPrivateServiceClient::AfterStartDisconnect
,
287 weak_factory_
.GetWeakPtr(), service_callbacks
->id
,
288 base::Owned(error
)));
291 void NetworkingPrivateServiceClient::SetWifiTDLSEnabledState(
292 const std::string
& ip_or_mac_address
,
294 const StringCallback
& success_callback
,
295 const FailureCallback
& failure_callback
) {
296 failure_callback
.Run(networking_private::kErrorNotSupported
);
299 void NetworkingPrivateServiceClient::GetWifiTDLSStatus(
300 const std::string
& ip_or_mac_address
,
301 const StringCallback
& success_callback
,
302 const FailureCallback
& failure_callback
) {
303 failure_callback
.Run(networking_private::kErrorNotSupported
);
306 void NetworkingPrivateServiceClient::GetCaptivePortalStatus(
307 const std::string
& guid
,
308 const StringCallback
& success_callback
,
309 const FailureCallback
& failure_callback
) {
310 failure_callback
.Run(networking_private::kErrorNotSupported
);
313 scoped_ptr
<base::ListValue
>
314 NetworkingPrivateServiceClient::GetEnabledNetworkTypes() {
315 scoped_ptr
<base::ListValue
> network_list
;
316 network_list
->AppendString(::onc::network_type::kWiFi
);
317 return network_list
.Pass();
320 scoped_ptr
<NetworkingPrivateDelegate::DeviceStateList
>
321 NetworkingPrivateServiceClient::GetDeviceStateList() {
322 scoped_ptr
<DeviceStateList
> device_state_list(new DeviceStateList
);
323 scoped_ptr
<core_api::networking_private::DeviceStateProperties
> properties(
324 new core_api::networking_private::DeviceStateProperties
);
325 properties
->type
= core_api::networking_private::NETWORK_TYPE_WIFI
;
326 properties
->state
= core_api::networking_private::DEVICE_STATE_TYPE_ENABLED
;
327 device_state_list
->push_back(properties
.Pass());
328 return device_state_list
.Pass();
331 bool NetworkingPrivateServiceClient::EnableNetworkType(
332 const std::string
& type
) {
336 bool NetworkingPrivateServiceClient::DisableNetworkType(
337 const std::string
& type
) {
341 bool NetworkingPrivateServiceClient::RequestScan() {
342 task_runner_
->PostTask(FROM_HERE
,
343 base::Bind(&WiFiService::RequestNetworkScan
,
344 base::Unretained(wifi_service_
.get())));
348 ////////////////////////////////////////////////////////////////////////////////
350 void NetworkingPrivateServiceClient::AfterGetProperties(
351 ServiceCallbacksID callback_id
,
352 const std::string
& network_guid
,
353 scoped_ptr
<base::DictionaryValue
> properties
,
354 const std::string
* error
) {
355 ServiceCallbacks
* service_callbacks
= callbacks_map_
.Lookup(callback_id
);
356 DCHECK(service_callbacks
);
357 if (!error
->empty()) {
358 DCHECK(!service_callbacks
->failure_callback
.is_null());
359 service_callbacks
->failure_callback
.Run(*error
);
361 DCHECK(!service_callbacks
->get_properties_callback
.is_null());
362 service_callbacks
->get_properties_callback
.Run(properties
.Pass());
364 RemoveServiceCallbacks(callback_id
);
367 void NetworkingPrivateServiceClient::AfterGetVisibleNetworks(
368 ServiceCallbacksID callback_id
,
369 scoped_ptr
<base::ListValue
> networks
) {
370 ServiceCallbacks
* service_callbacks
= callbacks_map_
.Lookup(callback_id
);
371 DCHECK(service_callbacks
);
372 DCHECK(!service_callbacks
->get_visible_networks_callback
.is_null());
373 service_callbacks
->get_visible_networks_callback
.Run(networks
.Pass());
374 RemoveServiceCallbacks(callback_id
);
377 void NetworkingPrivateServiceClient::AfterSetProperties(
378 ServiceCallbacksID callback_id
,
379 const std::string
* error
) {
380 ServiceCallbacks
* service_callbacks
= callbacks_map_
.Lookup(callback_id
);
381 DCHECK(service_callbacks
);
382 if (!error
->empty()) {
383 DCHECK(!service_callbacks
->failure_callback
.is_null());
384 service_callbacks
->failure_callback
.Run(*error
);
386 DCHECK(!service_callbacks
->set_properties_callback
.is_null());
387 service_callbacks
->set_properties_callback
.Run();
389 RemoveServiceCallbacks(callback_id
);
392 void NetworkingPrivateServiceClient::AfterCreateNetwork(
393 ServiceCallbacksID callback_id
,
394 const std::string
* network_guid
,
395 const std::string
* error
) {
396 ServiceCallbacks
* service_callbacks
= callbacks_map_
.Lookup(callback_id
);
397 DCHECK(service_callbacks
);
398 if (!error
->empty()) {
399 DCHECK(!service_callbacks
->failure_callback
.is_null());
400 service_callbacks
->failure_callback
.Run(*error
);
402 DCHECK(!service_callbacks
->create_network_callback
.is_null());
403 service_callbacks
->create_network_callback
.Run(*network_guid
);
405 RemoveServiceCallbacks(callback_id
);
408 void NetworkingPrivateServiceClient::AfterStartConnect(
409 ServiceCallbacksID callback_id
,
410 const std::string
* error
) {
411 ServiceCallbacks
* service_callbacks
= callbacks_map_
.Lookup(callback_id
);
412 DCHECK(service_callbacks
);
413 if (!error
->empty()) {
414 DCHECK(!service_callbacks
->failure_callback
.is_null());
415 service_callbacks
->failure_callback
.Run(*error
);
417 DCHECK(!service_callbacks
->start_connect_callback
.is_null());
418 service_callbacks
->start_connect_callback
.Run();
420 RemoveServiceCallbacks(callback_id
);
423 void NetworkingPrivateServiceClient::AfterStartDisconnect(
424 ServiceCallbacksID callback_id
,
425 const std::string
* error
) {
426 ServiceCallbacks
* service_callbacks
= callbacks_map_
.Lookup(callback_id
);
427 DCHECK(service_callbacks
);
428 if (!error
->empty()) {
429 DCHECK(!service_callbacks
->failure_callback
.is_null());
430 service_callbacks
->failure_callback
.Run(*error
);
432 DCHECK(!service_callbacks
->start_disconnect_callback
.is_null());
433 service_callbacks
->start_disconnect_callback
.Run();
435 RemoveServiceCallbacks(callback_id
);
438 void NetworkingPrivateServiceClient::OnNetworksChangedEventOnUIThread(
439 const std::vector
<std::string
>& network_guids
) {
440 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
441 FOR_EACH_OBSERVER(NetworkingPrivateDelegateObserver
,
442 network_events_observers_
,
443 OnNetworksChangedEvent(network_guids
));
446 void NetworkingPrivateServiceClient::OnNetworkListChangedEventOnUIThread(
447 const std::vector
<std::string
>& network_guids
) {
448 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
449 FOR_EACH_OBSERVER(NetworkingPrivateDelegateObserver
,
450 network_events_observers_
,
451 OnNetworkListChangedEvent(network_guids
));
454 } // namespace extensions