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 file,
5
* You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_dom_idbfilehandle_h__
8
#define mozilla_dom_idbfilehandle_h__
9
10
#include "IDBFileRequest.h"
11
#include "js/TypeDecls.h"
12
#include "mozilla/Attributes.h"
13
#include "mozilla/dom/FileModeBinding.h"
14
#include "mozilla/DOMEventTargetHelper.h"
15
#include "nsCycleCollectionParticipant.h"
16
#include "nsIRunnable.h"
17
#include "nsWeakReference.h"
18
19
namespace mozilla {
20
namespace dom {
21
22
class Blob;
23
class FileRequestData;
24
class FileRequestParams;
25
struct IDBFileMetadataParameters;
26
class IDBFileRequest;
27
class IDBMutableFile;
28
class StringOrArrayBufferOrArrayBufferViewOrBlob;
29
30
namespace indexedDB {
31
class BackgroundFileHandleChild;
32
}
33
34
class IDBFileHandle final : public DOMEventTargetHelper,
35
public nsIRunnable,
36
public nsSupportsWeakReference {
37
public:
38
enum ReadyState { INITIAL = 0, LOADING, FINISHING, DONE };
39
40
private:
41
RefPtr<IDBMutableFile> mMutableFile;
42
43
indexedDB::BackgroundFileHandleChild* mBackgroundActor;
44
45
uint64_t mLocation;
46
47
uint32_t mPendingRequestCount;
48
49
ReadyState mReadyState;
50
FileMode mMode;
51
52
bool mAborted;
53
bool mCreating;
54
55
#ifdef DEBUG
56
bool mSentFinishOrAbort;
57
bool mFiredCompleteOrAbort;
58
#endif
59
60
public:
61
static already_AddRefed<IDBFileHandle> Create(IDBMutableFile* aMutableFile,
62
FileMode aMode);
63
64
static IDBFileHandle* GetCurrent();
65
66
void AssertIsOnOwningThread() const
67
#ifdef DEBUG
68
;
69
#else
70
{
71
}
72
#endif
73
74
void SetBackgroundActor(indexedDB::BackgroundFileHandleChild* aActor);
75
76
void ClearBackgroundActor() {
77
AssertIsOnOwningThread();
78
79
mBackgroundActor = nullptr;
80
}
81
82
void StartRequest(IDBFileRequest* aFileRequest,
83
const FileRequestParams& aParams);
84
85
void OnNewRequest();
86
87
void OnRequestFinished(bool aActorDestroyedNormally);
88
89
void FireCompleteOrAbortEvents(bool aAborted);
90
91
bool IsOpen() const;
92
93
bool IsFinishingOrDone() const {
94
AssertIsOnOwningThread();
95
96
return mReadyState == FINISHING || mReadyState == DONE;
97
}
98
99
bool IsDone() const {
100
AssertIsOnOwningThread();
101
102
return mReadyState == DONE;
103
}
104
105
bool IsAborted() const {
106
AssertIsOnOwningThread();
107
return mAborted;
108
}
109
110
void Abort();
111
112
// WebIDL
113
IDBMutableFile* GetMutableFile() const {
114
AssertIsOnOwningThread();
115
return mMutableFile;
116
}
117
118
IDBMutableFile* GetFileHandle() const {
119
AssertIsOnOwningThread();
120
return GetMutableFile();
121
}
122
123
FileMode Mode() const {
124
AssertIsOnOwningThread();
125
return mMode;
126
}
127
128
bool Active() const {
129
AssertIsOnOwningThread();
130
return IsOpen();
131
}
132
133
Nullable<uint64_t> GetLocation() const {
134
AssertIsOnOwningThread();
135
136
if (mLocation == UINT64_MAX) {
137
return Nullable<uint64_t>();
138
}
139
140
return Nullable<uint64_t>(mLocation);
141
}
142
143
void SetLocation(const Nullable<uint64_t>& aLocation) {
144
AssertIsOnOwningThread();
145
146
// Null means the end-of-file.
147
if (aLocation.IsNull()) {
148
mLocation = UINT64_MAX;
149
} else {
150
mLocation = aLocation.Value();
151
}
152
}
153
154
already_AddRefed<IDBFileRequest> GetMetadata(
155
const IDBFileMetadataParameters& aParameters, ErrorResult& aRv);
156
157
already_AddRefed<IDBFileRequest> ReadAsArrayBuffer(uint64_t aSize,
158
ErrorResult& aRv) {
159
AssertIsOnOwningThread();
160
return Read(aSize, false, VoidString(), aRv);
161
}
162
163
already_AddRefed<IDBFileRequest> ReadAsText(uint64_t aSize,
164
const nsAString& aEncoding,
165
ErrorResult& aRv) {
166
AssertIsOnOwningThread();
167
return Read(aSize, true, aEncoding, aRv);
168
}
169
170
already_AddRefed<IDBFileRequest> Write(
171
const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
172
ErrorResult& aRv) {
173
AssertIsOnOwningThread();
174
return WriteOrAppend(aValue, false, aRv);
175
}
176
177
already_AddRefed<IDBFileRequest> Append(
178
const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue,
179
ErrorResult& aRv) {
180
AssertIsOnOwningThread();
181
return WriteOrAppend(aValue, true, aRv);
182
}
183
184
already_AddRefed<IDBFileRequest> Truncate(const Optional<uint64_t>& aSize,
185
ErrorResult& aRv);
186
187
already_AddRefed<IDBFileRequest> Flush(ErrorResult& aRv);
188
189
void Abort(ErrorResult& aRv);
190
191
IMPL_EVENT_HANDLER(complete)
192
IMPL_EVENT_HANDLER(abort)
193
IMPL_EVENT_HANDLER(error)
194
195
NS_DECL_ISUPPORTS_INHERITED
196
NS_DECL_NSIRUNNABLE
197
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBFileHandle, DOMEventTargetHelper)
198
199
// EventTarget
200
void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
201
202
// WrapperCache
203
virtual JSObject* WrapObject(JSContext* aCx,
204
JS::Handle<JSObject*> aGivenProto) override;
205
206
private:
207
IDBFileHandle(IDBMutableFile* aMutableFile, FileMode aMode);
208
~IDBFileHandle();
209
210
bool CheckState(ErrorResult& aRv);
211
212
bool CheckStateAndArgumentsForRead(uint64_t aSize, ErrorResult& aRv);
213
214
bool CheckStateForWrite(ErrorResult& aRv);
215
216
bool CheckStateForWriteOrAppend(bool aAppend, ErrorResult& aRv);
217
218
bool CheckWindow();
219
220
already_AddRefed<IDBFileRequest> Read(uint64_t aSize, bool aHasEncoding,
221
const nsAString& aEncoding,
222
ErrorResult& aRv);
223
224
already_AddRefed<IDBFileRequest> WriteOrAppend(
225
const StringOrArrayBufferOrArrayBufferViewOrBlob& aValue, bool aAppend,
226
ErrorResult& aRv);
227
228
already_AddRefed<IDBFileRequest> WriteOrAppend(const nsAString& aValue,
229
bool aAppend,
230
ErrorResult& aRv);
231
232
already_AddRefed<IDBFileRequest> WriteOrAppend(const ArrayBuffer& aValue,
233
bool aAppend,
234
ErrorResult& aRv);
235
236
already_AddRefed<IDBFileRequest> WriteOrAppend(const ArrayBufferView& aValue,
237
bool aAppend,
238
ErrorResult& aRv);
239
240
already_AddRefed<IDBFileRequest> WriteOrAppend(Blob& aValue, bool aAppend,
241
ErrorResult& aRv);
242
243
already_AddRefed<IDBFileRequest> WriteInternal(const FileRequestData& aData,
244
uint64_t aDataLength,
245
bool aAppend,
246
ErrorResult& aRv);
247
248
void SendFinish();
249
250
void SendAbort();
251
};
252
253
} // namespace dom
254
} // namespace mozilla
255
256
#endif // mozilla_dom_idbfilehandle_h__