summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/HTML/Window.h
blob: 96f659982667e7469a3737c3d134683c6e9434ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Badge.h>
#include <AK/RefPtr.h>
#include <AK/TypeCasts.h>
#include <AK/URL.h>
#include <LibJS/Heap/Heap.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/WindowGlobalMixin.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/AnimationFrameCallbackDriver.h>
#include <LibWeb/HTML/CrossOrigin/CrossOriginPropertyDescriptorMap.h>
#include <LibWeb/HTML/GlobalEventHandlers.h>
#include <LibWeb/HTML/MimeType.h>
#include <LibWeb/HTML/Plugin.h>
#include <LibWeb/HTML/Scripting/ImportMap.h>
#include <LibWeb/HTML/WindowEventHandlers.h>
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
#include <LibWeb/RequestIdleCallback/IdleRequest.h>

namespace Web::HTML {

class IdleCallback;

// https://w3c.github.io/csswg-drafts/cssom-view/#dictdef-scrolloptions
struct ScrollOptions {
    Bindings::ScrollBehavior behavior { Bindings::ScrollBehavior::Auto };
};

// https://w3c.github.io/csswg-drafts/cssom-view/#dictdef-scrolltooptions
struct ScrollToOptions : public ScrollOptions {
    Optional<double> left;
    Optional<double> top;
};

class Window final
    : public DOM::EventTarget
    , public GlobalEventHandlers
    , public WindowEventHandlers
    , public WindowOrWorkerGlobalScopeMixin
    , public Bindings::WindowGlobalMixin {
    WEB_PLATFORM_OBJECT(Window, DOM::EventTarget);

public:
    static WebIDL::ExceptionOr<JS::NonnullGCPtr<Window>> create(JS::Realm&);

    ~Window();

    using WindowOrWorkerGlobalScopeMixin::atob;
    using WindowOrWorkerGlobalScopeMixin::btoa;
    using WindowOrWorkerGlobalScopeMixin::clear_interval;
    using WindowOrWorkerGlobalScopeMixin::clear_timeout;
    using WindowOrWorkerGlobalScopeMixin::fetch;
    using WindowOrWorkerGlobalScopeMixin::queue_microtask;
    using WindowOrWorkerGlobalScopeMixin::set_interval;
    using WindowOrWorkerGlobalScopeMixin::set_timeout;
    using WindowOrWorkerGlobalScopeMixin::structured_clone;

    // ^DOM::EventTarget
    virtual bool dispatch_event(DOM::Event&) override;

    // ^WindowOrWorkerGlobalScopeMixin
    virtual Bindings::PlatformObject& this_impl() override { return *this; }
    virtual Bindings::PlatformObject const& this_impl() const override { return *this; }

    // ^JS::Object
    virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(JS::Object* prototype) override;

    Page* page();
    Page const* page() const;

    // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
    DOM::Document const& associated_document() const { return *m_associated_document; }
    DOM::Document& associated_document() { return *m_associated_document; }
    void set_associated_document(DOM::Document&);

    // https://html.spec.whatwg.org/multipage/window-object.html#window-bc
    BrowsingContext const* browsing_context() const;
    BrowsingContext* browsing_context();

    size_t document_tree_child_browsing_context_count() const;

    ImportMap const& import_map() const { return m_import_map; }

    bool import_maps_allowed() const { return m_import_maps_allowed; }
    void set_import_maps_allowed(bool import_maps_allowed) { m_import_maps_allowed = import_maps_allowed; }

    WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> open_impl(StringView url, StringView target, StringView features);
    bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }

    void did_set_location_href(Badge<Location>, AK::URL const& new_href);
    void did_call_location_reload(Badge<Location>);
    void did_call_location_replace(Badge<Location>, DeprecatedString url);

    DOM::Event* current_event() { return m_current_event.ptr(); }
    DOM::Event const* current_event() const { return m_current_event.ptr(); }
    void set_current_event(DOM::Event* event);

    Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const;

    void fire_a_page_transition_event(DeprecatedFlyString const& event_name, bool persisted);

    WebIDL::ExceptionOr<JS::NonnullGCPtr<Storage>> local_storage();
    WebIDL::ExceptionOr<JS::NonnullGCPtr<Storage>> session_storage();

    void start_an_idle_period();

    AnimationFrameCallbackDriver& animation_frame_callback_driver() { return m_animation_frame_callback_driver; }

    // https://html.spec.whatwg.org/multipage/interaction.html#transient-activation
    bool has_transient_activation() const;

    WebIDL::ExceptionOr<void> initialize_web_interfaces(Badge<WindowEnvironmentSettingsObject>);

