ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / extensions / browser / api / networking_config / networking_config_service.cc
blob60afd93308233da39bfb712a20c3d97f32794af1
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.
5 #include <algorithm>
6 #include <vector>
8 #include "base/bind.h"
9 #include "base/lazy_instance.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "chromeos/network/managed_network_configuration_handler.h"
13 #include "chromeos/network/network_handler.h"
14 #include "chromeos/network/network_state.h"
15 #include "chromeos/network/network_state_handler.h"
16 #include "extensions/browser/api/networking_config/networking_config_service.h"
17 #include "extensions/common/api/networking_config.h"
19 namespace extensions {
21 namespace {
23 bool IsValidNonEmptyHexString(const std::string& input) {
24 size_t count = input.size();
25 if (count == 0 || (count % 2) != 0)
26 return false;
27 for (const char& c : input)
28 if (!IsHexDigit<char>(c))
29 return false;
30 return true;
33 } // namespace
35 NetworkingConfigService::AuthenticationResult::AuthenticationResult()
36 : authentication_state(NetworkingConfigService::NOTRY) {
39 NetworkingConfigService::AuthenticationResult::AuthenticationResult(
40 ExtensionId extension_id,
41 std::string guid,
42 AuthenticationState authentication_state)
43 : extension_id(extension_id),
44 guid(guid),
45 authentication_state(authentication_state) {
48 NetworkingConfigService::NetworkingConfigService(
49 content::BrowserContext* browser_context,
50 scoped_ptr<EventDelegate> event_delegate,
51 ExtensionRegistry* extension_registry)
52 : browser_context_(browser_context),
53 registry_observer_(this),
54 event_delegate_(event_delegate.Pass()),
55 weak_factory_(this) {
56 registry_observer_.Add(extension_registry);
59 NetworkingConfigService::~NetworkingConfigService() {
62 void NetworkingConfigService::OnExtensionUnloaded(
63 content::BrowserContext* browser_context,
64 const Extension* extension,
65 UnloadedExtensionInfo::Reason reason) {
66 UnregisterExtension(extension->id());
69 std::string NetworkingConfigService::LookupExtensionIdForHexSsid(
70 std::string hex_ssid) const {
71 // Transform hex_ssid to uppercase.
72 transform(hex_ssid.begin(), hex_ssid.end(), hex_ssid.begin(), toupper);
74 const auto it = hex_ssid_to_extension_id_.find(hex_ssid);
75 if (it == hex_ssid_to_extension_id_.end())
76 return std::string();
77 return it->second;
80 bool NetworkingConfigService::IsRegisteredForCaptivePortalEvent(
81 std::string extension_id) const {
82 return event_delegate_->HasExtensionRegisteredForEvent(extension_id);
85 bool NetworkingConfigService::RegisterHexSsid(std::string hex_ssid,
86 const std::string& extension_id) {
87 if (!IsValidNonEmptyHexString(hex_ssid)) {
88 LOG(ERROR) << "\'" << hex_ssid << "\' is not a valid hex encoded string.";
89 return false;
92 // Transform hex_ssid to uppercase.
93 transform(hex_ssid.begin(), hex_ssid.end(), hex_ssid.begin(), toupper);
95 return hex_ssid_to_extension_id_.insert(make_pair(hex_ssid, extension_id))
96 .second;
99 void NetworkingConfigService::UnregisterExtension(
100 const std::string& extension_id) {
101 for (auto it = hex_ssid_to_extension_id_.begin();
102 it != hex_ssid_to_extension_id_.end();) {
103 if (it->second == extension_id)
104 hex_ssid_to_extension_id_.erase(it++);
105 else
106 ++it;
110 const NetworkingConfigService::AuthenticationResult&
111 NetworkingConfigService::GetAuthenticationResult() const {
112 return authentication_result_;
115 void NetworkingConfigService::ResetAuthenticationResult() {
116 authentication_result_ = AuthenticationResult();
119 void NetworkingConfigService::SetAuthenticationResult(
120 const AuthenticationResult& authentication_result) {
121 authentication_result_ = authentication_result;
124 void NetworkingConfigService::OnGotProperties(
125 const std::string& extension_id,
126 const std::string& guid,
127 const std::string& service_path,
128 const base::DictionaryValue& onc_network_config) {
129 EventRouter* eventRouter = EventRouter::Get(browser_context_);
131 // Try to extract |bssid| field.
132 const base::DictionaryValue* wifi_with_state = nullptr;
133 std::string bssid;
134 scoped_ptr<Event> event;
135 if (onc_network_config.GetDictionaryWithoutPathExpansion(
136 ::onc::network_config::kWiFi, &wifi_with_state) &&
137 wifi_with_state->GetStringWithoutPathExpansion(::onc::wifi::kBSSID,
138 &bssid)) {
139 event = CreatePortalDetectedEventAndDispatch(extension_id, guid, &bssid);
140 } else {
141 event = CreatePortalDetectedEventAndDispatch(extension_id, guid, nullptr);
143 eventRouter->DispatchEventToExtension(extension_id, event.Pass());
146 void NetworkingConfigService::OnGetPropertiesFailed(
147 const std::string& extension_id,
148 const std::string& guid,
149 const std::string& error_name,
150 scoped_ptr<base::DictionaryValue> error_data) {
151 LOG(WARNING) << "Failed to determine BSSID for network with guid " << guid
152 << ": " << error_name;
153 EventRouter* eventRouter = EventRouter::Get(browser_context_);
154 scoped_ptr<Event> event =
155 CreatePortalDetectedEventAndDispatch(extension_id, guid, nullptr);
156 eventRouter->DispatchEventToExtension(extension_id, event.Pass());
159 scoped_ptr<Event> NetworkingConfigService::CreatePortalDetectedEventAndDispatch(
160 const std::string& extension_id,
161 const std::string& guid,
162 const std::string* bssid) {
163 const chromeos::NetworkState* network = chromeos::NetworkHandler::Get()
164 ->network_state_handler()
165 ->GetNetworkStateFromGuid(guid);
166 if (!network)
167 return nullptr;
169 // Populate the NetworkInfo object.
170 extensions::core_api::networking_config::NetworkInfo network_info;
171 network_info.type =
172 extensions::core_api::networking_config::NETWORK_TYPE_WIFI;
173 const std::vector<uint8_t>& raw_ssid = network->raw_ssid();
174 std::string hex_ssid =
175 base::HexEncode(vector_as_array(&raw_ssid), raw_ssid.size());
176 network_info.hex_ssid = make_scoped_ptr(new std::string(hex_ssid));
177 network_info.ssid = make_scoped_ptr(new std::string(network->name()));
178 network_info.guid = make_scoped_ptr(new std::string(network->guid()));
179 if (bssid)
180 network_info.bssid.reset(new std::string(*bssid));
181 scoped_ptr<base::ListValue> results =
182 extensions::core_api::networking_config::OnCaptivePortalDetected::Create(
183 network_info);
184 scoped_ptr<Event> event(new Event(extensions::core_api::networking_config::
185 OnCaptivePortalDetected::kEventName,
186 results.Pass()));
187 return event.Pass();
190 void NetworkingConfigService::DispatchPortalDetectedEvent(
191 const std::string& extension_id,
192 const std::string& guid) {
193 // Determine |service_path| of network identified by |guid|.
194 chromeos::NetworkHandler* network_handler = chromeos::NetworkHandler::Get();
195 const chromeos::NetworkState* network =
196 network_handler->network_state_handler()->GetNetworkStateFromGuid(guid);
197 if (!network)
198 return;
199 const std::string service_path = network->path();
201 network_handler->managed_network_configuration_handler()->GetProperties(
202 service_path, base::Bind(&NetworkingConfigService::OnGotProperties,
203 weak_factory_.GetWeakPtr(), extension_id, guid),
204 base::Bind(&NetworkingConfigService::OnGetPropertiesFailed,
205 weak_factory_.GetWeakPtr(), extension_id, guid));
208 } // namespace extensions