blob: 7b94143540831e74eda8942efe026792d73ff2c1 [file] [log] [blame]
sorin395c2ac2014-09-16 21:31:071// 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.
4
sorin52ac0882015-01-24 01:15:005#ifndef COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_
6#define COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_
sorin395c2ac2014-09-16 21:31:077
8#include <string>
9#include <vector>
10
11#include "base/callback.h"
12#include "base/compiler_specific.h"
13#include "base/macros.h"
sorin958b5d32016-01-09 02:00:2014#include "base/memory/ref_counted.h"
sorin395c2ac2014-09-16 21:31:0715#include "base/memory/scoped_ptr.h"
waffles720946a2014-11-15 00:07:1516#include "base/threading/thread_checker.h"
sorin395c2ac2014-09-16 21:31:0717#include "net/url_request/url_fetcher_delegate.h"
18#include "url/gurl.h"
19
20namespace net {
21class URLFetcher;
22}
23
sorin52ac0882015-01-24 01:15:0024namespace update_client {
sorin395c2ac2014-09-16 21:31:0725
26class Configurator;
27
28// Sends a request to one of the urls provided. The class implements a chain
29// of responsibility design pattern, where the urls are tried in the order they
30// are specified, until the request to one of them succeeds or all have failed.
31class RequestSender : public net::URLFetcherDelegate {
32 public:
33 // The |source| refers to the fetcher object used to make the request. This
34 // parameter can be NULL in some error cases.
35 typedef base::Callback<void(const net::URLFetcher* source)>
36 RequestSenderCallback;
37
sorin958b5d32016-01-09 02:00:2038 explicit RequestSender(const scoped_refptr<Configurator>& config);
dcheng00ea022b2014-10-21 11:24:5639 ~RequestSender() override;
sorin395c2ac2014-09-16 21:31:0740
41 void Send(const std::string& request_string,
42 const std::vector<GURL>& urls,
43 const RequestSenderCallback& request_sender_callback);
44
45 private:
46 void SendInternal();
47
48 // Overrides for URLFetcherDelegate.
dcheng00ea022b2014-10-21 11:24:5649 void OnURLFetchComplete(const net::URLFetcher* source) override;
sorin395c2ac2014-09-16 21:31:0750
sorin958b5d32016-01-09 02:00:2051 const scoped_refptr<Configurator> config_;
sorin395c2ac2014-09-16 21:31:0752 std::vector<GURL> urls_;
53 std::vector<GURL>::const_iterator cur_url_;
54 scoped_ptr<net::URLFetcher> url_fetcher_;
55 std::string request_string_;
56 RequestSenderCallback request_sender_callback_;
57
waffles720946a2014-11-15 00:07:1558 base::ThreadChecker thread_checker_;
59
sorin395c2ac2014-09-16 21:31:0760 DISALLOW_COPY_AND_ASSIGN(RequestSender);
61};
62
sorin52ac0882015-01-24 01:15:0063} // namespace update_client
sorin395c2ac2014-09-16 21:31:0764
sorin52ac0882015-01-24 01:15:0065#endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_