IndexedDatabaseManager.h (5321B)
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/. */ 6 7 #ifndef mozilla_dom_indexeddatabasemanager_h__ 8 #define mozilla_dom_indexeddatabasemanager_h__ 9 10 #include "MainThreadUtils.h" 11 #include "SafeRefPtr.h" 12 #include "js/TypeDecls.h" 13 #include "mozilla/Atomics.h" 14 #include "mozilla/Logging.h" 15 #include "mozilla/Mutex.h" 16 #include "mozilla/dom/quota/PersistenceType.h" 17 #include "nsClassHashtable.h" 18 #include "nsHashKeys.h" 19 #include "nsIIndexedDatabaseManager.h" 20 21 namespace mozilla { 22 23 class EventChainPostVisitor; 24 25 namespace dom { 26 27 class IDBFactory; 28 29 namespace indexedDB { 30 31 class BackgroundUtilsChild; 32 class DatabaseFileManager; 33 class FileManagerInfo; 34 35 } // namespace indexedDB 36 37 class IndexedDatabaseManager final : public nsIIndexedDatabaseManager { 38 using PersistenceType = mozilla::dom::quota::PersistenceType; 39 using DatabaseFileManager = mozilla::dom::indexedDB::DatabaseFileManager; 40 using FileManagerInfo = mozilla::dom::indexedDB::FileManagerInfo; 41 42 public: 43 enum LoggingMode { 44 Logging_Disabled = 0, 45 Logging_Concise, 46 Logging_Detailed, 47 Logging_ConciseProfilerMarks, 48 Logging_DetailedProfilerMarks 49 }; 50 51 NS_DECL_ISUPPORTS 52 NS_DECL_NSIINDEXEDDATABASEMANAGER 53 54 // Returns a non-owning reference. 55 static IndexedDatabaseManager* GetOrCreate(); 56 57 // Returns a non-owning reference. 58 static IndexedDatabaseManager* Get(); 59 60 // No one should call this but the factory. 61 static already_AddRefed<IndexedDatabaseManager> FactoryCreate(); 62 63 static bool IsClosed(); 64 65 static bool IsMainProcess() 66 #ifdef DEBUG 67 ; 68 #else 69 { 70 return sIsMainProcess; 71 } 72 #endif 73 74 static bool FullSynchronous(); 75 76 static LoggingMode GetLoggingMode() 77 #ifdef DEBUG 78 ; 79 #else 80 { 81 return sLoggingMode; 82 } 83 #endif 84 85 static mozilla::LogModule* GetLoggingModule() 86 #ifdef DEBUG 87 ; 88 #else 89 { 90 return sLoggingModule; 91 } 92 #endif 93 94 static uint32_t DataThreshold(); 95 96 static uint32_t MaxStructuredCloneSize(); 97 98 static uint32_t MaxSerializedMsgSize(); 99 100 // The maximum number of extra entries to preload in an Cursor::OpenOp or 101 // Cursor::ContinueOp. 102 static int32_t MaxPreloadExtraRecords(); 103 104 void ClearBackgroundActor(); 105 106 [[nodiscard]] SafeRefPtr<DatabaseFileManager> GetFileManager( 107 PersistenceType aPersistenceType, const nsACString& aOrigin, 108 const nsAString& aDatabaseName); 109 110 [[nodiscard]] SafeRefPtr<DatabaseFileManager> 111 GetFileManagerByDatabaseFilePath(PersistenceType aPersistenceType, 112 const nsACString& aOrigin, 113 const nsAString& aDatabaseFilePath); 114 115 const nsTArray<SafeRefPtr<DatabaseFileManager>>& GetFileManagers( 116 PersistenceType aPersistenceType, const nsACString& aOrigin); 117 118 void AddFileManager(SafeRefPtr<DatabaseFileManager> aFileManager); 119 120 void InvalidateAllFileManagers(); 121 122 void InvalidateFileManagers(PersistenceType aPersistenceType); 123 124 void InvalidateFileManagers(PersistenceType aPersistenceType, 125 const nsACString& aOrigin); 126 127 void InvalidateFileManager(PersistenceType aPersistenceType, 128 const nsACString& aOrigin, 129 const nsAString& aDatabaseName); 130 131 // Don't call this method in real code, it blocks the main thread! 132 // It is intended to be used by mochitests to test correctness of the special 133 // reference counting of stored blobs/files. 134 nsresult BlockAndGetFileReferences(PersistenceType aPersistenceType, 135 const nsACString& aOrigin, 136 const nsAString& aDatabaseName, 137 int64_t aFileId, int32_t* aRefCnt, 138 int32_t* aDBRefCnt, bool* aResult); 139 140 nsresult FlushPendingFileDeletions(); 141 142 // XXX This extra explicit initialization should go away with bug 1730706. 143 nsresult EnsureLocale(); 144 145 static const nsCString& GetLocale(); 146 147 static bool ResolveSandboxBinding(JSContext* aCx); 148 149 static bool DefineIndexedDB(JSContext* aCx, JS::Handle<JSObject*> aGlobal); 150 151 private: 152 IndexedDatabaseManager(); 153 ~IndexedDatabaseManager(); 154 155 nsresult Init(); 156 157 void Destroy(); 158 159 nsresult EnsureBackgroundActor(); 160 161 static void LoggingModePrefChangedCallback(const char* aPrefName, 162 void* aClosure); 163 164 // Maintains a list of all DatabaseFileManager objects per origin. This list 165 // isn't protected by any mutex but it is only ever touched on the IO thread. 166 nsClassHashtable<nsCStringHashKey, FileManagerInfo> mFileManagerInfos; 167 168 nsClassHashtable<nsRefPtrHashKey<DatabaseFileManager>, nsTArray<int64_t>> 169 mPendingDeleteInfos; 170 171 nsCString mLocale; 172 bool mLocaleInitialized MOZ_GUARDED_BY(sMainThreadCapability); 173 174 indexedDB::BackgroundUtilsChild* mBackgroundActor; 175 176 static bool sIsMainProcess; 177 static bool sFullSynchronousMode; 178 static LazyLogModule sLoggingModule; 179 static Atomic<LoggingMode> sLoggingMode; 180 }; 181 182 } // namespace dom 183 } // namespace mozilla 184 185 #endif // mozilla_dom_indexeddatabasemanager_h__