Bug 657994: Factor out common bits of FileReader. r=smaug

This commit is contained in:
Kyle Huey 2011-09-29 12:06:35 -04:00
parent f4fb046f58
commit 4152a72565
9 changed files with 590 additions and 259 deletions

View File

@ -86,9 +86,62 @@ public:
return static_cast<nsDOMEventTargetWrapperCache*>(target);
}
void Init(JSContext* aCx = nsnull);
protected:
nsDOMEventTargetWrapperCache() : nsDOMEventTargetHelper(), nsWrapperCache() {}
virtual ~nsDOMEventTargetWrapperCache();
};
#define NS_DECL_EVENT_HANDLER(_event) \
protected: \
nsRefPtr<nsDOMEventListenerWrapper> mOn##_event##Listener; \
public:
#define NS_DECL_AND_IMPL_EVENT_HANDLER(_event) \
protected: \
nsRefPtr<nsDOMEventListenerWrapper> mOn##_event##Listener; \
public: \
NS_IMETHOD GetOn##_event(nsIDOMEventListener** a##_event) \
{ \
return GetInnerEventListener(mOn##_event##Listener, a##_event); \
} \
NS_IMETHOD SetOn##_event(nsIDOMEventListener* a##_event) \
{ \
return RemoveAddEventListener(NS_LITERAL_STRING(#_event), \
mOn##_event##Listener, a##_event); \
}
#define NS_IMPL_EVENT_HANDLER(_class, _event) \
NS_IMETHODIMP \
_class::GetOn##_event(nsIDOMEventListener** a##_event) \
{ \
return GetInnerEventListener(mOn##_event##Listener, a##_event); \
} \
NS_IMETHODIMP \
_class::SetOn##_event(nsIDOMEventListener* a##_event) \
{ \
return RemoveAddEventListener(NS_LITERAL_STRING(#_event), \
mOn##_event##Listener, a##_event); \
}
#define NS_IMPL_FORWARD_EVENT_HANDLER(_class, _event, _baseclass) \
NS_IMETHODIMP \
_class::GetOn##_event(nsIDOMEventListener** a##_event) \
{ \
return _baseclass::GetOn##_event(a##_event); \
} \
NS_IMETHODIMP \
_class::SetOn##_event(nsIDOMEventListener* a##_event) \
{ \
return _baseclass::SetOn##_event(a##_event); \
}
#define NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(_event) \
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOn##_event##Listener)
#define NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(_event) \
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOn##_event##Listener)
#endif // nsDOMEventTargetWrapperCache_h__

View File

@ -41,8 +41,8 @@ interface nsIDOMEventListener;
interface nsIDOMBlob;
interface nsIDOMFileError;
[scriptable, uuid(3d77e784-1459-4206-b8a2-0855d826f569)]
interface nsIDOMFileReader : nsISupports
[scriptable, builtinclass, uuid(fc316500-87c4-411e-ab75-dd62468f4174)]
interface nsIDOMFileReader : nsIDOMEventTarget
{
[implicit_jscontext]
void readAsArrayBuffer(in nsIDOMBlob filedata);
@ -60,6 +60,13 @@ interface nsIDOMFileReader : nsISupports
[implicit_jscontext]
readonly attribute jsval result;
readonly attribute nsIDOMFileError error;
attribute nsIDOMEventListener onloadstart;
attribute nsIDOMEventListener onprogress;
attribute nsIDOMEventListener onload;
attribute nsIDOMEventListener onabort;
attribute nsIDOMEventListener onerror;
attribute nsIDOMEventListener onloadend;
};
%{ C++

View File

@ -0,0 +1,300 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozila.org code.
*
* The Initial Developer of the Original Code is the Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Kyle Huey <me@kylehuey.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "FileIOObject.h"
#include "nsDOMFile.h"
#include "nsDOMError.h"
#include "nsIPrivateDOMEvent.h"
#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);
NS_IMPL_ADDREF_INHERITED(FileIOObject, nsDOMEventTargetWrapperCache)
NS_IMPL_RELEASE_INHERITED(FileIOObject, nsDOMEventTargetWrapperCache)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileIOObject)
NS_INTERFACE_MAP_ENTRY(nsITimerCallback)
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetWrapperCache)
NS_IMPL_CYCLE_COLLECTION_CLASS(FileIOObject)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FileIOObject,
nsDOMEventTargetWrapperCache)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mProgressNotifier)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(abort)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(error)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(progress)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FileIOObject,
nsDOMEventTargetWrapperCache)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mProgressNotifier)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(abort)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(error)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(progress)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
FileIOObject::FileIOObject()
: mProgressEventWasDelayed(PR_FALSE),
mTimerIsActive(PR_FALSE),
mReadyState(0),
mTotal(0), mTransferred(0)
{}
FileIOObject::~FileIOObject()
{
if (mListenerManager)
mListenerManager->Disconnect();
}
void
FileIOObject::StartProgressEventTimer()
{
if (!mProgressNotifier) {
mProgressNotifier = do_CreateInstance(NS_TIMER_CONTRACTID);
}
if (mProgressNotifier) {
mProgressEventWasDelayed = PR_FALSE;
mTimerIsActive = PR_TRUE;
mProgressNotifier->Cancel();
mProgressNotifier->InitWithCallback(this, NS_PROGRESS_EVENT_INTERVAL,
nsITimer::TYPE_ONE_SHOT);
}
}
void
FileIOObject::ClearProgressEventTimer()
{
mProgressEventWasDelayed = PR_FALSE;
mTimerIsActive = PR_FALSE;
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:
mError = new nsDOMFileError(nsIDOMFileError::NOT_FOUND_ERR);
break;
case NS_ERROR_FILE_ACCESS_DENIED:
mError = new nsDOMFileError(nsIDOMFileError::SECURITY_ERR);
break;
default:
mError = new nsDOMFileError(nsIDOMFileError::NOT_READABLE_ERR);
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;
nsresult rv = nsEventDispatcher::CreateEvent(nsnull, nsnull,
NS_LITERAL_STRING("ProgressEvent"),
getter_AddRefs(event));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIPrivateDOMEvent> privevent(do_QueryInterface(event));
NS_ENSURE_TRUE(privevent, NS_ERROR_UNEXPECTED);
privevent->SetTrusted(PR_TRUE);
nsCOMPtr<nsIDOMProgressEvent> progress = do_QueryInterface(event);
NS_ENSURE_TRUE(progress, NS_ERROR_UNEXPECTED);
bool known;
PRUint64 size;
if (mTotal != kUnknownSize) {
known = PR_TRUE;
size = mTotal;
} else {
known = PR_FALSE;
size = 0;
}
rv = progress->InitProgressEvent(aType, PR_FALSE, PR_FALSE, known,
mTransferred, size);
NS_ENSURE_SUCCESS(rv, rv);
return DispatchDOMEvent(nsnull, event, nsnull, nsnull);
}
// nsITimerCallback
NS_IMETHODIMP
FileIOObject::Notify(nsITimer* aTimer)
{
nsresult rv;
mTimerIsActive = PR_FALSE;
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) {
mProgressEventWasDelayed = PR_TRUE;
} 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()
{
if (mReadyState != 1)
return NS_ERROR_DOM_FILE_ABORT_ERR;
ClearProgressEventTimer();
mReadyState = 2; // There are DONE constants on multiple interfaces,
// but they all have value 2.
mError = new nsDOMFileError(nsIDOMFileError::ABORT_ERR);
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
FileIOObject::GetError(nsIDOMFileError** aError)
{
NS_IF_ADDREF(*aError = mError);
return NS_OK;
}
} // namespace dom
} // namespace mozilla

