blob: cc568fda380fa9ac8c08e640aad62dca69cc0497 [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
sorinfccbf2d2016-04-04 20:34:348#include <stdint.h>
9
dchengd0fc6aa92016-04-22 18:03:1210#include <memory>
sorin395c2ac2014-09-16 21:31:0711#include <string>
12#include <vector>
13
14#include "base/callback.h"
Sorin Jianuc303bf42018-09-07 16:19:3315#include "base/containers/flat_map.h"
sorin395c2ac2014-09-16 21:31:0716#include "base/macros.h"
sorin958b5d32016-01-09 02:00:2017#include "base/memory/ref_counted.h"
waffles720946a2014-11-15 00:07:1518#include "base/threading/thread_checker.h"
sorin395c2ac2014-09-16 21:31:0719#include "url/gurl.h"
20
mabbb61b52a2016-03-17 23:37:2321namespace client_update_protocol {
22class Ecdsa;
23}
24
sorin52ac0882015-01-24 01:15:0025namespace update_client {
sorin395c2ac2014-09-16 21:31:0726
27class Configurator;
Sorin Jianu75e6bf22019-02-12 16:07:1228class NetworkFetcher;
sorin395c2ac2014-09-16 21:31:0729
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.
sorin1bc5eff2016-02-17 18:45:1733// CUP signing is optional.
Antonio Gomesb6162502018-06-28 20:21:5534class RequestSender {
sorin395c2ac2014-09-16 21:31:0735 public:
sorin1bc5eff2016-02-17 18:45:1736 // If |error| is 0, then the response is provided in the |response| parameter.
sorinfccbf2d2016-04-04 20:34:3437 // |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 Jianua8ef73d2017-11-02 16:55:1742 using RequestSenderCallback = base::OnceCallback<
sorinfccbf2d2016-04-04 20:34:3443 void(int error, const std::string& response, int retry_after_sec)>;
sorin1bc5eff2016-02-17 18:45:1744
Sorin Jianu49126332018-02-13 17:07:4245 explicit RequestSender(scoped_refptr<Configurator> config);
Antonio Gomesb6162502018-06-28 20:21:5546 ~RequestSender();
sorin395c2ac2014-09-16 21:31:0747
sorin1bc5eff2016-02-17 18:45:1748 // |use_signing| enables CUP signing of protocol messages exchanged using
Sorin Jianud907dbf62018-02-21 17:18:2949 // 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 Jianuc303bf42018-09-07 16:19:3354 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);
sorin395c2ac2014-09-16 21:31:0760
61 private:
sorin1bc5eff2016-02-17 18:45:1762 // 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 Pawlicki902de4d2019-11-05 20:55:2768 void OnResponseStarted(int response_code, int64_t content_length);
sorin395c2ac2014-09-16 21:31:0769
Sorin Jianu75e6bf22019-02-12 16:07:1270 void OnNetworkFetcherComplete(const GURL& original_url,
Sorin Jianue97744a62019-02-20 21:05:2071 std::unique_ptr<std::string> response_body,
72 int net_error,
73 const std::string& header_etag,
74 int64_t xheader_retry_after_sec);
sorin395c2ac2014-09-16 21:31:0775
sorin1bc5eff2016-02-17 18:45:1776 // Implements the error handling and url fallback mechanism.
77 void SendInternal();
78
sorinfccbf2d2016-04-04 20:34:3479 // Called when SendInternal completes. |response_body| and |response_etag|
sorin1bc5eff2016-02-17 18:45:1780 // contain the body and the etag associated with the HTTP response.
81 void SendInternalComplete(int error,
82 const std::string& response_body,
sorinfccbf2d2016-04-04 20:34:3483 const std::string& response_etag,
84 int retry_after_sec);
sorin1bc5eff2016-02-17 18:45:1785
86 // Helper function to handle a non-continuable error in Send.
sorinfccbf2d2016-04-04 20:34:3487 void HandleSendError(int error, int retry_after_sec);
sorin395c2ac2014-09-16 21:31:0788
waffles720946a2014-11-15 00:07:1589 base::ThreadChecker thread_checker_;
90
sorin1bc5eff2016-02-17 18:45:1791 const scoped_refptr<Configurator> config_;
Sorin Jianua64a1e52018-02-28 16:53:3492
sorin1bc5eff2016-02-17 18:45:1793 std::vector<GURL> urls_;
Sorin Jianuc303bf42018-09-07 16:19:3394 base::flat_map<std::string, std::string> request_extra_headers_;
sorin1bc5eff2016-02-17 18:45:1795 std::string request_body_;
Sorin Jianu86ee558e2019-02-18 17:01:5096 bool use_signing_ = false; // True if CUP signing is used.
sorin1bc5eff2016-02-17 18:45:1797 RequestSenderCallback request_sender_callback_;
98
99 std::string public_key_;
100 std::vector<GURL>::const_iterator cur_url_;
Sorin Jianu75e6bf22019-02-12 16:07:12101 std::unique_ptr<NetworkFetcher> network_fetcher_;
dchengd0fc6aa92016-04-22 18:03:12102 std::unique_ptr<client_update_protocol::Ecdsa> signer_;
sorin1bc5eff2016-02-17 18:45:17103
Sorin Jianu86ee558e2019-02-18 17:01:50104 int response_code_ = -1;
105
sorin395c2ac2014-09-16 21:31:07106 DISALLOW_COPY_AND_ASSIGN(RequestSender);
107};
108
sorin52ac0882015-01-24 01:15:00109} // namespace update_client
sorin395c2ac2014-09-16 21:31:07110
sorin52ac0882015-01-24 01:15:00111#endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_