ladybird/Libraries/LibWeb/IndexedDB/IDBRequest.cpp
Andreas Kling a6dfc74e93 LibWeb: Only set prototype once for object with IDL interface
Before this change, we were going through the chain of base classes for
each IDL interface object and having them set the prototype to their
prototype.

Instead of doing that, reorder things so that we set the right prototype
immediately in Foo::initialize(), and then don't bother in all the base
class overrides.

This knocks off a ~1% profile item on Speedometer 3.
2025-04-20 18:43:11 +02:00

100 lines
3.1 KiB
C++

/*
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
* Copyright (c) 2024-2025, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Crypto/Crypto.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/IndexedDB/IDBRequest.h>
#include <LibWeb/IndexedDB/IDBTransaction.h>
namespace Web::IndexedDB {
GC_DEFINE_ALLOCATOR(IDBRequest);
IDBRequest::~IDBRequest() = default;
IDBRequest::IDBRequest(JS::Realm& realm, IDBRequestSource source)
: EventTarget(realm)
, m_source(source)
{
m_uuid = MUST(Crypto::generate_random_uuid());
}
void IDBRequest::initialize(JS::Realm& realm)
{
WEB_SET_PROTOTYPE_FOR_INTERFACE(IDBRequest);
Base::initialize(realm);
}
GC::Ref<IDBRequest> IDBRequest::create(JS::Realm& realm, IDBRequestSource source)
{
return realm.create<IDBRequest>(realm, source);
}
void IDBRequest::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_error);
visitor.visit(m_result);
visitor.visit(m_transaction);
}
// https://w3c.github.io/IndexedDB/#dom-idbrequest-onsuccess
void IDBRequest::set_onsuccess(WebIDL::CallbackType* event_handler)
{
set_event_handler_attribute(HTML::EventNames::success, event_handler);
}
// https://w3c.github.io/IndexedDB/#dom-idbrequest-onsuccess
WebIDL::CallbackType* IDBRequest::onsuccess()
{
return event_handler_attribute(HTML::EventNames::success);
}
// https://w3c.github.io/IndexedDB/#dom-idbrequest-onerror
void IDBRequest::set_onerror(WebIDL::CallbackType* event_handler)
{
set_event_handler_attribute(HTML::EventNames::error, event_handler);
}
// https://w3c.github.io/IndexedDB/#dom-idbrequest-onerror
WebIDL::CallbackType* IDBRequest::onerror()
{
return event_handler_attribute(HTML::EventNames::error);
}
// https://w3c.github.io/IndexedDB/#dom-idbrequest-readystate
[[nodiscard]] Bindings::IDBRequestReadyState IDBRequest::ready_state() const
{
// The readyState getter steps are to return "pending" if this's done flag is false, and "done" otherwise.
return m_done ? Bindings::IDBRequestReadyState::Done : Bindings::IDBRequestReadyState::Pending;
}
// https://w3c.github.io/IndexedDB/#dom-idbrequest-error
[[nodiscard]] WebIDL::ExceptionOr<GC::Ptr<WebIDL::DOMException>> IDBRequest::error() const
{
// 1. If this's done flag is false, then throw an "InvalidStateError" DOMException.
if (!m_done)
return WebIDL::InvalidStateError::create(realm(), "The request is not done"_string);
// 2. Otherwise, return this's error, or null if no error occurred.
return m_error;
}
// https://w3c.github.io/IndexedDB/#dom-idbrequest-result
[[nodiscard]] WebIDL::ExceptionOr<JS::Value> IDBRequest::result() const
{
// 1. If this's done flag is false, then throw an "InvalidStateError" DOMException.
if (!m_done)
return WebIDL::InvalidStateError::create(realm(), "The request is not done"_string);
// 2. Otherwise, return this's result, or undefined if the request resulted in an error.
return m_result;
}
}