sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 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. |
| 4 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 5 | #ifndef COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |
| 6 | #define COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/callback.h" |
| 12 | #include "base/compiler_specific.h" |
| 13 | #include "base/macros.h" |
sorin | 958b5d3 | 2016-01-09 02:00:20 | [diff] [blame^] | 14 | #include "base/memory/ref_counted.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
waffles | 720946a | 2014-11-15 00:07:15 | [diff] [blame] | 16 | #include "base/threading/thread_checker.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 17 | #include "net/url_request/url_fetcher_delegate.h" |
| 18 | #include "url/gurl.h" |
| 19 | |
| 20 | namespace net { |
| 21 | class URLFetcher; |
| 22 | } |
| 23 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 24 | namespace update_client { |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 25 | |
| 26 | class 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. |
| 31 | class 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 | |
sorin | 958b5d3 | 2016-01-09 02:00:20 | [diff] [blame^] | 38 | explicit RequestSender(const scoped_refptr<Configurator>& config); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 39 | ~RequestSender() override; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 40 | |
| 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. |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 49 | void OnURLFetchComplete(const net::URLFetcher* source) override; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 50 | |
sorin | 958b5d3 | 2016-01-09 02:00:20 | [diff] [blame^] | 51 | const scoped_refptr<Configurator> config_; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 52 | 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 | |
waffles | 720946a | 2014-11-15 00:07:15 | [diff] [blame] | 58 | base::ThreadChecker thread_checker_; |
| 59 | |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 60 | DISALLOW_COPY_AND_ASSIGN(RequestSender); |
| 61 | }; |
| 62 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 63 | } // namespace update_client |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 64 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 65 | #endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |