Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: blimp/engine/feature/geolocation/blimp_location_provider.cc

Issue 2328453003: Makes use of EngineGeolocationFeature weak_ptr threadsafe. (Closed)
Patch Set: Addresses Wez's #23 comments. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // 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
14 29
15 BlimpLocationProvider::BlimpLocationProvider( 30 BlimpLocationProvider::BlimpLocationProvider(
16 base::WeakPtr<BlimpLocationProvider::Delegate> delegate) 31 base::WeakPtr<BlimpLocationProvider::Delegate> delegate,
17 : delegate_(delegate), is_started_(false) {} 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) {}
18 37
19 BlimpLocationProvider::~BlimpLocationProvider() { 38 BlimpLocationProvider::~BlimpLocationProvider() {
20 if (is_started_) { 39 if (is_started_) {
21 StopProvider(); 40 StopProvider();
22 } 41 }
23 } 42 }
24 43
25 bool BlimpLocationProvider::StartProvider(bool high_accuracy) { 44 bool BlimpLocationProvider::StartProvider(bool high_accuracy) {
26 if (delegate_) { 45 is_started_ = true;
27 if (high_accuracy) { 46 delegate_task_runner_->PostTask(
28 delegate_->RequestAccuracy( 47 FROM_HERE,
29 GeolocationSetInterestLevelMessage::HIGH_ACCURACY); 48 base::Bind(
30 } else { 49 &BlimpLocationProvider::Delegate::RequestAccuracy, delegate_,
31 delegate_->RequestAccuracy( 50 (high_accuracy ? GeolocationSetInterestLevelMessage::HIGH_ACCURACY
32 GeolocationSetInterestLevelMessage::LOW_ACCURACY); 51 : GeolocationSetInterestLevelMessage::LOW_ACCURACY)));
33 }
34 is_started_ = true;
35 }
36 return is_started_; 52 return is_started_;
37 } 53 }
38 54
39 void BlimpLocationProvider::StopProvider() { 55 void BlimpLocationProvider::StopProvider() {
40 DCHECK(is_started_); 56 DCHECK(is_started_);
41 if (delegate_) { 57 delegate_task_runner_->PostTask(
42 delegate_->RequestAccuracy(GeolocationSetInterestLevelMessage::NO_INTEREST); 58 FROM_HERE,
43 is_started_ = false; 59 base::Bind(&BlimpLocationProvider::Delegate::RequestAccuracy, delegate_,
44 } 60 GeolocationSetInterestLevelMessage::NO_INTEREST));
61 is_started_ = false;
45 } 62 }
46 63
47 const device::Geoposition& BlimpLocationProvider::GetPosition() { 64 const device::Geoposition& BlimpLocationProvider::GetPosition() {
48 return cached_position_; 65 return cached_position_;
49 } 66 }
50 67
51 void BlimpLocationProvider::OnPermissionGranted() { 68 void BlimpLocationProvider::OnPermissionGranted() {
52 DCHECK(is_started_); 69 DCHECK(is_started_);
53 if (delegate_) { 70 delegate_task_runner_->PostTask(
54 delegate_->OnPermissionGranted(); 71 FROM_HERE,
55 } 72 base::Bind(&BlimpLocationProvider::Delegate::OnPermissionGranted,
73 delegate_));
56 } 74 }
57 75
58 void BlimpLocationProvider::SetUpdateCallback( 76 void BlimpLocationProvider::SetUpdateCallback(
59 const LocationProviderUpdateCallback& callback) { 77 const LocationProviderUpdateCallback& callback) {
60 if (delegate_) { 78 // We post a SetUpdateCallback call to the delegate thread.
61 delegate_->SetUpdateCallback(base::Bind(callback, base::Unretained(this))); 79 // InvokeGeopositionCallback runs on the delegate thread on geoposition
62 } 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);
63 } 95 }
64 96
65 } // namespace engine 97 } // namespace engine
66 } // namespace blimp 98 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698