This source file includes following definitions.
- m_isolate
- handleEvent
- setListenerObject
- invokeEventHandler
- shouldPreventDefault
- getReceiverObject
- belongsToTheCurrentWorld
- setWeakCallback
#include "config.h"
#include "bindings/v8/V8AbstractEventListener.h"
#include "V8Event.h"
#include "V8EventTarget.h"
#include "bindings/v8/V8Binding.h"
#include "bindings/v8/V8EventListenerList.h"
#include "bindings/v8/V8HiddenValue.h"
#include "core/events/BeforeUnloadEvent.h"
#include "core/events/Event.h"
#include "core/inspector/InspectorCounters.h"
#include "core/workers/WorkerGlobalScope.h"
namespace WebCore {
V8AbstractEventListener::V8AbstractEventListener(bool isAttribute, DOMWrapperWorld& world, v8::Isolate* isolate)
: EventListener(JSEventListenerType)
, m_isAttribute(isAttribute)
, m_world(world)
, m_isolate(isolate)
{
if (isMainThread())
InspectorCounters::incrementCounter(InspectorCounters::JSEventListenerCounter);
}
V8AbstractEventListener::~V8AbstractEventListener()
{
if (!m_listener.isEmpty()) {
v8::HandleScope scope(m_isolate);
V8EventListenerList::clearWrapper(m_listener.newLocal(m_isolate), m_isAttribute, m_isolate);
}
if (isMainThread())
InspectorCounters::decrementCounter(InspectorCounters::JSEventListenerCounter);
}
void V8AbstractEventListener::handleEvent(ExecutionContext* context, Event* event)
{
if (context->isJSExecutionForbidden())
return;
ASSERT(event);
RefPtr<V8AbstractEventListener> protect(this);
v8::HandleScope handleScope(m_isolate);
v8::Local<v8::Context> v8Context = toV8Context(context, world());
if (v8Context.IsEmpty())
return;
v8::Context::Scope scope(v8Context);
v8::Isolate* isolate = v8Context->GetIsolate();
v8::Handle<v8::Value> jsEvent = toV8(event, v8::Handle<v8::Object>(), isolate);
if (jsEvent.IsEmpty())
return;
invokeEventHandler(context, event, v8::Local<v8::Value>::New(isolate, jsEvent));
}
void V8AbstractEventListener::setListenerObject(v8::Handle<v8::Object> listener)
{
m_listener.set(m_isolate, listener);
m_listener.setWeak(this, &setWeakCallback);
}
void V8AbstractEventListener::invokeEventHandler(ExecutionContext* context, Event* event, v8::Local<v8::Value> jsEvent)
{
if (jsEvent.IsEmpty())
return;
v8::Local<v8::Context> v8Context = toV8Context(context, world());
if (v8Context.IsEmpty())
return;
v8::Isolate* isolate = v8Context->GetIsolate();
v8::Local<v8::Value> returnValue;
{
v8::TryCatch tryCatch;
tryCatch.SetVerbose(true);
v8::Local<v8::Value> savedEvent = V8HiddenValue::getHiddenValue(v8Context->GetIsolate(), v8Context->Global(), V8HiddenValue::event(isolate));
tryCatch.Reset();
V8HiddenValue::setHiddenValue(v8Context->GetIsolate(), v8Context->Global(), V8HiddenValue::event(isolate), jsEvent);
tryCatch.Reset();
returnValue = callListenerFunction(context, jsEvent, event);
if (tryCatch.HasCaught())
event->target()->uncaughtExceptionInEventHandler();
if (!tryCatch.CanContinue()) {
if (context->isWorkerGlobalScope())
toWorkerGlobalScope(context)->script()->forbidExecution();
return;
}
tryCatch.Reset();
if (savedEvent.IsEmpty())
V8HiddenValue::setHiddenValue(v8Context->GetIsolate(), v8Context->Global(), V8HiddenValue::event(isolate), v8::Undefined(v8Context->GetIsolate()));
else
V8HiddenValue::setHiddenValue(v8Context->GetIsolate(), v8Context->Global(), V8HiddenValue::event(isolate), savedEvent);
tryCatch.Reset();
}
if (returnValue.IsEmpty())
return;
if (m_isAttribute && !returnValue->IsNull() && !returnValue->IsUndefined() && event->isBeforeUnloadEvent()) {
V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, stringReturnValue, returnValue);
toBeforeUnloadEvent(event)->setReturnValue(stringReturnValue);
}
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(ExecutionContext* context, Event* event)
{
v8::Isolate* isolate = toV8Context(context, world())->GetIsolate();
v8::Local<v8::Object> listener = m_listener.newLocal(isolate);
if (!m_listener.isEmpty() && !listener->IsFunction())
return listener;
EventTarget* target = event->currentTarget();
v8::Handle<v8::Value> value = toV8(target, v8::Handle<v8::Object>(), isolate);
if (value.IsEmpty())
return v8::Local<v8::Object>();
return v8::Local<v8::Object>::New(isolate, v8::Handle<v8::Object>::Cast(value));
}
bool V8AbstractEventListener::belongsToTheCurrentWorld() const
{
return m_isolate->InContext() && m_world == &DOMWrapperWorld::current(m_isolate);
}
void V8AbstractEventListener::setWeakCallback(const v8::WeakCallbackData<v8::Object, V8AbstractEventListener> &data)
{
data.GetParameter()->m_listener.clear();
}
}