| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 "chrome/browser/geolocation/location_arbitrator.h" | |
| 6 | |
| 7 #include <map> | |
| 8 | |
| 9 #include "chrome/browser/geolocation/arbitrator_dependency_factory.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 const char* kDefaultNetworkProviderUrl = "https://www.google.com/loc/json"; | |
| 15 GeolocationArbitratorDependencyFactory* g_dependency_factory_for_test = NULL; | |
| 16 | |
| 17 } // namespace | |
| 18 | |
| 19 // To avoid oscillations, set this to twice the expected update interval of a | |
| 20 // a GPS-type location provider (in case it misses a beat) plus a little. | |
| 21 const int64 GeolocationArbitrator::kFixStaleTimeoutMilliseconds = | |
| 22 11 * base::Time::kMillisecondsPerSecond; | |
| 23 | |
| 24 GeolocationArbitrator::GeolocationArbitrator( | |
| 25 GeolocationArbitratorDependencyFactory* dependency_factory, | |
| 26 GeolocationObserver* observer) | |
| 27 : dependency_factory_(dependency_factory), | |
| 28 access_token_store_(dependency_factory->NewAccessTokenStore()), | |
| 29 context_getter_(dependency_factory->GetContextGetter()), | |
| 30 get_time_now_(dependency_factory->GetTimeFunction()), | |
| 31 observer_(observer), | |
| 32 position_provider_(NULL) { | |
| 33 DCHECK(GURL(kDefaultNetworkProviderUrl).is_valid()); | |
| 34 access_token_store_->LoadAccessTokens( | |
| 35 &request_consumer_, | |
| 36 NewCallback(this, | |
| 37 &GeolocationArbitrator::OnAccessTokenStoresLoaded)); | |
| 38 } | |
| 39 | |
| 40 GeolocationArbitrator::~GeolocationArbitrator() { | |
| 41 } | |
| 42 | |
| 43 GeolocationArbitrator* GeolocationArbitrator::Create( | |
| 44 GeolocationObserver* observer) { | |
| 45 GeolocationArbitratorDependencyFactory* dependency_factory = | |
| 46 g_dependency_factory_for_test ? | |
| 47 g_dependency_factory_for_test : | |
| 48 new DefaultGeolocationArbitratorDependencyFactory; | |
| 49 GeolocationArbitrator* arbitrator = | |
| 50 new GeolocationArbitrator(dependency_factory, observer); | |
| 51 g_dependency_factory_for_test = NULL; | |
| 52 return arbitrator; | |
| 53 } | |
| 54 | |
| 55 void GeolocationArbitrator::OnPermissionGranted( | |
| 56 const GURL& requesting_frame) { | |
| 57 most_recent_authorized_frame_ = requesting_frame; | |
| 58 for (ScopedVector<LocationProviderBase>::iterator i = providers_.begin(); | |
| 59 i != providers_.end(); ++i) { | |
| 60 (*i)->OnPermissionGranted(requesting_frame); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void GeolocationArbitrator::StartProviders( | |
| 65 const GeolocationObserverOptions& options) { | |
| 66 // Stash options incase OnAccessTokenStoresLoaded has not yet been called | |
| 67 // (in which case |providers_| will be empty). | |
| 68 current_provider_options_ = options; | |
| 69 StartProviders(); | |
| 70 } | |
| 71 | |
| 72 void GeolocationArbitrator::StartProviders() { | |
| 73 for (ScopedVector<LocationProviderBase>::iterator i = providers_.begin(); | |
| 74 i != providers_.end(); ++i) { | |
| 75 (*i)->StartProvider(current_provider_options_.use_high_accuracy); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 void GeolocationArbitrator::StopProviders() { | |
| 80 for (ScopedVector<LocationProviderBase>::iterator i = providers_.begin(); | |
| 81 i != providers_.end(); ++i) { | |
| 82 (*i)->StopProvider(); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 void GeolocationArbitrator::OnAccessTokenStoresLoaded( | |
| 87 AccessTokenStore::AccessTokenSet access_token_set) { | |
| 88 DCHECK(providers_.empty()) | |
| 89 << "OnAccessTokenStoresLoaded : has existing location " | |
| 90 << "provider. Race condition caused repeat load of tokens?"; | |
| 91 // If there are no access tokens, boot strap it with the default server URL. | |
| 92 if (access_token_set.empty()) | |
| 93 access_token_set[GURL(kDefaultNetworkProviderUrl)]; | |
| 94 for (AccessTokenStore::AccessTokenSet::iterator i = | |
| 95 access_token_set.begin(); | |
| 96 i != access_token_set.end(); ++i) { | |
| 97 RegisterProvider( | |
| 98 dependency_factory_->NewNetworkLocationProvider( | |
| 99 access_token_store_.get(), context_getter_.get(), | |
| 100 i->first, i->second)); | |
| 101 } | |
| 102 RegisterProvider(dependency_factory_->NewSystemLocationProvider()); | |
| 103 StartProviders(); | |
| 104 } | |
| 105 | |
| 106 void GeolocationArbitrator::RegisterProvider( | |
| 107 LocationProviderBase* provider) { | |
| 108 if (!provider) | |
| 109 return; | |
| 110 provider->RegisterListener(this); | |
| 111 if (most_recent_authorized_frame_.is_valid()) | |
| 112 provider->OnPermissionGranted(most_recent_authorized_frame_); | |
| 113 providers_->push_back(provider); | |
| 114 } | |
| 115 | |
| 116 void GeolocationArbitrator::LocationUpdateAvailable( | |
| 117 LocationProviderBase* provider) { | |
| 118 DCHECK(provider); | |
| 119 Geoposition new_position; | |
| 120 provider->GetPosition(&new_position); | |
| 121 DCHECK(new_position.IsInitialized()); | |
| 122 if (!IsNewPositionBetter(position_, new_position, | |
| 123 provider == position_provider_)) | |
| 124 return; | |
| 125 position_provider_ = provider; | |
| 126 position_ = new_position; | |
| 127 observer_->OnLocationUpdate(position_); | |
| 128 } | |
| 129 | |
| 130 bool GeolocationArbitrator::IsNewPositionBetter( | |
| 131 const Geoposition& old_position, const Geoposition& new_position, | |
| 132 bool from_same_provider) const { | |
| 133 // Updates location_info if it's better than what we currently have, | |
| 134 // or if it's a newer update from the same provider. | |
| 135 if (!old_position.IsValidFix()) { | |
| 136 // Older location wasn't locked. | |
| 137 return true; | |
| 138 } | |
| 139 if (new_position.IsValidFix()) { | |
| 140 // New location is locked, let's check if it's any better. | |
| 141 if (old_position.accuracy >= new_position.accuracy) { | |
| 142 // Accuracy is better. | |
| 143 return true; | |
| 144 } else if (from_same_provider) { | |
| 145 // Same provider, fresher location. | |
| 146 return true; | |
| 147 } else if ((get_time_now_() - old_position.timestamp).InMilliseconds() > | |
| 148 kFixStaleTimeoutMilliseconds) { | |
| 149 // Existing fix is stale. | |
| 150 return true; | |
| 151 } | |
| 152 } | |
| 153 return false; | |
| 154 } | |
| 155 | |
| 156 bool GeolocationArbitrator::HasPermissionBeenGranted() const { | |
| 157 return most_recent_authorized_frame_.is_valid(); | |
| 158 } | |
| 159 | |
| 160 void GeolocationArbitrator::SetDependencyFactoryForTest( | |
| 161 GeolocationArbitratorDependencyFactory* dependency_factory) { | |
| 162 g_dependency_factory_for_test = dependency_factory; | |
| 163 } | |
| OLD | NEW |