Bug 1798319 - Implement modulepreload in early hints r=manuel,smaug,necko-reviewers,kershaw

The aEarlyHintPreloaderId parameter for StartLoad/StartLoadInternal is changed
to be a member variable of ScriptLoadRequest instead so that an initiator type
of early hints can be set for module requests. Before, ModuleLoader would always
pass in a zero value for the id since ModuleLoaderBase has no concept of early
hints when it calls StartFetch.

As a prerequisite for early hints support, this commit also implements
modulepreload in link headers (Bug 1773056).

Differential Revision: https://phabricator.services.mozilla.com/D180020
This commit is contained in:
Em Zhan
2023-06-26 10:49:53 +00:00
parent 8c76ee691a
commit 5959006756
19 changed files with 204 additions and 63 deletions

View File

@@ -97,6 +97,8 @@
#include "mozilla/Utf8.h" // mozilla::Utf8Unit
#include "nsIScriptError.h"
#include "nsIAsyncOutputStream.h"
#include "js/loader/ModuleLoaderBase.h"
#include "mozilla/Maybe.h"
using JS::SourceText;
using namespace JS::loader;
@@ -526,7 +528,7 @@ nsresult ScriptLoader::RestartLoad(ScriptLoadRequest* aRequest) {
if (aRequest->IsModuleRequest()) {
rv = aRequest->AsModuleRequest()->RestartModuleLoad();
} else {
rv = StartLoad(aRequest, 0, Nothing());
rv = StartLoad(aRequest, Nothing());
}
if (NS_FAILED(rv)) {
return rv;
@@ -538,17 +540,17 @@ nsresult ScriptLoader::RestartLoad(ScriptLoadRequest* aRequest) {
}
nsresult ScriptLoader::StartLoad(
ScriptLoadRequest* aRequest, uint64_t aEarlyHintPreloaderId,
ScriptLoadRequest* aRequest,
const Maybe<nsAutoString>& aCharsetForPreload) {
if (aRequest->IsModuleRequest()) {
return aRequest->AsModuleRequest()->StartModuleLoad();
}
return StartClassicLoad(aRequest, aEarlyHintPreloaderId, aCharsetForPreload);
return StartClassicLoad(aRequest, aCharsetForPreload);
}
nsresult ScriptLoader::StartClassicLoad(
ScriptLoadRequest* aRequest, uint64_t aEarlyHintPreloaderId,
ScriptLoadRequest* aRequest,
const Maybe<nsAutoString>& aCharsetForPreload) {
MOZ_ASSERT(aRequest->IsFetching());
NS_ENSURE_TRUE(mDocument, NS_ERROR_NULL_POINTER);
@@ -573,8 +575,7 @@ nsresult ScriptLoader::StartClassicLoad(
securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
nsresult rv = StartLoadInternal(aRequest, securityFlags,
aEarlyHintPreloaderId, aCharsetForPreload);
nsresult rv = StartLoadInternal(aRequest, securityFlags, aCharsetForPreload);
NS_ENSURE_SUCCESS(rv, rv);
@@ -593,7 +594,6 @@ static bool IsWebExtensionRequest(ScriptLoadRequest* aRequest) {
nsresult ScriptLoader::StartLoadInternal(
ScriptLoadRequest* aRequest, nsSecurityFlags securityFlags,
uint64_t aEarlyHintPreloaderId,
const Maybe<nsAutoString>& aCharsetForPreload) {
nsContentPolicyType contentPolicyType =
ScriptLoadRequestToContentPolicyType(aRequest);
@@ -620,12 +620,13 @@ nsresult ScriptLoader::StartLoadInternal(
NS_ENSURE_SUCCESS(rv, rv);
if (aEarlyHintPreloaderId) {
if (aRequest->mEarlyHintPreloaderId) {
nsCOMPtr<nsIHttpChannelInternal> channelInternal =
do_QueryInterface(channel);
NS_ENSURE_TRUE(channelInternal != nullptr, NS_ERROR_FAILURE);
rv = channelInternal->SetEarlyHintPreloaderId(aEarlyHintPreloaderId);
rv = channelInternal->SetEarlyHintPreloaderId(
aRequest->mEarlyHintPreloaderId);
NS_ENSURE_SUCCESS(rv, rv);
}
@@ -767,7 +768,7 @@ nsresult ScriptLoader::StartLoadInternal(
// Set the initiator type
nsCOMPtr<nsITimedChannel> timedChannel(do_QueryInterface(httpChannel));
if (timedChannel) {
if (aEarlyHintPreloaderId) {
if (aRequest->mEarlyHintPreloaderId) {
timedChannel->SetInitiatorType(u"early-hints"_ns);
} else if (aRequest->GetScriptLoadContext()->IsLinkPreloadScript()) {
timedChannel->SetInitiatorType(u"link"_ns);
@@ -800,12 +801,13 @@ nsresult ScriptLoader::StartLoadInternal(
aRequest->GetScriptLoadContext()->IsLinkPreloadScript(),
aRequest->IsModuleRequest());
if (aEarlyHintPreloaderId) {
if (aRequest->mEarlyHintPreloaderId) {
nsCOMPtr<nsIHttpChannelInternal> channelInternal =
do_QueryInterface(channel);
NS_ENSURE_TRUE(channelInternal != nullptr, NS_ERROR_FAILURE);
rv = channelInternal->SetEarlyHintPreloaderId(aEarlyHintPreloaderId);
rv = channelInternal->SetEarlyHintPreloaderId(
aRequest->mEarlyHintPreloaderId);
NS_ENSURE_SUCCESS(rv, rv);
}
rv = channel->AsyncOpen(loader);
@@ -1031,7 +1033,7 @@ bool ScriptLoader::ProcessExternalScript(nsIScriptElement* aElement,
LOG(("ScriptLoadRequest (%p): Created request for external script",
request.get()));
nsresult rv = StartLoad(request, 0, Nothing());
nsresult rv = StartLoad(request, Nothing());
if (NS_FAILED(rv)) {
ReportErrorToConsole(request, rv);
@@ -3568,6 +3570,7 @@ void ScriptLoader::PreloadURI(nsIURI* aURI, const nsAString& aCharset,
request->GetScriptLoadContext()->mScriptFromHead = aScriptFromHead;
request->GetScriptLoadContext()->SetScriptMode(aDefer, aAsync, aLinkPreload);
request->GetScriptLoadContext()->SetIsPreloadRequest();
request->mEarlyHintPreloaderId = aEarlyHintPreloaderId;
if (LOG_ENABLED()) {
nsAutoCString url;
@@ -3577,7 +3580,7 @@ void ScriptLoader::PreloadURI(nsIURI* aURI, const nsAString& aCharset,
}
nsAutoString charset(aCharset);
nsresult rv = StartLoad(request, aEarlyHintPreloaderId, Some(charset));
nsresult rv = StartLoad(request, Some(charset));
if (NS_FAILED(rv)) {
return;
}