View File

@ -0,0 +1,124 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozila.org code.
*
* The Initial Developer of the Original Code is the Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Kyle Huey <me@kylehuey.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef FileIOObject_h__
#define FileIOObject_h__
#include "nsIDOMEventTarget.h"
#include "nsDOMEventTargetWrapperCache.h"
#include "nsIChannel.h"
#include "nsIFile.h"
#include "nsIDOMFile.h"
#include "nsIStreamListener.h"
#include "nsITimer.h"
#include "nsCOMPtr.h"
#define NS_PROGRESS_EVENT_INTERVAL 50
namespace mozilla {
namespace dom {
extern const PRUint64 kUnknownSize;
// A common base class for FileReader and FileSaver
class FileIOObject : public nsDOMEventTargetWrapperCache,
public nsIStreamListener,
public nsITimerCallback
{
public:
FileIOObject();
~FileIOObject();
NS_DECL_ISUPPORTS_INHERITED
// Common methods
NS_METHOD Abort();
NS_METHOD GetReadyState(PRUint16* aReadyState);
NS_METHOD GetError(nsIDOMFileError** aError);
NS_DECL_AND_IMPL_EVENT_HANDLER(abort);
NS_DECL_AND_IMPL_EVENT_HANDLER(error);
NS_DECL_AND_IMPL_EVENT_HANDLER(progress);
NS_DECL_NSITIMERCALLBACK
NS_DECL_NSISTREAMLISTENER
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FileIOObject,
nsDOMEventTargetWrapperCache)
protected:
// Implemented by the derived class to do whatever it needs to do for abort
NS_IMETHOD DoAbort(nsAString& aEvent) = 0;
// for onStartRequest (this has a default impl since FileReader doesn't need
// special handling
NS_IMETHOD DoOnStartRequest(nsIRequest *aRequest, nsISupports *aContext);
// for onStopRequest
NS_IMETHOD DoOnStopRequest(nsIRequest *aRequest, nsISupports *aContext,
nsresult aStatus, nsAString& aSuccessEvent,
nsAString& aTerminationEvent) = 0;
// and for onDataAvailable
NS_IMETHOD DoOnDataAvailable(nsIRequest *aRequest, nsISupports *aContext,
nsIInputStream *aInputStream, PRUint32 aOffset,
PRUint32 aCount) = 0;
void StartProgressEventTimer();
void ClearProgressEventTimer();
void DispatchError(nsresult rv, nsAString& finalEvent);
nsresult DispatchProgressEvent(const nsAString& aType);
nsCOMPtr<nsITimer> mProgressNotifier;
bool mProgressEventWasDelayed;
bool mTimerIsActive;
nsCOMPtr<nsIDOMFileError> mError;
nsCOMPtr<nsIChannel> mChannel;
PRUint16 mReadyState;
PRUint64 mTotal;
PRUint64 mTransferred;
};
} // namespace dom
} // namespace mozilla
#endif

