Skip to content

Commit 36bd940

Browse files
authored
refactor: ginify NativeImage (electron#24486)
1 parent f0a0e10 commit 36bd940

File tree

6 files changed

+56
-28
lines changed

6 files changed

+56
-28
lines changed

lib/browser/remote/server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type { MetaTypeFromRenderer, ObjectMember, MetaType, ObjProtoDescriptor }
88
const v8Util = process._linkedBinding('electron_common_v8_util');
99
const eventBinding = process._linkedBinding('electron_browser_event');
1010
const features = process._linkedBinding('electron_common_features');
11-
const { NativeImage } = process._linkedBinding('electron_common_native_image');
1211

1312
if (!features.isRemoteModuleEnabled()) {
1413
throw new Error('remote module is disabled');
@@ -102,7 +101,7 @@ const valueToMeta = function (sender: electron.WebContents, contextId: string, v
102101
// Recognize certain types of objects.
103102
if (value instanceof Buffer) {
104103
type = 'buffer';
105-
} else if (value instanceof NativeImage) {
104+
} else if (value && value.constructor && value.constructor.name === 'NativeImage') {
106105
type = 'nativeimage';
107106
} else if (Array.isArray(value)) {
108107
type = 'array';

lib/common/type-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { nativeImage, NativeImage } = process._linkedBinding('electron_common_native_image');
1+
const { nativeImage } = process._linkedBinding('electron_common_native_image');
22

33
export function isPromise (val: any) {
44
return (
@@ -80,7 +80,7 @@ function deserializeNativeImage (value: any) {
8080
}
8181

8282
export function serialize (value: any): any {
83-
if (value instanceof NativeImage) {
83+
if (value && value.constructor && value.constructor.name === 'NativeImage') {
8484
return serializeNativeImage(value);
8585
} if (Array.isArray(value)) {
8686
return value.map(serialize);

lib/renderer/api/remote.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { commonModuleList } from '@electron/internal/common/api/module-list';
88

99
const v8Util = process._linkedBinding('electron_common_v8_util');
1010
const { hasSwitch } = process._linkedBinding('electron_common_command_line');
11-
const { NativeImage } = process._linkedBinding('electron_common_native_image');
1211

1312
const callbacksRegistry = new CallbacksRegistry();
1413
const remoteObjectCache = new Map();
@@ -59,7 +58,7 @@ function wrapArgs (args: any[], visited = new Set()): any {
5958
};
6059
}
6160

62-
if (value instanceof NativeImage) {
61+
if (value && value.constructor && value.constructor.name === 'NativeImage') {
6362
return { type: 'nativeimage', value: serialize(value) };
6463
} else if (Array.isArray(value)) {
6564
visited.add(value);

shell/common/api/electron_api_native_image.cc

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
#include "base/strings/string_util.h"
1515
#include "base/strings/utf_string_conversions.h"
1616
#include "base/threading/thread_restrictions.h"
17+
#include "gin/arguments.h"
18+
#include "gin/object_template_builder.h"
19+
#include "gin/per_isolate_data.h"
1720
#include "net/base/data_url.h"
1821
#include "shell/common/asar/asar_util.h"
1922
#include "shell/common/gin_converters/file_path_converter.h"
2023
#include "shell/common/gin_converters/gfx_converter.h"
2124
#include "shell/common/gin_converters/gurl_converter.h"
2225
#include "shell/common/gin_converters/value_converter.h"
2326
#include "shell/common/gin_helper/dictionary.h"
27+
#include "shell/common/gin_helper/function_template_extensions.h"
2428
#include "shell/common/gin_helper/object_template_builder.h"
2529
#include "shell/common/node_includes.h"
2630
#include "shell/common/skia_util.h"
@@ -104,32 +108,30 @@ void Noop(char*, void*) {}
104108
} // namespace
105109

106110
NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
107-
: image_(image) {
108-
Init(isolate);
111+
: image_(image), isolate_(isolate) {
109112
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
110-
isolate->AdjustAmountOfExternalAllocatedMemory(
113+
isolate_->AdjustAmountOfExternalAllocatedMemory(
111114
image_.ToImageSkia()->bitmap()->computeByteSize());
112115
}
113116
}
114117

115118
#if defined(OS_WIN)
116119
NativeImage::NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path)
117-
: hicon_path_(hicon_path) {
120+
: hicon_path_(hicon_path), isolate_(isolate) {
118121
// Use the 256x256 icon as fallback icon.
119122
gfx::ImageSkia image_skia;
120123
electron::util::ReadImageSkiaFromICO(&image_skia, GetHICON(256));
121124
image_ = gfx::Image(image_skia);
122-
Init(isolate);
123125
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
124-
isolate->AdjustAmountOfExternalAllocatedMemory(
126+
isolate_->AdjustAmountOfExternalAllocatedMemory(
125127
image_.ToImageSkia()->bitmap()->computeByteSize());
126128
}
127129
}
128130
#endif
129131

130132
NativeImage::~NativeImage() {
131133
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
132-
isolate()->AdjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(
134+
isolate_->AdjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(
133135
image_.ToImageSkia()->bitmap()->computeByteSize()));
134136
}
135137
}
@@ -510,10 +512,19 @@ gin::Handle<NativeImage> NativeImage::CreateFromNamedImage(gin::Arguments* args,
510512
#endif
511513

512514
// static
513-
void NativeImage::BuildPrototype(v8::Isolate* isolate,
514-
v8::Local<v8::FunctionTemplate> prototype) {
515-
prototype->SetClassName(gin::StringToV8(isolate, "NativeImage"));
516-
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
515+
gin::ObjectTemplateBuilder NativeImage::GetObjectTemplateBuilder(
516+
v8::Isolate* isolate) {
517+
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
518+
auto* wrapper_info = &kWrapperInfo;
519+
v8::Local<v8::FunctionTemplate> constructor =
520+
data->GetFunctionTemplate(wrapper_info);
521+
if (constructor.IsEmpty()) {
522+
constructor = v8::FunctionTemplate::New(isolate);
523+
constructor->SetClassName(gin::StringToV8(isolate, GetTypeName()));
524+
data->SetFunctionTemplate(wrapper_info, constructor);
525+
}
526+
return gin::ObjectTemplateBuilder(isolate, GetTypeName(),
527+
constructor->InstanceTemplate())
517528
.SetMethod("toPNG", &NativeImage::ToPNG)
518529
.SetMethod("toJPEG", &NativeImage::ToJPEG)
519530
.SetMethod("toBitmap", &NativeImage::ToBitmap)
@@ -533,6 +544,13 @@ void NativeImage::BuildPrototype(v8::Isolate* isolate,
533544
.SetMethod("addRepresentation", &NativeImage::AddRepresentation);
534545
}
535546

547+
const char* NativeImage::GetTypeName() {
548+
return "NativeImage";
549+
}
550+
551+
// static
552+
gin::WrapperInfo NativeImage::kWrapperInfo = {gin::kEmbedderNativeGin};
553+
536554
} // namespace api
537555

538556
} // namespace electron
@@ -542,8 +560,9 @@ namespace gin {
542560
v8::Local<v8::Value> Converter<electron::api::NativeImage*>::ToV8(
543561
v8::Isolate* isolate,
544562
electron::api::NativeImage* val) {
545-
if (val)
546-
return val->GetWrapper();
563+
v8::Local<v8::Object> ret;
564+
if (val && val->GetWrapper(isolate).ToLocal(&ret))
565+
return ret;
547566
else
548567
return v8::Null(isolate);
549568
}
@@ -570,9 +589,11 @@ bool Converter<electron::api::NativeImage*>::FromV8(
570589
return true;
571590
}
572591

592+
// reinterpret_cast is safe here because NativeImage is the only subclass of
593+
// gin::Wrappable<NativeImage>.
573594
*out = static_cast<electron::api::NativeImage*>(
574-
static_cast<gin_helper::WrappableBase*>(
575-
gin_helper::internal::FromV8Impl(isolate, val)));
595+
static_cast<gin::WrappableBase*>(gin::internal::FromV8Impl(
596+
isolate, val, &electron::api::NativeImage::kWrapperInfo)));
576597
return *out != nullptr;
577598
}
578599

@@ -588,9 +609,6 @@ void Initialize(v8::Local<v8::Object> exports,
588609
void* priv) {
589610
v8::Isolate* isolate = context->GetIsolate();
590611
gin_helper::Dictionary dict(isolate, exports);
591-
dict.Set("NativeImage", NativeImage::GetConstructor(isolate)
592-
->GetFunction(context)
593-
.ToLocalChecked());
594612
gin_helper::Dictionary native_image = gin::Dictionary::CreateEmpty(isolate);
595613
dict.Set("nativeImage", native_image);
596614

shell/common/api/electron_api_native_image.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
#include "base/values.h"
1313
#include "gin/handle.h"
14+
#include "gin/wrappable.h"
1415
#include "shell/common/gin_helper/error_thrower.h"
15-
#include "shell/common/gin_helper/wrappable.h"
1616
#include "ui/gfx/image/image.h"
1717

1818
#if defined(OS_WIN)
@@ -35,11 +35,15 @@ namespace gin_helper {
3535
class Dictionary;
3636
}
3737

38+
namespace gin {
39+
class Arguments;
40+
}
41+
3842
namespace electron {
3943

4044
namespace api {
4145

42-
class NativeImage : public gin_helper::Wrappable<NativeImage> {
46+
class NativeImage : public gin::Wrappable<NativeImage> {
4347
public:
4448
static gin::Handle<NativeImage> CreateEmpty(v8::Isolate* isolate);
4549
static gin::Handle<NativeImage> Create(v8::Isolate* isolate,
@@ -65,8 +69,13 @@ class NativeImage : public gin_helper::Wrappable<NativeImage> {
6569
static gin::Handle<NativeImage> CreateFromNamedImage(gin::Arguments* args,
6670
std::string name);
6771

68-
static void BuildPrototype(v8::Isolate* isolate,
69-
v8::Local<v8::FunctionTemplate> prototype);
72+
static v8::Local<v8::FunctionTemplate> GetConstructor(v8::Isolate* isolate);
73+
74+
// gin::Wrappable
75+
static gin::WrapperInfo kWrapperInfo;
76+
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
77+
v8::Isolate* isolate) override;
78+
const char* GetTypeName() override;
7079

7180
#if defined(OS_WIN)
7281
HICON GetHICON(int size);
@@ -109,6 +118,8 @@ class NativeImage : public gin_helper::Wrappable<NativeImage> {
109118

110119
gfx::Image image_;
111120

121+
v8::Isolate* isolate_;
122+
112123
DISALLOW_COPY_AND_ASSIGN(NativeImage);
113124
};
114125

shell/common/api/electron_api_native_image_mac.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import <Cocoa/Cocoa.h>
1111

1212
#include "base/strings/sys_string_conversions.h"
13+
#include "gin/arguments.h"
1314
#include "ui/gfx/color_utils.h"
1415
#include "ui/gfx/image/image.h"
1516
#include "ui/gfx/image/image_skia.h"

0 commit comments

Comments
 (0)