DocumentL10n.h (2768B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 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_DocumentL10n_h 8 #define mozilla_dom_l10n_DocumentL10n_h 9 10 #include "mozilla/dom/DOMLocalization.h" 11 12 class nsIContentSink; 13 14 namespace mozilla::dom { 15 16 class Document; 17 18 enum class DocumentL10nState { 19 // State set when the DocumentL10n gets constructed. 20 Constructed = 0, 21 22 // State set when the initial translation got triggered. This happens 23 // if DocumentL10n was constructed during parsing of the document. 24 // 25 // If the DocumentL10n gets constructed later, we'll skip directly to 26 // Ready state. 27 InitialTranslationTriggered, 28 29 // State set the DocumentL10n has been fully initialized, potentially 30 // with initial translation being completed. 31 Ready, 32 }; 33 34 /** 35 * This class maintains localization status of the document. 36 * 37 * The document will initialize it lazily when a link with a localization 38 * resource is added to the document. 39 * 40 * Once initialized, DocumentL10n relays all API methods to an 41 * instance of mozILocalization and maintains a single promise 42 * which gets resolved the first time the document gets translated. 43 */ 44 class DocumentL10n final : public DOMLocalization { 45 public: 46 NS_DECL_ISUPPORTS_INHERITED 47 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DocumentL10n, DOMLocalization) 48 49 static RefPtr<DocumentL10n> Create(Document* aDocument, bool aSync); 50 static RefPtr<DocumentL10n> Create(Document* aDocument, bool aSync, 51 const nsTArray<nsCString>& aLocales); 52 53 protected: 54 explicit DocumentL10n(Document* aDocument, bool aSync); 55 explicit DocumentL10n(Document* aDocument, bool aSync, 56 const nsTArray<nsCString>& aLocales); 57 virtual ~DocumentL10n() = default; 58 59 RefPtr<Document> mDocument; 60 RefPtr<Promise> mReady; 61 DocumentL10nState mState; 62 nsCOMPtr<nsIContentSink> mContentSink; 63 64 public: 65 virtual JSObject* WrapObject(JSContext* aCx, 66 JS::Handle<JSObject*> aGivenProto) override; 67 68 Promise* Ready(); 69 70 void TriggerInitialTranslation(); 71 already_AddRefed<Promise> TranslateDocument(ErrorResult& aRv); 72 73 void InitialTranslationCompleted(bool aL10nCached); 74 75 Document* GetDocument() const { return mDocument; }; 76 void OnCreatePresShell(); 77 78 void ConnectRoot(nsINode& aNode, bool aTranslate, ErrorResult& aRv); 79 80 DocumentL10nState GetState() { return mState; }; 81 82 bool mBlockingLayout = false; 83 }; 84 85 } // namespace mozilla::dom 86 87 #endif // mozilla_dom_l10n_DocumentL10n_h