Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add checked casts for Event.
<https://webkit.org/b/128875>

Generate casting helpers for casting from Event to various subclasses
and go on static_cast replacement spree.

Reviewed by Sam Weinig.


Canonical link: https://commits.webkit.org/146915@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@164184 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
Andreas Kling committed Feb 16, 2014
1 parent 6384f02 commit e2941c6
Show file tree
Hide file tree
Showing 42 changed files with 142 additions and 114 deletions.
10 changes: 10 additions & 0 deletions Source/WebCore/ChangeLog
@@ -1,3 +1,13 @@
2014-02-15 Andreas Kling <akling@apple.com>

Add checked casts for Event.
<https://webkit.org/b/128875>

Generate casting helpers for casting from Event to various subclasses
and go on static_cast replacement spree.

Reviewed by Sam Weinig.

2014-02-15 Ryosuke Niwa <rniwa@webkit.org>

HTMLTextFormControlElement::subtreeHasChanged should be called before updating selection
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/bindings/js/JSErrorHandler.cpp
Expand Up @@ -66,7 +66,7 @@ void JSErrorHandler::handleEvent(ScriptExecutionContext* scriptExecutionContext,
if (!scriptExecutionContext)
return;

ErrorEvent* errorEvent = static_cast<ErrorEvent*>(event);
ErrorEvent* errorEvent = toErrorEvent(event);

JSLockHolder lock(scriptExecutionContext->vm());

Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/dom/BeforeTextInsertedEvent.h
Expand Up @@ -51,6 +51,8 @@ class BeforeTextInsertedEvent : public Event {
String m_text;
};

EVENT_TYPE_CASTS(BeforeTextInsertedEvent)

} // namespace

#endif
6 changes: 1 addition & 5 deletions Source/WebCore/dom/BeforeUnloadEvent.h
Expand Up @@ -51,11 +51,7 @@ class BeforeUnloadEvent final : public Event {
String m_returnValue;
};

inline BeforeUnloadEvent* toBeforeUnloadEvent(Event* event)
{
ASSERT_WITH_SECURITY_IMPLICATION(!event || event->isBeforeUnloadEvent());
return static_cast<BeforeUnloadEvent*>(event);
}
EVENT_TYPE_CASTS(BeforeUnloadEvent)

} // namespace WebCore

Expand Down
5 changes: 5 additions & 0 deletions Source/WebCore/dom/ErrorEvent.cpp
Expand Up @@ -74,4 +74,9 @@ EventInterface ErrorEvent::eventInterface() const
return ErrorEventInterfaceType;
}

bool ErrorEvent::isErrorEvent() const
{
return true;
}

} // namespace WebCore
4 changes: 4 additions & 0 deletions Source/WebCore/dom/ErrorEvent.h
Expand Up @@ -73,12 +73,16 @@ class ErrorEvent : public Event {
ErrorEvent(const String& message, const String& fileName, unsigned lineNumber, unsigned columnNumber);
ErrorEvent(const AtomicString&, const ErrorEventInit&);

virtual bool isErrorEvent() const override;

String m_message;
String m_fileName;
unsigned m_lineNumber;
unsigned m_columnNumber;
};

EVENT_TYPE_CASTS(ErrorEvent)

} // namespace WebCore

#endif // ErrorEvent_h
15 changes: 15 additions & 0 deletions Source/WebCore/dom/Event.cpp
Expand Up @@ -168,6 +168,21 @@ bool Event::isBeforeUnloadEvent() const
return false;
}

bool Event::isErrorEvent() const
{
return false;
}

bool Event::isTextEvent() const
{
return false;
}

bool Event::isWheelEvent() const
{
return false;
}

