Bug 1360807 - FileReaderSync must work with sync inputStream - part 3 - Base64EncodeInputStream needs a sync inputStream, r=smaug
☠☠ backed out by 797a743271c6 ☠ ☠
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 04 May 2017 14:51:11 +0200
changeset 572820 41e9af8078dd30bb0dd8de901c9ad1869a6b677c
parent 572819 e59616a3997a930cedecf4dd520fe8a595dba493
child 572821 08d888f39c153a5fd8c2aee35f4d1f609682ec92
push id57195
push userbmo:rbarker@mozilla.com
push dateThu, 04 May 2017 20:08:56 +0000
reviewerssmaug
bugs1360807
milestone55.0a1
Bug 1360807 - FileReaderSync must work with sync inputStream - part 3 - Base64EncodeInputStream needs a sync inputStream, r=smaug
dom/workers/FileReaderSync.cpp
--- a/dom/workers/FileReaderSync.cpp
+++ b/dom/workers/FileReaderSync.cpp
@@ -16,16 +16,17 @@
 #include "nsCExternalHandlerService.h"
 #include "nsComponentManagerUtils.h"
 #include "nsCOMPtr.h"
 #include "nsDOMClassInfoID.h"
 #include "nsError.h"
 #include "nsIConverterInputStream.h"
 #include "nsIInputStream.h"
 #include "nsIMultiplexInputStream.h"
+#include "nsStreamUtils.h"
 #include "nsStringStream.h"
 #include "nsISupportsImpl.h"
 #include "nsNetUtil.h"
 #include "nsServiceManagerUtils.h"
 #include "nsIAsyncInputStream.h"
 #include "WorkerPrivate.h"
 #include "WorkerRunnable.h"
 
@@ -77,17 +78,22 @@ FileReaderSync::ReadAsArrayBuffer(JSCont
     return;
   }
 
   uint32_t numRead;
   aRv = SyncRead(stream, bufferData.get(), blobSize, &numRead);
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
-  NS_ASSERTION(numRead == blobSize, "failed to read data");
+
+  // The file is changed in the meantime?
+  if (numRead != blobSize) {
+    aRv.Throw(NS_ERROR_FAILURE);
+    return;
+  }
 
   JSObject* arrayBuffer = JS_NewArrayBufferWithContents(aCx, blobSize, bufferData.get());
   if (!arrayBuffer) {
     aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
     return;
   }
   // arrayBuffer takes the ownership when it is not null. Otherwise we
   // need to release it explicitly.
@@ -145,16 +151,22 @@ FileReaderSync::ReadAsText(Blob& aBlob,
   }
 
   uint32_t numRead = 0;
   aRv = SyncRead(stream, sniffBuf.BeginWriting(), sniffBuf.Length(), &numRead);
   if (NS_WARN_IF(aRv.Failed())) {
     return;
   }
 
+  // No data, we don't need to continue.
+  if (numRead == 0) {
+    aResult.Truncate();
+    return;
+  }
+
   // The BOM sniffing is baked into the "decode" part of the Encoding
   // Standard, which the File API references.
   if (!nsContentUtils::CheckForBOM((const unsigned char*)sniffBuf.BeginReading(),
                                    numRead, encoding)) {
     // BOM sniffing failed. Try the API argument.
     if (!aEncoding.WasPassed() ||
         !EncodingUtils::FindEncodingForLabel(aEncoding.Value(),
                                              encoding)) {
@@ -239,29 +251,52 @@ 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 size = aBlob.GetSize(aRv);
+  nsCOMPtr<nsIInputStream> syncStream;
+  aRv = ConvertAsyncToSyncStream(stream, getter_AddRefs(syncStream));
+  if (NS_WARN_IF(aRv.Failed())) {
+    return;
+  }
+
+  // We need a buffered stream.
+  if (!NS_InputStreamIsBuffered(syncStream)) {
+    nsCOMPtr<nsIInputStream> bufferedStream;
+    aRv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream),
+                                    syncStream, 4096);
+    if (NS_WARN_IF(aRv.Failed())) {
+      return;
+    }
+
+    syncStream = bufferedStream;
+  }
+
+  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;
   }
 
-  nsCOMPtr<nsIInputStream> bufferedStream;
-  aRv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), stream, size);
-  if (NS_WARN_IF(aRv.Failed())){
+  // The file is changed in the meantime?
+  if (blobSize != size) {
     return;
   }
 
   nsAutoString encodedData;
-  aRv = Base64EncodeInputStream(bufferedStream, encodedData, size);
+  aRv = Base64EncodeInputStream(syncStream, encodedData, size);
   if (NS_WARN_IF(aRv.Failed())){
     return;
   }
 
   scratchResult.Append(encodedData);
 
   aResult = scratchResult;
 }