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

Side by Side Diff: device/geolocation/location_arbitrator.cc

Issue 2249283003: Hooks together Geolocation Feature in the Client and Engine. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lai
Patch Set: Put TestingLocationArbitrator out of anonymous namespace. 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
« no previous file with comments | « device/geolocation/location_arbitrator.h ('k') | device/geolocation/location_arbitrator_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "device/geolocation/location_arbitrator_impl.h" 5 #include "device/geolocation/location_arbitrator.h"
6 6
7 #include <map> 7 #include <map>
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "device/geolocation/access_token_store.h" 15 #include "device/geolocation/access_token_store.h"
16 #include "device/geolocation/geolocation_delegate.h" 16 #include "device/geolocation/geolocation_delegate.h"
17 #include "device/geolocation/network_location_provider.h" 17 #include "device/geolocation/network_location_provider.h"
18 #include "url/gurl.h" 18 #include "url/gurl.h"
19 19
20 namespace device { 20 namespace device {
21 namespace { 21 namespace {
22 22
23 const char* kDefaultNetworkProviderUrl = 23 const char* kDefaultNetworkProviderUrl =
24 "https://www.googleapis.com/geolocation/v1/geolocate"; 24 "https://www.googleapis.com/geolocation/v1/geolocate";
25 } // namespace 25 } // namespace
26 26
27 // To avoid oscillations, set this to twice the expected update interval of a 27 // To avoid oscillations, set this to twice the expected update interval of a
28 // a GPS-type location provider (in case it misses a beat) plus a little. 28 // a GPS-type location provider (in case it misses a beat) plus a little.
29 const int64_t LocationArbitratorImpl::kFixStaleTimeoutMilliseconds = 29 const int64_t LocationArbitrator::kFixStaleTimeoutMilliseconds =
30 11 * base::Time::kMillisecondsPerSecond; 30 11 * base::Time::kMillisecondsPerSecond;
31 31
32 LocationArbitratorImpl::LocationArbitratorImpl( 32 LocationArbitrator::LocationArbitrator(
33 GeolocationDelegate* delegate) 33 std::unique_ptr<GeolocationDelegate> delegate)
34 : delegate_(delegate), 34 : delegate_(std::move(delegate)),
35 position_provider_(nullptr), 35 position_provider_(nullptr),
36 is_permission_granted_(false), 36 is_permission_granted_(false),
37 is_running_(false) {} 37 is_running_(false) {}
38 38
39 LocationArbitratorImpl::~LocationArbitratorImpl() {} 39 LocationArbitrator::~LocationArbitrator() {}
40 40
41 GURL LocationArbitratorImpl::DefaultNetworkProviderURL() { 41 GURL LocationArbitrator::DefaultNetworkProviderURL() {
42 return GURL(kDefaultNetworkProviderUrl); 42 return GURL(kDefaultNetworkProviderUrl);
43 } 43 }
44 44
45 bool LocationArbitratorImpl::HasPermissionBeenGrantedForTest() const { 45 bool LocationArbitrator::HasPermissionBeenGrantedForTest() const {
46 return is_permission_granted_; 46 return is_permission_granted_;
47 } 47 }
48 48
49 void LocationArbitratorImpl::OnPermissionGranted() { 49 void LocationArbitrator::OnPermissionGranted() {
50 is_permission_granted_ = true; 50 is_permission_granted_ = true;
51 for (const auto& provider : providers_) 51 for (const auto& provider : providers_)
52 provider->OnPermissionGranted(); 52 provider->OnPermissionGranted();
53 } 53 }
54 54
55 bool LocationArbitratorImpl::StartProvider(bool enable_high_accuracy) { 55 bool LocationArbitrator::StartProvider(bool enable_high_accuracy) {
56 // Stash options as OnAccessTokenStoresLoaded has not yet been called. 56 // Stash options as OnAccessTokenStoresLoaded has not yet been called.
57 is_running_ = true; 57 is_running_ = true;
58 enable_high_accuracy_ = enable_high_accuracy; 58 enable_high_accuracy_ = enable_high_accuracy;
59 59
60 if (providers_.empty()) { 60 if (providers_.empty()) {
61 RegisterSystemProvider(); 61 RegisterSystemProvider();
62 62
63 const scoped_refptr<AccessTokenStore> access_token_store = 63 const scoped_refptr<AccessTokenStore> access_token_store =
64 GetAccessTokenStore(); 64 GetAccessTokenStore();
65 if (access_token_store && delegate_->UseNetworkLocationProviders()) { 65 if (access_token_store && delegate_->UseNetworkLocationProviders()) {
66 DCHECK(DefaultNetworkProviderURL().is_valid()); 66 DCHECK(DefaultNetworkProviderURL().is_valid());
67 token_store_callback_.Reset( 67 token_store_callback_.Reset(
68 base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded, 68 base::Bind(&LocationArbitrator::OnAccessTokenStoresLoaded,
69 base::Unretained(this))); 69 base::Unretained(this)));
70 access_token_store->LoadAccessTokens(token_store_callback_.callback()); 70 access_token_store->LoadAccessTokens(token_store_callback_.callback());
71 return true; 71 return true;
72 } 72 }
73 } 73 }
74 return DoStartProviders(); 74 return DoStartProviders();
75 } 75 }
76 76
77 bool LocationArbitratorImpl::DoStartProviders() { 77 bool LocationArbitrator::DoStartProviders() {
78 if (providers_.empty()) { 78 if (providers_.empty()) {
79 // If no providers are available, we report an error to avoid 79 // If no providers are available, we report an error to avoid
80 // callers waiting indefinitely for a reply. 80 // callers waiting indefinitely for a reply.
81 Geoposition position; 81 Geoposition position;
82 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; 82 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED;
83 arbitrator_update_callback_.Run(this, position); 83 arbitrator_update_callback_.Run(this, position);
84 return false; 84 return false;
85 } 85 }
86 bool started = false; 86 bool started = false;
87 for (const auto& provider : providers_) { 87 for (const auto& provider : providers_) {
88 started = provider->StartProvider(enable_high_accuracy_) || started; 88 started = provider->StartProvider(enable_high_accuracy_) || started;
89 } 89 }
90 return started; 90 return started;
91 } 91 }
92 92
93 void LocationArbitratorImpl::StopProvider() { 93 void LocationArbitrator::StopProvider() {
94 // Reset the reference location state (provider+position) 94 // Reset the reference location state (provider+position)
95 // so that future starts use fresh locations from 95 // so that future starts use fresh locations from
96 // the newly constructed providers. 96 // the newly constructed providers.
97 position_provider_ = nullptr; 97 position_provider_ = nullptr;
98 position_ = Geoposition(); 98 position_ = Geoposition();
99 99
100 providers_.clear(); 100 providers_.clear();
101 is_running_ = false; 101 is_running_ = false;
102 } 102 }
103 103
104 void LocationArbitratorImpl::OnAccessTokenStoresLoaded( 104 void LocationArbitrator::OnAccessTokenStoresLoaded(
105 AccessTokenStore::AccessTokenMap access_token_map, 105 AccessTokenStore::AccessTokenMap access_token_map,
106 const scoped_refptr<net::URLRequestContextGetter>& context_getter) { 106 const scoped_refptr<net::URLRequestContextGetter>& context_getter) {
107 // If there are no access tokens, boot strap it with the default server URL. 107 // If there are no access tokens, boot strap it with the default server URL.
108 if (access_token_map.empty()) 108 if (access_token_map.empty())
109 access_token_map[DefaultNetworkProviderURL()]; 109 access_token_map[DefaultNetworkProviderURL()];
110 for (const auto& entry : access_token_map) { 110 for (const auto& entry : access_token_map) {
111 RegisterProvider(NewNetworkLocationProvider( 111 RegisterProvider(NewNetworkLocationProvider(
112 GetAccessTokenStore(), context_getter, entry.first, entry.second)); 112 GetAccessTokenStore(), context_getter, entry.first, entry.second));
113 } 113 }
114 DoStartProviders(); 114 DoStartProviders();
115 } 115 }
116 116
117 void LocationArbitratorImpl::RegisterProvider( 117 void LocationArbitrator::RegisterProvider(
118 std::unique_ptr<LocationProvider> provider) { 118 std::unique_ptr<LocationProvider> provider) {
119 if (!provider) 119 if (!provider)
120 return; 120 return;
121 provider->SetUpdateCallback(base::Bind( 121 provider->SetUpdateCallback(base::Bind(&LocationArbitrator::OnLocationUpdate,
122 &LocationArbitratorImpl::OnLocationUpdate, base::Unretained(this))); 122 base::Unretained(this)));
123 if (is_permission_granted_) 123 if (is_permission_granted_)
124 provider->OnPermissionGranted(); 124 provider->OnPermissionGranted();
125 providers_.push_back(std::move(provider)); 125 providers_.push_back(std::move(provider));
126 } 126 }
127 127
128 void LocationArbitratorImpl::RegisterSystemProvider() { 128 void LocationArbitrator::RegisterSystemProvider() {
129 std::unique_ptr<LocationProvider> provider = 129 std::unique_ptr<LocationProvider> provider =
130 delegate_->OverrideSystemLocationProvider(); 130 delegate_->OverrideSystemLocationProvider();
131 if (!provider) 131 if (!provider)
132 provider = NewSystemLocationProvider(); 132 provider = NewSystemLocationProvider();
133 RegisterProvider(std::move(provider)); 133 RegisterProvider(std::move(provider));
134 } 134 }
135 135
136 void LocationArbitratorImpl::OnLocationUpdate(const LocationProvider* provider, 136 void LocationArbitrator::OnLocationUpdate(const LocationProvider* provider,
137 const Geoposition& new_position) { 137 const Geoposition& new_position) {
138 DCHECK(new_position.Validate() || 138 DCHECK(new_position.Validate() ||
139 new_position.error_code != Geoposition::ERROR_CODE_NONE); 139 new_position.error_code != Geoposition::ERROR_CODE_NONE);
140 if (!IsNewPositionBetter(position_, new_position, 140 if (!IsNewPositionBetter(position_, new_position,
141 provider == position_provider_)) 141 provider == position_provider_))
142 return; 142 return;
143 position_provider_ = provider; 143 position_provider_ = provider;
144 position_ = new_position; 144 position_ = new_position;
145 arbitrator_update_callback_.Run(this, position_); 145 arbitrator_update_callback_.Run(this, position_);
146 } 146 }
147 147
148 const Geoposition& LocationArbitratorImpl::GetPosition() { 148 const Geoposition& LocationArbitrator::GetPosition() {
149 return position_; 149 return position_;
150 } 150 }
151 151
152 void LocationArbitratorImpl::SetUpdateCallback( 152 void LocationArbitrator::SetUpdateCallback(
153 const LocationProviderUpdateCallback& callback) { 153 const LocationProviderUpdateCallback& callback) {
154 DCHECK(!callback.is_null()); 154 DCHECK(!callback.is_null());
155 arbitrator_update_callback_ = callback; 155 arbitrator_update_callback_ = callback;
156 } 156 }
157 157
158 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::NewAccessTokenStore() { 158 scoped_refptr<AccessTokenStore> LocationArbitrator::NewAccessTokenStore() {
159 return delegate_->CreateAccessTokenStore(); 159 return delegate_->CreateAccessTokenStore();
160 } 160 }
161 161
162 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::GetAccessTokenStore() { 162 scoped_refptr<AccessTokenStore> LocationArbitrator::GetAccessTokenStore() {
163 if (!access_token_store_) 163 if (!access_token_store_)
164 access_token_store_ = NewAccessTokenStore(); 164 access_token_store_ = NewAccessTokenStore();
165 return access_token_store_; 165 return access_token_store_;
166 } 166 }
167 167
168 std::unique_ptr<LocationProvider> 168 std::unique_ptr<LocationProvider>
169 LocationArbitratorImpl::NewNetworkLocationProvider( 169 LocationArbitrator::NewNetworkLocationProvider(
170 const scoped_refptr<AccessTokenStore>& access_token_store, 170 const scoped_refptr<AccessTokenStore>& access_token_store,
171 const scoped_refptr<net::URLRequestContextGetter>& context, 171 const scoped_refptr<net::URLRequestContextGetter>& context,
172 const GURL& url, 172 const GURL& url,
173 const base::string16& access_token) { 173 const base::string16& access_token) {
174 #if defined(OS_ANDROID) 174 #if defined(OS_ANDROID)
175 // Android uses its own SystemLocationProvider. 175 // Android uses its own SystemLocationProvider.
176 return nullptr; 176 return nullptr;
177 #else 177 #else
178 return base::MakeUnique<NetworkLocationProvider>(access_token_store, context, 178 return base::MakeUnique<NetworkLocationProvider>(access_token_store, context,
179 url, access_token); 179 url, access_token);
180 #endif 180 #endif
181 } 181 }
182 182
183 std::unique_ptr<LocationProvider> 183 std::unique_ptr<LocationProvider>
184 LocationArbitratorImpl::NewSystemLocationProvider() { 184 LocationArbitrator::NewSystemLocationProvider() {
185 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) 185 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
186 return nullptr; 186 return nullptr;
187 #else 187 #else
188 return device::NewSystemLocationProvider(); 188 return device::NewSystemLocationProvider();
189 #endif 189 #endif
190 } 190 }
191 191
192 base::Time LocationArbitratorImpl::GetTimeNow() const { 192 base::Time LocationArbitrator::GetTimeNow() const {
193 return base::Time::Now(); 193 return base::Time::Now();
194 } 194 }
195 195
196 bool LocationArbitratorImpl::IsNewPositionBetter( 196 bool LocationArbitrator::IsNewPositionBetter(const Geoposition& old_position,
197 const Geoposition& old_position, 197 const Geoposition& new_position,
198 const Geoposition& new_position, 198 bool from_same_provider) const {
199 bool from_same_provider) const {
200 // Updates location_info if it's better than what we currently have, 199 // Updates location_info if it's better than what we currently have,
201 // or if it's a newer update from the same provider. 200 // or if it's a newer update from the same provider.
202 if (!old_position.Validate()) { 201 if (!old_position.Validate()) {
203 // Older location wasn't locked. 202 // Older location wasn't locked.
204 return true; 203 return true;
205 } 204 }
206 if (new_position.Validate()) { 205 if (new_position.Validate()) {
207 // New location is locked, let's check if it's any better. 206 // New location is locked, let's check if it's any better.
208 if (old_position.accuracy >= new_position.accuracy) { 207 if (old_position.accuracy >= new_position.accuracy) {
209 // Accuracy is better. 208 // Accuracy is better.
210 return true; 209 return true;
211 } else if (from_same_provider) { 210 } else if (from_same_provider) {
212 // Same provider, fresher location. 211 // Same provider, fresher location.
213 return true; 212 return true;
214 } else if ((GetTimeNow() - old_position.timestamp).InMilliseconds() > 213 } else if ((GetTimeNow() - old_position.timestamp).InMilliseconds() >
215 kFixStaleTimeoutMilliseconds) { 214 kFixStaleTimeoutMilliseconds) {
216 // Existing fix is stale. 215 // Existing fix is stale.
217 return true; 216 return true;
218 } 217 }
219 } 218 }
220 return false; 219 return false;
221 } 220 }
222 221
223 } // namespace device 222 } // namespace device
OLDNEW
« no previous file with comments | « device/geolocation/location_arbitrator.h ('k') | device/geolocation/location_arbitrator_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698