Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / extensions / universalchardet / src / xpcom / nsUdetXPCOMWrapper.cpp
blobde018b9bdf928693a641495bd89edd502efc344d
1 /* -*- Mode: C++; tab-width: 2; 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 Universal charset detector code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2001
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Shy Shalom <shooshX@gmail.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * 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 ***** */
39 #include "nscore.h"
41 #include "nsUniversalDetector.h"
42 #include "nsUdetXPCOMWrapper.h"
43 #include "nsCharSetProber.h" // for DumpStatus
45 #include "nsUniversalCharDetDll.h"
46 //---- for XPCOM
47 #include "nsIFactory.h"
48 #include "nsISupports.h"
49 #include "pratom.h"
50 #include "prmem.h"
51 #include "nsCOMPtr.h"
53 static NS_DEFINE_CID(kUniversalDetectorCID, NS_UNIVERSAL_DETECTOR_CID);
54 static NS_DEFINE_CID(kUniversalStringDetectorCID, NS_UNIVERSAL_STRING_DETECTOR_CID);
56 //---------------------------------------------------------------------
57 nsXPCOMDetector:: nsXPCOMDetector(PRUint32 aLanguageFilter)
58 : nsUniversalDetector(aLanguageFilter)
61 //---------------------------------------------------------------------
62 nsXPCOMDetector::~nsXPCOMDetector()
65 //---------------------------------------------------------------------
67 NS_IMPL_ISUPPORTS1(nsXPCOMDetector, nsICharsetDetector)
69 //---------------------------------------------------------------------
70 NS_IMETHODIMP nsXPCOMDetector::Init(
71 nsICharsetDetectionObserver* aObserver)
73 NS_ASSERTION(mObserver == nsnull , "Init twice");
74 if(nsnull == aObserver)
75 return NS_ERROR_ILLEGAL_VALUE;
77 mObserver = aObserver;
78 return NS_OK;
80 //----------------------------------------------------------
81 NS_IMETHODIMP nsXPCOMDetector::DoIt(const char* aBuf,
82 PRUint32 aLen, PRBool* oDontFeedMe)
84 NS_ASSERTION(mObserver != nsnull , "have not init yet");
86 if((nsnull == aBuf) || (nsnull == oDontFeedMe))
87 return NS_ERROR_ILLEGAL_VALUE;
89 nsresult rv = this->HandleData(aBuf, aLen);
90 if (NS_FAILED(rv))
91 return rv;
93 if (mDone)
95 if (mDetectedCharset)
96 Report(mDetectedCharset);
98 *oDontFeedMe = PR_TRUE;
100 *oDontFeedMe = PR_FALSE;
101 return NS_OK;
103 //----------------------------------------------------------
104 NS_IMETHODIMP nsXPCOMDetector::Done()
106 NS_ASSERTION(mObserver != nsnull , "have not init yet");
107 #ifdef DEBUG_chardet
108 for (PRInt32 i = 0; i < NUM_OF_CHARSET_PROBERS; i++)
110 // If no data was received the array might stay filled with nulls
111 // the way it was initialized in the constructor.
112 if (mCharSetProbers[i])
113 mCharSetProbers[i]->DumpStatus();
115 #endif
117 this->DataEnd();
118 return NS_OK;
120 //----------------------------------------------------------
121 void nsXPCOMDetector::Report(const char* aCharset)
123 NS_ASSERTION(mObserver != nsnull , "have not init yet");
124 #ifdef DEBUG_chardet
125 printf("Universal Charset Detector report charset %s . \r\n", aCharset);
126 #endif
127 mObserver->Notify(aCharset, eBestAnswer);
131 //---------------------------------------------------------------------
132 nsXPCOMStringDetector:: nsXPCOMStringDetector(PRUint32 aLanguageFilter)
133 : nsUniversalDetector(aLanguageFilter)
136 //---------------------------------------------------------------------
137 nsXPCOMStringDetector::~nsXPCOMStringDetector()
140 //---------------------------------------------------------------------
141 NS_IMPL_ISUPPORTS1(nsXPCOMStringDetector, nsIStringCharsetDetector)
142 //---------------------------------------------------------------------
143 void nsXPCOMStringDetector::Report(const char *aCharset)
145 mResult = aCharset;
146 #ifdef DEBUG_chardet
147 printf("New Charset Prober report charset %s . \r\n", aCharset);
148 #endif
150 //---------------------------------------------------------------------
151 NS_IMETHODIMP nsXPCOMStringDetector::DoIt(const char* aBuf,
152 PRUint32 aLen, const char** oCharset,
153 nsDetectionConfident &oConf)
155 mResult = nsnull;
156 this->Reset();
157 nsresult rv = this->HandleData(aBuf, aLen);
158 if (NS_FAILED(rv))
159 return rv;
160 this->DataEnd();
161 if (mResult)
163 *oCharset=mResult;
164 oConf = eBestAnswer;
166 return NS_OK;