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 "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"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "nsIChannel.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIURI.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"
|
2018-11-04 23:12:41 +03:00
|
|
|
#include "nsErrorService.h"
|
2014-09-21 20:43:27 +04:00
|
|
|
#include "nsContentUtils.h"
|
2018-06-29 00:45:21 +03:00
|
|
|
#include "nsPersistentProperties.h"
|
|
|
|
#include "nsQueryObject.h"
|
2018-08-19 00:22:47 +03:00
|
|
|
#include "nsSimpleEnumerator.h"
|
2017-09-01 03:23:08 +03:00
|
|
|
#include "nsStringStream.h"
|
2018-06-29 00:45:21 +03:00
|
|
|
#include "mozilla/BinarySearch.h"
|
2020-09-22 20:13:27 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2017-09-01 03:23:08 +03:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
|
|
|
#include "mozilla/URLPreloader.h"
|
2018-06-29 00:45:21 +03:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
2018-06-30 08:53:12 +03:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2018-06-29 00:45:21 +03:00
|
|
|
#include "mozilla/dom/ipc/SharedStringMap.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;
|
|
|
|
|
2018-06-30 08:53:12 +03:00
|
|
|
using mozilla::dom::ContentParent;
|
|
|
|
using mozilla::dom::StringBundleDescriptor;
|
2018-06-29 00:45:21 +03:00
|
|
|
using mozilla::dom::ipc::SharedStringMap;
|
|
|
|
using mozilla::dom::ipc::SharedStringMapBuilder;
|
|
|
|
using mozilla::ipc::FileDescriptor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A set of string bundle URLs which are loaded by content processes, and
|
|
|
|
* should be allocated in a shared memory region, and then sent to content
|
|
|
|
* processes.
|
|
|
|
*
|
|
|
|
* Note: This layout is chosen to avoid having to create a separate char*
|
|
|
|
* array pointing to the string constant values, which would require
|
|
|
|
* per-process relocations. The second array size is the length of the longest
|
|
|
|
* URL plus its null terminator. Shorter strings are null padded to this
|
|
|
|
* length.
|
|
|
|
*
|
|
|
|
* This should be kept in sync with the similar array in nsContentUtils.cpp,
|
|
|
|
* and updated with any other property files which need to be loaded in all
|
|
|
|
* content processes.
|
|
|
|
*/
|
|
|
|
static const char kContentBundles[][52] = {
|
|
|
|
"chrome://branding/locale/brand.properties",
|
|
|
|
"chrome://global/locale/commonDialogs.properties",
|
|
|
|
"chrome://global/locale/css.properties",
|
|
|
|
"chrome://global/locale/dom/dom.properties",
|
|
|
|
"chrome://global/locale/layout/HtmlForm.properties",
|
|
|
|
"chrome://global/locale/layout/htmlparser.properties",
|
|
|
|
"chrome://global/locale/layout_errors.properties",
|
|
|
|
"chrome://global/locale/mathml/mathml.properties",
|
|
|
|
"chrome://global/locale/printing.properties",
|
|
|
|
"chrome://global/locale/security/csp.properties",
|
|
|
|
"chrome://global/locale/security/security.properties",
|
|
|
|
"chrome://global/locale/svg/svg.properties",
|
|
|
|
"chrome://global/locale/xul.properties",
|
|
|
|
"chrome://necko/locale/necko.properties",
|
|
|
|
};
|
1999-01-27 02:53:37 +03:00
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
static bool IsContentBundle(const nsCString& aUrl) {
|
|
|
|
size_t index;
|
|
|
|
return BinarySearchIf(
|
|
|
|
kContentBundles, 0, MOZ_ARRAY_LENGTH(kContentBundles),
|
2022-07-30 03:12:48 +03:00
|
|
|
[&](const char* aElem) {
|
|
|
|
return Compare(aUrl, nsDependentCString(aElem));
|
|
|
|
},
|
|
|
|
&index);
|
2018-06-29 00:45:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
#define STRINGBUNDLEPROXY_IID \
|
2018-11-30 13:46:48 +03:00
|
|
|
{ \
|
2018-06-29 00:45:21 +03:00
|
|
|
0x537cf21b, 0x99fc, 0x4002, { \
|
|
|
|
0x9e, 0xec, 0x97, 0xbe, 0x4d, 0xe0, 0xb3, 0xdc \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple proxy class for a string bundle instance which will be replaced by
|
|
|
|
* a different implementation later in the session.
|
|
|
|
*
|
|
|
|
* This is used when creating string bundles which should use shared memory,
|
|
|
|
* but the content process has not yet received their shared memory buffer.
|
|
|
|
* When the shared memory variant becomes available, this proxy is retarged to
|
|
|
|
* that instance, and the original non-shared instance is destroyed.
|
|
|
|
*
|
|
|
|
* At that point, the cache entry for the proxy is replaced with the shared
|
|
|
|
* memory instance, and callers which already have an instance of the proxy
|
|
|
|
* are redirected to the new instance.
|
|
|
|
*/
|
|
|
|
class StringBundleProxy : public nsIStringBundle {
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(STRINGBUNDLEPROXY_IID)
|
|
|
|
|
|
|
|
explicit StringBundleProxy(already_AddRefed<nsIStringBundle> aTarget)
|
2018-07-21 03:20:00 +03:00
|
|
|
: mMutex("StringBundleProxy::mMutex"), mTarget(aTarget) {}
|
2018-06-29 00:45:21 +03:00
|
|
|
|
|
|
|
void Retarget(nsIStringBundle* aTarget) {
|
2018-07-21 03:20:00 +03:00
|
|
|
MutexAutoLock automon(mMutex);
|
2018-06-29 00:45:21 +03:00
|
|
|
mTarget = aTarget;
|
|
|
|
}
|
|
|
|
|
2022-07-06 17:53:06 +03:00
|
|
|
// Forward nsIStringBundle methods (other than the `SizeOf*` methods) to
|
|
|
|
// `Target()`.
|
|
|
|
NS_IMETHOD GetStringFromID(int32_t aID, nsAString& _retval) override {
|
|
|
|
return Target()->GetStringFromID(aID, _retval);
|
|
|
|
}
|
|
|
|
NS_IMETHOD GetStringFromAUTF8Name(const nsACString& aName,
|
|
|
|
nsAString& _retval) override {
|
|
|
|
return Target()->GetStringFromAUTF8Name(aName, _retval);
|
|
|
|
}
|
|
|
|
NS_IMETHOD GetStringFromName(const char* aName, nsAString& _retval) override {
|
|
|
|
return Target()->GetStringFromName(aName, _retval);
|
|
|
|
}
|
|
|
|
NS_IMETHOD FormatStringFromID(int32_t aID, const nsTArray<nsString>& params,
|
|
|
|
nsAString& _retval) override {
|
|
|
|
return Target()->FormatStringFromID(aID, params, _retval);
|
|
|
|
}
|
|
|
|
NS_IMETHOD FormatStringFromAUTF8Name(const nsACString& aName,
|
|
|
|
const nsTArray<nsString>& params,
|
|
|
|
nsAString& _retval) override {
|
|
|
|
return Target()->FormatStringFromAUTF8Name(aName, params, _retval);
|
|
|
|
}
|
|
|
|
NS_IMETHOD FormatStringFromName(const char* aName,
|
|
|
|
const nsTArray<nsString>& params,
|
|
|
|
nsAString& _retval) override {
|
|
|
|
return Target()->FormatStringFromName(aName, params, _retval);
|
|
|
|
}
|
|
|
|
NS_IMETHOD GetSimpleEnumeration(nsISimpleEnumerator** _retval) override {
|
|
|
|
return Target()->GetSimpleEnumeration(_retval);
|
|
|
|
}
|
|
|
|
NS_IMETHOD AsyncPreload() override { return Target()->AsyncPreload(); }
|
|
|
|
|
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) override {
|
2018-06-25 01:47:10 +03:00
|
|
|
return aMallocSizeOf(this);
|
2018-06-29 00:45:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t SizeOfIncludingThisIfUnshared(
|
2022-07-06 17:53:06 +03:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) override {
|
2018-06-25 01:47:10 +03:00
|
|
|
return mRefCnt == 1 ? SizeOfIncludingThis(aMallocSizeOf) : 0;
|
2018-06-29 00:45:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~StringBundleProxy() = default;
|
|
|
|
|
|
|
|
private:
|
2022-03-16 21:47:08 +03:00
|
|
|
Mutex mMutex MOZ_UNANNOTATED;
|
2018-06-29 00:45:21 +03:00
|
|
|
nsCOMPtr<nsIStringBundle> mTarget;
|
|
|
|
|
|
|
|
// Atomically reads mTarget and returns a strong reference to it. This
|
|
|
|
// allows for safe multi-threaded use when the proxy may be retargetted by
|
|
|
|
// the main thread during access.
|
|
|
|
nsCOMPtr<nsIStringBundle> Target() {
|
2018-07-21 03:20:00 +03:00
|
|
|
MutexAutoLock automon(mMutex);
|
2018-06-29 00:45:21 +03:00
|
|
|
return mTarget;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(StringBundleProxy, STRINGBUNDLEPROXY_IID)
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(StringBundleProxy, nsIStringBundle, StringBundleProxy)
|
|
|
|
|
|
|
|
#define SHAREDSTRINGBUNDLE_IID \
|
2018-11-30 13:46:48 +03:00
|
|
|
{ \
|
2018-06-29 00:45:21 +03:00
|
|
|
0x7a8df5f7, 0x9e50, 0x44f6, { \
|
|
|
|
0xbf, 0x89, 0xc7, 0xad, 0x6c, 0x17, 0xf8, 0x5f \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A string bundle backed by a read-only, shared memory buffer. This should
|
|
|
|
* only be used for string bundles which are used in child processes.
|
|
|
|
*
|
|
|
|
* Important: The memory allocated by these string bundles will never be freed
|
|
|
|
* before process shutdown, per the restrictions in SharedStringMap.h, so they
|
|
|
|
* should never be used for short-lived bundles.
|
|
|
|
*/
|
|
|
|
class SharedStringBundle final : public nsStringBundleBase {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Initialize the string bundle with a file descriptor pointing to a
|
|
|
|
* pre-populated key-value store for this string bundle. This should only be
|
|
|
|
* called in child processes, for bundles initially created in the parent
|
|
|
|
* process.
|
|
|
|
*/
|
|
|
|
void SetMapFile(const FileDescriptor& aFile, size_t aSize);
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(SHAREDSTRINGBUNDLE_IID)
|
|
|
|
|
|
|
|
nsresult LoadProperties() override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a copy of the file descriptor pointing to the shared memory
|
|
|
|
* key-values tore for this string bundle. This should only be called in the
|
|
|
|
* parent process, and may be used to send shared string bundles to child
|
|
|
|
* processes.
|
|
|
|
*/
|
|
|
|
FileDescriptor CloneFileDescriptor() const {
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
if (mMapFile.isSome()) {
|
|
|
|
return mMapFile.ref();
|
|
|
|
}
|
|
|
|
return mStringMap->CloneFileDescriptor();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MapSize() const {
|
|
|
|
if (mMapFile.isSome()) {
|
|
|
|
return mMapSize;
|
|
|
|
}
|
2018-07-01 02:37:51 +03:00
|
|
|
if (mStringMap) {
|
|
|
|
return mStringMap->MapSize();
|
|
|
|
}
|
|
|
|
return 0;
|
2018-06-29 00:45:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Initialized() const { return mStringMap || mMapFile.isSome(); }
|
|
|
|
|
2018-06-30 08:53:12 +03:00
|
|
|
StringBundleDescriptor GetDescriptor() const {
|
|
|
|
MOZ_ASSERT(Initialized());
|
|
|
|
|
|
|
|
StringBundleDescriptor descriptor;
|
|
|
|
descriptor.bundleURL() = BundleURL();
|
|
|
|
descriptor.mapFile() = CloneFileDescriptor();
|
|
|
|
descriptor.mapSize() = MapSize();
|
|
|
|
return descriptor;
|
|
|
|
}
|
|
|
|
|
2022-07-06 17:53:06 +03:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) override;
|
2018-06-29 00:45:21 +03:00
|
|
|
|
|
|
|
static SharedStringBundle* Cast(nsIStringBundle* aStringBundle) {
|
|
|
|
return static_cast<SharedStringBundle*>(aStringBundle);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-06-25 01:47:10 +03:00
|
|
|
friend class nsStringBundleBase;
|
|
|
|
|
|
|
|
explicit SharedStringBundle(const char* aURLSpec)
|
|
|
|
: nsStringBundleBase(aURLSpec) {}
|
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
~SharedStringBundle() override = default;
|
|
|
|
|
|
|
|
nsresult GetStringImpl(const nsACString& aName, nsAString& aResult) override;
|
|
|
|
|
|
|
|
nsresult GetSimpleEnumerationImpl(nsISimpleEnumerator** elements) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<SharedStringMap> mStringMap;
|
|
|
|
|
|
|
|
Maybe<FileDescriptor> mMapFile;
|
|
|
|
size_t mMapSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(SharedStringBundle, SHAREDSTRINGBUNDLE_IID)
|
|
|
|
|
2018-08-19 00:22:47 +03:00
|
|
|
class StringMapEnumerator final : public nsSimpleEnumerator {
|
|
|
|
public:
|
2018-06-29 00:45:21 +03:00
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
|
|
|
|
|
|
|
explicit StringMapEnumerator(SharedStringMap* aStringMap)
|
|
|
|
: mStringMap(aStringMap) {}
|
|
|
|
|
2018-08-19 07:06:32 +03:00
|
|
|
const nsID& DefaultInterface() override {
|
|
|
|
return NS_GET_IID(nsIPropertyElement);
|
|
|
|
}
|
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
protected:
|
|
|
|
virtual ~StringMapEnumerator() = default;
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<SharedStringMap> mStringMap;
|
|
|
|
|
|
|
|
uint32_t mIndex = 0;
|
|
|
|
};
|
|
|
|
|
2018-06-25 01:47:10 +03:00
|
|
|
template <typename T, typename... Args>
|
|
|
|
already_AddRefed<T> MakeBundle(Args... args) {
|
|
|
|
return nsStringBundleBase::Create<T>(args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
RefPtr<T> MakeBundleRefPtr(Args... args) {
|
|
|
|
return nsStringBundleBase::Create<T>(args...);
|
|
|
|
}
|
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2018-06-25 01:47:10 +03:00
|
|
|
NS_IMPL_ISUPPORTS(nsStringBundleBase, nsIStringBundle, nsIMemoryReporter)
|
2018-06-29 00:45:21 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(nsStringBundle, nsStringBundleBase)
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(SharedStringBundle, nsStringBundleBase,
|
|
|
|
SharedStringBundle)
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-06-24 08:56:19 +03:00
|
|
|
nsStringBundleBase::nsStringBundleBase(const char* aURLSpec)
|
2001-11-02 01:46:39 +03:00
|
|
|
: mPropertiesURL(aURLSpec),
|
2018-07-21 03:20:00 +03:00
|
|
|
mMutex("nsStringBundle.mMutex"),
|
2011-10-17 18:59:28 +04:00
|
|
|
mAttemptedLoad(false),
|
|
|
|
mLoaded(false) {}
|
2001-05-04 00:21:18 +04:00
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
nsStringBundleBase::~nsStringBundleBase() {
|
2018-06-25 01:47:10 +03:00
|
|
|
UnregisterWeakMemoryReporter(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsStringBundleBase::RegisterMemoryReporter() {
|
|
|
|
RegisterWeakMemoryReporter(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
2019-02-26 01:08:21 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<T> nsStringBundleBase::Create(Args... args) {
|
2018-06-25 01:47:10 +03:00
|
|
|
RefPtr<T> bundle = new T(args...);
|
|
|
|
bundle->RegisterMemoryReporter();
|
|
|
|
return bundle.forget();
|
|
|
|
}
|
2018-06-29 00:45:21 +03:00
|
|
|
|
2018-06-24 08:56:19 +03:00
|
|
|
nsStringBundle::nsStringBundle(const char* aURLSpec)
|
|
|
|
: nsStringBundleBase(aURLSpec) {}
|
2018-06-29 00:45:21 +03:00
|
|
|
|
2020-03-04 12:11:10 +03:00
|
|
|
nsStringBundle::~nsStringBundle() = default;
|
2017-07-17 11:37:32 +03:00
|
|
|
|
2017-08-04 19:26:04 +03:00
|
|
|
NS_IMETHODIMP
|
2018-06-29 00:45:21 +03:00
|
|
|
nsStringBundleBase::AsyncPreload() {
|
2019-01-26 20:18:05 +03:00
|
|
|
return NS_DispatchToCurrentThreadQueue(
|
2018-06-29 00:45:21 +03:00
|
|
|
NewIdleRunnableMethod("nsStringBundleBase::LoadProperties", this,
|
2019-01-26 20:18:05 +03:00
|
|
|
&nsStringBundleBase::LoadProperties),
|
|
|
|
EventQueuePriority::Idle);
|
2017-08-04 19:26:04 +03:00
|
|
|
}
|
|
|
|
|
2018-06-25 01:47:10 +03:00
|
|
|
size_t nsStringBundle::SizeOfIncludingThis(
|
2022-07-06 17:53:06 +03:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) {
|
2018-05-18 23:16:31 +03:00
|
|
|
size_t n = 0;
|
|
|
|
if (mProps) {
|
|
|
|
n += mProps->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
return aMallocSizeOf(this) + n;
|
|
|
|
}
|
|
|
|
|
2022-07-06 17:53:06 +03:00
|
|
|
size_t nsStringBundleBase::SizeOfIncludingThis(
|
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-25 01:47:10 +03:00
|
|
|
size_t nsStringBundleBase::SizeOfIncludingThisIfUnshared(
|
2022-07-06 17:53:06 +03:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) {
|
2018-05-18 23:16:31 +03:00
|
|
|
if (mRefCnt == 1) {
|
|
|
|
return SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 01:47:10 +03:00
|
|
|
size_t SharedStringBundle::SizeOfIncludingThis(
|
2022-07-06 17:53:06 +03:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) {
|
2018-06-29 00:45:21 +03:00
|
|
|
size_t n = 0;
|
|
|
|
if (mStringMap) {
|
|
|
|
n += aMallocSizeOf(mStringMap);
|
|
|
|
}
|
|
|
|
return aMallocSizeOf(this) + n;
|
|
|
|
}
|
|
|
|
|
2018-06-25 01:47:10 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringBundleBase::CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, bool aAnonymize) {
|
|
|
|
// String bundle URLs are always local, and part of the distribution.
|
|
|
|
// There's no need to anonymize.
|
|
|
|
nsAutoCStringN<64> escapedURL(mPropertiesURL);
|
|
|
|
escapedURL.ReplaceChar('/', '\\');
|
|
|
|
|
|
|
|
size_t sharedSize = 0;
|
|
|
|
size_t heapSize = SizeOfIncludingThis(MallocSizeOf);
|
|
|
|
|
|
|
|
nsAutoCStringN<256> path("explicit/string-bundles/");
|
2018-07-01 02:37:51 +03:00
|
|
|
if (RefPtr<SharedStringBundle> shared = do_QueryObject(this)) {
|
|
|
|
path.AppendLiteral("SharedStringBundle");
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
sharedSize = shared->MapSize();
|
2018-06-25 01:47:10 +03:00
|
|
|
}
|
2018-07-01 02:37:51 +03:00
|
|
|
} else {
|
|
|
|
path.AppendLiteral("nsStringBundle");
|
2018-06-25 01:47:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
path.AppendLiteral("(url=\"");
|
|
|
|
path.Append(escapedURL);
|
|
|
|
|
|
|
|
// Note: The memory reporter service holds a strong reference to reporters
|
|
|
|
// while collecting reports, so we want to ignore the extra ref in reports.
|
|
|
|
path.AppendLiteral("\", shared=");
|
|
|
|
path.AppendASCII(mRefCnt > 2 ? "true" : "false");
|
|
|
|
path.AppendLiteral(", refCount=");
|
|
|
|
path.AppendInt(uint32_t(mRefCnt - 1));
|
|
|
|
|
|
|
|
if (sharedSize) {
|
|
|
|
path.AppendLiteral(", sharedMemorySize=");
|
|
|
|
path.AppendInt(uint32_t(sharedSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
path.AppendLiteral(")");
|
|
|
|
|
|
|
|
constexpr auto desc =
|
|
|
|
"A StringBundle instance representing the data in a (probably "
|
|
|
|
"localized) .properties file. Data may be shared between "
|
|
|
|
"processes."_ns;
|
|
|
|
|
2020-09-23 18:17:15 +03:00
|
|
|
aHandleReport->Callback(""_ns, path, KIND_HEAP, UNITS_BYTES, heapSize, desc,
|
|
|
|
aData);
|
2018-06-25 01:47:10 +03:00
|
|
|
|
|
|
|
if (sharedSize) {
|
|
|
|
path.ReplaceLiteral(0, sizeof("explicit/") - 1, "shared-");
|
|
|
|
|
2020-09-23 18:17:15 +03:00
|
|
|
aHandleReport->Callback(""_ns, path, KIND_OTHER, UNITS_BYTES, sharedSize,
|
|
|
|
desc, aData);
|
2018-06-25 01:47:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-06-29 00:45:21 +03:00
|
|
|
|
|
|
|
nsresult nsStringBundleBase::ParseProperties(nsIPersistentProperties** aProps) {
|
2001-05-04 00:21:18 +04:00
|
|
|
// 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
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"String bundles must be initialized on the main thread "
|
|
|
|
"before they may be used off-main-thread");
|
|
|
|
|
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;
|
|
|
|
|
2015-04-30 19:07:49 +03:00
|
|
|
// whitelist check for local schemes
|
|
|
|
nsCString scheme;
|
|
|
|
uri->GetScheme(scheme);
|
|
|
|
if (!scheme.EqualsLiteral("chrome") && !scheme.EqualsLiteral("jar") &&
|
|
|
|
!scheme.EqualsLiteral("resource") && !scheme.EqualsLiteral("file") &&
|
|
|
|
!scheme.EqualsLiteral("data")) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2017-09-01 03:23:08 +03:00
|
|
|
nsCOMPtr<nsIInputStream> in;
|
2014-09-21 20:43:27 +04:00
|
|
|
|
2017-09-01 03:23:08 +03:00
|
|
|
auto result = URLPreloader::ReadURI(uri);
|
|
|
|
if (result.isOk()) {
|
|
|
|
MOZ_TRY(NS_NewCStringInputStream(getter_AddRefs(in), result.unwrap()));
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel), uri,
|
|
|
|
nsContentUtils::GetSystemPrincipal(),
|
2020-07-15 14:20:45 +03:00
|
|
|
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
|
2017-09-01 03:23:08 +03:00
|
|
|
nsIContentPolicy::TYPE_OTHER);
|
2003-02-22 22:09:04 +03:00
|
|
|
|
2017-09-01 03:23:08 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2017-09-01 03:23:08 +03:00
|
|
|
// It's a string bundle. We expect a text/plain type, so set that as hint
|
|
|
|
channel->SetContentType("text/plain"_ns);
|
|
|
|
|
2019-02-12 19:08:25 +03:00
|
|
|
rv = channel->Open(getter_AddRefs(in));
|
2017-09-01 03:23:08 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
2003-02-22 22:09:04 +03:00
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
auto props = MakeRefPtr<nsPersistentProperties>();
|
2013-12-04 21:07:57 +04:00
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
mAttemptedLoad = true;
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
MOZ_TRY(props->Load(in));
|
|
|
|
props.forget(aProps);
|
2002-10-14 23:06:27 +04:00
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
mLoaded = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
nsresult nsStringBundle::LoadProperties() {
|
2020-10-28 07:38:50 +03:00
|
|
|
// Something such as Necko might use string bundle after ClearOnShutdown is
|
|
|
|
// called. LocaleService etc is already down, so we cannot get bundle data.
|
2021-02-27 17:38:14 +03:00
|
|
|
if (PastShutdownPhase(ShutdownPhase::XPCOMShutdown)) {
|
2020-10-28 07:38:50 +03:00
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
if (mProps) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return ParseProperties(getter_AddRefs(mProps));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult SharedStringBundle::LoadProperties() {
|
|
|
|
if (mStringMap) return NS_OK;
|
|
|
|
|
|
|
|
if (mMapFile.isSome()) {
|
|
|
|
mStringMap = new SharedStringMap(mMapFile.ref(), mMapSize);
|
|
|
|
mMapFile.reset();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-09-22 20:13:27 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"String bundles must be initialized on the main thread "
|
|
|
|
"before they may be used off-main-thread");
|
|
|
|
|
|
|
|
// We can't access the locale service after shutdown has started, which
|
|
|
|
// means we can't attempt to load chrome: locale resources (which most of
|
|
|
|
// our string bundles come from). Since shared string bundles won't be
|
|
|
|
// useful after shutdown has started anyway (and we almost certainly got
|
|
|
|
// here from a pre-load attempt in an idle task), just bail out.
|
2021-02-27 17:38:14 +03:00
|
|
|
if (PastShutdownPhase(ShutdownPhase::XPCOMShutdown)) {
|
2020-09-22 20:13:27 +03:00
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
|
|
|
|
}
|
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
// We should only populate shared memory string bundles in the parent
|
|
|
|
// process. Instances in the child process should always be instantiated
|
|
|
|
// with a shared memory file descriptor sent from the parent.
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPersistentProperties> props;
|
|
|
|
MOZ_TRY(ParseProperties(getter_AddRefs(props)));
|
|
|
|
|
|
|
|
SharedStringMapBuilder builder;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> iter;
|
|
|
|
MOZ_TRY(props->Enumerate(getter_AddRefs(iter)));
|
|
|
|
bool hasMore;
|
|
|
|
while (NS_SUCCEEDED(iter->HasMoreElements(&hasMore)) && hasMore) {
|
|
|
|
nsCOMPtr<nsISupports> next;
|
|
|
|
MOZ_TRY(iter->GetNext(getter_AddRefs(next)));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPropertyElement> elem = do_QueryInterface(next, &rv);
|
|
|
|
MOZ_TRY(rv);
|
|
|
|
|
|
|
|
nsCString key;
|
|
|
|
nsString value;
|
|
|
|
MOZ_TRY(elem->GetKey(key));
|
|
|
|
MOZ_TRY(elem->GetValue(value));
|
|
|
|
|
|
|
|
builder.Add(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
mStringMap = new SharedStringMap(std::move(builder));
|
2018-06-30 08:53:12 +03:00
|
|
|
|
|
|
|
ContentParent::BroadcastStringBundle(GetDescriptor());
|
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
return NS_OK;
|
2001-01-12 06:02:22 +03:00
|
|
|
}
|
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
void SharedStringBundle::SetMapFile(const FileDescriptor& aFile, size_t aSize) {
|
|
|
|
MOZ_ASSERT(XRE_IsContentProcess());
|
|
|
|
mStringMap = nullptr;
|
|
|
|
mMapFile.emplace(aFile);
|
|
|
|
mMapSize = aSize;
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:37:32 +03:00
|
|
|
NS_IMETHODIMP
|
2018-06-29 00:45:21 +03:00
|
|
|
nsStringBundleBase::GetStringFromID(int32_t aID, nsAString& aResult) {
|
2017-07-17 11:37:32 +03:00
|
|
|
nsAutoCString idStr;
|
|
|
|
idStr.AppendInt(aID, 10);
|
|
|
|
return GetStringFromName(idStr.get(), aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-06-29 00:45:21 +03:00
|
|
|
nsStringBundleBase::GetStringFromAUTF8Name(const nsACString& aName,
|
|
|
|
nsAString& aResult) {
|
2017-07-17 11:37:32 +03:00
|
|
|
return GetStringFromName(PromiseFlatCString(aName).get(), aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-06-29 00:45:21 +03:00
|
|
|
nsStringBundleBase::GetStringFromName(const char* aName, nsAString& aResult) {
|
2017-08-04 07:40:52 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aName);
|
2017-07-17 11:37:32 +03:00
|
|
|
|
2018-07-21 03:20:00 +03:00
|
|
|
MutexAutoLock autolock(mMutex);
|
2017-07-17 11:37:32 +03:00
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
return GetStringImpl(nsDependentCString(aName), aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsStringBundle::GetStringImpl(const nsACString& aName,
|
|
|
|
nsAString& aResult) {
|
|
|
|
MOZ_TRY(LoadProperties());
|
|
|
|
|
|
|
|
return mProps->GetStringProperty(aName, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult SharedStringBundle::GetStringImpl(const nsACString& aName,
|
|
|
|
nsAString& aResult) {
|
|
|
|
MOZ_TRY(LoadProperties());
|
|
|
|
|
|
|
|
if (mStringMap->Get(PromiseFlatCString(aName), aResult)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
2017-07-17 11:37:32 +03:00
|
|
|
}
|
|
|
|
|
2000-06-06 07:05:00 +04:00
|
|
|
NS_IMETHODIMP
|
2019-06-11 18:51:51 +03:00
|
|
|
nsStringBundleBase::FormatStringFromID(int32_t aID,
|
|
|
|
const nsTArray<nsString>& aParams,
|
|
|
|
nsAString& aResult) {
|
2017-07-12 08:13:37 +03:00
|
|
|
nsAutoCString idStr;
|
2000-06-06 07:05:00 +04:00
|
|
|
idStr.AppendInt(aID, 10);
|
2019-06-11 18:51:51 +03:00
|
|
|
return FormatStringFromName(idStr.get(), aParams, aResult);
|
2000-06-06 07:05:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// this function supports at most 10 parameters.. see below for why
|
|
|
|
NS_IMETHODIMP
|
2018-06-29 00:45:21 +03:00
|
|
|
nsStringBundleBase::FormatStringFromAUTF8Name(const nsACString& aName,
|
2019-06-11 18:51:51 +03:00
|
|
|
const nsTArray<nsString>& aParams,
|
2018-06-29 00:45:21 +03:00
|
|
|
nsAString& aResult) {
|
2019-06-11 18:51:51 +03:00
|
|
|
return FormatStringFromName(PromiseFlatCString(aName).get(), aParams,
|
2017-07-12 08:13:37 +03:00
|
|
|
aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this function supports at most 10 parameters.. see below for why
|
|
|
|
NS_IMETHODIMP
|
2018-06-29 00:45:21 +03:00
|
|
|
nsStringBundleBase::FormatStringFromName(const char* aName,
|
2019-06-11 18:51:51 +03:00
|
|
|
const nsTArray<nsString>& aParams,
|
|
|
|
nsAString& aResult) {
|
|
|
|
NS_ASSERTION(!aParams.IsEmpty(),
|
2017-07-17 11:36:16 +03:00
|
|
|
"FormatStringFromName() without format parameters: use "
|
|
|
|
"GetStringFromName() instead");
|
2004-11-11 00:36:38 +03:00
|
|
|
|
2000-06-06 07:05:00 +04:00
|
|
|
nsAutoString formatStr;
|
2017-08-04 07:40:52 +03:00
|
|
|
nsresult rv = GetStringFromName(aName, formatStr);
|
2000-07-25 09:45:56 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-06-06 07:05:00 +04:00
|
|
|
|
2019-06-11 18:51:51 +03:00
|
|
|
return FormatString(formatStr.get(), aParams, aResult);
|
2001-07-03 04:17:15 +04:00
|
|
|
}
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2000-05-02 09:08:39 +04:00
|
|
|
NS_IMETHODIMP
|
2018-06-29 00:45:21 +03:00
|
|
|
nsStringBundleBase::GetSimpleEnumeration(nsISimpleEnumerator** aElements) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aElements);
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
return GetSimpleEnumerationImpl(aElements);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsStringBundle::GetSimpleEnumerationImpl(
|
|
|
|
nsISimpleEnumerator** elements) {
|
|
|
|
MOZ_TRY(LoadProperties());
|
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
|
|
|
}
|
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
nsresult SharedStringBundle::GetSimpleEnumerationImpl(
|
|
|
|
nsISimpleEnumerator** aEnumerator) {
|
|
|
|
MOZ_TRY(LoadProperties());
|
|
|
|
|
|
|
|
auto iter = MakeRefPtr<StringMapEnumerator>(mStringMap);
|
|
|
|
iter.forget(aEnumerator);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StringMapEnumerator::HasMoreElements(bool* aHasMore) {
|
|
|
|
*aHasMore = mIndex < mStringMap->Count();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StringMapEnumerator::GetNext(nsISupports** aNext) {
|
|
|
|
if (mIndex >= mStringMap->Count()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto elem = MakeRefPtr<nsPropertyElement>(mStringMap->GetKeyAt(mIndex),
|
|
|
|
mStringMap->GetValueAt(mIndex));
|
|
|
|
|
|
|
|
elem.forget(aNext);
|
|
|
|
|
|
|
|
mIndex++;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsStringBundleBase::FormatString(const char16_t* aFormatStr,
|
2019-06-11 18:51:51 +03:00
|
|
|
const nsTArray<nsString>& aParams,
|
2018-06-29 00:45:21 +03:00
|
|
|
nsAString& aResult) {
|
2019-06-11 18:51:51 +03:00
|
|
|
auto length = aParams.Length();
|
|
|
|
NS_ENSURE_ARG(length <= 10); // enforce 10-parameter limit
|
2000-06-06 07:05:00 +04:00
|
|
|
|
|
|
|
// 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
|
2019-06-11 18:51:51 +03:00
|
|
|
nsTextFormatter::ssprintf(aResult, aFormatStr,
|
|
|
|
length >= 1 ? aParams[0].get() : nullptr,
|
|
|
|
length >= 2 ? aParams[1].get() : nullptr,
|
|
|
|
length >= 3 ? aParams[2].get() : nullptr,
|
|
|
|
length >= 4 ? aParams[3].get() : nullptr,
|
|
|
|
length >= 5 ? aParams[4].get() : nullptr,
|
|
|
|
length >= 6 ? aParams[5].get() : nullptr,
|
|
|
|
length >= 7 ? aParams[6].get() : nullptr,
|
|
|
|
length >= 8 ? aParams[7].get() : nullptr,
|
|
|
|
length >= 9 ? aParams[8].get() : nullptr,
|
|
|
|
length >= 10 ? aParams[9].get() : nullptr);
|
2017-08-04 07:40:52 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2000-06-06 07:05:00 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-02-20 14:40:14 +03:00
|
|
|
MOZ_COUNTED_DEFAULT_CTOR(bundleCacheEntry_t)
|
2014-04-22 03:34:07 +04:00
|
|
|
|
2020-02-20 14:40:14 +03:00
|
|
|
MOZ_COUNTED_DTOR(bundleCacheEntry_t)
|
2000-04-25 09:29:58 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
nsStringBundleService::nsStringBundleService()
|
2014-04-22 23:48:46 +04:00
|
|
|
: mBundleMap(MAX_CACHED_BUNDLES) {
|
2018-11-04 23:12:41 +03:00
|
|
|
mErrorService = nsErrorService::GetOrCreate();
|
|
|
|
MOZ_ALWAYS_TRUE(mErrorService);
|
1999-01-27 02:53:37 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsStringBundleService, nsIStringBundleService, nsIObserver,
|
2018-05-18 23:16:31 +03:00
|
|
|
nsISupportsWeakReference, nsIMemoryReporter)
|
2000-08-04 03:43:10 +04:00
|
|
|
|
1999-02-06 00:10:04 +03:00
|
|
|
nsStringBundleService::~nsStringBundleService() {
|
2018-05-18 23:16:31 +03:00
|
|
|
UnregisterWeakMemoryReporter(this);
|
2018-06-25 00:32:19 +03:00
|
|
|
flushBundleCache(/* ignoreShared = */ false);
|
2000-08-04 03:43:10 +04:00
|
|
|
}
|
|
|
|
|
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);
|
2017-04-18 11:03:05 +03:00
|
|
|
os->AddObserver(this, "intl:app-locales-changed", true);
|
2002-02-07 17:41:36 +03:00
|
|
|
}
|
2000-09-20 09:44:19 +04:00
|
|
|
|
2018-05-18 23:16:31 +03:00
|
|
|
RegisterWeakMemoryReporter(this);
|
|
|
|
|
2000-09-20 09:44:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-05-18 23:16:31 +03:00
|
|
|
size_t nsStringBundleService::SizeOfIncludingThis(
|
2022-07-06 17:53:06 +03:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) {
|
2018-05-18 23:16:31 +03:00
|
|
|
size_t n = mBundleMap.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& data : mBundleMap.Values()) {
|
|
|
|
n += aMallocSizeOf(data);
|
|
|
|
n += data->mHashKey.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
2018-05-18 23:16:31 +03:00
|
|
|
}
|
|
|
|
return aMallocSizeOf(this) + n;
|
|
|
|
}
|
|
|
|
|
2000-08-04 03:43:10 +04:00
|
|
|
NS_IMETHODIMP
|
2000-09-20 09:44:19 +04:00
|
|
|
nsStringBundleService::Observe(nsISupports* aSubject, const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aSomeData) {
|
2018-06-25 00:32:19 +03:00
|
|
|
if (strcmp("profile-do-change", aTopic) == 0 ||
|
2017-04-18 11:03:05 +03:00
|
|
|
strcmp("chrome-flush-caches", aTopic) == 0 ||
|
|
|
|
strcmp("intl:app-locales-changed", aTopic) == 0) {
|
2018-06-25 00:32:19 +03:00
|
|
|
flushBundleCache(/* ignoreShared = */ false);
|
|
|
|
} else if (strcmp("memory-pressure", aTopic) == 0) {
|
|
|
|
flushBundleCache(/* ignoreShared = */ true);
|
2007-08-28 23:48:47 +04:00
|
|
|
}
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2000-08-04 03:43:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-06-25 00:32:19 +03:00
|
|
|
void nsStringBundleService::flushBundleCache(bool ignoreShared) {
|
|
|
|
LinkedList<bundleCacheEntry_t> newList;
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2012-11-01 18:03:41 +04:00
|
|
|
while (!mBundleCache.isEmpty()) {
|
2018-06-25 00:32:19 +03:00
|
|
|
UniquePtr<bundleCacheEntry_t> entry(mBundleCache.popFirst());
|
|
|
|
auto* bundle = nsStringBundleBase::Cast(entry->mBundle);
|
2018-06-29 00:45:21 +03:00
|
|
|
|
2018-06-25 00:32:19 +03:00
|
|
|
if (ignoreShared && bundle->IsShared()) {
|
|
|
|
newList.insertBack(entry.release());
|
|
|
|
} else {
|
|
|
|
mBundleMap.Remove(entry->mHashKey);
|
|
|
|
}
|
2018-06-29 00:45:21 +03:00
|
|
|
}
|
2018-06-25 00:32:19 +03:00
|
|
|
|
|
|
|
mBundleCache = std::move(newList);
|
1999-01-27 02:53:37 +03:00
|
|
|
}
|
|
|
|
|
2000-08-04 03:43:10 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringBundleService::FlushBundles() {
|
2018-06-25 00:32:19 +03:00
|
|
|
flushBundleCache(/* ignoreShared = */ false);
|
2000-08-04 03:43:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-02-06 00:10:04 +03:00
|
|
|
|
2022-07-06 17:53:06 +03:00
|
|
|
void nsStringBundleService::SendContentBundles(ContentParent* aContentParent) {
|
2018-06-30 08:53:12 +03:00
|
|
|
nsTArray<StringBundleDescriptor> bundles;
|
|
|
|
|
|
|
|
for (auto* entry : mSharedBundles) {
|
|
|
|
auto bundle = SharedStringBundle::Cast(entry->mBundle);
|
|
|
|
|
|
|
|
if (bundle->Initialized()) {
|
|
|
|
bundles.AppendElement(bundle->GetDescriptor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Unused << aContentParent->SendRegisterStringBundles(std::move(bundles));
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsStringBundleService::RegisterContentBundle(
|
2022-07-06 17:53:06 +03:00
|
|
|
const nsACString& aBundleURL, const FileDescriptor& aMapFile,
|
2018-06-30 08:53:12 +03:00
|
|
|
size_t aMapSize) {
|
|
|
|
RefPtr<StringBundleProxy> proxy;
|
|
|
|
|
|
|
|
bundleCacheEntry_t* cacheEntry = mBundleMap.Get(aBundleURL);
|
|
|
|
if (cacheEntry) {
|
|
|
|
if (RefPtr<SharedStringBundle> shared =
|
|
|
|
do_QueryObject(cacheEntry->mBundle)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy = do_QueryObject(cacheEntry->mBundle);
|
|
|
|
MOZ_ASSERT(proxy);
|
|
|
|
cacheEntry->remove();
|
|
|
|
delete cacheEntry;
|
|
|
|
}
|
|
|
|
|
2022-07-06 17:53:06 +03:00
|
|
|
auto bundle = MakeBundleRefPtr<SharedStringBundle>(
|
|
|
|
PromiseFlatCString(aBundleURL).get());
|
2018-06-30 08:53:12 +03:00
|
|
|
bundle->SetMapFile(aMapFile, aMapSize);
|
|
|
|
|
|
|
|
if (proxy) {
|
|
|
|
proxy->Retarget(bundle);
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheEntry = insertIntoCache(bundle.forget(), aBundleURL);
|
|
|
|
mSharedBundles.insertBack(cacheEntry);
|
|
|
|
}
|
|
|
|
|
2000-04-25 09:29:58 +04:00
|
|
|
void 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
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
RefPtr<SharedStringBundle> shared;
|
|
|
|
|
2000-04-25 09:29:58 +04:00
|
|
|
if (cacheEntry) {
|
2018-06-25 00:32:19 +03:00
|
|
|
// Remove the entry from the list so it can be re-inserted at the back.
|
2012-11-01 18:03:41 +04:00
|
|
|
cacheEntry->remove();
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
shared = do_QueryObject(cacheEntry->mBundle);
|
2000-04-25 09:29:58 +04:00
|
|
|
} else {
|
2018-06-29 00:45:21 +03:00
|
|
|
nsCOMPtr<nsIStringBundle> bundle;
|
|
|
|
bool isContent = IsContentBundle(key);
|
|
|
|
if (!isContent || !XRE_IsParentProcess()) {
|
2018-06-25 01:47:10 +03:00
|
|
|
bundle = MakeBundle<nsStringBundle>(aURLSpec);
|
2018-06-29 00:45:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a bundle which is used by the content processes, we want to
|
|
|
|
// load it into a shared memory region.
|
|
|
|
//
|
|
|
|
// If we're in the parent process, just create a new SharedStringBundle,
|
|
|
|
// and populate it from the properties file.
|
|
|
|
//
|
|
|
|
// If we're in a child process, the fact that the bundle is not already in
|
|
|
|
// the cache means that we haven't received its shared memory descriptor
|
|
|
|
// from the parent yet. There's not much we can do about that besides
|
|
|
|
// wait, but we need to return a bundle now. So instead of a shared memory
|
|
|
|
// bundle, we create a temporary proxy, which points to a non-shared
|
|
|
|
// bundle initially, and is retarged to a shared memory bundle when it
|
|
|
|
// becomes available.
|
|
|
|
if (isContent) {
|
|
|
|
if (XRE_IsParentProcess()) {
|
2018-06-25 01:47:10 +03:00
|
|
|
shared = MakeBundle<SharedStringBundle>(aURLSpec);
|
2018-06-29 00:45:21 +03:00
|
|
|
bundle = shared;
|
|
|
|
} else {
|
|
|
|
bundle = new StringBundleProxy(bundle.forget());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 23:48:46 +04:00
|
|
|
cacheEntry = insertIntoCache(bundle.forget(), key);
|
2000-04-25 09:29:58 +04:00
|
|
|
}
|
|
|
|
|
2018-06-29 00:45:21 +03:00
|
|
|
if (shared) {
|
|
|
|
mSharedBundles.insertBack(cacheEntry);
|
|
|
|
} else {
|
2018-06-25 00:32:19 +03:00
|
|
|
mBundleCache.insertBack(cacheEntry);
|
2018-06-29 00:45:21 +03:00
|
|
|
}
|
2000-04-25 09:29:58 +04:00
|
|
|
|
|
|
|
// finally, return the value
|
|
|
|
*aResult = cacheEntry->mBundle;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
}
|
|
|
|
|
2018-06-25 00:32:19 +03:00
|
|
|
UniquePtr<bundleCacheEntry_t> nsStringBundleService::evictOneEntry() {
|
|
|
|
for (auto* entry : mBundleCache) {
|
|
|
|
auto* bundle = nsStringBundleBase::Cast(entry->mBundle);
|
|
|
|
if (!bundle->IsShared()) {
|
|
|
|
entry->remove();
|
|
|
|
mBundleMap.Remove(entry->mHashKey);
|
|
|
|
return UniquePtr<bundleCacheEntry_t>(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:41:57 +04:00
|
|
|
bundleCacheEntry_t* nsStringBundleService::insertIntoCache(
|
|
|
|
already_AddRefed<nsIStringBundle> aBundle, const nsACString& aHashKey) {
|
2018-06-25 00:32:19 +03:00
|
|
|
UniquePtr<bundleCacheEntry_t> cacheEntry;
|
2014-04-21 20:41:56 +04:00
|
|
|
|
2018-06-25 00:32:19 +03:00
|
|
|
if (mBundleMap.Count() >= MAX_CACHED_BUNDLES) {
|
|
|
|
cacheEntry = evictOneEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cacheEntry) {
|
|
|
|
cacheEntry.reset(new bundleCacheEntry_t());
|
2000-04-25 09:29:58 +04:00
|
|
|
}
|
2014-04-21 20:41:56 +04:00
|
|
|
|
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
|
|
|
|
2021-02-26 12:11:46 +03:00
|
|
|
mBundleMap.InsertOrUpdate(cacheEntry->mHashKey, cacheEntry.get());
|
2000-04-25 09:29:58 +04:00
|
|
|
|
2018-06-25 00:32:19 +03:00
|
|
|
return cacheEntry.release();
|
2000-04-25 09:29:58 +04:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-12-16 05:31:22 +03:00
|
|
|
getStringBundle(aURLSpec, aResult);
|
|
|
|
return NS_OK;
|
2001-01-12 06:02:22 +03:00
|
|
|
}
|
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
|
|
|
|
2019-06-11 18:51:51 +03:00
|
|
|
nsresult nsStringBundleService::FormatWithBundle(
|
|
|
|
nsIStringBundle* bundle, nsresult aStatus,
|
|
|
|
const nsTArray<nsString>& argArray, nsAString& result) {
|
2000-07-25 09:45:56 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
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);
|
2019-06-11 18:51:51 +03:00
|
|
|
rv = bundle->FormatStringFromID(code, argArray, 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)) {
|
2019-06-11 18:51:51 +03:00
|
|
|
AutoTArray<nsString, 1> otherArgArray;
|
|
|
|
otherArgArray.AppendElement()->AppendInt(static_cast<uint32_t>(aStatus),
|
|
|
|
16);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t code = NS_ERROR_GET_CODE(NS_ERROR_FAILURE);
|
2019-06-11 18:51:51 +03:00
|
|
|
rv = bundle->FormatStringFromID(code, otherArgArray, result);
|
2000-07-25 09:45:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringBundleService::FormatStatusMessage(nsresult aStatus,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aStatusArg,
|
2017-08-04 07:40:52 +03:00
|
|
|
nsAString& 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;
|
2017-08-21 13:01:27 +03:00
|
|
|
nsCString stringBundleURL;
|
2000-07-25 09:45:56 +04:00
|
|
|
|
|
|
|
// XXX hack for mailnews who has already formatted their messages:
|
|
|
|
if (aStatus == NS_OK && aStatusArg) {
|
2017-08-04 07:40:52 +03:00
|
|
|
result.Assign(aStatusArg);
|
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
|
2019-06-11 18:51:51 +03:00
|
|
|
AutoTArray<nsString, 10> argArray;
|
2000-07-25 09:45:56 +04:00
|
|
|
|
2019-06-11 18:51:51 +03:00
|
|
|
// convert the aStatusArg into an nsString array
|
2000-07-25 09:45:56 +04:00
|
|
|
if (argCount == 1) {
|
2019-06-11 18:51:51 +03:00
|
|
|
argArray.AppendElement(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);
|
2000-07-25 09:45:56 +04:00
|
|
|
if (pos == -1) pos = args.Length();
|
2019-06-11 18:51:51 +03:00
|
|
|
argArray.AppendElement(Substring(args, offset, pos - offset));
|
2000-07-25 09:45:56 +04:00
|
|
|
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)) {
|
2017-08-21 13:01:27 +03:00
|
|
|
getStringBundle(stringBundleURL.get(), getter_AddRefs(bundle));
|
2019-06-11 18:51:51 +03:00
|
|
|
rv = FormatWithBundle(bundle, aStatus, argArray, result);
|
2000-07-25 09:45:56 +04:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
2016-12-16 05:31:22 +03:00
|
|
|
getStringBundle(GLOBAL_PROPERTIES, getter_AddRefs(bundle));
|
2019-06-11 18:51:51 +03:00
|
|
|
rv = FormatWithBundle(bundle, aStatus, argArray, result);
|
2000-07-25 09:45:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|