blob: f833fe46db3713e9e1e887d61aa2d4e06bd0376b [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"
sorin395c2ac2014-09-16 21:31:0715#include "base/macros.h"
sorin958b5d32016-01-09 02:00:2016#include "base/memory/ref_counted.h"
Sorin Jianud907dbf62018-02-21 17:18:2917#include "base/optional.h"
waffles720946a2014-11-15 00:07:1518#include "base/threading/thread_checker.h"
sorin395c2ac2014-09-16 21:31:0719#include "net/url_request/url_fetcher_delegate.h"
20#include "url/gurl.h"
21
mabbb61b52a2016-03-17 23:37:2322namespace client_update_protocol {
23class Ecdsa;
24}
25
sorin395c2ac2014-09-16 21:31:0726namespace net {
27class URLFetcher;
28}
29
sorin52ac0882015-01-24 01:15:0030namespace update_client {
sorin395c2ac2014-09-16 21:31:0731
32class 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.
sorin1bc5eff2016-02-17 18:45:1737// CUP signing is optional.
sorin395c2ac2014-09-16 21:31:0738class RequestSender : public net::URLFetcherDelegate {
39 public:
sorin1bc5eff2016-02-17 18:45:1740 // If |error| is 0, then the response is provided in the |response| parameter.
sorinfccbf2d2016-04-04 20:34:3441 // |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 Jianua8ef73d2017-11-02 16:55:1746 using RequestSenderCallback = base::OnceCallback<
sorinfccbf2d2016-04-04 20:34:3447 void(int error, const std::string& response, int retry_after_sec)>;
sorin1bc5eff2016-02-17 18:45:1748
Andrew Grieved1978b0e2017-07-28 15:53:4149 // 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 };
sorin395c2ac2014-09-16 21:31:0754
Sorin Jianu49126332018-02-13 17:07:4255 explicit RequestSender(scoped_refptr<Configurator> config);
dcheng00ea022b2014-10-21 11:24:5656 ~RequestSender() override;
sorin395c2ac2014-09-16 21:31:0757
sorin1bc5eff2016-02-17 18:45:1758 // |use_signing| enables CUP signing of protocol messages exchanged using
Sorin Jianud907dbf62018-02-21 17:18:2959 // 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.
sorin1bc5eff2016-02-17 18:45:1764 void Send(bool use_signing,
65 const std::string& request_body,
Sorin Jianud907dbf62018-02-21 17:18:2966 base::Optional<bool> is_foreground,
sorin395c2ac2014-09-16 21:31:0767 const std::vector<GURL>& urls,
Sorin Jianua8ef73d2017-11-02 16:55:1768 RequestSenderCallback request_sender_callback);
sorin395c2ac2014-09-16 21:31:0769
70 private:
sorin1bc5eff2016-02-17 18:45:1771 // 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
sorinfccbf2d2016-04-04 20:34:3477 // 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);
sorin395c2ac2014-09-16 21:31:0786
87 // Overrides for URLFetcherDelegate.
dcheng00ea022b2014-10-21 11:24:5688 void OnURLFetchComplete(const net::URLFetcher* source) override;
sorin395c2ac2014-09-16 21:31:0789
sorin1bc5eff2016-02-17 18:45:1790 // Implements the error handling and url fallback mechanism.
91 void SendInternal();
92
sorinfccbf2d2016-04-04 20:34:3493 // Called when SendInternal completes. |response_body| and |response_etag|
sorin1bc5eff2016-02-17 18:45:1794 // contain the body and the etag associated with the HTTP response.
95 void SendInternalComplete(int error,
96 const std::string& response_body,
sorinfccbf2d2016-04-04 20:34:3497 const std::string& response_etag,
98 int retry_after_sec);
sorin1bc5eff2016-02-17 18:45:1799
100 // Helper function to handle a non-continuable error in Send.
sorinfccbf2d2016-04-04 20:34:34101 void HandleSendError(int error, int retry_after_sec);
sorin395c2ac2014-09-16 21:31:07102
waffles720946a2014-11-15 00:07:15103 base::ThreadChecker thread_checker_;
104
sorin1bc5eff2016-02-17 18:45:17105 const scoped_refptr<Configurator> config_;
sorinfccbf2d2016-04-04 20:34:34106 bool use_signing_; // True if CUP signing is used.
sorin1bc5eff2016-02-17 18:45:17107 std::vector<GURL> urls_;
108 std::string request_body_;
Sorin Jianud907dbf62018-02-21 17:18:29109 base::Optional<bool> is_foreground_;
sorin1bc5eff2016-02-17 18:45:17110 RequestSenderCallback request_sender_callback_;
111
112 std::string public_key_;
113 std::vector<GURL>::const_iterator cur_url_;
dchengd0fc6aa92016-04-22 18:03:12114 std::unique_ptr<net::URLFetcher> url_fetcher_;
115 std::unique_ptr<client_update_protocol::Ecdsa> signer_;
sorin1bc5eff2016-02-17 18:45:17116
sorin395c2ac2014-09-16 21:31:07117 DISALLOW_COPY_AND_ASSIGN(RequestSender);
118};
119
sorin52ac0882015-01-24 01:15:00120} // namespace update_client
sorin395c2ac2014-09-16 21:31:07121
sorin52ac0882015-01-24 01:15:00122#endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_