V8AbstractEventListener.cpp [plain text]
#include "config.h"
#include "V8AbstractEventListener.h"
#include "DateExtension.h"
#include "Document.h"
#include "Event.h"
#include "EventNames.h"
#include "Frame.h"
#include "InspectorCounters.h"
#include "V8Binding.h"
#include "V8Event.h"
#include "V8EventListenerList.h"
#include "V8HiddenPropertyName.h"
#include "V8Proxy.h"
#include "V8Utilities.h"
#include "WorkerContext.h"
#include "WorkerContextExecutionProxy.h"
namespace WebCore {
static void weakEventListenerCallback(v8::Persistent<v8::Value>, void* parameter)
{
V8AbstractEventListener* listener = static_cast<V8AbstractEventListener*>(parameter);
listener->disposeListenerObject();
}
V8AbstractEventListener::V8AbstractEventListener(bool isAttribute, const WorldContextHandle& worldContext)
: EventListener(JSEventListenerType)
, m_isAttribute(isAttribute)
, m_worldContext(worldContext)
{
#if ENABLE(INSPECTOR)
ThreadLocalInspectorCounters::current().incrementCounter(ThreadLocalInspectorCounters::JSEventListenerCounter);
#endif
}
V8AbstractEventListener::~V8AbstractEventListener()
{
if (!m_listener.IsEmpty()) {
v8::HandleScope scope;
v8::Local<v8::Object> listener = v8::Local<v8::Object>::New(m_listener);
V8EventListenerList::clearWrapper(listener, m_isAttribute);
}
disposeListenerObject();
#if ENABLE(INSPECTOR)
ThreadLocalInspectorCounters::current().decrementCounter(ThreadLocalInspectorCounters::JSEventListenerCounter);
#endif
}
void V8AbstractEventListener::handleEvent(ScriptExecutionContext* context, Event* event)
{
if (context->isJSExecutionForbidden())
return;
ASSERT(event);
RefPtr<V8AbstractEventListener> protect(this);
v8::HandleScope handleScope;
v8::Local<v8::Context> v8Context = toV8Context(context, worldContext());
if (v8Context.IsEmpty())
return;
v8::Context::Scope scope(v8Context);
v8::Handle<v8::Value> jsEvent = toV8(event);
ASSERT(!jsEvent.IsEmpty());
invokeEventHandler(context, event, jsEvent);
}
void V8AbstractEventListener::disposeListenerObject()
{
if (!m_listener.IsEmpty()) {
#ifndef NDEBUG
V8GCController::unregisterGlobalHandle(this, m_listener);
#endif
m_listener.Dispose();
m_listener.Clear();
}
}
void V8AbstractEventListener::setListenerObject(v8::Handle<v8::Object> listener)
{
disposeListenerObject();
m_listener = v8::Persistent<v8::Object>::New(listener);
#ifndef NDEBUG
V8GCController::registerGlobalHandle(EVENT_LISTENER, this, m_listener);
#endif
m_listener.MakeWeak(this, &weakEventListenerCallback);
}
void V8AbstractEventListener::invokeEventHandler(ScriptExecutionContext* context, Event* event, v8::Handle<v8::Value> jsEvent)
{
if (jsEvent.IsEmpty())
return;
v8::Local<v8::Context> v8Context = toV8Context(context, worldContext());
if (v8Context.IsEmpty())
return;
v8::Handle<v8::String> eventSymbol = V8HiddenPropertyName::event();
v8::Local<v8::Value> returnValue;
if (event->type() == eventNames().beforeunloadEvent || event->type() == eventNames().unloadEvent)
DateExtension::get()->setAllowSleep(false);
{
v8::TryCatch tryCatch;
tryCatch.SetVerbose(true);
v8::Local<v8::Value> savedEvent = v8Context->Global()->GetHiddenValue(eventSymbol);
tryCatch.Reset();
v8Context->Global()->SetHiddenValue(eventSymbol, jsEvent);
tryCatch.Reset();
returnValue = callListenerFunction(context, jsEvent, event);
if (tryCatch.HasCaught())
event->target()->uncaughtExceptionInEventHandler();
if (!tryCatch.CanContinue()) { #if ENABLE(WORKERS)
if (context->isWorkerContext())
static_cast<WorkerContext*>(context)->script()->forbidExecution();
#endif
return;
}
tryCatch.Reset();
if (savedEvent.IsEmpty())
v8Context->Global()->SetHiddenValue(eventSymbol, v8::Undefined());
else
v8Context->Global()->SetHiddenValue(eventSymbol, savedEvent);
tryCatch.Reset();
}
if (event->type() == eventNames().beforeunloadEvent || event->type() == eventNames().unloadEvent)
DateExtension::get()->setAllowSleep(true);
ASSERT(!V8Proxy::handleOutOfMemory() || returnValue.IsEmpty());
if (returnValue.IsEmpty())
return;
if (!returnValue->IsNull() && !returnValue->IsUndefined() && event->storesResultAsString())
event->storeResult(toWebCoreString(returnValue));
if (m_isAttribute && shouldPreventDefault(returnValue))
event->preventDefault();
}
bool V8AbstractEventListener::shouldPreventDefault(v8::Local<v8::Value> returnValue)
{
return returnValue->IsBoolean() && !returnValue->BooleanValue();
}
v8::Local<v8::Object> V8AbstractEventListener::getReceiverObject(Event* event)
{
if (!m_listener.IsEmpty() && !m_listener->IsFunction())
return v8::Local<v8::Object>::New(m_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));
}
}