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 | |
| 5 | #ifndef UPDATE_ENGINE_HTTP_FETCHER_H__ |
| 6 | #define UPDATE_ENGINE_HTTP_FETCHER_H__ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <glib.h> |
| 10 | #include "base/basictypes.h" |
| 11 | |
| 12 | // This class is a simple wrapper around an HTTP library (libcurl). We can |
| 13 | // easily mock out this interface for testing. |
| 14 | |
| 15 | // Implementations of this class should use asynchronous i/o. They can access |
| 16 | // the glib main loop to request callbacks when timers or file descriptors |
| 17 | // change. |
| 18 | |
| 19 | namespace chromeos_update_engine { |
| 20 | |
| 21 | class HttpFetcherDelegate; |
| 22 | |
| 23 | class HttpFetcher { |
| 24 | public: |
| 25 | HttpFetcher() : delegate_(NULL) {} |
| 26 | virtual ~HttpFetcher() {} |
| 27 | void set_delegate(HttpFetcherDelegate* delegate) { |
| 28 | delegate_ = delegate; |
| 29 | } |
| 30 | HttpFetcherDelegate* delegate() const { |
| 31 | return delegate_; |
| 32 | } |
| 33 | |
| 34 | // Optional: Post data to the server. The HttpFetcher should make a copy |
| 35 | // of this data and upload it via HTTP POST during the transfer. |
| 36 | void SetPostData(const void* data, size_t size) { |
| 37 | post_data_set_ = true; |
| 38 | post_data_.clear(); |
| 39 | const char *char_data = reinterpret_cast<const char*>(data); |
| 40 | post_data_.insert(post_data_.end(), char_data, char_data + size); |
| 41 | } |
| 42 | |
| 43 | // Begins the transfer to the specified URL. |
| 44 | virtual void BeginTransfer(const std::string& url) = 0; |
| 45 | |
| 46 | // Aborts the transfer. TransferComplete() will not be called on the |
| 47 | // delegate. |
| 48 | virtual void TerminateTransfer() = 0; |
| 49 | |
| 50 | // If data is coming in too quickly, you can call Pause() to pause the |
| 51 | // transfer. The delegate will not have ReceivedBytes() called while |
| 52 | // an HttpFetcher is paused. |
| 53 | virtual void Pause() = 0; |
| 54 | |
| 55 | // Used to unpause an HttpFetcher and let the bytes stream in again. |
| 56 | // If a delegate is set, ReceivedBytes() may be called on it before |
| 57 | // Unpause() returns |
| 58 | virtual void Unpause() = 0; |
| 59 | |
| 60 | protected: |
| 61 | // The URL we're actively fetching from |
| 62 | std::string url_; |
| 63 | |
| 64 | // POST data for the transfer, and whether or not it was ever set |
| 65 | bool post_data_set_; |
| 66 | std::vector<char> post_data_; |
| 67 | |
| 68 | // The delegate; may be NULL. |
| 69 | HttpFetcherDelegate* delegate_; |
| 70 | private: |
| 71 | DISALLOW_COPY_AND_ASSIGN(HttpFetcher); |
| 72 | }; |
| 73 | |
| 74 | // Interface for delegates |
| 75 | class HttpFetcherDelegate { |
| 76 | public: |
| 77 | // Called every time bytes are received, even if they are automatically |
| 78 | // delivered to an output file. |
| 79 | virtual void ReceivedBytes(HttpFetcher* fetcher, |
| 80 | const char* bytes, |
| 81 | int length) = 0; |
| 82 | |
| 83 | // Called when the transfer has completed successfully or been somehow |
| 84 | // aborted. |
| 85 | virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0; |
| 86 | }; |
| 87 | |
| 88 | } // namespace chromeos_update_engine |
| 89 | |
| 90 | #endif // UPDATE_ENGINE_HTTP_FETCHER_H__ |