mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:30:27 +00:00
2121ac8f2e
- Bug 1207030 - Enable -Wshadow flag in more directories that have no -Wshadow warnings. r=glandium (0654c75bf5) - don't build hunspell unified (a576cbdc65) - Bug 1218315 - Replace NS_LITERAL_STRING(...).get() with MOZ_UTF16(...) on rdf. r=nfroyd (795b627c84) - Bug 138009 - Remove unused platform.js, now with packaging goodness, r=mossop (f7547f1f16) - Bug 1195168 - Use channel->ascynOpen2 src/nsAutoConfig.cpp (r=sicking) (23ed743739) - Bug 1197926 - Use channel->Open2() in extensions/pref/autoconfig/src/nsReadConfig.cpp (r=sicking) (a6372aa60a) - Bug 1231256 (part 1) - Remove unused nsBoxLayoutState parameters from many functions. r=roc. (9d57344ee6) - Bug 1231256 (part 2) - Remove unused BlobDirState parameters from SetLazyData(). r=jld. (f3d62d9754) - Bug 1231256 (part 3) - Remove unused parameter from TryToOuterize(). r=roc. (0dc80be011) - Bug 1231256 (part 4) - Remove dead SetContext() declaration. r=jfkthame. (4108786c72) - Bug 1231256 (part 5) - Remove dead FromCSSPerspectiveInfo() function. r=vlad. (a9980426b0) - Bug 1231256 (part 6) - Remove unused parameter from Damage functions. r=roc. (e74906d54a) - Bug 1231256 (part 7) - Remove unused parameters from two layout functions. r=roc. (1cdcf91a10) - Bug 1231256 (part 8) - Remove unused parameters from MouseClicked(). r=roc. (2acf1b532b) - Bug 1231256 (part 9) - Remove unused parameter from ShowPopup(). r=roc. (e57daff2ee) - Bug 1231256 (part 10) - Remove unused parameters from StartAPZDrag(). r=roc. (b8d8ed57f4) - Bug 1231256 (part 11) - Remove unused parameter from HidePopupsInList(). r=roc. (ab9ea00111) - Bug 1231256 (part 12) - Remove TraceableVectorOperations::canAppendWithoutRealloc. r=terrence. (dc1ae504d5) - Bug 1230911 - modified asseert in order to prevent null pointer dereference. r=roc (71dce2e51c) - Bug 1230913 - modified assert in order to prevent null pointer dereference. r=roc (91b6092a83) - Bug 1228952 - Move texture metadata out of the BufferTextureClient/Host's buffer. r=Sotaro (5783f02382) - Bug 1215005 - SourceSurface has thread-safe refcount now. r=bas (13a0f58815) - Bug 1215438 - Part 1: Add utility functions to support transferring ImageBitmap. r=roc (5837bc686b) - Bug 1212009 - ReadFullySerializableObjects() doesn't use the aIndex param and it can be removed, r=smaug (db1b7f2a2c) - Bug 1215438 - Part 2: Add transfer support for ImageBitmap. r=baku (5f428ddaa3) - Bug 1215438 - Part 3: CairoImage can use in any thread. r=roc (7e6d6562ef) - Bug 1215438 - Part 4: Rename CairoImage to SourceSurfaceImage. r=roc (e336d6bcd7) - Bug 1215438 - Part 5: Add test for imagebitmap transfer. r=roc (71c7dbc4a5) - Bug 1172796 - Part 1: ImageEncoder can be used on worker thread. r=seth (8aa6b4d9bc) - Bug 1172796 - Part 2: Add write-only flag for origin-clean check. r=roc (dc4a1d5ef4) - Bug 1172796 - Part 3: Implement OffscreenCanvas constructor. r=roc r=smaug (466bef6e1a) - Bug 1172796 - Part 4: Implement OffscreenCanvas::ToBlob. r=roc r=smaug (0afb84ecb2) - Bug 1172796 - Part 5: Implements OffscreenCanvas::TransferImageBitmap. r=roc r=smaug (2709566e26) - Bug 1172796 - Part 6: Implements ImageBitmap::Close(). r=roc r=smaug (a45e423d77) - Bug 1172796 - Part 7: Implements ImageBitmapRenderingContext. r=roc r=smaug (c5a30f3132) - Bug 1172796 - Part 8: Add ImageBitmapRenderingContext to test_intefaces.html r=smaug (e8786603b2) - Bug 1172796 - Part 9: Use gfxPrefs to get webgl.enable-prototype-webgl2. r=jgilbert (c4ee72d49c) - fix Bug 1230686 - use RefPtr<DrawTarget>& instead of DrawTarget* (f92f5fae12) - reapply per misspatch Bug 1200595 - Consolidate the TextureClient's destruction logic. (960eb012ae) - Bug 1215236 - "Remove compilation warnings in StructuredCloneHolder". r=smaug (0ba2fdb23b) - Bug 1222123 - Warning fix in CompositorD3D11.cpp. r=Bas (f8d36dbb71) - Bug 1219494 - Part 4. gfx/layers and gfxCrash. r=mchang (b4046ee2df) - Bug 1229533: Ensure our RenderingTargetView for the backbuffer and the backbuffer itself have no lingering references, and log if they do. r=milan (89c9eed426) - Bug 1222569 - remove unused variables in CompositorD3D9.cpp; r=Bas (05f5a61e66) - Bug 1198021 - When the ContentClient of a ClientTiledPaintedLayer changes, don't destroy the old content client until after the layer transaction has been sent. r=nical (901edd5f56) - Bug 901097 - FileReader API in workers, r=sicking, r=nfroyd (01a4023d7e) - Bug 1217307 - Remove some unnecessary null checks in dom/base/. r=njn (4411cefe18) - Bug 264412. Implement HTMLElement.innerText. r=smaug,mats (28306eabfb) - Bug 1119157: Fix false over-recursion in web worker threads by cleaning up thread info in the processes forked from Nuwa. a=asuth (be1fb6ca67) - Bug 1125091 - Make Nuwa be Valgrind-friendly. r=njn. (bade1e2e1f) - Bug 1144535: Respect caller-specifed thread attributes in creating threads in the Nuwa process. r=asuth (33acdcf8ca) - Bug 1216354 - Disable LSan leak checking by default in mozglue. r=glandium (c8177284b2) - Bug 1196859 - Block Roboform binaries written against old SDK. r=bsmedberg (bf21a047ea) - Bug 763395 - Add nlsp.dll to blocklist. r=bsmedberg (ab96eafdbe) - Bug 1136968 - Add YCWebCameraSource.ax to DLL blocklist. r=benjamin (735282235d) - Bug 1170141 - Block RealNetworks Browser Record Plugin due to startup crashes. r=ehan (21937f4bc1) - Bug 1222819 - Add Orbit Downloader .dlls to Windows blocklist. r=aklotz (e8446eda95) - Bug 1198186 - increase limit used to detect int values passed as first argument to LdrLoadDll, r=dmajor (89f5ae4a87) - Bug 1151506: Block long hex name DLLs due to startup crashes. r=bsmedberg (34c0b81bfc) - Bug 1160148: Also block AVX2 in Win7RTM, and move the code to mozglue. r=froydnj (61ab36c6c6) - Bug 1203350 - Unify arithmetic operators in TimeStamp; r=froydnj (a96aeb7e11) - Bug 1179550 - Always terminate the str buffer. r=erahm (0c07fcad4f) - No bug, add missing include to StackWalk.cpp, DONTBUILD (88beda3fbb) - Bug 989499 - Part 2: Use FramePointerStackwalk on windows. r=rjmuizel (3f0db58ded) - Bug 989499 - Part 1: Add a way to get the stack top. r=BenWa (a1bdae796f) - Bug 1193838 - Expose ProfileGatherer as an nsISupports through nsIProfiler for process parent actors. r=BenWa (07121dd03d) - Bug 1233835 - Fix TaskTracer double init assert. r=mconley (895511b141) - Bug 1193838 - Make ProfileGatherer exist during the lifetime of a GeckoSampler. r=BenWa (9ca73c74e4) - Revert "Bug 1172157 - Save malformed profile JSON to file to help debug." (r=BenWa) (e4fc0084d4) - Bug 1196845 - Fix invalid JSON in saved Java thread profile; r=BenWa (d5b542da81) - Bug 1145333 - Skip NativeStackAddrs that conflict with pseudoStackAddrs and jsStackAddrs when merging sampler stacks. r=mstange (b50a048ab2) - Bug 1122721 - Don't call pthread_get_stackaddr_np during sampling. r=mstange (c6818c45a2) - Bug 1229850 - Avoid unnecessary combobox dropdown calculations in the content process. r=dbaron (174530328f) - Bug 1187605 pt 1 - In vertical writing modes, add an 'orientation' attribute with value='left' or 'right' to the <select> control's dropdown button. r=smontagu (76f9cb3fcd) - Bug 1187605 pt 2 - Make the <select> dropdown arrow respect the 'orientation' attribute, if present. r=smontagu (bd58cd1f89)
756 lines
19 KiB
C++
756 lines
19 KiB
C++
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
/* 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/. */
|
|
|
|
#include "FileReader.h"
|
|
|
|
#include "nsIEventTarget.h"
|
|
#include "nsIGlobalObject.h"
|
|
#include "nsITimer.h"
|
|
#include "nsITransport.h"
|
|
#include "nsIStreamTransportService.h"
|
|
|
|
#include "mozilla/Base64.h"
|
|
#include "mozilla/dom/DOMError.h"
|
|
#include "mozilla/dom/EncodingUtils.h"
|
|
#include "mozilla/dom/File.h"
|
|
#include "mozilla/dom/FileReaderBinding.h"
|
|
#include "mozilla/dom/ProgressEvent.h"
|
|
#include "nsContentUtils.h"
|
|
#include "nsCycleCollectionParticipant.h"
|
|
#include "nsDOMJSUtils.h"
|
|
#include "nsError.h"
|
|
#include "nsNetCID.h"
|
|
#include "nsNetUtil.h"
|
|
#include "xpcpublic.h"
|
|
|
|
#include "WorkerPrivate.h"
|
|
#include "WorkerScope.h"
|
|
|
|
namespace mozilla {
|
|
namespace dom {
|
|
|
|
using namespace workers;
|
|
|
|
#define ABORT_STR "abort"
|
|
#define LOAD_STR "load"
|
|
#define LOADSTART_STR "loadstart"
|
|
#define LOADEND_STR "loadend"
|
|
#define ERROR_STR "error"
|
|
#define PROGRESS_STR "progress"
|
|
|
|
const uint64_t kUnknownSize = uint64_t(-1);
|
|
|
|
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(FileReader)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FileReader,
|
|
DOMEventTargetHelper)
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBlob)
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mProgressNotifier)
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mError)
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FileReader,
|
|
DOMEventTargetHelper)
|
|
tmp->Shutdown();
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBlob)
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mProgressNotifier)
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mError)
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(FileReader,
|
|
DOMEventTargetHelper)
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mResultArrayBuffer)
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileReader)
|
|
NS_INTERFACE_MAP_ENTRY(nsITimerCallback)
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStreamCallback)
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
NS_IMPL_ADDREF_INHERITED(FileReader, DOMEventTargetHelper)
|
|
NS_IMPL_RELEASE_INHERITED(FileReader, DOMEventTargetHelper)
|
|
|
|
class MOZ_RAII FileReaderDecreaseBusyCounter
|
|
{
|
|
RefPtr<FileReader> mFileReader;
|
|
public:
|
|
explicit FileReaderDecreaseBusyCounter(FileReader* aFileReader)
|
|
: mFileReader(aFileReader)
|
|
{}
|
|
|
|
~FileReaderDecreaseBusyCounter()
|
|
{
|
|
mFileReader->DecreaseBusyCounter();
|
|
}
|
|
};
|
|
|
|
void
|
|
FileReader::RootResultArrayBuffer()
|
|
{
|
|
mozilla::HoldJSObjects(this);
|
|
}
|
|
|
|
//FileReader constructors/initializers
|
|
|
|
FileReader::FileReader(nsPIDOMWindow* aWindow,
|
|
WorkerPrivate* aWorkerPrivate)
|
|
: DOMEventTargetHelper(aWindow)
|
|
, mFileData(nullptr)
|
|
, mDataLen(0)
|
|
, mDataFormat(FILE_AS_BINARY)
|
|
, mResultArrayBuffer(nullptr)
|
|
, mProgressEventWasDelayed(false)
|
|
, mTimerIsActive(false)
|
|
, mReadyState(EMPTY)
|
|
, mTotal(0)
|
|
, mTransferred(0)
|
|
, mTarget(do_GetCurrentThread())
|
|
, mBusyCount(0)
|
|
, mWorkerPrivate(aWorkerPrivate)
|
|
{
|
|
MOZ_ASSERT_IF(!NS_IsMainThread(), mWorkerPrivate && !aWindow);
|
|
MOZ_ASSERT_IF(NS_IsMainThread(), !mWorkerPrivate);
|
|
SetDOMStringToNull(mResult);
|
|
}
|
|
|
|
FileReader::~FileReader()
|
|
{
|
|
Shutdown();
|
|
DropJSObjects(this);
|
|
}
|
|
|
|
/* static */ already_AddRefed<FileReader>
|
|
FileReader::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
|
|
{
|
|
// The owner can be null when this object is used by chrome code.
|
|
nsCOMPtr<nsPIDOMWindow> owner = do_QueryInterface(aGlobal.GetAsSupports());
|
|
WorkerPrivate* workerPrivate = nullptr;
|
|
|
|
if (!NS_IsMainThread()) {
|
|
JSContext* cx = aGlobal.Context();
|
|
workerPrivate = GetWorkerPrivateFromContext(cx);
|
|
MOZ_ASSERT(workerPrivate);
|
|
}
|
|
|
|
RefPtr<FileReader> fileReader = new FileReader(owner, workerPrivate);
|
|
|
|
if (!owner && nsContentUtils::ThreadsafeIsCallerChrome()) {
|
|
// Instead of grabbing some random global from the context stack,
|
|
// let's use the default one (junk scope) for now.
|
|
// We should move away from this Init...
|
|
fileReader->BindToOwner(xpc::NativeGlobal(xpc::PrivilegedJunkScope()));
|
|
}
|
|
|
|
return fileReader.forget();
|
|
}
|
|
|
|
// nsIInterfaceRequestor
|
|
|
|
NS_IMETHODIMP
|
|
FileReader::GetInterface(const nsIID & aIID, void **aResult)
|
|
{
|
|
return QueryInterface(aIID, aResult);
|
|
}
|
|
|
|
void
|
|
FileReader::GetResult(JSContext* aCx,
|
|
JS::MutableHandle<JS::Value> aResult,
|
|
ErrorResult& aRv)
|
|
{
|
|
JS::Rooted<JS::Value> result(aCx);
|
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
if (mReadyState == DONE && mResultArrayBuffer) {
|
|
result.setObject(*mResultArrayBuffer);
|
|
} else {
|
|
result.setNull();
|
|
}
|
|
|
|
if (!JS_WrapValue(aCx, &result)) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
|
|
aResult.set(result);
|
|
return;
|
|
}
|
|
|
|
nsString tmpResult = mResult;
|
|
if (!xpc::StringToJsval(aCx, tmpResult, aResult)) {
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
return;
|
|
}
|
|
}
|
|
|
|
static NS_IMETHODIMP
|
|
ReadFuncBinaryString(nsIInputStream* in,
|
|
void* closure,
|
|
const char* fromRawSegment,
|
|
uint32_t toOffset,
|
|
uint32_t count,
|
|
uint32_t *writeCount)
|
|
{
|
|
char16_t* dest = static_cast<char16_t*>(closure) + toOffset;
|
|
char16_t* end = dest + count;
|
|
const unsigned char* source = (const unsigned char*)fromRawSegment;
|
|
while (dest != end) {
|
|
*dest = *source;
|
|
++dest;
|
|
++source;
|
|
}
|
|
*writeCount = count;
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
FileReader::DoOnLoadEnd(nsresult aStatus,
|
|
nsAString& aSuccessEvent,
|
|
nsAString& aTerminationEvent)
|
|
{
|
|
// Make sure we drop all the objects that could hold files open now.
|
|
nsCOMPtr<nsIAsyncInputStream> stream;
|
|
mAsyncStream.swap(stream);
|
|
|
|
RefPtr<Blob> blob;
|
|
mBlob.swap(blob);
|
|
|
|
// Clear out the data if necessary
|
|
if (NS_FAILED(aStatus)) {
|
|
FreeFileData();
|
|
return NS_OK;
|
|
}
|
|
|
|
// In case we read a different number of bytes, we can assume that the
|
|
// underlying storage has changed. We should not continue.
|
|
if (mDataLen != mTotal) {
|
|
DispatchError(NS_ERROR_FAILURE, aTerminationEvent);
|
|
FreeFileData();
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
aSuccessEvent = NS_LITERAL_STRING(LOAD_STR);
|
|
aTerminationEvent = NS_LITERAL_STRING(LOADEND_STR);
|
|
|
|
nsresult rv = NS_OK;
|
|
switch (mDataFormat) {
|
|
case FILE_AS_ARRAYBUFFER: {
|
|
AutoJSAPI jsapi;
|
|
nsCOMPtr<nsIGlobalObject> globalObject;
|
|
|
|
if (NS_IsMainThread()) {
|
|
globalObject = do_QueryInterface(GetParentObject());
|
|
} else {
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
MOZ_ASSERT(mBusyCount);
|
|
globalObject = mWorkerPrivate->GlobalScope();
|
|
}
|
|
|
|
if (!globalObject || !jsapi.Init(globalObject)) {
|
|
FreeFileData();
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
RootResultArrayBuffer();
|
|
mResultArrayBuffer = JS_NewArrayBufferWithContents(jsapi.cx(), mDataLen, mFileData);
|
|
if (!mResultArrayBuffer) {
|
|
JS_ClearPendingException(jsapi.cx());
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
} else {
|
|
mFileData = nullptr; // Transfer ownership
|
|
}
|
|
break;
|
|
}
|
|
case FILE_AS_BINARY:
|
|
break; //Already accumulated mResult
|
|
case FILE_AS_TEXT:
|
|
if (!mFileData) {
|
|
if (mDataLen) {
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
break;
|
|
}
|
|
rv = GetAsText(blob, mCharset, "", mDataLen, mResult);
|
|
break;
|
|
}
|
|
rv = GetAsText(blob, mCharset, mFileData, mDataLen, mResult);
|
|
break;
|
|
case FILE_AS_DATAURL:
|
|
rv = GetAsDataURL(blob, mFileData, mDataLen, mResult);
|
|
break;
|
|
}
|
|
|
|
mResult.SetIsVoid(false);
|
|
|
|
FreeFileData();
|
|
|
|
return rv;
|
|
}
|
|
|
|
nsresult
|
|
FileReader::DoAsyncWait()
|
|
{
|
|
nsresult rv = IncreaseBusyCounter();
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
return rv;
|
|
}
|
|
|
|
rv = mAsyncStream->AsyncWait(this,
|
|
/* aFlags*/ 0,
|
|
/* aRequestedCount */ 0,
|
|
mTarget);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
DecreaseBusyCounter();
|
|
return rv;
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
FileReader::DoReadData(uint64_t aCount)
|
|
{
|
|
MOZ_ASSERT(mAsyncStream);
|
|
|
|
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);
|
|
NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
uint32_t bytesRead = 0;
|
|
mAsyncStream->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;
|
|
}
|
|
if (mDataFormat != FILE_AS_ARRAYBUFFER) {
|
|
mFileData = (char *) moz_realloc(mFileData, mDataLen + aCount);
|
|
NS_ENSURE_TRUE(mFileData, NS_ERROR_OUT_OF_MEMORY);
|
|
}
|
|
|
|
uint32_t bytesRead = 0;
|
|
mAsyncStream->Read(mFileData + mDataLen, aCount, &bytesRead);
|
|
NS_ASSERTION(bytesRead == aCount, "failed to read data");
|
|
}
|
|
|
|
mDataLen += aCount;
|
|
return NS_OK;
|
|
}
|
|
|
|
// Helper methods
|
|
|
|
void
|
|
FileReader::ReadFileContent(Blob& aBlob,
|
|
const nsAString &aCharset,
|
|
eDataFormat aDataFormat,
|
|
ErrorResult& aRv)
|
|
{
|
|
//Implicit abort to clear any other activity going on
|
|
ErrorResult error;
|
|
Abort(error);
|
|
error.SuppressException();
|
|
|
|
mError = nullptr;
|
|
SetDOMStringToNull(mResult);
|
|
mTransferred = 0;
|
|
mTotal = 0;
|
|
mReadyState = EMPTY;
|
|
FreeFileData();
|
|
|
|
mBlob = &aBlob;
|
|
mDataFormat = aDataFormat;
|
|
CopyUTF16toUTF8(aCharset, mCharset);
|
|
|
|
nsresult rv;
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
do_GetService(kStreamTransportServiceCID, &rv);
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
aRv.Throw(rv);
|
|
return;
|
|
}
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
mBlob->GetInternalStream(getter_AddRefs(stream), aRv);
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return;
|
|
}
|
|
|
|
nsCOMPtr<nsITransport> transport;
|
|
aRv = sts->CreateInputTransport(stream,
|
|
/* aStartOffset */ 0,
|
|
/* aReadLimit */ -1,
|
|
/* aCloseWhenDone */ true,
|
|
getter_AddRefs(transport));
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return;
|
|
}
|
|
|
|
nsCOMPtr<nsIInputStream> wrapper;
|
|
aRv = transport->OpenInputStream(/* aFlags */ 0,
|
|
/* aSegmentSize */ 0,
|
|
/* aSegmentCount */ 0,
|
|
getter_AddRefs(wrapper));
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return;
|
|
}
|
|
|
|
MOZ_ASSERT(!mAsyncStream);
|
|
mAsyncStream = do_QueryInterface(wrapper);
|
|
MOZ_ASSERT(mAsyncStream);
|
|
|
|
mTotal = mBlob->GetSize(aRv);
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return;
|
|
}
|
|
|
|
aRv = DoAsyncWait();
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
return;
|
|
}
|
|
|
|
//FileReader should be in loading state here
|
|
mReadyState = LOADING;
|
|
DispatchProgressEvent(NS_LITERAL_STRING(LOADSTART_STR));
|
|
|
|
if (mDataFormat == FILE_AS_ARRAYBUFFER) {
|
|
mFileData = js_pod_malloc<char>(mTotal);
|
|
if (!mFileData) {
|
|
NS_WARNING("Preallocation failed for ReadFileData");
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
}
|
|
}
|
|
}
|
|
|
|
nsresult
|
|
FileReader::GetAsText(Blob *aBlob,
|
|
const nsACString &aCharset,
|
|
const char *aFileData,
|
|
uint32_t aDataLen,
|
|
nsAString& aResult)
|
|
{
|
|
// 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;
|
|
aBlob->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");
|
|
}
|
|
}
|
|
}
|
|
|
|
nsDependentCSubstring data(aFileData, aDataLen);
|
|
return nsContentUtils::ConvertStringFromEncoding(encoding, data, aResult);
|
|
}
|
|
|
|
nsresult
|
|
FileReader::GetAsDataURL(Blob *aBlob,
|
|
const char *aFileData,
|
|
uint32_t aDataLen,
|
|
nsAString& aResult)
|
|
{
|
|
aResult.AssignLiteral("data:");
|
|
|
|
nsAutoString contentType;
|
|
aBlob->GetType(contentType);
|
|
if (!contentType.IsEmpty()) {
|
|
aResult.Append(contentType);
|
|
} else {
|
|
aResult.AppendLiteral("application/octet-stream");
|
|
}
|
|
aResult.AppendLiteral(";base64,");
|
|
|
|
nsCString encodedData;
|
|
nsresult rv = Base64Encode(Substring(aFileData, aDataLen), encodedData);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!AppendASCIItoUTF16(encodedData, aResult, fallible)) {
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
/* virtual */ JSObject*
|
|
FileReader::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
{
|
|
return FileReaderBinding::Wrap(aCx, this, aGivenProto);
|
|
}
|
|
|
|
void
|
|
FileReader::StartProgressEventTimer()
|
|
{
|
|
if (!mProgressNotifier) {
|
|
mProgressNotifier = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
}
|
|
|
|
if (mProgressNotifier) {
|
|
mProgressEventWasDelayed = false;
|
|
mTimerIsActive = true;
|
|
mProgressNotifier->Cancel();
|
|
mProgressNotifier->SetTarget(mTarget);
|
|
mProgressNotifier->InitWithCallback(this, NS_PROGRESS_EVENT_INTERVAL,
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
}
|
|
}
|
|
|
|
void
|
|
FileReader::ClearProgressEventTimer()
|
|
{
|
|
mProgressEventWasDelayed = false;
|
|
mTimerIsActive = false;
|
|
if (mProgressNotifier) {
|
|
mProgressNotifier->Cancel();
|
|
}
|
|
}
|
|
|
|
void
|
|
FileReader::DispatchError(nsresult rv, nsAString& finalEvent)
|
|
{
|
|
// Set the status attribute, and dispatch the error event
|
|
switch (rv) {
|
|
case NS_ERROR_FILE_NOT_FOUND:
|
|
mError = new DOMError(GetOwner(), NS_LITERAL_STRING("NotFoundError"));
|
|
break;
|
|
case NS_ERROR_FILE_ACCESS_DENIED:
|
|
mError = new DOMError(GetOwner(), NS_LITERAL_STRING("SecurityError"));
|
|
break;
|
|
default:
|
|
mError = new DOMError(GetOwner(), NS_LITERAL_STRING("NotReadableError"));
|
|
break;
|
|
}
|
|
|
|
// Dispatch error event to signify load failure
|
|
DispatchProgressEvent(NS_LITERAL_STRING(ERROR_STR));
|
|
DispatchProgressEvent(finalEvent);
|
|
}
|
|
|
|
nsresult
|
|
FileReader::DispatchProgressEvent(const nsAString& aType)
|
|
{
|
|
ProgressEventInit init;
|
|
init.mBubbles = false;
|
|
init.mCancelable = false;
|
|
init.mLoaded = mTransferred;
|
|
|
|
if (mTotal != kUnknownSize) {
|
|
init.mLengthComputable = true;
|
|
init.mTotal = mTotal;
|
|
} else {
|
|
init.mLengthComputable = false;
|
|
init.mTotal = 0;
|
|
}
|
|
RefPtr<ProgressEvent> event =
|
|
ProgressEvent::Constructor(this, aType, init);
|
|
event->SetTrusted(true);
|
|
|
|
return DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
|
}
|
|
|
|
// nsITimerCallback
|
|
NS_IMETHODIMP
|
|
FileReader::Notify(nsITimer* aTimer)
|
|
{
|
|
nsresult rv;
|
|
mTimerIsActive = false;
|
|
|
|
if (mProgressEventWasDelayed) {
|
|
rv = DispatchProgressEvent(NS_LITERAL_STRING("progress"));
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
StartProgressEventTimer();
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
// InputStreamCallback
|
|
NS_IMETHODIMP
|
|
FileReader::OnInputStreamReady(nsIAsyncInputStream* aStream)
|
|
{
|
|
if (mReadyState != LOADING || aStream != mAsyncStream) {
|
|
return NS_OK;
|
|
}
|
|
|
|
// We use this class to decrease the busy counter at the end of this method.
|
|
// In theory we can do it immediatelly but, for debugging reasons, we want to
|
|
// be 100% sure we have a feature when OnLoadEnd() is called.
|
|
FileReaderDecreaseBusyCounter RAII(this);
|
|
|
|
uint64_t aCount;
|
|
nsresult rv = aStream->Available(&aCount);
|
|
|
|
if (NS_SUCCEEDED(rv) && aCount) {
|
|
rv = DoReadData(aCount);
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
rv = DoAsyncWait();
|
|
}
|
|
|
|
if (NS_FAILED(rv) || !aCount) {
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
rv = NS_OK;
|
|
}
|
|
return OnLoadEnd(rv);
|
|
}
|
|
|
|
mTransferred += aCount;
|
|
|
|
//Notify the timer is the appropriate timeframe has passed
|
|
if (mTimerIsActive) {
|
|
mProgressEventWasDelayed = true;
|
|
} else {
|
|
rv = DispatchProgressEvent(NS_LITERAL_STRING(PROGRESS_STR));
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
StartProgressEventTimer();
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
nsresult
|
|
FileReader::OnLoadEnd(nsresult aStatus)
|
|
{
|
|
// Cancel the progress event timer
|
|
ClearProgressEventTimer();
|
|
|
|
// FileReader must be in DONE stage after an operation
|
|
mReadyState = DONE;
|
|
|
|
nsAutoString successEvent, termEvent;
|
|
nsresult rv = DoOnLoadEnd(aStatus, successEvent, termEvent);
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// Set the status field as appropriate
|
|
if (NS_FAILED(aStatus)) {
|
|
DispatchError(aStatus, termEvent);
|
|
return NS_OK;
|
|
}
|
|
|
|
// Dispatch event to signify end of a successful operation
|
|
DispatchProgressEvent(successEvent);
|
|
DispatchProgressEvent(termEvent);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
void
|
|
FileReader::Abort(ErrorResult& aRv)
|
|
{
|
|
if (mReadyState != LOADING) {
|
|
// XXX The spec doesn't say this
|
|
aRv.Throw(NS_ERROR_DOM_FILE_ABORT_ERR);
|
|
return;
|
|
}
|
|
|
|
ClearProgressEventTimer();
|
|
|
|
mReadyState = DONE;
|
|
|
|
// XXX The spec doesn't say this
|
|
mError = new DOMError(GetOwner(), NS_LITERAL_STRING("AbortError"));
|
|
|
|
// Revert status and result attributes
|
|
SetDOMStringToNull(mResult);
|
|
mResultArrayBuffer = nullptr;
|
|
|
|
mAsyncStream = nullptr;
|
|
mBlob = nullptr;
|
|
|
|
//Clean up memory buffer
|
|
FreeFileData();
|
|
|
|
// Dispatch the events
|
|
DispatchProgressEvent(NS_LITERAL_STRING(ABORT_STR));
|
|
DispatchProgressEvent(NS_LITERAL_STRING(LOADEND_STR));
|
|
}
|
|
|
|
nsresult
|
|
FileReader::IncreaseBusyCounter()
|
|
{
|
|
if (mWorkerPrivate && mBusyCount++ == 0 &&
|
|
!mWorkerPrivate->AddFeature(mWorkerPrivate->GetJSContext(), this)) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
void
|
|
FileReader::DecreaseBusyCounter()
|
|
{
|
|
MOZ_ASSERT_IF(mWorkerPrivate, mBusyCount);
|
|
if (mWorkerPrivate && --mBusyCount == 0) {
|
|
mWorkerPrivate->RemoveFeature(mWorkerPrivate->GetJSContext(), this);
|
|
}
|
|
}
|
|
|
|
bool
|
|
FileReader::Notify(JSContext* aCx, Status aStatus)
|
|
{
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (aStatus > Running) {
|
|
Shutdown();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void
|
|
FileReader::Shutdown()
|
|
{
|
|
FreeFileData();
|
|
mResultArrayBuffer = nullptr;
|
|
|
|
if (mAsyncStream) {
|
|
mAsyncStream->Close();
|
|
mAsyncStream = nullptr;
|
|
}
|
|
|
|
if (mWorkerPrivate && mBusyCount != 0) {
|
|
mWorkerPrivate->RemoveFeature(mWorkerPrivate->GetJSContext(), this);
|
|
mWorkerPrivate = nullptr;
|
|
mBusyCount = 0;
|
|
}
|
|
}
|
|
|
|
} // dom namespace
|
|
} // mozilla namespace
|