2015-02-11 01:03:19 -07:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 00:34:31 -07:00
|
|
|
#ifndef ELECTRON_SHELL_COMMON_API_ELECTRON_API_NATIVE_IMAGE_H_
|
|
|
|
#define ELECTRON_SHELL_COMMON_API_ELECTRON_API_NATIVE_IMAGE_H_
|
2015-02-11 01:03:19 -07:00
|
|
|
|
2016-05-20 02:51:05 -06:00
|
|
|
#include <map>
|
2015-02-11 23:32:51 -07:00
|
|
|
#include <string>
|
2020-05-18 10:29:24 -06:00
|
|
|
#include <vector>
|
2015-02-11 23:32:51 -07:00
|
|
|
|
2023-05-11 14:07:39 -06:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2016-10-04 11:26:01 -06:00
|
|
|
#include "base/values.h"
|
2019-10-25 07:03:28 -06:00
|
|
|
#include "gin/handle.h"
|
2020-07-13 15:44:12 -06:00
|
|
|
#include "gin/wrappable.h"
|
2019-10-17 18:31:29 -06:00
|
|
|
#include "shell/common/gin_helper/error_thrower.h"
|
2015-02-11 01:03:19 -07:00
|
|
|
#include "ui/gfx/image/image.h"
|
2022-03-24 19:39:03 -06:00
|
|
|
#include "ui/gfx/image/image_skia_rep.h"
|
2015-02-11 01:03:19 -07:00
|
|
|
|
2022-02-09 19:58:52 -07:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2016-05-20 02:51:05 -06:00
|
|
|
#include "base/files/file_path.h"
|
2016-05-19 20:31:02 -06:00
|
|
|
#include "base/win/scoped_gdi_object.h"
|
|
|
|
#endif
|
|
|
|
|
2015-02-11 23:27:53 -07:00
|
|
|
class GURL;
|
|
|
|
|
2015-02-11 03:41:06 -07:00
|
|
|
namespace base {
|
|
|
|
class FilePath;
|
|
|
|
}
|
|
|
|
|
2015-02-11 01:49:43 -07:00
|
|
|
namespace gfx {
|
2019-10-17 18:31:29 -06:00
|
|
|
class Rect;
|
2015-02-11 01:49:43 -07:00
|
|
|
class Size;
|
2019-10-17 18:31:29 -06:00
|
|
|
} // namespace gfx
|
2015-02-11 01:49:43 -07:00
|
|
|
|
2019-10-17 18:31:29 -06:00
|
|
|
namespace gin_helper {
|
|
|
|
class Dictionary;
|
2015-02-12 00:19:05 -07:00
|
|
|
}
|
|
|
|
|
2020-07-13 15:44:12 -06:00
|
|
|
namespace gin {
|
|
|
|
class Arguments;
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:55:47 -06:00
|
|
|
namespace electron::api {
|
2015-02-11 01:03:19 -07:00
|
|
|
|
2020-07-13 15:44:12 -06:00
|
|
|
class NativeImage : public gin::Wrappable<NativeImage> {
|
2015-02-11 01:03:19 -07:00
|
|
|
public:
|
2021-09-07 11:37:45 -06:00
|
|
|
NativeImage(v8::Isolate* isolate, const gfx::Image& image);
|
2022-02-09 19:58:52 -07:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2021-09-07 11:37:45 -06:00
|
|
|
NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path);
|
|
|
|
#endif
|
|
|
|
~NativeImage() override;
|
|
|
|
|
2021-11-03 05:41:45 -06:00
|
|
|
// disable copy
|
|
|
|
NativeImage(const NativeImage&) = delete;
|
|
|
|
NativeImage& operator=(const NativeImage&) = delete;
|
|
|
|
|
2019-10-25 07:03:28 -06:00
|
|
|
static gin::Handle<NativeImage> CreateEmpty(v8::Isolate* isolate);
|
|
|
|
static gin::Handle<NativeImage> Create(v8::Isolate* isolate,
|
|
|
|
const gfx::Image& image);
|
|
|
|
static gin::Handle<NativeImage> CreateFromPNG(v8::Isolate* isolate,
|
|
|
|
const char* buffer,
|
|
|
|
size_t length);
|
|
|
|
static gin::Handle<NativeImage> CreateFromJPEG(v8::Isolate* isolate,
|
2018-04-17 19:44:10 -06:00
|
|
|
const char* buffer,
|
|
|
|
size_t length);
|
2019-10-25 07:03:28 -06:00
|
|
|
static gin::Handle<NativeImage> CreateFromPath(v8::Isolate* isolate,
|
|
|
|
const base::FilePath& path);
|
|
|
|
static gin::Handle<NativeImage> CreateFromBitmap(
|
2019-10-17 18:31:29 -06:00
|
|
|
gin_helper::ErrorThrower thrower,
|
2019-03-14 12:00:38 -06:00
|
|
|
v8::Local<v8::Value> buffer,
|
2019-10-17 18:31:29 -06:00
|
|
|
const gin_helper::Dictionary& options);
|
2019-10-25 07:03:28 -06:00
|
|
|
static gin::Handle<NativeImage> CreateFromBuffer(
|
2019-10-17 18:31:29 -06:00
|
|
|
gin_helper::ErrorThrower thrower,
|
|
|
|
v8::Local<v8::Value> buffer,
|
|
|
|
gin::Arguments* args);
|
2019-10-25 07:03:28 -06:00
|
|
|
static gin::Handle<NativeImage> CreateFromDataURL(v8::Isolate* isolate,
|
|
|
|
const GURL& url);
|
|
|
|
static gin::Handle<NativeImage> CreateFromNamedImage(gin::Arguments* args,
|
2020-05-10 19:24:45 -06:00
|
|
|
std::string name);
|
2022-02-09 19:58:52 -07:00
|
|
|
#if !BUILDFLAG(IS_LINUX)
|
2020-08-24 10:36:13 -06:00
|
|
|
static v8::Local<v8::Promise> CreateThumbnailFromPath(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const base::FilePath& path,
|
|
|
|
const gfx::Size& size);
|
|
|
|
#endif
|
2015-02-11 01:03:19 -07:00
|
|
|
|
2021-01-24 18:24:10 -07:00
|
|
|
enum class OnConvertError { kThrow, kWarn };
|
|
|
|
|
|
|
|
static bool TryConvertNativeImage(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> image,
|
|
|
|
NativeImage** native_image,
|
|
|
|
OnConvertError on_error = OnConvertError::kThrow);
|
2021-01-04 13:58:31 -07:00
|
|
|
|
2020-07-13 15:44:12 -06:00
|
|
|
// gin::Wrappable
|
|
|
|
static gin::WrapperInfo kWrapperInfo;
|
|
|
|
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) override;
|
|
|
|
const char* GetTypeName() override;
|
2015-02-11 04:22:41 -07:00
|
|
|
|
2022-02-09 19:58:52 -07:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2016-05-20 02:51:05 -06:00
|
|
|
HICON GetHICON(int size);
|
2016-05-20 01:14:40 -06:00
|
|
|
#endif
|
2015-02-11 04:22:41 -07:00
|
|
|
|
2016-05-20 01:55:22 -06:00
|
|
|
const gfx::Image& image() const { return image_; }
|
|
|
|
|
2015-02-11 01:03:19 -07:00
|
|
|
private:
|
2019-10-17 18:31:29 -06:00
|
|
|
v8::Local<v8::Value> ToPNG(gin::Arguments* args);
|
2015-05-22 05:11:22 -06:00
|
|
|
v8::Local<v8::Value> ToJPEG(v8::Isolate* isolate, int quality);
|
2019-10-17 18:31:29 -06:00
|
|
|
v8::Local<v8::Value> ToBitmap(gin::Arguments* args);
|
2020-05-18 10:29:24 -06:00
|
|
|
std::vector<float> GetScaleFactors();
|
2019-10-17 18:31:29 -06:00
|
|
|
v8::Local<v8::Value> GetBitmap(gin::Arguments* args);
|
|
|
|
v8::Local<v8::Value> GetNativeHandle(gin_helper::ErrorThrower thrower);
|
2020-05-18 10:29:24 -06:00
|
|
|
gin::Handle<NativeImage> Resize(gin::Arguments* args,
|
2022-07-05 09:25:18 -06:00
|
|
|
base::Value::Dict options);
|
2019-10-25 07:03:28 -06:00
|
|
|
gin::Handle<NativeImage> Crop(v8::Isolate* isolate, const gfx::Rect& rect);
|
2019-10-17 18:31:29 -06:00
|
|
|
std::string ToDataURL(gin::Arguments* args);
|
2015-02-11 01:49:43 -07:00
|
|
|
bool IsEmpty();
|
2021-06-03 02:05:04 -06:00
|
|
|
gfx::Size GetSize(const absl::optional<float> scale_factor);
|
|
|
|
float GetAspectRatio(const absl::optional<float> scale_factor);
|
2019-10-17 18:31:29 -06:00
|
|
|
void AddRepresentation(const gin_helper::Dictionary& options);
|
2015-02-11 01:03:19 -07:00
|
|
|
|
2022-03-16 11:54:30 -06:00
|
|
|
void UpdateExternalAllocatedMemoryUsage();
|
2021-05-23 20:32:55 -06:00
|
|
|
|
2015-04-12 21:53:24 -06:00
|
|
|
// Mark the image as template image.
|
|
|
|
void SetTemplateImage(bool setAsTemplate);
|
2015-07-26 22:58:48 -06:00
|
|
|
// Determine if the image is a template image.
|
|
|
|
bool IsTemplateImage();
|
2015-04-12 21:53:24 -06:00
|
|
|
|
2022-02-09 19:58:52 -07:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2016-05-20 02:51:05 -06:00
|
|
|
base::FilePath hicon_path_;
|
|
|
|
std::map<int, base::win::ScopedHICON> hicons_;
|
2016-05-19 20:31:02 -06:00
|
|
|
#endif
|
|
|
|
|
2015-02-11 01:03:19 -07:00
|
|
|
gfx::Image image_;
|
|
|
|
|
2023-05-11 14:07:39 -06:00
|
|
|
raw_ptr<v8::Isolate> isolate_;
|
2022-03-16 11:54:30 -06:00
|
|
|
int32_t memory_usage_ = 0;
|
2015-02-11 01:03:19 -07:00
|
|
|
};
|
|
|
|
|
2022-06-29 13:55:47 -06:00
|
|
|
} // namespace electron::api
|
2015-02-11 01:03:19 -07:00
|
|
|
|
2021-11-22 00:34:31 -07:00
|
|
|
#endif // ELECTRON_SHELL_COMMON_API_ELECTRON_API_NATIVE_IMAGE_H_
|