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

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

Issue 6314010: Even more reordering the methods in headers and implementation in net/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « net/base/filter.h ('k') | net/base/host_resolver.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 #include "net/base/filter.h" 5 #include "net/base/filter.h"
6 6
7 #include "base/file_path.h" 7 #include "base/file_path.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "net/base/gzip_filter.h" 9 #include "net/base/gzip_filter.h"
10 #include "net/base/io_buffer.h" 10 #include "net/base/io_buffer.h"
(...skipping 21 matching lines...) Expand all
32 const char kApplicationXGunzip[] = "application/x-gunzip"; 32 const char kApplicationXGunzip[] = "application/x-gunzip";
33 const char kApplicationXCompress[] = "application/x-compress"; 33 const char kApplicationXCompress[] = "application/x-compress";
34 const char kApplicationCompress[] = "application/compress"; 34 const char kApplicationCompress[] = "application/compress";
35 const char kTextHtml[] = "text/html"; 35 const char kTextHtml[] = "text/html";
36 36
37 } // namespace 37 } // namespace
38 38
39 FilterContext::~FilterContext() { 39 FilterContext::~FilterContext() {
40 } 40 }
41 41
42 Filter::~Filter() {}
43
42 Filter* Filter::Factory(const std::vector<FilterType>& filter_types, 44 Filter* Filter::Factory(const std::vector<FilterType>& filter_types,
43 const FilterContext& filter_context) { 45 const FilterContext& filter_context) {
44 DCHECK_GT(filter_context.GetInputStreamBufferSize(), 0); 46 DCHECK_GT(filter_context.GetInputStreamBufferSize(), 0);
45 if (filter_types.empty() || filter_context.GetInputStreamBufferSize() <= 0) 47 if (filter_types.empty() || filter_context.GetInputStreamBufferSize() <= 0)
46 return NULL; 48 return NULL;
47 49
48 50
49 Filter* filter_list = NULL; // Linked list of filters. 51 Filter* filter_list = NULL; // Linked list of filters.
50 for (size_t i = 0; i < filter_types.size(); i++) { 52 for (size_t i = 0; i < filter_types.size(); i++) {
51 filter_list = PrependNewFilter(filter_types[i], filter_context, 53 filter_list = PrependNewFilter(filter_types[i], filter_context,
52 filter_list); 54 filter_list);
53 if (!filter_list) 55 if (!filter_list)
54 return NULL; 56 return NULL;
55 } 57 }
56 return filter_list; 58 return filter_list;
57 } 59 }
58 60
61 Filter::FilterStatus Filter::ReadData(char* dest_buffer, int* dest_len) {
62 const int dest_buffer_capacity = *dest_len;
63 if (last_status_ == FILTER_ERROR)
64 return last_status_;
65 if (!next_filter_.get())
66 return last_status_ = ReadFilteredData(dest_buffer, dest_len);
67 if (last_status_ == FILTER_NEED_MORE_DATA && !stream_data_len())
68 return next_filter_->ReadData(dest_buffer, dest_len);
69
70 do {
71 if (next_filter_->last_status() == FILTER_NEED_MORE_DATA) {
72 PushDataIntoNextFilter();
73 if (FILTER_ERROR == last_status_)
74 return FILTER_ERROR;
75 }
76 *dest_len = dest_buffer_capacity; // Reset the input/output parameter.
77 next_filter_->ReadData(dest_buffer, dest_len);
78 if (FILTER_NEED_MORE_DATA == last_status_)
79 return next_filter_->last_status();
80
81 // In the case where this filter has data internally, and is indicating such
82 // with a last_status_ of FILTER_OK, but at the same time the next filter in
83 // the chain indicated it FILTER_NEED_MORE_DATA, we have to be cautious
84 // about confusing the caller. The API confusion can appear if we return
85 // FILTER_OK (suggesting we have more data in aggregate), but yet we don't
86 // populate our output buffer. When that is the case, we need to
87 // alternately call our filter element, and the next_filter element until we
88 // get out of this state (by pumping data into the next filter until it
89 // outputs data, or it runs out of data and reports that it NEED_MORE_DATA.)
90 } while (FILTER_OK == last_status_ &&
91 FILTER_NEED_MORE_DATA == next_filter_->last_status() &&
92 0 == *dest_len);
93
94 if (next_filter_->last_status() == FILTER_ERROR)
95 return FILTER_ERROR;
96 return FILTER_OK;
97 }
98
99 bool Filter::FlushStreamBuffer(int stream_data_len) {
100 DCHECK(stream_data_len <= stream_buffer_size_);
101 if (stream_data_len <= 0 || stream_data_len > stream_buffer_size_)
102 return false;
103
104 DCHECK(stream_buffer());
105 // Bail out if there is more data in the stream buffer to be filtered.
106 if (!stream_buffer() || stream_data_len_)
107 return false;
108
109 next_stream_data_ = stream_buffer()->data();
110 stream_data_len_ = stream_data_len;
111 return true;
112 }
113
59 // static 114 // static
60 Filter::FilterType Filter::ConvertEncodingToType( 115 Filter::FilterType Filter::ConvertEncodingToType(
61 const std::string& filter_type) { 116 const std::string& filter_type) {
62 FilterType type_id; 117 FilterType type_id;
63 if (LowerCaseEqualsASCII(filter_type, kDeflate)) { 118 if (LowerCaseEqualsASCII(filter_type, kDeflate)) {
64 type_id = FILTER_TYPE_DEFLATE; 119 type_id = FILTER_TYPE_DEFLATE;
65 } else if (LowerCaseEqualsASCII(filter_type, kGZip) || 120 } else if (LowerCaseEqualsASCII(filter_type, kGZip) ||
66 LowerCaseEqualsASCII(filter_type, kXGZip)) { 121 LowerCaseEqualsASCII(filter_type, kXGZip)) {
67 type_id = FILTER_TYPE_GZIP; 122 type_id = FILTER_TYPE_GZIP;
68 } else if (LowerCaseEqualsASCII(filter_type, kSdch)) { 123 } else if (LowerCaseEqualsASCII(filter_type, kSdch)) {
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 // gunzip and tentative SDCH decoding. 280 // gunzip and tentative SDCH decoding.
226 // This approach nicely handles the empty() list as well, and should work with 281 // This approach nicely handles the empty() list as well, and should work with
227 // other (as yet undiscovered) proxies the choose to re-compressed with some 282 // other (as yet undiscovered) proxies the choose to re-compressed with some
228 // other encoding (such as bzip2, etc.). 283 // other encoding (such as bzip2, etc.).
229 encoding_types->insert(encoding_types->begin(), 284 encoding_types->insert(encoding_types->begin(),
230 FILTER_TYPE_GZIP_HELPING_SDCH); 285 FILTER_TYPE_GZIP_HELPING_SDCH);
231 encoding_types->insert(encoding_types->begin(), FILTER_TYPE_SDCH_POSSIBLE); 286 encoding_types->insert(encoding_types->begin(), FILTER_TYPE_SDCH_POSSIBLE);
232 return; 287 return;
233 } 288 }
234 289
290 Filter::Filter(const FilterContext& filter_context)
291 : stream_buffer_(NULL),
292 stream_buffer_size_(0),
293 next_stream_data_(NULL),
294 stream_data_len_(0),
295 next_filter_(NULL),
296 last_status_(FILTER_NEED_MORE_DATA),
297 filter_context_(filter_context) {
298 }
299
300 Filter::FilterStatus Filter::ReadFilteredData(char* dest_buffer,
301 int* dest_len) {
302 return Filter::FILTER_ERROR;
303 }
304
305 Filter::FilterStatus Filter::CopyOut(char* dest_buffer, int* dest_len) {
306 int out_len;
307 int input_len = *dest_len;
308 *dest_len = 0;
309
310 if (0 == stream_data_len_)
311 return Filter::FILTER_NEED_MORE_DATA;
312
313 out_len = std::min(input_len, stream_data_len_);
314 memcpy(dest_buffer, next_stream_data_, out_len);
315 *dest_len += out_len;
316 stream_data_len_ -= out_len;
317 if (0 == stream_data_len_) {
318 next_stream_data_ = NULL;
319 return Filter::FILTER_NEED_MORE_DATA;
320 } else {
321 next_stream_data_ += out_len;
322 return Filter::FILTER_OK;
323 }
324 }
325
235 // static 326 // static
236 Filter* Filter::PrependNewFilter(FilterType type_id, 327 Filter* Filter::PrependNewFilter(FilterType type_id,
237 const FilterContext& filter_context, 328 const FilterContext& filter_context,
238 Filter* filter_list) { 329 Filter* filter_list) {
239 Filter* first_filter = NULL; // Soon to be start of chain. 330 Filter* first_filter = NULL; // Soon to be start of chain.
240 switch (type_id) { 331 switch (type_id) {
241 case FILTER_TYPE_GZIP_HELPING_SDCH: 332 case FILTER_TYPE_GZIP_HELPING_SDCH:
242 case FILTER_TYPE_DEFLATE: 333 case FILTER_TYPE_DEFLATE:
243 case FILTER_TYPE_GZIP: { 334 case FILTER_TYPE_GZIP: {
244 scoped_ptr<GZipFilter> gz_filter(new GZipFilter(filter_context)); 335 scoped_ptr<GZipFilter> gz_filter(new GZipFilter(filter_context));
(...skipping 22 matching lines...) Expand all
267 if (!first_filter) { 358 if (!first_filter) {
268 // Cleanup and exit, since we can't construct this filter list. 359 // Cleanup and exit, since we can't construct this filter list.
269 delete filter_list; 360 delete filter_list;
270 return NULL; 361 return NULL;
271 } 362 }
272 363
273 first_filter->next_filter_.reset(filter_list); 364 first_filter->next_filter_.reset(filter_list);
274 return first_filter; 365 return first_filter;
275 } 366 }
276 367
277 Filter::Filter(const FilterContext& filter_context)
278 : stream_buffer_(NULL),
279 stream_buffer_size_(0),
280 next_stream_data_(NULL),
281 stream_data_len_(0),
282 next_filter_(NULL),
283 last_status_(FILTER_NEED_MORE_DATA),
284 filter_context_(filter_context) {
285 }
286
287 Filter::~Filter() {}
288
289 bool Filter::InitBuffer() { 368 bool Filter::InitBuffer() {
290 int buffer_size = filter_context_.GetInputStreamBufferSize(); 369 int buffer_size = filter_context_.GetInputStreamBufferSize();
291 DCHECK_GT(buffer_size, 0); 370 DCHECK_GT(buffer_size, 0);
292 if (buffer_size <= 0 || stream_buffer()) 371 if (buffer_size <= 0 || stream_buffer())
293 return false; 372 return false;
294 373
295 stream_buffer_ = new net::IOBuffer(buffer_size); 374 stream_buffer_ = new net::IOBuffer(buffer_size);
296 375
297 if (stream_buffer()) { 376 if (stream_buffer()) {
298 stream_buffer_size_ = buffer_size; 377 stream_buffer_size_ = buffer_size;
299 return true; 378 return true;
300 } 379 }
301 380
302 return false; 381 return false;
303 } 382 }
304 383
305
306 Filter::FilterStatus Filter::CopyOut(char* dest_buffer, int* dest_len) {
307 int out_len;
308 int input_len = *dest_len;
309 *dest_len = 0;
310
311 if (0 == stream_data_len_)
312 return Filter::FILTER_NEED_MORE_DATA;
313
314 out_len = std::min(input_len, stream_data_len_);
315 memcpy(dest_buffer, next_stream_data_, out_len);
316 *dest_len += out_len;
317 stream_data_len_ -= out_len;
318 if (0 == stream_data_len_) {
319 next_stream_data_ = NULL;
320 return Filter::FILTER_NEED_MORE_DATA;
321 } else {
322 next_stream_data_ += out_len;
323 return Filter::FILTER_OK;
324 }
325 }
326
327
328 Filter::FilterStatus Filter::ReadFilteredData(char* dest_buffer,
329 int* dest_len) {
330 return Filter::FILTER_ERROR;
331 }
332
333 Filter::FilterStatus Filter::ReadData(char* dest_buffer, int* dest_len) {
334 const int dest_buffer_capacity = *dest_len;
335 if (last_status_ == FILTER_ERROR)
336 return last_status_;
337 if (!next_filter_.get())
338 return last_status_ = ReadFilteredData(dest_buffer, dest_len);
339 if (last_status_ == FILTER_NEED_MORE_DATA && !stream_data_len())
340 return next_filter_->ReadData(dest_buffer, dest_len);
341
342 do {
343 if (next_filter_->last_status() == FILTER_NEED_MORE_DATA) {
344 PushDataIntoNextFilter();
345 if (FILTER_ERROR == last_status_)
346 return FILTER_ERROR;
347 }
348 *dest_len = dest_buffer_capacity; // Reset the input/output parameter.
349 next_filter_->ReadData(dest_buffer, dest_len);
350 if (FILTER_NEED_MORE_DATA == last_status_)
351 return next_filter_->last_status();
352
353 // In the case where this filter has data internally, and is indicating such
354 // with a last_status_ of FILTER_OK, but at the same time the next filter in
355 // the chain indicated it FILTER_NEED_MORE_DATA, we have to be cautious
356 // about confusing the caller. The API confusion can appear if we return
357 // FILTER_OK (suggesting we have more data in aggregate), but yet we don't
358 // populate our output buffer. When that is the case, we need to
359 // alternately call our filter element, and the next_filter element until we
360 // get out of this state (by pumping data into the next filter until it
361 // outputs data, or it runs out of data and reports that it NEED_MORE_DATA.)
362 } while (FILTER_OK == last_status_ &&
363 FILTER_NEED_MORE_DATA == next_filter_->last_status() &&
364 0 == *dest_len);
365
366 if (next_filter_->last_status() == FILTER_ERROR)
367 return FILTER_ERROR;
368 return FILTER_OK;
369 }
370
371 void Filter::PushDataIntoNextFilter() { 384 void Filter::PushDataIntoNextFilter() {
372 net::IOBuffer* next_buffer = next_filter_->stream_buffer(); 385 net::IOBuffer* next_buffer = next_filter_->stream_buffer();
373 int next_size = next_filter_->stream_buffer_size(); 386 int next_size = next_filter_->stream_buffer_size();
374 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); 387 last_status_ = ReadFilteredData(next_buffer->data(), &next_size);
375 if (FILTER_ERROR != last_status_) 388 if (FILTER_ERROR != last_status_)
376 next_filter_->FlushStreamBuffer(next_size); 389 next_filter_->FlushStreamBuffer(next_size);
377 } 390 }
378
379
380 bool Filter::FlushStreamBuffer(int stream_data_len) {
381 DCHECK(stream_data_len <= stream_buffer_size_);
382 if (stream_data_len <= 0 || stream_data_len > stream_buffer_size_)
383 return false;
384
385 DCHECK(stream_buffer());
386 // Bail out if there is more data in the stream buffer to be filtered.
387 if (!stream_buffer() || stream_data_len_)
388 return false;
389
390 next_stream_data_ = stream_buffer()->data();
391 stream_data_len_ = stream_data_len;
392 return true;
393 }
OLDNEW
« no previous file with comments | « net/base/filter.h ('k') | net/base/host_resolver.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698