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 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 10 | #include <memory> |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "base/callback.h" |
Sorin Jianu | c303bf4 | 2018-09-07 16:19:33 | [diff] [blame] | 15 | #include "base/containers/flat_map.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 16 | #include "base/macros.h" |
sorin | 958b5d3 | 2016-01-09 02:00:20 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
waffles | 720946a | 2014-11-15 00:07:15 | [diff] [blame] | 18 | #include "base/threading/thread_checker.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 19 | #include "url/gurl.h" |
| 20 | |
mab | bb61b52a | 2016-03-17 23:37:23 | [diff] [blame] | 21 | namespace client_update_protocol { |
| 22 | class Ecdsa; |
| 23 | } |
| 24 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 25 | namespace update_client { |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 26 | |
| 27 | class Configurator; |
Sorin Jianu | 75e6bf2 | 2019-02-12 16:07:12 | [diff] [blame] | 28 | class NetworkFetcher; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 29 | |
| 30 | // Sends a request to one of the urls provided. The class implements a chain |
| 31 | // of responsibility design pattern, where the urls are tried in the order they |
| 32 | // are specified, until the request to one of them succeeds or all have failed. |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 33 | // CUP signing is optional. |
Antonio Gomes | b616250 | 2018-06-28 20:21:55 | [diff] [blame] | 34 | class RequestSender { |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 35 | public: |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 36 | // If |error| is 0, then the response is provided in the |response| parameter. |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 37 | // |retry_after_sec| contains the value of the X-Retry-After response header, |
| 38 | // when the response was received from a cryptographically secure URL. The |
| 39 | // range for this value is [-1, 86400]. If |retry_after_sec| is -1 it means |
| 40 | // that the header could not be found, or trusted, or had an invalid value. |
| 41 | // The upper bound represents a delay of one day. |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 42 | using RequestSenderCallback = base::OnceCallback< |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 43 | void(int error, const std::string& response, int retry_after_sec)>; |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 44 | |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 45 | explicit RequestSender(scoped_refptr<Configurator> config); |
Antonio Gomes | b616250 | 2018-06-28 20:21:55 | [diff] [blame] | 46 | ~RequestSender(); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 47 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 48 | // |use_signing| enables CUP signing of protocol messages exchanged using |
Sorin Jianu | d907dbf6 | 2018-02-21 17:18:29 | [diff] [blame] | 49 | // this class. |is_foreground| controls the presence and the value for the |
| 50 | // X-GoogleUpdate-Interactvity header serialized in the protocol request. |
| 51 | // If this optional parameter is set, the values of "fg" or "bg" are sent |
| 52 | // for true or false values of this parameter. Otherwise the header is not |
| 53 | // sent at all. |
Sorin Jianu | c303bf4 | 2018-09-07 16:19:33 | [diff] [blame] | 54 | void Send( |
| 55 | const std::vector<GURL>& urls, |
| 56 | const base::flat_map<std::string, std::string>& request_extra_headers, |
| 57 | const std::string& request_body, |
| 58 | bool use_signing, |
| 59 | RequestSenderCallback request_sender_callback); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 60 | |
| 61 | private: |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 62 | // Combines the |url| and |query_params| parameters. |
| 63 | static GURL BuildUpdateUrl(const GURL& url, const std::string& query_params); |
| 64 | |
| 65 | // Decodes and returns the public key used by CUP. |
| 66 | static std::string GetKey(const char* key_bytes_base64); |
| 67 | |
Joshua Pawlicki | 902de4d | 2019-11-05 20:55:27 | [diff] [blame] | 68 | void OnResponseStarted(int response_code, int64_t content_length); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 69 | |
Sorin Jianu | 75e6bf2 | 2019-02-12 16:07:12 | [diff] [blame] | 70 | void OnNetworkFetcherComplete(const GURL& original_url, |
Sorin Jianu | e97744a6 | 2019-02-20 21:05:20 | [diff] [blame] | 71 | std::unique_ptr<std::string> response_body, |
| 72 | int net_error, |
| 73 | const std::string& header_etag, |
| 74 | int64_t xheader_retry_after_sec); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 75 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 76 | // Implements the error handling and url fallback mechanism. |
| 77 | void SendInternal(); |
| 78 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 79 | // Called when SendInternal completes. |response_body| and |response_etag| |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 80 | // contain the body and the etag associated with the HTTP response. |
| 81 | void SendInternalComplete(int error, |
| 82 | const std::string& response_body, |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 83 | const std::string& response_etag, |
| 84 | int retry_after_sec); |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 85 | |
| 86 | // Helper function to handle a non-continuable error in Send. |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 87 | void HandleSendError(int error, int retry_after_sec); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 88 | |
waffles | 720946a | 2014-11-15 00:07:15 | [diff] [blame] | 89 | base::ThreadChecker thread_checker_; |
| 90 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 91 | const scoped_refptr<Configurator> config_; |
Sorin Jianu | a64a1e5 | 2018-02-28 16:53:34 | [diff] [blame] | 92 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 93 | std::vector<GURL> urls_; |
Sorin Jianu | c303bf4 | 2018-09-07 16:19:33 | [diff] [blame] | 94 | base::flat_map<std::string, std::string> request_extra_headers_; |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 95 | std::string request_body_; |
Sorin Jianu | 86ee558e | 2019-02-18 17:01:50 | [diff] [blame] | 96 | bool use_signing_ = false; // True if CUP signing is used. |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 97 | RequestSenderCallback request_sender_callback_; |
| 98 | |
| 99 | std::string public_key_; |
| 100 | std::vector<GURL>::const_iterator cur_url_; |
Sorin Jianu | 75e6bf2 | 2019-02-12 16:07:12 | [diff] [blame] | 101 | std::unique_ptr<NetworkFetcher> network_fetcher_; |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 102 | std::unique_ptr<client_update_protocol::Ecdsa> signer_; |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 103 | |
Sorin Jianu | 86ee558e | 2019-02-18 17:01:50 | [diff] [blame] | 104 | int response_code_ = -1; |
| 105 | |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 106 | DISALLOW_COPY_AND_ASSIGN(RequestSender); |
| 107 | }; |
| 108 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 109 | } // namespace update_client |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 110 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 111 | #endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |