IDBDatabase.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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_idbdatabase_h__
  6. #define mozilla_dom_idbdatabase_h__
  7. #include "mozilla/Attributes.h"
  8. #include "mozilla/dom/IDBTransactionBinding.h"
  9. #include "mozilla/dom/StorageTypeBinding.h"
  10. #include "mozilla/dom/IDBWrapperCache.h"
  11. #include "mozilla/dom/quota/PersistenceType.h"
  12. #include "nsAutoPtr.h"
  13. #include "nsDataHashtable.h"
  14. #include "nsHashKeys.h"
  15. #include "nsString.h"
  16. #include "nsTHashtable.h"
  17. class nsIDocument;
  18. class nsPIDOMWindowInner;
  19. namespace mozilla {
  20. class ErrorResult;
  21. class EventChainPostVisitor;
  22. namespace dom {
  23. class Blob;
  24. class DOMStringList;
  25. class IDBFactory;
  26. class IDBMutableFile;
  27. class IDBObjectStore;
  28. struct IDBObjectStoreParameters;
  29. class IDBOpenDBRequest;
  30. class IDBRequest;
  31. class IDBTransaction;
  32. template <class> class Optional;
  33. class StringOrStringSequence;
  34. namespace indexedDB {
  35. class BackgroundDatabaseChild;
  36. class DatabaseSpec;
  37. class PBackgroundIDBDatabaseFileChild;
  38. }
  39. class IDBDatabase final
  40. : public IDBWrapperCache
  41. {
  42. typedef mozilla::dom::indexedDB::DatabaseSpec DatabaseSpec;
  43. typedef mozilla::dom::StorageType StorageType;
  44. typedef mozilla::dom::quota::PersistenceType PersistenceType;
  45. class Observer;
  46. friend class Observer;
  47. friend class IDBObjectStore;
  48. friend class IDBIndex;
  49. // The factory must be kept alive when IndexedDB is used in multiple
  50. // processes. If it dies then the entire actor tree will be destroyed with it
  51. // and the world will explode.
  52. RefPtr<IDBFactory> mFactory;
  53. nsAutoPtr<DatabaseSpec> mSpec;
  54. // Normally null except during a versionchange transaction.
  55. nsAutoPtr<DatabaseSpec> mPreviousSpec;
  56. indexedDB::BackgroundDatabaseChild* mBackgroundActor;
  57. nsTHashtable<nsPtrHashKey<IDBTransaction>> mTransactions;
  58. nsDataHashtable<nsISupportsHashKey, indexedDB::PBackgroundIDBDatabaseFileChild*>
  59. mFileActors;
  60. nsTHashtable<nsISupportsHashKey> mReceivedBlobs;
  61. RefPtr<Observer> mObserver;
  62. // Weak refs, IDBMutableFile strongly owns this IDBDatabase object.
  63. nsTArray<IDBMutableFile*> mLiveMutableFiles;
  64. const bool mFileHandleDisabled;
  65. bool mClosed;
  66. bool mInvalidated;
  67. bool mQuotaExceeded;
  68. public:
  69. static already_AddRefed<IDBDatabase>
  70. Create(IDBOpenDBRequest* aRequest,
  71. IDBFactory* aFactory,
  72. indexedDB::BackgroundDatabaseChild* aActor,
  73. DatabaseSpec* aSpec);
  74. #ifdef DEBUG
  75. void
  76. AssertIsOnOwningThread() const;
  77. PRThread*
  78. OwningThread() const;
  79. #else
  80. void
  81. AssertIsOnOwningThread() const
  82. { }
  83. #endif
  84. const nsString&
  85. Name() const;
  86. void
  87. GetName(nsAString& aName) const
  88. {
  89. AssertIsOnOwningThread();
  90. aName = Name();
  91. }
  92. uint64_t
  93. Version() const;
  94. already_AddRefed<nsIDocument>
  95. GetOwnerDocument() const;
  96. void
  97. Close()
  98. {
  99. AssertIsOnOwningThread();
  100. CloseInternal();
  101. }
  102. bool
  103. IsClosed() const
  104. {
  105. AssertIsOnOwningThread();
  106. return mClosed;
  107. }
  108. void
  109. Invalidate();
  110. // Whether or not the database has been invalidated. If it has then no further
  111. // transactions for this database will be allowed to run.
  112. bool
  113. IsInvalidated() const
  114. {
  115. AssertIsOnOwningThread();
  116. return mInvalidated;
  117. }
  118. void
  119. SetQuotaExceeded()
  120. {
  121. mQuotaExceeded = true;
  122. }
  123. void
  124. EnterSetVersionTransaction(uint64_t aNewVersion);
  125. void
  126. ExitSetVersionTransaction();
  127. // Called when a versionchange transaction is aborted to reset the
  128. // DatabaseInfo.
  129. void
  130. RevertToPreviousState();
  131. IDBFactory*
  132. Factory() const
  133. {
  134. AssertIsOnOwningThread();
  135. return mFactory;
  136. }
  137. void
  138. RegisterTransaction(IDBTransaction* aTransaction);
  139. void
  140. UnregisterTransaction(IDBTransaction* aTransaction);
  141. void
  142. AbortTransactions(bool aShouldWarn);
  143. indexedDB::PBackgroundIDBDatabaseFileChild*
  144. GetOrCreateFileActorForBlob(Blob* aBlob);
  145. void
  146. NoteFinishedFileActor(indexedDB::PBackgroundIDBDatabaseFileChild* aFileActor);
  147. void
  148. NoteReceivedBlob(Blob* aBlob);
  149. void
  150. DelayedMaybeExpireFileActors();
  151. // XXX This doesn't really belong here... It's only needed for IDBMutableFile
  152. // serialization and should be removed or fixed someday.
  153. nsresult
  154. GetQuotaInfo(nsACString& aOrigin, PersistenceType* aPersistenceType);
  155. bool
  156. IsFileHandleDisabled() const
  157. {
  158. return mFileHandleDisabled;
  159. }
  160. void
  161. NoteLiveMutableFile(IDBMutableFile* aMutableFile);
  162. void
  163. NoteFinishedMutableFile(IDBMutableFile* aMutableFile);
  164. nsPIDOMWindowInner*
  165. GetParentObject() const;
  166. already_AddRefed<DOMStringList>
  167. ObjectStoreNames() const;
  168. already_AddRefed<IDBObjectStore>
  169. CreateObjectStore(const nsAString& aName,
  170. const IDBObjectStoreParameters& aOptionalParameters,
  171. ErrorResult& aRv);
  172. void
  173. DeleteObjectStore(const nsAString& name, ErrorResult& aRv);
  174. // This will be called from the DOM.
  175. already_AddRefed<IDBTransaction>
  176. Transaction(JSContext* aCx,
  177. const StringOrStringSequence& aStoreNames,
  178. IDBTransactionMode aMode,
  179. ErrorResult& aRv);
  180. // This can be called from C++ to avoid JS exception.
  181. nsresult
  182. Transaction(JSContext* aCx,
  183. const StringOrStringSequence& aStoreNames,
  184. IDBTransactionMode aMode,
  185. IDBTransaction** aTransaction);
  186. StorageType
  187. Storage() const;
  188. IMPL_EVENT_HANDLER(abort)
  189. IMPL_EVENT_HANDLER(close)
  190. IMPL_EVENT_HANDLER(error)
  191. IMPL_EVENT_HANDLER(versionchange)
  192. already_AddRefed<IDBRequest>
  193. CreateMutableFile(JSContext* aCx,
  194. const nsAString& aName,
  195. const Optional<nsAString>& aType,
  196. ErrorResult& aRv);
  197. already_AddRefed<IDBRequest>
  198. MozCreateFileHandle(JSContext* aCx,
  199. const nsAString& aName,
  200. const Optional<nsAString>& aType,
  201. ErrorResult& aRv)
  202. {
  203. return CreateMutableFile(aCx, aName, aType, aRv);
  204. }
  205. void
  206. ClearBackgroundActor()
  207. {
  208. AssertIsOnOwningThread();
  209. mBackgroundActor = nullptr;
  210. }
  211. const DatabaseSpec*
  212. Spec() const
  213. {
  214. return mSpec;
  215. }
  216. NS_DECL_ISUPPORTS_INHERITED
  217. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBDatabase, IDBWrapperCache)
  218. // nsIDOMEventTarget
  219. virtual void
  220. LastRelease() override;
  221. virtual nsresult
  222. PostHandleEvent(EventChainPostVisitor& aVisitor) override;
  223. // nsWrapperCache
  224. virtual JSObject*
  225. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  226. private:
  227. IDBDatabase(IDBOpenDBRequest* aRequest,
  228. IDBFactory* aFactory,
  229. indexedDB::BackgroundDatabaseChild* aActor,
  230. DatabaseSpec* aSpec);
  231. ~IDBDatabase();
  232. void
  233. CloseInternal();
  234. void
  235. InvalidateInternal();
  236. bool
  237. RunningVersionChangeTransaction() const
  238. {
  239. AssertIsOnOwningThread();
  240. return !!mPreviousSpec;
  241. }
  242. void
  243. RefreshSpec(bool aMayDelete);
  244. void
  245. ExpireFileActors(bool aExpireAll);
  246. void
  247. InvalidateMutableFiles();
  248. void
  249. LogWarning(const char* aMessageName,
  250. const nsAString& aFilename,
  251. uint32_t aLineNumber,
  252. uint32_t aColumnNumber);
  253. // Only accessed by IDBObjectStore.
  254. nsresult
  255. RenameObjectStore(int64_t aObjectStoreId, const nsAString& aName);
  256. // Only accessed by IDBIndex.
  257. nsresult
  258. RenameIndex(int64_t aObjectStoreId, int64_t aIndexId, const nsAString& aName);
  259. };
  260. } // namespace dom
  261. } // namespace mozilla
  262. #endif // mozilla_dom_idbdatabase_h__