From fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:14:29 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- dom/script/ScriptLoader.cpp | 140 +++++++++++---------------------- dom/script/moz.build | 7 +- dom/script/nsIScriptLoaderObserver.h | 70 +++++++++++++++++ dom/script/nsIScriptLoaderObserver.idl | 48 ----------- 4 files changed, 119 insertions(+), 146 deletions(-) create mode 100644 dom/script/nsIScriptLoaderObserver.h delete mode 100644 dom/script/nsIScriptLoaderObserver.idl (limited to 'dom/script') diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp index 43f718ab64..4fa08e074d 100644 --- a/dom/script/ScriptLoader.cpp +++ b/dom/script/ScriptLoader.cpp @@ -12,21 +12,16 @@ #include "mozilla/Assertions.h" #include "mozilla/dom/FetchPriority.h" +#include "mozilla/glean/GleanMetrics.h" #include "mozilla/dom/RequestBinding.h" -#include "nsIChildChannel.h" -#include "zlib.h" #include "prsystem.h" -#include "jsapi.h" -#include "jsfriendapi.h" -#include "js/Array.h" // JS::GetArrayLength #include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin #include "js/CompilationAndEvaluation.h" #include "js/CompileOptions.h" // JS::CompileOptions, JS::OwningCompileOptions, JS::DecodeOptions, JS::OwningDecodeOptions, JS::DelazificationOption #include "js/ContextOptions.h" // JS::ContextOptionsRef #include "js/experimental/JSStencil.h" // JS::Stencil, JS::InstantiationStorage #include "js/experimental/CompileScript.h" // JS::FrontendContext, JS::NewFrontendContext, JS::DestroyFrontendContext, JS::SetNativeStackQuota, JS::ThreadStackQuotaForSize, JS::CompilationStorage, JS::CompileGlobalScriptToStencil, JS::CompileModuleScriptToStencil, JS::DecodeStencil, JS::PrepareForInstantiate -#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* #include "js/loader/ScriptLoadRequest.h" #include "ScriptCompression.h" #include "js/loader/LoadedScript.h" @@ -34,8 +29,6 @@ #include "js/MemoryFunctions.h" #include "js/Modules.h" #include "js/PropertyAndElement.h" // JS_DefineProperty -#include "js/Realm.h" -#include "js/SourceText.h" #include "js/Transcoding.h" // JS::TranscodeRange, JS::TranscodeResult, JS::IsTranscodeFailureResult #include "js/Utility.h" #include "xpcpublic.h" @@ -54,7 +47,6 @@ #include "mozilla/dom/WindowContext.h" #include "mozilla/Mutex.h" // mozilla::Mutex #include "mozilla/net/UrlClassifierFeatureFactory.h" -#include "mozilla/Preferences.h" #include "mozilla/StaticPrefs_dom.h" #include "mozilla/StaticPrefs_javascript.h" #include "mozilla/StaticPrefs_network.h" @@ -80,16 +72,12 @@ #include "nsUnicharUtils.h" #include "nsError.h" #include "nsThreadUtils.h" -#include "nsDocShellCID.h" #include "nsIContentSecurityPolicy.h" #include "mozilla/Logging.h" #include "nsCRT.h" #include "nsContentCreatorFunctions.h" #include "nsProxyRelease.h" -#include "nsSandboxFlags.h" -#include "nsContentTypeParser.h" #include "nsINetworkPredictor.h" -#include "nsMimeTypes.h" #include "mozilla/ConsoleReportCollector.h" #include "mozilla/CycleCollectedJSContext.h" #include "mozilla/EventQueue.h" @@ -103,14 +91,12 @@ #include "mozilla/Telemetry.h" #include "mozilla/TimeStamp.h" #include "mozilla/UniquePtr.h" -#include "mozilla/Unused.h" #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; using mozilla::Telemetry::LABELS_DOM_SCRIPT_PRELOAD_RESULT; @@ -668,53 +654,35 @@ static void AdjustPriorityAndClassOfServiceForLinkPreloadScripts( nsIChannel* aChannel, ScriptLoadRequest* aRequest) { MOZ_ASSERT(aRequest->GetScriptLoadContext()->IsLinkPreloadScript()); + // Put it to the group that is not blocked by leaders and doesn't block + // follower at the same time. + // Giving it a much higher priority will make this request be processed + // ahead of other Unblocked requests, but with the same weight as + // Leaders. This will make us behave similar way for both http2 and http1. + ScriptLoadContext::PrioritizeAsPreload(aChannel); + if (!StaticPrefs::network_fetchpriority_enabled()) { - // Put it to the group that is not blocked by leaders and doesn't block - // follower at the same time. - // Giving it a much higher priority will make this request be processed - // ahead of other Unblocked requests, but with the same weight as - // Leaders. This will make us behave similar way for both http2 and http1. - ScriptLoadContext::PrioritizeAsPreload(aChannel); return; } - if (nsCOMPtr cos = do_QueryInterface(aChannel)) { - cos->AddClassFlags(nsIClassOfService::Unblocked); - } - if (nsCOMPtr supportsPriority = do_QueryInterface(aChannel)) { LOG(("Is FetchPriority(); + const auto fetchPriority = ToFetchPriority(aRequest->FetchPriority()); // The spec defines the priority to be set in an implementation defined // manner (, step 15 and // ). - // For web-compatibility, the fetch priority mapping from - // - // is taken. - const int32_t supportsPriorityValue = [&]() { - switch (fetchPriority) { - case RequestPriority::Auto: { - return nsISupportsPriority::PRIORITY_HIGH; - } - case RequestPriority::High: { - return nsISupportsPriority::PRIORITY_HIGH; - } - case RequestPriority::Low: { - return nsISupportsPriority::PRIORITY_LOW; - } - default: { - MOZ_ASSERT_UNREACHABLE(); - return nsISupportsPriority::PRIORITY_NORMAL; - } - } - }(); - - LogPriorityMapping(ScriptLoader::gScriptLoaderLog, - ToFetchPriority(fetchPriority), supportsPriorityValue); - - supportsPriority->SetPriority(supportsPriorityValue); + // See corresponding preferences in StaticPrefList.yaml for more context. + const int32_t supportsPriorityDelta = + FETCH_PRIORITY_ADJUSTMENT_FOR(link_preload_script, fetchPriority); + supportsPriority->AdjustPriority(supportsPriorityDelta); +#ifdef DEBUG + int32_t adjustedPriority; + supportsPriority->GetPriority(&adjustedPriority); + LogPriorityMapping(ScriptLoader::gScriptLoaderLog, fetchPriority, + adjustedPriority); +#endif } } @@ -729,50 +697,40 @@ void AdjustPriorityForNonLinkPreloadScripts(nsIChannel* aChannel, if (nsCOMPtr supportsPriority = do_QueryInterface(aChannel)) { LOG(("Is not FetchPriority(); + const auto fetchPriority = ToFetchPriority(aRequest->FetchPriority()); // The spec defines the priority to be set in an implementation defined // manner (, step 15 and // ). - // - // provides more context for the priority mapping. - const int32_t supportsPriorityValue = [&]() { - switch (fetchPriority) { - case RequestPriority::Auto: { - if (aRequest->IsModuleRequest()) { - return nsISupportsPriority::PRIORITY_HIGH; - } - - const ScriptLoadContext* scriptLoadContext = - aRequest->GetScriptLoadContext(); - if (scriptLoadContext->IsAsyncScript() || - scriptLoadContext->IsDeferredScript()) { - return nsISupportsPriority::PRIORITY_LOW; - } - - if (scriptLoadContext->mScriptFromHead) { - return nsISupportsPriority::PRIORITY_HIGH; - } - - return nsISupportsPriority::PRIORITY_NORMAL; - } - case RequestPriority::Low: { - return nsISupportsPriority::PRIORITY_LOW; - } - case RequestPriority::High: { - return nsISupportsPriority::PRIORITY_HIGH; - } - default: { - MOZ_ASSERT_UNREACHABLE(); - return nsISupportsPriority::PRIORITY_NORMAL; - } + // See corresponding preferences in StaticPrefList.yaml for more context. + const int32_t supportsPriorityDelta = [&]() { + const ScriptLoadContext* scriptLoadContext = + aRequest->GetScriptLoadContext(); + if (aRequest->IsModuleRequest()) { + return FETCH_PRIORITY_ADJUSTMENT_FOR(module_script, fetchPriority); + } + + if (scriptLoadContext->IsAsyncScript() || + scriptLoadContext->IsDeferredScript()) { + return FETCH_PRIORITY_ADJUSTMENT_FOR(async_or_defer_script, + fetchPriority); + } + + if (scriptLoadContext->mScriptFromHead) { + return FETCH_PRIORITY_ADJUSTMENT_FOR(script_in_head, fetchPriority); } + + return FETCH_PRIORITY_ADJUSTMENT_FOR(other_script, fetchPriority); }(); - if (supportsPriorityValue) { - LogPriorityMapping(ScriptLoader::gScriptLoaderLog, - ToFetchPriority(fetchPriority), supportsPriorityValue); - supportsPriority->SetPriority(supportsPriorityValue); + if (supportsPriorityDelta) { + supportsPriority->AdjustPriority(supportsPriorityDelta); +#ifdef DEBUG + int32_t adjustedPriority; + supportsPriority->GetPriority(&adjustedPriority); + LogPriorityMapping(ScriptLoader::gScriptLoaderLog, fetchPriority, + adjustedPriority); +#endif } } } @@ -1933,14 +1891,13 @@ class ScriptOrModuleCompileTask final : public CompileOrDecodeTask { JS::SetNativeStackQuota(mFrontendContext, JS::ThreadStackQuotaForSize(stackSize)); - JS::CompilationStorage compileStorage; auto compile = [&](auto& source) { if constexpr (target == CompilationTarget::Script) { return JS::CompileGlobalScriptToStencil(mFrontendContext, mOptions, - source, compileStorage); + source); } return JS::CompileModuleScriptToStencil(mFrontendContext, mOptions, - source, compileStorage); + source); }; return mMaybeSource.mapNonEmpty(compile); } @@ -2004,7 +1961,6 @@ class ScriptDecodeTask final : public CompileOrDecodeTask { already_AddRefed Decode() { // NOTE: JS::DecodeStencil doesn't need the stack quota. - JS::CompilationStorage compileStorage; RefPtr stencil; mResult = JS::DecodeStencil(mFrontendContext, mDecodeOptions, mRange, getter_AddRefs(stencil)); diff --git a/dom/script/moz.build b/dom/script/moz.build index 17fa695342..2b4ab40c9a 100644 --- a/dom/script/moz.build +++ b/dom/script/moz.build @@ -7,14 +7,9 @@ with Files("**"): BUG_COMPONENT = ("Core", "DOM: Core & HTML") -XPIDL_SOURCES += [ - "nsIScriptLoaderObserver.idl", -] - -XPIDL_MODULE = "dom" - EXPORTS += [ "nsIScriptElement.h", + "nsIScriptLoaderObserver.h", ] EXPORTS.mozilla.dom += [ diff --git a/dom/script/nsIScriptLoaderObserver.h b/dom/script/nsIScriptLoaderObserver.h new file mode 100644 index 0000000000..449812c6e3 --- /dev/null +++ b/dom/script/nsIScriptLoaderObserver.h @@ -0,0 +1,70 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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_SCRIPT_NSISCRIPTLOADEROBSERVER_H_ +#define MOZILLA_DOM_SCRIPT_NSISCRIPTLOADEROBSERVER_H_ + +#include "nsISupports.h" +#include "js/GCAnnotations.h" + +class nsIScriptElement; +class nsIURI; + +#define NS_ISCRIPTLOADEROBSERVER_IID \ + { \ + 0x7b787204, 0x76fb, 0x4764, { \ + 0x96, 0xf1, 0xfb, 0x7a, 0x66, 0x6d, 0xb4, 0xf4 \ + } \ + } + +class NS_NO_VTABLE nsIScriptLoaderObserver : public nsISupports { + public: + NS_DECLARE_STATIC_IID_ACCESSOR(NS_ISCRIPTLOADEROBSERVER_IID) + + /** + * The script is available for evaluation. For inline scripts, this + * method will be called synchronously. For externally loaded scripts, + * this method will be called when the load completes. + * + * @param aResult A result code representing the result of loading + * a script. If this is a failure code, script evaluation + * will not occur. + * @param aElement The element being processed. + * @param aIsInline Is this an inline classic script (as opposed to an + * externally loaded classic script or module script)? + * @param aURI What is the URI of the script (the document URI if + * it is inline). + * @param aLineNo At what line does the script appear (generally 1 + * if it is a loaded script). + */ + JS_HAZ_CAN_RUN_SCRIPT NS_IMETHOD ScriptAvailable(nsresult aResult, + nsIScriptElement* aElement, + bool aIsInlineClassicScript, + nsIURI* aURI, + uint32_t aLineNo) = 0; + + /** + * The script has been evaluated. + * + * @param aResult A result code representing the success or failure of + * the script evaluation. + * @param aElement The element being processed. + * @param aIsInline Is this an inline script or externally loaded? + */ + JS_HAZ_CAN_RUN_SCRIPT MOZ_CAN_RUN_SCRIPT NS_IMETHOD ScriptEvaluated( + nsresult aResult, nsIScriptElement* aElement, bool aIsInline) = 0; +}; + +NS_DEFINE_STATIC_IID_ACCESSOR(nsIScriptLoaderObserver, + NS_ISCRIPTLOADEROBSERVER_IID) + +#define NS_DECL_NSISCRIPTLOADEROBSERVER \ + NS_IMETHOD ScriptAvailable(nsresult aResult, nsIScriptElement* aElement, \ + bool aIsInlineClassicScript, nsIURI* aURI, \ + uint32_t aLineNo) override; \ + MOZ_CAN_RUN_SCRIPT NS_IMETHOD ScriptEvaluated( \ + nsresult aResult, nsIScriptElement* aElement, bool aIsInline) override; + +#endif // MOZILLA_DOM_SCRIPT_NSISCRIPTLOADEROBSERVER_H_ diff --git a/dom/script/nsIScriptLoaderObserver.idl b/dom/script/nsIScriptLoaderObserver.idl deleted file mode 100644 index ceeb79cb39..0000000000 --- a/dom/script/nsIScriptLoaderObserver.idl +++ /dev/null @@ -1,48 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* 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 "nsISupports.idl" - -interface nsIScriptElement; -interface nsIURI; - -[scriptable, uuid(7b787204-76fb-4764-96f1-fb7a666db4f4)] -interface nsIScriptLoaderObserver : nsISupports { - - /** - * The script is available for evaluation. For inline scripts, this - * method will be called synchronously. For externally loaded scripts, - * this method will be called when the load completes. - * - * @param aResult A result code representing the result of loading - * a script. If this is a failure code, script evaluation - * will not occur. - * @param aElement The element being processed. - * @param aIsInline Is this an inline classic script (as opposed to an - * externally loaded classic script or module script)? - * @param aURI What is the URI of the script (the document URI if - * it is inline). - * @param aLineNo At what line does the script appear (generally 1 - * if it is a loaded script). - */ - void scriptAvailable(in nsresult aResult, - in nsIScriptElement aElement, - in boolean aIsInlineClassicScript, - in nsIURI aURI, - in uint32_t aLineNo); - - /** - * The script has been evaluated. - * - * @param aResult A result code representing the success or failure of - * the script evaluation. - * @param aElement The element being processed. - * @param aIsInline Is this an inline script or externally loaded? - */ - [can_run_script] - void scriptEvaluated(in nsresult aResult, - in nsIScriptElement aElement, - in boolean aIsInline); -}; -- cgit v1.2.3