TrustedTypePolicyFactory.h (4080B)
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_TRUSTEDTYPEPOLICYFACTORY_H_ 8 #define DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICYFACTORY_H_ 9 10 #include "js/TypeDecls.h" 11 #include "mozilla/AlreadyAddRefed.h" 12 #include "mozilla/RefPtr.h" 13 #include "mozilla/dom/BindingUtils.h" 14 #include "mozilla/dom/TrustedHTML.h" 15 #include "mozilla/dom/TrustedScript.h" 16 #include "mozilla/dom/TrustedScriptURL.h" 17 #include "nsIGlobalObject.h" 18 #include "nsISupportsImpl.h" 19 #include "nsStringFwd.h" 20 #include "nsTArray.h" 21 #include "nsWrapperCache.h" 22 23 class DOMString; 24 25 namespace mozilla { 26 class ErrorResult; 27 28 namespace dom { 29 class TrustedTypePolicy; 30 31 // https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy-factory 32 class TrustedTypePolicyFactory : public nsWrapperCache { 33 public: 34 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(TrustedTypePolicyFactory) 35 NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(TrustedTypePolicyFactory) 36 37 explicit TrustedTypePolicyFactory(nsIGlobalObject* aGlobalObject); 38 39 // Required for Web IDL binding. 40 nsIGlobalObject* GetParentObject() const { return mGlobalObject; } 41 42 // Required for Web IDL binding. 43 JSObject* WrapObject(JSContext* aCx, 44 JS::Handle<JSObject*> aGivenProto) override; 45 46 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-createpolicy 47 already_AddRefed<TrustedTypePolicy> CreatePolicy( 48 JSContext* aJSContext, const nsAString& aPolicyName, 49 const TrustedTypePolicyOptions& aPolicyOptions, ErrorResult& aRv); 50 51 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-ishtml 52 bool IsHTML(JSContext* aJSContext, const JS::Handle<JS::Value>& aValue) const; 53 54 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-isscript 55 bool IsScript(JSContext* aJSContext, 56 const JS::Handle<JS::Value>& aValue) const; 57 58 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-isscripturl 59 bool IsScriptURL(JSContext* aJSContext, 60 const JS::Handle<JS::Value>& aValue) const; 61 62 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-emptyhtml 63 already_AddRefed<TrustedHTML> EmptyHTML(); 64 65 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-emptyscript 66 already_AddRefed<TrustedScript> EmptyScript(); 67 68 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-getattributetype 69 void GetAttributeType(const nsAString& aTagName, const nsAString& aAttribute, 70 const nsAString& aElementNs, const nsAString& aAttrNs, 71 DOMString& aResult); 72 73 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-getpropertytype 74 void GetPropertyType(const nsAString& aTagName, const nsAString& aProperty, 75 const nsAString& aElementNs, DOMString& aResult); 76 77 // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-defaultpolicy 78 TrustedTypePolicy* GetDefaultPolicy() const { return mDefaultPolicy; } 79 80 private: 81 // Virtual destructor required because this class is ref-counted. 82 virtual ~TrustedTypePolicyFactory(); 83 84 enum class PolicyCreation { Blocked, Allowed }; 85 86 // https://w3c.github.io/trusted-types/dist/spec/#abstract-opdef-should-trusted-type-policy-creation-be-blocked-by-content-security-policy 87 PolicyCreation ShouldTrustedTypePolicyCreationBeBlockedByCSP( 88 JSContext* aJSContext, const nsAString& aPolicyName) const; 89 90 RefPtr<nsIGlobalObject> mGlobalObject; 91 92 nsTArray<nsString> mCreatedPolicyNames; 93 94 RefPtr<TrustedTypePolicy> mDefaultPolicy; 95 }; 96 97 } // namespace dom 98 } // namespace mozilla 99 100 #endif // DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICYFACTORY_H_