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

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

Issue 1662763002: [ON HOLD] Implement pull-based design for content decoding (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address Matt's comments 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/metrics/histogram_macros.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "base/strings/string_util.h"
12
13 namespace net {
14
15 namespace {
16
17 const size_t kBufferSize = 32 * 1024;
18
19 } // namespace
20
21 FilterSourceStream::FilterSourceStream(SourceType type,
22 std::unique_ptr<SourceStream> next)
23 : SourceStream(type),
24 next_(std::move(next)),
25 next_state_(STATE_NONE),
26 output_buffer_size_(0),
27 next_end_reached_(false) {
28 DCHECK(next_);
29 }
30
31 FilterSourceStream::~FilterSourceStream() {}
32
33 int FilterSourceStream::Read(IOBuffer* read_buffer,
34 size_t read_buffer_size,
35 const CompletionCallback& callback) {
36 // Allocate a BlockBuffer during first Read().
37 if (!input_buffer_) {
38 input_buffer_ = new IOBufferWithSize(kBufferSize);
39 }
mmenke 2016/08/01 21:55:30 nit: Remove braces.
xunjieli 2016/08/02 13:44:31 Done.
40 // Start with filtering data, which tells us whether it needs input data.
41 next_state_ = STATE_FILTER_DATA;
42
43 output_buffer_ = read_buffer;
44 output_buffer_size_ = read_buffer_size;
45 int rv = DoLoop(OK);
46 if (rv > OK) {
47 return rv;
48 } else if (rv == ERR_IO_PENDING) {
49 callback_ = callback;
50 }
51 return static_cast<Error>(rv);
mmenke 2016/08/01 21:55:30 cast not needed.
xunjieli 2016/08/02 13:44:31 Done.
52 }
53
54 std::string FilterSourceStream::OrderedTypeStringList() const {
55 std::string next_type_string = next_->OrderedTypeStringList();
56 if (next_type_string.empty()) {
57 return GetTypeAsString();
58 }
mmenke 2016/08/01 21:55:30 nit: Remove braces
xunjieli 2016/08/02 13:44:31 Done.
59 return next_type_string + "," + GetTypeAsString();
60 }
61
62 int FilterSourceStream::DoLoop(int result) {
63 DCHECK(this);
64 DCHECK_NE(STATE_NONE, next_state_);
65 int rv = result;
66 do {
67 State state = next_state_;
68 next_state_ = STATE_NONE;
69 switch (state) {
70 case STATE_READ_DATA:
71 rv = DoReadData();
72 break;
73 case STATE_READ_DATA_COMPLETE:
74 rv = DoReadDataComplete(rv);
75 break;
76 case STATE_FILTER_DATA:
77 rv = DoFilterData(rv);
78 break;
79 default:
80 NOTREACHED() << "bad state: " << state;
81 rv = ERR_UNEXPECTED;
82 break;
83 }
84 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
85 return rv;
86 }
87
88 int FilterSourceStream::DoReadData() {
89 // Read more data means subclasses have consumed all input or this is the
90 // first read in which case the |drainable_input_buffer_| is not initialized.
91 DCHECK(drainable_input_buffer_ == nullptr ||
92 0 == drainable_input_buffer_->BytesRemaining());
93 // Use base::Unretained here is safe because |this| owns |next_|.
94 int rv = next_->Read(input_buffer_.get(), kBufferSize,
95 base::Bind(&FilterSourceStream::OnNextReadCompleted,
96 base::Unretained(this)));
97
98 if (rv != ERR_IO_PENDING)
99 next_state_ = STATE_READ_DATA_COMPLETE;
100 return rv;
101 }
102
103 int FilterSourceStream::DoReadDataComplete(int result) {
104 DCHECK_NE(ERR_IO_PENDING, result);
105
106 if (result > OK) {
107 drainable_input_buffer_ = new DrainableIOBuffer(
108 input_buffer_.get(), base::checked_cast<size_t>(result));
mmenke 2016/08/01 21:55:30 static_cast (We have the > OK check just above, so
xunjieli 2016/08/02 13:44:31 Done.
109 next_state_ = STATE_FILTER_DATA;
110 } else {
111 next_end_reached_ = true;
112 }
113 return result;
114 }
115
116 void FilterSourceStream::OnNextReadCompleted(int result) {
117 next_state_ = STATE_READ_DATA_COMPLETE;
118 int rv = DoLoop(result);
119 if (rv != ERR_IO_PENDING)
120 DoCallback(rv);
121 }
122
123 int FilterSourceStream::DoFilterData(int result) {
124 DCHECK(output_buffer_);
125 DCHECK_LE(0, result);
126
127 // This is first Read(), short circuit it and go straight to read data from
128 // |next_|.
129 if (drainable_input_buffer_ == nullptr) {
130 next_state_ = STATE_READ_DATA;
131 return OK;
132 }
133
134 int bytes_output = FilterData(output_buffer_.get(), output_buffer_size_,
135 drainable_input_buffer_.get());
136 if (bytes_output == ERR_CONTENT_DECODING_FAILED) {
137 UMA_HISTOGRAM_ENUMERATION("Net.ContentDecodingFailed.FilterType", type(),
138 TYPE_MAX);
139 }
140 // FilterData() is not allowed to return ERR_IO_PENDING.
141 DCHECK_NE(ERR_IO_PENDING, bytes_output);
142
143 // If can still read data from |next_| and filter did not return any data,
144 // it is likely that the filter needs more input.
145 if (bytes_output == OK && !next_end_reached_)
146 next_state_ = STATE_READ_DATA;
147 return bytes_output;
148 }
149
150 void FilterSourceStream::DoCallback(int result) {
151 DCHECK_NE(ERR_IO_PENDING, result);
152
153 output_buffer_ = nullptr;
154 output_buffer_size_ = 0;
155
156 DCHECK(!callback_.is_null());
mmenke 2016/08/01 21:55:30 Suggest putting this up next to DCHECK_NE(ERR_IO_P
xunjieli 2016/08/02 13:44:31 Done.
157 base::ResetAndReturn(&callback_).Run(result);
158 }
159
160 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698