| 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 using content::Geoposition; |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const char* kDefaultNetworkProviderUrl = "https://maps.googleapis.com/maps/api/b
rowserlocation/json"; | 20 const char* kDefaultNetworkProviderUrl = |
| 21 "https://www.googleapis.com/geolocation/v1/geolocate"; |
| 21 GeolocationArbitratorDependencyFactory* g_dependency_factory_for_test = NULL; | 22 GeolocationArbitratorDependencyFactory* g_dependency_factory_for_test = NULL; |
| 22 | 23 |
| 23 } // namespace | 24 } // namespace |
| 24 | 25 |
| 25 // To avoid oscillations, set this to twice the expected update interval of a | 26 // 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. | 27 // a GPS-type location provider (in case it misses a beat) plus a little. |
| 27 const int64 GeolocationArbitrator::kFixStaleTimeoutMilliseconds = | 28 const int64 GeolocationArbitrator::kFixStaleTimeoutMilliseconds = |
| 28 11 * base::Time::kMillisecondsPerSecond; | 29 11 * base::Time::kMillisecondsPerSecond; |
| 29 | 30 |
| 30 GeolocationArbitrator::GeolocationArbitrator( | 31 GeolocationArbitrator::GeolocationArbitrator( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 47 GeolocationArbitratorDependencyFactory* dependency_factory = | 48 GeolocationArbitratorDependencyFactory* dependency_factory = |
| 48 g_dependency_factory_for_test ? | 49 g_dependency_factory_for_test ? |
| 49 g_dependency_factory_for_test : | 50 g_dependency_factory_for_test : |
| 50 new DefaultGeolocationArbitratorDependencyFactory; | 51 new DefaultGeolocationArbitratorDependencyFactory; |
| 51 GeolocationArbitrator* arbitrator = | 52 GeolocationArbitrator* arbitrator = |
| 52 new GeolocationArbitrator(dependency_factory, observer); | 53 new GeolocationArbitrator(dependency_factory, observer); |
| 53 g_dependency_factory_for_test = NULL; | 54 g_dependency_factory_for_test = NULL; |
| 54 return arbitrator; | 55 return arbitrator; |
| 55 } | 56 } |
| 56 | 57 |
| 58 GURL GeolocationArbitrator::DefaultNetworkProviderURL() { |
| 59 return GURL(kDefaultNetworkProviderUrl); |
| 60 } |
| 61 |
| 57 void GeolocationArbitrator::OnPermissionGranted() { | 62 void GeolocationArbitrator::OnPermissionGranted() { |
| 58 is_permission_granted_ = true; | 63 is_permission_granted_ = true; |
| 59 for (ScopedVector<LocationProviderBase>::iterator i = providers_.begin(); | 64 for (ScopedVector<LocationProviderBase>::iterator i = providers_.begin(); |
| 60 i != providers_.end(); ++i) { | 65 i != providers_.end(); ++i) { |
| 61 (*i)->OnPermissionGranted(); | 66 (*i)->OnPermissionGranted(); |
| 62 } | 67 } |
| 63 } | 68 } |
| 64 | 69 |
| 65 void GeolocationArbitrator::StartProviders( | 70 void GeolocationArbitrator::StartProviders( |
| 66 const GeolocationObserverOptions& options) { | 71 const GeolocationObserverOptions& options) { |
| 67 // Stash options as OnAccessTokenStoresLoaded has not yet been called. | 72 // Stash options as OnAccessTokenStoresLoaded has not yet been called. |
| 68 current_provider_options_ = options; | 73 current_provider_options_ = options; |
| 69 if (providers_.empty()) { | 74 if (providers_.empty()) { |
| 70 DCHECK(GURL(kDefaultNetworkProviderUrl).is_valid()); | 75 DCHECK(DefaultNetworkProviderURL().is_valid()); |
| 71 access_token_store_->LoadAccessTokens( | 76 access_token_store_->LoadAccessTokens( |
| 72 base::Bind(&GeolocationArbitrator::OnAccessTokenStoresLoaded, | 77 base::Bind(&GeolocationArbitrator::OnAccessTokenStoresLoaded, |
| 73 base::Unretained(this))); | 78 base::Unretained(this))); |
| 74 } else { | 79 } else { |
| 75 DoStartProviders(); | 80 DoStartProviders(); |
| 76 } | 81 } |
| 77 } | 82 } |
| 78 | 83 |
| 79 void GeolocationArbitrator::DoStartProviders() { | 84 void GeolocationArbitrator::DoStartProviders() { |
| 80 for (ScopedVector<LocationProviderBase>::iterator i = providers_.begin(); | 85 for (ScopedVector<LocationProviderBase>::iterator i = providers_.begin(); |
| 81 i != providers_.end(); ++i) { | 86 i != providers_.end(); ++i) { |
| 82 (*i)->StartProvider(current_provider_options_.use_high_accuracy); | 87 (*i)->StartProvider(current_provider_options_.use_high_accuracy); |
| 83 } | 88 } |
| 84 } | 89 } |
| 85 | 90 |
| 86 void GeolocationArbitrator::StopProviders() { | 91 void GeolocationArbitrator::StopProviders() { |
| 87 providers_.clear(); | 92 providers_.clear(); |
| 88 } | 93 } |
| 89 | 94 |
| 90 void GeolocationArbitrator::OnAccessTokenStoresLoaded( | 95 void GeolocationArbitrator::OnAccessTokenStoresLoaded( |
| 91 AccessTokenStore::AccessTokenSet access_token_set, | 96 AccessTokenStore::AccessTokenSet access_token_set, |
| 92 net::URLRequestContextGetter* context_getter) { | 97 net::URLRequestContextGetter* context_getter) { |
| 93 if (!providers_.empty()) { | 98 if (!providers_.empty()) { |
| 94 // A second StartProviders() call may have arrived before the first | 99 // A second StartProviders() call may have arrived before the first |
| 95 // completed. | 100 // completed. |
| 96 return; | 101 return; |
| 97 } | 102 } |
| 98 // If there are no access tokens, boot strap it with the default server URL. | 103 // If there are no access tokens, boot strap it with the default server URL. |
| 99 if (access_token_set.empty()) | 104 if (access_token_set.empty()) |
| 100 access_token_set[GURL(kDefaultNetworkProviderUrl)]; | 105 access_token_set[DefaultNetworkProviderURL()]; |
| 101 for (AccessTokenStore::AccessTokenSet::iterator i = | 106 for (AccessTokenStore::AccessTokenSet::iterator i = |
| 102 access_token_set.begin(); | 107 access_token_set.begin(); |
| 103 i != access_token_set.end(); ++i) { | 108 i != access_token_set.end(); ++i) { |
| 104 RegisterProvider( | 109 RegisterProvider( |
| 105 dependency_factory_->NewNetworkLocationProvider( | 110 dependency_factory_->NewNetworkLocationProvider( |
| 106 access_token_store_.get(), context_getter, | 111 access_token_store_.get(), context_getter, |
| 107 i->first, i->second)); | 112 i->first, i->second)); |
| 108 } | 113 } |
| 109 RegisterProvider(dependency_factory_->NewSystemLocationProvider()); | 114 RegisterProvider(dependency_factory_->NewSystemLocationProvider()); |
| 110 DoStartProviders(); | 115 DoStartProviders(); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 } | 167 } |
| 163 | 168 |
| 164 bool GeolocationArbitrator::HasPermissionBeenGranted() const { | 169 bool GeolocationArbitrator::HasPermissionBeenGranted() const { |
| 165 return is_permission_granted_; | 170 return is_permission_granted_; |
| 166 } | 171 } |
| 167 | 172 |
| 168 void GeolocationArbitrator::SetDependencyFactoryForTest( | 173 void GeolocationArbitrator::SetDependencyFactoryForTest( |
| 169 GeolocationArbitratorDependencyFactory* dependency_factory) { | 174 GeolocationArbitratorDependencyFactory* dependency_factory) { |
| 170 g_dependency_factory_for_test = dependency_factory; | 175 g_dependency_factory_for_test = dependency_factory; |
| 171 } | 176 } |
| OLD | NEW |