Implement CUP signing in UpdateClient.

BUG=583027

Review URL: https://codereview.chromium.org/1685323002

Cr-Commit-Position: refs/heads/master@{#375930}
diff --git a/components/update_client/request_sender.h b/components/update_client/request_sender.h
index b6ec23b7..71b41ee5 100644
--- a/components/update_client/request_sender.h
+++ b/components/update_client/request_sender.h
@@ -22,40 +22,70 @@
 
 namespace update_client {
 
+class ClientUpdateProtocolEcdsa;
 class Configurator;
 
 // Sends a request to one of the urls provided. The class implements a chain
 // of responsibility design pattern, where the urls are tried in the order they
 // are specified, until the request to one of them succeeds or all have failed.
+// CUP signing is optional.
 class RequestSender : public net::URLFetcherDelegate {
  public:
-  // The |source| refers to the fetcher object used to make the request. This
-  // parameter can be NULL in some error cases.
-  typedef base::Callback<void(const net::URLFetcher* source)>
-      RequestSenderCallback;
+  // If |error| is 0, then the response is provided in the |response| parameter.
+  using RequestSenderCallback =
+      base::Callback<void(int error, const std::string& response)>;
+
+  static int kErrorResponseNotTrusted;
 
   explicit RequestSender(const scoped_refptr<Configurator>& config);
   ~RequestSender() override;
 
-  void Send(const std::string& request_string,
+  // |use_signing| enables CUP signing of protocol messages exchanged using
+  // this class.
+  void Send(bool use_signing,
+            const std::string& request_body,
             const std::vector<GURL>& urls,
             const RequestSenderCallback& request_sender_callback);
 
  private:
-  void SendInternal();
+  // Combines the |url| and |query_params| parameters.
+  static GURL BuildUpdateUrl(const GURL& url, const std::string& query_params);
+
+  // Decodes and returns the public key used by CUP.
+  static std::string GetKey(const char* key_bytes_base64);
+
+  // Returns the Etag of the server response or an empty string if the
+  // Etag is not available.
+  static std::string GetServerETag(const net::URLFetcher* source);
 
   // Overrides for URLFetcherDelegate.
   void OnURLFetchComplete(const net::URLFetcher* source) override;
 
-  const scoped_refptr<Configurator> config_;
-  std::vector<GURL> urls_;
-  std::vector<GURL>::const_iterator cur_url_;
-  scoped_ptr<net::URLFetcher> url_fetcher_;
-  std::string request_string_;
-  RequestSenderCallback request_sender_callback_;
+  // Implements the error handling and url fallback mechanism.
+  void SendInternal();
+
+  // Called when SendInternal complets. |response_body|and |response_etag|
+  // contain the body and the etag associated with the HTTP response.
+  void SendInternalComplete(int error,
+                            const std::string& response_body,
+                            const std::string& response_etag);
+
+  // Helper function to handle a non-continuable error in Send.
+  void HandleSendError(int error);
 
   base::ThreadChecker thread_checker_;
 
+  const scoped_refptr<Configurator> config_;
+  bool use_signing_;
+  std::vector<GURL> urls_;
+  std::string request_body_;
+  RequestSenderCallback request_sender_callback_;
+
+  std::string public_key_;
+  std::vector<GURL>::const_iterator cur_url_;
+  scoped_ptr<net::URLFetcher> url_fetcher_;
+  scoped_ptr<ClientUpdateProtocolEcdsa> signer_;
+
   DISALLOW_COPY_AND_ASSIGN(RequestSender);
 };