2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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 12:30:26 +04:00
|
|
|
|
2009-11-09 23:05:16 +03:00
|
|
|
#include "nsDOMFileReader.h"
|
2009-10-07 12:30:26 +04:00
|
|
|
|
|
|
|
#include "nsContentCID.h"
|
|
|
|
#include "nsContentUtils.h"
|
2011-10-03 23:11:31 +04:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2009-10-07 12:30:26 +04:00
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsNetCID.h"
|
2015-09-29 07:31:56 +03:00
|
|
|
#include "nsNetUtil.h"
|
2009-10-07 12:30:26 +04:00
|
|
|
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2012-12-06 02:20:57 +04:00
|
|
|
#include "mozilla/Base64.h"
|
2012-11-08 03:04:22 +04:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2013-04-13 11:06:31 +04:00
|
|
|
#include "mozilla/dom/FileReaderBinding.h"
|
2011-09-29 20:06:36 +04:00
|
|
|
#include "xpcpublic.h"
|
2011-10-03 23:11:31 +04:00
|
|
|
#include "nsDOMJSUtils.h"
|
2011-05-27 08:53:03 +04:00
|
|
|
|
2012-01-14 21:43:00 +04:00
|
|
|
#include "jsfriendapi.h"
|
2011-12-24 12:27:04 +04:00
|
|
|
|
2014-06-10 22:48:36 +04:00
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIStreamTransportService.h"
|
|
|
|
|
2011-05-27 08:53:03 +04:00
|
|
|
using namespace mozilla;
|
2013-04-13 11:06:31 +04:00
|
|
|
using namespace mozilla::dom;
|
2009-10-07 12:30:26 +04:00
|
|
|
|
|
|
|
#define LOAD_STR "load"
|
|
|
|
#define LOADSTART_STR "loadstart"
|
|
|
|
#define LOADEND_STR "loadend"
|
|
|
|
|
2014-06-10 22:48:36 +04:00
|
|
|
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMFileReader)
|
|
|
|
|
2009-11-09 23:05:16 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMFileReader,
|
2011-09-29 20:06:35 +04:00
|
|
|
FileIOObject)
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBlob)
|
2009-10-07 12:30:26 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2009-11-09 23:05:16 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMFileReader,
|
2011-09-29 20:06:35 +04:00
|
|
|
FileIOObject)
|
2012-07-30 18:20:58 +04:00
|
|
|
tmp->mResultArrayBuffer = nullptr;
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBlob)
|
2009-10-07 12:30:26 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2011-07-01 04:50:44 +04:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(nsDOMFileReader,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2012-06-11 03:44:50 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mResultArrayBuffer)
|
2011-07-01 04:50:44 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2009-11-09 23:05:16 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMFileReader)
|
2013-04-13 11:06:31 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2009-11-09 23:05:16 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMFileReader)
|
2009-10-07 12:30:26 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2011-09-29 20:06:35 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(FileIOObject)
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2011-09-29 20:06:35 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsDOMFileReader, FileIOObject)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsDOMFileReader, FileIOObject)
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2012-08-31 07:45:16 +04: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 04:50:44 +04:00
|
|
|
void
|
|
|
|
nsDOMFileReader::RootResultArrayBuffer()
|
|
|
|
{
|
2013-08-17 00:10:17 +04:00
|
|
|
mozilla::HoldJSObjects(this);
|
2011-07-01 04:50:44 +04:00
|
|
|
}
|
|
|
|
|
2009-11-09 23:05:16 +03:00
|
|
|
//nsDOMFileReader constructors/initializers
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2009-11-09 23:05:16 +03:00
|
|
|
nsDOMFileReader::nsDOMFileReader()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mFileData(nullptr),
|
2009-11-25 04:53:17 +03:00
|
|
|
mDataLen(0), mDataFormat(FILE_AS_BINARY),
|
2013-04-20 02:18:33 +04:00
|
|
|
mResultArrayBuffer(nullptr)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2011-07-01 04:50:44 +04:00
|
|
|
SetDOMStringToNull(mResult);
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2009-11-09 23:05:16 +03:00
|
|
|
nsDOMFileReader::~nsDOMFileReader()
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2009-11-23 09:23:23 +03:00
|
|
|
FreeFileData();
|
2013-05-15 15:48:55 +04:00
|
|
|
mResultArrayBuffer = nullptr;
|
2013-08-17 00:10:17 +04:00
|
|
|
mozilla::DropJSObjects(this);
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2013-04-22 21:24:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This Init method is called from the factory constructor.
|
|
|
|
*/
|
2009-10-07 12:30:26 +04:00
|
|
|
nsresult
|
2009-11-09 23:05:16 +03:00
|
|
|
nsDOMFileReader::Init()
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2013-04-22 21:24:48 +04:00
|
|
|
// Instead of grabbing some random global from the context stack,
|
2013-07-16 17:04:28 +04:00
|
|
|
// let's use the default one (junk scope) for now.
|
2013-04-22 21:24:48 +04:00
|
|
|
// We should move away from this Init...
|
2014-09-21 23:31:53 +04:00
|
|
|
BindToOwner(xpc::NativeGlobal(xpc::PrivilegedJunkScope()));
|
2009-10-07 12:30:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-13 11:06:31 +04:00
|
|
|
/* static */ already_AddRefed<nsDOMFileReader>
|
|
|
|
nsDOMFileReader::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2013-04-13 11:06:31 +04:00
|
|
|
nsRefPtr<nsDOMFileReader> fileReader = new nsDOMFileReader();
|
|
|
|
|
2013-08-23 09:17:08 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> owner = do_QueryInterface(aGlobal.GetAsSupports());
|
2012-03-13 04:56:07 +04:00
|
|
|
if (!owner) {
|
2014-10-21 22:45:28 +04:00
|
|
|
NS_WARNING("Unexpected owner");
|
2013-04-13 11:06:31 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2013-04-13 11:06:31 +04:00
|
|
|
fileReader->BindToOwner(owner);
|
|
|
|
return fileReader.forget();
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIInterfaceRequestor
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-11-09 23:05:16 +03:00
|
|
|
nsDOMFileReader::GetInterface(const nsIID & aIID, void **aResult)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2009-11-09 23:05:16 +03:00
|
|
|
// nsIDOMFileReader
|
2009-10-07 12:30:26 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDOMFileReader::GetReadyState(uint16_t *aReadyState)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2013-04-13 11:06:31 +04:00
|
|
|
*aReadyState = ReadyState();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
void
|
|
|
|
nsDOMFileReader::GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
|
|
|
|
ErrorResult& aRv)
|
2013-04-13 11:06:31 +04:00
|
|
|
{
|
2014-06-12 00:26:52 +04:00
|
|
|
aRv = GetResult(aCx, aResult);
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 21:39:36 +04:00
|
|
|
nsDOMFileReader::GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2013-10-26 20:19:05 +04:00
|
|
|
JS::Rooted<JS::Value> result(aCx);
|
2011-07-01 04:50:44 +04:00
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
|
|
if (mReadyState == nsIDOMFileReader::DONE && mResultArrayBuffer) {
|
2013-10-26 20:19:05 +04:00
|
|
|
result.setObject(*mResultArrayBuffer);
|
2011-07-01 04:50:44 +04:00
|
|
|
} else {
|
2013-10-26 20:19:05 +04:00
|
|
|
result.setNull();
|
2011-07-01 04:50:44 +04:00
|
|
|
}
|
2013-10-26 20:19:05 +04:00
|
|
|
if (!JS_WrapValue(aCx, &result)) {
|
2012-03-24 04:04:55 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-01-09 21:39:36 +04:00
|
|
|
aResult.set(result);
|
2011-07-01 04:50:44 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-10-26 20:19:05 +04:00
|
|
|
|
2011-07-01 04:50:44 +04:00
|
|
|
nsString tmpResult = mResult;
|
2014-01-09 21:39:36 +04:00
|
|
|
if (!xpc::StringToJsval(aCx, tmpResult, aResult)) {
|
2011-07-01 04:50:44 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-05-18 21:52:06 +04:00
|
|
|
nsDOMFileReader::GetError(nsISupports** aError)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2013-04-13 11:06:31 +04:00
|
|
|
NS_IF_ADDREF(*aError = GetError());
|
|
|
|
return NS_OK;
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2011-07-01 04:50:44 +04:00
|
|
|
NS_IMETHODIMP
|
2015-05-12 15:09:51 +03:00
|
|
|
nsDOMFileReader::ReadAsArrayBuffer(nsIDOMBlob* aBlob, JSContext* aCx)
|
2011-07-01 04:50:44 +04:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_ENSURE_TRUE(aBlob, NS_ERROR_NULL_POINTER);
|
2013-04-13 11:06:31 +04:00
|
|
|
ErrorResult rv;
|
2015-05-12 15:09:51 +03:00
|
|
|
nsRefPtr<Blob> blob = static_cast<Blob*>(aBlob);
|
|
|
|
ReadAsArrayBuffer(aCx, *blob, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2011-07-01 04:50:44 +04:00
|
|
|
}
|
|
|
|
|
2009-10-07 12:30:26 +04:00
|
|
|
NS_IMETHODIMP
|
2015-05-12 15:09:51 +03:00
|
|
|
nsDOMFileReader::ReadAsBinaryString(nsIDOMBlob* aBlob)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_ENSURE_TRUE(aBlob, NS_ERROR_NULL_POINTER);
|
2013-04-13 11:06:31 +04:00
|
|
|
ErrorResult rv;
|
2015-05-12 15:09:51 +03:00
|
|
|
nsRefPtr<Blob> blob = static_cast<Blob*>(aBlob);
|
|
|
|
ReadAsBinaryString(*blob, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-12 15:09:51 +03:00
|
|
|
nsDOMFileReader::ReadAsText(nsIDOMBlob* aBlob,
|
2009-11-09 23:05:16 +03:00
|
|
|
const nsAString &aCharset)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_ENSURE_TRUE(aBlob, NS_ERROR_NULL_POINTER);
|
2013-04-13 11:06:31 +04:00
|
|
|
ErrorResult rv;
|
2015-05-12 15:09:51 +03:00
|
|
|
nsRefPtr<Blob> blob = static_cast<Blob*>(aBlob);
|
|
|
|
ReadAsText(*blob, aCharset, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-12 15:09:51 +03:00
|
|
|
nsDOMFileReader::ReadAsDataURL(nsIDOMBlob* aBlob)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_ENSURE_TRUE(aBlob, NS_ERROR_NULL_POINTER);
|
2013-04-13 11:06:31 +04:00
|
|
|
ErrorResult rv;
|
2015-05-12 15:09:51 +03:00
|
|
|
nsRefPtr<Blob> blob = static_cast<Blob*>(aBlob);
|
|
|
|
ReadAsDataURL(*blob, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-11-09 23:05:16 +03:00
|
|
|
nsDOMFileReader::Abort()
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2013-04-13 11:06:31 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
FileIOObject::Abort(rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2011-09-29 20:06:35 +04:00
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2013-04-13 11:06:31 +04:00
|
|
|
/* virtual */ void
|
2011-09-29 20:06:35 +04:00
|
|
|
nsDOMFileReader::DoAbort(nsAString& aEvent)
|
|
|
|
{
|
|
|
|
// Revert status and result attributes
|
2009-11-09 23:05:16 +03:00
|
|
|
SetDOMStringToNull(mResult);
|
2012-07-30 18:20:58 +04:00
|
|
|
mResultArrayBuffer = nullptr;
|
2014-06-10 22:48:36 +04:00
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
mAsyncStream = nullptr;
|
2015-05-12 15:09:51 +03:00
|
|
|
mBlob = nullptr;
|
2009-10-07 12:30:26 +04:00
|
|
|
|
|
|
|
//Clean up memory buffer
|
2009-11-23 09:23:23 +03:00
|
|
|
FreeFileData();
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2011-09-29 20:06:35 +04:00
|
|
|
// Tell the base class which event to dispatch
|
|
|
|
aEvent = NS_LITERAL_STRING(LOADEND_STR);
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2009-11-21 14:56:31 +03:00
|
|
|
static
|
|
|
|
NS_METHOD
|
|
|
|
ReadFuncBinaryString(nsIInputStream* in,
|
|
|
|
void* closure,
|
|
|
|
const char* fromRawSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t toOffset,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t *writeCount)
|
2009-11-21 14:56:31 +03:00
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* dest = static_cast<char16_t*>(closure) + toOffset;
|
|
|
|
char16_t* end = dest + count;
|
2009-11-21 14:56:31 +03:00
|
|
|
const unsigned char* source = (const unsigned char*)fromRawSegment;
|
|
|
|
while (dest != end) {
|
|
|
|
*dest = *source;
|
|
|
|
++dest;
|
|
|
|
++source;
|
|
|
|
}
|
|
|
|
*writeCount = count;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 20:06:35 +04:00
|
|
|
nsresult
|
2014-06-10 22:48:36 +04:00
|
|
|
nsDOMFileReader::DoOnLoadEnd(nsresult aStatus,
|
|
|
|
nsAString& aSuccessEvent,
|
|
|
|
nsAString& aTerminationEvent)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2012-09-06 06:41:02 +04:00
|
|
|
|
2012-08-28 18:59:18 +04:00
|
|
|
// Make sure we drop all the objects that could hold files open now.
|
2014-06-10 22:48:36 +04:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> stream;
|
|
|
|
mAsyncStream.swap(stream);
|
2015-05-19 17:36:37 +03:00
|
|
|
|
|
|
|
nsRefPtr<Blob> blob;
|
2015-05-12 15:09:51 +03:00
|
|
|
mBlob.swap(blob);
|
2012-08-28 18:59:18 +04:00
|
|
|
|
2011-09-29 20:06:35 +04:00
|
|
|
aSuccessEvent = NS_LITERAL_STRING(LOAD_STR);
|
|
|
|
aTerminationEvent = NS_LITERAL_STRING(LOADEND_STR);
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2011-09-29 20:06:35 +04:00
|
|
|
// Clear out the data if necessary
|
2009-10-07 12:30:26 +04:00
|
|
|
if (NS_FAILED(aStatus)) {
|
2009-11-23 09:23:23 +03:00
|
|
|
FreeFileData();
|
2009-10-07 12:30:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-25 04:53:17 +03:00
|
|
|
nsresult rv = NS_OK;
|
2009-10-07 12:30:26 +04:00
|
|
|
switch (mDataFormat) {
|
2014-10-07 21:44:07 +04: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 12:30:26 +04:00
|
|
|
case FILE_AS_BINARY:
|
2009-11-09 23:05:16 +03:00
|
|
|
break; //Already accumulated mResult
|
2009-10-07 12:30:26 +04:00
|
|
|
case FILE_AS_TEXT:
|
2013-12-17 14:47:25 +04:00
|
|
|
if (!mFileData) {
|
|
|
|
if (mDataLen) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2015-05-12 15:09:51 +03:00
|
|
|
rv = GetAsText(blob, mCharset, "", mDataLen, mResult);
|
2013-12-17 14:47:25 +04:00
|
|
|
break;
|
|
|
|
}
|
2015-05-12 15:09:51 +03:00
|
|
|
rv = GetAsText(blob, mCharset, mFileData, mDataLen, mResult);
|
2009-10-07 12:30:26 +04:00
|
|
|
break;
|
|
|
|
case FILE_AS_DATAURL:
|
2015-05-12 15:09:51 +03:00
|
|
|
rv = GetAsDataURL(blob, mFileData, mDataLen, mResult);
|
2009-10-07 12:30:26 +04:00
|
|
|
break;
|
|
|
|
}
|
2014-06-10 22:48:36 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mResult.SetIsVoid(false);
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2009-11-23 09:23:23 +03:00
|
|
|
FreeFileData();
|
|
|
|
|
2011-09-29 20:06:35 +04:00
|
|
|
return rv;
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
2014-06-10 22:48:36 +04: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;
|
2015-01-28 12:00:40 +03:00
|
|
|
mResult.GetMutableData(&buf, oldLen + aCount, fallible);
|
2014-06-10 22:48:36 +04:00
|
|
|
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 21:44:07 +04:00
|
|
|
if (mDataFormat != FILE_AS_ARRAYBUFFER) {
|
2015-02-19 07:51:06 +03:00
|
|
|
mFileData = (char *) realloc(mFileData, mDataLen + aCount);
|
2014-10-07 21:44:07 +04:00
|
|
|
NS_ENSURE_TRUE(mFileData, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
2014-06-10 22:48:36 +04: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 12:30:26 +04:00
|
|
|
// Helper methods
|
|
|
|
|
2013-04-13 11:06:31 +04:00
|
|
|
void
|
2015-05-12 15:09:51 +03:00
|
|
|
nsDOMFileReader::ReadFileContent(Blob& aBlob,
|
2009-11-09 23:05:16 +03:00
|
|
|
const nsAString &aCharset,
|
2013-04-13 11:06:31 +04:00
|
|
|
eDataFormat aDataFormat,
|
|
|
|
ErrorResult& aRv)
|
2009-11-25 04:53:17 +03:00
|
|
|
{
|
2009-10-07 12:30:26 +04:00
|
|
|
//Implicit abort to clear any other activity going on
|
|
|
|
Abort();
|
2012-07-30 18:20:58 +04:00
|
|
|
mError = nullptr;
|
2009-11-22 22:05:12 +03:00
|
|
|
SetDOMStringToNull(mResult);
|
2011-09-29 20:06:35 +04:00
|
|
|
mTransferred = 0;
|
|
|
|
mTotal = 0;
|
2009-11-23 09:23:23 +03:00
|
|
|
mReadyState = nsIDOMFileReader::EMPTY;
|
|
|
|
FreeFileData();
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
mBlob = &aBlob;
|
2009-11-23 09:23:23 +03:00
|
|
|
mDataFormat = aDataFormat;
|
2010-10-07 20:36:23 +04:00
|
|
|
CopyUTF16toUTF8(aCharset, mCharset);
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2014-06-10 22:48:36 +04:00
|
|
|
nsresult rv;
|
2012-09-17 19:24:39 +04:00
|
|
|
|
2014-06-10 22:48:36 +04:00
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(kStreamTransportServiceCID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
2010-09-05 22:00:05 +04:00
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2014-06-10 22:48:36 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2015-05-19 17:36:37 +03:00
|
|
|
mBlob->GetInternalStream(getter_AddRefs(stream), aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2014-06-10 22:48:36 +04:00
|
|
|
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);
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
mTotal = mBlob->GetSize(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2014-06-10 22:48:36 +04:00
|
|
|
rv = DoAsyncWait(mAsyncStream);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2009-11-09 23:05:16 +03:00
|
|
|
//FileReader should be in loading state here
|
|
|
|
mReadyState = nsIDOMFileReader::LOADING;
|
2009-10-07 12:30:26 +04:00
|
|
|
DispatchProgressEvent(NS_LITERAL_STRING(LOADSTART_STR));
|
2013-04-13 11:06:31 +04:00
|
|
|
|
2011-07-01 04:50:44 +04:00
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
2014-10-07 21:44:07 +04: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 04:50:44 +04:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-05-19 17:36:37 +03:00
|
|
|
nsDOMFileReader::GetAsText(Blob *aBlob,
|
2013-12-17 14:47:25 +04:00
|
|
|
const nsACString &aCharset,
|
2009-11-09 23:05:16 +03:00
|
|
|
const char *aFileData,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aDataLen,
|
2009-11-09 23:05:16 +03:00
|
|
|
nsAString& aResult)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
2013-12-17 14:47:25 +04: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;
|
2015-05-12 15:09:51 +03:00
|
|
|
aBlob->GetType(type16);
|
2013-12-17 14:47:25 +04:00
|
|
|
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-08 03:04:22 +04:00
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
2013-12-17 14:47:25 +04:00
|
|
|
nsDependentCSubstring data(aFileData, aDataLen);
|
|
|
|
return nsContentUtils::ConvertStringFromEncoding(encoding, data, aResult);
|
2009-10-07 12:30:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-05-19 17:36:37 +03:00
|
|
|
nsDOMFileReader::GetAsDataURL(Blob *aBlob,
|
2009-11-09 23:05:16 +03:00
|
|
|
const char *aFileData,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aDataLen,
|
2009-11-09 23:05:16 +03:00
|
|
|
nsAString& aResult)
|
2009-10-07 12:30:26 +04:00
|
|
|
{
|
|
|
|
aResult.AssignLiteral("data:");
|
|
|
|
|
2010-09-05 22:00:05 +04:00
|
|
|
nsString contentType;
|
2015-05-19 17:36:37 +03:00
|
|
|
aBlob->GetType(contentType);
|
|
|
|
if (!contentType.IsEmpty()) {
|
2010-09-05 22:00:05 +04:00
|
|
|
aResult.Append(contentType);
|
2009-10-07 12:30:26 +04:00
|
|
|
} else {
|
|
|
|
aResult.AppendLiteral("application/octet-stream");
|
|
|
|
}
|
|
|
|
aResult.AppendLiteral(";base64,");
|
|
|
|
|
2012-12-06 02:20:57 +04:00
|
|
|
nsCString encodedData;
|
2015-05-19 17:36:37 +03:00
|
|
|
nsresult rv = Base64Encode(Substring(aFileData, aDataLen), encodedData);
|
2012-12-06 02:20:57 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-12-06 10:09:30 +04:00
|
|
|
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!AppendASCIItoUTF16(encodedData, aResult, fallible)) {
|
2014-02-11 18:22:45 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2009-10-07 12:30:26 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-13 11:06:31 +04:00
|
|
|
/* virtual */ JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
nsDOMFileReader::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-04-13 11:06:31 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return FileReaderBinding::Wrap(aCx, this, aGivenProto);
|
2013-04-13 11:06:31 +04:00
|
|
|
}
|