1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
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/
8 : *
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.
13 : *
14 : * The Original Code is HTML Parser C++ Translator code.
15 : *
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.
20 : *
21 : * Contributor(s):
22 : * Henri Sivonen <hsivonen@iki.fi>
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsICharsetConverterManager.h"
39 : #include "nsServiceManagerUtils.h"
40 : #include "nsCharsetAlias.h"
41 : #include "nsEncoderDecoderUtils.h"
42 : #include "nsTraceRefcnt.h"
43 :
44 :
45 : void
46 0 : nsHtml5MetaScanner::sniff(nsHtml5ByteReadable* bytes, nsIUnicodeDecoder** decoder, nsACString& charset)
47 : {
48 0 : readable = bytes;
49 0 : stateLoop(stateSave);
50 0 : readable = nsnull;
51 0 : if (mUnicodeDecoder) {
52 0 : mUnicodeDecoder.forget(decoder);
53 0 : charset.Assign(mCharset);
54 : }
55 0 : }
56 :
57 : bool
58 0 : nsHtml5MetaScanner::tryCharset(nsString* charset)
59 : {
60 : // This code needs to stay in sync with
61 : // nsHtml5StreamParser::internalEncodingDeclaration. Unfortunately, the
62 : // trickery with member fields here leads to some copy-paste reuse. :-(
63 0 : nsresult res = NS_OK;
64 0 : nsCOMPtr<nsICharsetConverterManager> convManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
65 0 : if (NS_FAILED(res)) {
66 0 : NS_ERROR("Could not get CharsetConverterManager service.");
67 0 : return false;
68 : }
69 0 : nsCAutoString encoding;
70 0 : CopyUTF16toUTF8(*charset, encoding);
71 0 : encoding.Trim(" \t\r\n\f");
72 0 : if (encoding.LowerCaseEqualsLiteral("utf-16") ||
73 0 : encoding.LowerCaseEqualsLiteral("utf-16be") ||
74 0 : encoding.LowerCaseEqualsLiteral("utf-16le")) {
75 0 : mCharset.Assign("UTF-8");
76 0 : res = convManager->GetUnicodeDecoderRaw(mCharset.get(), getter_AddRefs(mUnicodeDecoder));
77 0 : if (NS_FAILED(res)) {
78 0 : NS_ERROR("Could not get decoder for UTF-8.");
79 0 : return false;
80 : }
81 0 : return true;
82 : }
83 0 : nsCAutoString preferred;
84 0 : res = nsCharsetAlias::GetPreferred(encoding, preferred);
85 0 : if (NS_FAILED(res)) {
86 0 : return false;
87 : }
88 0 : if (preferred.LowerCaseEqualsLiteral("utf-16") ||
89 0 : preferred.LowerCaseEqualsLiteral("utf-16be") ||
90 0 : preferred.LowerCaseEqualsLiteral("utf-16le") ||
91 0 : preferred.LowerCaseEqualsLiteral("utf-7") ||
92 0 : preferred.LowerCaseEqualsLiteral("jis_x0212-1990") ||
93 0 : preferred.LowerCaseEqualsLiteral("x-jis0208") ||
94 0 : preferred.LowerCaseEqualsLiteral("x-imap4-modified-utf7") ||
95 0 : preferred.LowerCaseEqualsLiteral("x-user-defined")) {
96 0 : return false;
97 : }
98 0 : res = convManager->GetUnicodeDecoderRaw(preferred.get(), getter_AddRefs(mUnicodeDecoder));
99 0 : if (res == NS_ERROR_UCONV_NOCONV) {
100 0 : return false;
101 0 : } else if (NS_FAILED(res)) {
102 0 : NS_ERROR("Getting an encoding decoder failed in a bad way.");
103 0 : mUnicodeDecoder = nsnull;
104 0 : return false;
105 : } else {
106 0 : NS_ASSERTION(mUnicodeDecoder, "Getter nsresult and object don't match.");
107 0 : mCharset.Assign(preferred);
108 0 : return true;
109 : }
110 : }
|