2009-10-07 08:30:26 +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/. */
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
#include "nsDOMFileReader.h"
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
#include "nsContentCID.h"
|
|
|
|
#include "nsContentUtils.h"
|
2011-10-03 19:11:31 +00:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2009-10-07 08:30:26 +00:00
|
|
|
#include "nsDOMFile.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2012-03-05 03:57:51 +00:00
|
|
|
#include "nsCharsetAlias.h"
|
2009-10-07 08:30:26 +00:00
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsIConverterInputStream.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
|
|
|
#include "plbase64.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
|
|
|
|
#include "nsLayoutCID.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsIJSContextStack.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
#include "nsIStreamConverterService.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsLayoutStatics.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2012-01-12 10:36:03 +00:00
|
|
|
#include "nsBlobProtocolHandler.h"
|
2011-05-27 04:53:03 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-09-29 16:06:36 +00:00
|
|
|
#include "xpcpublic.h"
|
2011-12-24 08:27:04 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2011-10-03 19:11:31 +00:00
|
|
|
#include "nsDOMJSUtils.h"
|
2012-02-08 02:53:33 +00:00
|
|
|
#include "nsDOMEventTargetHelper.h"
|
2011-05-27 04:53:03 +00:00
|
|
|
|
2012-01-14 17:43:00 +00:00
|
|
|
#include "jsfriendapi.h"
|
2011-12-24 08:27:04 +00:00
|
|
|
|
2011-05-27 04:53:03 +00:00
|
|
|
using namespace mozilla;
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
#define LOAD_STR "load"
|
|
|
|
#define LOADSTART_STR "loadstart"
|
|
|
|
#define LOADEND_STR "loadend"
|
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
using mozilla::dom::FileIOObject;
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMFileReader)
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMFileReader,
|
2011-09-29 16:06:35 +00:00
|
|
|
FileIOObject)
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mFile)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPrincipal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mChannel)
|
2011-09-29 19:18:58 +00:00
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(load)
|
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(loadstart)
|
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(loadend)
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMFileReader,
|
2011-09-29 16:06:35 +00:00
|
|
|
FileIOObject)
|
2012-07-30 14:20:58 +00:00
|
|
|
tmp->mResultArrayBuffer = nullptr;
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mFile)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mPrincipal)
|
2011-09-29 19:18:58 +00:00
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(load)
|
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(loadstart)
|
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(loadend)
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2011-07-01 00:50:44 +00:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(nsDOMFileReader,
|
2012-02-08 02:53:33 +00:00
|
|
|
nsDOMEventTargetHelper)
|
2012-06-10 23:44:50 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mResultArrayBuffer)
|
2011-07-01 00:50:44 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2010-01-12 13:08:43 +00:00
|
|
|
DOMCI_DATA(FileReader, nsDOMFileReader)
|
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMFileReader)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMFileReader)
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIJSNativeInitializer)
|
2010-03-17 15:09:05 +00:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(FileReader)
|
2011-09-29 16:06:35 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(FileIOObject)
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsDOMFileReader, FileIOObject)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsDOMFileReader, FileIOObject)
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2011-07-01 00:50:44 +00:00
|
|
|
void
|
|
|
|
nsDOMFileReader::RootResultArrayBuffer()
|
|
|
|
{
|
|
|
|
nsContentUtils::PreserveWrapper(
|
|
|
|
static_cast<nsIDOMEventTarget*>(
|
|
|
|
static_cast<nsDOMEventTargetHelper*>(this)), this);
|
|
|
|
}
|
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
//nsDOMFileReader constructors/initializers
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
nsDOMFileReader::nsDOMFileReader()
|
2012-07-30 14:20:58 +00:00
|
|
|
: mFileData(nullptr),
|
2009-11-25 01:53:17 +00:00
|
|
|
mDataLen(0), mDataFormat(FILE_AS_BINARY),
|
2012-07-30 14:20:58 +00:00
|
|
|
mResultArrayBuffer(nullptr)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
|
|
|
nsLayoutStatics::AddRef();
|
2011-07-01 00:50:44 +00:00
|
|
|
SetDOMStringToNull(mResult);
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
nsDOMFileReader::~nsDOMFileReader()
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2009-11-23 06:23:23 +00:00
|
|
|
FreeFileData();
|
|
|
|
|
2009-10-07 08:30:26 +00:00
|
|
|
nsLayoutStatics::Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2009-11-09 20:05:16 +00:00
|
|
|
nsDOMFileReader::Init()
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2012-02-08 02:53:33 +00:00
|
|
|
nsDOMEventTargetHelper::Init();
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
nsIScriptSecurityManager *secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
nsCOMPtr<nsIPrincipal> subjectPrincipal;
|
|
|
|
if (secMan) {
|
2011-06-21 16:52:22 +00:00
|
|
|
nsresult rv = secMan->GetSubjectPrincipal(getter_AddRefs(subjectPrincipal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
NS_ENSURE_STATE(subjectPrincipal);
|
2011-09-29 16:06:35 +00:00
|
|
|
mPrincipal.swap(subjectPrincipal);
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
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)
|
|
|
|
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_IMETHODIMP
|
2009-11-09 20:05:16 +00:00
|
|
|
nsDOMFileReader::Initialize(nsISupports* aOwner, JSContext* cx, JSObject* obj,
|
|
|
|
PRUint32 argc, jsval *argv)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2012-03-13 00:56:07 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> owner = do_QueryInterface(aOwner);
|
|
|
|
if (!owner) {
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_WARNING("Unexpected nsIJSNativeInitializer owner");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-13 00:56:07 +00:00
|
|
|
BindToOwner(owner);
|
|
|
|
|
2009-10-07 08:30:26 +00:00
|
|
|
// This object is bound to a |window|,
|
2012-03-13 00:56:07 +00:00
|
|
|
// so reset the principal.
|
2009-10-07 08:30:26 +00:00
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal = do_QueryInterface(aOwner);
|
|
|
|
NS_ENSURE_STATE(scriptPrincipal);
|
|
|
|
mPrincipal = scriptPrincipal->GetPrincipal();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIInterfaceRequestor
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-11-09 20:05:16 +00:00
|
|
|
nsDOMFileReader::GetInterface(const nsIID & aIID, void **aResult)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
// nsIDOMFileReader
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-11-09 20:05:16 +00:00
|
|
|
nsDOMFileReader::GetReadyState(PRUint16 *aReadyState)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2011-09-29 16:06:35 +00:00
|
|
|
return FileIOObject::GetReadyState(aReadyState);
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-07-01 00:50:44 +00:00
|
|
|
nsDOMFileReader::GetResult(JSContext* aCx, jsval* aResult)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2011-07-01 00:50:44 +00:00
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
if (mReadyState == nsIDOMFileReader::DONE && mResultArrayBuffer) {
|
|
|
|
JSObject* tmp = mResultArrayBuffer;
|
|
|
|
*aResult = OBJECT_TO_JSVAL(tmp);
|
|
|
|
} else {
|
|
|
|
*aResult = JSVAL_NULL;
|
|
|
|
}
|
2012-03-24 00:04:55 +00:00
|
|
|
if (!JS_WrapValue(aCx, aResult)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-07-01 00:50:44 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString tmpResult = mResult;
|
2011-12-24 08:27:04 +00:00
|
|
|
if (!xpc::StringToJsval(aCx, tmpResult, aResult)) {
|
2011-07-01 00:50:44 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-10-07 08:30:26 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-03-11 08:47:38 +00:00
|
|
|
nsDOMFileReader::GetError(nsIDOMDOMError** aError)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2011-09-29 16:06:35 +00:00
|
|
|
return FileIOObject::GetError(aError);
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
2011-07-01 00:50:44 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMFileReader::ReadAsArrayBuffer(nsIDOMBlob* aFile, JSContext* aCx)
|
|
|
|
{
|
|
|
|
return ReadFileContent(aCx, aFile, EmptyString(), FILE_AS_ARRAYBUFFER);
|
|
|
|
}
|
|
|
|
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_IMETHODIMP
|
2010-10-13 23:25:33 +00:00
|
|
|
nsDOMFileReader::ReadAsBinaryString(nsIDOMBlob* aFile)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
return ReadFileContent(nullptr, aFile, EmptyString(), FILE_AS_BINARY);
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-10-13 23:25:33 +00:00
|
|
|
nsDOMFileReader::ReadAsText(nsIDOMBlob* aFile,
|
2009-11-09 20:05:16 +00:00
|
|
|
const nsAString &aCharset)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
return ReadFileContent(nullptr, aFile, aCharset, FILE_AS_TEXT);
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-10-13 23:25:33 +00:00
|
|
|
nsDOMFileReader::ReadAsDataURL(nsIDOMBlob* aFile)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
return ReadFileContent(nullptr, aFile, EmptyString(), FILE_AS_DATAURL);
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-11-09 20:05:16 +00:00
|
|
|
nsDOMFileReader::Abort()
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2011-09-29 16:06:35 +00:00
|
|
|
return FileIOObject::Abort();
|
|
|
|
}
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
nsresult
|
|
|
|
nsDOMFileReader::DoAbort(nsAString& aEvent)
|
|
|
|
{
|
|
|
|
// Revert status and result attributes
|
2009-11-09 20:05:16 +00:00
|
|
|
SetDOMStringToNull(mResult);
|
2012-07-30 14:20:58 +00:00
|
|
|
mResultArrayBuffer = nullptr;
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
// Non-null channel indicates a read is currently active
|
2009-10-07 08:30:26 +00:00
|
|
|
if (mChannel) {
|
2011-09-29 16:06:35 +00:00
|
|
|
// Cancel request requires an error status
|
2009-10-07 08:30:26 +00:00
|
|
|
mChannel->Cancel(NS_ERROR_FAILURE);
|
2012-07-30 14:20:58 +00:00
|
|
|
mChannel = nullptr;
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
2012-07-30 14:20:58 +00:00
|
|
|
mFile = nullptr;
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
//Clean up memory buffer
|
2009-11-23 06:23:23 +00:00
|
|
|
FreeFileData();
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
// Tell the base class which event to dispatch
|
|
|
|
aEvent = NS_LITERAL_STRING(LOADEND_STR);
|
2009-10-07 08:30:26 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-21 11:56:31 +00:00
|
|
|
static
|
|
|
|
NS_METHOD
|
|
|
|
ReadFuncBinaryString(nsIInputStream* in,
|
|
|
|
void* closure,
|
|
|
|
const char* fromRawSegment,
|
|
|
|
PRUint32 toOffset,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *writeCount)
|
|
|
|
{
|
|
|
|
PRUnichar* dest = static_cast<PRUnichar*>(closure) + toOffset;
|
|
|
|
PRUnichar* end = dest + count;
|
|
|
|
const unsigned char* source = (const unsigned char*)fromRawSegment;
|
|
|
|
while (dest != end) {
|
|
|
|
*dest = *source;
|
|
|
|
++dest;
|
|
|
|
++source;
|
|
|
|
}
|
|
|
|
*writeCount = count;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
nsresult
|
|
|
|
nsDOMFileReader::DoOnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *aInputStream,
|
|
|
|
PRUint32 aOffset,
|
|
|
|
PRUint32 aCount)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2009-11-21 09:27:02 +00:00
|
|
|
if (mDataFormat == FILE_AS_BINARY) {
|
2009-11-21 11:56:31 +00:00
|
|
|
//Continuously update our binary string as data comes in
|
|
|
|
NS_ASSERTION(mResult.Length() == aOffset,
|
|
|
|
"unexpected mResult length");
|
2009-11-21 09:27:02 +00:00
|
|
|
PRUint32 oldLen = mResult.Length();
|
2012-07-30 14:20:58 +00:00
|
|
|
PRUnichar *buf = nullptr;
|
2012-05-18 16:42:01 +00:00
|
|
|
mResult.GetMutableData(&buf, oldLen + aCount, fallible_t());
|
2009-11-21 11:56:31 +00:00
|
|
|
NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
PRUint32 bytesRead = 0;
|
|
|
|
aInputStream->ReadSegments(ReadFuncBinaryString, buf + oldLen, aCount,
|
|
|
|
&bytesRead);
|
|
|
|
NS_ASSERTION(bytesRead == aCount, "failed to read data");
|
|
|
|
}
|
2011-07-01 00:50:44 +00:00
|
|
|
else if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
PRUint32 bytesRead = 0;
|
2012-01-14 17:43:00 +00:00
|
|
|
aInputStream->Read((char*)JS_GetArrayBufferData(mResultArrayBuffer, NULL) + aOffset,
|
|
|
|
aCount, &bytesRead);
|
2011-07-01 00:50:44 +00:00
|
|
|
NS_ASSERTION(bytesRead == aCount, "failed to read data");
|
|
|
|
}
|
2009-11-21 11:56:31 +00:00
|
|
|
else {
|
|
|
|
//Update memory buffer to reflect the contents of the file
|
|
|
|
mFileData = (char *)PR_Realloc(mFileData, aOffset + aCount);
|
|
|
|
NS_ENSURE_TRUE(mFileData, NS_ERROR_OUT_OF_MEMORY);
|
2009-11-21 09:27:02 +00:00
|
|
|
|
2009-11-21 11:56:31 +00:00
|
|
|
PRUint32 bytesRead = 0;
|
|
|
|
aInputStream->Read(mFileData + aOffset, aCount, &bytesRead);
|
|
|
|
NS_ASSERTION(bytesRead == aCount, "failed to read data");
|
2009-11-21 09:27:02 +00:00
|
|
|
|
2009-11-21 11:56:31 +00:00
|
|
|
mDataLen += aCount;
|
2009-11-21 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
2009-10-07 08:30:26 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
nsresult
|
|
|
|
nsDOMFileReader::DoOnStopRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsresult aStatus,
|
|
|
|
nsAString& aSuccessEvent,
|
|
|
|
nsAString& aTerminationEvent)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2011-09-29 16:06:35 +00:00
|
|
|
aSuccessEvent = NS_LITERAL_STRING(LOAD_STR);
|
|
|
|
aTerminationEvent = NS_LITERAL_STRING(LOADEND_STR);
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
// Clear out the data if necessary
|
2009-10-07 08:30:26 +00:00
|
|
|
if (NS_FAILED(aStatus)) {
|
2009-11-23 06:23:23 +00:00
|
|
|
FreeFileData();
|
2009-10-07 08:30:26 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-25 01:53:17 +00:00
|
|
|
nsresult rv = NS_OK;
|
2009-10-07 08:30:26 +00:00
|
|
|
switch (mDataFormat) {
|
2011-07-01 00:50:44 +00:00
|
|
|
case FILE_AS_ARRAYBUFFER:
|
|
|
|
break; //Already accumulated mResultArrayBuffer
|
2009-10-07 08:30:26 +00:00
|
|
|
case FILE_AS_BINARY:
|
2009-11-09 20:05:16 +00:00
|
|
|
break; //Already accumulated mResult
|
2009-10-07 08:30:26 +00:00
|
|
|
case FILE_AS_TEXT:
|
2009-11-09 20:05:16 +00:00
|
|
|
rv = GetAsText(mCharset, mFileData, mDataLen, mResult);
|
2009-10-07 08:30:26 +00:00
|
|
|
break;
|
|
|
|
case FILE_AS_DATAURL:
|
2009-11-09 20:05:16 +00:00
|
|
|
rv = GetAsDataURL(mFile, mFileData, mDataLen, mResult);
|
2009-10-07 08:30:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-10-07 16:36:23 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mResult.SetIsVoid(false);
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2009-11-23 06:23:23 +00:00
|
|
|
FreeFileData();
|
|
|
|
|
2011-09-29 16:06:35 +00:00
|
|
|
return rv;
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper methods
|
|
|
|
|
|
|
|
nsresult
|
2011-07-01 00:50:44 +00:00
|
|
|
nsDOMFileReader::ReadFileContent(JSContext* aCx,
|
|
|
|
nsIDOMBlob* aFile,
|
2009-11-09 20:05:16 +00:00
|
|
|
const nsAString &aCharset,
|
2009-11-25 01:53:17 +00:00
|
|
|
eDataFormat aDataFormat)
|
|
|
|
{
|
2010-09-05 18:00:05 +00:00
|
|
|
nsresult rv;
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_ENSURE_TRUE(aFile, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
//Implicit abort to clear any other activity going on
|
|
|
|
Abort();
|
2012-07-30 14:20:58 +00:00
|
|
|
mError = nullptr;
|
2009-11-22 19:05:12 +00:00
|
|
|
SetDOMStringToNull(mResult);
|
2011-09-29 16:06:35 +00:00
|
|
|
mTransferred = 0;
|
|
|
|
mTotal = 0;
|
2009-11-23 06:23:23 +00:00
|
|
|
mReadyState = nsIDOMFileReader::EMPTY;
|
|
|
|
FreeFileData();
|
|
|
|
|
2010-09-05 18:00:05 +00:00
|
|
|
mFile = aFile;
|
2009-11-23 06:23:23 +00:00
|
|
|
mDataFormat = aDataFormat;
|
2010-10-07 16:36:23 +00:00
|
|
|
CopyUTF16toUTF8(aCharset, mCharset);
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2010-09-05 15:32:57 +00:00
|
|
|
//Establish a channel with our file
|
2010-09-05 18:00:05 +00:00
|
|
|
{
|
|
|
|
// Hold the internal URL alive only as long as necessary
|
|
|
|
// After the channel is created it will own whatever is backing
|
|
|
|
// the DOMFile.
|
2010-09-18 01:22:44 +00:00
|
|
|
nsDOMFileInternalUrlHolder urlHolder(mFile, mPrincipal);
|
2010-09-05 18:00:05 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), urlHolder.mUrl);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2010-09-05 18:00:05 +00:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(mChannel), uri);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
//Obtain the total size of the file before reading
|
2011-09-29 16:06:35 +00:00
|
|
|
mTotal = mozilla::dom::kUnknownSize;
|
|
|
|
mFile->GetSize(&mTotal);
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = mChannel->AsyncOpen(this, nullptr);
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
//FileReader should be in loading state here
|
|
|
|
mReadyState = nsIDOMFileReader::LOADING;
|
2009-10-07 08:30:26 +00:00
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(LOADSTART_STR));
|
2011-07-01 00:50:44 +00:00
|
|
|
|
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
RootResultArrayBuffer();
|
2012-01-14 17:43:00 +00:00
|
|
|
mResultArrayBuffer = JS_NewArrayBuffer(aCx, mTotal);
|
2011-07-01 00:50:44 +00:00
|
|
|
if (!mResultArrayBuffer) {
|
|
|
|
NS_WARNING("Failed to create JS array buffer");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-10-07 16:36:23 +00:00
|
|
|
nsDOMFileReader::GetAsText(const nsACString &aCharset,
|
2009-11-09 20:05:16 +00:00
|
|
|
const char *aFileData,
|
|
|
|
PRUint32 aDataLen,
|
|
|
|
nsAString& aResult)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCAutoString charsetGuess;
|
|
|
|
if (!aCharset.IsEmpty()) {
|
2010-10-07 16:36:23 +00:00
|
|
|
charsetGuess = aCharset;
|
2009-10-07 08:30:26 +00:00
|
|
|
} else {
|
2012-06-03 16:33:52 +00:00
|
|
|
rv = nsContentUtils::GuessCharset(aFileData, aDataLen, charsetGuess);
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString charset;
|
2012-03-05 03:57:51 +00:00
|
|
|
rv = nsCharsetAlias::GetPreferred(charsetGuess, charset);
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ConvertStream(aFileData, aDataLen, charset.get(), aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-10-13 23:25:33 +00:00
|
|
|
nsDOMFileReader::GetAsDataURL(nsIDOMBlob *aFile,
|
2009-11-09 20:05:16 +00:00
|
|
|
const char *aFileData,
|
|
|
|
PRUint32 aDataLen,
|
|
|
|
nsAString& aResult)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
|
|
|
aResult.AssignLiteral("data:");
|
|
|
|
|
|
|
|
nsresult rv;
|
2010-09-05 18:00:05 +00:00
|
|
|
nsString contentType;
|
|
|
|
rv = aFile->GetType(contentType);
|
|
|
|
if (NS_SUCCEEDED(rv) && !contentType.IsEmpty()) {
|
|
|
|
aResult.Append(contentType);
|
2009-10-07 08:30:26 +00:00
|
|
|
} else {
|
|
|
|
aResult.AppendLiteral("application/octet-stream");
|
|
|
|
}
|
|
|
|
aResult.AppendLiteral(";base64,");
|
|
|
|
|
|
|
|
PRUint32 totalRead = 0;
|
2010-10-07 16:36:23 +00:00
|
|
|
while (aDataLen > totalRead) {
|
2009-10-07 08:30:26 +00:00
|
|
|
PRUint32 numEncode = 4096;
|
|
|
|
PRUint32 amtRemaining = aDataLen - totalRead;
|
|
|
|
if (numEncode > amtRemaining)
|
|
|
|
numEncode = amtRemaining;
|
|
|
|
|
|
|
|
//Unless this is the end of the file, encode in multiples of 3
|
|
|
|
if (numEncode > 3) {
|
|
|
|
PRUint32 leftOver = numEncode % 3;
|
|
|
|
numEncode -= leftOver;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Out buffer should be at least 4/3rds the read buf, plus a terminator
|
2012-07-30 14:20:58 +00:00
|
|
|
char *base64 = PL_Base64Encode(aFileData + totalRead, numEncode, nullptr);
|
2009-10-07 08:30:26 +00:00
|
|
|
AppendASCIItoUTF16(nsDependentCString(base64), aResult);
|
|
|
|
PR_Free(base64);
|
|
|
|
|
|
|
|
totalRead += numEncode;
|
2010-10-07 16:36:23 +00:00
|
|
|
}
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2009-11-09 20:05:16 +00:00
|
|
|
nsDOMFileReader::ConvertStream(const char *aFileData,
|
|
|
|
PRUint32 aDataLen,
|
|
|
|
const char *aCharset,
|
|
|
|
nsAString &aResult)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> charsetConverter =
|
|
|
|
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;
|
|
|
|
rv = charsetConverter->GetUnicodeDecoder(aCharset, getter_AddRefs(unicodeDecoder));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRInt32 destLength;
|
|
|
|
rv = unicodeDecoder->GetMaxLength(aFileData, aDataLen, &destLength);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-05-18 16:42:01 +00:00
|
|
|
if (!aResult.SetLength(destLength, fallible_t()))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
PRInt32 srcLength = aDataLen;
|
|
|
|
rv = unicodeDecoder->Convert(aFileData, &srcLength, aResult.BeginWriting(), &destLength);
|
|
|
|
aResult.SetLength(destLength); //Trim down to the correct size
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|