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