1 // Copyright 2015 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.
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "extensions/browser/api/networking_config/networking_config_api.h"
10 #include "extensions/browser/api/networking_config/networking_config_service.h"
11 #include "extensions/browser/api/networking_config/networking_config_service_factory.h"
12 #include "ui/base/l10n/l10n_util.h"
14 namespace extensions
{
18 const char kAuthenticationResultFailed
[] =
19 "Failed to set AuthenticationResult.";
20 const char kMalformedFilterDescription
[] = "Malformed filter description.";
21 const char kMalformedFilterDescriptionWithSSID
[] =
22 "Malformed filter description. Failed to register network with SSID "
24 const char kUnsupportedNetworkType
[] = "Unsupported network type.";
28 NetworkingConfigSetNetworkFilterFunction::
29 NetworkingConfigSetNetworkFilterFunction() {
32 ExtensionFunction::ResponseAction
33 NetworkingConfigSetNetworkFilterFunction::Run() {
35 api::networking_config::SetNetworkFilter::Params::Create(*args_
);
36 EXTENSION_FUNCTION_VALIDATE(parameters_
.get());
38 NetworkingConfigService
* service
=
39 NetworkingConfigServiceFactory::GetForBrowserContext(browser_context());
42 // Remove previously registered networks.
43 service
->UnregisterExtension(extension_id());
45 for (linked_ptr
<api::networking_config::NetworkInfo
>& ni
:
46 parameters_
->networks
) {
47 // |Type| field must be set to |WiFi|
48 if (ni
->type
!= api::networking_config::NETWORK_TYPE_WIFI
)
49 return RespondNow(Error(kUnsupportedNetworkType
));
51 // Either |ssid| or |hex_ssid| must be set.
52 if (!ni
->ssid
.get() && !ni
->hex_ssid
.get())
53 return RespondNow(Error(kMalformedFilterDescription
));
57 auto ssid_field
= ni
->ssid
.get();
58 hex_ssid
= base::HexEncode(ssid_field
->c_str(), ssid_field
->size());
60 if (ni
->hex_ssid
.get())
61 hex_ssid
= *ni
->hex_ssid
.get();
63 if (!service
->RegisterHexSsid(hex_ssid
, extension_id()))
64 return RespondNow(Error(kMalformedFilterDescriptionWithSSID
, hex_ssid
));
67 return RespondNow(NoArguments());
70 NetworkingConfigSetNetworkFilterFunction::
71 ~NetworkingConfigSetNetworkFilterFunction() {
74 NetworkingConfigFinishAuthenticationFunction::
75 NetworkingConfigFinishAuthenticationFunction() {
78 ExtensionFunction::ResponseAction
79 NetworkingConfigFinishAuthenticationFunction::Run() {
81 api::networking_config::FinishAuthentication::Params::Create(*args_
);
82 EXTENSION_FUNCTION_VALIDATE(parameters_
.get());
84 NetworkingConfigService
* service
=
85 NetworkingConfigServiceFactory::GetForBrowserContext(browser_context());
88 const NetworkingConfigService::AuthenticationResult
& last_result
=
89 service
->GetAuthenticationResult();
90 if (last_result
.authentication_state
!= NetworkingConfigService::NOTRY
||
91 last_result
.guid
!= parameters_
->guid
) {
92 return RespondNow(Error(kAuthenticationResultFailed
));
95 // Populate NetworkingCaptivePortalAPI::AuthenticationResult.
96 NetworkingConfigService::AuthenticationResult authentication_result
= {
97 extension_id(), parameters_
->guid
, NetworkingConfigService::FAILED
,
99 switch (parameters_
->result
) {
100 case api::networking_config::AUTHENTICATION_RESULT_NONE
:
103 case api::networking_config::AUTHENTICATION_RESULT_UNHANDLED
:
104 authentication_result
.authentication_state
=
105 NetworkingConfigService::FAILED
;
107 case api::networking_config::AUTHENTICATION_RESULT_REJECTED
:
108 authentication_result
.authentication_state
=
109 NetworkingConfigService::REJECTED
;
111 case api::networking_config::AUTHENTICATION_RESULT_FAILED
:
112 authentication_result
.authentication_state
=
113 NetworkingConfigService::FAILED
;
115 case api::networking_config::AUTHENTICATION_RESULT_SUCCEEDED
:
116 authentication_result
.authentication_state
=
117 NetworkingConfigService::SUCCESS
;
120 service
->SetAuthenticationResult(authentication_result
);
121 return RespondNow(NoArguments());
124 NetworkingConfigFinishAuthenticationFunction::
125 ~NetworkingConfigFinishAuthenticationFunction() {
128 } // namespace extensions