Bug 1381748 - Cleanup FetchConsumer workflow - part 1 - no mBody, r=catalinb
☠☠ backed out by fa20618aedb5 ☠ ☠
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 26 Jul 2017 08:54:07 +0200
changeset 422379 2a8ceea666e9d74fc162666ad42796ffaf480df1
parent 422378 4cd3ff1e99a289b90edf84fad83e30e51cafec29
child 422380 067897f212ac50c81efb30aadd98cd01b103c901
push id1517
push userjlorenzo@mozilla.com
push dateThu, 14 Sep 2017 16:50:54 +0000
treeherdermozilla-release@3b41fd564418 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscatalinb
bugs1381748
milestone56.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 1381748 - Cleanup FetchConsumer workflow - part 1 - no mBody, r=catalinb
dom/fetch/FetchConsumer.cpp
dom/fetch/FetchConsumer.h
--- a/dom/fetch/FetchConsumer.cpp
+++ b/dom/fetch/FetchConsumer.cpp
@@ -311,30 +311,40 @@ FetchBodyConsumer<Derived>::Create(nsIGl
                                    nsIEventTarget* aMainThreadEventTarget,
                                    FetchBody<Derived>* aBody,
                                    FetchConsumeType aType,
                                    ErrorResult& aRv)
 {
   MOZ_ASSERT(aBody);
   MOZ_ASSERT(aMainThreadEventTarget);
 
+  nsCOMPtr<nsIInputStream> bodyStream;
+  aBody->DerivedClass()->GetBody(getter_AddRefs(bodyStream));
+  if (!bodyStream) {
+    aRv = NS_NewCStringInputStream(getter_AddRefs(bodyStream), EmptyCString());
+    if (NS_WARN_IF(aRv.Failed())) {
+      return nullptr;
+    }
+  }
+
   RefPtr<Promise> promise = Promise::Create(aGlobal, aRv);
   if (aRv.Failed()) {
     return nullptr;
   }
 
   WorkerPrivate* workerPrivate = nullptr;
   if (!NS_IsMainThread()) {
     workerPrivate = GetCurrentThreadWorkerPrivate();
     MOZ_ASSERT(workerPrivate);
   }
 
   RefPtr<FetchBodyConsumer<Derived>> consumer =
     new FetchBodyConsumer<Derived>(aMainThreadEventTarget, aGlobal,
-                                   workerPrivate, aBody, promise, aType);
+                                   workerPrivate, aBody, bodyStream, promise,
+                                   aType);
 
   if (!NS_IsMainThread()) {
     MOZ_ASSERT(workerPrivate);
     if (NS_WARN_IF(!consumer->RegisterWorkerHolder(workerPrivate))) {
       aRv.Throw(NS_ERROR_FAILURE);
       return nullptr;
     }
   } else {
@@ -375,39 +385,61 @@ FetchBodyConsumer<Derived>::ReleaseObjec
     if (os) {
       os->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC);
       os->RemoveObserver(this, DOM_WINDOW_FROZEN_TOPIC);
     }
   }
 
   mGlobal = nullptr;
   mWorkerHolder = nullptr;
+
+#ifdef DEBUG
   mBody = nullptr;
+#endif
 }
 
 template <class Derived>
 FetchBodyConsumer<Derived>::FetchBodyConsumer(nsIEventTarget* aMainThreadEventTarget,
                                               nsIGlobalObject* aGlobalObject,
                                               WorkerPrivate* aWorkerPrivate,
                                               FetchBody<Derived>* aBody,
+                                              nsIInputStream* aBodyStream,
                                               Promise* aPromise,
                                               FetchConsumeType aType)
   : mTargetThread(NS_GetCurrentThread())
   , mMainThreadEventTarget(aMainThreadEventTarget)
+#ifdef DEBUG
   , mBody(aBody)
