Bug 1631593 - Move resourceIds to Localization C++. r=stas,jfkthame

Differential Revision: https://phabricator.services.mozilla.com/D71677
This commit is contained in:
Zibi Braniecki 2020-05-19 16:30:42 +00:00
Родитель e014d9fa76
Коммит 1b6968ba58
4 изменённых файлов: 50 добавлений и 73 удалений

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

@ -71,7 +71,7 @@ void Localization::Activate(const bool aSync, const bool aEager,
} }
mIsSync = aSync; mIsSync = aSync;
mLocalization->Activate(aSync, aEager, generateBundlesJS, mLocalization->Activate(mResourceIds, aSync, aEager, generateBundlesJS,
generateBundlesSyncJS); generateBundlesSyncJS);
RegisterObservers(); RegisterObservers();
@ -145,26 +145,23 @@ Localization::Observe(nsISupports* aSubject, const char* aTopic,
void Localization::OnChange() { void Localization::OnChange() {
if (mLocalization) { if (mLocalization) {
mLocalization->OnChange(); mLocalization->OnChange(mResourceIds);
} }
} }
uint32_t Localization::AddResourceId(const nsAString& aResourceId) { uint32_t Localization::AddResourceId(const nsAString& aResourceId) {
uint32_t ret = 0; if (!mResourceIds.Contains(aResourceId)) {
mLocalization->AddResourceId(aResourceId, &ret); mResourceIds.AppendElement(aResourceId);
return ret; OnChange();
}
return mResourceIds.Length();
} }
uint32_t Localization::RemoveResourceId(const nsAString& aResourceId) { uint32_t Localization::RemoveResourceId(const nsAString& aResourceId) {
// We need to guard against a scenario where the if (mResourceIds.RemoveElement(aResourceId)) {
// mLocalization has been unlinked, but the elements OnChange();
// are only now removed from DOM.
if (!mLocalization) {
return 0;
} }
uint32_t ret = 0; return mResourceIds.Length();
mLocalization->RemoveResourceId(aResourceId, &ret);
return ret;
} }
/** /**
@ -172,22 +169,33 @@ uint32_t Localization::RemoveResourceId(const nsAString& aResourceId) {
*/ */
uint32_t Localization::AddResourceIds(const nsTArray<nsString>& aResourceIds) { uint32_t Localization::AddResourceIds(const nsTArray<nsString>& aResourceIds) {
uint32_t ret = 0; bool added = false;
mLocalization->AddResourceIds(aResourceIds, &ret);
return ret; for (const auto& resId : aResourceIds) {
if (!mResourceIds.Contains(resId)) {
mResourceIds.AppendElement(resId);
added = true;
}
}
if (added) {
OnChange();
}
return mResourceIds.Length();
} }
uint32_t Localization::RemoveResourceIds( uint32_t Localization::RemoveResourceIds(
const nsTArray<nsString>& aResourceIds) { const nsTArray<nsString>& aResourceIds) {
// We need to guard against a scenario where the bool removed = false;
// mLocalization has been unlinked, but the elements
// are only now removed from DOM. for (const auto& resId : aResourceIds) {
if (!mLocalization) { if (mResourceIds.RemoveElement(resId)) {
return 0; removed = true;
}
} }
uint32_t ret = 0; if (removed) {
mLocalization->RemoveResourceIds(aResourceIds, &ret); OnChange();
return ret; }
return mResourceIds.Length();
} }
already_AddRefed<Promise> Localization::FormatValue( already_AddRefed<Promise> Localization::FormatValue(

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

@ -93,6 +93,7 @@ class Localization : public nsIObserver,
nsCOMPtr<nsIGlobalObject> mGlobal; nsCOMPtr<nsIGlobalObject> mGlobal;
nsCOMPtr<mozILocalization> mLocalization; nsCOMPtr<mozILocalization> mLocalization;
bool mIsSync; bool mIsSync;
nsTArray<nsString> mResourceIds;
}; };
} // namespace intl } // namespace intl

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

