| OLD | NEW |
| 1 /* | 1 /* |
| 2 Copyright (C) 1999 Lars Knoll (knoll@mpi-hd.mpg.de) | 2 Copyright (C) 1999 Lars Knoll (knoll@mpi-hd.mpg.de) |
| 3 Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com) | 3 Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com) |
| 4 Copyright (C) 2006, 2008 Apple Inc. All rights reserved. | 4 Copyright (C) 2006, 2008 Apple Inc. All rights reserved. |
| 5 | 5 |
| 6 This library is free software; you can redistribute it and/or | 6 This library is free software; you can redistribute it and/or |
| 7 modify it under the terms of the GNU Library General Public | 7 modify it under the terms of the GNU Library General Public |
| 8 License as published by the Free Software Foundation; either | 8 License as published by the Free Software Foundation; either |
| 9 version 2 of the License, or (at your option) any later version. | 9 version 2 of the License, or (at your option) any later version. |
| 10 | 10 |
| 11 This library is distributed in the hope that it will be useful, | 11 This library is distributed in the hope that it will be useful, |
| 12 but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 Library General Public License for more details. | 14 Library General Public License for more details. |
| 15 | 15 |
| 16 You should have received a copy of the GNU Library General Public License | 16 You should have received a copy of the GNU Library General Public License |
| 17 along with this library; see the file COPYING.LIB. If not, write to | 17 along with this library; see the file COPYING.LIB. If not, write to |
| 18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 Boston, MA 02110-1301, USA. | 19 Boston, MA 02110-1301, USA. |
| 20 | 20 |
| 21 */ | 21 */ |
| 22 | 22 |
| 23 #ifndef TextResourceDecoder_h | 23 #ifndef TextResourceDecoder_h |
| 24 #define TextResourceDecoder_h | 24 #define TextResourceDecoder_h |
| 25 | 25 |
| 26 #include <memory> |
| 26 #include "core/CoreExport.h" | 27 #include "core/CoreExport.h" |
| 28 #include "platform/weborigin/KURL.h" |
| 27 #include "wtf/PtrUtil.h" | 29 #include "wtf/PtrUtil.h" |
| 28 #include "wtf/text/TextEncoding.h" | 30 #include "wtf/text/TextEncoding.h" |
| 29 #include <memory> | |
| 30 | 31 |
| 31 namespace blink { | 32 namespace blink { |
| 32 | 33 |
| 33 class HTMLMetaCharsetParser; | 34 class HTMLMetaCharsetParser; |
| 34 | 35 |
| 35 class CORE_EXPORT TextResourceDecoder { | 36 class CORE_EXPORT TextResourceDecoder { |
| 36 USING_FAST_MALLOC(TextResourceDecoder); | 37 USING_FAST_MALLOC(TextResourceDecoder); |
| 37 WTF_MAKE_NONCOPYABLE(TextResourceDecoder); | 38 WTF_MAKE_NONCOPYABLE(TextResourceDecoder); |
| 38 | 39 |
| 39 public: | 40 public: |
| 40 enum EncodingSource { | 41 enum EncodingSource { |
| 41 DefaultEncoding, | 42 DefaultEncoding, |
| 42 AutoDetectedEncoding, | 43 AutoDetectedEncoding, |
| 43 EncodingFromContentSniffing, | 44 EncodingFromContentSniffing, |
| 44 EncodingFromXMLHeader, | 45 EncodingFromXMLHeader, |
| 45 EncodingFromMetaTag, | 46 EncodingFromMetaTag, |
| 46 EncodingFromCSSCharset, | 47 EncodingFromCSSCharset, |
| 47 EncodingFromHTTPHeader, | 48 EncodingFromHTTPHeader, |
| 48 EncodingFromParentFrame | 49 EncodingFromParentFrame |
| 49 }; | 50 }; |
| 50 | 51 |
| 51 static std::unique_ptr<TextResourceDecoder> create( | 52 static std::unique_ptr<TextResourceDecoder> create( |
| 52 const String& mimeType, | 53 const String& mimeType, |
| 53 const WTF::TextEncoding& defaultEncoding = WTF::TextEncoding()) { | 54 const WTF::TextEncoding& defaultEncoding = WTF::TextEncoding()) { |
| 54 return WTF::wrapUnique(new TextResourceDecoder( | 55 return WTF::wrapUnique(new TextResourceDecoder( |
| 55 mimeType, defaultEncoding, UseContentAndBOMBasedDetection, String())); | 56 mimeType, defaultEncoding, UseContentAndBOMBasedDetection, KURL())); |
| 56 } | 57 } |
| 57 | 58 |
| 58 static std::unique_ptr<TextResourceDecoder> createWithAutoDetection( | 59 static std::unique_ptr<TextResourceDecoder> createWithAutoDetection( |
| 59 const String& mimeType, | 60 const String& mimeType, |
| 60 const WTF::TextEncoding& defaultEncoding, | 61 const WTF::TextEncoding& defaultEncoding, |
| 61 const String& url) { | 62 const KURL& url) { |
| 62 return WTF::wrapUnique(new TextResourceDecoder(mimeType, defaultEncoding, | 63 return WTF::wrapUnique(new TextResourceDecoder(mimeType, defaultEncoding, |
| 63 UseAllAutoDetection, url)); | 64 UseAllAutoDetection, url)); |
| 64 } | 65 } |
| 65 | 66 |
| 66 // Corresponds to utf-8 decode in Encoding spec: | 67 // Corresponds to utf-8 decode in Encoding spec: |
| 67 // https://encoding.spec.whatwg.org/#utf-8-decode. | 68 // https://encoding.spec.whatwg.org/#utf-8-decode. |
| 68 static std::unique_ptr<TextResourceDecoder> createAlwaysUseUTF8ForText() { | 69 static std::unique_ptr<TextResourceDecoder> createAlwaysUseUTF8ForText() { |
| 69 return WTF::wrapUnique(new TextResourceDecoder( | 70 return WTF::wrapUnique(new TextResourceDecoder( |
| 70 "plain/text", UTF8Encoding(), AlwaysUseUTF8ForText, String())); | 71 "plain/text", UTF8Encoding(), AlwaysUseUTF8ForText, KURL())); |
| 71 } | 72 } |
| 72 ~TextResourceDecoder(); | 73 ~TextResourceDecoder(); |
| 73 | 74 |
| 74 void setEncoding(const WTF::TextEncoding&, EncodingSource); | 75 void setEncoding(const WTF::TextEncoding&, EncodingSource); |
| 75 const WTF::TextEncoding& encoding() const { return m_encoding; } | 76 const WTF::TextEncoding& encoding() const { return m_encoding; } |
| 76 bool encodingWasDetectedHeuristically() const { | 77 bool encodingWasDetectedHeuristically() const { |
| 77 return m_source == AutoDetectedEncoding || | 78 return m_source == AutoDetectedEncoding || |
| 78 m_source == EncodingFromContentSniffing; | 79 m_source == EncodingFromContentSniffing; |
| 79 } | 80 } |
| 80 | 81 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 106 // |m_contentType| must be |PlainTextContent| and | 107 // |m_contentType| must be |PlainTextContent| and |
| 107 // |m_encoding| must be UTF8Encoding. | 108 // |m_encoding| must be UTF8Encoding. |
| 108 // This doesn't change encoding based on BOMs, but still processes | 109 // This doesn't change encoding based on BOMs, but still processes |
| 109 // utf-8 BOMs so that utf-8 BOMs don't appear in the decoded result. | 110 // utf-8 BOMs so that utf-8 BOMs don't appear in the decoded result. |
| 110 AlwaysUseUTF8ForText | 111 AlwaysUseUTF8ForText |
| 111 }; | 112 }; |
| 112 | 113 |
| 113 TextResourceDecoder(const String& mimeType, | 114 TextResourceDecoder(const String& mimeType, |
| 114 const WTF::TextEncoding& defaultEncoding, | 115 const WTF::TextEncoding& defaultEncoding, |
| 115 EncodingDetectionOption, | 116 EncodingDetectionOption, |
| 116 const String& url); | 117 const KURL& hintUrl); |
| 117 | 118 |
| 118 private: | 119 private: |
| 119 enum ContentType { | 120 enum ContentType { |
| 120 PlainTextContent, | 121 PlainTextContent, |
| 121 HTMLContent, | 122 HTMLContent, |
| 122 XMLContent, | 123 XMLContent, |
| 123 CSSContent | 124 CSSContent |
| 124 }; // PlainText only checks for BOM. | 125 }; // PlainText only checks for BOM. |
| 125 static ContentType determineContentType(const String& mimeType); | 126 static ContentType determineContentType(const String& mimeType); |
| 126 static const WTF::TextEncoding& defaultEncoding( | 127 static const WTF::TextEncoding& defaultEncoding( |
| 127 ContentType, | 128 ContentType, |
| 128 const WTF::TextEncoding& defaultEncoding); | 129 const WTF::TextEncoding& defaultEncoding); |
| 129 | 130 |
| 130 bool checkForCSSCharset(const char*, size_t, bool& movedDataToBuffer); | 131 bool checkForCSSCharset(const char*, size_t, bool& movedDataToBuffer); |
| 131 bool checkForXMLCharset(const char*, size_t, bool& movedDataToBuffer); | 132 bool checkForXMLCharset(const char*, size_t, bool& movedDataToBuffer); |
| 132 void checkForMetaCharset(const char*, size_t); | 133 void checkForMetaCharset(const char*, size_t); |
| 133 bool shouldAutoDetect() const; | 134 bool shouldAutoDetect() const; |
| 134 | 135 |
| 135 ContentType m_contentType; | 136 ContentType m_contentType; |
| 136 WTF::TextEncoding m_encoding; | 137 WTF::TextEncoding m_encoding; |
| 137 std::unique_ptr<TextCodec> m_codec; | 138 std::unique_ptr<TextCodec> m_codec; |
| 138 EncodingSource m_source; | 139 EncodingSource m_source; |
| 139 const char* m_hintEncoding; | 140 const char* m_hintEncoding; |
| 140 const CString m_hintUrl; | 141 const KURL m_hintUrl; |
| 141 Vector<char> m_buffer; | 142 Vector<char> m_buffer; |
| 142 char m_hintLanguage[3]; | 143 char m_hintLanguage[3]; |
| 143 bool m_checkedForBOM; | 144 bool m_checkedForBOM; |
| 144 bool m_checkedForCSSCharset; | 145 bool m_checkedForCSSCharset; |
| 145 bool m_checkedForXMLCharset; | 146 bool m_checkedForXMLCharset; |
| 146 bool m_checkedForMetaCharset; | 147 bool m_checkedForMetaCharset; |
| 147 bool m_useLenientXMLDecoding; // Don't stop on XML decoding errors. | 148 bool m_useLenientXMLDecoding; // Don't stop on XML decoding errors. |
| 148 bool m_sawError; | 149 bool m_sawError; |
| 149 EncodingDetectionOption m_encodingDetectionOption; | 150 EncodingDetectionOption m_encodingDetectionOption; |
| 150 | 151 |
| 151 std::unique_ptr<HTMLMetaCharsetParser> m_charsetParser; | 152 std::unique_ptr<HTMLMetaCharsetParser> m_charsetParser; |
| 152 }; | 153 }; |
| 153 | 154 |
| 154 } // namespace blink | 155 } // namespace blink |
| 155 | 156 |
| 156 #endif | 157 #endif |
| OLD | NEW |