Bug 1613705 - [localization] part4: Enable observer on Localization. r=emilio,nika

Depends on D111178

Differential Revision: https://phabricator.services.mozilla.com/D113247
This commit is contained in:
Zibi Braniecki 2021-08-02 09:54:08 +00:00
Родитель 2ea7cbbaf1
Коммит 6f3c026ec7
3 изменённых файлов: 39 добавлений и 6 удалений

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

@ -82,14 +82,15 @@ static FallibleTArray<Nullable<L10nMessage>> ConvertToL10nMessages(
return l10nMessages;
}
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Localization, mGlobal)
NS_IMPL_CYCLE_COLLECTING_ADDREF(Localization)
NS_IMPL_CYCLE_COLLECTING_RELEASE(Localization)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Localization)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WEAK(Localization, mGlobal)
/* static */
already_AddRefed<Localization> Localization::Create(
@ -100,12 +101,14 @@ already_AddRefed<Localization> Localization::Create(
Localization::Localization(const nsTArray<nsCString>& aResIds, bool aIsSync)
: mIsSync(aIsSync) {
ffi::localization_new(&aResIds, mIsSync, getter_AddRefs(mRaw));
RegisterObservers();
}
Localization::Localization(nsIGlobalObject* aGlobal,
const nsTArray<nsCString>& aResIds, bool aIsSync)
: mGlobal(aGlobal), mIsSync(aIsSync) {
ffi::localization_new(&aResIds, mIsSync, getter_AddRefs(mRaw));
RegisterObservers();
}
Localization::Localization(nsIGlobalObject* aGlobal,
@ -120,12 +123,14 @@ Localization::Localization(nsIGlobalObject* aGlobal, bool aIsSync)
: mGlobal(aGlobal), mIsSync(aIsSync) {
nsTArray<nsCString> resIds;
ffi::localization_new(&resIds, mIsSync, getter_AddRefs(mRaw));
RegisterObservers();
}
Localization::Localization(nsIGlobalObject* aGlobal)
: mGlobal(aGlobal), mIsSync(false) {
nsTArray<nsCString> resIds;
ffi::localization_new(&resIds, mIsSync, getter_AddRefs(mRaw));
RegisterObservers();
}
already_AddRefed<Localization> Localization::Constructor(
@ -167,7 +172,18 @@ Localization::Observe(nsISupports* aSubject, const char* aTopic,
return NS_OK;
}
void Localization::OnChange() {}
void Localization::RegisterObservers() {
DebugOnly<nsresult> rv = Preferences::AddWeakObservers(this, kObservedPrefs);
MOZ_ASSERT(NS_SUCCEEDED(rv), "Adding observers failed.");
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->AddObserver(this, INTL_APP_LOCALES_CHANGED, true);
}
}
void Localization::OnChange() { ffi::localization_on_change(mRaw.get()); }
void Localization::SetIsSync(bool aIsSync) {
MOZ_ASSERT(!aIsSync, "We should only move from sync to async!");

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

@ -9,6 +9,7 @@
#include "nsCycleCollectionParticipant.h"
#include "nsIObserver.h"
#include "nsWeakReference.h"
#include "nsWrapperCache.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/Promise.h"
@ -20,13 +21,16 @@
namespace mozilla {
namespace intl {
class Localization : public nsIObserver, public nsWrapperCache {
class Localization : public nsIObserver,
public nsWrapperCache,
public nsSupportsWeakReference {
template <typename T, typename... Args>
friend already_AddRefed<T> mozilla::MakeAndAddRef(Args&&... aArgs);
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Localization)
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Localization,
nsIObserver)
NS_DECL_NSIOBSERVER
static already_AddRefed<Localization> Constructor(
@ -82,6 +86,7 @@ class Localization : public nsIObserver, public nsWrapperCache {
Localization(nsIGlobalObject* aGlobal, bool aIsSync);
virtual ~Localization();
void RegisterObservers();
virtual void OnChange();
already_AddRefed<dom::Promise> MaybeWrapPromise(dom::Promise* aInnerPromise);

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

@ -168,6 +168,10 @@ impl LocalizationRc {
self.inner.borrow().is_sync()
}
pub fn on_change(&self) {
self.inner.borrow_mut().on_change();
}
pub fn format_value_sync(
&self,
id: &nsACString,
@ -328,7 +332,10 @@ impl LocalizationRc {
assert_eq!(keys.len(), ret_val.len());
let errors = errors.into_iter().map(|err| err.to_string().into()).collect();
let errors = errors
.into_iter()
.map(|err| err.to_string().into())
.collect();
callback(&strong_promise, &ret_val, &errors);
})
@ -519,3 +526,8 @@ pub extern "C" fn localization_format_messages(
pub extern "C" fn localization_upgrade(loc: &LocalizationRc) {
loc.upgrade();
}
#[no_mangle]
pub extern "C" fn localization_on_change(loc: &LocalizationRc) {
loc.on_change();
}