V8CustomEventListener.cpp [plain text]
#include "config.h"
#include "V8CustomEventListener.h"
#include "V8Proxy.h"
namespace WebCore {
V8EventListener::V8EventListener(Frame* frame, v8::Local<v8::Object> listener, bool isAttribute)
: V8AbstractEventListener(frame, isAttribute)
{
m_listener = v8::Persistent<v8::Object>::New(listener);
#ifndef NDEBUG
V8GCController::registerGlobalHandle(EVENT_LISTENER, this, m_listener);
#endif
}
V8EventListener::~V8EventListener()
{
if (m_frame) {
V8Proxy* proxy = V8Proxy::retrieve(m_frame);
if (proxy)
proxy->removeV8EventListener(this);
}
disposeListenerObject();
}
v8::Local<v8::Function> V8EventListener::getListenerFunction()
{
if (m_listener.IsEmpty())
return v8::Local<v8::Function>();
if (m_listener->IsFunction())
return v8::Local<v8::Function>::New(v8::Persistent<v8::Function>::Cast(m_listener));
if (m_listener->IsObject()) {
v8::Local<v8::Value> property = m_listener->Get(v8::String::NewSymbol("handleEvent"));
if (property->IsFunction())
return v8::Local<v8::Function>::Cast(property);
}
return v8::Local<v8::Function>();
}
v8::Local<v8::Value> V8EventListener::callListenerFunction(v8::Handle<v8::Value> jsEvent, Event* event, bool isWindowEvent)
{
v8::Local<v8::Function> handlerFunction = getListenerFunction();
v8::Local<v8::Object> receiver = getReceiverObject(event, isWindowEvent);
if (handlerFunction.IsEmpty() || receiver.IsEmpty())
return v8::Local<v8::Value>();
v8::Handle<v8::Value> parameters[1] = { jsEvent };
V8Proxy* proxy = V8Proxy::retrieve(m_frame);
return proxy->callFunction(handlerFunction, receiver, 1, parameters);
}
}