| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 "content/browser/geolocation/wifi_data_provider.h" |
| 6 |
| 7 // statics |
| 8 WifiDataProvider* WifiDataProvider::instance_ = NULL; |
| 9 WifiDataProvider::ImplFactoryFunction WifiDataProvider::factory_function_ = |
| 10 DefaultFactoryFunction; |
| 11 |
| 12 AccessPointData::AccessPointData() |
| 13 : radio_signal_strength(kint32min), |
| 14 channel(kint32min), |
| 15 signal_to_noise(kint32min) { |
| 16 } |
| 17 |
| 18 AccessPointData::~AccessPointData() {} |
| 19 |
| 20 WifiData::WifiData() {} |
| 21 |
| 22 WifiData::~WifiData() {} |
| 23 |
| 24 bool WifiData::DiffersSignificantly(const WifiData& other) const { |
| 25 // More than 4 or 50% of access points added or removed is significant. |
| 26 static const size_t kMinChangedAccessPoints = 4; |
| 27 const size_t min_ap_count = |
| 28 std::min(access_point_data.size(), other.access_point_data.size()); |
| 29 const size_t max_ap_count = |
| 30 std::max(access_point_data.size(), other.access_point_data.size()); |
| 31 const size_t difference_threadhold = std::min(kMinChangedAccessPoints, |
| 32 min_ap_count / 2); |
| 33 if (max_ap_count > min_ap_count + difference_threadhold) |
| 34 return true; |
| 35 // Compute size of interesction of old and new sets. |
| 36 size_t num_common = 0; |
| 37 for (AccessPointDataSet::const_iterator iter = access_point_data.begin(); |
| 38 iter != access_point_data.end(); |
| 39 iter++) { |
| 40 if (other.access_point_data.find(*iter) != |
| 41 other.access_point_data.end()) { |
| 42 ++num_common; |
| 43 } |
| 44 } |
| 45 DCHECK(num_common <= min_ap_count); |
| 46 |
| 47 // Test how many have changed. |
| 48 return max_ap_count > num_common + difference_threadhold; |
| 49 } |
| 50 |
| 51 // static |
| 52 void WifiDataProvider::SetFactory( |
| 53 WifiDataProvider::ImplFactoryFunction factory_function_in) { |
| 54 factory_function_ = factory_function_in; |
| 55 } |
| 56 |
| 57 // static |
| 58 void WifiDataProvider::ResetFactory() { |
| 59 factory_function_ = DefaultFactoryFunction; |
| 60 } |
| 61 |
| 62 // static |
| 63 WifiDataProvider* WifiDataProvider::Register(ListenerInterface* listener) { |
| 64 bool need_to_start_thread = false; |
| 65 if (!instance_) { |
| 66 instance_ = new WifiDataProvider(); |
| 67 need_to_start_thread = true; |
| 68 } |
| 69 DCHECK(instance_); |
| 70 DCHECK(instance_->CalledOnValidThread()); |
| 71 instance_->AddListener(listener); |
| 72 // Start the provider after adding the listener, to avoid any race in |
| 73 // it receiving an early callback. |
| 74 if (need_to_start_thread) { |
| 75 bool started = instance_->StartDataProvider(); |
| 76 DCHECK(started); |
| 77 } |
| 78 return instance_; |
| 79 } |
| 80 |
| 81 // static |
| 82 bool WifiDataProvider::Unregister(ListenerInterface* listener) { |
| 83 DCHECK(instance_); |
| 84 DCHECK(instance_->CalledOnValidThread()); |
| 85 DCHECK(instance_->has_listeners()); |
| 86 if (!instance_->RemoveListener(listener)) { |
| 87 return false; |
| 88 } |
| 89 if (!instance_->has_listeners()) { |
| 90 // Must stop the provider (and any implementation threads) before |
| 91 // destroying to avoid any race conditions in access to the provider in |
| 92 // the destructor chain. |
| 93 instance_->StopDataProvider(); |
| 94 delete instance_; |
| 95 instance_ = NULL; |
| 96 } |
| 97 return true; |
| 98 } |
| 99 |
| 100 // obtain. |
| 101 bool WifiDataProvider::GetData(WifiData* data) { |
| 102 DCHECK(this->CalledOnValidThread()); |
| 103 return impl_->GetData(data); |
| 104 } |
| 105 |
| 106 WifiDataProvider::WifiDataProvider() { |
| 107 DCHECK(factory_function_); |
| 108 impl_ = (*factory_function_)(); |
| 109 DCHECK(impl_); |
| 110 impl_->SetContainer(this); |
| 111 } |
| 112 |
| 113 WifiDataProvider::~WifiDataProvider() { |
| 114 DCHECK(impl_); |
| 115 impl_->SetContainer(NULL); |
| 116 } |
| 117 |
| 118 void WifiDataProvider::AddListener( |
| 119 WifiDataProvider::ListenerInterface* listener) { |
| 120 impl_->AddListener(listener); |
| 121 } |
| 122 |
| 123 bool WifiDataProvider::RemoveListener( |
| 124 WifiDataProvider::ListenerInterface* listener) { |
| 125 return impl_->RemoveListener(listener); |
| 126 } |
| 127 |
| 128 bool WifiDataProvider::has_listeners() const { |
| 129 return impl_->has_listeners(); |
| 130 } |
| 131 |
| 132 bool WifiDataProvider::StartDataProvider() { |
| 133 return impl_->StartDataProvider(); |
| 134 } |
| 135 |
| 136 void WifiDataProvider::StopDataProvider() { |
| 137 impl_->StopDataProvider(); |
| 138 } |
| 139 |
| OLD | NEW |