Skip to content

Commit

Permalink
Bug 1739143 - Capture DOM L10n initial translation and mutations reje…
Browse files Browse the repository at this point in the history
…ctions and report to console. r=nordzilla

Differential Revision: https://phabricator.services.mozilla.com/D130797
  • Loading branch information
Zibi Braniecki committed Nov 10, 2021
1 parent d593374 commit f596f45
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 48 deletions.
22 changes: 21 additions & 1 deletion dom/l10n/DocumentL10n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "mozilla/dom/DocumentL10nBinding.h"
#include "mozilla/Telemetry.h"

using namespace mozilla;
using namespace mozilla::intl;
using namespace mozilla::dom;

NS_IMPL_CYCLE_COLLECTION_CLASS(DocumentL10n)
Expand Down Expand Up @@ -74,7 +76,25 @@ class L10nReadyHandler final : public PromiseNativeHandler {

void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
mDocumentL10n->InitialTranslationCompleted(false);
mPromise->MaybeRejectWithUndefined();

nsTArray<nsCString> errors{
"[dom/l10n] Could not complete initial document translation."_ns,
};
IgnoredErrorResult rv;
MaybeReportErrorsToGecko(errors, rv, mDocumentL10n->GetParentObject());

/**
* We resolve the mReady here even if we encountered failures, because
* there is nothing actionable for the user pending on `mReady` to do here
* and we don't want to incentivized consumers of this API to plan the
* same pending operation for resolve and reject scenario.
*
* Additionally, without it, the stderr received "uncaught promise
* rejection" warning, which is noisy and not-actionable.
*
* So instead, we just resolve and report errors.
*/
mPromise->MaybeResolveWithUndefined();
}

private:
Expand Down
45 changes: 45 additions & 0 deletions dom/l10n/L10nMutations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#include "L10nMutations.h"
#include "mozilla/dom/DocumentInlines.h"
#include "nsRefreshDriver.h"
#include "mozilla/intl/Localization.h"

using namespace mozilla;
using namespace mozilla::intl;
using namespace mozilla::dom;

NS_IMPL_CYCLE_COLLECTION_CLASS(L10nMutations)
Expand Down Expand Up @@ -124,6 +127,44 @@ void L10nMutations::WillRefresh(mozilla::TimeStamp aTime) {
FlushPendingTranslations();
}

/**
* The handler for the `TranslateElements` promise used to turn
* a potential rejection into a console warning.
**/
class L10nMutationFinalizationHandler final : public PromiseNativeHandler {
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(L10nMutationFinalizationHandler)

explicit L10nMutationFinalizationHandler(nsIGlobalObject* aGlobal)
: mGlobal(aGlobal) {}

void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
}

void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
nsTArray<nsCString> errors{
"[dom/l10n] Errors during l10n mutation frame."_ns,
};
IgnoredErrorResult rv;
MaybeReportErrorsToGecko(errors, rv, mGlobal);
}

private:
~L10nMutationFinalizationHandler() = default;

nsCOMPtr<nsIGlobalObject> mGlobal;
};

NS_IMPL_CYCLE_COLLECTION(L10nMutationFinalizationHandler, mGlobal)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(L10nMutationFinalizationHandler)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(L10nMutationFinalizationHandler)
NS_IMPL_CYCLE_COLLECTING_RELEASE(L10nMutationFinalizationHandler)

