Investigating leaks in bug 463263, backout bug 453403.
[wine-gecko.git] / intl / chardet / src / nsXMLEncodingObserver.cpp
blob22f605a45d3ef1336d3595147c13c0ec8eb22e42
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
38 #include "nsICharsetAlias.h"
39 #include "nsXMLEncodingObserver.h"
40 #include "nsIXMLEncodingService.h"
41 #include "nsIElementObserver.h"
42 #include "nsIObserver.h"
43 #include "nsIObserverService.h"
44 #include "nsISupports.h"
45 #include "nsCRT.h"
46 #include "nsIParser.h"
47 #include "pratom.h"
48 #include "nsCharDetDll.h"
49 #include "nsIServiceManager.h"
50 #include "nsObserverBase.h"
51 #include "nsWeakReference.h"
52 #include "nsReadableUtils.h"
53 #include "nsUnicharUtils.h"
55 static NS_DEFINE_CID(kCharsetAliasCID, NS_CHARSETALIAS_CID);
57 static const eHTMLTags gTags[] =
58 { eHTMLTag_instruction,
59 eHTMLTag_unknown
62 //-------------------------------------------------------------------------
63 nsXMLEncodingObserver::nsXMLEncodingObserver()
65 bXMLEncodingObserverStarted = PR_FALSE;
67 //-------------------------------------------------------------------------
68 nsXMLEncodingObserver::~nsXMLEncodingObserver()
70 // call to end the ObserverService
71 if (bXMLEncodingObserverStarted == PR_TRUE) {
72 End();
76 //-------------------------------------------------------------------------
77 NS_IMPL_ADDREF ( nsXMLEncodingObserver )
78 NS_IMPL_RELEASE ( nsXMLEncodingObserver )
80 // Use the new scheme
81 NS_IMPL_QUERY_INTERFACE4(nsXMLEncodingObserver,
82 nsIElementObserver,
83 nsIObserver,
84 nsIXMLEncodingService,
85 nsISupportsWeakReference)
87 //-------------------------------------------------------------------------
88 NS_IMETHODIMP nsXMLEncodingObserver::Notify(
89 PRUint32 aDocumentID,
90 const PRUnichar* aTag,
91 PRUint32 numOfAttributes,
92 const PRUnichar* nameArray[],
93 const PRUnichar* valueArray[])
95 if(!nsDependentString(aTag).LowerCaseEqualsLiteral("?xml"))
96 return NS_ERROR_ILLEGAL_VALUE;
97 else
98 return Notify(aDocumentID, numOfAttributes, nameArray, valueArray);
100 //-------------------------------------------------------------------------
101 NS_IMETHODIMP nsXMLEncodingObserver::Notify(
102 PRUint32 aDocumentID,
103 eHTMLTags aTag,
104 PRUint32 numOfAttributes,
105 const PRUnichar* nameArray[],
106 const PRUnichar* valueArray[])
108 if(eHTMLTag_meta != aTag)
109 return NS_ERROR_ILLEGAL_VALUE;
110 else
111 return Notify(aDocumentID, numOfAttributes, nameArray, valueArray);
113 //-------------------------------------------------------------------------
114 NS_IMETHODIMP nsXMLEncodingObserver::Notify(
115 PRUint32 aDocumentID,
116 PRUint32 numOfAttributes,
117 const PRUnichar* nameArray[],
118 const PRUnichar* valueArray[])
121 nsresult res = NS_OK;
122 PRUint32 i;
124 if(numOfAttributes >= 3)
126 PRBool bGotCurrentCharset=PR_FALSE;
127 PRBool bGotCurrentCharsetSource = PR_FALSE;
128 PRBool bGotEncoding = PR_FALSE;
130 nsCAutoString currentCharset(NS_LITERAL_CSTRING("unknown"));
131 nsAutoString charsetSourceStr(NS_LITERAL_STRING("unknown"));
132 nsCAutoString encoding(NS_LITERAL_CSTRING("unknown"));
134 for(i=0; i < numOfAttributes; i++)
136 if(0==nsCRT::strcmp(nameArray[i], NS_LITERAL_STRING("charset").get()))
138 bGotCurrentCharset = PR_TRUE;
139 LossyCopyUTF16toASCII(nsDependentString(valueArray[i]), currentCharset);
140 } else if(0==nsCRT::strcmp(nameArray[i], NS_LITERAL_STRING("charsetSource").get())) {
141 bGotCurrentCharsetSource = PR_TRUE;
142 charsetSourceStr = valueArray[i];
143 } else if(nsDependentString(nameArray[i]).LowerCaseEqualsLiteral("encoding")) {
144 bGotEncoding = PR_TRUE;
145 LossyCopyUTF16toASCII(nsDependentString(valueArray[i]), encoding);
149 // if we cannot find currentCharset or currentCharsetSource
150 // return error.
151 if( ! (bGotCurrentCharset && bGotCurrentCharsetSource))
153 return NS_ERROR_ILLEGAL_VALUE;
156 PRInt32 err;
157 PRInt32 charsetSourceInt = charsetSourceStr.ToInteger(&err);
159 // if we cannot convert the string into PRInt32, return error
160 if(NS_FAILED(err))
161 return NS_ERROR_ILLEGAL_VALUE;
163 PRInt32 currentCharsetSource = charsetSourceInt;
165 if(kCharsetFromMetaTag > currentCharsetSource)
167 if(! encoding.Equals(currentCharset))
169 nsCOMPtr<nsICharsetAlias> calias = do_GetService(kCharsetAliasCID, &res);
170 if(NS_SUCCEEDED(res) && (nsnull != calias) )
172 PRBool same = PR_FALSE;
173 res = calias->Equals( encoding, currentCharset, &same);
174 if(NS_SUCCEEDED(res) && (! same))
176 nsCAutoString preferred;
177 res = calias->GetPreferred(encoding,
178 preferred);
179 if(NS_SUCCEEDED(res))
181 res = NotifyWebShell(0,0, preferred.get(), kCharsetFromMetaTag );
182 return res;
183 } // if check for GetPreferred
184 } // if check res for Equals
185 } // if check res for GetService
186 } // if Equals
187 } // if
188 } // if
190 return NS_OK;
193 //-------------------------------------------------------------------------
194 NS_IMETHODIMP nsXMLEncodingObserver::Observe(nsISupports*, const char*, const PRUnichar*)
196 return NS_ERROR_NOT_IMPLEMENTED;
198 //-------------------------------------------------------------------------
199 NS_IMETHODIMP nsXMLEncodingObserver::Start()
201 nsresult res = NS_OK;
203 if (bXMLEncodingObserverStarted == PR_TRUE)
204 return res;
206 nsCOMPtr<nsIObserverService> anObserverService = do_GetService("@mozilla.org/observer-service;1", &res);
208 if (NS_SUCCEEDED(res)) {
209 res = anObserverService->AddObserver(this, "xmlparser", PR_TRUE);
211 bXMLEncodingObserverStarted = PR_TRUE;
214 return res;
216 //-------------------------------------------------------------------------
217 NS_IMETHODIMP nsXMLEncodingObserver::End()
219 nsresult res = NS_OK;
221 if (bXMLEncodingObserverStarted == PR_FALSE)
222 return res;
224 nsCOMPtr<nsIObserverService> anObserverService = do_GetService("@mozilla.org/observer-service;1", &res);
225 if (NS_SUCCEEDED(res)) {
226 res = anObserverService->RemoveObserver(this, "xmlparser");
228 bXMLEncodingObserverStarted = PR_FALSE;
231 return res;