| 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_STREAM_SOURCE_H |
| 6 #define NET_FILTER_SDCH_STREAM_SOURCE_H |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "net/base/net_export.h" |
| 11 #include "net/base/sdch_dictionary.h" |
| 12 #include "net/base/sdch_manager.h" |
| 13 #include "net/filter/filter_stream_source.h" |
| 14 |
| 15 namespace open_vcdiff { |
| 16 |
| 17 class VCDiffStreamingDecoder; |
| 18 |
| 19 } // namespace open_vcdiff |
| 20 |
| 21 namespace net { |
| 22 |
| 23 class BlockBuffer; |
| 24 |
| 25 class NET_EXPORT_PRIVATE SdchStreamSource : public FilterStreamSource { |
| 26 public: |
| 27 // The Delegate interface is responsible for error recovery and stats |
| 28 // gathering. See the methods below for descriptions of which errors the |
| 29 // delegate is expected to handle and what it can do to repair them. |
| 30 class NET_EXPORT_PRIVATE Delegate { |
| 31 public: |
| 32 virtual ~Delegate(){}; |
| 33 |
| 34 // Called by the SdchStreamSource if an error occurs while parsing the |
| 35 // server-sent dictionary ID, or if the specified dictionary can't be loaded |
| 36 // (i.e., GetDictionary returned false). This method is expected to correct |
| 37 // the error condition by either: |
| 38 // a) returning true and calling ReplaceOutput or Passthrough, or |
| 39 // b) returning false, in which case the StreamSource will return an error |
| 40 // to its caller |
| 41 // Conceptually, the return value is true if the error has been |
| 42 // handled/recovered from and false if not. |
| 43 // This method must not retain a reference to |source| after returning. |
| 44 virtual bool OnDictionaryError(SdchStreamSource* source) = 0; |
| 45 |
| 46 // Called by the SdchStreamSource if an error occurs while decoding the |
| 47 // vcdiff-compressed data stream. Semantics are similar to |
| 48 // OnDictionaryError above. This method must not retain a reference to |
| 49 // |source| after returning. |
| 50 virtual bool OnDecodingError(SdchStreamSource* source) = 0; |
| 51 |
| 52 // Called by the SdchStreamSource to request the text of the specified |
| 53 // dictionary. This method must either: |
| 54 // * Fill in |*text| and return true, or |
| 55 // * Leave |*text| untouched and return false. |
| 56 // The return value is true if the named dictionary could be found and false |
| 57 // otherwise. |
| 58 // |
| 59 // The |server_id| string is guaranteed to be a syntactically valid SDCH |
| 60 // server-id. |
| 61 virtual bool OnGetDictionary(const std::string& server_id, |
| 62 const std::string** text) = 0; |
| 63 }; |
| 64 |
| 65 SdchStreamSource(std::unique_ptr<StreamSource> previous, Delegate* delegate); |
| 66 ~SdchStreamSource() override; |
| 67 |
| 68 bool Init() override; |
| 69 |
| 70 // For use by SdchStreamSourceDelegate. Switches this stream source into |
| 71 // "passthrough" mode, where it will not decode any further output with |
| 72 // vcdiff. This method can only be called during an invocation of the |
| 73 // delegate's OnDictionaryError or OnDecodingError functions. |
| 74 virtual void StopDecoding(); |
| 75 |
| 76 // For use by SdchStreamSourceDelegate. Replaces the entire output of this |
| 77 // stream with the supplied |data|, which is of length |size|. Note that |
| 78 // calling this method also stops vcdiff decoding as a side-effect - |data| |
| 79 // should not point to vcdiff-encoded data. This method can only be called |
| 80 // during an invocation of the delegate's OnDictionaryError or |
| 81 // OnDecodingError functions. |
| 82 virtual void ReplaceOutput(const char* data, size_t size); |
| 83 |
| 84 private: |
| 85 enum SdchStreamState { |
| 86 SDCH_STREAM_ERROR, |
| 87 SDCH_STREAM_MORE_INPUT, |
| 88 SDCH_STREAM_MORE_OUTPUT_SPACE, |
| 89 }; |
| 90 |
| 91 // StreamSource implementation: |
| 92 std::string GetTypeAsString() const override; |
| 93 int FilterData(IOBuffer* output_buffer, |
| 94 size_t output_buffer_size, |
| 95 DrainableIOBuffer* input_buffer) override; |
| 96 |
| 97 SdchStreamState Decompress(IOBuffer* output_buffer, |
| 98 size_t output_buffer_size, |
| 99 DrainableIOBuffer* input_buffer, |
| 100 size_t* bytes_read); |
| 101 // Pass any remaining input through without alteration. |
| 102 SdchStreamState PassThrough(IOBuffer* output_buffer, |
| 103 size_t output_buffer_size, |
| 104 DrainableIOBuffer* input_buffer, |
| 105 size_t* bytes_read); |
| 106 |
| 107 // Flushes data in |buffered_output_| to |output_buffer|. |
| 108 SdchStreamState Flush(IOBuffer* output_buffer, |
| 109 size_t output_buffer_size, |
| 110 size_t* bytes_read); |
| 111 |
| 112 bool LoadDictionary(DrainableIOBuffer* input_buffer); |
| 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 bool AskDelegateToHandleDictionaryError(); |
| 119 bool AskDelegateToHandleDecodingError(); |
| 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_id_; |
| 127 |
| 128 // Since vcdiff may generate quite a bit of output at once, SdchStreamSource |
| 129 // has to buffer excess output (more than requested by the caller) here to |
| 130 // return later. This could maybe become quite large. |
| 131 std::string buffered_output_; |
| 132 |
| 133 bool output_replaced_; |
| 134 bool passthrough_; |
| 135 bool dictionary_tried_load_; |
| 136 |
| 137 bool in_delegate_handler_; |
| 138 }; |
| 139 |
| 140 } // namespace net |
| 141 |
| 142 #endif // NET_FILTER_SDCH_STREAM_SOURCE_H |
| OLD | NEW |