• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ZSTD,
27     TYPE_UNKNOWN,
28     TYPE_NONE,
29   };
30 
31   // |type| is the type of the SourceStream.
32   explicit SourceStream(SourceType type);
33 
34   SourceStream(const SourceStream&) = delete;
35   SourceStream& operator=(const SourceStream&) = delete;
36 
37   virtual ~SourceStream();
38 
39   // Initiaties a read from the stream.
40   // If it completes synchronously, it:
41   //   - Returns an int representing the number of bytes read. If 0, EOF has
42   //     been reached
43   //   - Bytes will be written into |*dest_buffer|
44   //   - Does not call |callback|
45   // If it completes asynchronously, it:
46   //   - Returns ERR_IO_PENDING
47   //   - Calls |callback| when it does complete, with an error code or a count
48   //     of bytes read and written into |*dest_buffer|.
49   // This method takes a reference to |*dest_buffer| if it completes
50   // asynchronously to ensure it does not get freed mid-read.
51   virtual int Read(IOBuffer* dest_buffer,
52                    int buffer_size,
53                    CompletionOnceCallback callback) = 0;
54 
55   // Returns a string that represents stream. This is for UMA and NetLog
56   // logging.
57   virtual std::string Description() const = 0;
58 
59   // Returns true if there may be more bytes to read in this source stream.
60   // This is not a guarantee that there are more bytes (in the case that
61   // the stream doesn't know).  However, if this returns false, then the stream
62   // is guaranteed to be complete.
63   virtual bool MayHaveMoreBytes() const = 0;
64 
type()65   SourceType type() const { return type_; }
66 
67  private:
68   SourceType type_;
69 };
70 
71 }  // namespace net
72 
73 #endif  // NET_FILTER_SOURCE_STREAM_H_
74