1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsStringBundle_h__
7 #define nsStringBundle_h__
9 #include "mozilla/Mutex.h"
10 #include "nsIStringBundle.h"
11 #include "nsIMemoryReporter.h"
14 #include "nsCOMArray.h"
16 class nsIPersistentProperties
;
18 class nsStringBundleBase
: public nsIStringBundle
, public nsIMemoryReporter
{
20 MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf
)
22 nsresult
ParseProperties(nsIPersistentProperties
**);
24 NS_DECL_THREADSAFE_ISUPPORTS
25 NS_DECL_NSISTRINGBUNDLE
26 NS_DECL_NSIMEMORYREPORTER
28 virtual nsresult
LoadProperties() = 0;
30 const nsCString
& BundleURL() const { return mPropertiesURL
; }
32 // Returns true if this bundle has more than one reference. If it has only
33 // a single reference, it is assumed to be held alive by the bundle cache.
34 bool IsShared() const { return mRefCnt
> 1; }
36 static nsStringBundleBase
* Cast(nsIStringBundle
* aBundle
) {
37 return static_cast<nsStringBundleBase
*>(aBundle
);
40 template <typename T
, typename
... Args
>
41 static already_AddRefed
<T
> Create(Args
... args
);
44 nsStringBundleBase(const char* aURLSpec
);
46 virtual ~nsStringBundleBase();
48 virtual nsresult
GetStringImpl(const nsACString
& aName
,
49 nsAString
& aResult
) = 0;
51 virtual nsresult
GetSimpleEnumerationImpl(nsISimpleEnumerator
** elements
) = 0;
53 void RegisterMemoryReporter();
55 nsCString mPropertiesURL
;
56 mozilla::Mutex mMutex MOZ_UNANNOTATED
;
61 static nsresult
FormatString(const char16_t
* formatStr
,
62 const nsTArray
<nsString
>& aParams
,
66 class nsStringBundle
: public nsStringBundleBase
{
68 NS_DECL_ISUPPORTS_INHERITED
70 nsCOMPtr
<nsIPersistentProperties
> mProps
;
72 nsresult
LoadProperties() override
;
74 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) override
;
77 friend class nsStringBundleBase
;
79 explicit nsStringBundle(const char* aURLSpec
);
81 virtual ~nsStringBundle();
83 nsresult
GetStringImpl(const nsACString
& aName
, nsAString
& aResult
) override
;
85 nsresult
GetSimpleEnumerationImpl(nsISimpleEnumerator
** elements
) override
;