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 "nsIFile.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2012-12-05 14:20:57 -08:00
|
|
|
#include "mozilla/Base64.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-10-03 12:11:31 -07:00
|
|
|
#include "nsDOMJSUtils.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
|
|
|
|
2014-06-10 11:48:36 -07:00
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIStreamTransportService.h"
|
|
|
|
|
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"
|
|
|
|
|
2014-06-10 11:48:36 -07:00
|
|
|
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
|
|
|
|
|
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)
|
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)
|
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,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
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(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
|
|
|
// 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...
|
2014-08-18 10:57:30 -07:00
|
|
|
BindToOwner(xpc::GetNativeForGlobal(xpc::PrivilegedJunkScope()));
|
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);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-11 13:26:52 -07:00
|
|
|
void
|
|
|
|
nsDOMFileReader::GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
|
|
|
|
ErrorResult& aRv)
|
2013-04-13 00:06:31 -07:00
|
|
|
{
|
2014-06-11 13:26:52 -07:00
|
|
|
aRv = GetResult(aCx, aResult);
|
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;
|
2014-06-10 11:48:36 -07:00
|
|
|
|
|
|
|
if (mAsyncStream) {
|
|
|
|
mAsyncStream = 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
|
2014-06-10 11:48:36 -07:00
|
|
|
nsDOMFileReader::DoOnLoadEnd(nsresult aStatus,
|
|
|
|
nsAString& aSuccessEvent,
|
|
|
|
nsAString& aTerminationEvent)
|
2009-10-07 01:30:26 -07:00
|
|
|
{
|
2012-09-05 19:41:02 -07:00
|
|
|
|
2012-08-28 07:59:18 -07:00
|
|
|
// Make sure we drop all the objects that could hold files open now.
|
2014-06-10 11:48:36 -07:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> stream;
|
|
|
|
mAsyncStream.swap(stream);
|
2012-08-28 07:59:18 -07:00
|
|
|
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;
|
|
|
|
}
|
2014-06-10 11:48:36 -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
|
|
|
}
|
|
|
|
|
2014-06-10 11:48:36 -07:00
|
|
|
nsresult
|
|
|
|
nsDOMFileReader::DoReadData(nsIAsyncInputStream* aStream, uint64_t aCount)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStream);
|
|
|
|
|
|
|
|
if (mDataFormat == FILE_AS_BINARY) {
|
|
|
|
//Continuously update our binary string as data comes in
|
|
|
|
uint32_t oldLen = mResult.Length();
|
|
|
|
NS_ASSERTION(mResult.Length() == mDataLen,
|
|
|
|
"unexpected mResult length");
|
|
|
|
if (uint64_t(oldLen) + aCount > UINT32_MAX)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
char16_t *buf = nullptr;
|
|
|
|
mResult.GetMutableData(&buf, oldLen + aCount, fallible_t());
|
|
|
|
NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
uint32_t bytesRead = 0;
|
|
|
|
aStream->ReadSegments(ReadFuncBinaryString, buf + oldLen, aCount,
|
|
|
|
&bytesRead);
|
|
|
|
NS_ASSERTION(bytesRead == aCount, "failed to read data");
|
|
|
|
}
|
|
|
|
else if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
uint32_t bytesRead = 0;
|
|
|
|
aStream->Read((char*) JS_GetArrayBufferData(mResultArrayBuffer) + mDataLen,
|
|
|
|
aCount, &bytesRead);
|
|
|
|
NS_ASSERTION(bytesRead == aCount, "failed to read data");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//Update memory buffer to reflect the contents of the file
|
|
|
|
if (mDataLen + aCount > UINT32_MAX) {
|
|
|
|
// PR_Realloc doesn't support over 4GB memory size even if 64-bit OS
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mFileData = (char *) moz_realloc(mFileData, mDataLen + aCount);
|
|
|
|
NS_ENSURE_TRUE(mFileData, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
uint32_t bytesRead = 0;
|
|
|
|
aStream->Read(mFileData + mDataLen, aCount, &bytesRead);
|
|
|
|
NS_ASSERTION(bytesRead == aCount, "failed to read data");
|
|
|
|
}
|
|
|
|
|
|
|
|
mDataLen += aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-06-10 11:48:36 -07:00
|
|
|
nsresult rv;
|
2012-09-17 08:24:39 -07:00
|
|
|
|
2014-06-10 11:48:36 -07:00
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(kStreamTransportServiceCID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
2010-09-05 11:00:05 -07:00
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2014-06-10 11:48:36 -07:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
rv = mFile->GetInternalStream(getter_AddRefs(stream));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransport> transport;
|
|
|
|
rv = sts->CreateInputTransport(stream,
|
|
|
|
/* aStartOffset */ 0,
|
|
|
|
/* aReadLimit */ -1,
|
|
|
|
/* aCloseWhenDone */ true,
|
|
|
|
getter_AddRefs(transport));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> wrapper;
|
|
|
|
rv = transport->OpenInputStream(/* aFlags */ 0,
|
|
|
|
/* aSegmentSize */ 0,
|
|
|
|
/* aSegmentCount */ 0,
|
|
|
|
getter_AddRefs(wrapper));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mAsyncStream);
|
|
|
|
mAsyncStream = do_QueryInterface(wrapper);
|
|
|
|
MOZ_ASSERT(mAsyncStream);
|
|
|
|
|
2011-09-29 09:06:35 -07:00
|
|
|
mTotal = mozilla::dom::kUnknownSize;
|
|
|
|
mFile->GetSize(&mTotal);
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2014-06-10 11:48:36 -07:00
|
|
|
rv = DoAsyncWait(mAsyncStream);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
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*
|
2014-04-08 15:27:18 -07:00
|
|
|
nsDOMFileReader::WrapObject(JSContext* aCx)
|
2013-04-13 00:06:31 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return FileReaderBinding::Wrap(aCx, this);
|
2013-04-13 00:06:31 -07:00
|
|
|
}
|