| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 InformProvidersPermissionGranted(); | 67 InformProvidersPermissionGranted(); |
| 66 } | 68 } |
| 67 | 69 |
| 68 void GeolocationProviderImpl::OverrideLocationForTesting( | 70 void GeolocationProviderImpl::OverrideLocationForTesting( |
| 69 const Geoposition& position) { | 71 const Geoposition& position) { |
| 70 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 72 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 71 ignore_location_updates_ = true; | 73 ignore_location_updates_ = true; |
| 72 NotifyClients(position); | 74 NotifyClients(position); |
| 73 } | 75 } |
| 74 | 76 |
| 75 void GeolocationProviderImpl::OnLocationUpdate(const Geoposition& position) { | 77 void GeolocationProviderImpl::OnLocationUpdate(const LocationProvider* provider, |
| 78 const Geoposition& position) { |
| 76 DCHECK(OnGeolocationThread()); | 79 DCHECK(OnGeolocationThread()); |
| 77 // Will be true only in testing. | 80 // Will be true only in testing. |
| 78 if (ignore_location_updates_) | 81 if (ignore_location_updates_) |
| 79 return; | 82 return; |
| 80 main_task_runner_->PostTask( | 83 main_task_runner_->PostTask( |
| 81 FROM_HERE, base::Bind(&GeolocationProviderImpl::NotifyClients, | 84 FROM_HERE, base::Bind(&GeolocationProviderImpl::NotifyClients, |
| 82 base::Unretained(this), position)); | 85 base::Unretained(this), position)); |
| 83 } | 86 } |
| 84 | 87 |
| 85 // static | 88 // static |
| (...skipping 11 matching lines...) Expand all Loading... |
| 97 &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this))); | 100 &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this))); |
| 98 low_accuracy_callbacks_.set_removal_callback(base::Bind( | 101 low_accuracy_callbacks_.set_removal_callback(base::Bind( |
| 99 &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this))); | 102 &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this))); |
| 100 } | 103 } |
| 101 | 104 |
| 102 GeolocationProviderImpl::~GeolocationProviderImpl() { | 105 GeolocationProviderImpl::~GeolocationProviderImpl() { |
| 103 Stop(); | 106 Stop(); |
| 104 DCHECK(!arbitrator_); | 107 DCHECK(!arbitrator_); |
| 105 } | 108 } |
| 106 | 109 |
| 110 void GeolocationProviderImpl::SetArbitratorForTesting( |
| 111 std::unique_ptr<LocationProvider> arbitrator) { |
| 112 arbitrator_ = std::move(arbitrator); |
| 113 } |
| 114 |
| 107 bool GeolocationProviderImpl::OnGeolocationThread() const { | 115 bool GeolocationProviderImpl::OnGeolocationThread() const { |
| 108 return task_runner()->BelongsToCurrentThread(); | 116 return task_runner()->BelongsToCurrentThread(); |
| 109 } | 117 } |
| 110 | 118 |
| 111 void GeolocationProviderImpl::OnClientsChanged() { | 119 void GeolocationProviderImpl::OnClientsChanged() { |
| 112 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 120 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 113 base::Closure task; | 121 base::Closure task; |
| 114 if (high_accuracy_callbacks_.empty() && low_accuracy_callbacks_.empty()) { | 122 if (high_accuracy_callbacks_.empty() && low_accuracy_callbacks_.empty()) { |
| 115 DCHECK(IsRunning()); | 123 DCHECK(IsRunning()); |
| 116 if (!ignore_location_updates_) { | 124 if (!ignore_location_updates_) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 133 task = base::Bind(&GeolocationProviderImpl::StartProviders, | 141 task = base::Bind(&GeolocationProviderImpl::StartProviders, |
| 134 base::Unretained(this), enable_high_accuracy); | 142 base::Unretained(this), enable_high_accuracy); |
| 135 } | 143 } |
| 136 | 144 |
| 137 task_runner()->PostTask(FROM_HERE, task); | 145 task_runner()->PostTask(FROM_HERE, task); |
| 138 } | 146 } |
| 139 | 147 |
| 140 void GeolocationProviderImpl::StopProviders() { | 148 void GeolocationProviderImpl::StopProviders() { |
| 141 DCHECK(OnGeolocationThread()); | 149 DCHECK(OnGeolocationThread()); |
| 142 DCHECK(arbitrator_); | 150 DCHECK(arbitrator_); |
| 143 arbitrator_->StopProviders(); | 151 arbitrator_->StopProvider(); |
| 144 } | 152 } |
| 145 | 153 |
| 146 void GeolocationProviderImpl::StartProviders(bool enable_high_accuracy) { | 154 void GeolocationProviderImpl::StartProviders(bool enable_high_accuracy) { |
| 147 DCHECK(OnGeolocationThread()); | 155 DCHECK(OnGeolocationThread()); |
| 148 DCHECK(arbitrator_); | 156 DCHECK(arbitrator_); |
| 149 arbitrator_->StartProviders(enable_high_accuracy); | 157 arbitrator_->StartProvider(enable_high_accuracy); |
| 150 } | 158 } |
| 151 | 159 |
| 152 void GeolocationProviderImpl::InformProvidersPermissionGranted() { | 160 void GeolocationProviderImpl::InformProvidersPermissionGranted() { |
| 153 DCHECK(IsRunning()); | 161 DCHECK(IsRunning()); |
| 154 if (!OnGeolocationThread()) { | 162 if (!OnGeolocationThread()) { |
| 155 task_runner()->PostTask( | 163 task_runner()->PostTask( |
| 156 FROM_HERE, | 164 FROM_HERE, |
| 157 base::Bind(&GeolocationProviderImpl::InformProvidersPermissionGranted, | 165 base::Bind(&GeolocationProviderImpl::InformProvidersPermissionGranted, |
| 158 base::Unretained(this))); | 166 base::Unretained(this))); |
| 159 return; | 167 return; |
| 160 } | 168 } |
| 161 DCHECK(OnGeolocationThread()); | 169 DCHECK(OnGeolocationThread()); |
| 162 DCHECK(arbitrator_); | 170 DCHECK(arbitrator_); |
| 163 arbitrator_->OnPermissionGranted(); | 171 arbitrator_->OnPermissionGranted(); |
| 164 } | 172 } |
| 165 | 173 |
| 166 void GeolocationProviderImpl::NotifyClients(const Geoposition& position) { | 174 void GeolocationProviderImpl::NotifyClients(const Geoposition& position) { |
| 167 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 175 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 168 DCHECK(position.Validate() || | 176 DCHECK(position.Validate() || |
| 169 position.error_code != Geoposition::ERROR_CODE_NONE); | 177 position.error_code != Geoposition::ERROR_CODE_NONE); |
| 170 position_ = position; | 178 position_ = position; |
| 171 high_accuracy_callbacks_.Notify(position_); | 179 high_accuracy_callbacks_.Notify(position_); |
| 172 low_accuracy_callbacks_.Notify(position_); | 180 low_accuracy_callbacks_.Notify(position_); |
| 173 } | 181 } |
| 174 | 182 |
| 175 void GeolocationProviderImpl::Init() { | 183 void GeolocationProviderImpl::Init() { |
| 176 DCHECK(OnGeolocationThread()); | 184 DCHECK(OnGeolocationThread()); |
| 177 DCHECK(!arbitrator_); | 185 |
| 178 arbitrator_ = CreateArbitrator(); | 186 if (!arbitrator_) { |
| 187 LocationProvider::LocationProviderUpdateCallback callback = base::Bind( |
| 188 &GeolocationProviderImpl::OnLocationUpdate, base::Unretained(this)); |
| 189 // Use the embedder's |g_delegate| or fall back to the default one. |
| 190 if (!g_delegate.Get()) |
| 191 g_delegate.Get().reset(new GeolocationDelegate); |
| 192 |
| 193 arbitrator_ = |
| 194 base::MakeUnique<LocationArbitratorImpl>(g_delegate.Get().get()); |
| 195 arbitrator_->SetUpdateCallback(callback); |
| 196 } |
| 179 } | 197 } |
| 180 | 198 |
| 181 void GeolocationProviderImpl::CleanUp() { | 199 void GeolocationProviderImpl::CleanUp() { |
| 182 DCHECK(OnGeolocationThread()); | 200 DCHECK(OnGeolocationThread()); |
| 183 arbitrator_.reset(); | 201 arbitrator_.reset(); |
| 184 } | 202 } |
| 185 | 203 |
| 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::MakeUnique<LocationArbitratorImpl>(callback, | |
| 195 g_delegate.Get().get()); | |
| 196 } | |
| 197 | |
| 198 } // namespace device | 204 } // namespace device |
| OLD | NEW |