Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS 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 CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ | 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ |
| 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ | 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include <base/basictypes.h> | 12 #include <base/basictypes.h> |
| 13 #include <base/logging.h> | 13 #include <base/logging.h> |
| 14 #include <glib.h> | 14 #include <glib.h> |
| 15 #include <google/protobuf/stubs/common.h> | |
| 15 | 16 |
| 16 #include "update_engine/proxy_resolver.h" | 17 #include "update_engine/proxy_resolver.h" |
| 17 | 18 |
| 18 // This class is a simple wrapper around an HTTP library (libcurl). We can | 19 // This class is a simple wrapper around an HTTP library (libcurl). We can |
| 19 // easily mock out this interface for testing. | 20 // easily mock out this interface for testing. |
| 20 | 21 |
| 21 // Implementations of this class should use asynchronous i/o. They can access | 22 // Implementations of this class should use asynchronous i/o. They can access |
| 22 // the glib main loop to request callbacks when timers or file descriptors | 23 // the glib main loop to request callbacks when timers or file descriptors |
| 23 // change. | 24 // change. |
| 24 | 25 |
| 25 namespace chromeos_update_engine { | 26 namespace chromeos_update_engine { |
| 26 | 27 |
| 27 class HttpFetcherDelegate; | 28 class HttpFetcherDelegate; |
| 28 | 29 |
| 29 class HttpFetcher { | 30 class HttpFetcher { |
| 30 public: | 31 public: |
| 31 // |proxy_resolver| is the resolver that will be consulted for proxy | 32 // |proxy_resolver| is the resolver that will be consulted for proxy |
| 32 // settings. It may be null, in which case direct connections will | 33 // settings. It may be null, in which case direct connections will |
| 33 // be used. Does not take ownership of the resolver. | 34 // be used. Does not take ownership of the resolver. |
| 34 explicit HttpFetcher(ProxyResolver* proxy_resolver) | 35 explicit HttpFetcher(ProxyResolver* proxy_resolver) |
| 35 : post_data_set_(false), | 36 : post_data_set_(false), |
| 36 http_response_code_(0), | 37 http_response_code_(0), |
| 37 delegate_(NULL), | 38 delegate_(NULL), |
| 38 proxies_(1, kNoProxy), | 39 proxies_(1, kNoProxy), |
| 39 proxy_resolver_(proxy_resolver) {} | 40 proxy_resolver_(proxy_resolver), |
| 40 virtual ~HttpFetcher() {} | 41 » » » » no_resolver_idle_id_(0), |
|
petkov
2011/02/14 22:13:50
s/tab/spaces/
adlr
2011/02/16 18:18:22
Sorry, been working w/ some 3rd party/non-google-s
| |
| 42 callback_(NULL) {} | |
| 43 » virtual ~HttpFetcher(); | |
|
petkov
2011/02/14 22:13:50
s/tab/spaces/
adlr
2011/02/16 18:18:22
Done.
| |
| 41 | 44 |
| 42 void set_delegate(HttpFetcherDelegate* delegate) { delegate_ = delegate; } | 45 void set_delegate(HttpFetcherDelegate* delegate) { delegate_ = delegate; } |
| 43 HttpFetcherDelegate* delegate() const { return delegate_; } | 46 HttpFetcherDelegate* delegate() const { return delegate_; } |
| 44 int http_response_code() const { return http_response_code_; } | 47 int http_response_code() const { return http_response_code_; } |
| 45 | 48 |
| 46 // Optional: Post data to the server. The HttpFetcher should make a copy | 49 // Optional: Post data to the server. The HttpFetcher should make a copy |
| 47 // of this data and upload it via HTTP POST during the transfer. | 50 // of this data and upload it via HTTP POST during the transfer. |
| 48 void SetPostData(const void* data, size_t size); | 51 void SetPostData(const void* data, size_t size); |
| 49 | 52 |
| 50 // Proxy methods to set the proxies, then to pop them off. | 53 // Proxy methods to set the proxies, then to pop them off. |
| 51 void ResolveProxiesForUrl(const std::string& url); | 54 // Returns true on success. |
| 55 bool ResolveProxiesForUrl(const std::string& url, | |
| 56 google::protobuf::Closure* callback); | |
| 52 | 57 |
| 53 void SetProxies(const std::deque<std::string>& proxies) { | 58 void SetProxies(const std::deque<std::string>& proxies) { |
| 54 proxies_ = proxies; | 59 proxies_ = proxies; |
| 55 } | 60 } |
| 56 const std::string& GetCurrentProxy() const { | 61 const std::string& GetCurrentProxy() const { |
| 57 return proxies_.front(); | 62 return proxies_.front(); |
| 58 } | 63 } |
| 59 bool HasProxy() const { return !proxies_.empty(); } | 64 bool HasProxy() const { return !proxies_.empty(); } |
| 60 void PopProxy() { proxies_.pop_front(); } | 65 void PopProxy() { proxies_.pop_front(); } |
| 61 | 66 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 // The server's HTTP response code from the last transfer. This | 108 // The server's HTTP response code from the last transfer. This |
| 104 // field should be set to 0 when a new transfer is initiated, and | 109 // field should be set to 0 when a new transfer is initiated, and |
| 105 // set to the response code when the transfer is complete. | 110 // set to the response code when the transfer is complete. |
| 106 int http_response_code_; | 111 int http_response_code_; |
| 107 | 112 |
| 108 // The delegate; may be NULL. | 113 // The delegate; may be NULL. |
| 109 HttpFetcherDelegate* delegate_; | 114 HttpFetcherDelegate* delegate_; |
| 110 | 115 |
| 111 // Proxy servers | 116 // Proxy servers |
| 112 std::deque<std::string> proxies_; | 117 std::deque<std::string> proxies_; |
| 113 | 118 |
| 114 ProxyResolver* const proxy_resolver_; | 119 ProxyResolver* const proxy_resolver_; |
| 115 | 120 |
| 121 // The ID of the idle callback, used when we have no proxy resolver. | |
|
petkov
2011/02/14 22:13:50
indentation is off
adlr
2011/02/16 18:18:22
Done.
| |
| 122 guint no_resolver_idle_id_; | |
| 123 | |
| 124 // Callback for when we are resolving proxies | |
| 125 google::protobuf::Closure* callback_; | |
| 126 | |
| 116 private: | 127 private: |
| 128 // Callback from the proxy resolver | |
| 129 void ProxiesResolved(const std::deque<std::string>& proxies); | |
| 130 static void StaticProxiesResolved(const std::deque<std::string>& proxies, | |
| 131 void* data) { | |
| 132 reinterpret_cast<HttpFetcher*>(data)->ProxiesResolved(proxies); | |
| 133 } | |
| 134 | |
| 117 DISALLOW_COPY_AND_ASSIGN(HttpFetcher); | 135 DISALLOW_COPY_AND_ASSIGN(HttpFetcher); |
| 118 }; | 136 }; |
| 119 | 137 |
| 120 // Interface for delegates | 138 // Interface for delegates |
| 121 class HttpFetcherDelegate { | 139 class HttpFetcherDelegate { |
| 122 public: | 140 public: |
| 123 // Called every time bytes are received. | 141 // Called every time bytes are received. |
| 124 virtual void ReceivedBytes(HttpFetcher* fetcher, | 142 virtual void ReceivedBytes(HttpFetcher* fetcher, |
| 125 const char* bytes, | 143 const char* bytes, |
| 126 int length) = 0; | 144 int length) = 0; |
| 127 | 145 |
| 128 // Called if the fetcher seeks to a particular offset. | 146 // Called if the fetcher seeks to a particular offset. |
| 129 virtual void SeekToOffset(off_t offset) {} | 147 virtual void SeekToOffset(off_t offset) {} |
| 130 | 148 |
| 131 // When a transfer has completed, exactly one of these two methods will be | 149 // When a transfer has completed, exactly one of these two methods will be |
| 132 // called. TransferTerminated is called when the transfer has been aborted | 150 // called. TransferTerminated is called when the transfer has been aborted |
| 133 // through TerminateTransfer. TransferComplete is called in all other | 151 // through TerminateTransfer. TransferComplete is called in all other |
| 134 // situations. It's OK to destroy the |fetcher| object in this callback. | 152 // situations. It's OK to destroy the |fetcher| object in this callback. |
| 135 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0; | 153 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0; |
| 136 virtual void TransferTerminated(HttpFetcher* fetcher) {} | 154 virtual void TransferTerminated(HttpFetcher* fetcher) {} |
| 137 }; | 155 }; |
| 138 | 156 |
| 139 } // namespace chromeos_update_engine | 157 } // namespace chromeos_update_engine |
| 140 | 158 |
| 141 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ | 159 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ |
| OLD | NEW |