L10nMutations.h (3103B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_l10n_L10nMutations_h 8 #define mozilla_dom_l10n_L10nMutations_h 9 10 #include "nsCycleCollectionParticipant.h" 11 #include "nsHashKeys.h" 12 #include "nsRefreshObservers.h" 13 #include "nsStubMutationObserver.h" 14 #include "nsTHashSet.h" 15 16 class nsRefreshDriver; 17 18 namespace mozilla::dom { 19 class Document; 20 class DOMLocalization; 21 /** 22 * L10nMutations manage observing roots for localization 23 * changes and coalescing pending translations into 24 * batches - one per animation frame. 25 */ 26 class L10nMutations final : public nsStubMultiMutationObserver, 27 public nsARefreshObserver { 28 public: 29 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 30 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(L10nMutations, nsIMutationObserver) 31 NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED 32 NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED 33 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED 34 NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED 35 36 explicit L10nMutations(DOMLocalization* aDOMLocalization); 37 38 /** 39 * Pause root observation. 40 * This is useful for injecting already-translated 41 * content into an observed root, without causing 42 * superflues translation. 43 */ 44 void PauseObserving(); 45 46 /** 47 * Resume root observation. 48 */ 49 void ResumeObserving(); 50 51 /** 52 * Disconnect roots, stop refresh observer 53 * and break the cycle collection deadlock 54 * by removing the reference to mDOMLocalization. 55 */ 56 void Disconnect(); 57 58 /** 59 * Called when PresShell gets created for the document. 60 * If there are already pending mutations, this 61 * will schedule the refresh driver to translate them. 62 */ 63 void OnCreatePresShell(); 64 65 bool HasPendingMutations() const { 66 return !mPendingElements.IsEmpty() || mPendingPromises; 67 } 68 69 MOZ_CAN_RUN_SCRIPT void PendingPromiseSettled(); 70 71 private: 72 bool mObserving = false; 73 bool mBlockingLoad = false; 74 bool mPendingBlockingLoadFlush = false; 75 uint32_t mPendingPromises = 0; 76 RefPtr<nsRefreshDriver> mRefreshDriver; 77 DOMLocalization* mDOMLocalization; 78 79 // The hash is used to speed up lookups into mPendingElements, which we need 80 // to guarantee some consistent ordering of operations. 81 nsTHashSet<RefPtr<Element>> mPendingElementsHash; 82 nsTArray<RefPtr<Element>> mPendingElements; 83 84 Document* GetDocument() const; 85 86 MOZ_CAN_RUN_SCRIPT void WillRefresh(mozilla::TimeStamp aTime) override; 87 88 void StartRefreshObserver(); 89 void StopRefreshObserver(); 90 void L10nElementChanged(Element* aElement); 91 MOZ_CAN_RUN_SCRIPT void FlushPendingTranslations(); 92 MOZ_CAN_RUN_SCRIPT void FlushPendingTranslationsBeforeLoad(); 93 MOZ_CAN_RUN_SCRIPT void MaybeFirePendingTranslationsFinished(); 94 95 ~L10nMutations(); 96 bool IsInRoots(nsINode* aNode); 97 }; 98 99 } // namespace mozilla::dom 100 101 #endif // mozilla_dom_l10n_L10nMutations_h__