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