TextResourceDecoder.h [plain text]
#pragma once
#include "TextEncoding.h"
#include <wtf/RefCounted.h>
namespace WebCore {
class HTMLMetaCharsetParser;
class TextResourceDecoder : public RefCounted<TextResourceDecoder> {
public:
enum EncodingSource {
DefaultEncoding,
AutoDetectedEncoding,
EncodingFromXMLHeader,
EncodingFromMetaTag,
EncodingFromCSSCharset,
EncodingFromHTTPHeader,
UserChosenEncoding,
EncodingFromParentFrame
};
static Ref<TextResourceDecoder> create(const String& mimeType, const TextEncoding& defaultEncoding = TextEncoding(), bool usesEncodingDetector = false)
{
return adoptRef(*new TextResourceDecoder(mimeType, defaultEncoding, usesEncodingDetector));
}
WEBCORE_EXPORT ~TextResourceDecoder();
void setEncoding(const TextEncoding&, EncodingSource);
const TextEncoding& encoding() const { return m_encoding; }
bool hasEqualEncodingForCharset(const String&) const;
WEBCORE_EXPORT String decode(const char* data, size_t length);
WEBCORE_EXPORT String flush();
WEBCORE_EXPORT String decodeAndFlush(const char* data, size_t length);
void setHintEncoding(const TextResourceDecoder* hintDecoder)
{
if (hintDecoder && hintDecoder->m_source == AutoDetectedEncoding)
m_hintEncoding = hintDecoder->encoding().name();
}
void useLenientXMLDecoding() { m_useLenientXMLDecoding = true; }
bool sawError() const { return m_sawError; }
private:
WEBCORE_EXPORT TextResourceDecoder(const String& mimeType, const TextEncoding& defaultEncoding, bool usesEncodingDetector);
enum ContentType { PlainText, HTML, XML, CSS }; static ContentType determineContentType(const String& mimeType);
static const TextEncoding& defaultEncoding(ContentType, const TextEncoding& defaultEncoding);
size_t checkForBOM(const char*, size_t);
bool checkForCSSCharset(const char*, size_t, bool& movedDataToBuffer);
bool checkForHeadCharset(const char*, size_t, bool& movedDataToBuffer);
bool checkForMetaCharset(const char*, size_t);
void detectJapaneseEncoding(const char*, size_t);
bool shouldAutoDetect() const;
ContentType m_contentType;
TextEncoding m_encoding;
std::unique_ptr<TextCodec> m_codec;
EncodingSource m_source;
const char* m_hintEncoding;
Vector<char> m_buffer;
bool m_checkedForBOM;
bool m_checkedForCSSCharset;
bool m_checkedForHeadCharset;
bool m_useLenientXMLDecoding; bool m_sawError;
bool m_usesEncodingDetector;
std::unique_ptr<HTMLMetaCharsetParser> m_charsetParser;
};
}