@ -226,6 +226,8 @@ class Localization {
/** /**
* Activate the instance of the `Localization` class. * Activate the instance of the `Localization` class.
* *
* @param {Array<String>} resourceIds - List of resource ids used by this
* localization.
* @param {bool} sync - Whether the instance should be * @param {bool} sync - Whether the instance should be
* synchronous. * synchronous.
* @param {bool} eager - Whether the initial bundles should be * @param {bool} eager - Whether the initial bundles should be
@ -233,14 +235,14 @@ class Localization {
* @param {Function} generateBundles - Custom FluentBundle asynchronous generator. * @param {Function} generateBundles - Custom FluentBundle asynchronous generator.
* @param {Function} generateBundlesSync - Custom FluentBundle generator. * @param {Function} generateBundlesSync - Custom FluentBundle generator.
*/ */
activate(sync, eager, generateBundles = defaultGenerateBundles, generateBundlesSync = defaultGenerateBundlesSync) { activate(resourceIds, sync, eager, generateBundles = defaultGenerateBundles, generateBundlesSync = defaultGenerateBundlesSync) {
if (this.bundles) { if (this.bundles) {
throw new Error("Attempt to initialize an already initialized instance."); throw new Error("Attempt to initialize an already initialized instance.");
} }
this.generateBundles = generateBundles; this.generateBundles = generateBundles;
this.generateBundlesSync = generateBundlesSync; this.generateBundlesSync = generateBundlesSync;
this.isSync = sync; this.isSync = sync;
this.regenerateBundles(eager); this.regenerateBundles(resourceIds, eager);
} }
setIsSync(isSync) { setIsSync(isSync) {
@ -255,42 +257,6 @@ class Localization {
} }
} }
/**
* @param {String} resourceId - Resource IDs
*/
addResourceId(resourceId) {
this.resourceIds.push(resourceId);
this.onChange();
return this.resourceIds.length;
}
/**
* @param {String} resourceId - Resource IDs
*/
removeResourceId(resourceId) {
this.resourceIds = this.resourceIds.filter(r => r !== resourceId);
this.onChange();
return this.resourceIds.length;
}
/**
* @param {Array<String>} resourceIds - List of resource IDs
*/
addResourceIds(resourceIds) {
this.resourceIds.push(...resourceIds);
this.onChange();
return this.resourceIds.length;
}
/**
* @param {Array<String>} resourceIds - List of resource IDs
*/
removeResourceIds(resourceIds) {
this.resourceIds = this.resourceIds.filter(r => !resourceIds.includes(r));
this.onChange();
return this.resourceIds.length;
}
/** /**
* Format translations and handle fallback if needed. * Format translations and handle fallback if needed.
* *
@ -494,9 +460,13 @@ class Localization {
return val; return val;
} }
onChange() { /**
* @param {Array<String>} resourceIds - List of resource ids used by this
* localization.
*/
onChange(resourceIds) {
if (this.bundles) { if (this.bundles) {
this.regenerateBundles(false); this.regenerateBundles(resourceIds, false);
} }
} }
@ -504,9 +474,13 @@ class Localization {
* This method should be called when there's a reason to believe * This method should be called when there's a reason to believe
* that language negotiation or available resources changed. * that language negotiation or available resources changed.
* *
* @param {Array<String>} resourceIds - List of resource ids used by this
* localization.
* @param {bool} eager - whether the I/O for new context should begin eagerly * @param {bool} eager - whether the I/O for new context should begin eagerly
*/ */
regenerateBundles(eager = false) { regenerateBundles(resourceIds, eager = false) {
// Store for error reporting from `formatWithFallback`.
this.resourceIds = resourceIds;
let generateMessages = this.isSync ? this.generateBundlesSync : this.generateBundles; let generateMessages = this.isSync ? this.generateBundlesSync : this.generateBundles;
this.bundles = this.cached(generateMessages(this.resourceIds)); this.bundles = this.cached(generateMessages(this.resourceIds));
if (eager) { if (eager) {

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

@ -14,16 +14,10 @@
[scriptable, uuid(7d468600-551f-4fe0-98c9-92a53b63ec8d)] [scriptable, uuid(7d468600-551f-4fe0-98c9-92a53b63ec8d)]
interface mozILocalization : nsISupports interface mozILocalization : nsISupports
{ {
void activate(in bool aSync, in bool aEager, in jsval aGenerateBundles, in jsval aGenerateBundlesSync); void activate(in Array<AString> aResourceIds, in bool aSync, in bool aEager, in jsval aGenerateBundles, in jsval aGenerateBundlesSync);
void setIsSync(in boolean isSync); void setIsSync(in boolean isSync);
unsigned long addResourceId(in AString resourceId);
unsigned long removeResourceId(in AString resourceId);
unsigned long addResourceIds(in Array<AString> resourceIds);
unsigned long removeResourceIds(in Array<AString> resourceIds);
Promise formatMessages(in Array<jsval> aKeys); Promise formatMessages(in Array<jsval> aKeys);
Promise formatValues(in Array<jsval> aKeys); Promise formatValues(in Array<jsval> aKeys);
Promise formatValue(in AUTF8String aId, [optional] in jsval aArgs); Promise formatValue(in AUTF8String aId, [optional] in jsval aArgs);
@ -32,7 +26,7 @@ interface mozILocalization : nsISupports
Array<AUTF8String> formatValuesSync(in Array<jsval> aKeys); Array<AUTF8String> formatValuesSync(in Array<jsval> aKeys);
Array<jsval> formatMessagesSync(in Array<jsval> aKeys); Array<jsval> formatMessagesSync(in Array<jsval> aKeys);
void onChange(); void onChange(in Array<AString> aResourceIds);
}; };
[scriptable, uuid(96632d26-1422-12e9-b1ce-9bb586acd241)] [scriptable, uuid(96632d26-1422-12e9-b1ce-9bb586acd241)]