fix 30213. make nsURLProperty file a singleton to improve performance

This commit is contained in:
ftang%netscape.com 2000-03-17 21:47:09 +00:00
Родитель 8cc3364af8
Коммит 0b8d285916
1 изменённых файлов: 18 добавлений и 13 удалений

Просмотреть файл

@ -33,6 +33,8 @@
NS_DEFINE_IID(kMacLocaleIID,NS_IMACLOCALE_IID); NS_DEFINE_IID(kMacLocaleIID,NS_IMACLOCALE_IID);
NS_DEFINE_CID(kMacLocaleFactoryCID,NS_MACLOCALEFACTORY_CID); NS_DEFINE_CID(kMacLocaleFactoryCID,NS_MACLOCALEFACTORY_CID);
static nsURLProperties *gInfo = nsnull;
static PRInt32 gCnt;
class nsMacCharset : public nsIPlatformCharset class nsMacCharset : public nsIPlatformCharset
{ {
@ -56,29 +58,36 @@ nsMacCharset::nsMacCharset()
{ {
NS_INIT_REFCNT(); NS_INIT_REFCNT();
PR_AtomicIncrement(&g_InstanceCount); PR_AtomicIncrement(&g_InstanceCount);
PR_AtomicIncrement(&gCnt);
// XXX we should make the following block critical section
if(gInfo == nsnull)
{
nsAutoString propertyURL("resource:/res/maccharset.properties"); nsAutoString propertyURL("resource:/res/maccharset.properties");
nsURLProperties *info = new nsURLProperties( propertyURL ); nsURLProperties *info = new nsURLProperties( propertyURL );
NS_ASSERTION(info , "cannot open properties file");
gInfo = info;
if( info ) { }
if( gInfo ) {
long ret = ::GetScriptManagerVariable(smRegionCode); long ret = ::GetScriptManagerVariable(smRegionCode);
PRInt32 reg = (PRInt32)(ret & 0x00FFFF); PRInt32 reg = (PRInt32)(ret & 0x00FFFF);
nsAutoString regionKey("region."); nsAutoString regionKey("region.");
regionKey.Append(reg, 10); regionKey.Append(reg, 10);
nsresult res = info->Get(regionKey, mCharset); nsresult res = gInfo->Get(regionKey, mCharset);
if(NS_FAILED(res)) { if(NS_FAILED(res)) {
ret = ::GetScriptManagerVariable(smSysScript); ret = ::GetScriptManagerVariable(smSysScript);
PRInt32 script = (PRInt32)(ret & 0x00FFFF); PRInt32 script = (PRInt32)(ret & 0x00FFFF);
nsAutoString scriptKey("script."); nsAutoString scriptKey("script.");
scriptKey.Append(script, 10); scriptKey.Append(script, 10);
nsresult res = info->Get(scriptKey, mCharset); nsresult res = gInfo->Get(scriptKey, mCharset);
if(NS_FAILED(res)) { if(NS_FAILED(res)) {
mCharset = "x-mac-roman"; mCharset = "x-mac-roman";
} }
} }
delete info;
} else { } else {
mCharset = "x-mac-roman"; mCharset = "x-mac-roman";
} }
@ -109,22 +118,18 @@ nsMacCharset::GetDefaultCharsetForLocale(const PRUnichar* localeName, PRUnichar*
rv = pMacLocale->GetPlatformLocale(&localeAsString,&script,&language,&region); rv = pMacLocale->GetPlatformLocale(&localeAsString,&script,&language,&region);
if (NS_FAILED(rv)) { *_retValue = charset.ToNewUnicode(); return rv; } if (NS_FAILED(rv)) { *_retValue = charset.ToNewUnicode(); return rv; }
nsAutoString property_url("resource:/res/maccharset.properties"); if (!gInfo) { *_retValue = charset.ToNewUnicode(); return NS_ERROR_OUT_OF_MEMORY; }
nsURLProperties *charset_properties = new nsURLProperties(property_url);
if (!charset_properties) { *_retValue = charset.ToNewUnicode(); return NS_ERROR_OUT_OF_MEMORY; }
nsAutoString locale_key("region."); nsAutoString locale_key("region.");
locale_key.Append(region,10); locale_key.Append(region,10);
rv = charset_properties->Get(locale_key,charset); rv = gInfo->Get(locale_key,charset);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
locale_key = "script."; locale_key = "script.";
locale_key.Append(script,10); locale_key.Append(script,10);
rv = charset_properties->Get(locale_key,charset); rv = gInfo->Get(locale_key,charset);
if (NS_FAILED(rv)) { charset="x-mac-roman";} if (NS_FAILED(rv)) { charset="x-mac-roman";}
} }
delete charset_properties;
*_retValue = charset.ToNewUnicode(); *_retValue = charset.ToNewUnicode();
return rv; return rv;
} }