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"
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/time/time.h"
13 #include "chromeos/geolocation/geoposition.h"
14 #include "net/url_request/url_request_context_getter.h"
19 TimeZoneProvider::TimeZoneProvider(
20 net::URLRequestContextGetter
* url_context_getter
,
22 : url_context_getter_(url_context_getter
), url_(url
) {
25 TimeZoneProvider::~TimeZoneProvider() {
26 DCHECK(thread_checker_
.CalledOnValidThread());
29 void TimeZoneProvider::RequestTimezone(
30 const Geoposition
& position
,
31 base::TimeDelta timeout
,
32 TimeZoneRequest::TimeZoneResponseCallback callback
) {
33 TimeZoneRequest
* request(new TimeZoneRequest(
34 url_context_getter_
.get(), url_
, position
,timeout
));
35 requests_
.push_back(request
);
37 // TimeZoneProvider owns all requests. It is safe to pass unretained "this"
38 // because destruction of TimeZoneProvider cancels all requests.
39 TimeZoneRequest::TimeZoneResponseCallback
callback_tmp(
40 base::Bind(&TimeZoneProvider::OnTimezoneResponse
,
41 base::Unretained(this),
44 request
->MakeRequest(callback_tmp
);
47 void TimeZoneProvider::OnTimezoneResponse(
48 TimeZoneRequest
* request
,
49 TimeZoneRequest::TimeZoneResponseCallback callback
,
50 scoped_ptr
<TimeZoneResponseData
> timezone
,
52 ScopedVector
<TimeZoneRequest
>::iterator new_end
=
53 std::remove(requests_
.begin(), requests_
.end(), request
);
54 DCHECK_EQ(std::distance(new_end
, requests_
.end()), 1);
55 requests_
.erase(new_end
, requests_
.end());
57 callback
.Run(timezone
.Pass(), server_error
);
60 } // namespace chromeos