Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Unified Diff: content/browser/geolocation/wifi_data_provider.h

Issue 10535157: [WIP] attempt to allow chrome OS to inject its wifi data provider (Closed) Base URL: http://git.chromium.org/chromium/src.git@remove_radio
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/geolocation/wifi_data_provider.h
diff --git a/content/browser/geolocation/device_data_provider.h b/content/browser/geolocation/wifi_data_provider.h
similarity index 60%
rename from content/browser/geolocation/device_data_provider.h
rename to content/browser/geolocation/wifi_data_provider.h
index 5c89d6d021f22319aa172356f91547ea1561f708..d8c749dd3e31cea088a53aa20226e05442ec9541 100644
--- a/content/browser/geolocation/device_data_provider.h
+++ b/content/browser/geolocation/wifi_data_provider.h
@@ -8,12 +8,12 @@
// instance of the device data provider, which is used by multiple
// NetworkLocationProvider objects.
//
-// This file providers DeviceDataProvider, which provides static methods to
+// This file providers WifiDataProvider, which provides static methods to
// access the singleton instance. The singleton instance uses a private
// implementation to abstract across platforms and also to allow mock providers
// to be used for testing.
//
-// This file also provides DeviceDataProviderImplBase, a base class which
+// This file also provides WifiDataProviderImplBase, a base class which
// provides commom functionality for the private implementations.
//
// This file also declares the data structures used to represent cell radio data
@@ -70,45 +70,101 @@ struct CONTENT_EXPORT WifiData {
AccessPointDataSet access_point_data;
};
-template<typename DataType>
-class DeviceDataProvider;
+class WifiDataProviderImplBase;
-// This class just exists to work-around MSVC2005 not being able to have a
-// template class implement RefCountedThreadSafe
-class CONTENT_EXPORT DeviceDataProviderImplBaseHack
- : public base::RefCountedThreadSafe<DeviceDataProviderImplBaseHack> {
- protected:
- friend class base::RefCountedThreadSafe<DeviceDataProviderImplBaseHack>;
- virtual ~DeviceDataProviderImplBaseHack() {}
+// A device data provider
+//
+// We use a singleton instance of this class which is shared by multiple network
+// location providers. These location providers access the instance through the
+// Register and Unregister methods.
+class WifiDataProvider : public base::NonThreadSafe {
+ public:
+ // Interface to be implemented by listeners to a device data provider.
+ class ListenerInterface {
+ public:
+ // Will be called in the context of the thread that called Register().
+ virtual void DeviceDataUpdateAvailable(WifiDataProvider* provider) = 0;
+ virtual ~ListenerInterface() {}
+ };
+
+ // Sets the factory function which will be used by Register to create the
+ // 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 WifiDataProviderImplBase* (*ImplFactoryFunction)(void);
+ static void SetFactory(ImplFactoryFunction factory_function_in);
+ static void ResetFactory();
+
+ // Adds a listener, which will be called back with DeviceDataUpdateAvailable
+ // whenever new data is available. Returns the singleton instance.
+ static WifiDataProvider* Register(ListenerInterface* listener);
+
+ // Removes a listener. If this is the last listener, deletes the singleton
+ // instance. Return value indicates success.
+ static bool Unregister(ListenerInterface* listener);
+
+ // 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(WifiData* data);
+
+ private:
+ // Private constructor and destructor, callers access singleton through
+ // Register and Unregister.
+ WifiDataProvider();
+ virtual ~WifiDataProvider();
+
+ void AddListener(ListenerInterface* listener);
+ bool RemoveListener(ListenerInterface* listener);
+
+ bool has_listeners() const;
+
+ bool StartDataProvider();
+ void StopDataProvider();
+
+ CONTENT_EXPORT static WifiDataProviderImplBase* DefaultFactoryFunction();
+
+ // The singleton-like instance of this class. (Not 'true' singleton, as it
+ // may go through multiple create/destroy/create cycles per process instance,
+ // e.g. when under test).
+ CONTENT_EXPORT static WifiDataProvider* instance_;
+
+ // The factory function used to create the singleton instance.
+ CONTENT_EXPORT static ImplFactoryFunction factory_function_;
+
+ // The internal implementation.
+ scoped_refptr<WifiDataProviderImplBase> impl_;
+
+ DISALLOW_COPY_AND_ASSIGN(WifiDataProvider);
};
-// See class DeviceDataProvider for the public client API.
-// DeviceDataProvider uses containment to hide platform-specific implementation
+
+// See class WifiDataProvider for the public client API.
+// WifiDataProvider uses containment to hide platform-specific implementation
// details from common code. This class provides common functionality for these
// contained implementation classes. This is a modified pimpl pattern: this
// class needs to be in the public header due to use of templating.
-template<typename DataType>
-class DeviceDataProviderImplBase : public DeviceDataProviderImplBaseHack {
+class WifiDataProviderImplBase
+ : public base::RefCountedThreadSafe<WifiDataProviderImplBase> {
public:
- DeviceDataProviderImplBase()
+ WifiDataProviderImplBase()
: container_(NULL), client_loop_(MessageLoop::current()) {
DCHECK(client_loop_);
}
virtual bool StartDataProvider() = 0;
virtual void StopDataProvider() = 0;
- virtual bool GetData(DataType* data) = 0;
+ virtual bool GetData(WifiData* data) = 0;
- // Sets the container of this class, which is of type DeviceDataProvider.
+ // Sets the container of this class, which is of type WifiDataProvider.
// This is required to pass as a parameter when making the callback to
// listeners.
- void SetContainer(DeviceDataProvider<DataType>* container) {
+ void SetContainer(WifiDataProvider* container) {
DCHECK(CalledOnClientThread());
container_ = container;
}
- typedef typename DeviceDataProvider<DataType>::ListenerInterface
- ListenerInterface;
+ typedef WifiDataProvider::ListenerInterface ListenerInterface;
void AddListener(ListenerInterface* listener) {
DCHECK(CalledOnClientThread());
listeners_.insert(listener);
@@ -124,7 +180,8 @@ class DeviceDataProviderImplBase : public DeviceDataProviderImplBaseHack {
}
protected:
- virtual ~DeviceDataProviderImplBase() {
+ friend class base::RefCountedThreadSafe<WifiDataProviderImplBase>;
+ virtual ~WifiDataProviderImplBase() {
DCHECK(CalledOnClientThread());
}
@@ -134,7 +191,7 @@ class DeviceDataProviderImplBase : public DeviceDataProviderImplBaseHack {
// Always make the notify callback via a posted task, so we can unwind
// callstack here and make callback without causing client re-entrancy.
client_loop_->PostTask(FROM_HERE, base::Bind(
- &DeviceDataProviderImplBase<DataType>::NotifyListenersInClientLoop,
+ &WifiDataProviderImplBase::NotifyListenersInClientLoop,
this));
}
@@ -151,7 +208,7 @@ class DeviceDataProviderImplBase : public DeviceDataProviderImplBaseHack {
DCHECK(CalledOnClientThread());
// It's possible that all the listeners (and the container) went away
// whilst this task was pending. This is fine; the loop will be a no-op.
- typename ListenersSet::const_iterator iter = listeners_.begin();
+ ListenersSet::const_iterator iter = listeners_.begin();
while (iter != listeners_.end()) {
ListenerInterface* listener = *iter;
++iter; // Advance iter before callback, in case listener unregisters.
@@ -159,7 +216,7 @@ class DeviceDataProviderImplBase : public DeviceDataProviderImplBaseHack {
}
}
- DeviceDataProvider<DataType>* container_;
+ WifiDataProvider* container_;
// Reference to the client's message loop, all callbacks and access to
// the listeners_ member should happen in this context.
@@ -167,140 +224,7 @@ class DeviceDataProviderImplBase : public DeviceDataProviderImplBaseHack {
ListenersSet listeners_;
- DISALLOW_COPY_AND_ASSIGN(DeviceDataProviderImplBase);
-};
-
-typedef DeviceDataProviderImplBase<WifiData> WifiDataProviderImplBase;
-
-// A device data provider
-//
-// We use a singleton instance of this class which is shared by multiple network
-// location providers. These location providers access the instance through the
-// Register and Unregister methods.
-template<typename DataType>
-class DeviceDataProvider : public base::NonThreadSafe {
- public:
- // Interface to be implemented by listeners to a device data provider.
- class ListenerInterface {
- public:
- // Will be called in the context of the thread that called Register().
- virtual void DeviceDataUpdateAvailable(
- DeviceDataProvider<DataType>* provider) = 0;
- virtual ~ListenerInterface() {}
- };
-
- // Sets the factory function which will be used by Register to create the
- // 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);
- static void SetFactory(ImplFactoryFunction factory_function_in) {
- factory_function_ = factory_function_in;
- }
-
- static void ResetFactory() {
- factory_function_ = DefaultFactoryFunction;
- }
-
- // Adds a listener, which will be called back with DeviceDataUpdateAvailable
- // whenever new data is available. Returns the singleton instance.
- static DeviceDataProvider* Register(ListenerInterface* listener) {
- bool need_to_start_thread = false;
- if (!instance_) {
- instance_ = new DeviceDataProvider();
- 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_;
- }
-
- // Removes a listener. If this is the last listener, deletes the singleton
- // instance. Return value indicates success.
- static bool 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;
- }
-
- // 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) {
- DCHECK(this->CalledOnValidThread());
- return impl_->GetData(data);
- }
-
- private:
- // Private constructor and destructor, callers access singleton through
- // Register and Unregister.
- DeviceDataProvider() {
- DCHECK(factory_function_);
- impl_ = (*factory_function_)();
- DCHECK(impl_);
- impl_->SetContainer(this);
- }
- virtual ~DeviceDataProvider() {
- DCHECK(impl_);
- impl_->SetContainer(NULL);
- }
-
- void AddListener(ListenerInterface* listener) {
- impl_->AddListener(listener);
- }
-
- bool RemoveListener(ListenerInterface* listener) {
- return impl_->RemoveListener(listener);
- }
-
- bool has_listeners() const {
- return impl_->has_listeners();
- }
-
- bool StartDataProvider() {
- return impl_->StartDataProvider();
- }
-
- void StopDataProvider() {
- impl_->StopDataProvider();
- }
-
- CONTENT_EXPORT static DeviceDataProviderImplBase<DataType>*
- DefaultFactoryFunction();
-
- // The singleton-like instance of this class. (Not 'true' singleton, as it
- // may go through multiple create/destroy/create cycles per process instance,
- // e.g. when under test).
- CONTENT_EXPORT static DeviceDataProvider* instance_;
-
- // The factory function used to create the singleton instance.
- CONTENT_EXPORT static ImplFactoryFunction factory_function_;
-
- // The internal implementation.
- scoped_refptr<DeviceDataProviderImplBase<DataType> > impl_;
-
- DISALLOW_COPY_AND_ASSIGN(DeviceDataProvider);
+ DISALLOW_COPY_AND_ASSIGN(WifiDataProviderImplBase);
};
-typedef DeviceDataProvider<WifiData> WifiDataProvider;
-
#endif // CONTENT_BROWSER_GEOLOCATION_DEVICE_DATA_PROVIDER_H_
« no previous file with comments | « content/browser/geolocation/network_location_request.h ('k') | content/browser/geolocation/wifi_data_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698