2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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/. */
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
#include "FileReader.h"
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsIGlobalObject.h"
|
|
|
|
#include "nsITimer.h"
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2019-03-05 02:19:16 +03:00
|
|
|
#include "js/ArrayBuffer.h" // JS::NewArrayBufferWithContents
|
2012-12-06 02:20:57 +04:00
|
|
|
#include "mozilla/Base64.h"
|
2016-01-26 13:19:15 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2017-08-06 20:47:00 +03:00
|
|
|
#include "mozilla/dom/DOMException.h"
|
2018-04-07 06:27:56 +03:00
|
|
|
#include "mozilla/dom/DOMExceptionBinding.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2013-04-13 11:06:31 +04:00
|
|
|
#include "mozilla/dom/FileReaderBinding.h"
|
2015-12-09 14:23:16 +03:00
|
|
|
#include "mozilla/dom/ProgressEvent.h"
|
2018-03-13 23:17:06 +03:00
|
|
|
#include "mozilla/dom/WorkerCommon.h"
|
|
|
|
#include "mozilla/dom/WorkerRef.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2017-06-16 15:11:03 +03:00
|
|
|
#include "mozilla/Encoding.h"
|
2017-10-17 14:17:51 +03:00
|
|
|
#include "nsAlgorithm.h"
|
2015-12-18 11:44:00 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2015-12-17 16:13:59 +03:00
|
|
|
#include "nsDOMJSUtils.h"
|
2015-12-18 11:44:00 +03:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "xpcpublic.h"
|
2018-08-22 16:59:43 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2011-05-27 08:53:03 +04:00
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
#define ABORT_STR "abort"
|
2009-10-07 12:30:26 +04:00
|
|
|
#define LOAD_STR "load"
|
|
|
|
#define LOADSTART_STR "loadstart"
|
|
|
|
#define LOADEND_STR "loadend"
|
2015-12-09 14:23:16 +03:00
|
|
|
#define ERROR_STR "error"
|
|
|
|
#define PROGRESS_STR "progress"
|
|
|
|
|
|
|
|
const uint64_t kUnknownSize = uint64_t(-1);
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(FileReader)
|
2013-08-02 05:29:05 +04:00
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FileReader,
|
2015-12-09 14:23:16 +03:00
|
|
|
DOMEventTargetHelper)
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBlob)
|
2015-12-09 14:23:16 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mProgressNotifier)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mError)
|
2009-10-07 12:30:26 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FileReader,
|
2015-12-09 14:23:16 +03:00
|
|
|
DOMEventTargetHelper)
|
2015-12-18 11:44:00 +03:00
|
|
|
tmp->Shutdown();
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBlob)
|
2015-12-09 14:23:16 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mProgressNotifier)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mError)
|
2009-10-07 12:30:26 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(FileReader, DOMEventTargetHelper)
|
2012-06-11 03:44:50 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mResultArrayBuffer)
|
2011-07-01 04:50:44 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileReader)
|
2018-09-09 01:44:23 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONCRETE(FileReader)
|
2015-12-09 14:23:16 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITimerCallback)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStreamCallback)
|
2009-10-07 12:30:26 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsINamed)
|
2015-12-09 14:23:16 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(FileReader, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(FileReader, DOMEventTargetHelper)
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
class MOZ_RAII FileReaderDecreaseBusyCounter {
|
|
|
|
RefPtr<FileReader> mFileReader;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
public:
|
|
|
|
explicit FileReaderDecreaseBusyCounter(FileReader* aFileReader)
|
|
|
|
: mFileReader(aFileReader) {}
|
|
|
|
|
|
|
|
~FileReaderDecreaseBusyCounter() { mFileReader->DecreaseBusyCounter(); }
|
|
|
|
};
|
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
void FileReader::RootResultArrayBuffer() { mozilla::HoldJSObjects(this); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
// FileReader constructors/initializers
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-01-20 21:17:57 +03:00
|
|
|
FileReader::FileReader(nsIGlobalObject* aGlobal, WeakWorkerRef* aWorkerRef)
|
|
|
|
: DOMEventTargetHelper(aGlobal),
|
2015-12-09 23:52:15 +03:00
|
|
|
mFileData(nullptr),
|
2015-12-09 14:23:16 +03:00
|
|
|
mDataLen(0),
|
|
|
|
mDataFormat(FILE_AS_BINARY),
|
|
|
|
mResultArrayBuffer(nullptr),
|
|
|
|
mProgressEventWasDelayed(false),
|
|
|
|
mTimerIsActive(false),
|
2015-12-09 23:52:15 +03:00
|
|
|
mReadyState(EMPTY),
|
2015-12-09 14:23:16 +03:00
|
|
|
mTotal(0),
|
|
|
|
mTransferred(0),
|
2015-12-18 11:44:00 +03:00
|
|
|
mBusyCount(0),
|
2018-03-13 23:17:06 +03:00
|
|
|
mWeakWorkerRef(aWorkerRef) {
|
2016-01-20 21:17:57 +03:00
|
|
|
MOZ_ASSERT(aGlobal);
|
2018-03-13 23:17:06 +03:00
|
|
|
MOZ_ASSERT_IF(NS_IsMainThread(), !mWeakWorkerRef);
|
2017-05-08 12:16:20 +03:00
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
mTarget = aGlobal->EventTargetFor(TaskCategory::Other);
|
|
|
|
} else {
|
2017-06-01 23:42:05 +03:00
|
|
|
mTarget = GetCurrentThreadSerialEventTarget();
|
2017-05-08 12:16:20 +03:00
|
|
|
}
|
|
|
|
|
2011-07-01 04:50:44 +04:00
|
|
|
SetDOMStringToNull(mResult);
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
FileReader::~FileReader() {
|
2015-12-18 11:44:00 +03:00
|
|
|
Shutdown();
|
2015-12-17 16:13:59 +03:00
|
|
|
DropJSObjects(this);
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<FileReader> FileReader::Constructor(
|
2019-09-12 14:01:17 +03:00
|
|
|
const GlobalObject& aGlobal) {
|
2016-01-20 21:17:57 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
2018-03-13 23:17:06 +03:00
|
|
|
RefPtr<WeakWorkerRef> workerRef;
|
2015-12-09 23:52:15 +03:00
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
JSContext* cx = aGlobal.Context();
|
2018-03-13 23:17:06 +03:00
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
|
|
|
|
|
|
|
|
workerRef = WeakWorkerRef::Create(workerPrivate);
|
2015-12-18 11:44:00 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 23:17:06 +03:00
|
|
|
RefPtr<FileReader> fileReader = new FileReader(global, workerRef);
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2013-04-13 11:06:31 +04:00
|
|
|
return fileReader.forget();
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIInterfaceRequestor
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-12-09 14:22:40 +03:00
|
|
|
FileReader::GetInterface(const nsIID& aIID, void** aResult) {
|
2009-10-07 12:30:26 +04:00
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:53:18 +03:00
|
|
|
void FileReader::GetResult(JSContext* aCx,
|
|
|
|
Nullable<OwningStringOrArrayBuffer>& aResult) {
|
2013-10-26 20:19:05 +04:00
|
|
|
JS::Rooted<JS::Value> result(aCx);
|
2015-12-09 23:52:15 +03:00
|
|
|
|
2011-07-01 04:50:44 +04:00
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
2019-10-22 14:53:18 +03:00
|
|
|
if (mReadyState != DONE || !mResultArrayBuffer ||
|
|
|
|
!aResult.SetValue().SetAsArrayBuffer().Init(mResultArrayBuffer)) {
|
|
|
|
aResult.SetNull();
|
2011-07-01 04:50:44 +04:00
|
|
|
}
|
2015-12-09 23:52:15 +03:00
|
|
|
|
|
|
|
return;
|
2011-07-01 04:50:44 +04:00
|
|
|
}
|
2013-10-26 20:19:05 +04:00
|
|
|
|
2019-10-22 14:53:18 +03:00
|
|
|
if (mResult.IsVoid()) {
|
|
|
|
aResult.SetNull();
|
2019-10-22 09:34:29 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-10-22 14:53:18 +03:00
|
|
|
|
|
|
|
aResult.SetValue().SetAsString() = mResult;
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
void FileReader::OnLoadEndArrayBuffer() {
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (!jsapi.Init(GetParentObject())) {
|
|
|
|
FreeDataAndDispatchError(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2015-05-19 17:36:37 +03:00
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
RootResultArrayBuffer();
|
2012-08-28 18:59:18 +04:00
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
|
2019-03-05 02:19:16 +03:00
|
|
|
mResultArrayBuffer = JS::NewArrayBufferWithContents(cx, mDataLen, mFileData);
|
2017-01-24 14:12:15 +03:00
|
|
|
if (mResultArrayBuffer) {
|
|
|
|
mFileData = nullptr; // Transfer ownership
|
|
|
|
FreeDataAndDispatchSuccess();
|
|
|
|
return;
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
// Let's handle the error status.
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> exceptionValue(cx);
|
|
|
|
if (!JS_GetPendingException(cx, &exceptionValue) ||
|
|
|
|
// This should not really happen, exception should always be an object.
|
|
|
|
!exceptionValue.isObject()) {
|
|
|
|
JS_ClearPendingException(jsapi.cx());
|
|
|
|
FreeDataAndDispatchError(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
2015-12-05 12:37:35 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
JS_ClearPendingException(jsapi.cx());
|
2014-10-07 21:44:07 +04:00
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
JS::Rooted<JSObject*> exceptionObject(cx, &exceptionValue.toObject());
|
|
|
|
JSErrorReport* er = JS_ErrorFromException(cx, exceptionObject);
|
|
|
|
if (!er || er->message()) {
|
|
|
|
FreeDataAndDispatchError(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
2014-06-10 22:48:36 +04:00
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
nsAutoString errorName;
|
2019-10-14 12:32:07 +03:00
|
|
|
JSLinearString* name = js::GetErrorTypeName(cx, er->exnType);
|
2017-01-24 14:12:15 +03:00
|
|
|
if (name) {
|
2019-10-14 12:32:07 +03:00
|
|
|
AssignJSLinearString(errorName, name);
|
2017-01-24 14:12:15 +03:00
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2017-08-06 20:47:00 +03:00
|
|
|
nsAutoCString errorMsg(er->message().c_str());
|
|
|
|
nsAutoCString errorNameC = NS_LossyConvertUTF16toASCII(errorName);
|
|
|
|
// XXX Code selected arbitrarily
|
2017-01-24 14:12:15 +03:00
|
|
|
mError =
|
2017-08-06 20:47:00 +03:00
|
|
|
new DOMException(NS_ERROR_DOM_INVALID_STATE_ERR, errorMsg, errorNameC,
|
2018-06-26 00:20:54 +03:00
|
|
|
DOMException_Binding::INVALID_STATE_ERR);
|
2009-11-23 09:23:23 +03:00
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
FreeDataAndDispatchError();
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
nsresult FileReader::DoAsyncWait() {
|
|
|
|
nsresult rv = IncreaseBusyCounter();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mAsyncStream->AsyncWait(this,
|
|
|
|
/* aFlags*/ 0,
|
|
|
|
/* aRequestedCount */ 0, mTarget);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
DecreaseBusyCounter();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-17 21:38:03 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void PopulateBufferForBinaryString(char16_t* aDest, const char* aSource,
|
|
|
|
uint32_t aCount) {
|
2018-08-22 16:59:43 +03:00
|
|
|
// Zero-extend each char to char16_t.
|
2019-09-18 11:26:34 +03:00
|
|
|
ConvertLatin1toUtf16(MakeSpan(aSource, aCount), MakeSpan(aDest, aCount));
|
2017-10-17 21:38:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ReadFuncBinaryString(nsIInputStream* aInputStream, void* aClosure,
|
|
|
|
const char* aFromRawSegment, uint32_t aToOffset,
|
|
|
|
uint32_t aCount, uint32_t* aWriteCount) {
|
|
|
|
char16_t* dest = static_cast<char16_t*>(aClosure) + aToOffset;
|
|
|
|
PopulateBufferForBinaryString(dest, aFromRawSegment, aCount);
|
|
|
|
*aWriteCount = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
nsresult FileReader::DoReadData(uint64_t aCount) {
|
|
|
|
MOZ_ASSERT(mAsyncStream);
|
2014-06-10 22:48:36 +04:00
|
|
|
|
2017-05-04 15:45:34 +03:00
|
|
|
uint32_t bytesRead = 0;
|
|
|
|
|
2014-06-10 22:48:36 +04:00
|
|
|
if (mDataFormat == FILE_AS_BINARY) {
|
|
|
|
// Continuously update our binary string as data comes in
|
2017-10-17 14:17:51 +03:00
|
|
|
CheckedInt<uint64_t> size = mResult.Length();
|
|
|
|
size += aCount;
|
|
|
|
|
|
|
|
if (!size.isValid() || size.value() > UINT32_MAX || size.value() > mTotal) {
|
2014-06-10 22:48:36 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2017-10-17 14:17:51 +03:00
|
|
|
}
|
2014-06-10 22:48:36 +04:00
|
|
|
|
2017-10-17 14:17:51 +03:00
|
|
|
uint32_t oldLen = mResult.Length();
|
|
|
|
MOZ_ASSERT(oldLen == mDataLen, "unexpected mResult length");
|
|
|
|
|
|
|
|
char16_t* dest = nullptr;
|
|
|
|
mResult.GetMutableData(&dest, size.value(), fallible);
|
|
|
|
NS_ENSURE_TRUE(dest, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
dest += oldLen;
|
|
|
|
|
2017-10-17 21:38:03 +03:00
|
|
|
if (NS_InputStreamIsBuffered(mAsyncStream)) {
|
|
|
|
nsresult rv = mAsyncStream->ReadSegments(ReadFuncBinaryString, dest,
|
|
|
|
aCount, &bytesRead);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
while (aCount > 0) {
|
|
|
|
char tmpBuffer[4096];
|
|
|
|
uint32_t minCount =
|
|
|
|
XPCOM_MIN(aCount, static_cast<uint64_t>(sizeof(tmpBuffer)));
|
|
|
|
uint32_t read;
|
2017-04-24 13:09:40 +03:00
|
|
|
|
2017-10-17 21:38:03 +03:00
|
|
|
nsresult rv = mAsyncStream->Read(tmpBuffer, minCount, &read);
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
2017-04-24 13:09:40 +03:00
|
|
|
|
2017-10-17 21:38:03 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-10-17 14:17:51 +03:00
|
|
|
|
2017-10-17 21:38:03 +03:00
|
|
|
if (read == 0) {
|
|
|
|
// The stream finished too early.
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2017-10-17 14:17:51 +03:00
|
|
|
|
2017-10-17 21:38:03 +03:00
|
|
|
PopulateBufferForBinaryString(dest, tmpBuffer, read);
|
2017-10-17 23:23:30 +03:00
|
|
|
|
2017-10-17 21:38:03 +03:00
|
|
|
dest += read;
|
|
|
|
aCount -= read;
|
|
|
|
bytesRead += read;
|
|
|
|
}
|
2017-03-07 10:44:54 +03:00
|
|
|
}
|
|
|
|
|
2017-10-17 14:17:51 +03:00
|
|
|
MOZ_ASSERT(size.value() == oldLen + bytesRead);
|
|
|
|
mResult.Truncate(size.value());
|
2014-06-10 22:48:36 +04:00
|
|
|
} else {
|
2016-01-26 13:19:15 +03:00
|
|
|
CheckedInt<uint64_t> size = mDataLen;
|
|
|
|
size += aCount;
|
|
|
|
|
2014-06-10 22:48:36 +04:00
|
|
|
// Update memory buffer to reflect the contents of the file
|
2016-01-26 13:19:15 +03:00
|
|
|
if (!size.isValid() ||
|
|
|
|
// PR_Realloc doesn't support over 4GB memory size even if 64-bit OS
|
2017-07-01 05:05:41 +03:00
|
|
|
// XXX: it's likely that this check is unnecessary and the comment is
|
|
|
|
// wrong because we no longer use PR_Realloc outside of NSPR and NSS.
|
2016-01-26 13:19:15 +03:00
|
|
|
size.value() > UINT32_MAX || size.value() > mTotal) {
|
2014-06-10 22:48:36 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2016-01-26 13:19:15 +03:00
|
|
|
|
2017-05-04 17:39:22 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mFileData);
|
2017-05-04 15:45:34 +03:00
|
|
|
MOZ_RELEASE_ASSERT((mDataLen + aCount) <= mTotal);
|
|
|
|
|
2017-03-07 10:44:54 +03:00
|
|
|
nsresult rv = mAsyncStream->Read(mFileData + mDataLen, aCount, &bytesRead);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-06-10 22:48:36 +04:00
|
|
|
}
|
|
|
|
|
2017-05-04 15:45:34 +03:00
|
|
|
mDataLen += bytesRead;
|
2014-06-10 22:48:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-10-07 12:30:26 +04:00
|
|
|
// Helper methods
|
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
void FileReader::ReadFileContent(Blob& aBlob, const nsAString& aCharset,
|
|
|
|
eDataFormat aDataFormat, ErrorResult& aRv) {
|
2018-03-13 23:17:06 +03:00
|
|
|
if (IsCurrentThreadRunningWorker() && !mWeakWorkerRef) {
|
|
|
|
// The worker is already shutting down.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-20 10:55:37 +03:00
|
|
|
if (mReadyState == LOADING) {
|
2016-12-01 20:57:36 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2016-01-20 10:55:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mError = nullptr;
|
2016-12-01 20:57:36 +03:00
|
|
|
|
2009-11-22 22:05:12 +03:00
|
|
|
SetDOMStringToNull(mResult);
|
2016-12-01 20:57:36 +03:00
|
|
|
mResultArrayBuffer = nullptr;
|
|
|
|
|
|
|
|
mAsyncStream = nullptr;
|
|
|
|
|
2011-09-29 20:06:35 +04:00
|
|
|
mTransferred = 0;
|
|
|
|
mTotal = 0;
|
2015-12-09 23:52:15 +03:00
|
|
|
mReadyState = EMPTY;
|
2009-11-23 09:23:23 +03:00
|
|
|
FreeFileData();
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
mBlob = &aBlob;
|
2009-11-23 09:23:23 +03:00
|
|
|
mDataFormat = aDataFormat;
|
2010-10-07 20:36:23 +04:00
|
|
|
CopyUTF16toUTF8(aCharset, mCharset);
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2018-01-31 18:45:20 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
mBlob->CreateInputStream(getter_AddRefs(stream), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2018-01-31 18:45:20 +03:00
|
|
|
aRv = NS_MakeAsyncNonBlockingInputStream(stream.forget(),
|
|
|
|
getter_AddRefs(mAsyncStream));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-10 22:48:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mAsyncStream);
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
mTotal = mBlob->GetSize(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2017-05-04 15:45:34 +03:00
|
|
|
// Binary Format doesn't need a post-processing of the data. Everything is
|
|
|
|
// written directly into mResult.
|
|
|
|
if (mDataFormat != FILE_AS_BINARY) {
|
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
mFileData = js_pod_malloc<char>(mTotal);
|
|
|
|
} else {
|
|
|
|
mFileData = (char*)malloc(mTotal);
|
|
|
|
}
|
|
|
|
|
2017-01-05 19:07:54 +03:00
|
|
|
if (!mFileData) {
|
|
|
|
NS_WARNING("Preallocation failed for ReadFileData");
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2017-01-16 19:35:40 +03:00
|
|
|
return;
|
2017-01-05 19:07:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 13:50:37 +03:00
|
|
|
aRv = DoAsyncWait();
|
2015-12-09 23:52:15 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2017-01-05 19:07:54 +03:00
|
|
|
FreeFileData();
|
2014-06-10 22:48:36 +04:00
|
|
|
return;
|
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2009-11-09 23:05:16 +03:00
|
|
|
// FileReader should be in loading state here
|
2015-12-09 23:52:15 +03:00
|
|
|
mReadyState = LOADING;
|
2009-10-07 12:30:26 +04:00
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(LOADSTART_STR));
|
|
|
|
}
|
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
nsresult FileReader::GetAsText(Blob* aBlob, const nsACString& aCharset,
|
|
|
|
const char* aFileData, uint32_t aDataLen,
|
|
|
|
nsAString& aResult) {
|
2017-06-16 15:11:03 +03:00
|
|
|
// Try the API argument.
|
|
|
|
const Encoding* encoding = Encoding::ForLabel(aCharset);
|
|
|
|
if (!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);
|
|
|
|
encoding = Encoding::ForLabel(specifiedCharset);
|
|
|
|
if (!encoding) {
|
|
|
|
// Type property failed. Use UTF-8.
|
|
|
|
encoding = UTF_8_ENCODING;
|
2013-12-17 14:47:25 +04:00
|
|
|
}
|
2012-11-08 03:04:22 +04:00
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2017-06-16 15:11:03 +03:00
|
|
|
auto data = MakeSpan(reinterpret_cast<const uint8_t*>(aFileData), aDataLen);
|
|
|
|
nsresult rv;
|
|
|
|
Tie(rv, encoding) = encoding->Decode(data, aResult);
|
|
|
|
return NS_FAILED(rv) ? rv : NS_OK;
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
nsresult FileReader::GetAsDataURL(Blob* aBlob, const char* aFileData,
|
|
|
|
uint32_t aDataLen, nsAString& aResult) {
|
2009-10-07 12:30:26 +04:00
|
|
|
aResult.AssignLiteral("data:");
|
|
|
|
|
2015-12-09 23:52:15 +03:00
|
|
|
nsAutoString contentType;
|
2015-05-19 17:36:37 +03:00
|
|
|
aBlob->GetType(contentType);
|
|
|
|
if (!contentType.IsEmpty()) {
|
2010-09-05 22:00:05 +04:00
|
|
|
aResult.Append(contentType);
|
2009-10-07 12:30:26 +04:00
|
|
|
} else {
|
|
|
|
aResult.AppendLiteral("application/octet-stream");
|
|
|
|
}
|
|
|
|
aResult.AppendLiteral(";base64,");
|
|
|
|
|
2012-12-06 02:20:57 +04:00
|
|
|
nsCString encodedData;
|
2015-05-19 17:36:37 +03:00
|
|
|
nsresult rv = Base64Encode(Substring(aFileData, aDataLen), encodedData);
|
2012-12-06 02:20:57 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-12-06 10:09:30 +04:00
|
|
|
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!AppendASCIItoUTF16(encodedData, aResult, fallible)) {
|
2014-02-11 18:22:45 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* virtual */
|
|
|
|
JSObject* FileReader::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return FileReader_Binding::Wrap(aCx, this, aGivenProto);
|
2013-04-13 11:06:31 +04:00
|
|
|
}
|
2015-12-09 14:23:16 +03:00
|
|
|
|
|
|
|
void FileReader::StartProgressEventTimer() {
|
|
|
|
if (!mProgressNotifier) {
|
2018-10-18 02:57:35 +03:00
|
|
|
mProgressNotifier = NS_NewTimer(mTarget);
|
2015-12-09 14:23:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mProgressNotifier) {
|
|
|
|
mProgressEventWasDelayed = false;
|
|
|
|
mTimerIsActive = true;
|
|
|
|
mProgressNotifier->Cancel();
|
|
|
|
mProgressNotifier->InitWithCallback(this, NS_PROGRESS_EVENT_INTERVAL,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileReader::ClearProgressEventTimer() {
|
|
|
|
mProgressEventWasDelayed = false;
|
|
|
|
mTimerIsActive = false;
|
|
|
|
if (mProgressNotifier) {
|
|
|
|
mProgressNotifier->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 17:57:04 +03:00
|
|
|
void FileReader::FreeFileData() {
|
|
|
|
if (mFileData) {
|
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
js_free(mFileData);
|
|
|
|
} else {
|
|
|
|
free(mFileData);
|
|
|
|
}
|
|
|
|
mFileData = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDataLen = 0;
|
|
|
|
}
|
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
void FileReader::FreeDataAndDispatchSuccess() {
|
|
|
|
FreeFileData();
|
|
|
|
mResult.SetIsVoid(false);
|
|
|
|
mAsyncStream = nullptr;
|
|
|
|
mBlob = nullptr;
|
|
|
|
|
|
|
|
// Dispatch event to signify end of a successful operation
|
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(LOAD_STR));
|
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(LOADEND_STR));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileReader::FreeDataAndDispatchError() {
|
|
|
|
MOZ_ASSERT(mError);
|
|
|
|
|
|
|
|
FreeFileData();
|
|
|
|
mResult.SetIsVoid(true);
|
|
|
|
mAsyncStream = nullptr;
|
|
|
|
mBlob = nullptr;
|
|
|
|
|
|
|
|
// Dispatch error event to signify load failure
|
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(ERROR_STR));
|
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(LOADEND_STR));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileReader::FreeDataAndDispatchError(nsresult aRv) {
|
2015-12-09 14:23:16 +03:00
|
|
|
// Set the status attribute, and dispatch the error event
|
2017-01-20 15:45:35 +03:00
|
|
|
switch (aRv) {
|
2015-12-09 14:23:16 +03:00
|
|
|
case NS_ERROR_FILE_NOT_FOUND:
|
2017-08-06 20:47:00 +03:00
|
|
|
mError = DOMException::Create(NS_ERROR_DOM_NOT_FOUND_ERR);
|
2015-12-09 14:23:16 +03:00
|
|
|
break;
|
|
|
|
case NS_ERROR_FILE_ACCESS_DENIED:
|
2017-08-06 20:47:00 +03:00
|
|
|
mError = DOMException::Create(NS_ERROR_DOM_SECURITY_ERR);
|
2015-12-09 14:23:16 +03:00
|
|
|
break;
|
|
|
|
default:
|
2017-08-06 20:47:00 +03:00
|
|
|
mError = DOMException::Create(NS_ERROR_DOM_FILE_NOT_READABLE_ERR);
|
2015-12-09 14:23:16 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
FreeDataAndDispatchError();
|
2015-12-09 14:23:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult FileReader::DispatchProgressEvent(const nsAString& aType) {
|
|
|
|
ProgressEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mLoaded = mTransferred;
|
|
|
|
|
|
|
|
if (mTotal != kUnknownSize) {
|
|
|
|
init.mLengthComputable = true;
|
|
|
|
init.mTotal = mTotal;
|
|
|
|
} else {
|
|
|
|
init.mLengthComputable = false;
|
|
|
|
init.mTotal = 0;
|
|
|
|
}
|
|
|
|
RefPtr<ProgressEvent> event = ProgressEvent::Constructor(this, aType, init);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
DispatchEvent(*event, rv);
|
|
|
|
return rv.StealNSResult();
|
2015-12-09 14:23:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsITimerCallback
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileReader::Notify(nsITimer* aTimer) {
|
|
|
|
nsresult rv;
|
|
|
|
mTimerIsActive = false;
|
|
|
|
|
|
|
|
if (mProgressEventWasDelayed) {
|
|
|
|
rv = DispatchProgressEvent(NS_LITERAL_STRING("progress"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
StartProgressEventTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// InputStreamCallback
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileReader::OnInputStreamReady(nsIAsyncInputStream* aStream) {
|
2015-12-09 23:52:15 +03:00
|
|
|
if (mReadyState != LOADING || aStream != mAsyncStream) {
|
2015-12-09 14:23:16 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
// We use this class to decrease the busy counter at the end of this method.
|
|
|
|
// In theory we can do it immediatelly but, for debugging reasons, we want to
|
2018-03-13 23:17:06 +03:00
|
|
|
// be 100% sure we have a workerRef when OnLoadEnd() is called.
|
2015-12-18 11:44:00 +03:00
|
|
|
FileReaderDecreaseBusyCounter RAII(this);
|
|
|
|
|
2017-03-07 10:44:54 +03:00
|
|
|
uint64_t count;
|
|
|
|
nsresult rv = aStream->Available(&count);
|
2015-12-09 14:23:16 +03:00
|
|
|
|
2017-03-07 10:44:54 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && count) {
|
|
|
|
rv = DoReadData(count);
|
2015-12-09 14:23:16 +03:00
|
|
|
|
2017-05-09 12:04:49 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = DoAsyncWait();
|
|
|
|
}
|
2015-12-09 14:23:16 +03:00
|
|
|
}
|
|
|
|
|
2017-03-07 10:44:54 +03:00
|
|
|
if (NS_FAILED(rv) || !count) {
|
2015-12-09 14:23:16 +03:00
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
return OnLoadEnd(rv);
|
|
|
|
}
|
|
|
|
|
2017-03-07 10:44:54 +03:00
|
|
|
mTransferred += count;
|
2015-12-09 14:23:16 +03:00
|
|
|
|
|
|
|
// Notify the timer is the appropriate timeframe has passed
|
|
|
|
if (mTimerIsActive) {
|
|
|
|
mProgressEventWasDelayed = true;
|
|
|
|
} else {
|
|
|
|
rv = DispatchProgressEvent(NS_LITERAL_STRING(PROGRESS_STR));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
StartProgressEventTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
// nsINamed
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileReader::GetName(nsACString& aName) {
|
|
|
|
aName.AssignLiteral("FileReader");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
nsresult FileReader::OnLoadEnd(nsresult aStatus) {
|
|
|
|
// Cancel the progress event timer
|
|
|
|
ClearProgressEventTimer();
|
|
|
|
|
|
|
|
// FileReader must be in DONE stage after an operation
|
2015-12-09 23:52:15 +03:00
|
|
|
mReadyState = DONE;
|
2015-12-09 14:23:16 +03:00
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
// Quick return, if failed.
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
FreeDataAndDispatchError(aStatus);
|
2017-01-20 15:45:35 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-12-09 14:23:16 +03:00
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
// In case we read a different number of bytes, we can assume that the
|
|
|
|
// underlying storage has changed. We should not continue.
|
|
|
|
if (mDataLen != mTotal) {
|
|
|
|
FreeDataAndDispatchError(NS_ERROR_FAILURE);
|
2015-12-09 14:23:16 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
// ArrayBuffer needs a custom handling.
|
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
OnLoadEndArrayBuffer();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// We don't do anything special for Binary format.
|
|
|
|
|
|
|
|
if (mDataFormat == FILE_AS_DATAURL) {
|
|
|
|
rv = GetAsDataURL(mBlob, mFileData, mDataLen, mResult);
|
|
|
|
} else if (mDataFormat == FILE_AS_TEXT) {
|
|
|
|
if (!mFileData && mDataLen) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else if (!mFileData) {
|
|
|
|
rv = GetAsText(mBlob, mCharset, "", mDataLen, mResult);
|
|
|
|
} else {
|
|
|
|
rv = GetAsText(mBlob, mCharset, mFileData, mDataLen, mResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
FreeDataAndDispatchError(rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-12-09 14:23:16 +03:00
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
FreeDataAndDispatchSuccess();
|
2015-12-09 14:23:16 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-12-02 16:16:59 +03:00
|
|
|
void FileReader::Abort() {
|
|
|
|
if (mReadyState == EMPTY || mReadyState == DONE) {
|
2015-12-09 14:23:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-02 16:16:59 +03:00
|
|
|
MOZ_ASSERT(mReadyState == LOADING);
|
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
ClearProgressEventTimer();
|
|
|
|
|
2015-12-09 23:52:15 +03:00
|
|
|
mReadyState = DONE;
|
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
// XXX The spec doesn't say this
|
2017-08-06 20:47:00 +03:00
|
|
|
mError = DOMException::Create(NS_ERROR_DOM_ABORT_ERR);
|
2015-12-09 14:23:16 +03:00
|
|
|
|
|
|
|
// Revert status and result attributes
|
|
|
|
SetDOMStringToNull(mResult);
|
|
|
|
mResultArrayBuffer = nullptr;
|
|
|
|
|
2018-10-30 13:50:37 +03:00
|
|
|
mAsyncStream = nullptr;
|
2015-12-09 14:23:16 +03:00
|
|
|
mBlob = nullptr;
|
|
|
|
|
|
|
|
// Clean up memory buffer
|
|
|
|
FreeFileData();
|
|
|
|
|
|
|
|
// Dispatch the events
|
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(ABORT_STR));
|
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(LOADEND_STR));
|
|
|
|
}
|
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
nsresult FileReader::IncreaseBusyCounter() {
|
2018-03-13 23:17:06 +03:00
|
|
|
if (mWeakWorkerRef && mBusyCount++ == 0) {
|
|
|
|
if (NS_WARN_IF(!mWeakWorkerRef->GetPrivate())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<FileReader> self = this;
|
|
|
|
|
|
|
|
RefPtr<StrongWorkerRef> ref =
|
|
|
|
StrongWorkerRef::Create(mWeakWorkerRef->GetPrivate(), "FileReader",
|
|
|
|
[self]() { self->Shutdown(); });
|
|
|
|
if (NS_WARN_IF(!ref)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStrongWorkerRef = ref;
|
2015-12-18 11:44:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileReader::DecreaseBusyCounter() {
|
2018-03-13 23:17:06 +03:00
|
|
|
MOZ_ASSERT_IF(mStrongWorkerRef, mBusyCount);
|
|
|
|
if (mStrongWorkerRef && --mBusyCount == 0) {
|
|
|
|
mStrongWorkerRef = nullptr;
|
2015-12-18 11:44:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileReader::Shutdown() {
|
2017-01-12 19:45:52 +03:00
|
|
|
mReadyState = DONE;
|
2015-12-18 11:44:00 +03:00
|
|
|
|
|
|
|
if (mAsyncStream) {
|
|
|
|
mAsyncStream->Close();
|
|
|
|
mAsyncStream = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-01-12 19:45:52 +03:00
|
|
|
FreeFileData();
|
|
|
|
mResultArrayBuffer = nullptr;
|
|
|
|
|
2018-03-13 23:17:06 +03:00
|
|
|
if (mWeakWorkerRef && mBusyCount != 0) {
|
|
|
|
mStrongWorkerRef = nullptr;
|
|
|
|
mWeakWorkerRef = nullptr;
|
2015-12-18 11:44:00 +03:00
|
|
|
mBusyCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|