Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "base/threading/sequenced_worker_pool.h" | |
| 6 #include "base/threading/thread.h" | |
| 7 #include "chrome/browser/extensions/api/networking_private/networking_private_se rvice_client_factory.h" | |
|
stevenjb
2014/05/05 18:19:49
This appears to be unused
Noam Samuel
2014/05/05 21:42:51
Done.
| |
| 8 #include "chrome/browser/local_discovery/wifi/wifi_service_client.h" | |
| 9 #include "components/onc/onc_constants.h" | |
| 10 #include "components/wifi/wifi_service.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 | |
| 13 using wifi::WiFiService; | |
| 14 | |
| 15 namespace local_discovery { | |
| 16 | |
| 17 namespace wifi { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 scoped_ptr<base::DictionaryValue> MakeProperties(const std::string& ssid, | |
| 22 const std::string& password) { | |
| 23 scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue); | |
| 24 | |
| 25 properties->SetString(onc::network_config::kType, onc::network_type::kWiFi); | |
| 26 base::DictionaryValue* wifi = new base::DictionaryValue; | |
| 27 properties->Set(onc::network_config::kWiFi, wifi); | |
| 28 | |
| 29 wifi->SetString(onc::wifi::kSSID, ssid); | |
| 30 wifi->SetString(onc::wifi::kPassphrase, password); | |
| 31 | |
| 32 return properties.Pass(); | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 class WifiServiceContainer { | |
| 38 public: | |
| 39 explicit WifiServiceContainer( | |
| 40 const base::WeakPtr<WifiServiceClient> service_client) | |
| 41 : service_client_(service_client), weak_factory_(this) {} | |
| 42 | |
| 43 ~WifiServiceContainer() {} | |
| 44 | |
| 45 void Start() { | |
| 46 wifi_service_.reset(WiFiService::Create()); | |
| 47 | |
| 48 wifi_service_->Initialize(base::MessageLoopProxy::current()); | |
| 49 | |
| 50 wifi_service_->SetEventObservers( | |
| 51 base::MessageLoopProxy::current(), | |
| 52 base::Bind(&WifiServiceContainer::OnNetworksChangedEvent, | |
| 53 base::Unretained(this)), | |
| 54 base::Bind(&WifiServiceContainer::OnNetworkListChangedEvent, | |
| 55 base::Unretained(this))); | |
| 56 } | |
| 57 | |
| 58 void GetSSIDList(const WifiClient::SSIDListCallback& callback) { | |
| 59 base::ListValue visible_networks; | |
| 60 | |
| 61 wifi_service_->GetVisibleNetworks(onc::network_type::kWiFi, | |
| 62 &visible_networks); | |
| 63 | |
| 64 std::vector<NetworkProperties> network_property_list; | |
| 65 | |
| 66 for (size_t i = 0; i < visible_networks.GetSize(); i++) { | |
| 67 const base::DictionaryValue* network_value = NULL; | |
| 68 NetworkProperties network_properties; | |
| 69 std::string connection_status; | |
| 70 | |
| 71 if (!visible_networks.GetDictionary(i, &network_value) || | |
| 72 !network_value->GetString(onc::network_config::kName, | |
| 73 &network_properties.ssid) || | |
| 74 !network_value->GetString(onc::network_config::kGUID, | |
| 75 &network_properties.internal_id) || | |
| 76 !network_value->GetString(onc::network_config::kConnectionState, | |
| 77 &connection_status)) { | |
| 78 NOTREACHED(); | |
| 79 continue; | |
| 80 } | |
| 81 | |
| 82 network_properties.connected = | |
| 83 (connection_status == onc::connection_state::kConnected); | |
| 84 | |
| 85 network_property_list.push_back(network_properties); | |
| 86 } | |
| 87 | |
| 88 content::BrowserThread::PostTask( | |
| 89 content::BrowserThread::UI, | |
| 90 FROM_HERE, | |
| 91 base::Bind(&WifiServiceClient::PostClosure, | |
| 92 service_client_, | |
| 93 base::Bind(callback, network_property_list))); | |
| 94 } | |
| 95 | |
| 96 void ConnectToNetwork(const std::string& ssid, | |
| 97 const std::string& internal_id, | |
| 98 const std::string& password, | |
| 99 const WifiClient::SuccessCallback& callback) { | |
| 100 scoped_ptr<base::DictionaryValue> properties = | |
| 101 MakeProperties(ssid, password); | |
| 102 std::string network_guid; | |
| 103 bool error = false; | |
| 104 std::string error_string; | |
| 105 | |
| 106 if (!internal_id.empty()) { | |
| 107 network_guid = internal_id; | |
| 108 wifi_service_->SetProperties( | |
| 109 network_guid, properties.Pass(), &error_string); | |
| 110 | |
| 111 if (!error_string.empty()) { | |
| 112 LOG(ERROR) << "Could not set properties on network: " << error_string; | |
| 113 error = true; | |
| 114 } | |
| 115 } else { | |
| 116 wifi_service_->CreateNetwork( | |
| 117 false, properties.Pass(), &network_guid, &error_string); | |
| 118 | |
| 119 if (!error_string.empty()) { | |
| 120 LOG(ERROR) << "Could not create network: " << error_string; | |
| 121 error = true; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 if (!error) { | |
| 126 wifi_service_->StartConnect(network_guid, &error_string); | |
| 127 | |
| 128 if (!error_string.empty()) { | |
| 129 LOG(ERROR) << "Could not connect to network: " << error_string; | |
| 130 error = true; | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 content::BrowserThread::PostTask(content::BrowserThread::UI, | |
| 135 FROM_HERE, | |
| 136 base::Bind(&WifiServiceClient::PostClosure, | |
| 137 service_client_, | |
| 138 base::Bind(callback, !error))); | |
| 139 } | |
| 140 | |
| 141 void OnNetworksChangedEvent(const std::vector<std::string>& network_guids) {} | |
| 142 | |
| 143 void OnNetworkListChangedEvent( | |
| 144 const std::vector<std::string>& network_guids) { | |
| 145 content::BrowserThread::PostTask( | |
| 146 content::BrowserThread::UI, | |
| 147 FROM_HERE, | |
| 148 base::Bind(&WifiServiceClient::OnNetworkListChanged, service_client_)); | |
| 149 } | |
| 150 | |
| 151 base::WeakPtr<WifiServiceContainer> AsWeakPtr() { | |
| 152 return weak_factory_.GetWeakPtr(); | |
| 153 } | |
| 154 | |
| 155 void RequestScan() { wifi_service_->RequestNetworkScan(); } | |
| 156 | |
| 157 void ConnectToNetworkByID(const std::string& network_guid, | |
| 158 const WifiClient::SuccessCallback& callback) { | |
| 159 std::string error_string; | |
| 160 bool error = false; | |
| 161 | |
| 162 wifi_service_->StartConnect(network_guid, &error_string); | |
| 163 | |
| 164 if (!error_string.empty()) { | |
| 165 LOG(ERROR) << "Could not connect to network by ID: " << error_string; | |
| 166 error = true; | |
| 167 } | |
| 168 | |
| 169 content::BrowserThread::PostTask(content::BrowserThread::UI, | |
| 170 FROM_HERE, | |
| 171 base::Bind(&WifiServiceClient::PostClosure, | |
| 172 service_client_, | |
| 173 base::Bind(callback, !error))); | |
| 174 } | |
| 175 | |
| 176 void GetNetworkCredentials(const std::string& network_guid, | |
| 177 const WifiClient::CredentialsCallback& callback) { | |
| 178 std::string error_string; | |
| 179 bool error = false; | |
| 180 std::string ssid; | |
| 181 std::string key; | |
| 182 | |
| 183 base::DictionaryValue properties; | |
| 184 | |
| 185 wifi_service_->GetProperties(network_guid, &properties, &error_string); | |
| 186 | |
| 187 if (!error_string.empty()) { | |
| 188 LOG(ERROR) << "Could not get network properties: " << error_string; | |
| 189 error = true; | |
| 190 } | |
| 191 | |
| 192 if (!properties.GetString(onc::network_config::kName, &ssid)) { | |
| 193 LOG(ERROR) << "Could not get network SSID"; | |
| 194 error = true; | |
| 195 } | |
| 196 | |
| 197 if (!error) { | |
| 198 wifi_service_->GetKeyFromSystem(network_guid, &key, &error_string); | |
|
mef
2014/05/05 18:14:40
On Windows |GetKeyFromSystem| without UAC escalati
| |
| 199 | |
| 200 if (!error_string.empty()) { | |
| 201 LOG(ERROR) << "Could not get key from system: " << error_string; | |
| 202 error = true; | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 content::BrowserThread::PostTask( | |
| 207 content::BrowserThread::UI, | |
| 208 FROM_HERE, | |
| 209 base::Bind(&WifiServiceClient::PostClosure, | |
| 210 service_client_, | |
| 211 base::Bind(callback, !error, ssid, key))); | |
| 212 } | |
| 213 | |
| 214 private: | |
| 215 scoped_ptr<WiFiService> wifi_service_; | |
| 216 base::WeakPtr<WifiServiceClient> service_client_; | |
| 217 base::WeakPtrFactory<WifiServiceContainer> weak_factory_; | |
| 218 }; | |
| 219 | |
| 220 WifiServiceClient::WifiServiceClient() : weak_factory_(this) { | |
| 221 } | |
| 222 | |
| 223 WifiServiceClient::~WifiServiceClient() { | |
| 224 } | |
| 225 | |
| 226 void WifiServiceClient::Start() { | |
| 227 task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( | |
| 228 content::BrowserThread::FILE); | |
| 229 | |
| 230 scoped_ptr<WifiServiceContainer, ThreadSpecificDeleter<WifiServiceContainer> > | |
| 231 wifi_container(new WifiServiceContainer(weak_factory_.GetWeakPtr()), | |
| 232 ThreadSpecificDeleter<WifiServiceContainer>(task_runner_)); | |
| 233 wifi_container_.swap(wifi_container); | |
| 234 | |
| 235 task_runner_->PostTask( | |
| 236 FROM_HERE, | |
| 237 base::Bind(&WifiServiceContainer::Start, wifi_container_->AsWeakPtr())); | |
| 238 } | |
| 239 | |
| 240 void WifiServiceClient::GetSSIDList(const SSIDListCallback& callback) { | |
| 241 task_runner_->PostTask(FROM_HERE, | |
| 242 base::Bind(&WifiServiceContainer::GetSSIDList, | |
| 243 wifi_container_->AsWeakPtr(), | |
| 244 callback)); | |
| 245 } | |
| 246 | |
| 247 void WifiServiceClient::RequestScan(const SuccessCallback& callback) { | |
| 248 scan_callbacks_.push_back(callback); | |
| 249 task_runner_->PostTask(FROM_HERE, | |
| 250 base::Bind(&WifiServiceContainer::RequestScan, | |
| 251 wifi_container_->AsWeakPtr())); | |
| 252 } | |
| 253 | |
| 254 void WifiServiceClient::OnNetworkListChanged() { | |
| 255 std::vector<SuccessCallback> scan_callbacks; | |
| 256 scan_callbacks.swap(scan_callbacks_); | |
| 257 | |
| 258 for (std::vector<SuccessCallback>::iterator i = scan_callbacks.begin(); | |
| 259 i != scan_callbacks.end(); | |
| 260 i++) { | |
| 261 i->Run(true); | |
| 262 } | |
| 263 } | |
| 264 | |
| 265 void WifiServiceClient::PostClosure(const base::Closure& callback) { | |
| 266 callback.Run(); | |
| 267 } | |
| 268 | |
| 269 void WifiServiceClient::ConnectToNetwork(const std::string& ssid, | |
| 270 const std::string& internal_id, | |
| 271 const std::string& password, | |
| 272 const SuccessCallback& callback) { | |
| 273 task_runner_->PostTask(FROM_HERE, | |
| 274 base::Bind(&WifiServiceContainer::ConnectToNetwork, | |
| 275 wifi_container_->AsWeakPtr(), | |
| 276 ssid, | |
| 277 internal_id, | |
| 278 password, | |
| 279 callback)); | |
| 280 } | |
| 281 | |
| 282 void WifiServiceClient::ConnectToNetworkByID(const std::string& internal_id, | |
| 283 const SuccessCallback& callback) { | |
| 284 task_runner_->PostTask(FROM_HERE, | |
| 285 base::Bind(&WifiServiceContainer::ConnectToNetworkByID, | |
| 286 wifi_container_->AsWeakPtr(), | |
| 287 internal_id, | |
| 288 callback)); | |
| 289 } | |
| 290 | |
| 291 void WifiServiceClient::GetNetworkCredentials( | |
| 292 const std::string& internal_id, | |
| 293 const CredentialsCallback& callback) { | |
| 294 task_runner_->PostTask( | |
| 295 FROM_HERE, | |
| 296 base::Bind(&WifiServiceContainer::GetNetworkCredentials, | |
| 297 wifi_container_->AsWeakPtr(), | |
| 298 internal_id, | |
| 299 callback)); | |
| 300 } | |
| 301 | |
| 302 } // namespace wifi | |
| 303 | |
| 304 } // namespace local_discovery | |
| OLD | NEW |