From a7bc62dcfe5495c8b53532c1b585af07171b4403 Mon Sep 17 00:00:00 2001 From: wolfbeast Date: Tue, 26 Jun 2018 13:53:12 +0200 Subject: Issue #12 Part 2: Stop using nsIDOMEvent in IsAcceptableInputEvent. --- dom/events/Event.cpp | 16 +++---------- editor/libeditor/EditorBase.cpp | 23 ++++++++----------- editor/libeditor/EditorBase.h | 8 +++---- editor/libeditor/EditorEventListener.cpp | 34 ++++++++++++++++------------ editor/libeditor/HTMLEditor.cpp | 12 ++++------ editor/libeditor/HTMLEditor.h | 2 +- editor/libeditor/HTMLEditorEventListener.cpp | 6 ++++- widget/BasicEvents.h | 6 +++++ widget/WidgetEventImpl.cpp | 34 ++++++++++++++++++++++++++++ 9 files changed, 87 insertions(+), 54 deletions(-) diff --git a/dom/events/Event.cpp b/dom/events/Event.cpp index 4b9776c0a..f33bfa5a8 100755 --- a/dom/events/Event.cpp +++ b/dom/events/Event.cpp @@ -280,16 +280,10 @@ Event::GetType(nsAString& aType) return NS_OK; } -static EventTarget* -GetDOMEventTarget(nsIDOMEventTarget* aTarget) -{ - return aTarget ? aTarget->GetTargetForDOMEvent() : nullptr; -} - EventTarget* Event::GetTarget() const { - return GetDOMEventTarget(mEvent->mTarget); + return mEvent->GetDOMEventTarget(); } NS_IMETHODIMP @@ -302,7 +296,7 @@ Event::GetTarget(nsIDOMEventTarget** aTarget) EventTarget* Event::GetCurrentTarget() const { - return GetDOMEventTarget(mEvent->mCurrentTarget); + return mEvent->GetCurrentDOMEventTarget(); } NS_IMETHODIMP @@ -349,11 +343,7 @@ Event::GetExplicitOriginalTarget(nsIDOMEventTarget** aRealEventTarget) EventTarget* Event::GetOriginalTarget() const { - if (mEvent->mOriginalTarget) { - return GetDOMEventTarget(mEvent->mOriginalTarget); - } - - return GetTarget(); + return mEvent->GetOriginalDOMEventTarget(); } NS_IMETHODIMP diff --git a/editor/libeditor/EditorBase.cpp b/editor/libeditor/EditorBase.cpp index 0c4a2a41d..f7988cd1a 100644 --- a/editor/libeditor/EditorBase.cpp +++ b/editor/libeditor/EditorBase.cpp @@ -5092,19 +5092,16 @@ EditorBase::IsActiveInDOMWindow() } bool -EditorBase::IsAcceptableInputEvent(nsIDOMEvent* aEvent) +EditorBase::IsAcceptableInputEvent(WidgetGUIEvent* aGUIEvent) { // If the event is trusted, the event should always cause input. - NS_ENSURE_TRUE(aEvent, false); - - WidgetEvent* widgetEvent = aEvent->WidgetEventPtr(); - if (NS_WARN_IF(!widgetEvent)) { + if (NS_WARN_IF(!aGUIEvent)) { return false; } // If this is dispatched by using cordinates but this editor doesn't have // focus, we shouldn't handle it. - if (widgetEvent->IsUsingCoordinates()) { + if (aGUIEvent->IsUsingCoordinates()) { nsCOMPtr focusedContent = GetFocusedContent(); if (!focusedContent) { return false; @@ -5117,8 +5114,7 @@ EditorBase::IsAcceptableInputEvent(nsIDOMEvent* aEvent) // Note that if we allow to handle such events, editor may be confused by // strange event order. bool needsWidget = false; - WidgetGUIEvent* widgetGUIEvent = nullptr; - switch (widgetEvent->mMessage) { + switch (aGUIEvent->mMessage) { case eUnidentifiedEvent: // If events are not created with proper event interface, their message // are initialized with eUnidentifiedEvent. Let's ignore such event. @@ -5130,25 +5126,26 @@ EditorBase::IsAcceptableInputEvent(nsIDOMEvent* aEvent) case eCompositionCommitAsIs: // Don't allow composition events whose internal event are not // WidgetCompositionEvent. - widgetGUIEvent = aEvent->WidgetEventPtr()->AsCompositionEvent(); + if (!aGUIEvent->AsCompositionEvent()) { + return false; + } needsWidget = true; break; default: break; } - if (needsWidget && - (!widgetGUIEvent || !widgetGUIEvent->mWidget)) { + if (needsWidget && !aGUIEvent->mWidget) { return false; } // Accept all trusted events. - if (widgetEvent->IsTrusted()) { + if (aGUIEvent->IsTrusted()) { return true; } // Ignore untrusted mouse event. // XXX Why are we handling other untrusted input events? - if (widgetEvent->AsMouseEventBase()) { + if (aGUIEvent->AsMouseEventBase()) { return false; } diff --git a/editor/libeditor/EditorBase.h b/editor/libeditor/EditorBase.h index 86be780c0..dbd00771e 100644 --- a/editor/libeditor/EditorBase.h +++ b/editor/libeditor/EditorBase.h @@ -871,12 +871,12 @@ public: virtual bool IsActiveInDOMWindow(); /** - * Whether the aEvent should be handled by this editor or not. When this - * returns FALSE, The aEvent shouldn't be handled on this editor, - * i.e., The aEvent should be handled by another inner editor or ancestor + * Whether the aGUIEvent should be handled by this editor or not. When this + * returns false, The aGUIEvent shouldn't be handled on this editor, + * i.e., The aGUIEvent should be handled by another inner editor or ancestor * elements. */ - virtual bool IsAcceptableInputEvent(nsIDOMEvent* aEvent); + virtual bool IsAcceptableInputEvent(WidgetGUIEvent* aGUIEvent); /** * FindSelectionRoot() returns a selection root of this editor when aNode diff --git a/editor/libeditor/EditorEventListener.cpp b/editor/libeditor/EditorEventListener.cpp index 69833ce84..5694fc1f2 100644 --- a/editor/libeditor/EditorEventListener.cpp +++ b/editor/libeditor/EditorEventListener.cpp @@ -590,7 +590,11 @@ EditorEventListener::KeyPress(nsIDOMKeyEvent* aKeyEvent) { NS_ENSURE_TRUE(aKeyEvent, NS_OK); - if (!mEditorBase->IsAcceptableInputEvent(aKeyEvent->AsEvent())) { + WidgetKeyboardEvent* keypressEvent = + aKeyEvent->AsEvent()->WidgetEventPtr()->AsKeyboardEvent(); + MOZ_ASSERT(keypressEvent, + "DOM key event's internal event must be WidgetKeyboardEvent"); + if (!mEditorBase->IsAcceptableInputEvent(keypressEvent)) { return NS_OK; } @@ -619,11 +623,7 @@ EditorEventListener::KeyPress(nsIDOMKeyEvent* aKeyEvent) } // Now, ask the native key bindings to handle the event. - WidgetKeyboardEvent* keyEvent = - aKeyEvent->AsEvent()->WidgetEventPtr()->AsKeyboardEvent(); - MOZ_ASSERT(keyEvent, - "DOM key event's internal event must be WidgetKeyboardEvent"); - nsIWidget* widget = keyEvent->mWidget; + nsIWidget* widget = keypressEvent->mWidget; // If the event is created by chrome script, the widget is always nullptr. if (!widget) { nsCOMPtr ps = GetPresShell(); @@ -635,7 +635,7 @@ EditorEventListener::KeyPress(nsIDOMKeyEvent* aKeyEvent) nsCOMPtr doc = mEditorBase->GetDocument(); bool handled = widget->ExecuteNativeKeyBinding( nsIWidget::NativeKeyBindingsForRichTextEditor, - *keyEvent, DoCommandCallback, doc); + *keypressEvent, DoCommandCallback, doc); if (handled) { aKeyEvent->AsEvent()->PreventDefault(); } @@ -646,8 +646,10 @@ nsresult EditorEventListener::MouseClick(nsIDOMMouseEvent* aMouseEvent) { // nothing to do if editor isn't editable or clicked on out of the editor. + WidgetMouseEvent* clickEvent = + aMouseEvent->AsEvent()->WidgetEventPtr()->AsMouseEvent(); if (mEditorBase->IsReadonly() || mEditorBase->IsDisabled() || - !mEditorBase->IsAcceptableInputEvent(aMouseEvent->AsEvent())) { + !mEditorBase->IsAcceptableInputEvent(clickEvent)) { return NS_OK; } @@ -782,7 +784,9 @@ EditorEventListener::MouseDown(nsIDOMMouseEvent* aMouseEvent) nsresult EditorEventListener::HandleText(nsIDOMEvent* aTextEvent) { - if (!mEditorBase->IsAcceptableInputEvent(aTextEvent)) { + WidgetCompositionEvent* compositionChangeEvent = + aTextEvent->WidgetEventPtr()->AsCompositionEvent(); + if (!mEditorBase->IsAcceptableInputEvent(compositionChangeEvent)) { return NS_OK; } @@ -793,8 +797,6 @@ EditorEventListener::HandleText(nsIDOMEvent* aTextEvent) // AsCompositionEvent() should always return non-nullptr. Anyway, it'll be // checked in TextEditor::UpdateIMEComposition(). - WidgetCompositionEvent* compositionChangeEvent = - aTextEvent->WidgetEventPtr()->AsCompositionEvent(); return mEditorBase->UpdateIMEComposition(compositionChangeEvent); } @@ -1036,18 +1038,20 @@ EditorEventListener::CanDrop(nsIDOMDragEvent* aEvent) nsresult EditorEventListener::HandleStartComposition(nsIDOMEvent* aCompositionEvent) { - if (!mEditorBase->IsAcceptableInputEvent(aCompositionEvent)) { - return NS_OK; - } WidgetCompositionEvent* compositionStart = aCompositionEvent->WidgetEventPtr()->AsCompositionEvent(); + if (!mEditorBase->IsAcceptableInputEvent(compositionStart)) { + return NS_OK; + } return mEditorBase->BeginIMEComposition(compositionStart); } void EditorEventListener::HandleEndComposition(nsIDOMEvent* aCompositionEvent) { - if (!mEditorBase->IsAcceptableInputEvent(aCompositionEvent)) { + WidgetCompositionEvent* compositionEnd = + aCompositionEvent->WidgetEventPtr()->AsCompositionEvent(); + if (!mEditorBase->IsAcceptableInputEvent(compositionEnd)) { return; } diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index dd47ffd3c..368f7a3e9 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -5161,23 +5161,22 @@ HTMLEditor::OurWindowHasFocus() } bool -HTMLEditor::IsAcceptableInputEvent(nsIDOMEvent* aEvent) +HTMLEditor::IsAcceptableInputEvent(WidgetGUIEvent* aGUIEvent) { - if (!EditorBase::IsAcceptableInputEvent(aEvent)) { + if (!EditorBase::IsAcceptableInputEvent(aGUIEvent)) { return false; } // While there is composition, all composition events in its top level window // are always fired on the composing editor. Therefore, if this editor has // composition, the composition events should be handled in this editor. - if (mComposition && aEvent->WidgetEventPtr()->AsCompositionEvent()) { + if (mComposition && aGUIEvent->AsCompositionEvent()) { return true; } NS_ENSURE_TRUE(mDocWeak, false); - nsCOMPtr target; - aEvent->GetTarget(getter_AddRefs(target)); + nsCOMPtr target = aGUIEvent->GetDOMEventTarget(); NS_ENSURE_TRUE(target, false); nsCOMPtr document = do_QueryReferent(mDocWeak); @@ -5201,8 +5200,7 @@ HTMLEditor::IsAcceptableInputEvent(nsIDOMEvent* aEvent) // If the event is a mouse event, we need to check if the target content is // the focused editing host or its descendant. - nsCOMPtr mouseEvent = do_QueryInterface(aEvent); - if (mouseEvent) { + if (aGUIEvent->AsMouseEventBase()) { nsIContent* editingHost = GetActiveEditingHost(); // If there is no active editing host, we cannot handle the mouse event // correctly. diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h index 477ec9741..dfcdd8d6b 100644 --- a/editor/libeditor/HTMLEditor.h +++ b/editor/libeditor/HTMLEditor.h @@ -114,7 +114,7 @@ public: virtual Element* GetEditorRoot() override; virtual already_AddRefed FindSelectionRoot( nsINode *aNode) override; - virtual bool IsAcceptableInputEvent(nsIDOMEvent* aEvent) override; + virtual bool IsAcceptableInputEvent(WidgetGUIEvent* aGUIEvent) override; virtual already_AddRefed GetInputEventTargetContent() override; virtual bool IsEditable(nsINode* aNode) override; using EditorBase::IsEditable; diff --git a/editor/libeditor/HTMLEditorEventListener.cpp b/editor/libeditor/HTMLEditorEventListener.cpp index 8fb9459c2..c4d857908 100644 --- a/editor/libeditor/HTMLEditorEventListener.cpp +++ b/editor/libeditor/HTMLEditorEventListener.cpp @@ -7,6 +7,7 @@ #include "HTMLEditUtils.h" #include "mozilla/HTMLEditor.h" +#include "mozilla/MouseEvents.h" #include "mozilla/dom/Event.h" #include "mozilla/dom/Selection.h" #include "nsCOMPtr.h" @@ -71,10 +72,13 @@ HTMLEditorEventListener::MouseUp(nsIDOMMouseEvent* aMouseEvent) nsresult HTMLEditorEventListener::MouseDown(nsIDOMMouseEvent* aMouseEvent) { + WidgetMouseEvent* mousedownEvent = + aMouseEvent->AsEvent()->WidgetEventPtr()->AsMouseEvent(); + HTMLEditor* htmlEditor = GetHTMLEditor(); // Contenteditable should disregard mousedowns outside it. // IsAcceptableInputEvent() checks it for a mouse event. - if (!htmlEditor->IsAcceptableInputEvent(aMouseEvent->AsEvent())) { + if (!htmlEditor->IsAcceptableInputEvent(mousedownEvent)) { // If it's not acceptable mousedown event (including when mousedown event // is fired outside of the active editing host), we need to commit // composition because it will be change the selection to the clicked diff --git a/widget/BasicEvents.h b/widget/BasicEvents.h index a6228f179..f24d7ca8a 100644 --- a/widget/BasicEvents.h +++ b/widget/BasicEvents.h @@ -407,10 +407,16 @@ public: nsString mSpecifiedEventTypeString; // Event targets, needed by DOM Events + // Note that when you need event target for DOM event, you should use + // Get*DOMEventTarget() instead of accessing these members directly. nsCOMPtr mTarget; nsCOMPtr mCurrentTarget; nsCOMPtr mOriginalTarget; + dom::EventTarget* GetDOMEventTarget() const; + dom::EventTarget* GetCurrentDOMEventTarget() const; + dom::EventTarget* GetOriginalDOMEventTarget() const; + void AssignEventData(const WidgetEvent& aEvent, bool aCopyTargets) { // mClass should be initialized with the constructor. diff --git a/widget/WidgetEventImpl.cpp b/widget/WidgetEventImpl.cpp index 7dd292cb0..59c80672b 100644 --- a/widget/WidgetEventImpl.cpp +++ b/widget/WidgetEventImpl.cpp @@ -12,6 +12,7 @@ #include "mozilla/Preferences.h" #include "mozilla/TextEvents.h" #include "mozilla/TouchEvents.h" +#include "nsIDOMEventTarget.h" #include "nsPrintfCString.h" namespace mozilla { @@ -410,6 +411,39 @@ WidgetEvent::IsAllowedToDispatchDOMEvent() const } } +/****************************************************************************** + * mozilla::WidgetEvent + * + * Misc methods. + ******************************************************************************/ + +static dom::EventTarget* +GetTargetForDOMEvent(nsIDOMEventTarget* aTarget) +{ + return aTarget ? aTarget->GetTargetForDOMEvent() : nullptr; +} + +dom::EventTarget* +WidgetEvent::GetDOMEventTarget() const +{ + return GetTargetForDOMEvent(mTarget); +} + +dom::EventTarget* +WidgetEvent::GetCurrentDOMEventTarget() const +{ + return GetTargetForDOMEvent(mCurrentTarget); +} + +dom::EventTarget* +WidgetEvent::GetOriginalDOMEventTarget() const +{ + if (mOriginalTarget) { + return GetTargetForDOMEvent(mOriginalTarget); + } + return GetDOMEventTarget(); +} + /****************************************************************************** * mozilla::WidgetInputEvent ******************************************************************************/ -- cgit v1.2.3