Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 28 matching lines...) Expand all Loading... | |
| 39 | 39 |
| 40 } // namespace | 40 } // namespace |
| 41 | 41 |
| 42 namespace net { | 42 namespace net { |
| 43 | 43 |
| 44 FilterContext::~FilterContext() { | 44 FilterContext::~FilterContext() { |
| 45 } | 45 } |
| 46 | 46 |
| 47 Filter::~Filter() {} | 47 Filter::~Filter() {} |
| 48 | 48 |
| 49 // static | |
| 49 Filter* Filter::Factory(const std::vector<FilterType>& filter_types, | 50 Filter* Filter::Factory(const std::vector<FilterType>& filter_types, |
| 50 const FilterContext& filter_context) { | 51 const FilterContext& filter_context) { |
| 51 if (filter_types.empty()) | 52 if (filter_types.empty()) |
| 52 return NULL; | 53 return NULL; |
| 53 | 54 |
| 54 Filter* filter_list = NULL; // Linked list of filters. | 55 Filter* filter_list = NULL; // Linked list of filters. |
| 55 for (size_t i = 0; i < filter_types.size(); i++) { | 56 for (size_t i = 0; i < filter_types.size(); i++) { |
| 56 filter_list = PrependNewFilter(filter_types[i], filter_context, | 57 filter_list = PrependNewFilter(filter_types[i], filter_context, |
| 57 kFilterBufSize, filter_list); | 58 kFilterBufSize, filter_list); |
| 58 if (!filter_list) | 59 if (!filter_list) |
| 59 return NULL; | 60 return NULL; |
| 60 } | 61 } |
| 61 return filter_list; | 62 return filter_list; |
| 62 } | 63 } |
| 63 | 64 |
| 65 // static | |
| 66 Filter* Filter::GZipFactory() { | |
| 67 return InitGZipFilter(FILTER_TYPE_GZIP, kFilterBufSize); | |
| 68 } | |
| 69 | |
| 70 // static | |
| 64 Filter* Filter::FactoryForTests(const std::vector<FilterType>& filter_types, | 71 Filter* Filter::FactoryForTests(const std::vector<FilterType>& filter_types, |
| 65 const FilterContext& filter_context, | 72 const FilterContext& filter_context, |
| 66 int buffer_size) { | 73 int buffer_size) { |
| 67 if (filter_types.empty()) | 74 if (filter_types.empty()) |
| 68 return NULL; | 75 return NULL; |
| 69 | 76 |
| 70 Filter* filter_list = NULL; // Linked list of filters. | 77 Filter* filter_list = NULL; // Linked list of filters. |
| 71 for (size_t i = 0; i < filter_types.size(); i++) { | 78 for (size_t i = 0; i < filter_types.size(); i++) { |
| 72 filter_list = PrependNewFilter(filter_types[i], filter_context, | 79 filter_list = PrependNewFilter(filter_types[i], filter_context, |
| 73 buffer_size, filter_list); | 80 buffer_size, filter_list); |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 if (0 == stream_data_len_) { | 340 if (0 == stream_data_len_) { |
| 334 next_stream_data_ = NULL; | 341 next_stream_data_ = NULL; |
| 335 return Filter::FILTER_NEED_MORE_DATA; | 342 return Filter::FILTER_NEED_MORE_DATA; |
| 336 } else { | 343 } else { |
| 337 next_stream_data_ += out_len; | 344 next_stream_data_ += out_len; |
| 338 return Filter::FILTER_OK; | 345 return Filter::FILTER_OK; |
| 339 } | 346 } |
| 340 } | 347 } |
| 341 | 348 |
| 342 // static | 349 // static |
| 350 Filter* Filter::InitGZipFilter(FilterType type_id, int buffer_size) { | |
| 351 scoped_ptr<GZipFilter> gz_filter(new GZipFilter()); | |
| 352 gz_filter->InitBuffer(buffer_size); | |
| 353 return gz_filter->InitDecoding(type_id) ? gz_filter.release() : NULL; | |
| 354 } | |
| 355 | |
| 356 // static | |
| 357 Filter* Filter::InitSdchFilter(FilterType type_id, | |
| 358 const FilterContext& filter_context, | |
| 359 int buffer_size) { | |
| 360 scoped_ptr<SdchFilter> sdch_filter(new SdchFilter(filter_context)); | |
| 361 sdch_filter->InitBuffer(buffer_size); | |
| 362 return sdch_filter->InitDecoding(type_id) ? sdch_filter.release() : NULL; | |
| 363 } | |
| 364 | |
| 365 // static | |
| 343 Filter* Filter::PrependNewFilter(FilterType type_id, | 366 Filter* Filter::PrependNewFilter(FilterType type_id, |
| 344 const FilterContext& filter_context, | 367 const FilterContext& filter_context, |
| 345 int buffer_size, | 368 int buffer_size, |
| 346 Filter* filter_list) { | 369 Filter* filter_list) { |
| 347 Filter* first_filter = NULL; // Soon to be start of chain. | 370 scoped_ptr<Filter> first_filter; // Soon to be start of chain. |
| 348 switch (type_id) { | 371 switch (type_id) { |
| 349 case FILTER_TYPE_GZIP_HELPING_SDCH: | 372 case FILTER_TYPE_GZIP_HELPING_SDCH: |
| 350 case FILTER_TYPE_DEFLATE: | 373 case FILTER_TYPE_DEFLATE: |
| 351 case FILTER_TYPE_GZIP: { | 374 case FILTER_TYPE_GZIP: |
| 352 scoped_ptr<net::GZipFilter> gz_filter( | 375 first_filter.reset(InitGZipFilter(type_id, buffer_size)); |
| 353 new net::GZipFilter()); | |
| 354 gz_filter->InitBuffer(buffer_size); | |
| 355 if (gz_filter->InitDecoding(type_id)) { | |
| 356 first_filter = gz_filter.release(); | |
| 357 } | |
| 358 break; | 376 break; |
| 359 } | |
| 360 case FILTER_TYPE_SDCH: | 377 case FILTER_TYPE_SDCH: |
| 361 case FILTER_TYPE_SDCH_POSSIBLE: { | 378 case FILTER_TYPE_SDCH_POSSIBLE: |
| 362 scoped_ptr<net::SdchFilter> sdch_filter( | 379 first_filter.reset(InitSdchFilter(type_id, filter_context, buffer_size)); |
| 363 new net::SdchFilter(filter_context)); | |
| 364 sdch_filter->InitBuffer(buffer_size); | |
| 365 if (sdch_filter->InitDecoding(type_id)) { | |
| 366 first_filter = sdch_filter.release(); | |
| 367 } | |
| 368 break; | 380 break; |
| 369 } | 381 default: |
| 370 default: { | |
| 371 break; | 382 break; |
| 372 } | |
| 373 } | 383 } |
| 374 | 384 |
| 375 if (!first_filter) { | 385 if (!first_filter.get()) |
| 376 // Cleanup and exit, since we can't construct this filter list. | |
| 377 delete filter_list; | |
|
eustas
2015/12/01 12:57:23
Won't this cause memleaks?
Usage pattern is:
Filte
| |
| 378 return NULL; | 386 return NULL; |
| 379 } | |
| 380 | 387 |
| 381 first_filter->next_filter_.reset(filter_list); | 388 first_filter->next_filter_.reset(filter_list); |
| 382 return first_filter; | 389 return first_filter.release(); |
| 383 } | 390 } |
| 384 | 391 |
| 385 void Filter::InitBuffer(int buffer_size) { | 392 void Filter::InitBuffer(int buffer_size) { |
| 386 DCHECK(!stream_buffer()); | 393 DCHECK(!stream_buffer()); |
| 387 DCHECK_GT(buffer_size, 0); | 394 DCHECK_GT(buffer_size, 0); |
| 388 stream_buffer_ = new net::IOBuffer(buffer_size); | 395 stream_buffer_ = new net::IOBuffer(buffer_size); |
| 389 stream_buffer_size_ = buffer_size; | 396 stream_buffer_size_ = buffer_size; |
| 390 } | 397 } |
| 391 | 398 |
| 392 void Filter::PushDataIntoNextFilter() { | 399 void Filter::PushDataIntoNextFilter() { |
| 393 net::IOBuffer* next_buffer = next_filter_->stream_buffer(); | 400 net::IOBuffer* next_buffer = next_filter_->stream_buffer(); |
| 394 int next_size = next_filter_->stream_buffer_size(); | 401 int next_size = next_filter_->stream_buffer_size(); |
| 395 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); | 402 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); |
| 396 if (FILTER_ERROR != last_status_) | 403 if (FILTER_ERROR != last_status_) |
| 397 next_filter_->FlushStreamBuffer(next_size); | 404 next_filter_->FlushStreamBuffer(next_size); |
| 398 } | 405 } |
| 399 | 406 |
| 400 } // namespace net | 407 } // namespace net |
| OLD | NEW |