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, 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 WifiDataProvider, which provides static methods to |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 // We use a singleton instance of this class which is shared by multiple network | 101 // 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 | 102 // location providers. These location providers access the instance through the |
103 // Register and Unregister methods. | 103 // Register and Unregister methods. |
104 class CONTENT_EXPORT WifiDataProvider { | 104 class CONTENT_EXPORT WifiDataProvider { |
105 public: | 105 public: |
106 // Sets the factory function which will be used by Register to create the | 106 // Sets the factory function which will be used by Register to create the |
107 // implementation used by the singleton instance. This factory approach is | 107 // implementation used by the singleton instance. This factory approach is |
108 // used both to abstract accross platform-specific implementations and to | 108 // used both to abstract accross platform-specific implementations and to |
109 // inject mock implementations for testing. | 109 // inject mock implementations for testing. |
110 typedef WifiDataProviderImplBase* (*ImplFactoryFunction)(void); | 110 typedef WifiDataProviderImplBase* (*ImplFactoryFunction)(void); |
111 static void SetFactory(ImplFactoryFunction factory_function_in) { | 111 static void SetFactory(ImplFactoryFunction factory_function_in); |
112 factory_function_ = factory_function_in; | |
113 } | |
114 | 112 |
115 static void ResetFactory() { | 113 // Resets the factory function to the default. |
116 factory_function_ = DefaultFactoryFunction; | 114 static void ResetFactory(); |
117 } | |
118 | 115 |
119 typedef base::Callback<void(WifiDataProvider*)> WifiDataUpdateCallback; | 116 typedef base::Callback<void(WifiDataProvider*)> WifiDataUpdateCallback; |
120 | 117 |
121 // Registers a callback, which will be run whenever new data is available. | 118 // Registers a callback, which will be run whenever new data is available. |
122 // Instantiates the singleton if necessary, and always returns it. | 119 // Instantiates the singleton if necessary, and always returns it. |
123 static WifiDataProvider* Register(WifiDataUpdateCallback* callback); | 120 static WifiDataProvider* Register(WifiDataUpdateCallback* callback); |
124 | 121 |
125 // Removes a callback. If this is the last callback, deletes the singleton | 122 // Removes a callback. If this is the last callback, deletes the singleton |
126 // instance. Return value indicates success. | 123 // instance. Return value indicates success. |
127 static bool Unregister(WifiDataUpdateCallback* callback) { | 124 static bool Unregister(WifiDataUpdateCallback* callback); |
128 DCHECK(instance_); | |
129 DCHECK(instance_->has_callbacks()); | |
130 if (!instance_->RemoveCallback(callback)) { | |
131 return false; | |
132 } | |
133 if (!instance_->has_callbacks()) { | |
134 // Must stop the data provider (and any implementation threads) before | |
135 // destroying to avoid any race conditions in access to the provider in | |
136 // the destructor chain. | |
137 instance_->StopDataProvider(); | |
138 delete instance_; | |
139 instance_ = NULL; | |
140 } | |
141 return true; | |
142 } | |
143 | 125 |
144 // Provides whatever data the provider has, which may be nothing. Return | 126 // Provides whatever data the provider has, which may be nothing. Return |
145 // value indicates whether this is all the data the provider could ever | 127 // value indicates whether this is all the data the provider could ever |
146 // obtain. | 128 // obtain. |
147 bool GetData(WifiData* data) { | 129 bool GetData(WifiData* data); |
148 return impl_->GetData(data); | |
149 } | |
150 | 130 |
151 private: | 131 private: |
152 // Private constructor and destructor, callers access singleton through | 132 // Private constructor and destructor, callers access singleton through |
153 // Register and Unregister. | 133 // Register and Unregister. |
154 WifiDataProvider(); | 134 WifiDataProvider(); |
155 virtual ~WifiDataProvider(); | 135 virtual ~WifiDataProvider(); |
156 | 136 |
157 void AddCallback(WifiDataUpdateCallback* callback) { | 137 void AddCallback(WifiDataUpdateCallback* callback); |
158 impl_->AddCallback(callback); | 138 bool RemoveCallback(WifiDataUpdateCallback* callback); |
159 } | 139 bool has_callbacks() const; |
160 | 140 |
161 bool RemoveCallback(WifiDataUpdateCallback* callback) { | 141 void StartDataProvider(); |
162 return impl_->RemoveCallback(callback); | 142 void StopDataProvider(); |
163 } | |
164 | |
165 bool has_callbacks() const { | |
166 return impl_->has_callbacks(); | |
167 } | |
168 | |
169 void StartDataProvider() { | |
170 impl_->StartDataProvider(); | |
171 } | |
172 | |
173 void StopDataProvider() { | |
174 impl_->StopDataProvider(); | |
175 } | |
176 | 143 |
177 static WifiDataProviderImplBase* DefaultFactoryFunction(); | 144 static WifiDataProviderImplBase* DefaultFactoryFunction(); |
178 | 145 |
179 // The singleton-like instance of this class. (Not 'true' singleton, as it | 146 // The singleton-like instance of this class. (Not 'true' singleton, as it |
180 // may go through multiple create/destroy/create cycles per process instance, | 147 // may go through multiple create/destroy/create cycles per process instance, |
181 // e.g. when under test). | 148 // e.g. when under test). |
182 static WifiDataProvider* instance_; | 149 static WifiDataProvider* instance_; |
183 | 150 |
184 // The factory function used to create the singleton instance. | 151 // The factory function used to create the singleton instance. |
185 static ImplFactoryFunction factory_function_; | 152 static ImplFactoryFunction factory_function_; |
186 | 153 |
187 // The internal implementation. | 154 // The internal implementation. |
188 scoped_refptr<WifiDataProviderImplBase> impl_; | 155 scoped_refptr<WifiDataProviderImplBase> impl_; |
189 | 156 |
190 DISALLOW_COPY_AND_ASSIGN(WifiDataProvider); | 157 DISALLOW_COPY_AND_ASSIGN(WifiDataProvider); |
191 }; | 158 }; |
192 | 159 |
193 } // namespace content | 160 } // namespace content |
194 | 161 |
195 #endif // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_ | 162 #endif // CONTENT_BROWSER_GEOLOCATION_WIFI_DATA_PROVIDER_H_ |
OLD | NEW |