Converted callback can only be called for once

This commit is contained in:
Cheng Zhao 2015-10-28 20:44:46 +08:00
parent 4eac6b31b1
commit f40a3f72d7
3 changed files with 32 additions and 25 deletions

View file

@ -16,18 +16,7 @@ namespace internal {
namespace { namespace {
// The callback which is passed to |handler|. // The callback which is passed to |handler|.
void HandlerCallback(const ResponseCallback& callback, void HandlerCallback(const ResponseCallback& callback, mate::Arguments* args) {
v8::Local<v8::Object> state,
mate::Arguments* args) {
v8::Isolate* isolate = args->isolate();
// Check if the callback has already been called.
v8::Local<v8::String> called_symbol = mate::StringToSymbol(isolate, "called");
if (state->Has(called_symbol))
return; // no nothing
else
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.
v8::Local<v8::Value> value; v8::Local<v8::Value> value;
if (!args->GetNext(&value)) { if (!args->GetNext(&value)) {
@ -57,10 +46,9 @@ 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);
v8::Local<v8::Object> state = v8::Object::New(isolate);
handler.Run(request, handler.Run(request,
mate::ConvertToV8(isolate, mate::ConvertToV8(isolate,
base::Bind(&HandlerCallback, callback, state))); base::Bind(&HandlerCallback, callback)));
} }
bool IsErrorOptions(base::Value* value, int* error) { bool IsErrorOptions(base::Value* value, int* error) {

View file

@ -4,23 +4,36 @@
#include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/callback.h"
#include "native_mate/wrappable.h"
namespace mate { namespace mate {
namespace internal { namespace internal {
namespace { namespace {
struct TranslaterHolder : public Wrappable { struct TranslaterHolder {
Translater translater; Translater translater;
}; };
// Cached JavaScript version of |CallTranslater|. // Cached JavaScript version of |CallTranslater|.
v8::Persistent<v8::FunctionTemplate> g_call_translater; v8::Persistent<v8::FunctionTemplate> g_call_translater;
void CallTranslater(TranslaterHolder* holder, mate::Arguments* args) { void CallTranslater(v8::Local<v8::External> external,
v8::Local<v8::Object> state,
mate::Arguments* args) {
v8::Isolate* isolate = args->isolate();
// Check if the callback has already been called.
v8::Local<v8::String> called_symbol = mate::StringToSymbol(isolate, "called");
if (state->Has(called_symbol)) {
args->ThrowError("callback can only be called for once");
return;
} else {
state->Set(called_symbol, v8::Boolean::New(isolate, true));
}
TranslaterHolder* holder = static_cast<TranslaterHolder*>(external->Value());
holder->translater.Run(args); holder->translater.Run(args);
delete holder;
} }
// func.bind(func, arg1). // func.bind(func, arg1).
@ -28,12 +41,13 @@ void CallTranslater(TranslaterHolder* holder, mate::Arguments* args) {
v8::Local<v8::Value> BindFunctionWith(v8::Isolate* isolate, v8::Local<v8::Value> BindFunctionWith(v8::Isolate* isolate,
v8::Local<v8::Context> context, v8::Local<v8::Context> context,
v8::Local<v8::Function> func, v8::Local<v8::Function> func,
v8::Local<v8::Value> arg1) { v8::Local<v8::Value> arg1,
v8::Local<v8::Value> arg2) {
v8::MaybeLocal<v8::Value> bind = func->Get(mate::StringToV8(isolate, "bind")); v8::MaybeLocal<v8::Value> bind = func->Get(mate::StringToV8(isolate, "bind"));
CHECK(!bind.IsEmpty()); CHECK(!bind.IsEmpty());
v8::Local<v8::Function> bind_func = v8::Local<v8::Function> bind_func =
v8::Local<v8::Function>::Cast(bind.ToLocalChecked()); v8::Local<v8::Function>::Cast(bind.ToLocalChecked());
v8::Local<v8::Value> converted[] = { func, arg1 }; v8::Local<v8::Value> converted[] = { func, arg1, arg2 };
return bind_func->Call( return bind_func->Call(
context, func, arraysize(converted), converted).ToLocalChecked(); context, func, arraysize(converted), converted).ToLocalChecked();
} }
@ -55,7 +69,8 @@ v8::Local<v8::Value> CreateFunctionFromTranslater(
return BindFunctionWith(isolate, return BindFunctionWith(isolate,
isolate->GetCurrentContext(), isolate->GetCurrentContext(),
call_translater->GetFunction(), call_translater->GetFunction(),
holder->GetWrapper(isolate)); v8::External::New(isolate, holder),
v8::Object::New(isolate));
} }
} // namespace internal } // namespace internal

View file

@ -23,8 +23,10 @@ describe 'protocol module', ->
it 'does not crash when handler is called twice', (done) -> it 'does not crash when handler is called twice', (done) ->
doubleHandler = (request, callback) -> doubleHandler = (request, callback) ->
try
callback(text) callback(text)
callback() callback()
catch
protocol.registerStringProtocol protocolName, doubleHandler, (error) -> protocol.registerStringProtocol protocolName, doubleHandler, (error) ->
return done(error) if error return done(error) if error
$.ajax $.ajax
@ -302,8 +304,10 @@ describe 'protocol module', ->
it 'does not crash when handler is called twice', (done) -> it 'does not crash when handler is called twice', (done) ->
doubleHandler = (request, callback) -> doubleHandler = (request, callback) ->
try
callback(text) callback(text)
callback() callback()
catch
protocol.interceptStringProtocol 'http', doubleHandler, (error) -> protocol.interceptStringProtocol 'http', doubleHandler, (error) ->
return done(error) if error return done(error) if error
$.ajax $.ajax