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

Side by Side Diff: blimp/engine/feature/geolocation/engine_geolocation_feature.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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 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/engine_geolocation_feature.h" 5 #include "blimp/engine/feature/geolocation/engine_geolocation_feature.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/thread_task_runner_handle.h" 12 #include "base/threading/thread_task_runner_handle.h"
13 #include "blimp/common/create_blimp_message.h" 13 #include "blimp/common/create_blimp_message.h"
14 #include "blimp/common/proto/blimp_message.pb.h" 14 #include "blimp/common/proto/blimp_message.pb.h"
15 #include "blimp/common/proto/geolocation.pb.h" 15 #include "blimp/common/proto/geolocation.pb.h"
16 #include "device/geolocation/geolocation_delegate.h" 16 #include "device/geolocation/geolocation_delegate.h"
17 #include "device/geolocation/location_provider.h" 17 #include "device/geolocation/location_provider.h"
18 #include "device/geolocation/geoposition.h" 18 #include "device/geolocation/geoposition.h"
19 #include "net/base/net_errors.h" 19 #include "net/base/net_errors.h"
20 20
21 namespace blimp { 21 namespace blimp {
22 namespace engine { 22 namespace engine {
23 namespace { 23 namespace {
24 24
25 class BlimpGeolocationDelegate : public device::GeolocationDelegate { 25 class BlimpGeolocationDelegate : public device::GeolocationDelegate {
26 public: 26 public:
27 explicit BlimpGeolocationDelegate( 27 explicit BlimpGeolocationDelegate(
28 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate) { 28 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate) {
29 feature_delegate_ = feature_delegate; 29 feature_delegate_ = feature_delegate;
30 feature_task_runner_ = base::ThreadTaskRunnerHandle::Get();
30 } 31 }
31 32
32 bool UseNetworkLocationProviders() final { return false; } 33 bool UseNetworkLocationProviders() final { return false; }
33 34
34 std::unique_ptr<device::LocationProvider> OverrideSystemLocationProvider() 35 std::unique_ptr<device::LocationProvider> OverrideSystemLocationProvider()
35 final { 36 final {
36 return base::MakeUnique<BlimpLocationProvider>(feature_delegate_); 37 return base::MakeUnique<BlimpLocationProvider>(feature_delegate_,
38 feature_task_runner_);
37 } 39 }
38 40
39 private: 41 private:
40 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate_; 42 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate_;
43 scoped_refptr<base::SingleThreadTaskRunner> feature_task_runner_;
41 44
42 DISALLOW_COPY_AND_ASSIGN(BlimpGeolocationDelegate); 45 DISALLOW_COPY_AND_ASSIGN(BlimpGeolocationDelegate);
43 }; 46 };
44 47
45 device::Geoposition::ErrorCode ConvertErrorCode( 48 device::Geoposition::ErrorCode ConvertErrorCode(
46 const GeolocationErrorMessage::ErrorCode& error_code) { 49 const GeolocationErrorMessage::ErrorCode& error_code) {
47 switch (error_code) { 50 switch (error_code) {
48 case GeolocationErrorMessage::PERMISSION_DENIED: 51 case GeolocationErrorMessage::PERMISSION_DENIED:
49 return device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; 52 return device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED;
50 case GeolocationErrorMessage::POSITION_UNAVAILABLE: 53 case GeolocationErrorMessage::POSITION_UNAVAILABLE:
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 case GeolocationMessage::TYPE_NOT_SET: 118 case GeolocationMessage::TYPE_NOT_SET:
116 result = net::ERR_UNEXPECTED; 119 result = net::ERR_UNEXPECTED;
117 } 120 }
118 if (!callback.is_null()) { 121 if (!callback.is_null()) {
119 callback.Run(result); 122 callback.Run(result);
120 } 123 }
121 } 124 }
122 125
123 void EngineGeolocationFeature::NotifyCallback( 126 void EngineGeolocationFeature::NotifyCallback(
124 const device::Geoposition& position) { 127 const device::Geoposition& position) {
125 callback_task_runner_->PostTask( 128 geoposition_received_callback_.Run(position);
126 FROM_HERE, base::Bind(geoposition_received_callback_, position));
127 } 129 }
128 130
129 void EngineGeolocationFeature::RequestAccuracy( 131 void EngineGeolocationFeature::RequestAccuracy(
130 GeolocationSetInterestLevelMessage::Level level) { 132 GeolocationSetInterestLevelMessage::Level level) {
131 GeolocationMessage* geolocation_message = nullptr; 133 GeolocationMessage* geolocation_message = nullptr;
132 std::unique_ptr<BlimpMessage> blimp_message = 134 std::unique_ptr<BlimpMessage> blimp_message =
133 CreateBlimpMessage(&geolocation_message); 135 CreateBlimpMessage(&geolocation_message);
134 136
135 GeolocationSetInterestLevelMessage* geolocation_interest = 137 GeolocationSetInterestLevelMessage* geolocation_interest =
136 geolocation_message->mutable_set_interest_level(); 138 geolocation_message->mutable_set_interest_level();
(...skipping 10 matching lines...) Expand all
147 149
148 geolocation_message->mutable_request_refresh(); 150 geolocation_message->mutable_request_refresh();
149 151
150 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), 152 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
151 net::CompletionCallback()); 153 net::CompletionCallback());
152 } 154 }
153 155
154 void EngineGeolocationFeature::SetUpdateCallback( 156 void EngineGeolocationFeature::SetUpdateCallback(
155 const GeopositionReceivedCallback& callback) { 157 const GeopositionReceivedCallback& callback) {
156 geoposition_received_callback_ = callback; 158 geoposition_received_callback_ = callback;
157
158 // Set |callback_task_runner_| to run on the current thread.
159 callback_task_runner_ = base::ThreadTaskRunnerHandle::Get();
160 } 159 }
161 160
162 } // namespace engine 161 } // namespace engine
163 } // namespace blimp 162 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698