blob: 71b41ee538dc56f4d92b74fff9e0a5dc620d1e05 [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
8#include <string>
9#include <vector>
10
11#include "base/callback.h"
sorin395c2ac2014-09-16 21:31:0712#include "base/macros.h"
sorin958b5d32016-01-09 02:00:2013#include "base/memory/ref_counted.h"
sorin395c2ac2014-09-16 21:31:0714#include "base/memory/scoped_ptr.h"
waffles720946a2014-11-15 00:07:1515#include "base/threading/thread_checker.h"
sorin395c2ac2014-09-16 21:31:0716#include "net/url_request/url_fetcher_delegate.h"
17#include "url/gurl.h"
18
19namespace net {
20class URLFetcher;
21}
22
sorin52ac0882015-01-24 01:15:0023namespace update_client {
sorin395c2ac2014-09-16 21:31:0724
sorin1bc5eff2016-02-17 18:45:1725class ClientUpdateProtocolEcdsa;
sorin395c2ac2014-09-16 21:31:0726class Configurator;
27
28// Sends a request to one of the urls provided. The class implements a chain
29// of responsibility design pattern, where the urls are tried in the order they
30// are specified, until the request to one of them succeeds or all have failed.
sorin1bc5eff2016-02-17 18:45:1731// CUP signing is optional.
sorin395c2ac2014-09-16 21:31:0732class RequestSender : public net::URLFetcherDelegate {
33 public:
sorin1bc5eff2016-02-17 18:45:1734 // If |error| is 0, then the response is provided in the |response| parameter.
35 using RequestSenderCallback =
36 base::Callback<void(int error, const std::string& response)>;
37
38 static int kErrorResponseNotTrusted;
sorin395c2ac2014-09-16 21:31:0739
sorin958b5d32016-01-09 02:00:2040 explicit RequestSender(const scoped_refptr<Configurator>& config);
dcheng00ea022b2014-10-21 11:24:5641 ~RequestSender() override;
sorin395c2ac2014-09-16 21:31:0742
sorin1bc5eff2016-02-17 18:45:1743 // |use_signing| enables CUP signing of protocol messages exchanged using
44 // this class.
45 void Send(bool use_signing,
46 const std::string& request_body,
sorin395c2ac2014-09-16 21:31:0747 const std::vector<GURL>& urls,
48 const RequestSenderCallback& request_sender_callback);
49
50 private:
sorin1bc5eff2016-02-17 18:45:1751 // Combines the |url| and |query_params| parameters.
52 static GURL BuildUpdateUrl(const GURL& url, const std::string& query_params);
53
54 // Decodes and returns the public key used by CUP.
55 static std::string GetKey(const char* key_bytes_base64);
56
57 // Returns the Etag of the server response or an empty string if the
58 // Etag is not available.
59 static std::string GetServerETag(const net::URLFetcher* source);
sorin395c2ac2014-09-16 21:31:0760
61 // Overrides for URLFetcherDelegate.
dcheng00ea022b2014-10-21 11:24:5662 void OnURLFetchComplete(const net::URLFetcher* source) override;
sorin395c2ac2014-09-16 21:31:0763
sorin1bc5eff2016-02-17 18:45:1764 // Implements the error handling and url fallback mechanism.
65 void SendInternal();
66
67 // Called when SendInternal complets. |response_body|and |response_etag|
68 // contain the body and the etag associated with the HTTP response.
69 void SendInternalComplete(int error,
70 const std::string& response_body,
71 const std::string& response_etag);
72
73 // Helper function to handle a non-continuable error in Send.
74 void HandleSendError(int error);
sorin395c2ac2014-09-16 21:31:0775
waffles720946a2014-11-15 00:07:1576 base::ThreadChecker thread_checker_;
77
sorin1bc5eff2016-02-17 18:45:1778 const scoped_refptr<Configurator> config_;
79 bool use_signing_;
80 std::vector<GURL> urls_;
81 std::string request_body_;
82 RequestSenderCallback request_sender_callback_;
83
84 std::string public_key_;
85 std::vector<GURL>::const_iterator cur_url_;
86 scoped_ptr<net::URLFetcher> url_fetcher_;
87 scoped_ptr<ClientUpdateProtocolEcdsa> signer_;
88
sorin395c2ac2014-09-16 21:31:0789 DISALLOW_COPY_AND_ASSIGN(RequestSender);
90};
91
sorin52ac0882015-01-24 01:15:0092} // namespace update_client
sorin395c2ac2014-09-16 21:31:0793
sorin52ac0882015-01-24 01:15:0094#endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_