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"
|
|
|
|
#include "nsDOMFile.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2011-09-29 09:06:35 -07:00
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDOMProgressEvent.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsEventDispatcher.h"
|
|
|
|
|
|
|
|
#define ERROR_STR "error"
|
|
|
|
#define ABORT_STR "abort"
|
|
|
|
#define PROGRESS_STR "progress"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
const PRUint64 kUnknownSize = PRUint64(-1);
|
|
|
|
|
2012-02-07 18:53:33 -08:00
|
|
|
NS_IMPL_ADDREF_INHERITED(FileIOObject, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(FileIOObject, nsDOMEventTargetHelper)
|
2011-09-29 09:06:35 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileIOObject)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITimerCallback)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
2012-02-07 18:53:33 -08:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
2011-09-29 09:06:35 -07:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(FileIOObject)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FileIOObject,
|
2012-02-07 18:53:33 -08:00
|
|
|
nsDOMEventTargetHelper)
|
2011-09-29 09:06:35 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mProgressNotifier)
|
2011-09-29 12:18:58 -07:00
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(abort)
|
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(error)
|
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(progress)
|
2012-06-05 06:02:04 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mError)
|
2011-09-29 09:06:35 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FileIOObject,
|
2012-02-07 18:53:33 -08:00
|
|
|
nsDOMEventTargetHelper)
|
2011-09-29 09:06:35 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mProgressNotifier)
|
2011-09-29 12:18:58 -07:00
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(abort)
|
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(error)
|
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(progress)
|
2012-06-05 06:02:04 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mError)
|
2011-09-29 09:06:35 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
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)
|
|
|
|
{}
|
|
|
|
|
|
|
|
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:
|
2012-03-11 00:47:38 -08:00
|
|
|
mError = DOMError::CreateWithName(NS_LITERAL_STRING("NotFoundError"));
|
2011-09-29 09:06:35 -07:00
|
|
|
break;
|
|
|
|
case NS_ERROR_FILE_ACCESS_DENIED:
|
2012-03-11 00:47:38 -08:00
|
|
|
mError = DOMError::CreateWithName(NS_LITERAL_STRING("SecurityError"));
|
2011-09-29 09:06:35 -07:00
|
|
|
break;
|
|
|
|
default:
|
2012-03-11 00:47:38 -08:00
|
|
|
mError = DOMError::CreateWithName(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)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsresult rv = nsEventDispatcher::CreateEvent(nullptr, nullptr,
|
2011-09-29 09:06:35 -07:00
|
|
|
NS_LITERAL_STRING("ProgressEvent"),
|
|
|
|
getter_AddRefs(event));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
event->SetTrusted(true);
|
2011-09-29 09:06:35 -07:00
|
|
|
nsCOMPtr<nsIDOMProgressEvent> progress = do_QueryInterface(event);
|
|
|
|
NS_ENSURE_TRUE(progress, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
bool known;
|
|
|
|
PRUint64 size;
|
|
|
|
if (mTotal != kUnknownSize) {
|
2011-10-17 07:59:28 -07:00
|
|
|
known = true;
|
2011-09-29 09:06:35 -07:00
|
|
|
size = mTotal;
|
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
known = false;
|
2011-09-29 09:06:35 -07:00
|
|
|
size = 0;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = progress->InitProgressEvent(aType, false, false, known,
|
2011-09-29 09:06:35 -07:00
|
|
|
mTransferred, size);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIStreamListener
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileIOObject::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext)
|
|
|
|
{
|
|
|
|
return DoOnStartRequest(aRequest, aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileIOObject::DoOnStartRequest(nsIRequest *request, nsISupports *ctxt)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileIOObject::OnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *aInputStream,
|
|
|
|
PRUint32 aOffset,
|
|
|
|
PRUint32 aCount)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
rv = DoOnDataAvailable(aRequest, aContext, aInputStream, aOffset, aCount);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileIOObject::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
// If we're here as a result of a call from Abort(),
|
|
|
|
// simply ignore the request.
|
|
|
|
if (aRequest != mChannel)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Cancel the progress event timer
|
|
|
|
ClearProgressEventTimer();
|
|
|
|
|
|
|
|
// FileIOObject must be in DONE stage after an operation
|
|
|
|
mReadyState = 2;
|
|
|
|
|
|
|
|
nsString successEvent, termEvent;
|
|
|
|
nsresult rv = DoOnStopRequest(aRequest, aContext, aStatus,
|
|
|
|
successEvent, termEvent);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileIOObject::Abort()
|
|
|
|
{
|
2012-03-11 00:47:38 -08:00
|
|
|
if (mReadyState != 1) {
|
|
|
|
// XXX The spec doesn't say this
|
2011-09-29 09:06:35 -07:00
|
|
|
return NS_ERROR_DOM_FILE_ABORT_ERR;
|
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
|
|
|
|
mError = DOMError::CreateWithName(NS_LITERAL_STRING("AbortError"));
|
2011-09-29 09:06:35 -07:00
|
|
|
|
|
|
|
nsString finalEvent;
|
|
|
|
nsresult rv = DoAbort(finalEvent);
|
|
|
|
|
|
|
|
// Dispatch the events
|
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(ABORT_STR));
|
|
|
|
DispatchProgressEvent(finalEvent);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FileIOObject::GetReadyState(PRUint16 *aReadyState)
|
|
|
|
{
|
|
|
|
*aReadyState = mReadyState;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-03-11 00:47:38 -08:00
|
|
|
FileIOObject::GetError(nsIDOMDOMError** aError)
|
2011-09-29 09:06:35 -07:00
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aError = mError);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|