| OLD | NEW |
| 1 // Copyright (c) 2011 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 | 14 |
| 14 using content::AccessTokenStore; | 15 using content::AccessTokenStore; |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 const char* kDefaultNetworkProviderUrl = "https://maps.googleapis.com/maps/api/b
rowserlocation/json"; | 19 const char* kDefaultNetworkProviderUrl = "https://maps.googleapis.com/maps/api/b
rowserlocation/json"; |
| 19 GeolocationArbitratorDependencyFactory* g_dependency_factory_for_test = NULL; | 20 GeolocationArbitratorDependencyFactory* g_dependency_factory_for_test = NULL; |
| 20 | 21 |
| 21 } // namespace | 22 } // namespace |
| 22 | 23 |
| 23 // To avoid oscillations, set this to twice the expected update interval of a | 24 // To avoid oscillations, set this to twice the expected update interval of a |
| 24 // a GPS-type location provider (in case it misses a beat) plus a little. | 25 // a GPS-type location provider (in case it misses a beat) plus a little. |
| 25 const int64 GeolocationArbitrator::kFixStaleTimeoutMilliseconds = | 26 const int64 GeolocationArbitrator::kFixStaleTimeoutMilliseconds = |
| 26 11 * base::Time::kMillisecondsPerSecond; | 27 11 * base::Time::kMillisecondsPerSecond; |
| 27 | 28 |
| 28 GeolocationArbitrator::GeolocationArbitrator( | 29 GeolocationArbitrator::GeolocationArbitrator( |
| 29 GeolocationArbitratorDependencyFactory* dependency_factory, | 30 GeolocationArbitratorDependencyFactory* dependency_factory, |
| 30 GeolocationObserver* observer) | 31 GeolocationObserver* observer) |
| 31 : dependency_factory_(dependency_factory), | 32 : dependency_factory_(dependency_factory), |
| 32 access_token_store_(dependency_factory->NewAccessTokenStore()), | 33 access_token_store_(dependency_factory->NewAccessTokenStore()), |
| 33 get_time_now_(dependency_factory->GetTimeFunction()), | 34 get_time_now_(dependency_factory->GetTimeFunction()), |
| 34 observer_(observer), | 35 observer_(observer), |
| 35 position_provider_(NULL) { | 36 position_provider_(NULL), |
| 37 is_permission_granted_(false) { |
| 36 | 38 |
| 37 } | 39 } |
| 38 | 40 |
| 39 GeolocationArbitrator::~GeolocationArbitrator() { | 41 GeolocationArbitrator::~GeolocationArbitrator() { |
| 40 } | 42 } |
| 41 | 43 |
| 42 GeolocationArbitrator* GeolocationArbitrator::Create( | 44 GeolocationArbitrator* GeolocationArbitrator::Create( |
| 43 GeolocationObserver* observer) { | 45 GeolocationObserver* observer) { |
| 44 GeolocationArbitratorDependencyFactory* dependency_factory = | 46 GeolocationArbitratorDependencyFactory* dependency_factory = |
| 45 g_dependency_factory_for_test ? | 47 g_dependency_factory_for_test ? |
| 46 g_dependency_factory_for_test : | 48 g_dependency_factory_for_test : |
| 47 new DefaultGeolocationArbitratorDependencyFactory; | 49 new DefaultGeolocationArbitratorDependencyFactory; |
| 48 GeolocationArbitrator* arbitrator = | 50 GeolocationArbitrator* arbitrator = |
| 49 new GeolocationArbitrator(dependency_factory, observer); | 51 new GeolocationArbitrator(dependency_factory, observer); |
| 50 g_dependency_factory_for_test = NULL; | 52 g_dependency_factory_for_test = NULL; |
| 51 return arbitrator; | 53 return arbitrator; |
| 52 } | 54 } |
| 53 | 55 |
| 54 void GeolocationArbitrator::OnPermissionGranted( | 56 void GeolocationArbitrator::OnPermissionGranted() { |
| 55 const GURL& requesting_frame) { | 57 is_permission_granted_ = true; |
| 56 most_recent_authorized_frame_ = requesting_frame; | |
| 57 for (ScopedVector<LocationProviderBase>::iterator i = providers_.begin(); | 58 for (ScopedVector<LocationProviderBase>::iterator i = providers_.begin(); |
| 58 i != providers_.end(); ++i) { | 59 i != providers_.end(); ++i) { |
| 59 (*i)->OnPermissionGranted(requesting_frame); | 60 (*i)->OnPermissionGranted(); |
| 60 } | 61 } |
| 61 } | 62 } |
| 62 | 63 |
| 63 void GeolocationArbitrator::StartProviders( | 64 void GeolocationArbitrator::StartProviders( |
| 64 const GeolocationObserverOptions& options) { | 65 const GeolocationObserverOptions& options) { |
| 65 // Stash options as OnAccessTokenStoresLoaded has not yet been called. | 66 // Stash options as OnAccessTokenStoresLoaded has not yet been called. |
| 66 current_provider_options_ = options; | 67 current_provider_options_ = options; |
| 67 if (providers_.empty()) { | 68 if (providers_.empty()) { |
| 68 DCHECK(GURL(kDefaultNetworkProviderUrl).is_valid()); | 69 DCHECK(GURL(kDefaultNetworkProviderUrl).is_valid()); |
| 69 access_token_store_->LoadAccessTokens( | 70 access_token_store_->LoadAccessTokens( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 107 } |
| 107 RegisterProvider(dependency_factory_->NewSystemLocationProvider()); | 108 RegisterProvider(dependency_factory_->NewSystemLocationProvider()); |
| 108 DoStartProviders(); | 109 DoStartProviders(); |
| 109 } | 110 } |
| 110 | 111 |
| 111 void GeolocationArbitrator::RegisterProvider( | 112 void GeolocationArbitrator::RegisterProvider( |
| 112 LocationProviderBase* provider) { | 113 LocationProviderBase* provider) { |
| 113 if (!provider) | 114 if (!provider) |
| 114 return; | 115 return; |
| 115 provider->RegisterListener(this); | 116 provider->RegisterListener(this); |
| 116 if (most_recent_authorized_frame_.is_valid()) | 117 if (is_permission_granted_) |
| 117 provider->OnPermissionGranted(most_recent_authorized_frame_); | 118 provider->OnPermissionGranted(); |
| 118 providers_->push_back(provider); | 119 providers_->push_back(provider); |
| 119 } | 120 } |
| 120 | 121 |
| 121 void GeolocationArbitrator::LocationUpdateAvailable( | 122 void GeolocationArbitrator::LocationUpdateAvailable( |
| 122 LocationProviderBase* provider) { | 123 LocationProviderBase* provider) { |
| 123 DCHECK(provider); | 124 DCHECK(provider); |
| 124 Geoposition new_position; | 125 Geoposition new_position; |
| 125 provider->GetPosition(&new_position); | 126 provider->GetPosition(&new_position); |
| 126 DCHECK(new_position.IsInitialized()); | 127 DCHECK(new_position.IsInitialized()); |
| 127 if (!IsNewPositionBetter(position_, new_position, | 128 if (!IsNewPositionBetter(position_, new_position, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 152 } else if ((get_time_now_() - old_position.timestamp).InMilliseconds() > | 153 } else if ((get_time_now_() - old_position.timestamp).InMilliseconds() > |
| 153 kFixStaleTimeoutMilliseconds) { | 154 kFixStaleTimeoutMilliseconds) { |
| 154 // Existing fix is stale. | 155 // Existing fix is stale. |
| 155 return true; | 156 return true; |
| 156 } | 157 } |
| 157 } | 158 } |
| 158 return false; | 159 return false; |
| 159 } | 160 } |
| 160 | 161 |
| 161 bool GeolocationArbitrator::HasPermissionBeenGranted() const { | 162 bool GeolocationArbitrator::HasPermissionBeenGranted() const { |
| 162 return most_recent_authorized_frame_.is_valid(); | 163 return is_permission_granted_; |
| 163 } | 164 } |
| 164 | 165 |
| 165 void GeolocationArbitrator::SetDependencyFactoryForTest( | 166 void GeolocationArbitrator::SetDependencyFactoryForTest( |
| 166 GeolocationArbitratorDependencyFactory* dependency_factory) { | 167 GeolocationArbitratorDependencyFactory* dependency_factory) { |
| 167 g_dependency_factory_for_test = dependency_factory; | 168 g_dependency_factory_for_test = dependency_factory; |
| 168 } | 169 } |
| OLD | NEW |