| OLD | NEW |
| 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 // Filter performs filtering on data streams. Sample usage: | 5 // Filter performs filtering on data streams. Sample usage: |
| 6 // | 6 // |
| 7 // IStream* pre_filter_source; | 7 // IStream* pre_filter_source; |
| 8 // ... | 8 // ... |
| 9 // Filter* filter = Filter::Factory(filter_type, size); | 9 // Filter* filter = Filter::Factory(filter_type, size); |
| 10 // int pre_filter_data_len = filter->stream_buffer_size(); | 10 // int pre_filter_data_len = filter->stream_buffer_size(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 #include "base/basictypes.h" | 36 #include "base/basictypes.h" |
| 37 #include "base/gtest_prod_util.h" | 37 #include "base/gtest_prod_util.h" |
| 38 #include "base/ref_counted.h" | 38 #include "base/ref_counted.h" |
| 39 #include "base/scoped_ptr.h" | 39 #include "base/scoped_ptr.h" |
| 40 #include "base/time.h" | 40 #include "base/time.h" |
| 41 | 41 |
| 42 class GURL; | 42 class GURL; |
| 43 | 43 |
| 44 namespace net { | 44 namespace net { |
| 45 class IOBuffer; | 45 class IOBuffer; |
| 46 class SdchFilterChainingTest; |
| 46 } | 47 } |
| 47 | 48 |
| 48 //------------------------------------------------------------------------------ | 49 //------------------------------------------------------------------------------ |
| 49 // Define an interface class that allows access to contextual information | 50 // Define an interface class that allows access to contextual information |
| 50 // supplied by the owner of this filter. In the case where there are a chain of | 51 // supplied by the owner of this filter. In the case where there are a chain of |
| 51 // filters, there is only one owner of all the chained filters, and that context | 52 // filters, there is only one owner of all the chained filters, and that context |
| 52 // is passed to the constructor of all those filters. To be clear, the context | 53 // is passed to the constructor of all those filters. To be clear, the context |
| 53 // does NOT reflect the position in a chain, or the fact that there are prior | 54 // does NOT reflect the position in a chain, or the fact that there are prior |
| 54 // or later filters in a chain. | 55 // or later filters in a chain. |
| 55 class FilterContext { | 56 class FilterContext { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 virtual bool IsSdchResponse() const = 0; | 88 virtual bool IsSdchResponse() const = 0; |
| 88 | 89 |
| 89 // How many bytes were read from the net or cache so far (and potentially | 90 // How many bytes were read from the net or cache so far (and potentially |
| 90 // pushed into a filter for processing)? | 91 // pushed into a filter for processing)? |
| 91 virtual int64 GetByteReadCount() const = 0; | 92 virtual int64 GetByteReadCount() const = 0; |
| 92 | 93 |
| 93 // What response code was received with the associated network transaction? | 94 // What response code was received with the associated network transaction? |
| 94 // For example: 200 is ok. 4xx are error codes. etc. | 95 // For example: 200 is ok. 4xx are error codes. etc. |
| 95 virtual int GetResponseCode() const = 0; | 96 virtual int GetResponseCode() const = 0; |
| 96 | 97 |
| 97 // What is the desirable input buffer size for these filters? | |
| 98 // This value is currently supplied by the context, and is constant for all | |
| 99 // filters, even when they are part of a chain of filters. (i.e., we currently | |
| 100 // don't change the input buffer sizes for a linked chain of filters, and the | |
| 101 // buffer size for input to all filters in a chain is this one constant). | |
| 102 virtual int GetInputStreamBufferSize() const = 0; | |
| 103 | |
| 104 // The following method forces the context to emit a specific set of | 98 // The following method forces the context to emit a specific set of |
| 105 // statistics as selected by the argument. | 99 // statistics as selected by the argument. |
| 106 virtual void RecordPacketStats(StatisticSelector statistic) const = 0; | 100 virtual void RecordPacketStats(StatisticSelector statistic) const = 0; |
| 107 }; | 101 }; |
| 108 | 102 |
| 109 //------------------------------------------------------------------------------ | 103 //------------------------------------------------------------------------------ |
| 110 class Filter { | 104 class Filter { |
| 111 public: | 105 public: |
| 112 // Return values of function ReadFilteredData. | 106 // Return values of function ReadFilteredData. |
| 113 enum FilterStatus { | 107 enum FilterStatus { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 // to the list. This includes handling known bugs in the Apache server (where | 186 // to the list. This includes handling known bugs in the Apache server (where |
| 193 // redundant gzip encoding is specified), as well as issues regarding SDCH | 187 // redundant gzip encoding is specified), as well as issues regarding SDCH |
| 194 // encoding, where various proxies and anti-virus products modify or strip the | 188 // encoding, where various proxies and anti-virus products modify or strip the |
| 195 // encodings. These fixups require context, which includes whether this | 189 // encodings. These fixups require context, which includes whether this |
| 196 // response was made to an SDCH request (i.e., an available dictionary was | 190 // response was made to an SDCH request (i.e., an available dictionary was |
| 197 // advertised in the GET), as well as the mime type of the content. | 191 // advertised in the GET), as well as the mime type of the content. |
| 198 static void FixupEncodingTypes(const FilterContext& filter_context, | 192 static void FixupEncodingTypes(const FilterContext& filter_context, |
| 199 std::vector<FilterType>* encoding_types); | 193 std::vector<FilterType>* encoding_types); |
| 200 | 194 |
| 201 protected: | 195 protected: |
| 202 FRIEND_TEST_ALL_PREFIXES(SdchFilterTest, ContentTypeId); | 196 friend class GZipUnitTest; |
| 197 friend class net::SdchFilterChainingTest; |
| 203 | 198 |
| 204 explicit Filter(const FilterContext& filter_context); | 199 explicit Filter(const FilterContext& filter_context); |
| 205 | 200 |
| 206 // Filters the data stored in stream_buffer_ and writes the output into the | 201 // Filters the data stored in stream_buffer_ and writes the output into the |
| 207 // dest_buffer passed in. | 202 // dest_buffer passed in. |
| 208 // | 203 // |
| 209 // Upon entry, *dest_len is the total size (in number of chars) of the | 204 // Upon entry, *dest_len is the total size (in number of chars) of the |
| 210 // destination buffer. Upon exit, *dest_len is the actual number of chars | 205 // destination buffer. Upon exit, *dest_len is the actual number of chars |
| 211 // written into the destination buffer. | 206 // written into the destination buffer. |
| 212 // | 207 // |
| (...skipping 16 matching lines...) Expand all Loading... |
| 229 // Maximum size of stream_buffer_ in number of chars. | 224 // Maximum size of stream_buffer_ in number of chars. |
| 230 int stream_buffer_size_; | 225 int stream_buffer_size_; |
| 231 | 226 |
| 232 // Pointer to the next data in stream_buffer_ to be filtered. | 227 // Pointer to the next data in stream_buffer_ to be filtered. |
| 233 char* next_stream_data_; | 228 char* next_stream_data_; |
| 234 | 229 |
| 235 // Total number of remaining chars in stream_buffer_ to be filtered. | 230 // Total number of remaining chars in stream_buffer_ to be filtered. |
| 236 int stream_data_len_; | 231 int stream_data_len_; |
| 237 | 232 |
| 238 private: | 233 private: |
| 234 // Allocates and initializes stream_buffer_ and stream_buffer_size_. |
| 235 void InitBuffer(int size); |
| 236 |
| 239 // A factory helper for creating filters for within a chain of potentially | 237 // A factory helper for creating filters for within a chain of potentially |
| 240 // multiple encodings. If a chain of filters is created, then this may be | 238 // multiple encodings. If a chain of filters is created, then this may be |
| 241 // called multiple times during the filter creation process. In most simple | 239 // called multiple times during the filter creation process. In most simple |
| 242 // cases, this is only called once. Returns NULL and cleans up (deleting | 240 // cases, this is only called once. Returns NULL and cleans up (deleting |
| 243 // filter_list) if a new filter can't be constructed. | 241 // filter_list) if a new filter can't be constructed. |
| 244 static Filter* PrependNewFilter(FilterType type_id, | 242 static Filter* PrependNewFilter(FilterType type_id, |
| 245 const FilterContext& filter_context, | 243 const FilterContext& filter_context, |
| 244 int buffer_size, |
| 246 Filter* filter_list); | 245 Filter* filter_list); |
| 247 | 246 |
| 248 // Allocates and initializes stream_buffer_ based on filter_context_. | |
| 249 // Establishes a buffer large enough to handle the amount specified in | |
| 250 // filter_context_.GetInputStreamBufferSize(). | |
| 251 bool InitBuffer(); | |
| 252 | |
| 253 // Helper function to empty our output into the next filter's input. | 247 // Helper function to empty our output into the next filter's input. |
| 254 void PushDataIntoNextFilter(); | 248 void PushDataIntoNextFilter(); |
| 255 | 249 |
| 250 // Constructs a filter with an internal buffer of the given size. |
| 251 // Only meant to be called by unit tests that need to control the buffer size. |
| 252 static Filter* FactoryForTests(const std::vector<FilterType>& filter_types, |
| 253 const FilterContext& filter_context, |
| 254 int buffer_size); |
| 255 |
| 256 // An optional filter to process output from this filter. | 256 // An optional filter to process output from this filter. |
| 257 scoped_ptr<Filter> next_filter_; | 257 scoped_ptr<Filter> next_filter_; |
| 258 // Remember what status or local filter last returned so we can better handle | 258 // Remember what status or local filter last returned so we can better handle |
| 259 // chained filters. | 259 // chained filters. |
| 260 FilterStatus last_status_; | 260 FilterStatus last_status_; |
| 261 | 261 |
| 262 // Context data from the owner of this filter. Some filters need additional | 262 // Context data from the owner of this filter. Some filters need additional |
| 263 // context information (mime type, etc.) to properly function, and they access | 263 // context information (mime type, etc.) to properly function, and they access |
| 264 // this data via this reference member. | 264 // this data via this reference member. |
| 265 const FilterContext& filter_context_; | 265 const FilterContext& filter_context_; |
| 266 | 266 |
| 267 DISALLOW_COPY_AND_ASSIGN(Filter); | 267 DISALLOW_COPY_AND_ASSIGN(Filter); |
| 268 }; | 268 }; |
| 269 | 269 |
| 270 #endif // NET_BASE_FILTER_H__ | 270 #endif // NET_BASE_FILTER_H__ |
| OLD | NEW |