+#endif
+  , mBodyStream(aBodyStream)
+  , mBlobStorageType(MutableBlobStorage::eOnlyInMemory)
   , mGlobal(aGlobalObject)
   , mWorkerPrivate(aWorkerPrivate)
   , mConsumeType(aType)
   , mConsumePromise(aPromise)
   , mBodyConsumed(false)
   , mShuttingDown(false)
 {
   MOZ_ASSERT(aMainThreadEventTarget);
   MOZ_ASSERT(aBody);
+  MOZ_ASSERT(aBodyStream);
   MOZ_ASSERT(aPromise);
+
+  const mozilla::UniquePtr<mozilla::ipc::PrincipalInfo>& principalInfo =
+    aBody->DerivedClass()->GetPrincipalInfo();
+  // We support temporary file for blobs only if the principal is known and
+  // it's system or content not in private Browsing.
+  if (principalInfo &&
+      (principalInfo->type() == mozilla::ipc::PrincipalInfo::TSystemPrincipalInfo ||
+       (principalInfo->type() == mozilla::ipc::PrincipalInfo::TContentPrincipalInfo &&
+        principalInfo->get_ContentPrincipalInfo().attrs().mPrivateBrowsingId == 0))) {
+    mBlobStorageType = MutableBlobStorage::eCouldBeInTemporaryFile;
+  }
+
+  mBodyMimeType = aBody->MimeType();
 }
 
 template <class Derived>
 FetchBodyConsumer<Derived>::~FetchBodyConsumer()
 {
 }
 
 template <class Derived>
@@ -449,55 +481,32 @@ FetchBodyConsumer<Derived>::BeginConsume
 
   // Nothing to do.
   if (mShuttingDown) {
     return;
   }
 
   AutoFailConsumeBody<Derived> autoReject(this);
 
-  nsresult rv;
-  nsCOMPtr<nsIInputStream> stream;
-  mBody->DerivedClass()->GetBody(getter_AddRefs(stream));
-  if (!stream) {
-    rv = NS_NewCStringInputStream(getter_AddRefs(stream), EmptyCString());
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return;
-    }
-  }
-
   nsCOMPtr<nsIInputStreamPump> pump;
-  rv = NS_NewInputStreamPump(getter_AddRefs(pump),
-                             stream, -1, -1, 0, 0, false,
-                             mMainThreadEventTarget);
+  nsresult rv = NS_NewInputStreamPump(getter_AddRefs(pump),
+                                      mBodyStream, -1, -1, 0, 0, false,
+                                      mMainThreadEventTarget);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return;
   }
 
   RefPtr<ConsumeBodyDoneObserver<Derived>> p =
    new ConsumeBodyDoneObserver<Derived>(this);
 
   nsCOMPtr<nsIStreamListener> listener;
   if (mConsumeType == CONSUME_BLOB) {
-    MutableBlobStorage::MutableBlobStorageType type =
-      MutableBlobStorage::eOnlyInMemory;
-
-    const mozilla::UniquePtr<mozilla::ipc::PrincipalInfo>& principalInfo =
-      mBody->DerivedClass()->GetPrincipalInfo();
-    // We support temporary file for blobs only if the principal is known and
-    // it's system or content not in private Browsing.
-    if (principalInfo &&
-        (principalInfo->type() == mozilla::ipc::PrincipalInfo::TSystemPrincipalInfo ||
-         (principalInfo->type() == mozilla::ipc::PrincipalInfo::TContentPrincipalInfo &&
-          principalInfo->get_ContentPrincipalInfo().attrs().mPrivateBrowsingId == 0))) {
-      type = MutableBlobStorage::eCouldBeInTemporaryFile;
-    }
-
-    listener = new MutableBlobStreamListener(type, nullptr, mBody->MimeType(),
-                                             p, mMainThreadEventTarget);
+    listener = new MutableBlobStreamListener(mBlobStorageType, nullptr,
+                                             mBodyMimeType, p,
+                                             mMainThreadEventTarget);
   } else {
     nsCOMPtr<nsIStreamLoader> loader;
     rv = NS_NewStreamLoader(getter_AddRefs(loader), p);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return;
     }
 
     listener = loader;
@@ -568,17 +577,17 @@ FetchBodyConsumer<Derived>::ContinueCons
   if (NS_FAILED(aStatus)) {
     return;
   }
 
   // Finish successfully consuming body according to type.
   MOZ_ASSERT(aResult);
 
   AutoJSAPI jsapi;
