Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Unified Diff: net/filter/filter_source_stream.h

Issue 2251853002: Add net::SourceStream and net::FilterSourceStream (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: self review Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..b81ffcafbddf483a692874bf097240767ffb73be
--- /dev/null
+++ b/net/filter/filter_source_stream.h
@@ -0,0 +1,107 @@
+// 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/io_buffer.h"
+#include "net/base/net_errors.h"
+#include "net/base/net_export.h"
+#include "net/filter/source_stream.h"
+
+namespace net {
+
+class IOBuffer;
Randy Smith (Not in Mondays) 2016/08/22 22:37:16 Why do we need both this and the #include "net/bas
xunjieli 2016/08/29 16:25:28 Done.
+
+// 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 OrderedTypeStringList() const override;
+
+ private:
+ enum State {
+ STATE_NONE,
+ // Reading data from |next|.
Randy Smith (Not in Mondays) 2016/08/22 22:37:16 nit, suggestion: From |upstream_| into |input_buff
Randy Smith (Not in Mondays) 2016/08/22 22:37:16 nit: Don't you mean |upstream_|?
xunjieli 2016/08/29 16:25:28 Done. Sorry about that. It's a leftover from the r
xunjieli 2016/08/29 16:25:28 Done.
+ STATE_READ_DATA,
+ // Reading data from |next| completed.
+ STATE_READ_DATA_COMPLETE,
+ // Filtering data contained in |buffer_|.
Randy Smith (Not in Mondays) 2016/08/22 22:37:16 nit: |input_buffer_|?
xunjieli 2016/08/29 16:25:28 Done.
+ 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();
+
+ // Helper method used as a callback in |upstream_->Read()|.
Randy Smith (Not in Mondays) 2016/08/22 22:37:16 nit, suggestion: "... used as a callback *argument
xunjieli 2016/08/29 16:25:28 Done.
+ void OnIOComplete(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). If an unrecoverable error occurred, this should return
+ // ERR_CONTENT_DECODING_FAILED or a more specific error code.
+ //
+ // FilterData() will be repeatedly invoked with the same |input_buffer| until
+ // FilterData() return 0, at which point, FilterSourceStream will read more
+ // input data from |upstream|.
Randy Smith (Not in Mondays) 2016/08/22 22:37:16 Confirming: This means that filters have no way to
xunjieli 2016/08/29 16:25:28 Yes, that is right. Filters must consume all data
mmenke 2016/08/29 16:41:03 If this is a problem, seems like there are a coupl
xunjieli 2016/08/30 17:27:21 Done. Thanks for the suggestions! I chose (1). PTA
+ virtual int FilterData(IOBuffer* output_buffer,
+ int output_buffer_size,
+ DrainableIOBuffer* input_buffer) = 0;
+
+ // Returns a string representation of the type of this FilterSourceStream.
+ // This is for UMA logging.
+ virtual std::string GetTypeAsString() const = 0;
+
+ // The SourceStream from which |this| will read data from. Data flows from
+ // |upstream_| to |this_|.
+ std::unique_ptr<SourceStream> upstream_;
+
+ State next_state_;
+
+ // 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_;
+
+ // Keep this as a member because subclass might not drain everything in a
+ // single FilterData().
Randy Smith (Not in Mondays) 2016/08/22 22:37:15 nit, suggestion: This comment describes why this i
xunjieli 2016/08/29 16:25:29 Done.
+ scoped_refptr<DrainableIOBuffer> drainable_input_buffer_;
+
+ // Not null if there is a pending Read.
+ scoped_refptr<IOBuffer> output_buffer_;
+ int output_buffer_size_;
+ CompletionCallback callback_;
+
+ // Reading from |upstream_| has returned 0 byte or an error code.
+ bool upstream_end_reached_;
Randy Smith (Not in Mondays) 2016/08/22 22:37:16 nit, suggestion: |upstream_eof_reached_|?
xunjieli 2016/08/29 16:25:28 I changed from |eof_reached_| to |end_reached_| in
+
+ DISALLOW_COPY_AND_ASSIGN(FilterSourceStream);
+};
+
+} // namespace net
+
+#endif // NET_FILTER_FILTER_SOURCE_STREAM_H_

Powered by Google App Engine
This is Rietveld 408576698