Skip to content

Commit 8fdd9b6

Browse files
tete17tcl3
authored andcommitted
LibWeb: First implementation of the TrustedTypePolicyFactory
Most of the functions are either not implemented of filled with dummy values.
1 parent 223b1cc commit 8fdd9b6

File tree

11 files changed

+116
-0
lines changed

11 files changed

+116
-0
lines changed

Libraries/LibWeb/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,7 @@ set(SOURCES
878878
SVG/SVGViewElement.cpp
879879
SVG/TagNames.cpp
880880
SVG/ViewBox.cpp
881+
TrustedTypes/TrustedTypePolicyFactory.cpp
881882
UIEvents/CompositionEvent.cpp
882883
UIEvents/EventNames.cpp
883884
UIEvents/FocusEvent.cpp

Libraries/LibWeb/Forward.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,3 +1183,9 @@ template<>
11831183
ErrorOr<Web::UniqueNodeID> decode(Decoder&);
11841184

11851185
}
1186+
1187+
namespace Web::TrustedTypes {
1188+
1189+
class TrustedTypePolicyFactory;
1190+
1191+
}

Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <LibWeb/Platform/ImageCodecPlugin.h>
4444
#include <LibWeb/ResourceTiming/PerformanceResourceTiming.h>
4545
#include <LibWeb/ServiceWorker/CacheStorage.h>
46+
#include <LibWeb/TrustedTypes/TrustedTypePolicyFactory.h>
4647
#include <LibWeb/UserTiming/PerformanceMark.h>
4748
#include <LibWeb/UserTiming/PerformanceMeasure.h>
4849
#include <LibWeb/WebIDL/AbstractOperations.h>
@@ -82,6 +83,7 @@ void WindowOrWorkerGlobalScopeMixin::visit_edges(JS::Cell::Visitor& visitor)
8283
visitor.visit(m_crypto);
8384
visitor.visit(m_cache_storage);
8485
visitor.visit(m_resource_timing_secondary_buffer);
86+
visitor.visit(m_trusted_type_policy_factory);
8587
}
8688

8789
void WindowOrWorkerGlobalScopeMixin::finalize()
@@ -1122,4 +1124,15 @@ GC::Ref<ServiceWorker::CacheStorage> WindowOrWorkerGlobalScopeMixin::caches()
11221124
return GC::Ref { *m_cache_storage };
11231125
}
11241126

