fix: nativeImage remote serialization (#23543)
We weren't serializing nativeImages properly in the remote module, leading to gin conversion errors when trying to, for example, create a new context menu in the renderer with icons using nativeImage. This fixes that by adding a new special case to handle them.
This commit is contained in:
parent
eb341b383d
commit
ee0f67d541
5 changed files with 100 additions and 14 deletions
|
@ -248,21 +248,32 @@ bool NativeImage::IsEmpty() {
|
|||
return image_.IsEmpty();
|
||||
}
|
||||
|
||||
gfx::Size NativeImage::GetSize() {
|
||||
return image_.Size();
|
||||
gfx::Size NativeImage::GetSize(const base::Optional<float> scale_factor) {
|
||||
float sf = scale_factor.value_or(1.0f);
|
||||
gfx::ImageSkiaRep image_rep = image_.AsImageSkia().GetRepresentation(sf);
|
||||
|
||||
return gfx::Size(image_rep.GetWidth(), image_rep.GetHeight());
|
||||
}
|
||||
|
||||
float NativeImage::GetAspectRatio() {
|
||||
gfx::Size size = GetSize();
|
||||
std::vector<float> NativeImage::GetScaleFactors() {
|
||||
gfx::ImageSkia image_skia = image_.AsImageSkia();
|
||||
return image_skia.GetSupportedScales();
|
||||
}
|
||||
|
||||
float NativeImage::GetAspectRatio(const base::Optional<float> scale_factor) {
|
||||
float sf = scale_factor.value_or(1.0f);
|
||||
gfx::Size size = GetSize(sf);
|
||||
if (size.IsEmpty())
|
||||
return 1.f;
|
||||
else
|
||||
return static_cast<float>(size.width()) / static_cast<float>(size.height());
|
||||
}
|
||||
|
||||
gin::Handle<NativeImage> NativeImage::Resize(v8::Isolate* isolate,
|
||||
gin::Handle<NativeImage> NativeImage::Resize(gin::Arguments* args,
|
||||
base::DictionaryValue options) {
|
||||
gfx::Size size = GetSize();
|
||||
float scale_factor = GetScaleFactorFromOptions(args);
|
||||
|
||||
gfx::Size size = GetSize(scale_factor);
|
||||
int width = size.width();
|
||||
int height = size.height();
|
||||
bool width_set = options.GetInteger("width", &width);
|
||||
|
@ -272,11 +283,12 @@ gin::Handle<NativeImage> NativeImage::Resize(v8::Isolate* isolate,
|
|||
if (width_set && !height_set) {
|
||||
// Scale height to preserve original aspect ratio
|
||||
size.set_height(width);
|
||||
size = gfx::ScaleToRoundedSize(size, 1.f, 1.f / GetAspectRatio());
|
||||
size =
|
||||
gfx::ScaleToRoundedSize(size, 1.f, 1.f / GetAspectRatio(scale_factor));
|
||||
} else if (height_set && !width_set) {
|
||||
// Scale width to preserve original aspect ratio
|
||||
size.set_width(height);
|
||||
size = gfx::ScaleToRoundedSize(size, GetAspectRatio(), 1.f);
|
||||
size = gfx::ScaleToRoundedSize(size, GetAspectRatio(scale_factor), 1.f);
|
||||
}
|
||||
|
||||
skia::ImageOperations::ResizeMethod method =
|
||||
|
@ -290,8 +302,8 @@ gin::Handle<NativeImage> NativeImage::Resize(v8::Isolate* isolate,
|
|||
|
||||
gfx::ImageSkia resized = gfx::ImageSkiaOperations::CreateResizedImage(
|
||||
image_.AsImageSkia(), method, size);
|
||||
return gin::CreateHandle(isolate,
|
||||
new NativeImage(isolate, gfx::Image(resized)));
|
||||
return gin::CreateHandle(
|
||||
args->isolate(), new NativeImage(args->isolate(), gfx::Image(resized)));
|
||||
}
|
||||
|
||||
gin::Handle<NativeImage> NativeImage::Crop(v8::Isolate* isolate,
|
||||
|
@ -505,6 +517,7 @@ void NativeImage::BuildPrototype(v8::Isolate* isolate,
|
|||
.SetMethod("toJPEG", &NativeImage::ToJPEG)
|
||||
.SetMethod("toBitmap", &NativeImage::ToBitmap)
|
||||
.SetMethod("getBitmap", &NativeImage::GetBitmap)
|
||||
.SetMethod("getScaleFactors", &NativeImage::GetScaleFactors)
|
||||
.SetMethod("getNativeHandle", &NativeImage::GetNativeHandle)
|
||||
.SetMethod("toDataURL", &NativeImage::ToDataURL)
|
||||
.SetMethod("isEmpty", &NativeImage::IsEmpty)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue