diff --git a/widget/IconLoader.cpp b/widget/IconLoader.cpp new file mode 100644 index 000000000000..0960b108c95b --- /dev/null +++ b/widget/IconLoader.cpp @@ -0,0 +1,170 @@ +/* -*- 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 "mozilla/widget/IconLoader.h" +#include "gfxPlatform.h" +#include "imgIContainer.h" +#include "imgLoader.h" +#include "mozilla/dom/Document.h" +#include "nsContentUtils.h" +#include "nsIContent.h" + +using namespace mozilla; + +using mozilla::gfx::SourceSurface; + +namespace mozilla::widget { + +NS_IMPL_ISUPPORTS(mozilla::widget::IconLoader, imgINotificationObserver) + +IconLoader::IconLoader(Helper* aHelper, nsINode* aContent, + const nsIntRect& aImageRegionRect) + : mContent(aContent), + mContentType(nsIContentPolicy::TYPE_INTERNAL_IMAGE), + mImageRegionRect(aImageRegionRect), + mLoadedIcon(false), + mHelper(aHelper) {} + +IconLoader::~IconLoader() { + if (mIconRequest) { + mIconRequest->CancelAndForgetObserver(NS_BINDING_ABORTED); + mIconRequest = nullptr; + } +} + +nsresult IconLoader::LoadIcon(nsIURI* aIconURI, bool aIsInternalIcon) { + if (mIconRequest) { + // Another icon request is already in flight. Kill it. + mIconRequest->Cancel(NS_BINDING_ABORTED); + mIconRequest = nullptr; + } + + mLoadedIcon = false; + + if (!mContent) { + return NS_ERROR_FAILURE; + } + + RefPtr document = mContent->OwnerDoc(); + + nsCOMPtr loadGroup = document->GetDocumentLoadGroup(); + if (!loadGroup) { + return NS_ERROR_FAILURE; + } + + RefPtr loader = nsContentUtils::GetImgLoaderForDocument(document); + if (!loader) { + return NS_ERROR_FAILURE; + } + + nsresult rv; + if (aIsInternalIcon) { + rv = loader->LoadImage( + aIconURI, nullptr, nullptr, nullptr, 0, loadGroup, this, nullptr, + nullptr, nsIRequest::LOAD_NORMAL, nullptr, mContentType, u""_ns, + /* aUseUrgentStartForChannel */ false, /* aLinkPreload */ false, + getter_AddRefs(mIconRequest)); + } else { + rv = loader->LoadImage( + aIconURI, nullptr, nullptr, mContent->NodePrincipal(), 0, loadGroup, + this, mContent, document, nsIRequest::LOAD_NORMAL, nullptr, + mContentType, u""_ns, /* aUseUrgentStartForChannel */ false, + /* aLinkPreload */ false, getter_AddRefs(mIconRequest)); + } + if (NS_FAILED(rv)) { + return rv; + } + + return NS_OK; +} + +// +// imgINotificationObserver +// + +void IconLoader::Notify(imgIRequest* aRequest, int32_t aType, + const nsIntRect* aData) { + if (aType == imgINotificationObserver::LOAD_COMPLETE) { + // Make sure the image loaded successfully. + uint32_t status = imgIRequest::STATUS_ERROR; + if (NS_FAILED(aRequest->GetImageStatus(&status)) || + (status & imgIRequest::STATUS_ERROR)) { + mIconRequest->Cancel(NS_BINDING_ABORTED); + mIconRequest = nullptr; + return; + } + + nsCOMPtr image; + aRequest->GetImage(getter_AddRefs(image)); + MOZ_ASSERT(image); + + // Ask the image to decode at its intrinsic size. + int32_t width = 0, height = 0; + image->GetWidth(&width); + image->GetHeight(&height); + image->RequestDecodeForSize(nsIntSize(width, height), + imgIContainer::FLAG_HIGH_QUALITY_SCALING); + } + + if (aType == imgINotificationObserver::FRAME_COMPLETE) { + nsresult rv = OnFrameComplete(aRequest); + + if (NS_FAILED(rv)) { + return; + } + + nsCOMPtr image; + aRequest->GetImage(getter_AddRefs(image)); + MOZ_ASSERT(image); + + mHelper->OnComplete(image, mImageRegionRect); + return; + } + + if (aType == imgINotificationObserver::DECODE_COMPLETE) { + if (mIconRequest && mIconRequest == aRequest) { + mIconRequest->Cancel(NS_BINDING_ABORTED); + mIconRequest = nullptr; + } + } +} + +nsresult IconLoader::OnFrameComplete(imgIRequest* aRequest) { + if (aRequest != mIconRequest) { + return NS_ERROR_FAILURE; + } + + // Only support one frame. + if (mLoadedIcon) { + return NS_OK; + } + + nsCOMPtr imageContainer; + aRequest->GetImage(getter_AddRefs(imageContainer)); + if (!imageContainer) { + return NS_ERROR_FAILURE; + } + + int32_t origWidth = 0, origHeight = 0; + imageContainer->GetWidth(&origWidth); + imageContainer->GetHeight(&origHeight); + + // If the image region is invalid, don't draw the image to almost match + // the behavior of other platforms. + if (!mImageRegionRect.IsEmpty() && (mImageRegionRect.XMost() > origWidth || + mImageRegionRect.YMost() > origHeight)) { + return NS_ERROR_FAILURE; + } + + if (mImageRegionRect.IsEmpty()) { + mImageRegionRect.SetRect(0, 0, origWidth, origHeight); + } + + mLoadedIcon = true; + + return NS_OK; +} + +} // namespace mozilla::widget \ No newline at end of file diff --git a/widget/IconLoader.h b/widget/IconLoader.h new file mode 100644 index 000000000000..0f1846d9c3dd --- /dev/null +++ b/widget/IconLoader.h @@ -0,0 +1,77 @@ +/* -*- 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_widget_IconLoader_h_ +#define mozilla_widget_IconLoader_h_ + +#include "imgINotificationObserver.h" +#include "mozilla/RefPtr.h" +#include "nsCOMPtr.h" +#include "nsIContentPolicy.h" + +class nsIURI; +class nsINode; +class imgRequestProxy; +class imgIContainer; + +namespace mozilla::widget { + +/** + * IconLoader is a utility for loading icons from either the disk or over + * the network, and then using them in native OS widgetry like the macOS + * global menu bar or the Windows notification area. + */ + +class IconLoader : public imgINotificationObserver { + public: + /** + * IconLoader itself is cross-platform, but each platform needs to supply + * it with a Helper that does the platform-specific conversion work. The + * Helper needs to implement the OnComplete method in order to handle the + * imgIContainer of the loaded icon. + */ + class Helper { + public: + NS_INLINE_DECL_REFCOUNTING(mozilla::widget::IconLoader::Helper) + virtual nsresult OnComplete(imgIContainer* aContainer, + const nsIntRect& aRect) = 0; + + protected: + virtual ~Helper() = default; + }; + + IconLoader(Helper* aHelper, nsINode* aContent, + const nsIntRect& aImageRegionRect); + + public: + NS_DECL_ISUPPORTS + NS_DECL_IMGINOTIFICATIONOBSERVER + + // LoadIcon will start a load request for the icon. + // The request may not complete until after LoadIcon returns. + // If aIsInternalIcon is true, the document and principal will not be + // used when loading. + nsresult LoadIcon(nsIURI* aIconURI, bool aIsInternalIcon = false); + + void ReleaseJSObjects() { mContent = nullptr; } + + void Destroy(); + + protected: + virtual ~IconLoader(); + + private: + nsresult OnFrameComplete(imgIRequest* aRequest); + + nsCOMPtr mContent; + nsContentPolicyType mContentType; + RefPtr mIconRequest; + nsIntRect mImageRegionRect; + bool mLoadedIcon; + RefPtr mHelper; +}; + +} // namespace mozilla::widget +#endif // mozilla_widget_IconLoader_h_ diff --git a/widget/cocoa/IconLoaderHelperCocoa.h b/widget/cocoa/IconLoaderHelperCocoa.h new file mode 100644 index 000000000000..5fbf9e152114 --- /dev/null +++ b/widget/cocoa/IconLoaderHelperCocoa.h @@ -0,0 +1,68 @@ +/* -*- 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_widget_IconLoaderHelperCocoa_h +#define mozilla_widget_IconLoaderHelperCocoa_h + +#include "mozilla/widget/IconLoader.h" + +namespace mozilla::widget { + +/** + * Classes that want to hear about when icons load should subclass + * IconLoaderListenerCocoa, and implement the OnComplete() method, + * which will be called once the load of the icon has completed. + */ +class IconLoaderListenerCocoa { + public: + IconLoaderListenerCocoa() = default; + + NS_INLINE_DECL_REFCOUNTING(mozilla::widget::IconLoaderListenerCocoa) + + virtual nsresult OnComplete() = 0; + + protected: + virtual ~IconLoaderListenerCocoa() = default; +}; + +/** + * This is a Helper used with mozilla::widget::IconLoader that implements the + * macOS-specific functionality for converting a loaded icon into an NSImage*. + */ +class IconLoaderHelperCocoa final : public mozilla::widget::IconLoader::Helper { + public: + IconLoaderHelperCocoa(mozilla::widget::IconLoaderListenerCocoa* aLoadListener, + uint32_t aIconHeight, uint32_t aIconWidth, CGFloat aScaleFactor = 0.0f); + + nsresult OnComplete(imgIContainer* aImage, const nsIntRect& aRect) override; + + /** + * IconLoaderHelperCocoa will default the NSImage* returned by + * GetNativeIconImage to an empty icon. Once the load of the icon + * by IconLoader has completed, GetNativeIconImage will return the + * loaded icon. + * + * Note that IconLoaderHelperCocoa owns this NSImage. If you don't + * need it to hold onto the NSImage anymore, call Destroy on it to + * deallocate. The IconLoaderHelperCocoa destructor will also deallocate + * the NSImage if necessary. + */ + NSImage* GetNativeIconImage(); // Owned by IconLoaderHelperCocoa + void Destroy(); + + protected: + ~IconLoaderHelperCocoa(); + + private: + RefPtr mLoadListener; + uint32_t mIconHeight; + uint32_t mIconWidth; + CGFloat mScaleFactor; + NSImage* mNativeIconImage; +}; + +} // namespace mozilla::widget + +#endif // mozilla_widget_IconLoaderHelperCocoa_h diff --git a/widget/cocoa/IconLoaderHelperCocoa.mm b/widget/cocoa/IconLoaderHelperCocoa.mm new file mode 100644 index 000000000000..799d343917c0 --- /dev/null +++ b/widget/cocoa/IconLoaderHelperCocoa.mm @@ -0,0 +1,139 @@ +/* -*- 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/. */ + +/* + * Retrieves and displays icons in native menu items on Mac OS X. + */ + +/* exception_defines.h defines 'try' to 'if (true)' which breaks objective-c + exceptions and produces errors like: error: unexpected '@' in program'. + If we define __EXCEPTIONS exception_defines.h will avoid doing this. + + See bug 666609 for more information. + + We use to get the libstdc++ version. */ +#include +#if __GLIBCXX__ <= 20070719 +# ifndef __EXCEPTIONS +# define __EXCEPTIONS +# endif +#endif + +#include "gfxPlatform.h" +#include "imgIContainer.h" +#include "imgLoader.h" +#include "imgRequestProxy.h" +#include "mozilla/dom/Document.h" +#include "nsCocoaUtils.h" +#include "nsContentUtils.h" +#include "nsIContent.h" +#include "nsNameSpaceManager.h" +#include "nsNetUtil.h" +#include "nsObjCExceptions.h" +#include "nsThreadUtils.h" +#include "nsToolkit.h" +#include "IconLoaderHelperCocoa.h" + +using namespace mozilla; + +using mozilla::gfx::SourceSurface; +using mozilla::widget::IconLoaderListenerCocoa; + +namespace mozilla::widget { + +IconLoaderHelperCocoa::IconLoaderHelperCocoa(IconLoaderListenerCocoa* aListener, + uint32_t aIconHeight, uint32_t aIconWidth, + CGFloat aScaleFactor) + : mLoadListener(aListener), + mIconHeight(aIconHeight), + mIconWidth(aIconWidth), + mScaleFactor(aScaleFactor) { + // Placeholder icon, which will later be replaced. + mNativeIconImage = [[NSImage alloc] initWithSize:NSMakeSize(mIconHeight, mIconWidth)]; + MOZ_ASSERT(aListener); +} + +IconLoaderHelperCocoa::~IconLoaderHelperCocoa() { Destroy(); } + +nsresult IconLoaderHelperCocoa::OnComplete(imgIContainer* aImage, const nsIntRect& aRect) { + NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT + + NS_ENSURE_ARG_POINTER(aImage); + + bool isEntirelyBlack = false; + NSImage* newImage = nil; + nsresult rv; + if (mScaleFactor != 0.0f) { + rv = nsCocoaUtils::CreateNSImageFromImageContainer(aImage, imgIContainer::FRAME_CURRENT, + &newImage, mScaleFactor, &isEntirelyBlack); + } else { + rv = nsCocoaUtils::CreateDualRepresentationNSImageFromImageContainer( + aImage, imgIContainer::FRAME_CURRENT, &newImage, &isEntirelyBlack); + } + + if (NS_FAILED(rv) || !newImage) { + mNativeIconImage = nil; + [newImage release]; + return NS_ERROR_FAILURE; + } + + NSSize requestedSize = NSMakeSize(mIconWidth, mIconHeight); + + int32_t origWidth = 0, origHeight = 0; + aImage->GetWidth(&origWidth); + aImage->GetHeight(&origHeight); + + bool createSubImage = + !(aRect.x == 0 && aRect.y == 0 && aRect.width == origWidth && aRect.height == origHeight); + + if (createSubImage) { + // If aRect is set using CSS, we need to slice a piece out of the + // overall image to use as the icon. + NSImage* subImage = + [NSImage imageWithSize:requestedSize + flipped:NO + drawingHandler:^BOOL(NSRect subImageRect) { + [newImage drawInRect:NSMakeRect(0, 0, mIconWidth, mIconHeight) + fromRect:NSMakeRect(aRect.x, aRect.y, aRect.width, aRect.height) + operation:NSCompositeCopy + fraction:1.0f]; + return YES; + }]; + [newImage release]; + newImage = [subImage retain]; + subImage = nil; + } + + // If all the color channels in the image are black, treat the image as a + // template. This will cause macOS to use the image's alpha channel as a mask + // and it will fill it with a color that looks good in the context that it's + // used in. For example, for regular menu items, the image will be black, but + // when the menu item is hovered (and its background is blue), it will be + // filled with white. + [newImage setTemplate:isEntirelyBlack]; + + [newImage setSize:requestedSize]; + + NSImage* placeholderImage = mNativeIconImage; + mNativeIconImage = newImage; + [placeholderImage release]; + placeholderImage = nil; + + mLoadListener->OnComplete(); + + return NS_OK; + NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT +} + +NSImage* IconLoaderHelperCocoa::GetNativeIconImage() { return mNativeIconImage; } + +void IconLoaderHelperCocoa::Destroy() { + if (mNativeIconImage) { + [mNativeIconImage release]; + mNativeIconImage = nil; + } +} + +} // namespace mozilla::widget diff --git a/widget/cocoa/moz.build b/widget/cocoa/moz.build index fb207d539a96..8d1cda8b7a20 100644 --- a/widget/cocoa/moz.build +++ b/widget/cocoa/moz.build @@ -31,6 +31,7 @@ EXPORTS += [ UNIFIED_SOURCES += [ 'ComplexTextInputPanel.mm', 'GfxInfo.mm', + 'IconLoaderHelperCocoa.mm', 'NativeKeyBindings.mm', 'nsAppShell.mm', 'nsBidiKeyboard.mm', @@ -41,7 +42,6 @@ UNIFIED_SOURCES += [ 'nsCursorManager.mm', 'nsDeviceContextSpecX.mm', 'nsFilePicker.mm', - 'nsIconLoaderService.mm', 'nsLookAndFeel.mm', 'nsMacCursor.mm', 'nsMacDockSupport.mm', diff --git a/widget/cocoa/nsIconLoaderObserver.h b/widget/cocoa/nsIconLoaderObserver.h deleted file mode 100644 index 19e0d433de36..000000000000 --- a/widget/cocoa/nsIconLoaderObserver.h +++ /dev/null @@ -1,28 +0,0 @@ -/* -*- Mode: IDL; 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 nsIconLoaderObserver_h_ -#define nsIconLoaderObserver_h_ - -#import - -#include "nsISupportsImpl.h" - -class nsIconLoaderObserver { - public: - NS_INLINE_DECL_REFCOUNTING(nsIconLoaderObserver) - - /* - * Called when the nsIconLoaderService finishes loading the icon. To be used - * as a completion handler for nsIconLoaderService. - */ - virtual nsresult OnComplete(NSImage* aImage) = 0; - - protected: - virtual ~nsIconLoaderObserver() {} -}; - -#endif // nsIconLoaderObserver_h_ diff --git a/widget/cocoa/nsIconLoaderService.h b/widget/cocoa/nsIconLoaderService.h deleted file mode 100644 index 0b8523f4e817..000000000000 --- a/widget/cocoa/nsIconLoaderService.h +++ /dev/null @@ -1,65 +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/. */ - -/* - * Retrieves and displays icons in native menu items on Mac OS X. - */ - -#ifndef nsIconLoaderService_h_ -#define nsIconLoaderService_h_ - -#include "imgINotificationObserver.h" -#include "mozilla/RefPtr.h" -#include "nsCOMPtr.h" -#include "nsIconLoaderObserver.h" -#include "nsIContentPolicy.h" - -class nsIURI; -class nsINode; -class nsIPrincipal; -class imgRequestProxy; - -class nsIconLoaderService : public imgINotificationObserver { - public: - // If aScaleFactor is not specified, then an image with both regular and - // HiDPI representations will be loaded. - nsIconLoaderService(nsINode* aContent, nsIntRect* aImageRegionRect, - RefPtr aObserver, uint32_t aIconHeight, - uint32_t aIconWidth, CGFloat aScaleFactor = 0.0f); - - public: - NS_DECL_ISUPPORTS - NS_DECL_IMGINOTIFICATIONOBSERVER - - // LoadIcon will start a load request for the icon. - // The request may not complete until after LoadIcon returns. - // If aIsInternalIcon is true, the document and principal will not be - // used when loading. - nsresult LoadIcon(nsIURI* aIconURI, bool aIsInternalIcon); - - NSImage* GetNativeIconImage(); - - void ReleaseJSObjects() { mContent = nil; } - - void Destroy(); - - protected: - virtual ~nsIconLoaderService(); - - private: - nsresult OnFrameComplete(imgIRequest* aRequest); - - nsCOMPtr mContent; - nsContentPolicyType mContentType; - RefPtr mIconRequest; - nsIntRect* mImageRegionRect; - bool mLoadedIcon; - NSImage* mNativeIconImage; - uint32_t mIconHeight; - uint32_t mIconWidth; - CGFloat mScaleFactor; - RefPtr mCompletionHandler; -}; -#endif // nsIconLoaderService_h_ diff --git a/widget/cocoa/nsIconLoaderService.mm b/widget/cocoa/nsIconLoaderService.mm deleted file mode 100644 index 9952fa215ac4..000000000000 --- a/widget/cocoa/nsIconLoaderService.mm +++ /dev/null @@ -1,267 +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/. */ - -/* - * Retrieves and displays icons in native menu items on Mac OS X. - */ - -/* exception_defines.h defines 'try' to 'if (true)' which breaks objective-c - exceptions and produces errors like: error: unexpected '@' in program'. - If we define __EXCEPTIONS exception_defines.h will avoid doing this. - - See bug 666609 for more information. - - We use to get the libstdc++ version. */ -#include -#if __GLIBCXX__ <= 20070719 -# ifndef __EXCEPTIONS -# define __EXCEPTIONS -# endif -#endif - -#include "gfxPlatform.h" -#include "imgIContainer.h" -#include "imgLoader.h" -#include "imgRequestProxy.h" -#include "mozilla/dom/Document.h" -#include "nsCocoaUtils.h" -#include "nsContentUtils.h" -#include "nsIconLoaderService.h" -#include "nsIContent.h" -#include "nsNameSpaceManager.h" -#include "nsNetUtil.h" -#include "nsObjCExceptions.h" -#include "nsThreadUtils.h" -#include "nsToolkit.h" - -using namespace mozilla; - -using mozilla::gfx::SourceSurface; - -NS_IMPL_ISUPPORTS(nsIconLoaderService, imgINotificationObserver) - -nsIconLoaderService::nsIconLoaderService(nsINode* aContent, nsIntRect* aImageRegionRect, - RefPtr aObserver, - uint32_t aIconHeight, uint32_t aIconWidth, - CGFloat aScaleFactor) - : mContent(aContent), - mContentType(nsIContentPolicy::TYPE_INTERNAL_IMAGE), - mImageRegionRect(aImageRegionRect), - mLoadedIcon(false), - mIconHeight(aIconHeight), - mIconWidth(aIconWidth), - mScaleFactor(aScaleFactor), - mCompletionHandler(aObserver) { - // Placeholder icon, which will later be replaced. - mNativeIconImage = [[NSImage alloc] initWithSize:NSMakeSize(mIconHeight, mIconWidth)]; -} - -nsIconLoaderService::~nsIconLoaderService() { Destroy(); } - -void nsIconLoaderService::Destroy() { - if (mIconRequest) { - mIconRequest->CancelAndForgetObserver(NS_BINDING_ABORTED); - mIconRequest = nullptr; - } - mNativeIconImage = nil; - mImageRegionRect = nullptr; - mCompletionHandler = nil; -} - -nsresult nsIconLoaderService::LoadIcon(nsIURI* aIconURI, bool aIsInternalIcon = false) { - NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; - - if (mIconRequest) { - // Another icon request is already in flight. Kill it. - mIconRequest->Cancel(NS_BINDING_ABORTED); - mIconRequest = nullptr; - } - - mLoadedIcon = false; - - if (!mContent) { - return NS_ERROR_FAILURE; - } - - RefPtr document = mContent->OwnerDoc(); - - nsCOMPtr loadGroup = document->GetDocumentLoadGroup(); - if (!loadGroup) { - return NS_ERROR_FAILURE; - } - - RefPtr loader = nsContentUtils::GetImgLoaderForDocument(document); - if (!loader) { - return NS_ERROR_FAILURE; - } - - nsresult rv; - if (aIsInternalIcon) { - rv = loader->LoadImage(aIconURI, nullptr, nullptr, nullptr, 0, loadGroup, this, nullptr, - nullptr, nsIRequest::LOAD_NORMAL, nullptr, mContentType, u""_ns, - /* aUseUrgentStartForChannel */ false, /* aLinkPreload */ false, - getter_AddRefs(mIconRequest)); - } else { - rv = loader->LoadImage(aIconURI, nullptr, nullptr, mContent->NodePrincipal(), 0, loadGroup, - this, mContent, document, nsIRequest::LOAD_NORMAL, nullptr, mContentType, - u""_ns, /* aUseUrgentStartForChannel */ false, - /* aLinkPreload */ false, getter_AddRefs(mIconRequest)); - } - if (NS_FAILED(rv)) { - return rv; - } - - return NS_OK; - - NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; -} - -NSImage* nsIconLoaderService::GetNativeIconImage() { return mNativeIconImage; } - -// -// imgINotificationObserver -// - -void nsIconLoaderService::Notify(imgIRequest* aRequest, int32_t aType, const nsIntRect* aData) { - if (aType == imgINotificationObserver::LOAD_COMPLETE) { - // Make sure the image loaded successfully. - uint32_t status = imgIRequest::STATUS_ERROR; - if (NS_FAILED(aRequest->GetImageStatus(&status)) || (status & imgIRequest::STATUS_ERROR)) { - mIconRequest->Cancel(NS_BINDING_ABORTED); - mIconRequest = nullptr; - return; - } - - nsCOMPtr image; - aRequest->GetImage(getter_AddRefs(image)); - MOZ_ASSERT(image); - - // Ask the image to decode at its intrinsic size. - int32_t width = 0, height = 0; - image->GetWidth(&width); - image->GetHeight(&height); - image->RequestDecodeForSize(nsIntSize(width, height), imgIContainer::FLAG_HIGH_QUALITY_SCALING); - } - - if (aType == imgINotificationObserver::FRAME_COMPLETE) { - nsresult rv = OnFrameComplete(aRequest); - - if (NS_FAILED(rv)) { - return; - } - - NSImage* newImage = mNativeIconImage; - mNativeIconImage = nil; - mCompletionHandler->OnComplete(newImage); - - return; - } - - if (aType == imgINotificationObserver::DECODE_COMPLETE) { - if (mIconRequest && mIconRequest == aRequest) { - mIconRequest->Cancel(NS_BINDING_ABORTED); - mIconRequest = nullptr; - } - } -} - -nsresult nsIconLoaderService::OnFrameComplete(imgIRequest* aRequest) { - NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; - - if (aRequest != mIconRequest) { - return NS_ERROR_FAILURE; - } - - // Only support one frame. - if (mLoadedIcon) { - return NS_OK; - } - - nsCOMPtr imageContainer; - aRequest->GetImage(getter_AddRefs(imageContainer)); - if (!imageContainer) { - mNativeIconImage = nil; - return NS_ERROR_FAILURE; - } - - int32_t origWidth = 0, origHeight = 0; - imageContainer->GetWidth(&origWidth); - imageContainer->GetHeight(&origHeight); - - // If the image region is invalid, don't draw the image to almost match - // the behavior of other platforms. - if (!mImageRegionRect->IsEmpty() && - (mImageRegionRect->XMost() > origWidth || mImageRegionRect->YMost() > origHeight)) { - mNativeIconImage = nil; - return NS_ERROR_FAILURE; - } - - if (mImageRegionRect->IsEmpty()) { - mImageRegionRect->SetRect(0, 0, origWidth, origHeight); - } - - bool isEntirelyBlack = false; - NSImage* newImage = nil; - nsresult rv; - if (mScaleFactor != 0.0f) { - rv = nsCocoaUtils::CreateNSImageFromImageContainer(imageContainer, imgIContainer::FRAME_CURRENT, - &newImage, mScaleFactor, &isEntirelyBlack); - } else { - rv = nsCocoaUtils::CreateDualRepresentationNSImageFromImageContainer( - imageContainer, imgIContainer::FRAME_CURRENT, &newImage, &isEntirelyBlack); - } - - if (NS_FAILED(rv) || !newImage) { - mNativeIconImage = nil; - [newImage release]; - return NS_ERROR_FAILURE; - } - - NSSize origSize = NSMakeSize(mIconWidth, mIconHeight); - - bool createSubImage = - !(mImageRegionRect->x == 0 && mImageRegionRect->y == 0 && - mImageRegionRect->width == origWidth && mImageRegionRect->height == origHeight); - - if (createSubImage) { - // If mImageRegionRect is set using CSS, we need to slice a piece out of the overall - // image to use as the icon. - NSImage* subImage = - [NSImage imageWithSize:origSize - flipped:NO - drawingHandler:^BOOL(NSRect subImageRect) { - [newImage drawInRect:NSMakeRect(0, 0, mIconWidth, mIconHeight) - fromRect:NSMakeRect(mImageRegionRect->x, mImageRegionRect->y, - mImageRegionRect->width, mImageRegionRect->height) - operation:NSCompositeCopy - fraction:1.0f]; - return YES; - }]; - [newImage release]; - newImage = [subImage retain]; - subImage = nil; - } - - // If all the color channels in the image are black, treat the image as a - // template. This will cause macOS to use the image's alpha channel as a mask - // and it will fill it with a color that looks good in the context that it's - // used in. For example, for regular menu items, the image will be black, but - // when the menu item is hovered (and its background is blue), it will be - // filled with white. - [newImage setTemplate:isEntirelyBlack]; - - [newImage setSize:origSize]; - - NSImage* placeholderImage = mNativeIconImage; - mNativeIconImage = newImage; - [placeholderImage release]; - placeholderImage = nil; - - mLoadedIcon = true; - - return NS_OK; - - NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; -} diff --git a/widget/cocoa/nsMenuItemIconX.h b/widget/cocoa/nsMenuItemIconX.h index 33a6bf81f78f..a5ecb3f9914d 100644 --- a/widget/cocoa/nsMenuItemIconX.h +++ b/widget/cocoa/nsMenuItemIconX.h @@ -10,7 +10,7 @@ #ifndef nsMenuItemIconX_h_ #define nsMenuItemIconX_h_ -#include "nsIconLoaderService.h" +#include "IconLoaderHelperCocoa.h" class nsIconLoaderService; class nsIURI; @@ -21,7 +21,7 @@ class nsMenuObjectX; #import -class nsMenuItemIconX : public nsIconLoaderObserver { +class nsMenuItemIconX : public mozilla::widget::IconLoaderListenerCocoa { public: nsMenuItemIconX(nsMenuObjectX* aMenuItem, nsIContent* aContent, NSMenuItem* aNativeMenuItem); @@ -37,16 +37,16 @@ class nsMenuItemIconX : public nsIconLoaderObserver { // GetIconURI fails if the item should not have any icon. nsresult GetIconURI(nsIURI** aIconURI); - // Overrides nsIconLoaderObserver::OnComplete. Handles the NSImage* created - // by nsIconLoaderService. - nsresult OnComplete(NSImage* aImage) override; - // Unless we take precautions, we may outlive the object that created us // (mMenuObject, which owns our native menu item (mNativeMenuItem)). // Destroy() should be called from mMenuObject's destructor to prevent // this from happening. See bug 499600. void Destroy(); + // Implements this method for mozilla::widget::IconLoaderListenerCocoa. + // Called once the icon load is complete. + nsresult OnComplete(); + protected: nsCOMPtr mContent; nsContentPolicyType mContentType; @@ -56,7 +56,8 @@ class nsMenuItemIconX : public nsIconLoaderObserver { NSMenuItem* mNativeMenuItem; // [weak] // The icon loader object should never outlive its creating nsMenuItemIconX // object. - RefPtr mIconLoader; + RefPtr mIconLoader; + RefPtr mIconLoaderHelper; }; #endif // nsMenuItemIconX_h_ diff --git a/widget/cocoa/nsMenuItemIconX.mm b/widget/cocoa/nsMenuItemIconX.mm index 3876bee8d345..20d9d895d741 100644 --- a/widget/cocoa/nsMenuItemIconX.mm +++ b/widget/cocoa/nsMenuItemIconX.mm @@ -36,6 +36,8 @@ using namespace mozilla; using mozilla::dom::Element; +using mozilla::widget::IconLoader; +using mozilla::widget::IconLoaderHelperCocoa; static const uint32_t kIconSize = 16; @@ -59,9 +61,11 @@ nsMenuItemIconX::~nsMenuItemIconX() { // are still outstanding). mMenuObjectX owns our mNativeMenuItem. void nsMenuItemIconX::Destroy() { if (mIconLoader) { - mIconLoader->Destroy(); mIconLoader = nullptr; } + if (mIconLoaderHelper) { + mIconLoaderHelper = nullptr; + } mMenuObject = nullptr; mNativeMenuItem = nil; } @@ -86,14 +90,15 @@ nsresult nsMenuItemIconX::SetupIcon() { } if (!mIconLoader) { - mIconLoader = new nsIconLoaderService(mContent, &mImageRegionRect, this, kIconSize, kIconSize); + mIconLoaderHelper = new IconLoaderHelperCocoa(this, kIconSize, kIconSize); + mIconLoader = new IconLoader(mIconLoaderHelper, mContent, mImageRegionRect); if (!mIconLoader) { return NS_ERROR_OUT_OF_MEMORY; } } if (!mSetIcon) { // Load placeholder icon. - [mNativeMenuItem setImage:mIconLoader->GetNativeIconImage()]; + [mNativeMenuItem setImage:mIconLoaderHelper->GetNativeIconImage()]; } rv = mIconLoader->LoadIcon(iconURI); @@ -194,26 +199,30 @@ nsresult nsMenuItemIconX::GetIconURI(nsIURI** aIconURI) { } // -// nsIconLoaderObserver +// mozilla::widget::IconLoaderListenerCocoa // -nsresult nsMenuItemIconX::OnComplete(NSImage* aImage) { - if (!mNativeMenuItem) { - if (aImage) { - [aImage release]; - } +nsresult nsMenuItemIconX::OnComplete() { + if (!mIconLoaderHelper) { return NS_ERROR_FAILURE; } - if (!aImage) { + NSImage* image = mIconLoaderHelper->GetNativeIconImage(); + if (!mNativeMenuItem) { + mIconLoaderHelper->Destroy(); + return NS_ERROR_FAILURE; + } + + if (!image) { [mNativeMenuItem setImage:nil]; return NS_OK; } - [mNativeMenuItem setImage:aImage]; + [mNativeMenuItem setImage:image]; if (mMenuObject) { mMenuObject->IconUpdated(); } - [aImage release]; + + mIconLoaderHelper->Destroy(); return NS_OK; } diff --git a/widget/cocoa/nsTouchBarInputIcon.h b/widget/cocoa/nsTouchBarInputIcon.h index 2263d72a9c02..8a37d9b44678 100644 --- a/widget/cocoa/nsTouchBarInputIcon.h +++ b/widget/cocoa/nsTouchBarInputIcon.h @@ -13,18 +13,17 @@ #import #include "mozilla/dom/Document.h" -#include "nsIconLoaderService.h" #include "nsTouchBarInput.h" #include "nsTouchBarNativeAPIDefines.h" +#include "IconLoaderHelperCocoa.h" using namespace mozilla::dom; -class nsIconLoaderService; class nsIURI; class nsIPrincipal; class imgRequestProxy; -class nsTouchBarInputIcon : public nsIconLoaderObserver { +class nsTouchBarInputIcon : public mozilla::widget::IconLoaderListenerCocoa { public: explicit nsTouchBarInputIcon(RefPtr aDocument, TouchBarInput* aInput, NSTouchBarItem* aItem); @@ -37,9 +36,9 @@ class nsTouchBarInputIcon : public nsIconLoaderObserver { // be no icon, in which case it clears any existing icon but still succeeds. nsresult SetupIcon(nsCOMPtr aIconURI); - // Overrides nsIconLoaderObserver::OnComplete. Handles the NSImage* created - // by nsIconLoaderService. - nsresult OnComplete(NSImage* aImage) override; + // Implements this method for mozilla::widget::IconLoaderListenerCocoa. + // Called once the icon load is complete. + nsresult OnComplete(); // Unless we take precautions, we may outlive the object that created us // (mTouchBar, which owns our native menu item (mTouchBarInput)). @@ -62,7 +61,8 @@ class nsTouchBarInputIcon : public nsIconLoaderObserver { NSPopoverTouchBarItem* mPopoverItem; // The icon loader object should never outlive its creating // nsTouchBarInputIcon object. - RefPtr mIconLoader; + RefPtr mIconLoader; + RefPtr mIconLoaderHelper; }; #endif // nsTouchBarInputIcon_h_ diff --git a/widget/cocoa/nsTouchBarInputIcon.mm b/widget/cocoa/nsTouchBarInputIcon.mm index 283ebe449450..be744365333d 100644 --- a/widget/cocoa/nsTouchBarInputIcon.mm +++ b/widget/cocoa/nsTouchBarInputIcon.mm @@ -18,6 +18,8 @@ #include "nsObjCExceptions.h" using namespace mozilla; +using mozilla::widget::IconLoader; +using mozilla::widget::IconLoaderHelperCocoa; static const uint32_t kIconSize = 16; static const CGFloat kHiDPIScalingFactor = 2.0f; @@ -50,9 +52,11 @@ nsTouchBarInputIcon::~nsTouchBarInputIcon() { void nsTouchBarInputIcon::Destroy() { ReleaseJSObjects(); if (mIconLoader) { - mIconLoader->Destroy(); mIconLoader = nullptr; } + if (mIconLoaderHelper) { + mIconLoaderHelper = nullptr; + } mButton = nil; mShareScrubber = nil; @@ -76,8 +80,8 @@ nsresult nsTouchBarInputIcon::SetupIcon(nsCOMPtr aIconURI) { if (!mIconLoader) { // We ask only for the HiDPI images since all Touch Bars are Retina // displays and we have no need for icons @1x. - mIconLoader = new nsIconLoaderService(mDocument, &mImageRegionRect, this, kIconSize, kIconSize, - kHiDPIScalingFactor); + mIconLoaderHelper = new IconLoaderHelperCocoa(this, kIconSize, kIconSize, kHiDPIScalingFactor); + mIconLoader = new IconLoader(mIconLoaderHelper, mDocument, mImageRegionRect); if (!mIconLoader) { return NS_ERROR_OUT_OF_MEMORY; } @@ -85,9 +89,9 @@ nsresult nsTouchBarInputIcon::SetupIcon(nsCOMPtr aIconURI) { if (!mSetIcon) { // Load placeholder icon. - [mButton setImage:mIconLoader->GetNativeIconImage()]; - [mShareScrubber setButtonImage:mIconLoader->GetNativeIconImage()]; - [mPopoverItem setCollapsedRepresentationImage:mIconLoader->GetNativeIconImage()]; + [mButton setImage:mIconLoaderHelper->GetNativeIconImage()]; + [mShareScrubber setButtonImage:mIconLoaderHelper->GetNativeIconImage()]; + [mPopoverItem setCollapsedRepresentationImage:mIconLoaderHelper->GetNativeIconImage()]; } nsresult rv = mIconLoader->LoadIcon(aIconURI, true /* aIsInternalIcon */); @@ -115,14 +119,19 @@ void nsTouchBarInputIcon::ReleaseJSObjects() { } // -// nsIconLoaderObserver +// mozilla::widget::IconLoaderListenerCocoa // -nsresult nsTouchBarInputIcon::OnComplete(NSImage* aImage) { - [mButton setImage:aImage]; - [mShareScrubber setButtonImage:aImage]; - [mPopoverItem setCollapsedRepresentationImage:aImage]; +nsresult nsTouchBarInputIcon::OnComplete() { + if (!mIconLoaderHelper) { + return NS_ERROR_FAILURE; + } - [aImage release]; + NSImage* image = mIconLoaderHelper->GetNativeIconImage(); + [mButton setImage:image]; + [mShareScrubber setButtonImage:image]; + [mPopoverItem setCollapsedRepresentationImage:image]; + + mIconLoaderHelper->Destroy(); return NS_OK; } diff --git a/widget/moz.build b/widget/moz.build index 3287000beeb9..2f0279c85a07 100644 --- a/widget/moz.build +++ b/widget/moz.build @@ -176,6 +176,7 @@ EXPORTS.mozilla += [ EXPORTS.mozilla.widget += [ 'CompositorWidget.h', + 'IconLoader.h', 'IMEData.h', 'InProcessCompositorWidget.h', 'MediaKeysEventSourceFactory.h', @@ -193,6 +194,7 @@ UNIFIED_SOURCES += [ 'GfxDriverInfo.cpp', 'GfxInfoBase.cpp', 'GfxInfoCollector.cpp', + 'IconLoader.cpp', 'IMEData.cpp', 'InProcessCompositorWidget.cpp', 'InputData.cpp',