blob: ae538f86201c2e99c59026bc635ac17d8dcafc85 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#ifndef NET_BASE_UPLOAD_DATA_STREAM_H_
6#define NET_BASE_UPLOAD_DATA_STREAM_H_
7
Avi Drissman13fc8932015-12-20 04:40:468#include <stdint.h>
9
danakj7f767e62016-04-16 23:20:2310#include <memory>
olli.raula6df48b2a2015-11-26 07:40:2211#include <vector>
12
Matt Menkecc1d3a902018-02-05 18:27:3313#include "net/base/completion_once_callback.h"
[email protected]172da1b2011-08-12 15:52:2614#include "net/base/net_export.h"
shivanishab9a143952016-09-19 17:23:4115#include "net/base/upload_progress.h"
mikecironef22f9812016-10-04 03:40:1916#include "net/log/net_log_with_source.h"
initial.commit586acc5fe2008-07-26 22:42:5217
18namespace net {
19
[email protected]597cf6e2009-05-29 09:43:2620class IOBuffer;
[email protected]d98961652012-09-11 20:27:2121class UploadElementReader;
[email protected]597cf6e2009-05-29 09:43:2622
mmenkecbc2b712014-10-09 20:29:0723// A class for retrieving all data to be sent as a request body. Supports both
24// chunked and non-chunked uploads.
[email protected]172da1b2011-08-12 15:52:2625class NET_EXPORT UploadDataStream {
initial.commit586acc5fe2008-07-26 22:42:5226 public:
mmenkecbc2b712014-10-09 20:29:0727 // |identifier| identifies a particular upload instance, which is used by the
28 // cache to formulate a cache key. This value should be unique across browser
29 // sessions. A value of 0 is used to indicate an unspecified identifier.
wtc69f8ea82015-06-04 00:08:1330 UploadDataStream(bool is_chunked, int64_t identifier);
Yutaka Hiranoc267fb242022-07-05 01:27:1731 UploadDataStream(bool is_chunked, bool has_null_source, int64_t identifier);
[email protected]b2d26cfd2012-12-11 10:36:0632
David Bienvenua03ac8c2020-11-06 15:55:3933 UploadDataStream(const UploadDataStream&) = delete;
34 UploadDataStream& operator=(const UploadDataStream&) = delete;
35
mmenkecbc2b712014-10-09 20:29:0736 virtual ~UploadDataStream();
[email protected]f288ef02012-12-15 20:28:2837
[email protected]e5d477342012-11-02 15:18:4638 // Initializes the stream. This function must be called before calling any
39 // other method. It is not valid to call any method (other than the
mmenkecbc2b712014-10-09 20:29:0740 // destructor) if Init() fails. This method can be called multiple times.
41 // Calling this method after an Init() success results in resetting the
42 // state (i.e. the stream is rewound).
[email protected]49ed6cc2012-02-02 08:59:1643 //
[email protected]df7adc62012-09-18 14:01:5344 // Does the initialization synchronously and returns the result if possible,
45 // otherwise returns ERR_IO_PENDING and runs the callback with the result.
46 //
[email protected]49ed6cc2012-02-02 08:59:1647 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected
48 // file modification time is set (usually not set, but set for sliced
49 // files) and the target file is changed.
Matt Menkecc1d3a902018-02-05 18:27:3350 int Init(CompletionOnceCallback callback, const NetLogWithSource& net_log);
[email protected]df7adc62012-09-18 14:01:5351
[email protected]5b768142012-10-17 10:15:1752 // When possible, reads up to |buf_len| bytes synchronously from the upload
53 // data stream to |buf| and returns the number of bytes read; otherwise,
54 // returns ERR_IO_PENDING and calls |callback| with the number of bytes read.
55 // Partial reads are allowed. Zero is returned on a call to Read when there
56 // are no remaining bytes in the stream, and IsEof() will return true
57 // hereafter.
[email protected]6db833d12012-01-21 00:45:1958 //
[email protected]0ab2e242012-02-08 06:07:0059 // If there's less data to read than we initially observed (i.e. the actual
60 // upload data is smaller than size()), zeros are padded to ensure that
61 // size() bytes can be read, which can happen for TYPE_FILE payloads.
mmenkecbc2b712014-10-09 20:29:0762 //
mmenkecbc2b712014-10-09 20:29:0763 // TODO(mmenke): Investigate letting reads fail.
Matt Menkecc1d3a902018-02-05 18:27:3364 int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
[email protected]5b768142012-10-17 10:15:1765
initial.commit586acc5fe2008-07-26 22:42:5266 // Returns the total size of the data stream and the current position.
mmenkecbc2b712014-10-09 20:29:0767 // When the data is chunked, always returns zero. Must always return the same
68 // value after each call to Initialize().
wtc69f8ea82015-06-04 00:08:1369 uint64_t size() const { return total_size_; }
70 uint64_t position() const { return current_position_; }
initial.commit586acc5fe2008-07-26 22:42:5271
mmenkecbc2b712014-10-09 20:29:0772 // See constructor for description.
wtc69f8ea82015-06-04 00:08:1373 int64_t identifier() const { return identifier_; }
[email protected]699efe602011-01-25 07:17:1174
mmenkecbc2b712014-10-09 20:29:0775 bool is_chunked() const { return is_chunked_; }
[email protected]0736d9e2012-11-28 19:50:4076
Yutaka Hiranoc267fb242022-07-05 01:27:1777 // Returns true if the stream has a null source which is defined at
78 // https://fetch.spec.whatwg.org/#concept-body-source.
79 bool has_null_source() const { return has_null_source_; }
80
[email protected]0ab2e242012-02-08 06:07:0081 // Returns true if all data has been consumed from this upload data
mmenkecbc2b712014-10-09 20:29:0782 // stream. For chunked uploads, returns false until the first read attempt.
83 // This makes some state machines a little simpler.
[email protected]0ab2e242012-02-08 06:07:0084 bool IsEOF() const;
[email protected]0c9bf872011-03-04 17:53:2285
mmenkecbc2b712014-10-09 20:29:0786 // Cancels all pending callbacks, and resets state. Any IOBuffer currently
87 // being read to is not safe for future use, as it may be in use on another
88 // thread.
[email protected]e5d477342012-11-02 15:18:4689 void Reset();
90
mmenkecbc2b712014-10-09 20:29:0791 // Returns true if the upload data in the stream is entirely in memory, and
92 // all read requests will succeed synchronously. Expected to return false for
93 // chunked requests.
94 virtual bool IsInMemory() const;
95
96 // Returns a list of element readers owned by |this|, if it has any.
danakj7f767e62016-04-16 23:20:2397 virtual const std::vector<std::unique_ptr<UploadElementReader>>*
olli.raula6df48b2a2015-11-26 07:40:2298 GetElementReaders() const;
mmenkecbc2b712014-10-09 20:29:0799
shivanishab9a143952016-09-19 17:23:41100 // Returns the upload progress. If the stream was not initialized
101 // successfully, or has been reset and not yet re-initialized, returns an
102 // empty UploadProgress.
103 virtual UploadProgress GetUploadProgress() const;
104
Yoichi Osato4c75c0c2020-06-24 08:03:57105 // Indicates whether fetch upload streaming is allowed/rejected over H/1.
106 // Even if this is false but there is a QUIC/H2 stream, the upload is allowed.
107 virtual bool AllowHTTP1() const;
108
mmenkecbc2b712014-10-09 20:29:07109 protected:
110 // Must be called by subclasses when InitInternal and ReadInternal complete
111 // asynchronously.
112 void OnInitCompleted(int result);
113 void OnReadCompleted(int result);
114
115 // Must be called before InitInternal completes, for non-chunked uploads.
116 // Must not be called for chunked uploads.
wtc69f8ea82015-06-04 00:08:13117 void SetSize(uint64_t size);
mmenkecbc2b712014-10-09 20:29:07118
119 // Must be called for chunked uploads before the final ReadInternal call
120 // completes. Must not be called for non-chunked uploads.
121 void SetIsFinalChunk();
122
[email protected]02cad5d2013-10-02 08:14:03123 private:
mmenkecbc2b712014-10-09 20:29:07124 // See Init(). If it returns ERR_IO_PENDING, OnInitCompleted must be called
125 // once it completes. If the upload is not chunked, SetSize must be called
126 // before it completes.
tfarina42834112016-09-22 13:38:20127 virtual int InitInternal(const NetLogWithSource& net_log) = 0;
[email protected]e5d477342012-11-02 15:18:46128
mmenkecbc2b712014-10-09 20:29:07129 // See Read(). For chunked uploads, must call SetIsFinalChunk if this is the
130 // final chunk. For non-chunked uploads, the UploadDataStream determins which
131 // read is the last based on size. Must read 1 or more bytes on every call,
132 // though the final chunk may be 0 bytes, for chunked requests. If it returns
133 // ERR_IO_PENDING, OnInitCompleted must be called once it completes. Must not
134 // return any error, other than ERR_IO_PENDING.
135 virtual int ReadInternal(IOBuffer* buf, int buf_len) = 0;
[email protected]df7adc62012-09-18 14:01:53136
mmenkecbc2b712014-10-09 20:29:07137 // Resets state and cancels any pending callbacks. Guaranteed to be called
Matt Menked9991f92018-03-23 20:16:33138 // at least once before every call to InitInternal.
mmenkecbc2b712014-10-09 20:29:07139 virtual void ResetInternal() = 0;
[email protected]9fa94db2012-10-22 03:34:07140
Tsuyoshi Horoe0235ed62022-06-09 01:42:30141 uint64_t total_size_ = 0;
142 uint64_t current_position_ = 0;
initial.commit586acc5fe2008-07-26 22:42:52143
wtc69f8ea82015-06-04 00:08:13144 const int64_t identifier_;
[email protected]b2d26cfd2012-12-11 10:36:06145
146 const bool is_chunked_;
Yutaka Hiranoc267fb242022-07-05 01:27:17147 const bool has_null_source_;
[email protected]fac16e22012-12-29 18:46:31148
[email protected]49ed6cc2012-02-02 08:59:16149 // True if the initialization was successful.
Tsuyoshi Horoe0235ed62022-06-09 01:42:30150 bool initialized_successfully_ = false;
[email protected]49ed6cc2012-02-02 08:59:16151
Tsuyoshi Horoe0235ed62022-06-09 01:42:30152 bool is_eof_ = false;
[email protected]9fa94db2012-10-22 03:34:07153
Matt Menkecc1d3a902018-02-05 18:27:33154 CompletionOnceCallback callback_;
[email protected]df7adc62012-09-18 14:01:53155
tfarina42834112016-09-22 13:38:20156 NetLogWithSource net_log_;
initial.commit586acc5fe2008-07-26 22:42:52157};
158
159} // namespace net
160
161#endif // NET_BASE_UPLOAD_DATA_STREAM_H_