1127+
// https://w3c.github.io/trusted-types/dist/spec/#extensions-to-the-windoworworkerglobalscope-interface
1128+
GC::Ref<TrustedTypes::TrustedTypePolicyFactory> WindowOrWorkerGlobalScopeMixin::trusted_types()
1129+
{
1130+
auto const& platform_object = this_impl();
1131+
auto& realm = platform_object.realm();
1132+
1133+
if (!m_trusted_type_policy_factory)
1134+
m_trusted_type_policy_factory = realm.create<TrustedTypes::TrustedTypePolicyFactory>(realm);
1135+
return *m_trusted_type_policy_factory;
1136+
}
1137+
11251138
}

Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class WindowOrWorkerGlobalScopeMixin {
103103

104104
[[nodiscard]] GC::Ref<ServiceWorker::CacheStorage> caches();
105105

106+
[[nodiscard]] GC::Ref<TrustedTypes::TrustedTypePolicyFactory> trusted_types();
107+
106108
protected:
107109
void initialize(JS::Realm&);
108110
void visit_edges(JS::Cell::Visitor&);
@@ -151,6 +153,8 @@ class WindowOrWorkerGlobalScopeMixin {
151153

152154
GC::Ptr<ServiceWorker::CacheStorage> m_cache_storage;
153155

156+
GC::Ptr<TrustedTypes::TrustedTypePolicyFactory> m_trusted_type_policy_factory;
157+
154158
bool m_error_reporting_mode { false };
155159

156160
WebSockets::WebSocket::List m_registered_web_sockets;

Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.idl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#import <HTML/MessagePort.idl>
77
#import <IndexedDB/IDBFactory.idl>
88
#import <ServiceWorker/CacheStorage.idl>
9+
#import <TrustedTypes/TrustedTypePolicyFactory.idl>
910

1011
// https://html.spec.whatwg.org/multipage/webappapis.html#timerhandler
1112
typedef (DOMString or Function) TimerHandler;
@@ -43,4 +44,7 @@ interface mixin WindowOrWorkerGlobalScope {
4344

4445
// https://w3c.github.io/ServiceWorker/#cache-storage-interface
4546
[SecureContext, SameObject] readonly attribute CacheStorage caches;
47+
48+
// https://w3c.github.io/trusted-types/dist/spec/#extensions-to-the-windoworworkerglobalscope-interface
49+
readonly attribute TrustedTypePolicyFactory trustedTypes;
4650
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2025, Miguel Sacristán Izcue <miguel_tete17@hotmail.com>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibWeb/TrustedTypes/TrustedTypePolicyFactory.h>
8+
9+
#include <LibJS/Runtime/Realm.h>
10+
#include <LibWeb/Bindings/Intrinsics.h>
11+
12+
namespace Web::TrustedTypes {
13+
14+
GC_DEFINE_ALLOCATOR(TrustedTypePolicyFactory);
15+
16+
GC::Ref<TrustedTypePolicyFactory> TrustedTypePolicyFactory::create(JS::Realm& realm)
17+
{
18+
return realm.create<TrustedTypePolicyFactory>(realm);
19+
}
20+
21+
TrustedTypePolicyFactory::TrustedTypePolicyFactory(JS::Realm& realm)
22+
: PlatformObject(realm)
23+
{
24+
}
25+
26+
void TrustedTypePolicyFactory::initialize(JS::Realm& realm)
27+
{
28+
WEB_SET_PROTOTYPE_FOR_INTERFACE(TrustedTypePolicyFactory);
29+
Base::initialize(realm);
30+
}
31+
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2025, Miguel Sacristán Izcue <miguel_tete17@hotmail.com>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <LibJS/Forward.h>
10+
#include <LibWeb/Bindings/PlatformObject.h>
11+
#include <LibWeb/Bindings/TrustedTypePolicyFactoryPrototype.h>
12+
13+
namespace Web::TrustedTypes {
14+
15+
class TrustedTypePolicyFactory final : public Bindings::PlatformObject {
16+
WEB_PLATFORM_OBJECT(TrustedTypePolicyFactory, Bindings::PlatformObject);
17+
GC_DECLARE_ALLOCATOR(TrustedTypePolicyFactory);
18+
19+
public:
20+
[[nodiscard]] static GC::Ref<TrustedTypePolicyFactory> create(JS::Realm&);
21+
22+
virtual ~TrustedTypePolicyFactory() override { }
23+
24+
private:
25+
explicit TrustedTypePolicyFactory(JS::Realm&);
26+
virtual void initialize(JS::Realm&) override;
27+
28+
Vector<String> m_created_policy_names;
29+
};
30+
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://w3c.github.io/trusted-types/dist/spec/#trustedtypepolicyfactory
2+
[Exposed=(Window,Worker)]
3+
interface TrustedTypePolicyFactory {
4+
[FIXME] TrustedTypePolicy createPolicy(
5+
DOMString policyName, optional TrustedTypePolicyOptions policyOptions = {});
6+
[FIXME] boolean isHTML(any value);
7+
[FIXME] boolean isScript(any value);
8+
[FIXME] boolean isScriptURL(any value);
9+
[FIXME] readonly attribute TrustedHTML emptyHTML;
10+
[FIXME] readonly attribute TrustedScript emptyScript;
11+
[FIXME] DOMString? getAttributeType(
12+
DOMString tagName,
13+
DOMString attribute,
14+
optional DOMString? elementNs = "",
15+
optional DOMString? attrNs = "");
16+
[FIXME] DOMString? getPropertyType(
17+
DOMString tagName,
18+
DOMString property,
19+
optional DOMString? elementNs = "");
20+
[FIXME] readonly attribute TrustedTypePolicy? defaultPolicy;
21+
};

Libraries/LibWeb/idl_files.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ libweb_js_bindings(Streams/TransformStreamDefaultController)
324324
libweb_js_bindings(Streams/WritableStream)
325325
libweb_js_bindings(Streams/WritableStreamDefaultController)
326326
libweb_js_bindings(Streams/WritableStreamDefaultWriter)
327+
libweb_js_bindings(TrustedTypes/TrustedTypePolicyFactory)
327328
libweb_js_bindings(SVG/SVGAElement)
328329
libweb_js_bindings(SVG/SVGAnimatedEnumeration)
329330
libweb_js_bindings(SVG/SVGAnimatedLength)

Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ static bool is_platform_object(Type const& type)
116116
"TextMetrics"sv,
117117
"TextTrack"sv,
118118
"TimeRanges"sv,
119+
"TrustedTypePolicyFactory"sv,
119120
"URLSearchParams"sv,
120121
"VTTRegion"sv,
121122
"VideoTrack"sv,
@@ -4840,6 +4841,7 @@ using namespace Web::ServiceWorker;
48404841
using namespace Web::StorageAPI;
48414842
using namespace Web::Streams;
48424843
using namespace Web::SVG;
4844+
using namespace Web::TrustedTypes;
48434845
using namespace Web::UIEvents;
48444846
using namespace Web::URLPattern;
48454847
using namespace Web::UserTiming;

0 commit comments

Comments
 (0)