1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_idbtransaction_h__
8 #define mozilla_dom_idbtransaction_h__
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/IDBTransactionBinding.h"
12 #include "mozilla/DOMEventTargetHelper.h"
13 #include "nsAutoPtr.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "nsIRunnable.h"
22 class EventChainPreVisitor
;
30 class IDBOpenDBRequest
;
32 class StrongWorkerRef
;
35 class BackgroundCursorChild
;
36 class BackgroundRequestChild
;
37 class BackgroundTransactionChild
;
38 class BackgroundVersionChangeTransactionChild
;
40 class ObjectStoreSpec
;
41 class OpenCursorParams
;
43 } // namespace indexedDB
45 class IDBTransaction final
: public DOMEventTargetHelper
, public nsIRunnable
{
46 friend class indexedDB::BackgroundCursorChild
;
47 friend class indexedDB::BackgroundRequestChild
;
57 // Only needed for IPC serialization helper, should never be used in code.
61 enum ReadyState
{ INITIAL
= 0, LOADING
, COMMITTING
, DONE
};
64 RefPtr
<IDBDatabase
> mDatabase
;
65 RefPtr
<DOMException
> mError
;
66 nsTArray
<nsString
> mObjectStoreNames
;
67 nsTArray
<RefPtr
<IDBObjectStore
>> mObjectStores
;
68 nsTArray
<RefPtr
<IDBObjectStore
>> mDeletedObjectStores
;
69 RefPtr
<StrongWorkerRef
> mWorkerRef
;
71 // Tagged with mMode. If mMode is VERSION_CHANGE then mBackgroundActor will be
72 // a BackgroundVersionChangeTransactionChild. Otherwise it will be a
73 // BackgroundTransactionChild.
75 indexedDB::BackgroundTransactionChild
* mNormalBackgroundActor
;
76 indexedDB::BackgroundVersionChangeTransactionChild
*
77 mVersionChangeBackgroundActor
;
80 const int64_t mLoggingSerialNumber
;
82 // Only used for VERSION_CHANGE transactions.
83 int64_t mNextObjectStoreId
;
87 uint32_t mPendingRequestCount
;
93 ReadyState mReadyState
;
98 bool mAbortedByScript
;
99 bool mNotedActiveTransaction
;
102 bool mSentCommitOrAbort
;
103 bool mFiredCompleteOrAbort
;
107 static already_AddRefed
<IDBTransaction
> CreateVersionChange(
108 IDBDatabase
* aDatabase
,
109 indexedDB::BackgroundVersionChangeTransactionChild
* aActor
,
110 IDBOpenDBRequest
* aOpenRequest
, int64_t aNextObjectStoreId
,
111 int64_t aNextIndexId
);
113 static already_AddRefed
<IDBTransaction
> Create(
114 JSContext
* aCx
, IDBDatabase
* aDatabase
,
115 const nsTArray
<nsString
>& aObjectStoreNames
, Mode aMode
);
117 static IDBTransaction
* GetCurrent();
119 void AssertIsOnOwningThread() const
127 void SetBackgroundActor(
128 indexedDB::BackgroundTransactionChild
* aBackgroundActor
);
130 void ClearBackgroundActor() {
131 AssertIsOnOwningThread();
133 if (mMode
== VERSION_CHANGE
) {
134 mBackgroundActor
.mVersionChangeBackgroundActor
= nullptr;
136 mBackgroundActor
.mNormalBackgroundActor
= nullptr;
139 // Note inactive transaction here if we didn't receive the Complete message
141 MaybeNoteInactiveTransaction();
144 indexedDB::BackgroundRequestChild
* StartRequest(
145 IDBRequest
* aRequest
, const indexedDB::RequestParams
& aParams
);
147 void OpenCursor(indexedDB::BackgroundCursorChild
* aBackgroundActor
,
148 const indexedDB::OpenCursorParams
& aParams
);
150 void RefreshSpec(bool aMayDelete
);
154 bool IsCommittingOrDone() const {
155 AssertIsOnOwningThread();
157 return mReadyState
== COMMITTING
|| mReadyState
== DONE
;
160 bool IsDone() const {
161 AssertIsOnOwningThread();
163 return mReadyState
== DONE
;
166 bool IsWriteAllowed() const {
167 AssertIsOnOwningThread();
168 return mMode
== READ_WRITE
|| mMode
== READ_WRITE_FLUSH
||
169 mMode
== CLEANUP
|| mMode
== VERSION_CHANGE
;
172 bool IsAborted() const {
173 AssertIsOnOwningThread();
174 return NS_FAILED(mAbortCode
);
177 nsresult
AbortCode() const {
178 AssertIsOnOwningThread();
182 void GetCallerLocation(nsAString
& aFilename
, uint32_t* aLineNo
,
183 uint32_t* aColumn
) const;
185 // 'Get' prefix is to avoid name collisions with the enum
186 Mode
GetMode() const {
187 AssertIsOnOwningThread();
191 IDBDatabase
* Database() const {
192 AssertIsOnOwningThread();
196 IDBDatabase
* Db() const { return Database(); }
198 const nsTArray
<nsString
>& ObjectStoreNamesInternal() const {
199 AssertIsOnOwningThread();
200 return mObjectStoreNames
;
203 already_AddRefed
<IDBObjectStore
> CreateObjectStore(
204 const indexedDB::ObjectStoreSpec
& aSpec
);
206 void DeleteObjectStore(int64_t aObjectStoreId
);
208 void RenameObjectStore(int64_t aObjectStoreId
, const nsAString
& aName
);
210 void CreateIndex(IDBObjectStore
* aObjectStore
,
211 const indexedDB::IndexMetadata
& aMetadata
);
213 void DeleteIndex(IDBObjectStore
* aObjectStore
, int64_t aIndexId
);
215 void RenameIndex(IDBObjectStore
* aObjectStore
, int64_t aIndexId
,
216 const nsAString
& aName
);
218 void Abort(IDBRequest
* aRequest
);
220 void Abort(nsresult aAbortCode
);
222 int64_t LoggingSerialNumber() const {
223 AssertIsOnOwningThread();
225 return mLoggingSerialNumber
;
228 nsIGlobalObject
* GetParentObject() const;
230 IDBTransactionMode
GetMode(ErrorResult
& aRv
) const;
232 DOMException
* GetError() const;
234 already_AddRefed
<IDBObjectStore
> ObjectStore(const nsAString
& aName
,
237 void Abort(ErrorResult
& aRv
);
239 IMPL_EVENT_HANDLER(abort
)
240 IMPL_EVENT_HANDLER(complete
)
241 IMPL_EVENT_HANDLER(error
)
243 already_AddRefed
<DOMStringList
> ObjectStoreNames() const;
245 void FireCompleteOrAbortEvents(nsresult aResult
);
247 // Only for VERSION_CHANGE transactions.
248 int64_t NextObjectStoreId();
250 // Only for VERSION_CHANGE transactions.
251 int64_t NextIndexId();
253 NS_DECL_ISUPPORTS_INHERITED
255 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBTransaction
, DOMEventTargetHelper
)
258 virtual JSObject
* WrapObject(JSContext
* aCx
,
259 JS::Handle
<JSObject
*> aGivenProto
) override
;
262 void GetEventTargetParent(EventChainPreVisitor
& aVisitor
) override
;
265 IDBTransaction(IDBDatabase
* aDatabase
,
266 const nsTArray
<nsString
>& aObjectStoreNames
, Mode aMode
);
269 void AbortInternal(nsresult aAbortCode
,
270 already_AddRefed
<DOMException
> aError
);
274 void SendAbort(nsresult aResultCode
);
276 void NoteActiveTransaction();
278 void MaybeNoteInactiveTransaction();
282 void OnRequestFinished(bool aActorDestroyedNormally
);
286 } // namespace mozilla
288 #endif // mozilla_dom_idbtransaction_h__