This source file includes following definitions.
- toNode
- toDOMWindow
- toMessagePort
- executingWindow
- addEventListener
- removeEventListener
- setAttributeEventListener
- getAttributeEventListener
- clearAttributeEventListener
- dispatchEvent
- dispatchEvent
- uncaughtExceptionInEventHandler
- legacyType
- countLegacyEvents
- fireEventListeners
- fireEventListeners
- getEventListeners
- eventTypes
- removeAllEventListeners
#include "config.h"
#include "core/events/EventTarget.h"
#include "RuntimeEnabledFeatures.h"
#include "bindings/v8/ExceptionState.h"
#include "core/events/Event.h"
#include "core/dom/ExceptionCode.h"
#include "core/inspector/InspectorInstrumentation.h"
#include "core/frame/DOMWindow.h"
#include "wtf/StdLibExtras.h"
#include "wtf/Vector.h"
using namespace WTF;
namespace WebCore {
EventTargetData::EventTargetData()
{
}
EventTargetData::~EventTargetData()
{
}
EventTarget::~EventTarget()
{
}
Node* EventTarget::toNode()
{
return 0;
}
DOMWindow* EventTarget::toDOMWindow()
{
return 0;
}
MessagePort* EventTarget::toMessagePort()
{
return 0;
}
inline DOMWindow* EventTarget::executingWindow()
{
if (ExecutionContext* context = executionContext())
return context->executingWindow();
return 0;
}
bool EventTarget::addEventListener(const AtomicString& eventType, PassRefPtr<EventListener> listener, bool useCapture)
{
if (!listener)
return false;
EventListener* eventListener = listener.get();
if (ensureEventTargetData().eventListenerMap.add(eventType, listener, useCapture)) {
InspectorInstrumentation::didAddEventListener(this, eventType, eventListener, useCapture);
return true;
}
return false;
}
bool EventTarget::removeEventListener(const AtomicString& eventType, EventListener* listener, bool useCapture)
{
EventTargetData* d = eventTargetData();
if (!d)
return false;
size_t indexOfRemovedListener;
RefPtr<EventListener> protect(listener);
if (!d->eventListenerMap.remove(eventType, listener, useCapture, indexOfRemovedListener))
return false;
InspectorInstrumentation::didRemoveEventListener(this, eventType, listener, useCapture);
if (!d->firingEventIterators)
return true;
for (size_t i = 0; i < d->firingEventIterators->size(); ++i) {
FiringEventIterator& firingIterator = d->firingEventIterators->at(i);
if (eventType != firingIterator.eventType)
continue;
if (indexOfRemovedListener >= firingIterator.end)
continue;
--firingIterator.end;
if (indexOfRemovedListener <= firingIterator.iterator)
--firingIterator.iterator;
}
return true;
}
bool EventTarget::setAttributeEventListener(const AtomicString& eventType, PassRefPtr<EventListener> listener)
{
clearAttributeEventListener(eventType);
if (!listener)
return false;
return addEventListener(eventType, listener, false);
}
EventListener* EventTarget::getAttributeEventListener(const AtomicString& eventType)
{
const EventListenerVector& entry = getEventListeners(eventType);
for (size_t i = 0; i < entry.size(); ++i) {
EventListener* listener = entry[i].listener.get();
if (listener->isAttribute() && listener->belongsToTheCurrentWorld())
return listener;
}
return 0;
}
bool EventTarget::clearAttributeEventListener(const AtomicString& eventType)
{
EventListener* listener = getAttributeEventListener(eventType);
if (!listener)
return false;
return removeEventListener(eventType, listener, false);
}
bool EventTarget::dispatchEvent(PassRefPtrWillBeRawPtr<Event> event, ExceptionState& exceptionState)
{
if (!event) {
exceptionState.throwDOMException(InvalidStateError, "The event provided is null.");
return false;
}
if (event->type().isEmpty()) {
exceptionState.throwDOMException(InvalidStateError, "The event provided is uninitialized.");
return false;
}
if (event->isBeingDispatched()) {
exceptionState.throwDOMException(InvalidStateError, "The event is already being dispatched.");
return false;
}
if (!executionContext())
return false;
return dispatchEvent(event);
}
bool EventTarget::dispatchEvent(PassRefPtrWillBeRawPtr<Event> event)
{
event->setTarget(this);
event->setCurrentTarget(this);
event->setEventPhase(Event::AT_TARGET);
bool defaultPrevented = fireEventListeners(event.get());
event->setEventPhase(0);
return defaultPrevented;
}
void EventTarget::uncaughtExceptionInEventHandler()
{
}
static const AtomicString& legacyType(const Event* event)
{
if (event->type() == EventTypeNames::transitionend)
return EventTypeNames::webkitTransitionEnd;
if (event->type() == EventTypeNames::animationstart)
return EventTypeNames::webkitAnimationStart;
if (event->type() == EventTypeNames::animationend)
return EventTypeNames::webkitAnimationEnd;
if (event->type() == EventTypeNames::animationiteration)
return EventTypeNames::webkitAnimationIteration;
if (event->type() == EventTypeNames::wheel)
return EventTypeNames::mousewheel;
return emptyAtom;
}
void EventTarget::countLegacyEvents(const AtomicString& legacyTypeName, EventListenerVector* listenersVector, EventListenerVector* legacyListenersVector)
{
UseCounter::Feature unprefixedFeature;
UseCounter::Feature prefixedFeature;
UseCounter::Feature prefixedAndUnprefixedFeature;
bool shouldCount = false;
if (legacyTypeName == EventTypeNames::webkitTransitionEnd) {
prefixedFeature = UseCounter::PrefixedTransitionEndEvent;
unprefixedFeature = UseCounter::UnprefixedTransitionEndEvent;
prefixedAndUnprefixedFeature = UseCounter::PrefixedAndUnprefixedTransitionEndEvent;
shouldCount = true;
} else if (legacyTypeName == EventTypeNames::webkitAnimationEnd) {
prefixedFeature = UseCounter::PrefixedAnimationEndEvent;
unprefixedFeature = UseCounter::UnprefixedAnimationEndEvent;
prefixedAndUnprefixedFeature = UseCounter::PrefixedAndUnprefixedAnimationEndEvent;
shouldCount = true;
} else if (legacyTypeName == EventTypeNames::webkitAnimationStart) {
prefixedFeature = UseCounter::PrefixedAnimationStartEvent;
unprefixedFeature = UseCounter::UnprefixedAnimationStartEvent;
prefixedAndUnprefixedFeature = UseCounter::PrefixedAndUnprefixedAnimationStartEvent;
shouldCount = true;
} else if (legacyTypeName == EventTypeNames::webkitAnimationIteration) {
prefixedFeature = UseCounter::PrefixedAnimationIterationEvent;
unprefixedFeature = UseCounter::UnprefixedAnimationIterationEvent;
prefixedAndUnprefixedFeature = UseCounter::PrefixedAndUnprefixedAnimationIterationEvent;
shouldCount = true;
}
if (shouldCount) {
if (DOMWindow* executingWindow = this->executingWindow()) {
if (legacyListenersVector) {
if (listenersVector)
UseCounter::count(executingWindow->document(), prefixedAndUnprefixedFeature);
else
UseCounter::count(executingWindow->document(), prefixedFeature);
} else if (listenersVector) {
UseCounter::count(executingWindow->document(), unprefixedFeature);
}
}
}
}
bool EventTarget::fireEventListeners(Event* event)
{
ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
ASSERT(event && !event->type().isEmpty());
EventTargetData* d = eventTargetData();
if (!d)
return true;
EventListenerVector* legacyListenersVector = 0;
AtomicString legacyTypeName = legacyType(event);
if (!legacyTypeName.isEmpty())
legacyListenersVector = d->eventListenerMap.find(legacyTypeName);
EventListenerVector* listenersVector = d->eventListenerMap.find(event->type());
if (!RuntimeEnabledFeatures::cssAnimationUnprefixedEnabled() && (event->type() == EventTypeNames::animationiteration || event->type() == EventTypeNames::animationend
|| event->type() == EventTypeNames::animationstart))
listenersVector = 0;
if (listenersVector) {
fireEventListeners(event, d, *listenersVector);
} else if (legacyListenersVector) {
AtomicString unprefixedTypeName = event->type();
event->setType(legacyTypeName);
fireEventListeners(event, d, *legacyListenersVector);
event->setType(unprefixedTypeName);
}
countLegacyEvents(legacyTypeName, listenersVector, legacyListenersVector);
return !event->defaultPrevented();
}
void EventTarget::fireEventListeners(Event* event, EventTargetData* d, EventListenerVector& entry)
{
RefPtr<EventTarget> protect = this;
if (event->type() == EventTypeNames::beforeunload) {
if (DOMWindow* executingWindow = this->executingWindow()) {
if (executingWindow->top())
UseCounter::count(executingWindow->document(), UseCounter::SubFrameBeforeUnloadFired);
UseCounter::count(executingWindow->document(), UseCounter::DocumentBeforeUnloadFired);
}
} else if (event->type() == EventTypeNames::unload) {
if (DOMWindow* executingWindow = this->executingWindow())
UseCounter::count(executingWindow->document(), UseCounter::DocumentUnloadFired);
}
size_t i = 0;
size_t size = entry.size();
if (!d->firingEventIterators)
d->firingEventIterators = adoptPtr(new FiringEventIteratorVector);
d->firingEventIterators->append(FiringEventIterator(event->type(), i, size));
for ( ; i < size; ++i) {
RegisteredEventListener& registeredListener = entry[i];
if (event->eventPhase() == Event::CAPTURING_PHASE && !registeredListener.useCapture)
continue;
if (event->eventPhase() == Event::BUBBLING_PHASE && registeredListener.useCapture)
continue;
if (event->immediatePropagationStopped())
break;
ExecutionContext* context = executionContext();
if (!context)
break;
InspectorInstrumentationCookie cookie = InspectorInstrumentation::willHandleEvent(this, event->type(), registeredListener.listener.get(), registeredListener.useCapture);
registeredListener.listener->handleEvent(context, event);
InspectorInstrumentation::didHandleEvent(cookie);
}
d->firingEventIterators->removeLast();
}
const EventListenerVector& EventTarget::getEventListeners(const AtomicString& eventType)
{
DEFINE_STATIC_LOCAL(EventListenerVector, emptyVector, ());
EventTargetData* d = eventTargetData();
if (!d)
return emptyVector;
EventListenerVector* listenerVector = d->eventListenerMap.find(eventType);
if (!listenerVector)
return emptyVector;
return *listenerVector;
}
Vector<AtomicString> EventTarget::eventTypes()
{
EventTargetData* d = eventTargetData();
return d ? d->eventListenerMap.eventTypes() : Vector<AtomicString>();
}
void EventTarget::removeAllEventListeners()
{
EventTargetData* d = eventTargetData();
if (!d)
return;
d->eventListenerMap.clear();
InspectorInstrumentation::didRemoveAllEventListeners(this);
if (d->firingEventIterators) {
for (size_t i = 0; i < d->firingEventIterators->size(); ++i) {
d->firingEventIterators->at(i).iterator = 0;
d->firingEventIterators->at(i).end = 0;
}
}
}
}