#ifndef NET_SHARED_DICTIONARY_SHARED_DICTIONARY_HEADER_CHECKER_SOURCE_STREAM_H_
#define NET_SHARED_DICTIONARY_SHARED_DICTIONARY_HEADER_CHECKER_SOURCE_STREAM_H_
#include <memory>
#include <string>
#include "net/base/completion_once_callback.h"
#include "net/base/hash_value.h"
#include "net/base/net_errors.h"
#include "net/base/net_export.h"
#include "net/filter/source_stream.h"
namespace net {
class IOBuffer;
class GrowableIOBuffer;
class NET_EXPORT SharedDictionaryHeaderCheckerSourceStream
: public SourceStream {
public:
enum class Type {
kDictionaryCompressedBrotli,
kDictionaryCompressedZstd,
};
SharedDictionaryHeaderCheckerSourceStream(
std::unique_ptr<SourceStream> upstream,
Type type,
const SHA256HashValue& dictionary_hash);
SharedDictionaryHeaderCheckerSourceStream(
const SharedDictionaryHeaderCheckerSourceStream&) = delete;
SharedDictionaryHeaderCheckerSourceStream& operator=(
const SharedDictionaryHeaderCheckerSourceStream&) = delete;
~SharedDictionaryHeaderCheckerSourceStream() override;
int Read(IOBuffer* dest_buffer,
int buffer_size,
CompletionOnceCallback callback) override;
std::string Description() const override;
bool MayHaveMoreBytes() const override;
private:
void ReadHeader();
void OnReadCompleted(int result);
bool CheckHeaderBuffer() const;
void HeaderCheckCompleted(int header_check_result);
base::span<const unsigned char> GetSignatureInBuffer() const;
base::span<const unsigned char> GetHashInBuffer() const;
std::unique_ptr<SourceStream> upstream_;
const Type type_;
const SHA256HashValue dictionary_hash_;
scoped_refptr<GrowableIOBuffer> head_read_buffer_;
int header_check_result_ = ERR_IO_PENDING;
scoped_refptr<IOBuffer> pending_read_buf_;
int pending_read_buf_len_ = 0;
CompletionOnceCallback pending_callback_;
};
}
#endif