1 // Copyright 2014 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_api.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "components/onc/onc_constants.h"
11 #include "extensions/browser/api/networking_private/networking_private_delegate.h"
12 #include "extensions/browser/api/networking_private/networking_private_delegate_factory.h"
13 #include "extensions/browser/extension_function_registry.h"
14 #include "extensions/common/api/networking_private.h"
18 const int kDefaultNetworkListLimit
= 1000;
20 extensions::NetworkingPrivateDelegate
* GetDelegate(
21 content::BrowserContext
* browser_context
) {
22 return extensions::NetworkingPrivateDelegateFactory::GetForBrowserContext(
28 namespace extensions
{
30 namespace private_api
= core_api::networking_private
;
32 namespace networking_private
{
35 const char kErrorInvalidNetworkGuid
[] = "Error.InvalidNetworkGuid";
36 const char kErrorNetworkUnavailable
[] = "Error.NetworkUnavailable";
37 const char kErrorEncryptionError
[] = "Error.EncryptionError";
38 const char kErrorNotReady
[] = "Error.NotReady";
39 const char kErrorNotSupported
[] = "Error.NotSupported";
41 } // namespace networking_private
43 ////////////////////////////////////////////////////////////////////////////////
44 // NetworkingPrivateGetPropertiesFunction
46 NetworkingPrivateGetPropertiesFunction::
47 ~NetworkingPrivateGetPropertiesFunction() {
50 bool NetworkingPrivateGetPropertiesFunction::RunAsync() {
51 scoped_ptr
<private_api::GetProperties::Params
> params
=
52 private_api::GetProperties::Params::Create(*args_
);
53 EXTENSION_FUNCTION_VALIDATE(params
);
55 GetDelegate(browser_context())
58 base::Bind(&NetworkingPrivateGetPropertiesFunction::Success
, this),
59 base::Bind(&NetworkingPrivateGetPropertiesFunction::Failure
, this));
63 void NetworkingPrivateGetPropertiesFunction::Success(
64 scoped_ptr
<base::DictionaryValue
> result
) {
65 SetResult(result
.release());
69 void NetworkingPrivateGetPropertiesFunction::Failure(const std::string
& error
) {
74 ////////////////////////////////////////////////////////////////////////////////
75 // NetworkingPrivateGetManagedPropertiesFunction
77 NetworkingPrivateGetManagedPropertiesFunction::
78 ~NetworkingPrivateGetManagedPropertiesFunction() {
81 bool NetworkingPrivateGetManagedPropertiesFunction::RunAsync() {
82 scoped_ptr
<private_api::GetManagedProperties::Params
> params
=
83 private_api::GetManagedProperties::Params::Create(*args_
);
84 EXTENSION_FUNCTION_VALIDATE(params
);
86 GetDelegate(browser_context())
87 ->GetManagedProperties(
89 base::Bind(&NetworkingPrivateGetManagedPropertiesFunction::Success
,
91 base::Bind(&NetworkingPrivateGetManagedPropertiesFunction::Failure
,
96 void NetworkingPrivateGetManagedPropertiesFunction::Success(
97 scoped_ptr
<base::DictionaryValue
> result
) {
98 SetResult(result
.release());
102 void NetworkingPrivateGetManagedPropertiesFunction::Failure(
103 const std::string
& error
) {
108 ////////////////////////////////////////////////////////////////////////////////
109 // NetworkingPrivateGetStateFunction
111 NetworkingPrivateGetStateFunction::~NetworkingPrivateGetStateFunction() {
114 bool NetworkingPrivateGetStateFunction::RunAsync() {
115 scoped_ptr
<private_api::GetState::Params
> params
=
116 private_api::GetState::Params::Create(*args_
);
117 EXTENSION_FUNCTION_VALIDATE(params
);
119 GetDelegate(browser_context())
120 ->GetState(params
->network_guid
,
121 base::Bind(&NetworkingPrivateGetStateFunction::Success
, this),
122 base::Bind(&NetworkingPrivateGetStateFunction::Failure
, this));
126 void NetworkingPrivateGetStateFunction::Success(
127 scoped_ptr
<base::DictionaryValue
> result
) {
128 SetResult(result
.release());
132 void NetworkingPrivateGetStateFunction::Failure(const std::string
& error
) {
137 ////////////////////////////////////////////////////////////////////////////////
138 // NetworkingPrivateSetPropertiesFunction
140 NetworkingPrivateSetPropertiesFunction::
141 ~NetworkingPrivateSetPropertiesFunction() {
144 bool NetworkingPrivateSetPropertiesFunction::RunAsync() {
145 scoped_ptr
<private_api::SetProperties::Params
> params
=
146 private_api::SetProperties::Params::Create(*args_
);
147 EXTENSION_FUNCTION_VALIDATE(params
);
149 scoped_ptr
<base::DictionaryValue
> properties_dict(
150 params
->properties
.ToValue());
152 GetDelegate(browser_context())
154 params
->network_guid
, properties_dict
.Pass(),
155 base::Bind(&NetworkingPrivateSetPropertiesFunction::Success
, this),
156 base::Bind(&NetworkingPrivateSetPropertiesFunction::Failure
, this));
160 void NetworkingPrivateSetPropertiesFunction::Success() {
164 void NetworkingPrivateSetPropertiesFunction::Failure(const std::string
& error
) {
169 ////////////////////////////////////////////////////////////////////////////////
170 // NetworkingPrivateCreateNetworkFunction
172 NetworkingPrivateCreateNetworkFunction::
173 ~NetworkingPrivateCreateNetworkFunction() {
176 bool NetworkingPrivateCreateNetworkFunction::RunAsync() {
177 scoped_ptr
<private_api::CreateNetwork::Params
> params
=
178 private_api::CreateNetwork::Params::Create(*args_
);
179 EXTENSION_FUNCTION_VALIDATE(params
);
181 scoped_ptr
<base::DictionaryValue
> properties_dict(
182 params
->properties
.ToValue());
184 GetDelegate(browser_context())
186 params
->shared
, properties_dict
.Pass(),
187 base::Bind(&NetworkingPrivateCreateNetworkFunction::Success
, this),
188 base::Bind(&NetworkingPrivateCreateNetworkFunction::Failure
, this));
192 void NetworkingPrivateCreateNetworkFunction::Success(const std::string
& guid
) {
193 results_
= private_api::CreateNetwork::Results::Create(guid
);
197 void NetworkingPrivateCreateNetworkFunction::Failure(const std::string
& error
) {
202 ////////////////////////////////////////////////////////////////////////////////
203 // NetworkingPrivateGetNetworksFunction
205 NetworkingPrivateGetNetworksFunction::~NetworkingPrivateGetNetworksFunction() {
208 bool NetworkingPrivateGetNetworksFunction::RunAsync() {
209 scoped_ptr
<private_api::GetNetworks::Params
> params
=
210 private_api::GetNetworks::Params::Create(*args_
);
211 EXTENSION_FUNCTION_VALIDATE(params
);
213 std::string network_type
= private_api::ToString(params
->filter
.network_type
);
214 const bool configured_only
=
215 params
->filter
.configured
? *params
->filter
.configured
: false;
216 const bool visible_only
=
217 params
->filter
.visible
? *params
->filter
.visible
: false;
219 params
->filter
.limit
? *params
->filter
.limit
: kDefaultNetworkListLimit
;
221 GetDelegate(browser_context())
223 network_type
, configured_only
, visible_only
, limit
,
224 base::Bind(&NetworkingPrivateGetNetworksFunction::Success
, this),
225 base::Bind(&NetworkingPrivateGetNetworksFunction::Failure
, this));
229 void NetworkingPrivateGetNetworksFunction::Success(
230 scoped_ptr
<base::ListValue
> network_list
) {
231 SetResult(network_list
.release());
235 void NetworkingPrivateGetNetworksFunction::Failure(const std::string
& error
) {
240 ////////////////////////////////////////////////////////////////////////////////
241 // NetworkingPrivateGetVisibleNetworksFunction
243 NetworkingPrivateGetVisibleNetworksFunction::
244 ~NetworkingPrivateGetVisibleNetworksFunction() {
247 bool NetworkingPrivateGetVisibleNetworksFunction::RunAsync() {
248 scoped_ptr
<private_api::GetVisibleNetworks::Params
> params
=
249 private_api::GetVisibleNetworks::Params::Create(*args_
);
250 EXTENSION_FUNCTION_VALIDATE(params
);
252 std::string network_type
= private_api::ToString(params
->network_type
);
253 const bool configured_only
= false;
254 const bool visible_only
= true;
256 GetDelegate(browser_context())
258 network_type
, configured_only
, visible_only
, kDefaultNetworkListLimit
,
259 base::Bind(&NetworkingPrivateGetVisibleNetworksFunction::Success
,
261 base::Bind(&NetworkingPrivateGetVisibleNetworksFunction::Failure
,
266 void NetworkingPrivateGetVisibleNetworksFunction::Success(
267 scoped_ptr
<base::ListValue
> network_properties_list
) {
268 SetResult(network_properties_list
.release());
272 void NetworkingPrivateGetVisibleNetworksFunction::Failure(
273 const std::string
& error
) {
278 ////////////////////////////////////////////////////////////////////////////////
279 // NetworkingPrivateGetEnabledNetworkTypesFunction
281 NetworkingPrivateGetEnabledNetworkTypesFunction::
282 ~NetworkingPrivateGetEnabledNetworkTypesFunction() {
285 bool NetworkingPrivateGetEnabledNetworkTypesFunction::RunSync() {
286 scoped_ptr
<base::ListValue
> enabled_networks_onc_types(
287 GetDelegate(browser_context())->GetEnabledNetworkTypes());
288 if (!enabled_networks_onc_types
) {
289 error_
= networking_private::kErrorNotSupported
;
292 scoped_ptr
<base::ListValue
> enabled_networks_list(new base::ListValue
);
293 for (base::ListValue::iterator iter
= enabled_networks_onc_types
->begin();
294 iter
!= enabled_networks_onc_types
->end(); ++iter
) {
296 if (!(*iter
)->GetAsString(&type
))
298 if (type
== ::onc::network_type::kEthernet
) {
299 enabled_networks_list
->AppendString(
300 private_api::ToString(private_api::NETWORK_TYPE_ETHERNET
));
301 } else if (type
== ::onc::network_type::kWiFi
) {
302 enabled_networks_list
->AppendString(
303 private_api::ToString(private_api::NETWORK_TYPE_WIFI
));
304 } else if (type
== ::onc::network_type::kWimax
) {
305 enabled_networks_list
->AppendString(
306 private_api::ToString(private_api::NETWORK_TYPE_WIMAX
));
307 } else if (type
== ::onc::network_type::kCellular
) {
308 enabled_networks_list
->AppendString(
309 private_api::ToString(private_api::NETWORK_TYPE_CELLULAR
));
311 LOG(ERROR
) << "networkingPrivate: Unexpected type: " << type
;
314 SetResult(enabled_networks_list
.release());
318 ////////////////////////////////////////////////////////////////////////////////
319 // NetworkingPrivateEnableNetworkTypeFunction
321 NetworkingPrivateEnableNetworkTypeFunction::
322 ~NetworkingPrivateEnableNetworkTypeFunction() {
325 bool NetworkingPrivateEnableNetworkTypeFunction::RunSync() {
326 scoped_ptr
<private_api::EnableNetworkType::Params
> params
=
327 private_api::EnableNetworkType::Params::Create(*args_
);
328 EXTENSION_FUNCTION_VALIDATE(params
);
330 return GetDelegate(browser_context())
331 ->EnableNetworkType(private_api::ToString(params
->network_type
));
334 ////////////////////////////////////////////////////////////////////////////////
335 // NetworkingPrivateDisableNetworkTypeFunction
337 NetworkingPrivateDisableNetworkTypeFunction::
338 ~NetworkingPrivateDisableNetworkTypeFunction() {
341 bool NetworkingPrivateDisableNetworkTypeFunction::RunSync() {
342 scoped_ptr
<private_api::DisableNetworkType::Params
> params
=
343 private_api::DisableNetworkType::Params::Create(*args_
);
345 return GetDelegate(browser_context())
346 ->DisableNetworkType(private_api::ToString(params
->network_type
));
349 ////////////////////////////////////////////////////////////////////////////////
350 // NetworkingPrivateRequestNetworkScanFunction
352 NetworkingPrivateRequestNetworkScanFunction::
353 ~NetworkingPrivateRequestNetworkScanFunction() {
356 bool NetworkingPrivateRequestNetworkScanFunction::RunSync() {
357 return GetDelegate(browser_context())->RequestScan();
360 ////////////////////////////////////////////////////////////////////////////////
361 // NetworkingPrivateStartConnectFunction
363 NetworkingPrivateStartConnectFunction::
364 ~NetworkingPrivateStartConnectFunction() {
367 bool NetworkingPrivateStartConnectFunction::RunAsync() {
368 scoped_ptr
<private_api::StartConnect::Params
> params
=
369 private_api::StartConnect::Params::Create(*args_
);
370 EXTENSION_FUNCTION_VALIDATE(params
);
372 GetDelegate(browser_context())
374 params
->network_guid
,
375 base::Bind(&NetworkingPrivateStartConnectFunction::Success
, this),
376 base::Bind(&NetworkingPrivateStartConnectFunction::Failure
, this));
380 void NetworkingPrivateStartConnectFunction::Success() {
384 void NetworkingPrivateStartConnectFunction::Failure(const std::string
& error
) {
389 ////////////////////////////////////////////////////////////////////////////////
390 // NetworkingPrivateStartDisconnectFunction
392 NetworkingPrivateStartDisconnectFunction::
393 ~NetworkingPrivateStartDisconnectFunction() {
396 bool NetworkingPrivateStartDisconnectFunction::RunAsync() {
397 scoped_ptr
<private_api::StartDisconnect::Params
> params
=
398 private_api::StartDisconnect::Params::Create(*args_
);
399 EXTENSION_FUNCTION_VALIDATE(params
);
401 GetDelegate(browser_context())
403 params
->network_guid
,
404 base::Bind(&NetworkingPrivateStartDisconnectFunction::Success
, this),
405 base::Bind(&NetworkingPrivateStartDisconnectFunction::Failure
, this));
409 void NetworkingPrivateStartDisconnectFunction::Success() {
413 void NetworkingPrivateStartDisconnectFunction::Failure(
414 const std::string
& error
) {
419 ////////////////////////////////////////////////////////////////////////////////
420 // NetworkingPrivateVerifyDestinationFunction
422 NetworkingPrivateVerifyDestinationFunction::
423 ~NetworkingPrivateVerifyDestinationFunction() {
426 bool NetworkingPrivateVerifyDestinationFunction::RunAsync() {
427 scoped_ptr
<private_api::VerifyDestination::Params
> params
=
428 private_api::VerifyDestination::Params::Create(*args_
);
429 EXTENSION_FUNCTION_VALIDATE(params
);
431 GetDelegate(browser_context())
434 base::Bind(&NetworkingPrivateVerifyDestinationFunction::Success
,
436 base::Bind(&NetworkingPrivateVerifyDestinationFunction::Failure
,
441 void NetworkingPrivateVerifyDestinationFunction::Success(bool result
) {
442 results_
= private_api::VerifyDestination::Results::Create(result
);
446 void NetworkingPrivateVerifyDestinationFunction::Failure(
447 const std::string
& error
) {
452 ////////////////////////////////////////////////////////////////////////////////
453 // NetworkingPrivateVerifyAndEncryptCredentialsFunction
455 NetworkingPrivateVerifyAndEncryptCredentialsFunction::
456 ~NetworkingPrivateVerifyAndEncryptCredentialsFunction() {
459 bool NetworkingPrivateVerifyAndEncryptCredentialsFunction::RunAsync() {
460 scoped_ptr
<private_api::VerifyAndEncryptCredentials::Params
> params
=
461 private_api::VerifyAndEncryptCredentials::Params::Create(*args_
);
462 EXTENSION_FUNCTION_VALIDATE(params
);
464 GetDelegate(browser_context())
465 ->VerifyAndEncryptCredentials(
466 params
->network_guid
, params
->properties
,
468 &NetworkingPrivateVerifyAndEncryptCredentialsFunction::Success
,
471 &NetworkingPrivateVerifyAndEncryptCredentialsFunction::Failure
,
476 void NetworkingPrivateVerifyAndEncryptCredentialsFunction::Success(
477 const std::string
& result
) {
478 results_
= private_api::VerifyAndEncryptCredentials::Results::Create(result
);
482 void NetworkingPrivateVerifyAndEncryptCredentialsFunction::Failure(
483 const std::string
& error
) {
488 ////////////////////////////////////////////////////////////////////////////////
489 // NetworkingPrivateVerifyAndEncryptDataFunction
491 NetworkingPrivateVerifyAndEncryptDataFunction::
492 ~NetworkingPrivateVerifyAndEncryptDataFunction() {
495 bool NetworkingPrivateVerifyAndEncryptDataFunction::RunAsync() {
496 scoped_ptr
<private_api::VerifyAndEncryptData::Params
> params
=
497 private_api::VerifyAndEncryptData::Params::Create(*args_
);
498 EXTENSION_FUNCTION_VALIDATE(params
);
500 GetDelegate(browser_context())
501 ->VerifyAndEncryptData(
502 params
->properties
, params
->data
,
503 base::Bind(&NetworkingPrivateVerifyAndEncryptDataFunction::Success
,
505 base::Bind(&NetworkingPrivateVerifyAndEncryptDataFunction::Failure
,
510 void NetworkingPrivateVerifyAndEncryptDataFunction::Success(
511 const std::string
& result
) {
512 results_
= private_api::VerifyAndEncryptData::Results::Create(result
);
516 void NetworkingPrivateVerifyAndEncryptDataFunction::Failure(
517 const std::string
& error
) {
522 ////////////////////////////////////////////////////////////////////////////////
523 // NetworkingPrivateSetWifiTDLSEnabledStateFunction
525 NetworkingPrivateSetWifiTDLSEnabledStateFunction::
526 ~NetworkingPrivateSetWifiTDLSEnabledStateFunction() {
529 bool NetworkingPrivateSetWifiTDLSEnabledStateFunction::RunAsync() {
530 scoped_ptr
<private_api::SetWifiTDLSEnabledState::Params
> params
=
531 private_api::SetWifiTDLSEnabledState::Params::Create(*args_
);
532 EXTENSION_FUNCTION_VALIDATE(params
);
534 GetDelegate(browser_context())
535 ->SetWifiTDLSEnabledState(
536 params
->ip_or_mac_address
, params
->enabled
,
537 base::Bind(&NetworkingPrivateSetWifiTDLSEnabledStateFunction::Success
,
539 base::Bind(&NetworkingPrivateSetWifiTDLSEnabledStateFunction::Failure
,
545 void NetworkingPrivateSetWifiTDLSEnabledStateFunction::Success(
546 const std::string
& result
) {
547 results_
= private_api::SetWifiTDLSEnabledState::Results::Create(result
);
551 void NetworkingPrivateSetWifiTDLSEnabledStateFunction::Failure(
552 const std::string
& error
) {
557 ////////////////////////////////////////////////////////////////////////////////
558 // NetworkingPrivateGetWifiTDLSStatusFunction
560 NetworkingPrivateGetWifiTDLSStatusFunction::
561 ~NetworkingPrivateGetWifiTDLSStatusFunction() {
564 bool NetworkingPrivateGetWifiTDLSStatusFunction::RunAsync() {
565 scoped_ptr
<private_api::GetWifiTDLSStatus::Params
> params
=
566 private_api::GetWifiTDLSStatus::Params::Create(*args_
);
567 EXTENSION_FUNCTION_VALIDATE(params
);
569 GetDelegate(browser_context())
571 params
->ip_or_mac_address
,
572 base::Bind(&NetworkingPrivateGetWifiTDLSStatusFunction::Success
,
574 base::Bind(&NetworkingPrivateGetWifiTDLSStatusFunction::Failure
,
580 void NetworkingPrivateGetWifiTDLSStatusFunction::Success(
581 const std::string
& result
) {
582 results_
= private_api::GetWifiTDLSStatus::Results::Create(result
);
586 void NetworkingPrivateGetWifiTDLSStatusFunction::Failure(
587 const std::string
& error
) {
592 ////////////////////////////////////////////////////////////////////////////////
593 // NetworkingPrivateGetCaptivePortalStatusFunction
595 NetworkingPrivateGetCaptivePortalStatusFunction::
596 ~NetworkingPrivateGetCaptivePortalStatusFunction() {
599 bool NetworkingPrivateGetCaptivePortalStatusFunction::RunAsync() {
600 scoped_ptr
<private_api::GetCaptivePortalStatus::Params
> params
=
601 private_api::GetCaptivePortalStatus::Params::Create(*args_
);
602 EXTENSION_FUNCTION_VALIDATE(params
);
604 GetDelegate(browser_context())
605 ->GetCaptivePortalStatus(
606 params
->network_guid
,
607 base::Bind(&NetworkingPrivateGetCaptivePortalStatusFunction::Success
,
609 base::Bind(&NetworkingPrivateGetCaptivePortalStatusFunction::Failure
,
614 void NetworkingPrivateGetCaptivePortalStatusFunction::Success(
615 const std::string
& result
) {
616 results_
= private_api::GetCaptivePortalStatus::Results::Create(
617 private_api::ParseCaptivePortalStatus(result
));
621 void NetworkingPrivateGetCaptivePortalStatusFunction::Failure(
622 const std::string
& error
) {
627 } // namespace extensions