1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 02:27:44 +00:00

LibWeb: Make DOM::Event and all its subclasses GC-allocated

This commit is contained in:
Andreas Kling 2022-08-08 22:29:40 +02:00
parent a4ddb0ef87
commit 7c3db526b0
76 changed files with 892 additions and 565 deletions

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/ProgressEventPrototype.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/XHR/ProgressEvent.h>
namespace Web::XHR {
ProgressEvent* ProgressEvent::create(Bindings::WindowObject& window_object, FlyString const& event_name, ProgressEventInit const& event_init)
{
return window_object.heap().allocate<ProgressEvent>(window_object.realm(), window_object, event_name, event_init);
}
ProgressEvent* ProgressEvent::create_with_global_object(Bindings::WindowObject& window_object, FlyString const& event_name, ProgressEventInit const& event_init)
{
return create(window_object, event_name, event_init);
}
ProgressEvent::ProgressEvent(Bindings::WindowObject& window_object, FlyString const& event_name, ProgressEventInit const& event_init)
: Event(window_object, event_name, event_init)
, m_length_computable(event_init.length_computable)
, m_loaded(event_init.loaded)
, m_total(event_init.total)
{
set_prototype(&window_object.ensure_web_prototype<Bindings::ProgressEventPrototype>("ProgressEvent"));
}
ProgressEvent::~ProgressEvent() = default;
}

View file

@ -19,37 +19,32 @@ struct ProgressEventInit : public DOM::EventInit {
u32 total { 0 };
};
class ProgressEvent : public DOM::Event {
class ProgressEvent final : public DOM::Event {
JS_OBJECT(ProgressEvent, DOM::Event);
public:
using WrapperType = Bindings::ProgressEventWrapper;
static ProgressEvent* create(Bindings::WindowObject&, FlyString const& event_name, ProgressEventInit const& event_init);
static ProgressEvent* create_with_global_object(Bindings::WindowObject&, FlyString const& event_name, ProgressEventInit const& event_init);
static NonnullRefPtr<ProgressEvent> create(FlyString const& event_name, ProgressEventInit const& event_init)
{
return adopt_ref(*new ProgressEvent(event_name, event_init));
}
static NonnullRefPtr<ProgressEvent> create_with_global_object(Bindings::WindowObject&, FlyString const& event_name, ProgressEventInit const& event_init)
{
return ProgressEvent::create(event_name, event_init);
}
ProgressEvent(Bindings::WindowObject&, FlyString const& event_name, ProgressEventInit const& event_init);
virtual ~ProgressEvent() override = default;
virtual ~ProgressEvent() override;
ProgressEvent& impl() { return *this; }
bool length_computable() const { return m_length_computable; }
u64 loaded() const { return m_loaded; }
u64 total() const { return m_total; }
protected:
ProgressEvent(FlyString const& event_name, ProgressEventInit const& event_init)
: Event(event_name, event_init)
, m_length_computable(event_init.length_computable)
, m_loaded(event_init.loaded)
, m_total(event_init.total)
{
}
private:
bool m_length_computable { false };
u64 m_loaded { 0 };
u64 m_total { 0 };
};
}
namespace Web::Bindings {
inline JS::Object* wrap(JS::Realm&, Web::XHR::ProgressEvent& object) { return &object; }
using ProgressEventWrapper = Web::XHR::ProgressEvent;
}

View file

@ -1,5 +1,6 @@
#import <DOM/Event.idl>
[NoInstanceWrapper]
interface ProgressEvent : Event {
constructor(DOMString type, optional ProgressEventInit eventInitDict = {});

View file

@ -16,7 +16,6 @@
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibTextCodec/Decoder.h>
#include <LibWeb/Bindings/EventWrapper.h>
#include <LibWeb/Bindings/IDLAbstractOperations.h>
#include <LibWeb/Bindings/XMLHttpRequestWrapper.h>
#include <LibWeb/DOM/DOMException.h>
@ -53,7 +52,7 @@ XMLHttpRequest::~XMLHttpRequest() = default;
void XMLHttpRequest::set_ready_state(ReadyState ready_state)
{
m_ready_state = ready_state;
dispatch_event(DOM::Event::create(EventNames::readystatechange));
dispatch_event(*DOM::Event::create(verify_cast<Bindings::WindowObject>(wrapper()->global_object()), EventNames::readystatechange));
}
void XMLHttpRequest::fire_progress_event(String const& event_name, u64 transmitted, u64 length)
@ -62,7 +61,7 @@ void XMLHttpRequest::fire_progress_event(String const& event_name, u64 transmitt
event_init.length_computable = true;
event_init.loaded = transmitted;
event_init.total = length;
dispatch_event(ProgressEvent::create(event_name, event_init));
dispatch_event(*ProgressEvent::create(verify_cast<Bindings::WindowObject>(wrapper()->global_object()), event_name, event_init));
}
// https://xhr.spec.whatwg.org/#dom-xmlhttprequest-responsetext
@ -469,7 +468,7 @@ DOM::ExceptionOr<void> XMLHttpRequest::send(Optional<XMLHttpRequestBodyInit> bod
if (should_enforce_same_origin_policy && !m_window->associated_document().origin().is_same_origin(request_url_origin)) {
dbgln("XHR failed to load: Same-Origin Policy violation: {} may not load {}", m_window->associated_document().url(), request_url);
set_ready_state(ReadyState::Done);
dispatch_event(DOM::Event::create(HTML::EventNames::error));
dispatch_event(*DOM::Event::create(verify_cast<Bindings::WindowObject>(wrapper()->global_object()), HTML::EventNames::error));
return {};
}
@ -539,7 +538,7 @@ DOM::ExceptionOr<void> XMLHttpRequest::send(Optional<XMLHttpRequestBodyInit> bod
xhr.m_status = status_code.value_or(0);
xhr.m_response_headers = move(response_headers);
xhr.m_send = false;
xhr.dispatch_event(DOM::Event::create(EventNames::readystatechange));
xhr.dispatch_event(*DOM::Event::create(verify_cast<Bindings::WindowObject>(xhr.wrapper()->global_object()), EventNames::readystatechange));
xhr.fire_progress_event(EventNames::load, transmitted, length);
xhr.fire_progress_event(EventNames::loadend, transmitted, length);
},
@ -551,7 +550,7 @@ DOM::ExceptionOr<void> XMLHttpRequest::send(Optional<XMLHttpRequestBodyInit> bod
auto& xhr = const_cast<XMLHttpRequest&>(*strong_this);
xhr.set_ready_state(ReadyState::Done);
xhr.set_status(status_code.value_or(0));
xhr.dispatch_event(DOM::Event::create(HTML::EventNames::error));
xhr.dispatch_event(*DOM::Event::create(verify_cast<Bindings::WindowObject>(xhr.wrapper()->global_object()), HTML::EventNames::error));
},
m_timeout,
[weak_this = make_weak_ptr()] {
@ -559,7 +558,7 @@ DOM::ExceptionOr<void> XMLHttpRequest::send(Optional<XMLHttpRequestBodyInit> bod
if (!strong_this)
return;
auto& xhr = const_cast<XMLHttpRequest&>(*strong_this);
xhr.dispatch_event(DOM::Event::create(EventNames::timeout));
xhr.dispatch_event(*DOM::Event::create(verify_cast<Bindings::WindowObject>(xhr.wrapper()->global_object()), EventNames::timeout));
});
} else {
TODO();