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
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
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 ***** */
41 #include "nsReadableUtils.h"
42 #include "nsUnicharUtils.h"
43 #include "nsICharsetAlias.h"
44 #include "nsIServiceManager.h"
45 #include "nsICategoryManager.h"
46 #include "nsICharsetConverterManager.h"
47 #include "nsEncoderDecoderUtils.h"
48 #include "nsIStringBundle.h"
49 #include "nsILocaleService.h"
50 #include "nsUConvDll.h"
53 #include "nsVoidArray.h"
54 #include "nsStringEnumerator.h"
57 #include "nsISupportsPrimitives.h"
59 // just for CONTRACTIDs
60 #include "nsCharsetConverterManager.h"
62 #ifdef MOZ_USE_NATIVE_UCONV
63 #include "nsNativeUConvService.h"
66 // Pattern of cached, commonly used, single byte decoder
67 #define NS_1BYTE_CODER_PATTERN "ISO-8859"
68 #define NS_1BYTE_CODER_PATTERN_LEN 8
70 // Class nsCharsetConverterManager [implementation]
72 NS_IMPL_THREADSAFE_ISUPPORTS1(nsCharsetConverterManager
,
73 nsICharsetConverterManager
)
75 nsCharsetConverterManager::nsCharsetConverterManager()
76 :mDataBundle(NULL
), mTitleBundle(NULL
)
78 #ifdef MOZ_USE_NATIVE_UCONV
79 mNativeUC
= do_GetService(NS_NATIVE_UCONV_SERVICE_CONTRACT_ID
);
83 nsCharsetConverterManager::~nsCharsetConverterManager()
85 NS_IF_RELEASE(mDataBundle
);
86 NS_IF_RELEASE(mTitleBundle
);
89 nsresult
nsCharsetConverterManager::Init()
91 if (!mDecoderHash
.Init())
92 return NS_ERROR_OUT_OF_MEMORY
;
96 nsresult
nsCharsetConverterManager::RegisterConverterManagerData()
99 nsCOMPtr
<nsICategoryManager
> catman
= do_GetService(NS_CATEGORYMANAGER_CONTRACTID
, &rv
);
103 RegisterConverterCategory(catman
, NS_TITLE_BUNDLE_CATEGORY
,
104 "chrome://global/locale/charsetTitles.properties");
105 RegisterConverterCategory(catman
, NS_DATA_BUNDLE_CATEGORY
,
106 "resource://gre/res/charsetData.properties");
112 nsCharsetConverterManager::RegisterConverterCategory(nsICategoryManager
* catman
,
113 const char* aCategory
,
116 return catman
->AddCategoryEntry(aCategory
, aURL
, "",
117 PR_TRUE
, PR_TRUE
, nsnull
);
120 nsresult
nsCharsetConverterManager::LoadExtensibleBundle(
121 const char* aCategory
,
122 nsIStringBundle
** aResult
)
126 nsCOMPtr
<nsIStringBundleService
> sbServ
=
127 do_GetService(NS_STRINGBUNDLE_CONTRACTID
, &rv
);
131 return sbServ
->CreateExtensibleBundle(aCategory
, aResult
);
134 nsresult
nsCharsetConverterManager::GetBundleValue(nsIStringBundle
* aBundle
,
136 const nsAFlatString
& aProp
,
137 PRUnichar
** aResult
)
141 key
.AssignWithConversion(aName
);
142 ToLowerCase(key
); // we lowercase the main comparison key
145 return aBundle
->GetStringFromName(key
.get(), aResult
);
148 nsresult
nsCharsetConverterManager::GetBundleValue(nsIStringBundle
* aBundle
,
150 const nsAFlatString
& aProp
,
156 rv
= GetBundleValue(aBundle
, aName
, aProp
, getter_Copies(value
));
166 //----------------------------------------------------------------------------//----------------------------------------------------------------------------
167 // Interface nsICharsetConverterManager [implementation]
170 nsCharsetConverterManager::GetUnicodeEncoder(const char * aDest
,
171 nsIUnicodeEncoder
** aResult
)
173 // resolve the charset first
174 nsCAutoString charset
;
176 // fully qualify to possibly avoid vtable call
177 nsCharsetConverterManager::GetCharsetAlias(aDest
, charset
);
179 return nsCharsetConverterManager::GetUnicodeEncoderRaw(charset
.get(),
185 nsCharsetConverterManager::GetUnicodeEncoderRaw(const char * aDest
,
186 nsIUnicodeEncoder
** aResult
)
189 nsCOMPtr
<nsIUnicodeEncoder
> encoder
;
191 #ifdef MOZ_USE_NATIVE_UCONV
193 nsCOMPtr
<nsISupports
> supports
;
194 mNativeUC
->GetNativeConverter("UCS-2",
196 getter_AddRefs(supports
));
198 encoder
= do_QueryInterface(supports
);
201 NS_ADDREF(*aResult
= encoder
);
209 contractid(NS_LITERAL_CSTRING(NS_UNICODEENCODER_CONTRACTID_BASE
) +
210 nsDependentCString(aDest
));
212 // Always create an instance since encoders hold state.
213 encoder
= do_CreateInstance(contractid
.get(), &rv
);
216 rv
= NS_ERROR_UCONV_NOCONV
;
219 *aResult
= encoder
.get();
226 nsCharsetConverterManager::GetUnicodeDecoder(const char * aSrc
,
227 nsIUnicodeDecoder
** aResult
)
229 // resolve the charset first
230 nsCAutoString charset
;
232 // fully qualify to possibly avoid vtable call
233 nsCharsetConverterManager::GetCharsetAlias(aSrc
, charset
);
235 return nsCharsetConverterManager::GetUnicodeDecoderRaw(charset
.get(),
240 nsCharsetConverterManager::GetUnicodeDecoderRaw(const char * aSrc
,
241 nsIUnicodeDecoder
** aResult
)
244 nsCOMPtr
<nsIUnicodeDecoder
> decoder
;
246 #ifdef MOZ_USE_NATIVE_UCONV
248 nsCOMPtr
<nsISupports
> supports
;
249 mNativeUC
->GetNativeConverter(aSrc
,
251 getter_AddRefs(supports
));
253 decoder
= do_QueryInterface(supports
);
256 NS_ADDREF(*aResult
= decoder
);
263 NS_NAMED_LITERAL_CSTRING(contractbase
, NS_UNICODEDECODER_CONTRACTID_BASE
);
264 nsDependentCString
src(aSrc
);
266 if (!strncmp(aSrc
, NS_1BYTE_CODER_PATTERN
, NS_1BYTE_CODER_PATTERN_LEN
))
268 // Single byte decoders don't hold state. Optimize by using a service, and
269 // cache it in our hash to avoid repeated trips through the service manager.
270 if (!mDecoderHash
.Get(aSrc
, getter_AddRefs(decoder
))) {
271 decoder
= do_GetService(PromiseFlatCString(contractbase
+ src
).get(),
273 if (NS_SUCCEEDED(rv
))
274 mDecoderHash
.Put(aSrc
, decoder
);
279 decoder
= do_CreateInstance(PromiseFlatCString(contractbase
+ src
).get(),
282 NS_ENSURE_SUCCESS(rv
, NS_ERROR_UCONV_NOCONV
);
284 decoder
.forget(aResult
);
289 nsCharsetConverterManager::GetList(const nsACString
& aCategory
,
290 const nsACString
& aPrefix
,
291 nsIUTF8StringEnumerator
** aResult
)
294 return NS_ERROR_NULL_POINTER
;
300 nsCOMPtr
<nsICategoryManager
> catman
= do_GetService(NS_CATEGORYMANAGER_CONTRACTID
, &rv
);
304 nsCStringArray
* array
= new nsCStringArray
;
306 return NS_ERROR_OUT_OF_MEMORY
;
308 nsCOMPtr
<nsISimpleEnumerator
> enumerator
;
309 catman
->EnumerateCategory(PromiseFlatCString(aCategory
).get(),
310 getter_AddRefs(enumerator
));
313 while (NS_SUCCEEDED(enumerator
->HasMoreElements(&hasMore
)) && hasMore
) {
314 nsCOMPtr
<nsISupports
> supports
;
315 if (NS_FAILED(enumerator
->GetNext(getter_AddRefs(supports
))))
318 nsCOMPtr
<nsISupportsCString
> supStr
= do_QueryInterface(supports
);
322 nsCAutoString
fullName(aPrefix
);
325 if (NS_FAILED(supStr
->GetData(name
)))
329 rv
= GetCharsetAlias(fullName
.get(), alias
);
333 rv
= array
->AppendCString(alias
);
336 return NS_NewAdoptingUTF8StringEnumerator(aResult
, array
);
339 // we should change the interface so that we can just pass back a enumerator!
341 nsCharsetConverterManager::GetDecoderList(nsIUTF8StringEnumerator
** aResult
)
343 return GetList(NS_LITERAL_CSTRING(NS_UNICODEDECODER_NAME
),
344 EmptyCString(), aResult
);
348 nsCharsetConverterManager::GetEncoderList(nsIUTF8StringEnumerator
** aResult
)
350 return GetList(NS_LITERAL_CSTRING(NS_UNICODEENCODER_NAME
),
351 EmptyCString(), aResult
);
355 nsCharsetConverterManager::GetCharsetDetectorList(nsIUTF8StringEnumerator
** aResult
)
357 return GetList(NS_LITERAL_CSTRING("charset-detectors"),
358 NS_LITERAL_CSTRING("chardet."), aResult
);
361 // XXX Improve the implementation of this method. Right now, it is build on
362 // top of the nsCharsetAlias service. We can make the nsCharsetAlias
363 // better, with its own hash table (not the StringBundle anymore) and
364 // a nicer file format.
366 nsCharsetConverterManager::GetCharsetAlias(const char * aCharset
,
369 NS_PRECONDITION(aCharset
, "null param");
371 return NS_ERROR_NULL_POINTER
;
373 // We try to obtain the preferred name for this charset from the charset
374 // aliases. If we don't get it from there, we just use the original string
375 nsDependentCString
charset(aCharset
);
376 nsCOMPtr
<nsICharsetAlias
> csAlias(do_GetService(NS_CHARSETALIAS_CONTRACTID
));
377 NS_ASSERTION(csAlias
, "failed to get the CharsetAlias service");
380 nsresult rv
= csAlias
->GetPreferred(charset
, aResult
);
381 if (NS_SUCCEEDED(rv
)) {
382 return (!aResult
.IsEmpty()) ? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
392 nsCharsetConverterManager::GetCharsetTitle(const char * aCharset
,
395 if (aCharset
== NULL
) return NS_ERROR_NULL_POINTER
;
397 if (mTitleBundle
== NULL
) {
398 nsresult rv
= LoadExtensibleBundle(NS_TITLE_BUNDLE_CATEGORY
, &mTitleBundle
);
403 return GetBundleValue(mTitleBundle
, aCharset
, NS_LITERAL_STRING(".title"), aResult
);
407 nsCharsetConverterManager::GetCharsetData(const char * aCharset
,
408 const PRUnichar
* aProp
,
411 if (aCharset
== NULL
)
412 return NS_ERROR_NULL_POINTER
;
415 if (mDataBundle
== NULL
) {
416 nsresult rv
= LoadExtensibleBundle(NS_DATA_BUNDLE_CATEGORY
, &mDataBundle
);
421 return GetBundleValue(mDataBundle
, aCharset
, nsDependentString(aProp
), aResult
);
425 nsCharsetConverterManager::GetCharsetLangGroup(const char * aCharset
,
428 // resolve the charset first
429 nsCAutoString charset
;
431 nsresult rv
= GetCharsetAlias(aCharset
, charset
);
435 // fully qualify to possibly avoid vtable call
436 return nsCharsetConverterManager::GetCharsetLangGroupRaw(charset
.get(),
441 nsCharsetConverterManager::GetCharsetLangGroupRaw(const char * aCharset
,
446 if (aCharset
== NULL
)
447 return NS_ERROR_NULL_POINTER
;
451 if (mDataBundle
== NULL
) {
452 rv
= LoadExtensibleBundle(NS_DATA_BUNDLE_CATEGORY
, &mDataBundle
);
457 nsAutoString langGroup
;
458 rv
= GetBundleValue(mDataBundle
, aCharset
, NS_LITERAL_STRING(".LangGroup"), langGroup
);
460 if (NS_SUCCEEDED(rv
))
461 *aResult
= NS_NewAtom(langGroup
);