upload_element_reader.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #ifndef NET_BASE_UPLOAD_ELEMENT_READER_H_
  5. #define NET_BASE_UPLOAD_ELEMENT_READER_H_
  6. #include <stdint.h>
  7. #include "net/base/completion_once_callback.h"
  8. #include "net/base/net_export.h"
  9. namespace net {
  10. class IOBuffer;
  11. class UploadBytesElementReader;
  12. class UploadFileElementReader;
  13. // An interface to read an upload data element.
  14. class NET_EXPORT UploadElementReader {
  15. public:
  16. UploadElementReader() = default;
  17. UploadElementReader(const UploadElementReader&) = delete;
  18. UploadElementReader& operator=(const UploadElementReader&) = delete;
  19. virtual ~UploadElementReader() = default;
  20. // Returns this instance's pointer as UploadBytesElementReader when possible,
  21. // otherwise returns NULL.
  22. virtual const UploadBytesElementReader* AsBytesReader() const;
  23. // Returns this instance's pointer as UploadFileElementReader when possible,
  24. // otherwise returns NULL.
  25. virtual const UploadFileElementReader* AsFileReader() const;
  26. // This function must be called before calling any other method. It is not
  27. // valid to call any method (other than the destructor) if Init() fails.
  28. // This method can be called multiple times. Calling this results in resetting
  29. // the state (i.e. the stream is rewound), and any previously pending Init()
  30. // or Read() calls are aborted.
  31. //
  32. // Initializes the instance synchronously when possible, otherwise does
  33. // initialization aynschronously, returns ERR_IO_PENDING and runs callback.
  34. // Calling this method again after a Init() success results in resetting the
  35. // state.
  36. virtual int Init(CompletionOnceCallback callback) = 0;
  37. // Returns the byte-length of the element. For files that do not exist, 0
  38. // is returned. This is done for consistency with Mozilla.
  39. virtual uint64_t GetContentLength() const = 0;
  40. // Returns the number of bytes remaining to read.
  41. virtual uint64_t BytesRemaining() const = 0;
  42. // Returns true if the upload element is entirely in memory.
  43. // The default implementation returns false.
  44. virtual bool IsInMemory() const;
  45. // Reads up to |buf_length| bytes synchronously and returns the number of
  46. // bytes read or error code when possible, otherwise, returns ERR_IO_PENDING
  47. // and runs |callback| with the result. |buf_length| must be greater than 0.
  48. virtual int Read(IOBuffer* buf,
  49. int buf_length,
  50. CompletionOnceCallback callback) = 0;
  51. };
  52. } // namespace net
  53. #endif // NET_BASE_UPLOAD_ELEMENT_READER_H_