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

Side by Side 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: address comments Created 4 years, 3 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 unified diff | Download patch
« no previous file with comments | « net/docs/filter.md ('k') | net/filter/filter_source_stream.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
Randy Smith (Not in Mondays) 2016/09/12 21:05:03 Could we remove the phrase "with the same |input_b
xunjieli 2016/09/13 19:45:24 I added this comment per an earlier review commmen
mmenke 2016/09/13 19:50:57 Don't think you understand the comment - the paren
xunjieli 2016/09/13 19:54:58 Thanks for the follow-up. I will do that in a sepa
70 // FilterData() returns 0 or an error. If FilterData() returns 0,
71 // |input_buffer| must be fully drained. Upstream EOF is reached when
72 // FilterData() is called with |input_buffer->BytesRemaining() == 0|.
Randy Smith (Not in Mondays) 2016/09/12 21:05:03 Helen, as I mentioned offline I think there's a pr
xunjieli 2016/09/13 19:45:24 Done. I will upload this change as a separate CL.
73 // TODO(xunjieli): consider allowing asynchronous response via callback
74 // to support off-thread decompression.
75 virtual int FilterData(IOBuffer* output_buffer,
76 int output_buffer_size,
77 DrainableIOBuffer* input_buffer) = 0;
78
79 // Returns a string representation of the type of this FilterSourceStream.
80 // This is for UMA logging.
81 virtual std::string GetTypeAsString() const = 0;
82
83 // Returns whether |this| still needs more input data from |upstream_|.
84 // By default, |this| will continue reading until |upstream_| returns an error
85 // or EOF. Subclass can override this to return false to skip reading all the
86 // input from |upstream_|.
87 virtual bool NeedMoreData() const;
88
89 // The SourceStream from which |this| will read data from. Data flows from
90 // |upstream_| to |this_|.
91 std::unique_ptr<SourceStream> upstream_;
92
93 State next_state_;
94
95 // Buffer for reading data out of |upstream_| and then for use by |this|
96 // before the filtered data is returned through Read().
97 scoped_refptr<IOBuffer> input_buffer_;
98
99 // Wrapper around |input_buffer_| that makes visible only the unread data.
100 // Keep this as a member because subclass might not drain everything in a
101 // single FilterData().
102 scoped_refptr<DrainableIOBuffer> drainable_input_buffer_;
103
104 // Not null if there is a pending Read.
105 scoped_refptr<IOBuffer> output_buffer_;
106 int output_buffer_size_;
107 CompletionCallback callback_;
108
109 // Reading from |upstream_| has returned 0 byte or an error code.
110 bool upstream_end_reached_;
111
112 DISALLOW_COPY_AND_ASSIGN(FilterSourceStream);
113 };
114
115 } // namespace net
116
117 #endif // NET_FILTER_FILTER_SOURCE_STREAM_H_
OLDNEW
« no previous file with comments | « net/docs/filter.md ('k') | net/filter/filter_source_stream.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698