2011-09-29 16:06:35 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-09-29 16:06:35 +00:00
|
|
|
|
|
|
|
#include "FileIOObject.h"
|
2014-10-08 16:15:23 +00:00
|
|
|
#include "mozilla/dom/File.h"
|
|
|
|
#include "mozilla/dom/ProgressEvent.h"
|
2014-03-18 04:48:21 +00:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2014-10-08 16:15:23 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2011-09-29 16:06:35 +00:00
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
|
|
|
|
#define ERROR_STR "error"
|
|
|
|
#define ABORT_STR "abort"
|
|
|
|
#define PROGRESS_STR "progress"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
const uint64_t kUnknownSize = uint64_t(-1);
|
2011-09-29 16:06:35 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(FileIOObject, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(FileIOObject, DOMEventTargetHelper)
|
2011-09-29 16:06:35 +00:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileIOObject)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITimerCallback)
|
2014-06-10 18:48:36 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStreamCallback)
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2011-09-29 16:06:35 +00:00
|
|
|
|
2013-08-02 01:29:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(FileIOObject)
|
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FileIOObject,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mProgressNotifier)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mError)
|
2011-09-29 16:06:35 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FileIOObject,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mProgressNotifier)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mError)
|
2011-09-29 16:06:35 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2014-06-11 03:59:45 +00:00
|
|
|
|
2013-01-10 07:32:03 +00:00
|
|
|
NS_IMPL_EVENT_HANDLER(FileIOObject, abort)
|
|
|
|
NS_IMPL_EVENT_HANDLER(FileIOObject, error)
|
|
|
|
NS_IMPL_EVENT_HANDLER(FileIOObject, progress)
|
2011-09-29 16:06:35 +00:00
|
|
|
|
|
|
|
FileIOObject::FileIOObject()
|
2011-10-17 14:59:28 +00:00
|
|
|
: mProgressEventWasDelayed(false),
|
|
|
|
mTimerIsActive(false),
|
2011-09-29 16:06:35 +00:00
|
|
|
mReadyState(0),
|
|
|
|
mTotal(0), mTransferred(0)
|
|
|
|
{}
|
|
|
|
|
2014-07-08 21:23:16 +00:00
|
|
|
FileIOObject::~FileIOObject()
|
|
|
|
{}
|
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
void
|
|
|
|
FileIOObject::StartProgressEventTimer()
|
|
|
|
{
|
|
|
|
if (!mProgressNotifier) {
|
|
|
|
mProgressNotifier = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
}
|
|
|
|
if (mProgressNotifier) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mProgressEventWasDelayed = false;
|
|
|
|
mTimerIsActive = true;
|
2011-09-29 16:06:35 +00:00
|
|
|
mProgressNotifier->Cancel();
|
|
|
|
mProgressNotifier->InitWithCallback(this, NS_PROGRESS_EVENT_INTERVAL,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileIOObject::ClearProgressEventTimer()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
mProgressEventWasDelayed = false;
|
|
|
|
mTimerIsActive = false;
|
2011-09-29 16:06:35 +00:00
|
|
|
if (mProgressNotifier) {
|
|
|
|
mProgressNotifier->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileIOObject::DispatchError(nsresult rv, nsAString& finalEvent)
|
|
|
|
{
|
|
|
|
// Set the status attribute, and dispatch the error event
|
|
|
|
switch (rv) {
|
|
|
|
case NS_ERROR_FILE_NOT_FOUND:
|
2013-05-18 17:52:06 +00:00
|
|
|
mError = new DOMError(GetOwner(), NS_LITERAL_STRING("NotFoundError"));
|
2011-09-29 16:06:35 +00:00
|
|
|
break;
|
|
|
|
case NS_ERROR_FILE_ACCESS_DENIED:
|
2013-05-18 17:52:06 +00:00
|
|
|
mError = new DOMError(GetOwner(), NS_LITERAL_STRING("SecurityError"));
|
2011-09-29 16:06:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-05-18 17:52:06 +00:00
|
|
|
mError = new DOMError(GetOwner(), NS_LITERAL_STRING("NotReadableError"));
|
2011-09-29 16:06:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dispatch error event to signify load failure
|
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(ERROR_STR));
|
|
|
|
DispatchProgressEvent(finalEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
FileIOObject::DispatchProgressEvent(const nsAString& aType)
|
|
|
|
{
|
2014-06-03 22:52:36 +00:00
|
|
|
ProgressEventInit init;
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mLoaded = mTransferred;
|
2011-09-29 16:06:35 +00:00
|
|
|
|
|
|
|
if (mTotal != kUnknownSize) {
|
2014-06-03 22:52:36 +00:00
|
|
|
init.mLengthComputable = true;
|
|
|
|
init.mTotal = mTotal;
|
2011-09-29 16:06:35 +00:00
|
|
|
} else {
|
2014-06-03 22:52:36 +00:00
|
|
|
init.mLengthComputable = false;
|
|
|
|
init.mTotal = 0;
|
2011-09-29 16:06:35 +00:00
|
|
|
}
|
2014-06-03 22:52:36 +00:00
|
|
|
nsRefPtr<ProgressEvent> event =
|
|
|
|
ProgressEvent::Constructor(this, aType, init);
|
|
|
|
event->SetTrusted(true);
|
2011-09-29 16:06:35 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
2011-09-29 16:06:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsITimerCallback
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileIOObject::Notify(nsITimer* aTimer)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2011-10-17 14:59:28 +00:00
|
|
|
mTimerIsActive = false;
|
2011-09-29 16:06:35 +00:00
|
|
|
|
|
|
|
if (mProgressEventWasDelayed) {
|
|
|
|
rv = DispatchProgressEvent(NS_LITERAL_STRING("progress"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
StartProgressEventTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-10 18:48:36 +00:00
|
|
|
// InputStreamCallback
|
2011-09-29 16:06:35 +00:00
|
|
|
NS_IMETHODIMP
|
2014-06-10 18:48:36 +00:00
|
|
|
FileIOObject::OnInputStreamReady(nsIAsyncInputStream* aStream)
|
2011-09-29 16:06:35 +00:00
|
|
|
{
|
2014-06-10 18:48:36 +00:00
|
|
|
if (mReadyState != 1 || aStream != mAsyncStream) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-09-29 16:06:35 +00:00
|
|
|
|
2014-06-10 18:48:36 +00:00
|
|
|
uint64_t aCount;
|
|
|
|
nsresult rv = aStream->Available(&aCount);
|
2011-09-29 16:06:35 +00:00
|
|
|
|
2014-06-10 18:48:36 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && aCount) {
|
|
|
|
rv = DoReadData(aStream, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = DoAsyncWait(aStream);
|
|
|
|
}
|
|
|
|
|
2014-07-01 14:17:36 +00:00
|
|
|
if (NS_FAILED(rv) || !aCount) {
|
2014-06-10 18:48:36 +00:00
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
return OnLoadEnd(rv);
|
|
|
|
}
|
2011-09-29 16:06:35 +00:00
|
|
|
|
|
|
|
mTransferred += aCount;
|
|
|
|
|
|
|
|
//Notify the timer is the appropriate timeframe has passed
|
|
|
|
if (mTimerIsActive) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mProgressEventWasDelayed = true;
|
2011-09-29 16:06:35 +00:00
|
|
|
} else {
|
|
|
|
rv = DispatchProgressEvent(NS_LITERAL_STRING(PROGRESS_STR));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
StartProgressEventTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-10 18:48:36 +00:00
|
|
|
nsresult
|
|
|
|
FileIOObject::OnLoadEnd(nsresult aStatus)
|
2011-09-29 16:06:35 +00:00
|
|
|
{
|
|
|
|
// Cancel the progress event timer
|
|
|
|
ClearProgressEventTimer();
|
|
|
|
|
|
|
|
// FileIOObject must be in DONE stage after an operation
|
|
|
|
mReadyState = 2;
|
|
|
|
|
|
|
|
nsString successEvent, termEvent;
|
2014-06-10 18:48:36 +00:00
|
|
|
nsresult rv = DoOnLoadEnd(aStatus, successEvent, termEvent);
|
2011-09-29 16:06:35 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Set the status field as appropriate
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
DispatchError(aStatus, termEvent);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dispatch event to signify end of a successful operation
|
|
|
|
DispatchProgressEvent(successEvent);
|
|
|
|
DispatchProgressEvent(termEvent);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-10 18:48:36 +00:00
|
|
|
nsresult
|
|
|
|
FileIOObject::DoAsyncWait(nsIAsyncInputStream* aStream)
|
|
|
|
{
|
|
|
|
return aStream->AsyncWait(this,
|
|
|
|
/* aFlags*/ 0,
|
|
|
|
/* aRequestedCount */ 0,
|
|
|
|
NS_GetCurrentThread());
|
|
|
|
}
|
|
|
|
|
2013-04-13 07:06:31 +00:00
|
|
|
void
|
|
|
|
FileIOObject::Abort(ErrorResult& aRv)
|
2011-09-29 16:06:35 +00:00
|
|
|
{
|
2012-03-11 08:47:38 +00:00
|
|
|
if (mReadyState != 1) {
|
|
|
|
// XXX The spec doesn't say this
|
2013-04-13 07:06:31 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_FILE_ABORT_ERR);
|
|
|
|
return;
|
2012-03-11 08:47:38 +00:00
|
|
|
}
|
2011-09-29 16:06:35 +00:00
|
|
|
|
|
|
|
ClearProgressEventTimer();
|
|
|
|
|
|
|
|
mReadyState = 2; // There are DONE constants on multiple interfaces,
|
|
|
|
// but they all have value 2.
|
2012-03-11 08:47:38 +00:00
|
|
|
// XXX The spec doesn't say this
|
2013-05-18 17:52:06 +00:00
|
|
|
mError = new DOMError(GetOwner(), NS_LITERAL_STRING("AbortError"));
|
2011-09-29 16:06:35 +00:00
|
|
|
|
|
|
|
nsString finalEvent;
|
2013-04-13 07:06:31 +00:00
|
|
|
DoAbort(finalEvent);
|
2011-09-29 16:06:35 +00:00
|
|
|
|
|
|
|
// Dispatch the events
|
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(ABORT_STR));
|
|
|
|
DispatchProgressEvent(finalEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|