From 49972af0172ce0abfa66fcf909f660ff649ff109 Mon Sep 17 00:00:00 2001 From: Zibi Braniecki Date: Thu, 6 Jun 2019 16:31:37 +0000 Subject: [PATCH] Bug 1552714 - Refactor constructors. r=smaug Differential Revision: https://phabricator.services.mozilla.com/D32956 --HG-- extra : moz-landing-system : lando --- dom/chrome-webidl/DOMLocalization.webidl | 9 +++++- dom/l10n/DOMLocalization.cpp | 39 +++++++++++++++--------- dom/l10n/DOMLocalization.h | 5 +-- intl/l10n/DocumentL10n.h | 2 +- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/dom/chrome-webidl/DOMLocalization.webidl b/dom/chrome-webidl/DOMLocalization.webidl index 0c9a285dbf86..429d9e07d9b2 100644 --- a/dom/chrome-webidl/DOMLocalization.webidl +++ b/dom/chrome-webidl/DOMLocalization.webidl @@ -52,7 +52,14 @@ dictionary L10nKey { * - translateRoots - translate all attached roots * */ -[ChromeOnly, Constructor] + +/** + * Constructor arguments: + * - aResourceids - a list of localization resource URIs + * which will provide messages for this + * Localization instance. + / +[ChromeOnly, Constructor(optional sequence aResourceIds)] interface DOMLocalization { /** * Localization API diff --git a/dom/l10n/DOMLocalization.cpp b/dom/l10n/DOMLocalization.cpp index 960e4e4fb43e..3d3cd92fed1e 100644 --- a/dom/l10n/DOMLocalization.cpp +++ b/dom/l10n/DOMLocalization.cpp @@ -50,19 +50,6 @@ NS_INTERFACE_MAP_END DOMLocalization::DOMLocalization(nsIGlobalObject* aGlobal) : mGlobal(aGlobal) { mMutations = new mozilla::dom::l10n::Mutations(this); } - -already_AddRefed DOMLocalization::Constructor( - const GlobalObject& aGlobal, ErrorResult& aRv) { - nsCOMPtr global = do_QueryInterface(aGlobal.GetAsSupports()); - if (!global) { - aRv.Throw(NS_ERROR_FAILURE); - return nullptr; - } - - RefPtr loc = new DOMLocalization(global); - return loc.forget(); -} - void DOMLocalization::Init(nsTArray& aResourceIds, ErrorResult& aRv) { nsCOMPtr jsm = do_ImportModule("resource://gre/modules/Localization.jsm"); @@ -88,6 +75,30 @@ void DOMLocalization::Init(nsTArray& aResourceIds, ErrorResult& aRv) { RegisterObservers(); } +already_AddRefed DOMLocalization::Constructor( + const GlobalObject& aGlobal, + const Optional>& aResourceIds, ErrorResult& aRv) { + nsCOMPtr global = do_QueryInterface(aGlobal.GetAsSupports()); + if (!global) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + + RefPtr loc = new DOMLocalization(global); + nsTArray resourceIds; + + if (aResourceIds.WasPassed()) { + resourceIds = aResourceIds.Value(); + } + + loc->Init(resourceIds, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + return loc.forget(); +} + nsIGlobalObject* DOMLocalization::GetParentObject() const { return mGlobal; } JSObject* DOMLocalization::WrapObject(JSContext* aCx, @@ -697,7 +708,7 @@ already_AddRefed DOMLocalization::MaybeWrapPromise( ErrorResult result; RefPtr docPromise = Promise::Create(mGlobal, result); - if (result.Failed()) { + if (NS_WARN_IF(result.Failed())) { return nullptr; } diff --git a/dom/l10n/DOMLocalization.h b/dom/l10n/DOMLocalization.h index 2c0765fde7f8..18d4410e6d29 100644 --- a/dom/l10n/DOMLocalization.h +++ b/dom/l10n/DOMLocalization.h @@ -29,10 +29,11 @@ class DOMLocalization : public nsIObserver, NS_DECL_NSIOBSERVER explicit DOMLocalization(nsIGlobalObject* aGlobal); + void Init(nsTArray& aResourceIds, ErrorResult& aRv); static already_AddRefed Constructor( - const GlobalObject& aGlobal, ErrorResult& aRv); - void Init(nsTArray& aResourceIds, ErrorResult& aRv); + const GlobalObject& aGlobal, + const Optional>& aResourceIds, ErrorResult& aRv); nsIGlobalObject* GetParentObject() const; diff --git a/intl/l10n/DocumentL10n.h b/intl/l10n/DocumentL10n.h index 0667c68d2f75..42b357d3d833 100644 --- a/intl/l10n/DocumentL10n.h +++ b/intl/l10n/DocumentL10n.h @@ -48,7 +48,7 @@ class DocumentL10n final : public l10n::DOMLocalization { explicit DocumentL10n(Document* aDocument); void Init(nsTArray& aResourceIds, ErrorResult& aRv); - protected: + protected: virtual ~DocumentL10n() = default; RefPtr mDocument;