source_stream.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2016 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_FILTER_SOURCE_STREAM_H_
  5. #define NET_FILTER_SOURCE_STREAM_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "net/base/completion_once_callback.h"
  10. #include "net/base/net_errors.h"
  11. #include "net/base/net_export.h"
  12. namespace net {
  13. class IOBuffer;
  14. // The SourceStream class implements a producer of bytes.
  15. class NET_EXPORT_PRIVATE SourceStream {
  16. public:
  17. enum SourceType {
  18. TYPE_BROTLI,
  19. TYPE_DEFLATE,
  20. TYPE_GZIP,
  21. TYPE_UNKNOWN,
  22. TYPE_NONE,
  23. };
  24. // |type| is the type of the SourceStream.
  25. explicit SourceStream(SourceType type);
  26. SourceStream(const SourceStream&) = delete;
  27. SourceStream& operator=(const SourceStream&) = delete;
  28. virtual ~SourceStream();
  29. // Initiaties a read from the stream.
  30. // If it completes synchronously, it:
  31. // - Returns an int representing the number of bytes read. If 0, EOF has
  32. // been reached
  33. // - Bytes will be written into |*dest_buffer|
  34. // - Does not call |callback|
  35. // If it completes asynchronously, it:
  36. // - Returns ERR_IO_PENDING
  37. // - Calls |callback| when it does complete, with an error code or a count
  38. // of bytes read and written into |*dest_buffer|.
  39. // This method takes a reference to |*dest_buffer| if it completes
  40. // asynchronously to ensure it does not get freed mid-read.
  41. virtual int Read(IOBuffer* dest_buffer,
  42. int buffer_size,
  43. CompletionOnceCallback callback) = 0;
  44. // Returns a string that represents stream. This is for UMA and NetLog
  45. // logging.
  46. virtual std::string Description() const = 0;
  47. // Returns true if there may be more bytes to read in this source stream.
  48. // This is not a guarantee that there are more bytes (in the case that
  49. // the stream doesn't know). However, if this returns false, then the stream
  50. // is guaranteed to be complete.
  51. virtual bool MayHaveMoreBytes() const = 0;
  52. SourceType type() const { return type_; }
  53. private:
  54. SourceType type_;
  55. };
  56. } // namespace net
  57. #endif // NET_FILTER_SOURCE_STREAM_H_