forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHandle.h
274 lines (212 loc) · 5.52 KB
/
FileHandle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_FileHandle_h
#define mozilla_dom_FileHandle_h
#include "MainThreadUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/FileModeBinding.h"
#include "mozilla/dom/Nullable.h"
#include "mozilla/dom/TypedArray.h"
#include "mozilla/ErrorResult.h"
#include "nsAutoPtr.h"
#include "nsCOMPtr.h"
#include "nsIInputStream.h"
#include "nsIRunnable.h"
#include "nsTArray.h"
class nsAString;
namespace mozilla {
namespace dom {
class File;
class FileHelper;
class FileRequestBase;
class FileService;
class FinishHelper;
class MetadataHelper;
class MutableFileBase;
/**
* This class provides a base for FileHandle implementations.
*/
class FileHandleBase
{
public:
enum RequestMode
{
NORMAL = 0, // Sequential
PARALLEL
};
enum ReadyState
{
INITIAL = 0,
LOADING,
FINISHING,
DONE
};
private:
friend class FileHelper;
friend class FileService;
friend class FinishHelper;
friend class MetadataHelper;
ReadyState mReadyState;
FileMode mMode;
RequestMode mRequestMode;
uint64_t mLocation;
uint32_t mPendingRequests;
nsTArray<nsCOMPtr<nsISupports>> mParallelStreams;
nsCOMPtr<nsISupports> mStream;
bool mAborted;
bool mCreating;
public:
NS_IMETHOD_(MozExternalRefCountType)
AddRef() = 0;
NS_IMETHOD_(MozExternalRefCountType)
Release() = 0;
nsresult
CreateParallelStream(nsISupports** aStream);
nsresult
GetOrCreateStream(nsISupports** aStream);
bool
IsOpen() const;
bool
IsAborted() const
{
return mAborted;
}
void
SetCreating()
{
mCreating = true;
}
virtual MutableFileBase*
MutableFile() const = 0;
nsresult
OpenInputStream(bool aWholeFile, uint64_t aStart, uint64_t aLength,
nsIInputStream** aResult);
// Shared WebIDL (IndexedDB FileHandle and FileSystem FileHandle)
FileMode
Mode() const
{
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
return mMode;
}
bool
Active() const
{
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
return IsOpen();
}
Nullable<uint64_t>
GetLocation() const
{
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
if (mLocation == UINT64_MAX) {
return Nullable<uint64_t>();
}
return Nullable<uint64_t>(mLocation);
}
void
SetLocation(const Nullable<uint64_t>& aLocation)
{
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
// Null means the end-of-file.
if (aLocation.IsNull()) {
mLocation = UINT64_MAX;
} else {
mLocation = aLocation.Value();
}
}
already_AddRefed<FileRequestBase>
Read(uint64_t aSize, bool aHasEncoding, const nsAString& aEncoding,
ErrorResult& aRv);
already_AddRefed<FileRequestBase>
Truncate(const Optional<uint64_t>& aSize, ErrorResult& aRv);
already_AddRefed<FileRequestBase>
Flush(ErrorResult& aRv);
void
Abort(ErrorResult& aRv);
protected:
FileHandleBase(FileMode aMode,
RequestMode aRequestMode);
~FileHandleBase();
void
OnNewRequest();
void
OnRequestFinished();
void
OnReturnToEventLoop();
virtual nsresult
OnCompleteOrAbort(bool aAborted) = 0;
bool
CheckState(ErrorResult& aRv);
bool
CheckStateAndArgumentsForRead(uint64_t aSize, ErrorResult& aRv);
bool
CheckStateForWrite(ErrorResult& aRv);
virtual bool
CheckWindow() = 0;
virtual already_AddRefed<FileRequestBase>
GenerateFileRequest() = 0;
template<class T>
already_AddRefed<FileRequestBase>
WriteOrAppend(const T& aValue, bool aAppend, ErrorResult& aRv)
{
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
// State checking for write
if (!CheckStateForWrite(aRv)) {
return nullptr;
}
// Additional state checking for write
if (!aAppend && mLocation == UINT64_MAX) {
aRv.Throw(NS_ERROR_DOM_FILEHANDLE_NOT_ALLOWED_ERR);
return nullptr;
}
uint64_t length;
nsCOMPtr<nsIInputStream> stream = GetInputStream(aValue, &length, aRv);
if (aRv.Failed()) {
return nullptr;
}
if (!length) {
return nullptr;
}
// Do nothing if the window is closed
if (!CheckWindow()) {
return nullptr;
}
return WriteInternal(stream, length, aAppend, aRv);
}
already_AddRefed<FileRequestBase>
WriteInternal(nsIInputStream* aInputStream, uint64_t aInputLength,
bool aAppend, ErrorResult& aRv);
nsresult
Finish();
static already_AddRefed<nsIInputStream>
GetInputStream(const ArrayBuffer& aValue, uint64_t* aInputLength,
ErrorResult& aRv);
static already_AddRefed<nsIInputStream>
GetInputStream(const File& aValue, uint64_t* aInputLength,
ErrorResult& aRv);
static already_AddRefed<nsIInputStream>
GetInputStream(const nsAString& aValue, uint64_t* aInputLength,
ErrorResult& aRv);
};
class FinishHelper MOZ_FINAL : public nsIRunnable
{
friend class FileHandleBase;
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIRUNNABLE
private:
explicit FinishHelper(FileHandleBase* aFileHandle);
~FinishHelper()
{ }
nsRefPtr<FileHandleBase> mFileHandle;
nsTArray<nsCOMPtr<nsISupports>> mParallelStreams;
nsCOMPtr<nsISupports> mStream;
bool mAborted;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_FileHandle_h