V8WorkerContextEventListener.cpp [plain text]
#include "config.h"
#if ENABLE(WORKERS)
#include "V8WorkerContextEventListener.h"
#include "Event.h"
#include "WorkerContextExecutionProxy.h"
namespace WebCore {
V8WorkerContextEventListener::V8WorkerContextEventListener(WorkerContextExecutionProxy* proxy, v8::Local<v8::Object> listener, bool isInline)
: V8EventListener(0, listener, isInline)
, m_proxy(proxy)
{
}
V8WorkerContextEventListener::~V8WorkerContextEventListener()
{
if (m_proxy)
m_proxy->RemoveEventListener(this);
disposeListenerObject();
}
void V8WorkerContextEventListener::handleEvent(Event* event, bool isWindowEvent)
{
if (disconnected())
return;
RefPtr<V8AbstractEventListener> protect(this);
v8::Locker locker;
v8::HandleScope handleScope;
v8::Handle<v8::Context> context = m_proxy->GetContext();
if (context.IsEmpty())
return;
v8::Context::Scope scope(context);
v8::Handle<v8::Value> jsEvent = WorkerContextExecutionProxy::EventToV8Object(event);
invokeEventHandler(context, event, jsEvent, isWindowEvent);
}
v8::Local<v8::Value> V8WorkerContextEventListener::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 };
v8::Local<v8::Value> result = handlerFunction->Call(receiver, 1, parameters);
m_proxy->trackEvent(event);
return result;
}
v8::Local<v8::Object> V8WorkerContextEventListener::getReceiverObject(Event* event, bool isWindowEvent)
{
if (!m_listener.IsEmpty() && !m_listener->IsFunction())
return v8::Local<v8::Object>::New(m_listener);
if (isWindowEvent)
return v8::Context::GetCurrent()->Global();
EventTarget* target = event->currentTarget();
v8::Handle<v8::Value> value = WorkerContextExecutionProxy::EventTargetToV8Object(target);
if (value.IsEmpty())
return v8::Local<v8::Object>();
return v8::Local<v8::Object>::New(v8::Handle<v8::Object>::Cast(value));
}
}
#endif // WORKERS