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_FILTER_SOURCE_STREAM_H_ 6 #define NET_FILTER_FILTER_SOURCE_STREAM_H_ 7 8 #include <memory> 9 #include <string> 10 11 #include "base/memory/scoped_refptr.h" 12 #include "base/types/expected.h" 13 #include "net/base/completion_once_callback.h" 14 #include "net/base/net_errors.h" 15 #include "net/base/net_export.h" 16 #include "net/filter/source_stream.h" 17 18 namespace net { 19 20 class DrainableIOBuffer; 21 class IOBuffer; 22 23 // FilterSourceStream represents SourceStreams that always have an upstream 24 // from which undecoded input can be read. Except the ultimate upstream in 25 // the filter chain, all other streams should implement FilterSourceStream 26 // instead of SourceStream. 27 class NET_EXPORT_PRIVATE FilterSourceStream : public SourceStream { 28 public: 29 // |upstream| is the SourceStream from which |this| will read data. 30 // |upstream| cannot be null. 31 FilterSourceStream(SourceType type, std::unique_ptr<SourceStream> upstream); 32 33 FilterSourceStream(const FilterSourceStream&) = delete; 34 FilterSourceStream& operator=(const FilterSourceStream&) = delete; 35 36 ~FilterSourceStream() override; 37 38 // SourceStream implementation. 39 int Read(IOBuffer* read_buffer, 40 int read_buffer_size, 41 CompletionOnceCallback callback) override; 42 std::string Description() const override; 43 bool MayHaveMoreBytes() const override; 44 45 static SourceType ParseEncodingType(const std::string& encoding); 46 47 private: 48 enum State { 49 STATE_NONE, 50 // Reading data from |upstream_| into |input_buffer_|. 51 STATE_READ_DATA, 52 // Reading data from |upstream_| completed. 53 STATE_READ_DATA_COMPLETE, 54 // Filtering data contained in |input_buffer_|. 55 STATE_FILTER_DATA, 56 // Filtering data contained in |input_buffer_| completed. 57 STATE_FILTER_DATA_COMPLETE, 58 STATE_DONE, 59 }; 60 61 int DoLoop(int result); 62 int DoReadData(); 63 int DoReadDataComplete(int result); 64 int DoFilterData(); 65 66 // Helper method used as a callback argument passed to |upstream_->Read()|. 67 void OnIOComplete(int result); 68 69 // Subclasses should implement this method to filter data from 70 // |input_buffer| and write to |output_buffer|. 71 // This method must complete synchronously (i.e. It cannot return 72 // ERR_IO_PENDING). If an unrecoverable error occurred, this should return 73 // ERR_CONTENT_DECODING_FAILED or a more specific error code. 74 // 75 // If FilterData() returns 0, *|consumed_bytes| must be equal to 76 // |input_buffer_size|. Upstream EOF is reached when FilterData() is called 77 // with |upstream_eof_reached| = true. 78 // TODO(xunjieli): consider allowing asynchronous response via callback 79 // to support off-thread decompression. 80 virtual base::expected<size_t, Error> FilterData( 81 IOBuffer* output_buffer, 82 size_t output_buffer_size, 83 IOBuffer* input_buffer, 84 size_t input_buffer_size, 85 size_t* consumed_bytes, 86 bool upstream_eof_reached) = 0; 87 88 // Returns a string representation of the type of this FilterSourceStream. 89 // This is for UMA logging. 90 virtual std::string GetTypeAsString() const = 0; 91 92 // Returns whether |this| still needs more input data from |upstream_|. 93 // By default, |this| will continue reading until |upstream_| returns an error 94 // or EOF. Subclass can override this to return false to skip reading all the 95 // input from |upstream_|. 96 virtual bool NeedMoreData() const; 97 98 // The SourceStream from which |this| will read data from. Data flows from 99 // |upstream_| to |this_|. 100 std::unique_ptr<SourceStream> upstream_; 101 102 State next_state_ = STATE_NONE; 103 104 // Buffer for reading data out of |upstream_| and then for use by |this| 105 // before the filtered data is returned through Read(). 106 scoped_refptr<IOBuffer> input_buffer_; 107 108 // Wrapper around |input_buffer_| that makes visible only the unread data. 109 // Keep this as a member because subclass might not drain everything in a 110 // single FilterData(). 111 scoped_refptr<DrainableIOBuffer> drainable_input_buffer_; 112 113 // Not null if there is a pending Read. 114 scoped_refptr<IOBuffer> output_buffer_; 115 size_t output_buffer_size_ = 0; 116 CompletionOnceCallback callback_; 117 118 // Reading from |upstream_| has returned 0 byte or an error code. 119 bool upstream_end_reached_ = false; 120 }; 121 122 } // namespace net 123 124 #endif // NET_FILTER_FILTER_SOURCE_STREAM_H_ 125