|
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_idbdatabase_h__ |
|
8 #define mozilla_dom_indexeddb_idbdatabase_h__ |
|
9 |
|
10 #include "mozilla/dom/indexedDB/IndexedDatabase.h" |
|
11 |
|
12 #include "nsIDocument.h" |
|
13 #include "nsIFileStorage.h" |
|
14 #include "nsIOfflineStorage.h" |
|
15 |
|
16 #include "mozilla/Attributes.h" |
|
17 #include "mozilla/DOMEventTargetHelper.h" |
|
18 #include "mozilla/dom/IDBObjectStoreBinding.h" |
|
19 #include "mozilla/dom/IDBTransactionBinding.h" |
|
20 #include "mozilla/dom/quota/PersistenceType.h" |
|
21 |
|
22 #include "mozilla/dom/indexedDB/FileManager.h" |
|
23 #include "mozilla/dom/indexedDB/IDBRequest.h" |
|
24 #include "mozilla/dom/indexedDB/IDBWrapperCache.h" |
|
25 |
|
26 class nsIScriptContext; |
|
27 class nsPIDOMWindow; |
|
28 |
|
29 namespace mozilla { |
|
30 class EventChainPostVisitor; |
|
31 namespace dom { |
|
32 class ContentParent; |
|
33 namespace quota { |
|
34 class Client; |
|
35 } |
|
36 } |
|
37 } |
|
38 |
|
39 BEGIN_INDEXEDDB_NAMESPACE |
|
40 |
|
41 class AsyncConnectionHelper; |
|
42 struct DatabaseInfo; |
|
43 class IDBFactory; |
|
44 class IDBIndex; |
|
45 class IDBObjectStore; |
|
46 class IDBTransaction; |
|
47 class IndexedDatabaseManager; |
|
48 class IndexedDBDatabaseChild; |
|
49 class IndexedDBDatabaseParent; |
|
50 struct ObjectStoreInfoGuts; |
|
51 |
|
52 class IDBDatabase : public IDBWrapperCache, |
|
53 public nsIOfflineStorage |
|
54 { |
|
55 friend class AsyncConnectionHelper; |
|
56 friend class IndexedDatabaseManager; |
|
57 friend class IndexedDBDatabaseParent; |
|
58 friend class IndexedDBDatabaseChild; |
|
59 |
|
60 public: |
|
61 NS_DECL_ISUPPORTS_INHERITED |
|
62 NS_DECL_NSIFILESTORAGE |
|
63 NS_DECL_NSIOFFLINESTORAGE |
|
64 |
|
65 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBDatabase, IDBWrapperCache) |
|
66 |
|
67 static already_AddRefed<IDBDatabase> |
|
68 Create(IDBWrapperCache* aOwnerCache, |
|
69 IDBFactory* aFactory, |
|
70 already_AddRefed<DatabaseInfo> aDatabaseInfo, |
|
71 const nsACString& aASCIIOrigin, |
|
72 FileManager* aFileManager, |
|
73 mozilla::dom::ContentParent* aContentParent); |
|
74 |
|
75 static IDBDatabase* |
|
76 FromStorage(nsIOfflineStorage* aStorage); |
|
77 |
|
78 static IDBDatabase* |
|
79 FromStorage(nsIFileStorage* aStorage) |
|
80 { |
|
81 nsCOMPtr<nsIOfflineStorage> storage = do_QueryInterface(aStorage); |
|
82 return storage ? FromStorage(storage) : nullptr; |
|
83 } |
|
84 |
|
85 // nsIDOMEventTarget |
|
86 virtual nsresult PostHandleEvent( |
|
87 EventChainPostVisitor& aVisitor) MOZ_OVERRIDE; |
|
88 |
|
89 DatabaseInfo* Info() const |
|
90 { |
|
91 return mDatabaseInfo; |
|
92 } |
|
93 |
|
94 const nsString& Name() const |
|
95 { |
|
96 return mName; |
|
97 } |
|
98 |
|
99 const nsString& FilePath() const |
|
100 { |
|
101 return mFilePath; |
|
102 } |
|
103 |
|
104 already_AddRefed<nsIDocument> GetOwnerDocument() |
|
105 { |
|
106 if (!GetOwner()) { |
|
107 return nullptr; |
|
108 } |
|
109 |
|
110 nsCOMPtr<nsIDocument> doc = GetOwner()->GetExtantDoc(); |
|
111 return doc.forget(); |
|
112 } |
|
113 |
|
114 void DisconnectFromActorParent(); |
|
115 |
|
116 void CloseInternal(bool aIsDead); |
|
117 |
|
118 void EnterSetVersionTransaction(); |
|
119 void ExitSetVersionTransaction(); |
|
120 |
|
121 // Called when a versionchange transaction is aborted to reset the |
|
122 // DatabaseInfo. |
|
123 void RevertToPreviousState(); |
|
124 |
|
125 FileManager* Manager() const |
|
126 { |
|
127 return mFileManager; |
|
128 } |
|
129 |
|
130 void |
|
131 SetActor(IndexedDBDatabaseChild* aActorChild) |
|
132 { |
|
133 NS_ASSERTION(!aActorChild || !mActorChild, "Shouldn't have more than one!"); |
|
134 mActorChild = aActorChild; |
|
135 } |
|
136 |
|
137 void |
|
138 SetActor(IndexedDBDatabaseParent* aActorParent) |
|
139 { |
|
140 NS_ASSERTION(!aActorParent || !mActorParent, |
|
141 "Shouldn't have more than one!"); |
|
142 mActorParent = aActorParent; |
|
143 } |
|
144 |
|
145 IndexedDBDatabaseChild* |
|
146 GetActorChild() const |
|
147 { |
|
148 return mActorChild; |
|
149 } |
|
150 |
|
151 IndexedDBDatabaseParent* |
|
152 GetActorParent() const |
|
153 { |
|
154 return mActorParent; |
|
155 } |
|
156 |
|
157 mozilla::dom::ContentParent* |
|
158 GetContentParent() const |
|
159 { |
|
160 return mContentParent; |
|
161 } |
|
162 |
|
163 already_AddRefed<IDBObjectStore> |
|
164 CreateObjectStoreInternal(IDBTransaction* aTransaction, |
|
165 const ObjectStoreInfoGuts& aInfo, |
|
166 ErrorResult& aRv); |
|
167 |
|
168 IDBFactory* |
|
169 Factory() const |
|
170 { |
|
171 return mFactory; |
|
172 } |
|
173 |
|
174 // nsWrapperCache |
|
175 virtual JSObject* |
|
176 WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
177 |
|
178 // WebIDL |
|
179 nsPIDOMWindow* |
|
180 GetParentObject() const |
|
181 { |
|
182 return GetOwner(); |
|
183 } |
|
184 |
|
185 void |
|
186 GetName(nsString& aName) const |
|
187 { |
|
188 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
|
189 aName.Assign(mName); |
|
190 } |
|
191 |
|
192 uint64_t |
|
193 Version() const; |
|
194 |
|
195 already_AddRefed<mozilla::dom::DOMStringList> |
|
196 GetObjectStoreNames(ErrorResult& aRv) const; |
|
197 |
|
198 already_AddRefed<IDBObjectStore> |
|
199 CreateObjectStore(JSContext* aCx, const nsAString& aName, |
|
200 const IDBObjectStoreParameters& aOptionalParameters, |
|
201 ErrorResult& aRv); |
|
202 |
|
203 void |
|
204 DeleteObjectStore(const nsAString& name, ErrorResult& aRv); |
|
205 |
|
206 already_AddRefed<indexedDB::IDBTransaction> |
|
207 Transaction(const nsAString& aStoreName, IDBTransactionMode aMode, |
|
208 ErrorResult& aRv) |
|
209 { |
|
210 Sequence<nsString> list; |
|
211 list.AppendElement(aStoreName); |
|
212 return Transaction(list, aMode, aRv); |
|
213 } |
|
214 |
|
215 already_AddRefed<indexedDB::IDBTransaction> |
|
216 Transaction(const Sequence<nsString>& aStoreNames, IDBTransactionMode aMode, |
|
217 ErrorResult& aRv); |
|
218 |
|
219 IMPL_EVENT_HANDLER(abort) |
|
220 IMPL_EVENT_HANDLER(error) |
|
221 IMPL_EVENT_HANDLER(versionchange) |
|
222 |
|
223 mozilla::dom::StorageType |
|
224 Storage() const |
|
225 { |
|
226 return PersistenceTypeToStorage(mPersistenceType); |
|
227 } |
|
228 |
|
229 already_AddRefed<IDBRequest> |
|
230 MozCreateFileHandle(const nsAString& aName, const Optional<nsAString>& aType, |
|
231 ErrorResult& aRv); |
|
232 |
|
233 virtual void LastRelease() MOZ_OVERRIDE; |
|
234 |
|
235 private: |
|
236 IDBDatabase(IDBWrapperCache* aOwnerCache); |
|
237 ~IDBDatabase(); |
|
238 |
|
239 void OnUnlink(); |
|
240 void InvalidateInternal(bool aIsDead); |
|
241 |
|
242 // The factory must be kept alive when IndexedDB is used in multiple |
|
243 // processes. If it dies then the entire actor tree will be destroyed with it |
|
244 // and the world will explode. |
|
245 nsRefPtr<IDBFactory> mFactory; |
|
246 |
|
247 nsRefPtr<DatabaseInfo> mDatabaseInfo; |
|
248 |
|
249 // Set to a copy of the existing DatabaseInfo when starting a versionchange |
|
250 // transaction. |
|
251 nsRefPtr<DatabaseInfo> mPreviousDatabaseInfo; |
|
252 nsCString mDatabaseId; |
|
253 nsString mName; |
|
254 nsString mFilePath; |
|
255 nsCString mASCIIOrigin; |
|
256 |
|
257 nsRefPtr<FileManager> mFileManager; |
|
258 |
|
259 IndexedDBDatabaseChild* mActorChild; |
|
260 IndexedDBDatabaseParent* mActorParent; |
|
261 |
|
262 mozilla::dom::ContentParent* mContentParent; |
|
263 |
|
264 nsRefPtr<mozilla::dom::quota::Client> mQuotaClient; |
|
265 |
|
266 bool mInvalidated; |
|
267 bool mRegistered; |
|
268 bool mClosed; |
|
269 bool mRunningVersionChange; |
|
270 }; |
|
271 |
|
272 END_INDEXEDDB_NAMESPACE |
|
273 |
|
274 #endif // mozilla_dom_indexeddb_idbdatabase_h__ |