| Index: content/browser/geolocation/wifi_data_provider.cc
|
| diff --git a/content/browser/geolocation/wifi_data_provider.cc b/content/browser/geolocation/wifi_data_provider.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..76a05f96edd383ed011194084387186d6ddde640
|
| --- /dev/null
|
| +++ b/content/browser/geolocation/wifi_data_provider.cc
|
| @@ -0,0 +1,139 @@
|
| +// Copyright (c) 2011 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 "content/browser/geolocation/wifi_data_provider.h"
|
| +
|
| +// statics
|
| +WifiDataProvider* WifiDataProvider::instance_ = NULL;
|
| +WifiDataProvider::ImplFactoryFunction WifiDataProvider::factory_function_ =
|
| + DefaultFactoryFunction;
|
| +
|
| +AccessPointData::AccessPointData()
|
| + : radio_signal_strength(kint32min),
|
| + channel(kint32min),
|
| + signal_to_noise(kint32min) {
|
| +}
|
| +
|
| +AccessPointData::~AccessPointData() {}
|
| +
|
| +WifiData::WifiData() {}
|
| +
|
| +WifiData::~WifiData() {}
|
| +
|
| +bool WifiData::DiffersSignificantly(const WifiData& other) const {
|
| + // More than 4 or 50% of access points added or removed is significant.
|
| + static const size_t kMinChangedAccessPoints = 4;
|
| + const size_t min_ap_count =
|
| + std::min(access_point_data.size(), other.access_point_data.size());
|
| + const size_t max_ap_count =
|
| + std::max(access_point_data.size(), other.access_point_data.size());
|
| + const size_t difference_threadhold = std::min(kMinChangedAccessPoints,
|
| + min_ap_count / 2);
|
| + if (max_ap_count > min_ap_count + difference_threadhold)
|
| + return true;
|
| + // Compute size of interesction of old and new sets.
|
| + size_t num_common = 0;
|
| + for (AccessPointDataSet::const_iterator iter = access_point_data.begin();
|
| + iter != access_point_data.end();
|
| + iter++) {
|
| + if (other.access_point_data.find(*iter) !=
|
| + other.access_point_data.end()) {
|
| + ++num_common;
|
| + }
|
| + }
|
| + DCHECK(num_common <= min_ap_count);
|
| +
|
| + // Test how many have changed.
|
| + return max_ap_count > num_common + difference_threadhold;
|
| +}
|
| +
|
| +// static
|
| +void WifiDataProvider::SetFactory(
|
| + WifiDataProvider::ImplFactoryFunction factory_function_in) {
|
| + factory_function_ = factory_function_in;
|
| +}
|
| +
|
| +// static
|
| +void WifiDataProvider::ResetFactory() {
|
| + factory_function_ = DefaultFactoryFunction;
|
| +}
|
| +
|
| +// static
|
| +WifiDataProvider* WifiDataProvider::Register(ListenerInterface* listener) {
|
| + bool need_to_start_thread = false;
|
| + if (!instance_) {
|
| + instance_ = new WifiDataProvider();
|
| + need_to_start_thread = true;
|
| + }
|
| + DCHECK(instance_);
|
| + DCHECK(instance_->CalledOnValidThread());
|
| + instance_->AddListener(listener);
|
| + // Start the provider after adding the listener, to avoid any race in
|
| + // it receiving an early callback.
|
| + if (need_to_start_thread) {
|
| + bool started = instance_->StartDataProvider();
|
| + DCHECK(started);
|
| + }
|
| + return instance_;
|
| +}
|
| +
|
| +// static
|
| +bool WifiDataProvider::Unregister(ListenerInterface* listener) {
|
| + DCHECK(instance_);
|
| + DCHECK(instance_->CalledOnValidThread());
|
| + DCHECK(instance_->has_listeners());
|
| + if (!instance_->RemoveListener(listener)) {
|
| + return false;
|
| + }
|
| + if (!instance_->has_listeners()) {
|
| + // Must stop the provider (and any implementation threads) before
|
| + // destroying to avoid any race conditions in access to the provider in
|
| + // the destructor chain.
|
| + instance_->StopDataProvider();
|
| + delete instance_;
|
| + instance_ = NULL;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +// obtain.
|
| +bool WifiDataProvider::GetData(WifiData* data) {
|
| + DCHECK(this->CalledOnValidThread());
|
| + return impl_->GetData(data);
|
| +}
|
| +
|
| +WifiDataProvider::WifiDataProvider() {
|
| + DCHECK(factory_function_);
|
| + impl_ = (*factory_function_)();
|
| + DCHECK(impl_);
|
| + impl_->SetContainer(this);
|
| +}
|
| +
|
| +WifiDataProvider::~WifiDataProvider() {
|
| + DCHECK(impl_);
|
| + impl_->SetContainer(NULL);
|
| +}
|
| +
|
| +void WifiDataProvider::AddListener(
|
| + WifiDataProvider::ListenerInterface* listener) {
|
| + impl_->AddListener(listener);
|
| +}
|
| +
|
| +bool WifiDataProvider::RemoveListener(
|
| + WifiDataProvider::ListenerInterface* listener) {
|
| + return impl_->RemoveListener(listener);
|
| +}
|
| +
|
| +bool WifiDataProvider::has_listeners() const {
|
| + return impl_->has_listeners();
|
| +}
|
| +
|
| +bool WifiDataProvider::StartDataProvider() {
|
| + return impl_->StartDataProvider();
|
| +}
|
| +
|
| +void WifiDataProvider::StopDataProvider() {
|
| + impl_->StopDataProvider();
|
| +}
|
| +
|
|
|