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 // GetAccessTokenStore() will return NULL for embedders not implementing
57 // the AccessTokenStore class, so we report an error to avoid JavaScript
58 // requests of location to wait eternally for a reply.
59 AccessTokenStore
* access_token_store
= GetAccessTokenStore();
60 if (!access_token_store
) {
62 position
.error_code
= Geoposition::ERROR_CODE_PERMISSION_DENIED
;
63 arbitrator_update_callback_
.Run(position
);
67 // Stash options as OnAccessTokenStoresLoaded has not yet been called.
69 use_high_accuracy_
= use_high_accuracy
;
70 if (providers_
.empty()) {
71 DCHECK(DefaultNetworkProviderURL().is_valid());
72 access_token_store
->LoadAccessTokens(
73 base::Bind(&LocationArbitratorImpl::OnAccessTokenStoresLoaded
,
74 base::Unretained(this)));
80 void LocationArbitratorImpl::DoStartProviders() {
81 for (ScopedVector
<LocationProvider
>::iterator i
= providers_
.begin();
82 i
!= providers_
.end(); ++i
) {
83 (*i
)->StartProvider(use_high_accuracy_
);
87 void LocationArbitratorImpl::StopProviders() {
88 // Reset the reference location state (provider+position)
89 // so that future starts use fresh locations from
90 // the newly constructed providers.
91 position_provider_
= NULL
;
92 position_
= Geoposition();
98 void LocationArbitratorImpl::OnAccessTokenStoresLoaded(
99 AccessTokenStore::AccessTokenSet access_token_set
,
100 net::URLRequestContextGetter
* context_getter
) {
101 if (!is_running_
|| !providers_
.empty()) {
102 // A second StartProviders() call may have arrived before the first
106 // If there are no access tokens, boot strap it with the default server URL.
107 if (access_token_set
.empty())
108 access_token_set
[DefaultNetworkProviderURL()];
109 for (AccessTokenStore::AccessTokenSet::iterator i
=
110 access_token_set
.begin();
111 i
!= access_token_set
.end(); ++i
) {
113 NewNetworkLocationProvider(
114 GetAccessTokenStore(), context_getter
,
115 i
->first
, i
->second
));
118 LocationProvider
* provider
=
119 GetContentClient()->browser()->OverrideSystemLocationProvider();
121 provider
= NewSystemLocationProvider();
122 RegisterProvider(provider
);
126 void LocationArbitratorImpl::RegisterProvider(
127 LocationProvider
* provider
) {
130 provider
->SetUpdateCallback(provider_update_callback_
);
131 if (is_permission_granted_
)
132 provider
->OnPermissionGranted();
133 providers_
.push_back(provider
);
136 void LocationArbitratorImpl::OnLocationUpdate(const LocationProvider
* provider
,
137 const Geoposition
& new_position
) {
138 DCHECK(new_position
.Validate() ||
139 new_position
.error_code
!= Geoposition::ERROR_CODE_NONE
);
140 if (!IsNewPositionBetter(position_
, new_position
,
141 provider
== position_provider_
))
143 position_provider_
= provider
;
144 position_
= new_position
;
145 arbitrator_update_callback_
.Run(position_
);
148 AccessTokenStore
* LocationArbitratorImpl::NewAccessTokenStore() {
149 return GetContentClient()->browser()->CreateAccessTokenStore();
152 AccessTokenStore
* LocationArbitratorImpl::GetAccessTokenStore() {
153 if (!access_token_store_
.get())
154 access_token_store_
= NewAccessTokenStore();
155 return access_token_store_
.get();
158 LocationProvider
* LocationArbitratorImpl::NewNetworkLocationProvider(
159 AccessTokenStore
* access_token_store
,
160 net::URLRequestContextGetter
* context
,
162 const base::string16
& access_token
) {
163 #if defined(OS_ANDROID)
164 // Android uses its own SystemLocationProvider.
167 return new NetworkLocationProvider(access_token_store
, context
, url
,
172 LocationProvider
* LocationArbitratorImpl::NewSystemLocationProvider() {
173 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
176 return content::NewSystemLocationProvider();
180 base::Time
LocationArbitratorImpl::GetTimeNow() const {
181 return base::Time::Now();
184 bool LocationArbitratorImpl::IsNewPositionBetter(
185 const Geoposition
& old_position
, const Geoposition
& new_position
,
186 bool from_same_provider
) const {
187 // Updates location_info if it's better than what we currently have,
188 // or if it's a newer update from the same provider.
189 if (!old_position
.Validate()) {
190 // Older location wasn't locked.
193 if (new_position
.Validate()) {
194 // New location is locked, let's check if it's any better.
195 if (old_position
.accuracy
>= new_position
.accuracy
) {
196 // Accuracy is better.
198 } else if (from_same_provider
) {
199 // Same provider, fresher location.
201 } else if ((GetTimeNow() - old_position
.timestamp
).InMilliseconds() >
202 kFixStaleTimeoutMilliseconds
) {
203 // Existing fix is stale.
210 bool LocationArbitratorImpl::HasPermissionBeenGranted() const {
211 return is_permission_granted_
;
214 } // namespace content