Bumping manifests a=b2g-bump
[gecko.git] / dom / indexedDB / IDBDatabase.h
blob015804a4681c529aab6506abf593a599716fba22
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/. */
7 #ifndef mozilla_dom_indexeddb_idbdatabase_h__
8 #define mozilla_dom_indexeddb_idbdatabase_h__
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/IDBTransactionBinding.h"
12 #include "mozilla/dom/StorageTypeBinding.h"
13 #include "mozilla/dom/indexedDB/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"
21 class nsIDocument;
22 class nsIWeakReference;
23 class nsPIDOMWindow;
25 namespace mozilla {
27 class ErrorResult;
28 class EventChainPostVisitor;
30 namespace dom {
32 class File;
33 class DOMStringList;
34 struct IDBObjectStoreParameters;
35 template <typename> class Sequence;
37 namespace indexedDB {
39 class BackgroundDatabaseChild;
40 class DatabaseSpec;
41 class FileManager;
42 class IDBFactory;
43 class IDBMutableFile;
44 class IDBObjectStore;
45 class IDBRequest;
46 class IDBTransaction;
47 class PBackgroundIDBDatabaseFileChild;
49 class IDBDatabase MOZ_FINAL
50 : public IDBWrapperCache
52 typedef mozilla::dom::StorageType StorageType;
53 typedef mozilla::dom::quota::PersistenceType PersistenceType;
55 class LogWarningRunnable;
56 friend class LogWarningRunnable;
58 class Observer;
59 friend class Observer;
61 // The factory must be kept alive when IndexedDB is used in multiple
62 // processes. If it dies then the entire actor tree will be destroyed with it
63 // and the world will explode.
64 nsRefPtr<IDBFactory> mFactory;
66 nsAutoPtr<DatabaseSpec> mSpec;
68 // Normally null except during a versionchange transaction.
69 nsAutoPtr<DatabaseSpec> mPreviousSpec;
71 nsRefPtr<FileManager> mFileManager;
73 BackgroundDatabaseChild* mBackgroundActor;
75 nsTHashtable<nsPtrHashKey<IDBTransaction>> mTransactions;
77 nsDataHashtable<nsISupportsHashKey, PBackgroundIDBDatabaseFileChild*>
78 mFileActors;
80 nsTHashtable<nsISupportsHashKey> mReceivedBlobs;
82 nsRefPtr<Observer> mObserver;
84 // Weak refs, IDBMutableFile strongly owns this IDBDatabase object.
85 nsTArray<IDBMutableFile*> mLiveMutableFiles;
87 bool mClosed;
88 bool mInvalidated;
90 public:
91 static already_AddRefed<IDBDatabase>
92 Create(IDBWrapperCache* aOwnerCache,
93 IDBFactory* aFactory,
94 BackgroundDatabaseChild* aActor,
95 DatabaseSpec* aSpec);
97 void
98 AssertIsOnOwningThread() const
99 #ifdef DEBUG
101 #else
103 #endif
105 const nsString&
106 Name() const;
108 void
109 GetName(nsAString& aName) const
111 AssertIsOnOwningThread();
113 aName = Name();
116 uint64_t
117 Version() const;
119 already_AddRefed<nsIDocument>
120 GetOwnerDocument() const;
122 void
123 Close()
125 AssertIsOnOwningThread();
127 CloseInternal();
130 bool
131 IsClosed() const
133 AssertIsOnOwningThread();
135 return mClosed;
138 void
139 Invalidate();
141 // Whether or not the database has been invalidated. If it has then no further
142 // transactions for this database will be allowed to run.
143 bool
144 IsInvalidated() const
146 AssertIsOnOwningThread();
148 return mInvalidated;
151 void
152 EnterSetVersionTransaction(uint64_t aNewVersion);
154 void
155 ExitSetVersionTransaction();
157 // Called when a versionchange transaction is aborted to reset the
158 // DatabaseInfo.
159 void
160 RevertToPreviousState();
162 IDBFactory*
163 Factory() const
165 AssertIsOnOwningThread();
167 return mFactory;
170 void
171 RegisterTransaction(IDBTransaction* aTransaction);
173 void
174 UnregisterTransaction(IDBTransaction* aTransaction);
176 void
177 AbortTransactions(bool aShouldWarn);
179 PBackgroundIDBDatabaseFileChild*
180 GetOrCreateFileActorForBlob(File* aBlob);
182 void
183 NoteFinishedFileActor(PBackgroundIDBDatabaseFileChild* aFileActor);
185 void
186 NoteReceivedBlob(File* aBlob);
188 void
189 DelayedMaybeExpireFileActors();
191 // XXX This doesn't really belong here... It's only needed for IDBMutableFile
192 // serialization and should be removed someday.
193 nsresult
194 GetQuotaInfo(nsACString& aOrigin, PersistenceType* aPersistenceType);
196 void
197 NoteLiveMutableFile(IDBMutableFile* aMutableFile);
199 void
200 NoteFinishedMutableFile(IDBMutableFile* aMutableFile);
202 nsPIDOMWindow*
203 GetParentObject() const;
205 already_AddRefed<DOMStringList>
206 ObjectStoreNames() const;
208 already_AddRefed<IDBObjectStore>
209 CreateObjectStore(const nsAString& aName,
210 const IDBObjectStoreParameters& aOptionalParameters,
211 ErrorResult& aRv);
213 void
214 DeleteObjectStore(const nsAString& name, ErrorResult& aRv);
216 already_AddRefed<IDBTransaction>
217 Transaction(const nsAString& aStoreName,
218 IDBTransactionMode aMode,
219 ErrorResult& aRv);
221 already_AddRefed<IDBTransaction>
222 Transaction(const Sequence<nsString>& aStoreNames,
223 IDBTransactionMode aMode,
224 ErrorResult& aRv);
226 StorageType
227 Storage() const;
229 IMPL_EVENT_HANDLER(abort)
230 IMPL_EVENT_HANDLER(error)
231 IMPL_EVENT_HANDLER(versionchange)
233 already_AddRefed<IDBRequest>
234 CreateMutableFile(const nsAString& aName,
235 const Optional<nsAString>& aType,
236 ErrorResult& aRv);
238 already_AddRefed<IDBRequest>
239 MozCreateFileHandle(const nsAString& aName,
240 const Optional<nsAString>& aType,
241 ErrorResult& aRv)
243 return CreateMutableFile(aName, aType, aRv);
246 void
247 ClearBackgroundActor()
249 AssertIsOnOwningThread();
251 mBackgroundActor = nullptr;
254 const DatabaseSpec*
255 Spec() const
257 return mSpec;
260 NS_DECL_ISUPPORTS_INHERITED
261 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBDatabase, IDBWrapperCache)
263 // nsIDOMEventTarget
264 virtual void
265 LastRelease() MOZ_OVERRIDE;
267 virtual nsresult
268 PostHandleEvent(EventChainPostVisitor& aVisitor) MOZ_OVERRIDE;
270 // nsWrapperCache
271 virtual JSObject*
272 WrapObject(JSContext* aCx) MOZ_OVERRIDE;
274 private:
275 IDBDatabase(IDBWrapperCache* aOwnerCache,
276 IDBFactory* aFactory,
277 BackgroundDatabaseChild* aActor,
278 DatabaseSpec* aSpec);
280 ~IDBDatabase();
282 void
283 CloseInternal();
285 void
286 InvalidateInternal();
288 bool
289 RunningVersionChangeTransaction() const
291 AssertIsOnOwningThread();
293 return !!mPreviousSpec;
296 void
297 RefreshSpec(bool aMayDelete);
299 void
300 ExpireFileActors(bool aExpireAll);
302 void
303 InvalidateMutableFiles();
305 void
306 LogWarning(const char* aMessageName,
307 const nsAString& aFilename,
308 uint32_t aLineNumber);
311 } // namespace indexedDB
312 } // namespace dom
313 } // namespace mozilla
315 #endif // mozilla_dom_indexeddb_idbdatabase_h__