V8WorkerContextEventListener.cpp [plain text]
#include "config.h"
#if ENABLE(WORKERS)
#include "V8WorkerContextEventListener.h"
#include "V8Binding.h"
#include "V8DOMWrapper.h"
#include "V8Event.h"
#include "WorkerContext.h"
#include "WorkerContextExecutionProxy.h"
namespace WebCore {
static WorkerContextExecutionProxy* workerProxy(ScriptExecutionContext* context)
{
ASSERT(context->isWorkerContext());
WorkerContext* workerContext = static_cast<WorkerContext*>(context);
return workerContext->script()->proxy();
}
V8WorkerContextEventListener::V8WorkerContextEventListener(v8::Local<v8::Object> listener, bool isInline, const WorldContextHandle& worldContext)
: V8EventListener(listener, isInline, worldContext)
{
}
void V8WorkerContextEventListener::handleEvent(ScriptExecutionContext* context, Event* event)
{
if (!context)
return;
RefPtr<V8AbstractEventListener> protect(this);
v8::HandleScope handleScope;
WorkerContextExecutionProxy* proxy = workerProxy(context);
if (!proxy)
return;
v8::Handle<v8::Context> v8Context = proxy->context();
if (v8Context.IsEmpty())
return;
v8::Context::Scope scope(v8Context);
v8::Handle<v8::Value> jsEvent = toV8(event);
invokeEventHandler(context, event, jsEvent);
}
v8::Local<v8::Value> V8WorkerContextEventListener::callListenerFunction(ScriptExecutionContext* context, v8::Handle<v8::Value> jsEvent, Event* event)
{
v8::Local<v8::Function> handlerFunction = getListenerFunction(context);
v8::Local<v8::Object> receiver = getReceiverObject(context, event);
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);
if (WorkerContextExecutionProxy* proxy = workerProxy(context))
proxy->trackEvent(event);
return result;
}
v8::Local<v8::Object> V8WorkerContextEventListener::getReceiverObject(ScriptExecutionContext* context, Event* event)
{
v8::Local<v8::Object> listener = getListenerObject(context);
if (!listener.IsEmpty() && !listener->IsFunction())
return listener;
EventTarget* target = event->currentTarget();
v8::Handle<v8::Value> value = V8DOMWrapper::convertEventTargetToV8Object(target);
if (value.IsEmpty())
return v8::Local<v8::Object>();
return v8::Local<v8::Object>::New(v8::Handle<v8::Object>::Cast(value));
}
}
#endif // WORKERS