Bug 1503602 - Use the Response's global to create the ReadableStream reader to consume the body, r=bz
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 01 Nov 2018 22:46:30 +0100
changeset 500478 bb6607c98119eda64ea5e431cafe93e683569f5f
parent 500477 589a6293a3b7e3ca5a222a34e0e877e06c56ba37
child 500479 e1238f83faea1c17761e683a615869382eec8cec
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs1503602
milestone65.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 1503602 - Use the Response's global to create the ReadableStream reader to consume the body, r=bz
dom/fetch/FetchStreamReader.cpp
--- a/dom/fetch/FetchStreamReader.cpp
+++ b/dom/fetch/FetchStreamReader.cpp
@@ -7,16 +7,17 @@
 #include "FetchStreamReader.h"
 #include "InternalResponse.h"
 #include "mozilla/dom/PromiseBinding.h"
 #include "mozilla/SystemGroup.h"
 #include "mozilla/TaskCategory.h"
 #include "nsContentUtils.h"
 #include "nsIScriptError.h"
 #include "nsPIDOMWindow.h"
+#include "jsapi.h"
 
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(FetchStreamReader)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(FetchStreamReader)
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(FetchStreamReader)
@@ -155,16 +156,22 @@ FetchStreamReader::StartConsuming(JSCont
                                   JS::MutableHandle<JSObject*> aReader,
                                   ErrorResult& aRv)
 {
   MOZ_DIAGNOSTIC_ASSERT(!mReader);
   MOZ_DIAGNOSTIC_ASSERT(aStream);
 
   aRv.MightThrowJSException();
 
+  // Here, by spec, we can pick any global we want. Just to avoid extra
+  // cross-compartment steps, we want to create the reader in the same
+  // compartment of the owning Fetch Body object.
+  // The same global will be used to retrieve data from this reader.
+  JSAutoRealm ar(aCx, mGlobal->GetGlobalJSObject());
+
   JS::Rooted<JSObject*> reader(aCx,
                                JS::ReadableStreamGetReader(aCx, aStream,
                                                            JS::ReadableStreamReaderMode::Default));
   if (!reader) {
     aRv.StealExceptionFromJSContext(aCx);
     CloseAndRelease(aCx, NS_ERROR_DOM_INVALID_STATE_ERR);
     return;
   }
@@ -190,18 +197,19 @@ FetchStreamReader::OnOutputStreamReady(n
   if (mStreamClosed) {
     return NS_OK;
   }
 
   if (mBuffer) {
     return WriteBuffer();
   }
 
-  // TODO: We need to verify this is the correct global per the spec.
-  //       See bug 1385890.
+  // Here we can retrieve data from the reader using any global we want because
+  // it is not observable. We want to use the reader's global, which is also the
+  // Response's one.
   AutoEntryScript aes(mGlobal, "ReadableStreamReader.read", !mWorkerRef);
 
   JS::Rooted<JSObject*> reader(aes.cx(), mReader);
   JS::Rooted<JSObject*> promise(aes.cx(),
                                 JS::ReadableStreamDefaultReaderRead(aes.cx(),
                                                                     reader));
   if (NS_WARN_IF(!promise)) {
     // Let's close the stream.