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

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: Addresses nyquist's #46 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) 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(GeolocationDelegate* delegate)
33 GeolocationDelegate* delegate)
34 : delegate_(delegate), 33 : delegate_(delegate),
35 position_provider_(nullptr), 34 position_provider_(nullptr),
36 is_permission_granted_(false), 35 is_permission_granted_(false),
37 is_running_(false) {} 36 is_running_(false) {}
38 37
39 LocationArbitratorImpl::~LocationArbitratorImpl() {} 38 LocationArbitrator::~LocationArbitrator() {}
40 39
41 GURL LocationArbitratorImpl::DefaultNetworkProviderURL() { 40 GURL LocationArbitrator::DefaultNetworkProviderURL() {
42 return GURL(kDefaultNetworkProviderUrl); 41 return GURL(kDefaultNetworkProviderUrl);
43 } 42 }
44 43
45 bool LocationArbitratorImpl::HasPermissionBeenGrantedForTest() const { 44 bool LocationArbitrator::HasPermissionBeenGrantedForTest() const {
46 return is_permission_granted_; 45 return is_permission_granted_;
47 } 46 }
48 47
49 void LocationArbitratorImpl::OnPermissionGranted() { 48 void LocationArbitrator::OnPermissionGranted() {
50 is_permission_granted_ = true; 49 is_permission_granted_ = true;
51 for (const auto& provider : providers_) 50 for (const auto& provider : providers_)
52 provider->OnPermissionGranted(); 51 provider->OnPermissionGranted();
53 } 52 }
54 53
55 bool LocationArbitratorImpl::StartProvider(bool enable_high_accuracy) { 54 bool LocationArbitrator::StartProvider(bool enable_high_accuracy) {
56 // Stash options as OnAccessTokenStoresLoaded has not yet been called. 55 // Stash options as OnAccessTokenStoresLoaded has not yet been called.
57 is_running_ = true; 56 is_running_ = true;
58 enable_high_accuracy_ = enable_high_accuracy; 57 enable_high_accuracy_ = enable_high_accuracy;
59 58
60 if (providers_.empty()) { 59 if (providers_.empty()) {
61 RegisterSystemProvider(); 60 RegisterSystemProvider();
62 61
63 const scoped_refptr<AccessTokenStore> access_token_store = 62 const scoped_refptr<AccessTokenStore> access_token_store =
64 GetAccessTokenStore(); 63 GetAccessTokenStore();
65 if (access_token_store && delegate_->UseNetworkLocationProviders()) { 64 if (access_token_store && delegate_->UseNetworkLocationProviders()) {
66 DCHECK(DefaultNetworkProviderURL().is_valid()); 65 DCHECK(DefaultNetworkProviderURL().is_valid());
67 token_store_callback_.Reset( 66 token_store_callback_.Reset(
68 base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded, 67 base::Bind(&LocationArbitrator::OnAccessTokenStoresLoaded,
69 base::Unretained(this))); 68 base::Unretained(this)));
70 access_token_store->LoadAccessTokens(token_store_callback_.callback()); 69 access_token_store->LoadAccessTokens(token_store_callback_.callback());
71 return true; 70 return true;
72 } 71 }
73 } 72 }
74 return DoStartProviders(); 73 return DoStartProviders();
75 } 74 }
76 75
77 bool LocationArbitratorImpl::DoStartProviders() { 76 bool LocationArbitrator::DoStartProviders() {
78 if (providers_.empty()) { 77 if (providers_.empty()) {
79 // If no providers are available, we report an error to avoid 78 // If no providers are available, we report an error to avoid
80 // callers waiting indefinitely for a reply. 79 // callers waiting indefinitely for a reply.
81 Geoposition position; 80 Geoposition position;
82 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; 81 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED;
83 arbitrator_update_callback_.Run(this, position); 82 arbitrator_update_callback_.Run(this, position);
84 return false; 83 return false;
85 } 84 }
86 bool started = false; 85 bool started = false;
87 for (const auto& provider : providers_) { 86 for (const auto& provider : providers_) {
88 started = provider->StartProvider(enable_high_accuracy_) || started; 87 started = provider->StartProvider(enable_high_accuracy_) || started;
89 } 88 }
90 return started; 89 return started;
91 } 90 }
92 91
93 void LocationArbitratorImpl::StopProvider() { 92 void LocationArbitrator::StopProvider() {
94 // Reset the reference location state (provider+position) 93 // Reset the reference location state (provider+position)
95 // so that future starts use fresh locations from 94 // so that future starts use fresh locations from
96 // the newly constructed providers. 95 // the newly constructed providers.
97 position_provider_ = nullptr; 96 position_provider_ = nullptr;
98 position_ = Geoposition(); 97 position_ = Geoposition();
99 98
100 providers_.clear(); 99 providers_.clear();
101 is_running_ = false; 100 is_running_ = false;
102 } 101 }
103 102
104 void LocationArbitratorImpl::OnAccessTokenStoresLoaded( 103 void LocationArbitrator::OnAccessTokenStoresLoaded(
105 AccessTokenStore::AccessTokenMap access_token_map, 104 AccessTokenStore::AccessTokenMap access_token_map,
106 const scoped_refptr<net::URLRequestContextGetter>& context_getter) { 105 const scoped_refptr<net::URLRequestContextGetter>& context_getter) {
107 // If there are no access tokens, boot strap it with the default server URL. 106 // If there are no access tokens, boot strap it with the default server URL.
108 if (access_token_map.empty()) 107 if (access_token_map.empty())
109 access_token_map[DefaultNetworkProviderURL()]; 108 access_token_map[DefaultNetworkProviderURL()];
110 for (const auto& entry : access_token_map) { 109 for (const auto& entry : access_token_map) {
111 RegisterProvider(NewNetworkLocationProvider( 110 RegisterProvider(NewNetworkLocationProvider(
112 GetAccessTokenStore(), context_getter, entry.first, entry.second)); 111 GetAccessTokenStore(), context_getter, entry.first, entry.second));
113 } 112 }
114 DoStartProviders(); 113 DoStartProviders();
115 } 114 }
116 115
117 void LocationArbitratorImpl::RegisterProvider( 116 void LocationArbitrator::RegisterProvider(
118 std::unique_ptr<LocationProvider> provider) { 117 std::unique_ptr<LocationProvider> provider) {
119 if (!provider) 118 if (!provider)
120 return; 119 return;
121 provider->SetUpdateCallback(base::Bind( 120 provider->SetUpdateCallback(base::Bind(&LocationArbitrator::OnLocationUpdate,
122 &LocationArbitratorImpl::OnLocationUpdate, base::Unretained(this))); 121 base::Unretained(this)));
123 if (is_permission_granted_) 122 if (is_permission_granted_)
124 provider->OnPermissionGranted(); 123 provider->OnPermissionGranted();
125 providers_.push_back(std::move(provider)); 124 providers_.push_back(std::move(provider));
126 } 125 }
127 126
128 void LocationArbitratorImpl::RegisterSystemProvider() { 127 void LocationArbitrator::RegisterSystemProvider() {
129 std::unique_ptr<LocationProvider> provider = 128 std::unique_ptr<LocationProvider> provider =
130 delegate_->OverrideSystemLocationProvider(); 129 delegate_->OverrideSystemLocationProvider();
131 if (!provider) 130 if (!provider)
132 provider = NewSystemLocationProvider(); 131 provider = NewSystemLocationProvider();
133 RegisterProvider(std::move(provider)); 132 RegisterProvider(std::move(provider));
134 } 133 }
135 134
136 void LocationArbitratorImpl::OnLocationUpdate(const LocationProvider* provider, 135 void LocationArbitrator::OnLocationUpdate(const LocationProvider* provider,
137 const Geoposition& new_position) { 136 const Geoposition& new_position) {
138 DCHECK(new_position.Validate() || 137 DCHECK(new_position.Validate() ||
139 new_position.error_code != Geoposition::ERROR_CODE_NONE); 138 new_position.error_code != Geoposition::ERROR_CODE_NONE);
140 if (!IsNewPositionBetter(position_, new_position, 139 if (!IsNewPositionBetter(position_, new_position,
141 provider == position_provider_)) 140 provider == position_provider_))
142 return; 141 return;
143 position_provider_ = provider; 142 position_provider_ = provider;
144 position_ = new_position; 143 position_ = new_position;
145 arbitrator_update_callback_.Run(this, position_); 144 arbitrator_update_callback_.Run(this, position_);
146 } 145 }
147 146
148 const Geoposition& LocationArbitratorImpl::GetPosition() { 147 const Geoposition& LocationArbitrator::GetPosition() {
149 return position_; 148 return position_;
150 } 149 }
151 150
152 void LocationArbitratorImpl::SetUpdateCallback( 151 void LocationArbitrator::SetUpdateCallback(
153 const LocationProviderUpdateCallback& callback) { 152 const LocationProviderUpdateCallback& callback) {
154 DCHECK(!callback.is_null()); 153 DCHECK(!callback.is_null());
155 arbitrator_update_callback_ = callback; 154 arbitrator_update_callback_ = callback;
156 } 155 }
157 156
158 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::NewAccessTokenStore() { 157 scoped_refptr<AccessTokenStore> LocationArbitrator::NewAccessTokenStore() {
159 return delegate_->CreateAccessTokenStore(); 158 return delegate_->CreateAccessTokenStore();
160 } 159 }
161 160
162 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::GetAccessTokenStore() { 161 scoped_refptr<AccessTokenStore> LocationArbitrator::GetAccessTokenStore() {
163 if (!access_token_store_) 162 if (!access_token_store_)
164 access_token_store_ = NewAccessTokenStore(); 163 access_token_store_ = NewAccessTokenStore();
165 return access_token_store_; 164 return access_token_store_;
166 } 165 }
167 166
168 std::unique_ptr<LocationProvider> 167 std::unique_ptr<LocationProvider>
169 LocationArbitratorImpl::NewNetworkLocationProvider( 168 LocationArbitrator::NewNetworkLocationProvider(
170 const scoped_refptr<AccessTokenStore>& access_token_store, 169 const scoped_refptr<AccessTokenStore>& access_token_store,
171 const scoped_refptr<net::URLRequestContextGetter>& context, 170 const scoped_refptr<net::URLRequestContextGetter>& context,
172 const GURL& url, 171 const GURL& url,
173 const base::string16& access_token) { 172 const base::string16& access_token) {
174 #if defined(OS_ANDROID) 173 #if defined(OS_ANDROID)
175 // Android uses its own SystemLocationProvider. 174 // Android uses its own SystemLocationProvider.
176 return nullptr; 175 return nullptr;
177 #else 176 #else
178 return base::MakeUnique<NetworkLocationProvider>(access_token_store, context, 177 return base::MakeUnique<NetworkLocationProvider>(access_token_store, context,
179 url, access_token); 178 url, access_token);
180 #endif 179 #endif
181 } 180 }
182 181
183 std::unique_ptr<LocationProvider> 182 std::unique_ptr<LocationProvider>
184 LocationArbitratorImpl::NewSystemLocationProvider() { 183 LocationArbitrator::NewSystemLocationProvider() {
185 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) 184 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
186 return nullptr; 185 return nullptr;
187 #else 186 #else
188 return device::NewSystemLocationProvider(); 187 return device::NewSystemLocationProvider();
189 #endif 188 #endif
190 } 189 }
191 190
192 base::Time LocationArbitratorImpl::GetTimeNow() const { 191 base::Time LocationArbitrator::GetTimeNow() const {
193 return base::Time::Now(); 192 return base::Time::Now();
194 } 193 }
195 194
196 bool LocationArbitratorImpl::IsNewPositionBetter( 195 bool LocationArbitrator::IsNewPositionBetter(const Geoposition& old_position,
197 const Geoposition& old_position, 196 const Geoposition& new_position,
198 const Geoposition& new_position, 197 bool from_same_provider) const {
199 bool from_same_provider) const {
200 // Updates location_info if it's better than what we currently have, 198 // Updates location_info if it's better than what we currently have,
201 // or if it's a newer update from the same provider. 199 // or if it's a newer update from the same provider.
202 if (!old_position.Validate()) { 200 if (!old_position.Validate()) {
203 // Older location wasn't locked. 201 // Older location wasn't locked.
204 return true; 202 return true;
205 } 203 }
206 if (new_position.Validate()) { 204 if (new_position.Validate()) {
207 // New location is locked, let's check if it's any better. 205 // New location is locked, let's check if it's any better.
208 if (old_position.accuracy >= new_position.accuracy) { 206 if (old_position.accuracy >= new_position.accuracy) {
209 // Accuracy is better. 207 // Accuracy is better.
210 return true; 208 return true;
211 } else if (from_same_provider) { 209 } else if (from_same_provider) {
212 // Same provider, fresher location. 210 // Same provider, fresher location.
213 return true; 211 return true;
214 } else if ((GetTimeNow() - old_position.timestamp).InMilliseconds() > 212 } else if ((GetTimeNow() - old_position.timestamp).InMilliseconds() >
215 kFixStaleTimeoutMilliseconds) { 213 kFixStaleTimeoutMilliseconds) {
216 // Existing fix is stale. 214 // Existing fix is stale.
217 return true; 215 return true;
218 } 216 }
219 } 217 }
220 return false; 218 return false;
221 } 219 }
222 220
223 } // namespace device 221 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698