    Vector<JS::NonnullGCPtr<Plugin>> pdf_viewer_plugin_objects();
    Vector<JS::NonnullGCPtr<MimeType>> pdf_viewer_mime_type_objects();

    CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; }
    CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; }

    // JS API functions
    JS::NonnullGCPtr<WindowProxy> window() const;
    JS::NonnullGCPtr<WindowProxy> self() const;
    JS::NonnullGCPtr<DOM::Document const> document() const;
    String name() const;
    void set_name(String const&);
    WebIDL::ExceptionOr<JS::NonnullGCPtr<Location>> location();
    JS::NonnullGCPtr<History> history() const;
    void focus();

    JS::NonnullGCPtr<WindowProxy> frames() const;
    u32 length() const;
    JS::GCPtr<WindowProxy const> top() const;
    JS::GCPtr<WindowProxy const> parent() const;
    JS::GCPtr<DOM::Element const> frame_element() const;
    WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> open(Optional<String> const& url, Optional<String> const& target, Optional<String> const& features);

    WebIDL::ExceptionOr<JS::NonnullGCPtr<Navigator>> navigator();

    void alert(String const& message = {});
    bool confirm(Optional<String> const& message);
    Optional<String> prompt(Optional<String> const& message, Optional<String> const& default_);

    void post_message(JS::Value message, String const&);

    Variant<JS::Handle<DOM::Event>, JS::Value> event() const;

    WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::CSSStyleDeclaration>> get_computed_style(DOM::Element&, Optional<String> const& pseudo_element) const;

    WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::MediaQueryList>> match_media(String const& query);
    WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::Screen>> screen();

    i32 inner_width() const;
    i32 inner_height() const;

    double scroll_x() const;
    double scroll_y() const;
    void scroll(ScrollToOptions const&);
    void scroll(double x, double y);
    void scroll_by(ScrollToOptions);
    void scroll_by(double x, double y);

    i32 screen_x() const;
    i32 screen_y() const;
    i32 outer_width() const;
    i32 outer_height() const;
    double device_pixel_ratio() const;

    i32 request_animation_frame(WebIDL::CallbackType&);
    void cancel_animation_frame(i32 handle);

    u32 request_idle_callback(WebIDL::CallbackType&, RequestIdleCallback::IdleRequestOptions const&);
    void cancel_idle_callback(u32 handle);

    JS::GCPtr<Selection::Selection> get_selection() const;

    WebIDL::ExceptionOr<JS::NonnullGCPtr<HighResolutionTime::Performance>> performance();

    WebIDL::ExceptionOr<JS::NonnullGCPtr<Crypto::Crypto>> crypto();

private:
    explicit Window(JS::Realm&);

    virtual void visit_edges(Cell::Visitor&) override;

    // ^HTML::GlobalEventHandlers
    virtual DOM::EventTarget& global_event_handlers_to_event_target(DeprecatedFlyString const&) override { return *this; }

    // ^HTML::WindowEventHandlers
    virtual DOM::EventTarget& window_event_handlers_to_event_target() override { return *this; }

    void invoke_idle_callbacks();

    // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
    JS::GCPtr<DOM::Document> m_associated_document;

    JS::GCPtr<DOM::Event> m_current_event;

    // https://html.spec.whatwg.org/multipage/webappapis.html#concept-window-import-map
    ImportMap m_import_map;

    // https://html.spec.whatwg.org/multipage/webappapis.html#import-maps-allowed
    bool m_import_maps_allowed { true };

    JS::GCPtr<HighResolutionTime::Performance> m_performance;
    JS::GCPtr<Crypto::Crypto> m_crypto;
    JS::GCPtr<CSS::Screen> m_screen;
    JS::GCPtr<Navigator> m_navigator;
    JS::GCPtr<Location> m_location;

    AnimationFrameCallbackDriver m_animation_frame_callback_driver;

    // https://w3c.github.io/requestidlecallback/#dfn-list-of-idle-request-callbacks
    Vector<NonnullRefPtr<IdleCallback>> m_idle_request_callbacks;
    // https://w3c.github.io/requestidlecallback/#dfn-list-of-runnable-idle-callbacks
    Vector<NonnullRefPtr<IdleCallback>> m_runnable_idle_callbacks;
    // https://w3c.github.io/requestidlecallback/#dfn-idle-callback-identifier
    u32 m_idle_callback_identifier = 0;

    // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-plugin-objects
    Vector<JS::NonnullGCPtr<Plugin>> m_pdf_viewer_plugin_objects;

    // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-mime-type-objects
    Vector<JS::NonnullGCPtr<MimeType>> m_pdf_viewer_mime_type_objects;

    // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap
    CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map;
};

void run_animation_frame_callbacks(DOM::Document&, double now);

}