tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit d9af9be984bddd7956a6be5f5256869ca4375efe
parent c06884446e582d0a125cf5635bf03412c59060b9
Author: Daniel Minor <dminor@mozilla.com>
Date:   Fri, 12 Dec 2025 14:12:37 +0000

Bug 1994828 - Set wasm mime type essence for worklets; r=allstarschh

Differential Revision: https://phabricator.services.mozilla.com/D275509

Diffstat:
Mdom/worklet/WorkletFetchHandler.cpp | 53++++++++++++++++++++++++++++++++++++++++++++++++-----
Mdom/worklet/WorkletFetchHandler.h | 3+++
2 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/dom/worklet/WorkletFetchHandler.cpp b/dom/worklet/WorkletFetchHandler.cpp @@ -9,6 +9,7 @@ #include "js/loader/ModuleLoadRequest.h" #include "mozilla/CycleCollectedJSContext.h" #include "mozilla/ScopeExit.h" +#include "mozilla/StaticPrefs_javascript.h" #include "mozilla/TaskQueue.h" #include "mozilla/dom/Document.h" #include "mozilla/dom/Fetch.h" @@ -161,12 +162,18 @@ class FetchCompleteRunnable final : public Runnable { public: FetchCompleteRunnable(WorkletImpl* aWorkletImpl, nsIURI* aURI, nsresult aResult, +#ifdef NIGHTLY_BUILD + bool aHasWasmMimeTypeEssence, +#endif UniquePtr<uint8_t[]> aScriptBuffer = nullptr, size_t aScriptLength = 0) : Runnable("Worklet::FetchCompleteRunnable"), mWorkletImpl(aWorkletImpl), mURI(aURI), mResult(aResult), +#ifdef NIGHTLY_BUILD + mHasWasmMimeTypeEssence(aHasWasmMimeTypeEssence), +#endif mScriptBuffer(std::move(aScriptBuffer)), mScriptLength(aScriptLength) { MOZ_ASSERT(NS_IsMainThread()); @@ -182,6 +189,9 @@ class FetchCompleteRunnable final : public Runnable { RefPtr<WorkletImpl> mWorkletImpl; nsCOMPtr<nsIURI> mURI; nsresult mResult; +#ifdef NIGHTLY_BUILD + bool mHasWasmMimeTypeEssence; +#endif UniquePtr<uint8_t[]> mScriptBuffer; size_t mScriptLength; }; @@ -217,6 +227,15 @@ NS_IMETHODIMP FetchCompleteRunnable::RunOnWorkletThread() { NS_ENSURE_SUCCESS(rv, rv); } +#ifdef NIGHTLY_BUILD + // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-single-module-script + // Extract the content-type. If its essence is wasm, we'll attempt to + // compile this module as a wasm module. (Steps 13.2, 13.6) + if (mHasWasmMimeTypeEssence) { + request->SetHasWasmMimeTypeEssence(); + } +#endif + request->SetBaseURL(mURI); request->OnFetchComplete(mResult); moduleLoader->RemoveRequest(mURI); @@ -499,8 +518,12 @@ nsresult WorkletFetchHandler::StartFetch(JSContext* aCx, nsIURI* aURI, } void WorkletFetchHandler::HandleFetchFailed(nsIURI* aURI) { - nsCOMPtr<nsIRunnable> runnable = new FetchCompleteRunnable( - mWorklet->mImpl, aURI, NS_ERROR_FAILURE, nullptr, 0); + nsCOMPtr<nsIRunnable> runnable = + new FetchCompleteRunnable(mWorklet->mImpl, aURI, NS_ERROR_FAILURE, +#ifdef NIGHTLY_BUILD + false, +#endif + nullptr, 0); if (NS_WARN_IF( NS_FAILED(mWorklet->mImpl->SendControlMessage(runnable.forget())))) { @@ -541,6 +564,18 @@ void WorkletScriptHandler::ResolvedCallback(JSContext* aCx, return; } +#ifdef NIGHTLY_BUILD + nsAutoCString contentType; + ErrorResult result; + if (response->GetInternalHeaders()) { + response->GetInternalHeaders()->Get("Content-Type"_ns, contentType, result); + if (!result.Failed()) { + mHasWasmMimeTypeEssence = nsContentUtils::HasWasmMimeTypeEssence( + NS_ConvertUTF8toUTF16(contentType)); + } + } +#endif + nsCOMPtr<nsIInputStream> inputStream; response->GetBody(getter_AddRefs(inputStream)); if (!inputStream) { @@ -598,8 +633,12 @@ NS_IMETHODIMP WorkletScriptHandler::OnStreamComplete(nsIStreamLoader* aLoader, UniquePtr<uint8_t[]> scriptTextBuf = MakeUnique<uint8_t[]>(aStringLen); memcpy(scriptTextBuf.get(), aString, aStringLen); - nsCOMPtr<nsIRunnable> runnable = new FetchCompleteRunnable( - mWorklet->mImpl, mURI, NS_OK, std::move(scriptTextBuf), aStringLen); + nsCOMPtr<nsIRunnable> runnable = + new FetchCompleteRunnable(mWorklet->mImpl, mURI, NS_OK, +#ifdef NIGHTLY_BUILD + mHasWasmMimeTypeEssence, +#endif + std::move(scriptTextBuf), aStringLen); if (NS_FAILED(mWorklet->mImpl->SendControlMessage(runnable.forget()))) { HandleFailure(NS_ERROR_FAILURE); @@ -625,7 +664,11 @@ void WorkletScriptHandler::HandleFailure(nsresult aResult) { void WorkletScriptHandler::DispatchFetchCompleteToWorklet(nsresult aRv) { nsCOMPtr<nsIRunnable> runnable = - new FetchCompleteRunnable(mWorklet->mImpl, mURI, aRv, nullptr, 0); + new FetchCompleteRunnable(mWorklet->mImpl, mURI, aRv, +#ifdef NIGHTLY_BUILD + mHasWasmMimeTypeEssence, +#endif + nullptr, 0); if (NS_WARN_IF( NS_FAILED(mWorklet->mImpl->SendControlMessage(runnable.forget())))) { diff --git a/dom/worklet/WorkletFetchHandler.h b/dom/worklet/WorkletFetchHandler.h @@ -114,6 +114,9 @@ class WorkletScriptHandler final : public PromiseNativeHandler, RefPtr<Worklet> mWorklet; nsCOMPtr<nsIURI> mURI; +#ifdef NIGHTLY_BUILD + bool mHasWasmMimeTypeEssence = false; +#endif }; } // namespace mozilla::dom