Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 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_ELEMENT_READER_H_ |
| 6 | #define NET_BASE_UPLOAD_ELEMENT_READER_H_ |
| 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 10 | #include "net/base/completion_once_callback.h" |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 11 | #include "net/base/net_export.h" |
| 12 | |
| 13 | namespace net { |
| 14 | |
[email protected] | 5b76814 | 2012-10-17 10:15:17 | [diff] [blame] | 15 | class IOBuffer; |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 16 | class UploadBytesElementReader; |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 17 | class UploadFileElementReader; |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 18 | |
| 19 | // An interface to read an upload data element. |
| 20 | class NET_EXPORT UploadElementReader { |
| 21 | public: |
David Bienvenu | a03ac8c | 2020-11-06 15:55:39 | [diff] [blame] | 22 | UploadElementReader() = default; |
| 23 | UploadElementReader(const UploadElementReader&) = delete; |
| 24 | UploadElementReader& operator=(const UploadElementReader&) = delete; |
| 25 | virtual ~UploadElementReader() = default; |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 26 | |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 27 | // Returns this instance's pointer as UploadBytesElementReader when possible, |
| 28 | // otherwise returns NULL. |
| 29 | virtual const UploadBytesElementReader* AsBytesReader() const; |
| 30 | |
| 31 | // Returns this instance's pointer as UploadFileElementReader when possible, |
| 32 | // otherwise returns NULL. |
| 33 | virtual const UploadFileElementReader* AsFileReader() const; |
| 34 | |
dmurph | 1f4afcf | 2016-05-20 19:51:55 | [diff] [blame] | 35 | // This function must be called before calling any other method. It is not |
| 36 | // valid to call any method (other than the destructor) if Init() fails. |
Matt Menke | ddd2148 | 2017-12-15 20:08:21 | [diff] [blame] | 37 | // This method can be called multiple times. Calling this results in resetting |
| 38 | // the state (i.e. the stream is rewound), and any previously pending Init() |
| 39 | // or Read() calls are aborted. |
dmurph | 1f4afcf | 2016-05-20 19:51:55 | [diff] [blame] | 40 | // |
[email protected] | df7adc6 | 2012-09-18 14:01:53 | [diff] [blame] | 41 | // Initializes the instance synchronously when possible, otherwise does |
| 42 | // initialization aynschronously, returns ERR_IO_PENDING and runs callback. |
[email protected] | e5d47734 | 2012-11-02 15:18:46 | [diff] [blame] | 43 | // Calling this method again after a Init() success results in resetting the |
| 44 | // state. |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 45 | virtual int Init(CompletionOnceCallback callback) = 0; |
[email protected] | df7adc6 | 2012-09-18 14:01:53 | [diff] [blame] | 46 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 47 | // Returns the byte-length of the element. For files that do not exist, 0 |
| 48 | // is returned. This is done for consistency with Mozilla. |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 49 | virtual uint64_t GetContentLength() const = 0; |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 50 | |
| 51 | // Returns the number of bytes remaining to read. |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 52 | virtual uint64_t BytesRemaining() const = 0; |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 53 | |
| 54 | // Returns true if the upload element is entirely in memory. |
| 55 | // The default implementation returns false. |
| 56 | virtual bool IsInMemory() const; |
| 57 | |
[email protected] | 5b76814 | 2012-10-17 10:15:17 | [diff] [blame] | 58 | // Reads up to |buf_length| bytes synchronously and returns the number of |
[email protected] | fac16e2 | 2012-12-29 18:46:31 | [diff] [blame] | 59 | // bytes read or error code when possible, otherwise, returns ERR_IO_PENDING |
| 60 | // and runs |callback| with the result. |buf_length| must be greater than 0. |
[email protected] | 5b76814 | 2012-10-17 10:15:17 | [diff] [blame] | 61 | virtual int Read(IOBuffer* buf, |
| 62 | int buf_length, |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 63 | CompletionOnceCallback callback) = 0; |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } // namespace net |
| 67 | |
| 68 | #endif // NET_BASE_UPLOAD_ELEMENT_READER_H_ |