| 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 |  | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame^] | 48 |   // Begins the transfer to the specified URL. This fetcher instance should not | 
 | 49 |   // be destroyed until either TransferComplete, or TransferTerminated is | 
 | 50 |   // called. | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 51 |   virtual void BeginTransfer(const std::string& url) = 0; | 
 | 52 |  | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame^] | 53 |   // Aborts the transfer. The transfer may not abort right away -- delegate's | 
 | 54 |   // TransferTerminated() will be called when the transfer is actually done. | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 55 |   virtual void TerminateTransfer() = 0; | 
 | 56 |  | 
 | 57 |   // If data is coming in too quickly, you can call Pause() to pause the | 
 | 58 |   // transfer. The delegate will not have ReceivedBytes() called while | 
 | 59 |   // an HttpFetcher is paused. | 
 | 60 |   virtual void Pause() = 0; | 
 | 61 |  | 
 | 62 |   // Used to unpause an HttpFetcher and let the bytes stream in again. | 
 | 63 |   // If a delegate is set, ReceivedBytes() may be called on it before | 
 | 64 |   // Unpause() returns | 
 | 65 |   virtual void Unpause() = 0; | 
 | 66 |  | 
| Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 67 |   // These two function are overloaded in LibcurlHttp fetcher to speed | 
 | 68 |   // testing. | 
 | 69 |   virtual void set_idle_seconds(int seconds) {} | 
 | 70 |   virtual void set_retry_seconds(int seconds) {} | 
 | 71 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 72 |  protected: | 
 | 73 |   // The URL we're actively fetching from | 
 | 74 |   std::string url_; | 
 | 75 |  | 
 | 76 |   // POST data for the transfer, and whether or not it was ever set | 
 | 77 |   bool post_data_set_; | 
 | 78 |   std::vector<char> post_data_; | 
 | 79 |  | 
| Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 80 |   // The server's HTTP response code from the last transfer. This | 
 | 81 |   // field should be set to 0 when a new transfer is initiated, and | 
 | 82 |   // set to the response code when the transfer is complete. | 
 | 83 |   int http_response_code_; | 
 | 84 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 85 |   // The delegate; may be NULL. | 
 | 86 |   HttpFetcherDelegate* delegate_; | 
 | 87 |  private: | 
 | 88 |   DISALLOW_COPY_AND_ASSIGN(HttpFetcher); | 
 | 89 | }; | 
 | 90 |  | 
 | 91 | // Interface for delegates | 
 | 92 | class HttpFetcherDelegate { | 
 | 93 |  public: | 
| Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 94 |   // Called every time bytes are received. | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 95 |   virtual void ReceivedBytes(HttpFetcher* fetcher, | 
 | 96 |                              const char* bytes, | 
 | 97 |                              int length) = 0; | 
 | 98 |  | 
| Andrew de los Reyes | 34e41a1 | 2010-10-26 20:07:58 -0700 | [diff] [blame] | 99 |   // Called if the fetcher seeks to a particular offset. | 
 | 100 |   virtual void SeekToOffset(off_t offset) {} | 
 | 101 |  | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame^] | 102 |   // Called when the transfer has completed successfully or been aborted through | 
 | 103 |   // means other than TerminateTransfer. It's OK to destroy the |fetcher| object | 
 | 104 |   // in this callback. | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 105 |   virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0; | 
| Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame^] | 106 |  | 
 | 107 |   // Called when the transfer has been aborted through TerminateTransfer. It's | 
 | 108 |   // OK to destroy the |fetcher| object in this callback. | 
 | 109 |   virtual void TransferTerminated(HttpFetcher* fetcher) {} | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 110 | }; | 
 | 111 |  | 
 | 112 | }  // namespace chromeos_update_engine | 
 | 113 |  | 
| adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 114 | #endif  // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__ |