blob: f8e510a61d4c7fd241a8b3f28e0317eb9deea145 [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:
Darin Petkovcb466212010-08-26 09:40:11 -070026 HttpFetcher()
27 : post_data_set_(false),
28 http_response_code_(0),
29 delegate_(NULL) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000030 virtual ~HttpFetcher() {}
Darin Petkovcb466212010-08-26 09:40:11 -070031
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.com49fdf182009-10-10 00:57:34 +000035
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
45 // Begins the transfer to the specified URL.
46 virtual void BeginTransfer(const std::string& url) = 0;
47
48 // Aborts the transfer. TransferComplete() will not be called on the
49 // delegate.
50 virtual void TerminateTransfer() = 0;
51
52 // If data is coming in too quickly, you can call Pause() to pause the
53 // transfer. The delegate will not have ReceivedBytes() called while
54 // an HttpFetcher is paused.
55 virtual void Pause() = 0;
56
57 // Used to unpause an HttpFetcher and let the bytes stream in again.
58 // If a delegate is set, ReceivedBytes() may be called on it before
59 // Unpause() returns
60 virtual void Unpause() = 0;
61
62 protected:
63 // The URL we're actively fetching from
64 std::string url_;
65
66 // POST data for the transfer, and whether or not it was ever set
67 bool post_data_set_;
68 std::vector<char> post_data_;
69
Darin Petkovcb466212010-08-26 09:40:11 -070070 // The server's HTTP response code from the last transfer. This
71 // field should be set to 0 when a new transfer is initiated, and
72 // set to the response code when the transfer is complete.
73 int http_response_code_;
74
rspangler@google.com49fdf182009-10-10 00:57:34 +000075 // The delegate; may be NULL.
76 HttpFetcherDelegate* delegate_;
77 private:
78 DISALLOW_COPY_AND_ASSIGN(HttpFetcher);
79};
80
81// Interface for delegates
82class HttpFetcherDelegate {
83 public:
84 // Called every time bytes are received, even if they are automatically
85 // delivered to an output file.
86 virtual void ReceivedBytes(HttpFetcher* fetcher,
87 const char* bytes,
88 int length) = 0;
89
90 // Called when the transfer has completed successfully or been somehow
91 // aborted.
92 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0;
93};
94
95} // namespace chromeos_update_engine
96
adlr@google.comc98a7ed2009-12-04 18:54:03 +000097#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__