• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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_BASE_SDCH_FILTER_H_
15 #define NET_BASE_SDCH_FILTER_H_
16 
17 #include <string>
18 
19 #include "base/scoped_ptr.h"
20 #include "net/base/filter.h"
21 #include "net/base/sdch_manager.h"
22 
23 class SafeOutputStringInterface;
24 
25 namespace open_vcdiff {
26   class VCDiffStreamingDecoder;
27 }
28 
29 class SdchFilter : public Filter {
30  public:
31   explicit SdchFilter(const FilterContext& filter_context);
32 
33   virtual ~SdchFilter();
34 
35   // Initializes filter decoding mode and internal control blocks.
36   bool InitDecoding(Filter::FilterType filter_type);
37 
38   // Decode the pre-filter data and writes the output into |dest_buffer|
39   // The function returns FilterStatus. See filter.h for its description.
40   //
41   // Upon entry, *dest_len is the total size (in number of chars) of the
42   // destination buffer. Upon exit, *dest_len is the actual number of chars
43   // written into the destination buffer.
44   virtual FilterStatus ReadFilteredData(char* dest_buffer, int* dest_len);
45 
46  private:
47   // Internal status.  Once we enter an error state, we stop processing data.
48   enum DecodingStatus {
49     DECODING_UNINITIALIZED,
50     WAITING_FOR_DICTIONARY_SELECTION,
51     DECODING_IN_PROGRESS,
52     DECODING_ERROR,
53     META_REFRESH_RECOVERY,  // Decoding error being handled by a meta-refresh.
54     PASS_THROUGH,  // Non-sdch content being passed without alteration.
55   };
56 
57   // Identify the suggested dictionary, and initialize underlying decompressor.
58   Filter::FilterStatus InitializeDictionary();
59 
60   // Move data that was internally buffered (after decompression) to the
61   // specified dest_buffer.
62   int OutputBufferExcess(char* const dest_buffer, size_t available_space);
63 
64   // Tracks the status of decoding.
65   // This variable is initialized by InitDecoding and updated only by
66   // ReadFilteredData.
67   DecodingStatus decoding_status_;
68 
69   // The underlying decoder that processes data.
70   // This data structure is initialized by InitDecoding and updated in
71   // ReadFilteredData.
72   scoped_ptr<open_vcdiff::VCDiffStreamingDecoder> vcdiff_streaming_decoder_;
73 
74   // In case we need to assemble the hash piecemeal, we have a place to store
75   // a part of the hash until we "get all 8 bytes plus a null."
76   std::string dictionary_hash_;
77 
78   // After assembling an entire dictionary hash (the first 9 bytes of the
79   // sdch payload, we check to see if it is plausible, meaning it has a null
80   // termination, and has 8 characters that are possible in a net-safe base64
81   // encoding.  If the hash is not plausible, then the payload is probably not
82   // an SDCH encoded bundle, and various error recovery strategies can be
83   // attempted.
84   bool dictionary_hash_is_plausible_;
85 
86   // We hold an in-memory copy of the dictionary during the entire decoding, as
87   // it is used directly by the VC-DIFF decoding system.
88   // That char* data is part of the dictionary_ we hold a reference to.
89   scoped_refptr<SdchManager::Dictionary> dictionary_;
90 
91   // The decoder may demand a larger output buffer than the target of
92   // ReadFilteredData so we buffer the excess output between calls.
93   std::string dest_buffer_excess_;
94   // To avoid moving strings around too much, we save the index into
95   // dest_buffer_excess_ that has the next byte to output.
96   size_t dest_buffer_excess_index_;
97 
98   // To get stats on activities, we keep track of source and target bytes.
99   // Visit about:histograms/Sdch to see histogram data.
100   size_t source_bytes_;
101   size_t output_bytes_;
102 
103   // Error recovery in content type may add an sdch filter type, in which case
104   // we should gracefully perform pass through if the format is incorrect, or
105   // an applicable dictionary can't be found.
106   bool possible_pass_through_;
107 
108   // The URL that is currently being filtered.
109   // This is used to restrict use of a dictionary to a specific URL or path.
110   GURL url_;
111 
112   // To facilitate error recovery, allow filter to know if content is text/html
113   // by checking within this mime type (we may do a meta-refresh via html).
114   std::string mime_type_;
115 
116   DISALLOW_COPY_AND_ASSIGN(SdchFilter);
117 };
118 
119 #endif  // NET_BASE_SDCH_FILTER_H_
120