Commit c58186cc authored by Avi Drissman's avatar Avi Drissman Committed by Commit Bot

Make gfx::Image's NSImage constructor retain the incoming image.

The Mac convention for ObjC objects is that if a callee needs to
have a reference, it should take the reference itself. The
current behavior of gfx::Image's NSImage constructor is surprising
and relying on explaining it in the comment is inadequate.

Note that gfx::Image's UIImage constructor had a similar change
made to it in 2a1f622d.

BUG=904645

Change-Id: I4dab00c3c7f3ff2f73e17520562d728b3a2e52aa
Reviewed-on: https://chromium-review.googlesource.com/c/1335957Reviewed-by: default avatarRobert Sesek <rsesek@chromium.org>
Commit-Queue: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608566}
parent 42cbbce1
...@@ -35,7 +35,7 @@ void IconLoader::ReadIcon() { ...@@ -35,7 +35,7 @@ void IconLoader::ReadIcon() {
if (icon_size_ == ALL) { if (icon_size_ == ALL) {
// The NSImage already has all sizes. // The NSImage already has all sizes.
image = std::make_unique<gfx::Image>([icon retain]); image = std::make_unique<gfx::Image>(icon);
} else { } else {
NSSize size = NSZeroSize; NSSize size = NSZeroSize;
switch (icon_size_) { switch (icon_size_) {
......
...@@ -133,7 +133,7 @@ gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { ...@@ -133,7 +133,7 @@ gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) {
return GetEmptyImage(); return GetEmptyImage();
} }
image = gfx::Image(ns_image.release()); image = gfx::Image(ns_image);
} }
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
......
...@@ -177,7 +177,8 @@ class ImageRepCocoaTouch : public ImageRep { ...@@ -177,7 +177,8 @@ class ImageRepCocoaTouch : public ImageRep {
explicit ImageRepCocoaTouch(UIImage* image) explicit ImageRepCocoaTouch(UIImage* image)
: ImageRep(Image::kImageRepCocoaTouch), : ImageRep(Image::kImageRepCocoaTouch),
image_(image) { image_(image) {
CHECK(image); CHECK(image_);
base::mac::NSObjectRetain(image_);
} }
~ImageRepCocoaTouch() override { ~ImageRepCocoaTouch() override {
...@@ -204,7 +205,8 @@ class ImageRepCocoa : public ImageRep { ...@@ -204,7 +205,8 @@ class ImageRepCocoa : public ImageRep {
explicit ImageRepCocoa(NSImage* image) explicit ImageRepCocoa(NSImage* image)
: ImageRep(Image::kImageRepCocoa), : ImageRep(Image::kImageRepCocoa),
image_(image) { image_(image) {
CHECK(image); CHECK(image_);
base::mac::NSObjectRetain(image_);
} }
~ImageRepCocoa() override { ~ImageRepCocoa() override {
...@@ -351,7 +353,6 @@ Image::Image(const ImageSkia& image) { ...@@ -351,7 +353,6 @@ Image::Image(const ImageSkia& image) {
#if defined(OS_IOS) #if defined(OS_IOS)
Image::Image(UIImage* image) { Image::Image(UIImage* image) {
if (image) { if (image) {
base::mac::NSObjectRetain(image);
storage_ = new internal::ImageStorage(Image::kImageRepCocoaTouch); storage_ = new internal::ImageStorage(Image::kImageRepCocoaTouch);
AddRepresentation(std::make_unique<internal::ImageRepCocoaTouch>(image)); AddRepresentation(std::make_unique<internal::ImageRepCocoaTouch>(image));
} }
...@@ -463,7 +464,6 @@ UIImage* Image::ToUIImage() const { ...@@ -463,7 +464,6 @@ UIImage* Image::ToUIImage() const {
const internal::ImageRepSkia* skia_rep = const internal::ImageRepSkia* skia_rep =
GetRepresentation(kImageRepSkia, true)->AsImageRepSkia(); GetRepresentation(kImageRepSkia, true)->AsImageRepSkia();
UIImage* image = UIImageFromImageSkia(*skia_rep->image()); UIImage* image = UIImageFromImageSkia(*skia_rep->image());
base::mac::NSObjectRetain(image);
scoped_rep.reset(new internal::ImageRepCocoaTouch(image)); scoped_rep.reset(new internal::ImageRepCocoaTouch(image));
break; break;
} }
...@@ -496,7 +496,6 @@ NSImage* Image::ToNSImage() const { ...@@ -496,7 +496,6 @@ NSImage* Image::ToNSImage() const {
GetRepresentation(kImageRepSkia, true)->AsImageRepSkia(); GetRepresentation(kImageRepSkia, true)->AsImageRepSkia();
NSImage* image = NSImageFromImageSkiaWithColorSpace(*skia_rep->image(), NSImage* image = NSImageFromImageSkiaWithColorSpace(*skia_rep->image(),
default_representation_color_space); default_representation_color_space);
base::mac::NSObjectRetain(image);
scoped_rep.reset(new internal::ImageRepCocoa(image)); scoped_rep.reset(new internal::ImageRepCocoa(image));
break; break;
} }
......
...@@ -71,9 +71,7 @@ class GFX_EXPORT Image { ...@@ -71,9 +71,7 @@ class GFX_EXPORT Image {
// Retains |image|. // Retains |image|.
explicit Image(UIImage* image); explicit Image(UIImage* image);
#elif defined(OS_MACOSX) #elif defined(OS_MACOSX)
// Does not retain |image|; expects to take ownership. // Retains |image|.
// A single NSImage object can contain multiple bitmaps so there's no reason
// to pass a vector of these.
explicit Image(NSImage* image); explicit Image(NSImage* image);
#endif #endif
......
...@@ -101,7 +101,7 @@ TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) { ...@@ -101,7 +101,7 @@ TEST_F(ImageMacTest, MultiResolutionNSImageToImageSkia) {
[ns_image addRepresentation:ns_image_rep1]; [ns_image addRepresentation:ns_image_rep1];
[ns_image addRepresentation:ns_image_rep2]; [ns_image addRepresentation:ns_image_rep2];
gfx::Image image(ns_image.release()); gfx::Image image(ns_image);
EXPECT_EQ(1u, image.RepresentationCount()); EXPECT_EQ(1u, image.RepresentationCount());
...@@ -132,7 +132,7 @@ TEST_F(ImageMacTest, UnalignedMultiResolutionNSImageToImageSkia) { ...@@ -132,7 +132,7 @@ TEST_F(ImageMacTest, UnalignedMultiResolutionNSImageToImageSkia) {
[[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]); [[NSImage alloc] initWithSize:NSMakeSize(kWidth1x, kHeight1x)]);
[ns_image addRepresentation:ns_image_rep4]; [ns_image addRepresentation:ns_image_rep4];
gfx::Image image(ns_image.release()); gfx::Image image(ns_image);
EXPECT_EQ(1u, image.RepresentationCount()); EXPECT_EQ(1u, image.RepresentationCount());
......
...@@ -19,11 +19,9 @@ ...@@ -19,11 +19,9 @@
#include "ui/gfx/image/image_skia.h" #include "ui/gfx/image/image_skia.h"
#if defined(OS_IOS) #if defined(OS_IOS)
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_cftyperef.h" #include "base/mac/scoped_cftyperef.h"
#include "skia/ext/skia_utils_ios.h" #include "skia/ext/skia_utils_ios.h"
#elif defined(OS_MACOSX) #elif defined(OS_MACOSX)
#include "base/mac/foundation_util.h"
#include "base/mac/mac_util.h" #include "base/mac/mac_util.h"
#include "skia/ext/skia_utils_mac.h" #include "skia/ext/skia_utils_mac.h"
#endif #endif
...@@ -222,12 +220,10 @@ PlatformImage CreatePlatformImage() { ...@@ -222,12 +220,10 @@ PlatformImage CreatePlatformImage() {
CGColorSpaceCreateDeviceRGB()); CGColorSpaceCreateDeviceRGB());
UIImage* image = UIImage* image =
skia::SkBitmapToUIImageWithColorSpace(bitmap, scale, color_space); skia::SkBitmapToUIImageWithColorSpace(bitmap, scale, color_space);
base::mac::NSObjectRetain(image);
return image; return image;
#elif defined(OS_MACOSX) #elif defined(OS_MACOSX)
NSImage* image = skia::SkBitmapToNSImageWithColorSpace( NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
bitmap, base::mac::GetGenericRGBColorSpace()); bitmap, base::mac::GetGenericRGBColorSpace());
base::mac::NSObjectRetain(image);
return image; return image;
#else #else
return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
...@@ -258,7 +254,7 @@ gfx::Image CopyViaPlatformType(const gfx::Image& image) { ...@@ -258,7 +254,7 @@ gfx::Image CopyViaPlatformType(const gfx::Image& image) {
#if defined(OS_IOS) #if defined(OS_IOS)
return gfx::Image(image.ToUIImage()); return gfx::Image(image.ToUIImage());
#elif defined(OS_MACOSX) #elif defined(OS_MACOSX)
return gfx::Image(image.CopyNSImage()); return gfx::Image(image.ToNSImage());
#else #else
return gfx::Image(image.AsImageSkia()); return gfx::Image(image.AsImageSkia());
#endif #endif
......
...@@ -128,7 +128,7 @@ TEST_F(NotificationControllerTest, BasicLayout) { ...@@ -128,7 +128,7 @@ TEST_F(NotificationControllerTest, BasicLayout) {
ASCIIToUTF16("Jonathan and 5 others"), gfx::Image(), base::string16(), ASCIIToUTF16("Jonathan and 5 others"), gfx::Image(), base::string16(),
GURL(), DummyNotifierId(), message_center::RichNotificationData(), GURL(), DummyNotifierId(), message_center::RichNotificationData(),
NULL)); NULL));
gfx::Image testIcon([TestIcon() retain]); gfx::Image testIcon(TestIcon());
notification->set_icon(testIcon); notification->set_icon(testIcon);
notification->set_small_image(testIcon); notification->set_small_image(testIcon);
...@@ -241,7 +241,7 @@ TEST_F(NotificationControllerTest, Update) { ...@@ -241,7 +241,7 @@ TEST_F(NotificationControllerTest, Update) {
EXPECT_FALSE([[controller smallImageView] image]); EXPECT_FALSE([[controller smallImageView] image]);
// Update the icon. // Update the icon.
gfx::Image testIcon([TestIcon() retain]); gfx::Image testIcon(TestIcon());
notification->set_icon(testIcon); notification->set_icon(testIcon);
notification->set_small_image(testIcon); notification->set_small_image(testIcon);
[controller updateNotification:notification.get()]; [controller updateNotification:notification.get()];
...@@ -284,7 +284,7 @@ TEST_F(NotificationControllerTest, Image) { ...@@ -284,7 +284,7 @@ TEST_F(NotificationControllerTest, Image) {
GURL(), DummyNotifierId(), message_center::RichNotificationData(), GURL(), DummyNotifierId(), message_center::RichNotificationData(),
NULL)); NULL));
NSImage* image = [NSImage imageNamed:NSImageNameFolder]; NSImage* image = [NSImage imageNamed:NSImageNameFolder];
notification->set_image(gfx::Image([image retain])); notification->set_image(gfx::Image(image));
MockMessageCenter message_center; MockMessageCenter message_center;
......
...@@ -235,8 +235,8 @@ TEST_F(PopupCollectionTest, UpdateIconAndBody) { ...@@ -235,8 +235,8 @@ TEST_F(PopupCollectionTest, UpdateIconAndBody) {
MCNotificationController* controller = MCNotificationController* controller =
[[popups objectAtIndex:1] notificationController]; [[popups objectAtIndex:1] notificationController];
EXPECT_FALSE([[controller iconView] image]); EXPECT_FALSE([[controller iconView] image]);
center_->SetNotificationIcon("2", center_->SetNotificationIcon(
gfx::Image([[NSImage imageNamed:NSImageNameUser] retain])); "2", gfx::Image([NSImage imageNamed:NSImageNameUser]));
WaitForAnimationEnded(); WaitForAnimationEnded();
EXPECT_TRUE([[controller iconView] image]); EXPECT_TRUE([[controller iconView] image]);
......
...@@ -50,9 +50,9 @@ bool GrabViewSnapshot(gfx::NativeView native_view, ...@@ -50,9 +50,9 @@ bool GrabViewSnapshot(gfx::NativeView native_view,
if (CGImageGetWidth(windowSnapshot) <= 0) if (CGImageGetWidth(windowSnapshot) <= 0)
return false; return false;
NSImage* nsImage = *image =
[[NSImage alloc] initWithCGImage:windowSnapshot size:NSZeroSize]; gfx::Image([[[NSImage alloc] initWithCGImage:windowSnapshot
*image = gfx::Image(nsImage); size:NSZeroSize] autorelease]);
return true; return true;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment