electron/shell/common/gin_helper/callback.cc

157 lines
5 KiB
C++
Raw Normal View History

// 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.
#include "shell/common/gin_helper/callback.h"
chore: bump chromium to 92.0.4488.0 (master) (#28676) * chore: bump chromium in DEPS to 92.0.4478.0 * chore: update chromium patches * chore: update v8 patches * fix: add scale parameter to LookupIconFromFilepath Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2748317 Follow up: https://github.com/electron/electron/issues/28678 * build: depend on gtkprint config for gtk_util.h Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2824022 * build: add missing print_job_constants header Refs: unknown * chore: bump chromium in DEPS to 92.0.4479.0 * update patches * chore: bump chromium in DEPS to 92.0.4480.0 * chore: bump chromium in DEPS to 92.0.4481.0 * chore: bump chromium in DEPS to 92.0.4482.2 * chore: bump chromium in DEPS to 92.0.4483.0 * chore: update patches * chore: bump chromium in DEPS to 92.0.4484.0 * chore: bump chromium in DEPS to 92.0.4485.0 * fix patches * update patches * 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...") https://chromium-review.googlesource.com/c/chromium/src/+/2810414 * 2781233: NotificationService: Plumb document_url for non-persistent notifications. https://chromium-review.googlesource.com/c/chromium/src/+/2781233 * fixup! 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...") * 2836669: Refactor GTK build target and dependencies https://chromium-review.googlesource.com/c/chromium/src/+/2836669 * chore: bump chromium in DEPS to 92.0.4486.0 * update patches * fix DecrementCapturerCount patch * explicitly include badging.mojom.h * include ui/gtk/gtk_ui_factory.h for BuildGtkUi() * fixup! 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...") * iwyu fix for base::size * iwyu for TRACE_EVENT0 * 2799631: Use structured interface for DevTools messages https://chromium-review.googlesource.com/c/chromium/src/+/2799631 * 2801573: Convert enum to enum class for Widget::InitParams::Activatable https://chromium-review.googlesource.com/c/chromium/src/+/2801573 * 2805764: Add ContentBrowserClient support for service worker-scoped binders https://chromium-review.googlesource.com/c/chromium/src/+/2805764 * fixup! 2799631: Use structured interface for DevTools messages * fixup! 2805764: Add ContentBrowserClient support for service worker-scoped binders * oops, use of linux_ui after std::move * fix devtools message handling for null params * disable node test parallel/test-debug-args https://chromium-review.googlesource.com/c/v8/v8/+/2843348 * fix gn check * chore: bump chromium in DEPS to 92.0.4487.0 * chore: update patches * chore: bump chromium in DEPS to 92.0.4488.0 * update patches * Remove vpython use from Chromium DEPS file https://chromium-review.googlesource.com/c/chromium/src/+/2810121 * Partial revert "workaround: disable CFG longjmp protection for Windows on Arm" https://chromium-review.googlesource.com/c/chromium/src/+/2788210 Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: Jeremy Rose <nornagon@nornagon.net>
2021-04-27 21:27:34 +00:00
#include "base/stl_util.h"
#include "content/public/browser/browser_thread.h"
#include "gin/dictionary.h"
namespace gin_helper {
namespace {
struct TranslaterHolder {
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;
Translater translater;
};
// Cached JavaScript version of |CallTranslater|.
v8::Persistent<v8::FunctionTemplate> g_call_translater;
void CallTranslater(v8::Local<v8::External> external,
v8::Local<v8::Object> state,
gin::Arguments* args) {
// Whether the callback should only be called once.
v8::Isolate* isolate = args->isolate();
auto context = isolate->GetCurrentContext();
bool one_time =
state->Has(context, gin::StringToSymbol(isolate, "oneTime")).ToChecked();
// Check if the callback has already been called.
if (one_time) {
auto called_symbol = gin::StringToSymbol(isolate, "called");
if (state->Has(context, called_symbol).ToChecked()) {
args->ThrowTypeError("One-time callback was called more than once");
return;
} else {
state->Set(context, called_symbol, v8::Boolean::New(isolate, true))
.ToChecked();
}
}
auto* holder = static_cast<TranslaterHolder*>(external->Value());
holder->translater.Run(args);
// Free immediately for one-time callback.
if (one_time)
delete holder;
}
} // namespace
// Destroy the class on UI thread when possible.
struct DeleteOnUIThread {
2018-04-18 01:55:30 +00:00
template <typename T>
static void Destruct(const T* x) {
if (gin_helper::Locker::IsBrowserProcess() &&
!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
content::BrowserThread::DeleteSoon(content::BrowserThread::UI, FROM_HERE,
x);
} 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> {
public:
RefCountedGlobal(v8::Isolate* isolate, v8::Local<v8::Value> value)
: handle_(isolate, value.As<T>()) {}
2018-04-18 01:55:30 +00:00
bool IsAlive() const { return !handle_.IsEmpty(); }
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);
};
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)) {}
SafeV8Function::SafeV8Function(const SafeV8Function& other) = default;
SafeV8Function::~SafeV8Function() = default;
bool SafeV8Function::IsAlive() const {
return v8_function_.get() && v8_function_->IsAlive();
}
v8::Local<v8::Function> SafeV8Function::NewHandle(v8::Isolate* isolate) const {
return v8_function_->NewHandle(isolate);
}
v8::Local<v8::Value> CreateFunctionFromTranslater(v8::Isolate* isolate,
const Translater& translater,
bool one_time) {
// The FunctionTemplate is cached.
if (g_call_translater.IsEmpty())
g_call_translater.Reset(
isolate,
CreateFunctionTemplate(isolate, base::BindRepeating(&CallTranslater)));
v8::Local<v8::FunctionTemplate> call_translater =
v8::Local<v8::FunctionTemplate>::New(isolate, g_call_translater);
auto* holder = new TranslaterHolder(isolate);
holder->translater = translater;
gin::Dictionary state = gin::Dictionary::CreateEmpty(isolate);
if (one_time)
state.Set("oneTime", true);
auto context = isolate->GetCurrentContext();
return BindFunctionWith(
isolate, context, call_translater->GetFunction(context).ToLocalChecked(),
holder->handle.Get(isolate), gin::ConvertToV8(isolate, state));
}
// 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) {
v8::MaybeLocal<v8::Value> bind =
func->Get(context, gin::StringToV8(isolate, "bind"));
CHECK(!bind.IsEmpty());
v8::Local<v8::Function> bind_func = bind.ToLocalChecked().As<v8::Function>();
v8::Local<v8::Value> converted[] = {func, arg1, arg2};
return bind_func->Call(context, func, base::size(converted), converted)
.ToLocalChecked();
}
} // namespace gin_helper