/src/serenity/Userland/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibWeb/Bindings/HostDefined.h> |
8 | | #include <LibWeb/Bindings/Intrinsics.h> |
9 | | #include <LibWeb/DOM/Document.h> |
10 | | #include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h> |
11 | | #include <LibWeb/HTML/Window.h> |
12 | | |
13 | | namespace Web::HTML { |
14 | | |
15 | | JS_DEFINE_ALLOCATOR(WindowEnvironmentSettingsObject); |
16 | | |
17 | | WindowEnvironmentSettingsObject::WindowEnvironmentSettingsObject(Window& window, NonnullOwnPtr<JS::ExecutionContext> execution_context) |
18 | 0 | : EnvironmentSettingsObject(move(execution_context)) |
19 | 0 | , m_window(window) |
20 | 0 | { |
21 | 0 | } |
22 | | |
23 | 0 | WindowEnvironmentSettingsObject::~WindowEnvironmentSettingsObject() = default; |
24 | | |
25 | | void WindowEnvironmentSettingsObject::visit_edges(JS::Cell::Visitor& visitor) |
26 | 0 | { |
27 | 0 | Base::visit_edges(visitor); |
28 | 0 | visitor.visit(m_window); |
29 | 0 | } |
30 | | |
31 | | // https://html.spec.whatwg.org/multipage/window-object.html#set-up-a-window-environment-settings-object |
32 | | void WindowEnvironmentSettingsObject::setup(Page& page, URL::URL const& creation_url, NonnullOwnPtr<JS::ExecutionContext> execution_context, JS::GCPtr<Environment> reserved_environment, URL::URL top_level_creation_url, URL::Origin top_level_origin) |
33 | 0 | { |
34 | | // 1. Let realm be the value of execution context's Realm component. |
35 | 0 | auto realm = execution_context->realm; |
36 | 0 | VERIFY(realm); |
37 | | |
38 | | // 2. Let window be realm's global object. |
39 | 0 | auto& window = verify_cast<HTML::Window>(realm->global_object()); |
40 | | |
41 | | // 3. Let settings object be a new environment settings object whose algorithms are defined as follows: |
42 | | // NOTE: See the functions defined for this class. |
43 | 0 | auto settings_object = realm->heap().allocate<WindowEnvironmentSettingsObject>(*realm, window, move(execution_context)); |
44 | | |
45 | | // 4. If reservedEnvironment is non-null, then: |
46 | 0 | if (reserved_environment) { |
47 | | // FIXME: 1. Set settings object's id to reservedEnvironment's id, |
48 | | // target browsing context to reservedEnvironment's target browsing context, |
49 | | // and active service worker to reservedEnvironment's active service worker. |
50 | 0 | settings_object->id = reserved_environment->id; |
51 | 0 | settings_object->target_browsing_context = reserved_environment->target_browsing_context; |
52 | | |
53 | | // 2. Set reservedEnvironment's id to the empty string. |
54 | 0 | reserved_environment->id = String {}; |
55 | 0 | } |
56 | | |
57 | | // 5. Otherwise, ... |
58 | 0 | else { |
59 | | // FIXME: ...set settings object's id to a new unique opaque string, |
60 | | // settings object's target browsing context to null, |
61 | | // and settings object's active service worker to null. |
62 | 0 | static i64 next_id = 1; |
63 | 0 | settings_object->id = String::number(next_id++); |
64 | 0 | settings_object->target_browsing_context = nullptr; |
65 | 0 | } |
66 | | |
67 | | // 6. Set settings object's creation URL to creationURL, |
68 | | // settings object's top-level creation URL to topLevelCreationURL, |
69 | | // and settings object's top-level origin to topLevelOrigin. |
70 | 0 | settings_object->creation_url = creation_url; |
71 | 0 | settings_object->top_level_creation_url = move(top_level_creation_url); |
72 | 0 | settings_object->top_level_origin = move(top_level_origin); |
73 | | |
74 | | // 7. Set realm's [[HostDefined]] field to settings object. |
75 | | // Non-Standard: We store the ESO next to the web intrinsics in a custom HostDefined object |
76 | 0 | auto intrinsics = realm->heap().allocate<Bindings::Intrinsics>(*realm, *realm); |
77 | 0 | auto host_defined = make<Bindings::HostDefined>(settings_object, intrinsics, page); |
78 | 0 | realm->set_host_defined(move(host_defined)); |
79 | | |
80 | | // Non-Standard: We cannot fully initialize window object until *after* the we set up |
81 | | // the realm's [[HostDefined]] internal slot as the internal slot contains the web platform intrinsics |
82 | 0 | MUST(window.initialize_web_interfaces({})); |
83 | 0 | } |
84 | | |
85 | | // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:responsible-document |
86 | | JS::GCPtr<DOM::Document> WindowEnvironmentSettingsObject::responsible_document() |
87 | 0 | { |
88 | | // Return window's associated Document. |
89 | 0 | return m_window->associated_document(); |
90 | 0 | } |
91 | | |
92 | | // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:api-url-character-encoding |
93 | | String WindowEnvironmentSettingsObject::api_url_character_encoding() |
94 | 0 | { |
95 | | // Return the current character encoding of window's associated Document. |
96 | 0 | return m_window->associated_document().encoding_or_default(); |
97 | 0 | } |
98 | | |
99 | | // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:api-base-url |
100 | | URL::URL WindowEnvironmentSettingsObject::api_base_url() |
101 | 0 | { |
102 | | // Return the current base URL of window's associated Document. |
103 | 0 | return m_window->associated_document().base_url(); |
104 | 0 | } |
105 | | |
106 | | // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:concept-settings-object-origin |
107 | | URL::Origin WindowEnvironmentSettingsObject::origin() |
108 | 0 | { |
109 | | // Return the origin of window's associated Document. |
110 | 0 | return m_window->associated_document().origin(); |
111 | 0 | } |
112 | | |
113 | | // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:concept-settings-object-policy-container |
114 | | PolicyContainer WindowEnvironmentSettingsObject::policy_container() |
115 | 0 | { |
116 | | // Return the policy container of window's associated Document. |
117 | 0 | return m_window->associated_document().policy_container(); |
118 | 0 | } |
119 | | |
120 | | // https://html.spec.whatwg.org/multipage/window-object.html#script-settings-for-window-objects:concept-settings-object-cross-origin-isolated-capability |
121 | | CanUseCrossOriginIsolatedAPIs WindowEnvironmentSettingsObject::cross_origin_isolated_capability() |
122 | 0 | { |
123 | | // FIXME: Return true if both of the following hold, and false otherwise: |
124 | | // 1. realm's agent cluster's cross-origin-isolation mode is "concrete", and |
125 | | // 2. window's associated Document is allowed to use the "cross-origin-isolated" feature. |
126 | 0 | return CanUseCrossOriginIsolatedAPIs::Yes; |
127 | 0 | } |
128 | | |
129 | | } |