Bug 1572460 - Refactor `selection` out of the `InspectorFront`. r=yulia
[gecko.git] / dom / indexedDB / IDBTransaction.h
blob80a90922b360583cdc300ec3b7f0b4c9773aafb6
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"
16 #include "nsString.h"
17 #include "nsTArray.h"
19 namespace mozilla {
21 class ErrorResult;
22 class EventChainPreVisitor;
24 namespace dom {
26 class DOMException;
27 class DOMStringList;
28 class IDBDatabase;
29 class IDBObjectStore;
30 class IDBOpenDBRequest;
31 class IDBRequest;
32 class StrongWorkerRef;
34 namespace indexedDB {
35 class BackgroundCursorChild;
36 class BackgroundRequestChild;
37 class BackgroundTransactionChild;
38 class BackgroundVersionChangeTransactionChild;
39 class IndexMetadata;
40 class ObjectStoreSpec;
41 class OpenCursorParams;
42 class RequestParams;
43 } // namespace indexedDB
45 class IDBTransaction final : public DOMEventTargetHelper, public nsIRunnable {
46 friend class indexedDB::BackgroundCursorChild;
47 friend class indexedDB::BackgroundRequestChild;
49 public:
50 enum Mode {
51 READ_ONLY = 0,
52 READ_WRITE,
53 READ_WRITE_FLUSH,
54 CLEANUP,
55 VERSION_CHANGE,
57 // Only needed for IPC serialization helper, should never be used in code.
58 MODE_INVALID
61 enum ReadyState { INITIAL = 0, LOADING, COMMITTING, DONE };
63 private:
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.
74 union {
75 indexedDB::BackgroundTransactionChild* mNormalBackgroundActor;
76 indexedDB::BackgroundVersionChangeTransactionChild*
77 mVersionChangeBackgroundActor;
78 } mBackgroundActor;
80 const int64_t mLoggingSerialNumber;
82 // Only used for VERSION_CHANGE transactions.
83 int64_t mNextObjectStoreId;
84 int64_t mNextIndexId;
86 nsresult mAbortCode;
87 uint32_t mPendingRequestCount;
89 nsString mFilename;
90 uint32_t mLineNo;
91 uint32_t mColumn;
93 ReadyState mReadyState;
94 Mode mMode;
96 bool mCreating;
97 bool mRegistered;
98 bool mAbortedByScript;
99 bool mNotedActiveTransaction;
101 #ifdef DEBUG
102 bool mSentCommitOrAbort;
103 bool mFiredCompleteOrAbort;
104 #endif
106 public:
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
120 #ifdef DEBUG
122 #else
125 #endif
127 void SetBackgroundActor(
128 indexedDB::BackgroundTransactionChild* aBackgroundActor);
130 void ClearBackgroundActor() {
131 AssertIsOnOwningThread();
133 if (mMode == VERSION_CHANGE) {
134 mBackgroundActor.mVersionChangeBackgroundActor = nullptr;
135 } else {
136 mBackgroundActor.mNormalBackgroundActor = nullptr;
139 // Note inactive transaction here if we didn't receive the Complete message
140 // from the parent.
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);
152 bool IsOpen() const;
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();
179 return mAbortCode;
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();
188 return mMode;
191 IDBDatabase* Database() const {
192 AssertIsOnOwningThread();
193 return mDatabase;
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,
235 ErrorResult& aRv);
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
254 NS_DECL_NSIRUNNABLE
255 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBTransaction, DOMEventTargetHelper)
257 // nsWrapperCache
258 virtual JSObject* WrapObject(JSContext* aCx,
259 JS::Handle<JSObject*> aGivenProto) override;
261 // EventTarget
262 void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
264 private:
265 IDBTransaction(IDBDatabase* aDatabase,
266 const nsTArray<nsString>& aObjectStoreNames, Mode aMode);
267 ~IDBTransaction();
269 void AbortInternal(nsresult aAbortCode,
270 already_AddRefed<DOMException> aError);
272 void SendCommit();
274 void SendAbort(nsresult aResultCode);
276 void NoteActiveTransaction();
278 void MaybeNoteInactiveTransaction();
280 void OnNewRequest();
282 void OnRequestFinished(bool aActorDestroyedNormally);
285 } // namespace dom
286 } // namespace mozilla
288 #endif // mozilla_dom_idbtransaction_h__