// Copyright (c) 2015 GitHub, Inc. All rights reserved. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_CALLBACK_H_ #define ATOM_COMMON_NATIVE_MATE_CONVERTERS_CALLBACK_H_ #include #include "atom/common/api/locker.h" #include "base/bind.h" #include "base/callback.h" #include "base/memory/weak_ptr.h" #include "base/message_loop/message_loop.h" #include "content/public/browser/browser_thread.h" #include "native_mate/function_template.h" #include "native_mate/scoped_persistent.h" namespace mate { namespace internal { template class RefCountedGlobal; // Manages the V8 function with RAII. class SafeV8Function { public: SafeV8Function(v8::Isolate* isolate, v8::Local value); SafeV8Function(const SafeV8Function& other); ~SafeV8Function(); bool IsAlive() const; v8::Local NewHandle(v8::Isolate* isolate) const; private: scoped_refptr> v8_function_; }; // Helper to invoke a V8 function with C++ parameters. template struct V8FunctionInvoker {}; template struct V8FunctionInvoker(ArgTypes...)> { static v8::Local Go(v8::Isolate* isolate, const SafeV8Function& function, ArgTypes... raw) { Locker locker(isolate); v8::EscapableHandleScope handle_scope(isolate); if (!function.IsAlive()) return v8::Null(isolate); v8::MicrotasksScope script_scope(isolate, v8::MicrotasksScope::kRunMicrotasks); v8::Local holder = function.NewHandle(isolate); v8::Local context = holder->CreationContext(); v8::Context::Scope context_scope(context); std::vector> args{ConvertToV8(isolate, raw)...}; v8::Local ret(holder->Call( holder, args.size(), args.empty() ? nullptr : &args.front())); return handle_scope.Escape(ret); } }; template struct V8FunctionInvoker { static void Go(v8::Isolate* isolate, const SafeV8Function& function, ArgTypes... raw) { Locker locker(isolate); v8::HandleScope handle_scope(isolate); if (!function.IsAlive()) return; v8::MicrotasksScope script_scope(isolate, v8::MicrotasksScope::kRunMicrotasks); v8::Local holder = function.NewHandle(isolate); v8::Local context = holder->CreationContext(); v8::Context::Scope context_scope(context); std::vector> args{ConvertToV8(isolate, raw)...}; holder->Call(holder, args.size(), args.empty() ? nullptr : &args.front()); } }; template struct V8FunctionInvoker { static ReturnType Go(v8::Isolate* isolate, const SafeV8Function& function, ArgTypes... raw) { Locker locker(isolate); v8::HandleScope handle_scope(isolate); ReturnType ret = ReturnType(); if (!function.IsAlive()) return ret; v8::MicrotasksScope script_scope(isolate, v8::MicrotasksScope::kRunMicrotasks); v8::Local holder = function.NewHandle(isolate); v8::Local context = holder->CreationContext(); v8::Context::Scope context_scope(context); std::vector> args{ConvertToV8(isolate, raw)...}; v8::Local result; auto maybe_result = holder->Call(context, holder, args.size(), args.empty() ? nullptr : &args.front()); if (maybe_result.ToLocal(&result)) Converter::FromV8(isolate, result, &ret); return ret; } }; // Helper to pass a C++ funtion to JavaScript. using Translater = base::Callback; v8::Local CreateFunctionFromTranslater(v8::Isolate* isolate, const Translater& translater); v8::Local BindFunctionWith(v8::Isolate* isolate, v8::Local context, v8::Local func, v8::Local arg1, v8::Local arg2); // Calls callback with Arguments. template struct NativeFunctionInvoker {}; template struct NativeFunctionInvoker { static void Go(base::Callback val, Arguments* args) { using Indices = typename IndicesGenerator::type; Invoker invoker(args, 0); if (invoker.IsOK()) invoker.DispatchToCallback(val); } }; } // namespace internal template struct Converter> { static bool FromV8(v8::Isolate* isolate, v8::Local val, base::OnceCallback* out) { if (!val->IsFunction()) return false; *out = base::BindOnce(&internal::V8FunctionInvoker::Go, isolate, internal::SafeV8Function(isolate, val)); return true; } }; template struct Converter> { static v8::Local ToV8(v8::Isolate* isolate, const base::RepeatingCallback& val) { // We don't use CreateFunctionTemplate here because it creates a new // FunctionTemplate everytime, which is cached by V8 and causes leaks. internal::Translater translater = base::BindRepeating(&internal::NativeFunctionInvoker::Go, val); return internal::CreateFunctionFromTranslater(isolate, translater); } static bool FromV8(v8::Isolate* isolate, v8::Local val, base::RepeatingCallback* out) { if (!val->IsFunction()) return false; *out = base::BindRepeating(&internal::V8FunctionInvoker::Go, isolate, internal::SafeV8Function(isolate, val)); return true; } }; } // namespace mate #endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_CALLBACK_H_