refactor: Clean up the implementation of the registerStreamProtocol (#11357)

* Use weak pointer to avoid race condition

* Use DeleteSoon to delete pointer across threads

* Simplify EventSubscriber

* No need to manually mange V8 convertions

* Fix cpplint warning

We should update cpplint for this, but let's do it in other PR.

* Move UI thread operations to EventSubscriber

* Less and more assertions

Some methods are now private so no more need to assert threads.

* Fix cpplint warnings

* No longer needs the EventEmitted

* EventSubscriber => StreamSubscriber

* Reduce the copies when passing data

* Fix cpplint warnings
This commit is contained in:
Cheng Zhao 2018-10-04 23:13:09 +09:00 committed by John Kleinschmidt
parent 3805c5f538
commit d3ae541397
9 changed files with 314 additions and 373 deletions

View file

@ -108,8 +108,8 @@ struct V8FunctionInvoker<ReturnType(ArgTypes...)> {
// Helper to pass a C++ funtion to JavaScript.
using Translater = base::Callback<void(Arguments* args)>;
v8::Local<v8::Value> CreateFunctionFromTranslater(v8::Isolate* isolate,
const Translater& translater);
v8::Local<v8::Value> CreateFunctionFromTranslater(
v8::Isolate* isolate, const Translater& translater, bool one_time);
v8::Local<v8::Value> BindFunctionWith(v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Function> func,
@ -152,9 +152,11 @@ struct Converter<base::RepeatingCallback<Sig>> {
const base::RepeatingCallback<Sig>& 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<Sig>::Go, val);
return internal::CreateFunctionFromTranslater(isolate, translater);
internal::Translater translater = base::Bind(
&internal::NativeFunctionInvoker<Sig>::Go, val);
// To avoid memory leak, we ensure that the callback can only be called
// for once.
return internal::CreateFunctionFromTranslater(isolate, translater, true);
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
@ -168,6 +170,16 @@ struct Converter<base::RepeatingCallback<Sig>> {
}
};
// Convert a callback to V8 without the call number limitation, this can easily
// cause memory leaks so use it with caution.
template<typename Sig>
v8::Local<v8::Value> CallbackToV8(v8::Isolate* isolate,
const base::Callback<Sig>& val) {
internal::Translater translater = base::Bind(
&internal::NativeFunctionInvoker<Sig>::Go, val);
return internal::CreateFunctionFromTranslater(isolate, translater, false);
}
} // namespace mate
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_CALLBACK_H_