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

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

Issue 2328453003: Makes use of EngineGeolocationFeature weak_ptr threadsafe. (Closed)
Patch Set: Adds comments, addresses Wez's #13 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.
Kevin M 2016/10/06 19:30:52 main blimp thread => blimp_task_runner.
20 void InvokeGeopositionCallback(
21 scoped_refptr<base::TaskRunner> blimp_task_runner,
22 const BlimpLocationProvider::LocationProviderUpdateCallback& callback,
23 base::WeakPtr<BlimpLocationProvider> provider,
24 const device::Geoposition& geoposition) {
25 if (provider)
26 blimp_task_runner->PostTask(FROM_HERE,
27 base::Bind(callback, provider.get(), geoposition));
28 }
29
30 } // namespace
14 31
15 BlimpLocationProvider::BlimpLocationProvider( 32 BlimpLocationProvider::BlimpLocationProvider(
16 base::WeakPtr<BlimpLocationProvider::Delegate> delegate) 33 base::WeakPtr<BlimpLocationProvider::Delegate> delegate,
17 : delegate_(delegate), is_started_(false) {} 34 scoped_refptr<base::SequencedTaskRunner> delegate_task_runner)
35 : delegate_(delegate),
36 delegate_task_runner_(delegate_task_runner),
37 is_started_(false),
38 weak_factory_(this) {}
18 39
19 BlimpLocationProvider::~BlimpLocationProvider() { 40 BlimpLocationProvider::~BlimpLocationProvider() {
20 if (is_started_) { 41 if (is_started_) {
21 StopProvider(); 42 StopProvider();
22 } 43 }
23 } 44 }
24 45
25 bool BlimpLocationProvider::StartProvider(bool high_accuracy) { 46 bool BlimpLocationProvider::StartProvider(bool high_accuracy) {
26 if (delegate_) { 47 is_started_ = true;
27 if (high_accuracy) { 48 delegate_task_runner_->PostTask(
28 delegate_->RequestAccuracy( 49 FROM_HERE,
29 GeolocationSetInterestLevelMessage::HIGH_ACCURACY); 50 base::Bind(
30 } else { 51 &BlimpLocationProvider::Delegate::RequestAccuracy, delegate_,
31 delegate_->RequestAccuracy( 52 (high_accuracy ? GeolocationSetInterestLevelMessage::HIGH_ACCURACY
32 GeolocationSetInterestLevelMessage::LOW_ACCURACY); 53 : GeolocationSetInterestLevelMessage::LOW_ACCURACY)));
33 }
34 is_started_ = true;
35 }
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 // We post a SetUpdateCallback call to the delegate thread.
61 delegate_->SetUpdateCallback(base::Bind(callback, base::Unretained(this))); 81 // InvokeGeopositionCallback runs on the delegate thread on geoposition
62 } 82 // update and then uses the task runner passed to it to post the results back
83 // to the blimp thread.
84 delegate_task_runner_->PostTask(
85 FROM_HERE,
86 base::Bind(&BlimpLocationProvider::Delegate::SetUpdateCallback, delegate_,
87 base::Bind(&InvokeGeopositionCallback,
88 base::ThreadTaskRunnerHandle::Get(), callback,
89 weak_factory_.GetWeakPtr())));
63 } 90 }
64 91
65 } // namespace engine 92 } // namespace engine
66 } // namespace blimp 93 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698