tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DOMLocalization.h (4587B)


      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_DOMLocalization_h
      8 #define mozilla_dom_l10n_DOMLocalization_h
      9 
     10 #include "mozilla/dom/DOMLocalizationBinding.h"
     11 #include "mozilla/dom/L10nMutations.h"
     12 #include "mozilla/dom/L10nOverlaysBinding.h"
     13 #include "mozilla/dom/LocalizationBinding.h"
     14 #include "mozilla/dom/PromiseNativeHandler.h"
     15 #include "mozilla/intl/L10nRegistry.h"
     16 #include "mozilla/intl/Localization.h"
     17 #include "nsTHashSet.h"
     18 #include "nsXULPrototypeDocument.h"
     19 
     20 // XXX Avoid including this here by moving function bodies to the cpp file
     21 #include "nsINode.h"
     22 
     23 namespace mozilla::dom {
     24 
     25 class Element;
     26 class L10nMutations;
     27 
     28 class DOMLocalization : public intl::Localization {
     29 public:
     30  NS_DECL_ISUPPORTS_INHERITED
     31  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMLocalization, Localization)
     32 
     33  void Destroy();
     34 
     35  static already_AddRefed<DOMLocalization> Constructor(
     36      const dom::GlobalObject& aGlobal,
     37      const dom::Sequence<dom::OwningUTF8StringOrResourceId>& aResourceIds,
     38      bool aIsSync,
     39      const dom::Optional<dom::NonNull<intl::L10nRegistry>>& aRegistry,
     40      const dom::Optional<dom::Sequence<nsCString>>& aLocales,
     41      ErrorResult& aRv);
     42 
     43  JSObject* WrapObject(JSContext*, JS::Handle<JSObject*> aGivenProto) override;
     44 
     45  bool HasPendingMutations() const;
     46 
     47  /**
     48   * DOMLocalization API
     49   *
     50   * Methods documentation in DOMLocalization.webidl
     51   */
     52 
     53  void ConnectRoot(nsINode& aNode);
     54  void DisconnectRoot(nsINode& aNode);
     55 
     56  void PauseObserving();
     57  void ResumeObserving();
     58 
     59  void SetAttributes(JSContext* aCx, Element& aElement, const nsAString& aId,
     60                     const Optional<JS::Handle<JSObject*>>& aArgs,
     61                     ErrorResult& aRv);
     62  void GetAttributes(Element& aElement, L10nIdArgs& aResult, ErrorResult& aRv);
     63 
     64  void SetArgs(JSContext* aCx, Element& aElement,
     65               const Optional<JS::Handle<JSObject*>>& aArgs, ErrorResult& aRv);
     66 
     67  already_AddRefed<Promise> TranslateFragment(nsINode& aNode, ErrorResult& aRv);
     68 
     69  already_AddRefed<Promise> TranslateElements(
     70      const nsTArray<OwningNonNull<Element>>& aElements, ErrorResult& aRv);
     71  already_AddRefed<Promise> TranslateElements(
     72      const nsTArray<OwningNonNull<Element>>& aElements,
     73      nsXULPrototypeDocument* aProto, ErrorResult& aRv);
     74 
     75  already_AddRefed<Promise> TranslateRoots(ErrorResult& aRv);
     76 
     77  /**
     78   * Helper methods
     79   */
     80 
     81  /**
     82   * Accumulates all translatable elements (ones containing
     83   * a `data-l10n-id` attribute) from under a node into
     84   * a list of elements.
     85   */
     86  static void GetTranslatables(nsINode& aNode,
     87                               Sequence<OwningNonNull<Element>>& aElements,
     88                               ErrorResult& aRv);
     89 
     90  /**
     91   * Sets the root information such as locale and direction.
     92   */
     93  static void SetRootInfo(Element* aElement);
     94 
     95  /**
     96   * Applies l10n translations on translatable elements.
     97   *
     98   * If `aProto` gets passed, it'll be used to cache
     99   * the localized elements.
    100   *
    101   * Result is `true` if all translations were applied
    102   * successfully, and `false` otherwise.
    103   */
    104  bool ApplyTranslations(nsTArray<nsCOMPtr<Element>>& aElements,
    105                         nsTArray<Nullable<L10nMessage>>& aTranslations,
    106                         nsXULPrototypeDocument* aProto, ErrorResult& aRv);
    107 
    108  bool SubtreeRootInRoots(nsINode* aSubtreeRoot) {
    109    for (const auto* key : mRoots) {
    110      nsINode* subtreeRoot = key->SubtreeRoot();
    111      if (subtreeRoot == aSubtreeRoot) {
    112        return true;
    113      }
    114    }
    115    return false;
    116  }
    117 
    118  DOMLocalization(nsIGlobalObject* aGlobal, bool aSync);
    119  DOMLocalization(nsIGlobalObject* aGlobal, bool aIsSync,
    120                  const intl::ffi::LocalizationRc* aRaw);
    121  DOMLocalization(nsIGlobalObject* aGlobal, bool aSync,
    122                  const nsTArray<nsCString>& aLocales);
    123 
    124 protected:
    125  virtual ~DOMLocalization();
    126  void OnChange() override;
    127  void DisconnectMutations();
    128  void DisconnectRoots();
    129  void ReportL10nOverlaysErrors(nsTArray<L10nOverlaysError>& aErrors);
    130  void ConvertStringToL10nArgs(const nsCString& aL10nId, const nsString& aInput,
    131                               intl::L10nArgs& aRetVal, ErrorResult& aRv);
    132 
    133  RefPtr<L10nMutations> mMutations;
    134  nsTHashSet<RefPtr<nsINode>> mRoots;
    135 };
    136 
    137 }  // namespace mozilla::dom
    138 
    139 #endif