Chromium Code Reviews| Index: blimp/engine/feature/geolocation/blimp_location_provider.cc |
| diff --git a/blimp/engine/feature/geolocation/blimp_location_provider.cc b/blimp/engine/feature/geolocation/blimp_location_provider.cc |
| index 9fd039774ab36b89f777a437f3f36fdd673ecb34..f63756ec28069f50ed58b8415ccf3ff2d6147ff3 100644 |
| --- a/blimp/engine/feature/geolocation/blimp_location_provider.cc |
| +++ b/blimp/engine/feature/geolocation/blimp_location_provider.cc |
| @@ -7,14 +7,35 @@ |
| #include "base/bind.h" |
| #include "base/bind_helpers.h" |
| #include "base/memory/weak_ptr.h" |
| +#include "base/task_runner.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| #include "device/geolocation/geoposition.h" |
| namespace blimp { |
| namespace engine { |
| +namespace { |
| + |
| +// Called on the delegate thread, this function posts the results |
| +// of the geolocation update back to the main blimp thread. |
|
Kevin M
2016/10/06 19:30:52
main blimp thread => blimp_task_runner.
|
| +void InvokeGeopositionCallback( |
| + scoped_refptr<base::TaskRunner> blimp_task_runner, |
| + const BlimpLocationProvider::LocationProviderUpdateCallback& callback, |
| + base::WeakPtr<BlimpLocationProvider> provider, |
| + const device::Geoposition& geoposition) { |
| + if (provider) |
| + blimp_task_runner->PostTask(FROM_HERE, |
| + base::Bind(callback, provider.get(), geoposition)); |
| +} |
| + |
| +} // namespace |
| BlimpLocationProvider::BlimpLocationProvider( |
| - base::WeakPtr<BlimpLocationProvider::Delegate> delegate) |
| - : delegate_(delegate), is_started_(false) {} |
| + base::WeakPtr<BlimpLocationProvider::Delegate> delegate, |
| + scoped_refptr<base::SequencedTaskRunner> delegate_task_runner) |
| + : delegate_(delegate), |
| + delegate_task_runner_(delegate_task_runner), |
| + is_started_(false), |
| + weak_factory_(this) {} |
| BlimpLocationProvider::~BlimpLocationProvider() { |
| if (is_started_) { |
| @@ -23,25 +44,23 @@ BlimpLocationProvider::~BlimpLocationProvider() { |
| } |
| bool BlimpLocationProvider::StartProvider(bool high_accuracy) { |
| - if (delegate_) { |
| - if (high_accuracy) { |
| - delegate_->RequestAccuracy( |
| - GeolocationSetInterestLevelMessage::HIGH_ACCURACY); |
| - } else { |
| - delegate_->RequestAccuracy( |
| - GeolocationSetInterestLevelMessage::LOW_ACCURACY); |
| - } |
| - is_started_ = true; |
| - } |
| + is_started_ = true; |
| + delegate_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind( |
| + &BlimpLocationProvider::Delegate::RequestAccuracy, delegate_, |
| + (high_accuracy ? GeolocationSetInterestLevelMessage::HIGH_ACCURACY |
| + : GeolocationSetInterestLevelMessage::LOW_ACCURACY))); |
| return is_started_; |
| } |
| void BlimpLocationProvider::StopProvider() { |
| DCHECK(is_started_); |
| - if (delegate_) { |
| - delegate_->RequestAccuracy(GeolocationSetInterestLevelMessage::NO_INTEREST); |
| - is_started_ = false; |
| - } |
| + delegate_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&BlimpLocationProvider::Delegate::RequestAccuracy, delegate_, |
| + GeolocationSetInterestLevelMessage::NO_INTEREST)); |
| + is_started_ = false; |
| } |
| const device::Geoposition& BlimpLocationProvider::GetPosition() { |
| @@ -50,16 +69,24 @@ const device::Geoposition& BlimpLocationProvider::GetPosition() { |
| void BlimpLocationProvider::OnPermissionGranted() { |
| DCHECK(is_started_); |
| - if (delegate_) { |
| - delegate_->OnPermissionGranted(); |
| - } |
| + delegate_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&BlimpLocationProvider::Delegate::OnPermissionGranted, |
| + delegate_)); |
| } |
| void BlimpLocationProvider::SetUpdateCallback( |
| const LocationProviderUpdateCallback& callback) { |
| - if (delegate_) { |
| - delegate_->SetUpdateCallback(base::Bind(callback, base::Unretained(this))); |
| - } |
| + // We post a SetUpdateCallback call to the delegate thread. |
| + // InvokeGeopositionCallback runs on the delegate thread on geoposition |
| + // update and then uses the task runner passed to it to post the results back |
| + // to the blimp thread. |
| + delegate_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&BlimpLocationProvider::Delegate::SetUpdateCallback, delegate_, |
| + base::Bind(&InvokeGeopositionCallback, |
| + base::ThreadTaskRunnerHandle::Get(), callback, |
| + weak_factory_.GetWeakPtr()))); |
| } |
| } // namespace engine |