2012-09-20 19:47:47 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2011-08-15 20:40:38 -07:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
#include "FileReaderSync.h"
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
#include "mozilla/Base64.h"
|
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2014-10-08 09:15:23 -07:00
|
|
|
#include "mozilla/dom/File.h"
|
2013-12-17 02:47:25 -08:00
|
|
|
#include "nsContentUtils.h"
|
2013-06-05 07:04:23 -07:00
|
|
|
#include "mozilla/dom/FileReaderSyncBinding.h"
|
2012-09-20 19:47:47 -07:00
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDOMClassInfoID.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2012-09-20 19:47:47 -07:00
|
|
|
#include "nsIConverterInputStream.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
#include "RuntimeService.h"
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
USING_WORKERS_NAMESPACE
|
2012-11-10 07:45:52 -08:00
|
|
|
using namespace mozilla;
|
2014-10-08 09:15:22 -07:00
|
|
|
using namespace mozilla::dom;
|
2012-09-20 19:47:47 -07:00
|
|
|
using mozilla::dom::Optional;
|
2013-08-22 22:17:08 -07:00
|
|
|
using mozilla::dom::GlobalObject;
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
// static
|
2013-09-29 21:15:37 -07:00
|
|
|
already_AddRefed<FileReaderSync>
|
2013-08-22 22:17:08 -07:00
|
|
|
FileReaderSync::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
|
2012-09-20 19:47:47 -07:00
|
|
|
{
|
2013-09-29 21:15:37 -07:00
|
|
|
nsRefPtr<FileReaderSync> frs = new FileReaderSync();
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2013-09-29 21:15:37 -07:00
|
|
|
return frs.forget();
|
2011-08-15 20:40:38 -07:00
|
|
|
}
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
FileReaderSync::WrapObject(JSContext* aCx)
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return FileReaderSyncBinding_workers::Wrap(aCx, this);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
void
|
2013-09-29 21:15:37 -07:00
|
|
|
FileReaderSync::ReadAsArrayBuffer(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aScopeObj,
|
2014-10-08 09:15:23 -07:00
|
|
|
File& aBlob,
|
2014-06-11 13:26:52 -07:00
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
2012-09-20 19:47:47 -07:00
|
|
|
ErrorResult& aRv)
|
2011-08-15 20:40:38 -07:00
|
|
|
{
|
2012-09-20 19:47:47 -07:00
|
|
|
uint64_t blobSize;
|
2014-10-08 09:15:22 -07:00
|
|
|
nsresult rv = aBlob.GetSize(&blobSize);
|
2012-09-20 19:47:47 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2014-06-11 13:26:52 -07:00
|
|
|
return;
|
2011-08-15 20:40:38 -07:00
|
|
|
}
|
|
|
|
|
2014-10-07 10:44:07 -07:00
|
|
|
UniquePtr<char[], JS::FreePolicy> bufferData(js_pod_malloc<char>(blobSize));
|
|
|
|
if (!bufferData) {
|
2014-02-13 09:17:41 -08:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2014-06-11 13:26:52 -07:00
|
|
|
return;
|
2014-02-13 09:17:41 -08:00
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2014-10-08 09:15:22 -07:00
|
|
|
rv = aBlob.GetInternalStream(getter_AddRefs(stream));
|
2012-09-20 19:47:47 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2014-06-11 13:26:52 -07:00
|
|
|
return;
|
2011-08-15 20:40:38 -07:00
|
|
|
}
|
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
uint32_t numRead;
|
2014-10-07 10:44:07 -07:00
|
|
|
rv = stream->Read(bufferData.get(), blobSize, &numRead);
|
2012-09-20 19:47:47 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2014-06-11 13:26:52 -07:00
|
|
|
return;
|
2011-08-15 20:40:38 -07:00
|
|
|
}
|
2014-10-07 10:44:07 -07:00
|
|
|
NS_ASSERTION(numRead == blobSize, "failed to read data");
|
|
|
|
|
|
|
|
JSObject* arrayBuffer = JS_NewArrayBufferWithContents(aCx, blobSize, bufferData.get());
|
|
|
|
if (!arrayBuffer) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bufferData.release();
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2014-10-07 10:44:07 -07:00
|
|
|
aRetval.set(arrayBuffer);
|
2012-09-20 19:47:47 -07:00
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
void
|
2014-10-08 09:15:23 -07:00
|
|
|
FileReaderSync::ReadAsBinaryString(File& aBlob,
|
2013-05-16 09:36:55 -07:00
|
|
|
nsAString& aResult,
|
2012-09-20 19:47:47 -07:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2014-10-08 09:15:22 -07:00
|
|
|
nsresult rv = aBlob.GetInternalStream(getter_AddRefs(stream));
|
2012-09-20 19:47:47 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
uint32_t numRead;
|
|
|
|
do {
|
|
|
|
char readBuf[4096];
|
|
|
|
rv = stream->Read(readBuf, sizeof(readBuf), &numRead);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
2011-08-15 20:40:38 -07:00
|
|
|
}
|
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
uint32_t oldLength = aResult.Length();
|
|
|
|
AppendASCIItoUTF16(Substring(readBuf, readBuf + numRead), aResult);
|
|
|
|
if (aResult.Length() - oldLength != numRead) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
2011-08-15 20:40:38 -07:00
|
|
|
}
|
2012-09-20 19:47:47 -07:00
|
|
|
} while (numRead > 0);
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
void
|
2014-10-08 09:15:23 -07:00
|
|
|
FileReaderSync::ReadAsText(File& aBlob,
|
2012-09-20 19:47:47 -07:00
|
|
|
const Optional<nsAString>& aEncoding,
|
|
|
|
nsAString& aResult,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2014-10-08 09:15:22 -07:00
|
|
|
nsresult rv = aBlob.GetInternalStream(getter_AddRefs(stream));
|
2012-09-20 19:47:47 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2013-12-17 02:47:25 -08:00
|
|
|
nsAutoCString encoding;
|
|
|
|
unsigned char sniffBuf[3] = { 0, 0, 0 };
|
|
|
|
uint32_t numRead;
|
|
|
|
rv = stream->Read(reinterpret_cast<char*>(sniffBuf),
|
|
|
|
sizeof(sniffBuf), &numRead);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2013-12-17 02:47:25 -08:00
|
|
|
// The BOM sniffing is baked into the "decode" part of the Encoding
|
|
|
|
// Standard, which the File API references.
|
|
|
|
if (!nsContentUtils::CheckForBOM(sniffBuf, numRead, encoding)) {
|
|
|
|
// BOM sniffing failed. Try the API argument.
|
|
|
|
if (!aEncoding.WasPassed() ||
|
|
|
|
!EncodingUtils::FindEncodingForLabel(aEncoding.Value(),
|
|
|
|
encoding)) {
|
|
|
|
// API argument failed. Try the type property of the blob.
|
|
|
|
nsAutoString type16;
|
2014-10-08 09:15:22 -07:00
|
|
|
aBlob.GetType(type16);
|
2013-12-17 02:47:25 -08:00
|
|
|
NS_ConvertUTF16toUTF8 type(type16);
|
|
|
|
nsAutoCString specifiedCharset;
|
|
|
|
bool haveCharset;
|
|
|
|
int32_t charsetStart, charsetEnd;
|
|
|
|
NS_ExtractCharsetFromContentType(type,
|
|
|
|
specifiedCharset,
|
|
|
|
&haveCharset,
|
|
|
|
&charsetStart,
|
|
|
|
&charsetEnd);
|
|
|
|
if (!EncodingUtils::FindEncodingForLabel(specifiedCharset, encoding)) {
|
|
|
|
// Type property failed. Use UTF-8.
|
|
|
|
encoding.AssignLiteral("UTF-8");
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
}
|
2013-12-17 02:47:25 -08:00
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2013-12-17 02:47:25 -08:00
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(stream);
|
|
|
|
if (!seekable) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
2012-09-20 19:47:47 -07:00
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2013-12-17 02:47:25 -08:00
|
|
|
// Seek to 0 because to undo the BOM sniffing advance. UTF-8 and UTF-16
|
|
|
|
// decoders will swallow the BOM.
|
|
|
|
rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, 0);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2012-09-20 19:47:47 -07:00
|
|
|
return;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2013-12-17 02:47:25 -08:00
|
|
|
rv = ConvertStream(stream, encoding.get(), aResult);
|
2012-09-20 19:47:47 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
2011-08-15 20:40:38 -07:00
|
|
|
}
|
2012-09-20 19:47:47 -07:00
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
void
|
2014-10-08 09:15:23 -07:00
|
|
|
FileReaderSync::ReadAsDataURL(File& aBlob, nsAString& aResult,
|
2012-09-20 19:47:47 -07:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsAutoString scratchResult;
|
|
|
|
scratchResult.AssignLiteral("data:");
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
nsString contentType;
|
2014-10-08 09:15:22 -07:00
|
|
|
aBlob.GetType(contentType);
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
if (contentType.IsEmpty()) {
|
|
|
|
scratchResult.AppendLiteral("application/octet-stream");
|
|
|
|
} else {
|
|
|
|
scratchResult.Append(contentType);
|
|
|
|
}
|
|
|
|
scratchResult.AppendLiteral(";base64,");
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2014-10-08 09:15:22 -07:00
|
|
|
nsresult rv = aBlob.GetInternalStream(getter_AddRefs(stream));
|
2012-09-20 19:47:47 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
uint64_t size;
|
2014-10-08 09:15:22 -07:00
|
|
|
rv = aBlob.GetSize(&size);
|
2012-09-20 19:47:47 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
nsCOMPtr<nsIInputStream> bufferedStream;
|
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), stream, size);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
2011-08-15 20:40:38 -07:00
|
|
|
}
|
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
nsAutoString encodedData;
|
|
|
|
rv = Base64EncodeInputStream(bufferedStream, encodedData, size);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
scratchResult.Append(encodedData);
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
aResult = scratchResult;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
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;
|
2011-08-15 20:40:38 -07:00
|
|
|
}
|
2012-09-20 19:47:47 -07:00
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-09-20 19:47:47 -07:00
|
|
|
return rv;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|