瀏覽代碼

LibWeb: Add IDBTransaction

stelar7 8 月之前
父節點
當前提交
20a92a81c4

+ 1 - 0
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

+ 1 - 0
Libraries/LibWeb/Forward.h

@@ -581,6 +581,7 @@ class IDBIndex;
 class IDBObjectStore;
 class IDBOpenDBRequest;
 class IDBRequest;
+class IDBTransaction;
 class IDBVersionChangeEvent;
 }
 

+ 71 - 0
Libraries/LibWeb/IndexedDB/IDBTransaction.cpp

@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/Bindings/Intrinsics.h>
+#include <LibWeb/HTML/EventNames.h>
+#include <LibWeb/IndexedDB/IDBTransaction.h>
+
+namespace Web::IndexedDB {
+
+GC_DEFINE_ALLOCATOR(IDBTransaction);
+
+IDBTransaction::~IDBTransaction() = default;
+
+IDBTransaction::IDBTransaction(JS::Realm& realm, GC::Ref<IDBDatabase> database)
+    : EventTarget(realm)
+    , m_connection(database)
+{
+}
+
+GC::Ref<IDBTransaction> IDBTransaction::create(JS::Realm& realm, GC::Ref<IDBDatabase> database)
+{
+    return realm.create<IDBTransaction>(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);
+}
+
+}

+ 67 - 0
Libraries/LibWeb/IndexedDB/IDBTransaction.h

@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibGC/Ptr.h>
+#include <LibWeb/Bindings/IDBDatabasePrototype.h>
+#include <LibWeb/Bindings/IDBTransactionPrototype.h>
+#include <LibWeb/DOM/Event.h>
+#include <LibWeb/DOM/EventTarget.h>
+#include <LibWeb/IndexedDB/IDBDatabase.h>
+
+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<IDBTransaction> create(JS::Realm&, GC::Ref<IDBDatabase>);
+    [[nodiscard]] Bindings::IDBTransactionMode mode() const { return m_mode; }
+    [[nodiscard]] TransactionState state() const { return m_state; }
+    [[nodiscard]] GC::Ptr<WebIDL::DOMException> error() const { return m_error; }
+    [[nodiscard]] GC::Ref<IDBDatabase> 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<WebIDL::DOMException> 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<IDBDatabase>);
+    virtual void initialize(JS::Realm&) override;
+    virtual void visit_edges(Visitor& visitor) override;
+
+private:
+    GC::Ref<IDBDatabase> m_connection;
+    Bindings::IDBTransactionMode m_mode;
+    Bindings::IDBTransactionDurability m_durability { Bindings::IDBTransactionDurability::Default };
+    TransactionState m_state;
+    GC::Ptr<WebIDL::DOMException> m_error;
+};
+}

+ 25 - 0
Libraries/LibWeb/IndexedDB/IDBTransaction.idl

@@ -0,0 +1,25 @@
+#import <DOM/EventTarget.idl>
+#import <IndexedDB/IDBDatabase.idl>
+#import <IndexedDB/IDBObjectStore.idl>
+
+[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"
+};

+ 1 - 0
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)

+ 1 - 0
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,

+ 1 - 0
Tests/LibWeb/Text/expected/all-window-properties.txt

@@ -206,6 +206,7 @@ IDBIndex
 IDBObjectStore
 IDBOpenDBRequest
 IDBRequest
+IDBTransaction
 IDBVersionChangeEvent
 IdleDeadline
 Image