From 113d2cb05c1f0fa7d5ac456a3c7dbe9eeed30cef Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Fri, 18 Oct 2013 15:10:20 +0900 Subject: [PATCH] Bug 920425 part.2 Implement mozilla::WidgetEvent::As*Event() methods r=roc --- content/events/public/MutationEvent.h | 2 + widget/BasicEvents.h | 22 +++++++- widget/ContentEvents.h | 34 +++++++++++++ widget/MiscEvents.h | 9 ++++ widget/MouseEvents.h | 20 ++++++-- widget/TextEvents.h | 21 +++++++- widget/TouchEvents.h | 14 +++++- widget/shared/WidgetEventImpl.cpp | 72 +++++++++++++++------------ 8 files changed, 157 insertions(+), 37 deletions(-) diff --git a/content/events/public/MutationEvent.h b/content/events/public/MutationEvent.h index d1b275a95fc6..834177064765 100644 --- a/content/events/public/MutationEvent.h +++ b/content/events/public/MutationEvent.h @@ -16,6 +16,8 @@ namespace mozilla { class InternalMutationEvent : public WidgetEvent { public: + virtual InternalMutationEvent* AsMutationEvent() MOZ_OVERRIDE { return this; } + InternalMutationEvent(bool aIsTrusted, uint32_t aMessage) : WidgetEvent(aIsTrusted, aMessage, NS_MUTATION_EVENT), mAttrChange(0) diff --git a/widget/BasicEvents.h b/widget/BasicEvents.h index c2afcbefe051..7ec0dbb82885 100644 --- a/widget/BasicEvents.h +++ b/widget/BasicEvents.h @@ -622,7 +622,7 @@ public: mFlags.mBubbles = true; } - ~WidgetEvent() + virtual ~WidgetEvent() { MOZ_COUNT_DTOR(WidgetEvent); } @@ -676,6 +676,20 @@ public: * Utils for checking event types */ + /** + * As*Event() returns the pointer of the instance only when the instance is + * the class or one of its derived class. + */ +#define NS_ROOT_EVENT_CLASS(aPrefix, aName) +#define NS_EVENT_CLASS(aPrefix, aName) \ + virtual aPrefix##aName* As##aName(); \ + const aPrefix##aName* As##aName() const; + +#include "mozilla/EventClassList.h" + +#undef NS_EVENT_CLASS +#undef NS_ROOT_EVENT_CLASS + /** * Returns true if the event is WidgetInputEvent or inherits it. */ @@ -805,6 +819,8 @@ protected: } public: + virtual WidgetGUIEvent* AsGUIEvent() MOZ_OVERRIDE { return this; } + WidgetGUIEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) : WidgetEvent(aIsTrusted, aMessage, NS_GUI_EVENT), widget(aWidget), pluginEvent(nullptr) @@ -892,6 +908,8 @@ protected: } public: + virtual WidgetInputEvent* AsInputEvent() MOZ_OVERRIDE { return this; } + WidgetInputEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, NS_INPUT_EVENT), modifiers(0) @@ -1006,6 +1024,8 @@ protected: } public: + virtual InternalUIEvent* AsUIEvent() MOZ_OVERRIDE { return this; } + InternalUIEvent(bool aIsTrusted, uint32_t aMessage, int32_t aDetail) : WidgetGUIEvent(aIsTrusted, aMessage, nullptr, NS_UI_EVENT), detail(aDetail) diff --git a/widget/ContentEvents.h b/widget/ContentEvents.h index 43c4e1071f0a..b10cf110c26c 100644 --- a/widget/ContentEvents.h +++ b/widget/ContentEvents.h @@ -26,6 +26,11 @@ namespace mozilla { class InternalScriptErrorEvent : public WidgetEvent { public: + virtual InternalScriptErrorEvent* AsScriptErrorEvent() MOZ_OVERRIDE + { + return this; + } + InternalScriptErrorEvent(bool aIsTrusted, uint32_t aMessage) : WidgetEvent(aIsTrusted, aMessage, NS_SCRIPT_ERROR_EVENT), lineNr(0), errorMsg(nullptr), fileName(nullptr) @@ -58,6 +63,11 @@ public: class InternalScrollPortEvent : public WidgetGUIEvent { public: + virtual InternalScrollPortEvent* AsScrollPortEvent() MOZ_OVERRIDE + { + return this; + } + enum orientType { vertical = 0, @@ -90,6 +100,11 @@ public: class InternalScrollAreaEvent : public WidgetGUIEvent { public: + virtual InternalScrollAreaEvent* AsScrollAreaEvent() MOZ_OVERRIDE + { + return this; + } + InternalScrollAreaEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, NS_SCROLLAREA_EVENT) @@ -117,6 +132,8 @@ public: class InternalFormEvent : public WidgetEvent { public: + virtual InternalFormEvent* AsFormEvent() MOZ_OVERRIDE { return this; } + InternalFormEvent(bool aIsTrusted, uint32_t aMessage) : WidgetEvent(aIsTrusted, aMessage, NS_FORM_EVENT), originator(nullptr) @@ -140,6 +157,11 @@ public: class InternalClipboardEvent : public WidgetEvent { public: + virtual InternalClipboardEvent* AsClipboardEvent() MOZ_OVERRIDE + { + return this; + } + InternalClipboardEvent(bool aIsTrusted, uint32_t aMessage) : WidgetEvent(aIsTrusted, aMessage, NS_CLIPBOARD_EVENT) { @@ -163,6 +185,8 @@ public: class InternalFocusEvent : public InternalUIEvent { public: + virtual InternalFocusEvent* AsFocusEvent() MOZ_OVERRIDE { return this; } + InternalFocusEvent(bool aIsTrusted, uint32_t aMessage) : InternalUIEvent(aIsTrusted, aMessage, NS_FOCUS_EVENT, 0), fromRaise(false), isRefocus(false) @@ -192,6 +216,11 @@ public: class InternalTransitionEvent : public WidgetEvent { public: + virtual InternalTransitionEvent* AsTransitionEvent() MOZ_OVERRIDE + { + return this; + } + InternalTransitionEvent(bool aIsTrusted, uint32_t aMessage, const nsAString& aPropertyName, float aElapsedTime, const nsAString& aPseudoElement) : @@ -222,6 +251,11 @@ public: class InternalAnimationEvent : public WidgetEvent { public: + virtual InternalAnimationEvent* AsAnimationEvent() MOZ_OVERRIDE + { + return this; + } + InternalAnimationEvent(bool aIsTrusted, uint32_t aMessage, const nsAString& aAnimationName, float aElapsedTime, const nsAString& aPseudoElement) : diff --git a/widget/MiscEvents.h b/widget/MiscEvents.h index 62dacf1d93c4..04ef86ddaf73 100644 --- a/widget/MiscEvents.h +++ b/widget/MiscEvents.h @@ -22,6 +22,11 @@ namespace mozilla { class WidgetContentCommandEvent : public WidgetGUIEvent { public: + virtual WidgetContentCommandEvent* AsContentCommandEvent() MOZ_OVERRIDE + { + return this; + } + WidgetContentCommandEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget, bool aOnlyEnabledCheck = false) : @@ -72,6 +77,8 @@ public: class WidgetCommandEvent : public WidgetGUIEvent { public: + virtual WidgetCommandEvent* AsCommandEvent() MOZ_OVERRIDE { return this; } + WidgetCommandEvent(bool aIsTrusted, nsIAtom* aEventType, nsIAtom* aCommand, nsIWidget* aWidget) : WidgetGUIEvent(aIsTrusted, NS_USER_DEFINED_EVENT, aWidget, @@ -102,6 +109,8 @@ public: class WidgetPluginEvent : public WidgetGUIEvent { public: + virtual WidgetPluginEvent* AsPluginEvent() MOZ_OVERRIDE { return this; } + WidgetPluginEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, NS_PLUGIN_EVENT), retargetToFocusedDocument(false) diff --git a/widget/MouseEvents.h b/widget/MouseEvents.h index ba996086305d..89861b8d18e7 100644 --- a/widget/MouseEvents.h +++ b/widget/MouseEvents.h @@ -46,7 +46,7 @@ private: friend class dom::PBrowserParent; friend class dom::PBrowserChild; -public: +protected: WidgetMouseEventBase() { } @@ -59,6 +59,9 @@ public: { } +public: + virtual WidgetMouseEventBase* AsMouseEventBase() MOZ_OVERRIDE { return this; } + /// The possible related target nsCOMPtr relatedTarget; @@ -136,11 +139,11 @@ public: eTopLevel }; +protected: WidgetMouseEvent() { } -protected: WidgetMouseEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget, nsEventStructType aStructType, reasonType aReason) : WidgetMouseEventBase(aIsTrusted, aMessage, aWidget, aStructType), @@ -162,6 +165,7 @@ protected: } public: + virtual WidgetMouseEvent* AsMouseEvent() MOZ_OVERRIDE { return this; } WidgetMouseEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget, reasonType aReason, contextType aContext = eNormal) : @@ -187,7 +191,8 @@ public: } #ifdef DEBUG - ~WidgetMouseEvent() { + virtual ~WidgetMouseEvent() + { NS_WARN_IF_FALSE(message != NS_CONTEXTMENU || button == ((context == eNormal) ? eRightButton : eLeftButton), @@ -225,6 +230,8 @@ public: class WidgetDragEvent : public WidgetMouseEvent { public: + virtual WidgetDragEvent* AsDragEvent() MOZ_OVERRIDE { return this; } + WidgetDragEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) : WidgetMouseEvent(aIsTrusted, aMessage, aWidget, NS_DRAG_EVENT, eReal), userCancelled(false) @@ -268,6 +275,11 @@ private: } public: + virtual WidgetMouseScrollEvent* AsMouseScrollEvent() MOZ_OVERRIDE + { + return this; + } + WidgetMouseScrollEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) : WidgetMouseEventBase(aIsTrusted, aMessage, aWidget, NS_MOUSE_SCROLL_EVENT), @@ -312,6 +324,8 @@ private: } public: + virtual WidgetWheelEvent* AsWheelEvent() MOZ_OVERRIDE { return this; } + WidgetWheelEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) : WidgetMouseEventBase(aIsTrusted, aMessage, aWidget, NS_WHEEL_EVENT), deltaX(0.0), deltaY(0.0), deltaZ(0.0), diff --git a/widget/TextEvents.h b/widget/TextEvents.h index b3d635bfa475..b6f72346b6d4 100644 --- a/widget/TextEvents.h +++ b/widget/TextEvents.h @@ -70,11 +70,13 @@ private: friend class dom::PBrowserParent; friend class dom::PBrowserChild; -public: WidgetKeyboardEvent() { } +public: + virtual WidgetKeyboardEvent* AsKeyboardEvent() MOZ_OVERRIDE { return this; } + WidgetKeyboardEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) : WidgetInputEvent(aIsTrusted, aMessage, aWidget, NS_KEY_EVENT), keyCode(0), charCode(0), @@ -314,6 +316,8 @@ public: uint32_t seqno; public: + virtual WidgetTextEvent* AsTextEvent() MOZ_OVERRIDE { return this; } + WidgetTextEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, NS_TEXT_EVENT), rangeCount(0), rangeArray(nullptr), isChar(false) @@ -363,6 +367,11 @@ public: uint32_t seqno; public: + virtual WidgetCompositionEvent* AsCompositionEvent() MOZ_OVERRIDE + { + return this; + } + WidgetCompositionEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, NS_COMPOSITION_EVENT) @@ -403,6 +412,11 @@ private: } public: + virtual WidgetQueryContentEvent* AsQueryContentEvent() MOZ_OVERRIDE + { + return this; + } + WidgetQueryContentEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, NS_QUERY_CONTENT_EVENT), @@ -513,6 +527,11 @@ public: uint32_t seqno; public: + virtual WidgetSelectionEvent* AsSelectionEvent() MOZ_OVERRIDE + { + return this; + } + WidgetSelectionEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget) : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, NS_SELECTION_EVENT), mExpandToClusterBoundary(true), mSucceeded(false) diff --git a/widget/TouchEvents.h b/widget/TouchEvents.h index bc6f1dcd0b86..40d484b1eff0 100644 --- a/widget/TouchEvents.h +++ b/widget/TouchEvents.h @@ -31,6 +31,11 @@ namespace mozilla { class WidgetGestureNotifyEvent : public WidgetGUIEvent { public: + virtual WidgetGestureNotifyEvent* AsGestureNotifyEvent() MOZ_OVERRIDE + { + return this; + } + WidgetGestureNotifyEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget *aWidget) : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, NS_GESTURENOTIFY_EVENT), @@ -67,6 +72,11 @@ public: class WidgetSimpleGestureEvent : public WidgetMouseEventBase { public: + virtual WidgetSimpleGestureEvent* AsSimpleGestureEvent() MOZ_OVERRIDE + { + return this; + } + WidgetSimpleGestureEvent(bool aIsTrusted, uint32_t aMessage, nsIWidget* aWidget, uint32_t aDirection, double aDelta) : @@ -114,6 +124,8 @@ public: class WidgetTouchEvent : public WidgetInputEvent { public: + virtual WidgetTouchEvent* AsTouchEvent() MOZ_OVERRIDE { return this; } + WidgetTouchEvent() { } @@ -134,7 +146,7 @@ public: MOZ_COUNT_CTOR(WidgetTouchEvent); } - ~WidgetTouchEvent() + virtual ~WidgetTouchEvent() { MOZ_COUNT_DTOR(WidgetTouchEvent); } diff --git a/widget/shared/WidgetEventImpl.cpp b/widget/shared/WidgetEventImpl.cpp index 890814d54443..908b69c7844b 100644 --- a/widget/shared/WidgetEventImpl.cpp +++ b/widget/shared/WidgetEventImpl.cpp @@ -4,11 +4,38 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "mozilla/BasicEvents.h" +#include "mozilla/ContentEvents.h" #include "mozilla/MiscEvents.h" #include "mozilla/MouseEvents.h" +#include "mozilla/MutationEvent.h" +#include "mozilla/TextEvents.h" +#include "mozilla/TouchEvents.h" namespace mozilla { +/****************************************************************************** + * As*Event() implementation + ******************************************************************************/ + +#define NS_ROOT_EVENT_CLASS(aPrefix, aName) +#define NS_EVENT_CLASS(aPrefix, aName) \ +aPrefix##aName* \ +WidgetEvent::As##aName() \ +{ \ + return nullptr; \ +} \ +\ +const aPrefix##aName* \ +WidgetEvent::As##aName() const \ +{ \ + return const_cast(this)->As##aName(); \ +} + +#include "mozilla/EventClassList.h" + +#undef NS_EVENT_CLASS +#undef NS_ROOT_EVENT_CLASS + /****************************************************************************** * mozilla::WidgetEvent * @@ -18,26 +45,13 @@ namespace mozilla { bool WidgetEvent::IsInputDerivedEvent() const { - switch (eventStructType) { - case NS_INPUT_EVENT: - case NS_MOUSE_EVENT: - case NS_KEY_EVENT: - case NS_TOUCH_EVENT: - case NS_DRAG_EVENT: - case NS_MOUSE_SCROLL_EVENT: - case NS_WHEEL_EVENT: - case NS_SIMPLE_GESTURE_EVENT: - return true; - default: - return false; - } + return AsInputEvent() != nullptr; } bool WidgetEvent::IsMouseDerivedEvent() const { - return eventStructType == NS_MOUSE_EVENT || - eventStructType == NS_DRAG_EVENT; + return AsMouseEvent() != nullptr; } bool @@ -155,33 +169,31 @@ WidgetEvent::HasPluginActivationEventMessage() const bool WidgetEvent::IsLeftClickEvent() const { - return eventStructType == NS_MOUSE_EVENT && - message == NS_MOUSE_CLICK && - static_cast(this)->button == - WidgetMouseEvent::eLeftButton; + const WidgetMouseEvent* mouseEvent = AsMouseEvent(); + return mouseEvent && message == NS_MOUSE_CLICK && + mouseEvent->button == WidgetMouseEvent::eLeftButton; } bool WidgetEvent::IsContextMenuKeyEvent() const { - return eventStructType == NS_MOUSE_EVENT && - message == NS_CONTEXTMENU && - static_cast(this)->context == - WidgetMouseEvent::eContextMenuKey; + const WidgetMouseEvent* mouseEvent = AsMouseEvent(); + return mouseEvent && message == NS_CONTEXTMENU && + mouseEvent->context == WidgetMouseEvent::eContextMenuKey; } bool WidgetEvent::IsRetargetedNativeEventDelivererForPlugin() const { - return IsNativeEventDelivererForPlugin() && - static_cast(this)->retargetToFocusedDocument; + const WidgetPluginEvent* pluginEvent = AsPluginEvent(); + return pluginEvent && pluginEvent->retargetToFocusedDocument; } bool WidgetEvent::IsNonRetargetedNativeEventDelivererForPlugin() const { - return IsNativeEventDelivererForPlugin() && - !static_cast(this)->retargetToFocusedDocument; + const WidgetPluginEvent* pluginEvent = AsPluginEvent(); + return pluginEvent && !pluginEvent->retargetToFocusedDocument; } bool @@ -228,14 +240,12 @@ WidgetEvent::IsAllowedToDispatchDOMEvent() const // DOM events. // Synthesized button up events also do not cause DOM events because they // do not have a reliable refPoint. - return static_cast(this)->reason == - WidgetMouseEvent::eReal; + return AsMouseEvent()->reason == WidgetMouseEvent::eReal; case NS_WHEEL_EVENT: { // wheel event whose all delta values are zero by user pref applied, it // shouldn't cause a DOM event. - const WidgetWheelEvent* wheelEvent = - static_cast(this); + const WidgetWheelEvent* wheelEvent = AsWheelEvent(); return wheelEvent->deltaX != 0.0 || wheelEvent->deltaY != 0.0 || wheelEvent->deltaZ != 0.0; }