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

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 #9 comments. Created 4 years, 3 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 void InvokeGeopositionCallback(
19 scoped_refptr<base::TaskRunner> task_runner,
20 const BlimpLocationProvider::LocationProviderUpdateCallback& callback,
21 base::WeakPtr<BlimpLocationProvider> provider,
22 const device::Geoposition& geoposition) {
23 if (provider)
Wez 2016/09/17 01:47:42 Isn't InvokeGeopositionCallback called from the de
CJ 2016/09/23 18:03:58 InvokeGeopositionCallback is called on the geoloca
Wez 2016/09/23 22:28:36 Are you sure? InvokeGepositionCallback is used to
24 task_runner->PostTask(FROM_HERE,
25 base::Bind(callback, provider.get(), geoposition));
Kevin M 2016/10/06 19:30:52 Weak pointers must be dereferenced and deleted on
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 delegate_task_runner_->PostTask(
61 delegate_->SetUpdateCallback(base::Bind(callback, base::Unretained(this))); 79 FROM_HERE,
62 } 80 base::Bind(&BlimpLocationProvider::Delegate::SetUpdateCallback, delegate_,
81 base::Bind(&InvokeGeopositionCallback,
82 base::ThreadTaskRunnerHandle::Get(), callback,
83 weak_factory_.GetWeakPtr())));
63 } 84 }
64 85
65 } // namespace engine 86 } // namespace engine
66 } // namespace blimp 87 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698