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