diff options
Diffstat (limited to 'chrome/browser/geolocation/device_data_provider.h')
-rw-r--r-- | chrome/browser/geolocation/device_data_provider.h | 130 |
1 files changed, 61 insertions, 69 deletions
diff --git a/chrome/browser/geolocation/device_data_provider.h b/chrome/browser/geolocation/device_data_provider.h index 6d5740e..cef140c 100644 --- a/chrome/browser/geolocation/device_data_provider.h +++ b/chrome/browser/geolocation/device_data_provider.h @@ -1,28 +1,7 @@ -// Copyright 2008, Google Inc. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// 2. Redistributions in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// 3. Neither the name of Google Inc. nor the names of its contributors may be -// used to endorse or promote products derived from this software without -// specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED -// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO -// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// +// 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. + // A device data provider provides data from the device that is used by a // NetworkLocationProvider to obtain a position fix. This data may be either // cell radio data or wifi data. For a given type of data, we use a singleton @@ -40,18 +19,20 @@ // This file also declares the data structures used to represent cell radio data // and wifi data. -#ifndef GEARS_GEOLOCATION_DEVICE_DATA_PROVIDER_H__ -#define GEARS_GEOLOCATION_DEVICE_DATA_PROVIDER_H__ +#ifndef CHROME_BROWSER_GEOLOCATION_DEVICE_DATA_PROVIDER_H_ +#define CHROME_BROWSER_GEOLOCATION_DEVICE_DATA_PROVIDER_H_ #include <algorithm> +#include <functional> #include <set> +#include <string> #include <vector> -#include "gears/base/common/basictypes.h" // For int64 -#include "gears/base/common/common.h" -#include "gears/base/common/mutex.h" -#include "gears/base/common/scoped_refptr.h" // For RefCount -#include "gears/base/common/string16.h" -#include "third_party/scoped_ptr/scoped_ptr.h" + +#include "base/basictypes.h" +#include "base/lock.h" +#include "base/string16.h" +#include "base/string_util.h" +#include "base/scoped_ptr.h" // The following data structures are used to store cell radio data and wifi // data. See the Geolocation API design document at @@ -128,12 +109,12 @@ struct RadioData { return !Matches(other); } - std::string16 device_id; + string16 device_id; std::vector<CellData> cell_data; int home_mobile_network_code; // For the device's home network. int home_mobile_country_code; // For the device's home network. RadioType radio_type; // Mobile radio type. - std::string16 carrier; // Carrier name. + string16 carrier; // Carrier name. }; // Wifi data relating to a single access point. @@ -144,19 +125,19 @@ struct AccessPointData { channel(kint32min), signal_to_noise(kint32min) {} - std::string16 mac_address; + string16 mac_address; int radio_signal_strength; // Measured in dBm int age; // Milliseconds since this access point was detected int channel; int signal_to_noise; // Ratio in dB - std::string16 ssid; // Network identifier + string16 ssid; // Network identifier }; // This is to allow AccessPointData to be used in std::set. We order // lexicographically by MAC address. struct AccessPointDataLess : public std::less<AccessPointData> { - bool operator()(const AccessPointData &data1, - const AccessPointData &data2) const { + bool operator()(const AccessPointData& data1, + const AccessPointData& data2) const { return data1.mac_address < data2.mac_address; } }; @@ -207,23 +188,25 @@ class DeviceDataProviderImplBase { DeviceDataProviderImplBase() : container_(NULL) {} virtual ~DeviceDataProviderImplBase() {} - virtual bool GetData(DataType *data) = 0; + virtual bool StartDataProvider() = 0; + + virtual bool GetData(DataType* data) = 0; // Sets the container of this class, which is of type DeviceDataProvider. // This is required to pass as a parameter when making the callback to // listeners. - void SetContainer(DeviceDataProvider<DataType> *container) { + void SetContainer(DeviceDataProvider<DataType>* container) { container_ = container; } typedef typename DeviceDataProvider<DataType>::ListenerInterface ListenerInterface; - void AddListener(ListenerInterface *listener) { - MutexLock mutex(&listeners_mutex_); + void AddListener(ListenerInterface* listener) { + AutoLock mutex(listeners_mutex_); listeners_.insert(listener); } - bool RemoveListener(ListenerInterface *listener) { - MutexLock mutex(&listeners_mutex_); + bool RemoveListener(ListenerInterface* listener) { + AutoLock mutex(listeners_mutex_); typename ListenersSet::iterator iter = find(listeners_.begin(), listeners_.end(), listener); @@ -238,7 +221,7 @@ class DeviceDataProviderImplBase { // Calls DeviceDataUpdateAvailable() on all registered listeners. typedef std::set<ListenerInterface*> ListenersSet; void NotifyListeners() { - MutexLock lock(&listeners_mutex_); + AutoLock lock(listeners_mutex_); for (typename ListenersSet::const_iterator iter = listeners_.begin(); iter != listeners_.end(); ++iter) { @@ -247,13 +230,16 @@ class DeviceDataProviderImplBase { } private: - DeviceDataProvider<DataType> *container_; + DeviceDataProvider<DataType>* container_; // The listeners to this class and their mutex. + // TODO(joth): Once we've established the client is always single threaded, + // remove mutex and instead capture client's MessageLoop to stage the + // NotifyListeners callback via. ListenersSet listeners_; - Mutex listeners_mutex_; + Lock listeners_mutex_; - DISALLOW_EVIL_CONSTRUCTORS(DeviceDataProviderImplBase); + DISALLOW_COPY_AND_ASSIGN(DeviceDataProviderImplBase); }; typedef DeviceDataProviderImplBase<RadioData> RadioDataProviderImplBase; @@ -270,8 +256,10 @@ class DeviceDataProvider { // Interface to be implemented by listeners to a device data provider. class ListenerInterface { public: + // NOTE this may be called back in the context of the implementation private + // worker thread. (TODO Is there a naming convention to use for this?) virtual void DeviceDataUpdateAvailable( - DeviceDataProvider<DataType> *provider) = 0; + DeviceDataProvider<DataType>* provider) = 0; virtual ~ListenerInterface() {} }; @@ -279,7 +267,7 @@ class DeviceDataProvider { // implementation used by the singleton instance. This factory approach is // used to abastract accross both platform-specific implementation and to // inject mock implementations for testing. - typedef DeviceDataProviderImplBase<DataType> *(*ImplFactoryFunction)(void); + typedef DeviceDataProviderImplBase<DataType>* (*ImplFactoryFunction)(void); static void SetFactory(ImplFactoryFunction factory_function_in) { factory_function_ = factory_function_in; } @@ -290,14 +278,16 @@ class DeviceDataProvider { // Adds a listener, which will be called back with DeviceDataUpdateAvailable // whenever new data is available. Returns the singleton instance. - static DeviceDataProvider *Register(ListenerInterface *listener) { + static DeviceDataProvider* Register(ListenerInterface* listener) { + // TODO(joth): The following comment applied when this was used in Gears; + // revisit if this is still needed once usage is established in Chromium. // We protect against Register and Unregister being called asynchronously // from different threads. This is the case when a device data provider is // used by a NetworkLocationProvider object. Register is always called from // the JavaScript thread. Unregister is called when NetworkLocationProvider // objects are destructed, which happens asynchronously once the // NetworkLocationProvider HTTP request has completed. - MutexLock mutex(&instance_mutex_); + AutoLock mutex(instance_mutex_); if (!instance_) { instance_ = new DeviceDataProvider(); } @@ -309,8 +299,8 @@ class DeviceDataProvider { // Removes a listener. If this is the last listener, deletes the singleton // instance. Return value indicates success. - static bool Unregister(ListenerInterface *listener) { - MutexLock mutex(&instance_mutex_); + static bool Unregister(ListenerInterface* listener) { + AutoLock mutex(instance_mutex_); if (!instance_->RemoveListener(listener)) { return false; } @@ -324,41 +314,44 @@ class DeviceDataProvider { // Provides whatever data the provider has, which may be nothing. Return // value indicates whether this is all the data the provider could ever // obtain. - bool GetData(DataType *data) { + bool GetData(DataType* data) { return impl_->GetData(data); } private: // Private constructor and destructor, callers access singleton through // Register and Unregister. - DeviceDataProvider() { + DeviceDataProvider() : count_(0) { assert(factory_function_); impl_.reset((*factory_function_)()); impl_->SetContainer(this); + bool started = impl_->StartDataProvider(); + assert(started); } virtual ~DeviceDataProvider() {} void Ref() { - count_.Ref(); + ++count_; } // Returns true when the ref count transitions from 1 to 0. bool Unref() { - return count_.Unref(); + --count_; + return count_ == 0; } - void AddListener(ListenerInterface *listener) { + void AddListener(ListenerInterface* listener) { impl_->AddListener(listener); } - bool RemoveListener(ListenerInterface *listener) { + bool RemoveListener(ListenerInterface* listener) { return impl_->RemoveListener(listener); } - static DeviceDataProviderImplBase<DataType> *DefaultFactoryFunction(); + static DeviceDataProviderImplBase<DataType>* DefaultFactoryFunction(); // The singleton instance of this class and its mutex. - static DeviceDataProvider *instance_; - static Mutex instance_mutex_; + static DeviceDataProvider* instance_; + static Lock instance_mutex_; // The factory function used to create the singleton instance. static ImplFactoryFunction factory_function_; @@ -366,19 +359,18 @@ class DeviceDataProvider { // The internal implementation. scoped_ptr<DeviceDataProviderImplBase<DataType> > impl_; - RefCount count_; + int count_; DISALLOW_EVIL_CONSTRUCTORS(DeviceDataProvider); }; // static template<typename DataType> -Mutex DeviceDataProvider<DataType>::instance_mutex_; +Lock DeviceDataProvider<DataType>::instance_mutex_; // static template<typename DataType> -DeviceDataProvider<DataType> *DeviceDataProvider<DataType>::instance_ = - NULL; +DeviceDataProvider<DataType>* DeviceDataProvider<DataType>::instance_ = NULL; // static template<typename DataType> @@ -388,4 +380,4 @@ typename DeviceDataProvider<DataType>::ImplFactoryFunction typedef DeviceDataProvider<RadioData> RadioDataProvider; typedef DeviceDataProvider<WifiData> WifiDataProvider; -#endif // GEARS_GEOLOCATION_DEVICE_DATA_PROVIDER_H__ +#endif // CHROME_BROWSER_GEOLOCATION_DEVICE_DATA_PROVIDER_H_ |