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 "content/public/browser/browser_thread.h"
12 #include "extensions/browser/api/networking_private/networking_private_api.h"
13 #include "extensions/browser/api/networking_private/networking_private_delegate_observer.h"
14 #include "extensions/common/api/networking_private.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::GetNetworks(
223 const std::string
& network_type
,
224 bool configured_only
,
227 const NetworkListCallback
& success_callback
,
228 const FailureCallback
& failure_callback
) {
229 ServiceCallbacks
* service_callbacks
= AddServiceCallbacks();
230 service_callbacks
->failure_callback
= failure_callback
;
231 service_callbacks
->get_visible_networks_callback
= success_callback
;
233 scoped_ptr
<base::ListValue
> networks(new base::ListValue
);
235 // TODO(stevenjb/mef): Apply filters (configured, visible, limit).
237 base::ListValue
* networks_ptr
= networks
.get();
238 task_runner_
->PostTaskAndReply(
239 FROM_HERE
, base::Bind(&WiFiService::GetVisibleNetworks
,
240 base::Unretained(wifi_service_
.get()), network_type
,
241 networks_ptr
, false),
242 base::Bind(&NetworkingPrivateServiceClient::AfterGetVisibleNetworks
,
243 weak_factory_
.GetWeakPtr(), service_callbacks
->id
,
244 base::Passed(&networks
)));
247 void NetworkingPrivateServiceClient::StartConnect(
248 const std::string
& guid
,
249 const VoidCallback
& success_callback
,
250 const FailureCallback
& failure_callback
) {
251 ServiceCallbacks
* service_callbacks
= AddServiceCallbacks();
252 service_callbacks
->failure_callback
= failure_callback
;
253 service_callbacks
->start_connect_callback
= success_callback
;
255 std::string
* error
= new std::string
;
257 task_runner_
->PostTaskAndReply(
258 FROM_HERE
, base::Bind(&WiFiService::StartConnect
,
259 base::Unretained(wifi_service_
.get()), guid
, error
),
260 base::Bind(&NetworkingPrivateServiceClient::AfterStartConnect
,
261 weak_factory_
.GetWeakPtr(), service_callbacks
->id
,
262 base::Owned(error
)));
265 void NetworkingPrivateServiceClient::StartDisconnect(
266 const std::string
& guid
,
267 const VoidCallback
& success_callback
,
268 const FailureCallback
& failure_callback
) {
269 ServiceCallbacks
* service_callbacks
= AddServiceCallbacks();
270 service_callbacks
->failure_callback
= failure_callback
;
271 service_callbacks
->start_disconnect_callback
= success_callback
;
273 std::string
* error
= new std::string
;
275 task_runner_
->PostTaskAndReply(
276 FROM_HERE
, base::Bind(&WiFiService::StartDisconnect
,
277 base::Unretained(wifi_service_
.get()), guid
, error
),
278 base::Bind(&NetworkingPrivateServiceClient::AfterStartDisconnect
,
279 weak_factory_
.GetWeakPtr(), service_callbacks
->id
,
280 base::Owned(error
)));
283 void NetworkingPrivateServiceClient::SetWifiTDLSEnabledState(
284 const std::string
& ip_or_mac_address
,
286 const StringCallback
& success_callback
,
287 const FailureCallback
& failure_callback
) {
288 failure_callback
.Run(networking_private::kErrorNotSupported
);
291 void NetworkingPrivateServiceClient::GetWifiTDLSStatus(
292 const std::string
& ip_or_mac_address
,
293 const StringCallback
& success_callback
,
294 const FailureCallback
& failure_callback
) {
295 failure_callback
.Run(networking_private::kErrorNotSupported
);
298 void NetworkingPrivateServiceClient::GetCaptivePortalStatus(
299 const std::string
& guid
,
300 const StringCallback
& success_callback
,
301 const FailureCallback
& failure_callback
) {
302 failure_callback
.Run(networking_private::kErrorNotSupported
);
305 scoped_ptr
<base::ListValue
>
306 NetworkingPrivateServiceClient::GetEnabledNetworkTypes() {
307 scoped_ptr
<base::ListValue
> network_list
;
308 return network_list
.Pass();
311 bool NetworkingPrivateServiceClient::EnableNetworkType(
312 const std::string
& type
) {
316 bool NetworkingPrivateServiceClient::DisableNetworkType(
317 const std::string
& type
) {
321 bool NetworkingPrivateServiceClient::RequestScan() {
322 task_runner_
->PostTask(FROM_HERE
,
323 base::Bind(&WiFiService::RequestNetworkScan
,
324 base::Unretained(wifi_service_
.get())));
328 ////////////////////////////////////////////////////////////////////////////////
330 void NetworkingPrivateServiceClient::AfterGetProperties(
331 ServiceCallbacksID callback_id
,
332 const std::string
& network_guid
,
333 scoped_ptr
<base::DictionaryValue
> properties
,
334 const std::string
* error
) {
335 ServiceCallbacks
* service_callbacks
= callbacks_map_
.Lookup(callback_id
);
336 DCHECK(service_callbacks
);
337 if (!error
->empty()) {
338 DCHECK(!service_callbacks
->failure_callback
.is_null());
339 service_callbacks
->failure_callback
.Run(*error
);
341 DCHECK(!service_callbacks
->get_properties_callback
.is_null());
342 service_callbacks
->get_properties_callback
.Run(properties
.Pass());
344 RemoveServiceCallbacks(callback_id
);
347 void NetworkingPrivateServiceClient::AfterGetVisibleNetworks(
348 ServiceCallbacksID callback_id
,
349 scoped_ptr
<base::ListValue
> networks
) {
350 ServiceCallbacks
* service_callbacks
= callbacks_map_
.Lookup(callback_id
);
351 DCHECK(service_callbacks
);
352 DCHECK(!service_callbacks
->get_visible_networks_callback
.is_null());
353 service_callbacks
->get_visible_networks_callback
.Run(networks
.Pass());
354 RemoveServiceCallbacks(callback_id
);
357 void NetworkingPrivateServiceClient::AfterSetProperties(
358 ServiceCallbacksID callback_id
,
359 const std::string
* error
) {
360 ServiceCallbacks
* service_callbacks
= callbacks_map_
.Lookup(callback_id
);
361 DCHECK(service_callbacks
);
362 if (!error
->empty()) {
363 DCHECK(!service_callbacks
->failure_callback
.is_null());
364 service_callbacks
->failure_callback
.Run(*error
);
366 DCHECK(!service_callbacks
->set_properties_callback
.is_null());
367 service_callbacks
->set_properties_callback
.Run();
369 RemoveServiceCallbacks(callback_id
);
372 void NetworkingPrivateServiceClient::AfterCreateNetwork(
373 ServiceCallbacksID callback_id
,
374 const std::string
* network_guid
,
375 const std::string
* error
) {
376 ServiceCallbacks
* service_callbacks
= callbacks_map_
.Lookup(callback_id
);
377 DCHECK(service_callbacks
);
378 if (!error
->empty()) {
379 DCHECK(!service_callbacks
->failure_callback
.is_null());
380 service_callbacks
->failure_callback
.Run(*error
);
382 DCHECK(!service_callbacks
->create_network_callback
.is_null());
383 service_callbacks
->create_network_callback
.Run(*network_guid
);
385 RemoveServiceCallbacks(callback_id
);
388 void NetworkingPrivateServiceClient::AfterStartConnect(
389 ServiceCallbacksID callback_id
,
390 const std::string
* error
) {
391 ServiceCallbacks
* service_callbacks
= callbacks_map_
.Lookup(callback_id
);
392 DCHECK(service_callbacks
);
393 if (!error
->empty()) {
394 DCHECK(!service_callbacks
->failure_callback
.is_null());
395 service_callbacks
->failure_callback
.Run(*error
);
397 DCHECK(!service_callbacks
->start_connect_callback
.is_null());
398 service_callbacks
->start_connect_callback
.Run();
400 RemoveServiceCallbacks(callback_id
);
403 void NetworkingPrivateServiceClient::AfterStartDisconnect(
404 ServiceCallbacksID callback_id
,
405 const std::string
* error
) {
406 ServiceCallbacks
* service_callbacks
= callbacks_map_
.Lookup(callback_id
);
407 DCHECK(service_callbacks
);
408 if (!error
->empty()) {
409 DCHECK(!service_callbacks
->failure_callback
.is_null());
410 service_callbacks
->failure_callback
.Run(*error
);
412 DCHECK(!service_callbacks
->start_disconnect_callback
.is_null());
413 service_callbacks
->start_disconnect_callback
.Run();
415 RemoveServiceCallbacks(callback_id
);
418 void NetworkingPrivateServiceClient::OnNetworksChangedEventOnUIThread(
419 const std::vector
<std::string
>& network_guids
) {
420 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
421 FOR_EACH_OBSERVER(NetworkingPrivateDelegateObserver
,
422 network_events_observers_
,
423 OnNetworksChangedEvent(network_guids
));
426 void NetworkingPrivateServiceClient::OnNetworkListChangedEventOnUIThread(
427 const std::vector
<std::string
>& network_guids
) {
428 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
429 FOR_EACH_OBSERVER(NetworkingPrivateDelegateObserver
,
430 network_events_observers_
,
431 OnNetworkListChangedEvent(network_guids
));
434 } // namespace extensions