| 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 virtual ~Delegate(){}; |
| 40 |
| 41 // Called by the SdchSourceStream if an error occurs while parsing the |
| 42 // server-sent dictionary ID, or if the specified dictionary can't be loaded |
| 43 // (i.e., GetDictionary returned false). This method is expected to correct |
| 44 // the error condition by either: |
| 45 // a) returning true and calling ReplaceOutput or Passthrough, or |
| 46 // b) returning false, in which case the SourceStream will return an error |
| 47 // to its caller |
| 48 // Conceptually, the return value is true if the error has been |
| 49 // handled/recovered from and false if not. |
| 50 // This method must not retain a reference to |source| after returning. |
| 51 virtual bool OnDictionaryError(SdchSourceStream* source) = 0; |
| 52 |
| 53 // Called by the SdchSourceStream if an error occurs while decoding the |
| 54 // vcdiff-compressed data stream. Semantics are similar to |
| 55 // OnDictionaryError above. This method must not retain a reference to |
| 56 // |source| after returning. |
| 57 virtual bool OnDecodingError(SdchSourceStream* source) = 0; |
| 58 |
| 59 // Called by the SdchSourceStream to request the text of the specified |
| 60 // dictionary. This method must either: |
| 61 // * Fill in |*text| and return true, or |
| 62 // * Leave |*text| untouched and return false. |
| 63 // The return value is true if the named dictionary could be found and false |
| 64 // otherwise. |
| 65 // |
| 66 // The |server_id| string is guaranteed to be a syntactically valid SDCH |
| 67 // server-id. |
| 68 virtual bool OnGetDictionary(const std::string& server_id, |
| 69 const std::string** text) = 0; |
| 70 }; |
| 71 |
| 72 SdchSourceStream(std::unique_ptr<SourceStream> previous, Delegate* delegate); |
| 73 ~SdchSourceStream() override; |
| 74 |
| 75 // For use by SdchSourceStreamDelegate. Switches this stream source into |
| 76 // "passthrough" mode, where it will not decode any further output with |
| 77 // vcdiff. This method can only be called during an invocation of the |
| 78 // delegate's OnDictionaryError or OnDecodingError functions. |
| 79 virtual void StopDecoding(); |
| 80 |
| 81 // For use by SdchSourceStreamDelegate. Replaces the entire output of this |
| 82 // stream with the supplied |data|, which is of length |size|. Note that |
| 83 // calling this method also stops vcdiff decoding as a side-effect - |data| |
| 84 // should not point to vcdiff-encoded data. This method can only be called |
| 85 // during an invocation of the delegate's OnDictionaryError or |
| 86 // OnDecodingError functions. |
| 87 virtual void ReplaceOutput(const char* data, size_t size); |
| 88 |
| 89 private: |
| 90 enum SdchStreamState { |
| 91 SDCH_STREAM_ERROR, |
| 92 SDCH_STREAM_MORE_INPUT, |
| 93 SDCH_STREAM_MORE_OUTPUT_SPACE, |
| 94 }; |
| 95 |
| 96 // SourceStream implementation: |
| 97 std::string GetTypeAsString() const override; |
| 98 int FilterData(IOBuffer* output_buffer, |
| 99 size_t output_buffer_size, |
| 100 DrainableIOBuffer* input_buffer) override; |
| 101 |
| 102 SdchStreamState Decompress(IOBuffer* output_buffer, |
| 103 size_t output_buffer_size, |
| 104 DrainableIOBuffer* input_buffer, |
| 105 size_t* bytes_read); |
| 106 // Pass any remaining input through without alteration. |
| 107 SdchStreamState PassThrough(IOBuffer* output_buffer, |
| 108 size_t output_buffer_size, |
| 109 DrainableIOBuffer* input_buffer, |
| 110 size_t* bytes_read); |
| 111 |
| 112 // Flushes data in |buffered_output_| to |output_buffer|. |
| 113 SdchStreamState Flush(IOBuffer* output_buffer, |
| 114 size_t output_buffer_size, |
| 115 size_t* bytes_read); |
| 116 |
| 117 bool LoadDictionary(DrainableIOBuffer* input_buffer); |
| 118 |
| 119 // Returns whether |id| looks like a dictionary ID, meaning 8 characters of |
| 120 // base64url followed by a null character. |
| 121 bool CouldBeDictionaryId(const std::string& id) const; |
| 122 |
| 123 bool AskDelegateToHandleDictionaryError(); |
| 124 bool AskDelegateToHandleDecodingError(); |
| 125 |
| 126 std::unique_ptr<open_vcdiff::VCDiffStreamingDecoder> decoder_; |
| 127 Delegate* delegate_; |
| 128 |
| 129 // After the encoded response SDCH header is read, this variable contains |
| 130 // the server hash with trailing null byte. |
| 131 std::string dictionary_id_; |
| 132 |
| 133 // Since vcdiff may generate quite a bit of output at once, SdchSourceStream |
| 134 // has to buffer excess output (more than requested by the caller) here to |
| 135 // return later. This could maybe become quite large. |
| 136 std::string buffered_output_; |
| 137 |
| 138 bool output_replaced_; |
| 139 bool passthrough_; |
| 140 bool dictionary_tried_load_; |
| 141 |
| 142 bool in_delegate_handler_; |
| 143 |
| 144 DISALLOW_COPY_AND_ASSIGN(SdchSourceStream); |
| 145 }; |
| 146 |
| 147 } // namespace net |
| 148 |
| 149 #endif // NET_FILTER_SDCH_SOURCE_STREAM_H_ |
| OLD | NEW |