View File

@ -155,6 +155,7 @@ CPPSRCS = \
nsInProcessTabChildGlobal.cpp \
ThirdPartyUtil.cpp \
nsEventSource.cpp \
FileIOObject.cpp \
$(NULL)
# Are we targeting x86-32 or x86-64? If so, we want to include SSE2 code for

View File

@ -40,6 +40,9 @@
#include "nsContentUtils.h"
#include "nsDOMEventTargetWrapperCache.h"
#include "nsIDocument.h"
#include "nsIJSContextStack.h"
#include "nsServiceManagerUtils.h"
#include "nsDOMJSUtils.h"
nsDOMEventTargetWrapperCache::~nsDOMEventTargetWrapperCache()
{
@ -69,3 +72,29 @@ NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
NS_IMPL_ADDREF_INHERITED(nsDOMEventTargetWrapperCache, nsDOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(nsDOMEventTargetWrapperCache, nsDOMEventTargetHelper)
void
nsDOMEventTargetWrapperCache::Init(JSContext* aCx)
{
// Set the original mScriptContext and mPrincipal, if available
JSContext* cx = aCx;
if (!cx) {
nsIJSContextStack* stack = nsContentUtils::ThreadJSContextStack();
if (!stack)
return;
if (NS_FAILED(stack->Peek(&cx)) || !cx)
return;
}
NS_ASSERTION(cx, "Should have returned earlier ...");
nsIScriptContext* context = GetScriptContextFromJSContext(cx);
if (context) {
mScriptContext = context;
nsCOMPtr<nsPIDOMWindow> window =
do_QueryInterface(context->GetGlobalObject());
if (window)
mOwner = window->GetCurrentInnerWindow();
}
}

View File

@ -72,7 +72,6 @@
#include "nsIDOMClassInfo.h"
#include "nsCExternalHandlerService.h"
#include "nsIStreamConverterService.h"
#include "nsEventDispatcher.h"
#include "nsCycleCollectionParticipant.h"
#include "nsLayoutStatics.h"
#include "nsIScriptObjectPrincipal.h"
@ -85,38 +84,36 @@
using namespace mozilla;
#define LOAD_STR "load"
#define ERROR_STR "error"
#define ABORT_STR "abort"
#define LOADSTART_STR "loadstart"
#define PROGRESS_STR "progress"
#define UPLOADPROGRESS_STR "uploadprogress"
#define LOADEND_STR "loadend"
#define NS_PROGRESS_EVENT_INTERVAL 50
const PRUint64 kUnknownSize = PRUint64(-1);
using mozilla::dom::FileIOObject;
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMFileReader)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMFileReader,
nsXHREventTarget)
FileIOObject)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mFile)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mProgressNotifier)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPrincipal)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mChannel)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(load)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(loadstart)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(loadend)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMFileReader,
nsXHREventTarget)
FileIOObject)
tmp->mResultArrayBuffer = nsnull;
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mFile)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mProgressNotifier)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mPrincipal)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mChannel)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(load)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(loadstart)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(loadend)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(nsDOMFileReader,
nsXHREventTarget)
nsDOMEventTargetWrapperCache)
if(tmp->mResultArrayBuffer) {
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_CALLBACK(tmp->mResultArrayBuffer,
"mResultArrayBuffer")
@ -127,17 +124,15 @@ DOMCI_DATA(FileReader, nsDOMFileReader)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMFileReader)
NS_INTERFACE_MAP_ENTRY(nsIDOMFileReader)
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY(nsIJSNativeInitializer)
NS_INTERFACE_MAP_ENTRY(nsITimerCallback)
NS_INTERFACE_MAP_ENTRY(nsICharsetDetectionObserver)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(FileReader)
NS_INTERFACE_MAP_END_INHERITING(nsXHREventTarget)
NS_INTERFACE_MAP_END_INHERITING(FileIOObject)
NS_IMPL_ADDREF_INHERITED(nsDOMFileReader, nsXHREventTarget)
NS_IMPL_RELEASE_INHERITED(nsDOMFileReader, nsXHREventTarget)
NS_IMPL_ADDREF_INHERITED(nsDOMFileReader, FileIOObject)
NS_IMPL_RELEASE_INHERITED(nsDOMFileReader, FileIOObject)
void
nsDOMFileReader::RootResultArrayBuffer()
@ -161,10 +156,6 @@ nsDOMFileReader::Notify(const char *aCharset, nsDetectionConfident aConf)
nsDOMFileReader::nsDOMFileReader()
: mFileData(nsnull),
mDataLen(0), mDataFormat(FILE_AS_BINARY),
mReadyState(nsIDOMFileReader::EMPTY),
mProgressEventWasDelayed(PR_FALSE),
mTimerIsActive(PR_FALSE),
mReadTotal(0), mReadTransferred(0),
mResultArrayBuffer(nsnull)
{
nsLayoutStatics::AddRef();
@ -173,9 +164,6 @@ nsDOMFileReader::nsDOMFileReader()
nsDOMFileReader::~nsDOMFileReader()
{
if (mListenerManager)
mListenerManager->Disconnect();
FreeFileData();
nsLayoutStatics::Release();
@ -184,20 +172,7 @@ nsDOMFileReader::~nsDOMFileReader()
nsresult
nsDOMFileReader::Init()
{
// Set the original mScriptContext and mPrincipal, if available.
// Get JSContext from stack.
nsCOMPtr<nsIJSContextStack> stack =
do_GetService("@mozilla.org/js/xpc/ContextStack;1");
if (!stack) {
return NS_OK;
}
JSContext *cx;
if (NS_FAILED(stack->Peek(&cx)) || !cx) {
return NS_OK;
}
nsDOMEventTargetWrapperCache::Init();
nsIScriptSecurityManager *secMan = nsContentUtils::GetSecurityManager();
nsCOMPtr<nsIPrincipal> subjectPrincipal;
@ -206,21 +181,18 @@ nsDOMFileReader::Init()
NS_ENSURE_SUCCESS(rv, rv);
}
NS_ENSURE_STATE(subjectPrincipal);
mPrincipal = subjectPrincipal;
nsIScriptContext* context = GetScriptContextFromJSContext(cx);
if (context) {
mScriptContext = context;
nsCOMPtr<nsPIDOMWindow> window =
do_QueryInterface(context->GetGlobalObject());
if (window) {
mOwner = window->GetCurrentInnerWindow();
}
}
mPrincipal.swap(subjectPrincipal);
return NS_OK;
}
NS_IMPL_EVENT_HANDLER(nsDOMFileReader, load)
NS_IMPL_EVENT_HANDLER(nsDOMFileReader, loadstart)
NS_IMPL_EVENT_HANDLER(nsDOMFileReader, loadend)
NS_IMPL_FORWARD_EVENT_HANDLER(nsDOMFileReader, abort, FileIOObject)
NS_IMPL_FORWARD_EVENT_HANDLER(nsDOMFileReader, progress, FileIOObject)
NS_IMPL_FORWARD_EVENT_HANDLER(nsDOMFileReader, error, FileIOObject)
NS_IMETHODIMP
nsDOMFileReader::Initialize(nsISupports* aOwner, JSContext* cx, JSObject* obj,
PRUint32 argc, jsval *argv)
@ -257,8 +229,7 @@ nsDOMFileReader::GetInterface(const nsIID & aIID, void **aResult)
NS_IMETHODIMP
nsDOMFileReader::GetReadyState(PRUint16 *aReadyState)
{
*aReadyState = mReadyState;
return NS_OK;
return FileIOObject::GetReadyState(aReadyState);
}
NS_IMETHODIMP
@ -284,8 +255,7 @@ nsDOMFileReader::GetResult(JSContext* aCx, jsval* aResult)
NS_IMETHODIMP
nsDOMFileReader::GetError(nsIDOMFileError** aError)
{
NS_IF_ADDREF(*aError = mError);
return NS_OK;
return FileIOObject::GetError(aError);
}
NS_IMETHODIMP
@ -316,25 +286,19 @@ nsDOMFileReader::ReadAsDataURL(nsIDOMBlob* aFile)
NS_IMETHODIMP
nsDOMFileReader::Abort()
{
if (mReadyState != nsIDOMFileReader::LOADING)
return NS_ERROR_DOM_FILE_ABORT_ERR;
return FileIOObject::Abort();
}
//Clear progress and file data
mProgressEventWasDelayed = PR_FALSE;
mTimerIsActive = PR_FALSE;
if (mProgressNotifier) {
mProgressNotifier->Cancel();
}
//Revert status, result and readystate attributes
nsresult
nsDOMFileReader::DoAbort(nsAString& aEvent)
{
// Revert status and result attributes
SetDOMStringToNull(mResult);
mResultArrayBuffer = nsnull;
mReadyState = nsIDOMFileReader::DONE;
mError = new nsDOMFileError(nsIDOMFileError::ABORT_ERR);
//Non-null channel indicates a read is currently active
// Non-null channel indicates a read is currently active
if (mChannel) {
//Cancel request requires an error status
// Cancel request requires an error status
mChannel->Cancel(NS_ERROR_FAILURE);
mChannel = nsnull;
}
@ -343,48 +307,8 @@ nsDOMFileReader::Abort()
//Clean up memory buffer
FreeFileData();
//Dispatch the abort event
DispatchProgressEvent(NS_LITERAL_STRING(ABORT_STR));
DispatchProgressEvent(NS_LITERAL_STRING(LOADEND_STR));
mReadyState = nsIDOMFileReader::EMPTY;
return NS_OK;
}
// nsITimerCallback
NS_IMETHODIMP
nsDOMFileReader::Notify(nsITimer* aTimer)
{
mTimerIsActive = PR_FALSE;
if (mProgressEventWasDelayed) {
DispatchProgressEvent(NS_LITERAL_STRING(PROGRESS_STR));
StartProgressEventTimer();
}
return NS_OK;
}
void
nsDOMFileReader::StartProgressEventTimer()
{
if (!mProgressNotifier) {
mProgressNotifier = do_CreateInstance(NS_TIMER_CONTRACTID);
}
if (mProgressNotifier) {
mProgressEventWasDelayed = PR_FALSE;
mTimerIsActive = PR_TRUE;
mProgressNotifier->Cancel();
mProgressNotifier->InitWithCallback(this, NS_PROGRESS_EVENT_INTERVAL,
nsITimer::TYPE_ONE_SHOT);
}
}
// nsIStreamListener
NS_IMETHODIMP
nsDOMFileReader::OnStartRequest(nsIRequest *request, nsISupports *ctxt)
{
// Tell the base class which event to dispatch
aEvent = NS_LITERAL_STRING(LOADEND_STR);
return NS_OK;
}
@ -410,12 +334,12 @@ ReadFuncBinaryString(nsIInputStream* in,
return NS_OK;
}
NS_IMETHODIMP
nsDOMFileReader::OnDataAvailable(nsIRequest *aRequest,
nsISupports *aContext,
nsIInputStream *aInputStream,
PRUint32 aOffset,
PRUint32 aCount)
nsresult
nsDOMFileReader::DoOnDataAvailable(nsIRequest *aRequest,
nsISupports *aContext,
nsIInputStream *aInputStream,
PRUint32 aOffset,
PRUint32 aCount)
{
if (mDataFormat == FILE_AS_BINARY) {
//Continuously update our binary string as data comes in
@ -451,44 +375,22 @@ nsDOMFileReader::OnDataAvailable(nsIRequest *aRequest,
mDataLen += aCount;
}
mReadTransferred += aCount;
//Notify the timer is the appropriate timeframe has passed
if (mTimerIsActive) {
mProgressEventWasDelayed = PR_TRUE;
}
else {
DispatchProgressEvent(NS_LITERAL_STRING(PROGRESS_STR));
StartProgressEventTimer();
}
return NS_OK;
}
NS_IMETHODIMP
nsDOMFileReader::OnStopRequest(nsIRequest *aRequest,
nsISupports *aContext,
nsresult aStatus)
nsresult
nsDOMFileReader::DoOnStopRequest(nsIRequest *aRequest,
nsISupports *aContext,
nsresult aStatus,
nsAString& aSuccessEvent,
nsAString& aTerminationEvent)
{
//If we're here as a result of a call from Abort(),
//simply ignore the request.
if (aRequest != mChannel)
return NS_OK;
aSuccessEvent = NS_LITERAL_STRING(LOAD_STR);
aTerminationEvent = NS_LITERAL_STRING(LOADEND_STR);
//Cancel the progress event timer
mProgressEventWasDelayed = PR_FALSE;
mTimerIsActive = PR_FALSE;
if (mProgressNotifier) {
mProgressNotifier->Cancel();
}
//FileReader must be in DONE stage after a load
mReadyState = nsIDOMFileReader::DONE;
//Set the status field as appropriate
// Clear out the data if necessary
if (NS_FAILED(aStatus)) {
FreeFileData();
DispatchError(aStatus);
return NS_OK;
}
@ -510,16 +412,7 @@ nsDOMFileReader::OnStopRequest(nsIRequest *aRequest,
FreeFileData();
if (NS_FAILED(rv)) {
DispatchError(rv);
return NS_OK;
}
//Dispatch load event to signify end of a successful load
DispatchProgressEvent(NS_LITERAL_STRING(LOAD_STR));
DispatchProgressEvent(NS_LITERAL_STRING(LOADEND_STR));
return NS_OK;
return rv;
}
// Helper methods
@ -537,8 +430,8 @@ nsDOMFileReader::ReadFileContent(JSContext* aCx,
Abort();
mError = nsnull;
SetDOMStringToNull(mResult);
mReadTransferred = 0;
mReadTotal = 0;
mTransferred = 0;
mTotal = 0;
mReadyState = nsIDOMFileReader::EMPTY;
FreeFileData();
@ -562,8 +455,8 @@ nsDOMFileReader::ReadFileContent(JSContext* aCx,
}
//Obtain the total size of the file before reading
mReadTotal = kUnknownSize;
mFile->GetSize(&mReadTotal);
mTotal = mozilla::dom::kUnknownSize;
mFile->GetSize(&mTotal);
rv = mChannel->AsyncOpen(this, nsnull);
NS_ENSURE_SUCCESS(rv, rv);
@ -574,7 +467,7 @@ nsDOMFileReader::ReadFileContent(JSContext* aCx,
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
RootResultArrayBuffer();
mResultArrayBuffer = js_CreateArrayBuffer(aCx, mReadTotal);
mResultArrayBuffer = js_CreateArrayBuffer(aCx, mTotal);
if (!mResultArrayBuffer) {
NS_WARNING("Failed to create JS array buffer");
return NS_ERROR_FAILURE;
@ -584,64 +477,6 @@ nsDOMFileReader::ReadFileContent(JSContext* aCx,
return NS_OK;
}
void
nsDOMFileReader::DispatchError(nsresult rv)
{
//Set the status attribute, and dispatch the error event
switch (rv) {
case NS_ERROR_FILE_NOT_FOUND:
mError = new nsDOMFileError(nsIDOMFileError::NOT_FOUND_ERR);
break;
case NS_ERROR_FILE_ACCESS_DENIED:
mError = new nsDOMFileError(nsIDOMFileError::SECURITY_ERR);
break;
default:
mError = new nsDOMFileError(nsIDOMFileError::NOT_READABLE_ERR);
break;
}
//Dispatch error event to signify load failure
DispatchProgressEvent(NS_LITERAL_STRING(ERROR_STR));
DispatchProgressEvent(NS_LITERAL_STRING(LOADEND_STR));
}
void
nsDOMFileReader::DispatchProgressEvent(const nsAString& aType)
{
nsCOMPtr<nsIDOMEvent> event;
nsresult rv = nsEventDispatcher::CreateEvent(nsnull, nsnull,
NS_LITERAL_STRING("ProgressEvent"),
getter_AddRefs(event));
if (NS_FAILED(rv))
return;
nsCOMPtr<nsIPrivateDOMEvent> privevent(do_QueryInterface(event));
if (!privevent)
return;
privevent->SetTrusted(PR_TRUE);
nsCOMPtr<nsIDOMProgressEvent> progress = do_QueryInterface(event);
if (!progress)
return;
bool known;
PRUint64 size;
if (mReadTotal != kUnknownSize) {
known = PR_TRUE;
size = mReadTotal;
} else {
known = PR_FALSE;
size = 0;
}
progress->InitProgressEvent(aType, PR_FALSE, PR_FALSE, known,
mReadTransferred, size);
this->DispatchDOMEvent(nsnull, event, nsnull, nsnull);
}
nsresult
nsDOMFileReader::GetAsText(const nsACString &aCharset,
const char *aFileData,

View File

@ -39,16 +39,15 @@
#define nsDOMFileReader_h__
#include "nsISupportsUtils.h"
#include "nsString.h"
#include "nsString.h"
#include "nsWeakReference.h"
#include "nsIStreamListener.h"
#include "nsIScriptContext.h"
#include "nsIInterfaceRequestor.h"
#include "nsJSUtils.h"
#include "nsTArray.h"
#include "nsIJSNativeInitializer.h"
#include "prtime.h"
#include "nsITimer.h"
#include "nsDOMEventTargetHelper.h"
#include "nsICharsetDetector.h"
#include "nsICharsetDetectionObserver.h"
@ -62,15 +61,13 @@
#include "nsIChannel.h"
#include "prmem.h"
#include "nsXMLHttpRequest.h"
#include "FileIOObject.h"
class nsDOMFileReader : public nsXHREventTarget,
class nsDOMFileReader : public mozilla::dom::FileIOObject,
public nsIDOMFileReader,
public nsIStreamListener,
public nsIInterfaceRequestor,
public nsSupportsWeakReference,
public nsIJSNativeInitializer,
public nsITimerCallback,
public nsICharsetDetectionObserver
{
public:
@ -81,35 +78,35 @@ public:
NS_DECL_NSIDOMFILEREADER
NS_FORWARD_NSIXMLHTTPREQUESTEVENTTARGET(nsXHREventTarget::);
// nsIStreamListener
NS_DECL_NSISTREAMLISTENER
// nsIRequestObserver
NS_DECL_NSIREQUESTOBSERVER
NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper::)
// nsIInterfaceRequestor
NS_DECL_NSIINTERFACEREQUESTOR
// nsITimerCallback
NS_DECL_NSITIMERCALLBACK
NS_DECL_EVENT_HANDLER(load)
NS_DECL_EVENT_HANDLER(loadend)
NS_DECL_EVENT_HANDLER(loadstart)
// nsIJSNativeInitializer
NS_IMETHOD Initialize(nsISupports* aOwner, JSContext* cx, JSObject* obj,
PRUint32 argc, jsval* argv);
NS_FORWARD_NSIDOMEVENTTARGET(nsXHREventTarget::)
// nsICharsetDetectionObserver
NS_IMETHOD Notify(const char *aCharset, nsDetectionConfident aConf);
void DispatchProgressEvent(const nsAString& aType);
// FileIOObject overrides
NS_IMETHOD DoAbort(nsAString& aEvent);
NS_IMETHOD DoOnStopRequest(nsIRequest* aRequest, nsISupports* aContext,
nsresult aStatus, nsAString& aSuccessEvent,
nsAString& aTerminationEvent);
NS_IMETHOD DoOnDataAvailable(nsIRequest* aRequest, nsISupports* aContext,
nsIInputStream* aInputStream, PRUint32 aOffset,
PRUint32 aCount);
nsresult Init();
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(nsDOMFileReader,
nsXHREventTarget)
FileIOObject)
void RootResultArrayBuffer();
protected:
@ -126,8 +123,6 @@ protected:
nsresult GetAsDataURL(nsIDOMBlob *aFile, const char *aFileData, PRUint32 aDataLen, nsAString &aResult);
nsresult GuessCharset(const char *aFileData, PRUint32 aDataLen, nsACString &aCharset);
nsresult ConvertStream(const char *aFileData, PRUint32 aDataLen, const char *aCharset, nsAString &aResult);
void DispatchError(nsresult rv);
void StartProgressEventTimer();
void FreeFileData() {
PR_Free(mFileData);
@ -143,18 +138,7 @@ protected:
eDataFormat mDataFormat;
nsString mResult;
PRUint16 mReadyState;
bool mProgressEventWasDelayed;
bool mTimerIsActive;
nsCOMPtr<nsIDOMFileError> mError;
nsCOMPtr<nsITimer> mProgressNotifier;
nsCOMPtr<nsIPrincipal> mPrincipal;
nsCOMPtr<nsIChannel> mChannel;
PRUint64 mReadTotal;
PRUint64 mReadTransferred;
JSObject* mResultArrayBuffer;
};

View File

@ -3823,8 +3823,6 @@ nsDOMClassInfo::Init()
DOM_CLASSINFO_MAP_BEGIN(FileReader, nsIDOMFileReader)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMFileReader)
DOM_CLASSINFO_MAP_ENTRY(nsIXMLHttpRequestEventTarget)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget)
DOM_CLASSINFO_MAP_ENTRY(nsIInterfaceRequestor)
DOM_CLASSINFO_MAP_END