1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_BASE_UPLOAD_DATA_STREAM_H_ 6 #define NET_BASE_UPLOAD_DATA_STREAM_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 #include <vector> 12 13 #include "net/base/completion_once_callback.h" 14 #include "net/base/net_export.h" 15 #include "net/base/upload_progress.h" 16 #include "net/log/net_log_with_source.h" 17 18 namespace net { 19 20 class IOBuffer; 21 class UploadElementReader; 22 23 // A class for retrieving all data to be sent as a request body. Supports both 24 // chunked and non-chunked uploads. 25 class NET_EXPORT UploadDataStream { 26 public: 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. 30 UploadDataStream(bool is_chunked, int64_t identifier); 31 UploadDataStream(bool is_chunked, bool has_null_source, int64_t identifier); 32 33 UploadDataStream(const UploadDataStream&) = delete; 34 UploadDataStream& operator=(const UploadDataStream&) = delete; 35 36 virtual ~UploadDataStream(); 37 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 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). 43 // 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 // 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. 50 int Init(CompletionOnceCallback callback, const NetLogWithSource& net_log); 51 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. 58 // 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. 62 // 63 // TODO(mmenke): Investigate letting reads fail. 64 int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback); 65 66 // Returns the total size of the data stream and the current position. 67 // When the data is chunked, always returns zero. Must always return the same 68 // value after each call to Initialize(). size()69 uint64_t size() const { return total_size_; } position()70 uint64_t position() const { return current_position_; } 71 72 // See constructor for description. identifier()73 int64_t identifier() const { return identifier_; } 74 is_chunked()75 bool is_chunked() const { return is_chunked_; } 76 77 // Returns true if the stream has a null source which is defined at 78 // https://fetch.spec.whatwg.org/#concept-body-source. has_null_source()79 bool has_null_source() const { return has_null_source_; } 80 81 // Returns true if all data has been consumed from this upload data 82 // stream. For chunked uploads, returns false until the first read attempt. 83 // This makes some state machines a little simpler. 84 bool IsEOF() const; 85 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. 89 void Reset(); 90 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. 97 virtual const std::vector<std::unique_ptr<UploadElementReader>>* 98 GetElementReaders() const; 99 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 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 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. 117 void SetSize(uint64_t size); 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 123 private: 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. 127 virtual int InitInternal(const NetLogWithSource& net_log) = 0; 128 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; 136 137 // Resets state and cancels any pending callbacks. Guaranteed to be called 138 // at least once before every call to InitInternal. 139 virtual void ResetInternal() = 0; 140 141 uint64_t total_size_ = 0; 142 uint64_t current_position_ = 0; 143 144 const int64_t identifier_; 145 146 const bool is_chunked_; 147 const bool has_null_source_; 148 149 // True if the initialization was successful. 150 bool initialized_successfully_ = false; 151 152 bool is_eof_ = false; 153 154 CompletionOnceCallback callback_; 155 156 NetLogWithSource net_log_; 157 }; 158 159 } // namespace net 160 161 #endif // NET_BASE_UPLOAD_DATA_STREAM_H_ 162