#ifndef JSEventListener_h
#define JSEventListener_h
#include "DOMWrapperWorld.h"
#include "EventListener.h"
#include <heap/StrongInlines.h>
#include <heap/Weak.h>
#include <heap/WeakInlines.h>
#include <wtf/Ref.h>
namespace WebCore {
class DOMWindow;
class EventTarget;
class HTMLElement;
class JSDOMGlobalObject;
class JSEventListener : public EventListener {
public:
static Ref<JSEventListener> create(JSC::JSObject* listener, JSC::JSObject* wrapper, bool isAttribute, DOMWrapperWorld& world)
{
return adoptRef(*new JSEventListener(listener, wrapper, isAttribute, world));
}
static const JSEventListener* cast(const EventListener* listener)
{
return listener->type() == JSEventListenerType
? static_cast<const JSEventListener*>(listener)
: 0;
}
virtual ~JSEventListener();
virtual bool operator==(const EventListener& other) override;
bool isAttribute() const { return m_isAttribute; }
JSC::JSObject* jsFunction(ScriptExecutionContext*) const;
DOMWrapperWorld& isolatedWorld() const { return *m_isolatedWorld; }
JSC::JSObject* wrapper() const { return m_wrapper.get(); }
void setWrapper(JSC::VM&, JSC::JSObject* wrapper) const { m_wrapper = JSC::Weak<JSC::JSObject>(wrapper); }
private:
virtual JSC::JSObject* initializeJSFunction(ScriptExecutionContext*) const;
virtual void visitJSFunction(JSC::SlotVisitor&) override;
virtual bool virtualisAttribute() const override;
protected:
JSEventListener(JSC::JSObject* function, JSC::JSObject* wrapper, bool isAttribute, DOMWrapperWorld&);
virtual void handleEvent(ScriptExecutionContext*, Event*) override;
private:
mutable JSC::Weak<JSC::JSObject> m_jsFunction;
mutable JSC::Weak<JSC::JSObject> m_wrapper;
bool m_isAttribute;
RefPtr<DOMWrapperWorld> m_isolatedWorld;
};
JSC::JSValue eventHandlerAttribute(EventTarget&, const AtomicString& eventType);
void setEventHandlerAttribute(JSC::ExecState&, JSC::JSObject&, EventTarget&, const AtomicString& eventType, JSC::JSValue);
JSC::JSValue windowEventHandlerAttribute(HTMLElement&, const AtomicString& eventType);
void setWindowEventHandlerAttribute(JSC::ExecState&, JSC::JSObject&, HTMLElement&, const AtomicString& eventType, JSC::JSValue);
JSC::JSValue windowEventHandlerAttribute(DOMWindow&, const AtomicString& eventType);
void setWindowEventHandlerAttribute(JSC::ExecState&, JSC::JSObject&, DOMWindow&, const AtomicString& eventType, JSC::JSValue);
JSC::JSValue documentEventHandlerAttribute(HTMLElement&, const AtomicString& eventType);
void setDocumentEventHandlerAttribute(JSC::ExecState&, JSC::JSObject&, HTMLElement&, const AtomicString& eventType, JSC::JSValue);
JSC::JSValue documentEventHandlerAttribute(Document&, const AtomicString& eventType);
void setDocumentEventHandlerAttribute(JSC::ExecState&, JSC::JSObject&, Document&, const AtomicString& eventType, JSC::JSValue);
Ref<JSEventListener> createJSEventListenerForAdd(JSC::ExecState&, JSC::JSObject& listener, JSC::JSObject& wrapper);
Ref<JSEventListener> createJSEventListenerForRemove(JSC::ExecState&, JSC::JSObject& listener, JSC::JSObject& wrapper);
inline JSC::JSObject* JSEventListener::jsFunction(ScriptExecutionContext* scriptExecutionContext) const
{
Ref<JSEventListener> protect(const_cast<JSEventListener&>(*this));
JSC::Strong<JSC::JSObject> wrapper(m_isolatedWorld->vm(), m_wrapper.get());
if (!m_jsFunction) {
JSC::JSObject* function = initializeJSFunction(scriptExecutionContext);
JSC::JSObject* wrapper = m_wrapper.get();
if (wrapper)
JSC::Heap::heap(wrapper)->writeBarrier(wrapper, function);
m_jsFunction = JSC::Weak<JSC::JSObject>(function);
}
ASSERT(!m_isolatedWorld->isNormal() || m_wrapper || !m_jsFunction);
if (!m_wrapper)
return 0;
ASSERT(!m_jsFunction || static_cast<JSC::JSCell*>(m_jsFunction.get())->isObject());
return m_jsFunction.get();
}
inline Ref<JSEventListener> createJSEventListenerForRemove(JSC::ExecState& state, JSC::JSObject& listener, JSC::JSObject& wrapper)
{
return createJSEventListenerForAdd(state, listener, wrapper);
}
}
#endif // JSEventListener_h