Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromeos / timezone / timezone_provider.cc
blob08e10e5359820e2b14efe859c3589f9cf00feac1
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/timezone/timezone_provider.h"
7 #include <algorithm>
8 #include <iterator>
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "chromeos/geolocation/geoposition.h"
13 #include "net/url_request/url_request_context_getter.h"
15 namespace chromeos {
17 TimeZoneProvider::TimeZoneProvider(
18 net::URLRequestContextGetter* url_context_getter,
19 const GURL& url)
20 : url_context_getter_(url_context_getter), url_(url) {
23 TimeZoneProvider::~TimeZoneProvider() {
24 DCHECK(thread_checker_.CalledOnValidThread());
27 void TimeZoneProvider::RequestTimezone(
28 const Geoposition& position,
29 base::TimeDelta timeout,
30 TimeZoneRequest::TimeZoneResponseCallback callback) {
31 TimeZoneRequest* request(new TimeZoneRequest(
32 url_context_getter_.get(), url_, position,timeout));
33 requests_.push_back(request);
35 // TimeZoneProvider owns all requests. It is safe to pass unretained "this"
36 // because destruction of TimeZoneProvider cancels all requests.
37 TimeZoneRequest::TimeZoneResponseCallback callback_tmp(
38 base::Bind(&TimeZoneProvider::OnTimezoneResponse,
39 base::Unretained(this),
40 request,
41 callback));
42 request->MakeRequest(callback_tmp);
45 void TimeZoneProvider::OnTimezoneResponse(
46 TimeZoneRequest* request,
47 TimeZoneRequest::TimeZoneResponseCallback callback,
48 scoped_ptr<TimeZoneResponseData> timezone,
49 bool server_error) {
50 ScopedVector<TimeZoneRequest>::iterator position =
51 std::find(requests_.begin(), requests_.end(), request);
52 DCHECK(position != requests_.end());
53 if (position != requests_.end()) {
54 std::swap(*position, *requests_.rbegin());
55 requests_.resize(requests_.size() - 1);
58 callback.Run(timezone.Pass(), server_error);
61 } // namespace chromeos