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"
|
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIStreamTransportService.h"
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2012-12-06 02:20:57 +04:00
|
|
|
#include "mozilla/Base64.h"
|
2016-01-26 13:19:15 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2015-12-18 11:44:00 +03:00
|
|
|
#include "mozilla/dom/DOMError.h"
|
2012-11-08 03:04:22 +04:00
|
|
|
#include "mozilla/dom/EncodingUtils.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"
|
2015-12-18 11:44:00 +03:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#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 "nsNetCID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "xpcpublic.h"
|
2011-05-27 08:53:03 +04:00
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "WorkerScope.h"
|
2014-06-10 22:48:36 +04:00
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
using namespace workers;
|
|
|
|
|
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
|
|
|
|
2014-06-10 22:48:36 +04:00
|
|
|
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
|
|
|
|
|
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,
|
2014-04-01 10:13:50 +04:00
|
|
|
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
|
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(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)
|
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;
|
|
|
|
public:
|
|
|
|
explicit FileReaderDecreaseBusyCounter(FileReader* aFileReader)
|
|
|
|
: mFileReader(aFileReader)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~FileReaderDecreaseBusyCounter()
|
|
|
|
{
|
|
|
|
mFileReader->DecreaseBusyCounter();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-01 04:50:44 +04:00
|
|
|
void
|
2015-12-09 14:22:40 +03:00
|
|
|
FileReader::RootResultArrayBuffer()
|
2011-07-01 04:50:44 +04:00
|
|
|
{
|
2013-08-17 00:10:17 +04:00
|
|
|
mozilla::HoldJSObjects(this);
|
2011-07-01 04:50:44 +04:00
|
|
|
}
|
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
//FileReader constructors/initializers
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2016-01-20 21:17:57 +03:00
|
|
|
FileReader::FileReader(nsIGlobalObject* aGlobal,
|
2015-12-18 11:44:00 +03:00
|
|
|
WorkerPrivate* aWorkerPrivate)
|
2016-01-20 21:17:57 +03:00
|
|
|
: 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)
|
|
|
|
, mWorkerPrivate(aWorkerPrivate)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2016-01-20 21:17:57 +03:00
|
|
|
MOZ_ASSERT(aGlobal);
|
|
|
|
MOZ_ASSERT(NS_IsMainThread() == !mWorkerPrivate);
|
2017-05-08 12:16:20 +03:00
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
mTarget = aGlobal->EventTargetFor(TaskCategory::Other);
|
|
|
|
} else {
|
|
|
|
mTarget = do_GetCurrentThread();
|
|
|
|
}
|
|
|
|
|
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()
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2015-12-09 14:22:40 +03:00
|
|
|
/* static */ already_AddRefed<FileReader>
|
|
|
|
FileReader::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2016-01-20 21:17:57 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
2015-12-18 11:44:00 +03:00
|
|
|
WorkerPrivate* workerPrivate = nullptr;
|
2015-12-09 23:52:15 +03:00
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
JSContext* cx = aGlobal.Context();
|
|
|
|
workerPrivate = GetWorkerPrivateFromContext(cx);
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
}
|
|
|
|
|
2016-01-20 21:17:57 +03:00
|
|
|
RefPtr<FileReader> fileReader = new FileReader(global, workerPrivate);
|
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);
|
|
|
|
}
|
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
void
|
2015-12-09 23:52:15 +03:00
|
|
|
FileReader::GetResult(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aResult,
|
2015-12-09 14:22:40 +03:00
|
|
|
ErrorResult& aRv)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
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) {
|
2015-12-09 23:52:15 +03:00
|
|
|
if (mReadyState == DONE && mResultArrayBuffer) {
|
2013-10-26 20:19:05 +04:00
|
|
|
result.setObject(*mResultArrayBuffer);
|
2011-07-01 04:50:44 +04:00
|
|
|
} else {
|
2013-10-26 20:19:05 +04:00
|
|
|
result.setNull();
|
2011-07-01 04:50:44 +04:00
|
|
|
}
|
2015-12-09 23:52:15 +03:00
|
|
|
|
2013-10-26 20:19:05 +04:00
|
|
|
if (!JS_WrapValue(aCx, &result)) {
|
2015-12-09 23:52:15 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
2012-03-24 04:04:55 +04:00
|
|
|
}
|
2015-12-09 23:52:15 +03:00
|
|
|
|
2014-01-09 21:39:36 +04:00
|
|
|
aResult.set(result);
|
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
|
|
|
|
2011-07-01 04:50:44 +04:00
|
|
|
nsString tmpResult = mResult;
|
2014-01-09 21:39:36 +04:00
|
|
|
if (!xpc::StringToJsval(aCx, tmpResult, aResult)) {
|
2015-12-09 23:52:15 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
2011-07-01 04:50:44 +04:00
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
static nsresult
|
2009-11-21 14:56:31 +03:00
|
|
|
ReadFuncBinaryString(nsIInputStream* in,
|
|
|
|
void* closure,
|
|
|
|
const char* fromRawSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t toOffset,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t *writeCount)
|
2009-11-21 14:56:31 +03:00
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* dest = static_cast<char16_t*>(closure) + toOffset;
|
|
|
|
char16_t* end = dest + count;
|
2009-11-21 14:56:31 +03:00
|
|
|
const unsigned char* source = (const unsigned char*)fromRawSegment;
|
|
|
|
while (dest != end) {
|
|
|
|
*dest = *source;
|
|
|
|
++dest;
|
|
|
|
++source;
|
|
|
|
}
|
|
|
|
*writeCount = count;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
void
|
|
|
|
FileReader::OnLoadEndArrayBuffer()
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2017-01-24 14:12:15 +03:00
|
|
|
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();
|
|
|
|
|
|
|
|
mResultArrayBuffer = JS_NewArrayBufferWithContents(cx, mDataLen, mFileData);
|
|
|
|
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;
|
|
|
|
JSFlatString* name = js::GetErrorTypeName(cx, er->exnType);
|
|
|
|
if (name) {
|
|
|
|
AssignJSFlatString(errorName, name);
|
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2017-01-24 14:12:15 +03:00
|
|
|
mError =
|
|
|
|
new DOMError(GetOwner(), errorName,
|
|
|
|
NS_ConvertUTF8toUTF16(er->message().c_str()));
|
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
|
|
|
}
|
|
|
|
|
2014-06-10 22:48:36 +04:00
|
|
|
nsresult
|
2015-12-18 11:44:00 +03:00
|
|
|
FileReader::DoAsyncWait()
|
2014-06-10 22:48:36 +04:00
|
|
|
{
|
2015-12-18 11:44:00 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
uint32_t oldLen = mResult.Length();
|
2016-12-13 04:25:37 +03:00
|
|
|
MOZ_ASSERT(mResult.Length() == mDataLen, "unexpected mResult length");
|
2014-06-10 22:48:36 +04:00
|
|
|
if (uint64_t(oldLen) + aCount > UINT32_MAX)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
char16_t *buf = nullptr;
|
2015-01-28 12:00:40 +03:00
|
|
|
mResult.GetMutableData(&buf, oldLen + aCount, fallible);
|
2014-06-10 22:48:36 +04:00
|
|
|
NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2017-04-24 13:09:40 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// nsFileStreams do not implement ReadSegment. In case here we are dealing
|
|
|
|
// with a nsIAsyncInputStream, in content process, we need to wrap a
|
|
|
|
// nsIBufferedInputStream around it.
|
|
|
|
if (!mBufferedStream) {
|
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(mBufferedStream),
|
|
|
|
mAsyncStream, 8192);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mBufferedStream->ReadSegments(ReadFuncBinaryString, buf + oldLen,
|
|
|
|
aCount, &bytesRead);
|
2017-03-07 10:44:54 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-05-04 15:45:34 +03:00
|
|
|
mResult.Truncate(oldLen + bytesRead);
|
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
|
|
|
|
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
|
|
|
|
|
2013-04-13 11:06:31 +04:00
|
|
|
void
|
2015-12-09 14:22:40 +03:00
|
|
|
FileReader::ReadFileContent(Blob& aBlob,
|
|
|
|
const nsAString &aCharset,
|
|
|
|
eDataFormat aDataFormat,
|
|
|
|
ErrorResult& aRv)
|
2009-11-25 04:53:17 +03:00
|
|
|
{
|
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;
|
2017-04-24 13:09:40 +03:00
|
|
|
mBufferedStream = nullptr;
|
2016-12-01 20:57:36 +03:00
|
|
|
|
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
|
|
|
|
2014-06-10 22:48:36 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(kStreamTransportServiceCID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
2010-09-05 22:00:05 +04:00
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2014-06-10 22:48:36 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2015-05-19 17:36:37 +03:00
|
|
|
mBlob->GetInternalStream(getter_AddRefs(stream), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2014-06-10 22:48:36 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-24 13:09:40 +03:00
|
|
|
mAsyncStream = do_QueryInterface(stream);
|
|
|
|
if (!mAsyncStream) {
|
|
|
|
nsCOMPtr<nsITransport> transport;
|
|
|
|
aRv = sts->CreateInputTransport(stream,
|
|
|
|
/* aStartOffset */ 0,
|
|
|
|
/* aReadLimit */ -1,
|
|
|
|
/* aCloseWhenDone */ true,
|
|
|
|
getter_AddRefs(transport));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-10 22:48:36 +04:00
|
|
|
|
2017-04-24 13:09:40 +03:00
|
|
|
nsCOMPtr<nsIInputStream> wrapper;
|
|
|
|
aRv = transport->OpenInputStream(/* aFlags */ 0,
|
|
|
|
/* aSegmentSize */ 0,
|
|
|
|
/* aSegmentCount */ 0,
|
|
|
|
getter_AddRefs(wrapper));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAsyncStream = do_QueryInterface(wrapper);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 11:44:00 +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));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-12-09 14:22:40 +03:00
|
|
|
FileReader::GetAsText(Blob *aBlob,
|
|
|
|
const nsACString &aCharset,
|
|
|
|
const char *aFileData,
|
|
|
|
uint32_t aDataLen,
|
|
|
|
nsAString& aResult)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2013-12-17 14:47:25 +04:00
|
|
|
// 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;
|
2015-05-12 15:09:51 +03:00
|
|
|
aBlob->GetType(type16);
|
2013-12-17 14:47:25 +04: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");
|
|
|
|
}
|
|
|
|
}
|
2012-11-08 03:04:22 +04:00
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2013-12-17 14:47:25 +04:00
|
|
|
nsDependentCSubstring data(aFileData, aDataLen);
|
|
|
|
return nsContentUtils::ConvertStringFromEncoding(encoding, data, aResult);
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-12-09 14:22:40 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-13 11:06:31 +04:00
|
|
|
/* virtual */ JSObject*
|
2015-12-09 14:22:40 +03:00
|
|
|
FileReader::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-04-13 11:06:31 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return FileReaderBinding::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) {
|
|
|
|
mProgressNotifier = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mProgressNotifier) {
|
|
|
|
mProgressEventWasDelayed = false;
|
|
|
|
mTimerIsActive = true;
|
|
|
|
mProgressNotifier->Cancel();
|
2015-12-18 11:44:00 +03:00
|
|
|
mProgressNotifier->SetTarget(mTarget);
|
2015-12-09 14:23:16 +03:00
|
|
|
mProgressNotifier->InitWithCallback(this, NS_PROGRESS_EVENT_INTERVAL,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileReader::ClearProgressEventTimer()
|
|
|
|
{
|
|
|
|
mProgressEventWasDelayed = false;
|
|
|
|
mTimerIsActive = false;
|
|
|
|
if (mProgressNotifier) {
|
|
|
|
mProgressNotifier->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-24 14:12:15 +03:00
|
|
|
FileReader::FreeDataAndDispatchSuccess()
|
|
|
|
{
|
|
|
|
FreeFileData();
|
|
|
|
mResult.SetIsVoid(false);
|
|
|
|
mAsyncStream = nullptr;
|
2017-04-24 13:09:40 +03:00
|
|
|
mBufferedStream = nullptr;
|
2017-01-24 14:12:15 +03:00
|
|
|
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;
|
2017-04-24 13:09:40 +03:00
|
|
|
mBufferedStream = nullptr;
|
2017-01-24 14:12:15 +03:00
|
|
|
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:
|
|
|
|
mError = new DOMError(GetOwner(), NS_LITERAL_STRING("NotFoundError"));
|
|
|
|
break;
|
|
|
|
case NS_ERROR_FILE_ACCESS_DENIED:
|
|
|
|
mError = new DOMError(GetOwner(), NS_LITERAL_STRING("SecurityError"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mError = new DOMError(GetOwner(), NS_LITERAL_STRING("NotReadableError"));
|
|
|
|
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);
|
|
|
|
|
|
|
|
return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-06-23 11:53:14 +03:00
|
|
|
// be 100% sure we have a workerHolder 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-12-02 16:16:59 +03:00
|
|
|
FileReader::Abort()
|
2015-12-09 14:23:16 +03:00
|
|
|
{
|
2016-12-02 16:16:59 +03:00
|
|
|
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
|
|
|
|
mError = new DOMError(GetOwner(), NS_LITERAL_STRING("AbortError"));
|
|
|
|
|
|
|
|
// Revert status and result attributes
|
|
|
|
SetDOMStringToNull(mResult);
|
|
|
|
mResultArrayBuffer = nullptr;
|
|
|
|
|
|
|
|
mAsyncStream = nullptr;
|
2017-04-24 13:09:40 +03:00
|
|
|
mBufferedStream = 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()
|
|
|
|
{
|
|
|
|
if (mWorkerPrivate && mBusyCount++ == 0 &&
|
2016-08-18 17:11:04 +03:00
|
|
|
!HoldWorker(mWorkerPrivate, Closing)) {
|
2015-12-18 11:44:00 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileReader::DecreaseBusyCounter()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT_IF(mWorkerPrivate, mBusyCount);
|
|
|
|
if (mWorkerPrivate && --mBusyCount == 0) {
|
2016-06-23 11:53:14 +03:00
|
|
|
ReleaseWorker();
|
2015-12-18 11:44:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-03-28 20:28:14 +03:00
|
|
|
FileReader::Notify(Status aStatus)
|
2015-12-18 11:44:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (aStatus > Running) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-24 13:09:40 +03:00
|
|
|
if (mBufferedStream) {
|
|
|
|
mBufferedStream->Close();
|
|
|
|
mBufferedStream = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-01-12 19:45:52 +03:00
|
|
|
FreeFileData();
|
|
|
|
mResultArrayBuffer = nullptr;
|
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
if (mWorkerPrivate && mBusyCount != 0) {
|
2016-06-23 11:53:14 +03:00
|
|
|
ReleaseWorker();
|
2015-12-18 11:44:00 +03:00
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
mBusyCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 14:23:16 +03:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|