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/lazy_instance.h"
8 #include "base/strings/string_util.h"
9 #include "extensions/browser/api/networking_config/networking_config_service.h"
11 namespace extensions
{
15 bool IsValidNonEmptyHexString(const std::string
& input
) {
16 size_t count
= input
.size();
17 if (count
== 0 || (count
% 2) != 0)
19 for (const char& c
: input
)
20 if (!IsHexDigit
<char>(c
))
27 NetworkingConfigService::AuthenticationResult::AuthenticationResult()
28 : authentication_state(NetworkingConfigService::NOTRY
) {
31 NetworkingConfigService::AuthenticationResult::AuthenticationResult(
32 ExtensionId extension_id
,
34 AuthenticationState authentication_state
)
35 : extension_id(extension_id
),
37 authentication_state(authentication_state
) {
40 NetworkingConfigService::NetworkingConfigService(
41 scoped_ptr
<EventDelegate
> event_delegate
,
42 ExtensionRegistry
* extension_registry
)
43 : registry_observer_(this), event_delegate_(event_delegate
.Pass()) {
44 registry_observer_
.Add(extension_registry
);
47 NetworkingConfigService::~NetworkingConfigService() {
50 void NetworkingConfigService::OnExtensionUnloaded(
51 content::BrowserContext
* browser_context
,
52 const Extension
* extension
,
53 UnloadedExtensionInfo::Reason reason
) {
54 UnregisterExtension(extension
->id());
57 std::string
NetworkingConfigService::LookupExtensionIdForHexSsid(
58 std::string hex_ssid
) const {
59 // Transform hex_ssid to uppercase.
60 transform(hex_ssid
.begin(), hex_ssid
.end(), hex_ssid
.begin(), toupper
);
62 const auto it
= hex_ssid_to_extension_id_
.find(hex_ssid
);
63 if (it
== hex_ssid_to_extension_id_
.end())
68 bool NetworkingConfigService::IsRegisteredForCaptivePortalEvent(
69 std::string extension_id
) const {
70 return event_delegate_
->HasExtensionRegisteredForEvent(extension_id
);
73 bool NetworkingConfigService::RegisterHexSsid(std::string hex_ssid
,
74 const std::string
& extension_id
) {
75 if (!IsValidNonEmptyHexString(hex_ssid
)) {
76 LOG(ERROR
) << "\'" << hex_ssid
<< "\' is not a valid hex encoded string.";
80 // Transform hex_ssid to uppercase.
81 transform(hex_ssid
.begin(), hex_ssid
.end(), hex_ssid
.begin(), toupper
);
83 return hex_ssid_to_extension_id_
.insert(make_pair(hex_ssid
, extension_id
))
87 void NetworkingConfigService::UnregisterExtension(
88 const std::string
& extension_id
) {
89 for (auto it
= hex_ssid_to_extension_id_
.begin();
90 it
!= hex_ssid_to_extension_id_
.end();) {
91 if (it
->second
== extension_id
)
92 hex_ssid_to_extension_id_
.erase(it
++);
98 const NetworkingConfigService::AuthenticationResult
&
99 NetworkingConfigService::GetAuthenticationResult() const {
100 return authentication_result_
;
103 void NetworkingConfigService::ResetAuthenticationResult() {
104 authentication_result_
= AuthenticationResult();
107 void NetworkingConfigService::SetAuthenticationResult(
108 const AuthenticationResult
& authentication_result
) {
109 authentication_result_
= authentication_result
;
112 } // namespace extensions