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(bool possible_pass_through, |
| 62 std::string* replace_output) = 0; |
| 63 |
| 64 // Called by the SdchSourceStream if the specified dictionary can't be |
| 65 // loaded (i.e., GetDictionary returned false). This method is expected to |
| 66 // handle the error condition by returning a ErrorRecovery enum. |
| 67 // If REPLACE_OUTPUT is returned, it will also write the output to be |
| 68 // replaced with to |replace_output|. |
| 69 virtual ErrorRecovery OnGetDictionaryError(bool possible_pass_through, |
| 70 std::string* replace_output) = 0; |
| 71 |
| 72 // Called by the SdchSourceStream if an error occurs while decoding the |
| 73 // vcdiff-compressed data stream. This method is expected to |
| 74 // handle the error condition by returning a ErrorRecovery enum. |
| 75 // If REPLACE_OUTPUT is returned, it will also write the output to be |
| 76 // replaced with to |replace_output|. |
| 77 virtual ErrorRecovery OnDecodingError(std::string* replace_output) = 0; |
| 78 |
| 79 // Called by the SdchSourceStream to request the text of the specified |
| 80 // dictionary. This method must either: |
| 81 // * Fill in |*text| and return true, or |
| 82 // * Leave |*text| untouched and return false. |
| 83 // The delegate is required to make sure that the pointer written into |
| 84 // |*text| remains valid for the lifetime of the delegate. |
| 85 // The return value is true if the named dictionary could be found and false |
| 86 // otherwise. |
| 87 // |
| 88 // The |server_id| string is guaranteed to be a syntactically valid SDCH |
| 89 // server-id. |
| 90 // TODO(xunjieli): If an async interface is required. Change |text| to use |
| 91 // an IOBuffer buffer and add a callback. |
| 92 virtual bool OnGetDictionary(const std::string& server_id, |
| 93 const std::string** text) = 0; |
| 94 virtual void OnStreamDestroyed( |
| 95 InputState input_state, |
| 96 const std::string& buffered_output, |
| 97 open_vcdiff::VCDiffStreamingDecoder* decoder) = 0; |
| 98 }; |
| 99 |
| 100 SdchSourceStream(std::unique_ptr<SourceStream> previous, |
| 101 std::unique_ptr<Delegate> delegate, |
| 102 SourceStream::SourceType type); |
| 103 ~SdchSourceStream() override; |
| 104 |
| 105 private: |
| 106 // SourceStream implementation: |
| 107 std::string GetTypeAsString() const override; |
| 108 int FilterData(IOBuffer* output_buffer, |
| 109 int output_buffer_size, |
| 110 IOBuffer* input_buffer, |
| 111 int input_buffer_size, |
| 112 int* consumed_bytes, |
| 113 bool upstream_end_reached) override; |
| 114 |
| 115 // Returns whether |id| looks like a dictionary ID, meaning 8 characters of |
| 116 // base64url followed by a null character. |
| 117 bool CouldBeDictionaryId(const std::string& id) const; |
| 118 |
| 119 // Helper method to handle error returned by Delegate. It sets |input_state_| |
| 120 // and returns true if the error can be handles, and false if the error is |
| 121 // not recoverable. |
| 122 bool HandleError(Delegate::ErrorRecovery error_recover); |
| 123 |
| 124 std::unique_ptr<open_vcdiff::VCDiffStreamingDecoder> decoder_; |
| 125 std::unique_ptr<Delegate> delegate_; |
| 126 |
| 127 // After the encoded response SDCH header is read, this variable contains |
| 128 // the server hash with trailing null byte. |
| 129 std::string dictionary_server_id_; |
| 130 |
| 131 // Since vcdiff may generate quite a bit of output at once, SdchSourceStream |
| 132 // has to buffer excess output (more than requested by the caller) here to |
| 133 // return later. This could become quite large. crbug.com/651577. |
| 134 std::string buffered_output_; |
| 135 |
| 136 // Set when SourceStream::SourceType is TYPE_SDCH_POSSIBLE. |
| 137 const bool possible_pass_through_; |
| 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 |