Chromium Code Reviews| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this))); | 97 &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this))); |
| 98 low_accuracy_callbacks_.set_removal_callback(base::Bind( | 98 low_accuracy_callbacks_.set_removal_callback(base::Bind( |
| 99 &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this))); | 99 &GeolocationProviderImpl::OnClientsChanged, base::Unretained(this))); |
| 100 } | 100 } |
| 101 | 101 |
| 102 GeolocationProviderImpl::~GeolocationProviderImpl() { | 102 GeolocationProviderImpl::~GeolocationProviderImpl() { |
| 103 Stop(); | 103 Stop(); |
| 104 DCHECK(!arbitrator_); | 104 DCHECK(!arbitrator_); |
| 105 } | 105 } |
| 106 | 106 |
| 107 void GeolocationProviderImpl::SetArbitratorForTesting( | |
| 108 LocationProvider* arbitrator) { | |
| 109 arbitrator_ = base::WrapUnique(arbitrator); | |
| 110 } | |
| 111 | |
| 107 bool GeolocationProviderImpl::OnGeolocationThread() const { | 112 bool GeolocationProviderImpl::OnGeolocationThread() const { |
| 108 return task_runner()->BelongsToCurrentThread(); | 113 return task_runner()->BelongsToCurrentThread(); |
| 109 } | 114 } |
| 110 | 115 |
| 111 void GeolocationProviderImpl::OnClientsChanged() { | 116 void GeolocationProviderImpl::OnClientsChanged() { |
| 112 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 117 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 113 base::Closure task; | 118 base::Closure task; |
| 114 if (high_accuracy_callbacks_.empty() && low_accuracy_callbacks_.empty()) { | 119 if (high_accuracy_callbacks_.empty() && low_accuracy_callbacks_.empty()) { |
| 115 DCHECK(IsRunning()); | 120 DCHECK(IsRunning()); |
| 116 if (!ignore_location_updates_) { | 121 if (!ignore_location_updates_) { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 133 task = base::Bind(&GeolocationProviderImpl::StartProviders, | 138 task = base::Bind(&GeolocationProviderImpl::StartProviders, |
| 134 base::Unretained(this), enable_high_accuracy); | 139 base::Unretained(this), enable_high_accuracy); |
| 135 } | 140 } |
| 136 | 141 |
| 137 task_runner()->PostTask(FROM_HERE, task); | 142 task_runner()->PostTask(FROM_HERE, task); |
| 138 } | 143 } |
| 139 | 144 |
| 140 void GeolocationProviderImpl::StopProviders() { | 145 void GeolocationProviderImpl::StopProviders() { |
| 141 DCHECK(OnGeolocationThread()); | 146 DCHECK(OnGeolocationThread()); |
| 142 DCHECK(arbitrator_); | 147 DCHECK(arbitrator_); |
| 143 arbitrator_->StopProviders(); | 148 arbitrator_->StopProvider(); |
| 144 } | 149 } |
| 145 | 150 |
| 146 void GeolocationProviderImpl::StartProviders(bool enable_high_accuracy) { | 151 void GeolocationProviderImpl::StartProviders(bool enable_high_accuracy) { |
| 147 DCHECK(OnGeolocationThread()); | 152 DCHECK(OnGeolocationThread()); |
| 148 DCHECK(arbitrator_); | 153 DCHECK(arbitrator_); |
| 149 arbitrator_->StartProviders(enable_high_accuracy); | 154 arbitrator_->StartProvider(enable_high_accuracy); |
| 150 } | 155 } |
| 151 | 156 |
| 152 void GeolocationProviderImpl::InformProvidersPermissionGranted() { | 157 void GeolocationProviderImpl::InformProvidersPermissionGranted() { |
| 153 DCHECK(IsRunning()); | 158 DCHECK(IsRunning()); |
| 154 if (!OnGeolocationThread()) { | 159 if (!OnGeolocationThread()) { |
| 155 task_runner()->PostTask( | 160 task_runner()->PostTask( |
| 156 FROM_HERE, | 161 FROM_HERE, |
| 157 base::Bind(&GeolocationProviderImpl::InformProvidersPermissionGranted, | 162 base::Bind(&GeolocationProviderImpl::InformProvidersPermissionGranted, |
| 158 base::Unretained(this))); | 163 base::Unretained(this))); |
| 159 return; | 164 return; |
| 160 } | 165 } |
| 161 DCHECK(OnGeolocationThread()); | 166 DCHECK(OnGeolocationThread()); |
| 162 DCHECK(arbitrator_); | 167 DCHECK(arbitrator_); |
| 163 arbitrator_->OnPermissionGranted(); | 168 arbitrator_->OnPermissionGranted(); |
| 164 } | 169 } |
| 165 | 170 |
| 166 void GeolocationProviderImpl::NotifyClients(const Geoposition& position) { | 171 void GeolocationProviderImpl::NotifyClients(const Geoposition& position) { |
| 167 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 172 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 168 DCHECK(position.Validate() || | 173 DCHECK(position.Validate() || |
| 169 position.error_code != Geoposition::ERROR_CODE_NONE); | 174 position.error_code != Geoposition::ERROR_CODE_NONE); |
| 170 position_ = position; | 175 position_ = position; |
| 171 high_accuracy_callbacks_.Notify(position_); | 176 high_accuracy_callbacks_.Notify(position_); |
| 172 low_accuracy_callbacks_.Notify(position_); | 177 low_accuracy_callbacks_.Notify(position_); |
| 173 } | 178 } |
| 174 | 179 |
| 175 void GeolocationProviderImpl::Init() { | 180 void GeolocationProviderImpl::Init() { |
| 176 DCHECK(OnGeolocationThread()); | 181 DCHECK(OnGeolocationThread()); |
| 177 DCHECK(!arbitrator_); | 182 |
| 178 arbitrator_ = CreateArbitrator(); | 183 if (!arbitrator_) { |
| 184 LocationArbitratorImpl::LocationUpdateCallback callback = base::Bind( | |
| 185 &GeolocationProviderImpl::OnLocationUpdate, base::Unretained(this)); | |
| 186 // Use the embedder's |g_delegate| or fall back to the default one. | |
| 187 if (!g_delegate.Get()) | |
| 188 g_delegate.Get().reset(new GeolocationDelegate); | |
| 189 | |
| 190 arbitrator_ = base::WrapUnique( | |
| 191 new LocationArbitratorImpl(callback, g_delegate.Get().get())); | |
|
Wez
2016/08/12 00:33:44
nit: You can use MakeUnique here. :)
CJ
2016/08/12 20:22:46
Done.
| |
| 192 } | |
| 179 } | 193 } |
| 180 | 194 |
| 181 void GeolocationProviderImpl::CleanUp() { | 195 void GeolocationProviderImpl::CleanUp() { |
| 182 DCHECK(OnGeolocationThread()); | 196 DCHECK(OnGeolocationThread()); |
| 183 arbitrator_.reset(); | 197 arbitrator_.reset(); |
| 184 } | 198 } |
| 185 | 199 |
| 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 | 200 } // namespace device |
| OLD | NEW |