gecko-dev/dom/l10n/DocumentL10n.cpp

140 строки
4.3 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "DocumentL10n.h"
#include "nsIContentSink.h"
#include "mozilla/dom/DocumentL10nBinding.h"
using namespace mozilla::dom;
NS_IMPL_CYCLE_COLLECTION_CLASS(DocumentL10n)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DocumentL10n, DOMLocalization)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mReady)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mContentSink)
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DocumentL10n, DOMLocalization)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReady)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContentSink)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_ADDREF_INHERITED(DocumentL10n, DOMLocalization)
NS_IMPL_RELEASE_INHERITED(DocumentL10n, DOMLocalization)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DocumentL10n)
NS_INTERFACE_MAP_END_INHERITING(DOMLocalization)
DocumentL10n::DocumentL10n(Document* aDocument)
: DOMLocalization(aDocument->GetScopeObject()),
mDocument(aDocument),
mState(DocumentL10nState::Initialized) {
mContentSink = do_QueryInterface(aDocument->GetCurrentContentSink());
}
void DocumentL10n::Init(nsTArray<nsString>& aResourceIds, ErrorResult& aRv) {
DOMLocalization::Init(aResourceIds, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
mReady = Promise::Create(mGlobal, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
}
JSObject* DocumentL10n::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return DocumentL10n_Binding::Wrap(aCx, this, aGivenProto);
}
class L10nReadyHandler final : public PromiseNativeHandler {
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(L10nReadyHandler)
explicit L10nReadyHandler(Promise* aPromise, DocumentL10n* aDocumentL10n)
: mPromise(aPromise), mDocumentL10n(aDocumentL10n) {}
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
mDocumentL10n->InitialDocumentTranslationCompleted();
mPromise->MaybeResolveWithUndefined();
}
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
mDocumentL10n->InitialDocumentTranslationCompleted();
mPromise->MaybeRejectWithUndefined();
}
private:
~L10nReadyHandler() = default;
RefPtr<Promise> mPromise;
RefPtr<DocumentL10n> mDocumentL10n;
};
NS_IMPL_CYCLE_COLLECTION(L10nReadyHandler, mPromise, mDocumentL10n)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(L10nReadyHandler)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(L10nReadyHandler)
NS_IMPL_CYCLE_COLLECTING_RELEASE(L10nReadyHandler)
void DocumentL10n::TriggerInitialDocumentTranslation() {
if (mState >= DocumentL10nState::InitialTranslationTriggered) {
return;
}
mState = DocumentL10nState::InitialTranslationTriggered;
Element* elem = mDocument->GetDocumentElement();
if (!elem) {
return;
}
Sequence<OwningNonNull<Element>> elements;
ErrorResult rv;
GetTranslatables(*elem, elements, rv);
ConnectRoot(*elem, rv);
RefPtr<Promise> promise = TranslateElements(elements, rv);
if (!promise) {
return;
}
RefPtr<PromiseNativeHandler> l10nReadyHandler =
new L10nReadyHandler(mReady, this);
promise->AppendNativeHandler(l10nReadyHandler);
}
void DocumentL10n::InitialDocumentTranslationCompleted() {
if (mState >= DocumentL10nState::InitialTranslationCompleted) {
return;
}
Element* documentElement = mDocument->GetDocumentElement();
if (documentElement) {
SetRootInfo(documentElement);
}
mState = DocumentL10nState::InitialTranslationCompleted;
mDocument->InitialDocumentTranslationCompleted();
// In XUL scenario contentSink is nullptr.
if (mContentSink) {
mContentSink->InitialDocumentTranslationCompleted();
}
}
Promise* DocumentL10n::Ready() { return mReady; }
void DocumentL10n::OnCreatePresShell() { mMutations->OnCreatePresShell(); }