Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / parser / html / nsHtml5MetaScannerCppSupplement.h
blobaa94745009ebf0187145a8f1cd1a2161b29e498a
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is HTML Parser C++ Translator code.
16 * The Initial Developer of the Original Code is
17 * Mozilla Foundation.
18 * Portions created by the Initial Developer are Copyright (C) 2009
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Henri Sivonen <hsivonen@iki.fi>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsICharsetConverterManager.h"
39 #include "nsServiceManagerUtils.h"
40 #include "nsICharsetAlias.h"
41 #include "nsEncoderDecoderUtils.h"
42 #include "nsTraceRefcnt.h"
44 static NS_DEFINE_CID(kCharsetAliasCID, NS_CHARSETALIAS_CID);
46 void
47 nsHtml5MetaScanner::sniff(nsHtml5ByteReadable* bytes, nsIUnicodeDecoder** decoder, nsACString& charset)
49 readable = bytes;
50 stateLoop(stateSave);
51 readable = nsnull;
52 if (mUnicodeDecoder) {
53 mUnicodeDecoder.forget(decoder);
54 charset.Assign(mCharset);
58 PRBool
59 nsHtml5MetaScanner::tryCharset(nsString* charset)
61 // This code needs to stay in sync with
62 // nsHtml5StreamParser::internalEncodingDeclaration. Unfortunately, the
63 // trickery with member fields here leads to some copy-paste reuse. :-(
64 nsresult res = NS_OK;
65 nsCOMPtr<nsICharsetConverterManager> convManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
66 if (NS_FAILED(res)) {
67 NS_ERROR("Could not get CharsetConverterManager service.");
68 return PR_FALSE;
70 nsCAutoString encoding;
71 CopyUTF16toUTF8(*charset, encoding);
72 encoding.Trim(" \t\r\n\f");
73 if (encoding.LowerCaseEqualsLiteral("utf-16") ||
74 encoding.LowerCaseEqualsLiteral("utf-16be") ||
75 encoding.LowerCaseEqualsLiteral("utf-16le")) {
76 mCharset.Assign("UTF-8");
77 res = convManager->GetUnicodeDecoderRaw(mCharset.get(), getter_AddRefs(mUnicodeDecoder));
78 if (NS_FAILED(res)) {
79 NS_ERROR("Could not get decoder for UTF-8.");
80 return PR_FALSE;
82 return PR_TRUE;
84 nsCAutoString preferred;
85 nsCOMPtr<nsICharsetAlias> calias(do_GetService(kCharsetAliasCID, &res));
86 if (NS_FAILED(res)) {
87 NS_ERROR("Could not get CharsetAlias service.");
88 return PR_FALSE;
90 res = calias->GetPreferred(encoding, preferred);
91 if (NS_FAILED(res)) {
92 return PR_FALSE;
94 if (preferred.LowerCaseEqualsLiteral("utf-16") ||
95 preferred.LowerCaseEqualsLiteral("utf-16be") ||
96 preferred.LowerCaseEqualsLiteral("utf-16le") ||
97 preferred.LowerCaseEqualsLiteral("utf-32") ||
98 preferred.LowerCaseEqualsLiteral("utf-32be") ||
99 preferred.LowerCaseEqualsLiteral("utf-32le") ||
100 preferred.LowerCaseEqualsLiteral("utf-7") ||
101 preferred.LowerCaseEqualsLiteral("jis_x0212-1990") ||
102 preferred.LowerCaseEqualsLiteral("x-jis0208") ||
103 preferred.LowerCaseEqualsLiteral("x-imap4-modified-utf7") ||
104 preferred.LowerCaseEqualsLiteral("x-user-defined")) {
105 return PR_FALSE;
107 res = convManager->GetUnicodeDecoderRaw(preferred.get(), getter_AddRefs(mUnicodeDecoder));
108 if (res == NS_ERROR_UCONV_NOCONV) {
109 return PR_FALSE;
110 } else if (NS_FAILED(res)) {
111 NS_ERROR("Getting an encoding decoder failed in a bad way.");
112 mUnicodeDecoder = nsnull;
113 return PR_FALSE;
114 } else {
115 NS_ASSERTION(mUnicodeDecoder, "Getter nsresult and object don't match.");
116 mCharset.Assign(preferred);
117 return PR_TRUE;