Files
palemoon27/dom/base/nsDOMFileReader.cpp
T
roytam1 b9329817d7 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1164425: Cleanup interfaces of |ListenSocket|, r=kmachulis (1a158f323)
- Bug 1164417: Add |ConnectionOrientedSocket::PrepareAccept| for accepting socket connections, r=kmachulis (94c6234e0)
- Bug 1154281: Don't include UnixSocket.h in NFC, r=allstars.chh (c4eff006b)
- Bug 1146270 - Move HCI Event Origin into NfcOptions.webidl. r=smaug, dimi (1d7b28488)
- Bug 1127735 - Update TechDiscoveredNotication. r=smaug, dimi (c27f2350c)
- Bug 1152667 - Remove NfcGonkMessage.h r=dimi (c7651156f)
- Bug 1166638: Use |StreamSocketConsumer| in |StreamSocket|, r=kmachulis, dlee, chucklee, htsai (e885dd269)
- Bug 1166638: Use |ListenSocketConsumer| in |ListenSocket|, r=kmachulis, dlee, chucklee, shuang (53315fa5b)
- Bug 1152095 - Check mResultHandlerQ is empty before dispatching DispatchError. r=tzimmermann (c3a187ac4)
- Bug 1159685 - Fix BluetoothSspVariant argument passing for SspRequestNotification. r=shuang (ed398bcd4)
- Bug 1161003: Don't send PDU when connection to Bluetooth daemon is closed, r=shuang (5bb9c69d2)
- Bug 1167207: Remove |BluetoothDaemonChannel|, r=shuang (4470926f6)
- Bug 1167210: Replace |union sockaddr_any| with |struct sockaddr_storage|, r=kmachulis (69af8f130)
- Bug 1159709: Cleanup inherited methods of Bluedroid's |BluetoothSocket|, r=kmachulis (29a11ec67)
- Bug 1168446: Remove |BluetoothSocket::GetSocketAddr|, r=shuang (f1ba86da8)
- Bug 1168446: Change |BluetoothSocket::{ListenSocket,ConnectSocket}| interface, r=shuang (ef9644d68)
- Bug 1168446: Don't store connection parameters in |BluetoothSocket|, r=shuang (c8acbb880)
- Bug 1166231 - Make nsIDOMBlob an empty interface, r=ehsan (4fae64ce9)
- Bug 1176800 part 1. When getting all types, include the types Promises are parametrized over. r=peterv (8ddb8a0c6)
- Bug 1176800 part 2. Add a ToJSValue overload for owning unions. r=peterv (e0a3625d2)
- Bug 1177688, part 1 - Add API and functionality to the BlobImpl classes so that BlobImpl's that are created from an nsIFile can provide information about whether or not the nsIFile was a directory. r=baku (d45beda77)
- Bug 1177688, part 2 - Add support to the FileSystem code for obtaining a listing of a Directory's Directory and File contents via a sequence of Blobs, forking appropriately to support both for e10s and non-e10s. r=baku (d0e5e3205)
- Bug 1177688, part 3 - Implement .getFilesAndDirectories() and .path for DOM Directory objects. r=baku (f22f86472)
- Bug 1177688, part 4 - Add DeviceStorage tests for the new .getFilesAndDirectories() and .path API on Directory. r=baku (70607068a)
- Bug 1177688, part 5 - Follow-up to use nsAutoString on the stack in Directory to avoid unnecessarily allocating. r=baku (d400df943)
- Bug 1177688, part 6 - Follow-up to provide a default BlobDirState::eUnknownIfDir arg to BlobImplBase's ctor to avoid excessively specifying the same argument. r=baku (7779e7b2d)
- Bug 1097479 - Allow nested content process to embed apps and add a nested pref check function. r=kanru (b11bf2b80)
- Bug 1151495 - Support permission prompting from workers for IDB, r=bent (f3983875d)
- Bug 1173525 - Add JS::Construct overload for specifying new.target. (r=Waldo) (1067d0fba)
- Bug 1138198 - Remove unused/undefined member function ParticularProcessPriorityManager::OnChannelConnected. r=khuey (21a5c2590)
- Bug 852925 - Freeze priority changes when the screen is turned off. r=khuey (812a9e303)
- Bug 1137151: Marked destructor of |nsScreenManagerGonk| as protected, r=mwu (4943f388c)
- Bug 1157874 - Remove screen related global variables from nsWindow.cpp r=mwu (3bdc584ad)
- Bug 1157030 - Prevent visibility changes from affecting the process priority when the screen is off. r=khuey (6582a9303)
- Bug 1157030 - Stop special-casing the homescreen in the process priority manager. r=khuey, r=dhylands (bb97a1b61)
- Bug 1161748 - Rehydrate ActivityOptions in a system scope. r=fabrice,gabor (70dbc3b1b)
- Bug 1144132 - Adjust the priority of an activity opener so that it's above other background applications. r=fabrice (9789a87b1)
- Bug 1150315 - Keep SpeechSynthesis state in sync after successive cancel/speak calls. r=smaug (93abc527d)
- Bug 1154741 - Disable test_speech_cancel.html on b2g. r=me (4bcc59b0e)
- Bug 1155034 - Let indirect audio speech services send their own events. Make pause/resume IPC safe. r=smaug (b91bf210b)
- Bug 1086545 - Bind speech task streams to a parent stream held by voice registry. r=padenot (cef7476ec)
- Bug 1155917 - Added volume argument to nsISpeechService.speak(). r=smaug (a945c472a)
- Bug 1162699 - Replace mochitest test synth services with global services to simplify tests. r=smaug (7724212d9)
- Bug 1162699 followup: Add 'override' keyword to overriding methods on FakeSynthCallback. rs=ehsan (15377091b)
2021-02-12 11:23:36 +08:00

