Chromium Code Reviews| Index: net/filter/filter_stream_source.h |
| diff --git a/net/filter/filter_stream_source.h b/net/filter/filter_stream_source.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..50ea961c7b3bc403f6f29436cb1d6c76cb63ff13 |
| --- /dev/null |
| +++ b/net/filter/filter_stream_source.h |
| @@ -0,0 +1,103 @@ |
| +// 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_STREAM_SOURCE_H |
| +#define NET_FILTER_FILTER_STREAM_SOURCE_H |
|
mmenke
2016/07/21 18:14:08
+_ (x3)
xunjieli
2016/07/27 20:32:01
Done.
|
| + |
| +#include <memory> |
| + |
| +#include "base/macros.h" |
| +#include "net/base/completion_callback.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/base/net_export.h" |
| +#include "net/filter/stream_source.h" |
| + |
| +namespace net { |
| + |
| +class IOBuffer; |
| + |
| +// FilterStreamSource represents StreamSources that always have a previous |
| +// source from which undecoded input can be read. Except the first source in |
| +// the filter chain, all other sources should implement FilterStreamSource |
| +// instead of StreamSource. |
| +class NET_EXPORT_PRIVATE FilterStreamSource : public StreamSource { |
| + public: |
| + // |previous| is the previous StreamSource in the chain. |previous| cannot be |
| + // null. |
| + FilterStreamSource(SourceType type, std::unique_ptr<StreamSource> previous); |
| + |
| + ~FilterStreamSource() override; |
| + |
| + int Read(IOBuffer* read_buffer, |
| + size_t read_buffer_size, |
| + const CompletionCallback& callback) override; |
| + |
| + std::string OrderedTypeStringList() const override; |
| + // Subclass override this method to perform any initialization that needs to |
| + // be completed before Read() can be called. Returns whether initialization |
| + // has succeeded. If failed, filter chain will not be used. |
| + virtual bool Init(); |
| + |
| + private: |
| + enum State { |
| + STATE_NONE, |
| + // Reading data from |previous|. |
| + STATE_READ_DATA, |
| + // Reading data from |previous| completed. |
| + STATE_READ_DATA_COMPLETE, |
| + // Filtering data contained in |buffer_|. |
| + STATE_FILTER_DATA, |
| + // Filtering data contained in |buffer_| completed. |
| + STATE_FILTER_DATA_COMPLETE, |
| + STATE_DONE, |
| + }; |
| + |
| + int DoLoop(int result); |
| + int DoReadData(); |
| + int DoReadDataComplete(int result); |
| + int DoFilterData(int result); |
| + int DoFilterDataComplete(int result); |
| + |
| + void DoCallback(int result); |
| + |
| + void OnPreviousReadCompleted(int result); |
| + // Subclasses should implement this method to filter data from |
| + // |input_buffer_| and write to |output_buffer|. |
| + // This method must complete synchronously (i.e. It cannot return |
| + // ERR_IO_PENDING). |
| + virtual int FilterData(IOBuffer* output_buffer, |
| + size_t output_buffer_size, |
| + DrainableIOBuffer* input_buffer) = 0; |
| + |
| + // Returns a string representation of the type of this FilterStreamSource. |
| + // This is for UMA logging. |
| + virtual std::string GetTypeAsString() const = 0; |
| + |
| + // Previous StreamSource in the chain. |
| + std::unique_ptr<StreamSource> previous_; |
| + |
| + State next_state_; |
| + |
| + // Buffer for reading data out of |previous_| and then for use by |this| |
| + // before the filtered data is returned through Read(). |
| + scoped_refptr<IOBuffer> input_buffer_; |
| + // Keep this as a member because subclass might not drain everything in a |
| + // single FilterData(). |
| + scoped_refptr<DrainableIOBuffer> drainable_input_buffer_; |
| + |
| + // Not null if there is a pending Read. |
| + scoped_refptr<IOBuffer> output_buffer_; |
| + size_t output_buffer_size_; |
| + CompletionCallback callback_; |
| + |
| + // Reading from |previous_| has returned 0 byte or an error code. |
| + bool previous_eof_reached_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FilterStreamSource); |
| +}; |
| + |
| +} // namespace net |
| + |
| +#endif // NET_FILTER_FILTER_STREAM_SOURCE_H |