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"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2009-10-07 08:30:26 +00: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 22:20:57 +00:00
|
|
|
#include "mozilla/Base64.h"
|
2012-11-07 23:04:22 +00:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2014-10-08 16:15:23 +00:00
|
|
|
#include "mozilla/dom/File.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-10-03 19:11:31 +00:00
|
|
|
#include "nsDOMJSUtils.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
|
|
|
|
2014-06-10 18:48:36 +00:00
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIStreamTransportService.h"
|
|
|
|
|
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"
|
|
|
|
|
2014-06-10 18:48:36 +00:00
|
|
|
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
|
|
|
|
|
2013-08-02 01:29:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMFileReader)
|
|
|
|
|
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)
|
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)
|
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,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
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(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()
|
|
|
|
{
|
2013-08-16 20:10:17 +00:00
|
|
|
mozilla::HoldJSObjects(this);
|
2011-07-01 00:50:44 +00:00
|
|
|
}
|
|
|
|
|
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),
|
2013-04-19 22:18:33 +00:00
|
|
|
mResultArrayBuffer(nullptr)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2011-07-01 00:50:44 +00:00
|
|
|
SetDOMStringToNull(mResult);
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
2009-11-09 20:05:16 +00:00
|
|
|
nsDOMFileReader::~nsDOMFileReader()
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2009-11-23 06:23:23 +00:00
|
|
|
FreeFileData();
|
2013-05-15 11:48:55 +00:00
|
|
|
mResultArrayBuffer = nullptr;
|
2013-08-16 20:10:17 +00:00
|
|
|
mozilla::DropJSObjects(this);
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 17:24:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This Init method is called from the factory constructor.
|
|
|
|
*/
|
2009-10-07 08:30:26 +00:00
|
|
|
nsresult
|
2009-11-09 20:05:16 +00:00
|
|
|
nsDOMFileReader::Init()
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2013-04-22 17:24:48 +00:00
|
|
|
// Instead of grabbing some random global from the context stack,
|
2013-07-16 13:04:28 +00:00
|
|
|
// let's use the default one (junk scope) for now.
|
2013-04-22 17:24:48 +00:00
|
|
|
// We should move away from this Init...
|
2014-09-21 19:31:53 +00:00
|
|
|
BindToOwner(xpc::NativeGlobal(xpc::PrivilegedJunkScope()));
|
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();
|
|
|
|
|
2013-08-23 05:17:08 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> owner = do_QueryInterface(aGlobal.GetAsSupports());
|
2012-03-13 00:56:07 +00:00
|
|
|
if (!owner) {
|
2014-10-21 18:45:28 +00:00
|
|
|
NS_WARNING("Unexpected 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);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-11 20:26:52 +00:00
|
|
|
void
|
|
|
|
nsDOMFileReader::GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
|
|
|
|
ErrorResult& aRv)
|
2013-04-13 07:06:31 +00:00
|
|
|
{
|
2014-06-11 20:26:52 +00:00
|
|
|
aRv = GetResult(aCx, aResult);
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 17:39:36 +00:00
|
|
|
nsDOMFileReader::GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2013-10-26 16:19:05 +00:00
|
|
|
JS::Rooted<JS::Value> result(aCx);
|
2011-07-01 00:50:44 +00:00
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
if (mReadyState == nsIDOMFileReader::DONE && mResultArrayBuffer) {
|
2013-10-26 16:19:05 +00:00
|
|
|
result.setObject(*mResultArrayBuffer);
|
2011-07-01 00:50:44 +00:00
|
|
|
} else {
|
2013-10-26 16:19:05 +00:00
|
|
|
result.setNull();
|
2011-07-01 00:50:44 +00:00
|
|
|
}
|
2013-10-26 16:19:05 +00:00
|
|
|
if (!JS_WrapValue(aCx, &result)) {
|
2012-03-24 00:04:55 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-01-09 17:39:36 +00:00
|
|
|
aResult.set(result);
|
2011-07-01 00:50:44 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-10-26 16:19:05 +00:00
|
|
|
|
2011-07-01 00:50:44 +00:00
|
|
|
nsString tmpResult = mResult;
|
2014-01-09 17:39:36 +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
|
2013-05-18 17:52:06 +00:00
|
|
|
nsDOMFileReader::GetError(nsISupports** 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;
|
2014-10-08 16:15:23 +00:00
|
|
|
nsRefPtr<File> file = static_cast<File*>(aFile);
|
2014-10-08 16:15:22 +00:00
|
|
|
ReadAsArrayBuffer(aCx, *file, rv);
|
2013-04-13 07:06:31 +00:00
|
|
|
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;
|
2014-10-08 16:15:23 +00:00
|
|
|
nsRefPtr<File> file = static_cast<File*>(aFile);
|
2014-10-08 16:15:22 +00:00
|
|
|
ReadAsBinaryString(*file, rv);
|
2013-04-13 07:06:31 +00:00
|
|
|
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;
|
2014-10-08 16:15:23 +00:00
|
|
|
nsRefPtr<File> file = static_cast<File*>(aFile);
|
2014-10-08 16:15:22 +00:00
|
|
|
ReadAsText(*file, aCharset, rv);
|
2013-04-13 07:06:31 +00:00
|
|
|
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;
|
2014-10-08 16:15:23 +00:00
|
|
|
nsRefPtr<File> file = static_cast<File*>(aFile);
|
2014-10-08 16:15:22 +00:00
|
|
|
ReadAsDataURL(*file, rv);
|
2013-04-13 07:06:31 +00:00
|
|
|
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;
|
2014-06-10 18:48:36 +00:00
|
|
|
|
|
|
|
if (mAsyncStream) {
|
|
|
|
mAsyncStream = 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
|
|
|
{
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t* dest = static_cast<char16_t*>(closure) + toOffset;
|
|
|
|
char16_t* end = dest + count;
|
2009-11-21 11:56:31 +00:00
|
|
|
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
|
2014-06-10 18:48:36 +00:00
|
|
|
nsDOMFileReader::DoOnLoadEnd(nsresult aStatus,
|
|
|
|
nsAString& aSuccessEvent,
|
|
|
|
nsAString& aTerminationEvent)
|
2009-10-07 08:30:26 +00:00
|
|
|
{
|
2012-09-06 02:41:02 +00:00
|
|
|
|
2012-08-28 14:59:18 +00:00
|
|
|
// Make sure we drop all the objects that could hold files open now.
|
2014-06-10 18:48:36 +00:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> stream;
|
|
|
|
mAsyncStream.swap(stream);
|
2012-08-28 14:59:18 +00:00
|
|
|
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) {
|
2014-10-07 17:44:07 +00:00
|
|
|
case FILE_AS_ARRAYBUFFER: {
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mozilla::DOMEventTargetHelper::GetParentObject()))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RootResultArrayBuffer();
|
|
|
|
mResultArrayBuffer = JS_NewArrayBufferWithContents(jsapi.cx(), mTotal, mFileData);
|
|
|
|
if (!mResultArrayBuffer) {
|
|
|
|
JS_ClearPendingException(jsapi.cx());
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
|
|
|
mFileData = nullptr; // Transfer ownership
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
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:
|
2013-12-17 10:47:25 +00: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 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;
|
|
|
|
}
|
2014-06-10 18:48:36 +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
|
|
|
}
|
|
|
|
|
2014-06-10 18:48:36 +00: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 {
|
|
|
|
//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;
|
|
|
|
}
|
2014-10-07 17:44:07 +00:00
|
|
|
if (mDataFormat != FILE_AS_ARRAYBUFFER) {
|
|
|
|
mFileData = (char *) moz_realloc(mFileData, mDataLen + aCount);
|
|
|
|
NS_ENSURE_TRUE(mFileData, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
2014-06-10 18:48:36 +00:00
|
|
|
|
|
|
|
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 08:30:26 +00:00
|
|
|
// Helper methods
|
|
|
|
|
2013-04-13 07:06:31 +00:00
|
|
|
void
|
2014-10-07 17:44:07 +00:00
|
|
|
nsDOMFileReader::ReadFileContent(File& 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
|
|
|
{
|
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();
|
|
|
|
|
2014-10-08 16:15:22 +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
|
|
|
|
2014-06-10 18:48:36 +00:00
|
|
|
nsresult rv;
|
2012-09-17 15:24:39 +00:00
|
|
|
|
2014-06-10 18:48:36 +00:00
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(kStreamTransportServiceCID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
2010-09-05 18:00:05 +00:00
|
|
|
}
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2014-06-10 18:48:36 +00: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 16:06:35 +00:00
|
|
|
mTotal = mozilla::dom::kUnknownSize;
|
|
|
|
mFile->GetSize(&mTotal);
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2014-06-10 18:48:36 +00:00
|
|
|
rv = DoAsyncWait(mAsyncStream);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
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) {
|
2014-10-07 17:44:07 +00:00
|
|
|
mFileData = js_pod_malloc<char>(mTotal);
|
|
|
|
if (!mFileData) {
|
|
|
|
NS_WARNING("Preallocation failed for ReadFileData");
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2011-07-01 00:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-12-17 10:47:25 +00:00
|
|
|
nsDOMFileReader::GetAsText(nsIDOMBlob *aFile,
|
|
|
|
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
|
|
|
{
|
2013-12-17 10:47:25 +00: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 23:04:22 +00:00
|
|
|
}
|
2009-10-07 08:30:26 +00:00
|
|
|
|
2013-12-17 10:47:25 +00:00
|
|
|
nsDependentCSubstring data(aFileData, aDataLen);
|
|
|
|
return nsContentUtils::ConvertStringFromEncoding(encoding, data, aResult);
|
2009-10-07 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-02-11 14:22:45 +00:00
|
|
|
if (!AppendASCIItoUTF16(encodedData, aResult, fallible_t())) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2009-10-07 08:30:26 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-13 07:06:31 +00:00
|
|
|
/* virtual */ JSObject*
|
2014-04-08 22:27:18 +00:00
|
|
|
nsDOMFileReader::WrapObject(JSContext* aCx)
|
2013-04-13 07:06:31 +00: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 22:27:17 +00:00
|
|
|
return FileReaderBinding::Wrap(aCx, this);
|
2013-04-13 07:06:31 +00:00
|
|
|
}
|