| 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 // 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(); |
| 11 // pre_filter_source->read(filter->stream_buffer(), pre_filter_data_len); | 11 // pre_filter_source->read(filter->stream_buffer(), pre_filter_data_len); |
| 12 // | 12 // |
| 13 // filter->FlushStreamBuffer(pre_filter_data_len); | 13 // filter->FlushStreamBuffer(pre_filter_data_len); |
| 14 // | 14 // |
| 15 // char post_filter_buf[kBufferSize]; | 15 // char post_filter_buf[kBufferSize]; |
| 16 // int post_filter_data_len = kBufferSize; | 16 // int post_filter_data_len = kBufferSize; |
| 17 // filter->ReadFilteredData(post_filter_buf, &post_filter_data_len); | 17 // filter->ReadFilteredData(post_filter_buf, &post_filter_data_len); |
| 18 // | 18 // |
| 19 // To filter a data stream, the caller first gets filter's stream_buffer_ | 19 // To filter a data stream, the caller first gets filter's stream_buffer_ |
| 20 // through its accessor and fills in stream_buffer_ with pre-filter data, next | 20 // through its accessor and fills in stream_buffer_ with pre-filter data, next |
| 21 // calls FlushStreamBuffer to notify Filter, then calls ReadFilteredData | 21 // calls FlushStreamBuffer to notify Filter, then calls ReadFilteredData |
| 22 // repeatedly to get all the filtered data. After all data have been filtered | 22 // repeatedly to get all the filtered data. After all data have been filtered |
| 23 // and read out, the caller may fill in stream_buffer_ again. This | 23 // and read out, the caller may fill in stream_buffer_ again. This |
| 24 // WriteBuffer-Flush-Read cycle is repeated until reaching the end of data | 24 // WriteBuffer-Flush-Read cycle is repeated until reaching the end of data |
| 25 // stream. | 25 // stream. |
| 26 // | 26 // |
| 27 // A return of FILTER_OK from ReadData() means that more data is | 27 // A return of FILTER_OK from ReadData() means that more data is |
| 28 // available to a future ReadData() call and data may not be written | 28 // available to a future ReadData() call and data may not be written |
| 29 // into stream_buffer(). A return of FILTER_NEED_MORE_DATA from ReadData() | 29 // into stream_buffer(). A return of FILTER_NEED_MORE_DATA from ReadData() |
| 30 // indicates that no data will be forthcoming from the filter until | 30 // indicates that no data will be forthcoming from the filter until |
| 31 // it receives more input data, and that the buffer at | 31 // it receives more input data, and that the buffer at |
| 32 // stream_buffer() may be written to. | 32 // stream_buffer() may be written to. |
| 33 // | 33 // |
| 34 // The filter being complete (no more data to provide) may be indicated | 34 // The filter being complete (no more data to provide) may be indicated |
| 35 // by either returning FILTER_DONE or by returning FILTER_OK and indicating | 35 // by either returning FILTER_DONE or by returning FILTER_OK and indicating |
| 36 // zero bytes output; consumers understand both those signals. Consumers | 36 // zero bytes output; consumers understand both those signals. Consumers |
| 37 // are responsible for not calling ReadData() on a filter after one of these | 37 // are responsible for not calling ReadData() on a filter after one of these |
| 38 // signals have been returned. Note that some filters may never signal that | 38 // signals have been returned. Note that some filters may never signal that |
| 39 // they are done (e.g. a pass-through filter will always | 39 // they are done (e.g. a pass-through filter will always |
| 40 // say FILTER_NEED_MORE_DATA), so the consumer will also need to | 40 // say FILTER_NEED_MORE_DATA), so the consumer will also need to |
| 41 // recognize the state of |no_more_input_data_available && | 41 // recognize the state of |no_more_input_data_available && |
| 42 // filter->stream_data_len() == 0| as FILTER_DONE. | 42 // filter->stream_data_len() == 0| as FILTER_DONE. |
| 43 // | 43 // |
| 44 // The lifetime of a Filter instance is completely controlled by its caller. | 44 // The lifetime of a Filter instance is completely controlled by its caller. |
| 45 | 45 |
| 46 #ifndef NET_FILTER_FILTER_H__ | 46 #ifndef NET_FILTER_FILTER_H__ |
| 47 #define NET_FILTER_FILTER_H__ | 47 #define NET_FILTER_FILTER_H__ |
| 48 | 48 |
| 49 #include <string> | 49 #include <string> |
| 50 #include <vector> | 50 #include <vector> |
| 51 | 51 |
| 52 #include "base/basictypes.h" | 52 #include "base/basictypes.h" |
| 53 #include "base/gtest_prod_util.h" | 53 #include "base/gtest_prod_util.h" |
| 54 #include "base/memory/ref_counted.h" | 54 #include "base/memory/ref_counted.h" |
| 55 #include "base/memory/scoped_ptr.h" | 55 #include "base/memory/scoped_ptr.h" |
| 56 #include "base/time/time.h" | 56 #include "base/time/time.h" |
| 57 #include "net/base/net_export.h" | 57 #include "net/base/net_export.h" |
| 58 #include "net/base/sdch_manager.h" |
| 58 | 59 |
| 59 class GURL; | 60 class GURL; |
| 60 | 61 |
| 61 namespace net { | 62 namespace net { |
| 62 | 63 |
| 63 class BoundNetLog; | 64 class BoundNetLog; |
| 64 class IOBuffer; | 65 class IOBuffer; |
| 65 class URLRequestContext; | 66 class URLRequestContext; |
| 66 | 67 |
| 67 //------------------------------------------------------------------------------ | 68 //------------------------------------------------------------------------------ |
| 68 // Define an interface class that allows access to contextual information | 69 // Define an interface class that allows access to contextual information |
| 69 // supplied by the owner of this filter. In the case where there are a chain of | 70 // supplied by the owner of this filter. In the case where there are a chain of |
| 70 // filters, there is only one owner of all the chained filters, and that context | 71 // filters, there is only one owner of all the chained filters, and that context |
| 71 // is passed to the constructor of all those filters. To be clear, the context | 72 // is passed to the constructor of all those filters. To be clear, the context |
| 72 // does NOT reflect the position in a chain, or the fact that there are prior | 73 // does NOT reflect the position in a chain, or the fact that there are prior |
| 73 // or later filters in a chain. | 74 // or later filters in a chain. |
| 75 // |
| 76 // TODO(rdsmith): FilterContext is a grab-bag of methods which may or may |
| 77 // not be relevant for any particular filter, and it's getting worse over |
| 78 // time. In addition, it only supports two filters, SDCH and gzip. |
| 79 // It would make more sense to implement FilterContext as a |
| 80 // base::SupportsUserData structure to which filter-specific information |
| 81 // could be added by whatever the ultimate consumer of the filter chain is, |
| 82 // and a particular filter (if included) could access that information. |
| 74 class NET_EXPORT_PRIVATE FilterContext { | 83 class NET_EXPORT_PRIVATE FilterContext { |
| 75 public: | 84 public: |
| 76 // Enum to control what histograms are emitted near end-of-life of this | 85 // Enum to control what histograms are emitted near end-of-life of this |
| 77 // instance. | 86 // instance. |
| 78 enum StatisticSelector { | 87 enum StatisticSelector { |
| 79 SDCH_DECODE, | 88 SDCH_DECODE, |
| 80 SDCH_PASSTHROUGH, | 89 SDCH_PASSTHROUGH, |
| 81 SDCH_EXPERIMENT_DECODE, | 90 SDCH_EXPERIMENT_DECODE, |
| 82 SDCH_EXPERIMENT_HOLDBACK, | 91 SDCH_EXPERIMENT_HOLDBACK, |
| 83 }; | 92 }; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 100 // When was this data requested from a server? | 109 // When was this data requested from a server? |
| 101 virtual base::Time GetRequestTime() const = 0; | 110 virtual base::Time GetRequestTime() const = 0; |
| 102 | 111 |
| 103 // Is data supplied from cache, or fresh across the net? | 112 // Is data supplied from cache, or fresh across the net? |
| 104 virtual bool IsCachedContent() const = 0; | 113 virtual bool IsCachedContent() const = 0; |
| 105 | 114 |
| 106 // Is this a download? | 115 // Is this a download? |
| 107 virtual bool IsDownload() const = 0; | 116 virtual bool IsDownload() const = 0; |
| 108 | 117 |
| 109 // Was this data flagged as a response to a request with an SDCH dictionary? | 118 // Was this data flagged as a response to a request with an SDCH dictionary? |
| 110 virtual bool SdchResponseExpected() const = 0; | 119 virtual SdchManager::DictionarySet* SdchDictionariesAdvertised() const = 0; |
| 111 | 120 |
| 112 // How many bytes were read from the net or cache so far (and potentially | 121 // How many bytes were read from the net or cache so far (and potentially |
| 113 // pushed into a filter for processing)? | 122 // pushed into a filter for processing)? |
| 114 virtual int64 GetByteReadCount() const = 0; | 123 virtual int64 GetByteReadCount() const = 0; |
| 115 | 124 |
| 116 // What response code was received with the associated network transaction? | 125 // What response code was received with the associated network transaction? |
| 117 // For example: 200 is ok. 4xx are error codes. etc. | 126 // For example: 200 is ok. 4xx are error codes. etc. |
| 118 virtual int GetResponseCode() const = 0; | 127 virtual int GetResponseCode() const = 0; |
| 119 | 128 |
| 120 // The URLRequestContext associated with the request. | 129 // The URLRequestContext associated with the request. |
| 121 virtual const URLRequestContext* GetURLRequestContext() const = 0; | 130 virtual const URLRequestContext* GetURLRequestContext() const = 0; |
| 122 | 131 |
| 123 // The following method forces the context to emit a specific set of | 132 // The following method forces the context to emit a specific set of |
| 124 // statistics as selected by the argument. | 133 // statistics as selected by the argument. |
| 125 virtual void RecordPacketStats(StatisticSelector statistic) const = 0; | 134 virtual void RecordPacketStats(StatisticSelector statistic) const = 0; |
| 126 | 135 |
| 127 // The BoundNetLog of the associated request. | 136 // The BoundNetLog of the associated request. |
| 128 virtual const BoundNetLog& GetNetLog() const = 0; | 137 virtual const BoundNetLog& GetNetLog() const = 0; |
| 129 }; | 138 }; |
| 130 | 139 |
| 131 //------------------------------------------------------------------------------ | 140 //------------------------------------------------------------------------------ |
| 132 class NET_EXPORT_PRIVATE Filter { | 141 class NET_EXPORT_PRIVATE Filter { |
| 133 public: | 142 public: |
| 134 // Return values of function ReadFilteredData. | 143 // Return values of function ReadFilteredData. |
| 135 enum FilterStatus { | 144 enum FilterStatus { |
| 136 // Read filtered data successfully | 145 // Read filtered data successfully |
| 137 FILTER_OK, | 146 FILTER_OK, |
| 138 // Read filtered data successfully, and the data in the buffer has been | 147 // Read filtered data successfully, and the data in the buffer has been |
| 139 // consumed by the filter, but more data is needed in order to continue | 148 // consumed by the filter, but more data is needed in order to continue |
| 140 // filtering. At this point, the caller is free to reuse the filter | 149 // filtering. At this point, the caller is free to reuse the filter |
| 141 // buffer to provide more data. | 150 // buffer to provide more data. |
| 142 FILTER_NEED_MORE_DATA, | 151 FILTER_NEED_MORE_DATA, |
| 143 // Read filtered data successfully, and filter reaches the end of the data | 152 // Read filtered data successfully, and filter reaches the end of the data |
| 144 // stream. | 153 // stream. |
| 145 FILTER_DONE, | 154 FILTER_DONE, |
| 146 // There is an error during filtering. | 155 // There is an error during filtering. |
| 147 FILTER_ERROR | 156 FILTER_ERROR |
| 148 }; | 157 }; |
| 149 | 158 |
| 150 // Specifies type of filters that can be created. | 159 // Specifies type of filters that can be created. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 162 // Creates a Filter object. | 171 // Creates a Filter object. |
| 163 // Parameters: Filter_types specifies the type of filter created; | 172 // Parameters: Filter_types specifies the type of filter created; |
| 164 // filter_context allows filters to acquire additional details needed for | 173 // filter_context allows filters to acquire additional details needed for |
| 165 // construction and operation, such as a specification of requisite input | 174 // construction and operation, such as a specification of requisite input |
| 166 // buffer size. | 175 // buffer size. |
| 167 // If success, the function returns the pointer to the Filter object created. | 176 // If success, the function returns the pointer to the Filter object created. |
| 168 // If failed or a filter is not needed, the function returns NULL. | 177 // If failed or a filter is not needed, the function returns NULL. |
| 169 // | 178 // |
| 170 // Note: filter_types is an array of filter types (content encoding types as | 179 // Note: filter_types is an array of filter types (content encoding types as |
| 171 // provided in an HTTP header), which will be chained together serially to do | 180 // provided in an HTTP header), which will be chained together serially to do |
| 172 // successive filtering of data. The types in the vector are ordered based on | 181 // successive filtering of data. The types in the vector are ordered based on |
| 173 // encoding order, and the filters are chained to operate in the reverse | 182 // encoding order, and the filters are chained to operate in the reverse |
| 174 // (decoding) order. For example, types[0] = FILTER_TYPE_SDCH, | 183 // (decoding) order. For example, types[0] = FILTER_TYPE_SDCH, |
| 175 // types[1] = FILTER_TYPE_GZIP will cause data to first be gunzip filtered, | 184 // types[1] = FILTER_TYPE_GZIP will cause data to first be gunzip filtered, |
| 176 // and the resulting output from that filter will be sdch decoded. | 185 // and the resulting output from that filter will be sdch decoded. |
| 177 static Filter* Factory(const std::vector<FilterType>& filter_types, | 186 static Filter* Factory(const std::vector<FilterType>& filter_types, |
| 178 const FilterContext& filter_context); | 187 const FilterContext& filter_context); |
| 179 | 188 |
| 180 // A simpler version of Factory() which creates a single, unchained | 189 // A simpler version of Factory() which creates a single, unchained |
| 181 // Filter of type FILTER_TYPE_GZIP, or NULL if the filter could not be | 190 // Filter of type FILTER_TYPE_GZIP, or NULL if the filter could not be |
| 182 // initialized. | 191 // initialized. |
| 183 static Filter* GZipFactory(); | 192 static Filter* GZipFactory(); |
| 184 | 193 |
| 185 // External call to obtain data from this filter chain. If ther is no | 194 // External call to obtain data from this filter chain. If ther is no |
| 186 // next_filter_, then it obtains data from this specific filter. | 195 // next_filter_, then it obtains data from this specific filter. |
| 187 FilterStatus ReadData(char* dest_buffer, int* dest_len); | 196 FilterStatus ReadData(char* dest_buffer, int* dest_len); |
| 188 | 197 |
| 189 // Returns a pointer to the stream_buffer_. | 198 // Returns a pointer to the stream_buffer_. |
| 190 IOBuffer* stream_buffer() const { return stream_buffer_.get(); } | 199 IOBuffer* stream_buffer() const { return stream_buffer_.get(); } |
| 191 | 200 |
| 192 // Returns the maximum size of stream_buffer_ in number of chars. | 201 // Returns the maximum size of stream_buffer_ in number of chars. |
| 193 int stream_buffer_size() const { return stream_buffer_size_; } | 202 int stream_buffer_size() const { return stream_buffer_size_; } |
| 194 | 203 |
| 195 // Returns the total number of chars remaining in stream_buffer_ to be | 204 // Returns the total number of chars remaining in stream_buffer_ to be |
| (...skipping 13 matching lines...) Expand all Loading... |
| 209 // The input stream_data_len is the length (in number of chars) of valid | 218 // The input stream_data_len is the length (in number of chars) of valid |
| 210 // data in stream_buffer_. It can not be greater than stream_buffer_size_. | 219 // data in stream_buffer_. It can not be greater than stream_buffer_size_. |
| 211 // The function returns true if success, and false otherwise. | 220 // The function returns true if success, and false otherwise. |
| 212 bool FlushStreamBuffer(int stream_data_len); | 221 bool FlushStreamBuffer(int stream_data_len); |
| 213 | 222 |
| 214 // Translate the text of a filter name (from Content-Encoding header) into a | 223 // Translate the text of a filter name (from Content-Encoding header) into a |
| 215 // FilterType. | 224 // FilterType. |
| 216 static FilterType ConvertEncodingToType(const std::string& filter_type); | 225 static FilterType ConvertEncodingToType(const std::string& filter_type); |
| 217 | 226 |
| 218 // Given a array of encoding_types, try to do some error recovery adjustment | 227 // Given a array of encoding_types, try to do some error recovery adjustment |
| 219 // to the list. This includes handling known bugs in the Apache server (where | 228 // to the list. This includes handling known bugs in the Apache server (where |
| 220 // redundant gzip encoding is specified), as well as issues regarding SDCH | 229 // redundant gzip encoding is specified), as well as issues regarding SDCH |
| 221 // encoding, where various proxies and anti-virus products modify or strip the | 230 // encoding, where various proxies and anti-virus products modify or strip the |
| 222 // encodings. These fixups require context, which includes whether this | 231 // encodings. These fixups require context, which includes whether this |
| 223 // response was made to an SDCH request (i.e., an available dictionary was | 232 // response was made to an SDCH request (i.e., an available dictionary was |
| 224 // advertised in the GET), as well as the mime type of the content. | 233 // advertised in the GET), as well as the mime type of the content. |
| 225 static void FixupEncodingTypes(const FilterContext& filter_context, | 234 static void FixupEncodingTypes(const FilterContext& filter_context, |
| 226 std::vector<FilterType>* encoding_types); | 235 std::vector<FilterType>* encoding_types); |
| 227 | 236 |
| 228 // Returns a string describing the FilterTypes implemented by this filter. | 237 // Returns a string describing the FilterTypes implemented by this filter. |
| 229 std::string OrderedFilterList() const; | 238 std::string OrderedFilterList() const; |
| 230 | 239 |
| 231 protected: | 240 protected: |
| 232 friend class GZipUnitTest; | 241 friend class GZipUnitTest; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 263 char* next_stream_data_; | 272 char* next_stream_data_; |
| 264 | 273 |
| 265 // Total number of remaining chars in stream_buffer_ to be filtered. | 274 // Total number of remaining chars in stream_buffer_ to be filtered. |
| 266 int stream_data_len_; | 275 int stream_data_len_; |
| 267 | 276 |
| 268 private: | 277 private: |
| 269 // Allocates and initializes stream_buffer_ and stream_buffer_size_. | 278 // Allocates and initializes stream_buffer_ and stream_buffer_size_. |
| 270 void InitBuffer(int size); | 279 void InitBuffer(int size); |
| 271 | 280 |
| 272 // A factory helper for creating filters for within a chain of potentially | 281 // A factory helper for creating filters for within a chain of potentially |
| 273 // multiple encodings. If a chain of filters is created, then this may be | 282 // multiple encodings. If a chain of filters is created, then this may be |
| 274 // called multiple times during the filter creation process. In most simple | 283 // called multiple times during the filter creation process. In most simple |
| 275 // cases, this is only called once. Returns NULL and cleans up (deleting | 284 // cases, this is only called once. Returns NULL and cleans up (deleting |
| 276 // filter_list) if a new filter can't be constructed. | 285 // filter_list) if a new filter can't be constructed. |
| 277 static Filter* PrependNewFilter(FilterType type_id, | 286 static Filter* PrependNewFilter(FilterType type_id, |
| 278 const FilterContext& filter_context, | 287 const FilterContext& filter_context, |
| 279 int buffer_size, | 288 int buffer_size, |
| 280 Filter* filter_list); | 289 Filter* filter_list); |
| 281 | 290 |
| 282 // Helper methods for PrependNewFilter. If initialization is successful, | 291 // Helper methods for PrependNewFilter. If initialization is successful, |
| 283 // they return a fully initialized Filter. Otherwise, return NULL. | 292 // they return a fully initialized Filter. Otherwise, return NULL. |
| 284 static Filter* InitGZipFilter(FilterType type_id, int buffer_size); | 293 static Filter* InitGZipFilter(FilterType type_id, int buffer_size); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 304 | 313 |
| 305 // The filter type this filter was constructed from. | 314 // The filter type this filter was constructed from. |
| 306 FilterType type_id_; | 315 FilterType type_id_; |
| 307 | 316 |
| 308 DISALLOW_COPY_AND_ASSIGN(Filter); | 317 DISALLOW_COPY_AND_ASSIGN(Filter); |
| 309 }; | 318 }; |
| 310 | 319 |
| 311 } // namespace net | 320 } // namespace net |
| 312 | 321 |
| 313 #endif // NET_FILTER_FILTER_H__ | 322 #endif // NET_FILTER_FILTER_H__ |
| OLD | NEW |