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