Trace DataReductionProxyCompressionStats update
[chromium-blink-merge.git] / chromeos / geolocation / simple_geolocation_provider.cc
blobe083b3edb0460b911f5c2e1398bfc8f75851dd52
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"
7 #include <algorithm>
8 #include <iterator>
10 #include "base/bind.h"
11 #include "chromeos/geolocation/geoposition.h"
12 #include "net/url_request/url_request_context_getter.h"
14 namespace chromeos {
16 namespace {
17 const char kDefaultGeolocationProviderUrl[] =
18 "https://www.googleapis.com/geolocation/v1/geolocate?";
19 } // namespace
21 SimpleGeolocationProvider::SimpleGeolocationProvider(
22 net::URLRequestContextGetter* url_context_getter,
23 const GURL& url)
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
41 // requests.
42 SimpleGeolocationRequest::ResponseCallback callback_tmp(
43 base::Bind(&SimpleGeolocationProvider::OnGeolocationResponse,
44 base::Unretained(this),
45 request,
46 callback));
47 request->MakeRequest(callback_tmp);
50 // static
51 GURL SimpleGeolocationProvider::DefaultGeolocationProviderURL() {
52 return GURL(kDefaultGeolocationProviderUrl);
55 void SimpleGeolocationProvider::OnGeolocationResponse(
56 SimpleGeolocationRequest* request,
57 SimpleGeolocationRequest::ResponseCallback callback,
58 const Geoposition& geoposition,
59 bool server_error,
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