Chromium Code Reviews| Index: net/filter/sdch_stream_source.h |
| diff --git a/net/filter/sdch_stream_source.h b/net/filter/sdch_stream_source.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e98ea909b634c422266055a07353f494cf2f47ab |
| --- /dev/null |
| +++ b/net/filter/sdch_stream_source.h |
| @@ -0,0 +1,142 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef NET_FILTER_SDCH_STREAM_SOURCE_H |
| +#define NET_FILTER_SDCH_STREAM_SOURCE_H |
|
mmenke
2016/07/21 18:14:09
+_ (x3)
xunjieli
2016/07/27 20:32:03
Done.
|
| + |
| +#include <memory> |
| + |
| +#include "net/base/net_export.h" |
| +#include "net/base/sdch_dictionary.h" |
| +#include "net/base/sdch_manager.h" |
| +#include "net/filter/filter_stream_source.h" |
| + |
| +namespace open_vcdiff { |
| + |
| +class VCDiffStreamingDecoder; |
| + |
| +} // namespace open_vcdiff |
| + |
| +namespace net { |
| + |
| +class BlockBuffer; |
| + |
| +class NET_EXPORT_PRIVATE SdchStreamSource : public FilterStreamSource { |
| + public: |
| + // The Delegate interface is responsible for error recovery and stats |
| + // gathering. See the methods below for descriptions of which errors the |
| + // delegate is expected to handle and what it can do to repair them. |
| + class NET_EXPORT_PRIVATE Delegate { |
| + public: |
| + virtual ~Delegate(){}; |
| + |
| + // Called by the SdchStreamSource if an error occurs while parsing the |
| + // server-sent dictionary ID, or if the specified dictionary can't be loaded |
| + // (i.e., GetDictionary returned false). This method is expected to correct |
| + // the error condition by either: |
| + // a) returning true and calling ReplaceOutput or Passthrough, or |
| + // b) returning false, in which case the StreamSource will return an error |
| + // to its caller |
| + // Conceptually, the return value is true if the error has been |
| + // handled/recovered from and false if not. |
| + // This method must not retain a reference to |source| after returning. |
| + virtual bool OnDictionaryError(SdchStreamSource* source) = 0; |
| + |
| + // Called by the SdchStreamSource if an error occurs while decoding the |
| + // vcdiff-compressed data stream. Semantics are similar to |
| + // OnDictionaryError above. This method must not retain a reference to |
| + // |source| after returning. |
| + virtual bool OnDecodingError(SdchStreamSource* source) = 0; |
| + |
| + // Called by the SdchStreamSource to request the text of the specified |
| + // dictionary. This method must either: |
| + // * Fill in |*text| and return true, or |
| + // * Leave |*text| untouched and return false. |
| + // The return value is true if the named dictionary could be found and false |
| + // otherwise. |
| + // |
| + // The |server_id| string is guaranteed to be a syntactically valid SDCH |
| + // server-id. |
| + virtual bool OnGetDictionary(const std::string& server_id, |
| + const std::string** text) = 0; |
| + }; |
| + |
| + SdchStreamSource(std::unique_ptr<StreamSource> previous, Delegate* delegate); |
| + ~SdchStreamSource() override; |
| + |
| + bool Init() override; |
| + |
| + // For use by SdchStreamSourceDelegate. Switches this stream source into |
| + // "passthrough" mode, where it will not decode any further output with |
| + // vcdiff. This method can only be called during an invocation of the |
| + // delegate's OnDictionaryError or OnDecodingError functions. |
| + virtual void StopDecoding(); |
| + |
| + // For use by SdchStreamSourceDelegate. Replaces the entire output of this |
| + // stream with the supplied |data|, which is of length |size|. Note that |
| + // calling this method also stops vcdiff decoding as a side-effect - |data| |
| + // should not point to vcdiff-encoded data. This method can only be called |
| + // during an invocation of the delegate's OnDictionaryError or |
| + // OnDecodingError functions. |
| + virtual void ReplaceOutput(const char* data, size_t size); |
| + |
| + private: |
| + enum SdchStreamState { |
| + SDCH_STREAM_ERROR, |
| + SDCH_STREAM_MORE_INPUT, |
| + SDCH_STREAM_MORE_OUTPUT_SPACE, |
| + }; |
| + |
| + // StreamSource implementation: |
| + std::string GetTypeAsString() const override; |
| + int FilterData(IOBuffer* output_buffer, |
| + size_t output_buffer_size, |
| + DrainableIOBuffer* input_buffer) override; |
| + |
| + SdchStreamState Decompress(IOBuffer* output_buffer, |
| + size_t output_buffer_size, |
| + DrainableIOBuffer* input_buffer, |
| + size_t* bytes_read); |
| + // Pass any remaining input through without alteration. |
| + SdchStreamState PassThrough(IOBuffer* output_buffer, |
| + size_t output_buffer_size, |
| + DrainableIOBuffer* input_buffer, |
| + size_t* bytes_read); |
| + |
| + // Flushes data in |buffered_output_| to |output_buffer|. |
| + SdchStreamState Flush(IOBuffer* output_buffer, |
| + size_t output_buffer_size, |
| + size_t* bytes_read); |
| + |
| + bool LoadDictionary(DrainableIOBuffer* input_buffer); |
| + |
| + // Returns whether |id| looks like a dictionary ID, meaning 8 characters of |
| + // base64url followed by a null character. |
| + bool CouldBeDictionaryId(const std::string& id) const; |
| + |
| + bool AskDelegateToHandleDictionaryError(); |
| + bool AskDelegateToHandleDecodingError(); |
| + |
| + std::unique_ptr<open_vcdiff::VCDiffStreamingDecoder> decoder_; |
| + Delegate* delegate_; |
| + |
| + // After the encoded response SDCH header is read, this variable contains |
| + // the server hash with trailing null byte. |
| + std::string dictionary_id_; |
| + |
| + // Since vcdiff may generate quite a bit of output at once, SdchStreamSource |
| + // has to buffer excess output (more than requested by the caller) here to |
| + // return later. This could maybe become quite large. |
| + std::string buffered_output_; |
| + |
| + bool output_replaced_; |
| + bool passthrough_; |
| + bool dictionary_tried_load_; |
| + |
| + bool in_delegate_handler_; |
| +}; |
| + |
| +} // namespace net |
| + |
| +#endif // NET_FILTER_SDCH_STREAM_SOURCE_H |