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/. */
7 #ifndef DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICY_H_
8 #define DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICY_H_
10 #include "js/TypeDecls.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/RefPtr.h"
14 #include "mozilla/UniquePtr.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"
21 #include "nsWrapperCache.h"
23 namespace mozilla::dom
{
26 class TrustedTypePolicyFactory
;
28 // https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy
29 class TrustedTypePolicy
: public nsWrapperCache
{
31 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(TrustedTypePolicy
)
32 NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(TrustedTypePolicy
)
35 RefPtr
<CreateHTMLCallback
> mCreateHTMLCallback
;
36 RefPtr
<CreateScriptCallback
> mCreateScriptCallback
;
37 RefPtr
<CreateScriptURLCallback
> mCreateScriptURLCallback
;
40 TrustedTypePolicy(TrustedTypePolicyFactory
* aParentObject
,
41 const nsAString
& aName
, Options
&& aOptions
);
43 // Required for Web IDL binding.
44 TrustedTypePolicyFactory
* GetParentObject() const { return mParentObject
; }
46 // Required for Web IDL binding.
47 JSObject
* WrapObject(JSContext
* aCx
,
48 JS::Handle
<JSObject
*> aGivenProto
) override
;
50 // https://w3c.github.io/trusted-types/dist/spec/#trustedtypepolicy-name
51 void GetName(DOMString
& aResult
) const;
53 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createhtml
54 MOZ_CAN_RUN_SCRIPT UniquePtr
<TrustedHTML
> CreateHTML(
55 JSContext
* aJSContext
, const nsAString
& aInput
,
56 const Sequence
<JS::Value
>& aArguments
, ErrorResult
& aErrorResult
) const;
58 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createscript
59 MOZ_CAN_RUN_SCRIPT UniquePtr
<TrustedScript
> CreateScript(
60 JSContext
* aJSContext
, const nsAString
& aInput
,
61 const Sequence
<JS::Value
>& aArguments
, ErrorResult
& aErrorResult
) const;
63 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createscripturl
64 MOZ_CAN_RUN_SCRIPT UniquePtr
<TrustedScriptURL
> CreateScriptURL(
65 JSContext
* aJSContext
, const nsAString
& aInput
,
66 const Sequence
<JS::Value
>& aArguments
, ErrorResult
& aErrorResult
) const;
69 // Required because this class is ref-counted.
70 virtual ~TrustedTypePolicy() = default;
72 // https://w3c.github.io/trusted-types/dist/spec/#abstract-opdef-create-a-trusted-type
73 template <typename T
, typename CallbackObject
>
74 MOZ_CAN_RUN_SCRIPT UniquePtr
<T
> CreateTrustedType(
75 const RefPtr
<CallbackObject
>& aCallbackObject
, const nsAString
& aValue
,
76 const Sequence
<JS::Value
>& aArguments
, ErrorResult
& aErrorResult
) const;
78 // https://w3c.github.io/trusted-types/dist/spec/#abstract-opdef-get-trusted-type-policy-value
80 // @param aResult may become void.
81 template <typename CallbackObject
>
82 MOZ_CAN_RUN_SCRIPT
void DetermineTrustedPolicyValue(
83 const RefPtr
<CallbackObject
>& aCallbackObject
, const nsAString
& aValue
,
84 const Sequence
<JS::Value
>& aArguments
, bool aThrowIfMissing
,
85 ErrorResult
& aErrorResult
, nsAString
& aResult
) const;
86 RefPtr
<TrustedTypePolicyFactory
> mParentObject
;
93 } // namespace mozilla::dom
95 #endif // DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICY_H_