TrustedTypePolicy.h (3923B)
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 DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICY_H_ 8 #define DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICY_H_ 9 10 #include "js/TypeDecls.h" 11 #include "js/Value.h" 12 #include "mozilla/AlreadyAddRefed.h" 13 #include "mozilla/Attributes.h" 14 #include "mozilla/RefPtr.h" 15 #include "mozilla/dom/BindingDeclarations.h" 16 #include "mozilla/dom/TrustedHTML.h" 17 #include "mozilla/dom/TrustedScript.h" 18 #include "mozilla/dom/TrustedScriptURL.h" 19 #include "nsISupportsImpl.h" 20 #include "nsString.h" 21 #include "nsWrapperCache.h" 22 23 template <typename T> 24 class nsTArray; 25 26 namespace mozilla::dom { 27 28 class DOMString; 29 class TrustedTypePolicyFactory; 30 31 // https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy 32 class TrustedTypePolicy : public nsWrapperCache { 33 public: 34 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(TrustedTypePolicy) 35 NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(TrustedTypePolicy) 36 37 struct Options { 38 RefPtr<CreateHTMLCallback> mCreateHTMLCallback; 39 RefPtr<CreateScriptCallback> mCreateScriptCallback; 40 RefPtr<CreateScriptURLCallback> mCreateScriptURLCallback; 41 }; 42 43 TrustedTypePolicy(TrustedTypePolicyFactory* aParentObject, 44 const nsAString& aName, Options&& aOptions); 45 46 // Required for Web IDL binding. 47 TrustedTypePolicyFactory* GetParentObject() const { return mParentObject; } 48 49 // Required for Web IDL binding. 50 JSObject* WrapObject(JSContext* aCx, 51 JS::Handle<JSObject*> aGivenProto) override; 52 53 // https://w3c.github.io/trusted-types/dist/spec/#trustedtypepolicy-name 54 void GetName(DOMString& aResult) const; 55 56 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createhtml 57 MOZ_CAN_RUN_SCRIPT already_AddRefed<TrustedHTML> CreateHTML( 58 JSContext* aJSContext, const nsAString& aInput, 59 const Sequence<JS::Value>& aArguments, ErrorResult& aErrorResult) const; 60 61 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createscript 62 MOZ_CAN_RUN_SCRIPT already_AddRefed<TrustedScript> CreateScript( 63 JSContext* aJSContext, const nsAString& aInput, 64 const Sequence<JS::Value>& aArguments, ErrorResult& aErrorResult) const; 65 66 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createscripturl 67 MOZ_CAN_RUN_SCRIPT already_AddRefed<TrustedScriptURL> CreateScriptURL( 68 JSContext* aJSContext, const nsAString& aInput, 69 const Sequence<JS::Value>& aArguments, ErrorResult& aErrorResult) const; 70 71 // https://w3c.github.io/trusted-types/dist/spec/#abstract-opdef-get-trusted-type-policy-value 72 // 73 // @param aResult may become void. 74 template <typename CallbackObject> 75 MOZ_CAN_RUN_SCRIPT void DetermineTrustedPolicyValue( 76 const RefPtr<CallbackObject>& aCallbackObject, const nsAString& aValue, 77 const nsTArray<JS::Value>& aArguments, bool aThrowIfMissing, 78 ErrorResult& aErrorResult, nsAString& aResult) const; 79 80 const Options& GetOptions() const { return mOptions; } 81 82 private: 83 // Required because this class is ref-counted. 84 virtual ~TrustedTypePolicy() = default; 85 86 // https://w3c.github.io/trusted-types/dist/spec/#abstract-opdef-create-a-trusted-type 87 template <typename T, typename CallbackObject> 88 MOZ_CAN_RUN_SCRIPT already_AddRefed<T> CreateTrustedType( 89 const RefPtr<CallbackObject>& aCallbackObject, const nsAString& aValue, 90 const Sequence<JS::Value>& aArguments, ErrorResult& aErrorResult) const; 91 92 RefPtr<TrustedTypePolicyFactory> mParentObject; 93 94 const nsString mName; 95 96 Options mOptions; 97 }; 98 99 } // namespace mozilla::dom 100 101 #endif // DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICY_H_