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 protected: | |
| 42 // Pulls data from |upstream_|. | |
| 43 int Pull(const CompletionCallback& callback); | |
|
Randy Smith (Not in Mondays)
2016/09/08 00:03:32
nit: Document return value? Also interface contra
| |
| 44 | |
| 45 // Implemented by subclasses to produce output to |read_buffer|. | |
| 46 // If more data is needed, subclasses should call Pull() to get input data | |
| 47 // from |upstream_|. See SourceStream::Read() for interface contract. | |
| 48 virtual int ReadInternal(IOBuffer* read_buffer, | |
| 49 int read_buffer_size, | |
| 50 const CompletionCallback& callback) = 0; | |
| 51 | |
| 52 // Whether an EOF or an error has been returned by |upstream_|. | |
| 53 bool upstream_end_reached() const { return upstream_end_reached_; } | |
|
Randy Smith (Not in Mondays)
2016/09/08 00:03:32
?? This information is already provided to subclas
| |
| 54 | |
| 55 // Returns a DrainableIOBuffer from which subclasses can consume data. If | |
| 56 // empty, subclasses can call Pull() to read from |upstream_| as desired. | |
|
Randy Smith (Not in Mondays)
2016/09/08 00:03:32
nit, suggestion: It seems a weird restriction that
| |
| 57 DrainableIOBuffer* drainable_input_buffer() { | |
| 58 return drainable_input_buffer_.get(); | |
| 59 } | |
| 60 | |
| 61 private: | |
| 62 // Returns a string representation of the type of this FilterSourceStream. | |
| 63 // This is for UMA logging. | |
| 64 virtual std::string GetTypeAsString() const = 0; | |
| 65 | |
| 66 // Passed to |upstream_->Read()| as a callback argument. | |
| 67 void OnIOComplete(int rv); | |
| 68 | |
| 69 // Fills |drainable_input_buffer_| if |bytes_read| is bigger or equal to 0. | |
| 70 void MaybeFillDrainableInputBuffer(int bytes_read); | |
| 71 | |
| 72 // The SourceStream from which |this| will read data from. Data flows from | |
| 73 // |upstream_| to |this_|. | |
| 74 std::unique_ptr<SourceStream> upstream_; | |
| 75 | |
| 76 // Buffer for reading data out of |upstream_| and then for use by |this| | |
| 77 // before the filtered data is returned through Read(). | |
| 78 scoped_refptr<IOBuffer> input_buffer_; | |
| 79 | |
| 80 // Wrapper around |input_buffer_| that makes visible only the unread data. | |
| 81 // Keep this as a member because subclass might not drain everything in a | |
| 82 // single FilterData(). | |
| 83 scoped_refptr<DrainableIOBuffer> drainable_input_buffer_; | |
| 84 | |
| 85 // Non-null if there is a pending Pull() call. | |
| 86 CompletionCallback pull_callback_; | |
| 87 | |
| 88 // Reading from |upstream_| has returned 0 byte or an error code. | |
| 89 bool upstream_end_reached_; | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(FilterSourceStream); | |
| 92 }; | |
| 93 | |
| 94 } // namespace net | |
| 95 | |
| 96 #endif // NET_FILTER_FILTER_SOURCE_STREAM_H_ | |
| OLD | NEW |