1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsUnicharUtils.h"
#include "nsCharsetAlias.h"
#include "nsICategoryManager.h"
#include "nsICharsetConverterManager.h"
#include "nsEncoderDecoderUtils.h"
#include "nsIStringBundle.h"
#include "nsTArray.h"
#include "nsStringEnumerator.h"
#include "mozilla/Services.h"
#include "nsComponentManagerUtils.h"
#include "nsISupportsPrimitives.h"
#include "nsServiceManagerUtils.h"
// just for CONTRACTIDs
#include "nsCharsetConverterManager.h"
static nsCOMPtr<nsIStringBundle> sDataBundle;
static nsCOMPtr<nsIStringBundle> sTitleBundle;
// Class nsCharsetConverterManager [implementation]
NS_IMPL_ISUPPORTS(nsCharsetConverterManager, nsICharsetConverterManager)
nsCharsetConverterManager::nsCharsetConverterManager()
{
}
nsCharsetConverterManager::~nsCharsetConverterManager()
{
sDataBundle = nullptr;
sTitleBundle = nullptr;
}
static
nsresult LoadBundle(const char* aBundleURLSpec, nsIStringBundle ** aResult)
{
nsCOMPtr<nsIStringBundleService> sbServ =
mozilla::services::GetStringBundleService();
if (!sbServ)
return NS_ERROR_FAILURE;
return sbServ->CreateBundle(aBundleURLSpec, aResult);
}
static
nsresult GetBundleValue(nsIStringBundle * aBundle,
const char * aName,
const nsAFlatString& aProp,
char16_t ** aResult)
{
nsAutoString key;
key.AssignWithConversion(aName);
ToLowerCase(key); // we lowercase the main comparison key
key.Append(aProp);
return aBundle->GetStringFromName(key.get(), aResult);
}
static
nsresult GetBundleValue(nsIStringBundle * aBundle,
const char * aName,
const nsAFlatString& aProp,
nsAString& aResult)
{
nsresult rv = NS_OK;
nsXPIDLString value;
rv = GetBundleValue(aBundle, aName, aProp, getter_Copies(value));
if (NS_FAILED(rv))
return rv;
aResult = value;
return NS_OK;
}
static
nsresult GetCharsetDataImpl(const char * aCharset, const char16_t * aProp,
nsAString& aResult)
{
NS_ENSURE_ARG_POINTER(aCharset);
// aProp can be nullptr
if (!sDataBundle) {
nsresult rv = LoadBundle("resource://gre-resources/charsetData.properties",
getter_AddRefs(sDataBundle));
if (NS_FAILED(rv))
return rv;
}
return GetBundleValue(sDataBundle, aCharset, nsDependentString(aProp), aResult);
}
//static
bool nsCharsetConverterManager::IsInternal(const nsACString& aCharset)
{
nsAutoString str;
// fully qualify to possibly avoid vtable call
nsresult rv = GetCharsetDataImpl(PromiseFlatCString(aCharset).get(),
u".isInternal",
str);
return NS_SUCCEEDED(rv);
}
//----------------------------------------------------------------------------//----------------------------------------------------------------------------
// Interface nsICharsetConverterManager [implementation]
NS_IMETHODIMP
nsCharsetConverterManager::GetUnicodeEncoder(const char * aDest,
nsIUnicodeEncoder ** aResult)
{
// resolve the charset first
nsAutoCString charset;
// fully qualify to possibly avoid vtable call
nsCharsetConverterManager::GetCharsetAlias(aDest, charset);
return nsCharsetConverterManager::GetUnicodeEncoderRaw(charset.get(),
aResult);
}
NS_IMETHODIMP
nsCharsetConverterManager::GetUnicodeEncoderRaw(const char * aDest,
nsIUnicodeEncoder ** aResult)
{
*aResult= nullptr;
nsCOMPtr<nsIUnicodeEncoder> encoder;
nsresult rv = NS_OK;
nsAutoCString
contractid(NS_LITERAL_CSTRING(NS_UNICODEENCODER_CONTRACTID_BASE) +
nsDependentCString(aDest));
// Always create an instance since encoders hold state.
encoder = do_CreateInstance(contractid.get(), &rv);
if (NS_FAILED(rv))
rv = NS_ERROR_UCONV_NOCONV;
else
{
*aResult = encoder.get();
NS_ADDREF(*aResult);
}
return rv;
}
NS_IMETHODIMP
nsCharsetConverterManager::GetUnicodeDecoder(const char * aSrc,
nsIUnicodeDecoder ** aResult)
{
// resolve the charset first
nsAutoCString charset;
// fully qualify to possibly avoid vtable call
if (NS_FAILED(nsCharsetConverterManager::GetCharsetAlias(aSrc, charset)))
return NS_ERROR_UCONV_NOCONV;
return nsCharsetConverterManager::GetUnicodeDecoderRaw(charset.get(),
aResult);
}
NS_IMETHODIMP
nsCharsetConverterManager::GetUnicodeDecoderInternal(const char * aSrc,
nsIUnicodeDecoder ** aResult)
{
// resolve the charset first
nsAutoCString charset;
nsresult rv = nsCharsetAlias::GetPreferredInternal(nsDependentCString(aSrc),
charset);
NS_ENSURE_SUCCESS(rv, rv);
return nsCharsetConverterManager::GetUnicodeDecoderRaw(charset.get(),
aResult);
}
NS_IMETHODIMP
nsCharsetConverterManager::GetUnicodeDecoderRaw(const char * aSrc,
nsIUnicodeDecoder ** aResult)
{
*aResult= nullptr;
nsCOMPtr<nsIUnicodeDecoder> decoder;
nsresult rv = NS_OK;
NS_NAMED_LITERAL_CSTRING(contractbase, NS_UNICODEDECODER_CONTRACTID_BASE);
nsDependentCString src(aSrc);
decoder = do_CreateInstance(PromiseFlatCString(contractbase + src).get(),
&rv);
NS_ENSURE_SUCCESS(rv, NS_ERROR_UCONV_NOCONV);
decoder.forget(aResult);
return rv;
}
static
nsresult GetList(const nsACString& aCategory,
const nsACString& aPrefix,
nsIUTF8StringEnumerator** aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
*aResult = nullptr;
nsresult rv;
nsCOMPtr<nsICategoryManager> catman = do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
if (NS_FAILED(rv))
return rv;
nsTArray<nsCString>* array = new nsTArray<nsCString>;
if (!array)
return NS_ERROR_OUT_OF_MEMORY;
nsCOMPtr<nsISimpleEnumerator> enumerator;
catman->EnumerateCategory(PromiseFlatCString(aCategory).get(),
getter_AddRefs(enumerator));
bool hasMore;
while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore) {
nsCOMPtr<nsISupports> supports;
if (NS_FAILED(enumerator->GetNext(getter_AddRefs(supports))))
continue;
nsCOMPtr<nsISupportsCString> supStr = do_QueryInterface(supports);
if (!supStr)
continue;
nsAutoCString name;
if (NS_FAILED(supStr->GetData(name)))
continue;
nsAutoCString fullName(aPrefix);
fullName.Append(name);
NS_ENSURE_TRUE(array->AppendElement(fullName), NS_ERROR_OUT_OF_MEMORY);
}
return NS_NewAdoptingUTF8StringEnumerator(aResult, array);
}
// we should change the interface so that we can just pass back a enumerator!
NS_IMETHODIMP
nsCharsetConverterManager::GetDecoderList(nsIUTF8StringEnumerator ** aResult)
{
return GetList(NS_LITERAL_CSTRING(NS_UNICODEDECODER_NAME),
EmptyCString(), aResult);
}
NS_IMETHODIMP
nsCharsetConverterManager::GetEncoderList(nsIUTF8StringEnumerator ** aResult)
{
return GetList(NS_LITERAL_CSTRING(NS_UNICODEENCODER_NAME),
EmptyCString(), aResult);
}
NS_IMETHODIMP
nsCharsetConverterManager::GetCharsetDetectorList(nsIUTF8StringEnumerator** aResult)
{
return GetList(NS_LITERAL_CSTRING("charset-detectors"),
NS_LITERAL_CSTRING("chardet."), aResult);
}
// XXX Improve the implementation of this method. Right now, it is build on
// top of the nsCharsetAlias service. We can make the nsCharsetAlias
// better, with its own hash table (not the StringBundle anymore) and
// a nicer file format.
NS_IMETHODIMP
nsCharsetConverterManager::GetCharsetAlias(const char * aCharset,
nsACString& aResult)
{
NS_ENSURE_ARG_POINTER(aCharset);
// We try to obtain the preferred name for this charset from the charset
// aliases.
nsresult rv;
rv = nsCharsetAlias::GetPreferred(nsDependentCString(aCharset), aResult);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
NS_IMETHODIMP
nsCharsetConverterManager::GetCharsetTitle(const char * aCharset,
nsAString& aResult)
{
NS_ENSURE_ARG_POINTER(aCharset);
if (!sTitleBundle) {
nsresult rv = LoadBundle("chrome://messenger/locale/charsetTitles.properties",
getter_AddRefs(sTitleBundle));
NS_ENSURE_SUCCESS(rv, rv);
}
return GetBundleValue(sTitleBundle, aCharset, NS_LITERAL_STRING(".title"), aResult);
}
NS_IMETHODIMP
nsCharsetConverterManager::GetCharsetData(const char * aCharset,
const char16_t * aProp,
nsAString& aResult)
{
return GetCharsetDataImpl(aCharset, aProp, aResult);
}
NS_IMETHODIMP
nsCharsetConverterManager::GetCharsetLangGroup(const char * aCharset,
nsIAtom** aResult)
{
// resolve the charset first
nsAutoCString charset;
nsresult rv = GetCharsetAlias(aCharset, charset);
NS_ENSURE_SUCCESS(rv, rv);
// fully qualify to possibly avoid vtable call
return nsCharsetConverterManager::GetCharsetLangGroupRaw(charset.get(),
aResult);
}
NS_IMETHODIMP
nsCharsetConverterManager::GetCharsetLangGroupRaw(const char * aCharset,
nsIAtom** aResult)
{
*aResult = nullptr;
nsAutoString langGroup;
// fully qualify to possibly avoid vtable call
nsresult rv = nsCharsetConverterManager::GetCharsetData(
aCharset, u".LangGroup", langGroup);
if (NS_SUCCEEDED(rv)) {
ToLowerCase(langGroup); // use lowercase for all language atoms
*aResult = NS_Atomize(langGroup).take();
}
return rv;
}
|