rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
| 8 | #include <string> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 9 | #include <vector> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 10 | #include <glib.h> |
| 11 | #include "base/basictypes.h" |
| 12 | |
| 13 | // This class is a simple wrapper around an HTTP library (libcurl). We can |
| 14 | // easily mock out this interface for testing. |
| 15 | |
| 16 | // Implementations of this class should use asynchronous i/o. They can access |
| 17 | // the glib main loop to request callbacks when timers or file descriptors |
| 18 | // change. |
| 19 | |
| 20 | namespace chromeos_update_engine { |
| 21 | |
| 22 | class HttpFetcherDelegate; |
| 23 | |
| 24 | class HttpFetcher { |
| 25 | public: |
Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 26 | HttpFetcher() |
| 27 | : post_data_set_(false), |
| 28 | http_response_code_(0), |
| 29 | delegate_(NULL) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 30 | virtual ~HttpFetcher() {} |
Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 31 | |
| 32 | void set_delegate(HttpFetcherDelegate* delegate) { delegate_ = delegate; } |
| 33 | HttpFetcherDelegate* delegate() const { return delegate_; } |
| 34 | int http_response_code() const { return http_response_code_; } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 | |
| 36 | // Optional: Post data to the server. The HttpFetcher should make a copy |
| 37 | // of this data and upload it via HTTP POST during the transfer. |
| 38 | void SetPostData(const void* data, size_t size) { |
| 39 | post_data_set_ = true; |
| 40 | post_data_.clear(); |
| 41 | const char *char_data = reinterpret_cast<const char*>(data); |
| 42 | post_data_.insert(post_data_.end(), char_data, char_data + size); |
| 43 | } |
| 44 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 45 | // Downloading should resume from this offset |
| 46 | virtual void SetOffset(off_t offset) = 0; |
| 47 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 48 | // Begins the transfer to the specified URL. |
| 49 | virtual void BeginTransfer(const std::string& url) = 0; |
| 50 | |
| 51 | // Aborts the transfer. TransferComplete() will not be called on the |
| 52 | // delegate. |
| 53 | virtual void TerminateTransfer() = 0; |
| 54 | |
| 55 | // If data is coming in too quickly, you can call Pause() to pause the |
| 56 | // transfer. The delegate will not have ReceivedBytes() called while |
| 57 | // an HttpFetcher is paused. |
| 58 | virtual void Pause() = 0; |
| 59 | |
| 60 | // Used to unpause an HttpFetcher and let the bytes stream in again. |
| 61 | // If a delegate is set, ReceivedBytes() may be called on it before |
| 62 | // Unpause() returns |
| 63 | virtual void Unpause() = 0; |
| 64 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 65 | // These two function are overloaded in LibcurlHttp fetcher to speed |
| 66 | // testing. |
| 67 | virtual void set_idle_seconds(int seconds) {} |
| 68 | virtual void set_retry_seconds(int seconds) {} |
| 69 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 70 | protected: |
| 71 | // The URL we're actively fetching from |
| 72 | std::string url_; |
| 73 | |
| 74 | // POST data for the transfer, and whether or not it was ever set |
| 75 | bool post_data_set_; |
| 76 | std::vector<char> post_data_; |
| 77 | |
Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 78 | // The server's HTTP response code from the last transfer. This |
| 79 | // field should be set to 0 when a new transfer is initiated, and |
| 80 | // set to the response code when the transfer is complete. |
| 81 | int http_response_code_; |
| 82 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 83 | // The delegate; may be NULL. |
| 84 | HttpFetcherDelegate* delegate_; |
| 85 | private: |
| 86 | DISALLOW_COPY_AND_ASSIGN(HttpFetcher); |
| 87 | }; |
| 88 | |
| 89 | // Interface for delegates |
| 90 | class HttpFetcherDelegate { |
| 91 | public: |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 92 | // Called every time bytes are received. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 93 | virtual void ReceivedBytes(HttpFetcher* fetcher, |
| 94 | const char* bytes, |
| 95 | int length) = 0; |
| 96 | |
Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 97 | // Called if the fetcher seeks to a particular offset. |
| 98 | virtual void SeekToOffset(off_t offset) {} |
| 99 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 100 | // Called when the transfer has completed successfully or been somehow |
| 101 | // aborted. |
| 102 | virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0; |
| 103 | }; |
| 104 | |
| 105 | } // namespace chromeos_update_engine |
| 106 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 107 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ |