Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame^] | 1 | // Copyright 2012 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #ifndef NET_BASE_UPLOAD_DATA_STREAM_H_ |
| 6 | #define NET_BASE_UPLOAD_DATA_STREAM_H_ |
| 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
danakj | 7f767e6 | 2016-04-16 23:20:23 | [diff] [blame] | 10 | #include <memory> |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 13 | #include "net/base/completion_once_callback.h" |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 14 | #include "net/base/net_export.h" |
shivanisha | b9a14395 | 2016-09-19 17:23:41 | [diff] [blame] | 15 | #include "net/base/upload_progress.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 16 | #include "net/log/net_log_with_source.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 17 | |
| 18 | namespace net { |
| 19 | |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 20 | class IOBuffer; |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 21 | class UploadElementReader; |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 22 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 23 | // A class for retrieving all data to be sent as a request body. Supports both |
| 24 | // chunked and non-chunked uploads. |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 25 | class NET_EXPORT UploadDataStream { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 26 | public: |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 27 | // |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. |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 30 | UploadDataStream(bool is_chunked, int64_t identifier); |
Yutaka Hirano | c267fb24 | 2022-07-05 01:27:17 | [diff] [blame] | 31 | UploadDataStream(bool is_chunked, bool has_null_source, int64_t identifier); |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 32 | |
David Bienvenu | a03ac8c | 2020-11-06 15:55:39 | [diff] [blame] | 33 | UploadDataStream(const UploadDataStream&) = delete; |
| 34 | UploadDataStream& operator=(const UploadDataStream&) = delete; |
| 35 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 36 | virtual ~UploadDataStream(); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 37 | |
[email protected] | e5d47734 | 2012-11-02 15:18:46 | [diff] [blame] | 38 | // 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 |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 40 | // 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] | 49ed6cc | 2012-02-02 08:59:16 | [diff] [blame] | 43 | // |
[email protected] | df7adc6 | 2012-09-18 14:01:53 | [diff] [blame] | 44 | // 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] | 49ed6cc | 2012-02-02 08:59:16 | [diff] [blame] | 47 | // 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 Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 50 | int Init(CompletionOnceCallback callback, const NetLogWithSource& net_log); |
[email protected] | df7adc6 | 2012-09-18 14:01:53 | [diff] [blame] | 51 | |
[email protected] | 5b76814 | 2012-10-17 10:15:17 | [diff] [blame] | 52 | // 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] | 6db833d1 | 2012-01-21 00:45:19 | [diff] [blame] | 58 | // |
[email protected] | 0ab2e24 | 2012-02-08 06:07:00 | [diff] [blame] | 59 | // 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. |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 62 | // |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 63 | // TODO(mmenke): Investigate letting reads fail. |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 64 | int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback); |
[email protected] | 5b76814 | 2012-10-17 10:15:17 | [diff] [blame] | 65 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 66 | // Returns the total size of the data stream and the current position. |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 67 | // When the data is chunked, always returns zero. Must always return the same |
| 68 | // value after each call to Initialize(). |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 69 | uint64_t size() const { return total_size_; } |
| 70 | uint64_t position() const { return current_position_; } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 71 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 72 | // See constructor for description. |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 73 | int64_t identifier() const { return identifier_; } |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 74 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 75 | bool is_chunked() const { return is_chunked_; } |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 76 | |
Yutaka Hirano | c267fb24 | 2022-07-05 01:27:17 | [diff] [blame] | 77 | // 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] | 0ab2e24 | 2012-02-08 06:07:00 | [diff] [blame] | 81 | // Returns true if all data has been consumed from this upload data |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 82 | // stream. For chunked uploads, returns false until the first read attempt. |
| 83 | // This makes some state machines a little simpler. |
[email protected] | 0ab2e24 | 2012-02-08 06:07:00 | [diff] [blame] | 84 | bool IsEOF() const; |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 85 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 86 | // 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] | e5d47734 | 2012-11-02 15:18:46 | [diff] [blame] | 89 | void Reset(); |
| 90 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 91 | // 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. |
danakj | 7f767e6 | 2016-04-16 23:20:23 | [diff] [blame] | 97 | virtual const std::vector<std::unique_ptr<UploadElementReader>>* |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 98 | GetElementReaders() const; |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 99 | |
shivanisha | b9a14395 | 2016-09-19 17:23:41 | [diff] [blame] | 100 | // 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 Osato | 4c75c0c | 2020-06-24 08:03:57 | [diff] [blame] | 105 | // 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 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 109 | 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. |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 117 | void SetSize(uint64_t size); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 118 | |
| 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] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 123 | private: |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 124 | // 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. |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 127 | virtual int InitInternal(const NetLogWithSource& net_log) = 0; |
[email protected] | e5d47734 | 2012-11-02 15:18:46 | [diff] [blame] | 128 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 129 | // 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] | df7adc6 | 2012-09-18 14:01:53 | [diff] [blame] | 136 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 137 | // Resets state and cancels any pending callbacks. Guaranteed to be called |
Matt Menke | d9991f9 | 2018-03-23 20:16:33 | [diff] [blame] | 138 | // at least once before every call to InitInternal. |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 139 | virtual void ResetInternal() = 0; |
[email protected] | 9fa94db | 2012-10-22 03:34:07 | [diff] [blame] | 140 | |
Tsuyoshi Horo | e0235ed6 | 2022-06-09 01:42:30 | [diff] [blame] | 141 | uint64_t total_size_ = 0; |
| 142 | uint64_t current_position_ = 0; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 143 | |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 144 | const int64_t identifier_; |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 145 | |
| 146 | const bool is_chunked_; |
Yutaka Hirano | c267fb24 | 2022-07-05 01:27:17 | [diff] [blame] | 147 | const bool has_null_source_; |
[email protected] | fac16e2 | 2012-12-29 18:46:31 | [diff] [blame] | 148 | |
[email protected] | 49ed6cc | 2012-02-02 08:59:16 | [diff] [blame] | 149 | // True if the initialization was successful. |
Tsuyoshi Horo | e0235ed6 | 2022-06-09 01:42:30 | [diff] [blame] | 150 | bool initialized_successfully_ = false; |
[email protected] | 49ed6cc | 2012-02-02 08:59:16 | [diff] [blame] | 151 | |
Tsuyoshi Horo | e0235ed6 | 2022-06-09 01:42:30 | [diff] [blame] | 152 | bool is_eof_ = false; |
[email protected] | 9fa94db | 2012-10-22 03:34:07 | [diff] [blame] | 153 | |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 154 | CompletionOnceCallback callback_; |
[email protected] | df7adc6 | 2012-09-18 14:01:53 | [diff] [blame] | 155 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 156 | NetLogWithSource net_log_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | } // namespace net |
| 160 | |
| 161 | #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ |