Source code

Revision control

Other Tools

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