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 #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 |
| 93 // Called by the SdchSourceStream to notify the delegate that it is being |
| 94 // destroyed.|input_state| indicates the InputState of the stream's input |
| 95 // data. |buffered_output_present| indicates whether there is still data |
| 96 // in the buffered output that is not consumed. If |decoding_not_finished| |
| 97 // is true, it indicates that decoding has not finished. |
| 98 virtual void OnStreamDestroyed(InputState input_state, |
| 99 bool buffered_output_present, |
| 100 bool decoding_not_finished) = 0; |
| 101 }; |
| 102 |
| 103 SdchSourceStream(std::unique_ptr<SourceStream> previous, |
| 104 std::unique_ptr<Delegate> delegate, |
| 105 SourceStream::SourceType type); |
| 106 ~SdchSourceStream() override; |
| 107 |
| 108 private: |
| 109 // SourceStream implementation: |
| 110 std::string GetTypeAsString() const override; |
| 111 int FilterData(IOBuffer* output_buffer, |
| 112 int output_buffer_size, |
| 113 IOBuffer* input_buffer, |
| 114 int input_buffer_size, |
| 115 int* consumed_bytes, |
| 116 bool upstream_end_reached) override; |
| 117 |
| 118 // Returns whether |id| looks like a dictionary ID, meaning 8 characters of |
| 119 // base64url followed by a null character. |
| 120 bool CouldBeDictionaryId(const std::string& id) const; |
| 121 |
| 122 // Helper method to handle error returned by Delegate. It sets |input_state_| |
| 123 // and returns true if the error can be handles, and false if the error is |
| 124 // not recoverable. |
| 125 bool HandleError(Delegate::ErrorRecovery error_recover); |
| 126 |
| 127 std::unique_ptr<open_vcdiff::VCDiffStreamingDecoder> decoder_; |
| 128 std::unique_ptr<Delegate> delegate_; |
| 129 |
| 130 // After the encoded response SDCH header is read, this variable contains |
| 131 // the server hash with trailing null byte. |
| 132 std::string dictionary_server_id_; |
| 133 |
| 134 // Since vcdiff may generate quite a bit of output at once, SdchSourceStream |
| 135 // has to buffer excess output (more than requested by the caller) here to |
| 136 // return later. This could become quite large. crbug.com/651577. |
| 137 std::string buffered_output_; |
| 138 |
| 139 // State of the input stream. |
| 140 InputState input_state_; |
| 141 |
| 142 DISALLOW_COPY_AND_ASSIGN(SdchSourceStream); |
| 143 }; |
| 144 |
| 145 } // namespace net |
| 146 |
| 147 #endif // NET_FILTER_SDCH_SOURCE_STREAM_H_ |
OLD | NEW |