Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / browser / api / networking_private / networking_private_service_client.cc
blob9859b5e4682f1d6ab2053d687f7dcd2767793118
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"
8 #include "base/bind.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 {
21 namespace {
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());
30 } // namespace
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()),
43 weak_factory_(this) {
44 sequence_token_ = BrowserThread::GetBlockingPool()->GetNamedSequenceToken(
45 kNetworkingPrivateSequenceTokenName);
46 task_runner_ =
47 BrowserThread::GetBlockingPool()
48 ->GetSequencedTaskRunnerWithShutdownBehavior(
49 sequence_token_, base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
50 task_runner_->PostTask(
51 FROM_HERE,
52 base::Bind(&WiFiService::Initialize,
53 base::Unretained(wifi_service_.get()), task_runner_));
54 task_runner_->PostTask(
55 FROM_HERE,
56 base::Bind(
57 &WiFiService::SetEventObservers,
58 base::Unretained(wifi_service_.get()),
59 base::MessageLoopProxy::current(),
60 base::Bind(
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(
173 FROM_HERE,
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(
202 bool shared,
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,
233 bool visible_only,
234 int limit,
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,
293 bool enabled,
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 return network_list.Pass();
319 bool NetworkingPrivateServiceClient::EnableNetworkType(
320 const std::string& type) {
321 return false;
324 bool NetworkingPrivateServiceClient::DisableNetworkType(
325 const std::string& type) {
326 return false;
329 bool NetworkingPrivateServiceClient::RequestScan() {
330 task_runner_->PostTask(FROM_HERE,
331 base::Bind(&WiFiService::RequestNetworkScan,
332 base::Unretained(wifi_service_.get())));
333 return true;
336 ////////////////////////////////////////////////////////////////////////////////
338 void NetworkingPrivateServiceClient::AfterGetProperties(
339 ServiceCallbacksID callback_id,
340 const std::string& network_guid,
341 scoped_ptr<base::DictionaryValue> properties,
342 const std::string* error) {
343 ServiceCallbacks* service_callbacks = callbacks_map_.Lookup(callback_id);
344 DCHECK(service_callbacks);
345 if (!error->empty()) {
346 DCHECK(!service_callbacks->failure_callback.is_null());
347 service_callbacks->failure_callback.Run(*error);
348 } else {
349 DCHECK(!service_callbacks->get_properties_callback.is_null());
350 service_callbacks->get_properties_callback.Run(properties.Pass());
352 RemoveServiceCallbacks(callback_id);
355 void NetworkingPrivateServiceClient::AfterGetVisibleNetworks(
356 ServiceCallbacksID callback_id,
357 scoped_ptr<base::ListValue> networks) {
358 ServiceCallbacks* service_callbacks = callbacks_map_.Lookup(callback_id);
359 DCHECK(service_callbacks);
360 DCHECK(!service_callbacks->get_visible_networks_callback.is_null());
361 service_callbacks->get_visible_networks_callback.Run(networks.Pass());
362 RemoveServiceCallbacks(callback_id);
365 void NetworkingPrivateServiceClient::AfterSetProperties(
366 ServiceCallbacksID callback_id,
367 const std::string* error) {
368 ServiceCallbacks* service_callbacks = callbacks_map_.Lookup(callback_id);
369 DCHECK(service_callbacks);
370 if (!error->empty()) {
371 DCHECK(!service_callbacks->failure_callback.is_null());
372 service_callbacks->failure_callback.Run(*error);
373 } else {
374 DCHECK(!service_callbacks->set_properties_callback.is_null());
375 service_callbacks->set_properties_callback.Run();
377 RemoveServiceCallbacks(callback_id);
380 void NetworkingPrivateServiceClient::AfterCreateNetwork(
381 ServiceCallbacksID callback_id,
382 const std::string* network_guid,
383 const std::string* error) {
384 ServiceCallbacks* service_callbacks = callbacks_map_.Lookup(callback_id);
385 DCHECK(service_callbacks);
386 if (!error->empty()) {
387 DCHECK(!service_callbacks->failure_callback.is_null());
388 service_callbacks->failure_callback.Run(*error);
389 } else {
390 DCHECK(!service_callbacks->create_network_callback.is_null());
391 service_callbacks->create_network_callback.Run(*network_guid);
393 RemoveServiceCallbacks(callback_id);
396 void NetworkingPrivateServiceClient::AfterStartConnect(
397 ServiceCallbacksID callback_id,
398 const std::string* error) {
399 ServiceCallbacks* service_callbacks = callbacks_map_.Lookup(callback_id);
400 DCHECK(service_callbacks);
401 if (!error->empty()) {
402 DCHECK(!service_callbacks->failure_callback.is_null());
403 service_callbacks->failure_callback.Run(*error);
404 } else {
405 DCHECK(!service_callbacks->start_connect_callback.is_null());
406 service_callbacks->start_connect_callback.Run();
408 RemoveServiceCallbacks(callback_id);
411 void NetworkingPrivateServiceClient::AfterStartDisconnect(
412 ServiceCallbacksID callback_id,
413 const std::string* error) {
414 ServiceCallbacks* service_callbacks = callbacks_map_.Lookup(callback_id);
415 DCHECK(service_callbacks);
416 if (!error->empty()) {
417 DCHECK(!service_callbacks->failure_callback.is_null());
418 service_callbacks->failure_callback.Run(*error);
419 } else {
420 DCHECK(!service_callbacks->start_disconnect_callback.is_null());
421 service_callbacks->start_disconnect_callback.Run();
423 RemoveServiceCallbacks(callback_id);
426 void NetworkingPrivateServiceClient::OnNetworksChangedEventOnUIThread(
427 const std::vector<std::string>& network_guids) {
428 DCHECK_CURRENTLY_ON(BrowserThread::UI);
429 FOR_EACH_OBSERVER(NetworkingPrivateDelegateObserver,
430 network_events_observers_,
431 OnNetworksChangedEvent(network_guids));
434 void NetworkingPrivateServiceClient::OnNetworkListChangedEventOnUIThread(
435 const std::vector<std::string>& network_guids) {
436 DCHECK_CURRENTLY_ON(BrowserThread::UI);
437 FOR_EACH_OBSERVER(NetworkingPrivateDelegateObserver,
438 network_events_observers_,
439 OnNetworkListChangedEvent(network_guids));
442 } // namespace extensions