FluentBundle.h (3233B)
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_intl_l10n_FluentBundle_h 8 #define mozilla_intl_l10n_FluentBundle_h 9 10 #include "mozilla/dom/BindingDeclarations.h" 11 #include "nsCycleCollectionParticipant.h" 12 #include "nsWrapperCache.h" 13 #include "mozilla/dom/FluentBinding.h" 14 #include "mozilla/dom/LocalizationBinding.h" 15 #include "mozilla/intl/FluentResource.h" 16 #include "mozilla/intl/FluentBindings.h" 17 18 class nsIGlobalObject; 19 20 namespace mozilla { 21 class ErrorResult; 22 23 namespace dom { 24 struct FluentMessage; 25 struct L10nMessage; 26 class OwningUTF8StringOrDouble; 27 class UTF8StringOrUTF8StringSequence; 28 struct FluentBundleOptions; 29 struct FluentBundleAddResourceOptions; 30 } // namespace dom 31 32 namespace intl { 33 34 class FluentResource; 35 36 using L10nArgs = 37 dom::Record<nsCString, dom::Nullable<dom::OwningUTF8StringOrDouble>>; 38 39 class FluentPattern : public nsWrapperCache { 40 public: 41 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(FluentPattern) 42 NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(FluentPattern) 43 44 FluentPattern(nsISupports* aParent, const nsACString& aId); 45 FluentPattern(nsISupports* aParent, const nsACString& aId, 46 const nsACString& aAttrName); 47 JSObject* WrapObject(JSContext* aCx, 48 JS::Handle<JSObject*> aGivenProto) override; 49 nsISupports* GetParentObject() const { return mParent; } 50 51 nsCString mId; 52 nsCString mAttrName; 53 54 protected: 55 virtual ~FluentPattern(); 56 57 nsCOMPtr<nsISupports> mParent; 58 }; 59 60 class FluentBundle final : public nsWrapperCache { 61 public: 62 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(FluentBundle) 63 NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(FluentBundle) 64 65 FluentBundle(nsISupports* aParent, UniquePtr<ffi::FluentBundleRc> aRaw); 66 67 static already_AddRefed<FluentBundle> Constructor( 68 const dom::GlobalObject& aGlobal, 69 const dom::UTF8StringOrUTF8StringSequence& aLocales, 70 const dom::FluentBundleOptions& aOptions, ErrorResult& aRv); 71 JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) final; 72 nsISupports* GetParentObject() const { return mParent; } 73 74 void GetLocales(nsTArray<nsCString>& aLocales); 75 76 void AddResource(FluentResource& aResource, 77 const dom::FluentBundleAddResourceOptions& aOptions); 78 bool HasMessage(const nsACString& aId); 79 void GetMessage(const nsACString& aId, 80 dom::Nullable<dom::FluentMessage>& aRetVal); 81 void FormatPattern(JSContext* aCx, const FluentPattern& aPattern, 82 const dom::Nullable<L10nArgs>& aArgs, 83 const dom::Optional<JS::Handle<JSObject*>>& aErrors, 84 nsACString& aRetVal, ErrorResult& aRv); 85 86 static void ConvertArgs(const L10nArgs& aArgs, 87 nsTArray<ffi::L10nArg>& aRetVal); 88 89 protected: 90 ~FluentBundle(); 91 92 nsCOMPtr<nsISupports> mParent; 93 UniquePtr<ffi::FluentBundleRc> mRaw; 94 }; 95 96 } // namespace intl 97 } // namespace mozilla 98 99 #endif