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