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 | |
Sorin Jianu | a64a1e5 | 2018-02-28 16:53:34 | [diff] [blame^] | 10 | #include <map> |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 11 | #include <memory> |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "base/callback.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 "net/url_request/url_fetcher_delegate.h" |
| 20 | #include "url/gurl.h" |
| 21 | |
mab | bb61b52a | 2016-03-17 23:37:23 | [diff] [blame] | 22 | namespace client_update_protocol { |
| 23 | class Ecdsa; |
| 24 | } |
| 25 | |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 26 | namespace net { |
| 27 | class URLFetcher; |
| 28 | } |
| 29 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 30 | namespace update_client { |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 31 | |
| 32 | class Configurator; |
| 33 | |
| 34 | // Sends a request to one of the urls provided. The class implements a chain |
| 35 | // of responsibility design pattern, where the urls are tried in the order they |
| 36 | // are specified, until the request to one of them succeeds or all have failed. |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 37 | // CUP signing is optional. |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 38 | class RequestSender : public net::URLFetcherDelegate { |
| 39 | public: |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 40 | // If |error| is 0, then the response is provided in the |response| parameter. |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 41 | // |retry_after_sec| contains the value of the X-Retry-After response header, |
| 42 | // when the response was received from a cryptographically secure URL. The |
| 43 | // range for this value is [-1, 86400]. If |retry_after_sec| is -1 it means |
| 44 | // that the header could not be found, or trusted, or had an invalid value. |
| 45 | // The upper bound represents a delay of one day. |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 46 | using RequestSenderCallback = base::OnceCallback< |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 47 | void(int error, const std::string& response, int retry_after_sec)>; |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 48 | |
Andrew Grieve | d1978b0e | 2017-07-28 15:53:41 | [diff] [blame] | 49 | // This value is chosen not to conflict with network errors defined by |
| 50 | // net/base/net_error_list.h. The callers don't have to handle this error in |
| 51 | // any meaningful way, but this value may be reported in UMA stats, therefore |
| 52 | // avoiding collisions with known network errors is desirable. |
| 53 | enum : int { kErrorResponseNotTrusted = -10000 }; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 54 | |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 55 | explicit RequestSender(scoped_refptr<Configurator> config); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 56 | ~RequestSender() override; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 57 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 58 | // |use_signing| enables CUP signing of protocol messages exchanged using |
Sorin Jianu | d907dbf6 | 2018-02-21 17:18:29 | [diff] [blame] | 59 | // this class. |is_foreground| controls the presence and the value for the |
| 60 | // X-GoogleUpdate-Interactvity header serialized in the protocol request. |
| 61 | // If this optional parameter is set, the values of "fg" or "bg" are sent |
| 62 | // for true or false values of this parameter. Otherwise the header is not |
| 63 | // sent at all. |
Sorin Jianu | a64a1e5 | 2018-02-28 16:53:34 | [diff] [blame^] | 64 | void Send(const std::vector<GURL>& urls, |
| 65 | const std::map<std::string, std::string>& request_extra_headers, |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 66 | const std::string& request_body, |
Sorin Jianu | a64a1e5 | 2018-02-28 16:53:34 | [diff] [blame^] | 67 | bool use_signing, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 68 | RequestSenderCallback request_sender_callback); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 69 | |
| 70 | private: |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 71 | // Combines the |url| and |query_params| parameters. |
| 72 | static GURL BuildUpdateUrl(const GURL& url, const std::string& query_params); |
| 73 | |
| 74 | // Decodes and returns the public key used by CUP. |
| 75 | static std::string GetKey(const char* key_bytes_base64); |
| 76 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 77 | // Returns the string value of a header of the server response or an empty |
| 78 | // string if the header is not available. |
| 79 | static std::string GetStringHeaderValue(const net::URLFetcher* source, |
| 80 | const char* header_name); |
| 81 | |
| 82 | // Returns the integral value of a header of the server response or -1 if |
| 83 | // if the header is not available or a conversion error has occured. |
| 84 | static int64_t GetInt64HeaderValue(const net::URLFetcher* source, |
| 85 | const char* header_name); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 86 | |
| 87 | // Overrides for URLFetcherDelegate. |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 88 | void OnURLFetchComplete(const net::URLFetcher* source) override; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 89 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 90 | // Implements the error handling and url fallback mechanism. |
| 91 | void SendInternal(); |
| 92 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 93 | // Called when SendInternal completes. |response_body| and |response_etag| |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 94 | // contain the body and the etag associated with the HTTP response. |
| 95 | void SendInternalComplete(int error, |
| 96 | const std::string& response_body, |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 97 | const std::string& response_etag, |
| 98 | int retry_after_sec); |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 99 | |
| 100 | // Helper function to handle a non-continuable error in Send. |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 101 | void HandleSendError(int error, int retry_after_sec); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 102 | |
waffles | 720946a | 2014-11-15 00:07:15 | [diff] [blame] | 103 | base::ThreadChecker thread_checker_; |
| 104 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 105 | const scoped_refptr<Configurator> config_; |
Sorin Jianu | a64a1e5 | 2018-02-28 16:53:34 | [diff] [blame^] | 106 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 107 | std::vector<GURL> urls_; |
Sorin Jianu | a64a1e5 | 2018-02-28 16:53:34 | [diff] [blame^] | 108 | std::map<std::string, std::string> request_extra_headers_; |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 109 | std::string request_body_; |
Sorin Jianu | a64a1e5 | 2018-02-28 16:53:34 | [diff] [blame^] | 110 | bool use_signing_; // True if CUP signing is used. |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 111 | RequestSenderCallback request_sender_callback_; |
| 112 | |
| 113 | std::string public_key_; |
| 114 | std::vector<GURL>::const_iterator cur_url_; |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 115 | std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 116 | std::unique_ptr<client_update_protocol::Ecdsa> signer_; |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame] | 117 | |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 118 | DISALLOW_COPY_AND_ASSIGN(RequestSender); |
| 119 | }; |
| 120 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 121 | } // namespace update_client |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 122 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 123 | #endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |