From 61145184632497f381e21b881b7edc19057e1e87 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Sun, 10 May 2020 18:24:45 -0700 Subject: [PATCH] fix: NSImageName string conversion (#23467) --- shell/browser/api/electron_api_web_contents.cc | 2 +- shell/common/api/electron_api_native_image.cc | 5 ++--- shell/common/api/electron_api_native_image.h | 2 +- .../api/electron_api_native_image_mac.mm | 18 +++++++++++++++--- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 6e1c7926c442..14c17afd4d7f 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -2363,7 +2363,7 @@ void WebContents::StartDrag(const gin_helper::Dictionary& item, } gin::Handle icon; - if (!item.Get("icon", &icon)) { + if (!item.Get("icon", &icon) || icon->image().IsEmpty()) { args->ThrowError("Must specify non-empty 'icon' option"); return; } diff --git a/shell/common/api/electron_api_native_image.cc b/shell/common/api/electron_api_native_image.cc index fc265b1f5e55..50ff68a48250 100644 --- a/shell/common/api/electron_api_native_image.cc +++ b/shell/common/api/electron_api_native_image.cc @@ -490,9 +490,8 @@ gin::Handle NativeImage::CreateFromDataURL(v8::Isolate* isolate, } #if !defined(OS_MACOSX) -gin::Handle NativeImage::CreateFromNamedImage( - gin::Arguments* args, - const std::string& name) { +gin::Handle NativeImage::CreateFromNamedImage(gin::Arguments* args, + std::string name) { return CreateEmpty(args->isolate()); } #endif diff --git a/shell/common/api/electron_api_native_image.h b/shell/common/api/electron_api_native_image.h index e1d94e78e81f..1cb438265a43 100644 --- a/shell/common/api/electron_api_native_image.h +++ b/shell/common/api/electron_api_native_image.h @@ -62,7 +62,7 @@ class NativeImage : public gin_helper::Wrappable { static gin::Handle CreateFromDataURL(v8::Isolate* isolate, const GURL& url); static gin::Handle CreateFromNamedImage(gin::Arguments* args, - const std::string& name); + std::string name); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); diff --git a/shell/common/api/electron_api_native_image_mac.mm b/shell/common/api/electron_api_native_image_mac.mm index 8d6cab665ec2..607f24b7575f 100644 --- a/shell/common/api/electron_api_native_image_mac.mm +++ b/shell/common/api/electron_api_native_image_mac.mm @@ -33,12 +33,24 @@ double safeShift(double in, double def) { return def; } -gin::Handle NativeImage::CreateFromNamedImage( - gin::Arguments* args, - const std::string& name) { +gin::Handle NativeImage::CreateFromNamedImage(gin::Arguments* args, + std::string name) { @autoreleasepool { std::vector hsl_shift; + + // The string representations of NSImageNames don't match the strings + // themselves; they instead follow the following pattern: + // * NSImageNameActionTemplate -> "NSActionTemplate" + // * NSImageNameMultipleDocuments -> "NSMultipleDocuments" + // To account for this, we strip out "ImageName" from the passed string. + std::string to_remove("ImageName"); + size_t pos = name.find(to_remove); + if (pos != std::string::npos) { + name.erase(pos, to_remove.length()); + } + NSImage* image = [NSImage imageNamed:base::SysUTF8ToNSString(name)]; + if (!image.valid) { return CreateEmpty(args->isolate()); }