Fix crash on app list start page keyboard navigation with <4 apps.
[chromium-blink-merge.git] / chromeos / timezone / timezone_provider.cc
blob89c1548992f13621088b1a68724b2d898b67a2b1
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 "base/time/time.h"
13 #include "chromeos/geolocation/geoposition.h"
14 #include "net/url_request/url_request_context_getter.h"
15 #include "url/gurl.h"
17 namespace chromeos {
19 TimeZoneProvider::TimeZoneProvider(
20 net::URLRequestContextGetter* url_context_getter,
21 const GURL& url)
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),
42 request,
43 callback));
44 request->MakeRequest(callback_tmp);
47 void TimeZoneProvider::OnTimezoneResponse(
48 TimeZoneRequest* request,
49 TimeZoneRequest::TimeZoneResponseCallback callback,
50 scoped_ptr<TimeZoneResponseData> timezone,
51 bool server_error) {
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