Chromium Code Reviews| Index: chrome/browser/local_discovery/wifi/wifi_service_client.cc |
| diff --git a/chrome/browser/local_discovery/wifi/wifi_service_client.cc b/chrome/browser/local_discovery/wifi/wifi_service_client.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b5169b947e915b0abbb285f9a4a210f94e4139c3 |
| --- /dev/null |
| +++ b/chrome/browser/local_discovery/wifi/wifi_service_client.cc |
| @@ -0,0 +1,304 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "base/threading/thread.h" |
| +#include "chrome/browser/extensions/api/networking_private/networking_private_service_client_factory.h" |
|
stevenjb
2014/05/05 18:19:49
This appears to be unused
Noam Samuel
2014/05/05 21:42:51
Done.
|
| +#include "chrome/browser/local_discovery/wifi/wifi_service_client.h" |
| +#include "components/onc/onc_constants.h" |
| +#include "components/wifi/wifi_service.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +using wifi::WiFiService; |
| + |
| +namespace local_discovery { |
| + |
| +namespace wifi { |
| + |
| +namespace { |
| + |
| +scoped_ptr<base::DictionaryValue> MakeProperties(const std::string& ssid, |
| + const std::string& password) { |
| + scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue); |
| + |
| + properties->SetString(onc::network_config::kType, onc::network_type::kWiFi); |
| + base::DictionaryValue* wifi = new base::DictionaryValue; |
| + properties->Set(onc::network_config::kWiFi, wifi); |
| + |
| + wifi->SetString(onc::wifi::kSSID, ssid); |
| + wifi->SetString(onc::wifi::kPassphrase, password); |
| + |
| + return properties.Pass(); |
| +} |
| + |
| +} // namespace |
| + |
| +class WifiServiceContainer { |
| + public: |
| + explicit WifiServiceContainer( |
| + const base::WeakPtr<WifiServiceClient> service_client) |
| + : service_client_(service_client), weak_factory_(this) {} |
| + |
| + ~WifiServiceContainer() {} |
| + |
| + void Start() { |
| + wifi_service_.reset(WiFiService::Create()); |
| + |
| + wifi_service_->Initialize(base::MessageLoopProxy::current()); |
| + |
| + wifi_service_->SetEventObservers( |
| + base::MessageLoopProxy::current(), |
| + base::Bind(&WifiServiceContainer::OnNetworksChangedEvent, |
| + base::Unretained(this)), |
| + base::Bind(&WifiServiceContainer::OnNetworkListChangedEvent, |
| + base::Unretained(this))); |
| + } |
| + |
| + void GetSSIDList(const WifiClient::SSIDListCallback& callback) { |
| + base::ListValue visible_networks; |
| + |
| + wifi_service_->GetVisibleNetworks(onc::network_type::kWiFi, |
| + &visible_networks); |
| + |
| + std::vector<NetworkProperties> network_property_list; |
| + |
| + for (size_t i = 0; i < visible_networks.GetSize(); i++) { |
| + const base::DictionaryValue* network_value = NULL; |
| + NetworkProperties network_properties; |
| + std::string connection_status; |
| + |
| + if (!visible_networks.GetDictionary(i, &network_value) || |
| + !network_value->GetString(onc::network_config::kName, |
| + &network_properties.ssid) || |
| + !network_value->GetString(onc::network_config::kGUID, |
| + &network_properties.internal_id) || |
| + !network_value->GetString(onc::network_config::kConnectionState, |
| + &connection_status)) { |
| + NOTREACHED(); |
| + continue; |
| + } |
| + |
| + network_properties.connected = |
| + (connection_status == onc::connection_state::kConnected); |
| + |
| + network_property_list.push_back(network_properties); |
| + } |
| + |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&WifiServiceClient::PostClosure, |
| + service_client_, |
| + base::Bind(callback, network_property_list))); |
| + } |
| + |
| + void ConnectToNetwork(const std::string& ssid, |
| + const std::string& internal_id, |
| + const std::string& password, |
| + const WifiClient::SuccessCallback& callback) { |
| + scoped_ptr<base::DictionaryValue> properties = |
| + MakeProperties(ssid, password); |
| + std::string network_guid; |
| + bool error = false; |
| + std::string error_string; |
| + |
| + if (!internal_id.empty()) { |
| + network_guid = internal_id; |
| + wifi_service_->SetProperties( |
| + network_guid, properties.Pass(), &error_string); |
| + |
| + if (!error_string.empty()) { |
| + LOG(ERROR) << "Could not set properties on network: " << error_string; |
| + error = true; |
| + } |
| + } else { |
| + wifi_service_->CreateNetwork( |
| + false, properties.Pass(), &network_guid, &error_string); |
| + |
| + if (!error_string.empty()) { |
| + LOG(ERROR) << "Could not create network: " << error_string; |
| + error = true; |
| + } |
| + } |
| + |
| + if (!error) { |
| + wifi_service_->StartConnect(network_guid, &error_string); |
| + |
| + if (!error_string.empty()) { |
| + LOG(ERROR) << "Could not connect to network: " << error_string; |
| + error = true; |
| + } |
| + } |
| + |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&WifiServiceClient::PostClosure, |
| + service_client_, |
| + base::Bind(callback, !error))); |
| + } |
| + |
| + void OnNetworksChangedEvent(const std::vector<std::string>& network_guids) {} |
| + |
| + void OnNetworkListChangedEvent( |
| + const std::vector<std::string>& network_guids) { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&WifiServiceClient::OnNetworkListChanged, service_client_)); |
| + } |
| + |
| + base::WeakPtr<WifiServiceContainer> AsWeakPtr() { |
| + return weak_factory_.GetWeakPtr(); |
| + } |
| + |
| + void RequestScan() { wifi_service_->RequestNetworkScan(); } |
| + |
| + void ConnectToNetworkByID(const std::string& network_guid, |
| + const WifiClient::SuccessCallback& callback) { |
| + std::string error_string; |
| + bool error = false; |
| + |
| + wifi_service_->StartConnect(network_guid, &error_string); |
| + |
| + if (!error_string.empty()) { |
| + LOG(ERROR) << "Could not connect to network by ID: " << error_string; |
| + error = true; |
| + } |
| + |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&WifiServiceClient::PostClosure, |
| + service_client_, |
| + base::Bind(callback, !error))); |
| + } |
| + |
| + void GetNetworkCredentials(const std::string& network_guid, |
| + const WifiClient::CredentialsCallback& callback) { |
| + std::string error_string; |
| + bool error = false; |
| + std::string ssid; |
| + std::string key; |
| + |
| + base::DictionaryValue properties; |
| + |
| + wifi_service_->GetProperties(network_guid, &properties, &error_string); |
| + |
| + if (!error_string.empty()) { |
| + LOG(ERROR) << "Could not get network properties: " << error_string; |
| + error = true; |
| + } |
| + |
| + if (!properties.GetString(onc::network_config::kName, &ssid)) { |
| + LOG(ERROR) << "Could not get network SSID"; |
| + error = true; |
| + } |
| + |
| + if (!error) { |
| + wifi_service_->GetKeyFromSystem(network_guid, &key, &error_string); |
|
mef
2014/05/05 18:14:40
On Windows |GetKeyFromSystem| without UAC escalati
|
| + |
| + if (!error_string.empty()) { |
| + LOG(ERROR) << "Could not get key from system: " << error_string; |
| + error = true; |
| + } |
| + } |
| + |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&WifiServiceClient::PostClosure, |
| + service_client_, |
| + base::Bind(callback, !error, ssid, key))); |
| + } |
| + |
| + private: |
| + scoped_ptr<WiFiService> wifi_service_; |
| + base::WeakPtr<WifiServiceClient> service_client_; |
| + base::WeakPtrFactory<WifiServiceContainer> weak_factory_; |
| +}; |
| + |
| +WifiServiceClient::WifiServiceClient() : weak_factory_(this) { |
| +} |
| + |
| +WifiServiceClient::~WifiServiceClient() { |
| +} |
| + |
| +void WifiServiceClient::Start() { |
| + task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( |
| + content::BrowserThread::FILE); |
| + |
| + scoped_ptr<WifiServiceContainer, ThreadSpecificDeleter<WifiServiceContainer> > |
| + wifi_container(new WifiServiceContainer(weak_factory_.GetWeakPtr()), |
| + ThreadSpecificDeleter<WifiServiceContainer>(task_runner_)); |
| + wifi_container_.swap(wifi_container); |
| + |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&WifiServiceContainer::Start, wifi_container_->AsWeakPtr())); |
| +} |
| + |
| +void WifiServiceClient::GetSSIDList(const SSIDListCallback& callback) { |
| + task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&WifiServiceContainer::GetSSIDList, |
| + wifi_container_->AsWeakPtr(), |
| + callback)); |
| +} |
| + |
| +void WifiServiceClient::RequestScan(const SuccessCallback& callback) { |
| + scan_callbacks_.push_back(callback); |
| + task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&WifiServiceContainer::RequestScan, |
| + wifi_container_->AsWeakPtr())); |
| +} |
| + |
| +void WifiServiceClient::OnNetworkListChanged() { |
| + std::vector<SuccessCallback> scan_callbacks; |
| + scan_callbacks.swap(scan_callbacks_); |
| + |
| + for (std::vector<SuccessCallback>::iterator i = scan_callbacks.begin(); |
| + i != scan_callbacks.end(); |
| + i++) { |
| + i->Run(true); |
| + } |
| +} |
| + |
| +void WifiServiceClient::PostClosure(const base::Closure& callback) { |
| + callback.Run(); |
| +} |
| + |
| +void WifiServiceClient::ConnectToNetwork(const std::string& ssid, |
| + const std::string& internal_id, |
| + const std::string& password, |
| + const SuccessCallback& callback) { |
| + task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&WifiServiceContainer::ConnectToNetwork, |
| + wifi_container_->AsWeakPtr(), |
| + ssid, |
| + internal_id, |
| + password, |
| + callback)); |
| +} |
| + |
| +void WifiServiceClient::ConnectToNetworkByID(const std::string& internal_id, |
| + const SuccessCallback& callback) { |
| + task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&WifiServiceContainer::ConnectToNetworkByID, |
| + wifi_container_->AsWeakPtr(), |
| + internal_id, |
| + callback)); |
| +} |
| + |
| +void WifiServiceClient::GetNetworkCredentials( |
| + const std::string& internal_id, |
| + const CredentialsCallback& callback) { |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&WifiServiceContainer::GetNetworkCredentials, |
| + wifi_container_->AsWeakPtr(), |
| + internal_id, |
| + callback)); |
| +} |
| + |
| +} // namespace wifi |
| + |
| +} // namespace local_discovery |