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 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/callback.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 12 | #include "base/macros.h" |
sorin | 958b5d3 | 2016-01-09 02:00:20 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
waffles | 720946a | 2014-11-15 00:07:15 | [diff] [blame] | 15 | #include "base/threading/thread_checker.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 16 | #include "net/url_request/url_fetcher_delegate.h" |
| 17 | #include "url/gurl.h" |
| 18 | |
| 19 | namespace net { |
| 20 | class URLFetcher; |
| 21 | } |
| 22 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 23 | namespace update_client { |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 24 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame^] | 25 | class ClientUpdateProtocolEcdsa; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 26 | class 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. |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame^] | 31 | // CUP signing is optional. |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 32 | class RequestSender : public net::URLFetcherDelegate { |
| 33 | public: |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame^] | 34 | // 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; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 39 | |
sorin | 958b5d3 | 2016-01-09 02:00:20 | [diff] [blame] | 40 | explicit RequestSender(const scoped_refptr<Configurator>& config); |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 41 | ~RequestSender() override; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 42 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame^] | 43 | // |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, |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 47 | const std::vector<GURL>& urls, |
| 48 | const RequestSenderCallback& request_sender_callback); |
| 49 | |
| 50 | private: |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame^] | 51 | // 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); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 60 | |
| 61 | // Overrides for URLFetcherDelegate. |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 62 | void OnURLFetchComplete(const net::URLFetcher* source) override; |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 63 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame^] | 64 | // 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); |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 75 | |
waffles | 720946a | 2014-11-15 00:07:15 | [diff] [blame] | 76 | base::ThreadChecker thread_checker_; |
| 77 | |
sorin | 1bc5eff | 2016-02-17 18:45:17 | [diff] [blame^] | 78 | 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 | |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 89 | DISALLOW_COPY_AND_ASSIGN(RequestSender); |
| 90 | }; |
| 91 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 92 | } // namespace update_client |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 93 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 94 | #endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |