Bug 563536, bug 204111 - Put data from uconv property files into code to avoid thread-unsafe property file loading. r=smontagu.

This commit is contained in:
Henri Sivonen 2010-05-10 17:09:13 +03:00
parent 5918a1b07d
commit 2120fc2a17
14 changed files with 215 additions and 483 deletions

View File

@ -58,12 +58,12 @@ CPPSRCS = \
nsConverterInputStream.cpp \
nsConverterOutputStream.cpp \
nsTextToSubURI.cpp \
nsGREResProperties.cpp \
nsCharsetConverterManager.cpp \
nsUTF8ConverterService.cpp \
nsUTF8ToUnicode.cpp \
nsUnicodeToUTF8.cpp \
nsScriptableUConv.cpp \
nsUConvPropertySearch.cpp \
$(NULL)
ifndef MOZ_USE_NATIVE_UCONV
@ -143,4 +143,37 @@ include $(topsrcdir)/config/rules.mk
# nl_langinfo(CODESET) to compile on these systems.
ifeq ($(OS_ARCH), Linux)
DEFINES += -D_XOPEN_SOURCE=500
endif
endif
nsCharsetAliasImp.$(OBJ_SUFFIX): charsetalias.properties.h
nsMacCharset.$(OBJ_SUFFIX): maccharset.properties.h
nsOS2Charset.$(OBJ_SUFFIX): os2charset.properties.h
nsWinCharset.$(OBJ_SUFFIX): wincharset.properties.h
nsUNIXCharset.$(OBJ_SUFFIX): unixcharset.properties.h
charsetalias.properties.h: props2arrays.py charsetalias.properties
$(PYTHON) $^ $@
maccharset.properties.h: props2arrays.py maccharset.properties
$(PYTHON) $^ $@
os2charset.properties.h: props2arrays.py os2charset.properties
$(PYTHON) $^ $@
wincharset.properties.h: props2arrays.py wincharset.properties
$(PYTHON) $^ $@
unixcharset.properties.h: props2arrays.py unixcharset.properties
$(PYTHON) $^ $@
GARBAGE += \
charsetalias.properties.h \
maccharset.properties.h \
os2charset.properties.h \
unixcharset.properties.h \
wincharset.properties.h \
$(NULL)

View File

@ -1,12 +1,2 @@
toolkit.jar:
res/charsetalias.properties (charsetalias.properties)
res/charsetData.properties (charsetData.properties)
#ifdef XP_OS2
res/os2charset.properties (os2charset.properties)
#elifdef XP_WIN
res/wincharset.properties (wincharset.properties)
#elifdef XP_MACOSX
res/maccharset.properties (maccharset.properties)
#elifndef XP_BEOS
res/unixcharset.properties (unixcharset.properties)
#endif

View File

@ -38,8 +38,6 @@
#define nsCharsetAlias_h__
#include "nsICharsetAlias.h"
#include "nsGREResProperties.h"
#include "mozilla/Mutex.h"
//==============================================================
class nsCharsetAlias2 : public nsICharsetAlias
@ -55,9 +53,6 @@ public:
NS_IMETHOD Equals(const nsACString& aCharset1, const nsACString& aCharset2, PRBool* oResult) ;
private:
nsGREResProperties* mDelegate;
mozilla::Mutex mDelegateMutex;
};
#endif // nsCharsetAlias_h__

View File

@ -45,7 +45,7 @@
#include "nsUConvDll.h"
#include "nsReadableUtils.h"
#include "nsUnicharUtils.h"
#include "nsGREResProperties.h"
#include "nsUConvPropertySearch.h"
#include "nsITimelineService.h"
#include "nsCharsetAlias.h"
@ -54,24 +54,16 @@ NS_IMPL_THREADSAFE_ISUPPORTS1(nsCharsetAlias2, nsICharsetAlias)
//--------------------------------------------------------------
nsCharsetAlias2::nsCharsetAlias2()
: mDelegate(nsnull)
, mDelegateMutex("nsCharsetAlias2 mDelegateMutex")
{
}
//--------------------------------------------------------------
nsCharsetAlias2::~nsCharsetAlias2()
{
if(mDelegate)
delete mDelegate;
}
//
static const char* kAliases[][3] = {
// Triple with { lower-case test string, out string, length of out string }
{ "iso-8859-1", "ISO-8859-1", (const char*)NS_INT32_TO_PTR(10) },
{ "utf-8", "UTF-8", (const char*)NS_INT32_TO_PTR(5) },
{ "x-sjis", "Shift_JIS", (const char*)NS_INT32_TO_PTR(9) },
{ "shift_jis", "Shift_JIS", (const char*)NS_INT32_TO_PTR(9) }
#include "charsetalias.properties.h"
};
//--------------------------------------------------------------
@ -82,44 +74,15 @@ NS_IMETHODIMP nsCharsetAlias2::GetPreferred(const nsACString& aAlias,
NS_TIMELINE_START_TIMER("nsCharsetAlias2:GetPreferred");
// Delay loading charsetalias.properties by hardcoding the most
// frequent aliases. Note that it's possible to recur in to this
// function *while loading* charsetalias.properties (see bug 190951),
// so we might have an |mDelegate| already that isn't valid yet, but
// the load is guaranteed to be "UTF-8" so things will be OK.
for (PRUint32 index = 0; index < NS_ARRAY_LENGTH(kAliases); index++) {
if (aAlias.LowerCaseEqualsASCII(kAliases[index][0])) {
oResult.Assign(nsDependentCString(kAliases[index][1],
NS_PTR_TO_UINT32(kAliases[index][2])));
NS_TIMELINE_STOP_TIMER("nsCharsetAlias2:GetPreferred");
return NS_OK;
}
}
oResult.Truncate();
if(!mDelegate) {
mozilla::MutexAutoLock autoLock(mDelegateMutex);
if (!mDelegate) {
mDelegate = new nsGREResProperties( NS_LITERAL_CSTRING("charsetalias.properties") );
NS_ASSERTION(mDelegate, "cannot create nsGREResProperties");
if(nsnull == mDelegate)
return NS_ERROR_OUT_OF_MEMORY;
}
}
NS_TIMELINE_STOP_TIMER("nsCharsetAlias2:GetPreferred");
NS_TIMELINE_MARK_TIMER("nsCharsetAlias2:GetPreferred");
nsCAutoString key(aAlias);
ToLowerCase(key);
// hack for now, have to fix nsGREResProperties, but we can't until
// string bundles use UTF8 keys
nsAutoString result;
nsresult rv = mDelegate->Get(NS_ConvertASCIItoUTF16(key), result);
LossyAppendUTF16toASCII(result, oResult);
return rv;
nsresult rv = nsUConvPropertySearch::SearchPropertyValue(kAliases,
NS_ARRAY_LENGTH(kAliases), key, oResult);
NS_TIMELINE_STOP_TIMER("nsCharsetAlias2:GetPreferred");
NS_TIMELINE_MARK_TIMER("nsCharsetAlias2:GetPreferred");
return rv;
}
//--------------------------------------------------------------

View File

