refactor: ginify NativeImage (#24486)
This commit is contained in:
parent
f0a0e10bd1
commit
36bd940bc3
6 changed files with 56 additions and 28 deletions
|
@ -8,7 +8,6 @@ import type { MetaTypeFromRenderer, ObjectMember, MetaType, ObjProtoDescriptor }
|
||||||
const v8Util = process._linkedBinding('electron_common_v8_util');
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
||||||
const eventBinding = process._linkedBinding('electron_browser_event');
|
const eventBinding = process._linkedBinding('electron_browser_event');
|
||||||
const features = process._linkedBinding('electron_common_features');
|
const features = process._linkedBinding('electron_common_features');
|
||||||
const { NativeImage } = process._linkedBinding('electron_common_native_image');
|
|
||||||
|
|
||||||
if (!features.isRemoteModuleEnabled()) {
|
if (!features.isRemoteModuleEnabled()) {
|
||||||
throw new Error('remote module is disabled');
|
throw new Error('remote module is disabled');
|
||||||
|
@ -102,7 +101,7 @@ const valueToMeta = function (sender: electron.WebContents, contextId: string, v
|
||||||
// Recognize certain types of objects.
|
// Recognize certain types of objects.
|
||||||
if (value instanceof Buffer) {
|
if (value instanceof Buffer) {
|
||||||
type = 'buffer';
|
type = 'buffer';
|
||||||
} else if (value instanceof NativeImage) {
|
} else if (value && value.constructor && value.constructor.name === 'NativeImage') {
|
||||||
type = 'nativeimage';
|
type = 'nativeimage';
|
||||||
} else if (Array.isArray(value)) {
|
} else if (Array.isArray(value)) {
|
||||||
type = 'array';
|
type = 'array';
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
const { nativeImage, NativeImage } = process._linkedBinding('electron_common_native_image');
|
const { nativeImage } = process._linkedBinding('electron_common_native_image');
|
||||||
|
|
||||||
export function isPromise (val: any) {
|
export function isPromise (val: any) {
|
||||||
return (
|
return (
|
||||||
|
@ -80,7 +80,7 @@ function deserializeNativeImage (value: any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function serialize (value: any): any {
|
export function serialize (value: any): any {
|
||||||
if (value instanceof NativeImage) {
|
if (value && value.constructor && value.constructor.name === 'NativeImage') {
|
||||||
return serializeNativeImage(value);
|
return serializeNativeImage(value);
|
||||||
} if (Array.isArray(value)) {
|
} if (Array.isArray(value)) {
|
||||||
return value.map(serialize);
|
return value.map(serialize);
|
||||||
|
|
|
@ -8,7 +8,6 @@ import { commonModuleList } from '@electron/internal/common/api/module-list';
|
||||||
|
|
||||||
const v8Util = process._linkedBinding('electron_common_v8_util');
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
||||||
const { hasSwitch } = process._linkedBinding('electron_common_command_line');
|
const { hasSwitch } = process._linkedBinding('electron_common_command_line');
|
||||||
const { NativeImage } = process._linkedBinding('electron_common_native_image');
|
|
||||||
|
|
||||||
const callbacksRegistry = new CallbacksRegistry();
|
const callbacksRegistry = new CallbacksRegistry();
|
||||||
const remoteObjectCache = new Map();
|
const remoteObjectCache = new Map();
|
||||||
|
@ -59,7 +58,7 @@ function wrapArgs (args: any[], visited = new Set()): any {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value instanceof NativeImage) {
|
if (value && value.constructor && value.constructor.name === 'NativeImage') {
|
||||||
return { type: 'nativeimage', value: serialize(value) };
|
return { type: 'nativeimage', value: serialize(value) };
|
||||||
} else if (Array.isArray(value)) {
|
} else if (Array.isArray(value)) {
|
||||||
visited.add(value);
|
visited.add(value);
|
||||||
|
|
|
@ -14,6 +14,9 @@
|
||||||
#include "base/strings/string_util.h"
|
#include "base/strings/string_util.h"
|
||||||
#include "base/strings/utf_string_conversions.h"
|
#include "base/strings/utf_string_conversions.h"
|
||||||
#include "base/threading/thread_restrictions.h"
|
#include "base/threading/thread_restrictions.h"
|
||||||
|
#include "gin/arguments.h"
|
||||||
|
#include "gin/object_template_builder.h"
|
||||||
|
#include "gin/per_isolate_data.h"
|
||||||
#include "net/base/data_url.h"
|
#include "net/base/data_url.h"
|
||||||
#include "shell/common/asar/asar_util.h"
|
#include "shell/common/asar/asar_util.h"
|
||||||
#include "shell/common/gin_converters/file_path_converter.h"
|
#include "shell/common/gin_converters/file_path_converter.h"
|
||||||
|
@ -21,6 +24,7 @@
|
||||||
#include "shell/common/gin_converters/gurl_converter.h"
|
#include "shell/common/gin_converters/gurl_converter.h"
|
||||||
#include "shell/common/gin_converters/value_converter.h"
|
#include "shell/common/gin_converters/value_converter.h"
|
||||||
#include "shell/common/gin_helper/dictionary.h"
|
#include "shell/common/gin_helper/dictionary.h"
|
||||||
|
#include "shell/common/gin_helper/function_template_extensions.h"
|
||||||
#include "shell/common/gin_helper/object_template_builder.h"
|
#include "shell/common/gin_helper/object_template_builder.h"
|
||||||
#include "shell/common/node_includes.h"
|
#include "shell/common/node_includes.h"
|
||||||
#include "shell/common/skia_util.h"
|
#include "shell/common/skia_util.h"
|
||||||
|
@ -104,24 +108,22 @@ void Noop(char*, void*) {}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
|
NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
|
||||||
: image_(image) {
|
: image_(image), isolate_(isolate) {
|
||||||
Init(isolate);
|
|
||||||
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
|
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
|
||||||
isolate->AdjustAmountOfExternalAllocatedMemory(
|
isolate_->AdjustAmountOfExternalAllocatedMemory(
|
||||||
image_.ToImageSkia()->bitmap()->computeByteSize());
|
image_.ToImageSkia()->bitmap()->computeByteSize());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
NativeImage::NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path)
|
NativeImage::NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path)
|
||||||
: hicon_path_(hicon_path) {
|
: hicon_path_(hicon_path), isolate_(isolate) {
|
||||||
// Use the 256x256 icon as fallback icon.
|
// Use the 256x256 icon as fallback icon.
|
||||||
gfx::ImageSkia image_skia;
|
gfx::ImageSkia image_skia;
|
||||||
electron::util::ReadImageSkiaFromICO(&image_skia, GetHICON(256));
|
electron::util::ReadImageSkiaFromICO(&image_skia, GetHICON(256));
|
||||||
image_ = gfx::Image(image_skia);
|
image_ = gfx::Image(image_skia);
|
||||||
Init(isolate);
|
|
||||||
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
|
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
|
||||||
isolate->AdjustAmountOfExternalAllocatedMemory(
|
isolate_->AdjustAmountOfExternalAllocatedMemory(
|
||||||
image_.ToImageSkia()->bitmap()->computeByteSize());
|
image_.ToImageSkia()->bitmap()->computeByteSize());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -129,7 +131,7 @@ NativeImage::NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path)
|
||||||
|
|
||||||
NativeImage::~NativeImage() {
|
NativeImage::~NativeImage() {
|
||||||
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
|
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
|
||||||
isolate()->AdjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(
|
isolate_->AdjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(
|
||||||
image_.ToImageSkia()->bitmap()->computeByteSize()));
|
image_.ToImageSkia()->bitmap()->computeByteSize()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -510,10 +512,19 @@ gin::Handle<NativeImage> NativeImage::CreateFromNamedImage(gin::Arguments* args,
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void NativeImage::BuildPrototype(v8::Isolate* isolate,
|
gin::ObjectTemplateBuilder NativeImage::GetObjectTemplateBuilder(
|
||||||
v8::Local<v8::FunctionTemplate> prototype) {
|
v8::Isolate* isolate) {
|
||||||
prototype->SetClassName(gin::StringToV8(isolate, "NativeImage"));
|
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
|
||||||
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
auto* wrapper_info = &kWrapperInfo;
|
||||||
|
v8::Local<v8::FunctionTemplate> constructor =
|
||||||
|
data->GetFunctionTemplate(wrapper_info);
|
||||||
|
if (constructor.IsEmpty()) {
|
||||||
|
constructor = v8::FunctionTemplate::New(isolate);
|
||||||
|
constructor->SetClassName(gin::StringToV8(isolate, GetTypeName()));
|
||||||
|
data->SetFunctionTemplate(wrapper_info, constructor);
|
||||||
|
}
|
||||||
|
return gin::ObjectTemplateBuilder(isolate, GetTypeName(),
|
||||||
|
constructor->InstanceTemplate())
|
||||||
.SetMethod("toPNG", &NativeImage::ToPNG)
|
.SetMethod("toPNG", &NativeImage::ToPNG)
|
||||||
.SetMethod("toJPEG", &NativeImage::ToJPEG)
|
.SetMethod("toJPEG", &NativeImage::ToJPEG)
|
||||||
.SetMethod("toBitmap", &NativeImage::ToBitmap)
|
.SetMethod("toBitmap", &NativeImage::ToBitmap)
|
||||||
|
@ -533,6 +544,13 @@ void NativeImage::BuildPrototype(v8::Isolate* isolate,
|
||||||
.SetMethod("addRepresentation", &NativeImage::AddRepresentation);
|
.SetMethod("addRepresentation", &NativeImage::AddRepresentation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char* NativeImage::GetTypeName() {
|
||||||
|
return "NativeImage";
|
||||||
|
}
|
||||||
|
|
||||||
|
// static
|
||||||
|
gin::WrapperInfo NativeImage::kWrapperInfo = {gin::kEmbedderNativeGin};
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
||||||
} // namespace electron
|
} // namespace electron
|
||||||
|
@ -542,8 +560,9 @@ namespace gin {
|
||||||
v8::Local<v8::Value> Converter<electron::api::NativeImage*>::ToV8(
|
v8::Local<v8::Value> Converter<electron::api::NativeImage*>::ToV8(
|
||||||
v8::Isolate* isolate,
|
v8::Isolate* isolate,
|
||||||
electron::api::NativeImage* val) {
|
electron::api::NativeImage* val) {
|
||||||
if (val)
|
v8::Local<v8::Object> ret;
|
||||||
return val->GetWrapper();
|
if (val && val->GetWrapper(isolate).ToLocal(&ret))
|
||||||
|
return ret;
|
||||||
else
|
else
|
||||||
return v8::Null(isolate);
|
return v8::Null(isolate);
|
||||||
}
|
}
|
||||||
|
@ -570,9 +589,11 @@ bool Converter<electron::api::NativeImage*>::FromV8(
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reinterpret_cast is safe here because NativeImage is the only subclass of
|
||||||
|
// gin::Wrappable<NativeImage>.
|
||||||
*out = static_cast<electron::api::NativeImage*>(
|
*out = static_cast<electron::api::NativeImage*>(
|
||||||
static_cast<gin_helper::WrappableBase*>(
|
static_cast<gin::WrappableBase*>(gin::internal::FromV8Impl(
|
||||||
gin_helper::internal::FromV8Impl(isolate, val)));
|
isolate, val, &electron::api::NativeImage::kWrapperInfo)));
|
||||||
return *out != nullptr;
|
return *out != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -588,9 +609,6 @@ void Initialize(v8::Local<v8::Object> exports,
|
||||||
void* priv) {
|
void* priv) {
|
||||||
v8::Isolate* isolate = context->GetIsolate();
|
v8::Isolate* isolate = context->GetIsolate();
|
||||||
gin_helper::Dictionary dict(isolate, exports);
|
gin_helper::Dictionary dict(isolate, exports);
|
||||||
dict.Set("NativeImage", NativeImage::GetConstructor(isolate)
|
|
||||||
->GetFunction(context)
|
|
||||||
.ToLocalChecked());
|
|
||||||
gin_helper::Dictionary native_image = gin::Dictionary::CreateEmpty(isolate);
|
gin_helper::Dictionary native_image = gin::Dictionary::CreateEmpty(isolate);
|
||||||
dict.Set("nativeImage", native_image);
|
dict.Set("nativeImage", native_image);
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,8 @@
|
||||||
|
|
||||||
#include "base/values.h"
|
#include "base/values.h"
|
||||||
#include "gin/handle.h"
|
#include "gin/handle.h"
|
||||||
|
#include "gin/wrappable.h"
|
||||||
#include "shell/common/gin_helper/error_thrower.h"
|
#include "shell/common/gin_helper/error_thrower.h"
|
||||||
#include "shell/common/gin_helper/wrappable.h"
|
|
||||||
#include "ui/gfx/image/image.h"
|
#include "ui/gfx/image/image.h"
|
||||||
|
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
|
@ -35,11 +35,15 @@ namespace gin_helper {
|
||||||
class Dictionary;
|
class Dictionary;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace gin {
|
||||||
|
class Arguments;
|
||||||
|
}
|
||||||
|
|
||||||
namespace electron {
|
namespace electron {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
class NativeImage : public gin_helper::Wrappable<NativeImage> {
|
class NativeImage : public gin::Wrappable<NativeImage> {
|
||||||
public:
|
public:
|
||||||
static gin::Handle<NativeImage> CreateEmpty(v8::Isolate* isolate);
|
static gin::Handle<NativeImage> CreateEmpty(v8::Isolate* isolate);
|
||||||
static gin::Handle<NativeImage> Create(v8::Isolate* isolate,
|
static gin::Handle<NativeImage> Create(v8::Isolate* isolate,
|
||||||
|
@ -65,8 +69,13 @@ class NativeImage : public gin_helper::Wrappable<NativeImage> {
|
||||||
static gin::Handle<NativeImage> CreateFromNamedImage(gin::Arguments* args,
|
static gin::Handle<NativeImage> CreateFromNamedImage(gin::Arguments* args,
|
||||||
std::string name);
|
std::string name);
|
||||||
|
|
||||||
static void BuildPrototype(v8::Isolate* isolate,
|
static v8::Local<v8::FunctionTemplate> GetConstructor(v8::Isolate* isolate);
|
||||||
v8::Local<v8::FunctionTemplate> prototype);
|
|
||||||
|
// gin::Wrappable
|
||||||
|
static gin::WrapperInfo kWrapperInfo;
|
||||||
|
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||||
|
v8::Isolate* isolate) override;
|
||||||
|
const char* GetTypeName() override;
|
||||||
|
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
HICON GetHICON(int size);
|
HICON GetHICON(int size);
|
||||||
|
@ -109,6 +118,8 @@ class NativeImage : public gin_helper::Wrappable<NativeImage> {
|
||||||
|
|
||||||
gfx::Image image_;
|
gfx::Image image_;
|
||||||
|
|
||||||
|
v8::Isolate* isolate_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(NativeImage);
|
DISALLOW_COPY_AND_ASSIGN(NativeImage);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#import <Cocoa/Cocoa.h>
|
#import <Cocoa/Cocoa.h>
|
||||||
|
|
||||||
#include "base/strings/sys_string_conversions.h"
|
#include "base/strings/sys_string_conversions.h"
|
||||||
|
#include "gin/arguments.h"
|
||||||
#include "ui/gfx/color_utils.h"
|
#include "ui/gfx/color_utils.h"
|
||||||
#include "ui/gfx/image/image.h"
|
#include "ui/gfx/image/image.h"
|
||||||
#include "ui/gfx/image/image_skia.h"
|
#include "ui/gfx/image/image_skia.h"
|
||||||
|
|
Loading…
Reference in a new issue