electron/shell/common/api/electron_api_native_image.cc

641 lines
21 KiB
C++
Raw Normal View History

// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/api/electron_api_native_image.h"
#include <memory>
2015-02-11 13:59:08 +00:00
#include <string>
#include <utility>
2015-02-11 13:59:08 +00:00
#include <vector>
#include "base/files/file_util.h"
2015-09-02 07:16:49 +00:00
#include "base/strings/pattern.h"
2016-08-26 22:30:02 +00:00
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
2020-07-13 21:44:12 +00:00
#include "gin/arguments.h"
#include "gin/object_template_builder.h"
#include "gin/per_isolate_data.h"
2015-02-12 06:27:53 +00:00
#include "net/base/data_url.h"
#include "shell/common/asar/asar_util.h"
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_converters/gfx_converter.h"
#include "shell/common/gin_converters/gurl_converter.h"
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/dictionary.h"
2020-07-13 21:44:12 +00:00
#include "shell/common/gin_helper/function_template_extensions.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/node_includes.h"
#include "shell/common/skia_util.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkImageInfo.h"
2016-08-26 22:30:02 +00:00
#include "third_party/skia/include/core/SkPixelRef.h"
#include "ui/base/layout.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/gfx/codec/png_codec.h"
2015-02-11 08:49:43 +00:00
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_skia.h"
2016-10-04 17:26:01 +00:00
#include "ui/gfx/image/image_skia_operations.h"
2015-02-11 09:47:54 +00:00
#include "ui/gfx/image/image_util.h"
#if defined(OS_WIN)
#include "base/win/scoped_gdi_object.h"
#include "shell/common/asar/archive.h"
#include "ui/gfx/icon_util.h"
#endif
namespace electron {
namespace api {
namespace {
2017-03-07 00:29:39 +00:00
// Get the scale factor from options object at the first argument
float GetScaleFactorFromOptions(gin::Arguments* args) {
2017-03-07 00:29:39 +00:00
float scale_factor = 1.0f;
gin_helper::Dictionary options;
2017-03-07 00:29:39 +00:00
if (args->GetNext(&options))
options.Get("scaleFactor", &scale_factor);
return scale_factor;
}
2016-03-07 22:21:24 +00:00
base::FilePath NormalizePath(const base::FilePath& path) {
if (!path.ReferencesParent()) {
return path;
}
base::ThreadRestrictions::ScopedAllowIO allow_blocking;
base::FilePath absolute_path = MakeAbsoluteFilePath(path);
// MakeAbsoluteFilePath returns an empty path on failures so use original path
if (absolute_path.empty()) {
return path;
} else {
return absolute_path;
}
}
chore: bump chromium to 0e4ca9c0a63d7a39bd910997ad4c6 (master) (#24687) * chore: bump chromium in DEPS to 1f1c4d91f6eaa4a033ec8f499d63a0717f79a42a * viz: Do not apply white level scaling for RGBA fp16 HDR video https://chromium-review.googlesource.com/c/chromium/src/+/2296006 * Move WebPreferences to WebContents https://chromium-review.googlesource.com/c/chromium/src/+/2263635 * Fix missing WeakPtr check in PreconnectManager https://chromium-review.googlesource.com/c/chromium/src/+/2309029 * Fixup swiftshader roll revision * Update patch indices * Move WebDeviceEmulationParams into common. https://chromium-review.googlesource.com/c/chromium/src/+/2303356 * Move EnableDisableDeviceEmulation to blink mojom messages https://chromium-review.googlesource.com/c/chromium/src/+/2303367 * PDF Viewer: Remove flag for two-up view https://chromium-review.googlesource.com/c/chromium/src/+/2311130 * Add mojom definition for DeviceEmulationParams. https://chromium-review.googlesource.com/c/chromium/src/+/2303491 * Remove ServiceWorkerContextWatcher from PaymentAppInstaller https://chromium-review.googlesource.com/c/chromium/src/+/2291186 * Loader: Move transferrable_url_loader.mojom into blink's mojom directory https://chromium-review.googlesource.com/c/chromium/src/+/2306123 * chore: bump chromium in DEPS to 4974f436479739025a90ebc2cc2e36d67ee1ac46 * mac: Work around Xcode 12b3 SDK bug https://chromium-review.googlesource.com/c/chromium/src/+/2315078 * Reland Update core items for macOS Big Sur. https://chromium-review.googlesource.com/c/chromium/src/+/2315162 * Update Swiftshader revision * mac/arm64: When cross-building the snapshot, use page size of the target ISA instead of the host. https://chromium-review.googlesource.com/c/v8/v8/+/2310575 * Update patch indices * Rename {,Non}ClientView::CanClose() to OnWindowCloseRequested() https://chromium-review.googlesource.com/c/chromium/src/+/2247838 * chore: bump chromium in DEPS to e9465d70d1dea539400f0fddad43358ea3c31d71 * chore: bump chromium in DEPS to bd5b71c5f20288eb26068a39ae6e0579566a51c5 * chore: bump chromium in DEPS to 786ee543048bd07d07c5ac50b7dbbdd6bdd8dcce * chore: bump chromium in DEPS to 34eb6ecbf2c5894b648900bf771a2a29de204798 * chore: bump chromium in DEPS to 567ff038d68e3adb8116a01eec863cdf34d775f5 * chore: bump chromium in DEPS to 340b45c8d4ceb2dd61969fc34e1928d3c46db48c * chore: update patches * chore: base::DeleteFile with two params is removed Should use base::DeleteFile and base::DeletePathRecursively when appropriate Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2313376 * chore: add patch for NodePlatform::PostJob impl * chore: update patches * chore: extension file access is now instrumented Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2209995 * chore: implement SetWindowFrameInScreen in OSR RWHV Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2321409 * chore: NotifyUserActivation requires a type now This is just for a histogram thing and therefore it does not matter what we pass in Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2281303 * chore: update patches * chore: bump chromium in DEPS to cd570e6dd3dcb84463ac252b04e92ceb02d8400c * chore: update patches * chore: bump chromium in DEPS to 0187908a31866992b90c59719ac1d016328f6ee0 * chore: bump chromium in DEPS to 3c9df38c508f3dba26a75248beed4882ddfb98e9 * chore: bump chromium in DEPS to 1a47d3b9cee710bd3c958c4f2d8b205710df9d50 * chore: bump chromium in DEPS to baac93040d96abdab72d46dd034c60f86e108702 * chore: bump chromium in DEPS to 13836145f97299e636491de38064b78861c4fb2e * update patches * change OS_MACOSX -> OS_MAC Refs: https://bugs.chromium.org/p/chromium/issues/detail?id=1105907 * patch: add header for ToExecutionContext in WebMessagePortConverter * chore: bump chromium in DEPS to 91ab9b6ac5d04dc034a03ad847fbfa8261328c2b * update patches * NeedToFireBeforeUnloadOrUnload -> NeedToFireBeforeUnloadOrUnloadEvents Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2288711 * chore: bump chromium in DEPS to 290deb11f0e30cb1382fd8f8793d340560283c23 * update patches * add dragdrop header for autofill popup * int -> x11::Time * patch out accessibility private API use Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2330812 * remove usage of XEvent Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2317767 * trigger recalculation of WebPreferences before renderer initialization Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2263635 * chore: bump chromium in DEPS to 6bdb484583b99c96ef3388d0c2184326581b2d5a * chore: bump chromium in DEPS to 1eb2a79cde04fd5c8ae51b4d813e6521635269e5 * chore: bump chromium in DEPS to 3dc8e3c0f400e4ca9c0a63d7a39bd910997ad4c6 * chore: update patches * fixup! trigger recalculation of WebPreferences before renderer initialization * views: Make MenuButton and RadioButton default constructible https://chromium-review.googlesource.com/c/chromium/src/+/2339586 * chore: fix code style Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: Samuel Attard <sattard@slack-corp.com> Co-authored-by: Andy Locascio <andy@slack-corp.com> Co-authored-by: deepak1556 <hop2deep@gmail.com>
2020-08-12 18:33:58 +00:00
#if defined(OS_MAC)
bool IsTemplateFilename(const base::FilePath& path) {
2015-09-02 07:16:49 +00:00
return (base::MatchPattern(path.value(), "*Template.*") ||
base::MatchPattern(path.value(), "*Template@*x.*"));
}
#endif
#if defined(OS_WIN)
base::win::ScopedHICON ReadICOFromPath(int size, const base::FilePath& path) {
// If file is in asar archive, we extract it to a temp file so LoadImage can
// load it.
base::FilePath asar_path, relative_path;
base::FilePath image_path(path);
if (asar::GetAsarArchivePath(image_path, &asar_path, &relative_path)) {
std::shared_ptr<asar::Archive> archive =
asar::GetOrCreateAsarArchive(asar_path);
if (archive)
archive->CopyFileOut(relative_path, &image_path);
}
// Load the icon from file.
2018-04-18 01:55:30 +00:00
return base::win::ScopedHICON(
static_cast<HICON>(LoadImage(NULL, image_path.value().c_str(), IMAGE_ICON,
size, size, LR_LOADFROMFILE)));
2016-05-20 02:31:02 +00:00
}
#endif
} // namespace
2016-04-25 01:17:54 +00:00
NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
2020-07-13 21:44:12 +00:00
: image_(image), isolate_(isolate) {
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
2020-07-13 21:44:12 +00:00
isolate_->AdjustAmountOfExternalAllocatedMemory(
2018-04-18 01:55:30 +00:00
image_.ToImageSkia()->bitmap()->computeByteSize());
}
2016-04-25 01:17:54 +00:00
}
2016-05-20 02:31:02 +00:00
#if defined(OS_WIN)
NativeImage::NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path)
2020-07-13 21:44:12 +00:00
: hicon_path_(hicon_path), isolate_(isolate) {
// Use the 256x256 icon as fallback icon.
gfx::ImageSkia image_skia;
electron::util::ReadImageSkiaFromICO(&image_skia, GetHICON(256));
image_ = gfx::Image(image_skia);
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
2020-07-13 21:44:12 +00:00
isolate_->AdjustAmountOfExternalAllocatedMemory(
2018-04-18 01:55:30 +00:00
image_.ToImageSkia()->bitmap()->computeByteSize());
}
2016-05-20 02:31:02 +00:00
}
#endif
NativeImage::~NativeImage() {
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
2020-07-13 21:44:12 +00:00
isolate_->AdjustAmountOfExternalAllocatedMemory(-static_cast<int64_t>(
2018-04-18 01:55:30 +00:00
image_.ToImageSkia()->bitmap()->computeByteSize()));
}
}
2016-05-20 07:55:22 +00:00
#if defined(OS_WIN)
HICON NativeImage::GetHICON(int size) {
auto iter = hicons_.find(size);
if (iter != hicons_.end())
return iter->second.get();
// First try loading the icon with specified size.
if (!hicon_path_.empty()) {
hicons_[size] = ReadICOFromPath(size, hicon_path_);
return hicons_[size].get();
}
// Then convert the image to ICO.
2016-05-20 07:55:22 +00:00
if (image_.IsEmpty())
return NULL;
hicons_[size] = IconUtil::CreateHICONFromSkBitmap(image_.AsBitmap());
return hicons_[size].get();
2016-05-20 07:55:22 +00:00
}
#endif
v8::Local<v8::Value> NativeImage::ToPNG(gin::Arguments* args) {
2017-03-07 00:29:39 +00:00
float scale_factor = GetScaleFactorFromOptions(args);
2017-03-07 00:19:16 +00:00
if (scale_factor == 1.0f) {
// Use raw 1x PNG bytes when available
scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes();
if (png->size() > 0) {
const char* data = reinterpret_cast<const char*>(png->front());
size_t size = png->size();
return node::Buffer::Copy(args->isolate(), data, size).ToLocalChecked();
}
}
2017-03-07 00:19:16 +00:00
const SkBitmap bitmap =
image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap();
2017-07-31 06:45:59 +00:00
std::vector<unsigned char> encoded;
gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &encoded);
const char* data = reinterpret_cast<char*>(encoded.data());
size_t size = encoded.size();
2017-03-07 00:19:16 +00:00
return node::Buffer::Copy(args->isolate(), data, size).ToLocalChecked();
}
v8::Local<v8::Value> NativeImage::ToBitmap(gin::Arguments* args) {
2017-03-07 00:29:39 +00:00
float scale_factor = GetScaleFactorFromOptions(args);
2017-03-07 00:19:16 +00:00
const SkBitmap bitmap =
image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap();
SkImageInfo info =
SkImageInfo::MakeN32Premul(bitmap.width(), bitmap.height());
auto array_buffer =
v8::ArrayBuffer::New(args->isolate(), info.computeMinByteSize());
auto backing_store = array_buffer->GetBackingStore();
if (bitmap.readPixels(info, backing_store->Data(), info.minRowBytes(), 0,
0)) {
return node::Buffer::New(args->isolate(), array_buffer, 0,
info.computeMinByteSize())
.ToLocalChecked();
}
return node::Buffer::New(args->isolate(), 0).ToLocalChecked();
}
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> NativeImage::ToJPEG(v8::Isolate* isolate, int quality) {
2015-02-11 09:47:54 +00:00
std::vector<unsigned char> output;
gfx::JPEG1xEncodedDataFromImage(image_, quality, &output);
if (output.empty())
return node::Buffer::New(isolate, 0).ToLocalChecked();
2018-04-18 01:55:30 +00:00
return node::Buffer::Copy(isolate,
reinterpret_cast<const char*>(&output.front()),
output.size())
.ToLocalChecked();
2015-02-11 09:47:54 +00:00
}
std::string NativeImage::ToDataURL(gin::Arguments* args) {
2017-03-07 00:29:39 +00:00
float scale_factor = GetScaleFactorFromOptions(args);
2017-03-07 00:19:16 +00:00
if (scale_factor == 1.0f) {
// Use raw 1x PNG bytes when available
scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes();
if (png->size() > 0)
return webui::GetPngDataUrl(png->front(), png->size());
}
2015-02-12 06:32:51 +00:00
2017-03-07 00:19:16 +00:00
return webui::GetBitmapDataUrl(
image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap());
2017-03-07 00:19:16 +00:00
}
void SkUnref(char* data, void* hint) {
reinterpret_cast<SkRefCnt*>(hint)->unref();
}
v8::Local<v8::Value> NativeImage::GetBitmap(gin::Arguments* args) {
2017-03-07 00:29:39 +00:00
float scale_factor = GetScaleFactorFromOptions(args);
2017-03-07 00:19:16 +00:00
const SkBitmap bitmap =
image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap();
2017-03-07 00:19:16 +00:00
SkPixelRef* ref = bitmap.pixelRef();
if (!ref)
return node::Buffer::New(args->isolate(), 0).ToLocalChecked();
ref->ref();
2017-03-07 00:19:16 +00:00
return node::Buffer::New(args->isolate(),
2016-08-05 08:55:57 +00:00
reinterpret_cast<char*>(ref->pixels()),
bitmap.computeByteSize(), &SkUnref, ref)
2018-04-18 01:55:30 +00:00
.ToLocalChecked();
2016-08-05 08:55:57 +00:00
}
v8::Local<v8::Value> NativeImage::GetNativeHandle(
gin_helper::ErrorThrower thrower) {
chore: bump chromium to 0e4ca9c0a63d7a39bd910997ad4c6 (master) (#24687) * chore: bump chromium in DEPS to 1f1c4d91f6eaa4a033ec8f499d63a0717f79a42a * viz: Do not apply white level scaling for RGBA fp16 HDR video https://chromium-review.googlesource.com/c/chromium/src/+/2296006 * Move WebPreferences to WebContents https://chromium-review.googlesource.com/c/chromium/src/+/2263635 * Fix missing WeakPtr check in PreconnectManager https://chromium-review.googlesource.com/c/chromium/src/+/2309029 * Fixup swiftshader roll revision * Update patch indices * Move WebDeviceEmulationParams into common. https://chromium-review.googlesource.com/c/chromium/src/+/2303356 * Move EnableDisableDeviceEmulation to blink mojom messages https://chromium-review.googlesource.com/c/chromium/src/+/2303367 * PDF Viewer: Remove flag for two-up view https://chromium-review.googlesource.com/c/chromium/src/+/2311130 * Add mojom definition for DeviceEmulationParams. https://chromium-review.googlesource.com/c/chromium/src/+/2303491 * Remove ServiceWorkerContextWatcher from PaymentAppInstaller https://chromium-review.googlesource.com/c/chromium/src/+/2291186 * Loader: Move transferrable_url_loader.mojom into blink's mojom directory https://chromium-review.googlesource.com/c/chromium/src/+/2306123 * chore: bump chromium in DEPS to 4974f436479739025a90ebc2cc2e36d67ee1ac46 * mac: Work around Xcode 12b3 SDK bug https://chromium-review.googlesource.com/c/chromium/src/+/2315078 * Reland Update core items for macOS Big Sur. https://chromium-review.googlesource.com/c/chromium/src/+/2315162 * Update Swiftshader revision * mac/arm64: When cross-building the snapshot, use page size of the target ISA instead of the host. https://chromium-review.googlesource.com/c/v8/v8/+/2310575 * Update patch indices * Rename {,Non}ClientView::CanClose() to OnWindowCloseRequested() https://chromium-review.googlesource.com/c/chromium/src/+/2247838 * chore: bump chromium in DEPS to e9465d70d1dea539400f0fddad43358ea3c31d71 * chore: bump chromium in DEPS to bd5b71c5f20288eb26068a39ae6e0579566a51c5 * chore: bump chromium in DEPS to 786ee543048bd07d07c5ac50b7dbbdd6bdd8dcce * chore: bump chromium in DEPS to 34eb6ecbf2c5894b648900bf771a2a29de204798 * chore: bump chromium in DEPS to 567ff038d68e3adb8116a01eec863cdf34d775f5 * chore: bump chromium in DEPS to 340b45c8d4ceb2dd61969fc34e1928d3c46db48c * chore: update patches * chore: base::DeleteFile with two params is removed Should use base::DeleteFile and base::DeletePathRecursively when appropriate Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2313376 * chore: add patch for NodePlatform::PostJob impl * chore: update patches * chore: extension file access is now instrumented Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2209995 * chore: implement SetWindowFrameInScreen in OSR RWHV Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2321409 * chore: NotifyUserActivation requires a type now This is just for a histogram thing and therefore it does not matter what we pass in Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2281303 * chore: update patches * chore: bump chromium in DEPS to cd570e6dd3dcb84463ac252b04e92ceb02d8400c * chore: update patches * chore: bump chromium in DEPS to 0187908a31866992b90c59719ac1d016328f6ee0 * chore: bump chromium in DEPS to 3c9df38c508f3dba26a75248beed4882ddfb98e9 * chore: bump chromium in DEPS to 1a47d3b9cee710bd3c958c4f2d8b205710df9d50 * chore: bump chromium in DEPS to baac93040d96abdab72d46dd034c60f86e108702 * chore: bump chromium in DEPS to 13836145f97299e636491de38064b78861c4fb2e * update patches * change OS_MACOSX -> OS_MAC Refs: https://bugs.chromium.org/p/chromium/issues/detail?id=1105907 * patch: add header for ToExecutionContext in WebMessagePortConverter * chore: bump chromium in DEPS to 91ab9b6ac5d04dc034a03ad847fbfa8261328c2b * update patches * NeedToFireBeforeUnloadOrUnload -> NeedToFireBeforeUnloadOrUnloadEvents Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2288711 * chore: bump chromium in DEPS to 290deb11f0e30cb1382fd8f8793d340560283c23 * update patches * add dragdrop header for autofill popup * int -> x11::Time * patch out accessibility private API use Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2330812 * remove usage of XEvent Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2317767 * trigger recalculation of WebPreferences before renderer initialization Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2263635 * chore: bump chromium in DEPS to 6bdb484583b99c96ef3388d0c2184326581b2d5a * chore: bump chromium in DEPS to 1eb2a79cde04fd5c8ae51b4d813e6521635269e5 * chore: bump chromium in DEPS to 3dc8e3c0f400e4ca9c0a63d7a39bd910997ad4c6 * chore: update patches * fixup! trigger recalculation of WebPreferences before renderer initialization * views: Make MenuButton and RadioButton default constructible https://chromium-review.googlesource.com/c/chromium/src/+/2339586 * chore: fix code style Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: Samuel Attard <sattard@slack-corp.com> Co-authored-by: Andy Locascio <andy@slack-corp.com> Co-authored-by: deepak1556 <hop2deep@gmail.com>
2020-08-12 18:33:58 +00:00
#if defined(OS_MAC)
2018-04-18 01:55:30 +00:00
if (IsEmpty())
return node::Buffer::New(thrower.isolate(), 0).ToLocalChecked();
NSImage* ptr = image_.AsNSImage();
return node::Buffer::Copy(thrower.isolate(), reinterpret_cast<char*>(ptr),
2018-04-18 01:55:30 +00:00
sizeof(void*))
.ToLocalChecked();
2016-03-15 02:48:40 +00:00
#else
thrower.ThrowError("Not implemented");
return v8::Undefined(thrower.isolate());
#endif
}
2015-02-11 08:49:43 +00:00
bool NativeImage::IsEmpty() {
return image_.IsEmpty();
}
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());
}
std::vector<float> NativeImage::GetScaleFactors() {
gfx::ImageSkia image_skia = image_.AsImageSkia();
return image_skia.GetSupportedScales();
2015-02-11 08:49:43 +00:00
}
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(gin::Arguments* args,
base::DictionaryValue options) {
float scale_factor = GetScaleFactorFromOptions(args);
gfx::Size size = GetSize(scale_factor);
2016-10-04 17:26:01 +00:00
int width = size.width();
int height = size.height();
bool width_set = options.GetInteger("width", &width);
bool height_set = options.GetInteger("height", &height);
size.SetSize(width, height);
2017-04-12 18:54:03 +00:00
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(scale_factor));
} else if (height_set && !width_set) {
// Scale width to preserve original aspect ratio
size.set_width(height);
size = gfx::ScaleToRoundedSize(size, GetAspectRatio(scale_factor), 1.f);
}
2016-10-04 17:26:01 +00:00
skia::ImageOperations::ResizeMethod method =
skia::ImageOperations::ResizeMethod::RESIZE_BEST;
std::string quality;
options.GetString("quality", &quality);
if (quality == "good")
method = skia::ImageOperations::ResizeMethod::RESIZE_GOOD;
else if (quality == "better")
method = skia::ImageOperations::ResizeMethod::RESIZE_BETTER;
gfx::ImageSkia resized = gfx::ImageSkiaOperations::CreateResizedImage(
image_.AsImageSkia(), method, size);
return gin::CreateHandle(
args->isolate(), new NativeImage(args->isolate(), gfx::Image(resized)));
2016-10-04 17:26:01 +00:00
}
gin::Handle<NativeImage> NativeImage::Crop(v8::Isolate* isolate,
const gfx::Rect& rect) {
2018-04-18 01:55:30 +00:00
gfx::ImageSkia cropped =
gfx::ImageSkiaOperations::ExtractSubset(image_.AsImageSkia(), rect);
return gin::CreateHandle(isolate,
new NativeImage(isolate, gfx::Image(cropped)));
2016-10-04 18:23:18 +00:00
}
void NativeImage::AddRepresentation(const gin_helper::Dictionary& options) {
2017-03-07 21:48:12 +00:00
int width = 0;
int height = 0;
float scale_factor = 1.0f;
options.Get("width", &width);
options.Get("height", &height);
options.Get("scaleFactor", &scale_factor);
bool skia_rep_added = false;
gfx::ImageSkia image_skia = image_.AsImageSkia();
2017-03-07 21:48:12 +00:00
v8::Local<v8::Value> buffer;
GURL url;
2017-03-07 21:48:12 +00:00
if (options.Get("buffer", &buffer) && node::Buffer::HasInstance(buffer)) {
auto* data = reinterpret_cast<unsigned char*>(node::Buffer::Data(buffer));
auto size = node::Buffer::Length(buffer);
skia_rep_added = electron::util::AddImageSkiaRepFromBuffer(
&image_skia, data, size, width, height, scale_factor);
} else if (options.Get("dataURL", &url)) {
std::string mime_type, charset, data;
if (net::DataURL::Parse(url, &mime_type, &charset, &data)) {
auto* data_ptr = reinterpret_cast<const unsigned char*>(data.c_str());
if (mime_type == "image/png") {
skia_rep_added = electron::util::AddImageSkiaRepFromPNG(
&image_skia, data_ptr, data.size(), scale_factor);
} else if (mime_type == "image/jpeg") {
skia_rep_added = electron::util::AddImageSkiaRepFromJPEG(
&image_skia, data_ptr, data.size(), scale_factor);
}
2017-03-07 21:48:12 +00:00
}
}
// Re-initialize image when first representation is added to an empty image
if (skia_rep_added && IsEmpty()) {
gfx::Image image(image_skia);
image_ = std::move(image);
}
2017-03-07 21:48:12 +00:00
}
2016-10-04 18:23:18 +00:00
chore: bump chromium to 0e4ca9c0a63d7a39bd910997ad4c6 (master) (#24687) * chore: bump chromium in DEPS to 1f1c4d91f6eaa4a033ec8f499d63a0717f79a42a * viz: Do not apply white level scaling for RGBA fp16 HDR video https://chromium-review.googlesource.com/c/chromium/src/+/2296006 * Move WebPreferences to WebContents https://chromium-review.googlesource.com/c/chromium/src/+/2263635 * Fix missing WeakPtr check in PreconnectManager https://chromium-review.googlesource.com/c/chromium/src/+/2309029 * Fixup swiftshader roll revision * Update patch indices * Move WebDeviceEmulationParams into common. https://chromium-review.googlesource.com/c/chromium/src/+/2303356 * Move EnableDisableDeviceEmulation to blink mojom messages https://chromium-review.googlesource.com/c/chromium/src/+/2303367 * PDF Viewer: Remove flag for two-up view https://chromium-review.googlesource.com/c/chromium/src/+/2311130 * Add mojom definition for DeviceEmulationParams. https://chromium-review.googlesource.com/c/chromium/src/+/2303491 * Remove ServiceWorkerContextWatcher from PaymentAppInstaller https://chromium-review.googlesource.com/c/chromium/src/+/2291186 * Loader: Move transferrable_url_loader.mojom into blink's mojom directory https://chromium-review.googlesource.com/c/chromium/src/+/2306123 * chore: bump chromium in DEPS to 4974f436479739025a90ebc2cc2e36d67ee1ac46 * mac: Work around Xcode 12b3 SDK bug https://chromium-review.googlesource.com/c/chromium/src/+/2315078 * Reland Update core items for macOS Big Sur. https://chromium-review.googlesource.com/c/chromium/src/+/2315162 * Update Swiftshader revision * mac/arm64: When cross-building the snapshot, use page size of the target ISA instead of the host. https://chromium-review.googlesource.com/c/v8/v8/+/2310575 * Update patch indices * Rename {,Non}ClientView::CanClose() to OnWindowCloseRequested() https://chromium-review.googlesource.com/c/chromium/src/+/2247838 * chore: bump chromium in DEPS to e9465d70d1dea539400f0fddad43358ea3c31d71 * chore: bump chromium in DEPS to bd5b71c5f20288eb26068a39ae6e0579566a51c5 * chore: bump chromium in DEPS to 786ee543048bd07d07c5ac50b7dbbdd6bdd8dcce * chore: bump chromium in DEPS to 34eb6ecbf2c5894b648900bf771a2a29de204798 * chore: bump chromium in DEPS to 567ff038d68e3adb8116a01eec863cdf34d775f5 * chore: bump chromium in DEPS to 340b45c8d4ceb2dd61969fc34e1928d3c46db48c * chore: update patches * chore: base::DeleteFile with two params is removed Should use base::DeleteFile and base::DeletePathRecursively when appropriate Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2313376 * chore: add patch for NodePlatform::PostJob impl * chore: update patches * chore: extension file access is now instrumented Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2209995 * chore: implement SetWindowFrameInScreen in OSR RWHV Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2321409 * chore: NotifyUserActivation requires a type now This is just for a histogram thing and therefore it does not matter what we pass in Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2281303 * chore: update patches * chore: bump chromium in DEPS to cd570e6dd3dcb84463ac252b04e92ceb02d8400c * chore: update patches * chore: bump chromium in DEPS to 0187908a31866992b90c59719ac1d016328f6ee0 * chore: bump chromium in DEPS to 3c9df38c508f3dba26a75248beed4882ddfb98e9 * chore: bump chromium in DEPS to 1a47d3b9cee710bd3c958c4f2d8b205710df9d50 * chore: bump chromium in DEPS to baac93040d96abdab72d46dd034c60f86e108702 * chore: bump chromium in DEPS to 13836145f97299e636491de38064b78861c4fb2e * update patches * change OS_MACOSX -> OS_MAC Refs: https://bugs.chromium.org/p/chromium/issues/detail?id=1105907 * patch: add header for ToExecutionContext in WebMessagePortConverter * chore: bump chromium in DEPS to 91ab9b6ac5d04dc034a03ad847fbfa8261328c2b * update patches * NeedToFireBeforeUnloadOrUnload -> NeedToFireBeforeUnloadOrUnloadEvents Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2288711 * chore: bump chromium in DEPS to 290deb11f0e30cb1382fd8f8793d340560283c23 * update patches * add dragdrop header for autofill popup * int -> x11::Time * patch out accessibility private API use Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2330812 * remove usage of XEvent Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2317767 * trigger recalculation of WebPreferences before renderer initialization Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2263635 * chore: bump chromium in DEPS to 6bdb484583b99c96ef3388d0c2184326581b2d5a * chore: bump chromium in DEPS to 1eb2a79cde04fd5c8ae51b4d813e6521635269e5 * chore: bump chromium in DEPS to 3dc8e3c0f400e4ca9c0a63d7a39bd910997ad4c6 * chore: update patches * fixup! trigger recalculation of WebPreferences before renderer initialization * views: Make MenuButton and RadioButton default constructible https://chromium-review.googlesource.com/c/chromium/src/+/2339586 * chore: fix code style Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: Samuel Attard <sattard@slack-corp.com> Co-authored-by: Andy Locascio <andy@slack-corp.com> Co-authored-by: deepak1556 <hop2deep@gmail.com>
2020-08-12 18:33:58 +00:00
#if !defined(OS_MAC)
2018-04-18 01:55:30 +00:00
void NativeImage::SetTemplateImage(bool setAsTemplate) {}
2015-07-29 03:48:40 +00:00
bool NativeImage::IsTemplateImage() {
2015-07-29 04:36:01 +00:00
return false;
}
#endif
2015-02-12 05:55:45 +00:00
// static
gin::Handle<NativeImage> NativeImage::CreateEmpty(v8::Isolate* isolate) {
return gin::CreateHandle(isolate, new NativeImage(isolate, gfx::Image()));
2015-02-12 05:55:45 +00:00
}
// static
gin::Handle<NativeImage> NativeImage::Create(v8::Isolate* isolate,
const gfx::Image& image) {
return gin::CreateHandle(isolate, new NativeImage(isolate, image));
}
// static
gin::Handle<NativeImage> NativeImage::CreateFromPNG(v8::Isolate* isolate,
const char* buffer,
size_t length) {
gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(
2015-02-12 06:27:53 +00:00
reinterpret_cast<const unsigned char*>(buffer), length);
return Create(isolate, image);
}
// static
gin::Handle<NativeImage> NativeImage::CreateFromJPEG(v8::Isolate* isolate,
const char* buffer,
size_t length) {
gfx::Image image = gfx::ImageFrom1xJPEGEncodedData(
2015-02-12 06:27:53 +00:00
reinterpret_cast<const unsigned char*>(buffer), length);
return Create(isolate, image);
}
// static
gin::Handle<NativeImage> NativeImage::CreateFromPath(
2018-04-18 01:55:30 +00:00
v8::Isolate* isolate,
const base::FilePath& path) {
2016-03-07 22:22:16 +00:00
base::FilePath image_path = NormalizePath(path);
#if defined(OS_WIN)
2016-05-20 13:22:15 +00:00
if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
return gin::CreateHandle(isolate, new NativeImage(isolate, image_path));
2016-05-20 13:22:15 +00:00
}
#endif
2016-05-20 13:22:15 +00:00
gfx::ImageSkia image_skia;
electron::util::PopulateImageSkiaRepsFromPath(&image_skia, image_path);
2016-05-20 13:22:15 +00:00
gfx::Image image(image_skia);
gin::Handle<NativeImage> handle = Create(isolate, image);
chore: bump chromium to 0e4ca9c0a63d7a39bd910997ad4c6 (master) (#24687) * chore: bump chromium in DEPS to 1f1c4d91f6eaa4a033ec8f499d63a0717f79a42a * viz: Do not apply white level scaling for RGBA fp16 HDR video https://chromium-review.googlesource.com/c/chromium/src/+/2296006 * Move WebPreferences to WebContents https://chromium-review.googlesource.com/c/chromium/src/+/2263635 * Fix missing WeakPtr check in PreconnectManager https://chromium-review.googlesource.com/c/chromium/src/+/2309029 * Fixup swiftshader roll revision * Update patch indices * Move WebDeviceEmulationParams into common. https://chromium-review.googlesource.com/c/chromium/src/+/2303356 * Move EnableDisableDeviceEmulation to blink mojom messages https://chromium-review.googlesource.com/c/chromium/src/+/2303367 * PDF Viewer: Remove flag for two-up view https://chromium-review.googlesource.com/c/chromium/src/+/2311130 * Add mojom definition for DeviceEmulationParams. https://chromium-review.googlesource.com/c/chromium/src/+/2303491 * Remove ServiceWorkerContextWatcher from PaymentAppInstaller https://chromium-review.googlesource.com/c/chromium/src/+/2291186 * Loader: Move transferrable_url_loader.mojom into blink's mojom directory https://chromium-review.googlesource.com/c/chromium/src/+/2306123 * chore: bump chromium in DEPS to 4974f436479739025a90ebc2cc2e36d67ee1ac46 * mac: Work around Xcode 12b3 SDK bug https://chromium-review.googlesource.com/c/chromium/src/+/2315078 * Reland Update core items for macOS Big Sur. https://chromium-review.googlesource.com/c/chromium/src/+/2315162 * Update Swiftshader revision * mac/arm64: When cross-building the snapshot, use page size of the target ISA instead of the host. https://chromium-review.googlesource.com/c/v8/v8/+/2310575 * Update patch indices * Rename {,Non}ClientView::CanClose() to OnWindowCloseRequested() https://chromium-review.googlesource.com/c/chromium/src/+/2247838 * chore: bump chromium in DEPS to e9465d70d1dea539400f0fddad43358ea3c31d71 * chore: bump chromium in DEPS to bd5b71c5f20288eb26068a39ae6e0579566a51c5 * chore: bump chromium in DEPS to 786ee543048bd07d07c5ac50b7dbbdd6bdd8dcce * chore: bump chromium in DEPS to 34eb6ecbf2c5894b648900bf771a2a29de204798 * chore: bump chromium in DEPS to 567ff038d68e3adb8116a01eec863cdf34d775f5 * chore: bump chromium in DEPS to 340b45c8d4ceb2dd61969fc34e1928d3c46db48c * chore: update patches * chore: base::DeleteFile with two params is removed Should use base::DeleteFile and base::DeletePathRecursively when appropriate Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2313376 * chore: add patch for NodePlatform::PostJob impl * chore: update patches * chore: extension file access is now instrumented Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2209995 * chore: implement SetWindowFrameInScreen in OSR RWHV Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2321409 * chore: NotifyUserActivation requires a type now This is just for a histogram thing and therefore it does not matter what we pass in Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2281303 * chore: update patches * chore: bump chromium in DEPS to cd570e6dd3dcb84463ac252b04e92ceb02d8400c * chore: update patches * chore: bump chromium in DEPS to 0187908a31866992b90c59719ac1d016328f6ee0 * chore: bump chromium in DEPS to 3c9df38c508f3dba26a75248beed4882ddfb98e9 * chore: bump chromium in DEPS to 1a47d3b9cee710bd3c958c4f2d8b205710df9d50 * chore: bump chromium in DEPS to baac93040d96abdab72d46dd034c60f86e108702 * chore: bump chromium in DEPS to 13836145f97299e636491de38064b78861c4fb2e * update patches * change OS_MACOSX -> OS_MAC Refs: https://bugs.chromium.org/p/chromium/issues/detail?id=1105907 * patch: add header for ToExecutionContext in WebMessagePortConverter * chore: bump chromium in DEPS to 91ab9b6ac5d04dc034a03ad847fbfa8261328c2b * update patches * NeedToFireBeforeUnloadOrUnload -> NeedToFireBeforeUnloadOrUnloadEvents Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2288711 * chore: bump chromium in DEPS to 290deb11f0e30cb1382fd8f8793d340560283c23 * update patches * add dragdrop header for autofill popup * int -> x11::Time * patch out accessibility private API use Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2330812 * remove usage of XEvent Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2317767 * trigger recalculation of WebPreferences before renderer initialization Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2263635 * chore: bump chromium in DEPS to 6bdb484583b99c96ef3388d0c2184326581b2d5a * chore: bump chromium in DEPS to 1eb2a79cde04fd5c8ae51b4d813e6521635269e5 * chore: bump chromium in DEPS to 3dc8e3c0f400e4ca9c0a63d7a39bd910997ad4c6 * chore: update patches * fixup! trigger recalculation of WebPreferences before renderer initialization * views: Make MenuButton and RadioButton default constructible https://chromium-review.googlesource.com/c/chromium/src/+/2339586 * chore: fix code style Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: Samuel Attard <sattard@slack-corp.com> Co-authored-by: Andy Locascio <andy@slack-corp.com> Co-authored-by: deepak1556 <hop2deep@gmail.com>
2020-08-12 18:33:58 +00:00
#if defined(OS_MAC)
2016-05-20 13:22:15 +00:00
if (IsTemplateFilename(image_path))
handle->SetTemplateImage(true);
#endif
2016-05-20 13:22:15 +00:00
return handle;
}
// static
gin::Handle<NativeImage> NativeImage::CreateFromBitmap(
gin_helper::ErrorThrower thrower,
v8::Local<v8::Value> buffer,
const gin_helper::Dictionary& options) {
if (!node::Buffer::HasInstance(buffer)) {
thrower.ThrowError("buffer must be a node Buffer");
return gin::Handle<NativeImage>();
}
unsigned int width = 0;
unsigned int height = 0;
double scale_factor = 1.;
if (!options.Get("width", &width)) {
thrower.ThrowError("width is required");
return gin::Handle<NativeImage>();
}
if (!options.Get("height", &height)) {
thrower.ThrowError("height is required");
return gin::Handle<NativeImage>();
}
auto info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
auto size_bytes = info.computeMinByteSize();
if (size_bytes != node::Buffer::Length(buffer)) {
thrower.ThrowError("invalid buffer size");
return gin::Handle<NativeImage>();
}
options.Get("scaleFactor", &scale_factor);
if (width == 0 || height == 0) {
return CreateEmpty(thrower.isolate());
}
SkBitmap bitmap;
bitmap.allocN32Pixels(width, height, false);
bitmap.writePixels({info, node::Buffer::Data(buffer), bitmap.rowBytes()});
gfx::ImageSkia image_skia;
image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale_factor));
return Create(thrower.isolate(), gfx::Image(image_skia));
}
2015-02-12 07:19:05 +00:00
// static
gin::Handle<NativeImage> NativeImage::CreateFromBuffer(
gin_helper::ErrorThrower thrower,
v8::Local<v8::Value> buffer,
gin::Arguments* args) {
if (!node::Buffer::HasInstance(buffer)) {
thrower.ThrowError("buffer must be a node Buffer");
return gin::Handle<NativeImage>();
}
int width = 0;
int height = 0;
2015-02-12 07:19:05 +00:00
double scale_factor = 1.;
gin_helper::Dictionary options;
if (args->GetNext(&options)) {
options.Get("width", &width);
options.Get("height", &height);
options.Get("scaleFactor", &scale_factor);
}
2015-02-12 07:19:05 +00:00
gfx::ImageSkia image_skia;
electron::util::AddImageSkiaRepFromBuffer(
&image_skia, reinterpret_cast<unsigned char*>(node::Buffer::Data(buffer)),
node::Buffer::Length(buffer), width, height, scale_factor);
2015-02-12 07:19:05 +00:00
return Create(args->isolate(), gfx::Image(image_skia));
}
2015-02-12 06:27:53 +00:00
// static
gin::Handle<NativeImage> NativeImage::CreateFromDataURL(v8::Isolate* isolate,
const GURL& url) {
2015-02-12 06:27:53 +00:00
std::string mime_type, charset, data;
if (net::DataURL::Parse(url, &mime_type, &charset, &data)) {
if (mime_type == "image/png")
return CreateFromPNG(isolate, data.c_str(), data.size());
else if (mime_type == "image/jpeg")
return CreateFromJPEG(isolate, data.c_str(), data.size());
}
return CreateEmpty(isolate);
}
chore: bump chromium to 0e4ca9c0a63d7a39bd910997ad4c6 (master) (#24687) * chore: bump chromium in DEPS to 1f1c4d91f6eaa4a033ec8f499d63a0717f79a42a * viz: Do not apply white level scaling for RGBA fp16 HDR video https://chromium-review.googlesource.com/c/chromium/src/+/2296006 * Move WebPreferences to WebContents https://chromium-review.googlesource.com/c/chromium/src/+/2263635 * Fix missing WeakPtr check in PreconnectManager https://chromium-review.googlesource.com/c/chromium/src/+/2309029 * Fixup swiftshader roll revision * Update patch indices * Move WebDeviceEmulationParams into common. https://chromium-review.googlesource.com/c/chromium/src/+/2303356 * Move EnableDisableDeviceEmulation to blink mojom messages https://chromium-review.googlesource.com/c/chromium/src/+/2303367 * PDF Viewer: Remove flag for two-up view https://chromium-review.googlesource.com/c/chromium/src/+/2311130 * Add mojom definition for DeviceEmulationParams. https://chromium-review.googlesource.com/c/chromium/src/+/2303491 * Remove ServiceWorkerContextWatcher from PaymentAppInstaller https://chromium-review.googlesource.com/c/chromium/src/+/2291186 * Loader: Move transferrable_url_loader.mojom into blink's mojom directory https://chromium-review.googlesource.com/c/chromium/src/+/2306123 * chore: bump chromium in DEPS to 4974f436479739025a90ebc2cc2e36d67ee1ac46 * mac: Work around Xcode 12b3 SDK bug https://chromium-review.googlesource.com/c/chromium/src/+/2315078 * Reland Update core items for macOS Big Sur. https://chromium-review.googlesource.com/c/chromium/src/+/2315162 * Update Swiftshader revision * mac/arm64: When cross-building the snapshot, use page size of the target ISA instead of the host. https://chromium-review.googlesource.com/c/v8/v8/+/2310575 * Update patch indices * Rename {,Non}ClientView::CanClose() to OnWindowCloseRequested() https://chromium-review.googlesource.com/c/chromium/src/+/2247838 * chore: bump chromium in DEPS to e9465d70d1dea539400f0fddad43358ea3c31d71 * chore: bump chromium in DEPS to bd5b71c5f20288eb26068a39ae6e0579566a51c5 * chore: bump chromium in DEPS to 786ee543048bd07d07c5ac50b7dbbdd6bdd8dcce * chore: bump chromium in DEPS to 34eb6ecbf2c5894b648900bf771a2a29de204798 * chore: bump chromium in DEPS to 567ff038d68e3adb8116a01eec863cdf34d775f5 * chore: bump chromium in DEPS to 340b45c8d4ceb2dd61969fc34e1928d3c46db48c * chore: update patches * chore: base::DeleteFile with two params is removed Should use base::DeleteFile and base::DeletePathRecursively when appropriate Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2313376 * chore: add patch for NodePlatform::PostJob impl * chore: update patches * chore: extension file access is now instrumented Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2209995 * chore: implement SetWindowFrameInScreen in OSR RWHV Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2321409 * chore: NotifyUserActivation requires a type now This is just for a histogram thing and therefore it does not matter what we pass in Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2281303 * chore: update patches * chore: bump chromium in DEPS to cd570e6dd3dcb84463ac252b04e92ceb02d8400c * chore: update patches * chore: bump chromium in DEPS to 0187908a31866992b90c59719ac1d016328f6ee0 * chore: bump chromium in DEPS to 3c9df38c508f3dba26a75248beed4882ddfb98e9 * chore: bump chromium in DEPS to 1a47d3b9cee710bd3c958c4f2d8b205710df9d50 * chore: bump chromium in DEPS to baac93040d96abdab72d46dd034c60f86e108702 * chore: bump chromium in DEPS to 13836145f97299e636491de38064b78861c4fb2e * update patches * change OS_MACOSX -> OS_MAC Refs: https://bugs.chromium.org/p/chromium/issues/detail?id=1105907 * patch: add header for ToExecutionContext in WebMessagePortConverter * chore: bump chromium in DEPS to 91ab9b6ac5d04dc034a03ad847fbfa8261328c2b * update patches * NeedToFireBeforeUnloadOrUnload -> NeedToFireBeforeUnloadOrUnloadEvents Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2288711 * chore: bump chromium in DEPS to 290deb11f0e30cb1382fd8f8793d340560283c23 * update patches * add dragdrop header for autofill popup * int -> x11::Time * patch out accessibility private API use Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2330812 * remove usage of XEvent Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2317767 * trigger recalculation of WebPreferences before renderer initialization Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2263635 * chore: bump chromium in DEPS to 6bdb484583b99c96ef3388d0c2184326581b2d5a * chore: bump chromium in DEPS to 1eb2a79cde04fd5c8ae51b4d813e6521635269e5 * chore: bump chromium in DEPS to 3dc8e3c0f400e4ca9c0a63d7a39bd910997ad4c6 * chore: update patches * fixup! trigger recalculation of WebPreferences before renderer initialization * views: Make MenuButton and RadioButton default constructible https://chromium-review.googlesource.com/c/chromium/src/+/2339586 * chore: fix code style Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: Samuel Attard <sattard@slack-corp.com> Co-authored-by: Andy Locascio <andy@slack-corp.com> Co-authored-by: deepak1556 <hop2deep@gmail.com>
2020-08-12 18:33:58 +00:00
#if !defined(OS_MAC)
gin::Handle<NativeImage> NativeImage::CreateFromNamedImage(gin::Arguments* args,
std::string name) {
return CreateEmpty(args->isolate());
}
#endif
2016-04-25 01:17:54 +00:00
// static
2020-07-13 21:44:12 +00:00
gin::ObjectTemplateBuilder NativeImage::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
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())
2016-06-22 22:46:22 +00:00
.SetMethod("toPNG", &NativeImage::ToPNG)
.SetMethod("toJPEG", &NativeImage::ToJPEG)
.SetMethod("toBitmap", &NativeImage::ToBitmap)
2016-08-04 17:36:01 +00:00
.SetMethod("getBitmap", &NativeImage::GetBitmap)
.SetMethod("getScaleFactors", &NativeImage::GetScaleFactors)
2016-04-25 01:17:54 +00:00
.SetMethod("getNativeHandle", &NativeImage::GetNativeHandle)
.SetMethod("toDataURL", &NativeImage::ToDataURL)
.SetMethod("isEmpty", &NativeImage::IsEmpty)
.SetMethod("getSize", &NativeImage::GetSize)
.SetMethod("setTemplateImage", &NativeImage::SetTemplateImage)
.SetMethod("isTemplateImage", &NativeImage::IsTemplateImage)
.SetProperty("isMacTemplateImage", &NativeImage::IsTemplateImage,
&NativeImage::SetTemplateImage)
2016-10-04 17:26:01 +00:00
.SetMethod("resize", &NativeImage::Resize)
2016-10-04 18:23:18 +00:00
.SetMethod("crop", &NativeImage::Crop)
.SetMethod("getAspectRatio", &NativeImage::GetAspectRatio)
.SetMethod("addRepresentation", &NativeImage::AddRepresentation);
2016-04-25 01:17:54 +00:00
}
2020-07-13 21:44:12 +00:00
const char* NativeImage::GetTypeName() {
return "NativeImage";
}
// static
gin::WrapperInfo NativeImage::kWrapperInfo = {gin::kEmbedderNativeGin};
} // namespace api
} // namespace electron
namespace gin {
v8::Local<v8::Value> Converter<electron::api::NativeImage*>::ToV8(
v8::Isolate* isolate,
electron::api::NativeImage* val) {
2020-07-13 21:44:12 +00:00
v8::Local<v8::Object> ret;
if (val && val->GetWrapper(isolate).ToLocal(&ret))
return ret;
else
return v8::Null(isolate);
}
bool Converter<electron::api::NativeImage*>::FromV8(
2018-04-18 01:55:30 +00:00
v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::NativeImage** out) {
// Try converting from file path.
base::FilePath path;
if (ConvertFromV8(isolate, val, &path)) {
*out = electron::api::NativeImage::CreateFromPath(isolate, path).get();
if ((*out)->image().IsEmpty()) {
#if defined(OS_WIN)
const auto img_path = base::UTF16ToUTF8(path.value());
#else
const auto img_path = path.value();
#endif
isolate->ThrowException(v8::Exception::Error(
StringToV8(isolate, "Image could not be created from " + img_path)));
return false;
}
return true;
}
2020-07-13 21:44:12 +00:00
// reinterpret_cast is safe here because NativeImage is the only subclass of
// gin::Wrappable<NativeImage>.
*out = static_cast<electron::api::NativeImage*>(
2020-07-13 21:44:12 +00:00
static_cast<gin::WrappableBase*>(gin::internal::FromV8Impl(
isolate, val, &electron::api::NativeImage::kWrapperInfo)));
return *out != nullptr;
}
} // namespace gin
namespace {
using electron::api::NativeImage;
2018-04-18 01:55:30 +00:00
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary dict(isolate, exports);
gin_helper::Dictionary native_image = gin::Dictionary::CreateEmpty(isolate);
dict.Set("nativeImage", native_image);
native_image.SetMethod("createEmpty", &NativeImage::CreateEmpty);
native_image.SetMethod("createFromPath", &NativeImage::CreateFromPath);
native_image.SetMethod("createFromBitmap", &NativeImage::CreateFromBitmap);
native_image.SetMethod("createFromBuffer", &NativeImage::CreateFromBuffer);
native_image.SetMethod("createFromDataURL", &NativeImage::CreateFromDataURL);
native_image.SetMethod("createFromNamedImage",
&NativeImage::CreateFromNamedImage);
#if !defined(OS_LINUX)
native_image.SetMethod("createThumbnailFromPath",
&NativeImage::CreateThumbnailFromPath);
#endif
}
} // namespace
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_common_native_image, Initialize)