From 4e5948b3f20d532fff947b7fabc18cccee7f73d3 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Tue, 9 May 2017 11:03:40 +0200 Subject: [PATCH] Bug 1360807 - FileReaderSync must work with sync inputStream - part 1 - SyncRead should read as much as required, r=smaug --- dom/workers/FileReaderSync.cpp | 38 +++++++++++++++++++++++++++------- dom/workers/FileReaderSync.h | 4 ++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/dom/workers/FileReaderSync.cpp b/dom/workers/FileReaderSync.cpp index 4bf8e5fd6d06..8214f81356a4 100644 --- a/dom/workers/FileReaderSync.cpp +++ b/dom/workers/FileReaderSync.cpp @@ -78,7 +78,7 @@ FileReaderSync::ReadAsArrayBuffer(JSContext* aCx, } uint32_t numRead; - aRv = Read(stream, bufferData.get(), blobSize, &numRead); + aRv = SyncRead(stream, bufferData.get(), blobSize, &numRead); if (NS_WARN_IF(aRv.Failed())) { return; } @@ -110,7 +110,7 @@ FileReaderSync::ReadAsBinaryString(Blob& aBlob, uint32_t numRead; do { char readBuf[4096]; - aRv = Read(stream, readBuf, sizeof(readBuf), &numRead); + aRv = SyncRead(stream, readBuf, sizeof(readBuf), &numRead); if (NS_WARN_IF(aRv.Failed())) { return; } @@ -145,7 +145,7 @@ FileReaderSync::ReadAsText(Blob& aBlob, } uint32_t numRead = 0; - aRv = Read(stream, sniffBuf.BeginWriting(), sniffBuf.Length(), &numRead); + aRv = SyncRead(stream, sniffBuf.BeginWriting(), sniffBuf.Length(), &numRead); if (NS_WARN_IF(aRv.Failed())) { return; } @@ -361,8 +361,8 @@ NS_INTERFACE_MAP_END } // anonymous nsresult -FileReaderSync::Read(nsIInputStream* aStream, char* aBuffer, uint32_t aBufferSize, - uint32_t* aRead) +FileReaderSync::SyncRead(nsIInputStream* aStream, char* aBuffer, + uint32_t aBufferSize, uint32_t* aRead) { MOZ_ASSERT(aStream); MOZ_ASSERT(aBuffer); @@ -370,10 +370,34 @@ FileReaderSync::Read(nsIInputStream* aStream, char* aBuffer, uint32_t aBufferSiz // Let's try to read, directly. nsresult rv = aStream->Read(aBuffer, aBufferSize, aRead); - if (NS_SUCCEEDED(rv) || rv != NS_BASE_STREAM_WOULD_BLOCK) { + + // Nothing else to read. + if (rv == NS_BASE_STREAM_CLOSED || + (NS_SUCCEEDED(rv) && *aRead == 0)) { + return NS_OK; + } + + // An error. + if (NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK) { return rv; } + // All good. + if (NS_SUCCEEDED(rv)) { + // Not enough data, let's read recursively. + if (*aRead != aBufferSize) { + uint32_t byteRead = 0; + rv = SyncRead(aStream, aBuffer + *aRead, aBufferSize - *aRead, &byteRead); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + *aRead += byteRead; + } + + return NS_OK; + } + // We need to proceed async. nsCOMPtr asyncStream = do_QueryInterface(aStream); if (!asyncStream) { @@ -408,5 +432,5 @@ FileReaderSync::Read(nsIInputStream* aStream, char* aBuffer, uint32_t aBufferSiz } // Now, we can try to read again. - return Read(aStream, aBuffer, aBufferSize, aRead); + return SyncRead(aStream, aBuffer, aBufferSize, aRead); } diff --git a/dom/workers/FileReaderSync.h b/dom/workers/FileReaderSync.h index c23803161b92..a23877a60abc 100644 --- a/dom/workers/FileReaderSync.h +++ b/dom/workers/FileReaderSync.h @@ -32,8 +32,8 @@ private: nsresult ConvertStream(nsIInputStream *aStream, const char *aCharset, nsAString &aResult); - nsresult Read(nsIInputStream* aStream, char* aBuffer, uint32_t aBufferSize, - uint32_t* aRead); + nsresult SyncRead(nsIInputStream* aStream, char* aBuffer, + uint32_t aBufferSize, uint32_t* aRead); public: static already_AddRefed