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/network_location_provider.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/time/time.h"
10 #include "content/public/browser/access_token_store.h"
14 // The maximum period of time we'll wait for a complete set of wifi data
15 // before sending the request.
16 const int kDataCompleteWaitSeconds
= 2;
20 const size_t NetworkLocationProvider::PositionCache::kMaximumSize
= 10;
22 NetworkLocationProvider::PositionCache::PositionCache() {}
24 NetworkLocationProvider::PositionCache::~PositionCache() {}
26 bool NetworkLocationProvider::PositionCache::CachePosition(
27 const WifiData
& wifi_data
,
28 const Geoposition
& position
) {
29 // Check that we can generate a valid key for the wifi data.
31 if (!MakeKey(wifi_data
, &key
)) {
34 // If the cache is full, remove the oldest entry.
35 if (cache_
.size() == kMaximumSize
) {
36 DCHECK(cache_age_list_
.size() == kMaximumSize
);
37 CacheAgeList::iterator oldest_entry
= cache_age_list_
.begin();
38 DCHECK(oldest_entry
!= cache_age_list_
.end());
39 cache_
.erase(*oldest_entry
);
40 cache_age_list_
.erase(oldest_entry
);
42 DCHECK_LT(cache_
.size(), kMaximumSize
);
43 // Insert the position into the cache.
44 std::pair
<CacheMap::iterator
, bool> result
=
45 cache_
.insert(std::make_pair(key
, position
));
47 NOTREACHED(); // We never try to add the same key twice.
48 CHECK_EQ(cache_
.size(), cache_age_list_
.size());
51 cache_age_list_
.push_back(result
.first
);
52 DCHECK_EQ(cache_
.size(), cache_age_list_
.size());
56 // Searches for a cached position response for the current WiFi data. Returns
57 // the cached position if available, NULL otherwise.
58 const Geoposition
* NetworkLocationProvider::PositionCache::FindPosition(
59 const WifiData
& wifi_data
) {
61 if (!MakeKey(wifi_data
, &key
)) {
64 CacheMap::const_iterator iter
= cache_
.find(key
);
65 return iter
== cache_
.end() ? NULL
: &iter
->second
;
68 // Makes the key for the map of cached positions, using the available data.
69 // Returns true if a good key was generated, false otherwise.
72 bool NetworkLocationProvider::PositionCache::MakeKey(
73 const WifiData
& wifi_data
,
74 base::string16
* key
) {
75 // Currently we use only WiFi data and base the key only on the MAC addresses.
78 const size_t kCharsPerMacAddress
= 6 * 3 + 1; // e.g. "11:22:33:44:55:66|"
79 key
->reserve(wifi_data
.access_point_data
.size() * kCharsPerMacAddress
);
80 const base::string16
separator(base::ASCIIToUTF16("|"));
81 for (WifiData::AccessPointDataSet::const_iterator iter
=
82 wifi_data
.access_point_data
.begin();
83 iter
!= wifi_data
.access_point_data
.end();
86 *key
+= iter
->mac_address
;
89 // If the key is the empty string, return false, as we don't want to cache a
90 // position for such data.
94 // NetworkLocationProvider factory function
95 LocationProviderBase
* NewNetworkLocationProvider(
96 AccessTokenStore
* access_token_store
,
97 net::URLRequestContextGetter
* context
,
99 const base::string16
& access_token
) {
100 return new NetworkLocationProvider(
101 access_token_store
, context
, url
, access_token
);
104 // NetworkLocationProvider
105 NetworkLocationProvider::NetworkLocationProvider(
106 AccessTokenStore
* access_token_store
,
107 net::URLRequestContextGetter
* url_context_getter
,
109 const base::string16
& access_token
)
110 : access_token_store_(access_token_store
),
111 wifi_data_provider_(NULL
),
112 wifi_data_update_callback_(
113 base::Bind(&NetworkLocationProvider::WifiDataUpdateAvailable
,
114 base::Unretained(this))),
115 is_wifi_data_complete_(false),
116 access_token_(access_token
),
117 is_permission_granted_(false),
118 is_new_data_available_(false),
119 weak_factory_(this) {
120 // Create the position cache.
121 position_cache_
.reset(new PositionCache());
123 NetworkLocationRequest::LocationResponseCallback callback
=
124 base::Bind(&NetworkLocationProvider::LocationResponseAvailable
,
125 base::Unretained(this));
126 request_
.reset(new NetworkLocationRequest(url_context_getter
, url
, callback
));
129 NetworkLocationProvider::~NetworkLocationProvider() {
133 // LocationProvider implementation
134 void NetworkLocationProvider::GetPosition(Geoposition
* position
) {
136 *position
= position_
;
139 void NetworkLocationProvider::RequestRefresh() {
140 // TODO(joth): When called via the public (base class) interface, this should
141 // poke each data provider to get them to expedite their next scan.
142 // Whilst in the delayed start, only send request if all data is ready.
143 if (!weak_factory_
.HasWeakPtrs() || is_wifi_data_complete_
) {
148 void NetworkLocationProvider::OnPermissionGranted() {
149 const bool was_permission_granted
= is_permission_granted_
;
150 is_permission_granted_
= true;
151 if (!was_permission_granted
&& IsStarted()) {
156 void NetworkLocationProvider::WifiDataUpdateAvailable(
157 WifiDataProvider
* provider
) {
158 DCHECK(provider
== wifi_data_provider_
);
159 is_wifi_data_complete_
= wifi_data_provider_
->GetData(&wifi_data_
);
163 void NetworkLocationProvider::LocationResponseAvailable(
164 const Geoposition
& position
,
166 const base::string16
& access_token
,
167 const WifiData
& wifi_data
) {
168 DCHECK(CalledOnValidThread());
169 // Record the position and update our cache.
170 position_
= position
;
171 if (position
.Validate()) {
172 position_cache_
->CachePosition(wifi_data
, position
);
175 // Record access_token if it's set.
176 if (!access_token
.empty() && access_token_
!= access_token
) {
177 access_token_
= access_token
;
178 access_token_store_
->SaveAccessToken(request_
->url(), access_token
);
181 // Let listeners know that we now have a position available.
182 NotifyCallback(position_
);
185 bool NetworkLocationProvider::StartProvider(bool high_accuracy
) {
186 DCHECK(CalledOnValidThread());
189 DCHECK(wifi_data_provider_
== NULL
);
190 if (!request_
->url().is_valid()) {
191 LOG(WARNING
) << "StartProvider() : Failed, Bad URL: "
192 << request_
->url().possibly_invalid_spec();
196 // Registers a callback with the data provider. The first call to Register
197 // will create a singleton data provider and it will be deleted when the last
198 // callback is removed with Unregister.
199 wifi_data_provider_
= WifiDataProvider::Register(&wifi_data_update_callback_
);
201 base::MessageLoop::current()->PostDelayedTask(
203 base::Bind(&NetworkLocationProvider::RequestPosition
,
204 weak_factory_
.GetWeakPtr()),
205 base::TimeDelta::FromSeconds(kDataCompleteWaitSeconds
));
206 // Get the wifi data.
207 is_wifi_data_complete_
= wifi_data_provider_
->GetData(&wifi_data_
);
208 if (is_wifi_data_complete_
)
213 void NetworkLocationProvider::OnWifiDataUpdated() {
214 DCHECK(CalledOnValidThread());
215 wifi_data_updated_timestamp_
= base::Time::Now();
217 is_new_data_available_
= is_wifi_data_complete_
;
221 void NetworkLocationProvider::StopProvider() {
222 DCHECK(CalledOnValidThread());
224 wifi_data_provider_
->Unregister(&wifi_data_update_callback_
);
226 wifi_data_provider_
= NULL
;
227 weak_factory_
.InvalidateWeakPtrs();
231 void NetworkLocationProvider::RequestPosition() {
232 DCHECK(CalledOnValidThread());
233 if (!is_new_data_available_
)
236 const Geoposition
* cached_position
=
237 position_cache_
->FindPosition(wifi_data_
);
238 DCHECK(!wifi_data_updated_timestamp_
.is_null()) <<
239 "Timestamp must be set before looking up position";
240 if (cached_position
) {
241 DCHECK(cached_position
->Validate());
242 // Record the position and update its timestamp.
243 position_
= *cached_position
;
244 // The timestamp of a position fix is determined by the timestamp
245 // of the source data update. (The value of position_.timestamp from
246 // the cache could be from weeks ago!)
247 position_
.timestamp
= wifi_data_updated_timestamp_
;
248 is_new_data_available_
= false;
249 // Let listeners know that we now have a position available.
250 NotifyCallback(position_
);
253 // Don't send network requests until authorized. http://crbug.com/39171
254 if (!is_permission_granted_
)
257 weak_factory_
.InvalidateWeakPtrs();
258 is_new_data_available_
= false;
260 // TODO(joth): Rather than cancel pending requests, we should create a new
261 // NetworkLocationRequest for each and hold a set of pending requests.
262 if (request_
->is_request_pending()) {
263 DVLOG(1) << "NetworkLocationProvider - pre-empting pending network request "
264 "with new data. Wifi APs: "
265 << wifi_data_
.access_point_data
.size();
267 request_
->MakeRequest(access_token_
, wifi_data_
,
268 wifi_data_updated_timestamp_
);
271 bool NetworkLocationProvider::IsStarted() const {
272 return wifi_data_provider_
!= NULL
;
275 } // namespace content