V8AbstractEventListener.cpp [plain text]
#include "config.h"
#include "V8AbstractEventListener.h"
#include "Document.h"
#include "Event.h"
#include "Frame.h"
#include "Tokenizer.h"
#include "V8Binding.h"
namespace WebCore {
V8AbstractEventListener::V8AbstractEventListener(Frame* frame, bool isAttribute)
: m_isAttribute(isAttribute)
, m_frame(frame)
, m_lineNumber(0)
, m_columnNumber(0)
{
if (!m_frame)
return;
if (m_isAttribute && m_frame->document()->tokenizer()) {
m_lineNumber = m_frame->document()->tokenizer()->lineNumber();
m_columnNumber = m_frame->document()->tokenizer()->columnNumber();
}
}
void V8AbstractEventListener::invokeEventHandler(v8::Handle<v8::Context> context, Event* event, v8::Handle<v8::Value> jsEvent, bool isWindowEvent)
{
v8::Local<v8::String> eventSymbol = v8::String::NewSymbol("event");
v8::Local<v8::Value> returnValue;
{
v8::TryCatch tryCatch;
tryCatch.SetVerbose(true);
v8::Local<v8::Value> savedEvent = context->Global()->GetHiddenValue(eventSymbol);
tryCatch.Reset();
context->Global()->SetHiddenValue(eventSymbol, jsEvent);
tryCatch.Reset();
returnValue = callListenerFunction(jsEvent, event, isWindowEvent);
tryCatch.Reset();
if (savedEvent.IsEmpty())
context->Global()->SetHiddenValue(eventSymbol, v8::Undefined());
else
context->Global()->SetHiddenValue(eventSymbol, savedEvent);
tryCatch.Reset();
}
ASSERT(!V8Proxy::HandleOutOfMemory() || returnValue.IsEmpty());
if (returnValue.IsEmpty())
return;
if (!returnValue->IsNull() && !returnValue->IsUndefined() && event->storesResultAsString())
event->storeResult(toWebCoreString(returnValue));
if (m_isAttribute && returnValue->IsBoolean() && !returnValue->BooleanValue())
event->preventDefault();
}
void V8AbstractEventListener::handleEvent(Event* event, bool isWindowEvent)
{
if (disconnected())
return;
RefPtr<V8AbstractEventListener> protect(this);
v8::HandleScope handleScope;
v8::Handle<v8::Context> context = V8Proxy::GetContext(m_frame);
if (context.IsEmpty())
return;
RefPtr<Frame> protectFrame(m_frame);
v8::Context::Scope scope(context);
v8::Handle<v8::Value> jsEvent = V8Proxy::EventToV8Object(event);
invokeEventHandler(context, event, jsEvent, isWindowEvent);
Document::updateStyleForAllDocuments();
}
void V8AbstractEventListener::disposeListenerObject()
{
if (!m_listener.IsEmpty()) {
#ifndef NDEBUG
V8Proxy::UnregisterGlobalHandle(this, m_listener);
#endif
m_listener.Dispose();
m_listener.Clear();
}
}
v8::Local<v8::Object> V8AbstractEventListener::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 = V8Proxy::EventTargetToV8Object(target);
if (value.IsEmpty())
return v8::Local<v8::Object>();
return v8::Local<v8::Object>::New(v8::Handle<v8::Object>::Cast(value));
}
}