1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/geolocation/wifi_data_provider_manager.h"
7 #include "content/browser/geolocation/wifi_data_provider.h"
12 WifiDataProviderManager
* WifiDataProviderManager::instance_
= NULL
;
15 WifiDataProviderManager::ImplFactoryFunction
16 WifiDataProviderManager::factory_function_
= DefaultFactoryFunction
;
19 void WifiDataProviderManager::SetFactoryForTesting(
20 ImplFactoryFunction factory_function_in
) {
21 factory_function_
= factory_function_in
;
25 void WifiDataProviderManager::ResetFactoryForTesting() {
26 factory_function_
= DefaultFactoryFunction
;
30 WifiDataProviderManager
* WifiDataProviderManager::Register(
31 WifiDataUpdateCallback
* callback
) {
32 bool need_to_start_data_provider
= false;
34 instance_
= new WifiDataProviderManager();
35 need_to_start_data_provider
= true;
38 instance_
->AddCallback(callback
);
39 // Start the provider after adding the callback, to avoid any race in
41 if (need_to_start_data_provider
)
42 instance_
->StartDataProvider();
47 bool WifiDataProviderManager::Unregister(WifiDataUpdateCallback
* callback
) {
49 DCHECK(instance_
->has_callbacks());
50 if (!instance_
->RemoveCallback(callback
)) {
53 if (!instance_
->has_callbacks()) {
54 // Must stop the data provider (and any implementation threads) before
55 // destroying to avoid any race conditions in access to the provider in
56 // the destructor chain.
57 instance_
->StopDataProvider();
64 WifiDataProviderManager::WifiDataProviderManager() {
65 DCHECK(factory_function_
);
66 impl_
= (*factory_function_
)();
70 WifiDataProviderManager::~WifiDataProviderManager() {
74 bool WifiDataProviderManager::GetData(WifiData
* data
) {
75 return impl_
->GetData(data
);
78 void WifiDataProviderManager::AddCallback(WifiDataUpdateCallback
* callback
) {
79 impl_
->AddCallback(callback
);
82 bool WifiDataProviderManager::RemoveCallback(WifiDataUpdateCallback
* callback
) {
83 return impl_
->RemoveCallback(callback
);
86 bool WifiDataProviderManager::has_callbacks() const {
87 return impl_
->has_callbacks();
90 void WifiDataProviderManager::StartDataProvider() {
91 impl_
->StartDataProvider();
94 void WifiDataProviderManager::StopDataProvider() {
95 impl_
->StopDataProvider();
98 } // namespace content