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> v8Context, 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 = v8Context->Global()->GetHiddenValue(eventSymbol);
tryCatch.Reset();
v8Context->Global()->SetHiddenValue(eventSymbol, jsEvent);
tryCatch.Reset();
returnValue = callListenerFunction(jsEvent, event, isWindowEvent);
tryCatch.Reset();
if (savedEvent.IsEmpty())
v8Context->Global()->SetHiddenValue(eventSymbol, v8::Undefined());
else
v8Context->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> v8Context = V8Proxy::context(m_frame);
if (v8Context.IsEmpty())
return;
RefPtr<Frame> protectFrame(m_frame);
v8::Context::Scope scope(v8Context);
v8::Handle<v8::Value> jsEvent = V8DOMWrapper::convertEventToV8Object(event);
invokeEventHandler(v8Context, event, jsEvent, isWindowEvent);
Document::updateStyleForAllDocuments();
}
void V8AbstractEventListener::disposeListenerObject()
{
if (!m_listener.IsEmpty()) {
#ifndef NDEBUG
V8GCController::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 = V8DOMWrapper::convertEventTargetToV8Object(target);
if (value.IsEmpty())
return v8::Local<v8::Object>();
return v8::Local<v8::Object>::New(v8::Handle<v8::Object>::Cast(value));
}
}