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
3 // found in the LICENSE file.
5 #include "content/browser/geolocation/location_arbitrator_impl.h"
10 #include "base/bind_helpers.h"
11 #include "content/browser/geolocation/network_location_provider.h"
12 #include "content/public/browser/access_token_store.h"
13 #include "content/public/browser/content_browser_client.h"
14 #include "content/public/common/content_client.h"
20 const char* kDefaultNetworkProviderUrl
=
21 "https://www.googleapis.com/geolocation/v1/geolocate";
24 // 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 const int64
LocationArbitratorImpl::kFixStaleTimeoutMilliseconds
=
27 11 * base::Time::kMillisecondsPerSecond
;
29 LocationArbitratorImpl::LocationArbitratorImpl(
30 const LocationUpdateCallback
& callback
)
31 : arbitrator_update_callback_(callback
),
32 provider_update_callback_(
33 base::Bind(&LocationArbitratorImpl::OnLocationUpdate
,
34 base::Unretained(this))),
35 position_provider_(NULL
),
36 is_permission_granted_(false),
40 LocationArbitratorImpl::~LocationArbitratorImpl() {
43 GURL
LocationArbitratorImpl::DefaultNetworkProviderURL() {
44 return GURL(kDefaultNetworkProviderUrl
);
47 void LocationArbitratorImpl::OnPermissionGranted() {
48 is_permission_granted_
= true;
49 for (ScopedVector
<LocationProvider
>::iterator i
= providers_
.begin();
50 i
!= providers_
.end(); ++i
) {
51 (*i
)->OnPermissionGranted();
55 void LocationArbitratorImpl::StartProviders(bool use_high_accuracy
) {
56 // Stash options as OnAccessTokenStoresLoaded has not yet been called.
58 use_high_accuracy_
= use_high_accuracy
;
59 if (providers_
.empty()) {
60 DCHECK(DefaultNetworkProviderURL().is_valid());
61 GetAccessTokenStore()->LoadAccessTokens(
62 base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded
,
63 base::Unretained(this)));
69 void LocationArbitratorImpl::DoStartProviders() {
70 for (ScopedVector
<LocationProvider
>::iterator i
= providers_
.begin();
71 i
!= providers_
.end(); ++i
) {
72 (*i
)->StartProvider(use_high_accuracy_
);
76 void LocationArbitratorImpl::StopProviders() {
77 // Reset the reference location state (provider+position)
78 // so that future starts use fresh locations from
79 // the newly constructed providers.
80 position_provider_
= NULL
;
81 position_
= Geoposition();
87 void LocationArbitratorImpl::OnAccessTokenStoresLoaded(
88 AccessTokenStore::AccessTokenSet access_token_set
,
89 net::URLRequestContextGetter
* context_getter
) {
90 if (!is_running_
|| !providers_
.empty()) {
91 // A second StartProviders() call may have arrived before the first
95 // If there are no access tokens, boot strap it with the default server URL.
96 if (access_token_set
.empty())
97 access_token_set
[DefaultNetworkProviderURL()];
98 for (AccessTokenStore::AccessTokenSet::iterator i
=
99 access_token_set
.begin();
100 i
!= access_token_set
.end(); ++i
) {
102 NewNetworkLocationProvider(
103 GetAccessTokenStore(), context_getter
,
104 i
->first
, i
->second
));
107 LocationProvider
* provider
=
108 GetContentClient()->browser()->OverrideSystemLocationProvider();
110 provider
= NewSystemLocationProvider();
111 RegisterProvider(provider
);
115 void LocationArbitratorImpl::RegisterProvider(
116 LocationProvider
* provider
) {
119 provider
->SetUpdateCallback(provider_update_callback_
);
120 if (is_permission_granted_
)
121 provider
->OnPermissionGranted();
122 providers_
.push_back(provider
);
125 void LocationArbitratorImpl::OnLocationUpdate(const LocationProvider
* provider
,
126 const Geoposition
& new_position
) {
127 DCHECK(new_position
.Validate() ||
128 new_position
.error_code
!= Geoposition::ERROR_CODE_NONE
);
129 if (!IsNewPositionBetter(position_
, new_position
,
130 provider
== position_provider_
))
132 position_provider_
= provider
;
133 position_
= new_position
;
134 arbitrator_update_callback_
.Run(position_
);
137 AccessTokenStore
* LocationArbitratorImpl::NewAccessTokenStore() {
138 return GetContentClient()->browser()->CreateAccessTokenStore();
141 AccessTokenStore
* LocationArbitratorImpl::GetAccessTokenStore() {
142 if (!access_token_store_
.get())
143 access_token_store_
= NewAccessTokenStore();
144 return access_token_store_
.get();
147 LocationProvider
* LocationArbitratorImpl::NewNetworkLocationProvider(
148 AccessTokenStore
* access_token_store
,
149 net::URLRequestContextGetter
* context
,
151 const base::string16
& access_token
) {
152 #if defined(OS_ANDROID)
153 // Android uses its own SystemLocationProvider.
156 return new NetworkLocationProvider(access_token_store
, context
, url
,
161 LocationProvider
* LocationArbitratorImpl::NewSystemLocationProvider() {
162 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
165 return content::NewSystemLocationProvider();
169 base::Time
LocationArbitratorImpl::GetTimeNow() const {
170 return base::Time::Now();
173 bool LocationArbitratorImpl::IsNewPositionBetter(
174 const Geoposition
& old_position
, const Geoposition
& new_position
,
175 bool from_same_provider
) const {
176 // Updates location_info if it's better than what we currently have,
177 // or if it's a newer update from the same provider.
178 if (!old_position
.Validate()) {
179 // Older location wasn't locked.
182 if (new_position
.Validate()) {
183 // New location is locked, let's check if it's any better.
184 if (old_position
.accuracy
>= new_position
.accuracy
) {
185 // Accuracy is better.
187 } else if (from_same_provider
) {
188 // Same provider, fresher location.
190 } else if ((GetTimeNow() - old_position
.timestamp
).InMilliseconds() >
191 kFixStaleTimeoutMilliseconds
) {
192 // Existing fix is stale.
199 bool LocationArbitratorImpl::HasPermissionBeenGranted() const {
200 return is_permission_granted_
;
203 } // namespace content