2019-07-08 12:27:47 +03: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-07-08 13:02:35 +03:00
|
|
|
|
2019-06-25 22:39:53 +03:00
|
|
|
#ifndef mozilla_intl_l10n_Localization_h
|
|
|
|
#define mozilla_intl_l10n_Localization_h
|
|
|
|
|
|
|
|
#include "nsWeakReference.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "mozILocalization.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/dom/LocalizationBinding.h"
|
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
|
|
|
|
|
|
|
class nsIGlobalObject;
|
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace mozilla {
|
2020-11-23 19:12:02 +03:00
|
|
|
class ErrorResult;
|
|
|
|
|
2019-06-25 22:39:53 +03:00
|
|
|
namespace intl {
|
|
|
|
|
2020-03-11 10:17:07 +03:00
|
|
|
typedef Record<nsCString, Nullable<OwningUTF8StringOrDouble>> L10nArgs;
|
2019-06-25 22:43:50 +03:00
|
|
|
|
2019-06-25 22:39:53 +03:00
|
|
|
class Localization : public nsIObserver,
|
|
|
|
public nsSupportsWeakReference,
|
|
|
|
public nsWrapperCache {
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Localization,
|
|
|
|
nsIObserver)
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2020-06-04 20:02:01 +03:00
|
|
|
static already_AddRefed<Localization> Create(
|
|
|
|
nsIGlobalObject* aGlobal, const bool aSync,
|
|
|
|
const BundleGenerator& aBundleGenerator);
|
|
|
|
|
|
|
|
void Activate(const bool aEager);
|
2020-05-30 12:36:50 +03:00
|
|
|
|
2020-05-31 10:12:33 +03:00
|
|
|
void Destroy();
|
|
|
|
|
2019-06-25 22:39:53 +03:00
|
|
|
static already_AddRefed<Localization> Constructor(
|
2020-04-11 02:35:03 +03:00
|
|
|
const GlobalObject& aGlobal, const Sequence<nsString>& aResourceIds,
|
|
|
|
const bool aSync, const BundleGenerator& aBundleGenerator,
|
2019-06-25 22:39:53 +03:00
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
nsIGlobalObject* GetParentObject() const;
|
|
|
|
|
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
2020-04-30 20:56:24 +03:00
|
|
|
uint32_t AddResourceId(const nsAString& aResourceId);
|
|
|
|
uint32_t RemoveResourceId(const nsAString& aResourceId);
|
|
|
|
|
2019-06-25 22:39:53 +03:00
|
|
|
/**
|
|
|
|
* Localization API
|
|
|
|
*
|
|
|
|
* Methods documentation in Localization.webidl
|
|
|
|
*/
|
2020-04-30 20:55:38 +03:00
|
|
|
uint32_t AddResourceIds(const nsTArray<nsString>& aResourceIds);
|
2019-06-25 22:39:53 +03:00
|
|
|
|
|
|
|
uint32_t RemoveResourceIds(const nsTArray<nsString>& aResourceIds);
|
|
|
|
|
2020-03-11 10:17:07 +03:00
|
|
|
already_AddRefed<Promise> FormatValue(JSContext* aCx, const nsACString& aId,
|
2019-06-25 22:43:50 +03:00
|
|
|
const Optional<L10nArgs>& aArgs,
|
|
|
|
ErrorResult& aRv);
|
2019-06-25 22:39:53 +03:00
|
|
|
|
2020-06-08 21:11:00 +03:00
|
|
|
already_AddRefed<Promise> FormatValues(
|
|
|
|
JSContext* aCx, const Sequence<OwningUTF8StringOrL10nIdArgs>& aKeys,
|
|
|
|
ErrorResult& aRv);
|
2019-06-25 22:39:53 +03:00
|
|
|
|
2020-06-08 21:11:00 +03:00
|
|
|
already_AddRefed<Promise> FormatMessages(
|
|
|
|
JSContext* aCx, const Sequence<OwningUTF8StringOrL10nIdArgs>& aKeys,
|
|
|
|
ErrorResult& aRv);
|
2019-06-25 22:39:53 +03:00
|
|
|
|
2020-04-11 02:35:03 +03:00
|
|
|
void SetIsSync(const bool aIsSync);
|
|
|
|
|
|
|
|
void FormatValueSync(JSContext* aCx, const nsACString& aId,
|
|
|
|
const Optional<L10nArgs>& aArgs, nsACString& aRetVal,
|
|
|
|
ErrorResult& aRv);
|
2020-06-08 21:11:00 +03:00
|
|
|
void FormatValuesSync(JSContext* aCx,
|
|
|
|
const Sequence<OwningUTF8StringOrL10nIdArgs>& aKeys,
|
2020-04-11 02:35:03 +03:00
|
|
|
nsTArray<nsCString>& aRetVal, ErrorResult& aRv);
|
2020-06-08 21:11:00 +03:00
|
|
|
void FormatMessagesSync(JSContext* aCx,
|
|
|
|
const Sequence<OwningUTF8StringOrL10nIdArgs>& aKeys,
|
2020-04-11 02:35:03 +03:00
|
|
|
nsTArray<Nullable<L10nMessage>>& aRetVal,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2019-06-25 22:39:53 +03:00
|
|
|
protected:
|
2020-06-04 20:02:01 +03:00
|
|
|
Localization(nsIGlobalObject* aGlobal, const bool aSync,
|
|
|
|
const BundleGenerator& aBundleGenerator);
|
|
|
|
virtual bool Init();
|
|
|
|
|
2019-06-25 22:39:53 +03:00
|
|
|
virtual ~Localization();
|
|
|
|
void RegisterObservers();
|
2019-09-13 13:08:26 +03:00
|
|
|
virtual void OnChange();
|
2019-06-25 22:39:53 +03:00
|
|
|
already_AddRefed<Promise> MaybeWrapPromise(Promise* aInnerPromise);
|
2019-06-25 22:43:50 +03:00
|
|
|
void ConvertL10nArgsToJSValue(JSContext* aCx, const L10nArgs& aArgs,
|
|
|
|
JS::MutableHandle<JS::Value> aRetVal,
|
|
|
|
ErrorResult& aRv);
|
2019-06-25 22:39:53 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobal;
|
|
|
|
nsCOMPtr<mozILocalization> mLocalization;
|
2020-05-31 10:12:33 +03:00
|
|
|
|
2019-07-01 20:56:57 +03:00
|
|
|
bool mIsSync;
|
2020-05-31 10:12:38 +03:00
|
|
|
nsTArray<nsString> mResourceIds;
|
2020-05-31 10:12:31 +03:00
|
|
|
|
|
|
|
JS::Heap<JS::Value> mBundles;
|
2020-05-31 10:12:33 +03:00
|
|
|
JS::Heap<JS::Value> mGenerateBundles;
|
|
|
|
JS::Heap<JS::Value> mGenerateBundlesSync;
|
2019-06-25 22:39:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace intl
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2019-07-08 12:27:47 +03:00
|
|
|
#endif
|