Serenity Operating System
at master 263 lines 10 kB view raw
1/* 2 * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#pragma once 9 10#include <AK/Badge.h> 11#include <AK/IDAllocator.h> 12#include <AK/RefPtr.h> 13#include <AK/TypeCasts.h> 14#include <AK/URL.h> 15#include <LibJS/Heap/Heap.h> 16#include <LibWeb/Bindings/Intrinsics.h> 17#include <LibWeb/Bindings/WindowGlobalMixin.h> 18#include <LibWeb/DOM/EventTarget.h> 19#include <LibWeb/Forward.h> 20#include <LibWeb/HTML/AnimationFrameCallbackDriver.h> 21#include <LibWeb/HTML/CrossOrigin/CrossOriginPropertyDescriptorMap.h> 22#include <LibWeb/HTML/GlobalEventHandlers.h> 23#include <LibWeb/HTML/MimeType.h> 24#include <LibWeb/HTML/Plugin.h> 25#include <LibWeb/HTML/Scripting/ImportMap.h> 26#include <LibWeb/HTML/WindowEventHandlers.h> 27#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h> 28#include <LibWeb/RequestIdleCallback/IdleRequest.h> 29 30namespace Web::HTML { 31 32class IdleCallback; 33 34// https://html.spec.whatwg.org/#timerhandler 35using TimerHandler = Variant<JS::Handle<WebIDL::CallbackType>, DeprecatedString>; 36 37// https://w3c.github.io/csswg-drafts/cssom-view/#dictdef-scrolloptions 38struct ScrollOptions { 39 Bindings::ScrollBehavior behavior { Bindings::ScrollBehavior::Auto }; 40}; 41 42// https://w3c.github.io/csswg-drafts/cssom-view/#dictdef-scrolltooptions 43struct ScrollToOptions : public ScrollOptions { 44 Optional<double> left; 45 Optional<double> top; 46}; 47 48class Window final 49 : public DOM::EventTarget 50 , public GlobalEventHandlers 51 , public WindowEventHandlers 52 , public WindowOrWorkerGlobalScopeMixin 53 , public Bindings::WindowGlobalMixin { 54 WEB_PLATFORM_OBJECT(Window, DOM::EventTarget); 55 56public: 57 static WebIDL::ExceptionOr<JS::NonnullGCPtr<Window>> create(JS::Realm&); 58 59 ~Window(); 60 61 using WindowOrWorkerGlobalScopeMixin::atob; 62 using WindowOrWorkerGlobalScopeMixin::btoa; 63 using WindowOrWorkerGlobalScopeMixin::fetch; 64 using WindowOrWorkerGlobalScopeMixin::queue_microtask; 65 using WindowOrWorkerGlobalScopeMixin::structured_clone; 66 67 // ^DOM::EventTarget 68 virtual bool dispatch_event(DOM::Event&) override; 69 70 // ^WindowOrWorkerGlobalScopeMixin 71 virtual Bindings::PlatformObject& this_impl() override { return *this; } 72 virtual Bindings::PlatformObject const& this_impl() const override { return *this; } 73 74 // ^JS::Object 75 virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(JS::Object* prototype) override; 76 77 Page* page(); 78 Page const* page() const; 79 80 // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window 81 DOM::Document const& associated_document() const { return *m_associated_document; } 82 DOM::Document& associated_document() { return *m_associated_document; } 83 void set_associated_document(DOM::Document&); 84 85 // https://html.spec.whatwg.org/multipage/window-object.html#window-bc 86 BrowsingContext const* browsing_context() const; 87 BrowsingContext* browsing_context(); 88 89 size_t document_tree_child_browsing_context_count() const; 90 91 ImportMap const& import_map() const { return m_import_map; } 92 93 bool import_maps_allowed() const { return m_import_maps_allowed; } 94 void set_import_maps_allowed(bool import_maps_allowed) { m_import_maps_allowed = import_maps_allowed; } 95 96 WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> open_impl(StringView url, StringView target, StringView features); 97 bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); } 98 99 i32 set_timeout_impl(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments); 100 i32 set_interval_impl(TimerHandler, i32 timeout, JS::MarkedVector<JS::Value> arguments); 101 void clear_timeout_impl(i32); 102 void clear_interval_impl(i32); 103 104 void did_set_location_href(Badge<Location>, AK::URL const& new_href); 105 void did_call_location_reload(Badge<Location>); 106 void did_call_location_replace(Badge<Location>, DeprecatedString url); 107 108 void deallocate_timer_id(Badge<Timer>, i32); 109 110 DOM::Event* current_event() { return m_current_event.ptr(); } 111 DOM::Event const* current_event() const { return m_current_event.ptr(); } 112 void set_current_event(DOM::Event* event); 113 114 Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const; 115 116 void fire_a_page_transition_event(DeprecatedFlyString const& event_name, bool persisted); 117 118 WebIDL::ExceptionOr<JS::NonnullGCPtr<Storage>> local_storage(); 119 WebIDL::ExceptionOr<JS::NonnullGCPtr<Storage>> session_storage(); 120 121 void start_an_idle_period(); 122 123 AnimationFrameCallbackDriver& animation_frame_callback_driver() { return m_animation_frame_callback_driver; } 124 125 // https://html.spec.whatwg.org/multipage/interaction.html#transient-activation 126 bool has_transient_activation() const; 127 128 WebIDL::ExceptionOr<void> initialize_web_interfaces(Badge<WindowEnvironmentSettingsObject>); 129 130 Vector<JS::NonnullGCPtr<Plugin>> pdf_viewer_plugin_objects(); 131 Vector<JS::NonnullGCPtr<MimeType>> pdf_viewer_mime_type_objects(); 132 133 CrossOriginPropertyDescriptorMap const& cross_origin_property_descriptor_map() const { return m_cross_origin_property_descriptor_map; } 134 CrossOriginPropertyDescriptorMap& cross_origin_property_descriptor_map() { return m_cross_origin_property_descriptor_map; } 135 136 // JS API functions 137 JS::NonnullGCPtr<WindowProxy> window() const; 138 JS::NonnullGCPtr<WindowProxy> self() const; 139 JS::NonnullGCPtr<DOM::Document const> document() const; 140 String name() const; 141 void set_name(String const&); 142 WebIDL::ExceptionOr<JS::NonnullGCPtr<Location>> location(); 143 JS::NonnullGCPtr<History> history() const; 144 void focus(); 145 146 JS::NonnullGCPtr<WindowProxy> frames() const; 147 u32 length() const; 148 JS::GCPtr<WindowProxy const> top() const; 149 JS::GCPtr<WindowProxy const> parent() const; 150 JS::GCPtr<DOM::Element const> frame_element() const; 151 WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> open(Optional<String> const& url, Optional<String> const& target, Optional<String> const& features); 152 153 WebIDL::ExceptionOr<JS::NonnullGCPtr<Navigator>> navigator(); 154 155 void alert(String const& message = {}); 156 bool confirm(Optional<String> const& message); 157 Optional<String> prompt(Optional<String> const& message, Optional<String> const& default_); 158 159 void post_message(JS::Value message, String const&); 160 161 Variant<JS::Handle<DOM::Event>, JS::Value> event() const; 162 163 WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::CSSStyleDeclaration>> get_computed_style(DOM::Element&, Optional<String> const& pseudo_element) const; 164 165 WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::MediaQueryList>> match_media(String const& query); 166 WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::Screen>> screen(); 167 168 i32 inner_width() const; 169 i32 inner_height() const; 170 171 double scroll_x() const; 172 double scroll_y() const; 173 void scroll(ScrollToOptions const&); 174 void scroll(double x, double y); 175 void scroll_by(ScrollToOptions); 176 void scroll_by(double x, double y); 177 178 i32 screen_x() const; 179 i32 screen_y() const; 180 i32 outer_width() const; 181 i32 outer_height() const; 182 double device_pixel_ratio() const; 183 184 i32 request_animation_frame(WebIDL::CallbackType&); 185 void cancel_animation_frame(i32 handle); 186 187 u32 request_idle_callback(WebIDL::CallbackType&, RequestIdleCallback::IdleRequestOptions const&); 188 void cancel_idle_callback(u32 handle); 189 190 JS::GCPtr<Selection::Selection> get_selection() const; 191 192 WebIDL::ExceptionOr<JS::NonnullGCPtr<HighResolutionTime::Performance>> performance(); 193 194 WebIDL::ExceptionOr<JS::NonnullGCPtr<Crypto::Crypto>> crypto(); 195 196private: 197 explicit Window(JS::Realm&); 198 199 virtual void visit_edges(Cell::Visitor&) override; 200 201 // ^HTML::GlobalEventHandlers 202 virtual DOM::EventTarget& global_event_handlers_to_event_target(DeprecatedFlyString const&) override { return *this; } 203 204 // ^HTML::WindowEventHandlers 205 virtual DOM::EventTarget& window_event_handlers_to_event_target() override { return *this; } 206 207 enum class Repeat { 208 Yes, 209 No, 210 }; 211 i32 run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS::MarkedVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {}); 212 213 void invoke_idle_callbacks(); 214 215 // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window 216 JS::GCPtr<DOM::Document> m_associated_document; 217 218 JS::GCPtr<DOM::Event> m_current_event; 219 220 IDAllocator m_timer_id_allocator; 221 HashMap<int, JS::NonnullGCPtr<Timer>> m_timers; 222 223 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-window-import-map 224 ImportMap m_import_map; 225 226 // https://html.spec.whatwg.org/multipage/webappapis.html#import-maps-allowed 227 bool m_import_maps_allowed { true }; 228 229 JS::GCPtr<HighResolutionTime::Performance> m_performance; 230 JS::GCPtr<Crypto::Crypto> m_crypto; 231 JS::GCPtr<CSS::Screen> m_screen; 232 JS::GCPtr<Navigator> m_navigator; 233 JS::GCPtr<Location> m_location; 234 235 AnimationFrameCallbackDriver m_animation_frame_callback_driver; 236 237 // https://w3c.github.io/requestidlecallback/#dfn-list-of-idle-request-callbacks 238 Vector<NonnullRefPtr<IdleCallback>> m_idle_request_callbacks; 239 // https://w3c.github.io/requestidlecallback/#dfn-list-of-runnable-idle-callbacks 240 Vector<NonnullRefPtr<IdleCallback>> m_runnable_idle_callbacks; 241 // https://w3c.github.io/requestidlecallback/#dfn-idle-callback-identifier 242 u32 m_idle_callback_identifier = 0; 243 244 // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-plugin-objects 245 Vector<JS::NonnullGCPtr<Plugin>> m_pdf_viewer_plugin_objects; 246 247 // https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-mime-type-objects 248 Vector<JS::NonnullGCPtr<MimeType>> m_pdf_viewer_mime_type_objects; 249 250 // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap 251 CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map; 252 253 JS_DECLARE_NATIVE_FUNCTION(location_setter); 254 255 JS_DECLARE_NATIVE_FUNCTION(set_interval); 256 JS_DECLARE_NATIVE_FUNCTION(set_timeout); 257 JS_DECLARE_NATIVE_FUNCTION(clear_interval); 258 JS_DECLARE_NATIVE_FUNCTION(clear_timeout); 259}; 260 261void run_animation_frame_callbacks(DOM::Document&, double now); 262 263}