#ifndef TextResourceDecoder_h
#define TextResourceDecoder_h
#include "wtf/RefCounted.h"
#include "wtf/text/TextEncoding.h"
namespace WebCore {
class DocumentEncodingData;
class HTMLMetaCharsetParser;
class TextResourceDecoder {
public:
enum EncodingSource {
DefaultEncoding,
AutoDetectedEncoding,
EncodingFromContentSniffing,
EncodingFromXMLHeader,
EncodingFromMetaTag,
EncodingFromCSSCharset,
EncodingFromHTTPHeader,
UserChosenEncoding,
EncodingFromParentFrame
};
static PassOwnPtr<TextResourceDecoder> create(const String& mimeType, const WTF::TextEncoding& defaultEncoding = WTF::TextEncoding(), bool usesEncodingDetector = false)
{
return adoptPtr(new TextResourceDecoder(mimeType, defaultEncoding, usesEncodingDetector));
}
~TextResourceDecoder();
void setEncoding(const WTF::TextEncoding&, EncodingSource);
const WTF::TextEncoding& encoding() const { return m_encoding; }
bool encodingWasDetectedHeuristically() const
{
return m_source == AutoDetectedEncoding
|| m_source == EncodingFromContentSniffing;
}
String decode(const char* data, size_t length);
String flush();
void setHintEncoding(const WTF::TextEncoding& encoding)
{
m_hintEncoding = encoding.name();
}
void useLenientXMLDecoding() { m_useLenientXMLDecoding = true; }
bool sawError() const { return m_sawError; }
private:
TextResourceDecoder(const String& mimeType, const WTF::TextEncoding& defaultEncoding, bool usesEncodingDetector);
enum ContentType { PlainTextContent, HTMLContent, XMLContent, CSSContent };
static ContentType determineContentType(const String& mimeType);
static const WTF::TextEncoding& defaultEncoding(ContentType, const WTF::TextEncoding& defaultEncoding);
size_t checkForBOM(const char*, size_t);
bool checkForCSSCharset(const char*, size_t, bool& movedDataToBuffer);
bool checkForXMLCharset(const char*, size_t, bool& movedDataToBuffer);
void checkForMetaCharset(const char*, size_t);
void detectJapaneseEncoding(const char*, size_t);
bool shouldAutoDetect() const;
ContentType m_contentType;
WTF::TextEncoding m_encoding;
OwnPtr<TextCodec> m_codec;
EncodingSource m_source;
const char* m_hintEncoding;
Vector<char> m_buffer;
bool m_checkedForBOM;
bool m_checkedForCSSCharset;
bool m_checkedForXMLCharset;
bool m_checkedForMetaCharset;
bool m_useLenientXMLDecoding;
bool m_sawError;
bool m_usesEncodingDetector;
OwnPtr<HTMLMetaCharsetParser> m_charsetParser;
};
}
#endif