2020-04-01 18:53:28 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-01 18:53:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibGUI/DisplayLink.h>
|
2021-06-27 21:48:34 +02:00
|
|
|
#include <LibJS/Runtime/FunctionObject.h>
|
2020-05-18 21:52:50 +02:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2020-10-18 13:43:44 +02:00
|
|
|
#include <LibWeb/DOM/Event.h>
|
|
|
|
#include <LibWeb/DOM/EventDispatcher.h>
|
2020-06-27 18:30:29 +02:00
|
|
|
#include <LibWeb/DOM/Timer.h>
|
2020-04-01 18:53:28 +02:00
|
|
|
#include <LibWeb/DOM/Window.h>
|
2020-09-29 18:19:18 +02:00
|
|
|
#include <LibWeb/HighResolutionTime/Performance.h>
|
2021-09-08 11:27:46 +02:00
|
|
|
#include <LibWeb/Layout/InitialContainingBlock.h>
|
2021-05-30 12:36:53 +02:00
|
|
|
#include <LibWeb/Page/BrowsingContext.h>
|
2021-08-24 16:28:08 +02:00
|
|
|
#include <LibWeb/Page/Page.h>
|
2020-04-01 18:53:28 +02:00
|
|
|
|
2020-07-26 19:37:56 +02:00
|
|
|
namespace Web::DOM {
|
2020-04-01 18:53:28 +02:00
|
|
|
|
|
|
|
NonnullRefPtr<Window> Window::create_with_document(Document& document)
|
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
return adopt_ref(*new Window(document));
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Window::Window(Document& document)
|
2020-10-18 13:43:44 +02:00
|
|
|
: EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
|
|
|
|
, m_document(document)
|
2020-09-29 18:19:18 +02:00
|
|
|
, m_performance(make<HighResolutionTime::Performance>(*this))
|
2021-04-04 00:14:39 +02:00
|
|
|
, m_screen(CSS::Screen::create(*this))
|
2020-04-01 18:53:28 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Window::~Window()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-20 17:45:27 +02:00
|
|
|
void Window::set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject& wrapper)
|
|
|
|
{
|
|
|
|
m_wrapper = wrapper.make_weak_ptr();
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:53:28 +02:00
|
|
|
void Window::alert(const String& message)
|
|
|
|
{
|
2020-11-12 18:23:05 +01:00
|
|
|
if (auto* page = m_document.page())
|
|
|
|
page->client().page_did_request_alert(message);
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:29:26 -04:00
|
|
|
bool Window::confirm(const String& message)
|
|
|
|
{
|
2021-02-10 08:37:13 +01:00
|
|
|
if (auto* page = m_document.page())
|
|
|
|
return page->client().page_did_request_confirm(message);
|
|
|
|
return false;
|
2020-04-16 21:29:26 -04:00
|
|
|
}
|
|
|
|
|
2021-02-20 12:05:18 +01:00
|
|
|
String Window::prompt(const String& message, const String& default_)
|
|
|
|
{
|
|
|
|
if (auto* page = m_document.page())
|
|
|
|
return page->client().page_did_request_prompt(message, default_);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
i32 Window::set_interval(JS::FunctionObject& callback, i32 interval)
|
2020-04-01 18:53:28 +02:00
|
|
|
{
|
2020-06-27 18:30:29 +02:00
|
|
|
auto timer = Timer::create_interval(*this, interval, callback);
|
|
|
|
m_timers.set(timer->id(), timer);
|
|
|
|
return timer->id();
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
i32 Window::set_timeout(JS::FunctionObject& callback, i32 interval)
|
2020-04-05 00:56:16 +02:00
|
|
|
{
|
2020-06-27 18:30:29 +02:00
|
|
|
auto timer = Timer::create_timeout(*this, interval, callback);
|
|
|
|
m_timers.set(timer->id(), timer);
|
|
|
|
return timer->id();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::timer_did_fire(Badge<Timer>, Timer& timer)
|
|
|
|
{
|
|
|
|
// We should not be here if there's no JS wrapper for the Window object.
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(wrapper());
|
2020-09-27 20:31:13 +02:00
|
|
|
auto& vm = wrapper()->vm();
|
2020-06-27 18:30:29 +02:00
|
|
|
|
|
|
|
// NOTE: This protector pointer keeps the timer alive until the end of this function no matter what.
|
|
|
|
NonnullRefPtr protector(timer);
|
|
|
|
|
|
|
|
if (timer.type() == Timer::Type::Timeout) {
|
|
|
|
m_timers.remove(timer.id());
|
|
|
|
}
|
|
|
|
|
2020-12-20 16:09:48 -07:00
|
|
|
[[maybe_unused]] auto rc = vm.call(timer.callback(), wrapper());
|
2020-09-27 20:31:13 +02:00
|
|
|
if (vm.exception())
|
|
|
|
vm.clear_exception();
|
2020-06-27 18:30:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
i32 Window::allocate_timer_id(Badge<Timer>)
|
|
|
|
{
|
|
|
|
return m_timer_id_allocator.allocate();
|
|
|
|
}
|
|
|
|
|
2020-11-30 08:03:13 +01:00
|
|
|
void Window::deallocate_timer_id(Badge<Timer>, i32 id)
|
|
|
|
{
|
|
|
|
m_timer_id_allocator.deallocate(id);
|
|
|
|
}
|
|
|
|
|
2020-06-27 18:30:29 +02:00
|
|
|
void Window::clear_timeout(i32 timer_id)
|
|
|
|
{
|
|
|
|
m_timers.remove(timer_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::clear_interval(i32 timer_id)
|
|
|
|
{
|
|
|
|
m_timers.remove(timer_id);
|
2020-04-05 00:56:16 +02:00
|
|
|
}
|
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
i32 Window::request_animation_frame(JS::FunctionObject& callback)
|
2020-04-01 18:53:28 +02:00
|
|
|
{
|
2020-04-21 23:49:09 +02:00
|
|
|
// FIXME: This is extremely fake!
|
|
|
|
static double fake_timestamp = 0;
|
|
|
|
|
2020-04-01 18:53:28 +02:00
|
|
|
i32 link_id = GUI::DisplayLink::register_callback([handle = make_handle(&callback)](i32 link_id) {
|
2021-06-27 21:48:34 +02:00
|
|
|
auto& function = const_cast<JS::FunctionObject&>(static_cast<const JS::FunctionObject&>(*handle.cell()));
|
2020-09-27 20:31:13 +02:00
|
|
|
auto& vm = function.vm();
|
2020-04-21 23:49:09 +02:00
|
|
|
fake_timestamp += 10;
|
2021-03-26 19:35:54 +01:00
|
|
|
[[maybe_unused]] auto rc = vm.call(function, JS::js_undefined(), JS::Value(fake_timestamp));
|
2020-09-27 20:31:13 +02:00
|
|
|
if (vm.exception())
|
|
|
|
vm.clear_exception();
|
2020-04-01 18:53:28 +02:00
|
|
|
GUI::DisplayLink::unregister_callback(link_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
// FIXME: Don't hand out raw DisplayLink ID's to JavaScript!
|
|
|
|
return link_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::cancel_animation_frame(i32 id)
|
|
|
|
{
|
|
|
|
// FIXME: We should not be passing untrusted numbers to DisplayLink::unregister_callback()!
|
|
|
|
GUI::DisplayLink::unregister_callback(id);
|
|
|
|
}
|
|
|
|
|
2020-12-02 12:09:24 +03:30
|
|
|
void Window::did_set_location_href(Badge<Bindings::LocationObject>, const URL& new_href)
|
2020-05-18 21:52:50 +02:00
|
|
|
{
|
2021-05-30 12:36:53 +02:00
|
|
|
auto* frame = document().browsing_context();
|
2020-05-18 21:52:50 +02:00
|
|
|
if (!frame)
|
|
|
|
return;
|
2020-07-07 17:25:33 +02:00
|
|
|
frame->loader().load(new_href, FrameLoader::Type::Navigation);
|
2020-05-18 21:52:50 +02:00
|
|
|
}
|
|
|
|
|
2020-05-18 22:05:13 +02:00
|
|
|
void Window::did_call_location_reload(Badge<Bindings::LocationObject>)
|
|
|
|
{
|
2021-05-30 12:36:53 +02:00
|
|
|
auto* frame = document().browsing_context();
|
2020-05-18 22:05:13 +02:00
|
|
|
if (!frame)
|
|
|
|
return;
|
2020-07-07 17:25:33 +02:00
|
|
|
frame->loader().load(document().url(), FrameLoader::Type::Reload);
|
2020-05-18 22:05:13 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 18:32:39 +00:00
|
|
|
bool Window::dispatch_event(NonnullRefPtr<Event> event)
|
2020-10-18 13:43:44 +02:00
|
|
|
{
|
2020-11-21 18:32:39 +00:00
|
|
|
return EventDispatcher::dispatch(*this, event, true);
|
2020-10-18 13:43:44 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 12:15:02 +01:00
|
|
|
JS::Object* Window::create_wrapper(JS::GlobalObject& global_object)
|
2020-10-18 13:43:44 +02:00
|
|
|
{
|
2021-01-18 12:15:02 +01:00
|
|
|
return &global_object;
|
2020-10-18 13:43:44 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 17:22:01 +01:00
|
|
|
int Window::inner_width() const
|
|
|
|
{
|
|
|
|
if (!document().layout_node())
|
|
|
|
return 0;
|
|
|
|
return document().layout_node()->width();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Window::inner_height() const
|
|
|
|
{
|
|
|
|
if (!document().layout_node())
|
|
|
|
return 0;
|
|
|
|
return document().layout_node()->height();
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|