dom/indexedDB/IndexedDatabaseManager.h

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

mercurial