1 // Copyright 2014 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 "chromeos/geolocation/simple_geolocation_provider.h"
10 #include "base/bind.h"
11 #include "chromeos/geolocation/geoposition.h"
12 #include "net/url_request/url_request_context_getter.h"
17 const char kDefaultGeolocationProviderUrl
[] =
18 "https://www.googleapis.com/geolocation/v1/geolocate?";
21 SimpleGeolocationProvider::SimpleGeolocationProvider(
22 net::URLRequestContextGetter
* url_context_getter
,
24 : url_context_getter_(url_context_getter
), url_(url
) {
27 SimpleGeolocationProvider::~SimpleGeolocationProvider() {
28 DCHECK(thread_checker_
.CalledOnValidThread());
31 void SimpleGeolocationProvider::RequestGeolocation(
32 base::TimeDelta timeout
,
33 SimpleGeolocationRequest::ResponseCallback callback
) {
34 DCHECK(thread_checker_
.CalledOnValidThread());
35 SimpleGeolocationRequest
* request(
36 new SimpleGeolocationRequest(url_context_getter_
.get(), url_
, timeout
));
37 requests_
.push_back(request
);
39 // SimpleGeolocationProvider owns all requests. It is safe to pass unretained
40 // "this" because destruction of SimpleGeolocationProvider cancels all
42 SimpleGeolocationRequest::ResponseCallback
callback_tmp(
43 base::Bind(&SimpleGeolocationProvider::OnGeolocationResponse
,
44 base::Unretained(this),
47 request
->MakeRequest(callback_tmp
);
51 GURL
SimpleGeolocationProvider::DefaultGeolocationProviderURL() {
52 return GURL(kDefaultGeolocationProviderUrl
);
55 void SimpleGeolocationProvider::OnGeolocationResponse(
56 SimpleGeolocationRequest
* request
,
57 SimpleGeolocationRequest::ResponseCallback callback
,
58 const Geoposition
& geoposition
,
60 const base::TimeDelta elapsed
) {
61 DCHECK(thread_checker_
.CalledOnValidThread());
63 callback
.Run(geoposition
, server_error
, elapsed
);
65 ScopedVector
<SimpleGeolocationRequest
>::iterator position
=
66 std::find(requests_
.begin(), requests_
.end(), request
);
67 DCHECK(position
!= requests_
.end());
68 if (position
!= requests_
.end()) {
69 std::swap(*position
, *requests_
.rbegin());
70 requests_
.resize(requests_
.size() - 1);
74 } // namespace chromeos