| 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..622eab5dbc62d1889d8042e36382570d704b4549 100644
|
| --- a/blimp/engine/feature/geolocation/blimp_location_provider.cc
|
| +++ b/blimp/engine/feature/geolocation/blimp_location_provider.cc
|
| @@ -7,14 +7,33 @@
|
| #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.
|
| +void InvokeGeopositionCallback(
|
| + scoped_refptr<base::TaskRunner> geolocation_task_runner,
|
| + const base::Callback<void(const device::Geoposition&)>& callback,
|
| + const device::Geoposition& geoposition) {
|
| + geolocation_task_runner->PostTask(FROM_HERE,
|
| + base::Bind(callback, 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 +42,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 +67,31 @@ 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.
|
| + location_update_callback_ = callback;
|
| + delegate_task_runner_->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&BlimpLocationProvider::Delegate::SetUpdateCallback, delegate_,
|
| + base::Bind(&InvokeGeopositionCallback,
|
| + base::ThreadTaskRunnerHandle::Get(),
|
| + base::Bind(&BlimpLocationProvider::OnLocationUpdate,
|
| + weak_factory_.GetWeakPtr()))));
|
| +}
|
| +
|
| +void BlimpLocationProvider::OnLocationUpdate(
|
| + const device::Geoposition& geoposition) {
|
| + location_update_callback_.Run(this, geoposition);
|
| }
|
|
|
| } // namespace engine
|
|
|