OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 #ifndef NET_FILTER_SDCH_SOURCE_STREAM_H_ | |
6 #define NET_FILTER_SDCH_SOURCE_STREAM_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/macros.h" | |
11 #include "net/base/net_export.h" | |
12 #include "net/base/sdch_dictionary.h" | |
13 #include "net/base/sdch_manager.h" | |
14 #include "net/filter/filter_source_stream.h" | |
15 | |
16 namespace open_vcdiff { | |
17 | |
18 class VCDiffStreamingDecoder; | |
19 | |
20 } // namespace open_vcdiff | |
21 | |
22 namespace net { | |
23 | |
24 class IOBuffer; | |
25 | |
26 // SdchSourceStream applies open_vcdiff content decoding to a datastream. | |
27 // This decoding uses a pre-cached dictionary of text fragments to decode | |
28 // (expand) the stream back to its original contents. | |
29 // | |
30 // This SdchSourceStream internally uses open_vcdiff/vcdec library to do | |
31 // decoding. | |
32 class NET_EXPORT_PRIVATE SdchSourceStream : public FilterSourceStream { | |
33 public: | |
34 // The Delegate interface is responsible for error recovery and stats | |
35 // gathering. See the methods below for descriptions of which errors the | |
36 // delegate is expected to handle and what it can do to repair them. | |
37 class NET_EXPORT_PRIVATE Delegate { | |
38 public: | |
39 virtual ~Delegate(){}; | |
40 | |
41 // Called by the SdchSourceStream if an error occurs while parsing the | |
42 // server-sent dictionary ID, or if the specified dictionary can't be loaded | |
43 // (i.e., GetDictionary returned false). This method is expected to correct | |
44 // the error condition by either: | |
45 // a) returning true and calling ReplaceOutput or Passthrough, or | |
Randy Smith (Not in Mondays)
2016/09/27 20:09:13
nit: I think that RepalceOutput() or Passthrough()
xunjieli
2016/09/30 15:32:18
Done.
| |
46 // b) returning false, in which case the SourceStream will return an error | |
47 // to its caller | |
48 // Conceptually, the return value is true if the error has been | |
49 // handled/recovered from and false if not. | |
50 // This method must not retain a reference to |source| after returning. | |
51 virtual bool OnDictionaryError(SdchSourceStream* source) = 0; | |
Randy Smith (Not in Mondays)
2016/09/27 20:09:13
Suggestion: So I flip-flopped a bit on this in thi
xunjieli
2016/09/30 15:32:18
Done.
| |
52 | |
53 // Called by the SdchSourceStream if an error occurs while decoding the | |
54 // vcdiff-compressed data stream. Semantics are similar to | |
55 // OnDictionaryError above. This method must not retain a reference to | |
56 // |source| after returning. | |
57 virtual bool OnDecodingError(SdchSourceStream* source) = 0; | |
58 | |
59 // Called by the SdchSourceStream to request the text of the specified | |
60 // dictionary. This method must either: | |
61 // * Fill in |*text| and return true, or | |
62 // * Leave |*text| untouched and return false. | |
63 // The return value is true if the named dictionary could be found and false | |
64 // otherwise. | |
65 // | |
66 // The |server_id| string is guaranteed to be a syntactically valid SDCH | |
67 // server-id. | |
68 virtual bool OnGetDictionary(const std::string& server_id, | |
69 const std::string** text) = 0; | |
Randy Smith (Not in Mondays)
2016/09/27 20:09:13
Would you do me a favor and put O(5m) of thought i
xunjieli
2016/09/30 15:32:18
Done.
| |
70 }; | |
71 | |
72 SdchSourceStream(std::unique_ptr<SourceStream> previous, Delegate* delegate); | |
73 ~SdchSourceStream() override; | |
74 | |
75 // For use by SdchSourceStreamDelegate. Switches this stream source into | |
76 // "passthrough" mode, where it will not decode any further output with | |
77 // vcdiff. This method can only be called during an invocation of the | |
78 // delegate's OnDictionaryError or OnDecodingError functions. | |
79 virtual void StopDecoding(); | |
80 | |
81 // For use by SdchSourceStreamDelegate. Replaces the entire output of this | |
82 // stream with the supplied |data|, which is of length |size|. Note that | |
83 // calling this method also stops vcdiff decoding as a side-effect - |data| | |
84 // should not point to vcdiff-encoded data. This method can only be called | |
85 // during an invocation of the delegate's OnDictionaryError or | |
86 // OnDecodingError functions. | |
87 virtual void ReplaceOutput(const char* data, size_t size); | |
Randy Smith (Not in Mondays)
2016/09/27 20:09:13
nit, suggestion: If the above two are only called
xunjieli
2016/09/30 15:32:18
Acknowledged. These two methods are removed.
| |
88 | |
89 private: | |
90 enum InputState { | |
91 STATE_LOAD_DICTIONARY, | |
92 STATE_FLUSH_INTERNAL_BUFFER, | |
93 STATE_PASS_THROUGH, | |
94 STATE_HANDLE_ERROR, | |
95 STATE_DECODE, | |
96 }; | |
97 | |
98 // SourceStream implementation: | |
99 std::string GetTypeAsString() const override; | |
100 int FilterData(IOBuffer* output_buffer, | |
101 int output_buffer_size, | |
102 IOBuffer* input_buffer, | |
103 int input_buffer_size, | |
104 int* consumed_bytes, | |
105 bool upstream_end_reached) override; | |
106 | |
107 bool LoadDictionary(DrainableIOBuffer* input_buffer); | |
108 | |
109 // Returns whether |id| looks like a dictionary ID, meaning 8 characters of | |
110 // base64url followed by a null character. | |
111 bool CouldBeDictionaryId(const std::string& id) const; | |
112 | |
113 bool AskDelegateToHandleDictionaryError(); | |
114 bool AskDelegateToHandleDecodingError(); | |
115 | |
116 std::unique_ptr<open_vcdiff::VCDiffStreamingDecoder> decoder_; | |
117 Delegate* delegate_; | |
118 | |
119 // After the encoded response SDCH header is read, this variable contains | |
120 // the server hash with trailing null byte. | |
121 std::string dictionary_id_; | |
Randy Smith (Not in Mondays)
2016/09/27 20:09:13
nit, suggestion: I've had enough trouble with serv
xunjieli
2016/09/30 15:32:18
Done.
| |
122 | |
123 // Since vcdiff may generate quite a bit of output at once, SdchSourceStream | |
124 // has to buffer excess output (more than requested by the caller) here to | |
125 // return later. This could maybe become quite large. | |
Randy Smith (Not in Mondays)
2016/09/27 20:09:13
nit: "could maybe" seems redundant to me; eliminat
Randy Smith (Not in Mondays)
2016/09/27 20:09:13
I find myself wanting to file a bug on upstream as
xunjieli
2016/09/30 15:32:18
Done.
xunjieli
2016/09/30 15:32:18
Done.
| |
126 std::string buffered_output_; | |
127 | |
128 bool output_replaced_; | |
129 bool passthrough_; | |
130 | |
131 bool in_delegate_handler_; | |
132 | |
133 InputState input_state_; | |
134 | |
135 DISALLOW_COPY_AND_ASSIGN(SdchSourceStream); | |
136 }; | |
137 | |
138 } // namespace net | |
139 | |
140 #endif // NET_FILTER_SDCH_SOURCE_STREAM_H_ | |
OLD | NEW |