Bug 1663784 - Generalize nsIconLoaderService so other platforms can use it. r=mstange

In this patch, I've tried to abstract out all of the platform-specific parts
of what was the nsIconLoaderService into something generic enough to be used
across multiple platforms. I also renamed it and namespaced it to
mozilla::widget::IconLoader.

Depends on D89788

Differential Revision: https://phabricator.services.mozilla.com/D89972
This commit is contained in:
Mike Conley 2020-09-30 21:14:02 +00:00
Родитель bef332377f
Коммит 31794d68b0
13 изменённых файлов: 514 добавлений и 399 удалений

170
widget/IconLoader.cpp Normal file
Просмотреть файл

@ -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<mozilla::dom::Document> document = mContent->OwnerDoc();
nsCOMPtr<nsILoadGroup> loadGroup = document->GetDocumentLoadGroup();
if (!loadGroup) {
return NS_ERROR_FAILURE;
}
RefPtr<imgLoader> 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<imgIContainer> 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<imgIContainer> 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<imgIContainer> 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

77
widget/IconLoader.h Normal file
Просмотреть файл

@ -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<nsINode> mContent;
nsContentPolicyType mContentType;
RefPtr<imgRequestProxy> mIconRequest;
nsIntRect mImageRegionRect;
bool mLoadedIcon;
RefPtr<Helper> mHelper;
};
} // namespace mozilla::widget
#endif // mozilla_widget_IconLoader_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<mozilla::widget::IconLoaderListenerCocoa> mLoadListener;
uint32_t mIconHeight;
uint32_t mIconWidth;
CGFloat mScaleFactor;
NSImage* mNativeIconImage;
};
} // namespace mozilla::widget
#endif // mozilla_widget_IconLoaderHelperCocoa_h

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

@ -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 <limits> to get the libstdc++ version. */
#include <limits>
#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

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

@ -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',

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

@ -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 <Cocoa/Cocoa.h>
#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_

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

@ -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<nsIconLoaderObserver> 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<nsINode> mContent;
nsContentPolicyType mContentType;
RefPtr<imgRequestProxy> mIconRequest;
nsIntRect* mImageRegionRect;
bool mLoadedIcon;
NSImage* mNativeIconImage;
uint32_t mIconHeight;
uint32_t mIconWidth;
CGFloat mScaleFactor;
RefPtr<nsIconLoaderObserver> mCompletionHandler;
};
#endif // nsIconLoaderService_h_

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

@ -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 <limits> to get the libstdc++ version. */
#include <limits>
#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<nsIconLoaderObserver> 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<mozilla::dom::Document> document = mContent->OwnerDoc();
nsCOMPtr<nsILoadGroup> loadGroup = document->GetDocumentLoadGroup();
if (!loadGroup) {
return NS_ERROR_FAILURE;
}
RefPtr<imgLoader> 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<imgIContainer> 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<imgIContainer> 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;
}

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

@ -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 <Cocoa/Cocoa.h>
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<nsIContent> 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<nsIconLoaderService> mIconLoader;
RefPtr<mozilla::widget::IconLoader> mIconLoader;
RefPtr<mozilla::widget::IconLoaderHelperCocoa> mIconLoaderHelper;
};
#endif // nsMenuItemIconX_h_

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

@ -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;
}

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

@ -13,18 +13,17 @@
#import <Cocoa/Cocoa.h>
#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<Document> 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<nsIURI> 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<nsIconLoaderService> mIconLoader;
RefPtr<mozilla::widget::IconLoader> mIconLoader;
RefPtr<mozilla::widget::IconLoaderHelperCocoa> mIconLoaderHelper;
};
#endif // nsTouchBarInputIcon_h_

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

@ -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<nsIURI> 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<nsIURI> 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;
}

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

@ -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',