| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 wifi data provider provides wifi data from the device that is used by a | 5 // A wifi data provider provides wifi data from the device that is used by a |
| 6 // NetworkLocationProvider to obtain a position fix. We use a singleton | 6 // NetworkLocationProvider to obtain a position fix. We use a singleton |
| 7 // instance of the wifi data provider, which is used by multiple | 7 // instance of the wifi data provider manager, which is used by multiple |
| 8 // NetworkLocationProvider objects. | 8 // NetworkLocationProvider objects. |
| 9 // | 9 // |
| 10 // This file provides WifiDataProvider, which provides static methods to | 10 // This file provides WifiDataProviderManager, which provides static methods to |
| 11 // access the singleton instance. The singleton instance uses a private | 11 // access the singleton instance. The singleton instance uses a private |
| 12 // implementation to abstract across platforms and also to allow mock providers | 12 // implementation of WifiDataProvider to abstract across platforms and also to |
| 13 // to be used for testing. | 13 // allow mock providers to be used for testing. |
| 14 // | |
| 15 // This file also provides WifiDataProviderImplBase, a base class which | |
| 16 // provides common functionality for the private implementations. | |
| 17 | 14 |
| 18 #ifndef CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_ | 15 #ifndef CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_MANAGER_H_ |
| 19 #define CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_ | 16 #define CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_MANAGER_H_ |
| 20 | 17 |
| 21 #include <set> | 18 #include <set> |
| 22 | 19 |
| 23 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
| 24 #include "base/bind.h" | 21 #include "base/bind.h" |
| 25 #include "base/callback.h" | 22 #include "base/callback.h" |
| 26 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
| 27 #include "base/message_loop/message_loop.h" | 24 #include "base/message_loop/message_loop.h" |
| 28 #include "base/strings/string16.h" | 25 #include "base/strings/string16.h" |
| 29 #include "base/strings/string_util.h" | 26 #include "base/strings/string_util.h" |
| 30 #include "content/browser/geolocation/wifi_data.h" | 27 #include "content/browser/geolocation/wifi_data.h" |
| 31 #include "content/common/content_export.h" | 28 #include "content/common/content_export.h" |
| 32 | 29 |
| 33 namespace content { | 30 namespace content { |
| 34 | 31 |
| 35 class WifiDataProvider; | 32 class WifiDataProvider; |
| 36 | 33 |
| 37 // See class WifiDataProvider for the public client API. | 34 // A manager for wifi data providers. |
| 38 // WifiDataProvider uses containment to hide platform-specific implementation | |
| 39 // details from common code. This class provides common functionality for these | |
| 40 // contained implementation classes. This is a modified pimpl pattern. | |
| 41 class CONTENT_EXPORT WifiDataProviderImplBase | |
| 42 : public base::RefCountedThreadSafe<WifiDataProviderImplBase> { | |
| 43 public: | |
| 44 WifiDataProviderImplBase(); | |
| 45 | |
| 46 // Tells the provider to start looking for data. Callbacks will start | |
| 47 // receiving notifications after this call. | |
| 48 virtual void StartDataProvider() = 0; | |
| 49 | |
| 50 // Tells the provider to stop looking for data. Callbacks will stop | |
| 51 // receiving notifications after this call. | |
| 52 virtual void StopDataProvider() = 0; | |
| 53 | |
| 54 // Provides whatever data the provider has, which may be nothing. Return | |
| 55 // value indicates whether this is all the data the provider could ever | |
| 56 // obtain. | |
| 57 virtual bool GetData(WifiData* data) = 0; | |
| 58 | |
| 59 // Sets the container of this class, which is of type WifiDataProvider. | |
| 60 // This is required to pass as a parameter when calling a callback. | |
| 61 void SetContainer(WifiDataProvider* container); | |
| 62 | |
| 63 typedef base::Callback<void(WifiDataProvider*)> WifiDataUpdateCallback; | |
| 64 | |
| 65 void AddCallback(WifiDataUpdateCallback* callback); | |
| 66 | |
| 67 bool RemoveCallback(WifiDataUpdateCallback* callback); | |
| 68 | |
| 69 bool has_callbacks() const; | |
| 70 | |
| 71 protected: | |
| 72 friend class base::RefCountedThreadSafe<WifiDataProviderImplBase>; | |
| 73 virtual ~WifiDataProviderImplBase(); | |
| 74 | |
| 75 typedef std::set<WifiDataUpdateCallback*> CallbackSet; | |
| 76 | |
| 77 // Runs all callbacks via a posted task, so we can unwind callstack here and | |
| 78 // avoid client reentrancy. | |
| 79 void RunCallbacks(); | |
| 80 | |
| 81 bool CalledOnClientThread() const; | |
| 82 | |
| 83 base::MessageLoop* client_loop() const; | |
| 84 | |
| 85 private: | |
| 86 void DoRunCallbacks(); | |
| 87 | |
| 88 WifiDataProvider* container_; | |
| 89 | |
| 90 // Reference to the client's message loop. All callbacks should happen in this | |
| 91 // context. | |
| 92 base::MessageLoop* client_loop_; | |
| 93 | |
| 94 CallbackSet callbacks_; | |
| 95 | |
| 96 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderImplBase); | |
| 97 }; | |
| 98 | |
| 99 // A wifi data provider | |
| 100 // | 35 // |
| 101 // We use a singleton instance of this class which is shared by multiple network | 36 // We use a singleton instance of this class which is shared by multiple network |
| 102 // location providers. These location providers access the instance through the | 37 // location providers. These location providers access the instance through the |
| 103 // Register and Unregister methods. | 38 // Register and Unregister methods. |
| 104 class CONTENT_EXPORT WifiDataProvider { | 39 class CONTENT_EXPORT WifiDataProviderManager { |
| 105 public: | 40 public: |
| 106 // Sets the factory function which will be used by Register to create the | 41 // Sets the factory function which will be used by Register to create the |
| 107 // implementation used by the singleton instance. This factory approach is | 42 // implementation used by the singleton instance. This factory approach is |
| 108 // used both to abstract accross platform-specific implementations and to | 43 // used both to abstract accross platform-specific implementations and to |
| 109 // inject mock implementations for testing. | 44 // inject mock implementations for testing. |
| 110 typedef WifiDataProviderImplBase* (*ImplFactoryFunction)(void); | 45 typedef WifiDataProvider* (*ImplFactoryFunction)(void); |
| 111 static void SetFactory(ImplFactoryFunction factory_function_in); | 46 static void SetFactory(ImplFactoryFunction factory_function_in); |
| 112 | 47 |
| 113 // Resets the factory function to the default. | 48 // Resets the factory function to the default. |
| 114 static void ResetFactory(); | 49 static void ResetFactory(); |
| 115 | 50 |
| 116 typedef base::Callback<void(WifiDataProvider*)> WifiDataUpdateCallback; | 51 typedef base::Callback<void(WifiDataProviderManager*)> WifiDataUpdateCallback; |
| 117 | 52 |
| 118 // Registers a callback, which will be run whenever new data is available. | 53 // Registers a callback, which will be run whenever new data is available. |
| 119 // Instantiates the singleton if necessary, and always returns it. | 54 // Instantiates the singleton if necessary, and always returns it. |
| 120 static WifiDataProvider* Register(WifiDataUpdateCallback* callback); | 55 static WifiDataProviderManager* Register(WifiDataUpdateCallback* callback); |
| 121 | 56 |
| 122 // Removes a callback. If this is the last callback, deletes the singleton | 57 // Removes a callback. If this is the last callback, deletes the singleton |
| 123 // instance. Return value indicates success. | 58 // instance. Return value indicates success. |
| 124 static bool Unregister(WifiDataUpdateCallback* callback); | 59 static bool Unregister(WifiDataUpdateCallback* callback); |
| 125 | 60 |
| 126 // Provides whatever data the provider has, which may be nothing. Return | 61 // Provides whatever data the provider has, which may be nothing. Return |
| 127 // value indicates whether this is all the data the provider could ever | 62 // value indicates whether this is all the data the provider could ever |
| 128 // obtain. | 63 // obtain. |
| 129 bool GetData(WifiData* data); | 64 bool GetData(WifiData* data); |
| 130 | 65 |
| 131 private: | 66 private: |
| 132 // Private constructor and destructor, callers access singleton through | 67 // Private constructor and destructor, callers access singleton through |
| 133 // Register and Unregister. | 68 // Register and Unregister. |
| 134 WifiDataProvider(); | 69 WifiDataProviderManager(); |
| 135 virtual ~WifiDataProvider(); | 70 ~WifiDataProviderManager(); |
| 136 | 71 |
| 137 void AddCallback(WifiDataUpdateCallback* callback); | 72 void AddCallback(WifiDataUpdateCallback* callback); |
| 138 bool RemoveCallback(WifiDataUpdateCallback* callback); | 73 bool RemoveCallback(WifiDataUpdateCallback* callback); |
| 139 bool has_callbacks() const; | 74 bool has_callbacks() const; |
| 140 | 75 |
| 141 void StartDataProvider(); | 76 void StartDataProvider(); |
| 142 void StopDataProvider(); | 77 void StopDataProvider(); |
| 143 | 78 |
| 144 static WifiDataProviderImplBase* DefaultFactoryFunction(); | 79 static WifiDataProvider* DefaultFactoryFunction(); |
| 145 | 80 |
| 146 // The singleton-like instance of this class. (Not 'true' singleton, as it | 81 // The singleton-like instance of this class. (Not 'true' singleton, as it |
| 147 // may go through multiple create/destroy/create cycles per process instance, | 82 // may go through multiple create/destroy/create cycles per process instance, |
| 148 // e.g. when under test). | 83 // e.g. when under test). |
| 149 static WifiDataProvider* instance_; | 84 static WifiDataProviderManager* instance_; |
| 150 | 85 |
| 151 // The factory function used to create the singleton instance. | 86 // The factory function used to create the singleton instance. |
| 152 static ImplFactoryFunction factory_function_; | 87 static ImplFactoryFunction factory_function_; |
| 153 | 88 |
| 154 // The internal implementation. | 89 // The internal implementation. |
| 155 scoped_refptr<WifiDataProviderImplBase> impl_; | 90 scoped_refptr<WifiDataProvider> impl_; |
| 156 | 91 |
| 157 DISALLOW_COPY_AND_ASSIGN(WifiDataProvider); | 92 DISALLOW_COPY_AND_ASSIGN(WifiDataProviderManager); |
| 158 }; | 93 }; |
| 159 | 94 |
| 160 } // namespace content | 95 } // namespace content |
| 161 | 96 |
| 162 #endif // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_ | 97 #endif // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_MANAGER_H_ |
| OLD | NEW |