OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "blimp/engine/feature/geolocation/blimp_location_provider.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/memory/weak_ptr.h" | |
10 #include "base/task_runner.h" | |
11 #include "base/threading/thread_task_runner_handle.h" | |
12 #include "device/geolocation/geoposition.h" | |
13 | |
14 namespace blimp { | |
15 namespace engine { | |
16 namespace { | |
17 | |
18 // Called on the delegate thread, this function posts the results | |
19 // of the geolocation update back to the main blimp thread. | |
20 void InvokeGeopositionCallback( | |
21 scoped_refptr<base::TaskRunner> geolocation_task_runner, | |
22 const base::Callback<void(const device::Geoposition&)>& callback, | |
23 const device::Geoposition& geoposition) { | |
24 geolocation_task_runner->PostTask(FROM_HERE, | |
25 base::Bind(callback, geoposition)); | |
26 } | |
27 | |
28 } // namespace | |
29 | |
30 BlimpLocationProvider::BlimpLocationProvider( | |
31 base::WeakPtr<BlimpLocationProvider::Delegate> delegate, | |
32 scoped_refptr<base::SequencedTaskRunner> delegate_task_runner) | |
33 : delegate_(delegate), | |
34 delegate_task_runner_(delegate_task_runner), | |
35 is_started_(false), | |
36 weak_factory_(this) {} | |
37 | |
38 BlimpLocationProvider::~BlimpLocationProvider() { | |
39 if (is_started_) { | |
40 StopProvider(); | |
41 } | |
42 } | |
43 | |
44 bool BlimpLocationProvider::StartProvider(bool high_accuracy) { | |
45 is_started_ = true; | |
46 delegate_task_runner_->PostTask( | |
47 FROM_HERE, | |
48 base::Bind( | |
49 &BlimpLocationProvider::Delegate::RequestAccuracy, delegate_, | |
50 (high_accuracy ? GeolocationSetInterestLevelMessage::HIGH_ACCURACY | |
51 : GeolocationSetInterestLevelMessage::LOW_ACCURACY))); | |
52 return is_started_; | |
53 } | |
54 | |
55 void BlimpLocationProvider::StopProvider() { | |
56 DCHECK(is_started_); | |
57 delegate_task_runner_->PostTask( | |
58 FROM_HERE, | |
59 base::Bind(&BlimpLocationProvider::Delegate::RequestAccuracy, delegate_, | |
60 GeolocationSetInterestLevelMessage::NO_INTEREST)); | |
61 is_started_ = false; | |
62 } | |
63 | |
64 const device::Geoposition& BlimpLocationProvider::GetPosition() { | |
65 return cached_position_; | |
66 } | |
67 | |
68 void BlimpLocationProvider::OnPermissionGranted() { | |
69 DCHECK(is_started_); | |
70 delegate_task_runner_->PostTask( | |
71 FROM_HERE, | |
72 base::Bind(&BlimpLocationProvider::Delegate::OnPermissionGranted, | |
73 delegate_)); | |
74 } | |
75 | |
76 void BlimpLocationProvider::SetUpdateCallback( | |
77 const LocationProviderUpdateCallback& callback) { | |
78 // We post a SetUpdateCallback call to the delegate thread. | |
79 // InvokeGeopositionCallback runs on the delegate thread on geoposition | |
80 // update and then uses the task runner passed to it to post the results back | |
81 // to the blimp thread. | |
82 location_update_callback_ = callback; | |
83 delegate_task_runner_->PostTask( | |
84 FROM_HERE, | |
85 base::Bind(&BlimpLocationProvider::Delegate::SetUpdateCallback, delegate_, | |
86 base::Bind(&InvokeGeopositionCallback, | |
87 base::ThreadTaskRunnerHandle::Get(), | |
88 base::Bind(&BlimpLocationProvider::OnLocationUpdate, | |
89 weak_factory_.GetWeakPtr())))); | |
90 } | |
91 | |
92 void BlimpLocationProvider::OnLocationUpdate( | |
93 const device::Geoposition& geoposition) { | |
94 location_update_callback_.Run(this, geoposition); | |
95 } | |
96 | |
97 } // namespace engine | |
98 } // namespace blimp | |
OLD | NEW |