| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ | 5 #ifndef COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |
| 6 #define COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ | 6 #define COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> |
| 9 |
| 8 #include <string> | 10 #include <string> |
| 9 #include <vector> | 11 #include <vector> |
| 10 | 12 |
| 11 #include "base/callback.h" | 13 #include "base/callback.h" |
| 12 #include "base/macros.h" | 14 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 16 #include "net/url_request/url_fetcher_delegate.h" | 18 #include "net/url_request/url_fetcher_delegate.h" |
| 17 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 18 | 20 |
| 19 namespace net { | 21 namespace net { |
| 20 class URLFetcher; | 22 class URLFetcher; |
| 21 } | 23 } |
| 22 | 24 |
| 23 namespace update_client { | 25 namespace update_client { |
| 24 | 26 |
| 25 class ClientUpdateProtocolEcdsa; | 27 class ClientUpdateProtocolEcdsa; |
| 26 class Configurator; | 28 class Configurator; |
| 27 | 29 |
| 28 // Sends a request to one of the urls provided. The class implements a chain | 30 // 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 | 31 // 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. | 32 // are specified, until the request to one of them succeeds or all have failed. |
| 31 // CUP signing is optional. | 33 // CUP signing is optional. |
| 32 class RequestSender : public net::URLFetcherDelegate { | 34 class RequestSender : public net::URLFetcherDelegate { |
| 33 public: | 35 public: |
| 34 // If |error| is 0, then the response is provided in the |response| parameter. | 36 // If |error| is 0, then the response is provided in the |response| parameter. |
| 35 using RequestSenderCallback = | 37 // |retry_after_sec| contains the value of the X-Retry-After response header, |
| 36 base::Callback<void(int error, const std::string& response)>; | 38 // when the response was received from a cryptografically secure URL. The |
| 39 // range for this value is [0, 86400], where the upper bound represents one |
| 40 // day, and 0 means that no retry policy should be in effect. |
| 41 using RequestSenderCallback = base::Callback< |
| 42 void(int error, const std::string& response, int retry_after_sec)>; |
| 37 | 43 |
| 38 static int kErrorResponseNotTrusted; | 44 static int kErrorResponseNotTrusted; |
| 39 | 45 |
| 40 explicit RequestSender(const scoped_refptr<Configurator>& config); | 46 explicit RequestSender(const scoped_refptr<Configurator>& config); |
| 41 ~RequestSender() override; | 47 ~RequestSender() override; |
| 42 | 48 |
| 43 // |use_signing| enables CUP signing of protocol messages exchanged using | 49 // |use_signing| enables CUP signing of protocol messages exchanged using |
| 44 // this class. | 50 // this class. |
| 45 void Send(bool use_signing, | 51 void Send(bool use_signing, |
| 46 const std::string& request_body, | 52 const std::string& request_body, |
| 47 const std::vector<GURL>& urls, | 53 const std::vector<GURL>& urls, |
| 48 const RequestSenderCallback& request_sender_callback); | 54 const RequestSenderCallback& request_sender_callback); |
| 49 | 55 |
| 50 private: | 56 private: |
| 51 // Combines the |url| and |query_params| parameters. | 57 // Combines the |url| and |query_params| parameters. |
| 52 static GURL BuildUpdateUrl(const GURL& url, const std::string& query_params); | 58 static GURL BuildUpdateUrl(const GURL& url, const std::string& query_params); |
| 53 | 59 |
| 54 // Decodes and returns the public key used by CUP. | 60 // Decodes and returns the public key used by CUP. |
| 55 static std::string GetKey(const char* key_bytes_base64); | 61 static std::string GetKey(const char* key_bytes_base64); |
| 56 | 62 |
| 57 // Returns the Etag of the server response or an empty string if the | 63 // Returns the string value of a header of the server response or an empty |
| 58 // Etag is not available. | 64 // string if the header is not available. |
| 59 static std::string GetServerETag(const net::URLFetcher* source); | 65 static std::string GetStringHeaderValue(const net::URLFetcher* source, |
| 66 const char* header_name); |
| 67 |
| 68 // Returns the integral value of a header of the server response or -1 if |
| 69 // if the header is not available or a conversion error has occured. |
| 70 static int64_t GetInt64HeaderValue(const net::URLFetcher* source, |
| 71 const char* header_name); |
| 60 | 72 |
| 61 // Overrides for URLFetcherDelegate. | 73 // Overrides for URLFetcherDelegate. |
| 62 void OnURLFetchComplete(const net::URLFetcher* source) override; | 74 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 63 | 75 |
| 64 // Implements the error handling and url fallback mechanism. | 76 // Implements the error handling and url fallback mechanism. |
| 65 void SendInternal(); | 77 void SendInternal(); |
| 66 | 78 |
| 67 // Called when SendInternal complets. |response_body|and |response_etag| | 79 // Called when SendInternal complets. |response_body|and |response_etag| |
| 68 // contain the body and the etag associated with the HTTP response. | 80 // contain the body and the etag associated with the HTTP response. |
| 69 void SendInternalComplete(int error, | 81 void SendInternalComplete(int error, |
| 70 const std::string& response_body, | 82 const std::string& response_body, |
| 71 const std::string& response_etag); | 83 const std::string& response_etag, |
| 84 int retry_after_sec); |
| 72 | 85 |
| 73 // Helper function to handle a non-continuable error in Send. | 86 // Helper function to handle a non-continuable error in Send. |
| 74 void HandleSendError(int error); | 87 void HandleSendError(int error, int retry_after_sec); |
| 75 | 88 |
| 76 base::ThreadChecker thread_checker_; | 89 base::ThreadChecker thread_checker_; |
| 77 | 90 |
| 78 const scoped_refptr<Configurator> config_; | 91 const scoped_refptr<Configurator> config_; |
| 79 bool use_signing_; | 92 bool use_signing_; |
| 80 std::vector<GURL> urls_; | 93 std::vector<GURL> urls_; |
| 81 std::string request_body_; | 94 std::string request_body_; |
| 82 RequestSenderCallback request_sender_callback_; | 95 RequestSenderCallback request_sender_callback_; |
| 83 | 96 |
| 84 std::string public_key_; | 97 std::string public_key_; |
| 85 std::vector<GURL>::const_iterator cur_url_; | 98 std::vector<GURL>::const_iterator cur_url_; |
| 86 scoped_ptr<net::URLFetcher> url_fetcher_; | 99 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 87 scoped_ptr<ClientUpdateProtocolEcdsa> signer_; | 100 scoped_ptr<ClientUpdateProtocolEcdsa> signer_; |
| 88 | 101 |
| 89 DISALLOW_COPY_AND_ASSIGN(RequestSender); | 102 DISALLOW_COPY_AND_ASSIGN(RequestSender); |
| 90 }; | 103 }; |
| 91 | 104 |
| 92 } // namespace update_client | 105 } // namespace update_client |
| 93 | 106 |
| 94 #endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ | 107 #endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |
| OLD | NEW |