blob: d0f9cb207ad4d72ee9f0fd9f8b5caa0a8c19d965 [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
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070045 // Downloading should resume from this offset
46 virtual void SetOffset(off_t offset) = 0;
47
rspangler@google.com49fdf182009-10-10 00:57:34 +000048 // Begins the transfer to the specified URL.
49 virtual void BeginTransfer(const std::string& url) = 0;
50
51 // Aborts the transfer. TransferComplete() will not be called on the
52 // delegate.
53 virtual void TerminateTransfer() = 0;
54
55 // If data is coming in too quickly, you can call Pause() to pause the
56 // transfer. The delegate will not have ReceivedBytes() called while
57 // an HttpFetcher is paused.
58 virtual void Pause() = 0;
59
60 // Used to unpause an HttpFetcher and let the bytes stream in again.
61 // If a delegate is set, ReceivedBytes() may be called on it before
62 // Unpause() returns
63 virtual void Unpause() = 0;
64
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070065 // These two function are overloaded in LibcurlHttp fetcher to speed
66 // testing.
67 virtual void set_idle_seconds(int seconds) {}
68 virtual void set_retry_seconds(int seconds) {}
69
rspangler@google.com49fdf182009-10-10 00:57:34 +000070 protected:
71 // The URL we're actively fetching from
72 std::string url_;
73
74 // POST data for the transfer, and whether or not it was ever set
75 bool post_data_set_;
76 std::vector<char> post_data_;
77
Darin Petkovcb466212010-08-26 09:40:11 -070078 // The server's HTTP response code from the last transfer. This
79 // field should be set to 0 when a new transfer is initiated, and
80 // set to the response code when the transfer is complete.
81 int http_response_code_;
82
rspangler@google.com49fdf182009-10-10 00:57:34 +000083 // The delegate; may be NULL.
84 HttpFetcherDelegate* delegate_;
85 private:
86 DISALLOW_COPY_AND_ASSIGN(HttpFetcher);
87};
88
89// Interface for delegates
90class HttpFetcherDelegate {
91 public:
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070092 // Called every time bytes are received.
rspangler@google.com49fdf182009-10-10 00:57:34 +000093 virtual void ReceivedBytes(HttpFetcher* fetcher,
94 const char* bytes,
95 int length) = 0;
96
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070097 // Called if the fetcher seeks to a particular offset.
98 virtual void SeekToOffset(off_t offset) {}
99
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100 // Called when the transfer has completed successfully or been somehow
101 // aborted.
102 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0;
103};
104
105} // namespace chromeos_update_engine
106
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000107#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__