Bug 1771867 - Early Hints Phase 2 - Part 5: Pass early hint preload to script preloader r=necko-reviewers,valentin

Note that modules can't be specified in Link preloads with `rel=preload`,
only in `rel=modulepreload`.  We currently only support `rel=preload` in
early hints. See Bug 1798319 for updates on module preloads.

Differential Revision: https://phabricator.services.mozilla.com/D161175
This commit is contained in:
Manuel Bucher 2022-12-02 16:15:54 +00:00
Родитель 817a908c7b
Коммит 7681efff11
5 изменённых файлов: 48 добавлений и 17 удалений

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

@ -107,7 +107,8 @@ nsresult ModuleLoader::StartFetch(ModuleLoadRequest* aRequest) {
securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
// Delegate Shared Behavior to base ScriptLoader
nsresult rv = GetScriptLoader()->StartLoadInternal(aRequest, securityFlags);
nsresult rv =
GetScriptLoader()->StartLoadInternal(aRequest, securityFlags, 0);
NS_ENSURE_SUCCESS(rv, rv);
// https://html.spec.whatwg.org/multipage/webappapis.html#fetch-an-import()-module-script-graph

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

@ -9,6 +9,7 @@
#include "ScriptTrace.h"
#include "ModuleLoader.h"
#include "nsIChildChannel.h"
#include "zlib.h"
#include "prsystem.h"
@ -519,7 +520,7 @@ nsresult ScriptLoader::RestartLoad(ScriptLoadRequest* aRequest) {
if (aRequest->IsModuleRequest()) {
rv = aRequest->AsModuleRequest()->RestartModuleLoad();
} else {
rv = StartLoad(aRequest);
rv = StartLoad(aRequest, 0);
}
if (NS_FAILED(rv)) {
return rv;
@ -530,15 +531,17 @@ nsresult ScriptLoader::RestartLoad(ScriptLoadRequest* aRequest) {
return NS_BINDING_RETARGETED;
}
nsresult ScriptLoader::StartLoad(ScriptLoadRequest* aRequest) {
nsresult ScriptLoader::StartLoad(ScriptLoadRequest* aRequest,
uint64_t aEarlyHintPreloaderId) {
if (aRequest->IsModuleRequest()) {
return aRequest->AsModuleRequest()->StartModuleLoad();
}
return StartClassicLoad(aRequest);
return StartClassicLoad(aRequest, aEarlyHintPreloaderId);
}
nsresult ScriptLoader::StartClassicLoad(ScriptLoadRequest* aRequest) {
nsresult ScriptLoader::StartClassicLoad(ScriptLoadRequest* aRequest,
uint64_t aEarlyHintPreloaderId) {
MOZ_ASSERT(aRequest->IsFetching());
NS_ENSURE_TRUE(mDocument, NS_ERROR_NULL_POINTER);
aRequest->SetUnknownDataType();
@ -562,7 +565,8 @@ nsresult ScriptLoader::StartClassicLoad(ScriptLoadRequest* aRequest) {
securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
nsresult rv = StartLoadInternal(aRequest, securityFlags);
nsresult rv =
StartLoadInternal(aRequest, securityFlags, aEarlyHintPreloaderId);
NS_ENSURE_SUCCESS(rv, rv);
@ -580,7 +584,8 @@ static bool IsWebExtensionRequest(ScriptLoadRequest* aRequest) {
}
nsresult ScriptLoader::StartLoadInternal(ScriptLoadRequest* aRequest,
nsSecurityFlags securityFlags) {
nsSecurityFlags securityFlags,
uint64_t aEarlyHintPreloaderId) {
nsContentPolicyType contentPolicyType =
ScriptLoadRequestToContentPolicyType(aRequest);
nsCOMPtr<nsINode> context;
@ -606,6 +611,15 @@ nsresult ScriptLoader::StartLoadInternal(ScriptLoadRequest* aRequest,
NS_ENSURE_SUCCESS(rv, rv);
if (aEarlyHintPreloaderId) {
nsCOMPtr<nsIHttpChannelInternal> channelInternal =
do_QueryInterface(channel);
NS_ENSURE_TRUE(channelInternal != nullptr, NS_ERROR_FAILURE);
rv = channelInternal->SetEarlyHintPreloaderId(aEarlyHintPreloaderId);
NS_ENSURE_SUCCESS(rv, rv);
}
// snapshot the nonce at load start time for performing CSP checks
if (contentPolicyType == nsIContentPolicy::TYPE_INTERNAL_SCRIPT ||
contentPolicyType == nsIContentPolicy::TYPE_INTERNAL_MODULE) {
@ -727,7 +741,9 @@ nsresult ScriptLoader::StartLoadInternal(ScriptLoadRequest* aRequest,
// Set the initiator type
nsCOMPtr<nsITimedChannel> timedChannel(do_QueryInterface(httpChannel));
if (timedChannel) {
if (aRequest->GetScriptLoadContext()->IsLinkPreloadScript()) {
if (aEarlyHintPreloaderId) {
timedChannel->SetInitiatorType(u"early-hints"_ns);
} else if (aRequest->GetScriptLoadContext()->IsLinkPreloadScript()) {
timedChannel->SetInitiatorType(u"link"_ns);
} else {
timedChannel->SetInitiatorType(u"script"_ns);
@ -757,6 +773,14 @@ nsresult ScriptLoader::StartLoadInternal(ScriptLoadRequest* aRequest,
key, channel, mDocument,
aRequest->GetScriptLoadContext()->IsLinkPreloadScript());
if (aEarlyHintPreloaderId) {
nsCOMPtr<nsIHttpChannelInternal> channelInternal =
do_QueryInterface(channel);
NS_ENSURE_TRUE(channelInternal != nullptr, NS_ERROR_FAILURE);
rv = channelInternal->SetEarlyHintPreloaderId(aEarlyHintPreloaderId);
NS_ENSURE_SUCCESS(rv, rv);
}
rv = channel->AsyncOpen(loader);
if (NS_FAILED(rv)) {
@ -1002,7 +1026,7 @@ bool ScriptLoader::ProcessExternalScript(nsIScriptElement* aElement,
LOG(("ScriptLoadRequest (%p): Created request for external script",
request.get()));
nsresult rv = StartLoad(request);
nsresult rv = StartLoad(request, 0);
if (NS_FAILED(rv)) {
ReportErrorToConsole(request, rv);
@ -3536,7 +3560,8 @@ void ScriptLoader::PreloadURI(nsIURI* aURI, const nsAString& aCharset,
const nsAString& aIntegrity, bool aScriptFromHead,
bool aAsync, bool aDefer, bool aNoModule,
bool aLinkPreload,
const ReferrerPolicy aReferrerPolicy) {
const ReferrerPolicy aReferrerPolicy,
uint64_t aEarlyHintPreloaderId) {
NS_ENSURE_TRUE_VOID(mDocument);
// Check to see if scripts has been turned off.
if (!mEnabled || !mDocument->IsScriptEnabled()) {
@ -3584,7 +3609,7 @@ void ScriptLoader::PreloadURI(nsIURI* aURI, const nsAString& aCharset,
request.get(), url.get()));
}
nsresult rv = StartLoad(request);
nsresult rv = StartLoad(request, aEarlyHintPreloaderId);
if (NS_FAILED(rv)) {
return;
}

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

@ -392,7 +392,8 @@ class ScriptLoader final : public JS::loader::ScriptLoaderInterface {
const nsAString& aIntegrity, bool aScriptFromHead,
bool aAsync, bool aDefer, bool aNoModule,
bool aLinkPreload,
const ReferrerPolicy aReferrerPolicy);
const ReferrerPolicy aReferrerPolicy,
uint64_t aEarlyHintPreloaderId);
/**
* Process a request that was deferred so that the script could be compiled
@ -490,18 +491,21 @@ class ScriptLoader final : public JS::loader::ScriptLoaderInterface {
/**
* Start a load for aRequest's URI.
*/
nsresult StartLoad(ScriptLoadRequest* aRequest);
nsresult StartLoad(ScriptLoadRequest* aRequest,
uint64_t aEarlyHintPreloaderId);
/**
* Start a load for a classic script URI.
* Sets up the necessary security flags before calling StartLoadInternal.
*/
nsresult StartClassicLoad(ScriptLoadRequest* aRequest);
nsresult StartClassicLoad(ScriptLoadRequest* aRequest,
uint64_t aEarlyHintPreloaderId);
/**
* Start a load for a module script URI.
*/
nsresult StartLoadInternal(ScriptLoadRequest* aRequest,
nsSecurityFlags securityFlags);
nsSecurityFlags securityFlags,
uint64_t aEarlyHintPreloaderId);
/**
* Abort the current stream, and re-start with a new load request from scratch

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

@ -1218,7 +1218,7 @@ void nsHtml5TreeOpExecutor::PreloadScript(
mDocument->ScriptLoader()->PreloadURI(
uri, aCharset, aType, aCrossOrigin, aIntegrity, aScriptFromHead, aAsync,
aDefer, aNoModule, aLinkPreload,
GetPreloadReferrerPolicy(aReferrerPolicy));
GetPreloadReferrerPolicy(aReferrerPolicy), 0);
}
void nsHtml5TreeOpExecutor::PreloadStyle(const nsAString& aURL,

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

@ -198,7 +198,8 @@ void PreloadService::PreloadScript(nsIURI* aURI, const nsAString& aType,
uint64_t aEarlyHintPreloaderId) {
mDocument->ScriptLoader()->PreloadURI(
aURI, aCharset, aType, aCrossOrigin, aIntegrity, aScriptFromHead, false,
false, false, true, PreloadReferrerPolicy(aReferrerPolicy));
false, false, true, PreloadReferrerPolicy(aReferrerPolicy),
aEarlyHintPreloaderId);
}
void PreloadService::PreloadImage(nsIURI* aURI, const nsAString& aCrossOrigin,