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