diff options
Diffstat (limited to 'chrome/browser/geolocation')
3 files changed, 208 insertions, 0 deletions
diff --git a/chrome/browser/geolocation/wifi_data_provider_chromeos.cc b/chrome/browser/geolocation/wifi_data_provider_chromeos.cc new file mode 100644 index 0000000..41097d3 --- /dev/null +++ b/chrome/browser/geolocation/wifi_data_provider_chromeos.cc @@ -0,0 +1,98 @@ +// Copyright (c) 2010 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. + +// Provides wifi scan API binding for chromeos, using proprietary APIs. + +#include "chrome/browser/geolocation/wifi_data_provider_chromeos.h" + +#include "base/utf_string_conversions.h" +#include "chrome/browser/chromeos/cros/network_library.h" + +namespace { +// The time periods between successive polls of the wifi data. +const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s +const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins +const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins +} + +namespace chromeos { +namespace { +// Wifi API binding to network_library.h, to allow reuse of the polling behavior +// defined in WifiDataProviderCommon. +class NetworkLibraryWlanApi : public WifiDataProviderCommon::WlanApiInterface { + public: + // Does not transfer ownership, |lib| must remain valid for lifetime of + // this object. + explicit NetworkLibraryWlanApi(NetworkLibrary* lib); + ~NetworkLibraryWlanApi(); + + // WifiDataProviderCommon::WlanApiInterface + bool GetAccessPointData(WifiData::AccessPointDataSet* data); + + private: + NetworkLibrary* network_library_; + + DISALLOW_COPY_AND_ASSIGN(NetworkLibraryWlanApi); +}; + +NetworkLibraryWlanApi::NetworkLibraryWlanApi(NetworkLibrary* lib) + : network_library_(lib) { + DCHECK(network_library_ != NULL); +} + +NetworkLibraryWlanApi::~NetworkLibraryWlanApi() { +} + +bool NetworkLibraryWlanApi::GetAccessPointData( + WifiData::AccessPointDataSet* data) { + const WifiNetworkVector& networks = network_library_->wifi_networks(); + for (WifiNetworkVector::const_iterator i = networks.begin(); + i != networks.end(); ++i) { + for (WifiNetwork::AccessPointVector::const_iterator j = + i->access_points().begin(); j != i->access_points().end(); ++j) { + AccessPointData ap_data; + ap_data.mac_address = ASCIIToUTF16(j->mac_address); + ap_data.radio_signal_strength = j->signal_strength; + ap_data.channel = j->channel; + ap_data.signal_to_noise = j->signal_to_noise; + ap_data.ssid = UTF8ToUTF16(i->name()); + data->insert(ap_data); + } + } + return !data->empty() || network_library_->wifi_available(); +} + +} // namespace +} // namespace chromeos + +template<> +WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() { + return new WifiDataProviderChromeOs(); +} + +WifiDataProviderChromeOs::WifiDataProviderChromeOs() { +} + +WifiDataProviderChromeOs::~WifiDataProviderChromeOs() { +} + +WifiDataProviderCommon::WlanApiInterface* + WifiDataProviderChromeOs::NewWlanApi(chromeos::NetworkLibrary* lib) { + return new chromeos::NetworkLibraryWlanApi(lib); +} + +WifiDataProviderCommon::WlanApiInterface* + WifiDataProviderChromeOs::NewWlanApi() { + if (network_library_ == NULL) { + network_library_.reset(new chromeos::NetworkLibraryImpl()); + // TODO(joth): Check net_lib loaded ok, if not return NULL. + } + return NewWlanApi(network_library_.get()); +} + +PollingPolicyInterface* WifiDataProviderChromeOs::NewPollingPolicy() { + return new GenericPollingPolicy<kDefaultPollingIntervalMilliseconds, + kNoChangePollingIntervalMilliseconds, + kTwoNoChangePollingIntervalMilliseconds>; +} diff --git a/chrome/browser/geolocation/wifi_data_provider_chromeos.h b/chrome/browser/geolocation/wifi_data_provider_chromeos.h new file mode 100644 index 0000000..0e041e9 --- /dev/null +++ b/chrome/browser/geolocation/wifi_data_provider_chromeos.h @@ -0,0 +1,33 @@ +// Copyright (c) 2010 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. + +#ifndef CHROME_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_CHROMEOS_H_ +#define CHROME_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_CHROMEOS_H_ + +#include "chrome/browser/geolocation/wifi_data_provider_common.h" + +#include "base/scoped_ptr.h" + +namespace chromeos { class NetworkLibrary; } + +class WifiDataProviderChromeOs : public WifiDataProviderCommon { + public: + WifiDataProviderChromeOs(); + + // Allows injection of |lib| for testing. + static WlanApiInterface* NewWlanApi(chromeos::NetworkLibrary* lib); + + private: + virtual ~WifiDataProviderChromeOs(); + + // WifiDataProviderCommon + virtual WlanApiInterface* NewWlanApi(); + virtual PollingPolicyInterface* NewPollingPolicy(); + + scoped_ptr<chromeos::NetworkLibrary> network_library_; + + DISALLOW_COPY_AND_ASSIGN(WifiDataProviderChromeOs); +}; + +#endif // CHROME_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_CHROMEOS_H_ diff --git a/chrome/browser/geolocation/wifi_data_provider_unittest_chromeos.cc b/chrome/browser/geolocation/wifi_data_provider_unittest_chromeos.cc new file mode 100644 index 0000000..ca31a21 --- /dev/null +++ b/chrome/browser/geolocation/wifi_data_provider_unittest_chromeos.cc @@ -0,0 +1,77 @@ +// Copyright (c) 2010 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 "chrome/browser/geolocation/wifi_data_provider_chromeos.h" + +#include "base/utf_string_conversions.h" +#include "chrome/browser/chromeos/cros/mock_network_library.h" +#include "testing/gtest/include/gtest/gtest.h" + +using ::testing::Return; +using ::testing::ReturnRef; + +namespace chromeos { + +class GeolocationChromeOsWifiDataProviderTest : public testing::Test { + protected: + GeolocationChromeOsWifiDataProviderTest() + : api_(WifiDataProviderChromeOs::NewWlanApi(&net_lib_)){} + virtual void SetUp() { + EXPECT_CALL(net_lib_, wifi_networks()) + .WillOnce(ReturnRef(wifi_network_data_)); + } + void AddWifiAps(int ssids, int aps_per_ssid) { + for (int i = 0; i < ssids; ++i) { + WifiNetwork network; + network.set_name(StringPrintf("SSID %d", i)); + std::vector<WifiNetwork::AccessPoint> aps; + for (int j = 0; j < aps_per_ssid; ++j) { + WifiNetwork::AccessPoint ap; + ap.channel = i * 10 + j; + ap.mac_address = StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", + i, j, 3, 4, 5, 6); + ap.signal_strength = j; + ap.signal_to_noise = i; + aps.push_back(ap); + } + network.set_access_points(aps); + wifi_network_data_.push_back(network); + } + } + + chromeos::MockNetworkLibrary net_lib_; + scoped_ptr<WifiDataProviderCommon::WlanApiInterface> api_; + WifiNetworkVector wifi_network_data_; + WifiData::AccessPointDataSet ap_data_; +}; + +TEST_F(GeolocationChromeOsWifiDataProviderTest, NoWifiAvailable) { + EXPECT_CALL(net_lib_, wifi_available()) + .WillRepeatedly(Return(false)); + EXPECT_FALSE(api_->GetAccessPointData(&ap_data_)); + EXPECT_EQ(0u, ap_data_.size()); +} + +TEST_F(GeolocationChromeOsWifiDataProviderTest, NoAccessPointsInRange) { + EXPECT_CALL(net_lib_, wifi_available()) + .WillRepeatedly(Return(true)); + EXPECT_TRUE(api_->GetAccessPointData(&ap_data_)); + EXPECT_EQ(0u, ap_data_.size()); +} + +TEST_F(GeolocationChromeOsWifiDataProviderTest, GetOneAccessPoint) { + AddWifiAps(1, 1); + EXPECT_TRUE(api_->GetAccessPointData(&ap_data_)); + EXPECT_EQ(1u, ap_data_.size()); + EXPECT_EQ("00:00:03:04:05:06", UTF16ToUTF8(ap_data_.begin()->mac_address)); + EXPECT_EQ("SSID 0", UTF16ToUTF8(ap_data_.begin()->ssid)); +} + +TEST_F(GeolocationChromeOsWifiDataProviderTest, GetManyAccessPoints) { + AddWifiAps(3, 4); + EXPECT_TRUE(api_->GetAccessPointData(&ap_data_)); + EXPECT_EQ(12u, ap_data_.size()); +} + +} // namespace chromeos |