/work/obj-fuzz/dist/include/mozilla/dom/DocumentL10n.h
Line | Count | Source (jump to first uncovered line) |
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_DocumentL10n_h |
8 | | #define mozilla_dom_DocumentL10n_h |
9 | | |
10 | | #include "js/TypeDecls.h" |
11 | | #include "mozilla/Attributes.h" |
12 | | #include "mozilla/ErrorResult.h" |
13 | | #include "mozilla/dom/BindingDeclarations.h" |
14 | | #include "nsIDOMEventListener.h" |
15 | | #include "nsCycleCollectionParticipant.h" |
16 | | #include "nsWrapperCache.h" |
17 | | #include "nsIDocument.h" |
18 | | #include "nsINode.h" |
19 | | #include "mozIDOMLocalization.h" |
20 | | |
21 | | namespace mozilla { |
22 | | namespace dom { |
23 | | |
24 | | class Element; |
25 | | class Promise; |
26 | | struct L10nKey; |
27 | | |
28 | | enum class DocumentL10nState |
29 | | { |
30 | | Initialized = 0, |
31 | | InitialTranslationTriggered |
32 | | }; |
33 | | |
34 | | /** |
35 | | * This class maintains localization status of the nsDocument. |
36 | | * |
37 | | * The nsDocument will initialize it lazily when a link with a |
38 | | * localization resource is added to the document. |
39 | | * |
40 | | * Once initialized, DocumentL10n relays all API methods to an |
41 | | * instance of mozIDOMLocalization and maintaines a single promise |
42 | | * which gets resolved the first time the document gets translated. |
43 | | */ |
44 | | class DocumentL10n final : public nsIDOMEventListener, |
45 | | public nsWrapperCache |
46 | | { |
47 | | public: |
48 | | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
49 | | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DocumentL10n) |
50 | | NS_DECL_NSIDOMEVENTLISTENER |
51 | | |
52 | | public: |
53 | | explicit DocumentL10n(nsIDocument* aDocument); |
54 | | bool Init(nsTArray<nsString>& aResourceIds); |
55 | | |
56 | | protected: |
57 | | virtual ~DocumentL10n(); |
58 | | |
59 | | nsCOMPtr<nsIDocument> mDocument; |
60 | | RefPtr<Promise> mReady; |
61 | | DocumentL10nState mState; |
62 | | nsCOMPtr<mozIDOMLocalization> mDOMLocalization; |
63 | | |
64 | | public: |
65 | 0 | nsIDocument* GetParentObject() const { return mDocument; }; |
66 | | |
67 | | virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; |
68 | | |
69 | | /** |
70 | | * A method for adding resources to the localization context. |
71 | | * |
72 | | * Returns a new count of resources used by the context. |
73 | | */ |
74 | | uint32_t AddResourceIds(nsTArray<nsString>& aResourceIds); |
75 | | |
76 | | /** |
77 | | * A method for removing resources from the localization context. |
78 | | * |
79 | | * Returns a new count of resources used by the context. |
80 | | */ |
81 | | uint32_t RemoveResourceIds(nsTArray<nsString>& aResourceIds); |
82 | | |
83 | | already_AddRefed<Promise> FormatMessages(JSContext* aCx, const Sequence<L10nKey>& aKeys, ErrorResult& aRv); |
84 | | already_AddRefed<Promise> FormatValues(JSContext* aCx, const Sequence<L10nKey>& aKeys, ErrorResult& aRv); |
85 | | already_AddRefed<Promise> FormatValue(JSContext* aCx, const nsAString& aId, const Optional<JS::Handle<JSObject*>>& aArgs, ErrorResult& aRv); |
86 | | |
87 | | void SetAttributes(JSContext* aCx, Element& aElement, const nsAString& aId, const Optional<JS::Handle<JSObject*>>& aArgs, ErrorResult& aRv); |
88 | | void GetAttributes(JSContext* aCx, Element& aElement, L10nKey& aResult, ErrorResult& aRv); |
89 | | |
90 | | already_AddRefed<Promise> TranslateFragment(nsINode& aNode, ErrorResult& aRv); |
91 | | already_AddRefed<Promise> TranslateElements(const Sequence<OwningNonNull<Element>>& aElements, ErrorResult& aRv); |
92 | | |
93 | | Promise* Ready(); |
94 | | |
95 | | void TriggerInitialDocumentTranslation(); |
96 | | }; |
97 | | |
98 | | } // namespace dom |
99 | | } // namespace mozilla |
100 | | |
101 | | #endif // mozilla_dom_DocumentL10n_h |