Chromium Code Reviews| 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 enum ErrorRecover { | |
|
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
nit: ErrorRecovery?
xunjieli
2016/10/05 13:44:57
Done.
| |
| 40 // Do not recover the error. | |
| 41 NONE, | |
| 42 // Pass remaining input unchanged to downstream. | |
| 43 PASS_THROUGH, | |
| 44 // Pass an alternative output to downstream. | |
| 45 REPLACE_OUTPUT, | |
| 46 }; | |
| 47 virtual ~Delegate(){}; | |
| 48 | |
| 49 // Called by the SdchSourceStream if an error occurs while parsing the | |
| 50 // server-sent dictionary ID, or if the specified dictionary can't be loaded | |
| 51 // (i.e., GetDictionary returned false). This method is expected to correct | |
| 52 // the error condition by returning a ErrorRecover enum. If REPLACE_OUTPUT | |
| 53 // 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.
| |
| 54 // |replace_output|. | |
| 55 // This method must not retain a reference to |source| after returning. | |
| 56 virtual ErrorRecover OnDictionaryIdError(SdchSourceStream* source, | |
| 57 std::string* replace_output) = 0; | |
| 58 | |
| 59 // Called by the SdchSourceStream if the specified dictionary can't be | |
| 60 // loaded (i.e., GetDictionary returned false). This method is expected to | |
| 61 // correct the error condition by returning a ErrorRecover enum. | |
| 62 // 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.
| |
| 63 // to |replace_output|. | |
| 64 // This method must not retain a reference to |source| after returning. | |
| 65 virtual ErrorRecover OnGetDictionaryError(SdchSourceStream* source, | |
| 66 std::string* replace_output) = 0; | |
| 67 | |
| 68 // Called by the SdchSourceStream if an error occurs while decoding the | |
| 69 // vcdiff-compressed data stream. Semantics are similar to | |
| 70 // 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.
| |
| 71 // |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.
| |
| 72 virtual ErrorRecover OnDecodingError(SdchSourceStream* source, | |
| 73 std::string* replace_output) = 0; | |
| 74 | |
| 75 // Called by the SdchSourceStream to request the text of the specified | |
| 76 // dictionary. This method must either: | |
| 77 // * Fill in |*text| and return true, or | |
| 78 // * Leave |*text| untouched and return false. | |
| 79 // The return value is true if the named dictionary could be found and false | |
| 80 // 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.
| |
| 81 // | |
| 82 // The |server_id| string is guaranteed to be a syntactically valid SDCH | |
| 83 // server-id. | |
| 84 // TODO(xunjieli): If an async interface is required. Change |text| to use | |
| 85 // 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.
| |
| 86 virtual bool OnGetDictionary(const std::string& server_id, | |
| 87 const std::string** text) = 0; | |
| 88 }; | |
| 89 | |
| 90 SdchSourceStream(std::unique_ptr<SourceStream> previous, Delegate* delegate); | |
| 91 ~SdchSourceStream() override; | |
| 92 | |
| 93 private: | |
| 94 enum InputState { | |
| 95 STATE_LOAD_DICTIONARY, | |
| 96 STATE_DECODE, | |
| 97 STATE_FLUSH_INTERNAL_BUFFER, | |
| 98 STATE_PASS_THROUGH, | |
| 99 }; | |
| 100 | |
| 101 // SourceStream implementation: | |
| 102 std::string GetTypeAsString() const override; | |
| 103 int FilterData(IOBuffer* output_buffer, | |
| 104 int output_buffer_size, | |
| 105 IOBuffer* input_buffer, | |
| 106 int input_buffer_size, | |
| 107 int* consumed_bytes, | |
| 108 bool upstream_end_reached) override; | |
| 109 | |
| 110 // Flushes |buffered_output_| to |output_buffer|. Returns number of bytes | |
| 111 // flushed. | |
| 112 int FlushBufferedOutput(char* output_buffer, int output_buffer_size); | |
| 113 | |
| 114 // Returns whether |id| looks like a dictionary ID, meaning 8 characters of | |
| 115 // base64url followed by a null character. | |
| 116 bool CouldBeDictionaryId(const std::string& id) const; | |
| 117 | |
| 118 // 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.
| |
| 119 bool HandleError(Delegate::ErrorRecover error_recover); | |
| 120 | |
| 121 std::unique_ptr<open_vcdiff::VCDiffStreamingDecoder> decoder_; | |
| 122 Delegate* delegate_; | |
| 123 | |
| 124 // After the encoded response SDCH header is read, this variable contains | |
| 125 // the server hash with trailing null byte. | |
| 126 std::string dictionary_server_id_; | |
| 127 | |
| 128 // Since vcdiff may generate quite a bit of output at once, SdchSourceStream | |
| 129 // has to buffer excess output (more than requested by the caller) here to | |
| 130 // return later. This could become quite large. crbug.com/651577. | |
| 131 std::string buffered_output_; | |
| 132 | |
| 133 // Whether a dictionary error or a decoding error has occurred. | |
| 134 bool error_occurred_; | |
| 135 | |
| 136 // State of the input stream. | |
| 137 InputState input_state_; | |
| 138 | |
| 139 DISALLOW_COPY_AND_ASSIGN(SdchSourceStream); | |
| 140 }; | |
| 141 | |
| 142 } // namespace net | |
| 143 | |
| 144 #endif // NET_FILTER_SDCH_SOURCE_STREAM_H_ | |
| OLD | NEW |