blob: 08e10e5359820e2b14efe859c3589f9cf00feac1 [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/timezone/timezone_provider.h"
#include <algorithm>
#include <iterator>
#include "base/bind.h"
#include "base/logging.h"
#include "chromeos/geolocation/geoposition.h"
#include "net/url_request/url_request_context_getter.h"
namespace chromeos {
TimeZoneProvider::TimeZoneProvider(
net::URLRequestContextGetter* url_context_getter,
const GURL& url)
: url_context_getter_(url_context_getter), url_(url) {
}
TimeZoneProvider::~TimeZoneProvider() {
DCHECK(thread_checker_.CalledOnValidThread());
}
void TimeZoneProvider::RequestTimezone(
const Geoposition& position,
base::TimeDelta timeout,
TimeZoneRequest::TimeZoneResponseCallback callback) {
TimeZoneRequest* request(new TimeZoneRequest(
url_context_getter_.get(), url_, position,timeout));
requests_.push_back(request);
// TimeZoneProvider owns all requests. It is safe to pass unretained "this"
// because destruction of TimeZoneProvider cancels all requests.
TimeZoneRequest::TimeZoneResponseCallback callback_tmp(
base::Bind(&TimeZoneProvider::OnTimezoneResponse,
base::Unretained(this),
request,
callback));
request->MakeRequest(callback_tmp);
}
void TimeZoneProvider::OnTimezoneResponse(
TimeZoneRequest* request,
TimeZoneRequest::TimeZoneResponseCallback callback,
scoped_ptr<TimeZoneResponseData> timezone,
bool server_error) {
ScopedVector<TimeZoneRequest>::iterator position =
std::find(requests_.begin(), requests_.end(), request);
DCHECK(position != requests_.end());
if (position != requests_.end()) {
std::swap(*position, *requests_.rbegin());
requests_.resize(requests_.size() - 1);
}
callback.Run(timezone.Pass(), server_error);
}
} // namespace chromeos