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"
|
2009-10-07 08:30:26 +00:00
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsIConverterInputStream.h"
|
2013-03-17 07:55:15 +00:00
|
|
|
#include "nsIDocument.h"
|
2009-10-07 08:30:26 +00:00
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsNetUtil.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-09-25 03:24:44 +00:00
|
|
|
#include "nsHostObjectProtocolHandler.h"
|
2012-12-05 22:20:57 +00:00
|
|
|
#include "mozilla/Base64.h"
|
2011-05-27 04:53:03 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-11-07 23:04:22 +00:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2013-04-13 07:06:31 +00:00
|
|
|
#include "mozilla/dom/FileReaderBinding.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;
|
2013-04-13 07:06:31 +00:00
|
|
|
using namespace mozilla::dom;
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
#define LOAD_STR "load"
|
|
|
|
#define LOADSTART_STR "loadstart"
|
|
|
|
#define LOADEND_STR "loadend"
|
|
|
|
|
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)
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFile)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPrincipal)
|
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;
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFile)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPrincipal)
|
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
|
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMFileReader)
|
2013-04-13 07:06:31 +00:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2009-11-09 20:05:16 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMFileReader)
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
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
|
|
|
|
2012-08-31 03:45:16 +00: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-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);
|
2013-04-13 07:06:31 +00:00
|
|
|
SetIsDOMBinding();
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-13 07:06:31 +00:00
|
|
|
/* static */ already_AddRefed<nsDOMFileReader>
|
|
|
|
nsDOMFileReader::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2013-04-13 07:06:31 +00:00
|
|
|
nsRefPtr<nsDOMFileReader> fileReader = new nsDOMFileReader();
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> owner = do_QueryInterface(aGlobal.Get());
|
2012-03-13 00:56:07 +00:00
|
|
|
if (!owner) {
|
2009-10-07 08:30:26 +00:00
|
|
|
NS_WARNING("Unexpected nsIJSNativeInitializer owner");
|
2013-04-13 07:06:31 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 07:06:31 +00:00
|
|
|
fileReader->BindToOwner(owner);
|
2012-03-13 00:56:07 +00:00
|
|
|
|
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.
|
2013-04-13 07:06:31 +00:00
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal = do_QueryInterface(owner);
|
|
|
|
if (!scriptPrincipal) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
fileReader->mPrincipal = scriptPrincipal->GetPrincipal();
|
|
|
|
return fileReader.forget();
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2012-08-22 15:56:38 +00:00
|
|
|
nsDOMFileReader::GetReadyState(uint16_t *aReadyState)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2013-04-13 07:06:31 +00:00
|
|
|
*aReadyState = ReadyState();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value
|
|
|
|
nsDOMFileReader::GetResult(JSContext* aCx, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
JS::Value result = JS::UndefinedValue();
|
|
|
|
aRv = GetResult(aCx, &result);
|
|
|
|
return result;
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-03-23 05:11:52 +00:00
|
|
|
nsDOMFileReader::GetResult(JSContext* aCx, JS::Value* 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
|
|
|
{
|
2013-04-13 07:06:31 +00:00
|
|
|
NS_IF_ADDREF(*aError = GetError());
|
|
|
|
return NS_OK;
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
2011-07-01 00:50:44 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMFileReader::ReadAsArrayBuffer(nsIDOMBlob* aFile, JSContext* aCx)
|
|
|
|
{
|
2013-04-13 07:06:31 +00:00
|
|
|
NS_ENSURE_TRUE(aFile, NS_ERROR_NULL_POINTER);
|
|
|
|
ErrorResult rv;
|
|
|
|
ReadAsArrayBuffer(aCx, aFile, rv);
|
|
|
|
return rv.ErrorCode();
|
2011-07-01 00:50:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-04-13 07:06:31 +00:00
|
|
|
NS_ENSURE_TRUE(aFile, NS_ERROR_NULL_POINTER);
|
|
|
|
ErrorResult rv;
|
|
|
|
ReadAsBinaryString(aFile, rv);
|
|
|
|
return rv.ErrorCode();
|
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
|
|
|
{
|
2013-04-13 07:06:31 +00:00
|
|
|
NS_ENSURE_TRUE(aFile, NS_ERROR_NULL_POINTER);
|
|
|
|
ErrorResult rv;
|
|
|
|
ReadAsText(aFile, aCharset, rv);
|
|
|
|
return rv.ErrorCode();
|
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
|
|
|
{
|
2013-04-13 07:06:31 +00:00
|
|
|
NS_ENSURE_TRUE(aFile, NS_ERROR_NULL_POINTER);
|
|
|
|
ErrorResult rv;
|
|
|
|
ReadAsDataURL(aFile, rv);
|
|
|
|
return rv.ErrorCode();
|
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
|
|
|
{
|
2013-04-13 07:06:31 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
FileIOObject::Abort(rv);
|
|
|
|
return rv.ErrorCode();
|
2011-09-29 16:06:35 +00:00
|
|
|
}
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2013-04-13 07:06:31 +00:00
|
|
|
/* virtual */ void
|
2011-09-29 16:06:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-11-21 11:56:31 +00:00
|
|
|
static
|
|
|
|
NS_METHOD
|
|
|
|
ReadFuncBinaryString(nsIInputStream* in,
|
|
|
|
void* closure,
|
|
|
|
const char* fromRawSegment,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t toOffset,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t *writeCount)
|
2009-11-21 11:56:31 +00: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 16:06:35 +00:00
|
|
|
nsresult
|
|
|
|
nsDOMFileReader::DoOnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *aInputStream,
|
2012-09-06 02:41:02 +00:00
|
|
|
uint64_t aOffset,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t 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");
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t oldLen = mResult.Length();
|
2012-09-28 06:57:33 +00:00
|
|
|
if (uint64_t(oldLen) + aCount > UINT32_MAX)
|
2012-09-06 02:41:02 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
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);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t bytesRead = 0;
|
2009-11-21 11:56:31 +00:00
|
|
|
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) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t bytesRead = 0;
|
2012-11-14 17:56:26 +00:00
|
|
|
aInputStream->Read((char*)JS_GetArrayBufferData(mResultArrayBuffer) + aOffset,
|
2012-01-14 17:43:00 +00:00
|
|
|
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
|
2012-09-28 06:57:33 +00:00
|
|
|
if (aOffset + aCount > UINT32_MAX) {
|
2012-09-06 02:41:02 +00:00
|
|
|
// PR_Realloc doesn't support over 4GB memory size even if 64-bit OS
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-12-05 22:20:57 +00:00
|
|
|
mFileData = (char *)moz_realloc(mFileData, aOffset + aCount);
|
2009-11-21 11:56:31 +00:00
|
|
|
NS_ENSURE_TRUE(mFileData, NS_ERROR_OUT_OF_MEMORY);
|
2009-11-21 09:27:02 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t bytesRead = 0;
|
2009-11-21 11:56:31 +00:00
|
|
|
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
|
|
|
{
|
2012-08-28 14:59:18 +00: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 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:
|
2012-08-28 14:59:18 +00:00
|
|
|
rv = GetAsDataURL(file, 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
|
|
|
|
|
2013-04-13 07:06:31 +00:00
|
|
|
void
|
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,
|
2013-04-13 07:06:31 +00:00
|
|
|
eDataFormat aDataFormat,
|
|
|
|
ErrorResult& aRv)
|
2009-11-25 01:53:17 +00:00
|
|
|
{
|
2013-04-13 07:06:31 +00:00
|
|
|
MOZ_ASSERT(aFile);
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
//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;
|
2013-04-13 07:06:31 +00:00
|
|
|
aRv = NS_NewURI(getter_AddRefs(uri), urlHolder.mUrl);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv.ErrorCode());
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2012-09-17 15:24:39 +00:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
if (HasOrHasHadOwner()) {
|
2013-04-13 07:06:31 +00:00
|
|
|
if (!GetOwner()) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2012-09-17 15:24:39 +00:00
|
|
|
nsIDocument* doc = GetOwner()->GetExtantDoc();
|
|
|
|
if (doc) {
|
|
|
|
loadGroup = doc->GetDocumentLoadGroup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-13 07:06:31 +00:00
|
|
|
aRv = NS_NewChannel(getter_AddRefs(mChannel), uri, nullptr, loadGroup,
|
|
|
|
nullptr, nsIRequest::LOAD_BACKGROUND);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv.ErrorCode());
|
2010-09-05 18:00:05 +00:00
|
|
|
}
|
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
|
|
|
|
2013-04-13 07:06:31 +00:00
|
|
|
aRv = mChannel->AsyncOpen(this, nullptr);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv.ErrorCode());
|
2009-10-07 08:30:26 +00:00
|
|
|
|
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));
|
2013-04-13 07:06:31 +00:00
|
|
|
|
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");
|
2013-04-13 07:06:31 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2011-07-01 00:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aDataLen,
|
2009-11-09 20:05:16 +00:00
|
|
|
nsAString& aResult)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString charsetGuess;
|
2009-10-07 08:30:26 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString charset;
|
2012-11-07 23:04:22 +00:00
|
|
|
if (!EncodingUtils::FindEncodingForLabel(charsetGuess, charset)) {
|
|
|
|
return NS_ERROR_DOM_ENCODING_NOT_SUPPORTED_ERR;
|
|
|
|
}
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
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,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aDataLen,
|
2009-11-09 20:05:16 +00:00
|
|
|
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,");
|
|
|
|
|
2012-12-05 22:20:57 +00:00
|
|
|
nsCString encodedData;
|
|
|
|
rv = Base64Encode(Substring(aFileData, aDataLen), encodedData);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-12-06 06:09:30 +00:00
|
|
|
|
2012-12-05 22:20:57 +00:00
|
|
|
AppendASCIItoUTF16(encodedData, aResult);
|
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,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aDataLen,
|
2009-11-09 20:05:16 +00:00
|
|
|
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);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t destLength;
|
2009-10-07 08:30:26 +00:00
|
|
|
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
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t srcLength = aDataLen;
|
2009-10-07 08:30:26 +00:00
|
|
|
rv = unicodeDecoder->Convert(aFileData, &srcLength, aResult.BeginWriting(), &destLength);
|
|
|
|
aResult.SetLength(destLength); //Trim down to the correct size
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2013-04-13 07:06:31 +00:00
|
|
|
|
|
|
|
/* virtual */ JSObject*
|
|
|
|
nsDOMFileReader::WrapObject(JSContext* aCx, JSObject* aScope)
|
|
|
|
{
|
|
|
|
return FileReaderBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|