void L10nMutations::FlushPendingTranslations() {
if (!mDOMLocalization) {
return;
Expand All @@ -147,6 +188,10 @@ void L10nMutations::FlushPendingTranslations() {
mPendingElements.Clear();

RefPtr<Promise> promise = mDOMLocalization->TranslateElements(elements, rv);

RefPtr<PromiseNativeHandler> l10nMutationFinalizationHandler =
new L10nMutationFinalizationHandler(mDOMLocalization->GetParentObject());
promise->AppendNativeHandler(l10nMutationFinalizationHandler);
}

void L10nMutations::Disconnect() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
SimpleTest.waitForExplicitFinish();

document.addEventListener("DOMContentLoaded", async function() {
/**
* Even when we fail to localize all elements, we will
* still resolve the `ready` promise to communicate that
* the initial translation phase is now completed.
*/
document.l10n.ready.then(() => {
is(1, 2, "the ready should not resolve");
SimpleTest.finish();
}, (err) => {
is(1, 1, "the ready should reject");
is(1, 1, "the ready should resolve");
SimpleTest.finish();
});
});
Expand Down
43 changes: 0 additions & 43 deletions intl/l10n/Localization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Localization.h"
#include "nsContentUtils.h"
#include "nsIObserverService.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/Preferences.h"
Expand All @@ -21,48 +20,6 @@ using namespace mozilla::intl;

static const char* kObservedPrefs[] = {L10N_PSEUDO_PREF, nullptr};

// The state where the application contains incomplete localization resources
// is much more common than for other types of core resources.
//
// In result, we our localization is designed to handle missing resources
// gracefully, and we need a more fine-tuned way to communicate those problems
// to developers.
//
// In particular, we want developers and early adopters to be able to reason
// about missing translations, without bothering end user in production, where
// the user cannot react to that.
//
// We currently differentiate between nightly/dev-edition builds or automation
// where we report the errors, and beta/release, where we silence them.
static bool MaybeReportErrorsToGecko(const nsTArray<nsCString>& aErrors,
ErrorResult& aRv,
nsIGlobalObject* global) {
if (!aErrors.IsEmpty()) {
if (xpc::IsInAutomation()) {
aRv.ThrowInvalidStateError(aErrors.ElementAt(0));
return true;
}

#if defined(NIGHTLY_BUILD) || defined(MOZ_DEV_EDITION) || defined(DEBUG)
Document* doc = nullptr;
if (global) {
nsPIDOMWindowInner* innerWindow = global->AsInnerWindow();
if (innerWindow) {
doc = innerWindow->GetExtantDoc();
}
}

for (const auto& error : aErrors) {
nsContentUtils::ReportToConsoleNonLocalized(NS_ConvertUTF8toUTF16(error),
nsIScriptError::warningFlag,
"l10n"_ns, doc);
}
#endif
}

return false;
}

static nsTArray<ffi::L10nKey> ConvertFromL10nKeys(
const Sequence<OwningUTF8StringOrL10nIdArgs>& aKeys) {
nsTArray<ffi::L10nKey> l10nKeys(aKeys.Length());
Expand Down
46 changes: 46 additions & 0 deletions intl/l10n/Localization.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "nsWeakReference.h"
#include "nsWrapperCache.h"
#include "nsWeakReference.h"
#include "nsIScriptError.h"
#include "nsContentUtils.h"
#include "nsPIDOMWindow.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/BindingDeclarations.h"
Expand All @@ -22,6 +25,49 @@
namespace mozilla {
namespace intl {

// The state where the application contains incomplete localization resources
// is much more common than for other types of core resources.
//
// In result, our localization is designed to handle missing resources
// gracefully, and we need a more fine-tuned way to communicate those problems
// to developers.
//
// In particular, we want developers and early adopters to be able to reason
// about missing translations, without bothering end user in production, where
// the user cannot react to that.
//
// We currently differentiate between nightly/dev-edition builds or automation
// where we report the errors, and beta/release, where we silence them.
[[maybe_unused]] static bool MaybeReportErrorsToGecko(
const nsTArray<nsCString>& aErrors, ErrorResult& aRv,
nsIGlobalObject* aGlobal) {
if (!aErrors.IsEmpty()) {
if (xpc::IsInAutomation()) {
aRv.ThrowInvalidStateError(aErrors.ElementAt(0));
return true;
}

#if defined(NIGHTLY_BUILD) || defined(MOZ_DEV_EDITION) || defined(DEBUG)
dom::Document* doc = nullptr;
if (aGlobal) {
nsPIDOMWindowInner* innerWindow = aGlobal->AsInnerWindow();
if (innerWindow) {
doc = innerWindow->GetExtantDoc();
}
}

for (const auto& error : aErrors) {
nsContentUtils::ReportToConsoleNonLocalized(NS_ConvertUTF8toUTF16(error),
nsIScriptError::warningFlag,
"l10n"_ns, doc);
printf_stderr("%s\n", error.get());
}
#endif
}

return false;
}

class Localization : public nsIObserver,
public nsWrapperCache,
public nsSupportsWeakReference {
Expand Down

0 comments on commit f596f45

Please sign in to comment.