| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // A device data provider provides data from the device that is used by a | 5 // A device data provider provides data from the device that is used by a |
| 6 // NetworkLocationProvider to obtain a position fix. This data may be either | 6 // NetworkLocationProvider to obtain a position fix. This data may be either |
| 7 // cell radio data or wifi data. For a given type of data, we use a singleton | 7 // cell radio data or wifi data. For a given type of data, we use a singleton |
| 8 // instance of the device data provider, which is used by multiple | 8 // instance of the device data provider, which is used by multiple |
| 9 // NetworkLocationProvider objects. | 9 // NetworkLocationProvider objects. |
| 10 // | 10 // |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 string16 carrier; // Carrier name. | 117 string16 carrier; // Carrier name. |
| 118 }; | 118 }; |
| 119 | 119 |
| 120 // Wifi data relating to a single access point. | 120 // Wifi data relating to a single access point. |
| 121 struct AccessPointData { | 121 struct AccessPointData { |
| 122 AccessPointData() | 122 AccessPointData() |
| 123 : radio_signal_strength(kint32min), | 123 : radio_signal_strength(kint32min), |
| 124 age(kint32min), | 124 age(kint32min), |
| 125 channel(kint32min), | 125 channel(kint32min), |
| 126 signal_to_noise(kint32min) {} | 126 signal_to_noise(kint32min) {} |
| 127 | 127 // MAC address, formatted as per MacAddressAsString16. |
| 128 string16 mac_address; | 128 string16 mac_address; |
| 129 int radio_signal_strength; // Measured in dBm | 129 int radio_signal_strength; // Measured in dBm |
| 130 int age; // Milliseconds since this access point was detected | 130 int age; // Milliseconds since this access point was detected |
| 131 int channel; | 131 int channel; |
| 132 int signal_to_noise; // Ratio in dB | 132 int signal_to_noise; // Ratio in dB |
| 133 string16 ssid; // Network identifier | 133 string16 ssid; // Network identifier |
| 134 }; | 134 }; |
| 135 | 135 |
| 136 // This is to allow AccessPointData to be used in std::set. We order | 136 // This is to allow AccessPointData to be used in std::set. We order |
| 137 // lexicographically by MAC address. | 137 // lexicographically by MAC address. |
| 138 struct AccessPointDataLess : public std::less<AccessPointData> { | 138 struct AccessPointDataLess : public std::less<AccessPointData> { |
| 139 bool operator()(const AccessPointData& data1, | 139 bool operator()(const AccessPointData& data1, |
| 140 const AccessPointData& data2) const { | 140 const AccessPointData& data2) const { |
| 141 return data1.mac_address < data2.mac_address; | 141 return data1.mac_address < data2.mac_address; |
| 142 } | 142 } |
| 143 }; | 143 }; |
| 144 | 144 |
| 145 // All data for wifi. | 145 // All data for wifi. |
| 146 struct WifiData { | 146 struct WifiData { |
| 147 // Determines whether a new set of WiFi data differs significantly from this. | 147 // Determines whether a new set of WiFi data differs significantly from this. |
| 148 bool DiffersSignificantly(const WifiData &other) const { | 148 bool DiffersSignificantly(const WifiData& other) const { |
| 149 // At least 5 or 50% of access points added or removed is significant. | 149 // More than 4 or 50% of access points added or removed is significant. |
| 150 static const size_t kMinChangedAccessPoints = 5; | 150 static const size_t kMinChangedAccessPoints = 4; |
| 151 | 151 const size_t min_ap_count = |
| 152 std::min(access_point_data.size(), other.access_point_data.size()); |
| 153 const size_t max_ap_count = |
| 154 std::max(access_point_data.size(), other.access_point_data.size()); |
| 155 const size_t difference_threadhold = std::min(kMinChangedAccessPoints, |
| 156 min_ap_count / 2); |
| 157 if (max_ap_count > min_ap_count + difference_threadhold) |
| 158 return true; |
| 152 // Compute size of interesction of old and new sets. | 159 // Compute size of interesction of old and new sets. |
| 153 size_t num_common = 0; | 160 size_t num_common = 0; |
| 154 for (AccessPointDataSet::const_iterator iter = access_point_data.begin(); | 161 for (AccessPointDataSet::const_iterator iter = access_point_data.begin(); |
| 155 iter != access_point_data.end(); | 162 iter != access_point_data.end(); |
| 156 iter++) { | 163 iter++) { |
| 157 if (other.access_point_data.find(*iter) != | 164 if (other.access_point_data.find(*iter) != |
| 158 other.access_point_data.end()) { | 165 other.access_point_data.end()) { |
| 159 ++num_common; | 166 ++num_common; |
| 160 } | 167 } |
| 161 } | 168 } |
| 162 assert(num_common <= access_point_data.size()); | 169 DCHECK(num_common <= min_ap_count); |
| 163 assert(num_common <= other.access_point_data.size()); | |
| 164 | 170 |
| 165 // Test how many have changed. | 171 // Test how many have changed. |
| 166 size_t added_or_removed = std::max( | 172 return max_ap_count > num_common + difference_threadhold; |
| 167 other.access_point_data.size() - num_common, | |
| 168 access_point_data.size() - num_common); | |
| 169 return added_or_removed >= | |
| 170 std::min(kMinChangedAccessPoints, access_point_data.size() / 2); | |
| 171 } | 173 } |
| 172 | 174 |
| 173 // Store access points as a set, sorted by MAC address. This allows quick | 175 // Store access points as a set, sorted by MAC address. This allows quick |
| 174 // comparison of sets for detecting changes and for caching. | 176 // comparison of sets for detecting changes and for caching. |
| 175 typedef std::set<AccessPointData, AccessPointDataLess> AccessPointDataSet; | 177 typedef std::set<AccessPointData, AccessPointDataLess> AccessPointDataSet; |
| 176 AccessPointDataSet access_point_data; | 178 AccessPointDataSet access_point_data; |
| 177 }; | 179 }; |
| 178 | 180 |
| 179 template<typename DataType> | 181 template<typename DataType> |
| 180 class DeviceDataProvider; | 182 class DeviceDataProvider; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 // We protect against Register and Unregister being called asynchronously | 286 // We protect against Register and Unregister being called asynchronously |
| 285 // from different threads. This is the case when a device data provider is | 287 // from different threads. This is the case when a device data provider is |
| 286 // used by a NetworkLocationProvider object. Register is always called from | 288 // used by a NetworkLocationProvider object. Register is always called from |
| 287 // the JavaScript thread. Unregister is called when NetworkLocationProvider | 289 // the JavaScript thread. Unregister is called when NetworkLocationProvider |
| 288 // objects are destructed, which happens asynchronously once the | 290 // objects are destructed, which happens asynchronously once the |
| 289 // NetworkLocationProvider HTTP request has completed. | 291 // NetworkLocationProvider HTTP request has completed. |
| 290 AutoLock mutex(instance_mutex_); | 292 AutoLock mutex(instance_mutex_); |
| 291 if (!instance_) { | 293 if (!instance_) { |
| 292 instance_ = new DeviceDataProvider(); | 294 instance_ = new DeviceDataProvider(); |
| 293 } | 295 } |
| 294 assert(instance_); | 296 DCHECK(instance_); |
| 295 instance_->Ref(); | 297 instance_->Ref(); |
| 296 instance_->AddListener(listener); | 298 instance_->AddListener(listener); |
| 297 return instance_; | 299 return instance_; |
| 298 } | 300 } |
| 299 | 301 |
| 300 // Removes a listener. If this is the last listener, deletes the singleton | 302 // Removes a listener. If this is the last listener, deletes the singleton |
| 301 // instance. Return value indicates success. | 303 // instance. Return value indicates success. |
| 302 static bool Unregister(ListenerInterface* listener) { | 304 static bool Unregister(ListenerInterface* listener) { |
| 303 AutoLock mutex(instance_mutex_); | 305 AutoLock mutex(instance_mutex_); |
| 304 if (!instance_->RemoveListener(listener)) { | 306 if (!instance_->RemoveListener(listener)) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 315 // value indicates whether this is all the data the provider could ever | 317 // value indicates whether this is all the data the provider could ever |
| 316 // obtain. | 318 // obtain. |
| 317 bool GetData(DataType* data) { | 319 bool GetData(DataType* data) { |
| 318 return impl_->GetData(data); | 320 return impl_->GetData(data); |
| 319 } | 321 } |
| 320 | 322 |
| 321 private: | 323 private: |
| 322 // Private constructor and destructor, callers access singleton through | 324 // Private constructor and destructor, callers access singleton through |
| 323 // Register and Unregister. | 325 // Register and Unregister. |
| 324 DeviceDataProvider() : count_(0) { | 326 DeviceDataProvider() : count_(0) { |
| 325 assert(factory_function_); | 327 DCHECK(factory_function_); |
| 326 impl_.reset((*factory_function_)()); | 328 impl_.reset((*factory_function_)()); |
| 327 impl_->SetContainer(this); | 329 impl_->SetContainer(this); |
| 328 bool started = impl_->StartDataProvider(); | 330 bool started = impl_->StartDataProvider(); |
| 329 assert(started); | 331 DCHECK(started); |
| 330 } | 332 } |
| 331 virtual ~DeviceDataProvider() {} | 333 virtual ~DeviceDataProvider() {} |
| 332 | 334 |
| 333 void Ref() { | 335 void Ref() { |
| 334 ++count_; | 336 ++count_; |
| 335 } | 337 } |
| 336 // Returns true when the ref count transitions from 1 to 0. | 338 // Returns true when the ref count transitions from 1 to 0. |
| 337 bool Unref() { | 339 bool Unref() { |
| 338 --count_; | 340 --count_; |
| 339 return count_ == 0; | 341 return count_ == 0; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 | 376 |
| 375 // static | 377 // static |
| 376 template<typename DataType> | 378 template<typename DataType> |
| 377 typename DeviceDataProvider<DataType>::ImplFactoryFunction | 379 typename DeviceDataProvider<DataType>::ImplFactoryFunction |
| 378 DeviceDataProvider<DataType>::factory_function_ = DefaultFactoryFunction; | 380 DeviceDataProvider<DataType>::factory_function_ = DefaultFactoryFunction; |
| 379 | 381 |
| 380 typedef DeviceDataProvider<RadioData> RadioDataProvider; | 382 typedef DeviceDataProvider<RadioData> RadioDataProvider; |
| 381 typedef DeviceDataProvider<WifiData> WifiDataProvider; | 383 typedef DeviceDataProvider<WifiData> WifiDataProvider; |
| 382 | 384 |
| 383 #endif // CHROME_BROWSER_GEOLOCATION_DEVICE_DATA_PROVIDER_H_ | 385 #endif // CHROME_BROWSER_GEOLOCATION_DEVICE_DATA_PROVIDER_H_ |
| OLD | NEW |