tor-browser

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

commit 30ceed98862fc8df9e45c0b2f1cae76483eff8ff
parent 0264cf850afd91cdd3956066e0b00b982566f684
Author: Tom Schuster <tschuster@mozilla.com>
Date:   Mon,  8 Dec 2025 13:13:19 +0000

Bug 2003789 - Add JS ChromeUtils.fetchDecodedImage helper. r=tnikkel

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

Diffstat:
Mdom/base/ChromeUtils.cpp | 38++++++++++++++++++++++++++++++++++++++
Mdom/base/ChromeUtils.h | 5+++++
Mdom/chrome-webidl/ChromeUtils.webidl | 6+++++-
Mimage/FetchDecodedImage.cpp | 9++++++++-
Mimage/FetchDecodedImage.h | 5+++++
5 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/dom/base/ChromeUtils.cpp b/dom/base/ChromeUtils.cpp @@ -58,6 +58,7 @@ #include "mozilla/dom/WindowGlobalParent.h" #include "mozilla/dom/WorkerScope.h" #include "mozilla/dom/quota/QuotaManager.h" +#include "mozilla/image/FetchDecodedImage.h" #include "mozilla/ipc/GeckoChildProcessHost.h" #include "mozilla/ipc/UtilityProcessHost.h" #include "mozilla/ipc/UtilityProcessManager.h" @@ -2743,6 +2744,43 @@ Nullable<bool> ChromeUtils::GetGlobalWindowCommandEnabled( return handler->IsCommandEnabled(aName, nullptr); } +already_AddRefed<Promise> ChromeUtils::FetchDecodedImage(GlobalObject& aGlobal, + nsIURI* aURI, + nsIChannel* aChannel, + ErrorResult& aRv) { + nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports()); + MOZ_ASSERT(global); + RefPtr<Promise> domPromise = Promise::Create(global, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } + + image::FetchDecodedImage(aURI, aChannel, gfx::IntSize{}) + ->Then( + GetCurrentSerialEventTarget(), __func__, + [global, domPromise](already_AddRefed<imgIContainer> aImage) { + nsCOMPtr<imgIContainer> image(std::move(aImage)); + + AutoJSAPI jsapi; + if (!jsapi.Init(global)) { + domPromise->MaybeRejectWithUndefined(); + return; + } + + JS::Rooted<JS::Value> value(jsapi.cx()); + if (!WrapObject(jsapi.cx(), image, &NS_GET_IID(imgIContainer), + &value)) { + domPromise->MaybeRejectWithUndefined(); + return; + } + + domPromise->MaybeResolve(value); + }, + [domPromise](nsresult aStatus) { domPromise->MaybeReject(aStatus); }); + + return domPromise.forget(); +} + void ChromeUtils::EncodeURIForSrcset(GlobalObject&, const nsACString& aIn, nsACString& aOut) { const auto inputLen = aIn.Length(); diff --git a/dom/base/ChromeUtils.h b/dom/base/ChromeUtils.h @@ -366,6 +366,11 @@ class ChromeUtils { nsIPrincipal* aLoadingPrincipal, ErrorResult& aRv); static bool IsJSIdentifier(GlobalObject& aGlobal, const nsAString& aStr); + + static already_AddRefed<Promise> FetchDecodedImage(GlobalObject& aGlobal, + nsIURI* aURI, + nsIChannel* aChannel, + ErrorResult& aRv); }; } // namespace dom diff --git a/dom/chrome-webidl/ChromeUtils.webidl b/dom/chrome-webidl/ChromeUtils.webidl @@ -4,11 +4,12 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ +interface imgIContainer; interface nsIContentParentKeepAlive; interface nsIDOMProcessChild; interface nsIDOMProcessParent; -interface Principal; interface nsIRFPTargetSetIDL; +interface Principal; /** * An optimized QueryInterface method, generated by generateQI. @@ -860,6 +861,9 @@ partial namespace ChromeUtils { // * true if all windows handle it _and_ it's unconditionally enabled. // * false if all windows handle and it is not unconditionally enabled. boolean? getGlobalWindowCommandEnabled(UTF8String name); + + [Throws] + Promise<imgIContainer> fetchDecodedImage(URI uri, MozChannel channel); }; /* diff --git a/image/FetchDecodedImage.cpp b/image/FetchDecodedImage.cpp @@ -124,6 +124,13 @@ RefPtr<FetchDecodedImagePromise> FetchDecodedImage( return FetchDecodedImagePromise::CreateAndReject(rv, __func__); } + return FetchDecodedImage(aURI, channel, aSize); +} + +RefPtr<FetchDecodedImagePromise> FetchDecodedImage(nsIURI* aURI, + nsIChannel* aChannel, + gfx::IntSize aSize) { + nsresult rv; nsCOMPtr<imgITools> imgTools = do_GetService("@mozilla.org/image/tools;1", &rv); if (NS_FAILED(rv)) { @@ -135,7 +142,7 @@ RefPtr<FetchDecodedImagePromise> FetchDecodedImage( RefPtr<FetchDecodedImageHelper> helper = new FetchDecodedImageHelper(aSize, promise); - rv = imgTools->DecodeImageFromChannelAsync(aURI, channel, helper, helper); + rv = imgTools->DecodeImageFromChannelAsync(aURI, aChannel, helper, helper); if (NS_FAILED(rv)) { helper->OnError(rv); } diff --git a/image/FetchDecodedImage.h b/image/FetchDecodedImage.h @@ -13,6 +13,7 @@ #include "nsIContentPolicy.h" class imgIContainer; +class nsIChannel; namespace mozilla::image { @@ -35,6 +36,10 @@ RefPtr<FetchDecodedImagePromise> FetchDecodedImage( nsContentPolicyType aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE); +RefPtr<FetchDecodedImagePromise> FetchDecodedImage(nsIURI* aURI, + nsIChannel* aChannel, + gfx::IntSize aSize); + } // namespace mozilla::image #endif // mozilla_image_FetchDecodedImage_h