Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / update_client / request_sender.cc
blobf91cbabfecd103efd3508fdd5a191c002fa14326
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 "components/update_client/request_sender.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "components/update_client/configurator.h"
14 #include "components/update_client/utils.h"
15 #include "net/url_request/url_fetcher.h"
17 namespace update_client {
19 RequestSender::RequestSender(const Configurator& config) : config_(config) {
22 RequestSender::~RequestSender() {
23 DCHECK(thread_checker_.CalledOnValidThread());
26 void RequestSender::Send(const std::string& request_string,
27 const std::vector<GURL>& urls,
28 const RequestSenderCallback& request_sender_callback) {
29 DCHECK(thread_checker_.CalledOnValidThread());
30 if (urls.empty()) {
31 base::ThreadTaskRunnerHandle::Get()->PostTask(
32 FROM_HERE, base::Bind(request_sender_callback, nullptr));
33 return;
36 request_string_ = request_string;
37 urls_ = urls;
38 request_sender_callback_ = request_sender_callback;
40 cur_url_ = urls_.begin();
42 SendInternal();
45 void RequestSender::SendInternal() {
46 DCHECK(cur_url_ != urls_.end());
47 DCHECK(cur_url_->is_valid());
48 DCHECK(thread_checker_.CalledOnValidThread());
50 url_fetcher_ = SendProtocolRequest(*cur_url_, request_string_, this,
51 config_.RequestContext());
54 void RequestSender::OnURLFetchComplete(const net::URLFetcher* source) {
55 DCHECK(thread_checker_.CalledOnValidThread());
56 if (GetFetchError(*source) == 0) {
57 request_sender_callback_.Run(source);
58 return;
61 if (++cur_url_ != urls_.end() &&
62 base::ThreadTaskRunnerHandle::Get()->PostTask(
63 FROM_HERE,
64 base::Bind(&RequestSender::SendInternal, base::Unretained(this)))) {
65 return;
68 base::ThreadTaskRunnerHandle::Get()->PostTask(
69 FROM_HERE, base::Bind(request_sender_callback_, source));
72 } // namespace update_client