forked from mirrors/gecko-dev
In the previous implementation, FetchEvent.preloadResponse is resolved when the response fetching finishes. However, ServiceWorker responding letency could be increased since waiting for preloadResponse finishes. The patch resolves FetchEvent.preloadResponse earlier when the response is available. The basic idea is to resolve the preload response when FetchInstance::OnResponseAvailableInternal() is called. Then propagating the response from the parent process main thread to the content process worker thread. This is achieved by IPC PFetchEventOp::Send/RecvPreloadResponse -> PFetchEventOpProxy::Send/RecvPreloadResponse. Since we can only get the response's ResourceTiming when FetchInstance::OnResponseEnd() is called. This patch introduces a new IPC method to propagate the ResourceTiming information from the parent process main thread to the content process worker thread. PFetchEventOp::Send/RecvPreloadResponseEnd -> PFetchEventOpProxy->Send/RecvPreloadResponseEnd. The tricky of this patch is we must extend the life cycle of FetchEventOp object if preloadResponse is set into FetchEvent. That because ServiceWorker could resolve FetchEvent.respondWith() by using FetchEvent.preloadResponse. In that case, FetchEventOp will get into a finish state and try to call FetchEventOpProxyChild::Senddelete with the operation result. However, the ResponseEnd could not be called at the moment, and we need to wait for the corresponding timing information and its end reason. To extend the life cycle of FetchEventOp, this patch cached the operation result while we get FetchEventOp::Callback is called. Then call FetchEventOpProxyChile::Senddelete() in FetchEventOpProxyChild::RecvPreloadResponseEnd() to close IPC. Or Senddelete() will be called while ActorDestroy() caused by shutdown. Differential Revision: https://phabricator.services.mozilla.com/D145338
88 lines
2.8 KiB
C++
88 lines
2.8 KiB
C++
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
/* 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/. */
|
|
|
|
#include "FetchEventOpParent.h"
|
|
|
|
#include "mozilla/dom/FetchTypes.h"
|
|
#include "nsDebug.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
#include "mozilla/RefPtr.h"
|
|
#include "mozilla/Unused.h"
|
|
#include "mozilla/dom/FetchEventOpProxyParent.h"
|
|
#include "mozilla/dom/FetchStreamUtils.h"
|
|
#include "mozilla/dom/InternalResponse.h"
|
|
#include "mozilla/dom/RemoteWorkerControllerParent.h"
|
|
#include "mozilla/dom/RemoteWorkerParent.h"
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
|
|
|
namespace mozilla {
|
|
|
|
using namespace ipc;
|
|
|
|
namespace dom {
|
|
|
|
Tuple<Maybe<ParentToParentInternalResponse>, Maybe<ResponseEndArgs>>
|
|
FetchEventOpParent::OnStart(
|
|
MovingNotNull<RefPtr<FetchEventOpProxyParent>> aFetchEventOpProxyParent) {
|
|
Maybe<ParentToParentInternalResponse> preloadResponse =
|
|
std::move(mState.as<Pending>().mPreloadResponse);
|
|
Maybe<ResponseEndArgs> preloadResponseEndArgs =
|
|
std::move(mState.as<Pending>().mEndArgs);
|
|
mState = AsVariant(Started{std::move(aFetchEventOpProxyParent)});
|
|
return MakeTuple(preloadResponse, preloadResponseEndArgs);
|
|
}
|
|
|
|
void FetchEventOpParent::OnFinish() {
|
|
MOZ_ASSERT(mState.is<Started>());
|
|
mState = AsVariant(Finished());
|
|
}
|
|
|
|
mozilla::ipc::IPCResult FetchEventOpParent::RecvPreloadResponse(
|
|
ParentToParentInternalResponse&& aResponse) {
|
|
AssertIsOnBackgroundThread();
|
|
|
|
mState.match(
|
|
[&aResponse](Pending& aPending) {
|
|
MOZ_ASSERT(aPending.mPreloadResponse.isNothing());
|
|
aPending.mPreloadResponse = Some(std::move(aResponse));
|
|
},
|
|
[&aResponse](Started& aStarted) {
|
|
auto backgroundParent = WrapNotNull(
|
|
WrapNotNull(aStarted.mFetchEventOpProxyParent->Manager())
|
|
->Manager());
|
|
Unused << aStarted.mFetchEventOpProxyParent->SendPreloadResponse(
|
|
ToParentToChild(aResponse, backgroundParent));
|
|
},
|
|
[](const Finished&) {});
|
|
|
|
return IPC_OK();
|
|
}
|
|
|
|
mozilla::ipc::IPCResult FetchEventOpParent::RecvPreloadResponseEnd(
|
|
ResponseEndArgs&& aArgs) {
|
|
AssertIsOnBackgroundThread();
|
|
|
|
mState.match(
|
|
[&aArgs](Pending& aPending) {
|
|
MOZ_ASSERT(aPending.mEndArgs.isNothing());
|
|
aPending.mEndArgs = Some(std::move(aArgs));
|
|
},
|
|
[&aArgs](Started& aStarted) {
|
|
Unused << aStarted.mFetchEventOpProxyParent->SendPreloadResponseEnd(
|
|
std::move(aArgs));
|
|
},
|
|
[](const Finished&) {});
|
|
|
|
return IPC_OK();
|
|
}
|
|
|
|
void FetchEventOpParent::ActorDestroy(ActorDestroyReason) {
|
|
AssertIsOnBackgroundThread();
|
|
}
|
|
|
|
} // namespace dom
|
|
} // namespace mozilla
|