Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / extensions / browser / api / networking_private / networking_private_api.cc
blob7ebeedcdf9420804db6e70180be54209bd7a519c
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"
7 #include "base/bind.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"
16 namespace {
18 const int kDefaultNetworkListLimit = 1000;
20 extensions::NetworkingPrivateDelegate* GetDelegate(
21 content::BrowserContext* browser_context) {
22 return extensions::NetworkingPrivateDelegateFactory::GetForBrowserContext(
23 browser_context);
26 } // namespace
28 namespace extensions {
30 namespace private_api = core_api::networking_private;
32 namespace networking_private {
34 // static
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())
56 ->GetProperties(
57 params->network_guid,
58 base::Bind(&NetworkingPrivateGetPropertiesFunction::Success, this),
59 base::Bind(&NetworkingPrivateGetPropertiesFunction::Failure, this));
60 return true;
63 void NetworkingPrivateGetPropertiesFunction::Success(
64 scoped_ptr<base::DictionaryValue> result) {
65 SetResult(result.release());
66 SendResponse(true);
69 void NetworkingPrivateGetPropertiesFunction::Failure(const std::string& error) {
70 error_ = error;
71 SendResponse(false);
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(
88 params->network_guid,
89 base::Bind(&NetworkingPrivateGetManagedPropertiesFunction::Success,
90 this),
91 base::Bind(&NetworkingPrivateGetManagedPropertiesFunction::Failure,
92 this));
93 return true;
96 void NetworkingPrivateGetManagedPropertiesFunction::Success(
97 scoped_ptr<base::DictionaryValue> result) {
98 SetResult(result.release());
99 SendResponse(true);
102 void NetworkingPrivateGetManagedPropertiesFunction::Failure(
103 const std::string& error) {
104 error_ = error;
105 SendResponse(false);
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));
123 return true;
126 void NetworkingPrivateGetStateFunction::Success(
127 scoped_ptr<base::DictionaryValue> result) {
128 SetResult(result.release());
129 SendResponse(true);
132 void NetworkingPrivateGetStateFunction::Failure(const std::string& error) {
133 error_ = error;
134 SendResponse(false);
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())
153 ->SetProperties(
154 params->network_guid, properties_dict.Pass(),
155 base::Bind(&NetworkingPrivateSetPropertiesFunction::Success, this),
156 base::Bind(&NetworkingPrivateSetPropertiesFunction::Failure, this));
157 return true;
160 void NetworkingPrivateSetPropertiesFunction::Success() {
161 SendResponse(true);
164 void NetworkingPrivateSetPropertiesFunction::Failure(const std::string& error) {
165 error_ = error;
166 SendResponse(false);
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())
185 ->CreateNetwork(
186 params->shared, properties_dict.Pass(),
187 base::Bind(&NetworkingPrivateCreateNetworkFunction::Success, this),
188 base::Bind(&NetworkingPrivateCreateNetworkFunction::Failure, this));
189 return true;
192 void NetworkingPrivateCreateNetworkFunction::Success(const std::string& guid) {
193 results_ = private_api::CreateNetwork::Results::Create(guid);
194 SendResponse(true);
197 void NetworkingPrivateCreateNetworkFunction::Failure(const std::string& error) {
198 error_ = error;
199 SendResponse(false);
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;
218 const int limit =
219 params->filter.limit ? *params->filter.limit : kDefaultNetworkListLimit;
221 GetDelegate(browser_context())
222 ->GetNetworks(
223 network_type, configured_only, visible_only, limit,
224 base::Bind(&NetworkingPrivateGetNetworksFunction::Success, this),
225 base::Bind(&NetworkingPrivateGetNetworksFunction::Failure, this));
226 return true;
229 void NetworkingPrivateGetNetworksFunction::Success(
230 scoped_ptr<base::ListValue> network_list) {
231 SetResult(network_list.release());
232 SendResponse(true);
235 void NetworkingPrivateGetNetworksFunction::Failure(const std::string& error) {
236 error_ = error;
237 SendResponse(false);
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())
257 ->GetNetworks(
258 network_type, configured_only, visible_only, kDefaultNetworkListLimit,
259 base::Bind(&NetworkingPrivateGetVisibleNetworksFunction::Success,
260 this),
261 base::Bind(&NetworkingPrivateGetVisibleNetworksFunction::Failure,
262 this));
263 return true;
266 void NetworkingPrivateGetVisibleNetworksFunction::Success(
267 scoped_ptr<base::ListValue> network_properties_list) {
268 SetResult(network_properties_list.release());
269 SendResponse(true);
272 void NetworkingPrivateGetVisibleNetworksFunction::Failure(
273 const std::string& error) {
274 error_ = error;
275 SendResponse(false);
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;
290 return false;
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) {
295 std::string type;
296 if (!(*iter)->GetAsString(&type))
297 NOTREACHED();
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));
310 } else {
311 LOG(ERROR) << "networkingPrivate: Unexpected type: " << type;
314 SetResult(enabled_networks_list.release());
315 return true;
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())
373 ->StartConnect(
374 params->network_guid,
375 base::Bind(&NetworkingPrivateStartConnectFunction::Success, this),
376 base::Bind(&NetworkingPrivateStartConnectFunction::Failure, this));
377 return true;
380 void NetworkingPrivateStartConnectFunction::Success() {
381 SendResponse(true);
384 void NetworkingPrivateStartConnectFunction::Failure(const std::string& error) {
385 error_ = error;
386 SendResponse(false);
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())
402 ->StartDisconnect(
403 params->network_guid,
404 base::Bind(&NetworkingPrivateStartDisconnectFunction::Success, this),
405 base::Bind(&NetworkingPrivateStartDisconnectFunction::Failure, this));
406 return true;
409 void NetworkingPrivateStartDisconnectFunction::Success() {
410 SendResponse(true);
413 void NetworkingPrivateStartDisconnectFunction::Failure(
414 const std::string& error) {
415 error_ = error;
416 SendResponse(false);
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())
432 ->VerifyDestination(
433 params->properties,
434 base::Bind(&NetworkingPrivateVerifyDestinationFunction::Success,
435 this),
436 base::Bind(&NetworkingPrivateVerifyDestinationFunction::Failure,
437 this));
438 return true;
441 void NetworkingPrivateVerifyDestinationFunction::Success(bool result) {
442 results_ = private_api::VerifyDestination::Results::Create(result);
443 SendResponse(true);
446 void NetworkingPrivateVerifyDestinationFunction::Failure(
447 const std::string& error) {
448 error_ = error;
449 SendResponse(false);
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,
467 base::Bind(
468 &NetworkingPrivateVerifyAndEncryptCredentialsFunction::Success,
469 this),
470 base::Bind(
471 &NetworkingPrivateVerifyAndEncryptCredentialsFunction::Failure,
472 this));
473 return true;
476 void NetworkingPrivateVerifyAndEncryptCredentialsFunction::Success(
477 const std::string& result) {
478 results_ = private_api::VerifyAndEncryptCredentials::Results::Create(result);
479 SendResponse(true);
482 void NetworkingPrivateVerifyAndEncryptCredentialsFunction::Failure(
483 const std::string& error) {
484 error_ = error;
485 SendResponse(false);
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,
504 this),
505 base::Bind(&NetworkingPrivateVerifyAndEncryptDataFunction::Failure,
506 this));
507 return true;
510 void NetworkingPrivateVerifyAndEncryptDataFunction::Success(
511 const std::string& result) {
512 results_ = private_api::VerifyAndEncryptData::Results::Create(result);
513 SendResponse(true);
516 void NetworkingPrivateVerifyAndEncryptDataFunction::Failure(
517 const std::string& error) {
518 error_ = error;
519 SendResponse(false);
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,
538 this),
539 base::Bind(&NetworkingPrivateSetWifiTDLSEnabledStateFunction::Failure,
540 this));
542 return true;
545 void NetworkingPrivateSetWifiTDLSEnabledStateFunction::Success(
546 const std::string& result) {
547 results_ = private_api::SetWifiTDLSEnabledState::Results::Create(result);
548 SendResponse(true);
551 void NetworkingPrivateSetWifiTDLSEnabledStateFunction::Failure(
552 const std::string& error) {
553 error_ = error;
554 SendResponse(false);
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())
570 ->GetWifiTDLSStatus(
571 params->ip_or_mac_address,
572 base::Bind(&NetworkingPrivateGetWifiTDLSStatusFunction::Success,
573 this),
574 base::Bind(&NetworkingPrivateGetWifiTDLSStatusFunction::Failure,
575 this));
577 return true;
580 void NetworkingPrivateGetWifiTDLSStatusFunction::Success(
581 const std::string& result) {
582 results_ = private_api::GetWifiTDLSStatus::Results::Create(result);
583 SendResponse(true);
586 void NetworkingPrivateGetWifiTDLSStatusFunction::Failure(
587 const std::string& error) {
588 error_ = error;
589 SendResponse(false);
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,
608 this),
609 base::Bind(&NetworkingPrivateGetCaptivePortalStatusFunction::Failure,
610 this));
611 return true;
614 void NetworkingPrivateGetCaptivePortalStatusFunction::Success(
615 const std::string& result) {
616 results_ = private_api::GetCaptivePortalStatus::Results::Create(
617 private_api::ParseCaptivePortalStatus(result));
618 SendResponse(true);
621 void NetworkingPrivateGetCaptivePortalStatusFunction::Failure(
622 const std::string& error) {
623 error_ = error;
624 SendResponse(false);
627 } // namespace extensions