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. 4 5 #include "content/browser/geolocation/wifi_data_provider_manager.h" 6 7 #include "content/browser/geolocation/wifi_data_provider.h" 8 9 namespace content { 10 11 // static 12 WifiDataProviderManager* WifiDataProviderManager::instance_ = NULL; 13 14 // static 15 WifiDataProviderManager::ImplFactoryFunction 16 WifiDataProviderManager::factory_function_ = DefaultFactoryFunction; 17 18 // static 19 void WifiDataProviderManager::SetFactoryForTesting( 20 ImplFactoryFunction factory_function_in) { 21 factory_function_ = factory_function_in; 22 } 23 24 // static 25 void WifiDataProviderManager::ResetFactoryForTesting() { 26 factory_function_ = DefaultFactoryFunction; 27 } 28 29 // static 30 WifiDataProviderManager* WifiDataProviderManager::Register( 31 WifiDataUpdateCallback* callback) { 32 bool need_to_start_data_provider = false; 33 if (!instance_) { 34 instance_ = new WifiDataProviderManager(); 35 need_to_start_data_provider = true; 36 } 37 DCHECK(instance_); 38 instance_->AddCallback(callback); 39 // Start the provider after adding the callback, to avoid any race in 40 // it running early. 41 if (need_to_start_data_provider) 42 instance_->StartDataProvider(); 43 return instance_; 44 } 45 46 // static 47 bool WifiDataProviderManager::Unregister(WifiDataUpdateCallback* callback) { 48 DCHECK(instance_); 49 DCHECK(instance_->has_callbacks()); 50 if (!instance_->RemoveCallback(callback)) { 51 return false; 52 } 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(); 58 delete instance_; 59 instance_ = NULL; 60 } 61 return true; 62 } 63 64 WifiDataProviderManager::WifiDataProviderManager() { 65 DCHECK(factory_function_); 66 impl_ = (*factory_function_)(); 67 DCHECK(impl_.get()); 68 } 69 70 WifiDataProviderManager::~WifiDataProviderManager() { 71 DCHECK(impl_.get()); 72 } 73 74 bool WifiDataProviderManager::GetData(WifiData* data) { 75 return impl_->GetData(data); 76 } 77 78 void WifiDataProviderManager::AddCallback(WifiDataUpdateCallback* callback) { 79 impl_->AddCallback(callback); 80 } 81 82 bool WifiDataProviderManager::RemoveCallback(WifiDataUpdateCallback* callback) { 83 return impl_->RemoveCallback(callback); 84 } 85 86 bool WifiDataProviderManager::has_callbacks() const { 87 return impl_->has_callbacks(); 88 } 89 90 void WifiDataProviderManager::StartDataProvider() { 91 impl_->StartDataProvider(); 92 } 93 94 void WifiDataProviderManager::StopDataProvider() { 95 impl_->StopDataProvider(); 96 } 97 98 } // namespace content 99