| OLD | NEW |
| 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_impl.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> |
| 9 #include <utility> |
| 8 | 10 |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 12 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 13 #include "device/geolocation/access_token_store.h" | 15 #include "device/geolocation/access_token_store.h" |
| 14 #include "device/geolocation/geolocation_delegate.h" | 16 #include "device/geolocation/geolocation_delegate.h" |
| 15 #include "device/geolocation/network_location_provider.h" | 17 #include "device/geolocation/network_location_provider.h" |
| 16 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 17 | 19 |
| 18 namespace device { | 20 namespace device { |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 const char* kDefaultNetworkProviderUrl = | 23 const char* kDefaultNetworkProviderUrl = |
| 22 "https://www.googleapis.com/geolocation/v1/geolocate"; | 24 "https://www.googleapis.com/geolocation/v1/geolocate"; |
| 23 } // namespace | 25 } // namespace |
| 24 | 26 |
| 25 // 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 |
| 26 // 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. |
| 27 const int64_t LocationArbitratorImpl::kFixStaleTimeoutMilliseconds = | 29 const int64_t LocationArbitratorImpl::kFixStaleTimeoutMilliseconds = |
| 28 11 * base::Time::kMillisecondsPerSecond; | 30 11 * base::Time::kMillisecondsPerSecond; |
| 29 | 31 |
| 30 LocationArbitratorImpl::LocationArbitratorImpl( | 32 LocationArbitratorImpl::LocationArbitratorImpl( |
| 31 const LocationUpdateCallback& callback, | |
| 32 GeolocationDelegate* delegate) | 33 GeolocationDelegate* delegate) |
| 33 : delegate_(delegate), | 34 : delegate_(delegate), |
| 34 arbitrator_update_callback_(callback), | |
| 35 provider_update_callback_( | |
| 36 base::Bind(&LocationArbitratorImpl::OnLocationUpdate, | |
| 37 base::Unretained(this))), | |
| 38 position_provider_(nullptr), | 35 position_provider_(nullptr), |
| 39 is_permission_granted_(false), | 36 is_permission_granted_(false), |
| 40 is_running_(false) {} | 37 is_running_(false) {} |
| 41 | 38 |
| 42 LocationArbitratorImpl::~LocationArbitratorImpl() {} | 39 LocationArbitratorImpl::~LocationArbitratorImpl() {} |
| 43 | 40 |
| 44 GURL LocationArbitratorImpl::DefaultNetworkProviderURL() { | 41 GURL LocationArbitratorImpl::DefaultNetworkProviderURL() { |
| 45 return GURL(kDefaultNetworkProviderUrl); | 42 return GURL(kDefaultNetworkProviderUrl); |
| 46 } | 43 } |
| 47 | 44 |
| 45 bool LocationArbitratorImpl::HasPermissionBeenGrantedForTest() const { |
| 46 return is_permission_granted_; |
| 47 } |
| 48 |
| 48 void LocationArbitratorImpl::OnPermissionGranted() { | 49 void LocationArbitratorImpl::OnPermissionGranted() { |
| 49 is_permission_granted_ = true; | 50 is_permission_granted_ = true; |
| 50 for (const auto& provider : providers_) | 51 for (const auto& provider : providers_) |
| 51 provider->OnPermissionGranted(); | 52 provider->OnPermissionGranted(); |
| 52 } | 53 } |
| 53 | 54 |
| 54 void LocationArbitratorImpl::StartProviders(bool enable_high_accuracy) { | 55 bool LocationArbitratorImpl::StartProvider(bool enable_high_accuracy) { |
| 55 // Stash options as OnAccessTokenStoresLoaded has not yet been called. | 56 // Stash options as OnAccessTokenStoresLoaded has not yet been called. |
| 56 is_running_ = true; | 57 is_running_ = true; |
| 57 enable_high_accuracy_ = enable_high_accuracy; | 58 enable_high_accuracy_ = enable_high_accuracy; |
| 58 | 59 |
| 59 if (providers_.empty()) { | 60 if (providers_.empty()) { |
| 60 RegisterSystemProvider(); | 61 RegisterSystemProvider(); |
| 61 | 62 |
| 62 const scoped_refptr<AccessTokenStore> access_token_store = | 63 const scoped_refptr<AccessTokenStore> access_token_store = |
| 63 GetAccessTokenStore(); | 64 GetAccessTokenStore(); |
| 64 if (access_token_store && delegate_->UseNetworkLocationProviders()) { | 65 if (access_token_store && delegate_->UseNetworkLocationProviders()) { |
| 65 DCHECK(DefaultNetworkProviderURL().is_valid()); | 66 DCHECK(DefaultNetworkProviderURL().is_valid()); |
| 66 token_store_callback_.Reset( | 67 token_store_callback_.Reset( |
| 67 base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded, | 68 base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded, |
| 68 base::Unretained(this))); | 69 base::Unretained(this))); |
| 69 access_token_store->LoadAccessTokens(token_store_callback_.callback()); | 70 access_token_store->LoadAccessTokens(token_store_callback_.callback()); |
| 70 return; | 71 return true; |
| 71 } | 72 } |
| 72 } | 73 } |
| 73 DoStartProviders(); | 74 return DoStartProviders(); |
| 74 } | 75 } |
| 75 | 76 |
| 76 void LocationArbitratorImpl::DoStartProviders() { | 77 bool LocationArbitratorImpl::DoStartProviders() { |
| 77 if (providers_.empty()) { | 78 if (providers_.empty()) { |
| 78 // If no providers are available, we report an error to avoid | 79 // If no providers are available, we report an error to avoid |
| 79 // callers waiting indefinitely for a reply. | 80 // callers waiting indefinitely for a reply. |
| 80 Geoposition position; | 81 Geoposition position; |
| 81 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; | 82 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; |
| 82 arbitrator_update_callback_.Run(position); | 83 arbitrator_update_callback_.Run(this, position); |
| 83 return; | 84 return false; |
| 84 } | 85 } |
| 85 for (const auto& provider : providers_) | 86 bool started = false; |
| 86 provider->StartProvider(enable_high_accuracy_); | 87 for (const auto& provider : providers_) { |
| 88 started = provider->StartProvider(enable_high_accuracy_) || started; |
| 89 } |
| 90 return started; |
| 87 } | 91 } |
| 88 | 92 |
| 89 void LocationArbitratorImpl::StopProviders() { | 93 void LocationArbitratorImpl::StopProvider() { |
| 90 // Reset the reference location state (provider+position) | 94 // Reset the reference location state (provider+position) |
| 91 // so that future starts use fresh locations from | 95 // so that future starts use fresh locations from |
| 92 // the newly constructed providers. | 96 // the newly constructed providers. |
| 93 position_provider_ = nullptr; | 97 position_provider_ = nullptr; |
| 94 position_ = Geoposition(); | 98 position_ = Geoposition(); |
| 95 | 99 |
| 96 providers_.clear(); | 100 providers_.clear(); |
| 97 is_running_ = false; | 101 is_running_ = false; |
| 98 } | 102 } |
| 99 | 103 |
| 100 void LocationArbitratorImpl::OnAccessTokenStoresLoaded( | 104 void LocationArbitratorImpl::OnAccessTokenStoresLoaded( |
| 101 AccessTokenStore::AccessTokenMap access_token_map, | 105 AccessTokenStore::AccessTokenMap access_token_map, |
| 102 const scoped_refptr<net::URLRequestContextGetter>& context_getter) { | 106 const scoped_refptr<net::URLRequestContextGetter>& context_getter) { |
| 103 // 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. |
| 104 if (access_token_map.empty()) | 108 if (access_token_map.empty()) |
| 105 access_token_map[DefaultNetworkProviderURL()]; | 109 access_token_map[DefaultNetworkProviderURL()]; |
| 106 for (const auto& entry : access_token_map) { | 110 for (const auto& entry : access_token_map) { |
| 107 RegisterProvider(NewNetworkLocationProvider( | 111 RegisterProvider(NewNetworkLocationProvider( |
| 108 GetAccessTokenStore(), context_getter, entry.first, entry.second)); | 112 GetAccessTokenStore(), context_getter, entry.first, entry.second)); |
| 109 } | 113 } |
| 110 DoStartProviders(); | 114 DoStartProviders(); |
| 111 } | 115 } |
| 112 | 116 |
| 113 void LocationArbitratorImpl::RegisterProvider( | 117 void LocationArbitratorImpl::RegisterProvider( |
| 114 std::unique_ptr<LocationProvider> provider) { | 118 std::unique_ptr<LocationProvider> provider) { |
| 115 if (!provider) | 119 if (!provider) |
| 116 return; | 120 return; |
| 117 provider->SetUpdateCallback(provider_update_callback_); | 121 provider->SetUpdateCallback(base::Bind( |
| 122 &LocationArbitratorImpl::OnLocationUpdate, base::Unretained(this))); |
| 118 if (is_permission_granted_) | 123 if (is_permission_granted_) |
| 119 provider->OnPermissionGranted(); | 124 provider->OnPermissionGranted(); |
| 120 providers_.push_back(std::move(provider)); | 125 providers_.push_back(std::move(provider)); |
| 121 } | 126 } |
| 122 | 127 |
| 123 void LocationArbitratorImpl::RegisterSystemProvider() { | 128 void LocationArbitratorImpl::RegisterSystemProvider() { |
| 124 std::unique_ptr<LocationProvider> provider = | 129 std::unique_ptr<LocationProvider> provider = |
| 125 delegate_->OverrideSystemLocationProvider(); | 130 delegate_->OverrideSystemLocationProvider(); |
| 126 if (!provider) | 131 if (!provider) |
| 127 provider = NewSystemLocationProvider(); | 132 provider = NewSystemLocationProvider(); |
| 128 RegisterProvider(std::move(provider)); | 133 RegisterProvider(std::move(provider)); |
| 129 } | 134 } |
| 130 | 135 |
| 131 void LocationArbitratorImpl::OnLocationUpdate(const LocationProvider* provider, | 136 void LocationArbitratorImpl::OnLocationUpdate(const LocationProvider* provider, |
| 132 const Geoposition& new_position) { | 137 const Geoposition& new_position) { |
| 133 DCHECK(new_position.Validate() || | 138 DCHECK(new_position.Validate() || |
| 134 new_position.error_code != Geoposition::ERROR_CODE_NONE); | 139 new_position.error_code != Geoposition::ERROR_CODE_NONE); |
| 135 if (!IsNewPositionBetter(position_, new_position, | 140 if (!IsNewPositionBetter(position_, new_position, |
| 136 provider == position_provider_)) | 141 provider == position_provider_)) |
| 137 return; | 142 return; |
| 138 position_provider_ = provider; | 143 position_provider_ = provider; |
| 139 position_ = new_position; | 144 position_ = new_position; |
| 140 arbitrator_update_callback_.Run(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; |
| 141 } | 156 } |
| 142 | 157 |
| 143 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::NewAccessTokenStore() { | 158 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::NewAccessTokenStore() { |
| 144 return delegate_->CreateAccessTokenStore(); | 159 return delegate_->CreateAccessTokenStore(); |
| 145 } | 160 } |
| 146 | 161 |
| 147 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::GetAccessTokenStore() { | 162 scoped_refptr<AccessTokenStore> LocationArbitratorImpl::GetAccessTokenStore() { |
| 148 if (!access_token_store_) | 163 if (!access_token_store_) |
| 149 access_token_store_ = NewAccessTokenStore(); | 164 access_token_store_ = NewAccessTokenStore(); |
| 150 return access_token_store_; | 165 return access_token_store_; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 return true; | 213 return true; |
| 199 } else if ((GetTimeNow() - old_position.timestamp).InMilliseconds() > | 214 } else if ((GetTimeNow() - old_position.timestamp).InMilliseconds() > |
| 200 kFixStaleTimeoutMilliseconds) { | 215 kFixStaleTimeoutMilliseconds) { |
| 201 // Existing fix is stale. | 216 // Existing fix is stale. |
| 202 return true; | 217 return true; |
| 203 } | 218 } |
| 204 } | 219 } |
| 205 return false; | 220 return false; |
| 206 } | 221 } |
| 207 | 222 |
| 208 bool LocationArbitratorImpl::HasPermissionBeenGranted() const { | |
| 209 return is_permission_granted_; | |
| 210 } | |
| 211 | |
| 212 } // namespace device | 223 } // namespace device |
| OLD | NEW |