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