537 lines
14 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=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 "nsDOMFileReader.h"
#include "nsContentCID.h"
#include "nsContentUtils.h"
#include "nsDOMClassInfoID.h"
#include "nsError.h"
#include "nsIFile.h"
#include "nsNetCID.h"
#include "nsNetUtil.h"
#include "nsXPCOM.h"
#include "nsIDOMEventListener.h"
#include "nsJSEnvironment.h"
#include "nsCycleCollectionParticipant.h"
#include "mozilla/Base64.h"
#include "mozilla/dom/EncodingUtils.h"
#include "mozilla/dom/File.h"
#include "mozilla/dom/FileReaderBinding.h"
#include "xpcpublic.h"
#include "nsDOMJSUtils.h"
#include "jsfriendapi.h"
#include "nsITransport.h"
#include "nsIStreamTransportService.h"
using namespace mozilla;
using namespace mozilla::dom;
#define LOAD_STR "load"
#define LOADSTART_STR "loadstart"
#define LOADEND_STR "loadend"
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMFileReader)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMFileReader,
FileIOObject)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBlob)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMFileReader,
FileIOObject)
tmp->mResultArrayBuffer = nullptr;
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBlob)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(nsDOMFileReader,
DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mResultArrayBuffer)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMFileReader)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsIDOMFileReader)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_END_INHERITING(FileIOObject)
NS_IMPL_ADDREF_INHERITED(nsDOMFileReader, FileIOObject)
NS_IMPL_RELEASE_INHERITED(nsDOMFileReader, FileIOObject)
NS_IMPL_EVENT_HANDLER(nsDOMFileReader, load)
NS_IMPL_EVENT_HANDLER(nsDOMFileReader, loadend)
NS_IMPL_EVENT_HANDLER(nsDOMFileReader, loadstart)
NS_IMPL_FORWARD_EVENT_HANDLER(nsDOMFileReader, abort, FileIOObject)
NS_IMPL_FORWARD_EVENT_HANDLER(nsDOMFileReader, progress, FileIOObject)
NS_IMPL_FORWARD_EVENT_HANDLER(nsDOMFileReader, error, FileIOObject)
void
nsDOMFileReader::RootResultArrayBuffer()
{
mozilla::HoldJSObjects(this);
}
//nsDOMFileReader constructors/initializers
nsDOMFileReader::nsDOMFileReader()
: mFileData(nullptr),
mDataLen(0), mDataFormat(FILE_AS_BINARY),
mResultArrayBuffer(nullptr)
{
SetDOMStringToNull(mResult);
}
nsDOMFileReader::~nsDOMFileReader()
{
FreeFileData();
mResultArrayBuffer = nullptr;
mozilla::DropJSObjects(this);
}
/**
* This Init method is called from the factory constructor.
*/
nsresult
nsDOMFileReader::Init()
{
// Instead of grabbing some random global from the context stack,
// let's use the default one (junk scope) for now.
// We should move away from this Init...
BindToOwner(xpc::NativeGlobal(xpc::PrivilegedJunkScope()));
return NS_OK;
}
/* static */ already_AddRefed<nsDOMFileReader>
nsDOMFileReader::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
{
nsRefPtr<nsDOMFileReader> fileReader = new nsDOMFileReader();
nsCOMPtr<nsPIDOMWindow> owner = do_QueryInterface(aGlobal.GetAsSupports());
if (!owner) {
NS_WARNING("Unexpected owner");
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
fileReader->BindToOwner(owner);
return fileReader.forget();
}
// nsIInterfaceRequestor
NS_IMETHODIMP
nsDOMFileReader::GetInterface(const nsIID & aIID, void **aResult)
{
return QueryInterface(aIID, aResult);
}
// nsIDOMFileReader
NS_IMETHODIMP
nsDOMFileReader::GetReadyState(uint16_t *aReadyState)
{
*aReadyState = ReadyState();
return NS_OK;
}
void
nsDOMFileReader::GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
ErrorResult& aRv)
{
aRv = GetResult(aCx, aResult);
}
NS_IMETHODIMP
nsDOMFileReader::GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult)
{
JS::Rooted<JS::Value> result(aCx);
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
if (mReadyState == nsIDOMFileReader::DONE && mResultArrayBuffer) {
result.setObject(*mResultArrayBuffer);
} else {
result.setNull();
}
if (!JS_WrapValue(aCx, &result)) {
return NS_ERROR_FAILURE;
}
aResult.set(result);
return NS_OK;
}
nsString tmpResult = mResult;
if (!xpc::StringToJsval(aCx, tmpResult, aResult)) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
NS_IMETHODIMP
nsDOMFileReader::GetError(nsISupports** aError)
{
NS_IF_ADDREF(*aError = GetError());
return NS_OK;
}
NS_IMETHODIMP
nsDOMFileReader::ReadAsArrayBuffer(nsIDOMBlob* aBlob, JSContext* aCx)
{
NS_ENSURE_TRUE(aBlob, NS_ERROR_NULL_POINTER);
ErrorResult rv;
nsRefPtr<Blob> blob = static_cast<Blob*>(aBlob);
ReadAsArrayBuffer(aCx, *blob, rv);
return rv.StealNSResult();
}
NS_IMETHODIMP
nsDOMFileReader::ReadAsBinaryString(nsIDOMBlob* aBlob)
{
NS_ENSURE_TRUE(aBlob, NS_ERROR_NULL_POINTER);
ErrorResult rv;
nsRefPtr<Blob> blob = static_cast<Blob*>(aBlob);
ReadAsBinaryString(*blob, rv);
return rv.StealNSResult();
}
NS_IMETHODIMP
nsDOMFileReader::ReadAsText(nsIDOMBlob* aBlob,
const nsAString &aCharset)
{
NS_ENSURE_TRUE(aBlob, NS_ERROR_NULL_POINTER);
ErrorResult rv;
nsRefPtr<Blob> blob = static_cast<Blob*>(aBlob);
ReadAsText(*blob, aCharset, rv);
return rv.StealNSResult();
}
NS_IMETHODIMP
nsDOMFileReader::ReadAsDataURL(nsIDOMBlob* aBlob)
{
NS_ENSURE_TRUE(aBlob, NS_ERROR_NULL_POINTER);
ErrorResult rv;
nsRefPtr<Blob> blob = static_cast<Blob*>(aBlob);
ReadAsDataURL(*blob, rv);
return rv.StealNSResult();
}
NS_IMETHODIMP
nsDOMFileReader::Abort()
{
ErrorResult rv;
FileIOObject::Abort(rv);
return rv.StealNSResult();
}
/* virtual */ void
nsDOMFileReader::DoAbort(nsAString& aEvent)
{
// Revert status and result attributes
SetDOMStringToNull(mResult);
mResultArrayBuffer = nullptr;
mAsyncStream = nullptr;
mBlob = nullptr;
//Clean up memory buffer
FreeFileData();
// Tell the base class which event to dispatch
aEvent = NS_LITERAL_STRING(LOADEND_STR);
}
static
NS_METHOD
ReadFuncBinaryString(nsIInputStream* in,
void* closure,
const char* fromRawSegment,
uint32_t toOffset,
uint32_t count,
uint32_t *writeCount)
{
char16_t* dest = static_cast<char16_t*>(closure) + toOffset;
char16_t* end = dest + count;
const unsigned char* source = (const unsigned char*)fromRawSegment;
while (dest != end) {
*dest = *source;
++dest;
++source;
}
*writeCount = count;
return NS_OK;
}
nsresult
nsDOMFileReader::DoOnLoadEnd(nsresult aStatus,
nsAString& aSuccessEvent,
nsAString& aTerminationEvent)
{
// Make sure we drop all the objects that could hold files open now.
nsCOMPtr<nsIAsyncInputStream> stream;
mAsyncStream.swap(stream);
nsRefPtr<Blob> blob;
mBlob.swap(blob);
aSuccessEvent = NS_LITERAL_STRING(LOAD_STR);
aTerminationEvent = NS_LITERAL_STRING(LOADEND_STR);
// Clear out the data if necessary
if (NS_FAILED(aStatus)) {
FreeFileData();
return NS_OK;
}
nsresult rv = NS_OK;
switch (mDataFormat) {
case FILE_AS_ARRAYBUFFER: {
AutoJSAPI jsapi;
if (NS_WARN_IF(!jsapi.Init(mozilla::DOMEventTargetHelper::GetParentObject()))) {
return NS_ERROR_FAILURE;
}
RootResultArrayBuffer();
mResultArrayBuffer = JS_NewArrayBufferWithContents(jsapi.cx(), mTotal, mFileData);
if (!mResultArrayBuffer) {
JS_ClearPendingException(jsapi.cx());
rv = NS_ERROR_OUT_OF_MEMORY;
} else {
mFileData = nullptr; // Transfer ownership
}
break;
}
case FILE_AS_BINARY:
break; //Already accumulated mResult
case FILE_AS_TEXT:
if (!mFileData) {
if (mDataLen) {
rv = NS_ERROR_OUT_OF_MEMORY;
break;
}
rv = GetAsText(blob, mCharset, "", mDataLen, mResult);
break;
}
rv = GetAsText(blob, mCharset, mFileData, mDataLen, mResult);
break;
case FILE_AS_DATAURL:
rv = GetAsDataURL(blob, mFileData, mDataLen, mResult);
break;
}
mResult.SetIsVoid(false);
FreeFileData();
return rv;
}
nsresult
nsDOMFileReader::DoReadData(nsIAsyncInputStream* aStream, uint64_t aCount)
{
MOZ_ASSERT(aStream);
if (mDataFormat == FILE_AS_BINARY) {
//Continuously update our binary string as data comes in
uint32_t oldLen = mResult.Length();
NS_ASSERTION(mResult.Length() == mDataLen,
"unexpected mResult length");
if (uint64_t(oldLen) + aCount > UINT32_MAX)
return NS_ERROR_OUT_OF_MEMORY;
char16_t *buf = nullptr;
mResult.GetMutableData(&buf, oldLen + aCount, fallible);
NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY);
uint32_t bytesRead = 0;
aStream->ReadSegments(ReadFuncBinaryString, buf + oldLen, aCount,
&bytesRead);
NS_ASSERTION(bytesRead == aCount, "failed to read data");
}
else {
//Update memory buffer to reflect the contents of the file
if (mDataLen + aCount > UINT32_MAX) {
// PR_Realloc doesn't support over 4GB memory size even if 64-bit OS
return NS_ERROR_OUT_OF_MEMORY;
}
if (mDataFormat != FILE_AS_ARRAYBUFFER) {
mFileData = (char *) moz_realloc(mFileData, mDataLen + aCount);
NS_ENSURE_TRUE(mFileData, NS_ERROR_OUT_OF_MEMORY);
}
uint32_t bytesRead = 0;
aStream->Read(mFileData + mDataLen, aCount, &bytesRead);
NS_ASSERTION(bytesRead == aCount, "failed to read data");
}
mDataLen += aCount;
return NS_OK;
}
// Helper methods
void
nsDOMFileReader::ReadFileContent(Blob& aBlob,
const nsAString &aCharset,
eDataFormat aDataFormat,
ErrorResult& aRv)
{
//Implicit abort to clear any other activity going on
Abort();
mError = nullptr;
SetDOMStringToNull(mResult);
mTransferred = 0;
mTotal = 0;
mReadyState = nsIDOMFileReader::EMPTY;
FreeFileData();
mBlob = &aBlob;
mDataFormat = aDataFormat;
CopyUTF16toUTF8(aCharset, mCharset);
nsresult rv;
nsCOMPtr<nsIStreamTransportService> sts =
do_GetService(kStreamTransportServiceCID, &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
aRv.Throw(rv);
return;
}
nsCOMPtr<nsIInputStream> stream;
mBlob->GetInternalStream(getter_AddRefs(stream), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
nsCOMPtr<nsITransport> transport;
rv = sts->CreateInputTransport(stream,
/* aStartOffset */ 0,
/* aReadLimit */ -1,
/* aCloseWhenDone */ true,
getter_AddRefs(transport));
if (NS_WARN_IF(NS_FAILED(rv))) {
aRv.Throw(rv);
return;
}
nsCOMPtr<nsIInputStream> wrapper;
rv = transport->OpenInputStream(/* aFlags */ 0,
/* aSegmentSize */ 0,
/* aSegmentCount */ 0,
getter_AddRefs(wrapper));
if (NS_WARN_IF(NS_FAILED(rv))) {
aRv.Throw(rv);
return;
}
MOZ_ASSERT(!mAsyncStream);
mAsyncStream = do_QueryInterface(wrapper);
MOZ_ASSERT(mAsyncStream);
mTotal = mBlob->GetSize(aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
rv = DoAsyncWait(mAsyncStream);
if (NS_WARN_IF(NS_FAILED(rv))) {
aRv.Throw(rv);
return;
}
//FileReader should be in loading state here
mReadyState = nsIDOMFileReader::LOADING;
DispatchProgressEvent(NS_LITERAL_STRING(LOADSTART_STR));
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
mFileData = js_pod_malloc<char>(mTotal);
if (!mFileData) {
NS_WARNING("Preallocation failed for ReadFileData");
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
}
}
}
nsresult
nsDOMFileReader::GetAsText(Blob *aBlob,
const nsACString &aCharset,
const char *aFileData,
uint32_t aDataLen,
nsAString& aResult)
{
// The BOM sniffing is baked into the "decode" part of the Encoding
// Standard, which the File API references.
nsAutoCString encoding;
if (!nsContentUtils::CheckForBOM(
reinterpret_cast<const unsigned char *>(aFileData),
aDataLen,
encoding)) {
// BOM sniffing failed. Try the API argument.
if (!EncodingUtils::FindEncodingForLabel(aCharset,
encoding)) {
// API argument failed. Try the type property of the blob.
nsAutoString type16;
aBlob->GetType(type16);
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");
}
}
}
nsDependentCSubstring data(aFileData, aDataLen);
return nsContentUtils::ConvertStringFromEncoding(encoding, data, aResult);
}
nsresult
nsDOMFileReader::GetAsDataURL(Blob *aBlob,
const char *aFileData,
uint32_t aDataLen,
nsAString& aResult)
{
aResult.AssignLiteral("data:");
nsString contentType;
aBlob->GetType(contentType);
if (!contentType.IsEmpty()) {
aResult.Append(contentType);
} else {
aResult.AppendLiteral("application/octet-stream");
}
aResult.AppendLiteral(";base64,");
nsCString encodedData;
nsresult rv = Base64Encode(Substring(aFileData, aDataLen), encodedData);
NS_ENSURE_SUCCESS(rv, rv);
if (!AppendASCIItoUTF16(encodedData, aResult, fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
/* virtual */ JSObject*
nsDOMFileReader::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return FileReaderBinding::Wrap(aCx, this, aGivenProto);
}