2019-09-19 15:09:15 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_OBJECT_TEMPLATE_BUILDER_H_
|
|
|
|
#define ELECTRON_SHELL_COMMON_GIN_HELPER_OBJECT_TEMPLATE_BUILDER_H_
|
2019-09-19 15:09:15 +00:00
|
|
|
|
2024-01-11 01:00:37 +00:00
|
|
|
#include <string_view>
|
|
|
|
|
2023-05-11 20:07:39 +00:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2019-09-19 15:09:15 +00:00
|
|
|
#include "shell/common/gin_helper/function_template.h"
|
|
|
|
|
|
|
|
namespace gin_helper {
|
|
|
|
|
|
|
|
// This class works like gin::ObjectTemplateBuilder, but operates on existing
|
|
|
|
// prototype template instead of creating a new one.
|
|
|
|
//
|
|
|
|
// It also uses gin_helper::CreateFunctionTemplate for function templates to
|
|
|
|
// support gin_helper types.
|
|
|
|
//
|
|
|
|
// TODO(zcbenz): We should patch gin::ObjectTemplateBuilder to provide the same
|
|
|
|
// functionality after removing gin_helper/function_template.h.
|
|
|
|
class ObjectTemplateBuilder {
|
|
|
|
public:
|
|
|
|
ObjectTemplateBuilder(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::ObjectTemplate> templ);
|
2021-06-01 04:00:09 +00:00
|
|
|
~ObjectTemplateBuilder() = default;
|
2019-09-19 15:09:15 +00:00
|
|
|
|
|
|
|
// It's against Google C++ style to return a non-const ref, but we take some
|
|
|
|
// poetic license here in order that all calls to Set() can be via the '.'
|
|
|
|
// operator and line up nicely.
|
|
|
|
template <typename T>
|
2024-01-11 01:00:37 +00:00
|
|
|
ObjectTemplateBuilder& SetValue(const std::string_view name, T val) {
|
2019-09-19 15:09:15 +00:00
|
|
|
return SetImpl(name, ConvertToV8(isolate_, val));
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the following methods, T and U can be function pointer, member function
|
2021-05-06 22:01:04 +00:00
|
|
|
// pointer, base::RepeatingCallback, or v8::FunctionTemplate. Most clients
|
|
|
|
// will want to use one of the first two options. Also see
|
|
|
|
// gin::CreateFunctionTemplate() for creating raw function templates.
|
2019-09-19 15:09:15 +00:00
|
|
|
template <typename T>
|
2024-01-11 01:00:37 +00:00
|
|
|
ObjectTemplateBuilder& SetMethod(const std::string_view name,
|
2019-09-19 15:09:15 +00:00
|
|
|
const T& callback) {
|
|
|
|
return SetImpl(name, CallbackTraits<T>::CreateTemplate(isolate_, callback));
|
|
|
|
}
|
|
|
|
template <typename T>
|
2024-01-11 01:00:37 +00:00
|
|
|
ObjectTemplateBuilder& SetProperty(const std::string_view name,
|
2019-09-19 15:09:15 +00:00
|
|
|
const T& getter) {
|
|
|
|
return SetPropertyImpl(name,
|
|
|
|
CallbackTraits<T>::CreateTemplate(isolate_, getter),
|
|
|
|
v8::Local<v8::FunctionTemplate>());
|
|
|
|
}
|
|
|
|
template <typename T, typename U>
|
2024-01-11 01:00:37 +00:00
|
|
|
ObjectTemplateBuilder& SetProperty(const std::string_view name,
|
2019-09-19 15:09:15 +00:00
|
|
|
const T& getter,
|
|
|
|
const U& setter) {
|
|
|
|
return SetPropertyImpl(name,
|
|
|
|
CallbackTraits<T>::CreateTemplate(isolate_, getter),
|
2019-10-02 00:30:55 +00:00
|
|
|
CallbackTraits<U>::CreateTemplate(isolate_, setter));
|
2019-09-19 15:09:15 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> Build();
|
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
private:
|
2024-01-11 01:00:37 +00:00
|
|
|
ObjectTemplateBuilder& SetImpl(const std::string_view name,
|
2019-09-19 15:09:15 +00:00
|
|
|
v8::Local<v8::Data> val);
|
|
|
|
ObjectTemplateBuilder& SetPropertyImpl(
|
2024-01-11 01:00:37 +00:00
|
|
|
const std::string_view name,
|
2019-09-19 15:09:15 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> getter,
|
|
|
|
v8::Local<v8::FunctionTemplate> setter);
|
|
|
|
|
2023-05-11 20:07:39 +00:00
|
|
|
raw_ptr<v8::Isolate> isolate_;
|
2019-09-19 15:09:15 +00:00
|
|
|
|
|
|
|
// ObjectTemplateBuilder should only be used on the stack.
|
|
|
|
v8::Local<v8::ObjectTemplate> template_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace gin_helper
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_COMMON_GIN_HELPER_OBJECT_TEMPLATE_BUILDER_H_
|