PassRefPtr<Event> Event::cloneFor(HTMLIFrameElement*) const
{
return Event::create(type(), bubbles(), cancelable());
Expand Down
8 changes: 8 additions & 0 deletions Source/WebCore/dom/Event.h
Expand Up @@ -145,6 +145,10 @@ class Event : public ScriptWrappable, public RefCounted<Event> {

virtual bool isBeforeUnloadEvent() const;

virtual bool isErrorEvent() const;
virtual bool isTextEvent() const;
virtual bool isWheelEvent() const;

bool propagationStopped() const { return m_propagationStopped || m_immediatePropagationStopped; }
bool immediatePropagationStopped() const { return m_immediatePropagationStopped; }

Expand Down Expand Up @@ -201,6 +205,10 @@ class Event : public ScriptWrappable, public RefCounted<Event> {
RefPtr<Event> m_underlyingEvent;
};

#define EVENT_TYPE_CASTS(ToValueTypeName) \
TYPE_CASTS_BASE(ToValueTypeName, Event, event, event->is##ToValueTypeName(), event.is##ToValueTypeName())


} // namespace WebCore

#endif // Event_h
12 changes: 1 addition & 11 deletions Source/WebCore/dom/FocusEvent.h
Expand Up @@ -70,17 +70,7 @@ class FocusEvent : public UIEvent {
RefPtr<EventTarget> m_relatedTarget;
};

inline FocusEvent* toFocusEvent(Event* event)
{
ASSERT_WITH_SECURITY_IMPLICATION(event && event->isFocusEvent());
return static_cast<FocusEvent*>(event);
}

inline FocusEvent& toFocusEvent(Event& event)
{
ASSERT(event.isFocusEvent());
return static_cast<FocusEvent&>(event);
}
EVENT_TYPE_CASTS(FocusEvent)

} // namespace WebCore

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/dom/KeyboardEvent.cpp
Expand Up @@ -207,7 +207,7 @@ KeyboardEvent* findKeyboardEvent(Event* event)
{
for (Event* e = event; e; e = e->underlyingEvent())
if (e->isKeyboardEvent())
return static_cast<KeyboardEvent*>(e);
return toKeyboardEvent(e);
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/dom/KeyboardEvent.h
Expand Up @@ -124,6 +124,8 @@ class KeyboardEvent : public UIEventWithKeyState {
#endif
};

EVENT_TYPE_CASTS(KeyboardEvent)

KeyboardEvent* findKeyboardEvent(Event*);

} // namespace WebCore
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/dom/MouseEvent.cpp
Expand Up @@ -277,7 +277,7 @@ SimulatedMouseEvent::SimulatedMouseEvent(const AtomicString& eventType, PassRefP
setUnderlyingEvent(underlyingEvent);

if (this->underlyingEvent() && this->underlyingEvent()->isMouseEvent()) {
MouseEvent* mouseEvent = static_cast<MouseEvent*>(this->underlyingEvent());
MouseEvent* mouseEvent = toMouseEvent(this->underlyingEvent());
m_screenLocation = mouseEvent->screenLocation();
initCoordinates(mouseEvent->clientLocation());
} else if (target) {
Expand Down
12 changes: 1 addition & 11 deletions Source/WebCore/dom/MouseEvent.h
Expand Up @@ -131,17 +131,7 @@ class SimulatedMouseEvent : public MouseEvent {
SimulatedMouseEvent(const AtomicString& eventType, PassRefPtr<AbstractView>, PassRefPtr<Event> underlyingEvent, Element* target);
};

inline MouseEvent* toMouseEvent(Event* event)
{
ASSERT_WITH_SECURITY_IMPLICATION(event && event->isMouseEvent());
return static_cast<MouseEvent*>(event);
}

inline MouseEvent& toMouseEvent(Event& event)
{
ASSERT(event.isMouseEvent());
return static_cast<MouseEvent&>(event);
}
EVENT_TYPE_CASTS(MouseEvent)

} // namespace WebCore

Expand Down
18 changes: 7 additions & 11 deletions Source/WebCore/dom/Node.cpp
Expand Up @@ -2101,9 +2101,9 @@ void Node::defaultEventHandler(Event* event)
if (eventType == eventNames().keydownEvent || eventType == eventNames().keypressEvent) {
if (event->isKeyboardEvent())
if (Frame* frame = document().frame())
frame->eventHandler().defaultKeyboardEventHandler(static_cast<KeyboardEvent*>(event));
frame->eventHandler().defaultKeyboardEventHandler(toKeyboardEvent(event));
} else if (eventType == eventNames().clickEvent) {
int detail = event->isUIEvent() ? static_cast<UIEvent*>(event)->detail() : 0;
int detail = event->isUIEvent() ? toUIEvent(event)->detail() : 0;
if (dispatchDOMActivateEvent(detail, event))
event->setDefaultHandled();
#if ENABLE(CONTEXT_MENUS)
Expand All @@ -2115,11 +2115,10 @@ void Node::defaultEventHandler(Event* event)
} else if (eventType == eventNames().textInputEvent) {
if (event->eventInterface() == TextEventInterfaceType)
if (Frame* frame = document().frame())
frame->eventHandler().defaultTextInputEventHandler(static_cast<TextEvent*>(event));
frame->eventHandler().defaultTextInputEventHandler(toTextEvent(event));
#if ENABLE(PAN_SCROLLING)
} else if (eventType == eventNames().mousedownEvent && event->isMouseEvent()) {
MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
if (mouseEvent->button() == MiddleButton) {
if (toMouseEvent(event)->button() == MiddleButton) {
if (enclosingLinkEventParentOrSelf())
return;

Expand All @@ -2134,8 +2133,7 @@ void Node::defaultEventHandler(Event* event)
}
#endif
} else if ((eventType == eventNames().wheelEvent || eventType == eventNames().mousewheelEvent) && event->eventInterface() == WheelEventInterfaceType) {
WheelEvent* wheelEvent = static_cast<WheelEvent*>(event);


// If we don't have a renderer, send the wheel event to the first node we find with a renderer.
// This is needed for <option> and <optgroup> elements so that <select>s get a wheel scroll.
Node* startNode = this;
Expand All @@ -2144,18 +2142,16 @@ void Node::defaultEventHandler(Event* event)

if (startNode && startNode->renderer())
if (Frame* frame = document().frame())
frame->eventHandler().defaultWheelEventHandler(startNode, wheelEvent);
frame->eventHandler().defaultWheelEventHandler(startNode, toWheelEvent(event));
#if ENABLE(TOUCH_EVENTS) && PLATFORM(IOS)
} else if (event->eventInterface() == TouchEventInterfaceType && eventNames().isTouchEventType(eventType)) {
TouchEvent* touchEvent = static_cast<TouchEvent*>(event);

RenderObject* renderer = this->renderer();
while (renderer && (!renderer->isBox() || !toRenderBox(renderer)->canBeScrolledAndHasScrollableArea()))
renderer = renderer->parent();

if (renderer && renderer->node()) {
if (Frame* frame = document().frame())
frame->eventHandler().defaultTouchEventHandler(renderer->node(), touchEvent);
frame->eventHandler().defaultTouchEventHandler(renderer->node(), toTouchEvent(touchEvent));
}
#endif
} else if (event->type() == eventNames().webkitEditableContentChangedEvent) {
Expand Down
5 changes: 5 additions & 0 deletions Source/WebCore/dom/TextEvent.cpp
Expand Up @@ -118,4 +118,9 @@ EventInterface TextEvent::eventInterface() const
return TextEventInterfaceType;
}

bool TextEvent::isTextEvent() const
{
return true;
}

} // namespace WebCore
4 changes: 4 additions & 0 deletions Source/WebCore/dom/TextEvent.h
Expand Up @@ -73,6 +73,8 @@ namespace WebCore {
bool shouldSmartReplace, bool shouldMatchStyle);
TextEvent(PassRefPtr<AbstractView>, const String& data, const Vector<DictationAlternative>& dictationAlternatives);

virtual bool isTextEvent() const override;

TextEventInputType m_inputType;
String m_data;

Expand All @@ -82,6 +84,8 @@ namespace WebCore {
Vector<DictationAlternative> m_dictationAlternatives;
};

EVENT_TYPE_CASTS(TextEvent)

} // namespace WebCore

#endif // TextEvent_h
12 changes: 1 addition & 11 deletions Source/WebCore/dom/TouchEvent.h
Expand Up @@ -86,17 +86,7 @@ class TouchEvent : public MouseRelatedEvent {
RefPtr<TouchList> m_changedTouches;
};

inline TouchEvent* toTouchEvent(Event* event)
{
ASSERT_WITH_SECURITY_IMPLICATION(event && event->isTouchEvent());
return static_cast<TouchEvent*>(event);
}

inline TouchEvent& toTouchEvent(Event& event)
{
ASSERT(event.isTouchEvent());
return static_cast<TouchEvent&>(event);
}
EVENT_TYPE_CASTS(TouchEvent)

} // namespace WebCore

Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/dom/UIEvent.h
Expand Up @@ -85,6 +85,8 @@ class UIEvent : public Event {
int m_detail;
};

EVENT_TYPE_CASTS(UIEvent)

} // namespace WebCore

#endif // UIEvent_h
5 changes: 5 additions & 0 deletions Source/WebCore/dom/WheelEvent.cpp
Expand Up @@ -124,4 +124,9 @@ bool WheelEvent::isMouseEvent() const
return false;
}

bool WheelEvent::isWheelEvent() const
{
return true;
}

} // namespace WebCore
4 changes: 4 additions & 0 deletions Source/WebCore/dom/WheelEvent.h
Expand Up @@ -102,6 +102,8 @@ class WheelEvent : public MouseEvent {
unsigned, PassRefPtr<AbstractView>, const IntPoint& screenLocation, const IntPoint& pageLocation,
bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool directionInvertedFromDevice, double timestamp);

virtual bool isWheelEvent() const override;

IntPoint m_wheelDelta;
double m_deltaX;
double m_deltaY;
Expand All @@ -110,6 +112,8 @@ class WheelEvent : public MouseEvent {
bool m_directionInvertedFromDevice;
};

EVENT_TYPE_CASTS(WheelEvent)

} // namespace WebCore

#endif // WheelEvent_h
12 changes: 6 additions & 6 deletions Source/WebCore/html/HTMLAnchorElement.cpp
Expand Up @@ -168,7 +168,7 @@ static void appendServerMapMousePosition(StringBuilder& url, Event* event)
RenderImage* renderer = toRenderImage(imageElement->renderer());

// FIXME: This should probably pass true for useTransforms.
FloatPoint absolutePosition = renderer->absoluteToLocal(FloatPoint(static_cast<MouseEvent*>(event)->pageX(), static_cast<MouseEvent*>(event)->pageY()));
FloatPoint absolutePosition = renderer->absoluteToLocal(FloatPoint(toMouseEvent(event)->pageX(), toMouseEvent(event)->pageY()));
int x = absolutePosition.x();
int y = absolutePosition.y();
url.append('?');
Expand All @@ -194,9 +194,9 @@ void HTMLAnchorElement::defaultEventHandler(Event* event)
if (hasEditableStyle()) {
// This keeps track of the editable block that the selection was in (if it was in one) just before the link was clicked
// for the LiveWhenNotFocused editable link behavior
if (event->type() == eventNames().mousedownEvent && event->isMouseEvent() && static_cast<MouseEvent*>(event)->button() != RightButton && document().frame()) {
if (event->type() == eventNames().mousedownEvent && event->isMouseEvent() && toMouseEvent(event)->button() != RightButton && document().frame()) {
setRootEditableElementForSelectionOnMouseDown(document().frame()->selection().selection().rootEditableElement());
m_wasShiftKeyDownOnMouseDown = static_cast<MouseEvent*>(event)->shiftKey();
m_wasShiftKeyDownOnMouseDown = toMouseEvent(event)->shiftKey();
} else if (event->type() == eventNames().mouseoverEvent) {
// These are cleared on mouseover and not mouseout because their values are needed for drag events,
// but drag events happen after mouse out events.
Expand Down Expand Up @@ -556,7 +556,7 @@ HTMLAnchorElement::EventType HTMLAnchorElement::eventType(Event* event)
{
if (!event->isMouseEvent())
return NonMouseEvent;
return static_cast<MouseEvent*>(event)->shiftKey() ? MouseEventWithShiftKey : MouseEventWithoutShiftKey;
return toMouseEvent(event)->shiftKey() ? MouseEventWithShiftKey : MouseEventWithoutShiftKey;
}

bool HTMLAnchorElement::treatLinkAsLiveForEventType(EventType eventType) const
Expand Down Expand Up @@ -591,12 +591,12 @@ bool HTMLAnchorElement::treatLinkAsLiveForEventType(EventType eventType) const

bool isEnterKeyKeydownEvent(Event* event)
{
return event->type() == eventNames().keydownEvent && event->isKeyboardEvent() && static_cast<KeyboardEvent*>(event)->keyIdentifier() == "Enter";
return event->type() == eventNames().keydownEvent && event->isKeyboardEvent() && toKeyboardEvent(event)->keyIdentifier() == "Enter";
}

bool isLinkClick(Event* event)
{
return event->type() == eventNames().clickEvent && (!event->isMouseEvent() || static_cast<MouseEvent*>(event)->button() != RightButton);
return event->type() == eventNames().clickEvent && (!event->isMouseEvent() || toMouseEvent(event)->button() != RightButton);
}

bool shouldProhibitLinks(Element* element)
Expand Down
6 changes: 3 additions & 3 deletions Source/WebCore/html/HTMLButtonElement.cpp
Expand Up @@ -124,13 +124,13 @@ void HTMLButtonElement::defaultEventHandler(Event* event)
}

if (event->isKeyboardEvent()) {
if (event->type() == eventNames().keydownEvent && static_cast<KeyboardEvent*>(event)->keyIdentifier() == "U+0020") {
if (event->type() == eventNames().keydownEvent && toKeyboardEvent(event)->keyIdentifier() == "U+0020") {
setActive(true, true);
// No setDefaultHandled() - IE dispatches a keypress in this case.
return;
}
if (event->type() == eventNames().keypressEvent) {
switch (static_cast<KeyboardEvent*>(event)->charCode()) {
switch (toKeyboardEvent(event)->charCode()) {
case '\r':
dispatchSimulatedClick(event);
event->setDefaultHandled();
Expand All @@ -141,7 +141,7 @@ void HTMLButtonElement::defaultEventHandler(Event* event)
return;
}
}
if (event->type() == eventNames().keyupEvent && static_cast<KeyboardEvent*>(event)->keyIdentifier() == "U+0020") {
if (event->type() == eventNames().keyupEvent && toKeyboardEvent(event)->keyIdentifier() == "U+0020") {
if (active())
dispatchSimulatedClick(event);
event->setDefaultHandled();
Expand Down

0 comments on commit e2941c6

Please sign in to comment.