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