michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "FileReaderSync.h" michael@0: michael@0: #include "jsfriendapi.h" michael@0: #include "mozilla/Base64.h" michael@0: #include "mozilla/dom/EncodingUtils.h" michael@0: #include "nsContentUtils.h" michael@0: #include "mozilla/dom/FileReaderSyncBinding.h" michael@0: #include "nsCExternalHandlerService.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsDOMClassInfoID.h" michael@0: #include "nsError.h" michael@0: #include "nsIDOMFile.h" michael@0: #include "nsIConverterInputStream.h" michael@0: #include "nsIInputStream.h" michael@0: #include "nsISeekableStream.h" michael@0: #include "nsISupportsImpl.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: michael@0: #include "File.h" michael@0: #include "RuntimeService.h" michael@0: michael@0: USING_WORKERS_NAMESPACE michael@0: using namespace mozilla; michael@0: using mozilla::dom::Optional; michael@0: using mozilla::dom::GlobalObject; michael@0: michael@0: // static michael@0: already_AddRefed michael@0: FileReaderSync::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv) michael@0: { michael@0: nsRefPtr frs = new FileReaderSync(); michael@0: michael@0: return frs.forget(); michael@0: } michael@0: michael@0: JSObject* michael@0: FileReaderSync::WrapObject(JSContext* aCx) michael@0: { michael@0: return FileReaderSyncBinding_workers::Wrap(aCx, this); michael@0: } michael@0: michael@0: void michael@0: FileReaderSync::ReadAsArrayBuffer(JSContext* aCx, michael@0: JS::Handle aScopeObj, michael@0: JS::Handle aBlob, michael@0: JS::MutableHandle aRetval, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsIDOMBlob* blob = file::GetDOMBlobFromJSObject(aBlob); michael@0: if (!blob) { michael@0: aRv.Throw(NS_ERROR_INVALID_ARG); michael@0: return; michael@0: } michael@0: michael@0: uint64_t blobSize; michael@0: nsresult rv = blob->GetSize(&blobSize); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: michael@0: JS::Rooted jsArrayBuffer(aCx, JS_NewArrayBuffer(aCx, blobSize)); michael@0: if (!jsArrayBuffer) { michael@0: // XXXkhuey we need a way to indicate to the bindings that the call failed michael@0: // but there's already a pending exception that we should not clobber. michael@0: aRv.Throw(NS_ERROR_OUT_OF_MEMORY); michael@0: return; michael@0: } michael@0: michael@0: uint32_t bufferLength = JS_GetArrayBufferByteLength(jsArrayBuffer); michael@0: uint8_t* arrayBuffer = JS_GetStableArrayBufferData(aCx, jsArrayBuffer); michael@0: if (!arrayBuffer) { michael@0: aRv.Throw(NS_ERROR_OUT_OF_MEMORY); michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr stream; michael@0: rv = blob->GetInternalStream(getter_AddRefs(stream)); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: michael@0: uint32_t numRead; michael@0: rv = stream->Read((char*)arrayBuffer, bufferLength, &numRead); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: NS_ASSERTION(numRead == bufferLength, "failed to read data"); michael@0: michael@0: aRetval.set(jsArrayBuffer); michael@0: } michael@0: michael@0: void michael@0: FileReaderSync::ReadAsBinaryString(JS::Handle aBlob, michael@0: nsAString& aResult, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsIDOMBlob* blob = file::GetDOMBlobFromJSObject(aBlob); michael@0: if (!blob) { michael@0: aRv.Throw(NS_ERROR_INVALID_ARG); michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr stream; michael@0: nsresult rv = blob->GetInternalStream(getter_AddRefs(stream)); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: michael@0: uint32_t numRead; michael@0: do { michael@0: char readBuf[4096]; michael@0: rv = stream->Read(readBuf, sizeof(readBuf), &numRead); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: michael@0: uint32_t oldLength = aResult.Length(); michael@0: AppendASCIItoUTF16(Substring(readBuf, readBuf + numRead), aResult); michael@0: if (aResult.Length() - oldLength != numRead) { michael@0: aRv.Throw(NS_ERROR_OUT_OF_MEMORY); michael@0: return; michael@0: } michael@0: } while (numRead > 0); michael@0: } michael@0: michael@0: void michael@0: FileReaderSync::ReadAsText(JS::Handle aBlob, michael@0: const Optional& aEncoding, michael@0: nsAString& aResult, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsIDOMBlob* blob = file::GetDOMBlobFromJSObject(aBlob); michael@0: if (!blob) { michael@0: aRv.Throw(NS_ERROR_INVALID_ARG); michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr stream; michael@0: nsresult rv = blob->GetInternalStream(getter_AddRefs(stream)); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString encoding; michael@0: unsigned char sniffBuf[3] = { 0, 0, 0 }; michael@0: uint32_t numRead; michael@0: rv = stream->Read(reinterpret_cast(sniffBuf), michael@0: sizeof(sniffBuf), &numRead); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: michael@0: // The BOM sniffing is baked into the "decode" part of the Encoding michael@0: // Standard, which the File API references. michael@0: if (!nsContentUtils::CheckForBOM(sniffBuf, numRead, encoding)) { michael@0: // BOM sniffing failed. Try the API argument. michael@0: if (!aEncoding.WasPassed() || michael@0: !EncodingUtils::FindEncodingForLabel(aEncoding.Value(), michael@0: encoding)) { michael@0: // API argument failed. Try the type property of the blob. michael@0: nsAutoString type16; michael@0: blob->GetType(type16); michael@0: NS_ConvertUTF16toUTF8 type(type16); michael@0: nsAutoCString specifiedCharset; michael@0: bool haveCharset; michael@0: int32_t charsetStart, charsetEnd; michael@0: NS_ExtractCharsetFromContentType(type, michael@0: specifiedCharset, michael@0: &haveCharset, michael@0: &charsetStart, michael@0: &charsetEnd); michael@0: if (!EncodingUtils::FindEncodingForLabel(specifiedCharset, encoding)) { michael@0: // Type property failed. Use UTF-8. michael@0: encoding.AssignLiteral("UTF-8"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr seekable = do_QueryInterface(stream); michael@0: if (!seekable) { michael@0: aRv.Throw(NS_ERROR_FAILURE); michael@0: return; michael@0: } michael@0: michael@0: // Seek to 0 because to undo the BOM sniffing advance. UTF-8 and UTF-16 michael@0: // decoders will swallow the BOM. michael@0: rv = seekable->Seek(nsISeekableStream::NS_SEEK_SET, 0); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: michael@0: rv = ConvertStream(stream, encoding.get(), aResult); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: void michael@0: FileReaderSync::ReadAsDataURL(JS::Handle aBlob, nsAString& aResult, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsIDOMBlob* blob = file::GetDOMBlobFromJSObject(aBlob); michael@0: if (!blob) { michael@0: aRv.Throw(NS_ERROR_INVALID_ARG); michael@0: return; michael@0: } michael@0: michael@0: nsAutoString scratchResult; michael@0: scratchResult.AssignLiteral("data:"); michael@0: michael@0: nsString contentType; michael@0: blob->GetType(contentType); michael@0: michael@0: if (contentType.IsEmpty()) { michael@0: scratchResult.AppendLiteral("application/octet-stream"); michael@0: } else { michael@0: scratchResult.Append(contentType); michael@0: } michael@0: scratchResult.AppendLiteral(";base64,"); michael@0: michael@0: nsCOMPtr stream; michael@0: nsresult rv = blob->GetInternalStream(getter_AddRefs(stream)); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: michael@0: uint64_t size; michael@0: rv = blob->GetSize(&size); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr bufferedStream; michael@0: rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), stream, size); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: michael@0: nsAutoString encodedData; michael@0: rv = Base64EncodeInputStream(bufferedStream, encodedData, size); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return; michael@0: } michael@0: michael@0: scratchResult.Append(encodedData); michael@0: michael@0: aResult = scratchResult; michael@0: } michael@0: michael@0: nsresult michael@0: FileReaderSync::ConvertStream(nsIInputStream *aStream, michael@0: const char *aCharset, michael@0: nsAString &aResult) michael@0: { michael@0: nsCOMPtr converterStream = michael@0: do_CreateInstance("@mozilla.org/intl/converter-input-stream;1"); michael@0: NS_ENSURE_TRUE(converterStream, NS_ERROR_FAILURE); michael@0: michael@0: nsresult rv = converterStream->Init(aStream, aCharset, 8192, michael@0: nsIConverterInputStream::DEFAULT_REPLACEMENT_CHARACTER); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr unicharStream = michael@0: do_QueryInterface(converterStream); michael@0: NS_ENSURE_TRUE(unicharStream, NS_ERROR_FAILURE); michael@0: michael@0: uint32_t numChars; michael@0: nsString result; michael@0: while (NS_SUCCEEDED(unicharStream->ReadString(8192, result, &numChars)) && michael@0: numChars > 0) { michael@0: uint32_t oldLength = aResult.Length(); michael@0: aResult.Append(result); michael@0: if (aResult.Length() - oldLength != result.Length()) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: