Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef mozilla_dom_indexeddb_indexeddatabasemanager_h__ |
michael@0 | 8 | #define mozilla_dom_indexeddb_indexeddatabasemanager_h__ |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/dom/indexedDB/IndexedDatabase.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "nsIIndexedDatabaseManager.h" |
michael@0 | 13 | #include "nsIObserver.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "js/TypeDecls.h" |
michael@0 | 16 | #include "mozilla/Atomics.h" |
michael@0 | 17 | #include "mozilla/dom/quota/PersistenceType.h" |
michael@0 | 18 | #include "mozilla/Mutex.h" |
michael@0 | 19 | #include "nsClassHashtable.h" |
michael@0 | 20 | #include "nsHashKeys.h" |
michael@0 | 21 | |
michael@0 | 22 | #define INDEXEDDB_MANAGER_CONTRACTID "@mozilla.org/dom/indexeddb/manager;1" |
michael@0 | 23 | |
michael@0 | 24 | class nsPIDOMWindow; |
michael@0 | 25 | |
michael@0 | 26 | namespace mozilla { |
michael@0 | 27 | class EventChainPostVisitor; |
michael@0 | 28 | namespace dom { |
michael@0 | 29 | class TabContext; |
michael@0 | 30 | namespace quota { |
michael@0 | 31 | class OriginOrPatternString; |
michael@0 | 32 | } |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | BEGIN_INDEXEDDB_NAMESPACE |
michael@0 | 37 | |
michael@0 | 38 | class FileManager; |
michael@0 | 39 | class FileManagerInfo; |
michael@0 | 40 | |
michael@0 | 41 | class IndexedDatabaseManager MOZ_FINAL : public nsIIndexedDatabaseManager, |
michael@0 | 42 | public nsIObserver |
michael@0 | 43 | { |
michael@0 | 44 | typedef mozilla::dom::quota::OriginOrPatternString OriginOrPatternString; |
michael@0 | 45 | typedef mozilla::dom::quota::PersistenceType PersistenceType; |
michael@0 | 46 | |
michael@0 | 47 | public: |
michael@0 | 48 | NS_DECL_ISUPPORTS |
michael@0 | 49 | NS_DECL_NSIINDEXEDDATABASEMANAGER |
michael@0 | 50 | NS_DECL_NSIOBSERVER |
michael@0 | 51 | |
michael@0 | 52 | // Returns a non-owning reference. |
michael@0 | 53 | static IndexedDatabaseManager* |
michael@0 | 54 | GetOrCreate(); |
michael@0 | 55 | |
michael@0 | 56 | // Returns a non-owning reference. |
michael@0 | 57 | static IndexedDatabaseManager* |
michael@0 | 58 | Get(); |
michael@0 | 59 | |
michael@0 | 60 | // Returns an owning reference! No one should call this but the factory. |
michael@0 | 61 | static IndexedDatabaseManager* |
michael@0 | 62 | FactoryCreate(); |
michael@0 | 63 | |
michael@0 | 64 | static bool |
michael@0 | 65 | IsClosed(); |
michael@0 | 66 | |
michael@0 | 67 | static bool |
michael@0 | 68 | IsMainProcess() |
michael@0 | 69 | #ifdef DEBUG |
michael@0 | 70 | ; |
michael@0 | 71 | #else |
michael@0 | 72 | { |
michael@0 | 73 | return sIsMainProcess; |
michael@0 | 74 | } |
michael@0 | 75 | #endif |
michael@0 | 76 | |
michael@0 | 77 | static bool |
michael@0 | 78 | InLowDiskSpaceMode() |
michael@0 | 79 | #ifdef DEBUG |
michael@0 | 80 | ; |
michael@0 | 81 | #else |
michael@0 | 82 | { |
michael@0 | 83 | return !!sLowDiskSpaceMode; |
michael@0 | 84 | } |
michael@0 | 85 | #endif |
michael@0 | 86 | |
michael@0 | 87 | already_AddRefed<FileManager> |
michael@0 | 88 | GetFileManager(PersistenceType aPersistenceType, |
michael@0 | 89 | const nsACString& aOrigin, |
michael@0 | 90 | const nsAString& aDatabaseName); |
michael@0 | 91 | |
michael@0 | 92 | void |
michael@0 | 93 | AddFileManager(FileManager* aFileManager); |
michael@0 | 94 | |
michael@0 | 95 | void |
michael@0 | 96 | InvalidateAllFileManagers(); |
michael@0 | 97 | |
michael@0 | 98 | void |
michael@0 | 99 | InvalidateFileManagers(PersistenceType aPersistenceType, |
michael@0 | 100 | const OriginOrPatternString& aOriginOrPattern); |
michael@0 | 101 | |
michael@0 | 102 | void |
michael@0 | 103 | InvalidateFileManager(PersistenceType aPersistenceType, |
michael@0 | 104 | const nsACString& aOrigin, |
michael@0 | 105 | const nsAString& aDatabaseName); |
michael@0 | 106 | |
michael@0 | 107 | nsresult |
michael@0 | 108 | AsyncDeleteFile(FileManager* aFileManager, |
michael@0 | 109 | int64_t aFileId); |
michael@0 | 110 | |
michael@0 | 111 | // Don't call this method in real code, it blocks the main thread! |
michael@0 | 112 | // It is intended to be used by mochitests to test correctness of the special |
michael@0 | 113 | // reference counting of stored blobs/files. |
michael@0 | 114 | nsresult |
michael@0 | 115 | BlockAndGetFileReferences(PersistenceType aPersistenceType, |
michael@0 | 116 | const nsACString& aOrigin, |
michael@0 | 117 | const nsAString& aDatabaseName, |
michael@0 | 118 | int64_t aFileId, |
michael@0 | 119 | int32_t* aRefCnt, |
michael@0 | 120 | int32_t* aDBRefCnt, |
michael@0 | 121 | int32_t* aSliceRefCnt, |
michael@0 | 122 | bool* aResult); |
michael@0 | 123 | |
michael@0 | 124 | static mozilla::Mutex& |
michael@0 | 125 | FileMutex() |
michael@0 | 126 | { |
michael@0 | 127 | IndexedDatabaseManager* mgr = Get(); |
michael@0 | 128 | NS_ASSERTION(mgr, "Must have a manager here!"); |
michael@0 | 129 | |
michael@0 | 130 | return mgr->mFileMutex; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | static nsresult |
michael@0 | 134 | FireWindowOnError(nsPIDOMWindow* aOwner, |
michael@0 | 135 | EventChainPostVisitor& aVisitor); |
michael@0 | 136 | |
michael@0 | 137 | static bool |
michael@0 | 138 | TabContextMayAccessOrigin(const mozilla::dom::TabContext& aContext, |
michael@0 | 139 | const nsACString& aOrigin); |
michael@0 | 140 | |
michael@0 | 141 | static bool |
michael@0 | 142 | DefineIndexedDB(JSContext* aCx, JS::Handle<JSObject*> aGlobal); |
michael@0 | 143 | |
michael@0 | 144 | private: |
michael@0 | 145 | IndexedDatabaseManager(); |
michael@0 | 146 | ~IndexedDatabaseManager(); |
michael@0 | 147 | |
michael@0 | 148 | nsresult |
michael@0 | 149 | Init(); |
michael@0 | 150 | |
michael@0 | 151 | void |
michael@0 | 152 | Destroy(); |
michael@0 | 153 | |
michael@0 | 154 | static PLDHashOperator |
michael@0 | 155 | InvalidateAndRemoveFileManagers(const nsACString& aKey, |
michael@0 | 156 | nsAutoPtr<FileManagerInfo>& aValue, |
michael@0 | 157 | void* aUserArg); |
michael@0 | 158 | |
michael@0 | 159 | // Maintains a list of all file managers per origin. This list isn't |
michael@0 | 160 | // protected by any mutex but it is only ever touched on the IO thread. |
michael@0 | 161 | nsClassHashtable<nsCStringHashKey, FileManagerInfo> mFileManagerInfos; |
michael@0 | 162 | |
michael@0 | 163 | // Lock protecting FileManager.mFileInfos and nsDOMFileBase.mFileInfos |
michael@0 | 164 | // It's s also used to atomically update FileInfo.mRefCnt, FileInfo.mDBRefCnt |
michael@0 | 165 | // and FileInfo.mSliceRefCnt |
michael@0 | 166 | mozilla::Mutex mFileMutex; |
michael@0 | 167 | |
michael@0 | 168 | static bool sIsMainProcess; |
michael@0 | 169 | static mozilla::Atomic<bool> sLowDiskSpaceMode; |
michael@0 | 170 | }; |
michael@0 | 171 | |
michael@0 | 172 | END_INDEXEDDB_NAMESPACE |
michael@0 | 173 | |
michael@0 | 174 | #endif /* mozilla_dom_indexeddb_indexeddatabasemanager_h__ */ |