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

Side by Side Diff: net/filter/filter_source_stream.cc

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 unified diff | Download patch
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 #include "net/filter/filter_source_stream.h"
6
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/logging.h"
10 #include "base/metrics/histogram_macros.h"
11 #include "base/numerics/safe_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "net/base/net_errors.h"
14
15 namespace net {
16
17 namespace {
18
19 const size_t kBufferSize = 32 * 1024;
20
21 } // namespace
22
23 FilterSourceStream::FilterSourceStream(SourceType type,
24 std::unique_ptr<SourceStream> upstream)
25 : SourceStream(type),
26 upstream_(std::move(upstream)),
27 next_state_(STATE_NONE),
28 output_buffer_size_(0),
29 upstream_end_reached_(false) {
30 DCHECK(upstream_);
31 }
32
33 FilterSourceStream::~FilterSourceStream() {}
34
35 int FilterSourceStream::Read(IOBuffer* read_buffer,
36 int read_buffer_size,
37 const CompletionCallback& callback) {
38 DCHECK_EQ(STATE_NONE, next_state_);
39 DCHECK(read_buffer);
40 DCHECK_LT(0, read_buffer_size);
41
42 // Allocate a BlockBuffer during first Read().
43 if (!input_buffer_) {
44 input_buffer_ = new IOBufferWithSize(kBufferSize);
45 // This is first Read(), start with reading data from |upstream_|.
46 next_state_ = STATE_READ_DATA;
47 } else {
48 // Otherwise start with filtering data, which tells us whether this stream
49 // needs input data.
50 next_state_ = STATE_FILTER_DATA;
51 }
52
53 output_buffer_ = read_buffer;
54 output_buffer_size_ = read_buffer_size;
55 int rv = DoLoop(OK);
56
57 if (rv == ERR_IO_PENDING)
58 callback_ = callback;
59 return rv;
60 }
61
62 std::string FilterSourceStream::OrderedTypeStringList() const {
63 std::string next_type_string = upstream_->OrderedTypeStringList();
64 if (next_type_string.empty())
65 return GetTypeAsString();
66 return next_type_string + "," + GetTypeAsString();
67 }
68
69 int FilterSourceStream::DoLoop(int result) {
70 DCHECK_NE(STATE_NONE, next_state_);
71
72 int rv = result;
73 do {
74 State state = next_state_;
75 next_state_ = STATE_NONE;
76 switch (state) {
77 case STATE_READ_DATA:
78 rv = DoReadData();
79 break;
80 case STATE_READ_DATA_COMPLETE:
81 rv = DoReadDataComplete(rv);
82 break;
83 case STATE_FILTER_DATA:
84 DCHECK_LE(0, rv);
85 rv = DoFilterData();
86 break;
87 default:
88 NOTREACHED() << "bad state: " << state;
89 rv = ERR_UNEXPECTED;
90 break;
91 }
92 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
93 return rv;
94 }
95
96 int FilterSourceStream::DoReadData() {
97 // Read more data means subclasses have consumed all input or this is the
98 // first read in which case the |drainable_input_buffer_| is not initialized.
99 DCHECK(drainable_input_buffer_ == nullptr ||
100 0 == drainable_input_buffer_->BytesRemaining());
101
102 next_state_ = STATE_READ_DATA_COMPLETE;
103 // Use base::Unretained here is safe because |this| owns |upstream_|.
104 int rv = upstream_->Read(
105 input_buffer_.get(), kBufferSize,
106 base::Bind(&FilterSourceStream::OnIOComplete, base::Unretained(this)));
107
108 return rv;
109 }
110
111 int FilterSourceStream::DoReadDataComplete(int result) {
112 DCHECK_NE(ERR_IO_PENDING, result);
113
114 // If EOF is read (result == OK), also pass that to the the filter.
Randy Smith (Not in Mondays) 2016/08/22 22:37:15 I don't understand how the comment relates to the
xunjieli 2016/08/29 16:25:28 Done. Sorry about the outdated comment.
115 if (result >= OK) {
116 drainable_input_buffer_ =
117 new DrainableIOBuffer(input_buffer_.get(), result);
118 next_state_ = STATE_FILTER_DATA;
119 }
120 if (result <= OK)
121 upstream_end_reached_ = true;
122 return result;
123 }
124
125 void FilterSourceStream::OnIOComplete(int result) {
126 DCHECK_EQ(STATE_READ_DATA_COMPLETE, next_state_);
127 int rv = DoLoop(result);
128 if (rv == ERR_IO_PENDING)
129 return;
130
131 output_buffer_ = nullptr;
132 output_buffer_size_ = 0;
133
134 base::ResetAndReturn(&callback_).Run(rv);
135 }
136
137 int FilterSourceStream::DoFilterData() {
138 DCHECK(output_buffer_);
139 DCHECK(drainable_input_buffer_);
140
141 int bytes_output = FilterData(output_buffer_.get(), output_buffer_size_,
142 drainable_input_buffer_.get());
143 if (bytes_output == ERR_CONTENT_DECODING_FAILED) {
144 UMA_HISTOGRAM_ENUMERATION("Net.ContentDecodingFailed.FilterType", type(),
145 TYPE_MAX);
146 }
147 // FilterData() is not allowed to return ERR_IO_PENDING.
148 DCHECK_NE(ERR_IO_PENDING, bytes_output);
149
150 // If data can still be read from |upstream_| and filter did not return any
151 // data, it is likely that the filter needs more input.
Randy Smith (Not in Mondays) 2016/08/22 22:37:15 "it is likely": What other things can it mean?
xunjieli 2016/08/29 16:25:28 Done. A misbehaving filter? :) I got rid of "likel
152 if (bytes_output == OK && !upstream_end_reached_)
153 next_state_ = STATE_READ_DATA;
154 return bytes_output;
155 }
156
157 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698