OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // SdchFilter applies open_vcdiff content decoding to a datastream. | |
6 // This decoding uses a pre-cached dictionary of text fragments to decode | |
7 // (expand) the stream back to its original contents. | |
8 // | |
9 // This SdchFilter internally uses open_vcdiff/vcdec library to do decoding. | |
10 // | |
11 // SdchFilter is also a subclass of Filter. See the latter's header file | |
12 // filter.h for sample usage. | |
13 | |
14 #ifndef NET_FILTER_SDCH_FILTER_H_ | |
15 #define NET_FILTER_SDCH_FILTER_H_ | |
16 | |
17 #include <stddef.h> | |
18 | |
19 #include <memory> | |
20 #include <string> | |
21 | |
22 #include "base/macros.h" | |
23 #include "net/base/net_export.h" | |
24 #include "net/base/sdch_dictionary.h" | |
25 #include "net/base/sdch_manager.h" | |
26 #include "net/filter/filter.h" | |
27 | |
28 namespace open_vcdiff { | |
29 class VCDiffStreamingDecoder; | |
30 } | |
31 | |
32 namespace net { | |
33 | |
34 class NET_EXPORT_PRIVATE SdchFilter : public Filter { | |
35 public: | |
36 ~SdchFilter() override; | |
37 | |
38 // Initializes filter decoding mode and internal control blocks. | |
39 bool InitDecoding(Filter::FilterType filter_type); | |
40 | |
41 // Decode the pre-filter data and writes the output into |dest_buffer| | |
42 // The function returns FilterStatus. See filter.h for its description. | |
43 // | |
44 // Upon entry, *dest_len is the total size (in number of chars) of the | |
45 // destination buffer. Upon exit, *dest_len is the actual number of chars | |
46 // written into the destination buffer. | |
47 FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len) override; | |
48 | |
49 private: | |
50 // Internal status. Once we enter an error state, we stop processing data. | |
51 enum DecodingStatus { | |
52 DECODING_UNINITIALIZED, | |
53 WAITING_FOR_DICTIONARY_SELECTION, | |
54 DECODING_IN_PROGRESS, | |
55 DECODING_ERROR, | |
56 META_REFRESH_RECOVERY, // Decoding error being handled by a meta-refresh. | |
57 PASS_THROUGH, // Non-sdch content being passed without alteration. | |
58 }; | |
59 | |
60 // Only to be instantiated by Filter::Factory. | |
61 SdchFilter(FilterType type, const FilterContext& filter_context); | |
62 friend class Filter; | |
63 | |
64 // Identify the suggested dictionary, and initialize underlying decompressor. | |
65 Filter::FilterStatus InitializeDictionary(); | |
66 | |
67 // Move data that was internally buffered (after decompression) to the | |
68 // specified dest_buffer. | |
69 int OutputBufferExcess(char* const dest_buffer, size_t available_space); | |
70 | |
71 // Add SDCH Problem to net-log and record histogram. | |
72 void LogSdchProblem(SdchProblemCode problem); | |
73 | |
74 // Context data from the owner of this filter. | |
75 const FilterContext& filter_context_; | |
76 | |
77 // Tracks the status of decoding. | |
78 // This variable is initialized by InitDecoding and updated only by | |
79 // ReadFilteredData. | |
80 DecodingStatus decoding_status_; | |
81 | |
82 // The underlying decoder that processes data. | |
83 // This data structure is initialized by InitDecoding and updated in | |
84 // ReadFilteredData. | |
85 std::unique_ptr<open_vcdiff::VCDiffStreamingDecoder> | |
86 vcdiff_streaming_decoder_; | |
87 | |
88 // After the encoded response SDCH header is read, this variable contains | |
89 // the server hash with trailing null byte. | |
90 std::string dictionary_hash_; | |
91 | |
92 // After assembling an entire dictionary hash (the first 9 bytes of the | |
93 // sdch payload, we check to see if it is plausible, meaning it has a null | |
94 // termination, and has 8 characters that are possible in a net-safe base64 | |
95 // encoding. If the hash is not plausible, then the payload is probably not | |
96 // an SDCH encoded bundle, and various error recovery strategies can be | |
97 // attempted. | |
98 bool dictionary_hash_is_plausible_; | |
99 | |
100 // We keep a copy of the URLRequestContext for use in the destructor, (at | |
101 // which point GetURLRequestContext() will likely return null because of | |
102 // the disassociation of the URLRequest from the URLRequestJob). This is | |
103 // safe because the URLRequestJob (and any filters) are guaranteed to be | |
104 // deleted before the URLRequestContext is destroyed. | |
105 const URLRequestContext* const url_request_context_; | |
106 | |
107 // The decoder may demand a larger output buffer than the target of | |
108 // ReadFilteredData so we buffer the excess output between calls. | |
109 std::string dest_buffer_excess_; | |
110 // To avoid moving strings around too much, we save the index into | |
111 // dest_buffer_excess_ that has the next byte to output. | |
112 size_t dest_buffer_excess_index_; | |
113 | |
114 // To get stats on activities, we keep track of source and target bytes. | |
115 // Visit about:histograms/Sdch to see histogram data. | |
116 size_t source_bytes_; | |
117 size_t output_bytes_; | |
118 | |
119 // Error recovery in content type may add an sdch filter type, in which case | |
120 // we should gracefully perform pass through if the format is incorrect, or | |
121 // an applicable dictionary can't be found. | |
122 bool possible_pass_through_; | |
123 | |
124 // The URL that is currently being filtered. | |
125 // This is used to restrict use of a dictionary to a specific URL or path. | |
126 GURL url_; | |
127 | |
128 // To facilitate error recovery, allow filter to know if content is text/html | |
129 // by checking within this mime type (we may do a meta-refresh via html). | |
130 std::string mime_type_; | |
131 | |
132 // If the response was encoded with a dictionary different than those | |
133 // advertised (e.g. a cached response using an old dictionary), this | |
134 // variable preserves that dictionary from deletion during decoding. | |
135 std::unique_ptr<SdchManager::DictionarySet> unexpected_dictionary_handle_; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(SdchFilter); | |
138 }; | |
139 | |
140 } // namespace net | |
141 | |
142 #endif // NET_FILTER_SDCH_FILTER_H_ | |
OLD | NEW |