Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / geolocation / wifi_data_provider_manager.cc
blobcf53657e53a61ba13ffbac13d8307ac6e6761c82
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"
9 namespace content {
11 // static
12 WifiDataProviderManager* WifiDataProviderManager::instance_ = NULL;
14 // static
15 WifiDataProviderManager::ImplFactoryFunction
16 WifiDataProviderManager::factory_function_ = DefaultFactoryFunction;
18 // static
19 void WifiDataProviderManager::SetFactoryForTesting(
20 ImplFactoryFunction factory_function_in) {
21 factory_function_ = factory_function_in;
24 // static
25 void WifiDataProviderManager::ResetFactoryForTesting() {
26 factory_function_ = DefaultFactoryFunction;
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;
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_;
46 // static
47 bool WifiDataProviderManager::Unregister(WifiDataUpdateCallback* callback) {
48 DCHECK(instance_);
49 DCHECK(instance_->has_callbacks());
50 if (!instance_->RemoveCallback(callback)) {
51 return false;
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;
61 return true;
64 WifiDataProviderManager::WifiDataProviderManager() {
65 DCHECK(factory_function_);
66 impl_ = (*factory_function_)();
67 DCHECK(impl_.get());
70 WifiDataProviderManager::~WifiDataProviderManager() {
71 DCHECK(impl_.get());
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