Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1033)

Side by Side Diff: net/filter/sdch_source_stream.h

Issue 2368433002: Add net::SdchSourceStream and net::SdchPolicyDelegate (Closed)
Patch Set: Address Randy's comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "net/filter/source_stream.h"
16
17 namespace open_vcdiff {
18 class VCDiffStreamingDecoder;
19 } // namespace open_vcdiff
20
21 namespace net {
22
23 class IOBuffer;
24
25 // SdchSourceStream applies open_vcdiff content decoding to a datastream.
26 // This decoding uses a pre-cached dictionary of text fragments to decode
27 // (expand) the stream back to its original contents.
28 //
29 // This SdchSourceStream internally uses open_vcdiff/vcdec library to do
30 // decoding.
31 class NET_EXPORT_PRIVATE SdchSourceStream : public FilterSourceStream {
32 public:
33 enum InputState {
34 STATE_LOAD_DICTIONARY,
35 STATE_DECODE,
36 STATE_OUTPUT_REPLACE,
37 STATE_PASS_THROUGH,
38 };
39
40 // The Delegate interface is responsible for error recovery and stats
41 // gathering. See the methods below for descriptions of which errors the
42 // delegate is expected to handle and what it can do to repair them.
43 class NET_EXPORT_PRIVATE Delegate {
44 public:
45 enum ErrorRecovery {
46 // Do not recover the error.
47 NONE,
48 // Pass remaining input unchanged to downstream.
49 PASS_THROUGH,
50 // Pass an alternative output to downstream.
51 REPLACE_OUTPUT,
52 };
53 virtual ~Delegate(){};
54
55 // Called by the SdchSourceStream if an error occurs while parsing the
56 // server-sent dictionary ID, or if the specified dictionary can't be loaded
57 // (i.e., GetDictionary returned false). This method is expected to handle
58 // the error condition by returning a ErrorRecovery enum. If REPLACE_OUTPUT
59 // is returned, it will also write the output to be replaced with to
60 // |replace_output|.
61 virtual ErrorRecovery OnDictionaryIdError(std::string* replace_output) = 0;
62
63 // Called by the SdchSourceStream if the specified dictionary can't be
64 // loaded (i.e., GetDictionary returned false). This method is expected to
65 // handle the error condition by returning a ErrorRecovery enum.
66 // If REPLACE_OUTPUT is returned, it will also write the output to be
67 // replaced with to |replace_output|.
68 virtual ErrorRecovery OnGetDictionaryError(std::string* replace_output) = 0;
69
70 // Called by the SdchSourceStream if an error occurs while decoding the
71 // vcdiff-compressed data stream. This method is expected to
72 // handle the error condition by returning a ErrorRecovery enum.
73 // If REPLACE_OUTPUT is returned, it will also write the output to be
74 // replaced with to |replace_output|.
75 virtual ErrorRecovery OnDecodingError(std::string* replace_output) = 0;
76
77 // Called by the SdchSourceStream to request the text of the specified
78 // dictionary. This method must either:
79 // * Fill in |*text| and return true, or
80 // * Leave |*text| untouched and return false.
81 // The delegate is required to make sure that the pointer written into
82 // |*text| remains valid for the lifetime of the delegate.
83 // The return value is true if the named dictionary could be found and false
84 // otherwise.
85 //
86 // The |server_id| string is guaranteed to be a syntactically valid SDCH
87 // server-id.
88 // TODO(xunjieli): If an async interface is required. Change |text| to use
89 // an IOBuffer buffer and add a callback.
90 virtual bool OnGetDictionary(const std::string& server_id,
91 const std::string** text) = 0;
92 virtual void OnStreamDestroyed(
93 InputState input_state,
94 const std::string& buffered_output,
95 open_vcdiff::VCDiffStreamingDecoder* decoder) = 0;
96 };
97
98 SdchSourceStream(std::unique_ptr<SourceStream> previous,
99 std::unique_ptr<Delegate> delegate,
100 SourceStream::SourceType type);
101 ~SdchSourceStream() override;
102
103 private:
104 // SourceStream implementation:
105 std::string GetTypeAsString() const override;
106 int FilterData(IOBuffer* output_buffer,
107 int output_buffer_size,
108 IOBuffer* input_buffer,
109 int input_buffer_size,
110 int* consumed_bytes,
111 bool upstream_end_reached) override;
112
113 // Returns whether |id| looks like a dictionary ID, meaning 8 characters of
114 // base64url followed by a null character.
115 bool CouldBeDictionaryId(const std::string& id) const;
116
117 // Helper method to handle error returned by Delegate. It sets |input_state_|
118 // and returns true if the error can be handles, and false if the error is
119 // not recoverable.
120 bool HandleError(Delegate::ErrorRecovery error_recover);
121
122 std::unique_ptr<open_vcdiff::VCDiffStreamingDecoder> decoder_;
123 std::unique_ptr<Delegate> delegate_;
124
125 // After the encoded response SDCH header is read, this variable contains
126 // the server hash with trailing null byte.
127 std::string dictionary_server_id_;
128
129 // Since vcdiff may generate quite a bit of output at once, SdchSourceStream
130 // has to buffer excess output (more than requested by the caller) here to
131 // return later. This could become quite large. crbug.com/651577.
132 std::string buffered_output_;
133
134 // State of the input stream.
135 InputState input_state_;
136
137 DISALLOW_COPY_AND_ASSIGN(SdchSourceStream);
138 };
139
140 } // namespace net
141
142 #endif // NET_FILTER_SDCH_SOURCE_STREAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698