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