2019-07-05 17:05:57 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2019-06-25 19:39:53 +00:00
|
|
|
#ifndef mozilla_dom_l10n_L10nMutations_h
|
|
|
|
#define mozilla_dom_l10n_L10nMutations_h
|
2019-05-21 19:21:54 +00:00
|
|
|
|
|
|
|
#include "nsRefreshDriver.h"
|
|
|
|
#include "nsStubMutationObserver.h"
|
|
|
|
#include "nsTHashtable.h"
|
2019-06-06 16:32:58 +00:00
|
|
|
#include "mozilla/dom/DOMLocalization.h"
|
2019-05-21 19:21:54 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
/**
|
2019-06-06 16:32:58 +00:00
|
|
|
* L10nMutations manage observing roots for localization
|
2019-05-21 19:21:54 +00:00
|
|
|
* changes and coalescing pending translations into
|
|
|
|
* batches - one per animation frame.
|
|
|
|
*/
|
2019-06-06 16:32:58 +00:00
|
|
|
class L10nMutations final : public nsStubMutationObserver,
|
|
|
|
public nsARefreshObserver {
|
2019-05-21 19:21:54 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2019-06-06 16:32:58 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(L10nMutations, nsIMutationObserver)
|
2019-05-21 19:21:54 +00:00
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
|
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
|
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
|
|
|
|
|
2019-06-06 16:32:58 +00:00
|
|
|
explicit L10nMutations(DOMLocalization* aDOMLocalization);
|
2019-05-21 19:21:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pause root observation.
|
|
|
|
* This is useful for injecting already-translated
|
|
|
|
* content into an observed root, without causing
|
|
|
|
* superflues translation.
|
|
|
|
*/
|
|
|
|
void PauseObserving();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resume root observation.
|
|
|
|
*/
|
|
|
|
void ResumeObserving();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect roots, stop refresh observer
|
|
|
|
* and break the cycle collection deadlock
|
2019-06-06 16:31:25 +00:00
|
|
|
* by removing the reference to mDOMLocalization.
|
2019-05-21 19:21:54 +00:00
|
|
|
*/
|
|
|
|
void Disconnect();
|
|
|
|
|
2019-06-03 10:00:47 +00:00
|
|
|
/**
|
|
|
|
* Called when PresShell gets created for the document.
|
|
|
|
* If there are already pending mutations, this
|
|
|
|
* will schedule the refresh driver to translate them.
|
|
|
|
*/
|
|
|
|
void OnCreatePresShell();
|
|
|
|
|
2019-05-21 19:21:54 +00:00
|
|
|
protected:
|
|
|
|
bool mObserving = false;
|
|
|
|
bool mRefreshObserver = false;
|
|
|
|
RefPtr<nsRefreshDriver> mRefreshDriver;
|
2019-06-06 16:31:25 +00:00
|
|
|
DOMLocalization* mDOMLocalization;
|
2019-05-21 19:21:54 +00:00
|
|
|
|
|
|
|
// The hash is used to speed up lookups into mPendingElements.
|
|
|
|
nsTHashtable<nsRefPtrHashKey<Element>> mPendingElementsHash;
|
|
|
|
nsTArray<RefPtr<Element>> mPendingElements;
|
|
|
|
|
|
|
|
virtual void WillRefresh(mozilla::TimeStamp aTime) override;
|
|
|
|
|
|
|
|
void StartRefreshObserver();
|
|
|
|
void StopRefreshObserver();
|
|
|
|
void L10nElementChanged(Element* aElement);
|
|
|
|
void FlushPendingTranslations();
|
|
|
|
|
|
|
|
private:
|
2019-06-06 16:32:58 +00:00
|
|
|
~L10nMutations() {
|
2019-05-21 19:21:54 +00:00
|
|
|
StopRefreshObserver();
|
2019-06-06 16:31:25 +00:00
|
|
|
MOZ_ASSERT(!mDOMLocalization,
|
2019-06-06 16:32:58 +00:00
|
|
|
"DOMLocalization<-->L10nMutations cycle should be broken.");
|
2019-05-21 19:21:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2019-06-06 16:32:58 +00:00
|
|
|
#endif // mozilla_dom_l10n_L10nMutations_h__
|