michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_dom_indexeddb_indexeddatabasemanager_h__ michael@0: #define mozilla_dom_indexeddb_indexeddatabasemanager_h__ michael@0: michael@0: #include "mozilla/dom/indexedDB/IndexedDatabase.h" michael@0: michael@0: #include "nsIIndexedDatabaseManager.h" michael@0: #include "nsIObserver.h" michael@0: michael@0: #include "js/TypeDecls.h" michael@0: #include "mozilla/Atomics.h" michael@0: #include "mozilla/dom/quota/PersistenceType.h" michael@0: #include "mozilla/Mutex.h" michael@0: #include "nsClassHashtable.h" michael@0: #include "nsHashKeys.h" michael@0: michael@0: #define INDEXEDDB_MANAGER_CONTRACTID "@mozilla.org/dom/indexeddb/manager;1" michael@0: michael@0: class nsPIDOMWindow; michael@0: michael@0: namespace mozilla { michael@0: class EventChainPostVisitor; michael@0: namespace dom { michael@0: class TabContext; michael@0: namespace quota { michael@0: class OriginOrPatternString; michael@0: } michael@0: } michael@0: } michael@0: michael@0: BEGIN_INDEXEDDB_NAMESPACE michael@0: michael@0: class FileManager; michael@0: class FileManagerInfo; michael@0: michael@0: class IndexedDatabaseManager MOZ_FINAL : public nsIIndexedDatabaseManager, michael@0: public nsIObserver michael@0: { michael@0: typedef mozilla::dom::quota::OriginOrPatternString OriginOrPatternString; michael@0: typedef mozilla::dom::quota::PersistenceType PersistenceType; michael@0: michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIINDEXEDDATABASEMANAGER michael@0: NS_DECL_NSIOBSERVER michael@0: michael@0: // Returns a non-owning reference. michael@0: static IndexedDatabaseManager* michael@0: GetOrCreate(); michael@0: michael@0: // Returns a non-owning reference. michael@0: static IndexedDatabaseManager* michael@0: Get(); michael@0: michael@0: // Returns an owning reference! No one should call this but the factory. michael@0: static IndexedDatabaseManager* michael@0: FactoryCreate(); michael@0: michael@0: static bool michael@0: IsClosed(); michael@0: michael@0: static bool michael@0: IsMainProcess() michael@0: #ifdef DEBUG michael@0: ; michael@0: #else michael@0: { michael@0: return sIsMainProcess; michael@0: } michael@0: #endif michael@0: michael@0: static bool michael@0: InLowDiskSpaceMode() michael@0: #ifdef DEBUG michael@0: ; michael@0: #else michael@0: { michael@0: return !!sLowDiskSpaceMode; michael@0: } michael@0: #endif michael@0: michael@0: already_AddRefed michael@0: GetFileManager(PersistenceType aPersistenceType, michael@0: const nsACString& aOrigin, michael@0: const nsAString& aDatabaseName); michael@0: michael@0: void michael@0: AddFileManager(FileManager* aFileManager); michael@0: michael@0: void michael@0: InvalidateAllFileManagers(); michael@0: michael@0: void michael@0: InvalidateFileManagers(PersistenceType aPersistenceType, michael@0: const OriginOrPatternString& aOriginOrPattern); michael@0: michael@0: void michael@0: InvalidateFileManager(PersistenceType aPersistenceType, michael@0: const nsACString& aOrigin, michael@0: const nsAString& aDatabaseName); michael@0: michael@0: nsresult michael@0: AsyncDeleteFile(FileManager* aFileManager, michael@0: int64_t aFileId); michael@0: michael@0: // Don't call this method in real code, it blocks the main thread! michael@0: // It is intended to be used by mochitests to test correctness of the special michael@0: // reference counting of stored blobs/files. michael@0: nsresult michael@0: BlockAndGetFileReferences(PersistenceType aPersistenceType, michael@0: const nsACString& aOrigin, michael@0: const nsAString& aDatabaseName, michael@0: int64_t aFileId, michael@0: int32_t* aRefCnt, michael@0: int32_t* aDBRefCnt, michael@0: int32_t* aSliceRefCnt, michael@0: bool* aResult); michael@0: michael@0: static mozilla::Mutex& michael@0: FileMutex() michael@0: { michael@0: IndexedDatabaseManager* mgr = Get(); michael@0: NS_ASSERTION(mgr, "Must have a manager here!"); michael@0: michael@0: return mgr->mFileMutex; michael@0: } michael@0: michael@0: static nsresult michael@0: FireWindowOnError(nsPIDOMWindow* aOwner, michael@0: EventChainPostVisitor& aVisitor); michael@0: michael@0: static bool michael@0: TabContextMayAccessOrigin(const mozilla::dom::TabContext& aContext, michael@0: const nsACString& aOrigin); michael@0: michael@0: static bool michael@0: DefineIndexedDB(JSContext* aCx, JS::Handle aGlobal); michael@0: michael@0: private: michael@0: IndexedDatabaseManager(); michael@0: ~IndexedDatabaseManager(); michael@0: michael@0: nsresult michael@0: Init(); michael@0: michael@0: void michael@0: Destroy(); michael@0: michael@0: static PLDHashOperator michael@0: InvalidateAndRemoveFileManagers(const nsACString& aKey, michael@0: nsAutoPtr& aValue, michael@0: void* aUserArg); michael@0: michael@0: // Maintains a list of all file managers per origin. This list isn't michael@0: // protected by any mutex but it is only ever touched on the IO thread. michael@0: nsClassHashtable mFileManagerInfos; michael@0: michael@0: // Lock protecting FileManager.mFileInfos and nsDOMFileBase.mFileInfos michael@0: // It's s also used to atomically update FileInfo.mRefCnt, FileInfo.mDBRefCnt michael@0: // and FileInfo.mSliceRefCnt michael@0: mozilla::Mutex mFileMutex; michael@0: michael@0: static bool sIsMainProcess; michael@0: static mozilla::Atomic sLowDiskSpaceMode; michael@0: }; michael@0: michael@0: END_INDEXEDDB_NAMESPACE michael@0: michael@0: #endif /* mozilla_dom_indexeddb_indexeddatabasemanager_h__ */