Bug 1437114 - ExternalHelperAppChild should send data to the parent actor in chunks, r=froydnj a=RyanVM
authorAndrea Marchesini <amarchesini@mozilla.com>
Mon, 12 Feb 2018 17:41:34 +0100
changeset 454962 e0f32aa8b45d4f9b0280228c6caeeeece9739a4b
parent 454961 75f5893a686fae9133324782ee4061647537e587
child 454963 0210188bd3aacb07cb298f4af4ca5e5be62cbd65
push id1648
push usermtabara@mozilla.com
push dateThu, 01 Mar 2018 12:45:47 +0000
treeherdermozilla-release@cbb9688c2eeb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj, RyanVM
bugs1437114
milestone59.0
Bug 1437114 - ExternalHelperAppChild should send data to the parent actor in chunks, r=froydnj a=RyanVM
uriloader/exthandler/ExternalHelperAppChild.cpp
--- a/uriloader/exthandler/ExternalHelperAppChild.cpp
+++ b/uriloader/exthandler/ExternalHelperAppChild.cpp
@@ -38,23 +38,38 @@ ExternalHelperAppChild::OnDataAvailable(
                                         nsISupports *ctx,
                                         nsIInputStream *input,
                                         uint64_t offset,
                                         uint32_t count)
 {
   if (NS_FAILED(mStatus))
     return mStatus;
 
+  static uint32_t const kCopyChunkSize = 128 * 1024;
+  uint32_t toRead = std::min<uint32_t>(count, kCopyChunkSize);
+
   nsCString data;
-  nsresult rv = NS_ReadInputStreamToString(input, data, count);
-  if (NS_FAILED(rv))
-    return rv;
+  if (NS_WARN_IF(!data.SetCapacity(toRead, fallible))) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
 
-  if (!SendOnDataAvailable(data, offset, count))
-    return NS_ERROR_UNEXPECTED;
+  while (count) {
+    nsresult rv = NS_ReadInputStreamToString(input, data, toRead);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+
+    if (NS_WARN_IF(!SendOnDataAvailable(data, offset, toRead))) {
+      return NS_ERROR_UNEXPECTED;
+    }
+
+    count -= toRead;
+    offset += toRead;
+    toRead = std::min<uint32_t>(count, kCopyChunkSize);
+  }
 
   return NS_OK;
 }
 
 //////////////////////////////////////////////////////////////////////////////
 // nsIRequestObserver
 //////////////////////////////////////////////////////////////////////////////