| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "device/geolocation/geolocation_provider_impl.h" | 5 #include "device/geolocation/geolocation_provider_impl.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 11 #include "base/callback.h" |
| 10 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 11 #include "base/location.h" | 13 #include "base/location.h" |
| 12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/singleton.h" | 16 #include "base/memory/singleton.h" |
| 15 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this))); | 99 &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this))); |
| 98 low_accuracy_callbacks_.set_removal_callback(base::Bind( | 100 low_accuracy_callbacks_.set_removal_callback(base::Bind( |
| 99 &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this))); | 101 &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this))); |
| 100 } | 102 } |
| 101 | 103 |
| 102 GeolocationProviderImpl::~GeolocationProviderImpl() { | 104 GeolocationProviderImpl::~GeolocationProviderImpl() { |
| 103 Stop(); | 105 Stop(); |
| 104 DCHECK(!arbitrator_); | 106 DCHECK(!arbitrator_); |
| 105 } | 107 } |
| 106 | 108 |
| 109 void GeolocationProviderImpl::SetArbitratorForTesting( |
| 110 std::unique_ptr<LocationProvider> arbitrator) { |
| 111 arbitrator_ = std::move(arbitrator); |
| 112 } |
| 113 |
| 107 bool GeolocationProviderImpl::OnGeolocationThread() const { | 114 bool GeolocationProviderImpl::OnGeolocationThread() const { |
| 108 return task_runner()->BelongsToCurrentThread(); | 115 return task_runner()->BelongsToCurrentThread(); |
| 109 } | 116 } |
| 110 | 117 |
| 111 void GeolocationProviderImpl::OnClientsChanged() { | 118 void GeolocationProviderImpl::OnClientsChanged() { |
| 112 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 119 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 113 base::Closure task; | 120 base::Closure task; |
| 114 if (high_accuracy_callbacks_.empty() && low_accuracy_callbacks_.empty()) { | 121 if (high_accuracy_callbacks_.empty() && low_accuracy_callbacks_.empty()) { |
| 115 DCHECK(IsRunning()); | 122 DCHECK(IsRunning()); |
| 116 if (!ignore_location_updates_) { | 123 if (!ignore_location_updates_) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 133 task = base::Bind(&GeolocationProviderImpl::StartProviders, | 140 task = base::Bind(&GeolocationProviderImpl::StartProviders, |
| 134 base::Unretained(this), enable_high_accuracy); | 141 base::Unretained(this), enable_high_accuracy); |
| 135 } | 142 } |
| 136 | 143 |
| 137 task_runner()->PostTask(FROM_HERE, task); | 144 task_runner()->PostTask(FROM_HERE, task); |
| 138 } | 145 } |
| 139 | 146 |
| 140 void GeolocationProviderImpl::StopProviders() { | 147 void GeolocationProviderImpl::StopProviders() { |
| 141 DCHECK(OnGeolocationThread()); | 148 DCHECK(OnGeolocationThread()); |
| 142 DCHECK(arbitrator_); | 149 DCHECK(arbitrator_); |
| 143 arbitrator_->StopProviders(); | 150 arbitrator_->StopProvider(); |
| 144 } | 151 } |
| 145 | 152 |
| 146 void GeolocationProviderImpl::StartProviders(bool enable_high_accuracy) { | 153 void GeolocationProviderImpl::StartProviders(bool enable_high_accuracy) { |
| 147 DCHECK(OnGeolocationThread()); | 154 DCHECK(OnGeolocationThread()); |
| 148 DCHECK(arbitrator_); | 155 DCHECK(arbitrator_); |
| 149 arbitrator_->StartProviders(enable_high_accuracy); | 156 arbitrator_->StartProvider(enable_high_accuracy); |
| 150 } | 157 } |
| 151 | 158 |
| 152 void GeolocationProviderImpl::InformProvidersPermissionGranted() { | 159 void GeolocationProviderImpl::InformProvidersPermissionGranted() { |
| 153 DCHECK(IsRunning()); | 160 DCHECK(IsRunning()); |
| 154 if (!OnGeolocationThread()) { | 161 if (!OnGeolocationThread()) { |
| 155 task_runner()->PostTask( | 162 task_runner()->PostTask( |
| 156 FROM_HERE, | 163 FROM_HERE, |
| 157 base::Bind(&GeolocationProviderImpl::InformProvidersPermissionGranted, | 164 base::Bind(&GeolocationProviderImpl::InformProvidersPermissionGranted, |
| 158 base::Unretained(this))); | 165 base::Unretained(this))); |
| 159 return; | 166 return; |
| 160 } | 167 } |
| 161 DCHECK(OnGeolocationThread()); | 168 DCHECK(OnGeolocationThread()); |
| 162 DCHECK(arbitrator_); | 169 DCHECK(arbitrator_); |
| 163 arbitrator_->OnPermissionGranted(); | 170 arbitrator_->OnPermissionGranted(); |
| 164 } | 171 } |
| 165 | 172 |
| 166 void GeolocationProviderImpl::NotifyClients(const Geoposition& position) { | 173 void GeolocationProviderImpl::NotifyClients(const Geoposition& position) { |
| 167 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 174 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 168 DCHECK(position.Validate() || | 175 DCHECK(position.Validate() || |
| 169 position.error_code != Geoposition::ERROR_CODE_NONE); | 176 position.error_code != Geoposition::ERROR_CODE_NONE); |
| 170 position_ = position; | 177 position_ = position; |
| 171 high_accuracy_callbacks_.Notify(position_); | 178 high_accuracy_callbacks_.Notify(position_); |
| 172 low_accuracy_callbacks_.Notify(position_); | 179 low_accuracy_callbacks_.Notify(position_); |
| 173 } | 180 } |
| 174 | 181 |
| 175 void GeolocationProviderImpl::Init() { | 182 void GeolocationProviderImpl::Init() { |
| 176 DCHECK(OnGeolocationThread()); | 183 DCHECK(OnGeolocationThread()); |
| 177 DCHECK(!arbitrator_); | 184 |
| 178 arbitrator_ = CreateArbitrator(); | 185 if (!arbitrator_) { |
| 186 LocationArbitratorImpl::LocationUpdateCallback callback = base::Bind( |
| 187 &GeolocationProviderImpl::OnLocationUpdate, base::Unretained(this)); |
| 188 // Use the embedder's |g_delegate| or fall back to the default one. |
| 189 if (!g_delegate.Get()) |
| 190 g_delegate.Get().reset(new GeolocationDelegate); |
| 191 |
| 192 arbitrator_ = base::MakeUnique<LocationArbitratorImpl>( |
| 193 callback, g_delegate.Get().get()); |
| 194 } |
| 179 } | 195 } |
| 180 | 196 |
| 181 void GeolocationProviderImpl::CleanUp() { | 197 void GeolocationProviderImpl::CleanUp() { |
| 182 DCHECK(OnGeolocationThread()); | 198 DCHECK(OnGeolocationThread()); |
| 183 arbitrator_.reset(); | 199 arbitrator_.reset(); |
| 184 } | 200 } |
| 185 | 201 |
| 186 std::unique_ptr<LocationArbitrator> | |
| 187 GeolocationProviderImpl::CreateArbitrator() { | |
| 188 LocationArbitratorImpl::LocationUpdateCallback callback = base::Bind( | |
| 189 &GeolocationProviderImpl::OnLocationUpdate, base::Unretained(this)); | |
| 190 // Use the embedder's |g_delegate| or fall back to the default one. | |
| 191 if (!g_delegate.Get()) | |
| 192 g_delegate.Get().reset(new GeolocationDelegate); | |
| 193 | |
| 194 return base::WrapUnique( | |
| 195 new LocationArbitratorImpl(callback, g_delegate.Get().get())); | |
| 196 } | |
| 197 | |
| 198 } // namespace device | 202 } // namespace device |
| OLD | NEW |