-  if (!jsapi.Init(mBody->DerivedClass()->GetParentObject())) {
+  if (!jsapi.Init(mGlobal)) {
     localPromise->MaybeReject(NS_ERROR_UNEXPECTED);
     return;
   }
 
   JSContext* cx = jsapi.cx();
   ErrorResult error;
 
   switch (mConsumeType) {
@@ -602,18 +611,17 @@ FetchBodyConsumer<Derived>::ContinueCons
       break;
     }
     case CONSUME_FORMDATA: {
       nsCString data;
       data.Adopt(reinterpret_cast<char*>(aResult), aResultLength);
       aResult = nullptr;
 
       RefPtr<dom::FormData> fd =
-        BodyUtil::ConsumeFormData(mBody->DerivedClass()->GetParentObject(),
-                                  mBody->MimeType(), data, error);
+        BodyUtil::ConsumeFormData(mGlobal, mBodyMimeType, data, error);
       if (!error.Failed()) {
         localPromise->MaybeResolve(fd);
       }
       break;
     }
     case CONSUME_TEXT:
       // fall through handles early exit.
     case CONSUME_JSON: {
@@ -658,18 +666,17 @@ FetchBodyConsumer<Derived>::ContinueCons
   MOZ_ASSERT(mBody->BodyUsed());
 
   MOZ_ASSERT(mConsumePromise);
   RefPtr<Promise> localPromise = mConsumePromise.forget();
 
   // Release the pump.
   ShutDownMainThreadConsuming();
 
-  RefPtr<dom::Blob> blob =
-    dom::Blob::Create(mBody->DerivedClass()->GetParentObject(), aBlobImpl);
+  RefPtr<dom::Blob> blob = dom::Blob::Create(mGlobal, aBlobImpl);
   MOZ_ASSERT(blob);
 
   localPromise->MaybeResolve(blob);
 
   ReleaseObject();
 }
 
 template <class Derived>
--- a/dom/fetch/FetchConsumer.h
+++ b/dom/fetch/FetchConsumer.h
@@ -3,16 +3,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_FetchConsumer_h
 #define mozilla_dom_FetchConsumer_h
 
 #include "Fetch.h"
+#include "mozilla/dom/MutableBlobStorage.h"
 #include "nsIObserver.h"
 #include "nsWeakReference.h"
 
 class nsIThread;
 
 namespace mozilla {
 namespace dom {
 
@@ -41,22 +42,16 @@ public:
          nsIEventTarget* aMainThreadEventTarget,
          FetchBody<Derived>* aBody,
          FetchConsumeType aType,
          ErrorResult& aRv);
 
   void
   ReleaseObject();
 
-  FetchBody<Derived>*
-  Body() const
-  {
-    return mBody;
-  }
-
   void
   BeginConsumeBodyMainThread();
 
   void
   ContinueConsumeBody(nsresult aStatus, uint32_t aLength, uint8_t* aResult);
 
   void
   ContinueConsumeBlobBody(BlobImpl* aBlobImpl);
@@ -76,30 +71,39 @@ public:
     mConsumeBodyPump = nullptr;
   }
 
 private:
   FetchBodyConsumer(nsIEventTarget* aMainThreadEventTarget,
                     nsIGlobalObject* aGlobalObject,
                     workers::WorkerPrivate* aWorkerPrivate,
                     FetchBody<Derived>* aBody,
+                    nsIInputStream* aBodyStream,
                     Promise* aPromise,
                     FetchConsumeType aType);
 
   ~FetchBodyConsumer();
 
   void
   AssertIsOnTargetThread() const;
 
   bool
   RegisterWorkerHolder(workers::WorkerPrivate* aWorkerPrivate);
 
   nsCOMPtr<nsIThread> mTargetThread;
   nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
+
+#ifdef DEBUG
+  // This is used only to check if the body has been correctly consumed.
   RefPtr<FetchBody<Derived>> mBody;
+#endif
+
+  nsCOMPtr<nsIInputStream> mBodyStream;
+  MutableBlobStorage::MutableBlobStorageType mBlobStorageType;
+  nsCString mBodyMimeType;
 
   // Set when consuming the body is attempted on a worker.
   // Unset when consumption is done/aborted.
   // This WorkerHolder keeps alive the consumer via a cycle.
   UniquePtr<workers::WorkerHolder> mWorkerHolder;
 
   nsCOMPtr<nsIGlobalObject> mGlobal;