Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / geolocation / location_arbitrator_impl.cc
blob20d9a5127dbd3c3eb1a45c4bf52480b8a5ee6f56
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"
7 #include <map>
9 #include "base/bind.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"
15 #include "url/gurl.h"
17 namespace content {
18 namespace {
20 const char* kDefaultNetworkProviderUrl =
21 "https://www.googleapis.com/geolocation/v1/geolocate";
22 } // namespace
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),
37 is_running_(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) {
61 Geoposition position;
62 position.error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED;
63 arbitrator_update_callback_.Run(position);
64 return;
67 // Stash options as OnAccessTokenStoresLoaded has not yet been called.
68 is_running_ = true;
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)));
75 } else {
76 DoStartProviders();
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();
94 providers_.clear();
95 is_running_ = false;
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
103 // completed.
104 return;
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) {
112 RegisterProvider(
113 NewNetworkLocationProvider(
114 GetAccessTokenStore(), context_getter,
115 i->first, i->second));
118 LocationProvider* provider =
119 GetContentClient()->browser()->OverrideSystemLocationProvider();
120 if (!provider)
121 provider = NewSystemLocationProvider();
122 RegisterProvider(provider);
123 DoStartProviders();
126 void LocationArbitratorImpl::RegisterProvider(
127 LocationProvider* provider) {
128 if (!provider)
129 return;
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_))
142 return;
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,
161 const GURL& url,
162 const base::string16& access_token) {
163 #if defined(OS_ANDROID)
164 // Android uses its own SystemLocationProvider.
165 return NULL;
166 #else
167 return new NetworkLocationProvider(access_token_store, context, url,
168 access_token);
169 #endif
172 LocationProvider* LocationArbitratorImpl::NewSystemLocationProvider() {
173 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
174 return NULL;
175 #else
176 return content::NewSystemLocationProvider();
177 #endif
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.
191 return true;
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.
197 return true;
198 } else if (from_same_provider) {
199 // Same provider, fresher location.
200 return true;
201 } else if ((GetTimeNow() - old_position.timestamp).InMilliseconds() >
202 kFixStaleTimeoutMilliseconds) {
203 // Existing fix is stale.
204 return true;
207 return false;
210 bool LocationArbitratorImpl::HasPermissionBeenGranted() const {
211 return is_permission_granted_;
214 } // namespace content