OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include <algorithm> |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/strings/string_util.h" |
| 9 #include "extensions/browser/api/networking_config/networking_config_service.h" |
| 10 |
| 11 namespace extensions { |
| 12 |
| 13 namespace { |
| 14 |
| 15 bool IsValidNonEmptyHexString(const std::string& input) { |
| 16 size_t count = input.size(); |
| 17 if (count == 0 || (count % 2) != 0) |
| 18 return false; |
| 19 for (const char& c : input) |
| 20 if (!IsHexDigit<char>(c)) |
| 21 return false; |
| 22 return true; |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 27 NetworkingConfigService::AuthenticationResult::AuthenticationResult() |
| 28 : authentication_state(NetworkingConfigService::NOTRY) { |
| 29 } |
| 30 |
| 31 NetworkingConfigService::AuthenticationResult::AuthenticationResult( |
| 32 ExtensionId extension_id, |
| 33 std::string guid, |
| 34 AuthenticationState authentication_state) |
| 35 : extension_id(extension_id), |
| 36 guid(guid), |
| 37 authentication_state(authentication_state) { |
| 38 } |
| 39 |
| 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); |
| 45 } |
| 46 |
| 47 NetworkingConfigService::~NetworkingConfigService() { |
| 48 } |
| 49 |
| 50 void NetworkingConfigService::OnExtensionUnloaded( |
| 51 content::BrowserContext* browser_context, |
| 52 const Extension* extension, |
| 53 UnloadedExtensionInfo::Reason reason) { |
| 54 UnregisterExtension(extension->id()); |
| 55 } |
| 56 |
| 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); |
| 61 |
| 62 const auto it = hex_ssid_to_extension_id_.find(hex_ssid); |
| 63 if (it == hex_ssid_to_extension_id_.end()) |
| 64 return std::string(); |
| 65 return it->second; |
| 66 } |
| 67 |
| 68 bool NetworkingConfigService::IsRegisteredForCaptivePortalEvent( |
| 69 std::string extension_id) const { |
| 70 return event_delegate_->HasExtensionRegisteredForEvent(extension_id); |
| 71 } |
| 72 |
| 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."; |
| 77 return false; |
| 78 } |
| 79 |
| 80 // Transform hex_ssid to uppercase. |
| 81 transform(hex_ssid.begin(), hex_ssid.end(), hex_ssid.begin(), toupper); |
| 82 |
| 83 return hex_ssid_to_extension_id_.insert(make_pair(hex_ssid, extension_id)) |
| 84 .second; |
| 85 } |
| 86 |
| 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++); |
| 93 else |
| 94 ++it; |
| 95 } |
| 96 } |
| 97 |
| 98 const NetworkingConfigService::AuthenticationResult& |
| 99 NetworkingConfigService::GetAuthenticationResult() const { |
| 100 return authentication_result_; |
| 101 } |
| 102 |
| 103 void NetworkingConfigService::ResetAuthenticationResult() { |
| 104 authentication_result_ = AuthenticationResult(); |
| 105 } |
| 106 |
| 107 void NetworkingConfigService::SetAuthenticationResult( |
| 108 const AuthenticationResult& authentication_result) { |
| 109 authentication_result_ = authentication_result; |
| 110 } |
| 111 |
| 112 } // namespace extensions |
OLD | NEW |