Use the callback converter in JsAsker
This commit is contained in:
parent
5d8a31c140
commit
9847747736
1 changed files with 9 additions and 60 deletions
|
@ -8,7 +8,6 @@
|
||||||
|
|
||||||
#include "atom/common/native_mate_converters/callback.h"
|
#include "atom/common/native_mate_converters/callback.h"
|
||||||
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
||||||
#include "native_mate/function_template.h"
|
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
@ -16,18 +15,12 @@ namespace internal {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
struct CallbackHolder {
|
|
||||||
ResponseCallback callback;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Cached JavaScript version of |HandlerCallback|.
|
|
||||||
v8::Persistent<v8::FunctionTemplate> g_handler_callback_;
|
|
||||||
|
|
||||||
// The callback which is passed to |handler|.
|
// The callback which is passed to |handler|.
|
||||||
void HandlerCallback(v8::Isolate* isolate,
|
void HandlerCallback(const ResponseCallback& callback,
|
||||||
v8::Local<v8::External> external,
|
|
||||||
v8::Local<v8::Object> state,
|
v8::Local<v8::Object> state,
|
||||||
mate::Arguments* args) {
|
mate::Arguments* args) {
|
||||||
|
v8::Isolate* isolate = args->isolate();
|
||||||
|
|
||||||
// Check if the callback has already been called.
|
// Check if the callback has already been called.
|
||||||
v8::Local<v8::String> called_symbol = mate::StringToSymbol(isolate, "called");
|
v8::Local<v8::String> called_symbol = mate::StringToSymbol(isolate, "called");
|
||||||
if (state->Has(called_symbol))
|
if (state->Has(called_symbol))
|
||||||
|
@ -36,14 +29,11 @@ void HandlerCallback(v8::Isolate* isolate,
|
||||||
state->Set(called_symbol, v8::Boolean::New(isolate, true));
|
state->Set(called_symbol, v8::Boolean::New(isolate, true));
|
||||||
|
|
||||||
// If there is no argument passed then we failed.
|
// If there is no argument passed then we failed.
|
||||||
scoped_ptr<CallbackHolder> holder(
|
|
||||||
static_cast<CallbackHolder*>(external->Value()));
|
|
||||||
CHECK(holder);
|
|
||||||
v8::Local<v8::Value> value;
|
v8::Local<v8::Value> value;
|
||||||
if (!args->GetNext(&value)) {
|
if (!args->GetNext(&value)) {
|
||||||
content::BrowserThread::PostTask(
|
content::BrowserThread::PostTask(
|
||||||
content::BrowserThread::IO, FROM_HERE,
|
content::BrowserThread::IO, FROM_HERE,
|
||||||
base::Bind(holder->callback, false, nullptr));
|
base::Bind(callback, false, nullptr));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,42 +43,7 @@ void HandlerCallback(v8::Isolate* isolate,
|
||||||
scoped_ptr<base::Value> options(converter.FromV8Value(value, context));
|
scoped_ptr<base::Value> options(converter.FromV8Value(value, context));
|
||||||
content::BrowserThread::PostTask(
|
content::BrowserThread::PostTask(
|
||||||
content::BrowserThread::IO, FROM_HERE,
|
content::BrowserThread::IO, FROM_HERE,
|
||||||
base::Bind(holder->callback, true, base::Passed(&options)));
|
base::Bind(callback, true, base::Passed(&options)));
|
||||||
}
|
|
||||||
|
|
||||||
// func.bind(func, arg1, arg2).
|
|
||||||
// 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(mate::StringToV8(isolate, "bind"));
|
|
||||||
CHECK(!bind.IsEmpty());
|
|
||||||
v8::Local<v8::Function> bind_func =
|
|
||||||
v8::Local<v8::Function>::Cast(bind.ToLocalChecked());
|
|
||||||
v8::Local<v8::Value> converted[] = { func, arg1, arg2 };
|
|
||||||
return bind_func->Call(
|
|
||||||
context, func, arraysize(converted), converted).ToLocalChecked();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate the callback that will be passed to |handler|.
|
|
||||||
v8::MaybeLocal<v8::Value> GenerateCallback(v8::Isolate* isolate,
|
|
||||||
v8::Local<v8::Context> context,
|
|
||||||
const ResponseCallback& callback) {
|
|
||||||
// The FunctionTemplate is cached.
|
|
||||||
if (g_handler_callback_.IsEmpty())
|
|
||||||
g_handler_callback_.Reset(
|
|
||||||
isolate,
|
|
||||||
mate::CreateFunctionTemplate(isolate, base::Bind(&HandlerCallback)));
|
|
||||||
|
|
||||||
v8::Local<v8::FunctionTemplate> handler_callback =
|
|
||||||
v8::Local<v8::FunctionTemplate>::New(isolate, g_handler_callback_);
|
|
||||||
CallbackHolder* holder = new CallbackHolder;
|
|
||||||
holder->callback = callback;
|
|
||||||
return BindFunctionWith(isolate, context, handler_callback->GetFunction(),
|
|
||||||
v8::External::New(isolate, holder),
|
|
||||||
v8::Object::New(isolate));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -102,16 +57,10 @@ void AskForOptions(v8::Isolate* isolate,
|
||||||
v8::HandleScope handle_scope(isolate);
|
v8::HandleScope handle_scope(isolate);
|
||||||
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
||||||
v8::Context::Scope context_scope(context);
|
v8::Context::Scope context_scope(context);
|
||||||
// We don't convert the callback to C++ directly because creating
|
v8::Local<v8::Object> state = v8::Object::New(isolate);
|
||||||
// FunctionTemplate will cause memory leak since V8 never releases it. So we
|
handler.Run(request,
|
||||||
// have to create the function object in JavaScript to work around it.
|
mate::ConvertToV8(isolate,
|
||||||
v8::MaybeLocal<v8::Value> wrapped_callback = GenerateCallback(
|
base::Bind(&HandlerCallback, callback, state)));
|
||||||
isolate, context, callback);
|
|
||||||
if (wrapped_callback.IsEmpty()) {
|
|
||||||
callback.Run(false, nullptr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
handler.Run(request, wrapped_callback.ToLocalChecked());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsErrorOptions(base::Value* value, int* error) {
|
bool IsErrorOptions(base::Value* value, int* error) {
|
||||||
|
|
Loading…
Reference in a new issue