Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 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/macros.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "net/base/completion_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() override; | |
| 34 | |
| 35 int Read(IOBuffer* read_buffer, | |
| 36 int read_buffer_size, | |
| 37 const CompletionCallback& callback) override; | |
| 38 | |
| 39 std::string Description() const override; | |
| 40 | |
| 41 private: | |
| 42 enum State { | |
| 43 STATE_NONE, | |
| 44 // Reading data from |upstream_| into |input_buffer_|. | |
| 45 STATE_READ_DATA, | |
| 46 // Reading data from |upstream_| completed. | |
| 47 STATE_READ_DATA_COMPLETE, | |
| 48 // Filtering data contained in |input_buffer_|. | |
| 49 STATE_FILTER_DATA, | |
| 50 // Filtering data contained in |input_buffer_| completed. | |
| 51 STATE_FILTER_DATA_COMPLETE, | |
| 52 STATE_DONE, | |
| 53 }; | |
| 54 | |
| 55 int DoLoop(int result); | |
| 56 int DoReadData(); | |
| 57 int DoReadDataComplete(int result); | |
| 58 int DoFilterData(); | |
| 59 | |
| 60 // Helper method used as a callback argument passed to |upstream_->Read()|. | |
| 61 void OnIOComplete(int result); | |
| 62 | |
| 63 // Subclasses should implement this method to filter data from | |
| 64 // |input_buffer| and write to |output_buffer|. | |
| 65 // This method must complete synchronously (i.e. It cannot return | |
| 66 // ERR_IO_PENDING). If an unrecoverable error occurred, this should return | |
| 67 // ERR_CONTENT_DECODING_FAILED or a more specific error code. | |
| 68 // | |
| 69 // FilterData() will be repeatedly invoked with the same |input_buffer| until | |
| 70 // FilterData() return 0, at which point, FilterSourceStream will read more | |
|
Randy Smith (Not in Mondays)
2016/09/08 18:57:41
Should we not document the requirement that all da
xunjieli
2016/09/08 19:11:43
Done.
| |
| 71 // input data from |upstream_|. Upstream EOF is reached when | |
|
Randy Smith (Not in Mondays)
2016/09/08 18:57:41
nit: "... when FilterData() is called with |input_
xunjieli
2016/09/08 19:11:43
Done.
| |
| 72 // |input_buffer->BytesRemaining()| is 0. | |
| 73 // TODO(xunjieli): consider allowing asynchronous decompression. | |
|
Randy Smith (Not in Mondays)
2016/09/08 18:57:41
nit, suggestion: "consider allowing asynchronous r
xunjieli
2016/09/08 19:11:43
Done.
| |
| 74 virtual int FilterData(IOBuffer* output_buffer, | |
| 75 int output_buffer_size, | |
| 76 DrainableIOBuffer* input_buffer) = 0; | |
| 77 | |
| 78 // Returns a string representation of the type of this FilterSourceStream. | |
| 79 // This is for UMA logging. | |
| 80 virtual std::string GetTypeAsString() const = 0; | |
| 81 | |
| 82 // Returns whether |this| still needs more input data from |upstream_|. | |
| 83 // By default, |this| will continue reading until |upstream_| returns an error | |
| 84 // or EOF. Subclass can override this to return false to skip reading all the | |
| 85 // input from |upstream_|. | |
| 86 virtual bool NeedMoreData() const; | |
| 87 | |
| 88 // The SourceStream from which |this| will read data from. Data flows from | |
| 89 // |upstream_| to |this_|. | |
| 90 std::unique_ptr<SourceStream> upstream_; | |
| 91 | |
| 92 State next_state_; | |
| 93 | |
| 94 // Buffer for reading data out of |upstream_| and then for use by |this| | |
| 95 // before the filtered data is returned through Read(). | |
| 96 scoped_refptr<IOBuffer> input_buffer_; | |
| 97 | |
| 98 // Wrapper around |input_buffer_| that makes visible only the unread data. | |
| 99 // Keep this as a member because subclass might not drain everything in a | |
| 100 // single FilterData(). | |
| 101 scoped_refptr<DrainableIOBuffer> drainable_input_buffer_; | |
| 102 | |
| 103 // Not null if there is a pending Read. | |
| 104 scoped_refptr<IOBuffer> output_buffer_; | |
| 105 int output_buffer_size_; | |
| 106 CompletionCallback callback_; | |
| 107 | |
| 108 // Reading from |upstream_| has returned 0 byte or an error code. | |
| 109 bool upstream_end_reached_; | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(FilterSourceStream); | |
| 112 }; | |
| 113 | |
| 114 } // namespace net | |
| 115 | |
| 116 #endif // NET_FILTER_FILTER_SOURCE_STREAM_H_ | |
| OLD | NEW |