Line data Source code
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 : { }
104 : #endif
105 :
106 : nsIEventTarget*
107 : EventTarget() const;
108 :
109 : const nsString&
110 : Name() const;
111 :
112 : void
113 0 : GetName(nsAString& aName) const
114 : {
115 0 : AssertIsOnOwningThread();
116 :
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 0 : Close()
128 : {
129 0 : AssertIsOnOwningThread();
130 :
131 0 : CloseInternal();
132 0 : }
133 :
134 : bool
135 0 : IsClosed() const
136 : {
137 0 : AssertIsOnOwningThread();
138 :
139 0 : 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 0 : IsInvalidated() const
149 : {
150 0 : AssertIsOnOwningThread();
151 :
152 0 : return mInvalidated;
153 : }
154 :
155 : void
156 0 : SetQuotaExceeded()
157 : {
158 0 : mQuotaExceeded = true;
159 0 : }
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 0 : Factory() const
174 : {
175 0 : AssertIsOnOwningThread();
176 :
177 0 : 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 0 : IsFileHandleDisabled() const
208 : {
209 0 : 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 0 : IMPL_EVENT_HANDLER(abort)
250 0 : IMPL_EVENT_HANDLER(close)
251 0 : IMPL_EVENT_HANDLER(error)
252 0 : 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 0 : MozCreateFileHandle(JSContext* aCx,
262 : const nsAString& aName,
263 : const Optional<nsAString>& aType,
264 : ErrorResult& aRv)
265 : {
266 0 : return CreateMutableFile(aCx, aName, aType, aRv);
267 : }
268 :
269 : void
270 0 : ClearBackgroundActor()
271 : {
272 0 : AssertIsOnOwningThread();
273 :
274 : // Decrease the number of active databases if it was not done in
275 : // CloseInternal().
276 0 : MaybeDecreaseActiveDatabaseCount();
277 :
278 0 : mBackgroundActor = nullptr;
279 0 : }
280 :
281 : const DatabaseSpec*
282 0 : Spec() const
283 : {
284 0 : return mSpec;
285 : }
286 :
287 : NS_DECL_ISUPPORTS_INHERITED
288 0 : NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBDatabase, IDBWrapperCache)
289 :
290 : // nsIDOMEventTarget
291 : virtual void
292 : LastRelease() override;
293 :
294 : virtual nsresult
295 : PostHandleEvent(EventChainPostVisitor& aVisitor) override;
296 :
297 : // nsWrapperCache
298 : virtual JSObject*
299 : WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
300 :
301 : private:
302 : IDBDatabase(IDBOpenDBRequest* aRequest,
303 : IDBFactory* aFactory,
304 : indexedDB::BackgroundDatabaseChild* aActor,
305 : DatabaseSpec* aSpec);
306 :
307 : ~IDBDatabase();
308 :
309 : void
310 : CloseInternal();
311 :
312 : void
313 : InvalidateInternal();
314 :
315 : bool
316 0 : RunningVersionChangeTransaction() const
317 : {
318 0 : AssertIsOnOwningThread();
319 :
320 0 : return !!mPreviousSpec;
321 : }
322 :
323 : void
324 : RefreshSpec(bool aMayDelete);
325 :
326 : void
327 : ExpireFileActors(bool aExpireAll);
328 :
329 : void
330 : InvalidateMutableFiles();
331 :
332 : void
333 : NoteInactiveTransactionDelayed();
334 :
335 : void
336 : LogWarning(const char* aMessageName,
337 : const nsAString& aFilename,
338 : uint32_t aLineNumber,
339 : uint32_t aColumnNumber);
340 :
341 : // Only accessed by IDBObjectStore.
342 : nsresult
343 : RenameObjectStore(int64_t aObjectStoreId, const nsAString& aName);
344 :
345 : // Only accessed by IDBIndex.
346 : nsresult
347 : RenameIndex(int64_t aObjectStoreId, int64_t aIndexId, const nsAString& aName);
348 :
349 : void
350 : IncreaseActiveDatabaseCount();
351 :
352 : void
353 : MaybeDecreaseActiveDatabaseCount();
354 : };
355 :
356 : } // namespace dom
357 : } // namespace mozilla
358 :
359 : #endif // mozilla_dom_idbdatabase_h__
|