Serenity Operating System
1/*
2 * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
3 * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#pragma once
9
10#include <AK/URL.h>
11#include <LibJS/Runtime/ExecutionContext.h>
12#include <LibJS/Runtime/GlobalObject.h>
13#include <LibJS/Runtime/Object.h>
14#include <LibJS/Runtime/Realm.h>
15#include <LibWeb/HTML/EventLoop/EventLoop.h>
16#include <LibWeb/HTML/Origin.h>
17#include <LibWeb/HTML/Scripting/ModuleMap.h>
18
19namespace Web::HTML {
20
21// https://html.spec.whatwg.org/multipage/webappapis.html#environment
22struct Environment {
23 virtual ~Environment() = default;
24
25 // An id https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-id
26 DeprecatedString id;
27
28 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-creation-url
29 AK::URL creation_url;
30
31 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-creation-url
32 AK::URL top_level_creation_url;
33
34 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-top-level-origin
35 Origin top_level_origin;
36
37 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-context
38 JS::GCPtr<BrowsingContext> target_browsing_context;
39
40 // FIXME: An active service worker https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-active-service-worker
41
42 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-execution-ready-flag
43 bool execution_ready { false };
44};
45
46enum class CanUseCrossOriginIsolatedAPIs {
47 No,
48 Yes,
49};
50
51enum class RunScriptDecision {
52 Run,
53 DoNotRun,
54};
55
56// https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object
57struct EnvironmentSettingsObject
58 : public JS::Cell
59 , public Environment {
60 JS_CELL(EnvironmentSettingsObject, JS::Cell);
61
62 virtual ~EnvironmentSettingsObject() override;
63
64 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-target-browsing-context
65 JS::ExecutionContext& realm_execution_context();
66
67 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-module-map
68 ModuleMap& module_map();
69
70 // https://html.spec.whatwg.org/multipage/webappapis.html#responsible-document
71 virtual JS::GCPtr<DOM::Document> responsible_document() = 0;
72
73 // https://html.spec.whatwg.org/multipage/webappapis.html#api-url-character-encoding
74 virtual DeprecatedString api_url_character_encoding() = 0;
75
76 // https://html.spec.whatwg.org/multipage/webappapis.html#api-base-url
77 virtual AK::URL api_base_url() = 0;
78
79 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-origin
80 virtual Origin origin() = 0;
81
82 // A policy container https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-policy-container
83 virtual PolicyContainer policy_container() = 0;
84
85 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-cross-origin-isolated-capability
86 virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() = 0;
87
88 AK::URL parse_url(StringView);
89
90 JS::Realm& realm();
91 JS::Object& global_object();
92 EventLoop& responsible_event_loop();
93
94 RunScriptDecision can_run_script();
95 void prepare_to_run_script();
96 void clean_up_after_running_script();
97
98 void prepare_to_run_callback();
99 void clean_up_after_running_callback();
100
101 void push_onto_outstanding_rejected_promises_weak_set(JS::Promise*);
102
103 // Returns true if removed, false otherwise.
104 bool remove_from_outstanding_rejected_promises_weak_set(JS::Promise*);
105
106 void push_onto_about_to_be_notified_rejected_promises_list(JS::NonnullGCPtr<JS::Promise>);
107
108 // Returns true if removed, false otherwise.
109 bool remove_from_about_to_be_notified_rejected_promises_list(JS::NonnullGCPtr<JS::Promise>);
110
111 void notify_about_rejected_promises(Badge<EventLoop>);
112
113 bool is_scripting_enabled() const;
114 bool is_scripting_disabled() const;
115
116 bool module_type_allowed(DeprecatedString const& module_type) const;
117
118 void disallow_further_import_maps();
119
120protected:
121 explicit EnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext>);
122
123 virtual void visit_edges(Cell::Visitor&) override;
124
125private:
126 NonnullOwnPtr<JS::ExecutionContext> m_realm_execution_context;
127 ModuleMap m_module_map;
128
129 EventLoop* m_responsible_event_loop { nullptr };
130
131 // https://html.spec.whatwg.org/multipage/webappapis.html#outstanding-rejected-promises-weak-set
132 // The outstanding rejected promises weak set must not create strong references to any of its members, and implementations are free to limit its size, e.g. by removing old entries from it when new ones are added.
133 Vector<JS::Promise*> m_outstanding_rejected_promises_weak_set;
134
135 // https://html.spec.whatwg.org/multipage/webappapis.html#about-to-be-notified-rejected-promises-list
136 Vector<JS::Handle<JS::Promise>> m_about_to_be_notified_rejected_promises_list;
137};
138
139EnvironmentSettingsObject& incumbent_settings_object();
140JS::Realm& incumbent_realm();
141JS::Object& incumbent_global_object();
142EnvironmentSettingsObject& current_settings_object();
143JS::Object& current_global_object();
144JS::Realm& relevant_realm(JS::Object const&);
145EnvironmentSettingsObject& relevant_settings_object(JS::Object const&);
146EnvironmentSettingsObject& relevant_settings_object(DOM::Node const&);
147JS::Object& relevant_global_object(JS::Object const&);
148JS::Realm& entry_realm();
149EnvironmentSettingsObject& entry_settings_object();
150JS::Object& entry_global_object();
151[[nodiscard]] bool is_secure_context(Environment const&);
152[[nodiscard]] bool is_non_secure_context(Environment const&);
153
154}