2020-04-01 18:53:28 +02:00
|
|
|
/*
|
2021-01-23 13:23:17 +01:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2021-09-08 12:22:28 +01:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2020-04-01 18:53:28 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-01 18:53:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2020-11-21 18:32:39 +00:00
|
|
|
#include <AK/TypeCasts.h>
|
2020-06-20 17:45:27 +02:00
|
|
|
#include <AK/Weakable.h>
|
2021-09-28 23:54:42 +01:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2020-04-01 18:53:28 +02:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
|
#include <LibWeb/Forward.h>
|
2021-09-22 16:39:15 +02:00
|
|
|
#include <LibWeb/HTML/GlobalEventHandlers.h>
|
2020-04-01 18:53:28 +02:00
|
|
|
|
|
|
|
|
namespace Web {
|
|
|
|
|
namespace Bindings {
|
|
|
|
|
|
2020-06-20 17:45:27 +02:00
|
|
|
class WindowObject final
|
|
|
|
|
: public JS::GlobalObject
|
|
|
|
|
, public Weakable<WindowObject> {
|
2021-03-17 16:52:26 +01:00
|
|
|
JS_OBJECT(WindowObject, JS::GlobalObject);
|
|
|
|
|
|
2020-04-01 18:53:28 +02:00
|
|
|
public:
|
2020-07-26 19:37:56 +02:00
|
|
|
explicit WindowObject(DOM::Window&);
|
2021-03-17 16:52:26 +01:00
|
|
|
virtual void initialize_global_object() override;
|
2020-04-01 18:53:28 +02:00
|
|
|
virtual ~WindowObject() override;
|
|
|
|
|
|
2020-07-26 19:37:56 +02:00
|
|
|
DOM::Window& impl() { return *m_impl; }
|
|
|
|
|
const DOM::Window& impl() const { return *m_impl; }
|
2020-04-01 18:53:28 +02:00
|
|
|
|
2020-09-22 18:24:49 +02:00
|
|
|
Origin origin() const;
|
|
|
|
|
|
2021-09-12 14:59:49 +01:00
|
|
|
LocationObject* location_object() { return m_location_object; }
|
|
|
|
|
LocationObject const* location_object() const { return m_location_object; }
|
|
|
|
|
|
2021-01-18 09:50:00 +01:00
|
|
|
JS::Object* web_prototype(const String& class_name) { return m_prototypes.get(class_name).value_or(nullptr); }
|
|
|
|
|
JS::NativeFunction* web_constructor(const String& class_name) { return m_constructors.get(class_name).value_or(nullptr); }
|
2021-01-18 11:36:34 +01:00
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
JS::Object& ensure_web_prototype(const String& class_name)
|
|
|
|
|
{
|
|
|
|
|
auto it = m_prototypes.find(class_name);
|
|
|
|
|
if (it != m_prototypes.end())
|
|
|
|
|
return *it->value;
|
|
|
|
|
auto* prototype = heap().allocate<T>(*this, *this);
|
|
|
|
|
m_prototypes.set(class_name, prototype);
|
|
|
|
|
return *prototype;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
JS::NativeFunction& ensure_web_constructor(const String& class_name)
|
|
|
|
|
{
|
|
|
|
|
auto it = m_constructors.find(class_name);
|
|
|
|
|
if (it != m_constructors.end())
|
|
|
|
|
return *it->value;
|
|
|
|
|
auto* constructor = heap().allocate<T>(*this, *this);
|
|
|
|
|
m_constructors.set(class_name, constructor);
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(class_name, JS::Value(constructor), JS::Attribute::Writable | JS::Attribute::Configurable);
|
2021-01-18 11:36:34 +01:00
|
|
|
return *constructor;
|
|
|
|
|
}
|
2021-01-18 09:50:00 +01:00
|
|
|
|
2021-09-28 23:54:42 +01:00
|
|
|
virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(JS::Object* prototype) override;
|
2021-09-26 15:26:30 +01:00
|
|
|
|
2020-04-01 18:53:28 +02:00
|
|
|
private:
|
2020-11-28 14:33:36 +01:00
|
|
|
virtual void visit_edges(Visitor&) override;
|
2020-04-01 18:53:28 +02:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(top_getter);
|
2021-04-07 11:19:51 +02:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(document_getter);
|
2020-04-01 18:53:28 +02:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(performance_getter);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(history_getter);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(screen_getter);
|
2020-09-29 18:19:18 +02:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(event_getter);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(event_setter);
|
2020-11-21 18:32:39 +00:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(inner_width_getter);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(inner_height_getter);
|
2021-03-16 17:22:01 +01:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(parent_getter);
|
2021-04-11 22:34:57 +01:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(device_pixel_ratio_getter);
|
2021-09-27 16:34:06 +02:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(scroll_x_getter);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(scroll_y_getter);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(scroll);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(scroll_by);
|
2021-09-08 12:22:28 +01:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(screen_x_getter);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(screen_y_getter);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(screen_left_getter);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(screen_top_getter);
|
2021-09-29 00:15:26 +02:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(alert);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(confirm);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(prompt);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(set_interval);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(set_timeout);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(clear_interval);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(clear_timeout);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(request_animation_frame);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(atob);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(btoa);
|
2020-04-01 18:53:28 +02:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(get_computed_style);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(match_media);
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(get_selection);
|
2021-09-11 00:33:30 +02:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(queue_microtask);
|
2021-09-26 14:36:20 +02:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
JS_DECLARE_NATIVE_FUNCTION(crypto_getter);
|
2021-09-30 20:02:55 +03:00
|
|
|
|
2021-10-31 08:31:43 -04:00
|
|
|
#define __ENUMERATE(attribute, event_name) \
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(attribute##_getter); \
|
|
|
|
|
JS_DECLARE_NATIVE_FUNCTION(attribute##_setter);
|
2021-09-22 16:39:15 +02:00
|
|
|
ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE);
|
|
|
|
|
#undef __ENUMERATE
|
|
|
|
|
|
2020-07-26 19:37:56 +02:00
|
|
|
NonnullRefPtr<DOM::Window> m_impl;
|
2020-04-08 21:11:51 +02:00
|
|
|
|
2021-09-12 16:23:30 +02:00
|
|
|
LocationObject* m_location_object { nullptr };
|
2021-09-12 14:59:49 +01:00
|
|
|
|
2021-01-18 09:50:00 +01:00
|
|
|
HashMap<String, JS::Object*> m_prototypes;
|
|
|
|
|
HashMap<String, JS::NativeFunction*> m_constructors;
|
2020-04-01 18:53:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|