IndexedDatabaseManager.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_indexeddatabasemanager_h__
  6. #define mozilla_dom_indexeddatabasemanager_h__
  7. #include "js/TypeDecls.h"
  8. #include "mozilla/Atomics.h"
  9. #include "mozilla/dom/quota/PersistenceType.h"
  10. #include "mozilla/Mutex.h"
  11. #include "nsClassHashtable.h"
  12. #include "nsCOMPtr.h"
  13. #include "nsHashKeys.h"
  14. #include "nsITimer.h"
  15. class nsIEventTarget;
  16. namespace mozilla {
  17. class EventChainPostVisitor;
  18. namespace dom {
  19. class IDBFactory;
  20. namespace quota {
  21. class QuotaManager;
  22. } // namespace quota
  23. namespace indexedDB {
  24. class BackgroundUtilsChild;
  25. class FileManager;
  26. class FileManagerInfo;
  27. } // namespace indexedDB
  28. class IndexedDatabaseManager final
  29. : public nsITimerCallback
  30. {
  31. typedef mozilla::dom::quota::PersistenceType PersistenceType;
  32. typedef mozilla::dom::quota::QuotaManager QuotaManager;
  33. typedef mozilla::dom::indexedDB::FileManager FileManager;
  34. typedef mozilla::dom::indexedDB::FileManagerInfo FileManagerInfo;
  35. public:
  36. enum LoggingMode
  37. {
  38. Logging_Disabled = 0,
  39. Logging_Concise,
  40. Logging_Detailed,
  41. Logging_ConciseProfilerMarks,
  42. Logging_DetailedProfilerMarks
  43. };
  44. NS_DECL_ISUPPORTS
  45. NS_DECL_NSITIMERCALLBACK
  46. // Returns a non-owning reference.
  47. static IndexedDatabaseManager*
  48. GetOrCreate();
  49. // Returns a non-owning reference.
  50. static IndexedDatabaseManager*
  51. Get();
  52. static bool
  53. IsClosed();
  54. static bool
  55. IsMainProcess()
  56. #ifdef DEBUG
  57. ;
  58. #else
  59. {
  60. return sIsMainProcess;
  61. }
  62. #endif
  63. static bool
  64. InTestingMode();
  65. static bool
  66. FullSynchronous();
  67. static LoggingMode
  68. GetLoggingMode()
  69. #ifdef DEBUG
  70. ;
  71. #else
  72. {
  73. return sLoggingMode;
  74. }
  75. #endif
  76. static mozilla::LogModule*
  77. GetLoggingModule()
  78. #ifdef DEBUG
  79. ;
  80. #else
  81. {
  82. return sLoggingModule;
  83. }
  84. #endif
  85. static bool
  86. ExperimentalFeaturesEnabled();
  87. static bool
  88. ExperimentalFeaturesEnabled(JSContext* aCx, JSObject* aGlobal);
  89. static bool
  90. IsFileHandleEnabled();
  91. static uint32_t
  92. DataThreshold();
  93. static uint32_t
  94. MaxSerializedMsgSize();
  95. void
  96. ClearBackgroundActor();
  97. void
  98. NoteLiveQuotaManager(QuotaManager* aQuotaManager);
  99. void
  100. NoteShuttingDownQuotaManager();
  101. already_AddRefed<FileManager>
  102. GetFileManager(PersistenceType aPersistenceType,
  103. const nsACString& aOrigin,
  104. const nsAString& aDatabaseName);
  105. void
  106. AddFileManager(FileManager* aFileManager);
  107. void
  108. InvalidateAllFileManagers();
  109. void
  110. InvalidateFileManagers(PersistenceType aPersistenceType,
  111. const nsACString& aOrigin);
  112. void
  113. InvalidateFileManager(PersistenceType aPersistenceType,
  114. const nsACString& aOrigin,
  115. const nsAString& aDatabaseName);
  116. nsresult
  117. AsyncDeleteFile(FileManager* aFileManager,
  118. int64_t aFileId);
  119. // Don't call this method in real code, it blocks the main thread!
  120. // It is intended to be used by mochitests to test correctness of the special
  121. // reference counting of stored blobs/files.
  122. nsresult
  123. BlockAndGetFileReferences(PersistenceType aPersistenceType,
  124. const nsACString& aOrigin,
  125. const nsAString& aDatabaseName,
  126. int64_t aFileId,
  127. int32_t* aRefCnt,
  128. int32_t* aDBRefCnt,
  129. int32_t* aSliceRefCnt,
  130. bool* aResult);
  131. nsresult
  132. FlushPendingFileDeletions();
  133. static const nsCString&
  134. GetLocale();
  135. static mozilla::Mutex&
  136. FileMutex()
  137. {
  138. IndexedDatabaseManager* mgr = Get();
  139. NS_ASSERTION(mgr, "Must have a manager here!");
  140. return mgr->mFileMutex;
  141. }
  142. static nsresult
  143. CommonPostHandleEvent(EventChainPostVisitor& aVisitor, IDBFactory* aFactory);
  144. static bool
  145. ResolveSandboxBinding(JSContext* aCx);
  146. static bool
  147. DefineIndexedDB(JSContext* aCx, JS::Handle<JSObject*> aGlobal);
  148. private:
  149. IndexedDatabaseManager();
  150. ~IndexedDatabaseManager();
  151. nsresult
  152. Init();
  153. void
  154. Destroy();
  155. static void
  156. LoggingModePrefChangedCallback(const char* aPrefName, void* aClosure);
  157. nsCOMPtr<nsIEventTarget> mBackgroundThread;
  158. nsCOMPtr<nsITimer> mDeleteTimer;
  159. // Maintains a list of all file managers per origin. This list isn't
  160. // protected by any mutex but it is only ever touched on the IO thread.
  161. nsClassHashtable<nsCStringHashKey, FileManagerInfo> mFileManagerInfos;
  162. nsClassHashtable<nsRefPtrHashKey<FileManager>,
  163. nsTArray<int64_t>> mPendingDeleteInfos;
  164. // Lock protecting FileManager.mFileInfos.
  165. // It's s also used to atomically update FileInfo.mRefCnt, FileInfo.mDBRefCnt
  166. // and FileInfo.mSliceRefCnt
  167. mozilla::Mutex mFileMutex;
  168. nsCString mLocale;
  169. indexedDB::BackgroundUtilsChild* mBackgroundActor;
  170. static bool sIsMainProcess;
  171. static bool sFullSynchronousMode;
  172. static LazyLogModule sLoggingModule;
  173. static Atomic<LoggingMode> sLoggingMode;
  174. };
  175. } // namespace dom
  176. } // namespace mozilla
  177. #endif // mozilla_dom_indexeddatabasemanager_h__