| OLD | NEW |
| (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( |
| 22 SourceType type, |
| 23 std::unique_ptr<SourceStream> next_stream) |
| 24 : SourceStream(type), |
| 25 next_stream_(std::move(next_stream)), |
| 26 next_state_(STATE_NONE), |
| 27 output_buffer_size_(0), |
| 28 next_stream_end_reached_(false) { |
| 29 DCHECK(next_stream_); |
| 30 } |
| 31 |
| 32 FilterSourceStream::~FilterSourceStream() {} |
| 33 |
| 34 int FilterSourceStream::Read(IOBuffer* read_buffer, |
| 35 size_t read_buffer_size, |
| 36 const CompletionCallback& callback) { |
| 37 // Allocate a BlockBuffer during first Read(). |
| 38 if (!input_buffer_) |
| 39 input_buffer_ = new IOBufferWithSize(kBufferSize); |
| 40 |
| 41 // Start with filtering data, which tells us whether it needs input data. |
| 42 next_state_ = STATE_FILTER_DATA; |
| 43 |
| 44 output_buffer_ = read_buffer; |
| 45 output_buffer_size_ = read_buffer_size; |
| 46 int rv = DoLoop(OK); |
| 47 if (rv > OK) { |
| 48 return rv; |
| 49 } else if (rv == ERR_IO_PENDING) { |
| 50 callback_ = callback; |
| 51 } |
| 52 return rv; |
| 53 } |
| 54 |
| 55 std::string FilterSourceStream::OrderedTypeStringList() const { |
| 56 std::string next_type_string = next_stream_->OrderedTypeStringList(); |
| 57 if (next_type_string.empty()) |
| 58 return GetTypeAsString(); |
| 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_stream_|. |
| 94 int rv = |
| 95 next_stream_->Read(input_buffer_.get(), kBufferSize, |
| 96 base::Bind(&FilterSourceStream::OnNextReadCompleted, |
| 97 base::Unretained(this))); |
| 98 |
| 99 if (rv != ERR_IO_PENDING) |
| 100 next_state_ = STATE_READ_DATA_COMPLETE; |
| 101 return rv; |
| 102 } |
| 103 |
| 104 int FilterSourceStream::DoReadDataComplete(int result) { |
| 105 DCHECK_NE(ERR_IO_PENDING, result); |
| 106 |
| 107 if (result > OK) { |
| 108 drainable_input_buffer_ = |
| 109 new DrainableIOBuffer(input_buffer_.get(), result); |
| 110 next_state_ = STATE_FILTER_DATA; |
| 111 } else { |
| 112 next_stream_end_reached_ = true; |
| 113 } |
| 114 return result; |
| 115 } |
| 116 |
| 117 void FilterSourceStream::OnNextReadCompleted(int result) { |
| 118 next_state_ = STATE_READ_DATA_COMPLETE; |
| 119 int rv = DoLoop(result); |
| 120 if (rv != ERR_IO_PENDING) |
| 121 DoCallback(rv); |
| 122 } |
| 123 |
| 124 int FilterSourceStream::DoFilterData(int result) { |
| 125 DCHECK(output_buffer_); |
| 126 DCHECK_LE(0, result); |
| 127 |
| 128 // This is first Read(), short circuit it and go straight to read data from |
| 129 // |next_stream_|. |
| 130 if (drainable_input_buffer_ == nullptr) { |
| 131 next_state_ = STATE_READ_DATA; |
| 132 return OK; |
| 133 } |
| 134 |
| 135 int bytes_output = FilterData(output_buffer_.get(), output_buffer_size_, |
| 136 drainable_input_buffer_.get()); |
| 137 if (bytes_output == ERR_CONTENT_DECODING_FAILED) { |
| 138 UMA_HISTOGRAM_ENUMERATION("Net.ContentDecodingFailed.FilterType", type(), |
| 139 TYPE_MAX); |
| 140 } |
| 141 // FilterData() is not allowed to return ERR_IO_PENDING. |
| 142 DCHECK_NE(ERR_IO_PENDING, bytes_output); |
| 143 |
| 144 // If can still read data from |next_stream_| and filter did not return any |
| 145 // data, |
| 146 // it is likely that the filter needs more input. |
| 147 if (bytes_output == OK && !next_stream_end_reached_) |
| 148 next_state_ = STATE_READ_DATA; |
| 149 return bytes_output; |
| 150 } |
| 151 |
| 152 void FilterSourceStream::DoCallback(int result) { |
| 153 DCHECK_NE(ERR_IO_PENDING, result); |
| 154 DCHECK(!callback_.is_null()); |
| 155 |
| 156 output_buffer_ = nullptr; |
| 157 output_buffer_size_ = 0; |
| 158 |
| 159 base::ResetAndReturn(&callback_).Run(result); |
| 160 } |
| 161 |
| 162 } // namespace net |
| OLD | NEW |