2024-11-07 19:00:20 +01:00
/*
2025-04-01 19:51:22 +02:00
* Copyright ( c ) 2024 - 2025 , stelar7 < dudedbz @ gmail . com >
2024-11-07 19:00:20 +01:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# pragma once
2025-04-01 19:51:22 +02:00
# include <AK/Vector.h>
2024-11-07 19:00:20 +01:00
# include <LibGC/Ptr.h>
# include <LibWeb/Bindings/IDBDatabasePrototype.h>
# include <LibWeb/Bindings/IDBTransactionPrototype.h>
# include <LibWeb/DOM/Event.h>
# include <LibWeb/DOM/EventTarget.h>
2025-04-01 19:51:22 +02:00
# include <LibWeb/HTML/EventLoop/EventLoop.h>
2024-11-07 19:00:20 +01:00
# include <LibWeb/IndexedDB/IDBDatabase.h>
2025-04-01 19:51:22 +02:00
# include <LibWeb/IndexedDB/IDBRequest.h>
# include <LibWeb/IndexedDB/Internal/ObjectStore.h>
# include <LibWeb/IndexedDB/Internal/RequestList.h>
2024-11-07 19:00:20 +01:00
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 ;
2025-04-01 19:51:22 +02:00
[ [ nodiscard ] ] static GC : : Ref < IDBTransaction > create ( JS : : Realm & , GC : : Ref < IDBDatabase > , Bindings : : IDBTransactionMode , Bindings : : IDBTransactionDurability , Vector < GC : : Ref < ObjectStore > > ) ;
2024-11-07 19:00:20 +01:00
[ [ 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 ; }
2024-12-01 21:57:32 +01:00
[ [ nodiscard ] ] GC : : Ptr < IDBRequest > associated_request ( ) const { return m_associated_request ; }
2024-12-01 22:52:33 +01:00
[ [ nodiscard ] ] bool aborted ( ) const { return m_aborted ; }
2025-04-01 19:51:22 +02:00
[ [ nodiscard ] ] GC : : Ref < HTML : : DOMStringList > object_store_names ( ) ;
2025-04-09 23:11:12 +02:00
[ [ nodiscard ] ] RequestList & request_list ( ) { return m_request_list ; }
2025-04-01 19:51:22 +02:00
[ [ nodiscard ] ] ReadonlySpan < GC : : Ref < ObjectStore > > scope ( ) const { return m_scope ; }
2025-04-02 10:28:04 +02:00
[ [ nodiscard ] ] String uuid ( ) const { return m_uuid ; }
2025-06-16 16:41:29 +02:00
[ [ nodiscard ] ] GC : : Ptr < HTML : : EventLoop > cleanup_event_loop ( ) const { return m_cleanup_event_loop ; }
2024-11-07 19:00:20 +01:00
void set_mode ( Bindings : : IDBTransactionMode mode ) { m_mode = mode ; }
void set_error ( GC : : Ptr < WebIDL : : DOMException > error ) { m_error = error ; }
2024-12-01 21:57:32 +01:00
void set_associated_request ( GC : : Ptr < IDBRequest > request ) { m_associated_request = request ; }
2024-12-01 22:52:33 +01:00
void set_aborted ( bool aborted ) { m_aborted = aborted ; }
2025-04-25 18:08:40 +02:00
void set_cleanup_event_loop ( GC : : Ptr < HTML : : EventLoop > event_loop ) { m_cleanup_event_loop = event_loop ; }
2025-04-09 23:14:10 +02:00
void set_state ( TransactionState state ) { m_state = state ; }
2024-11-07 19:00:20 +01:00
[ [ 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 ; }
2025-04-09 22:59:34 +02:00
[ [ nodiscard ] ] bool is_finished ( ) const { return m_state = = TransactionState : : Finished ; }
2025-05-13 23:14:02 +02:00
[ [ nodiscard ] ] bool is_active ( ) const { return m_state = = TransactionState : : Active ; }
[ [ nodiscard ] ] bool is_inactive ( ) const { return m_state = = TransactionState : : Inactive ; }
[ [ nodiscard ] ] bool is_committing ( ) const { return m_state = = TransactionState : : Committing ; }
2024-11-07 19:00:20 +01:00
2025-04-09 23:24:44 +02:00
GC : : Ptr < ObjectStore > object_store_named ( String const & name ) const ;
2025-04-25 22:33:59 +02:00
void add_to_scope ( GC : : Ref < ObjectStore > object_store ) { m_scope . append ( object_store ) ; }
2025-04-09 23:24:44 +02:00
2024-12-01 21:58:42 +01:00
WebIDL : : ExceptionOr < void > abort ( ) ;
2025-04-09 23:11:12 +02:00
WebIDL : : ExceptionOr < void > commit ( ) ;
2025-04-09 23:24:44 +02:00
WebIDL : : ExceptionOr < GC : : Ref < IDBObjectStore > > object_store ( String const & name ) ;
2024-12-01 21:58:42 +01:00
2024-11-07 19:00:20 +01:00
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 :
2025-04-01 19:51:22 +02:00
explicit IDBTransaction ( JS : : Realm & , GC : : Ref < IDBDatabase > , Bindings : : IDBTransactionMode , Bindings : : IDBTransactionDurability , Vector < GC : : Ref < ObjectStore > > ) ;
2024-11-07 19:00:20 +01:00
virtual void initialize ( JS : : Realm & ) override ;
virtual void visit_edges ( Visitor & visitor ) override ;
private :
2025-04-01 19:51:22 +02:00
// AD-HOC: The transaction has a connection
2024-11-07 19:00:20 +01:00
GC : : Ref < IDBDatabase > m_connection ;
2025-04-01 19:51:22 +02:00
// A transaction has a mode that determines which types of interactions can be performed upon that transaction.
2024-11-07 19:00:20 +01:00
Bindings : : IDBTransactionMode m_mode ;
2025-04-01 19:51:22 +02:00
// A transaction has a durability hint. This is a hint to the user agent of whether to prioritize performance or durability when committing the transaction.
2024-11-07 19:00:20 +01:00
Bindings : : IDBTransactionDurability m_durability { Bindings : : IDBTransactionDurability : : Default } ;
2025-04-01 19:51:22 +02:00
// A transaction has a state
2024-11-07 19:00:20 +01:00
TransactionState m_state ;
2025-04-01 19:51:22 +02:00
// A transaction has a error which is set if the transaction is aborted.
2024-11-07 19:00:20 +01:00
GC : : Ptr < WebIDL : : DOMException > m_error ;
2024-12-01 21:57:32 +01:00
2025-04-01 19:51:22 +02:00
// A transaction has an associated upgrade request
2024-12-01 21:57:32 +01:00
GC : : Ptr < IDBRequest > m_associated_request ;
2025-04-01 19:51:22 +02:00
// AD-HOC: We need to track abort state separately, since we cannot rely on only the error.
2024-12-01 22:52:33 +01:00
bool m_aborted { false } ;
2025-04-01 19:51:22 +02:00
// A transaction has a scope which is a set of object stores that the transaction may interact with.
Vector < GC : : Ref < ObjectStore > > m_scope ;
// A transaction has a request list of pending requests which have been made against the transaction.
RequestList m_request_list ;
// A transaction optionally has a cleanup event loop which is an event loop.
GC : : Ptr < HTML : : EventLoop > m_cleanup_event_loop ;
2025-04-02 10:28:04 +02:00
// NOTE: Used for debug purposes
String m_uuid ;
2024-11-07 19:00:20 +01:00
} ;
2025-05-13 07:06:33 -04:00
2024-11-07 19:00:20 +01:00
}