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

Side by Side Diff: net/filter/sdch_stream_source.cc

Issue 1662763002: [ON HOLD] Implement pull-based design for content decoding (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactor common logic Created 4 years, 9 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 #include "base/auto_reset.h"
6 #include "base/bind.h"
7 #include "net/filter/block_buffer.h"
8 #include "net/filter/sdch_stream_source.h"
9 #include "sdch/open-vcdiff/src/google/vcdecoder.h"
10
11 namespace net {
12
13 namespace {
14
15 const size_t kServerIdLength = 9;
16
17 } // namespace
18
19 SdchStreamSource::SdchStreamSource(scoped_ptr<StreamSource> previous,
20 SdchStreamSourceDelegate* delegate)
21 : StreamSource(StreamSource::SOURCE_SDCH, std::move(previous)),
22 decoder_(new open_vcdiff::VCDiffStreamingDecoder),
23 delegate_(delegate),
24 output_replaced_(false),
25 passthrough_(false),
26 dictionary_tried_load_(false) {}
27
28 SdchStreamSource::~SdchStreamSource() {
29 // In unit tests, delegate_ may be null.
30 if (delegate_)
31 delegate_->NotifyStreamDone();
32 }
33
34 bool SdchStreamSource::Init() {
35 return true;
36 }
37
38 void SdchStreamSource::StopDecoding() {
39 DCHECK(in_delegate_handler_);
40 passthrough_ = true;
41 }
42
43 void SdchStreamSource::ReplaceOutput(const char* data, size_t size) {
44 DCHECK(in_delegate_handler_);
45 buffered_output_.assign(data, size);
46 }
47
48 Error SdchStreamSource::ReadInternal(IOBuffer* dest_buffer,
49 size_t buffer_size,
50 size_t* bytes_read) {
51 // If there is no loaded dictionary, try loading a dictionary ID from
52 // |buffer_| and then loading a dictionary. If anything goes wrong in
53 // LoadDictionary, the delegate's HandleDictionaryError() call is expected
54 // to recover, either by calling StopDecoding() or ReplaceOutput(). Both of
55 // these are handled by Decompress (as is having no dictionary loaded). If
56 // the delegate's HandleDictionaryError cannot recover, this function
57 // returns a read error.
58 if (!dictionary_tried_load_ && !LoadDictionary())
59 return ERR_FAILED;
60
61 // Run the synchronous decompressor.
62 SdchStreamState state = Decompress(dest_buffer, buffer_size, bytes_read);
63
64 // If decompression failed, call HandleDecodingError on the delegate, then
65 // rerun the decompressor. The decompressor shouldn't actually decompress
66 // this time, since the delegate should have called either Passthrough or
67 // ReplaceOutput.
68 if (state == SDCH_STREAM_ERROR) {
69 bool handled = AskDelegateToHandleDecodingError();
70 if (!handled)
71 return ERR_FAILED;
72 state = Decompress(dest_buffer, buffer_size, bytes_read);
73 }
74
75 if (*bytes_read > 0) {
76 return OK;
77 }
78
79 DCHECK(state == SDCH_STREAM_MORE_INPUT);
80 DCHECK(!buffer_->HasMoreBytes());
81
82 // If someone called ReplaceOutput to replace this stream's output,
83 // Decompress will return all the buffered output; if it did not do so, this
84 // stream is at EOF.
85 if (output_replaced_)
86 return OK;
87
88 return OK;
89 }
90
91 // Synchronous decompressor core.
92 SdchStreamSource::SdchStreamState SdchStreamSource::Decompress(
93 IOBuffer* dest_buffer,
94 size_t buffer_size,
95 size_t* bytes_read) {
96 // If output has been replaced via ReplaceOutput, short-circuit any other
97 // processing, and emit as much of the buffered output as possible. If there's
98 // no buffered output left, return EOF.
99 if (output_replaced_)
100 return Flush(dest_buffer, buffer_size, bytes_read);
101
102 // If this stream is no longer decoding, pass any input through.
103 if (passthrough_)
104 return Passthrough(dest_buffer, buffer_size, bytes_read);
105
106 // Can't do any decoding until a dictionary is loaded, which requires more
107 // input.
108 if (!dictionary_tried_load_)
109 return SDCH_STREAM_MORE_INPUT;
110
111 // If there's any data left in the output buffer, try flushing some of it.
112 if (Flush(dest_buffer, buffer_size, bytes_read) != SDCH_STREAM_MORE_INPUT)
113 return SDCH_STREAM_MORE_OUTPUT_SPACE;
114
115 // This is awkward. In principle, this function is called only when the input
116 // buffer has more bytes, since vcdiff always consumes input; unfortunately,
117 // the dictionary-loading code earlier in the Read() loop can consume all the
118 // currently buffered input. If that happened, Decompress returns early here.
119 if (!buffer_->HasMoreBytes())
120 return SDCH_STREAM_MORE_INPUT;
121
122 // Now there is no data left in the output buffer and there is data in the
123 // input buffer, so run the vcdiff decoder.
124 DCHECK(buffered_output_.empty());
125 DCHECK(buffer_->HasMoreBytes());
126
127 // Calls to DecodeChunk always consume all their input, so this always drains
128 // the entire buffer.
129 bool ok = decoder_->DecodeChunk(buffer_->bytes(), buffer_->bytes_left(),
130 &buffered_output_);
131 if (ok) {
132 buffer_->WasDrained(buffer_->bytes_left());
133 } else {
134 if (!AskDelegateToHandleDecodingError())
135 return SDCH_STREAM_ERROR;
136 }
137
138 return Flush(dest_buffer, buffer_size, bytes_read);
139 }
140
141 SdchStreamSource::SdchStreamState SdchStreamSource::Passthrough(
142 IOBuffer* dest_buffer,
143 size_t buffer_size,
144 size_t* bytes_read) {
145 SdchStreamState state = Flush(dest_buffer, buffer_size, bytes_read);
146 if (state == SDCH_STREAM_MORE_OUTPUT_SPACE)
147 return state;
148
149 // No buffered data to flush. See if there's any input.
150 DCHECK(buffered_output_.empty());
151 if (!buffer_->HasMoreBytes())
152 return SDCH_STREAM_MORE_INPUT;
153
154 size_t to_copy = buffer_size;
155 if (to_copy > buffer_->bytes_left())
156 to_copy = buffer_->bytes_left();
157 memcpy(dest_buffer->data(), buffer_->bytes(), to_copy);
158 buffer_->WasDrained(to_copy);
159 if (buffer_->HasMoreBytes())
160 return SDCH_STREAM_MORE_OUTPUT_SPACE;
161 else
162 return SDCH_STREAM_MORE_INPUT;
163 }
164
165 SdchStreamSource::SdchStreamState SdchStreamSource::Flush(IOBuffer* dest_buffer,
166 size_t buffer_size,
167 size_t* bytes_read) {
168 size_t to_copy = buffer_size;
169 if (to_copy > buffered_output_.length())
170 to_copy = buffered_output_.length();
171 if (to_copy == 0)
172 return SDCH_STREAM_MORE_INPUT;
173 memcpy(dest_buffer->data(), buffered_output_.data(), to_copy);
174 buffered_output_.erase(0, to_copy);
175 *bytes_read = to_copy;
176 return SDCH_STREAM_MORE_OUTPUT_SPACE;
177 }
178
179 // Attempts to find a dictionary ID at the start of the input stream and load
180 // the dictionary with that identifier. Returns true if no error happened or an
181 // error happened but the delegate repaired it, and false if an error happened
182 // and the delegate did not repair it.
183 bool SdchStreamSource::LoadDictionary() {
184 size_t to_copy = kServerIdLength - dictionary_id_.length();
185 const std::string* dictionary_text = nullptr;
186 if (to_copy > buffer_->bytes_left())
187 to_copy = buffer_->bytes_left();
188 dictionary_id_.append(buffer_->bytes(), to_copy);
189 buffer_->WasDrained(to_copy);
190
191 // Not enough bytes for a dictionary ID accumulated yet.
192 if (dictionary_id_.length() != kServerIdLength)
193 return true;
194
195 dictionary_tried_load_ = true;
196
197 // If the dictionary ID looks bogus, or the delegate can't find a dictionary
198 // with that identifier, then call HandleDictionaryError and bail.
199 // Also, stick the dictionary ID into the output buffer here; LoadDictionary
200 // will consume it assuming it's a dictionary ID, so if it's really not, the
201 // "dictionary ID" should appear in the output stream.
202 //
203 // To avoid passing a std::string with a null terminator into GetDictionary(),
204 // |stem_id| here removes the last byte blindly, and this method only calls
205 // GetDictionary with it if CouldBeDictionaryId returns true.
206 std::string stem_id = dictionary_id_.substr(0, kServerIdLength - 1);
207 if (!CouldBeDictionaryId(dictionary_id_) ||
208 !delegate_->GetDictionary(stem_id, &dictionary_text)) {
209 buffered_output_.append(dictionary_id_);
210 return AskDelegateToHandleDictionaryError();
211 }
212
213 decoder_->StartDecoding(dictionary_text->data(), dictionary_text->length());
214 return true;
215 }
216
217 bool SdchStreamSource::CouldBeDictionaryId(const std::string& id) const {
218 std::string kBase64UrlChars =
219 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
220 for (size_t i = 0; i < kServerIdLength - 1; i++) {
221 if (kBase64UrlChars.find(id[i]) == std::string::npos)
222 return false;
223 }
224 if (id[kServerIdLength - 1] != '\0')
225 return false;
226 return true;
227 }
228
229 bool SdchStreamSource::AskDelegateToHandleDictionaryError() {
230 base::AutoReset<bool> resetter(&in_delegate_handler_, true);
231 return delegate_->HandleDictionaryError(this);
232 }
233
234 bool SdchStreamSource::AskDelegateToHandleDecodingError() {
235 base::AutoReset<bool> resetter(&in_delegate_handler_, true);
236 return delegate_->HandleDecodingError(this);
237 }
238
239 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698