## TestCallback.idl
module test {
interface [
Conditional=DATABASE,
Callback
] TestCallback {
boolean callbackWithNoParam();
boolean callbackWithClass1Param(in Class1 class1Param);
boolean callbackWithClass2Param(in Class2 class2Param, in DOMString strArg);
long callbackWithNonBoolReturnType(in Class3 class3Param);
[Custom] long customCallback(in Class5 class5Param, in Class6 class6Param);
boolean callbackWithStringList(in DOMStringList listParam);
};
}
## JSTestCallback.h
#ifndef JSTestCallback_h
#define JSTestCallback_h
#if ENABLE(DATABASE)
#include "ActiveDOMCallback.h"
#include "JSCallbackData.h"
#include "TestCallback.h"
#include <wtf/Forward.h>
namespace WebCore {
class JSTestCallback : public TestCallback, public ActiveDOMCallback {
public:
static PassRefPtr<JSTestCallback> create(JSC::JSObject* callback, JSDOMGlobalObject* globalObject)
{
return adoptRef(new JSTestCallback(callback, globalObject));
}
virtual ~JSTestCallback();
// Functions
virtual bool callbackWithNoParam();
virtual bool callbackWithClass1Param(Class1* class1Param);
virtual bool callbackWithClass2Param(Class2* class2Param, const String& strArg);
COMPILE_ASSERT(false) virtual int callbackWithNonBoolReturnType(Class3* class3Param);
virtual int customCallback(Class5* class5Param, Class6* class6Param);
virtual bool callbackWithStringList(DOMStringList* listParam);
private:
JSTestCallback(JSC::JSObject* callback, JSDOMGlobalObject*);
JSCallbackData* m_data;
};
} // namespace WebCore
#endif // ENABLE(DATABASE)
#endif
## JSTestcallback.cpp
#include "config.h"
#if ENABLE(DATABASE)
#include "JSTestCallback.h"
#include "JSClass1.h"
#include "JSClass2.h"
#include "JSDOMStringList.h"
#include "ScriptExecutionContext.h"
#include <runtime/JSLock.h>
#include <wtf/MainThread.h>
using namespace JSC;
namespace WebCore {
JSTestCallback::JSTestCallback(JSObject* callback, JSDOMGlobalObject* globalObject)
: ActiveDOMCallback(globalObject->scriptExecutionContext())
, m_data(new JSCallbackData(callback, globalObject))
{
}
JSTestCallback::~JSTestCallback()
{
ScriptExecutionContext* context = scriptExecutionContext();
// When the context is destroyed, all tasks with a reference to a callback
// should be deleted. So if the context is 0, we are on the context thread.
if (!context || context->isContextThread())
delete m_data;
else
context->postTask(DeleteCallbackDataTask::create(m_data));
#ifndef NDEBUG
m_data = 0;
#endif
}
// Functions
bool JSTestCallback::callbackWithNoParam()
{
if (!canInvokeCallback())
return true;
RefPtr<JSTestCallback> protect(this);
JSLock lock(SilenceAssertionsOnly);
ExecState* exec = m_data->globalObject()->globalExec();
MarkedArgumentBuffer args;
bool raisedException = false;
m_data->invokeCallback(args, &raisedException);
return !raisedException;
}
bool JSTestCallback::callbackWithClass1Param(Class1* class1Param)
{
if (!canInvokeCallback())
return true;
RefPtr<JSTestCallback> protect(this);
JSLock lock(SilenceAssertionsOnly);
ExecState* exec = m_data->globalObject()->globalExec();
MarkedArgumentBuffer args;
args.append(toJS(exec, class1Param));
bool raisedException = false;
m_data->invokeCallback(args, &raisedException);
return !raisedException;
}
bool JSTestCallback::callbackWithClass2Param(Class2* class2Param, const String& strArg)
{
if (!canInvokeCallback())
return true;
RefPtr<JSTestCallback> protect(this);
JSLock lock(SilenceAssertionsOnly);
ExecState* exec = m_data->globalObject()->globalExec();
MarkedArgumentBuffer args;
args.append(toJS(exec, class2Param));
args.append(jsString(exec, strArg));
bool raisedException = false;
m_data->invokeCallback(args, &raisedException);
return !raisedException;
}
bool JSTestCallback::callbackWithStringList(DOMStringList* listParam)
{
if (!canInvokeCallback())
return true;
RefPtr<JSTestCallback> protect(this);
JSLock lock(SilenceAssertionsOnly);
ExecState* exec = m_data->globalObject()->globalExec();
MarkedArgumentBuffer args;
args.append(toJS(exec, listParam));
bool raisedException = false;
m_data->invokeCallback(args, &raisedException);
return !raisedException;
}
}
#endif // ENABLE(DATABASE)
=========================
## V8TestCallback.h
#if ENABLE(DATABASE)
#ifndef V8TestCallback_h
#define V8TestCallback_h
#include "ActiveDOMCallback.h"
#include "TestCallback.h"
#include "WorldContextHandle.h"
#include <v8.h>
#include <wtf/Forward.h>
namespace WebCore {
class ScriptExecutionContext;
class V8TestCallback : public TestCallback, public ActiveDOMCallback {
public:
static PassRefPtr<V8TestCallback> create(v8::Local<v8::Value> value, ScriptExecutionContext* context)
{
ASSERT(value->IsObject());
ASSERT(context);
return adoptRef(new V8TestCallback(value->ToObject(), context));
}
virtual ~V8TestCallback();
// Functions
virtual bool callbackWithNoParam();
virtual bool callbackWithClass1Param(Class1* class1Param);
virtual bool callbackWithClass2Param(Class2* class2Param, const String& strArg);
COMPILE_ASSERT(false) virtual int callbackWithNonBoolReturnType(Class3* class3Param);
virtual int customCallback(Class5* class5Param, Class6* class6Param);
virtual bool callbackWithStringList(PassRefPtr<DOMStringList> listParam);
private:
V8TestCallback(v8::Local<v8::Object>, ScriptExecutionContext*);
v8::Persistent<v8::Object> m_callback;
WorldContextHandle m_worldContext;
};
}
#endif // V8TestCallback_h
#endif // ENABLE(DATABASE)
## V8TestCallback.cpp
#include "config.h"
#include "V8TestCallback.h"
#if ENABLE(DATABASE)
#include "ScriptExecutionContext.h"
#include "V8Binding.h"
#include "V8Class1.h"
#include "V8Class2.h"
#include "V8CustomVoidCallback.h"
#include "V8DOMStringList.h"
#include "V8Proxy.h"
#include <wtf/GetPtr.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
#include <wtf/Assertions.h>
namespace WebCore {
V8TestCallback::V8TestCallback(v8::Local<v8::Object> callback, ScriptExecutionContext* context)
: ActiveDOMCallback(context)
, m_callback(v8::Persistent<v8::Object>::New(callback))
, m_worldContext(UseCurrentWorld)
{
}
V8TestCallback::~V8TestCallback()
{
m_callback.Dispose();
}
// Functions
bool V8TestCallback::callbackWithNoParam()
{
if (!canInvokeCallback())
return true;
v8::HandleScope handleScope;
v8::Handle<v8::Context> v8Context = toV8Context(scriptExecutionContext(), m_worldContext);
if (v8Context.IsEmpty())
return true;
v8::Context::Scope scope(v8Context);
v8::Handle<v8::Value> *argv = 0;
bool callbackReturnValue = false;
return !invokeCallback(m_callback, 0, argv, callbackReturnValue, scriptExecutionContext());
}
bool V8TestCallback::callbackWithClass1Param(Class1* class1Param)
{
if (!canInvokeCallback())
return true;
v8::HandleScope handleScope;
v8::Handle<v8::Context> v8Context = toV8Context(scriptExecutionContext(), m_worldContext);
if (v8Context.IsEmpty())
return true;
v8::Context::Scope scope(v8Context);
v8::Handle<v8::Value> class1ParamHandle = toV8(class1Param);
if (class1ParamHandle.IsEmpty()) {
CRASH();
return true;
}
v8::Handle<v8::Value> argv[] = {
class1ParamHandle
};
bool callbackReturnValue = false;
return !invokeCallback(m_callback, 1, argv, callbackReturnValue, scriptExecutionContext());
}
bool V8TestCallback::callbackWithClass2Param(Class2* class2Param, const String& strArg)
{
if (!canInvokeCallback())
return true;
v8::HandleScope handleScope;
v8::Handle<v8::Context> v8Context = toV8Context(scriptExecutionContext(), m_worldContext);
if (v8Context.IsEmpty())
return true;
v8::Context::Scope scope(v8Context);
v8::Handle<v8::Value> class2ParamHandle = toV8(class2Param);
if (class2ParamHandle.IsEmpty()) {
CRASH();
return true;
}
v8::Handle<v8::Value> strArgHandle = v8String(strArg);
if (strArgHandle.IsEmpty()) {
CRASH();
return true;
}
v8::Handle<v8::Value> argv[] = {
class2ParamHandle,
strArgHandle
};
bool callbackReturnValue = false;
return !invokeCallback(m_callback, 2, argv, callbackReturnValue, scriptExecutionContext());
}
bool V8TestCallback::callbackWithStringList(PassRefPtr<DOMStringList> listParam)
{
if (!canInvokeCallback())
return true;
v8::HandleScope handleScope;
v8::Handle<v8::Context> v8Context = toV8Context(scriptExecutionContext(), m_worldContext);
if (v8Context.IsEmpty())
return true;
v8::Context::Scope scope(v8Context);
v8::Handle<v8::Value> listParamHandle = toV8(listParam);
if (listParamHandle.IsEmpty()) {
CRASH();
return true;
}
v8::Handle<v8::Value> argv[] = {
listParamHandle
};
bool callbackReturnValue = false;
return !invokeCallback(m_callback, 1, argv, callbackReturnValue, scriptExecutionContext());
}
} // namespace WebCore
#endif // ENABLE(DATABASE)