blob: 5cd6f08559fc33976cd70be8d81953f45a869f8c [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// 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.comc98a7ed2009-12-04 18:54:03 +00005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
8#include <string>
adlr@google.comc98a7ed2009-12-04 18:54:03 +00009#include <vector>
rspangler@google.com49fdf182009-10-10 00:57:34 +000010#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
20namespace chromeos_update_engine {
21
22class HttpFetcherDelegate;
23
24class HttpFetcher {
25 public:
adlr@google.comc98a7ed2009-12-04 18:54:03 +000026 HttpFetcher() : post_data_set_(false), delegate_(NULL) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000027 virtual ~HttpFetcher() {}
28 void set_delegate(HttpFetcherDelegate* delegate) {
29 delegate_ = delegate;
30 }
31 HttpFetcherDelegate* delegate() const {
32 return delegate_;
33 }
34
35 // Optional: Post data to the server. The HttpFetcher should make a copy
36 // of this data and upload it via HTTP POST during the transfer.
37 void SetPostData(const void* data, size_t size) {
38 post_data_set_ = true;
39 post_data_.clear();
40 const char *char_data = reinterpret_cast<const char*>(data);
41 post_data_.insert(post_data_.end(), char_data, char_data + size);
42 }
43
44 // Begins the transfer to the specified URL.
45 virtual void BeginTransfer(const std::string& url) = 0;
46
47 // Aborts the transfer. TransferComplete() will not be called on the
48 // delegate.
49 virtual void TerminateTransfer() = 0;
50
51 // If data is coming in too quickly, you can call Pause() to pause the
52 // transfer. The delegate will not have ReceivedBytes() called while
53 // an HttpFetcher is paused.
54 virtual void Pause() = 0;
55
56 // Used to unpause an HttpFetcher and let the bytes stream in again.
57 // If a delegate is set, ReceivedBytes() may be called on it before
58 // Unpause() returns
59 virtual void Unpause() = 0;
60
61 protected:
62 // The URL we're actively fetching from
63 std::string url_;
64
65 // POST data for the transfer, and whether or not it was ever set
66 bool post_data_set_;
67 std::vector<char> post_data_;
68
69 // The delegate; may be NULL.
70 HttpFetcherDelegate* delegate_;
71 private:
72 DISALLOW_COPY_AND_ASSIGN(HttpFetcher);
73};
74
75// Interface for delegates
76class HttpFetcherDelegate {
77 public:
78 // Called every time bytes are received, even if they are automatically
79 // delivered to an output file.
80 virtual void ReceivedBytes(HttpFetcher* fetcher,
81 const char* bytes,
82 int length) = 0;
83
84 // Called when the transfer has completed successfully or been somehow
85 // aborted.
86 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0;
87};
88
89} // namespace chromeos_update_engine
90
adlr@google.comc98a7ed2009-12-04 18:54:03 +000091#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__