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
106110NativeImage::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)
116119NativeImage::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
130132NativeImage::~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 {
542560v8::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
0 commit comments