IDBTransaction.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_idbtransaction_h__
  6. #define mozilla_dom_idbtransaction_h__
  7. #include "mozilla/Attributes.h"
  8. #include "mozilla/dom/IDBTransactionBinding.h"
  9. #include "mozilla/dom/IDBWrapperCache.h"
  10. #include "nsAutoPtr.h"
  11. #include "nsCycleCollectionParticipant.h"
  12. #include "nsIRunnable.h"
  13. #include "nsString.h"
  14. #include "nsTArray.h"
  15. class nsPIDOMWindowInner;
  16. namespace mozilla {
  17. class ErrorResult;
  18. class EventChainPreVisitor;
  19. namespace dom {
  20. class DOMError;
  21. class DOMStringList;
  22. class IDBDatabase;
  23. class IDBObjectStore;
  24. class IDBOpenDBRequest;
  25. class IDBRequest;
  26. namespace indexedDB {
  27. class BackgroundCursorChild;
  28. class BackgroundRequestChild;
  29. class BackgroundTransactionChild;
  30. class BackgroundVersionChangeTransactionChild;
  31. class IndexMetadata;
  32. class ObjectStoreSpec;
  33. class OpenCursorParams;
  34. class RequestParams;
  35. }
  36. class IDBTransaction final
  37. : public IDBWrapperCache
  38. , public nsIRunnable
  39. {
  40. friend class indexedDB::BackgroundCursorChild;
  41. friend class indexedDB::BackgroundRequestChild;
  42. class WorkerHolder;
  43. friend class WorkerHolder;
  44. public:
  45. enum Mode
  46. {
  47. READ_ONLY = 0,
  48. READ_WRITE,
  49. READ_WRITE_FLUSH,
  50. CLEANUP,
  51. VERSION_CHANGE,
  52. // Only needed for IPC serialization helper, should never be used in code.
  53. MODE_INVALID
  54. };
  55. enum ReadyState
  56. {
  57. INITIAL = 0,
  58. LOADING,
  59. COMMITTING,
  60. DONE
  61. };
  62. private:
  63. RefPtr<IDBDatabase> mDatabase;
  64. RefPtr<DOMError> mError;
  65. nsTArray<nsString> mObjectStoreNames;
  66. nsTArray<RefPtr<IDBObjectStore>> mObjectStores;
  67. nsTArray<RefPtr<IDBObjectStore>> mDeletedObjectStores;
  68. nsAutoPtr<WorkerHolder> mWorkerHolder;
  69. // Tagged with mMode. If mMode is VERSION_CHANGE then mBackgroundActor will be
  70. // a BackgroundVersionChangeTransactionChild. Otherwise it will be a
  71. // BackgroundTransactionChild.
  72. union {
  73. indexedDB::BackgroundTransactionChild* mNormalBackgroundActor;
  74. indexedDB::BackgroundVersionChangeTransactionChild* mVersionChangeBackgroundActor;
  75. } mBackgroundActor;
  76. const int64_t mLoggingSerialNumber;
  77. // Only used for VERSION_CHANGE transactions.
  78. int64_t mNextObjectStoreId;
  79. int64_t mNextIndexId;
  80. nsresult mAbortCode;
  81. uint32_t mPendingRequestCount;
  82. nsString mFilename;
  83. uint32_t mLineNo;
  84. uint32_t mColumn;
  85. ReadyState mReadyState;
  86. Mode mMode;
  87. bool mCreating;
  88. bool mRegistered;
  89. bool mAbortedByScript;
  90. #ifdef DEBUG
  91. bool mSentCommitOrAbort;
  92. bool mFiredCompleteOrAbort;
  93. #endif
  94. public:
  95. static already_AddRefed<IDBTransaction>
  96. CreateVersionChange(IDBDatabase* aDatabase,
  97. indexedDB::BackgroundVersionChangeTransactionChild* aActor,
  98. IDBOpenDBRequest* aOpenRequest,
  99. int64_t aNextObjectStoreId,
  100. int64_t aNextIndexId);
  101. static already_AddRefed<IDBTransaction>
  102. Create(JSContext* aCx, IDBDatabase* aDatabase,
  103. const nsTArray<nsString>& aObjectStoreNames,
  104. Mode aMode);
  105. static IDBTransaction*
  106. GetCurrent();
  107. void
  108. AssertIsOnOwningThread() const
  109. #ifdef DEBUG
  110. ;
  111. #else
  112. { }
  113. #endif
  114. void
  115. SetBackgroundActor(indexedDB::BackgroundTransactionChild* aBackgroundActor);
  116. void
  117. ClearBackgroundActor()
  118. {
  119. AssertIsOnOwningThread();
  120. if (mMode == VERSION_CHANGE) {
  121. mBackgroundActor.mVersionChangeBackgroundActor = nullptr;
  122. } else {
  123. mBackgroundActor.mNormalBackgroundActor = nullptr;
  124. }
  125. }
  126. indexedDB::BackgroundRequestChild*
  127. StartRequest(IDBRequest* aRequest, const indexedDB::RequestParams& aParams);
  128. void
  129. OpenCursor(indexedDB::BackgroundCursorChild* aBackgroundActor,
  130. const indexedDB::OpenCursorParams& aParams);
  131. void
  132. RefreshSpec(bool aMayDelete);
  133. bool
  134. IsOpen() const;
  135. bool
  136. IsCommittingOrDone() const
  137. {
  138. AssertIsOnOwningThread();
  139. return mReadyState == COMMITTING || mReadyState == DONE;
  140. }
  141. bool
  142. IsDone() const
  143. {
  144. AssertIsOnOwningThread();
  145. return mReadyState == DONE;
  146. }
  147. bool
  148. IsWriteAllowed() const
  149. {
  150. AssertIsOnOwningThread();
  151. return mMode == READ_WRITE ||
  152. mMode == READ_WRITE_FLUSH ||
  153. mMode == CLEANUP ||
  154. mMode == VERSION_CHANGE;
  155. }
  156. bool
  157. IsAborted() const
  158. {
  159. AssertIsOnOwningThread();
  160. return NS_FAILED(mAbortCode);
  161. }
  162. nsresult
  163. AbortCode() const
  164. {
  165. AssertIsOnOwningThread();
  166. return mAbortCode;
  167. }
  168. void
  169. GetCallerLocation(nsAString& aFilename, uint32_t* aLineNo,
  170. uint32_t* aColumn) const;
  171. // 'Get' prefix is to avoid name collisions with the enum
  172. Mode
  173. GetMode() const
  174. {
  175. AssertIsOnOwningThread();
  176. return mMode;
  177. }
  178. IDBDatabase*
  179. Database() const
  180. {
  181. AssertIsOnOwningThread();
  182. return mDatabase;
  183. }
  184. IDBDatabase*
  185. Db() const
  186. {
  187. return Database();
  188. }
  189. const nsTArray<nsString>&
  190. ObjectStoreNamesInternal() const
  191. {
  192. AssertIsOnOwningThread();
  193. return mObjectStoreNames;
  194. }
  195. already_AddRefed<IDBObjectStore>
  196. CreateObjectStore(const indexedDB::ObjectStoreSpec& aSpec);
  197. void
  198. DeleteObjectStore(int64_t aObjectStoreId);
  199. void
  200. RenameObjectStore(int64_t aObjectStoreId, const nsAString& aName);
  201. void
  202. CreateIndex(IDBObjectStore* aObjectStore, const indexedDB::IndexMetadata& aMetadata);
  203. void
  204. DeleteIndex(IDBObjectStore* aObjectStore, int64_t aIndexId);
  205. void
  206. RenameIndex(IDBObjectStore* aObjectStore, int64_t aIndexId, const nsAString& aName);
  207. void
  208. Abort(IDBRequest* aRequest);
  209. void
  210. Abort(nsresult aAbortCode);
  211. int64_t
  212. LoggingSerialNumber() const
  213. {
  214. AssertIsOnOwningThread();
  215. return mLoggingSerialNumber;
  216. }
  217. nsPIDOMWindowInner*
  218. GetParentObject() const;
  219. IDBTransactionMode
  220. GetMode(ErrorResult& aRv) const;
  221. DOMError*
  222. GetError() const;
  223. already_AddRefed<IDBObjectStore>
  224. ObjectStore(const nsAString& aName, ErrorResult& aRv);
  225. void
  226. Abort(ErrorResult& aRv);
  227. IMPL_EVENT_HANDLER(abort)
  228. IMPL_EVENT_HANDLER(complete)
  229. IMPL_EVENT_HANDLER(error)
  230. already_AddRefed<DOMStringList>
  231. ObjectStoreNames() const;
  232. void
  233. FireCompleteOrAbortEvents(nsresult aResult);
  234. // Only for VERSION_CHANGE transactions.
  235. int64_t
  236. NextObjectStoreId();
  237. // Only for VERSION_CHANGE transactions.
  238. int64_t
  239. NextIndexId();
  240. NS_DECL_ISUPPORTS_INHERITED
  241. NS_DECL_NSIRUNNABLE
  242. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBTransaction, IDBWrapperCache)
  243. // nsWrapperCache
  244. virtual JSObject*
  245. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  246. // nsIDOMEventTarget
  247. virtual nsresult
  248. GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
  249. private:
  250. IDBTransaction(IDBDatabase* aDatabase,
  251. const nsTArray<nsString>& aObjectStoreNames,
  252. Mode aMode);
  253. ~IDBTransaction();
  254. void
  255. AbortInternal(nsresult aAbortCode, already_AddRefed<DOMError> aError);
  256. void
  257. SendCommit();
  258. void
  259. SendAbort(nsresult aResultCode);
  260. void
  261. OnNewRequest();
  262. void
  263. OnRequestFinished(bool aActorDestroyedNormally);
  264. };
  265. } // namespace dom
  266. } // namespace mozilla
  267. #endif // mozilla_dom_idbtransaction_h__