2019-09-04 15:45:25 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc. All rights reserved.
|
2015-10-28 09:36:01 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-09-04 15:45:25 +00:00
|
|
|
#include "shell/common/gin_helper/callback.h"
|
2015-11-06 12:23:41 +00:00
|
|
|
|
2021-04-27 21:27:34 +00:00
|
|
|
#include "base/stl_util.h"
|
2019-09-04 15:45:25 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "gin/dictionary.h"
|
2015-10-28 09:36:01 +00:00
|
|
|
|
2019-09-04 15:45:25 +00:00
|
|
|
namespace gin_helper {
|
2015-10-28 09:36:01 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-10-28 12:44:46 +00:00
|
|
|
struct TranslaterHolder {
|
2018-10-04 14:13:09 +00:00
|
|
|
explicit TranslaterHolder(v8::Isolate* isolate)
|
|
|
|
: handle(isolate, v8::External::New(isolate, this)) {
|
|
|
|
handle.SetWeak(this, &GC, v8::WeakCallbackType::kFinalizer);
|
|
|
|
}
|
|
|
|
~TranslaterHolder() {
|
|
|
|
if (!handle.IsEmpty()) {
|
|
|
|
handle.ClearWeak();
|
|
|
|
handle.Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GC(const v8::WeakCallbackInfo<TranslaterHolder>& data) {
|
|
|
|
delete data.GetParameter();
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Global<v8::External> handle;
|
2015-10-28 09:36:01 +00:00
|
|
|
Translater translater;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Cached JavaScript version of |CallTranslater|.
|
|
|
|
v8::Persistent<v8::FunctionTemplate> g_call_translater;
|
|
|
|
|
2015-10-28 12:44:46 +00:00
|
|
|
void CallTranslater(v8::Local<v8::External> external,
|
|
|
|
v8::Local<v8::Object> state,
|
2019-09-04 15:45:25 +00:00
|
|
|
gin::Arguments* args) {
|
2020-03-12 01:07:54 +00:00
|
|
|
// Whether the callback should only be called once.
|
2015-10-28 12:44:46 +00:00
|
|
|
v8::Isolate* isolate = args->isolate();
|
2019-01-09 19:17:05 +00:00
|
|
|
auto context = isolate->GetCurrentContext();
|
|
|
|
bool one_time =
|
2019-09-06 05:52:54 +00:00
|
|
|
state->Has(context, gin::StringToSymbol(isolate, "oneTime")).ToChecked();
|
2015-10-28 12:44:46 +00:00
|
|
|
|
|
|
|
// Check if the callback has already been called.
|
2018-10-04 14:13:09 +00:00
|
|
|
if (one_time) {
|
2019-09-06 05:52:54 +00:00
|
|
|
auto called_symbol = gin::StringToSymbol(isolate, "called");
|
2019-01-09 19:17:05 +00:00
|
|
|
if (state->Has(context, called_symbol).ToChecked()) {
|
2020-03-12 01:07:54 +00:00
|
|
|
args->ThrowTypeError("One-time callback was called more than once");
|
2018-10-04 14:13:09 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2019-01-09 19:17:05 +00:00
|
|
|
state->Set(context, called_symbol, v8::Boolean::New(isolate, true))
|
|
|
|
.ToChecked();
|
2018-10-04 14:13:09 +00:00
|
|
|
}
|
2015-10-28 12:44:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 18:56:31 +00:00
|
|
|
auto* holder = static_cast<TranslaterHolder*>(external->Value());
|
2015-10-28 09:36:01 +00:00
|
|
|
holder->translater.Run(args);
|
2018-10-04 14:13:09 +00:00
|
|
|
|
|
|
|
// Free immediately for one-time callback.
|
|
|
|
if (one_time)
|
|
|
|
delete holder;
|
2015-10-28 09:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-12-03 03:24:33 +00:00
|
|
|
// Destroy the class on UI thread when possible.
|
|
|
|
struct DeleteOnUIThread {
|
2018-04-18 01:55:30 +00:00
|
|
|
template <typename T>
|
2015-12-03 03:24:33 +00:00
|
|
|
static void Destruct(const T* x) {
|
2019-10-31 07:56:00 +00:00
|
|
|
if (gin_helper::Locker::IsBrowserProcess() &&
|
2019-09-04 15:45:25 +00:00
|
|
|
!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
|
|
|
content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE,
|
|
|
|
x);
|
2015-12-03 03:24:33 +00:00
|
|
|
} else {
|
|
|
|
delete x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Like v8::Global, but ref-counted.
|
2018-04-18 01:55:30 +00:00
|
|
|
template <typename T>
|
|
|
|
class RefCountedGlobal
|
|
|
|
: public base::RefCountedThreadSafe<RefCountedGlobal<T>, DeleteOnUIThread> {
|
2015-12-03 03:24:33 +00:00
|
|
|
public:
|
|
|
|
RefCountedGlobal(v8::Isolate* isolate, v8::Local<v8::Value> value)
|
2021-05-12 07:38:21 +00:00
|
|
|
: handle_(isolate, value.As<T>()) {}
|
2015-12-03 03:24:33 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
bool IsAlive() const { return !handle_.IsEmpty(); }
|
2015-12-03 03:24:33 +00:00
|
|
|
|
|
|
|
v8::Local<T> NewHandle(v8::Isolate* isolate) const {
|
|
|
|
return v8::Local<T>::New(isolate, handle_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
v8::Global<T> handle_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(RefCountedGlobal);
|
|
|
|
};
|
|
|
|
|
2015-11-06 12:23:41 +00:00
|
|
|
SafeV8Function::SafeV8Function(v8::Isolate* isolate, v8::Local<v8::Value> value)
|
2018-04-18 01:55:30 +00:00
|
|
|
: v8_function_(new RefCountedGlobal<v8::Function>(isolate, value)) {}
|
2015-11-06 12:23:41 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
SafeV8Function::SafeV8Function(const SafeV8Function& other) = default;
|
2015-11-06 12:23:41 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
SafeV8Function::~SafeV8Function() = default;
|
2015-11-06 12:23:41 +00:00
|
|
|
|
2015-12-03 03:24:33 +00:00
|
|
|
bool SafeV8Function::IsAlive() const {
|
|
|
|
return v8_function_.get() && v8_function_->IsAlive();
|
2015-11-06 12:23:41 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 03:24:33 +00:00
|
|
|
v8::Local<v8::Function> SafeV8Function::NewHandle(v8::Isolate* isolate) const {
|
|
|
|
return v8_function_->NewHandle(isolate);
|
2015-11-06 12:23:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 15:38:27 +00:00
|
|
|
v8::Local<v8::Value> CreateFunctionFromTranslater(v8::Isolate* isolate,
|
|
|
|
const Translater& translater,
|
|
|
|
bool one_time) {
|
2015-10-28 09:36:01 +00:00
|
|
|
// The FunctionTemplate is cached.
|
|
|
|
if (g_call_translater.IsEmpty())
|
2019-09-04 15:45:25 +00:00
|
|
|
g_call_translater.Reset(
|
|
|
|
isolate,
|
|
|
|
CreateFunctionTemplate(isolate, base::BindRepeating(&CallTranslater)));
|
2015-10-28 09:36:01 +00:00
|
|
|
|
|
|
|
v8::Local<v8::FunctionTemplate> call_translater =
|
|
|
|
v8::Local<v8::FunctionTemplate>::New(isolate, g_call_translater);
|
2018-10-04 14:13:09 +00:00
|
|
|
auto* holder = new TranslaterHolder(isolate);
|
2015-10-28 09:36:01 +00:00
|
|
|
holder->translater = translater;
|
2019-09-04 15:45:25 +00:00
|
|
|
gin::Dictionary state = gin::Dictionary::CreateEmpty(isolate);
|
2018-10-04 14:13:09 +00:00
|
|
|
if (one_time)
|
|
|
|
state.Set("oneTime", true);
|
2019-01-09 19:17:05 +00:00
|
|
|
auto context = isolate->GetCurrentContext();
|
|
|
|
return BindFunctionWith(
|
|
|
|
isolate, context, call_translater->GetFunction(context).ToLocalChecked(),
|
2019-09-04 15:45:25 +00:00
|
|
|
holder->handle.Get(isolate), gin::ConvertToV8(isolate, state));
|
2015-10-28 09:36:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 18:00:41 +00:00
|
|
|
// func.bind(func, arg1).
|
|
|
|
// NB(zcbenz): Using C++11 version crashes VS.
|
|
|
|
v8::Local<v8::Value> BindFunctionWith(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::Function> func,
|
|
|
|
v8::Local<v8::Value> arg1,
|
|
|
|
v8::Local<v8::Value> arg2) {
|
2019-05-01 00:18:22 +00:00
|
|
|
v8::MaybeLocal<v8::Value> bind =
|
2019-09-06 05:52:54 +00:00
|
|
|
func->Get(context, gin::StringToV8(isolate, "bind"));
|
2017-11-03 18:00:41 +00:00
|
|
|
CHECK(!bind.IsEmpty());
|
2021-05-12 07:38:21 +00:00
|
|
|
v8::Local<v8::Function> bind_func = bind.ToLocalChecked().As<v8::Function>();
|
2017-11-03 18:00:41 +00:00
|
|
|
v8::Local<v8::Value> converted[] = {func, arg1, arg2};
|
2019-01-21 18:26:33 +00:00
|
|
|
return bind_func->Call(context, func, base::size(converted), converted)
|
2017-11-03 18:00:41 +00:00
|
|
|
.ToLocalChecked();
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:45:25 +00:00
|
|
|
} // namespace gin_helper
|