Bug 1381748 - Cleanup FetchConsumer workflow - part 1 - no mBody, r=catalinb

This commit is contained in:
Andrea Marchesini 2017-07-26 08:54:07 +02:00
Родитель 3bacd995dc
Коммит cad3905562
2 изменённых файлов: 52 добавлений и 41 удалений

Просмотреть файл

@ -316,6 +316,15 @@ FetchBodyConsumer<Derived>::Create(nsIGlobalObject* aGlobal,
MOZ_ASSERT(aBody); MOZ_ASSERT(aBody);
MOZ_ASSERT(aMainThreadEventTarget); 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); RefPtr<Promise> promise = Promise::Create(aGlobal, aRv);
if (aRv.Failed()) { if (aRv.Failed()) {
return nullptr; return nullptr;
@ -329,7 +338,8 @@ FetchBodyConsumer<Derived>::Create(nsIGlobalObject* aGlobal,
RefPtr<FetchBodyConsumer<Derived>> consumer = RefPtr<FetchBodyConsumer<Derived>> consumer =
new FetchBodyConsumer<Derived>(aMainThreadEventTarget, aGlobal, new FetchBodyConsumer<Derived>(aMainThreadEventTarget, aGlobal,
workerPrivate, aBody, promise, aType); workerPrivate, aBody, bodyStream, promise,
aType);
if (!NS_IsMainThread()) { if (!NS_IsMainThread()) {
MOZ_ASSERT(workerPrivate); MOZ_ASSERT(workerPrivate);
@ -380,7 +390,10 @@ FetchBodyConsumer<Derived>::ReleaseObject()
mGlobal = nullptr; mGlobal = nullptr;
mWorkerHolder = nullptr; mWorkerHolder = nullptr;
#ifdef DEBUG
mBody = nullptr; mBody = nullptr;
#endif
} }
template <class Derived> template <class Derived>
@ -388,11 +401,16 @@ FetchBodyConsumer<Derived>::FetchBodyConsumer(nsIEventTarget* aMainThreadEventTa
nsIGlobalObject* aGlobalObject, nsIGlobalObject* aGlobalObject,
WorkerPrivate* aWorkerPrivate, WorkerPrivate* aWorkerPrivate,
FetchBody<Derived>* aBody, FetchBody<Derived>* aBody,
nsIInputStream* aBodyStream,
Promise* aPromise, Promise* aPromise,
FetchConsumeType aType) FetchConsumeType aType)
: mTargetThread(NS_GetCurrentThread()) : mTargetThread(NS_GetCurrentThread())
, mMainThreadEventTarget(aMainThreadEventTarget) , mMainThreadEventTarget(aMainThreadEventTarget)
#ifdef DEBUG
, mBody(aBody) , mBody(aBody)
#endif
, mBodyStream(aBodyStream)
, mBlobStorageType(MutableBlobStorage::eOnlyInMemory)
, mGlobal(aGlobalObject) , mGlobal(aGlobalObject)
, mWorkerPrivate(aWorkerPrivate) , mWorkerPrivate(aWorkerPrivate)
, mConsumeType(aType) , mConsumeType(aType)
@ -402,7 +420,21 @@ FetchBodyConsumer<Derived>::FetchBodyConsumer(nsIEventTarget* aMainThreadEventTa
{ {
MOZ_ASSERT(aMainThreadEventTarget); MOZ_ASSERT(aMainThreadEventTarget);
MOZ_ASSERT(aBody); MOZ_ASSERT(aBody);
MOZ_ASSERT(aBodyStream);
MOZ_ASSERT(aPromise); 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> template <class Derived>
@ -454,20 +486,10 @@ FetchBodyConsumer<Derived>::BeginConsumeBodyMainThread()
AutoFailConsumeBody<Derived> autoReject(this); 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; nsCOMPtr<nsIInputStreamPump> pump;
rv = NS_NewInputStreamPump(getter_AddRefs(pump), nsresult rv = NS_NewInputStreamPump(getter_AddRefs(pump),
stream, -1, -1, 0, 0, false, mBodyStream, -1, -1, 0, 0, false,
mMainThreadEventTarget); mMainThreadEventTarget);
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
return; return;
} }
@ -477,22 +499,9 @@ FetchBodyConsumer<Derived>::BeginConsumeBodyMainThread()
nsCOMPtr<nsIStreamListener> listener; nsCOMPtr<nsIStreamListener> listener;
if (mConsumeType == CONSUME_BLOB) { if (mConsumeType == CONSUME_BLOB) {
MutableBlobStorage::MutableBlobStorageType type = listener = new MutableBlobStreamListener(mBlobStorageType, nullptr,
MutableBlobStorage::eOnlyInMemory; mBodyMimeType, p,
mMainThreadEventTarget);
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);
} else { } else {
nsCOMPtr<nsIStreamLoader> loader; nsCOMPtr<nsIStreamLoader> loader;
rv = NS_NewStreamLoader(getter_AddRefs(loader), p); rv = NS_NewStreamLoader(getter_AddRefs(loader), p);
@ -573,7 +582,7 @@ FetchBodyConsumer<Derived>::ContinueConsumeBody(nsresult aStatus,
MOZ_ASSERT(aResult); MOZ_ASSERT(aResult);
AutoJSAPI jsapi; AutoJSAPI jsapi;
if (!jsapi.Init(mBody->DerivedClass()->GetParentObject())) { if (!jsapi.Init(mGlobal)) {
localPromise->MaybeReject(NS_ERROR_UNEXPECTED); localPromise->MaybeReject(NS_ERROR_UNEXPECTED);
return; return;
} }
@ -607,8 +616,7 @@ FetchBodyConsumer<Derived>::ContinueConsumeBody(nsresult aStatus,
aResult = nullptr; aResult = nullptr;
RefPtr<dom::FormData> fd = RefPtr<dom::FormData> fd =
BodyUtil::ConsumeFormData(mBody->DerivedClass()->GetParentObject(), BodyUtil::ConsumeFormData(mGlobal, mBodyMimeType, data, error);
mBody->MimeType(), data, error);
if (!error.Failed()) { if (!error.Failed()) {
localPromise->MaybeResolve(fd); localPromise->MaybeResolve(fd);
} }
@ -663,8 +671,7 @@ FetchBodyConsumer<Derived>::ContinueConsumeBlobBody(BlobImpl* aBlobImpl)
// Release the pump. // Release the pump.
ShutDownMainThreadConsuming(); ShutDownMainThreadConsuming();
RefPtr<dom::Blob> blob = RefPtr<dom::Blob> blob = dom::Blob::Create(mGlobal, aBlobImpl);
dom::Blob::Create(mBody->DerivedClass()->GetParentObject(), aBlobImpl);
MOZ_ASSERT(blob); MOZ_ASSERT(blob);
localPromise->MaybeResolve(blob); localPromise->MaybeResolve(blob);

Просмотреть файл

@ -8,6 +8,7 @@
#define mozilla_dom_FetchConsumer_h #define mozilla_dom_FetchConsumer_h
#include "Fetch.h" #include "Fetch.h"
#include "mozilla/dom/MutableBlobStorage.h"
#include "nsIObserver.h" #include "nsIObserver.h"
#include "nsWeakReference.h" #include "nsWeakReference.h"
@ -46,12 +47,6 @@ public:
void void
ReleaseObject(); ReleaseObject();
FetchBody<Derived>*
Body() const
{
return mBody;
}
void void
BeginConsumeBodyMainThread(); BeginConsumeBodyMainThread();
@ -81,6 +76,7 @@ private:
nsIGlobalObject* aGlobalObject, nsIGlobalObject* aGlobalObject,
workers::WorkerPrivate* aWorkerPrivate, workers::WorkerPrivate* aWorkerPrivate,
FetchBody<Derived>* aBody, FetchBody<Derived>* aBody,
nsIInputStream* aBodyStream,
Promise* aPromise, Promise* aPromise,
FetchConsumeType aType); FetchConsumeType aType);
@ -94,7 +90,15 @@ private:
nsCOMPtr<nsIThread> mTargetThread; nsCOMPtr<nsIThread> mTargetThread;
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget; nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
#ifdef DEBUG
// This is used only to check if the body has been correctly consumed.
RefPtr<FetchBody<Derived>> mBody; RefPtr<FetchBody<Derived>> mBody;
#endif
nsCOMPtr<nsIInputStream> mBodyStream;
MutableBlobStorage::MutableBlobStorageType mBlobStorageType;
nsCString mBodyMimeType;
// Set when consuming the body is attempted on a worker. // Set when consuming the body is attempted on a worker.
// Unset when consumption is done/aborted. // Unset when consumption is done/aborted.