This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathFile.cpp
More file actions
441 lines (363 loc) · 10.5 KB
/
Copy pathFile.cpp
File metadata and controls
441 lines (363 loc) · 10.5 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* 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/. */
#include "File.h"
#include "nsIDOMFile.h"
#include "nsDOMBlobBuilder.h"
#include "nsError.h"
#include "jsapi.h"
#include "jsfriendapi.h"
#include "nsCOMPtr.h"
#include "nsJSUtils.h"
#include "nsStringGlue.h"
#include "Exceptions.h"
#include "WorkerInlines.h"
#include "WorkerPrivate.h"
#define PROPERTY_FLAGS \
(JSPROP_ENUMERATE | JSPROP_SHARED)
USING_WORKERS_NAMESPACE
using mozilla::dom::workers::exceptions::ThrowDOMExceptionForNSResult;
namespace {
class Blob
{
// Blob should never be instantiated.
Blob();
~Blob();
static JSClass sClass;
static const JSPropertySpec sProperties[];
static const JSFunctionSpec sFunctions[];
public:
static JSObject*
InitClass(JSContext* aCx, JSObject* aObj)
{
return JS_InitClass(aCx, aObj, NULL, &sClass, Construct, 0, sProperties,
sFunctions, NULL, NULL);
}
static JSObject*
Create(JSContext* aCx, nsIDOMBlob* aBlob)
{
JS_ASSERT(SameCOMIdentity(static_cast<nsISupports*>(aBlob), aBlob));
JSObject* obj = JS_NewObject(aCx, &sClass, NULL, NULL);
if (obj) {
JS_SetPrivate(obj, aBlob);
NS_ADDREF(aBlob);
}
return obj;
}
static nsIDOMBlob*
GetPrivate(JSObject* aObj);
private:
static nsIDOMBlob*
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName)
{
nsIDOMBlob* blob = GetPrivate(aObj);
if (blob) {
return blob;
}
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
JS_GetClass(aObj)->name);
return NULL;
}
static nsIDOMBlob*
Unwrap(JSContext* aCx, JSObject* aObj)
{
return GetPrivate(aObj);
}
static JSBool
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
{
nsRefPtr<nsDOMMultipartFile> file = new nsDOMMultipartFile();
nsresult rv = file->InitBlob(aCx, aArgc, JS_ARGV(aCx, aVp), Unwrap);
if (NS_FAILED(rv)) {
ThrowDOMExceptionForNSResult(aCx, rv);
return false;
}
JSObject* obj = file::CreateBlob(aCx, file);
if (!obj) {
return false;
}
JS_SET_RVAL(aCx, aVp, OBJECT_TO_JSVAL(obj));
return true;
}
static void
Finalize(JSFreeOp* aFop, JSObject* aObj)
{
JS_ASSERT(JS_GetClass(aObj) == &sClass);
nsIDOMBlob* blob = GetPrivate(aObj);
NS_IF_RELEASE(blob);
}
static JSBool
GetSize(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, JSMutableHandleValue aVp)
{
nsIDOMBlob* blob = GetInstancePrivate(aCx, aObj, "size");
if (!blob) {
return false;
}
uint64_t size;
if (NS_FAILED(blob->GetSize(&size))) {
ThrowDOMExceptionForNSResult(aCx, NS_ERROR_DOM_FILE_NOT_READABLE_ERR);
return false;
}
aVp.set(JS_NumberValue(double(size)));
return true;
}
static JSBool
GetType(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, JSMutableHandleValue aVp)
{
nsIDOMBlob* blob = GetInstancePrivate(aCx, aObj, "type");
if (!blob) {
return false;
}
nsString type;
if (NS_FAILED(blob->GetType(type))) {
ThrowDOMExceptionForNSResult(aCx, NS_ERROR_DOM_FILE_NOT_READABLE_ERR);
return false;
}
JSString* jsType = JS_NewUCStringCopyN(aCx, type.get(), type.Length());
if (!jsType) {
return false;
}
aVp.set(STRING_TO_JSVAL(jsType));
return true;
}
static JSBool
Slice(JSContext* aCx, unsigned aArgc, jsval* aVp)
{
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
if (!obj) {
return false;
}
nsIDOMBlob* blob = GetInstancePrivate(aCx, obj, "slice");
if (!blob) {
return false;
}
double start = 0, end = 0;
JSString* jsContentType = JS_GetEmptyString(JS_GetRuntime(aCx));
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "/IIS", &start,
&end, &jsContentType)) {
return false;
}
nsDependentJSString contentType;
if (!contentType.init(aCx, jsContentType)) {
return false;
}
uint8_t optionalArgc = aArgc;
nsCOMPtr<nsIDOMBlob> rtnBlob;
if (NS_FAILED(blob->Slice(static_cast<uint64_t>(start),
static_cast<uint64_t>(end),
contentType, optionalArgc,
getter_AddRefs(rtnBlob)))) {
ThrowDOMExceptionForNSResult(aCx, NS_ERROR_DOM_FILE_NOT_READABLE_ERR);
return false;
}
JSObject* rtnObj = file::CreateBlob(aCx, rtnBlob);
if (!rtnObj) {
return false;
}
JS_SET_RVAL(aCx, aVp, OBJECT_TO_JSVAL(rtnObj));
return true;
}
};
JSClass Blob::sClass = {
"Blob",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize
};
const JSPropertySpec Blob::sProperties[] = {
{ "size", 0, PROPERTY_FLAGS, JSOP_WRAPPER(GetSize), JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
{ "type", 0, PROPERTY_FLAGS, JSOP_WRAPPER(GetType), JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
{ 0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER }
};
const JSFunctionSpec Blob::sFunctions[] = {
JS_FN("slice", Slice, 1, JSPROP_ENUMERATE),
JS_FS_END
};
class File : public Blob
{
// File should never be instantiated.
File();
~File();
static JSClass sClass;
static const JSPropertySpec sProperties[];
public:
static JSObject*
InitClass(JSContext* aCx, JSObject* aObj, JSObject* aParentProto)
{
return JS_InitClass(aCx, aObj, aParentProto, &sClass, Construct, 0,
sProperties, NULL, NULL, NULL);
}
static JSObject*
Create(JSContext* aCx, nsIDOMFile* aFile)
{
JS_ASSERT(SameCOMIdentity(static_cast<nsISupports*>(aFile), aFile));
JSObject* obj = JS_NewObject(aCx, &sClass, NULL, NULL);
if (obj) {
JS_SetPrivate(obj, aFile);
NS_ADDREF(aFile);
}
return obj;
}
static nsIDOMFile*
GetPrivate(JSObject* aObj)
{
if (aObj) {
JSClass* classPtr = JS_GetClass(aObj);
if (classPtr == &sClass) {
nsISupports* priv = static_cast<nsISupports*>(JS_GetPrivate(aObj));
nsCOMPtr<nsIDOMFile> file = do_QueryInterface(priv);
JS_ASSERT_IF(priv, file);
return file;
}
}
return NULL;
}
static JSClass*
Class()
{
return &sClass;
}
private:
static nsIDOMFile*
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName)
{
nsIDOMFile* file = GetPrivate(aObj);
if (file) {
return file;
}
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
JS_GetClass(aObj)->name);
return NULL;
}
static JSBool
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
{
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
sClass.name);
return false;
}
static void
Finalize(JSFreeOp* aFop, JSObject* aObj)
{
JS_ASSERT(JS_GetClass(aObj) == &sClass);
nsIDOMFile* file = GetPrivate(aObj);
NS_IF_RELEASE(file);
}
static JSBool
GetMozFullPath(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, JSMutableHandleValue aVp)
{
nsIDOMFile* file = GetInstancePrivate(aCx, aObj, "mozFullPath");
if (!file) {
return false;
}
nsString fullPath;
if (GetWorkerPrivateFromContext(aCx)->UsesSystemPrincipal() &&
NS_FAILED(file->GetMozFullPathInternal(fullPath))) {
ThrowDOMExceptionForNSResult(aCx, NS_ERROR_DOM_FILE_NOT_READABLE_ERR);
return false;
}
JSString* jsFullPath = JS_NewUCStringCopyN(aCx, fullPath.get(),
fullPath.Length());
if (!jsFullPath) {
return false;
}
aVp.set(STRING_TO_JSVAL(jsFullPath));
return true;
}
static JSBool
GetName(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, JSMutableHandleValue aVp)
{
nsIDOMFile* file = GetInstancePrivate(aCx, aObj, "name");
if (!file) {
return false;
}
nsString name;
if (NS_FAILED(file->GetName(name))) {
name.Truncate();
}
JSString* jsName = JS_NewUCStringCopyN(aCx, name.get(), name.Length());
if (!jsName) {
return false;
}
aVp.set(STRING_TO_JSVAL(jsName));
return true;
}
static JSBool
GetLastModifiedDate(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, JSMutableHandleValue aVp)
{
nsIDOMFile* file = GetInstancePrivate(aCx, aObj, "lastModifiedDate");
if (!file) {
return false;
}
JS::Value value;
if (NS_FAILED(file->GetLastModifiedDate(aCx, &value))) {
return false;
}
aVp.set(value);
return true;
}
};
JSClass File::sClass = {
"File",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize
};
const JSPropertySpec File::sProperties[] = {
{ "name", 0, PROPERTY_FLAGS, JSOP_WRAPPER(GetName),
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
{ "lastModifiedDate", 0, PROPERTY_FLAGS, JSOP_WRAPPER(GetLastModifiedDate),
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
{ "mozFullPath", 0, PROPERTY_FLAGS, JSOP_WRAPPER(GetMozFullPath),
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
{ 0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER }
};
nsIDOMBlob*
Blob::GetPrivate(JSObject* aObj)
{
if (aObj) {
JSClass* classPtr = JS_GetClass(aObj);
if (classPtr == &sClass || classPtr == File::Class()) {
nsISupports* priv = static_cast<nsISupports*>(JS_GetPrivate(aObj));
nsCOMPtr<nsIDOMBlob> blob = do_QueryInterface(priv);
JS_ASSERT_IF(priv, blob);
return blob;
}
}
return NULL;
}
} // anonymous namespace
BEGIN_WORKERS_NAMESPACE
namespace file {
JSObject*
CreateBlob(JSContext* aCx, nsIDOMBlob* aBlob)
{
return Blob::Create(aCx, aBlob);
}
bool
InitClasses(JSContext* aCx, JSObject* aGlobal)
{
JSObject* blobProto = Blob::InitClass(aCx, aGlobal);
return blobProto && File::InitClass(aCx, aGlobal, blobProto);
}
nsIDOMBlob*
GetDOMBlobFromJSObject(JSObject* aObj)
{
return Blob::GetPrivate(aObj);
}
JSObject*
CreateFile(JSContext* aCx, nsIDOMFile* aFile)
{
return File::Create(aCx, aFile);
}
nsIDOMFile*
GetDOMFileFromJSObject(JSObject* aObj)
{
return File::GetPrivate(aObj);
}
} // namespace file
END_WORKERS_NAMESPACE