fix: NSImageName string conversion (#23467)

This commit is contained in:
Shelley Vohr 2020-05-10 18:24:45 -07:00 committed by GitHub
parent 392ea320cf
commit 6114518463
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 8 deletions

View file

@ -2363,7 +2363,7 @@ void WebContents::StartDrag(const gin_helper::Dictionary& item,
} }
gin::Handle<NativeImage> icon; gin::Handle<NativeImage> icon;
if (!item.Get("icon", &icon)) { if (!item.Get("icon", &icon) || icon->image().IsEmpty()) {
args->ThrowError("Must specify non-empty 'icon' option"); args->ThrowError("Must specify non-empty 'icon' option");
return; return;
} }

View file

@ -490,9 +490,8 @@ gin::Handle<NativeImage> NativeImage::CreateFromDataURL(v8::Isolate* isolate,
} }
#if !defined(OS_MACOSX) #if !defined(OS_MACOSX)
gin::Handle<NativeImage> NativeImage::CreateFromNamedImage( gin::Handle<NativeImage> NativeImage::CreateFromNamedImage(gin::Arguments* args,
gin::Arguments* args, std::string name) {
const std::string& name) {
return CreateEmpty(args->isolate()); return CreateEmpty(args->isolate());
} }
#endif #endif

View file

@ -62,7 +62,7 @@ class NativeImage : public gin_helper::Wrappable<NativeImage> {
static gin::Handle<NativeImage> CreateFromDataURL(v8::Isolate* isolate, static gin::Handle<NativeImage> CreateFromDataURL(v8::Isolate* isolate,
const GURL& url); const GURL& url);
static gin::Handle<NativeImage> CreateFromNamedImage(gin::Arguments* args, static gin::Handle<NativeImage> CreateFromNamedImage(gin::Arguments* args,
const std::string& name); std::string name);
static void BuildPrototype(v8::Isolate* isolate, static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype); v8::Local<v8::FunctionTemplate> prototype);

View file

@ -33,12 +33,24 @@ double safeShift(double in, double def) {
return def; return def;
} }
gin::Handle<NativeImage> NativeImage::CreateFromNamedImage( gin::Handle<NativeImage> NativeImage::CreateFromNamedImage(gin::Arguments* args,
gin::Arguments* args, std::string name) {
const std::string& name) {
@autoreleasepool { @autoreleasepool {
std::vector<double> hsl_shift; std::vector<double> 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)]; NSImage* image = [NSImage imageNamed:base::SysUTF8ToNSString(name)];
if (!image.valid) { if (!image.valid) {
return CreateEmpty(args->isolate()); return CreateEmpty(args->isolate());
} }