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 // NetworkingPrivateForgetNetworkFunction
205 NetworkingPrivateForgetNetworkFunction::
206 ~NetworkingPrivateForgetNetworkFunction() {
209 bool NetworkingPrivateForgetNetworkFunction::RunAsync() {
210 scoped_ptr
<private_api::ForgetNetwork::Params
> params
=
211 private_api::ForgetNetwork::Params::Create(*args_
);
212 EXTENSION_FUNCTION_VALIDATE(params
);
214 GetDelegate(browser_context())
216 params
->network_guid
,
217 base::Bind(&NetworkingPrivateForgetNetworkFunction::Success
, this),
218 base::Bind(&NetworkingPrivateForgetNetworkFunction::Failure
, this));
222 void NetworkingPrivateForgetNetworkFunction::Success() {
226 void NetworkingPrivateForgetNetworkFunction::Failure(const std::string
& error
) {
231 ////////////////////////////////////////////////////////////////////////////////
232 // NetworkingPrivateGetNetworksFunction
234 NetworkingPrivateGetNetworksFunction::~NetworkingPrivateGetNetworksFunction() {
237 bool NetworkingPrivateGetNetworksFunction::RunAsync() {
238 scoped_ptr
<private_api::GetNetworks::Params
> params
=
239 private_api::GetNetworks::Params::Create(*args_
);
240 EXTENSION_FUNCTION_VALIDATE(params
);
242 std::string network_type
= private_api::ToString(params
->filter
.network_type
);
243 const bool configured_only
=
244 params
->filter
.configured
? *params
->filter
.configured
: false;
245 const bool visible_only
=
246 params
->filter
.visible
? *params
->filter
.visible
: false;
248 params
->filter
.limit
? *params
->filter
.limit
: kDefaultNetworkListLimit
;
250 GetDelegate(browser_context())
252 network_type
, configured_only
, visible_only
, limit
,
253 base::Bind(&NetworkingPrivateGetNetworksFunction::Success
, this),
254 base::Bind(&NetworkingPrivateGetNetworksFunction::Failure
, this));
258 void NetworkingPrivateGetNetworksFunction::Success(
259 scoped_ptr
<base::ListValue
> network_list
) {
260 SetResult(network_list
.release());
264 void NetworkingPrivateGetNetworksFunction::Failure(const std::string
& error
) {
269 ////////////////////////////////////////////////////////////////////////////////
270 // NetworkingPrivateGetVisibleNetworksFunction
272 NetworkingPrivateGetVisibleNetworksFunction::
273 ~NetworkingPrivateGetVisibleNetworksFunction() {
276 bool NetworkingPrivateGetVisibleNetworksFunction::RunAsync() {
277 scoped_ptr
<private_api::GetVisibleNetworks::Params
> params
=
278 private_api::GetVisibleNetworks::Params::Create(*args_
);
279 EXTENSION_FUNCTION_VALIDATE(params
);
281 std::string network_type
= private_api::ToString(params
->network_type
);
282 const bool configured_only
= false;
283 const bool visible_only
= true;
285 GetDelegate(browser_context())
287 network_type
, configured_only
, visible_only
, kDefaultNetworkListLimit
,
288 base::Bind(&NetworkingPrivateGetVisibleNetworksFunction::Success
,
290 base::Bind(&NetworkingPrivateGetVisibleNetworksFunction::Failure
,
295 void NetworkingPrivateGetVisibleNetworksFunction::Success(
296 scoped_ptr
<base::ListValue
> network_properties_list
) {
297 SetResult(network_properties_list
.release());
301 void NetworkingPrivateGetVisibleNetworksFunction::Failure(
302 const std::string
& error
) {
307 ////////////////////////////////////////////////////////////////////////////////
308 // NetworkingPrivateGetEnabledNetworkTypesFunction
310 NetworkingPrivateGetEnabledNetworkTypesFunction::
311 ~NetworkingPrivateGetEnabledNetworkTypesFunction() {
314 bool NetworkingPrivateGetEnabledNetworkTypesFunction::RunSync() {
315 scoped_ptr
<base::ListValue
> enabled_networks_onc_types(
316 GetDelegate(browser_context())->GetEnabledNetworkTypes());
317 if (!enabled_networks_onc_types
) {
318 error_
= networking_private::kErrorNotSupported
;
321 scoped_ptr
<base::ListValue
> enabled_networks_list(new base::ListValue
);
322 for (base::ListValue::iterator iter
= enabled_networks_onc_types
->begin();
323 iter
!= enabled_networks_onc_types
->end(); ++iter
) {
325 if (!(*iter
)->GetAsString(&type
))
327 if (type
== ::onc::network_type::kEthernet
) {
328 enabled_networks_list
->AppendString(
329 private_api::ToString(private_api::NETWORK_TYPE_ETHERNET
));
330 } else if (type
== ::onc::network_type::kWiFi
) {
331 enabled_networks_list
->AppendString(
332 private_api::ToString(private_api::NETWORK_TYPE_WIFI
));
333 } else if (type
== ::onc::network_type::kWimax
) {
334 enabled_networks_list
->AppendString(
335 private_api::ToString(private_api::NETWORK_TYPE_WIMAX
));
336 } else if (type
== ::onc::network_type::kCellular
) {
337 enabled_networks_list
->AppendString(
338 private_api::ToString(private_api::NETWORK_TYPE_CELLULAR
));
340 LOG(ERROR
) << "networkingPrivate: Unexpected type: " << type
;
343 SetResult(enabled_networks_list
.release());
347 ////////////////////////////////////////////////////////////////////////////////
348 // NetworkingPrivateGetDeviceStatesFunction
350 NetworkingPrivateGetDeviceStatesFunction::
351 ~NetworkingPrivateGetDeviceStatesFunction() {
354 bool NetworkingPrivateGetDeviceStatesFunction::RunSync() {
355 scoped_ptr
<NetworkingPrivateDelegate::DeviceStateList
> device_states(
356 GetDelegate(browser_context())->GetDeviceStateList());
357 if (!device_states
) {
358 error_
= networking_private::kErrorNotSupported
;
362 scoped_ptr
<base::ListValue
> device_state_list(new base::ListValue
);
363 for (const private_api::DeviceStateProperties
* properties
: *device_states
)
364 device_state_list
->Append(properties
->ToValue().release());
365 SetResult(device_state_list
.release());
369 ////////////////////////////////////////////////////////////////////////////////
370 // NetworkingPrivateEnableNetworkTypeFunction
372 NetworkingPrivateEnableNetworkTypeFunction::
373 ~NetworkingPrivateEnableNetworkTypeFunction() {
376 bool NetworkingPrivateEnableNetworkTypeFunction::RunSync() {
377 scoped_ptr
<private_api::EnableNetworkType::Params
> params
=
378 private_api::EnableNetworkType::Params::Create(*args_
);
379 EXTENSION_FUNCTION_VALIDATE(params
);
381 return GetDelegate(browser_context())
382 ->EnableNetworkType(private_api::ToString(params
->network_type
));
385 ////////////////////////////////////////////////////////////////////////////////
386 // NetworkingPrivateDisableNetworkTypeFunction
388 NetworkingPrivateDisableNetworkTypeFunction::
389 ~NetworkingPrivateDisableNetworkTypeFunction() {
392 bool NetworkingPrivateDisableNetworkTypeFunction::RunSync() {
393 scoped_ptr
<private_api::DisableNetworkType::Params
> params
=
394 private_api::DisableNetworkType::Params::Create(*args_
);
396 return GetDelegate(browser_context())
397 ->DisableNetworkType(private_api::ToString(params
->network_type
));
400 ////////////////////////////////////////////////////////////////////////////////
401 // NetworkingPrivateRequestNetworkScanFunction
403 NetworkingPrivateRequestNetworkScanFunction::
404 ~NetworkingPrivateRequestNetworkScanFunction() {
407 bool NetworkingPrivateRequestNetworkScanFunction::RunSync() {
408 return GetDelegate(browser_context())->RequestScan();
411 ////////////////////////////////////////////////////////////////////////////////
412 // NetworkingPrivateStartConnectFunction
414 NetworkingPrivateStartConnectFunction::
415 ~NetworkingPrivateStartConnectFunction() {
418 bool NetworkingPrivateStartConnectFunction::RunAsync() {
419 scoped_ptr
<private_api::StartConnect::Params
> params
=
420 private_api::StartConnect::Params::Create(*args_
);
421 EXTENSION_FUNCTION_VALIDATE(params
);
423 GetDelegate(browser_context())
425 params
->network_guid
,
426 base::Bind(&NetworkingPrivateStartConnectFunction::Success
, this),
427 base::Bind(&NetworkingPrivateStartConnectFunction::Failure
, this));
431 void NetworkingPrivateStartConnectFunction::Success() {
435 void NetworkingPrivateStartConnectFunction::Failure(const std::string
& error
) {
440 ////////////////////////////////////////////////////////////////////////////////
441 // NetworkingPrivateStartDisconnectFunction
443 NetworkingPrivateStartDisconnectFunction::
444 ~NetworkingPrivateStartDisconnectFunction() {
447 bool NetworkingPrivateStartDisconnectFunction::RunAsync() {
448 scoped_ptr
<private_api::StartDisconnect::Params
> params
=
449 private_api::StartDisconnect::Params::Create(*args_
);
450 EXTENSION_FUNCTION_VALIDATE(params
);
452 GetDelegate(browser_context())
454 params
->network_guid
,
455 base::Bind(&NetworkingPrivateStartDisconnectFunction::Success
, this),
456 base::Bind(&NetworkingPrivateStartDisconnectFunction::Failure
, this));
460 void NetworkingPrivateStartDisconnectFunction::Success() {
464 void NetworkingPrivateStartDisconnectFunction::Failure(
465 const std::string
& error
) {
470 ////////////////////////////////////////////////////////////////////////////////
471 // NetworkingPrivateStartActivateFunction
473 NetworkingPrivateStartActivateFunction::
474 ~NetworkingPrivateStartActivateFunction() {
477 bool NetworkingPrivateStartActivateFunction::RunAsync() {
478 scoped_ptr
<private_api::StartActivate::Params
> params
=
479 private_api::StartActivate::Params::Create(*args_
);
480 EXTENSION_FUNCTION_VALIDATE(params
);
482 GetDelegate(browser_context())
484 params
->network_guid
, params
->carrier
? *params
->carrier
: "",
485 base::Bind(&NetworkingPrivateStartActivateFunction::Success
, this),
486 base::Bind(&NetworkingPrivateStartActivateFunction::Failure
, this));
490 void NetworkingPrivateStartActivateFunction::Success() {
494 void NetworkingPrivateStartActivateFunction::Failure(const std::string
& error
) {
499 ////////////////////////////////////////////////////////////////////////////////
500 // NetworkingPrivateVerifyDestinationFunction
502 NetworkingPrivateVerifyDestinationFunction::
503 ~NetworkingPrivateVerifyDestinationFunction() {
506 bool NetworkingPrivateVerifyDestinationFunction::RunAsync() {
507 scoped_ptr
<private_api::VerifyDestination::Params
> params
=
508 private_api::VerifyDestination::Params::Create(*args_
);
509 EXTENSION_FUNCTION_VALIDATE(params
);
511 GetDelegate(browser_context())
514 base::Bind(&NetworkingPrivateVerifyDestinationFunction::Success
,
516 base::Bind(&NetworkingPrivateVerifyDestinationFunction::Failure
,
521 void NetworkingPrivateVerifyDestinationFunction::Success(bool result
) {
522 results_
= private_api::VerifyDestination::Results::Create(result
);
526 void NetworkingPrivateVerifyDestinationFunction::Failure(
527 const std::string
& error
) {
532 ////////////////////////////////////////////////////////////////////////////////
533 // NetworkingPrivateVerifyAndEncryptCredentialsFunction
535 NetworkingPrivateVerifyAndEncryptCredentialsFunction::
536 ~NetworkingPrivateVerifyAndEncryptCredentialsFunction() {
539 bool NetworkingPrivateVerifyAndEncryptCredentialsFunction::RunAsync() {
540 scoped_ptr
<private_api::VerifyAndEncryptCredentials::Params
> params
=
541 private_api::VerifyAndEncryptCredentials::Params::Create(*args_
);
542 EXTENSION_FUNCTION_VALIDATE(params
);
544 GetDelegate(browser_context())
545 ->VerifyAndEncryptCredentials(
546 params
->network_guid
, params
->properties
,
548 &NetworkingPrivateVerifyAndEncryptCredentialsFunction::Success
,
551 &NetworkingPrivateVerifyAndEncryptCredentialsFunction::Failure
,
556 void NetworkingPrivateVerifyAndEncryptCredentialsFunction::Success(
557 const std::string
& result
) {
558 results_
= private_api::VerifyAndEncryptCredentials::Results::Create(result
);
562 void NetworkingPrivateVerifyAndEncryptCredentialsFunction::Failure(
563 const std::string
& error
) {
568 ////////////////////////////////////////////////////////////////////////////////
569 // NetworkingPrivateVerifyAndEncryptDataFunction
571 NetworkingPrivateVerifyAndEncryptDataFunction::
572 ~NetworkingPrivateVerifyAndEncryptDataFunction() {
575 bool NetworkingPrivateVerifyAndEncryptDataFunction::RunAsync() {
576 scoped_ptr
<private_api::VerifyAndEncryptData::Params
> params
=
577 private_api::VerifyAndEncryptData::Params::Create(*args_
);
578 EXTENSION_FUNCTION_VALIDATE(params
);
580 GetDelegate(browser_context())
581 ->VerifyAndEncryptData(
582 params
->properties
, params
->data
,
583 base::Bind(&NetworkingPrivateVerifyAndEncryptDataFunction::Success
,
585 base::Bind(&NetworkingPrivateVerifyAndEncryptDataFunction::Failure
,
590 void NetworkingPrivateVerifyAndEncryptDataFunction::Success(
591 const std::string
& result
) {
592 results_
= private_api::VerifyAndEncryptData::Results::Create(result
);
596 void NetworkingPrivateVerifyAndEncryptDataFunction::Failure(
597 const std::string
& error
) {
602 ////////////////////////////////////////////////////////////////////////////////
603 // NetworkingPrivateSetWifiTDLSEnabledStateFunction
605 NetworkingPrivateSetWifiTDLSEnabledStateFunction::
606 ~NetworkingPrivateSetWifiTDLSEnabledStateFunction() {
609 bool NetworkingPrivateSetWifiTDLSEnabledStateFunction::RunAsync() {
610 scoped_ptr
<private_api::SetWifiTDLSEnabledState::Params
> params
=
611 private_api::SetWifiTDLSEnabledState::Params::Create(*args_
);
612 EXTENSION_FUNCTION_VALIDATE(params
);
614 GetDelegate(browser_context())
615 ->SetWifiTDLSEnabledState(
616 params
->ip_or_mac_address
, params
->enabled
,
617 base::Bind(&NetworkingPrivateSetWifiTDLSEnabledStateFunction::Success
,
619 base::Bind(&NetworkingPrivateSetWifiTDLSEnabledStateFunction::Failure
,
625 void NetworkingPrivateSetWifiTDLSEnabledStateFunction::Success(
626 const std::string
& result
) {
627 results_
= private_api::SetWifiTDLSEnabledState::Results::Create(result
);
631 void NetworkingPrivateSetWifiTDLSEnabledStateFunction::Failure(
632 const std::string
& error
) {
637 ////////////////////////////////////////////////////////////////////////////////
638 // NetworkingPrivateGetWifiTDLSStatusFunction
640 NetworkingPrivateGetWifiTDLSStatusFunction::
641 ~NetworkingPrivateGetWifiTDLSStatusFunction() {
644 bool NetworkingPrivateGetWifiTDLSStatusFunction::RunAsync() {
645 scoped_ptr
<private_api::GetWifiTDLSStatus::Params
> params
=
646 private_api::GetWifiTDLSStatus::Params::Create(*args_
);
647 EXTENSION_FUNCTION_VALIDATE(params
);
649 GetDelegate(browser_context())
651 params
->ip_or_mac_address
,
652 base::Bind(&NetworkingPrivateGetWifiTDLSStatusFunction::Success
,
654 base::Bind(&NetworkingPrivateGetWifiTDLSStatusFunction::Failure
,
660 void NetworkingPrivateGetWifiTDLSStatusFunction::Success(
661 const std::string
& result
) {
662 results_
= private_api::GetWifiTDLSStatus::Results::Create(result
);
666 void NetworkingPrivateGetWifiTDLSStatusFunction::Failure(
667 const std::string
& error
) {
672 ////////////////////////////////////////////////////////////////////////////////
673 // NetworkingPrivateGetCaptivePortalStatusFunction
675 NetworkingPrivateGetCaptivePortalStatusFunction::
676 ~NetworkingPrivateGetCaptivePortalStatusFunction() {
679 bool NetworkingPrivateGetCaptivePortalStatusFunction::RunAsync() {
680 scoped_ptr
<private_api::GetCaptivePortalStatus::Params
> params
=
681 private_api::GetCaptivePortalStatus::Params::Create(*args_
);
682 EXTENSION_FUNCTION_VALIDATE(params
);
684 GetDelegate(browser_context())
685 ->GetCaptivePortalStatus(
686 params
->network_guid
,
687 base::Bind(&NetworkingPrivateGetCaptivePortalStatusFunction::Success
,
689 base::Bind(&NetworkingPrivateGetCaptivePortalStatusFunction::Failure
,
694 void NetworkingPrivateGetCaptivePortalStatusFunction::Success(
695 const std::string
& result
) {
696 results_
= private_api::GetCaptivePortalStatus::Results::Create(
697 private_api::ParseCaptivePortalStatus(result
));
701 void NetworkingPrivateGetCaptivePortalStatusFunction::Failure(
702 const std::string
& error
) {
707 } // namespace extensions