@ -38,7 +38,7 @@
#include <Carbon/Carbon.h>
#include "nsIPlatformCharset.h"
#include "pratom.h"
#include "nsGREResProperties.h"
#include "nsUConvPropertySearch.h"
#include "nsUConvDll.h"
#include "nsCOMPtr.h"
#include "nsIServiceManager.h"
@ -48,36 +48,17 @@
#include "nsPlatformCharset.h"
#include "nsEncoderDecoderUtils.h"
static nsGREResProperties *gInfo = nsnull;
static PRInt32 gCnt = 0;
static const char* kMacCharsets[][3] = {
#include "maccharset.properties.h"
};
NS_IMPL_ISUPPORTS1(nsPlatformCharset, nsIPlatformCharset)
nsPlatformCharset::nsPlatformCharset()
{
PR_AtomicIncrement(&gCnt);
}
nsPlatformCharset::~nsPlatformCharset()
{
PR_AtomicDecrement(&gCnt);
if((0 == gCnt) && (nsnull != gInfo)) {
delete gInfo;
gInfo = nsnull;
}
}
nsresult nsPlatformCharset::InitInfo()
{
// load the .property file if necessary
if (gInfo == nsnull) {
nsGREResProperties *info =
new nsGREResProperties(NS_LITERAL_CSTRING("maccharset.properties"));
NS_ASSERTION(info , "cannot open properties file");
NS_ENSURE_TRUE(info, NS_ERROR_FAILURE);
gInfo = info;
}
return NS_OK;
}
nsresult nsPlatformCharset::MapToCharset(short script, short region, nsACString& outCharset)
@ -93,26 +74,19 @@ nsresult nsPlatformCharset::MapToCharset(short script, short region, nsACString&
return NS_OK;
}
// ensure the .property file is loaded
nsresult rv = InitInfo();
NS_ENSURE_SUCCESS(rv, rv);
// try mapping from region then from script
nsAutoString key(NS_LITERAL_STRING("region."));
nsCAutoString key("region.");
key.AppendInt(region, 10);
nsAutoString uCharset;
rv = gInfo->Get(key, uCharset);
if (NS_SUCCEEDED(rv))
LossyCopyUTF16toASCII(uCharset, outCharset);
else {
nsresult rv = nsUConvPropertySearch::SearchPropertyValue(kMacCharsets,
NS_ARRAY_LENGTH(kMacCharsets), key, outCharset);
if (NS_FAILED(rv)) {
key.AssignLiteral("script.");
key.AppendInt(script, 10);
rv = gInfo->Get(key, uCharset);
rv = nsUConvPropertySearch::SearchPropertyValue(kMacCharsets,
NS_ARRAY_LENGTH(kMacCharsets), key, outCharset);
// not found in the .property file, assign x-mac-roman
if (NS_SUCCEEDED(rv))
LossyCopyUTF16toASCII(uCharset, outCharset);
else {
if (NS_FAILED(rv)) {
outCharset.AssignLiteral("x-mac-roman");
}
}

View File

@ -39,7 +39,7 @@
*
* ***** END LICENSE BLOCK ***** */
#include "nsIPlatformCharset.h"
#include "nsGREResProperties.h"
#include "nsUConvPropertySearch.h"
#include "pratom.h"
#define INCL_WIN
#include <os2.h>
@ -52,8 +52,9 @@
#include "nsITimelineService.h"
#include "nsPlatformCharset.h"
static nsGREResProperties *gInfo = nsnull;
static PRInt32 gCnt= 0;
static const char* kOS2Charsets[][3] = {
#include "os2charset.properties.h"
};
NS_IMPL_ISUPPORTS1(nsPlatformCharset, nsIPlatformCharset)
@ -72,59 +73,20 @@ nsPlatformCharset::nsPlatformCharset()
}
nsPlatformCharset::~nsPlatformCharset()
{
PR_AtomicDecrement(&gCnt);
if ((0 == gCnt) && (nsnull != gInfo)) {
delete gInfo;
gInfo = nsnull;
}
}
nsresult
nsPlatformCharset::InitInfo()
{
PR_AtomicIncrement(&gCnt); // count for gInfo
if (gInfo == nsnull) {
nsGREResProperties *info =
new nsGREResProperties(NS_LITERAL_CSTRING("os2charset.properties"));
NS_ASSERTION(info , "cannot open properties file");
NS_ENSURE_TRUE(info, NS_ERROR_FAILURE);
gInfo = info;
}
return NS_OK;
}
nsresult
nsPlatformCharset::MapToCharset(nsAString& inANSICodePage, nsACString& outCharset)
{
//delay loading os2charset.properties bundle if possible
if (inANSICodePage.EqualsLiteral("os2.850")) {
outCharset.AssignLiteral("IBM850");
return NS_OK;
}
nsCAutoString key;
LossyCopyUTF16toASCII(inANSICodePage, key);
if (inANSICodePage.EqualsLiteral("os2.932")) {
outCharset.AssignLiteral("Shift_JIS");
return NS_OK;
}
// ensure the .property file is loaded
nsresult rv = InitInfo();
nsresult rv = nsUConvPropertySearch::SearchPropertyValue(kOS2Charsets,
NS_ARRAY_LENGTH(kOS2Charsets), key, outCharset);
if (NS_FAILED(rv)) {
outCharset.AssignLiteral("IBM850");
return rv;
}
nsAutoString charset;
rv = gInfo->Get(inANSICodePage, charset);
if (NS_FAILED(rv)) {
outCharset.AssignLiteral("IBM850");
return rv;
}
LossyCopyUTF16toASCII(charset, outCharset);
return NS_OK;
return rv;
}
NS_IMETHODIMP

View File

@ -56,7 +56,6 @@ private:
nsCString mCharset;
nsString mLocale; // remember the locale & charset
nsresult InitInfo();
nsresult MapToCharset(short script, short region, nsACString& outCharset);
nsresult MapToCharset(nsAString& inANSICodePage, nsACString& outCharset);
nsresult InitGetCharset(nsACString& oString);

View File

@ -1,4 +1,3 @@
/* vim: set sts=2 sw=2 et cin: */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
@ -12,14 +11,15 @@
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is gre/res property file loading code.
* The Original Code is Property file to C++ array conversion code.
*
* The Initial Developer of the Original Code is
* Christian Biesinger <cbiesinger@web.de>.
* Portions created by the Initial Developer are Copyright (C) 2005
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -35,52 +35,33 @@
*
* ***** END LICENSE BLOCK ***** */
#include "nsGREResProperties.h"
#include "nsILocalFile.h"
#include "nsDirectoryServiceDefs.h"
#include "nsDirectoryServiceUtils.h"
#include "nsNetUtil.h"
#include "nsUConvPropertySearch.h"
#include "nsCRT.h"
nsGREResProperties::nsGREResProperties(const nsACString& aFile)
// static
nsresult
nsUConvPropertySearch::SearchPropertyValue(const char* aProperties[][3],
PRInt32 aNumberOfProperties,
const nsACString& aKey,
nsACString& aValue)
{
nsresult rv;
nsCOMPtr<nsIIOService> ioservice(do_GetIOService(&rv));
nsCOMPtr<nsIInputStream> inStr;
nsCOMPtr<nsIURI> uri;
nsCOMPtr<nsIChannel> channel;
if (NS_FAILED(rv))
return;
rv = ioservice->NewURI(NS_LITERAL_CSTRING("resource://gre-resources/") + aFile,
nsnull, nsnull, getter_AddRefs(uri));
if (NS_FAILED(rv))
return;
rv = NS_NewChannel(getter_AddRefs(channel), uri, ioservice);
if (NS_FAILED(rv))
return;
rv = channel->Open(getter_AddRefs(inStr));
if (NS_FAILED(rv))
return;
mProps = do_CreateInstance(NS_PERSISTENTPROPERTIES_CONTRACTID);
if (mProps) {
rv = mProps->Load(inStr);
if (NS_FAILED(rv))
mProps = nsnull;
const char* key = PromiseFlatCString(aKey).get();
PRInt32 lo = 0;
PRInt32 hi = aNumberOfProperties - 1;
while (lo <= hi) {
PRUint32 mid = (lo + hi) / 2;
PRInt32 comp = nsCRT::strcmp(aProperties[mid][0], key);
if (comp > 0) {
hi = mid - 1;
} else if (comp < 0) {
lo = mid + 1;
} else {
nsDependentCString val(aProperties[mid][1],
NS_PTR_TO_UINT32(aProperties[mid][2]));
aValue.Assign(val);
return NS_OK;
}
}
}
PRBool nsGREResProperties::DidLoad() const
{
return mProps != nsnull;
}
nsresult nsGREResProperties::Get(const nsAString& aKey, nsAString& aValue)
{
if (!mProps)
return NS_ERROR_NOT_INITIALIZED;
return mProps->GetStringProperty(NS_ConvertUTF16toUTF8(aKey), aValue);
aValue.Truncate();
return NS_ERROR_FAILURE;
}

View File

@ -1,4 +1,3 @@
/* vim: set sts=2 sw=2 et cin: */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
@ -12,14 +11,15 @@
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is gre/res property file loading code.
* The Original Code is Property file to C++ array conversion code.
*
* The Initial Developer of the Original Code is
* Christian Biesinger <cbiesinger@web.de>.
* Portions created by the Initial Developer are Copyright (C) 2005
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -35,32 +35,29 @@
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsGREResProperties_h__
#define nsGREResProperties_h__
#ifndef nsUConvPropertySearch_h_
#define nsUConvPropertySearch_h_
#include "nsString.h"
#include "nsIPersistentProperties2.h"
#include "nsCOMPtr.h"
/**
* This class loads a .properties file from the gre/res directory; the file
* to load is specified as a constructor argument.
*/
class nsGREResProperties {
public:
/**
* @param aFile The file to load. Must be an ASCII string.
*/
nsGREResProperties(const nsACString& aFile);
/**
* Returns whether loading the file succeeded.
*/
PRBool DidLoad() const;
nsresult Get(const nsAString& aKey, nsAString& value);
private:
nsCOMPtr<nsIPersistentProperties> mProps;
class nsUConvPropertySearch
{
public:
/**
* Looks up a property by value.
*
* @param aProperties
* the static property array
* @param aKey
* the key to look up
* @param aValue
* the return value (empty string if not found)
* @return NS_OK if found or NS_ERROR_FAILURE if not found
*/
static nsresult SearchPropertyValue(const char* aProperties[][3],
PRInt32 aNumberOfProperties,
const nsACString& aKey,
nsACString& aValue);
};
#endif // nsGREResProperties_h__
#endif /* nsUConvPropertySearch_h_ */

View File

@ -38,7 +38,7 @@
#include <locale.h>
#include "nsIPlatformCharset.h"
#include "pratom.h"
#include "nsGREResProperties.h"
#include "nsUConvPropertySearch.h"
#include "nsCOMPtr.h"
#include "nsReadableUtils.h"
#include "nsLocaleCID.h"
@ -63,66 +63,38 @@
#include "prinit.h"
#include "nsUnicharUtils.h"
static const char* kUnixCharsets[][3] = {
#include "unixcharset.properties.h"
};
NS_IMPL_THREADSAFE_ISUPPORTS1(nsPlatformCharset, nsIPlatformCharset)
static nsGREResProperties *gNLInfo = nsnull;
static nsGREResProperties *gInfo_deprecated = nsnull;
static PRInt32 gCnt=0;
//this lock is for protecting above static variable operation
static PRLock *gLock = nsnull;
static PRStatus InitLock(void)
{
gLock = PR_NewLock();
if (gLock)
return PR_SUCCESS;
return PR_FAILURE;
}
nsPlatformCharset::nsPlatformCharset()
{
PR_AtomicIncrement(&gCnt);
static PRCallOnceType once;
PR_CallOnce(&once, InitLock);
NS_ASSERTION(gLock, "Can't allocate a lock?!");
}
nsresult
nsPlatformCharset::ConvertLocaleToCharsetUsingDeprecatedConfig(nsAString& locale, nsACString& oResult)
{
// locked for thread safety
{
nsAutoLock guard(gLock);
if (!gInfo_deprecated) {
nsGREResProperties *info =
new nsGREResProperties(NS_LITERAL_CSTRING("unixcharset.properties"));
NS_ASSERTION(info, "cannot create nsGREResProperties");
gInfo_deprecated = info;
}
}
if (gInfo_deprecated && !(locale.IsEmpty())) {
nsAutoString platformLocaleKey;
if (!(locale.IsEmpty())) {
nsCAutoString platformLocaleKey;
// note: NS_LITERAL_STRING("locale." OSTYPE ".") does not compile on AIX
platformLocaleKey.AssignLiteral("locale.");
platformLocaleKey.AppendWithConversion(OSTYPE);
platformLocaleKey.Append(OSTYPE);
platformLocaleKey.AppendLiteral(".");
platformLocaleKey.Append(locale);
platformLocaleKey.AppendWithConversion(locale);
nsAutoString charset;
nsresult res = gInfo_deprecated->Get(platformLocaleKey, charset);
nsresult res = nsUConvPropertySearch::SearchPropertyValue(kUnixCharsets,
NS_ARRAY_LENGTH(kUnixCharsets), platformLocaleKey, oResult);
if (NS_SUCCEEDED(res)) {
LossyCopyUTF16toASCII(charset, oResult);
return NS_OK;
}
nsAutoString localeKey;
nsCAutoString localeKey;
localeKey.AssignLiteral("locale.all.");
localeKey.Append(locale);
res = gInfo_deprecated->Get(localeKey, charset);
localeKey.AppendWithConversion(locale);
res = nsUConvPropertySearch::SearchPropertyValue(kUnixCharsets,
NS_ARRAY_LENGTH(kUnixCharsets), localeKey, oResult);
if (NS_SUCCEEDED(res)) {
LossyCopyUTF16toASCII(charset, oResult);
return NS_OK;
}
}
@ -134,19 +106,6 @@ nsPlatformCharset::ConvertLocaleToCharsetUsingDeprecatedConfig(nsAString& locale
nsPlatformCharset::~nsPlatformCharset()
{
PR_AtomicDecrement(&gCnt);
if (!gCnt) {
if (gNLInfo) {
delete gNLInfo;
gNLInfo = nsnull;
PR_DestroyLock(gLock);
gLock = nsnull;
}
if (gInfo_deprecated) {
delete gInfo_deprecated;
gInfo_deprecated = nsnull;
}
}
}
NS_IMETHODIMP
@ -233,75 +192,6 @@ nsPlatformCharset::InitGetCharset(nsACString &oString)
}
}
// locked for thread safety
{
nsAutoLock guard(gLock);
if (!gNLInfo) {
nsCAutoString propertyFile;
// note: NS_LITERAL_CSTRING("unixcharset." OSARCH ".properties") does not compile on AIX
propertyFile.AssignLiteral("unixcharset.");
propertyFile.AppendLiteral(NS_STRINGIFY(OSARCH));
propertyFile.AppendLiteral(".properties");
nsGREResProperties *info = new nsGREResProperties(propertyFile);
NS_ASSERTION(info, "cannot create nsGREResProperties");
if (info) {
PRBool didLoad = info->DidLoad();
if (!didLoad) {
delete info;
info = nsnull;
}
}
gNLInfo = info;
}
}
//
// See if we are remapping nl_langinfo(CODESET)
//
if (gNLInfo && nl_langinfo_codeset) {
nsAutoString localeKey;
#if HAVE_GNU_GET_LIBC_VERSION
//
// look for an glibc version specific charset remap
//
const char *glibc_version = gnu_get_libc_version();
if ((glibc_version != nsnull) && (strlen(glibc_version))) {
localeKey.AssignLiteral("nllic.");
localeKey.AppendWithConversion(glibc_version);
localeKey.AppendLiteral(".");
localeKey.AppendWithConversion(nl_langinfo_codeset);
nsAutoString uCharset;
res = gNLInfo->Get(localeKey, uCharset);
if (NS_SUCCEEDED(res)) {
aCharset.AssignWithConversion(uCharset);
res = VerifyCharset(aCharset);
if (NS_SUCCEEDED(res)) {
oString = aCharset;
return res;
}
}
}
#endif
//
// look for a charset specific charset remap
//
localeKey.AssignLiteral("nllic.");
localeKey.AppendWithConversion(nl_langinfo_codeset);
nsAutoString uCharset;
res = gNLInfo->Get(localeKey, uCharset);
if (NS_SUCCEEDED(res)) {
aCharset.AssignWithConversion(uCharset);
res = VerifyCharset(aCharset);
if (NS_SUCCEEDED(res)) {
oString = aCharset;
return res;
}
}
}
NS_ERROR("unable to use nl_langinfo(CODESET)");
#endif
@ -400,9 +290,3 @@ nsPlatformCharset::VerifyCharset(nsCString &aCharset)
NS_ASSERTION(NS_SUCCEEDED(res), "failed to get preferred charset name, using non-preferred");
return NS_OK;
}
nsresult
nsPlatformCharset::InitInfo()
{
return NS_OK;
}

View File

@ -36,7 +36,7 @@
* ***** END LICENSE BLOCK ***** */
#include "nsIPlatformCharset.h"
#include "nsGREResProperties.h"
#include "nsUConvPropertySearch.h"
#include "pratom.h"
#include <windows.h>
#include "nsUConvDll.h"
@ -48,8 +48,9 @@
#include "nsITimelineService.h"
#include "nsPlatformCharset.h"
static nsGREResProperties *gInfo = nsnull;
static PRInt32 gCnt = 0;
static const char* kWinCharsets[][3] = {
#include "wincharset.properties.h"
};
NS_IMPL_ISUPPORTS1(nsPlatformCharset, nsIPlatformCharset)
@ -57,7 +58,6 @@ nsPlatformCharset::nsPlatformCharset()
{
NS_TIMELINE_START_TIMER("nsPlatformCharset()");
PR_AtomicIncrement(&gCnt);
nsAutoString acpKey(NS_LITERAL_STRING("acp."));
acpKey.AppendInt(PRInt32(::GetACP() & 0x00FFFF), 10);
MapToCharset(acpKey, mCharset);
@ -68,56 +68,20 @@ nsPlatformCharset::nsPlatformCharset()
nsPlatformCharset::~nsPlatformCharset()
{
PR_AtomicDecrement(&gCnt);
if ((0 == gCnt) && (nsnull != gInfo)) {
delete gInfo;
gInfo = nsnull;
}
}
nsresult
nsPlatformCharset::InitInfo()
{
if (gInfo == nsnull) {
nsGREResProperties *info = new nsGREResProperties(NS_LITERAL_CSTRING("wincharset.properties"));
NS_ASSERTION(info , "cannot open properties file");
NS_ENSURE_TRUE(info, NS_ERROR_FAILURE);
gInfo = info;
}
return NS_OK;
}
nsresult
nsPlatformCharset::MapToCharset(nsAString& inANSICodePage, nsACString& outCharset)
{
//delay loading wincharset.properties bundle if possible
if (inANSICodePage.EqualsLiteral("acp.1252")) {
outCharset.AssignLiteral("windows-1252");
return NS_OK;
}
nsCAutoString key;
LossyCopyUTF16toASCII(inANSICodePage, key);
if (inANSICodePage.EqualsLiteral("acp.932")) {
outCharset.AssignLiteral("Shift_JIS");
return NS_OK;
}
// ensure the .property file is loaded
nsresult rv = InitInfo();
nsresult rv = nsUConvPropertySearch::SearchPropertyValue(kWinCharsets,
NS_ARRAY_LENGTH(kWinCharsets), key, outCharset);
if (NS_FAILED(rv)) {
outCharset.AssignLiteral("windows-1252");
return rv;
}
nsAutoString charset;
rv = gInfo->Get(inANSICodePage, charset);
if (NS_FAILED(rv)) {
outCharset.AssignLiteral("windows-1252");
return rv;
}
LossyCopyUTF16toASCII(charset, outCharset);
return NS_OK;
return rv;
}
NS_IMETHODIMP

View File

@ -0,0 +1,72 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Property file to C++ array conversion code.
#
# The Initial Developer of the Original Code is
# Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2010
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Henri Sivonen <hsivonen@iki.fi>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
import sys
mappings = {}
propFile = open(sys.argv[1], "r");
for line in propFile:
line = line.strip()
if not line.startswith('#'):
parts = line.split("=", 1)
if len(parts) == 2 and len(parts[0]) > 0:
mappings[parts[0].strip()] = parts[1].strip()
propFile.close()
keys = mappings.keys()
keys.sort()
hFile = open(sys.argv[2], "w");
hFile.write("// This is a generated file. Please do not edit.\n")
hFile.write("// Please edit the corresponding .properties file instead.\n")
first = 1
for key in keys:
if first:
first = 0
else:
hFile.write(',\n')
hFile.write('{ "%s", "%s", (const char*)NS_INT32_TO_PTR(%d) }'
% (key, mappings[key], len(mappings[key])));
hFile.write('\n')
hFile.flush()
hFile.close()

View File

@ -1,82 +0,0 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1999
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
# This file (unixcharset.sample.properties) is a sample file for
# mapping between nl_langinfo(CODESET) and the charset name aliases
# that Mozilla uses. For Linux ($OSARCH=Linux) the file name would be
# unixcharset.Linux.properties
# These mappings are only needed if a nl_langinfo(CODESET)
# return value is in conflict with the Mozilla charset name
# aliases.
#
# The left hand side specifies a nl_langinfo(CODESET) return
# value to remap to right hand side Mozilla charset name alias.
#
# There are 2 ways to specify a mapping:
#
# 1) generic (all glib versions)
# 2) GLIBC version specific
#
# The generic form is:
#
# nllic.$NLLIC_CODESET=$MOZ_CODESET
#
# eg:
#
# nllic.KOI8=KOI8-R
#
# The GLIBC specific form is:
#
# nllic.$GLIBC.$NLLIC_CODESET=$MOZ_CODESET
#
# (nllic = nl_langinfo(CODESET))
# ( nl l i c )
#
# eg: for a glibc version 2.1.3:
#
# nllic.2.1.3.KOI8=KOI8-R
#
# If different glibc versions of nl_langinfo(CODESET) need
# different remappings these may coexist in this file as needed.
#
# (To determine a system's glibc version use: glib-config --version)
#
#
# Note:
# At present this is a sample file.
# The nl_langinfo(CODESET) return values on Linux can be used
# directly and thus this file is not needed.
#

View File

@ -24,6 +24,6 @@ function check_file(file) {
}
function run_test() {
for each(let file in ["charsetalias.properties", "charsetData.properties"])
for each(let file in ["charsetData.properties"])
check_file(file)
}