123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- #ifndef UPDATE_ENGINE_COMMON_HTTP_FETCHER_H_
- #define UPDATE_ENGINE_COMMON_HTTP_FETCHER_H_
- #include <deque>
- #include <memory>
- #include <string>
- #include <vector>
- #include <base/callback.h>
- #include <base/logging.h>
- #include <base/macros.h>
- #include <brillo/message_loops/message_loop.h>
- #include "update_engine/common/http_common.h"
- #include "update_engine/common/proxy_resolver.h"
- namespace chromeos_update_engine {
- class HttpFetcherDelegate;
- class HttpFetcher {
- public:
-
-
-
- explicit HttpFetcher(ProxyResolver* proxy_resolver)
- : post_data_set_(false),
- http_response_code_(0),
- delegate_(nullptr),
- proxies_(1, kNoProxy),
- proxy_resolver_(proxy_resolver),
- callback_(nullptr) {}
- virtual ~HttpFetcher();
- void set_delegate(HttpFetcherDelegate* delegate) { delegate_ = delegate; }
- HttpFetcherDelegate* delegate() const { return delegate_; }
- int http_response_code() const { return http_response_code_; }
-
-
-
- void SetPostData(const void* data, size_t size, HttpContentType type);
-
- void SetPostData(const void* data, size_t size);
-
- void ResolveProxiesForUrl(const std::string& url,
- const base::Closure& callback);
- void SetProxies(const std::deque<std::string>& proxies) {
- proxies_ = proxies;
- }
- const std::string& GetCurrentProxy() const { return proxies_.front(); }
- bool HasProxy() const { return !proxies_.empty(); }
- void PopProxy() { proxies_.pop_front(); }
-
- virtual void SetOffset(off_t offset) = 0;
-
- virtual void SetLength(size_t length) = 0;
- virtual void UnsetLength() = 0;
-
-
-
- virtual void BeginTransfer(const std::string& url) = 0;
-
-
- virtual void TerminateTransfer() = 0;
-
-
-
- virtual void SetHeader(const std::string& header_name,
- const std::string& header_value) = 0;
-
-
-
- virtual void Pause() = 0;
-
-
-
- virtual void Unpause() = 0;
-
-
- virtual void set_idle_seconds(int seconds) {}
- virtual void set_retry_seconds(int seconds) {}
-
-
-
- virtual void set_low_speed_limit(int low_speed_bps, int low_speed_sec) = 0;
-
-
- virtual void set_connect_timeout(int connect_timeout_seconds) = 0;
-
- virtual void set_max_retry_count(int max_retry_count) = 0;
-
- virtual size_t GetBytesDownloaded() = 0;
- ProxyResolver* proxy_resolver() const { return proxy_resolver_; }
- protected:
-
-
-
- bool CancelProxyResolution();
-
- std::string url_;
-
- bool post_data_set_;
- brillo::Blob post_data_;
- HttpContentType post_content_type_;
-
-
-
- int http_response_code_;
-
- HttpFetcherDelegate* delegate_;
-
- std::deque<std::string> proxies_;
- ProxyResolver* const proxy_resolver_;
-
- brillo::MessageLoop::TaskId no_resolver_idle_id_{
- brillo::MessageLoop::kTaskIdNull};
-
- std::unique_ptr<base::Closure> callback_;
- private:
-
- void ProxiesResolved(const std::deque<std::string>& proxies);
-
-
- void NoProxyResolverCallback();
-
-
- ProxyRequestId proxy_request_{kProxyRequestIdNull};
- DISALLOW_COPY_AND_ASSIGN(HttpFetcher);
- };
- class HttpFetcherDelegate {
- public:
- virtual ~HttpFetcherDelegate() = default;
-
-
- virtual bool ReceivedBytes(HttpFetcher* fetcher,
- const void* bytes,
- size_t length) = 0;
-
- virtual void SeekToOffset(off_t offset) {}
-
-
-
-
- virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0;
- virtual void TransferTerminated(HttpFetcher* fetcher) {}
- };
- }
- #endif
|