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