2001-09-26 04:40:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
1999-01-27 02:53:37 +03:00
|
|
|
|
2002-09-25 01:50:05 +04:00
|
|
|
#include "nsStringBundle.h"
|
1999-01-27 02:53:37 +03:00
|
|
|
#include "nsID.h"
|
2000-03-26 13:27:29 +04:00
|
|
|
#include "nsString.h"
|
1999-01-27 02:53:37 +03:00
|
|
|
#include "nsIStringBundle.h"
|
2002-10-14 23:06:27 +04:00
|
|
|
#include "nsStringBundleService.h"
|
|
|
|
#include "nsStringBundleTextOverride.h"
|
2002-07-09 04:07:09 +04:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2006-04-12 19:43:32 +04:00
|
|
|
#include "nsIMutableArray.h"
|
2002-10-14 23:06:27 +04:00
|
|
|
#include "nsArrayEnumerator.h"
|
1999-01-27 02:53:37 +03:00
|
|
|
#include "nscore.h"
|
2000-06-03 13:46:12 +04:00
|
|
|
#include "nsMemory.h"
|
1999-11-30 07:50:42 +03:00
|
|
|
#include "nsNetUtil.h"
|
2000-09-20 09:44:19 +04:00
|
|
|
#include "nsIObserverService.h"
|
2002-10-14 23:06:27 +04:00
|
|
|
#include "nsCOMArray.h"
|
2000-06-06 07:05:00 +04:00
|
|
|
#include "nsTextFormatter.h"
|
2000-07-25 09:45:56 +04:00
|
|
|
#include "nsIErrorService.h"
|
2002-07-09 04:07:09 +04:00
|
|
|
#include "nsICategoryManager.h"
|
2014-09-21 20:43:27 +04:00
|
|
|
#include "nsContentUtils.h"
|
2000-05-14 23:42:27 +04:00
|
|
|
|
2001-01-12 06:02:22 +03:00
|
|
|
// for async loading
|
2001-11-02 01:46:39 +03:00
|
|
|
#ifdef ASYNC_LOADING
|
2001-01-12 06:02:22 +03:00
|
|
|
#include "nsIBinaryInputStream.h"
|
|
|
|
#include "nsIStringStream.h"
|
2001-11-02 01:46:39 +03:00
|
|
|
#endif
|
2001-01-12 06:02:22 +03:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2000-07-25 09:45:56 +04:00
|
|
|
static NS_DEFINE_CID(kErrorServiceCID, NS_ERRORSERVICE_CID);
|
1999-01-27 02:53:37 +03:00
|
|
|
|
|
|
|
nsStringBundle::~nsStringBundle()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-10-14 23:06:27 +04:00
|
|
|
nsStringBundle::nsStringBundle(const char* aURLSpec,
|
|
|
|
nsIStringBundleOverride* aOverrideStrings) :
|
2001-11-02 01:46:39 +03:00
|
|
|
mPropertiesURL(aURLSpec),
|
2002-10-14 23:06:27 +04:00
|
|
|
mOverrideStrings(aOverrideStrings),
|
2011-04-29 23:21:57 +04:00
|
|
|
mReentrantMonitor("nsStringBundle.mReentrantMonitor"),
|
2011-10-17 18:59:28 +04:00
|
|
|
mAttemptedLoad(false),
|
|
|
|
mLoaded(false)
|
2001-01-12 06:02:22 +03:00
|
|
|
{
|
2001-05-04 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsStringBundle::LoadProperties()
|
|
|
|
{
|
|
|
|
// this is different than mLoaded, because we only want to attempt
|
|
|
|
// to load once
|
|
|
|
// we only want to load once, but if we've tried once and failed,
|
|
|
|
// continue to throw an error!
|
|
|
|
if (mAttemptedLoad) {
|
|
|
|
if (mLoaded)
|
|
|
|
return NS_OK;
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2005-01-30 21:09:34 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2001-05-04 00:21:18 +04:00
|
|
|
}
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mAttemptedLoad = true;
|
2001-01-12 06:02:22 +03:00
|
|
|
|
|
|
|
nsresult rv;
|
2001-11-02 01:46:39 +03:00
|
|
|
|
2002-10-14 23:06:27 +04:00
|
|
|
// do it synchronously
|
2001-01-12 06:02:22 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2002-03-06 10:48:55 +03:00
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), mPropertiesURL);
|
2001-01-12 06:02:22 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-02-22 22:09:04 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2014-09-21 20:43:27 +04:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
uri,
|
|
|
|
nsContentUtils::GetSystemPrincipal(),
|
|
|
|
nsILoadInfo::SEC_NORMAL,
|
|
|
|
nsIContentPolicy::TYPE_OTHER);
|
|
|
|
|
2003-02-22 22:09:04 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-09-14 01:20:35 +04:00
|
|
|
// It's a string bundle. We expect a text/plain type, so set that as hint
|
|
|
|
channel->SetContentType(NS_LITERAL_CSTRING("text/plain"));
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2002-10-14 23:06:27 +04:00
|
|
|
nsCOMPtr<nsIInputStream> in;
|
2003-02-22 22:09:04 +03:00
|
|
|
rv = channel->Open(getter_AddRefs(in));
|
2002-10-14 23:06:27 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2003-02-22 22:09:04 +03:00
|
|
|
|
2002-10-14 23:06:27 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv) && in, "Error in OpenBlockingStream");
|
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && in, NS_ERROR_FAILURE);
|
2013-12-04 21:07:57 +04:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID);
|
2001-05-04 00:21:18 +04:00
|
|
|
mProps = do_CreateInstance(kPersistentPropertiesCID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mAttemptedLoad = mLoaded = true;
|
2001-04-13 15:31:03 +04:00
|
|
|
rv = mProps->Load(in);
|
2002-10-14 23:06:27 +04:00
|
|
|
|
|
|
|
mLoaded = NS_SUCCEEDED(rv);
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2001-04-13 15:31:03 +04:00
|
|
|
return rv;
|
2001-01-12 06:02:22 +03:00
|
|
|
}
|
|
|
|
|
2001-11-02 01:46:39 +03:00
|
|
|
|
1999-07-22 08:38:02 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsStringBundle::GetStringFromID(int32_t aID, nsAString& aResult)
|
2014-04-21 20:41:56 +04:00
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter automon(mReentrantMonitor);
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString name;
|
2000-04-15 15:40:33 +04:00
|
|
|
name.AppendInt(aID, 10);
|
2002-10-14 23:06:27 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2002-10-14 23:06:27 +04:00
|
|
|
// try override first
|
|
|
|
if (mOverrideStrings) {
|
|
|
|
rv = mOverrideStrings->GetStringFromName(mPropertiesURL,
|
|
|
|
name,
|
|
|
|
aResult);
|
|
|
|
if (NS_SUCCEEDED(rv)) return rv;
|
|
|
|
}
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2002-10-14 23:06:27 +04:00
|
|
|
rv = mProps->GetStringProperty(name, aResult);
|
1999-01-27 02:53:37 +03:00
|
|
|
|
2001-05-04 00:21:18 +04:00
|
|
|
return rv;
|
1999-01-27 02:53:37 +03:00
|
|
|
}
|
|
|
|
|
1999-07-22 08:38:02 +04:00
|
|
|
nsresult
|
2002-03-24 00:42:00 +03:00
|
|
|
nsStringBundle::GetStringFromName(const nsAString& aName,
|
2001-11-02 01:46:39 +03:00
|
|
|
nsAString& aResult)
|
1999-01-27 02:53:37 +03:00
|
|
|
{
|
2001-05-04 00:21:18 +04:00
|
|
|
nsresult rv;
|
2001-01-12 06:02:22 +03:00
|
|
|
|
2002-10-14 23:06:27 +04:00
|
|
|
// try override first
|
|
|
|
if (mOverrideStrings) {
|
|
|
|
rv = mOverrideStrings->GetStringFromName(mPropertiesURL,
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_ConvertUTF16toUTF8(aName),
|
2002-10-14 23:06:27 +04:00
|
|
|
aResult);
|
|
|
|
if (NS_SUCCEEDED(rv)) return rv;
|
|
|
|
}
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2006-02-03 17:18:39 +03:00
|
|
|
rv = mProps->GetStringProperty(NS_ConvertUTF16toUTF8(aName), aResult);
|
2001-05-04 00:21:18 +04:00
|
|
|
return rv;
|
1999-07-22 08:38:02 +04:00
|
|
|
}
|
|
|
|
|
2000-06-06 07:05:00 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsStringBundle::FormatStringFromID(int32_t aID,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t **aParams,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aLength,
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t ** aResult)
|
2000-06-06 07:05:00 +04:00
|
|
|
{
|
|
|
|
nsAutoString idStr;
|
|
|
|
idStr.AppendInt(aID, 10);
|
|
|
|
|
2001-06-30 15:02:25 +04:00
|
|
|
return FormatStringFromName(idStr.get(), aParams, aLength, aResult);
|
2000-06-06 07:05:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// this function supports at most 10 parameters.. see below for why
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
nsStringBundle::FormatStringFromName(const char16_t *aName,
|
|
|
|
const char16_t **aParams,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aLength,
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t **aResult)
|
2000-06-06 07:05:00 +04:00
|
|
|
{
|
2004-11-11 00:36:38 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aName);
|
2004-12-05 00:10:36 +03:00
|
|
|
NS_ASSERTION(aParams && aLength, "FormatStringFromName() without format parameters: use GetStringFromName() instead");
|
2004-11-11 00:36:38 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
|
2000-08-04 03:43:10 +04:00
|
|
|
nsresult rv;
|
2001-05-04 00:21:18 +04:00
|
|
|
rv = LoadProperties();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2000-06-06 07:05:00 +04:00
|
|
|
nsAutoString formatStr;
|
2001-05-19 15:27:30 +04:00
|
|
|
rv = GetStringFromName(nsDependentString(aName), formatStr);
|
2000-07-25 09:45:56 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-06-06 07:05:00 +04:00
|
|
|
|
2001-07-03 05:28:13 +04:00
|
|
|
return FormatString(formatStr.get(), aParams, aLength, aResult);
|
2001-07-03 04:17:15 +04:00
|
|
|
}
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2001-07-03 04:17:15 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsStringBundle, nsIStringBundle)
|
1999-07-22 08:38:02 +04:00
|
|
|
|
|
|
|
/* void GetStringFromID (in long aID, out wstring aResult); */
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
nsStringBundle::GetStringFromID(int32_t aID, char16_t **aResult)
|
1999-07-22 08:38:02 +04:00
|
|
|
{
|
2001-05-04 00:21:18 +04:00
|
|
|
nsresult rv;
|
|
|
|
rv = LoadProperties();
|
2006-06-24 04:02:21 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2000-06-06 07:05:00 +04:00
|
|
|
nsAutoString tmpstr;
|
1999-07-22 08:38:02 +04:00
|
|
|
|
2005-01-30 21:09:34 +03:00
|
|
|
rv = GetStringFromID(aID, tmpstr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*aResult = ToNewUnicode(tmpstr);
|
|
|
|
NS_ENSURE_TRUE(*aResult, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-07-22 08:38:02 +04:00
|
|
|
}
|
|
|
|
|
2001-11-02 01:46:39 +03:00
|
|
|
/* void GetStringFromName (in wstring aName, out wstring aResult); */
|
2014-04-21 20:41:56 +04:00
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
nsStringBundle::GetStringFromName(const char16_t *aName, char16_t **aResult)
|
1999-07-22 08:38:02 +04:00
|
|
|
{
|
2004-11-11 00:36:38 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aName);
|
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
|
2001-05-04 00:21:18 +04:00
|
|
|
nsresult rv;
|
|
|
|
rv = LoadProperties();
|
2006-06-24 04:02:21 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-01-12 06:02:22 +03:00
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
ReentrantMonitorAutoEnter automon(mReentrantMonitor);
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2000-06-06 07:05:00 +04:00
|
|
|
nsAutoString tmpstr;
|
2001-11-02 01:46:39 +03:00
|
|
|
rv = GetStringFromName(nsDependentString(aName), tmpstr);
|
2005-03-21 09:06:55 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2005-06-20 22:38:46 +04:00
|
|
|
#if 0
|
|
|
|
// it is not uncommon for apps to request a string name which may not exist
|
2014-04-21 20:41:56 +04:00
|
|
|
// so be quiet about it.
|
2005-03-21 09:06:55 +03:00
|
|
|
NS_WARNING("String missing from string bundle");
|
2006-02-03 17:18:39 +03:00
|
|
|
printf(" '%s' missing from bundle %s\n", NS_ConvertUTF16toUTF8(aName).get(), mPropertiesURL.get());
|
2005-03-21 09:06:55 +03:00
|
|
|
#endif
|
|
|
|
return rv;
|
|
|
|
}
|
2005-01-30 21:09:34 +03:00
|
|
|
|
|
|
|
*aResult = ToNewUnicode(tmpstr);
|
|
|
|
NS_ENSURE_TRUE(*aResult, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-01-27 02:53:37 +03:00
|
|
|
}
|
|
|
|
|
2002-10-14 23:06:27 +04:00
|
|
|
nsresult
|
2002-11-05 10:02:07 +03:00
|
|
|
nsStringBundle::GetCombinedEnumeration(nsIStringBundleOverride* aOverrideStrings,
|
2002-10-14 23:06:27 +04:00
|
|
|
nsISimpleEnumerator** aResult)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
nsCOMPtr<nsIPropertyElement> propElement;
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2002-10-14 23:06:27 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2006-04-12 19:43:32 +04:00
|
|
|
nsCOMPtr<nsIMutableArray> resultArray =
|
|
|
|
do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
|
2005-01-30 21:09:34 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-10-14 23:06:27 +04:00
|
|
|
|
2002-11-05 10:02:07 +03:00
|
|
|
// first, append the override elements
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> overrideEnumerator;
|
|
|
|
rv = aOverrideStrings->EnumerateKeysInBundle(mPropertiesURL,
|
|
|
|
getter_AddRefs(overrideEnumerator));
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasMore;
|
2005-01-30 21:09:34 +03:00
|
|
|
rv = overrideEnumerator->HasMoreElements(&hasMore);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-11-05 10:02:07 +03:00
|
|
|
while (hasMore) {
|
2002-10-14 23:06:27 +04:00
|
|
|
|
2002-11-05 10:02:07 +03:00
|
|
|
rv = overrideEnumerator->GetNext(getter_AddRefs(supports));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
resultArray->AppendElement(supports, false);
|
2002-10-14 23:06:27 +04:00
|
|
|
|
2005-01-30 21:09:34 +03:00
|
|
|
rv = overrideEnumerator->HasMoreElements(&hasMore);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-11-05 10:02:07 +03:00
|
|
|
}
|
2002-10-14 23:06:27 +04:00
|
|
|
|
2002-11-05 10:02:07 +03:00
|
|
|
// ok, now we have the override elements in resultArray
|
2002-10-14 23:06:27 +04:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> propEnumerator;
|
|
|
|
rv = mProps->Enumerate(getter_AddRefs(propEnumerator));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// no elements in mProps anyway, just return what we have
|
|
|
|
return NS_NewArrayEnumerator(aResult, resultArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
// second, append all the elements that are in mProps
|
|
|
|
do {
|
|
|
|
rv = propEnumerator->GetNext(getter_AddRefs(supports));
|
|
|
|
if (NS_SUCCEEDED(rv) &&
|
2002-11-05 10:02:07 +03:00
|
|
|
(propElement = do_QueryInterface(supports, &rv))) {
|
|
|
|
|
|
|
|
// now check if its in the override bundle
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString key;
|
2002-11-05 10:02:07 +03:00
|
|
|
propElement->GetKey(key);
|
|
|
|
|
|
|
|
nsAutoString value;
|
|
|
|
rv = aOverrideStrings->GetStringFromName(mPropertiesURL, key, value);
|
2002-10-14 23:06:27 +04:00
|
|
|
|
2002-11-05 10:02:07 +03:00
|
|
|
// if it isn't there, then it is safe to append
|
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
resultArray->AppendElement(propElement, false);
|
2002-11-05 10:02:07 +03:00
|
|
|
}
|
2002-10-14 23:06:27 +04:00
|
|
|
|
2005-01-30 21:09:34 +03:00
|
|
|
rv = propEnumerator->HasMoreElements(&hasMore);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-10-14 23:06:27 +04:00
|
|
|
} while (hasMore);
|
|
|
|
|
2002-11-05 10:02:07 +03:00
|
|
|
return resultArray->Enumerate(aResult);
|
2002-10-14 23:06:27 +04:00
|
|
|
}
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2002-10-14 23:06:27 +04:00
|
|
|
|
2000-05-02 09:08:39 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringBundle::GetSimpleEnumeration(nsISimpleEnumerator** elements)
|
|
|
|
{
|
|
|
|
if (!elements)
|
|
|
|
return NS_ERROR_INVALID_POINTER;
|
|
|
|
|
2001-11-05 05:33:16 +03:00
|
|
|
nsresult rv;
|
|
|
|
rv = LoadProperties();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2002-11-05 10:02:07 +03:00
|
|
|
if (mOverrideStrings)
|
|
|
|
return GetCombinedEnumeration(mOverrideStrings, elements);
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2002-08-30 21:13:34 +04:00
|
|
|
return mProps->Enumerate(elements);
|
1999-06-11 06:22:34 +04:00
|
|
|
}
|
|
|
|
|
2000-06-06 07:05:00 +04:00
|
|
|
nsresult
|
2014-01-04 19:02:17 +04:00
|
|
|
nsStringBundle::FormatString(const char16_t *aFormatStr,
|
|
|
|
const char16_t **aParams, uint32_t aLength,
|
|
|
|
char16_t **aResult)
|
2000-06-06 07:05:00 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
NS_ENSURE_ARG(aLength <= 10); // enforce 10-parameter limit
|
|
|
|
|
|
|
|
// implementation note: you would think you could use vsmprintf
|
|
|
|
// to build up an arbitrary length array.. except that there
|
|
|
|
// is no way to build up a va_list at runtime!
|
|
|
|
// Don't believe me? See:
|
|
|
|
// http://www.eskimo.com/~scs/C-faq/q15.13.html
|
|
|
|
// -alecf
|
2014-04-21 20:41:56 +04:00
|
|
|
char16_t *text =
|
2000-06-06 07:05:00 +04:00
|
|
|
nsTextFormatter::smprintf(aFormatStr,
|
2012-07-30 18:20:58 +04:00
|
|
|
aLength >= 1 ? aParams[0] : nullptr,
|
|
|
|
aLength >= 2 ? aParams[1] : nullptr,
|
|
|
|
aLength >= 3 ? aParams[2] : nullptr,
|
|
|
|
aLength >= 4 ? aParams[3] : nullptr,
|
|
|
|
aLength >= 5 ? aParams[4] : nullptr,
|
|
|
|
aLength >= 6 ? aParams[5] : nullptr,
|
|
|
|
aLength >= 7 ? aParams[6] : nullptr,
|
|
|
|
aLength >= 8 ? aParams[7] : nullptr,
|
|
|
|
aLength >= 9 ? aParams[8] : nullptr,
|
|
|
|
aLength >= 10 ? aParams[9] : nullptr);
|
2007-07-01 23:29:43 +04:00
|
|
|
|
|
|
|
if (!text) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2007-07-01 23:29:43 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsTextFormatter does not use the shared nsMemory allocator.
|
|
|
|
// Instead it is required to free the memory it allocates using
|
|
|
|
// nsTextFormatter::smprintf_free. Let's instead use nsMemory based
|
|
|
|
// allocation for the result that we give out and free the string
|
|
|
|
// returned by smprintf ourselves!
|
|
|
|
*aResult = NS_strdup(text);
|
|
|
|
nsTextFormatter::smprintf_free(text);
|
|
|
|
|
|
|
|
return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
2000-06-06 07:05:00 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsExtensibleStringBundle, nsIStringBundle)
|
2000-01-07 00:54:52 +03:00
|
|
|
|
2001-04-13 15:31:03 +04:00
|
|
|
nsExtensibleStringBundle::nsExtensibleStringBundle()
|
2000-01-07 00:54:52 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mLoaded = false;
|
2001-04-13 15:31:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2002-07-09 04:07:09 +04:00
|
|
|
nsExtensibleStringBundle::Init(const char * aCategory,
|
2014-04-21 20:41:56 +04:00
|
|
|
nsIStringBundleService* aBundleService)
|
2001-04-13 15:31:03 +04:00
|
|
|
{
|
2000-01-14 02:18:35 +03:00
|
|
|
|
2002-07-09 04:07:09 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsICategoryManager> catman =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-01-07 00:54:52 +03:00
|
|
|
|
2002-07-09 04:07:09 +04:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
|
|
|
rv = catman->EnumerateCategory(aCategory, getter_AddRefs(enumerator));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-01-07 00:54:52 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasMore;
|
2002-07-09 04:07:09 +04:00
|
|
|
while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore) {
|
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
rv = enumerator->GetNext(getter_AddRefs(supports));
|
|
|
|
if (NS_FAILED(rv))
|
2001-04-13 15:31:03 +04:00
|
|
|
continue;
|
2000-01-07 00:54:52 +03:00
|
|
|
|
2002-08-06 04:53:19 +04:00
|
|
|
nsCOMPtr<nsISupportsCString> supStr = do_QueryInterface(supports, &rv);
|
2002-07-09 04:07:09 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
2000-01-07 00:54:52 +03:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString name;
|
2002-08-27 01:20:34 +04:00
|
|
|
rv = supStr->GetData(name);
|
2002-07-09 04:07:09 +04:00
|
|
|
if (NS_FAILED(rv))
|
2001-04-13 15:31:03 +04:00
|
|
|
continue;
|
2000-01-07 00:54:52 +03:00
|
|
|
|
2002-07-09 04:07:09 +04:00
|
|
|
nsCOMPtr<nsIStringBundle> bundle;
|
2002-08-27 01:20:34 +04:00
|
|
|
rv = aBundleService->CreateBundle(name.get(), getter_AddRefs(bundle));
|
2002-07-09 04:07:09 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
2000-01-07 00:54:52 +03:00
|
|
|
|
2002-10-14 23:06:27 +04:00
|
|
|
mBundles.AppendObject(bundle);
|
2000-01-07 00:54:52 +03:00
|
|
|
}
|
|
|
|
|
2002-07-09 04:07:09 +04:00
|
|
|
return rv;
|
2000-01-07 00:54:52 +03:00
|
|
|
}
|
|
|
|
|
2014-04-21 20:41:56 +04:00
|
|
|
nsExtensibleStringBundle::~nsExtensibleStringBundle()
|
2000-01-07 00:54:52 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
nsresult nsExtensibleStringBundle::GetStringFromID(int32_t aID, char16_t ** aResult)
|
2000-01-07 00:54:52 +03:00
|
|
|
{
|
2001-05-04 00:21:18 +04:00
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t size = mBundles.Count();
|
|
|
|
for (uint32_t i = 0; i < size; ++i) {
|
2002-10-14 23:06:27 +04:00
|
|
|
nsIStringBundle *bundle = mBundles[i];
|
|
|
|
if (bundle) {
|
2008-07-10 00:21:24 +04:00
|
|
|
rv = bundle->GetStringFromID(aID, aResult);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return NS_OK;
|
2000-08-24 05:02:58 +04:00
|
|
|
}
|
2000-01-07 00:54:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
nsresult nsExtensibleStringBundle::GetStringFromName(const char16_t *aName,
|
|
|
|
char16_t ** aResult)
|
2000-01-07 00:54:52 +03:00
|
|
|
{
|
2008-07-10 00:21:24 +04:00
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t size = mBundles.Count();
|
|
|
|
for (uint32_t i = 0; i < size; ++i) {
|
2002-10-14 23:06:27 +04:00
|
|
|
nsIStringBundle* bundle = mBundles[i];
|
|
|
|
if (bundle) {
|
2008-07-10 00:21:24 +04:00
|
|
|
rv = bundle->GetStringFromName(aName, aResult);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return NS_OK;
|
2000-08-24 05:02:58 +04:00
|
|
|
}
|
2000-01-07 00:54:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2000-06-06 07:05:00 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsExtensibleStringBundle::FormatStringFromID(int32_t aID,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t ** aParams,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aLength,
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t ** aResult)
|
2000-06-06 07:05:00 +04:00
|
|
|
{
|
|
|
|
nsAutoString idStr;
|
|
|
|
idStr.AppendInt(aID, 10);
|
2001-06-30 15:02:25 +04:00
|
|
|
return FormatStringFromName(idStr.get(), aParams, aLength, aResult);
|
2000-06-06 07:05:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
nsExtensibleStringBundle::FormatStringFromName(const char16_t *aName,
|
|
|
|
const char16_t ** aParams,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aLength,
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t ** aResult)
|
2000-06-06 07:05:00 +04:00
|
|
|
{
|
|
|
|
nsXPIDLString formatStr;
|
2009-04-02 21:12:38 +04:00
|
|
|
nsresult rv;
|
|
|
|
rv = GetStringFromName(aName, getter_Copies(formatStr));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2000-06-06 07:05:00 +04:00
|
|
|
|
|
|
|
return nsStringBundle::FormatString(formatStr, aParams, aLength, aResult);
|
|
|
|
}
|
|
|
|
|
2000-05-02 09:08:39 +04:00
|
|
|
nsresult nsExtensibleStringBundle::GetSimpleEnumeration(nsISimpleEnumerator ** aResult)
|
|
|
|
{
|
|
|
|
// XXX write me
|
2012-08-24 11:49:00 +04:00
|
|
|
*aResult = nullptr;
|
2000-05-02 09:08:39 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2000-01-07 00:54:52 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
1999-06-22 03:46:15 +04:00
|
|
|
|
2001-10-30 11:48:49 +03:00
|
|
|
#define MAX_CACHED_BUNDLES 16
|
2000-04-25 09:29:58 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
struct bundleCacheEntry_t final : public LinkedListElement<bundleCacheEntry_t> {
|
2014-04-22 23:48:46 +04:00
|
|
|
nsCString mHashKey;
|
2014-04-21 20:41:57 +04:00
|
|
|
nsCOMPtr<nsIStringBundle> mBundle;
|
2014-04-22 03:34:07 +04:00
|
|
|
|
|
|
|
bundleCacheEntry_t()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(bundleCacheEntry_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
~bundleCacheEntry_t()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(bundleCacheEntry_t);
|
|
|
|
}
|
2000-04-25 09:29:58 +04:00
|
|
|
};
|
|
|
|
|
1999-01-27 02:53:37 +03:00
|
|
|
|
2000-04-25 09:29:58 +04:00
|
|
|
nsStringBundleService::nsStringBundleService() :
|
2014-04-22 23:48:46 +04:00
|
|
|
mBundleMap(MAX_CACHED_BUNDLES)
|
1999-01-27 02:53:37 +03:00
|
|
|
{
|
2000-07-25 09:45:56 +04:00
|
|
|
mErrorService = do_GetService(kErrorServiceCID);
|
|
|
|
NS_ASSERTION(mErrorService, "Couldn't get error service");
|
1999-01-27 02:53:37 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsStringBundleService,
|
|
|
|
nsIStringBundleService,
|
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference)
|
2000-08-04 03:43:10 +04:00
|
|
|
|
1999-02-06 00:10:04 +03:00
|
|
|
nsStringBundleService::~nsStringBundleService()
|
1999-01-27 02:53:37 +03:00
|
|
|
{
|
2000-08-04 03:43:10 +04:00
|
|
|
flushBundleCache();
|
|
|
|
}
|
|
|
|
|
2000-09-20 09:44:19 +04:00
|
|
|
nsresult
|
|
|
|
nsStringBundleService::Init()
|
|
|
|
{
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
2002-02-07 17:41:36 +03:00
|
|
|
if (os) {
|
2011-10-17 18:59:28 +04:00
|
|
|
os->AddObserver(this, "memory-pressure", true);
|
|
|
|
os->AddObserver(this, "profile-do-change", true);
|
|
|
|
os->AddObserver(this, "chrome-flush-caches", true);
|
|
|
|
os->AddObserver(this, "xpcom-category-entry-added", true);
|
2002-02-07 17:41:36 +03:00
|
|
|
}
|
2000-09-20 09:44:19 +04:00
|
|
|
|
2002-10-14 23:06:27 +04:00
|
|
|
// instantiate the override service, if there is any.
|
|
|
|
// at some point we probably want to make this a category, and
|
|
|
|
// support multiple overrides
|
|
|
|
mOverrideStrings = do_GetService(NS_STRINGBUNDLETEXTOVERRIDE_CONTRACTID);
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2000-09-20 09:44:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-08-04 03:43:10 +04:00
|
|
|
NS_IMETHODIMP
|
2000-09-20 09:44:19 +04:00
|
|
|
nsStringBundleService::Observe(nsISupports* aSubject,
|
2001-10-20 00:52:59 +04:00
|
|
|
const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aSomeData)
|
2000-08-04 03:43:10 +04:00
|
|
|
{
|
2004-03-09 22:59:09 +03:00
|
|
|
if (strcmp("memory-pressure", aTopic) == 0 ||
|
|
|
|
strcmp("profile-do-change", aTopic) == 0 ||
|
|
|
|
strcmp("chrome-flush-caches", aTopic) == 0)
|
2007-08-28 23:48:47 +04:00
|
|
|
{
|
2000-09-20 09:44:19 +04:00
|
|
|
flushBundleCache();
|
2007-08-28 23:48:47 +04:00
|
|
|
}
|
|
|
|
else if (strcmp("xpcom-category-entry-added", aTopic) == 0 &&
|
2014-04-21 20:41:56 +04:00
|
|
|
NS_LITERAL_STRING("xpcom-autoregistration").Equals(aSomeData))
|
2007-08-28 23:48:47 +04:00
|
|
|
{
|
|
|
|
mOverrideStrings = do_GetService(NS_STRINGBUNDLETEXTOVERRIDE_CONTRACTID);
|
|
|
|
}
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2000-08-04 03:43:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsStringBundleService::flushBundleCache()
|
|
|
|
{
|
|
|
|
// release all bundles in the cache
|
2014-04-22 23:48:46 +04:00
|
|
|
mBundleMap.Clear();
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2012-11-01 18:03:41 +04:00
|
|
|
while (!mBundleCache.isEmpty()) {
|
2014-04-21 20:41:57 +04:00
|
|
|
delete mBundleCache.popFirst();
|
2000-04-25 09:29:58 +04:00
|
|
|
}
|
1999-01-27 02:53:37 +03:00
|
|
|
}
|
|
|
|
|
2000-08-04 03:43:10 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringBundleService::FlushBundles()
|
|
|
|
{
|
|
|
|
flushBundleCache();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-02-06 00:10:04 +03:00
|
|
|
|
2000-04-25 09:29:58 +04:00
|
|
|
nsresult
|
|
|
|
nsStringBundleService::getStringBundle(const char *aURLSpec,
|
|
|
|
nsIStringBundle **aResult)
|
|
|
|
{
|
2014-04-22 23:48:46 +04:00
|
|
|
nsDependentCString key(aURLSpec);
|
|
|
|
bundleCacheEntry_t* cacheEntry = mBundleMap.Get(key);
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2000-04-25 09:29:58 +04:00
|
|
|
if (cacheEntry) {
|
|
|
|
// cache hit!
|
|
|
|
// remove it from the list, it will later be reinserted
|
|
|
|
// at the head of the list
|
2012-11-01 18:03:41 +04:00
|
|
|
cacheEntry->remove();
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2000-04-25 09:29:58 +04:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// hasn't been cached, so insert it into the hash table
|
2014-04-21 20:41:57 +04:00
|
|
|
nsRefPtr<nsStringBundle> bundle = new nsStringBundle(aURLSpec, mOverrideStrings);
|
2014-04-22 23:48:46 +04:00
|
|
|
cacheEntry = insertIntoCache(bundle.forget(), key);
|
2000-04-25 09:29:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// at this point the cacheEntry should exist in the hashtable,
|
|
|
|
// but is not in the LRU cache.
|
|
|
|
// put the cache entry at the front of the list
|
2012-11-01 18:03:41 +04:00
|
|
|
mBundleCache.insertFront(cacheEntry);
|
2000-04-25 09:29:58 +04:00
|
|
|
|
|
|
|
// finally, return the value
|
|
|
|
*aResult = cacheEntry->mBundle;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bundleCacheEntry_t *
|
2014-04-21 20:41:57 +04:00
|
|
|
nsStringBundleService::insertIntoCache(already_AddRefed<nsIStringBundle> aBundle,
|
2014-04-22 23:48:46 +04:00
|
|
|
nsCString &aHashKey)
|
2000-04-25 09:29:58 +04:00
|
|
|
{
|
|
|
|
bundleCacheEntry_t *cacheEntry;
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2000-04-25 09:29:58 +04:00
|
|
|
if (mBundleMap.Count() < MAX_CACHED_BUNDLES) {
|
2000-05-14 23:42:27 +04:00
|
|
|
// cache not full - create a new entry
|
2014-04-21 20:41:56 +04:00
|
|
|
cacheEntry = new bundleCacheEntry_t();
|
2000-04-25 09:29:58 +04:00
|
|
|
} else {
|
2000-05-14 23:42:27 +04:00
|
|
|
// cache is full
|
2000-04-25 09:29:58 +04:00
|
|
|
// take the last entry in the list, and recycle it.
|
2012-11-01 18:03:41 +04:00
|
|
|
cacheEntry = mBundleCache.getLast();
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2000-04-25 09:29:58 +04:00
|
|
|
// remove it from the hash table and linked list
|
2014-04-22 23:48:46 +04:00
|
|
|
NS_ASSERTION(mBundleMap.Contains(cacheEntry->mHashKey),
|
2000-04-25 09:29:58 +04:00
|
|
|
"Element will not be removed!");
|
|
|
|
mBundleMap.Remove(cacheEntry->mHashKey);
|
2012-11-01 18:03:41 +04:00
|
|
|
cacheEntry->remove();
|
2000-04-25 09:29:58 +04:00
|
|
|
}
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2000-04-25 09:29:58 +04:00
|
|
|
// at this point we have a new cacheEntry that doesn't exist
|
|
|
|
// in the hashtable, so set up the cacheEntry
|
2014-04-22 23:48:46 +04:00
|
|
|
cacheEntry->mHashKey = aHashKey;
|
2014-04-21 20:41:57 +04:00
|
|
|
cacheEntry->mBundle = aBundle;
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2000-04-25 09:29:58 +04:00
|
|
|
// insert the entry into the cache and map, make it the MRU
|
|
|
|
mBundleMap.Put(cacheEntry->mHashKey, cacheEntry);
|
|
|
|
|
|
|
|
return cacheEntry;
|
|
|
|
}
|
|
|
|
|
1999-01-27 02:53:37 +03:00
|
|
|
NS_IMETHODIMP
|
2014-04-21 20:41:56 +04:00
|
|
|
nsStringBundleService::CreateBundle(const char* aURLSpec,
|
2001-05-04 00:21:18 +04:00
|
|
|
nsIStringBundle** aResult)
|
1999-01-27 02:53:37 +03:00
|
|
|
{
|
2001-11-02 01:46:39 +03:00
|
|
|
return getStringBundle(aURLSpec,aResult);
|
2001-01-12 06:02:22 +03:00
|
|
|
}
|
2008-07-10 00:21:24 +04:00
|
|
|
|
2000-01-07 00:54:52 +03:00
|
|
|
NS_IMETHODIMP
|
2008-07-10 00:21:24 +04:00
|
|
|
nsStringBundleService::CreateExtensibleBundle(const char* aCategory,
|
2000-01-07 00:54:52 +03:00
|
|
|
nsIStringBundle** aResult)
|
|
|
|
{
|
2012-08-24 11:49:00 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2014-07-09 00:46:24 +04:00
|
|
|
*aResult = nullptr;
|
2000-01-07 00:54:52 +03:00
|
|
|
|
2014-07-09 00:46:24 +04:00
|
|
|
nsRefPtr<nsExtensibleStringBundle> bundle = new nsExtensibleStringBundle();
|
2001-04-13 15:31:03 +04:00
|
|
|
|
2014-07-09 00:46:24 +04:00
|
|
|
nsresult res = bundle->Init(aCategory, this);
|
2000-01-07 00:54:52 +03:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-03-12 16:43:50 +03:00
|
|
|
bundle.forget(aResult);
|
|
|
|
return NS_OK;
|
2000-01-07 00:54:52 +03:00
|
|
|
}
|
|
|
|
|
2007-01-09 22:46:16 +03:00
|
|
|
#define GLOBAL_PROPERTIES "chrome://global/locale/global-strres.properties"
|
2000-07-25 09:45:56 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsStringBundleService::FormatWithBundle(nsIStringBundle* bundle, nsresult aStatus,
|
2014-01-04 19:02:17 +04:00
|
|
|
uint32_t argCount, char16_t** argArray,
|
|
|
|
char16_t* *result)
|
2000-07-25 09:45:56 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsXPIDLCString key;
|
|
|
|
|
2014-03-04 21:49:40 +04:00
|
|
|
// try looking up the error message with the int key:
|
|
|
|
uint16_t code = NS_ERROR_GET_CODE(aStatus);
|
|
|
|
rv = bundle->FormatStringFromID(code, (const char16_t**)argArray, argCount, result);
|
2000-07-25 09:45:56 +04:00
|
|
|
|
|
|
|
// If the int key fails, try looking up the default error message. E.g. print:
|
|
|
|
// An unknown error has occurred (0x804B0003).
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-08-09 12:36:40 +04:00
|
|
|
nsAutoString statusStr;
|
2012-08-22 19:56:38 +04:00
|
|
|
statusStr.AppendInt(static_cast<uint32_t>(aStatus), 16);
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* otherArgArray[1];
|
2001-06-30 15:02:25 +04:00
|
|
|
otherArgArray[0] = statusStr.get();
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t code = NS_ERROR_GET_CODE(NS_ERROR_FAILURE);
|
2000-07-25 09:45:56 +04:00
|
|
|
rv = bundle->FormatStringFromID(code, otherArgArray, 1, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringBundleService::FormatStatusMessage(nsresult aStatus,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aStatusArg,
|
|
|
|
char16_t* *result)
|
2000-07-25 09:45:56 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i, argCount = 0;
|
2000-07-25 09:45:56 +04:00
|
|
|
nsCOMPtr<nsIStringBundle> bundle;
|
|
|
|
nsXPIDLCString stringBundleURL;
|
|
|
|
|
|
|
|
// XXX hack for mailnews who has already formatted their messages:
|
|
|
|
if (aStatus == NS_OK && aStatusArg) {
|
2013-08-22 14:31:48 +04:00
|
|
|
*result = NS_strdup(aStatusArg);
|
2005-01-30 21:09:34 +03:00
|
|
|
NS_ENSURE_TRUE(*result, NS_ERROR_OUT_OF_MEMORY);
|
2000-07-25 09:45:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aStatus == NS_OK) {
|
|
|
|
return NS_ERROR_FAILURE; // no message to format
|
|
|
|
}
|
|
|
|
|
|
|
|
// format the arguments:
|
2002-07-27 04:46:21 +04:00
|
|
|
const nsDependentString args(aStatusArg);
|
2014-01-04 19:02:17 +04:00
|
|
|
argCount = args.CountChar(char16_t('\n')) + 1;
|
2000-07-25 09:45:56 +04:00
|
|
|
NS_ENSURE_ARG(argCount <= 10); // enforce 10-parameter limit
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* argArray[10];
|
2000-07-25 09:45:56 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
// convert the aStatusArg into a char16_t array
|
2000-07-25 09:45:56 +04:00
|
|
|
if (argCount == 1) {
|
|
|
|
// avoid construction for the simple case:
|
2014-01-04 19:02:17 +04:00
|
|
|
argArray[0] = (char16_t*)aStatusArg;
|
2000-07-25 09:45:56 +04:00
|
|
|
}
|
|
|
|
else if (argCount > 1) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t offset = 0;
|
2000-07-25 09:45:56 +04:00
|
|
|
for (i = 0; i < argCount; i++) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t pos = args.FindChar('\n', offset);
|
2014-04-21 20:41:56 +04:00
|
|
|
if (pos == -1)
|
2000-07-25 09:45:56 +04:00
|
|
|
pos = args.Length();
|
2003-07-09 09:08:25 +04:00
|
|
|
argArray[i] = ToNewUnicode(Substring(args, offset, pos - offset));
|
2012-07-30 18:20:58 +04:00
|
|
|
if (argArray[i] == nullptr) {
|
2000-07-25 09:45:56 +04:00
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
argCount = i - 1; // don't try to free uninitialized memory
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
offset = pos + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the string bundle for the error's module:
|
2014-04-21 20:41:56 +04:00
|
|
|
rv = mErrorService->GetErrorStringBundle(NS_ERROR_GET_MODULE(aStatus),
|
2000-07-25 09:45:56 +04:00
|
|
|
getter_Copies(stringBundleURL));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2001-11-02 01:46:39 +03:00
|
|
|
rv = getStringBundle(stringBundleURL, getter_AddRefs(bundle));
|
2000-07-25 09:45:56 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = FormatWithBundle(bundle, aStatus, argCount, argArray, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
2001-11-02 01:46:39 +03:00
|
|
|
rv = getStringBundle(GLOBAL_PROPERTIES, getter_AddRefs(bundle));
|
2000-07-25 09:45:56 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = FormatWithBundle(bundle, aStatus, argCount, argArray, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (argCount > 1) {
|
|
|
|
for (i = 0; i < argCount; i++) {
|
|
|
|
if (argArray[i])
|
2015-03-27 03:01:12 +03:00
|
|
|
free(argArray[i]);
|
2000-07-25 09:45:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|