refactor: migrate dialog API to //gin (#19482)

* get ShowMessageBoxSync working with gin

* move more dialog methods

* all methods moved

* cleanup

* add util func for template creation
This commit is contained in:
Micha Hanselmann 2019-08-01 07:57:41 -07:00 committed by John Kleinschmidt
parent c190e64012
commit b80429ab7f
9 changed files with 209 additions and 59 deletions

View file

@ -0,0 +1,27 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef SHELL_COMMON_API_GIN_UTILS_H_
#define SHELL_COMMON_API_GIN_UTILS_H_
#include "gin/function_template.h"
namespace gin {
// NOTE: V8 caches FunctionTemplates. Therefore it is user's responsibility
// to ensure this function is called for one type only ONCE in the program's
// whole lifetime, otherwise we would have memory leak.
template <typename Sig>
v8::Local<v8::Function> ConvertCallbackToV8Leaked(
v8::Isolate* isolate,
const base::RepeatingCallback<Sig>& callback) {
// LOG(WARNING) << "Leaky conversion from callback to V8 triggered.";
return gin::CreateFunctionTemplate(isolate, callback)
->GetFunction(isolate->GetCurrentContext())
.ToLocalChecked();
}
} // namespace gin
#endif // SHELL_COMMON_API_GIN_UTILS_H_

View file

@ -0,0 +1,33 @@
// Copyright (c) 2019 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_CONVERTERS_FILE_DIALOG_CONVERTER_GIN_ADAPTER_H_
#define SHELL_COMMON_GIN_CONVERTERS_FILE_DIALOG_CONVERTER_GIN_ADAPTER_H_
#include "gin/converter.h"
#include "shell/common/native_mate_converters/file_dialog_converter.h"
// TODO(deermichel): replace adapter with real implementation after removing
// mate
// -- this adapter forwards all conversions to the existing mate converter --
// (other direction might be preferred, but this is safer for now :D)
namespace gin {
template <>
struct Converter<file_dialog::DialogSettings> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const file_dialog::DialogSettings& in) {
return mate::ConvertToV8(isolate, in);
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
file_dialog::DialogSettings* out) {
return mate::ConvertFromV8(isolate, val, out);
}
};
} // namespace gin
#endif // SHELL_COMMON_GIN_CONVERTERS_FILE_DIALOG_CONVERTER_GIN_ADAPTER_H_

View file

@ -0,0 +1,29 @@
// Copyright (c) 2019 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_CONVERTERS_IMAGE_CONVERTER_GIN_ADAPTER_H_
#define SHELL_COMMON_GIN_CONVERTERS_IMAGE_CONVERTER_GIN_ADAPTER_H_
#include "gin/converter.h"
#include "shell/common/native_mate_converters/image_converter.h"
// TODO(deermichel): replace adapter with real implementation after removing
// mate
// -- this adapter forwards all conversions to the existing mate converter --
// (other direction might be preferred, but this is safer for now :D)
namespace gin {
template <>
struct Converter<gfx::ImageSkia> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
gfx::ImageSkia* out) {
return mate::ConvertFromV8(isolate, val, out);
}
};
} // namespace gin
#endif // SHELL_COMMON_GIN_CONVERTERS_IMAGE_CONVERTER_GIN_ADAPTER_H_

View file

@ -1,21 +1,19 @@
// Copyright (c) 2015 GitHub, Inc.
// Copyright (c) 2019 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/message_box_converter.h"
#include "shell/common/gin_converters/message_box_converter.h"
#include "native_mate/dictionary.h"
#include "shell/browser/api/atom_api_browser_window.h"
#include "shell/common/native_mate_converters/file_path_converter.h"
#include "shell/common/native_mate_converters/image_converter.h"
#include "gin/dictionary.h"
#include "shell/common/gin_converters/image_converter_gin_adapter.h"
namespace mate {
namespace gin {
bool Converter<electron::MessageBoxSettings>::FromV8(
v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::MessageBoxSettings* out) {
mate::Dictionary dict;
gin::Dictionary dict(nullptr);
int type = 0;
if (!ConvertFromV8(isolate, val, &dict))
return false;
@ -35,4 +33,4 @@ bool Converter<electron::MessageBoxSettings>::FromV8(
return true;
}
} // namespace mate
} // namespace gin

View file

@ -0,0 +1,33 @@
// Copyright (c) 2019 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_CONVERTERS_MESSAGE_BOX_CONVERTER_H_
#define SHELL_COMMON_GIN_CONVERTERS_MESSAGE_BOX_CONVERTER_H_
#include "gin/converter.h"
#include "shell/browser/api/atom_api_browser_window.h"
#include "shell/browser/ui/message_box.h"
namespace gin {
// TODO(deermichel): remove adapter after removing mate
template <>
struct Converter<electron::NativeWindow*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::NativeWindow** out) {
return mate::ConvertFromV8(isolate, val, out);
}
};
template <>
struct Converter<electron::MessageBoxSettings> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::MessageBoxSettings* out);
};
} // namespace gin
#endif // SHELL_COMMON_GIN_CONVERTERS_MESSAGE_BOX_CONVERTER_H_

View file

@ -0,0 +1,34 @@
// Copyright (c) 2019 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_CONVERTERS_NET_CONVERTER_GIN_ADAPTER_H_
#define SHELL_COMMON_GIN_CONVERTERS_NET_CONVERTER_GIN_ADAPTER_H_
#include "gin/converter.h"
#include "shell/common/native_mate_converters/net_converter.h"
// TODO(deermichel): replace adapter with real implementation after removing
// mate
// -- this adapter forwards all conversions to the existing mate converter --
// (other direction might be preferred, but this is safer for now :D)
namespace gin {
template <>
struct Converter<scoped_refptr<net::X509Certificate>> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const scoped_refptr<net::X509Certificate>& val) {
return mate::ConvertToV8(isolate, val);
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
scoped_refptr<net::X509Certificate>* out) {
return mate::ConvertFromV8(isolate, val, out);
}
};
} // namespace gin
#endif // SHELL_COMMON_GIN_CONVERTERS_NET_CONVERTER_GIN_ADAPTER_H_

View file

@ -1,22 +0,0 @@
// Copyright (c) 2019 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_MESSAGE_BOX_CONVERTER_H_
#define SHELL_COMMON_NATIVE_MATE_CONVERTERS_MESSAGE_BOX_CONVERTER_H_
#include "native_mate/converter.h"
#include "shell/browser/ui/message_box.h"
namespace mate {
template <>
struct Converter<electron::MessageBoxSettings> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::MessageBoxSettings* out);
};
} // namespace mate
#endif // SHELL_COMMON_NATIVE_MATE_CONVERTERS_MESSAGE_BOX_CONVERTER_H_