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"
|
2009-10-07 01:30:26 -07:00
|
|
|
#include "nsIConverterInputStream.h"
|
2013-03-17 00:55:15 -07:00
|
|
|
#include "nsIDocument.h"
|
2009-10-07 01:30:26 -07: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 "nsJSEnvironment.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
#include "nsIStreamConverterService.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2012-09-24 20:24:44 -07:00
|
|
|
#include "nsHostObjectProtocolHandler.h"
|
2012-12-05 14:20:57 -08:00
|
|
|
#include "mozilla/Base64.h"
|
2011-05-26 21:53:03 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-11-07 15:04:22 -08:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2013-04-13 00:06:31 -07:00
|
|
|
#include "mozilla/dom/FileReaderBinding.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;
|
2013-04-13 00:06:31 -07:00
|
|
|
using namespace mozilla::dom;
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
#define LOAD_STR "load"
|
|
|
|
#define LOADSTART_STR "loadstart"
|
|
|
|
#define LOADEND_STR "loadend"
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMFileReader)
|
|
|
|
|
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)
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFile)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPrincipal)
|
2009-10-07 01:30:26 -07:00
|
|
|
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;
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFile)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPrincipal)
|
2009-10-07 01:30:26 -07:00
|
|
|
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
|
|
|
|
|
2009-11-09 12:05:16 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMFileReader)
|
2013-04-13 00:06:31 -07:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2009-11-09 12:05:16 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMFileReader)
|
2009-10-07 01:30:26 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
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()
|
|
|
|
{
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::HoldJSObjects(this);
|
2011-06-30 17:50:44 -07:00
|
|
|
}
|
|
|
|
|
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),
|
2013-04-19 15:18:33 -07:00
|
|
|
mResultArrayBuffer(nullptr)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2011-06-30 17:50:44 -07:00
|
|
|
SetDOMStringToNull(mResult);
|
2013-04-13 00:06:31 -07:00
|
|
|
SetIsDOMBinding();
|
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();
|
2013-05-15 04:48:55 -07:00
|
|
|
mResultArrayBuffer = nullptr;
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::DropJSObjects(this);
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
2013-04-22 10:24:48 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This Init method is called from the factory constructor.
|
|
|
|
*/
|
2009-10-07 01:30:26 -07:00
|
|
|
nsresult
|
2009-11-09 12:05:16 -08:00
|
|
|
nsDOMFileReader::Init()
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2013-04-22 10:24:48 -07:00
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
2009-10-07 01:30:26 -07:00
|
|
|
if (secMan) {
|
2013-04-22 10:24:48 -07:00
|
|
|
secMan->GetSystemPrincipal(getter_AddRefs(principal));
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2013-04-22 10:24:48 -07:00
|
|
|
NS_ENSURE_STATE(principal);
|
|
|
|
mPrincipal.swap(principal);
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2013-04-22 10:24:48 -07:00
|
|
|
// Instead of grabbing some random global from the context stack,
|
2013-07-16 06:04:28 -07:00
|
|
|
// let's use the default one (junk scope) for now.
|
2013-04-22 10:24:48 -07:00
|
|
|
// We should move away from this Init...
|
2013-07-16 06:04:28 -07:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = xpc::GetJunkScopeGlobal();
|
|
|
|
NS_ENSURE_TRUE(global, NS_ERROR_FAILURE);
|
|
|
|
BindToOwner(global);
|
2009-10-07 01:30:26 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-13 00:06:31 -07:00
|
|
|
/* static */ already_AddRefed<nsDOMFileReader>
|
|
|
|
nsDOMFileReader::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2013-04-13 00:06:31 -07:00
|
|
|
nsRefPtr<nsDOMFileReader> fileReader = new nsDOMFileReader();
|
|
|
|
|
2013-08-22 22:17:08 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> owner = do_QueryInterface(aGlobal.GetAsSupports());
|
2012-03-12 17:56:07 -07:00
|
|
|
if (!owner) {
|
2009-10-07 01:30:26 -07:00
|
|
|
NS_WARNING("Unexpected nsIJSNativeInitializer owner");
|
2013-04-13 00:06:31 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
2013-04-13 00:06:31 -07:00
|
|
|
fileReader->BindToOwner(owner);
|
2012-03-12 17:56:07 -07:00
|
|
|
|
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.
|
2013-04-13 00:06:31 -07: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 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
2013-04-13 00:06:31 -07:00
|
|
|
*aReadyState = ReadyState();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value
|
|
|
|
nsDOMFileReader::GetResult(JSContext* aCx, ErrorResult& aRv)
|
|
|
|
{
|
2013-10-26 09:19:05 -07:00
|
|
|
JS::Rooted<JS::Value> result(aCx);
|
2014-01-09 09:39:36 -08:00
|
|
|
aRv = GetResult(aCx, &result);
|
2013-04-13 00:06:31 -07:00
|
|
|
return result;
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 09:39:36 -08:00
|
|
|
nsDOMFileReader::GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2013-10-26 09:19:05 -07:00
|
|
|
JS::Rooted<JS::Value> result(aCx);
|
2011-06-30 17:50:44 -07:00
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
if (mReadyState == nsIDOMFileReader::DONE && mResultArrayBuffer) {
|
2013-10-26 09:19:05 -07:00
|
|
|
result.setObject(*mResultArrayBuffer);
|
2011-06-30 17:50:44 -07:00
|
|
|
} else {
|
2013-10-26 09:19:05 -07:00
|
|
|
result.setNull();
|
2011-06-30 17:50:44 -07:00
|
|
|
}
|
2013-10-26 09:19:05 -07:00
|
|
|
if (!JS_WrapValue(aCx, &result)) {
|
2012-03-23 17:04:55 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-01-09 09:39:36 -08:00
|
|
|
aResult.set(result);
|
2011-06-30 17:50:44 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-10-26 09:19:05 -07:00
|
|
|
|
2011-06-30 17:50:44 -07:00
|
|
|
nsString tmpResult = mResult;
|
2014-01-09 09:39:36 -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
|
2013-05-18 10:52:06 -07:00
|
|
|
nsDOMFileReader::GetError(nsISupports** aError)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2013-04-13 00:06:31 -07:00
|
|
|
NS_IF_ADDREF(*aError = GetError());
|
|
|
|
return NS_OK;
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
2011-06-30 17:50:44 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMFileReader::ReadAsArrayBuffer(nsIDOMBlob* aFile, JSContext* aCx)
|
|
|
|
{
|
2013-04-13 00:06:31 -07:00
|
|
|
NS_ENSURE_TRUE(aFile, NS_ERROR_NULL_POINTER);
|
|
|
|
ErrorResult rv;
|
|
|
|
ReadAsArrayBuffer(aCx, aFile, rv);
|
|
|
|
return rv.ErrorCode();
|
2011-06-30 17:50:44 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-04-13 00:06:31 -07:00
|
|
|
NS_ENSURE_TRUE(aFile, NS_ERROR_NULL_POINTER);
|
|
|
|
ErrorResult rv;
|
|
|
|
ReadAsBinaryString(aFile, rv);
|
|
|
|
return rv.ErrorCode();
|
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
|
|
|
{
|
2013-04-13 00:06:31 -07:00
|
|
|
NS_ENSURE_TRUE(aFile, NS_ERROR_NULL_POINTER);
|
|
|
|
ErrorResult rv;
|
|
|
|
ReadAsText(aFile, aCharset, rv);
|
|
|
|
return rv.ErrorCode();
|
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
|
|
|
{
|
2013-04-13 00:06:31 -07:00
|
|
|
NS_ENSURE_TRUE(aFile, NS_ERROR_NULL_POINTER);
|
|
|
|
ErrorResult rv;
|
|
|
|
ReadAsDataURL(aFile, rv);
|
|
|
|
return rv.ErrorCode();
|
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
|
|
|
{
|
2013-04-13 00:06:31 -07:00
|
|
|
ErrorResult rv;
|
|
|
|
FileIOObject::Abort(rv);
|
|
|
|
return rv.ErrorCode();
|
2011-09-29 09:06:35 -07:00
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2013-04-13 00:06:31 -07:00
|
|
|
/* virtual */ void
|
2011-09-29 09:06:35 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t* dest = static_cast<char16_t*>(closure) + toOffset;
|
|
|
|
char16_t* end = dest + count;
|
2009-11-21 03:56:31 -08:00
|
|
|
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-27 23:57:33 -07:00
|
|
|
if (uint64_t(oldLen) + aCount > UINT32_MAX)
|
2012-09-05 19:41:02 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t *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-11-14 09:56:26 -08:00
|
|
|
aInputStream->Read((char*)JS_GetArrayBufferData(mResultArrayBuffer) + aOffset,
|
2012-01-14 09:43:00 -08:00
|
|
|
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-27 23:57:33 -07:00
|
|
|
if (aOffset + aCount > UINT32_MAX) {
|
2012-09-05 19:41:02 -07:00
|
|
|
// PR_Realloc doesn't support over 4GB memory size even if 64-bit OS
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-12-05 14:20:57 -08:00
|
|
|
mFileData = (char *)moz_realloc(mFileData, aOffset + aCount);
|
2009-11-21 03:56:31 -08:00
|
|
|
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:
|
2013-12-17 02:47:25 -08:00
|
|
|
if (!mFileData) {
|
|
|
|
if (mDataLen) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rv = GetAsText(file, mCharset, "", mDataLen, mResult);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rv = GetAsText(file, 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
|
|
|
|
|
2013-04-13 00:06:31 -07:00
|
|
|
void
|
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,
|
2013-04-13 00:06:31 -07:00
|
|
|
eDataFormat aDataFormat,
|
|
|
|
ErrorResult& aRv)
|
2009-11-24 17:53:17 -08:00
|
|
|
{
|
2013-04-13 00:06:31 -07:00
|
|
|
MOZ_ASSERT(aFile);
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
//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;
|
2013-04-13 00:06:31 -07:00
|
|
|
aRv = NS_NewURI(getter_AddRefs(uri), urlHolder.mUrl);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv.ErrorCode());
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2012-09-17 08:24:39 -07:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
if (HasOrHasHadOwner()) {
|
2013-04-13 00:06:31 -07:00
|
|
|
if (!GetOwner()) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2012-09-17 08:24:39 -07:00
|
|
|
nsIDocument* doc = GetOwner()->GetExtantDoc();
|
|
|
|
if (doc) {
|
|
|
|
loadGroup = doc->GetDocumentLoadGroup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-13 00:06:31 -07:00
|
|
|
aRv = NS_NewChannel(getter_AddRefs(mChannel), uri, nullptr, loadGroup,
|
|
|
|
nullptr, nsIRequest::LOAD_BACKGROUND);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv.ErrorCode());
|
2010-09-05 11:00:05 -07:00
|
|
|
}
|
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
|
|
|
|
2013-04-13 00:06:31 -07:00
|
|
|
aRv = mChannel->AsyncOpen(this, nullptr);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv.ErrorCode());
|
2009-10-07 01:30:26 -07:00
|
|
|
|
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));
|
2013-04-13 00:06:31 -07:00
|
|
|
|
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");
|
2013-04-13 00:06:31 -07:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2011-06-30 17:50:44 -07:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-12-17 02:47:25 -08:00
|
|
|
nsDOMFileReader::GetAsText(nsIDOMBlob *aFile,
|
|
|
|
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
|
|
|
{
|
2013-12-17 02:47:25 -08:00
|
|
|
// The BOM sniffing is baked into the "decode" part of the Encoding
|
|
|
|
// Standard, which the File API references.
|
|
|
|
nsAutoCString encoding;
|
|
|
|
if (!nsContentUtils::CheckForBOM(
|
|
|
|
reinterpret_cast<const unsigned char *>(aFileData),
|
|
|
|
aDataLen,
|
|
|
|
encoding)) {
|
|
|
|
// BOM sniffing failed. Try the API argument.
|
|
|
|
if (!EncodingUtils::FindEncodingForLabel(aCharset,
|
|
|
|
encoding)) {
|
|
|
|
// API argument failed. Try the type property of the blob.
|
|
|
|
nsAutoString type16;
|
|
|
|
aFile->GetType(type16);
|
|
|
|
NS_ConvertUTF16toUTF8 type(type16);
|
|
|
|
nsAutoCString specifiedCharset;
|
|
|
|
bool haveCharset;
|
|
|
|
int32_t charsetStart, charsetEnd;
|
|
|
|
NS_ExtractCharsetFromContentType(type,
|
|
|
|
specifiedCharset,
|
|
|
|
&haveCharset,
|
|
|
|
&charsetStart,
|
|
|
|
&charsetEnd);
|
|
|
|
if (!EncodingUtils::FindEncodingForLabel(specifiedCharset, encoding)) {
|
|
|
|
// Type property failed. Use UTF-8.
|
|
|
|
encoding.AssignLiteral("UTF-8");
|
|
|
|
}
|
|
|
|
}
|
2012-11-07 15:04:22 -08:00
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2013-12-17 02:47:25 -08:00
|
|
|
nsDependentCSubstring data(aFileData, aDataLen);
|
|
|
|
return nsContentUtils::ConvertStringFromEncoding(encoding, data, aResult);
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-05 14:20:57 -08:00
|
|
|
nsCString encodedData;
|
|
|
|
rv = Base64Encode(Substring(aFileData, aDataLen), encodedData);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-12-05 22:09:30 -08:00
|
|
|
|
2014-02-11 06:22:45 -08:00
|
|
|
if (!AppendASCIItoUTF16(encodedData, aResult, fallible_t())) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-13 00:06:31 -07:00
|
|
|
/* virtual */ JSObject*
|
2013-04-25 09:29:54 -07:00
|
|
|
nsDOMFileReader::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2013-04-13 00:06:31 -07:00
|
|
|
{
|
|
|
|
return FileReaderBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|