1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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 
8 #include <stdint.h>
9 
10 #include "net/base/completion_once_callback.h"
11 #include "net/base/net_export.h"
12 
13 namespace net {
14 
15 class IOBuffer;
16 class UploadBytesElementReader;
17 class UploadFileElementReader;
18 
19 // An interface to read an upload data element.
20 class NET_EXPORT UploadElementReader {
21  public:
22   UploadElementReader() = default;
23   UploadElementReader(const UploadElementReader&) = delete;
24   UploadElementReader& operator=(const UploadElementReader&) = delete;
25   virtual ~UploadElementReader() = default;
26 
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 
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.
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.
40   //
41   // Initializes the instance synchronously when possible, otherwise does
42   // initialization aynschronously, returns ERR_IO_PENDING and runs callback.
43   // Calling this method again after a Init() success results in resetting the
44   // state.
45   virtual int Init(CompletionOnceCallback callback) = 0;
46 
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.
49   virtual uint64_t GetContentLength() const = 0;
50 
51   // Returns the number of bytes remaining to read.
52   virtual uint64_t BytesRemaining() const = 0;
53 
54   // Returns true if the upload element is entirely in memory.
55   // The default implementation returns false.
56   virtual bool IsInMemory() const;
57 
58   // Reads up to |buf_length| bytes synchronously and returns the number of
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.
61   virtual int Read(IOBuffer* buf,
62                    int buf_length,
63                    CompletionOnceCallback callback) = 0;
64 };
65 
66 }  // namespace net
67 
68 #endif  // NET_BASE_UPLOAD_ELEMENT_READER_H_
69