| 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 <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 16 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
| 17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 | 18 |
| 19 namespace net { | 19 namespace net { |
| 20 class URLFetcher; | 20 class URLFetcher; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace update_client { | 23 namespace update_client { |
| 24 | 24 |
| 25 class ClientUpdateProtocolEcdsa; |
| 25 class Configurator; | 26 class Configurator; |
| 26 | 27 |
| 27 // Sends a request to one of the urls provided. The class implements a chain | 28 // Sends a request to one of the urls provided. The class implements a chain |
| 28 // of responsibility design pattern, where the urls are tried in the order they | 29 // of responsibility design pattern, where the urls are tried in the order they |
| 29 // are specified, until the request to one of them succeeds or all have failed. | 30 // are specified, until the request to one of them succeeds or all have failed. |
| 31 // CUP signing is optional. |
| 30 class RequestSender : public net::URLFetcherDelegate { | 32 class RequestSender : public net::URLFetcherDelegate { |
| 31 public: | 33 public: |
| 32 // The |source| refers to the fetcher object used to make the request. This | 34 // If |error| is 0, then the response is provided in the |response| parameter. |
| 33 // parameter can be NULL in some error cases. | 35 using RequestSenderCallback = |
| 34 typedef base::Callback<void(const net::URLFetcher* source)> | 36 base::Callback<void(int error, const std::string& response)>; |
| 35 RequestSenderCallback; | 37 |
| 38 static int kErrorResponseNotTrusted; |
| 36 | 39 |
| 37 explicit RequestSender(const scoped_refptr<Configurator>& config); | 40 explicit RequestSender(const scoped_refptr<Configurator>& config); |
| 38 ~RequestSender() override; | 41 ~RequestSender() override; |
| 39 | 42 |
| 40 void Send(const std::string& request_string, | 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, |
| 41 const std::vector<GURL>& urls, | 47 const std::vector<GURL>& urls, |
| 42 const RequestSenderCallback& request_sender_callback); | 48 const RequestSenderCallback& request_sender_callback); |
| 43 | 49 |
| 44 private: | 50 private: |
| 45 void SendInternal(); | 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); |
| 46 | 60 |
| 47 // Overrides for URLFetcherDelegate. | 61 // Overrides for URLFetcherDelegate. |
| 48 void OnURLFetchComplete(const net::URLFetcher* source) override; | 62 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 49 | 63 |
| 50 const scoped_refptr<Configurator> config_; | 64 // Implements the error handling and url fallback mechanism. |
| 51 std::vector<GURL> urls_; | 65 void SendInternal(); |
| 52 std::vector<GURL>::const_iterator cur_url_; | 66 |
| 53 scoped_ptr<net::URLFetcher> url_fetcher_; | 67 // Called when SendInternal complets. |response_body|and |response_etag| |
| 54 std::string request_string_; | 68 // contain the body and the etag associated with the HTTP response. |
| 55 RequestSenderCallback request_sender_callback_; | 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); |
| 56 | 75 |
| 57 base::ThreadChecker thread_checker_; | 76 base::ThreadChecker thread_checker_; |
| 58 | 77 |
| 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 |
| 59 DISALLOW_COPY_AND_ASSIGN(RequestSender); | 89 DISALLOW_COPY_AND_ASSIGN(RequestSender); |
| 60 }; | 90 }; |
| 61 | 91 |
| 62 } // namespace update_client | 92 } // namespace update_client |
| 63 | 93 |
| 64 #endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ | 94 #endif // COMPONENTS_UPDATE_CLIENT_REQUEST_SENDER_H_ |
| OLD | NEW |