blob: 6d8608b62996f72d7d198983917471f360dc3913 [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
Darin Petkov9ce452b2010-11-17 14:33:28 -080048 // 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.com49fdf182009-10-10 00:57:34 +000051 virtual void BeginTransfer(const std::string& url) = 0;
52
Darin Petkov9ce452b2010-11-17 14:33:28 -080053 // 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.com49fdf182009-10-10 00:57:34 +000055 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 Reyes3fd5d302010-10-07 20:07:18 -070067 // 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.com49fdf182009-10-10 00:57:34 +000072 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 Petkovcb466212010-08-26 09:40:11 -070080 // 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.com49fdf182009-10-10 00:57:34 +000085 // The delegate; may be NULL.
86 HttpFetcherDelegate* delegate_;
87 private:
88 DISALLOW_COPY_AND_ASSIGN(HttpFetcher);
89};
90
91// Interface for delegates
92class HttpFetcherDelegate {
93 public:
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070094 // Called every time bytes are received.
rspangler@google.com49fdf182009-10-10 00:57:34 +000095 virtual void ReceivedBytes(HttpFetcher* fetcher,
96 const char* bytes,
97 int length) = 0;
98
Andrew de los Reyes34e41a12010-10-26 20:07:58 -070099 // Called if the fetcher seeks to a particular offset.
100 virtual void SeekToOffset(off_t offset) {}
101
Darin Petkov9ce452b2010-11-17 14:33:28 -0800102 // 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.com49fdf182009-10-10 00:57:34 +0000105 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) = 0;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800106
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.com49fdf182009-10-10 00:57:34 +0000110};
111
112} // namespace chromeos_update_engine
113
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000114#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_HTTP_FETCHER_H__