Index: net/filter/sdch_source_stream.h |
diff --git a/net/filter/sdch_source_stream.h b/net/filter/sdch_source_stream.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..08fa2f483af9c64756c351e98387d136d660ded7 |
--- /dev/null |
+++ b/net/filter/sdch_source_stream.h |
@@ -0,0 +1,144 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef NET_FILTER_SDCH_SOURCE_STREAM_H_ |
+#define NET_FILTER_SDCH_SOURCE_STREAM_H_ |
+ |
+#include <memory> |
+ |
+#include "base/macros.h" |
+#include "net/base/net_export.h" |
+#include "net/base/sdch_dictionary.h" |
+#include "net/base/sdch_manager.h" |
+#include "net/filter/filter_source_stream.h" |
+ |
+namespace open_vcdiff { |
+ |
+class VCDiffStreamingDecoder; |
+ |
+} // namespace open_vcdiff |
+ |
+namespace net { |
+ |
+class IOBuffer; |
+ |
+// SdchSourceStream applies open_vcdiff content decoding to a datastream. |
+// This decoding uses a pre-cached dictionary of text fragments to decode |
+// (expand) the stream back to its original contents. |
+// |
+// This SdchSourceStream internally uses open_vcdiff/vcdec library to do |
+// decoding. |
+class NET_EXPORT_PRIVATE SdchSourceStream : public FilterSourceStream { |
+ public: |
+ // The Delegate interface is responsible for error recovery and stats |
+ // gathering. See the methods below for descriptions of which errors the |
+ // delegate is expected to handle and what it can do to repair them. |
+ class NET_EXPORT_PRIVATE Delegate { |
+ public: |
+ enum ErrorRecover { |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
nit: ErrorRecovery?
xunjieli
2016/10/05 13:44:57
Done.
|
+ // Do not recover the error. |
+ NONE, |
+ // Pass remaining input unchanged to downstream. |
+ PASS_THROUGH, |
+ // Pass an alternative output to downstream. |
+ REPLACE_OUTPUT, |
+ }; |
+ virtual ~Delegate(){}; |
+ |
+ // Called by the SdchSourceStream if an error occurs while parsing the |
+ // server-sent dictionary ID, or if the specified dictionary can't be loaded |
+ // (i.e., GetDictionary returned false). This method is expected to correct |
+ // the error condition by returning a ErrorRecover enum. If REPLACE_OUTPUT |
+ // is returned, also write the output to be replaced with to |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
nit: "it will also write ..."
xunjieli
2016/10/05 13:44:57
Done.
|
+ // |replace_output|. |
+ // This method must not retain a reference to |source| after returning. |
+ virtual ErrorRecover OnDictionaryIdError(SdchSourceStream* source, |
+ std::string* replace_output) = 0; |
+ |
+ // Called by the SdchSourceStream if the specified dictionary can't be |
+ // loaded (i.e., GetDictionary returned false). This method is expected to |
+ // correct the error condition by returning a ErrorRecover enum. |
+ // If REPLACE_OUTPUT is returned, also write the output to be replaced with |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
ditto: "it will also write .."
xunjieli
2016/10/05 13:44:57
Done.
|
+ // to |replace_output|. |
+ // This method must not retain a reference to |source| after returning. |
+ virtual ErrorRecover OnGetDictionaryError(SdchSourceStream* source, |
+ std::string* replace_output) = 0; |
+ |
+ // Called by the SdchSourceStream if an error occurs while decoding the |
+ // vcdiff-compressed data stream. Semantics are similar to |
+ // OnDictionaryError above. This method must not retain a reference to |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
nit: OnDictionaryIdError.
xunjieli
2016/10/05 13:44:57
Done.
|
+ // |source| after returning. |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
nit: For consistency I'd either have OnGetDictiona
xunjieli
2016/10/05 13:44:57
Done.
|
+ virtual ErrorRecover OnDecodingError(SdchSourceStream* source, |
+ std::string* replace_output) = 0; |
+ |
+ // Called by the SdchSourceStream to request the text of the specified |
+ // dictionary. This method must either: |
+ // * Fill in |*text| and return true, or |
+ // * Leave |*text| untouched and return false. |
+ // The return value is true if the named dictionary could be found and false |
+ // otherwise. |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
Would you note somewhere in here that the delegate
xunjieli
2016/10/05 13:44:57
Done.
|
+ // |
+ // The |server_id| string is guaranteed to be a syntactically valid SDCH |
+ // server-id. |
+ // TODO(xunjieli): If an async interface is required. Change |text| to use |
+ // a refcounted buffer and add a callback. |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
nit, suggestion: I'd refer to an IOBuffer rather t
xunjieli
2016/10/05 13:44:57
Done.
|
+ virtual bool OnGetDictionary(const std::string& server_id, |
+ const std::string** text) = 0; |
+ }; |
+ |
+ SdchSourceStream(std::unique_ptr<SourceStream> previous, Delegate* delegate); |
+ ~SdchSourceStream() override; |
+ |
+ private: |
+ enum InputState { |
+ STATE_LOAD_DICTIONARY, |
+ STATE_DECODE, |
+ STATE_FLUSH_INTERNAL_BUFFER, |
+ STATE_PASS_THROUGH, |
+ }; |
+ |
+ // SourceStream implementation: |
+ std::string GetTypeAsString() const override; |
+ int FilterData(IOBuffer* output_buffer, |
+ int output_buffer_size, |
+ IOBuffer* input_buffer, |
+ int input_buffer_size, |
+ int* consumed_bytes, |
+ bool upstream_end_reached) override; |
+ |
+ // Flushes |buffered_output_| to |output_buffer|. Returns number of bytes |
+ // flushed. |
+ int FlushBufferedOutput(char* output_buffer, int output_buffer_size); |
+ |
+ // Returns whether |id| looks like a dictionary ID, meaning 8 characters of |
+ // base64url followed by a null character. |
+ bool CouldBeDictionaryId(const std::string& id) const; |
+ |
+ // Helper method to handle error returned by Delegate. |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
nit, suggestion: Add documentation indicating that
xunjieli
2016/10/05 13:44:57
Done.
|
+ bool HandleError(Delegate::ErrorRecover error_recover); |
+ |
+ std::unique_ptr<open_vcdiff::VCDiffStreamingDecoder> decoder_; |
+ Delegate* delegate_; |
+ |
+ // After the encoded response SDCH header is read, this variable contains |
+ // the server hash with trailing null byte. |
+ std::string dictionary_server_id_; |
+ |
+ // Since vcdiff may generate quite a bit of output at once, SdchSourceStream |
+ // has to buffer excess output (more than requested by the caller) here to |
+ // return later. This could become quite large. crbug.com/651577. |
+ std::string buffered_output_; |
+ |
+ // Whether a dictionary error or a decoding error has occurred. |
+ bool error_occurred_; |
+ |
+ // State of the input stream. |
+ InputState input_state_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SdchSourceStream); |
+}; |
+ |
+} // namespace net |
+ |
+#endif // NET_FILTER_SDCH_SOURCE_STREAM_H_ |