| 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 "content/browser/geolocation/location_arbitrator_impl.h" | 5 #include "content/browser/geolocation/location_arbitrator_impl.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "content/browser/geolocation/network_location_provider.h" | 13 #include "content/browser/geolocation/network_location_provider.h" |
| 14 #include "content/public/browser/access_token_store.h" | 14 #include "content/public/browser/access_token_store.h" |
| 15 #include "content/public/browser/content_browser_client.h" | |
| 16 #include "content/public/common/content_client.h" | 15 #include "content/public/common/content_client.h" |
| 17 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 18 | 17 |
| 19 namespace content { | 18 namespace content { |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 const char* kDefaultNetworkProviderUrl = | 21 const char* kDefaultNetworkProviderUrl = |
| 23 "https://www.googleapis.com/geolocation/v1/geolocate"; | 22 "https://www.googleapis.com/geolocation/v1/geolocate"; |
| 24 } // namespace | 23 } // namespace |
| 25 | 24 |
| 26 // To avoid oscillations, set this to twice the expected update interval of a | 25 // 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. | 26 // a GPS-type location provider (in case it misses a beat) plus a little. |
| 28 const int64_t LocationArbitratorImpl::kFixStaleTimeoutMilliseconds = | 27 const int64_t LocationArbitratorImpl::kFixStaleTimeoutMilliseconds = |
| 29 11 * base::Time::kMillisecondsPerSecond; | 28 11 * base::Time::kMillisecondsPerSecond; |
| 30 | 29 |
| 31 LocationArbitratorImpl::LocationArbitratorImpl( | 30 LocationArbitratorImpl::LocationArbitratorImpl( |
| 32 const LocationUpdateCallback& callback) | 31 const LocationUpdateCallback& callback, |
| 33 : arbitrator_update_callback_(callback), | 32 GeolocationProvider::Delegate* delegate) |
| 33 : delegate_(delegate), |
| 34 arbitrator_update_callback_(callback), |
| 34 provider_update_callback_( | 35 provider_update_callback_( |
| 35 base::Bind(&LocationArbitratorImpl::OnLocationUpdate, | 36 base::Bind(&LocationArbitratorImpl::OnLocationUpdate, |
| 36 base::Unretained(this))), | 37 base::Unretained(this))), |
| 37 position_provider_(NULL), | 38 position_provider_(NULL), |
| 38 is_permission_granted_(false), | 39 is_permission_granted_(false), |
| 39 is_running_(false) { | 40 is_running_(false) {} |
| 40 } | |
| 41 | 41 |
| 42 LocationArbitratorImpl::~LocationArbitratorImpl() { | 42 LocationArbitratorImpl::~LocationArbitratorImpl() { |
| 43 } | 43 } |
| 44 | 44 |
| 45 GURL LocationArbitratorImpl::DefaultNetworkProviderURL() { | 45 GURL LocationArbitratorImpl::DefaultNetworkProviderURL() { |
| 46 return GURL(kDefaultNetworkProviderUrl); | 46 return GURL(kDefaultNetworkProviderUrl); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void LocationArbitratorImpl::OnPermissionGranted() { | 49 void LocationArbitratorImpl::OnPermissionGranted() { |
| 50 is_permission_granted_ = true; | 50 is_permission_granted_ = true; |
| 51 for (const auto& provider : providers_) | 51 for (const auto& provider : providers_) |
| 52 provider->OnPermissionGranted(); | 52 provider->OnPermissionGranted(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void LocationArbitratorImpl::StartProviders(bool enable_high_accuracy) { | 55 void LocationArbitratorImpl::StartProviders(bool enable_high_accuracy) { |
| 56 // Stash options as OnAccessTokenStoresLoaded has not yet been called. | 56 // Stash options as OnAccessTokenStoresLoaded has not yet been called. |
| 57 is_running_ = true; | 57 is_running_ = true; |
| 58 enable_high_accuracy_ = enable_high_accuracy; | 58 enable_high_accuracy_ = enable_high_accuracy; |
| 59 | 59 |
| 60 if (providers_.empty()) { | 60 if (providers_.empty()) { |
| 61 RegisterSystemProvider(); | 61 RegisterSystemProvider(); |
| 62 | 62 |
| 63 AccessTokenStore* access_token_store = GetAccessTokenStore(); | 63 AccessTokenStore* access_token_store = GetAccessTokenStore(); |
| 64 if (access_token_store && | 64 if (access_token_store && delegate_->UseNetworkLocationProviders()) { |
| 65 GetContentClient()->browser()->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(&LocationArbitratorImpl::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; | 70 return; |
| 72 } | 71 } |
| 73 } | 72 } |
| 74 DoStartProviders(); | 73 DoStartProviders(); |
| 75 } | 74 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 std::unique_ptr<LocationProvider> provider) { | 114 std::unique_ptr<LocationProvider> provider) { |
| 116 if (!provider) | 115 if (!provider) |
| 117 return; | 116 return; |
| 118 provider->SetUpdateCallback(provider_update_callback_); | 117 provider->SetUpdateCallback(provider_update_callback_); |
| 119 if (is_permission_granted_) | 118 if (is_permission_granted_) |
| 120 provider->OnPermissionGranted(); | 119 provider->OnPermissionGranted(); |
| 121 providers_.push_back(std::move(provider)); | 120 providers_.push_back(std::move(provider)); |
| 122 } | 121 } |
| 123 | 122 |
| 124 void LocationArbitratorImpl::RegisterSystemProvider() { | 123 void LocationArbitratorImpl::RegisterSystemProvider() { |
| 125 std::unique_ptr<LocationProvider> provider = base::WrapUnique( | 124 std::unique_ptr<LocationProvider> provider = |
| 126 GetContentClient()->browser()->OverrideSystemLocationProvider()); | 125 base::WrapUnique(delegate_->OverrideSystemLocationProvider()); |
| 127 if (!provider) | 126 if (!provider) |
| 128 provider = NewSystemLocationProvider(); | 127 provider = NewSystemLocationProvider(); |
| 129 RegisterProvider(std::move(provider)); | 128 RegisterProvider(std::move(provider)); |
| 130 } | 129 } |
| 131 | 130 |
| 132 void LocationArbitratorImpl::OnLocationUpdate(const LocationProvider* provider, | 131 void LocationArbitratorImpl::OnLocationUpdate(const LocationProvider* provider, |
| 133 const Geoposition& new_position) { | 132 const Geoposition& new_position) { |
| 134 DCHECK(new_position.Validate() || | 133 DCHECK(new_position.Validate() || |
| 135 new_position.error_code != Geoposition::ERROR_CODE_NONE); | 134 new_position.error_code != Geoposition::ERROR_CODE_NONE); |
| 136 if (!IsNewPositionBetter(position_, new_position, | 135 if (!IsNewPositionBetter(position_, new_position, |
| 137 provider == position_provider_)) | 136 provider == position_provider_)) |
| 138 return; | 137 return; |
| 139 position_provider_ = provider; | 138 position_provider_ = provider; |
| 140 position_ = new_position; | 139 position_ = new_position; |
| 141 arbitrator_update_callback_.Run(position_); | 140 arbitrator_update_callback_.Run(position_); |
| 142 } | 141 } |
| 143 | 142 |
| 144 AccessTokenStore* LocationArbitratorImpl::NewAccessTokenStore() { | 143 AccessTokenStore* LocationArbitratorImpl::NewAccessTokenStore() { |
| 145 return GetContentClient()->browser()->CreateAccessTokenStore(); | 144 return delegate_->CreateAccessTokenStore(); |
| 146 } | 145 } |
| 147 | 146 |
| 148 AccessTokenStore* LocationArbitratorImpl::GetAccessTokenStore() { | 147 AccessTokenStore* LocationArbitratorImpl::GetAccessTokenStore() { |
| 149 if (!access_token_store_.get()) | 148 if (!access_token_store_.get()) |
| 150 access_token_store_ = NewAccessTokenStore(); | 149 access_token_store_ = NewAccessTokenStore(); |
| 151 return access_token_store_.get(); | 150 return access_token_store_.get(); |
| 152 } | 151 } |
| 153 | 152 |
| 154 std::unique_ptr<LocationProvider> | 153 std::unique_ptr<LocationProvider> |
| 155 LocationArbitratorImpl::NewNetworkLocationProvider( | 154 LocationArbitratorImpl::NewNetworkLocationProvider( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 } | 202 } |
| 204 } | 203 } |
| 205 return false; | 204 return false; |
| 206 } | 205 } |
| 207 | 206 |
| 208 bool LocationArbitratorImpl::HasPermissionBeenGranted() const { | 207 bool LocationArbitratorImpl::HasPermissionBeenGranted() const { |
| 209 return is_permission_granted_; | 208 return is_permission_granted_; |
| 210 } | 209 } |
| 211 | 210 |
| 212 } // namespace content | 211 } // namespace content |
| OLD | NEW |