chore: remove native_mate (Part 11) (#20719)
* refactor: convert Menu and globalShortcut to gin * refactor: convert api::Cookies to gin * refactor: convert View and WebContentsView to gin * refactor: convert WebContents related classes to gin * refactor: convert powerMonitor to gin * refactor: prepare for header change * refactor: remove last uses of mate::EventEmitter * refactor: remove mate::EventEmitter * refactor: move trackable_object to gin_helper * fix: custom converter should not use Handle * fix: no more need to check if icon is empty It was a bug that the Handle<NativeImage> can be non-empty when the image file does not exist. The bug was caused by the converter code writing out the image even when the convertion fails. The bug was work-arounded by adding an additional check, but since the original bug had been fixed, the additional check is no longer needed. * fix: should always set frameId even when callback is null * fix: do not mix gin/mate handles for NativeImage
This commit is contained in:
parent
0e0d4fe990
commit
0fe6767d6b
80 changed files with 823 additions and 1087 deletions
|
@ -259,7 +259,7 @@ float NativeImage::GetAspectRatio() {
|
|||
return static_cast<float>(size.width()) / static_cast<float>(size.height());
|
||||
}
|
||||
|
||||
mate::Handle<NativeImage> NativeImage::Resize(
|
||||
gin::Handle<NativeImage> NativeImage::Resize(
|
||||
v8::Isolate* isolate,
|
||||
const base::DictionaryValue& options) {
|
||||
gfx::Size size = GetSize();
|
||||
|
@ -290,16 +290,16 @@ mate::Handle<NativeImage> NativeImage::Resize(
|
|||
|
||||
gfx::ImageSkia resized = gfx::ImageSkiaOperations::CreateResizedImage(
|
||||
image_.AsImageSkia(), method, size);
|
||||
return mate::CreateHandle(isolate,
|
||||
new NativeImage(isolate, gfx::Image(resized)));
|
||||
return gin::CreateHandle(isolate,
|
||||
new NativeImage(isolate, gfx::Image(resized)));
|
||||
}
|
||||
|
||||
mate::Handle<NativeImage> NativeImage::Crop(v8::Isolate* isolate,
|
||||
const gfx::Rect& rect) {
|
||||
gin::Handle<NativeImage> NativeImage::Crop(v8::Isolate* isolate,
|
||||
const gfx::Rect& rect) {
|
||||
gfx::ImageSkia cropped =
|
||||
gfx::ImageSkiaOperations::ExtractSubset(image_.AsImageSkia(), rect);
|
||||
return mate::CreateHandle(isolate,
|
||||
new NativeImage(isolate, gfx::Image(cropped)));
|
||||
return gin::CreateHandle(isolate,
|
||||
new NativeImage(isolate, gfx::Image(cropped)));
|
||||
}
|
||||
|
||||
void NativeImage::AddRepresentation(const gin_helper::Dictionary& options) {
|
||||
|
@ -350,48 +350,48 @@ bool NativeImage::IsTemplateImage() {
|
|||
#endif
|
||||
|
||||
// static
|
||||
mate::Handle<NativeImage> NativeImage::CreateEmpty(v8::Isolate* isolate) {
|
||||
return mate::CreateHandle(isolate, new NativeImage(isolate, gfx::Image()));
|
||||
gin::Handle<NativeImage> NativeImage::CreateEmpty(v8::Isolate* isolate) {
|
||||
return gin::CreateHandle(isolate, new NativeImage(isolate, gfx::Image()));
|
||||
}
|
||||
|
||||
// static
|
||||
mate::Handle<NativeImage> NativeImage::Create(v8::Isolate* isolate,
|
||||
const gfx::Image& image) {
|
||||
return mate::CreateHandle(isolate, new NativeImage(isolate, image));
|
||||
gin::Handle<NativeImage> NativeImage::Create(v8::Isolate* isolate,
|
||||
const gfx::Image& image) {
|
||||
return gin::CreateHandle(isolate, new NativeImage(isolate, image));
|
||||
}
|
||||
|
||||
// static
|
||||
mate::Handle<NativeImage> NativeImage::CreateFromPNG(v8::Isolate* isolate,
|
||||
const char* buffer,
|
||||
size_t length) {
|
||||
gin::Handle<NativeImage> NativeImage::CreateFromPNG(v8::Isolate* isolate,
|
||||
const char* buffer,
|
||||
size_t length) {
|
||||
gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(
|
||||
reinterpret_cast<const unsigned char*>(buffer), length);
|
||||
return Create(isolate, image);
|
||||
}
|
||||
|
||||
// static
|
||||
mate::Handle<NativeImage> NativeImage::CreateFromJPEG(v8::Isolate* isolate,
|
||||
const char* buffer,
|
||||
size_t length) {
|
||||
gin::Handle<NativeImage> NativeImage::CreateFromJPEG(v8::Isolate* isolate,
|
||||
const char* buffer,
|
||||
size_t length) {
|
||||
gfx::Image image = gfx::ImageFrom1xJPEGEncodedData(
|
||||
reinterpret_cast<const unsigned char*>(buffer), length);
|
||||
return Create(isolate, image);
|
||||
}
|
||||
|
||||
// static
|
||||
mate::Handle<NativeImage> NativeImage::CreateFromPath(
|
||||
gin::Handle<NativeImage> NativeImage::CreateFromPath(
|
||||
v8::Isolate* isolate,
|
||||
const base::FilePath& path) {
|
||||
base::FilePath image_path = NormalizePath(path);
|
||||
#if defined(OS_WIN)
|
||||
if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
|
||||
return mate::CreateHandle(isolate, new NativeImage(isolate, image_path));
|
||||
return gin::CreateHandle(isolate, new NativeImage(isolate, image_path));
|
||||
}
|
||||
#endif
|
||||
gfx::ImageSkia image_skia;
|
||||
electron::util::PopulateImageSkiaRepsFromPath(&image_skia, image_path);
|
||||
gfx::Image image(image_skia);
|
||||
mate::Handle<NativeImage> handle = Create(isolate, image);
|
||||
gin::Handle<NativeImage> handle = Create(isolate, image);
|
||||
#if defined(OS_MACOSX)
|
||||
if (IsTemplateFilename(image_path))
|
||||
handle->SetTemplateImage(true);
|
||||
|
@ -400,13 +400,13 @@ mate::Handle<NativeImage> NativeImage::CreateFromPath(
|
|||
}
|
||||
|
||||
// static
|
||||
mate::Handle<NativeImage> NativeImage::CreateFromBitmap(
|
||||
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 mate::Handle<NativeImage>();
|
||||
return gin::Handle<NativeImage>();
|
||||
}
|
||||
|
||||
unsigned int width = 0;
|
||||
|
@ -415,12 +415,12 @@ mate::Handle<NativeImage> NativeImage::CreateFromBitmap(
|
|||
|
||||
if (!options.Get("width", &width)) {
|
||||
thrower.ThrowError("width is required");
|
||||
return mate::Handle<NativeImage>();
|
||||
return gin::Handle<NativeImage>();
|
||||
}
|
||||
|
||||
if (!options.Get("height", &height)) {
|
||||
thrower.ThrowError("height is required");
|
||||
return mate::Handle<NativeImage>();
|
||||
return gin::Handle<NativeImage>();
|
||||
}
|
||||
|
||||
auto info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
|
||||
|
@ -428,7 +428,7 @@ mate::Handle<NativeImage> NativeImage::CreateFromBitmap(
|
|||
|
||||
if (size_bytes != node::Buffer::Length(buffer)) {
|
||||
thrower.ThrowError("invalid buffer size");
|
||||
return mate::Handle<NativeImage>();
|
||||
return gin::Handle<NativeImage>();
|
||||
}
|
||||
|
||||
options.Get("scaleFactor", &scale_factor);
|
||||
|
@ -448,13 +448,13 @@ mate::Handle<NativeImage> NativeImage::CreateFromBitmap(
|
|||
}
|
||||
|
||||
// static
|
||||
mate::Handle<NativeImage> NativeImage::CreateFromBuffer(
|
||||
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 mate::Handle<NativeImage>();
|
||||
return gin::Handle<NativeImage>();
|
||||
}
|
||||
|
||||
int width = 0;
|
||||
|
@ -476,8 +476,8 @@ mate::Handle<NativeImage> NativeImage::CreateFromBuffer(
|
|||
}
|
||||
|
||||
// static
|
||||
mate::Handle<NativeImage> NativeImage::CreateFromDataURL(v8::Isolate* isolate,
|
||||
const GURL& url) {
|
||||
gin::Handle<NativeImage> NativeImage::CreateFromDataURL(v8::Isolate* isolate,
|
||||
const GURL& url) {
|
||||
std::string mime_type, charset, data;
|
||||
if (net::DataURL::Parse(url, &mime_type, &charset, &data)) {
|
||||
if (mime_type == "image/png")
|
||||
|
@ -490,7 +490,7 @@ mate::Handle<NativeImage> NativeImage::CreateFromDataURL(v8::Isolate* isolate,
|
|||
}
|
||||
|
||||
#if !defined(OS_MACOSX)
|
||||
mate::Handle<NativeImage> NativeImage::CreateFromNamedImage(
|
||||
gin::Handle<NativeImage> NativeImage::CreateFromNamedImage(
|
||||
gin::Arguments* args,
|
||||
const std::string& name) {
|
||||
return CreateEmpty(args->isolate());
|
||||
|
@ -526,32 +526,31 @@ void NativeImage::BuildPrototype(v8::Isolate* isolate,
|
|||
|
||||
namespace gin {
|
||||
|
||||
v8::Local<v8::Value> Converter<mate::Handle<electron::api::NativeImage>>::ToV8(
|
||||
v8::Local<v8::Value> Converter<electron::api::NativeImage*>::ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const mate::Handle<electron::api::NativeImage>& val) {
|
||||
return val.ToV8();
|
||||
electron::api::NativeImage* val) {
|
||||
if (val)
|
||||
return val->GetWrapper();
|
||||
else
|
||||
return v8::Null(isolate);
|
||||
}
|
||||
|
||||
bool Converter<mate::Handle<electron::api::NativeImage>>::FromV8(
|
||||
bool Converter<electron::api::NativeImage*>::FromV8(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
mate::Handle<electron::api::NativeImage>* out) {
|
||||
electron::api::NativeImage** out) {
|
||||
// Try converting from file path.
|
||||
base::FilePath path;
|
||||
if (ConvertFromV8(isolate, val, &path)) {
|
||||
*out = electron::api::NativeImage::CreateFromPath(isolate, path);
|
||||
*out = electron::api::NativeImage::CreateFromPath(isolate, path).get();
|
||||
// Should throw when failed to initialize from path.
|
||||
return !(*out)->image().IsEmpty();
|
||||
}
|
||||
|
||||
auto* wrapper = static_cast<mate::WrappableBase*>(
|
||||
mate::internal::FromV8Impl(isolate, val));
|
||||
if (!wrapper)
|
||||
return false;
|
||||
|
||||
*out = mate::CreateHandle(isolate,
|
||||
static_cast<electron::api::NativeImage*>(wrapper));
|
||||
return true;
|
||||
*out = static_cast<electron::api::NativeImage*>(
|
||||
static_cast<mate::WrappableBase*>(
|
||||
mate::internal::FromV8Impl(isolate, val)));
|
||||
return *out != nullptr;
|
||||
}
|
||||
|
||||
} // namespace gin
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#include <string>
|
||||
|
||||
#include "base/values.h"
|
||||
#include "native_mate/handle.h"
|
||||
#include "gin/handle.h"
|
||||
#include "native_mate/wrappable.h"
|
||||
#include "shell/common/gin_helper/error_thrower.h"
|
||||
#include "ui/gfx/image/image.h"
|
||||
|
@ -40,30 +40,29 @@ namespace api {
|
|||
|
||||
class NativeImage : public mate::Wrappable<NativeImage> {
|
||||
public:
|
||||
static mate::Handle<NativeImage> CreateEmpty(v8::Isolate* isolate);
|
||||
static mate::Handle<NativeImage> Create(v8::Isolate* isolate,
|
||||
const gfx::Image& image);
|
||||
static mate::Handle<NativeImage> CreateFromPNG(v8::Isolate* isolate,
|
||||
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,
|
||||
const char* buffer,
|
||||
size_t length);
|
||||
static mate::Handle<NativeImage> CreateFromJPEG(v8::Isolate* isolate,
|
||||
const char* buffer,
|
||||
size_t length);
|
||||
static mate::Handle<NativeImage> CreateFromPath(v8::Isolate* isolate,
|
||||
const base::FilePath& path);
|
||||
static mate::Handle<NativeImage> CreateFromBitmap(
|
||||
static gin::Handle<NativeImage> CreateFromPath(v8::Isolate* isolate,
|
||||
const base::FilePath& path);
|
||||
static gin::Handle<NativeImage> CreateFromBitmap(
|
||||
gin_helper::ErrorThrower thrower,
|
||||
v8::Local<v8::Value> buffer,
|
||||
const gin_helper::Dictionary& options);
|
||||
static mate::Handle<NativeImage> CreateFromBuffer(
|
||||
static gin::Handle<NativeImage> CreateFromBuffer(
|
||||
gin_helper::ErrorThrower thrower,
|
||||
v8::Local<v8::Value> buffer,
|
||||
gin::Arguments* args);
|
||||
static mate::Handle<NativeImage> CreateFromDataURL(v8::Isolate* isolate,
|
||||
const GURL& url);
|
||||
static mate::Handle<NativeImage> CreateFromNamedImage(
|
||||
gin::Arguments* args,
|
||||
const std::string& name);
|
||||
static gin::Handle<NativeImage> CreateFromDataURL(v8::Isolate* isolate,
|
||||
const GURL& url);
|
||||
static gin::Handle<NativeImage> CreateFromNamedImage(gin::Arguments* args,
|
||||
const std::string& name);
|
||||
|
||||
static void BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::FunctionTemplate> prototype);
|
||||
|
@ -87,9 +86,9 @@ class NativeImage : public mate::Wrappable<NativeImage> {
|
|||
v8::Local<v8::Value> ToBitmap(gin::Arguments* args);
|
||||
v8::Local<v8::Value> GetBitmap(gin::Arguments* args);
|
||||
v8::Local<v8::Value> GetNativeHandle(gin_helper::ErrorThrower thrower);
|
||||
mate::Handle<NativeImage> Resize(v8::Isolate* isolate,
|
||||
const base::DictionaryValue& options);
|
||||
mate::Handle<NativeImage> Crop(v8::Isolate* isolate, const gfx::Rect& rect);
|
||||
gin::Handle<NativeImage> Resize(v8::Isolate* isolate,
|
||||
const base::DictionaryValue& options);
|
||||
gin::Handle<NativeImage> Crop(v8::Isolate* isolate, const gfx::Rect& rect);
|
||||
std::string ToDataURL(gin::Arguments* args);
|
||||
bool IsEmpty();
|
||||
gfx::Size GetSize();
|
||||
|
@ -119,36 +118,14 @@ namespace gin {
|
|||
|
||||
// A custom converter that allows converting path to NativeImage.
|
||||
template <>
|
||||
struct Converter<mate::Handle<electron::api::NativeImage>> {
|
||||
static v8::Local<v8::Value> ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const mate::Handle<electron::api::NativeImage>& val);
|
||||
struct Converter<electron::api::NativeImage*> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
electron::api::NativeImage* val);
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
mate::Handle<electron::api::NativeImage>* out);
|
||||
electron::api::NativeImage** out);
|
||||
};
|
||||
|
||||
} // namespace gin
|
||||
|
||||
namespace mate {
|
||||
|
||||
// Keep compatibility with native_mate code.
|
||||
//
|
||||
// TODO(zcbenz): Remove this after removing native_mate.
|
||||
template <>
|
||||
struct Converter<mate::Handle<electron::api::NativeImage>> {
|
||||
static v8::Local<v8::Value> ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const mate::Handle<electron::api::NativeImage>& val) {
|
||||
return gin::ConvertToV8(isolate, val);
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
mate::Handle<electron::api::NativeImage>* out) {
|
||||
return gin::ConvertFromV8(isolate, val, out);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // SHELL_COMMON_API_ATOM_API_NATIVE_IMAGE_H_
|
||||
|
|
|
@ -33,7 +33,7 @@ double safeShift(double in, double def) {
|
|||
return def;
|
||||
}
|
||||
|
||||
mate::Handle<NativeImage> NativeImage::CreateFromNamedImage(
|
||||
gin::Handle<NativeImage> NativeImage::CreateFromNamedImage(
|
||||
gin::Arguments* args,
|
||||
const std::string& name) {
|
||||
@autoreleasepool {
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include "base/hash/hash.h"
|
||||
#include "electron/buildflags/buildflags.h"
|
||||
#include "shell/common/gin_converters/content_converter.h"
|
||||
#include "shell/common/gin_converters/gurl_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
|
@ -19,10 +20,6 @@
|
|||
#include "shell/common/api/remote/remote_object_freer.h"
|
||||
#endif
|
||||
|
||||
// TODO(zcbenz): Remove the includes after removing native_mate.
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "shell/common/native_mate_converters/content_converter.h"
|
||||
|
||||
namespace std {
|
||||
|
||||
// The hash function used by DoubleIDWeakMap.
|
||||
|
@ -125,11 +122,8 @@ void Initialize(v8::Local<v8::Object> exports,
|
|||
dict.SetMethod("getObjectHash", &GetObjectHash);
|
||||
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
|
||||
#if BUILDFLAG(ENABLE_REMOTE_MODULE)
|
||||
// TODO(zcbenz): Use gin_helper::Dictionary when content_converter.h is moved
|
||||
// to gin.
|
||||
mate::Dictionary mdict(context->GetIsolate(), exports);
|
||||
mdict.SetMethod("setRemoteCallbackFreer",
|
||||
&electron::RemoteCallbackFreer::BindTo);
|
||||
dict.SetMethod("setRemoteCallbackFreer",
|
||||
&electron::RemoteCallbackFreer::BindTo);
|
||||
dict.SetMethod("setRemoteObjectFreer", &electron::RemoteObjectFreer::BindTo);
|
||||
dict.SetMethod("addRemoteObjectRef", &electron::RemoteObjectFreer::AddRef);
|
||||
dict.SetMethod("createIDWeakMap",
|
||||
|
|
|
@ -2,13 +2,13 @@
|
|||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/common/native_mate_converters/accelerator_converter.h"
|
||||
#include "shell/common/gin_converters/accelerator_converter.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "shell/browser/ui/accelerator_util.h"
|
||||
|
||||
namespace mate {
|
||||
namespace gin {
|
||||
|
||||
// static
|
||||
bool Converter<ui::Accelerator>::FromV8(v8::Isolate* isolate,
|
||||
|
@ -20,4 +20,4 @@ bool Converter<ui::Accelerator>::FromV8(v8::Isolate* isolate,
|
|||
return accelerator_util::StringToAccelerator(keycode, out);
|
||||
}
|
||||
|
||||
} // namespace mate
|
||||
} // namespace gin
|
|
@ -2,16 +2,16 @@
|
|||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_COMMON_NATIVE_MATE_CONVERTERS_ACCELERATOR_CONVERTER_H_
|
||||
#define SHELL_COMMON_NATIVE_MATE_CONVERTERS_ACCELERATOR_CONVERTER_H_
|
||||
#ifndef SHELL_COMMON_GIN_CONVERTERS_ACCELERATOR_CONVERTER_H_
|
||||
#define SHELL_COMMON_GIN_CONVERTERS_ACCELERATOR_CONVERTER_H_
|
||||
|
||||
#include "native_mate/converter.h"
|
||||
#include "gin/converter.h"
|
||||
|
||||
namespace ui {
|
||||
class Accelerator;
|
||||
}
|
||||
|
||||
namespace mate {
|
||||
namespace gin {
|
||||
|
||||
template <>
|
||||
struct Converter<ui::Accelerator> {
|
||||
|
@ -20,6 +20,6 @@ struct Converter<ui::Accelerator> {
|
|||
ui::Accelerator* out);
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
} // namespace gin
|
||||
|
||||
#endif // SHELL_COMMON_NATIVE_MATE_CONVERTERS_ACCELERATOR_CONVERTER_H_
|
||||
#endif // SHELL_COMMON_GIN_CONVERTERS_ACCELERATOR_CONVERTER_H_
|
|
@ -12,6 +12,15 @@
|
|||
|
||||
namespace gin {
|
||||
|
||||
template <>
|
||||
struct Converter<blink::WebKeyboardEvent> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
blink::WebKeyboardEvent* out) {
|
||||
return mate::ConvertFromV8(isolate, val, out);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<blink::CloneableMessage> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
|
@ -25,6 +34,46 @@ struct Converter<blink::CloneableMessage> {
|
|||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<blink::WebDeviceEmulationParams> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
blink::WebDeviceEmulationParams* out) {
|
||||
return mate::ConvertFromV8(isolate, val, out);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<blink::WebContextMenuData::MediaType> {
|
||||
static v8::Local<v8::Value> ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const blink::WebContextMenuData::MediaType& in) {
|
||||
return mate::ConvertToV8(isolate, in);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<blink::WebContextMenuData::InputFieldType> {
|
||||
static v8::Local<v8::Value> ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const blink::WebContextMenuData::InputFieldType& in) {
|
||||
return mate::ConvertToV8(isolate, in);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<network::mojom::ReferrerPolicy> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const network::mojom::ReferrerPolicy& in) {
|
||||
return mate::ConvertToV8(isolate, in);
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
network::mojom::ReferrerPolicy* out) {
|
||||
return mate::ConvertFromV8(isolate, val, out);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace gin
|
||||
|
||||
#endif // SHELL_COMMON_GIN_CONVERTERS_BLINK_CONVERTER_GIN_ADAPTER_H_
|
||||
|
|
|
@ -2,22 +2,22 @@
|
|||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/common/native_mate_converters/content_converter.h"
|
||||
#include "shell/common/gin_converters/content_converter.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "content/public/browser/native_web_keyboard_event.h"
|
||||
#include "content/public/browser/web_contents.h"
|
||||
#include "content/public/common/context_menu_params.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "shell/browser/api/atom_api_web_contents.h"
|
||||
#include "shell/browser/web_contents_permission_helper.h"
|
||||
#include "shell/common/native_mate_converters/blink_converter.h"
|
||||
#include "shell/common/native_mate_converters/callback_converter_deprecated.h"
|
||||
#include "shell/common/native_mate_converters/gurl_converter.h"
|
||||
#include "shell/common/native_mate_converters/string16_converter.h"
|
||||
#include "shell/common/native_mate_converters/ui_base_types_converter.h"
|
||||
#include "shell/common/native_mate_converters/value_converter.h"
|
||||
#include "shell/common/gin_converters/blink_converter_gin_adapter.h"
|
||||
#include "shell/common/gin_converters/callback_converter.h"
|
||||
#include "shell/common/gin_converters/gurl_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "ui/events/keycodes/dom/keycode_converter.h"
|
||||
#include "ui/events/keycodes/keyboard_code_conversion.h"
|
||||
|
||||
namespace {
|
||||
|
||||
|
@ -38,7 +38,7 @@ v8::Local<v8::Value> MenuItemToV8(
|
|||
content::WebContents* web_contents,
|
||||
const content::CustomContextMenuContext& context,
|
||||
const content::MenuItem& item) {
|
||||
mate::Dictionary v8_item = mate::Dictionary::CreateEmpty(isolate);
|
||||
gin_helper::Dictionary v8_item = gin::Dictionary::CreateEmpty(isolate);
|
||||
switch (item.type) {
|
||||
case content::MenuItem::CHECKABLE_OPTION:
|
||||
case content::MenuItem::GROUP:
|
||||
|
@ -69,12 +69,31 @@ v8::Local<v8::Value> MenuToV8(v8::Isolate* isolate,
|
|||
v8_menu.reserve(menu.size());
|
||||
for (const auto& menu_item : menu)
|
||||
v8_menu.push_back(MenuItemToV8(isolate, web_contents, context, menu_item));
|
||||
return mate::ConvertToV8(isolate, v8_menu);
|
||||
return gin::ConvertToV8(isolate, v8_menu);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace mate {
|
||||
namespace gin {
|
||||
|
||||
template <>
|
||||
struct Converter<ui::MenuSourceType> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const ui::MenuSourceType& in) {
|
||||
switch (in) {
|
||||
case ui::MENU_SOURCE_MOUSE:
|
||||
return StringToV8(isolate, "mouse");
|
||||
case ui::MENU_SOURCE_KEYBOARD:
|
||||
return StringToV8(isolate, "keyboard");
|
||||
case ui::MENU_SOURCE_TOUCH:
|
||||
return StringToV8(isolate, "touch");
|
||||
case ui::MENU_SOURCE_TOUCH_EDIT_MENU:
|
||||
return StringToV8(isolate, "touchMenu");
|
||||
default:
|
||||
return StringToV8(isolate, "none");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// static
|
||||
v8::Local<v8::Value> Converter<content::MenuItem::Type>::ToV8(
|
||||
|
@ -100,7 +119,7 @@ v8::Local<v8::Value> Converter<ContextMenuParamsWithWebContents>::ToV8(
|
|||
v8::Isolate* isolate,
|
||||
const ContextMenuParamsWithWebContents& val) {
|
||||
const auto& params = val.first;
|
||||
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
||||
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
||||
dict.Set("x", params.x);
|
||||
dict.Set("y", params.y);
|
||||
dict.Set("linkURL", params.link_url);
|
||||
|
@ -109,13 +128,13 @@ v8::Local<v8::Value> Converter<ContextMenuParamsWithWebContents>::ToV8(
|
|||
dict.Set("frameURL", params.frame_url);
|
||||
dict.Set("srcURL", params.src_url);
|
||||
dict.Set("mediaType", params.media_type);
|
||||
dict.Set("mediaFlags", MediaFlagsToV8(isolate, params.media_flags));
|
||||
dict.Set("mediaFlags", mate::MediaFlagsToV8(isolate, params.media_flags));
|
||||
bool has_image_contents =
|
||||
(params.media_type == blink::WebContextMenuData::kMediaTypeImage) &&
|
||||
params.has_image_contents;
|
||||
dict.Set("hasImageContents", has_image_contents);
|
||||
dict.Set("isEditable", params.is_editable);
|
||||
dict.Set("editFlags", EditFlagsToV8(isolate, params.edit_flags));
|
||||
dict.Set("editFlags", mate::EditFlagsToV8(isolate, params.edit_flags));
|
||||
dict.Set("selectionText", params.selection_text);
|
||||
dict.Set("titleText", params.title_text);
|
||||
dict.Set("misspelledWord", params.misspelled_word);
|
||||
|
@ -126,7 +145,7 @@ v8::Local<v8::Value> Converter<ContextMenuParamsWithWebContents>::ToV8(
|
|||
if (params.custom_context.is_pepper_menu)
|
||||
dict.Set("menu", MenuToV8(isolate, val.second, params.custom_context,
|
||||
params.custom_items));
|
||||
return mate::ConvertToV8(isolate, dict);
|
||||
return gin::ConvertToV8(isolate, dict);
|
||||
}
|
||||
|
||||
// static
|
||||
|
@ -215,7 +234,7 @@ bool Converter<content::WebContents*>::FromV8(v8::Isolate* isolate,
|
|||
v8::Local<v8::Value> val,
|
||||
content::WebContents** out) {
|
||||
electron::api::WebContents* web_contents = nullptr;
|
||||
if (!ConvertFromV8(isolate, val, &web_contents) || !web_contents)
|
||||
if (!gin::ConvertFromV8(isolate, val, &web_contents) || !web_contents)
|
||||
return false;
|
||||
|
||||
*out = web_contents->web_contents();
|
||||
|
@ -226,17 +245,17 @@ bool Converter<content::WebContents*>::FromV8(v8::Isolate* isolate,
|
|||
v8::Local<v8::Value> Converter<content::Referrer>::ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const content::Referrer& val) {
|
||||
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
||||
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
||||
dict.Set("url", ConvertToV8(isolate, val.url));
|
||||
dict.Set("policy", ConvertToV8(isolate, val.policy));
|
||||
return mate::ConvertToV8(isolate, dict);
|
||||
return gin::ConvertToV8(isolate, dict);
|
||||
}
|
||||
|
||||
// static
|
||||
bool Converter<content::Referrer>::FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
content::Referrer* out) {
|
||||
mate::Dictionary dict;
|
||||
gin_helper::Dictionary dict;
|
||||
if (!ConvertFromV8(isolate, val, &dict))
|
||||
return false;
|
||||
|
||||
|
@ -249,4 +268,42 @@ bool Converter<content::Referrer>::FromV8(v8::Isolate* isolate,
|
|||
return true;
|
||||
}
|
||||
|
||||
} // namespace mate
|
||||
// static
|
||||
bool Converter<content::NativeWebKeyboardEvent>::FromV8(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
content::NativeWebKeyboardEvent* out) {
|
||||
gin_helper::Dictionary dict;
|
||||
if (!ConvertFromV8(isolate, val, &dict))
|
||||
return false;
|
||||
if (!ConvertFromV8(isolate, val, static_cast<blink::WebKeyboardEvent*>(out)))
|
||||
return false;
|
||||
dict.Get("skipInBrowser", &out->skip_in_browser);
|
||||
return true;
|
||||
}
|
||||
|
||||
// static
|
||||
v8::Local<v8::Value> Converter<content::NativeWebKeyboardEvent>::ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const content::NativeWebKeyboardEvent& in) {
|
||||
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
||||
|
||||
if (in.GetType() == blink::WebInputEvent::Type::kRawKeyDown)
|
||||
dict.Set("type", "keyDown");
|
||||
else if (in.GetType() == blink::WebInputEvent::Type::kKeyUp)
|
||||
dict.Set("type", "keyUp");
|
||||
dict.Set("key", ui::KeycodeConverter::DomKeyToKeyString(in.dom_key));
|
||||
dict.Set("code", ui::KeycodeConverter::DomCodeToCodeString(
|
||||
static_cast<ui::DomCode>(in.dom_code)));
|
||||
|
||||
using Modifiers = blink::WebInputEvent::Modifiers;
|
||||
dict.Set("isAutoRepeat", (in.GetModifiers() & Modifiers::kIsAutoRepeat) != 0);
|
||||
dict.Set("shift", (in.GetModifiers() & Modifiers::kShiftKey) != 0);
|
||||
dict.Set("control", (in.GetModifiers() & Modifiers::kControlKey) != 0);
|
||||
dict.Set("alt", (in.GetModifiers() & Modifiers::kAltKey) != 0);
|
||||
dict.Set("meta", (in.GetModifiers() & Modifiers::kMetaKey) != 0);
|
||||
|
||||
return dict.GetHandle();
|
||||
}
|
||||
|
||||
} // namespace gin
|
|
@ -2,8 +2,8 @@
|
|||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_COMMON_NATIVE_MATE_CONVERTERS_CONTENT_CONVERTER_H_
|
||||
#define SHELL_COMMON_NATIVE_MATE_CONVERTERS_CONTENT_CONVERTER_H_
|
||||
#ifndef SHELL_COMMON_GIN_CONVERTERS_CONTENT_CONVERTER_H_
|
||||
#define SHELL_COMMON_GIN_CONVERTERS_CONTENT_CONVERTER_H_
|
||||
|
||||
#include <utility>
|
||||
|
||||
|
@ -11,18 +11,19 @@
|
|||
#include "content/public/common/menu_item.h"
|
||||
#include "content/public/common/referrer.h"
|
||||
#include "content/public/common/stop_find_action.h"
|
||||
#include "native_mate/converter.h"
|
||||
#include "gin/converter.h"
|
||||
#include "third_party/blink/public/mojom/permissions/permission_status.mojom.h"
|
||||
|
||||
namespace content {
|
||||
struct ContextMenuParams;
|
||||
struct NativeWebKeyboardEvent;
|
||||
class WebContents;
|
||||
} // namespace content
|
||||
|
||||
using ContextMenuParamsWithWebContents =
|
||||
std::pair<content::ContextMenuParams, content::WebContents*>;
|
||||
|
||||
namespace mate {
|
||||
namespace gin {
|
||||
|
||||
template <>
|
||||
struct Converter<content::MenuItem::Type> {
|
||||
|
@ -74,6 +75,15 @@ struct Converter<content::Referrer> {
|
|||
content::Referrer* out);
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
template <>
|
||||
struct Converter<content::NativeWebKeyboardEvent> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
content::NativeWebKeyboardEvent* out);
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const content::NativeWebKeyboardEvent& in);
|
||||
};
|
||||
|
||||
#endif // SHELL_COMMON_NATIVE_MATE_CONVERTERS_CONTENT_CONVERTER_H_
|
||||
} // namespace gin
|
||||
|
||||
#endif // SHELL_COMMON_GIN_CONVERTERS_CONTENT_CONVERTER_H_
|
|
@ -27,9 +27,8 @@ bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate,
|
|||
if (val->IsNull())
|
||||
return true;
|
||||
|
||||
// TODO(deermichel): remove mate:: after dropping mate
|
||||
mate::Handle<electron::api::NativeImage> native_image;
|
||||
if (!mate::ConvertFromV8(isolate, val, &native_image))
|
||||
gin::Handle<electron::api::NativeImage> native_image;
|
||||
if (!gin::ConvertFromV8(isolate, val, &native_image))
|
||||
return false;
|
||||
|
||||
*out = native_image->image();
|
||||
|
@ -38,7 +37,8 @@ bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate,
|
|||
|
||||
v8::Local<v8::Value> Converter<gfx::Image>::ToV8(v8::Isolate* isolate,
|
||||
const gfx::Image& val) {
|
||||
return ConvertToV8(isolate, electron::api::NativeImage::Create(isolate, val));
|
||||
return gin::ConvertToV8(isolate,
|
||||
electron::api::NativeImage::Create(isolate, val));
|
||||
}
|
||||
|
||||
} // namespace gin
|
||||
|
|
|
@ -285,6 +285,58 @@ v8::Local<v8::Value> Converter<network::ResourceRequestBody>::ToV8(
|
|||
return arr;
|
||||
}
|
||||
|
||||
// static
|
||||
v8::Local<v8::Value>
|
||||
Converter<scoped_refptr<network::ResourceRequestBody>>::ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const scoped_refptr<network::ResourceRequestBody>& val) {
|
||||
if (!val)
|
||||
return v8::Null(isolate);
|
||||
return ConvertToV8(isolate, *val);
|
||||
}
|
||||
|
||||
// static
|
||||
bool Converter<scoped_refptr<network::ResourceRequestBody>>::FromV8(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
scoped_refptr<network::ResourceRequestBody>* out) {
|
||||
auto list = std::make_unique<base::ListValue>();
|
||||
if (!ConvertFromV8(isolate, val, list.get()))
|
||||
return false;
|
||||
*out = new network::ResourceRequestBody();
|
||||
for (size_t i = 0; i < list->GetSize(); ++i) {
|
||||
base::DictionaryValue* dict = nullptr;
|
||||
std::string type;
|
||||
if (!list->GetDictionary(i, &dict))
|
||||
return false;
|
||||
dict->GetString("type", &type);
|
||||
if (type == "rawData") {
|
||||
base::Value* bytes = nullptr;
|
||||
dict->GetBinary("bytes", &bytes);
|
||||
(*out)->AppendBytes(
|
||||
reinterpret_cast<const char*>(bytes->GetBlob().data()),
|
||||
base::checked_cast<int>(bytes->GetBlob().size()));
|
||||
} else if (type == "file") {
|
||||
std::string file;
|
||||
int offset = 0, length = -1;
|
||||
double modification_time = 0.0;
|
||||
dict->GetStringWithoutPathExpansion("filePath", &file);
|
||||
dict->GetInteger("offset", &offset);
|
||||
dict->GetInteger("file", &length);
|
||||
dict->GetDouble("modificationTime", &modification_time);
|
||||
(*out)->AppendFileRange(base::FilePath::FromUTF8Unsafe(file),
|
||||
static_cast<uint64_t>(offset),
|
||||
static_cast<uint64_t>(length),
|
||||
base::Time::FromDoubleT(modification_time));
|
||||
} else if (type == "blob") {
|
||||
std::string uuid;
|
||||
dict->GetString("blobUUID", &uuid);
|
||||
(*out)->AppendBlob(uuid);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// static
|
||||
v8::Local<v8::Value> Converter<network::ResourceRequest>::ToV8(
|
||||
v8::Isolate* isolate,
|
||||
|
|
|
@ -75,6 +75,16 @@ struct Converter<network::ResourceRequestBody> {
|
|||
const network::ResourceRequestBody& val);
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<scoped_refptr<network::ResourceRequestBody>> {
|
||||
static v8::Local<v8::Value> ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const scoped_refptr<network::ResourceRequestBody>& val);
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
scoped_refptr<network::ResourceRequestBody>* out);
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<network::ResourceRequest> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
|
|
|
@ -39,6 +39,13 @@ struct Converter<unsigned long> { // NOLINT(runtime/int)
|
|||
};
|
||||
#endif
|
||||
|
||||
template <>
|
||||
struct Converter<std::nullptr_t> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, std::nullptr_t val) {
|
||||
return v8::Null(isolate);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<const char*> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, const char* val) {
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
#include "shell/common/gin_helper/event_emitter.h"
|
||||
|
||||
#include "content/public/browser/render_frame_host.h"
|
||||
#include "shell/browser/api/event.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/gin_helper/object_template_builder.h"
|
||||
#include "ui/events/event_constants.h"
|
||||
|
@ -62,6 +64,30 @@ v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags) {
|
|||
return obj.GetHandle();
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> CreateNativeEvent(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::Object> sender,
|
||||
content::RenderFrameHost* frame,
|
||||
base::Optional<electron::mojom::ElectronBrowser::MessageSyncCallback>
|
||||
callback) {
|
||||
v8::Local<v8::Object> event;
|
||||
if (frame && callback) {
|
||||
mate::Handle<mate::Event> native_event = mate::Event::Create(isolate);
|
||||
native_event->SetCallback(std::move(callback));
|
||||
event = v8::Local<v8::Object>::Cast(native_event.ToV8());
|
||||
} else {
|
||||
// No need to create native event if we do not need to send reply.
|
||||
event = CreateEvent(isolate);
|
||||
}
|
||||
|
||||
Dictionary dict(isolate, event);
|
||||
dict.Set("sender", sender);
|
||||
// Should always set frameId even when callback is null.
|
||||
if (frame)
|
||||
dict.Set("frameId", frame->GetRoutingID());
|
||||
return event;
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
} // namespace gin_helper
|
||||
|
|
|
@ -9,9 +9,15 @@
|
|||
#include <vector>
|
||||
|
||||
#include "base/optional.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "electron/shell/common/api/api.mojom.h"
|
||||
#include "native_mate/wrappable.h"
|
||||
#include "shell/common/gin_helper/event_emitter_caller.h"
|
||||
|
||||
namespace content {
|
||||
class RenderFrameHost;
|
||||
}
|
||||
|
||||
namespace gin_helper {
|
||||
|
||||
namespace internal {
|
||||
|
@ -21,16 +27,21 @@ v8::Local<v8::Object> CreateEvent(
|
|||
v8::Local<v8::Object> sender = v8::Local<v8::Object>(),
|
||||
v8::Local<v8::Object> custom_event = v8::Local<v8::Object>());
|
||||
v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags);
|
||||
v8::Local<v8::Object> CreateNativeEvent(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::Object> sender,
|
||||
content::RenderFrameHost* frame,
|
||||
base::Optional<electron::mojom::ElectronBrowser::MessageSyncCallback>
|
||||
callback);
|
||||
|
||||
} // namespace internal
|
||||
|
||||
// Provide helperers to emit event in JavaScript.
|
||||
//
|
||||
// TODO(zcbenz): Inherit from Wrappable directly after removing native_mate.
|
||||
template <typename Base>
|
||||
class EventEmitter : public Base {
|
||||
template <typename T>
|
||||
class EventEmitter : public mate::Wrappable<T> {
|
||||
public:
|
||||
typedef std::vector<v8::Local<v8::Value>> ValueArray;
|
||||
using Base = mate::Wrappable<T>;
|
||||
using ValueArray = std::vector<v8::Local<v8::Value>>;
|
||||
|
||||
// Make the convinient methods visible:
|
||||
// https://isocpp.org/wiki/faq/templates#nondependent-name-lookup-members
|
||||
|
@ -64,13 +75,30 @@ class EventEmitter : public Base {
|
|||
v8::Locker locker(isolate());
|
||||
v8::HandleScope handle_scope(isolate());
|
||||
v8::Local<v8::Object> wrapper = GetWrapper();
|
||||
if (wrapper.IsEmpty()) {
|
||||
if (wrapper.IsEmpty())
|
||||
return false;
|
||||
}
|
||||
v8::Local<v8::Object> event = internal::CreateEvent(isolate(), wrapper);
|
||||
return EmitWithEvent(name, event, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// this.emit(name, new Event(sender, message), args...);
|
||||
template <typename... Args>
|
||||
bool EmitWithSender(
|
||||
base::StringPiece name,
|
||||
content::RenderFrameHost* sender,
|
||||
base::Optional<electron::mojom::ElectronBrowser::InvokeCallback> callback,
|
||||
Args&&... args) {
|
||||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
||||
v8::Locker locker(isolate());
|
||||
v8::HandleScope handle_scope(isolate());
|
||||
v8::Local<v8::Object> wrapper = GetWrapper();
|
||||
if (wrapper.IsEmpty())
|
||||
return false;
|
||||
v8::Local<v8::Object> event = internal::CreateNativeEvent(
|
||||
isolate(), wrapper, sender, std::move(callback));
|
||||
return EmitWithEvent(name, event, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
protected:
|
||||
EventEmitter() {}
|
||||
|
||||
|
|
71
shell/common/gin_helper/trackable_object.cc
Normal file
71
shell/common/gin_helper/trackable_object.cc
Normal file
|
@ -0,0 +1,71 @@
|
|||
// 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/gin_helper/trackable_object.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/supports_user_data.h"
|
||||
#include "shell/browser/atom_browser_main_parts.h"
|
||||
#include "shell/common/api/locker.h"
|
||||
|
||||
namespace gin_helper {
|
||||
|
||||
namespace {
|
||||
|
||||
const char* kTrackedObjectKey = "TrackedObjectKey";
|
||||
|
||||
class IDUserData : public base::SupportsUserData::Data {
|
||||
public:
|
||||
explicit IDUserData(int32_t id) : id_(id) {}
|
||||
|
||||
operator int32_t() const { return id_; }
|
||||
|
||||
private:
|
||||
int32_t id_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(IDUserData);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
TrackableObjectBase::TrackableObjectBase() : weak_factory_(this) {
|
||||
// TODO(zcbenz): Make TrackedObject work in renderer process.
|
||||
DCHECK(mate::Locker::IsBrowserProcess())
|
||||
<< "This class only works for browser process";
|
||||
|
||||
electron::AtomBrowserMainParts::Get()->RegisterDestructionCallback(
|
||||
GetDestroyClosure());
|
||||
}
|
||||
|
||||
TrackableObjectBase::~TrackableObjectBase() = default;
|
||||
|
||||
base::OnceClosure TrackableObjectBase::GetDestroyClosure() {
|
||||
return base::BindOnce(&TrackableObjectBase::Destroy,
|
||||
weak_factory_.GetWeakPtr());
|
||||
}
|
||||
|
||||
void TrackableObjectBase::Destroy() {
|
||||
delete this;
|
||||
}
|
||||
|
||||
void TrackableObjectBase::AttachAsUserData(base::SupportsUserData* wrapped) {
|
||||
wrapped->SetUserData(kTrackedObjectKey,
|
||||
std::make_unique<IDUserData>(weak_map_id_));
|
||||
}
|
||||
|
||||
// static
|
||||
int32_t TrackableObjectBase::GetIDFromWrappedClass(
|
||||
base::SupportsUserData* wrapped) {
|
||||
if (wrapped) {
|
||||
auto* id =
|
||||
static_cast<IDUserData*>(wrapped->GetUserData(kTrackedObjectKey));
|
||||
if (id)
|
||||
return *id;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace gin_helper
|
135
shell/common/gin_helper/trackable_object.h
Normal file
135
shell/common/gin_helper/trackable_object.h
Normal file
|
@ -0,0 +1,135 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_COMMON_GIN_HELPER_TRACKABLE_OBJECT_H_
|
||||
#define SHELL_COMMON_GIN_HELPER_TRACKABLE_OBJECT_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#include "shell/common/gin_helper/event_emitter.h"
|
||||
#include "shell/common/key_weak_map.h"
|
||||
|
||||
namespace base {
|
||||
class SupportsUserData;
|
||||
}
|
||||
|
||||
namespace gin_helper {
|
||||
|
||||
// Users should use TrackableObject instead.
|
||||
class TrackableObjectBase {
|
||||
public:
|
||||
TrackableObjectBase();
|
||||
|
||||
// The ID in weak map.
|
||||
int32_t weak_map_id() const { return weak_map_id_; }
|
||||
|
||||
// Wrap TrackableObject into a class that SupportsUserData.
|
||||
void AttachAsUserData(base::SupportsUserData* wrapped);
|
||||
|
||||
// Get the weak_map_id from SupportsUserData.
|
||||
static int32_t GetIDFromWrappedClass(base::SupportsUserData* wrapped);
|
||||
|
||||
protected:
|
||||
virtual ~TrackableObjectBase();
|
||||
|
||||
// Returns a closure that can destroy the native class.
|
||||
base::OnceClosure GetDestroyClosure();
|
||||
|
||||
int32_t weak_map_id_ = 0;
|
||||
|
||||
private:
|
||||
void Destroy();
|
||||
|
||||
base::WeakPtrFactory<TrackableObjectBase> weak_factory_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(TrackableObjectBase);
|
||||
};
|
||||
|
||||
// All instances of TrackableObject will be kept in a weak map and can be got
|
||||
// from its ID.
|
||||
template <typename T>
|
||||
class TrackableObject : public TrackableObjectBase, public EventEmitter<T> {
|
||||
public:
|
||||
// Mark the JS object as destroyed.
|
||||
void MarkDestroyed() {
|
||||
v8::Local<v8::Object> wrapper = mate::Wrappable<T>::GetWrapper();
|
||||
if (!wrapper.IsEmpty()) {
|
||||
wrapper->SetAlignedPointerInInternalField(0, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
bool IsDestroyed() {
|
||||
v8::Local<v8::Object> wrapper = mate::Wrappable<T>::GetWrapper();
|
||||
return wrapper->InternalFieldCount() == 0 ||
|
||||
wrapper->GetAlignedPointerFromInternalField(0) == nullptr;
|
||||
}
|
||||
|
||||
// Finds out the TrackableObject from its ID in weak map.
|
||||
static T* FromWeakMapID(v8::Isolate* isolate, int32_t id) {
|
||||
if (!weak_map_)
|
||||
return nullptr;
|
||||
|
||||
v8::MaybeLocal<v8::Object> object = weak_map_->Get(isolate, id);
|
||||
if (object.IsEmpty())
|
||||
return nullptr;
|
||||
|
||||
T* self = nullptr;
|
||||
gin::ConvertFromV8(isolate, object.ToLocalChecked(), &self);
|
||||
return self;
|
||||
}
|
||||
|
||||
// Finds out the TrackableObject from the class it wraps.
|
||||
static T* FromWrappedClass(v8::Isolate* isolate,
|
||||
base::SupportsUserData* wrapped) {
|
||||
int32_t id = GetIDFromWrappedClass(wrapped);
|
||||
if (!id)
|
||||
return nullptr;
|
||||
return FromWeakMapID(isolate, id);
|
||||
}
|
||||
|
||||
// Returns all objects in this class's weak map.
|
||||
static std::vector<v8::Local<v8::Object>> GetAll(v8::Isolate* isolate) {
|
||||
if (weak_map_)
|
||||
return weak_map_->Values(isolate);
|
||||
else
|
||||
return std::vector<v8::Local<v8::Object>>();
|
||||
}
|
||||
|
||||
// Removes this instance from the weak map.
|
||||
void RemoveFromWeakMap() {
|
||||
if (weak_map_ && weak_map_->Has(weak_map_id()))
|
||||
weak_map_->Remove(weak_map_id());
|
||||
}
|
||||
|
||||
protected:
|
||||
TrackableObject() { weak_map_id_ = ++next_id_; }
|
||||
|
||||
~TrackableObject() override { RemoveFromWeakMap(); }
|
||||
|
||||
void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) override {
|
||||
mate::WrappableBase::InitWith(isolate, wrapper);
|
||||
if (!weak_map_) {
|
||||
weak_map_ = new electron::KeyWeakMap<int32_t>;
|
||||
}
|
||||
weak_map_->Set(isolate, weak_map_id_, wrapper);
|
||||
}
|
||||
|
||||
private:
|
||||
static int32_t next_id_;
|
||||
static electron::KeyWeakMap<int32_t>* weak_map_; // leaked on purpose
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(TrackableObject);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
int32_t TrackableObject<T>::next_id_ = 0;
|
||||
|
||||
template <typename T>
|
||||
electron::KeyWeakMap<int32_t>* TrackableObject<T>::weak_map_ = nullptr;
|
||||
|
||||
} // namespace gin_helper
|
||||
|
||||
#endif // SHELL_COMMON_GIN_HELPER_TRACKABLE_OBJECT_H_
|
|
@ -11,7 +11,6 @@
|
|||
|
||||
#include "base/strings/string_util.h"
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "content/public/browser/native_web_keyboard_event.h"
|
||||
#include "gin/converter.h"
|
||||
#include "mojo/public/cpp/base/values_mojom_traits.h"
|
||||
#include "mojo/public/mojom/base/values.mojom.h"
|
||||
|
@ -20,6 +19,7 @@
|
|||
#include "shell/common/keyboard_util.h"
|
||||
#include "shell/common/native_mate_converters/value_converter.h"
|
||||
#include "third_party/blink/public/platform/web_input_event.h"
|
||||
#include "third_party/blink/public/platform/web_keyboard_event.h"
|
||||
#include "third_party/blink/public/platform/web_mouse_event.h"
|
||||
#include "third_party/blink/public/platform/web_mouse_wheel_event.h"
|
||||
#include "third_party/blink/public/web/web_device_emulation_params.h"
|
||||
|
@ -216,42 +216,6 @@ bool Converter<blink::WebKeyboardEvent>::FromV8(v8::Isolate* isolate,
|
|||
return true;
|
||||
}
|
||||
|
||||
bool Converter<content::NativeWebKeyboardEvent>::FromV8(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
content::NativeWebKeyboardEvent* out) {
|
||||
mate::Dictionary dict;
|
||||
if (!ConvertFromV8(isolate, val, &dict))
|
||||
return false;
|
||||
if (!ConvertFromV8(isolate, val, static_cast<blink::WebKeyboardEvent*>(out)))
|
||||
return false;
|
||||
dict.Get("skipInBrowser", &out->skip_in_browser);
|
||||
return true;
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Converter<content::NativeWebKeyboardEvent>::ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const content::NativeWebKeyboardEvent& in) {
|
||||
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
||||
|
||||
if (in.GetType() == blink::WebInputEvent::Type::kRawKeyDown)
|
||||
dict.Set("type", "keyDown");
|
||||
else if (in.GetType() == blink::WebInputEvent::Type::kKeyUp)
|
||||
dict.Set("type", "keyUp");
|
||||
dict.Set("key", ui::KeycodeConverter::DomKeyToKeyString(in.dom_key));
|
||||
dict.Set("code", ui::KeycodeConverter::DomCodeToCodeString(
|
||||
static_cast<ui::DomCode>(in.dom_code)));
|
||||
|
||||
using Modifiers = blink::WebInputEvent::Modifiers;
|
||||
dict.Set("isAutoRepeat", (in.GetModifiers() & Modifiers::kIsAutoRepeat) != 0);
|
||||
dict.Set("shift", (in.GetModifiers() & Modifiers::kShiftKey) != 0);
|
||||
dict.Set("control", (in.GetModifiers() & Modifiers::kControlKey) != 0);
|
||||
dict.Set("alt", (in.GetModifiers() & Modifiers::kAltKey) != 0);
|
||||
dict.Set("meta", (in.GetModifiers() & Modifiers::kMetaKey) != 0);
|
||||
|
||||
return dict.GetHandle();
|
||||
}
|
||||
|
||||
bool Converter<blink::WebMouseEvent>::FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
blink::WebMouseEvent* out) {
|
||||
|
|
|
@ -21,10 +21,6 @@ struct WebPoint;
|
|||
struct WebSize;
|
||||
} // namespace blink
|
||||
|
||||
namespace content {
|
||||
struct NativeWebKeyboardEvent;
|
||||
}
|
||||
|
||||
namespace mate {
|
||||
|
||||
blink::WebInputEvent::Type GetWebInputEventType(v8::Isolate* isolate,
|
||||
|
@ -44,15 +40,6 @@ struct Converter<blink::WebKeyboardEvent> {
|
|||
blink::WebKeyboardEvent* out);
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<content::NativeWebKeyboardEvent> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
content::NativeWebKeyboardEvent* out);
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const content::NativeWebKeyboardEvent& in);
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<blink::WebMouseEvent> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
|
|
|
@ -1,94 +0,0 @@
|
|||
// Copyright (c) 2018 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/common/native_mate_converters/network_converter.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "base/numerics/safe_conversions.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "services/network/public/cpp/resource_request_body.h"
|
||||
#include "shell/common/native_mate_converters/value_converter.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
// static
|
||||
v8::Local<v8::Value>
|
||||
Converter<scoped_refptr<network::ResourceRequestBody>>::ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const scoped_refptr<network::ResourceRequestBody>& val) {
|
||||
if (!val)
|
||||
return v8::Null(isolate);
|
||||
auto list = std::make_unique<base::ListValue>();
|
||||
for (const auto& element : *(val->elements())) {
|
||||
auto post_data_dict = std::make_unique<base::DictionaryValue>();
|
||||
auto type = element.type();
|
||||
if (type == network::mojom::DataElementType::kBytes) {
|
||||
auto bytes = std::make_unique<base::Value>(std::vector<char>(
|
||||
element.bytes(), element.bytes() + (element.length())));
|
||||
post_data_dict->SetString("type", "rawData");
|
||||
post_data_dict->Set("bytes", std::move(bytes));
|
||||
} else if (type == network::mojom::DataElementType::kFile) {
|
||||
post_data_dict->SetString("type", "file");
|
||||
post_data_dict->SetKey("filePath",
|
||||
base::Value(element.path().AsUTF8Unsafe()));
|
||||
post_data_dict->SetInteger("offset", static_cast<int>(element.offset()));
|
||||
post_data_dict->SetInteger("length", static_cast<int>(element.length()));
|
||||
post_data_dict->SetDouble(
|
||||
"modificationTime", element.expected_modification_time().ToDoubleT());
|
||||
} else if (type == network::mojom::DataElementType::kBlob) {
|
||||
post_data_dict->SetString("type", "blob");
|
||||
post_data_dict->SetString("blobUUID", element.blob_uuid());
|
||||
}
|
||||
list->Append(std::move(post_data_dict));
|
||||
}
|
||||
return ConvertToV8(isolate, *list);
|
||||
}
|
||||
|
||||
// static
|
||||
bool Converter<scoped_refptr<network::ResourceRequestBody>>::FromV8(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
scoped_refptr<network::ResourceRequestBody>* out) {
|
||||
auto list = std::make_unique<base::ListValue>();
|
||||
if (!ConvertFromV8(isolate, val, list.get()))
|
||||
return false;
|
||||
*out = new network::ResourceRequestBody();
|
||||
for (size_t i = 0; i < list->GetSize(); ++i) {
|
||||
base::DictionaryValue* dict = nullptr;
|
||||
std::string type;
|
||||
if (!list->GetDictionary(i, &dict))
|
||||
return false;
|
||||
dict->GetString("type", &type);
|
||||
if (type == "rawData") {
|
||||
base::Value* bytes = nullptr;
|
||||
dict->GetBinary("bytes", &bytes);
|
||||
(*out)->AppendBytes(
|
||||
reinterpret_cast<const char*>(bytes->GetBlob().data()),
|
||||
base::checked_cast<int>(bytes->GetBlob().size()));
|
||||
} else if (type == "file") {
|
||||
std::string file;
|
||||
int offset = 0, length = -1;
|
||||
double modification_time = 0.0;
|
||||
dict->GetStringWithoutPathExpansion("filePath", &file);
|
||||
dict->GetInteger("offset", &offset);
|
||||
dict->GetInteger("file", &length);
|
||||
dict->GetDouble("modificationTime", &modification_time);
|
||||
(*out)->AppendFileRange(base::FilePath::FromUTF8Unsafe(file),
|
||||
static_cast<uint64_t>(offset),
|
||||
static_cast<uint64_t>(length),
|
||||
base::Time::FromDoubleT(modification_time));
|
||||
} else if (type == "blob") {
|
||||
std::string uuid;
|
||||
dict->GetString("blobUUID", &uuid);
|
||||
(*out)->AppendBlob(uuid);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace mate
|
|
@ -1,29 +0,0 @@
|
|||
// Copyright (c) 2018 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_COMMON_NATIVE_MATE_CONVERTERS_NETWORK_CONVERTER_H_
|
||||
#define SHELL_COMMON_NATIVE_MATE_CONVERTERS_NETWORK_CONVERTER_H_
|
||||
|
||||
#include "base/memory/scoped_refptr.h"
|
||||
#include "native_mate/converter.h"
|
||||
|
||||
namespace network {
|
||||
class ResourceRequestBody;
|
||||
}
|
||||
|
||||
namespace mate {
|
||||
|
||||
template <>
|
||||
struct Converter<scoped_refptr<network::ResourceRequestBody>> {
|
||||
static v8::Local<v8::Value> ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const scoped_refptr<network::ResourceRequestBody>& val);
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
scoped_refptr<network::ResourceRequestBody>* out);
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // SHELL_COMMON_NATIVE_MATE_CONVERTERS_NETWORK_CONVERTER_H_
|
|
@ -1,34 +0,0 @@
|
|||
// Copyright (c) 2016 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_COMMON_NATIVE_MATE_CONVERTERS_UI_BASE_TYPES_CONVERTER_H_
|
||||
#define SHELL_COMMON_NATIVE_MATE_CONVERTERS_UI_BASE_TYPES_CONVERTER_H_
|
||||
|
||||
#include "native_mate/converter.h"
|
||||
#include "ui/base/ui_base_types.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
template <>
|
||||
struct Converter<ui::MenuSourceType> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const ui::MenuSourceType& in) {
|
||||
switch (in) {
|
||||
case ui::MENU_SOURCE_MOUSE:
|
||||
return mate::StringToV8(isolate, "mouse");
|
||||
case ui::MENU_SOURCE_KEYBOARD:
|
||||
return mate::StringToV8(isolate, "keyboard");
|
||||
case ui::MENU_SOURCE_TOUCH:
|
||||
return mate::StringToV8(isolate, "touch");
|
||||
case ui::MENU_SOURCE_TOUCH_EDIT_MENU:
|
||||
return mate::StringToV8(isolate, "touchMenu");
|
||||
default:
|
||||
return mate::StringToV8(isolate, "none");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // SHELL_COMMON_NATIVE_MATE_CONVERTERS_UI_BASE_TYPES_CONVERTER_H_
|
Loading…
Add table
Add a link
Reference in a new issue