blob: 8877532f3160acd550e4fb86014f2753e59ddf66 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
[email protected]d98961652012-09-11 20:27:212// 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 Drissman13fc8932015-12-20 04:40:468#include <stdint.h>
9
Matt Menkecc1d3a902018-02-05 18:27:3310#include "net/base/completion_once_callback.h"
[email protected]d98961652012-09-11 20:27:2111#include "net/base/net_export.h"
12
13namespace net {
14
[email protected]5b768142012-10-17 10:15:1715class IOBuffer;
[email protected]0736d9e2012-11-28 19:50:4016class UploadBytesElementReader;
[email protected]0736d9e2012-11-28 19:50:4017class UploadFileElementReader;
[email protected]d98961652012-09-11 20:27:2118
19// An interface to read an upload data element.
20class NET_EXPORT UploadElementReader {
21 public:
David Bienvenua03ac8c2020-11-06 15:55:3922 UploadElementReader() = default;
23 UploadElementReader(const UploadElementReader&) = delete;
24 UploadElementReader& operator=(const UploadElementReader&) = delete;
25 virtual ~UploadElementReader() = default;
[email protected]d98961652012-09-11 20:27:2126
[email protected]0736d9e2012-11-28 19:50:4027 // 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
dmurph1f4afcf2016-05-20 19:51:5535 // 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 Menkeddd21482017-12-15 20:08:2137 // 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.
dmurph1f4afcf2016-05-20 19:51:5540 //
[email protected]df7adc62012-09-18 14:01:5341 // Initializes the instance synchronously when possible, otherwise does
42 // initialization aynschronously, returns ERR_IO_PENDING and runs callback.
[email protected]e5d477342012-11-02 15:18:4643 // Calling this method again after a Init() success results in resetting the
44 // state.
Matt Menkecc1d3a902018-02-05 18:27:3345 virtual int Init(CompletionOnceCallback callback) = 0;
[email protected]df7adc62012-09-18 14:01:5346
mmenkecbc2b712014-10-09 20:29:0747 // Returns the byte-length of the element. For files that do not exist, 0
48 // is returned. This is done for consistency with Mozilla.
wtc69f8ea82015-06-04 00:08:1349 virtual uint64_t GetContentLength() const = 0;
[email protected]d98961652012-09-11 20:27:2150
51 // Returns the number of bytes remaining to read.
wtc69f8ea82015-06-04 00:08:1352 virtual uint64_t BytesRemaining() const = 0;
[email protected]d98961652012-09-11 20:27:2153
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]5b768142012-10-17 10:15:1758 // Reads up to |buf_length| bytes synchronously and returns the number of
[email protected]fac16e22012-12-29 18:46:3159 // 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]5b768142012-10-17 10:15:1761 virtual int Read(IOBuffer* buf,
62 int buf_length,
Matt Menkecc1d3a902018-02-05 18:27:3363 CompletionOnceCallback callback) = 0;
[email protected]d98961652012-09-11 20:27:2164};
65
66} // namespace net
67
68#endif // NET_BASE_UPLOAD_ELEMENT_READER_H_