| 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.h" | 5 #include "content/browser/geolocation/location_arbitrator.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 "content/browser/geolocation/arbitrator_dependency_factory.h" | 11 #include "content/browser/geolocation/arbitrator_dependency_factory.h" |
| 12 #include "content/public/browser/access_token_store.h" | 12 #include "content/public/browser/access_token_store.h" |
| 13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 14 | 14 |
| 15 using content::AccessTokenStore; | 15 using content::AccessTokenStore; |
| 16 using content::Geoposition; |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const char* kDefaultNetworkProviderUrl = "https://maps.googleapis.com/maps/api/b
rowserlocation/json"; | 20 const char* kDefaultNetworkProviderUrl = "https://maps.googleapis.com/maps/api/b
rowserlocation/json"; |
| 20 GeolocationArbitratorDependencyFactory* g_dependency_factory_for_test = NULL; | 21 GeolocationArbitratorDependencyFactory* g_dependency_factory_for_test = NULL; |
| 21 | 22 |
| 22 } // namespace | 23 } // namespace |
| 23 | 24 |
| 24 // 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 |
| 25 // 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. |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 if (is_permission_granted_) | 118 if (is_permission_granted_) |
| 118 provider->OnPermissionGranted(); | 119 provider->OnPermissionGranted(); |
| 119 providers_->push_back(provider); | 120 providers_->push_back(provider); |
| 120 } | 121 } |
| 121 | 122 |
| 122 void GeolocationArbitrator::LocationUpdateAvailable( | 123 void GeolocationArbitrator::LocationUpdateAvailable( |
| 123 LocationProviderBase* provider) { | 124 LocationProviderBase* provider) { |
| 124 DCHECK(provider); | 125 DCHECK(provider); |
| 125 Geoposition new_position; | 126 Geoposition new_position; |
| 126 provider->GetPosition(&new_position); | 127 provider->GetPosition(&new_position); |
| 127 DCHECK(new_position.IsInitialized()); | 128 DCHECK(new_position.Validate() || |
| 129 new_position.error_code != content::Geoposition::ERROR_CODE_NONE); |
| 128 if (!IsNewPositionBetter(position_, new_position, | 130 if (!IsNewPositionBetter(position_, new_position, |
| 129 provider == position_provider_)) | 131 provider == position_provider_)) |
| 130 return; | 132 return; |
| 131 position_provider_ = provider; | 133 position_provider_ = provider; |
| 132 position_ = new_position; | 134 position_ = new_position; |
| 133 observer_->OnLocationUpdate(position_); | 135 observer_->OnLocationUpdate(position_); |
| 134 } | 136 } |
| 135 | 137 |
| 136 bool GeolocationArbitrator::IsNewPositionBetter( | 138 bool GeolocationArbitrator::IsNewPositionBetter( |
| 137 const Geoposition& old_position, const Geoposition& new_position, | 139 const Geoposition& old_position, const Geoposition& new_position, |
| 138 bool from_same_provider) const { | 140 bool from_same_provider) const { |
| 139 // Updates location_info if it's better than what we currently have, | 141 // Updates location_info if it's better than what we currently have, |
| 140 // or if it's a newer update from the same provider. | 142 // or if it's a newer update from the same provider. |
| 141 if (!old_position.IsValidFix()) { | 143 if (!old_position.Validate()) { |
| 142 // Older location wasn't locked. | 144 // Older location wasn't locked. |
| 143 return true; | 145 return true; |
| 144 } | 146 } |
| 145 if (new_position.IsValidFix()) { | 147 if (new_position.Validate()) { |
| 146 // New location is locked, let's check if it's any better. | 148 // New location is locked, let's check if it's any better. |
| 147 if (old_position.accuracy >= new_position.accuracy) { | 149 if (old_position.accuracy >= new_position.accuracy) { |
| 148 // Accuracy is better. | 150 // Accuracy is better. |
| 149 return true; | 151 return true; |
| 150 } else if (from_same_provider) { | 152 } else if (from_same_provider) { |
| 151 // Same provider, fresher location. | 153 // Same provider, fresher location. |
| 152 return true; | 154 return true; |
| 153 } else if ((get_time_now_() - old_position.timestamp).InMilliseconds() > | 155 } else if ((get_time_now_() - old_position.timestamp).InMilliseconds() > |
| 154 kFixStaleTimeoutMilliseconds) { | 156 kFixStaleTimeoutMilliseconds) { |
| 155 // Existing fix is stale. | 157 // Existing fix is stale. |
| 156 return true; | 158 return true; |
| 157 } | 159 } |
| 158 } | 160 } |
| 159 return false; | 161 return false; |
| 160 } | 162 } |
| 161 | 163 |
| 162 bool GeolocationArbitrator::HasPermissionBeenGranted() const { | 164 bool GeolocationArbitrator::HasPermissionBeenGranted() const { |
| 163 return is_permission_granted_; | 165 return is_permission_granted_; |
| 164 } | 166 } |
| 165 | 167 |
| 166 void GeolocationArbitrator::SetDependencyFactoryForTest( | 168 void GeolocationArbitrator::SetDependencyFactoryForTest( |
| 167 GeolocationArbitratorDependencyFactory* dependency_factory) { | 169 GeolocationArbitratorDependencyFactory* dependency_factory) { |
| 168 g_dependency_factory_for_test = dependency_factory; | 170 g_dependency_factory_for_test = dependency_factory; |
| 169 } | 171 } |
| OLD | NEW |