OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 // The basic usage of the Filter interface is described in the comment at |
| 6 // the beginning of filter.h. If Filter::Factory is passed a vector of |
| 7 // size greater than 1, that interface is implemented by a series of filters |
| 8 // connected in a chain. In such a case the first filter |
| 9 // in the chain proxies calls to ReadData() so that its return values |
| 10 // apply to the entire chain. |
| 11 // |
| 12 // In a filter chain, the data flows from first filter (held by the |
| 13 // caller) down the chain. When ReadData() is called on any filter |
| 14 // except for the last filter, it proxies the call down the chain, |
| 15 // filling in the input buffers of subsequent filters if needed (== |
| 16 // that filter's last_status() value is FILTER_NEED_MORE_DATA) and |
| 17 // available (== the current filter has data it can output). The last |
| 18 // Filter will then output data if possible, and return |
| 19 // FILTER_NEED_MORE_DATA if not. Because the indirection pushes |
| 20 // data along the filter chain at each level if it's available and the |
| 21 // next filter needs it, a return value of FILTER_NEED_MORE_DATA from the |
| 22 // final filter will apply to the entire chain. |
| 23 |
5 #include "net/filter/filter.h" | 24 #include "net/filter/filter.h" |
6 | 25 |
7 #include "base/files/file_path.h" | 26 #include "base/files/file_path.h" |
8 #include "base/strings/string_util.h" | 27 #include "base/strings/string_util.h" |
9 #include "net/base/filename_util_unsafe.h" | 28 #include "net/base/filename_util_unsafe.h" |
10 #include "net/base/io_buffer.h" | 29 #include "net/base/io_buffer.h" |
11 #include "net/base/mime_util.h" | 30 #include "net/base/mime_util.h" |
12 #include "net/filter/gzip_filter.h" | 31 #include "net/filter/gzip_filter.h" |
13 #include "net/filter/sdch_filter.h" | 32 #include "net/filter/sdch_filter.h" |
14 #include "net/url_request/url_request_context.h" | 33 #include "net/url_request/url_request_context.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 } | 99 } |
81 return filter_list; | 100 return filter_list; |
82 } | 101 } |
83 | 102 |
84 Filter::FilterStatus Filter::ReadData(char* dest_buffer, int* dest_len) { | 103 Filter::FilterStatus Filter::ReadData(char* dest_buffer, int* dest_len) { |
85 const int dest_buffer_capacity = *dest_len; | 104 const int dest_buffer_capacity = *dest_len; |
86 if (last_status_ == FILTER_ERROR) | 105 if (last_status_ == FILTER_ERROR) |
87 return last_status_; | 106 return last_status_; |
88 if (!next_filter_.get()) | 107 if (!next_filter_.get()) |
89 return last_status_ = ReadFilteredData(dest_buffer, dest_len); | 108 return last_status_ = ReadFilteredData(dest_buffer, dest_len); |
| 109 |
| 110 // This filter needs more data, but it's not clear that the rest of |
| 111 // the chain does; delegate the actual status return to the next filter. |
90 if (last_status_ == FILTER_NEED_MORE_DATA && !stream_data_len()) | 112 if (last_status_ == FILTER_NEED_MORE_DATA && !stream_data_len()) |
91 return next_filter_->ReadData(dest_buffer, dest_len); | 113 return next_filter_->ReadData(dest_buffer, dest_len); |
92 | 114 |
93 do { | 115 do { |
94 if (next_filter_->last_status() == FILTER_NEED_MORE_DATA) { | 116 if (next_filter_->last_status() == FILTER_NEED_MORE_DATA) { |
95 PushDataIntoNextFilter(); | 117 PushDataIntoNextFilter(); |
96 if (FILTER_ERROR == last_status_) | 118 if (FILTER_ERROR == last_status_) |
97 return FILTER_ERROR; | 119 return FILTER_ERROR; |
98 } | 120 } |
99 *dest_len = dest_buffer_capacity; // Reset the input/output parameter. | 121 *dest_len = dest_buffer_capacity; // Reset the input/output parameter. |
(...skipping 24 matching lines...) Expand all Loading... |
124 if (stream_data_len <= 0 || stream_data_len > stream_buffer_size_) | 146 if (stream_data_len <= 0 || stream_data_len > stream_buffer_size_) |
125 return false; | 147 return false; |
126 | 148 |
127 DCHECK(stream_buffer()); | 149 DCHECK(stream_buffer()); |
128 // Bail out if there is more data in the stream buffer to be filtered. | 150 // Bail out if there is more data in the stream buffer to be filtered. |
129 if (!stream_buffer() || stream_data_len_) | 151 if (!stream_buffer() || stream_data_len_) |
130 return false; | 152 return false; |
131 | 153 |
132 next_stream_data_ = stream_buffer()->data(); | 154 next_stream_data_ = stream_buffer()->data(); |
133 stream_data_len_ = stream_data_len; | 155 stream_data_len_ = stream_data_len; |
| 156 last_status_ = FILTER_OK; |
134 return true; | 157 return true; |
135 } | 158 } |
136 | 159 |
137 // static | 160 // static |
138 Filter::FilterType Filter::ConvertEncodingToType( | 161 Filter::FilterType Filter::ConvertEncodingToType( |
139 const std::string& filter_type) { | 162 const std::string& filter_type) { |
140 FilterType type_id; | 163 FilterType type_id; |
141 if (LowerCaseEqualsASCII(filter_type, kDeflate)) { | 164 if (LowerCaseEqualsASCII(filter_type, kDeflate)) { |
142 type_id = FILTER_TYPE_DEFLATE; | 165 type_id = FILTER_TYPE_DEFLATE; |
143 } else if (LowerCaseEqualsASCII(filter_type, kGZip) || | 166 } else if (LowerCaseEqualsASCII(filter_type, kGZip) || |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 | 423 |
401 void Filter::PushDataIntoNextFilter() { | 424 void Filter::PushDataIntoNextFilter() { |
402 IOBuffer* next_buffer = next_filter_->stream_buffer(); | 425 IOBuffer* next_buffer = next_filter_->stream_buffer(); |
403 int next_size = next_filter_->stream_buffer_size(); | 426 int next_size = next_filter_->stream_buffer_size(); |
404 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); | 427 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); |
405 if (FILTER_ERROR != last_status_) | 428 if (FILTER_ERROR != last_status_) |
406 next_filter_->FlushStreamBuffer(next_size); | 429 next_filter_->FlushStreamBuffer(next_size); |
407 } | 430 } |
408 | 431 |
409 } // namespace net | 432 } // namespace net |
OLD | NEW |