diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 4081f101e4b..fc1ee6c85b8 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -518,6 +518,7 @@ set(SOURCES IndexedDB/IDBIndex.cpp IndexedDB/IDBObjectStore.cpp IndexedDB/IDBRequest.cpp + IndexedDB/IDBTransaction.cpp IndexedDB/IDBVersionChangeEvent.cpp Internals/Inspector.cpp Internals/InternalAnimationTimeline.cpp diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index 73d80632bc0..fb694f8ed50 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -581,6 +581,7 @@ class IDBIndex; class IDBObjectStore; class IDBOpenDBRequest; class IDBRequest; +class IDBTransaction; class IDBVersionChangeEvent; } diff --git a/Libraries/LibWeb/IndexedDB/IDBTransaction.cpp b/Libraries/LibWeb/IndexedDB/IDBTransaction.cpp new file mode 100644 index 00000000000..61408832412 --- /dev/null +++ b/Libraries/LibWeb/IndexedDB/IDBTransaction.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2024, stelar7 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace Web::IndexedDB { + +GC_DEFINE_ALLOCATOR(IDBTransaction); + +IDBTransaction::~IDBTransaction() = default; + +IDBTransaction::IDBTransaction(JS::Realm& realm, GC::Ref database) + : EventTarget(realm) + , m_connection(database) +{ +} + +GC::Ref IDBTransaction::create(JS::Realm& realm, GC::Ref database) +{ + return realm.create(realm, database); +} + +void IDBTransaction::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(IDBTransaction); +} + +void IDBTransaction::visit_edges(Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_connection); + visitor.visit(m_error); +} + +void IDBTransaction::set_onabort(WebIDL::CallbackType* event_handler) +{ + set_event_handler_attribute(HTML::EventNames::abort, event_handler); +} + +WebIDL::CallbackType* IDBTransaction::onabort() +{ + return event_handler_attribute(HTML::EventNames::abort); +} + +void IDBTransaction::set_oncomplete(WebIDL::CallbackType* event_handler) +{ + set_event_handler_attribute(HTML::EventNames::complete, event_handler); +} + +WebIDL::CallbackType* IDBTransaction::oncomplete() +{ + return event_handler_attribute(HTML::EventNames::complete); +} + +void IDBTransaction::set_onerror(WebIDL::CallbackType* event_handler) +{ + set_event_handler_attribute(HTML::EventNames::error, event_handler); +} + +WebIDL::CallbackType* IDBTransaction::onerror() +{ + return event_handler_attribute(HTML::EventNames::error); +} + +} diff --git a/Libraries/LibWeb/IndexedDB/IDBTransaction.h b/Libraries/LibWeb/IndexedDB/IDBTransaction.h new file mode 100644 index 00000000000..2528c05fe85 --- /dev/null +++ b/Libraries/LibWeb/IndexedDB/IDBTransaction.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024, stelar7 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Web::IndexedDB { + +// https://w3c.github.io/IndexedDB/#transaction +class IDBTransaction : public DOM::EventTarget { + WEB_PLATFORM_OBJECT(IDBTransaction, DOM::EventTarget); + GC_DECLARE_ALLOCATOR(IDBTransaction); + + enum TransactionState { + Active, + Inactive, + Committing, + Finished + }; + +public: + virtual ~IDBTransaction() override; + + [[nodiscard]] static GC::Ref create(JS::Realm&, GC::Ref); + [[nodiscard]] Bindings::IDBTransactionMode mode() const { return m_mode; } + [[nodiscard]] TransactionState state() const { return m_state; } + [[nodiscard]] GC::Ptr error() const { return m_error; } + [[nodiscard]] GC::Ref connection() const { return m_connection; } + [[nodiscard]] Bindings::IDBTransactionDurability durability() const { return m_durability; } + + void set_mode(Bindings::IDBTransactionMode mode) { m_mode = mode; } + void set_state(TransactionState state) { m_state = state; } + void set_error(GC::Ptr error) { m_error = error; } + + [[nodiscard]] bool is_upgrade_transaction() const { return m_mode == Bindings::IDBTransactionMode::Versionchange; } + [[nodiscard]] bool is_readonly() const { return m_mode == Bindings::IDBTransactionMode::Readonly; } + [[nodiscard]] bool is_readwrite() const { return m_mode == Bindings::IDBTransactionMode::Readwrite; } + + void set_onabort(WebIDL::CallbackType*); + WebIDL::CallbackType* onabort(); + void set_oncomplete(WebIDL::CallbackType*); + WebIDL::CallbackType* oncomplete(); + void set_onerror(WebIDL::CallbackType*); + WebIDL::CallbackType* onerror(); + +protected: + explicit IDBTransaction(JS::Realm&, GC::Ref); + virtual void initialize(JS::Realm&) override; + virtual void visit_edges(Visitor& visitor) override; + +private: + GC::Ref m_connection; + Bindings::IDBTransactionMode m_mode; + Bindings::IDBTransactionDurability m_durability { Bindings::IDBTransactionDurability::Default }; + TransactionState m_state; + GC::Ptr m_error; +}; +} diff --git a/Libraries/LibWeb/IndexedDB/IDBTransaction.idl b/Libraries/LibWeb/IndexedDB/IDBTransaction.idl new file mode 100644 index 00000000000..f08d9a38d7a --- /dev/null +++ b/Libraries/LibWeb/IndexedDB/IDBTransaction.idl @@ -0,0 +1,25 @@ +#import +#import +#import + +[Exposed=(Window,Worker)] +interface IDBTransaction : EventTarget { + [FIXME] readonly attribute DOMStringList objectStoreNames; + readonly attribute IDBTransactionMode mode; + readonly attribute IDBTransactionDurability durability; + [FIXME, SameObject] readonly attribute IDBDatabase db; + readonly attribute DOMException? error; + [FIXME] IDBObjectStore objectStore(DOMString name); + [FIXME] undefined commit(); + [FIXME] undefined abort(); + + attribute EventHandler onabort; + attribute EventHandler oncomplete; + attribute EventHandler onerror; +}; + +enum IDBTransactionMode { + "readonly", + "readwrite", + "versionchange" +}; diff --git a/Libraries/LibWeb/idl_files.cmake b/Libraries/LibWeb/idl_files.cmake index 323330734f4..e3dd190533d 100644 --- a/Libraries/LibWeb/idl_files.cmake +++ b/Libraries/LibWeb/idl_files.cmake @@ -252,6 +252,7 @@ libweb_js_bindings(IndexedDB/IDBIndex) libweb_js_bindings(IndexedDB/IDBObjectStore) libweb_js_bindings(IndexedDB/IDBOpenDBRequest) libweb_js_bindings(IndexedDB/IDBRequest) +libweb_js_bindings(IndexedDB/IDBTransaction) libweb_js_bindings(IndexedDB/IDBVersionChangeEvent) libweb_js_bindings(Internals/Inspector) libweb_js_bindings(Internals/InternalAnimationTimeline) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 6f025961093..ebba786cf98 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -62,6 +62,7 @@ static bool is_platform_object(Type const& type) "IDBCursor"sv, "IDBIndex"sv, "IDBObjectStore"sv, + "IDBTransaction"sv, "ImageBitmap"sv, "ImageData"sv, "Instance"sv, diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt index ddde15c2cfa..99729115a3d 100644 --- a/Tests/LibWeb/Text/expected/all-window-properties.txt +++ b/Tests/LibWeb/Text/expected/all-window-properties.txt @@ -206,6 +206,7 @@ IDBIndex IDBObjectStore IDBOpenDBRequest IDBRequest +IDBTransaction IDBVersionChangeEvent IdleDeadline Image