Bug 1397810 - FileReaderSync should use Blob::GetSize() to known the size of the stream and not stream::Available(), r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Fri, 08 Sep 2017 11:23:02 +0200
changeset 429245 5f7438b737c0c4e8361b3c2e7bd4db3c87f9a40b
parent 429244 8961364199aa772738bd745b58932517ba1e25b2
child 429246 1c70e1ffbbbd5625ea5ce8aabb354f8871b7411e
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1397810
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1397810 - FileReaderSync should use Blob::GetSize() to known the size of the stream and not stream::Available(), r=smaug
dom/workers/FileReaderSync.cpp
dom/workers/FileReaderSync.h
--- a/dom/workers/FileReaderSync.cpp
+++ b/dom/workers/FileReaderSync.cpp
@@ -204,18 +204,24 @@ FileReaderSync::ReadAsText(Blob& aBlob,
     return;
   }
 
   aRv = multiplexStream->AppendStream(sniffStringStream);
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
 
+  uint64_t blobSize = aBlob.GetSize(aRv);
+  if (NS_WARN_IF(aRv.Failed())){
+    return;
+  }
+
   nsCOMPtr<nsIInputStream> syncStream;
-  aRv = ConvertAsyncToSyncStream(stream, getter_AddRefs(syncStream));
+  aRv = ConvertAsyncToSyncStream(blobSize - sniffBuf.Length(), stream,
+                                 getter_AddRefs(syncStream));
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
 
   // ConvertAsyncToSyncStream returns a null syncStream if the stream has been
   // already closed or there is nothing to read.
   if (syncStream) {
     aRv = multiplexStream->AppendStream(syncStream);
@@ -250,35 +256,35 @@ FileReaderSync::ReadAsDataURL(Blob& aBlo
   scratchResult.AppendLiteral(";base64,");
 
   nsCOMPtr<nsIInputStream> stream;
   aBlob.GetInternalStream(getter_AddRefs(stream), aRv);
   if (NS_WARN_IF(aRv.Failed())){
     return;
   }
 
+  uint64_t blobSize = aBlob.GetSize(aRv);
+  if (NS_WARN_IF(aRv.Failed())){
+    return;
+  }
+
   nsCOMPtr<nsIInputStream> syncStream;
-  aRv = ConvertAsyncToSyncStream(stream, getter_AddRefs(syncStream));
+  aRv = ConvertAsyncToSyncStream(blobSize, stream, getter_AddRefs(syncStream));
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
 
   MOZ_ASSERT(syncStream);
 
   uint64_t size;
   aRv = syncStream->Available(&size);
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
 
-  uint64_t blobSize = aBlob.GetSize(aRv);
-  if (NS_WARN_IF(aRv.Failed())){
-    return;
-  }
-
   // The file is changed in the meantime?
   if (blobSize != size) {
     return;
   }
 
   nsAutoString encodedData;
   aRv = Base64EncodeInputStream(syncStream, encodedData, size);
   if (NS_WARN_IF(aRv.Failed())){
@@ -461,49 +467,39 @@ FileReaderSync::SyncRead(nsIInputStream*
     return NS_ERROR_DOM_INVALID_STATE_ERR;
   }
 
   // Now, we can try to read again.
   return SyncRead(aStream, aBuffer, aBufferSize, aRead);
 }
 
 nsresult
-FileReaderSync::ConvertAsyncToSyncStream(nsIInputStream* aAsyncStream,
+FileReaderSync::ConvertAsyncToSyncStream(uint64_t aStreamSize,
+                                         nsIInputStream* aAsyncStream,
                                          nsIInputStream** aSyncStream)
 {
   // If the stream is not async, we just need it to be bufferable.
   nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(aAsyncStream);
   if (!asyncStream) {
     return NS_NewBufferedInputStream(aSyncStream, aAsyncStream, 4096);
   }
 
-  uint64_t length;
-  nsresult rv = aAsyncStream->Available(&length);
-  if (rv == NS_BASE_STREAM_CLOSED) {
-    // The stream has already been closed. Nothing to do.
-    *aSyncStream = nullptr;
-    return NS_OK;
+  nsAutoCString buffer;
+  if (!buffer.SetLength(aStreamSize, fallible)) {
+    return NS_ERROR_OUT_OF_MEMORY;
   }
 
+  uint32_t read;
+  nsresult rv =
+    SyncRead(aAsyncStream, buffer.BeginWriting(), aStreamSize, &read);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  nsAutoCString buffer;
-  if (!buffer.SetLength(length, fallible)) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
-  uint32_t read;
-  rv = SyncRead(aAsyncStream, buffer.BeginWriting(), length, &read);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
-
-  if (read != length) {
+  if (read != aStreamSize) {
     return NS_ERROR_FAILURE;
   }
 
   rv = NS_NewCStringInputStream(aSyncStream, buffer);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
--- a/dom/workers/FileReaderSync.h
+++ b/dom/workers/FileReaderSync.h
@@ -27,17 +27,18 @@ private:
   // Private destructor, to discourage deletion outside of Release():
   ~FileReaderSync()
   {
   }
 
   nsresult ConvertStream(nsIInputStream *aStream, const char *aCharset,
                          nsAString &aResult);
 
-  nsresult ConvertAsyncToSyncStream(nsIInputStream* aAsyncStream,
+  nsresult ConvertAsyncToSyncStream(uint64_t aStreamSize,
+                                    nsIInputStream* aAsyncStream,
                                     nsIInputStream** aSyncStream);
 
   nsresult SyncRead(nsIInputStream* aStream, char* aBuffer,
                     uint32_t aBufferSize, uint32_t* aRead);
 
 public:
   static already_AddRefed<FileReaderSync>
   Constructor(const GlobalObject& aGlobal, ErrorResult& aRv);