2014-04-15 03:04:36 +00:00
|
|
|
// Copyright 2013 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE.chromium file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_WRAPPABLE_H_
|
|
|
|
#define ELECTRON_SHELL_COMMON_GIN_HELPER_WRAPPABLE_H_
|
2014-04-15 03:04:36 +00:00
|
|
|
|
2023-02-03 11:43:42 +00:00
|
|
|
#include "base/functional/bind.h"
|
2017-03-08 08:11:29 +00:00
|
|
|
#include "gin/per_isolate_data.h"
|
2019-12-05 09:46:34 +00:00
|
|
|
#include "shell/common/gin_helper/constructor.h"
|
2014-04-15 03:04:36 +00:00
|
|
|
|
2019-12-05 09:46:34 +00:00
|
|
|
namespace gin_helper {
|
2014-04-15 03:04:36 +00:00
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
2015-05-22 11:11:02 +00:00
|
|
|
void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val);
|
2014-04-15 03:04:36 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2018-06-29 16:44:28 +00:00
|
|
|
template <typename T>
|
2016-04-25 01:17:39 +00:00
|
|
|
class Wrappable : public WrappableBase {
|
|
|
|
public:
|
2019-08-26 00:16:28 +00:00
|
|
|
Wrappable() = default;
|
2016-04-25 01:17:39 +00:00
|
|
|
|
2018-06-29 16:44:28 +00:00
|
|
|
template <typename Sig>
|
2016-08-02 08:01:19 +00:00
|
|
|
static void SetConstructor(v8::Isolate* isolate,
|
2021-05-06 22:01:04 +00:00
|
|
|
const base::RepeatingCallback<Sig>& constructor) {
|
2020-04-02 23:07:56 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> templ = gin_helper::CreateFunctionTemplate(
|
2021-01-25 01:27:40 +00:00
|
|
|
isolate, base::BindRepeating(&internal::InvokeNew<Sig>, constructor));
|
2016-08-02 09:06:20 +00:00
|
|
|
templ->InstanceTemplate()->SetInternalFieldCount(1);
|
|
|
|
T::BuildPrototype(isolate, templ);
|
2018-06-29 16:44:28 +00:00
|
|
|
gin::PerIsolateData::From(isolate)->SetFunctionTemplate(&kWrapperInfo,
|
|
|
|
templ);
|
2016-08-02 08:01:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 08:20:41 +00:00
|
|
|
static v8::Local<v8::FunctionTemplate> GetConstructor(v8::Isolate* isolate) {
|
2016-04-25 01:17:39 +00:00
|
|
|
// Fill the object template.
|
2018-06-29 16:44:28 +00:00
|
|
|
auto* data = gin::PerIsolateData::From(isolate);
|
2017-03-08 08:11:29 +00:00
|
|
|
auto templ = data->GetFunctionTemplate(&kWrapperInfo);
|
|
|
|
if (templ.IsEmpty()) {
|
|
|
|
templ = v8::FunctionTemplate::New(isolate);
|
2016-08-02 07:08:00 +00:00
|
|
|
templ->InstanceTemplate()->SetInternalFieldCount(1);
|
2016-08-02 09:06:20 +00:00
|
|
|
T::BuildPrototype(isolate, templ);
|
2017-03-08 08:11:29 +00:00
|
|
|
data->SetFunctionTemplate(&kWrapperInfo, templ);
|
2016-04-25 01:17:39 +00:00
|
|
|
}
|
2017-03-08 08:11:29 +00:00
|
|
|
return templ;
|
2016-08-02 08:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Init the class with T::BuildPrototype.
|
|
|
|
void Init(v8::Isolate* isolate) {
|
|
|
|
v8::Local<v8::FunctionTemplate> templ = GetConstructor(isolate);
|
2016-04-25 01:17:39 +00:00
|
|
|
|
|
|
|
// |wrapper| may be empty in some extreme cases, e.g., when
|
|
|
|
// Object.prototype.constructor is overwritten.
|
2016-08-02 08:20:41 +00:00
|
|
|
v8::Local<v8::Object> wrapper;
|
2018-06-29 16:44:28 +00:00
|
|
|
if (!templ->InstanceTemplate()
|
|
|
|
->NewInstance(isolate->GetCurrentContext())
|
|
|
|
.ToLocal(&wrapper)) {
|
2016-04-25 01:17:39 +00:00
|
|
|
// The current wrappable object will be no longer managed by V8. Delete
|
|
|
|
// this now.
|
|
|
|
delete this;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
InitWith(isolate, wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-03-08 08:11:29 +00:00
|
|
|
static gin::WrapperInfo kWrapperInfo;
|
2014-04-15 03:04:36 +00:00
|
|
|
};
|
|
|
|
|
2016-04-25 01:17:39 +00:00
|
|
|
// static
|
2018-06-29 16:44:28 +00:00
|
|
|
template <typename T>
|
|
|
|
gin::WrapperInfo Wrappable<T>::kWrapperInfo = {gin::kEmbedderNativeGin};
|
2014-04-15 03:04:36 +00:00
|
|
|
|
2019-12-05 09:46:34 +00:00
|
|
|
} // namespace gin_helper
|
2014-04-15 03:04:36 +00:00
|
|
|
|
2019-10-15 01:15:23 +00:00
|
|
|
namespace gin {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct Converter<
|
|
|
|
T*,
|
|
|
|
typename std::enable_if<
|
2019-12-05 09:46:34 +00:00
|
|
|
std::is_convertible<T*, gin_helper::WrappableBase*>::value>::type> {
|
2019-10-15 01:15:23 +00:00
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, T* val) {
|
|
|
|
if (val)
|
|
|
|
return val->GetWrapper();
|
|
|
|
else
|
|
|
|
return v8::Null(isolate);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, T** out) {
|
2019-12-05 09:46:34 +00:00
|
|
|
*out = static_cast<T*>(static_cast<gin_helper::WrappableBase*>(
|
|
|
|
gin_helper::internal::FromV8Impl(isolate, val)));
|
2019-10-15 01:15:23 +00:00
|
|
|
return *out != nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace gin
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_COMMON_GIN_HELPER_WRAPPABLE_H_
|