/work/obj-fuzz/dist/include/mozilla/dom/IDBDatabase.h
Line | Count | Source (jump to first uncovered line) |
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_idbdatabase_h__ |
8 | | #define mozilla_dom_idbdatabase_h__ |
9 | | |
10 | | #include "mozilla/Attributes.h" |
11 | | #include "mozilla/dom/IDBTransactionBinding.h" |
12 | | #include "mozilla/dom/StorageTypeBinding.h" |
13 | | #include "mozilla/dom/IDBWrapperCache.h" |
14 | | #include "mozilla/dom/quota/PersistenceType.h" |
15 | | #include "nsAutoPtr.h" |
16 | | #include "nsDataHashtable.h" |
17 | | #include "nsHashKeys.h" |
18 | | #include "nsString.h" |
19 | | #include "nsTHashtable.h" |
20 | | |
21 | | class nsIDocument; |
22 | | class nsIEventTarget; |
23 | | class nsPIDOMWindowInner; |
24 | | |
25 | | namespace mozilla { |
26 | | |
27 | | class ErrorResult; |
28 | | class EventChainPostVisitor; |
29 | | |
30 | | namespace dom { |
31 | | |
32 | | class Blob; |
33 | | class DOMStringList; |
34 | | class IDBFactory; |
35 | | class IDBMutableFile; |
36 | | class IDBObjectStore; |
37 | | struct IDBObjectStoreParameters; |
38 | | class IDBOpenDBRequest; |
39 | | class IDBRequest; |
40 | | class IDBTransaction; |
41 | | template <class> class Optional; |
42 | | class StringOrStringSequence; |
43 | | |
44 | | namespace indexedDB { |
45 | | class BackgroundDatabaseChild; |
46 | | class DatabaseSpec; |
47 | | class PBackgroundIDBDatabaseFileChild; |
48 | | } |
49 | | |
50 | | class IDBDatabase final |
51 | | : public IDBWrapperCache |
52 | | { |
53 | | typedef mozilla::dom::indexedDB::DatabaseSpec DatabaseSpec; |
54 | | typedef mozilla::dom::StorageType StorageType; |
55 | | typedef mozilla::dom::quota::PersistenceType PersistenceType; |
56 | | |
57 | | class Observer; |
58 | | friend class Observer; |
59 | | |
60 | | friend class IDBObjectStore; |
61 | | friend class IDBIndex; |
62 | | |
63 | | // The factory must be kept alive when IndexedDB is used in multiple |
64 | | // processes. If it dies then the entire actor tree will be destroyed with it |
65 | | // and the world will explode. |
66 | | RefPtr<IDBFactory> mFactory; |
67 | | |
68 | | nsAutoPtr<DatabaseSpec> mSpec; |
69 | | |
70 | | // Normally null except during a versionchange transaction. |
71 | | nsAutoPtr<DatabaseSpec> mPreviousSpec; |
72 | | |
73 | | indexedDB::BackgroundDatabaseChild* mBackgroundActor; |
74 | | |
75 | | nsTHashtable<nsPtrHashKey<IDBTransaction>> mTransactions; |
76 | | |
77 | | nsDataHashtable<nsISupportsHashKey, indexedDB::PBackgroundIDBDatabaseFileChild*> |
78 | | mFileActors; |
79 | | |
80 | | RefPtr<Observer> mObserver; |
81 | | |
82 | | // Weak refs, IDBMutableFile strongly owns this IDBDatabase object. |
83 | | nsTArray<IDBMutableFile*> mLiveMutableFiles; |
84 | | |
85 | | const bool mFileHandleDisabled; |
86 | | bool mClosed; |
87 | | bool mInvalidated; |
88 | | bool mQuotaExceeded; |
89 | | bool mIncreasedActiveDatabaseCount; |
90 | | |
91 | | public: |
92 | | static already_AddRefed<IDBDatabase> |
93 | | Create(IDBOpenDBRequest* aRequest, |
94 | | IDBFactory* aFactory, |
95 | | indexedDB::BackgroundDatabaseChild* aActor, |
96 | | DatabaseSpec* aSpec); |
97 | | |
98 | | void |
99 | | AssertIsOnOwningThread() const |
100 | | #ifdef DEBUG |
101 | | ; |
102 | | #else |
103 | 0 | { } |
104 | | #endif |
105 | | |
106 | | nsIEventTarget* |
107 | | EventTarget() const; |
108 | | |
109 | | const nsString& |
110 | | Name() const; |
111 | | |
112 | | void |
113 | | GetName(nsAString& aName) const |
114 | 0 | { |
115 | 0 | AssertIsOnOwningThread(); |
116 | 0 |
|
117 | 0 | aName = Name(); |
118 | 0 | } |
119 | | |
120 | | uint64_t |
121 | | Version() const; |
122 | | |
123 | | already_AddRefed<nsIDocument> |
124 | | GetOwnerDocument() const; |
125 | | |
126 | | void |
127 | | Close() |
128 | 0 | { |
129 | 0 | AssertIsOnOwningThread(); |
130 | 0 |
|
131 | 0 | CloseInternal(); |
132 | 0 | } |
133 | | |
134 | | bool |
135 | | IsClosed() const |
136 | | { |
137 | | AssertIsOnOwningThread(); |
138 | | |
139 | | return mClosed; |
140 | | } |
141 | | |
142 | | void |
143 | | Invalidate(); |
144 | | |
145 | | // Whether or not the database has been invalidated. If it has then no further |
146 | | // transactions for this database will be allowed to run. |
147 | | bool |
148 | | IsInvalidated() const |
149 | | { |
150 | | AssertIsOnOwningThread(); |
151 | | |
152 | | return mInvalidated; |
153 | | } |
154 | | |
155 | | void |
156 | | SetQuotaExceeded() |
157 | | { |
158 | | mQuotaExceeded = true; |
159 | | } |
160 | | |
161 | | void |
162 | | EnterSetVersionTransaction(uint64_t aNewVersion); |
163 | | |
164 | | void |
165 | | ExitSetVersionTransaction(); |
166 | | |
167 | | // Called when a versionchange transaction is aborted to reset the |
168 | | // DatabaseInfo. |
169 | | void |
170 | | RevertToPreviousState(); |
171 | | |
172 | | IDBFactory* |
173 | | Factory() const |
174 | | { |
175 | | AssertIsOnOwningThread(); |
176 | | |
177 | | return mFactory; |
178 | | } |
179 | | |
180 | | void |
181 | | RegisterTransaction(IDBTransaction* aTransaction); |
182 | | |
183 | | void |
184 | | UnregisterTransaction(IDBTransaction* aTransaction); |
185 | | |
186 | | void |
187 | | AbortTransactions(bool aShouldWarn); |
188 | | |
189 | | indexedDB::PBackgroundIDBDatabaseFileChild* |
190 | | GetOrCreateFileActorForBlob(Blob* aBlob); |
191 | | |
192 | | void |
193 | | NoteFinishedFileActor(indexedDB::PBackgroundIDBDatabaseFileChild* aFileActor); |
194 | | |
195 | | void |
196 | | NoteActiveTransaction(); |
197 | | |
198 | | void |
199 | | NoteInactiveTransaction(); |
200 | | |
201 | | // XXX This doesn't really belong here... It's only needed for IDBMutableFile |
202 | | // serialization and should be removed or fixed someday. |
203 | | nsresult |
204 | | GetQuotaInfo(nsACString& aOrigin, PersistenceType* aPersistenceType); |
205 | | |
206 | | bool |
207 | | IsFileHandleDisabled() const |
208 | | { |
209 | | return mFileHandleDisabled; |
210 | | } |
211 | | |
212 | | void |
213 | | NoteLiveMutableFile(IDBMutableFile* aMutableFile); |
214 | | |
215 | | void |
216 | | NoteFinishedMutableFile(IDBMutableFile* aMutableFile); |
217 | | |
218 | | nsPIDOMWindowInner* |
219 | | GetParentObject() const; |
220 | | |
221 | | already_AddRefed<DOMStringList> |
222 | | ObjectStoreNames() const; |
223 | | |
224 | | already_AddRefed<IDBObjectStore> |
225 | | CreateObjectStore(const nsAString& aName, |
226 | | const IDBObjectStoreParameters& aOptionalParameters, |
227 | | ErrorResult& aRv); |
228 | | |
229 | | void |
230 | | DeleteObjectStore(const nsAString& name, ErrorResult& aRv); |
231 | | |
232 | | // This will be called from the DOM. |
233 | | already_AddRefed<IDBTransaction> |
234 | | Transaction(JSContext* aCx, |
235 | | const StringOrStringSequence& aStoreNames, |
236 | | IDBTransactionMode aMode, |
237 | | ErrorResult& aRv); |
238 | | |
239 | | // This can be called from C++ to avoid JS exception. |
240 | | nsresult |
241 | | Transaction(JSContext* aCx, |
242 | | const StringOrStringSequence& aStoreNames, |
243 | | IDBTransactionMode aMode, |
244 | | IDBTransaction** aTransaction); |
245 | | |
246 | | StorageType |
247 | | Storage() const; |
248 | | |
249 | | IMPL_EVENT_HANDLER(abort) |
250 | | IMPL_EVENT_HANDLER(close) |
251 | | IMPL_EVENT_HANDLER(error) |
252 | | IMPL_EVENT_HANDLER(versionchange) |
253 | | |
254 | | already_AddRefed<IDBRequest> |
255 | | CreateMutableFile(JSContext* aCx, |
256 | | const nsAString& aName, |
257 | | const Optional<nsAString>& aType, |
258 | | ErrorResult& aRv); |
259 | | |
260 | | already_AddRefed<IDBRequest> |
261 | | MozCreateFileHandle(JSContext* aCx, |
262 | | const nsAString& aName, |
263 | | const Optional<nsAString>& aType, |
264 | | ErrorResult& aRv) |
265 | 0 | { |
266 | 0 | return CreateMutableFile(aCx, aName, aType, aRv); |
267 | 0 | } |
268 | | |
269 | | void |
270 | | ClearBackgroundActor() |
271 | | { |
272 | | AssertIsOnOwningThread(); |
273 | | |
274 | | // Decrease the number of active databases if it was not done in |
275 | | // CloseInternal(). |
276 | | MaybeDecreaseActiveDatabaseCount(); |
277 | | |
278 | | mBackgroundActor = nullptr; |
279 | | } |
280 | | |
281 | | const DatabaseSpec* |
282 | | Spec() const |
283 | | { |
284 | | return mSpec; |
285 | | } |
286 | | |
287 | | NS_DECL_ISUPPORTS_INHERITED |
288 | | NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBDatabase, IDBWrapperCache) |
289 | | |
290 | | // DOMEventTargetHelper |
291 | | void |
292 | | DisconnectFromOwner() override; |
293 | | |
294 | | virtual void |
295 | | LastRelease() override; |
296 | | |
297 | | virtual nsresult |
298 | | PostHandleEvent(EventChainPostVisitor& aVisitor) override; |
299 | | |
300 | | // nsWrapperCache |
301 | | virtual JSObject* |
302 | | WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; |
303 | | |
304 | | private: |
305 | | IDBDatabase(IDBOpenDBRequest* aRequest, |
306 | | IDBFactory* aFactory, |
307 | | indexedDB::BackgroundDatabaseChild* aActor, |
308 | | DatabaseSpec* aSpec); |
309 | | |
310 | | ~IDBDatabase(); |
311 | | |
312 | | void |
313 | | CloseInternal(); |
314 | | |
315 | | void |
316 | | InvalidateInternal(); |
317 | | |
318 | | bool |
319 | | RunningVersionChangeTransaction() const |
320 | | { |
321 | | AssertIsOnOwningThread(); |
322 | | |
323 | | return !!mPreviousSpec; |
324 | | } |
325 | | |
326 | | void |
327 | | RefreshSpec(bool aMayDelete); |
328 | | |
329 | | void |
330 | | ExpireFileActors(bool aExpireAll); |
331 | | |
332 | | void |
333 | | InvalidateMutableFiles(); |
334 | | |
335 | | void |
336 | | NoteInactiveTransactionDelayed(); |
337 | | |
338 | | void |
339 | | LogWarning(const char* aMessageName, |
340 | | const nsAString& aFilename, |
341 | | uint32_t aLineNumber, |
342 | | uint32_t aColumnNumber); |
343 | | |
344 | | // Only accessed by IDBObjectStore. |
345 | | nsresult |
346 | | RenameObjectStore(int64_t aObjectStoreId, const nsAString& aName); |
347 | | |
348 | | // Only accessed by IDBIndex. |
349 | | nsresult |
350 | | RenameIndex(int64_t aObjectStoreId, int64_t aIndexId, const nsAString& aName); |
351 | | |
352 | | void |
353 | | IncreaseActiveDatabaseCount(); |
354 | | |
355 | | void |
356 | | MaybeDecreaseActiveDatabaseCount(); |
357 | | }; |
358 | | |
359 | | } // namespace dom |
360 | | } // namespace mozilla |
361 | | |
362 | | #endif // mozilla_dom_idbdatabase_h__ |