forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileReaderSync.cpp
397 lines (340 loc) · 10.1 KB
/
FileReaderSync.cpp
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
/* -*- Mode: C++; tab-width: 2; 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/. */
#include "FileReaderSync.h"
#include "nsCExternalHandlerService.h"
#include "nsComponentManagerUtils.h"
#include "nsCOMPtr.h"
#include "nsDOMClassInfoID.h"
#include "nsError.h"
#include "nsIDOMFile.h"
#include "nsCharsetAlias.h"
#include "nsICharsetDetector.h"
#include "nsIConverterInputStream.h"
#include "nsIInputStream.h"
#include "nsIPlatformCharset.h"
#include "nsISeekableStream.h"
#include "nsISupportsImpl.h"
#include "nsISupportsImpl.h"
#include "nsNetUtil.h"
#include "nsServiceManagerUtils.h"
#include "File.h"
#include "RuntimeService.h"
#include "DOMBindingInlines.h"
#include "mozilla/Base64.h"
USING_WORKERS_NAMESPACE
using mozilla::ErrorResult;
using mozilla::dom::Optional;
NS_IMPL_ADDREF_INHERITED(FileReaderSync, DOMBindingBase)
NS_IMPL_RELEASE_INHERITED(FileReaderSync, DOMBindingBase)
NS_INTERFACE_MAP_BEGIN(FileReaderSync)
NS_INTERFACE_MAP_ENTRY(nsICharsetDetectionObserver)
NS_INTERFACE_MAP_END_INHERITING(DOMBindingBase)
FileReaderSync::FileReaderSync(JSContext* aCx)
: DOMBindingBase(aCx)
{
}
void
FileReaderSync::_trace(JSTracer* aTrc)
{
DOMBindingBase::_trace(aTrc);
}
void
FileReaderSync::_finalize(JSFreeOp* aFop)
{
DOMBindingBase::_finalize(aFop);
}
// static
FileReaderSync*
FileReaderSync::Constructor(JSContext* aCx, JSObject* aGlobal,
ErrorResult& aRv)
{
nsRefPtr<FileReaderSync> frs = new FileReaderSync(aCx);
if (!Wrap(aCx, aGlobal, frs)) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
return frs;
}
JSObject*
FileReaderSync::ReadAsArrayBuffer(JSContext* aCx, JSObject* aBlob,
ErrorResult& aRv)
{
nsIDOMBlob* blob = file::GetDOMBlobFromJSObject(aBlob);
if (!blob) {
aRv.Throw(NS_ERROR_INVALID_ARG);
return nullptr;
}
uint64_t blobSize;
nsresult rv = blob->GetSize(&blobSize);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
JSObject* jsArrayBuffer = JS_NewArrayBuffer(aCx, blobSize);
if (!jsArrayBuffer) {
// XXXkhuey we need a way to indicate to the bindings that the call failed
// but there's already a pending exception that we should not clobber.
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}
uint32_t bufferLength = JS_GetArrayBufferByteLength(jsArrayBuffer, aCx);
uint8_t* arrayBuffer = JS_GetArrayBufferData(jsArrayBuffer, aCx);
nsCOMPtr<nsIInputStream> stream;
rv = blob->GetInternalStream(getter_AddRefs(stream));
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
uint32_t numRead;
rv = stream->Read((char*)arrayBuffer, bufferLength, &numRead);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
NS_ASSERTION(numRead == bufferLength, "failed to read data");
return jsArrayBuffer;
}
void
FileReaderSync::ReadAsBinaryString(JSObject* aBlob, nsAString& aResult,
ErrorResult& aRv)
{
nsIDOMBlob* blob = file::GetDOMBlobFromJSObject(aBlob);
if (!blob) {
aRv.Throw(NS_ERROR_INVALID_ARG);
return;
}
nsCOMPtr<nsIInputStream> stream;
nsresult rv = blob->GetInternalStream(getter_AddRefs(stream));
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
uint32_t numRead;
do {
char readBuf[4096];
rv = stream->Read(readBuf, sizeof(readBuf), &numRead);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
uint32_t oldLength = aResult.Length();
AppendASCIItoUTF16(Substring(readBuf, readBuf + numRead), aResult);
if (aResult.Length() - oldLength != numRead) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
} while (numRead > 0);
}
void
FileReaderSync::ReadAsText(JSObject* aBlob,
const Optional<nsAString>& aEncoding,
nsAString& aResult,
ErrorResult& aRv)
{
nsIDOMBlob* blob = file::GetDOMBlobFromJSObject(aBlob);
if (!blob) {
aRv.Throw(NS_ERROR_INVALID_ARG);
return;
}
nsCOMPtr<nsIInputStream> stream;
nsresult rv = blob->GetInternalStream(getter_AddRefs(stream));
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
nsCString charsetGuess;
if (!aEncoding.WasPassed() || aEncoding.Value().IsEmpty()) {
rv = GuessCharset(stream, charsetGuess);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(stream);
if (!seekable) {
aRv.Throw(NS_ERROR_FAILURE);
return;
}
// Seek to 0 because guessing the charset advances the stream.
rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, 0);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
} else {
CopyUTF16toUTF8(aEncoding.Value(), charsetGuess);
}
nsCString charset;
rv = nsCharsetAlias::GetPreferred(charsetGuess, charset);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
rv = ConvertStream(stream, charset.get(), aResult);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
}
void
FileReaderSync::ReadAsDataURL(JSObject* aBlob, nsAString& aResult,
ErrorResult& aRv)
{
nsIDOMBlob* blob = file::GetDOMBlobFromJSObject(aBlob);
if (!blob) {
aRv.Throw(NS_ERROR_INVALID_ARG);
return;
}
nsAutoString scratchResult;
scratchResult.AssignLiteral("data:");
nsString contentType;
blob->GetType(contentType);
if (contentType.IsEmpty()) {
scratchResult.AppendLiteral("application/octet-stream");
} else {
scratchResult.Append(contentType);
}
scratchResult.AppendLiteral(";base64,");
nsCOMPtr<nsIInputStream> stream;
nsresult rv = blob->GetInternalStream(getter_AddRefs(stream));
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
uint64_t size;
rv = blob->GetSize(&size);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
nsCOMPtr<nsIInputStream> bufferedStream;
rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), stream, size);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
nsAutoString encodedData;
rv = Base64EncodeInputStream(bufferedStream, encodedData, size);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return;
}
scratchResult.Append(encodedData);
aResult = scratchResult;
}
nsresult
FileReaderSync::ConvertStream(nsIInputStream *aStream,
const char *aCharset,
nsAString &aResult)
{
nsCOMPtr<nsIConverterInputStream> converterStream =
do_CreateInstance("@mozilla.org/intl/converter-input-stream;1");
NS_ENSURE_TRUE(converterStream, NS_ERROR_FAILURE);
nsresult rv = converterStream->Init(aStream, aCharset, 8192,
nsIConverterInputStream::DEFAULT_REPLACEMENT_CHARACTER);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIUnicharInputStream> unicharStream =
do_QueryInterface(converterStream);
NS_ENSURE_TRUE(unicharStream, NS_ERROR_FAILURE);
uint32_t numChars;
nsString result;
while (NS_SUCCEEDED(unicharStream->ReadString(8192, result, &numChars)) &&
numChars > 0) {
uint32_t oldLength = aResult.Length();
aResult.Append(result);
if (aResult.Length() - oldLength != result.Length()) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
return rv;
}
nsresult
FileReaderSync::GuessCharset(nsIInputStream *aStream, nsACString &aCharset)
{
// First try the universal charset detector
nsCOMPtr<nsICharsetDetector> detector
= do_CreateInstance(NS_CHARSET_DETECTOR_CONTRACTID_BASE
"universal_charset_detector");
if (!detector) {
RuntimeService* runtime = RuntimeService::GetService();
NS_ASSERTION(runtime, "This should never be null!");
// No universal charset detector, try the default charset detector
const nsACString& detectorName = runtime->GetDetectorName();
if (!detectorName.IsEmpty()) {
nsAutoCString detectorContractID;
detectorContractID.AssignLiteral(NS_CHARSET_DETECTOR_CONTRACTID_BASE);
detectorContractID += detectorName;
detector = do_CreateInstance(detectorContractID.get());
}
}
nsresult rv;
if (detector) {
detector->Init(this);
bool done;
uint32_t numRead;
do {
char readBuf[4096];
rv = aStream->Read(readBuf, sizeof(readBuf), &numRead);
NS_ENSURE_SUCCESS(rv, rv);
if (numRead <= 0) {
break;
}
rv = detector->DoIt(readBuf, numRead, &done);
NS_ENSURE_SUCCESS(rv, rv);
} while (!done);
rv = detector->Done();
NS_ENSURE_SUCCESS(rv, rv);
} else {
// no charset detector available, check the BOM
unsigned char sniffBuf[4];
uint32_t numRead;
rv = aStream->Read(reinterpret_cast<char*>(sniffBuf),
sizeof(sniffBuf), &numRead);
NS_ENSURE_SUCCESS(rv, rv);
if (numRead >= 4 &&
sniffBuf[0] == 0x00 &&
sniffBuf[1] == 0x00 &&
sniffBuf[2] == 0xfe &&
sniffBuf[3] == 0xff) {
mCharset = "UTF-32BE";
} else if (numRead >= 4 &&
sniffBuf[0] == 0xff &&
sniffBuf[1] == 0xfe &&
sniffBuf[2] == 0x00 &&
sniffBuf[3] == 0x00) {
mCharset = "UTF-32LE";
} else if (numRead >= 2 &&
sniffBuf[0] == 0xfe &&
sniffBuf[1] == 0xff) {
mCharset = "UTF-16BE";
} else if (numRead >= 2 &&
sniffBuf[0] == 0xff &&
sniffBuf[1] == 0xfe) {
mCharset = "UTF-16LE";
} else if (numRead >= 3 &&
sniffBuf[0] == 0xef &&
sniffBuf[1] == 0xbb &&
sniffBuf[2] == 0xbf) {
mCharset = "UTF-8";
}
}
if (mCharset.IsEmpty()) {
RuntimeService* runtime = RuntimeService::GetService();
mCharset = runtime->GetSystemCharset();
}
if (mCharset.IsEmpty()) {
// no sniffed or default charset, try UTF-8
mCharset.AssignLiteral("UTF-8");
}
aCharset = mCharset;
mCharset.Truncate();
return NS_OK;
}
NS_IMETHODIMP
FileReaderSync::Notify(const char* aCharset, nsDetectionConfident aConf)
{
mCharset.Assign(aCharset);
return NS_OK;
}