Chromium Code Reviews| Index: net/filter/filter_source_stream.h |
| diff --git a/net/filter/filter_source_stream.h b/net/filter/filter_source_stream.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c1ee6ded0334fe28da862f2698bc0a6aa931f39a |
| --- /dev/null |
| +++ b/net/filter/filter_source_stream.h |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef NET_FILTER_FILTER_SOURCE_STREAM_H_ |
| +#define NET_FILTER_FILTER_SOURCE_STREAM_H_ |
| + |
| +#include <memory> |
| +#include <string> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "net/base/completion_callback.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/base/net_export.h" |
| +#include "net/filter/source_stream.h" |
| + |
| +namespace net { |
| + |
| +class DrainableIOBuffer; |
| +class IOBuffer; |
| + |
| +// FilterSourceStream represents SourceStreams that always have an upstream |
| +// from which undecoded input can be read. Except the ultimate upstream in |
| +// the filter chain, all other streams should implement FilterSourceStream |
| +// instead of SourceStream. |
| +class NET_EXPORT_PRIVATE FilterSourceStream : public SourceStream { |
| + public: |
| + // |upstream| is the SourceStream from which |this| will read data. |
| + // |upstream| cannot be null. |
| + FilterSourceStream(SourceType type, std::unique_ptr<SourceStream> upstream); |
| + |
| + ~FilterSourceStream() override; |
| + |
| + int Read(IOBuffer* read_buffer, |
| + int read_buffer_size, |
| + const CompletionCallback& callback) override; |
| + |
| + std::string Description() const override; |
| + |
| + protected: |
| + // Pulls data from |upstream_|. |
| + int Pull(const CompletionCallback& callback); |
|
Randy Smith (Not in Mondays)
2016/09/08 00:03:32
nit: Document return value? Also interface contra
|
| + |
| + // Implemented by subclasses to produce output to |read_buffer|. |
| + // If more data is needed, subclasses should call Pull() to get input data |
| + // from |upstream_|. See SourceStream::Read() for interface contract. |
| + virtual int ReadInternal(IOBuffer* read_buffer, |
| + int read_buffer_size, |
| + const CompletionCallback& callback) = 0; |
| + |
| + // Whether an EOF or an error has been returned by |upstream_|. |
| + 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
|
| + |
| + // Returns a DrainableIOBuffer from which subclasses can consume data. If |
| + // 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
|
| + DrainableIOBuffer* drainable_input_buffer() { |
| + return drainable_input_buffer_.get(); |
| + } |
| + |
| + private: |
| + // Returns a string representation of the type of this FilterSourceStream. |
| + // This is for UMA logging. |
| + virtual std::string GetTypeAsString() const = 0; |
| + |
| + // Passed to |upstream_->Read()| as a callback argument. |
| + void OnIOComplete(int rv); |
| + |
| + // Fills |drainable_input_buffer_| if |bytes_read| is bigger or equal to 0. |
| + void MaybeFillDrainableInputBuffer(int bytes_read); |
| + |
| + // The SourceStream from which |this| will read data from. Data flows from |
| + // |upstream_| to |this_|. |
| + std::unique_ptr<SourceStream> upstream_; |
| + |
| + // Buffer for reading data out of |upstream_| and then for use by |this| |
| + // before the filtered data is returned through Read(). |
| + scoped_refptr<IOBuffer> input_buffer_; |
| + |
| + // Wrapper around |input_buffer_| that makes visible only the unread data. |
| + // Keep this as a member because subclass might not drain everything in a |
| + // single FilterData(). |
| + scoped_refptr<DrainableIOBuffer> drainable_input_buffer_; |
| + |
| + // Non-null if there is a pending Pull() call. |
| + CompletionCallback pull_callback_; |
| + |
| + // Reading from |upstream_| has returned 0 byte or an error code. |
| + bool upstream_end_reached_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FilterSourceStream); |
| +}; |
| + |
| +} // namespace net |
| + |
| +#endif // NET_FILTER_FILTER_SOURCE_STREAM_H_ |