Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 "blimp/engine/feature/geolocation/blimp_location_provider.h" | 5 #include "blimp/engine/feature/geolocation/blimp_location_provider.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/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/task_runner.h" | |
| 11 #include "base/threading/thread_task_runner_handle.h" | |
| 10 #include "device/geolocation/geoposition.h" | 12 #include "device/geolocation/geoposition.h" |
| 11 | 13 |
| 12 namespace blimp { | 14 namespace blimp { |
| 13 namespace engine { | 15 namespace engine { |
| 16 namespace { | |
| 17 | |
| 18 void InvokeGeopositionCallback( | |
|
Wez
2016/09/13 20:19:07
Ah, I think we're going to need bind this to the l
CJ
2016/09/13 23:34:02
|callback| is bound to |this|, the BlimpLocationPr
Wez
2016/09/16 00:40:16
Right, but Bind(callback, this) is just used to su
CJ
2016/09/16 22:55:53
Done.
| |
| 19 scoped_refptr<base::TaskRunner> task_runner, | |
| 20 BlimpLocationProvider::Delegate::GeopositionReceivedCallback callback, | |
| 21 const device::Geoposition& geoposition) { | |
| 22 // this calls task_runner->PostTas | |
|
Wez
2016/09/13 20:19:07
?
CJ
2016/09/13 23:34:02
Done.
| |
| 23 task_runner->PostTask(FROM_HERE, base::Bind(callback, geoposition)); | |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 14 | 27 |
| 15 BlimpLocationProvider::BlimpLocationProvider( | 28 BlimpLocationProvider::BlimpLocationProvider( |
| 16 base::WeakPtr<BlimpLocationProvider::Delegate> delegate) | 29 base::WeakPtr<BlimpLocationProvider::Delegate> delegate, |
| 17 : delegate_(delegate), is_started_(false) {} | 30 scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner) |
| 31 : delegate_(delegate), | |
| 32 delegate_task_runner_(delegate_task_runner), | |
| 33 is_started_(false) {} | |
| 18 | 34 |
| 19 BlimpLocationProvider::~BlimpLocationProvider() { | 35 BlimpLocationProvider::~BlimpLocationProvider() { |
| 20 if (is_started_) { | 36 if (is_started_) { |
| 21 StopProvider(); | 37 StopProvider(); |
| 22 } | 38 } |
| 23 } | 39 } |
| 24 | 40 |
| 25 bool BlimpLocationProvider::StartProvider(bool high_accuracy) { | 41 bool BlimpLocationProvider::StartProvider(bool high_accuracy) { |
| 26 if (delegate_) { | 42 is_started_ = true; |
| 27 if (high_accuracy) { | 43 if (high_accuracy) { |
| 28 delegate_->RequestAccuracy( | 44 delegate_task_runner_->PostTask( |
| 29 GeolocationSetInterestLevelMessage::HIGH_ACCURACY); | 45 FROM_HERE, |
| 30 } else { | 46 base::Bind(&BlimpLocationProvider::Delegate::RequestAccuracy, delegate_, |
| 31 delegate_->RequestAccuracy( | 47 GeolocationSetInterestLevelMessage::HIGH_ACCURACY)); |
|
Wez
2016/09/13 20:19:07
nit: You can merge this into a single PostTask wit
CJ
2016/09/13 23:34:02
Done.
| |
| 32 GeolocationSetInterestLevelMessage::LOW_ACCURACY); | 48 } else { |
| 33 } | 49 delegate_task_runner_->PostTask( |
| 34 is_started_ = true; | 50 FROM_HERE, |
| 51 base::Bind(&BlimpLocationProvider::Delegate::RequestAccuracy, delegate_, | |
| 52 GeolocationSetInterestLevelMessage::LOW_ACCURACY)); | |
| 35 } | 53 } |
| 36 return is_started_; | 54 return is_started_; |
| 37 } | 55 } |
| 38 | 56 |
| 39 void BlimpLocationProvider::StopProvider() { | 57 void BlimpLocationProvider::StopProvider() { |
| 40 DCHECK(is_started_); | 58 DCHECK(is_started_); |
| 41 if (delegate_) { | 59 delegate_task_runner_->PostTask( |
| 42 delegate_->RequestAccuracy(GeolocationSetInterestLevelMessage::NO_INTEREST); | 60 FROM_HERE, |
| 43 is_started_ = false; | 61 base::Bind(&BlimpLocationProvider::Delegate::RequestAccuracy, delegate_, |
| 44 } | 62 GeolocationSetInterestLevelMessage::NO_INTEREST)); |
| 63 is_started_ = false; | |
| 45 } | 64 } |
| 46 | 65 |
| 47 const device::Geoposition& BlimpLocationProvider::GetPosition() { | 66 const device::Geoposition& BlimpLocationProvider::GetPosition() { |
| 48 return cached_position_; | 67 return cached_position_; |
| 49 } | 68 } |
| 50 | 69 |
| 51 void BlimpLocationProvider::OnPermissionGranted() { | 70 void BlimpLocationProvider::OnPermissionGranted() { |
| 52 DCHECK(is_started_); | 71 DCHECK(is_started_); |
| 53 if (delegate_) { | 72 delegate_task_runner_->PostTask( |
| 54 delegate_->OnPermissionGranted(); | 73 FROM_HERE, |
| 55 } | 74 base::Bind(&BlimpLocationProvider::Delegate::OnPermissionGranted, |
| 75 delegate_)); | |
| 56 } | 76 } |
| 57 | 77 |
| 58 void BlimpLocationProvider::SetUpdateCallback( | 78 void BlimpLocationProvider::SetUpdateCallback( |
| 59 const LocationProviderUpdateCallback& callback) { | 79 const LocationProviderUpdateCallback& callback) { |
| 60 if (delegate_) { | 80 delegate_task_runner_->PostTask( |
| 61 delegate_->SetUpdateCallback(base::Bind(callback, base::Unretained(this))); | 81 FROM_HERE, |
| 62 } | 82 base::Bind(&BlimpLocationProvider::Delegate::SetUpdateCallback, delegate_, |
| 83 base::Bind(&InvokeGeopositionCallback, | |
| 84 base::ThreadTaskRunnerHandle::Get(), | |
| 85 base::Bind(callback, this)))); | |
| 63 } | 86 } |
| 64 | 87 |
| 65 } // namespace engine | 88 } // namespace engine |
| 66 } // namespace blimp | 89 } // namespace blimp |
| OLD | NEW |