Bug 1504131 - part 2: Remove ResizerMouseMotionListener r=m_kato

ResizerMouseMotionListener listens to "mousemove" events for resizers
or grabber to move absolutely position element and it calls only
HTMLEditor::MouseMove().  Fortunately, neither EditorEventListener not
HTMLEditorEventListener listens to "mousemove" events.  Therefore, we
can use HTMLEditorEventListener instead.

Differential Revision: https://phabricator.services.mozilla.com/D10869

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2018-11-06 06:09:18 +00:00
Родитель b5f863bf51
Коммит dbc466a1f7
7 изменённых файлов: 180 добавлений и 121 удалений

Просмотреть файл

@ -6,6 +6,7 @@
#include <math.h>
#include "HTMLEditorEventListener.h"
#include "HTMLEditorObjectResizerUtils.h"
#include "HTMLEditRules.h"
#include "HTMLEditUtils.h"
@ -436,20 +437,14 @@ HTMLEditor::SnapToGrid(int32_t& newX, int32_t& newY)
nsresult
HTMLEditor::GrabberClicked()
{
// add a mouse move listener to the editor
nsresult rv = NS_OK;
if (!mMouseMotionListenerP) {
EventTarget* eventTarget = GetDOMEventTarget();
if (NS_WARN_IF(!eventTarget)) {
return NS_ERROR_FAILURE;
}
mMouseMotionListenerP = new ResizerMouseMotionListener(*this);
EventListenerManager* eventListenerManager =
eventTarget->GetOrCreateListenerManager();
eventListenerManager->AddEventListenerByType(
mMouseMotionListenerP,
NS_LITERAL_STRING("mousemove"),
TrustedEventsAtSystemGroupBubble());
if (NS_WARN_IF(!mEventListener)) {
return NS_ERROR_NOT_INITIALIZED;
}
nsresult rv =
static_cast<HTMLEditorEventListener*>(mEventListener.get())->
ListenToMouseMoveEventForGrabber(true);
if (NS_WARN_IF(NS_FAILED(rv))) {
return NS_OK;
}
mGrabberClicked = true;
return rv;
@ -467,16 +462,13 @@ HTMLEditor::EndMoving()
mPositioningShadow = nullptr;
}
EventTarget* eventTarget = GetDOMEventTarget();
if (eventTarget && mMouseMotionListenerP) {
EventListenerManager* eventListenerManager =
eventTarget->GetOrCreateListenerManager();
eventListenerManager->RemoveEventListenerByType(
mMouseMotionListenerP,
NS_LITERAL_STRING("mousemove"),
TrustedEventsAtSystemGroupBubble());
if (mEventListener) {
DebugOnly<nsresult> rvIgnored =
static_cast<HTMLEditorEventListener*>(mEventListener.get())->
ListenToMouseMoveEventForGrabber(false);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
"Failed to remove mousemove event listener");
}
mMouseMotionListenerP = nullptr;
mGrabberClicked = false;
mIsMoving = false;

Просмотреть файл

@ -286,7 +286,6 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(HTMLEditor, TextEditor)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResizingShadow)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResizingInfo)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResizedObject)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMouseMotionListenerP)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAbsolutelyPositionedObject)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGrabber)
@ -543,27 +542,6 @@ HTMLEditor::RemoveEventListeners()
return;
}
RefPtr<EventTarget> target = GetDOMEventTarget();
if (target) {
// mMouseMotionListenerP can be registerd with other targets than the DOM
// event receiver that we can reach from here. But nonetheless, unregister
// the event listener with the DOM event receiver (if it's registerd with
// other targets, it'll get unregisterd once the target goes
// away).
if (mMouseMotionListenerP) {
// mMouseMotionListenerP might be registerd either as bubbling or
// capturing, unregister by both.
target->RemoveEventListener(NS_LITERAL_STRING("mousemove"),
mMouseMotionListenerP, false);
target->RemoveEventListener(NS_LITERAL_STRING("mousemove"),
mMouseMotionListenerP, true);
}
}
mMouseMotionListenerP = nullptr;
TextEditor::RemoveEventListeners();
}

Просмотреть файл

@ -227,7 +227,7 @@ public:
* event callback when the mouse pointer is moved
* @param aMouseEvent [IN] the event
*/
nsresult OnMouseMove(dom::MouseEvent* aMouseEvent);
MOZ_CAN_RUN_SCRIPT nsresult OnMouseMove(dom::MouseEvent* aMouseEvent);
/**
* IsCSSEnabled() returns true if this editor treats styles with style
@ -2559,8 +2559,6 @@ protected:
RefPtr<Element> mResizedObject;
nsCOMPtr<nsIDOMEventListener> mMouseMotionListenerP;
int32_t mOriginalX;
int32_t mOriginalY;

Просмотреть файл

@ -45,6 +45,16 @@ HTMLEditorEventListener::Disconnect()
EditorEventListener::Disconnect();
}
if (mListeningToMouseMoveEventForResizers) {
DebugOnly<nsresult> rvIgnored = ListenToMouseMoveEventForResizers(false);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
"Failed to remove resize event listener of resizers");
}
if (mListeningToMouseMoveEventForGrabber) {
DebugOnly<nsresult> rvIgnored = ListenToMouseMoveEventForGrabber(false);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
"Failed to remove resize event listener of grabber");
}
if (mListeningToResizeEvent) {
DebugOnly<nsresult> rvIgnored = ListenToWindowResizeEvent(false);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
@ -59,6 +69,23 @@ HTMLEditorEventListener::HandleEvent(Event* aEvent)
{
WidgetEvent* internalEvent = aEvent->WidgetEventPtr();
switch (internalEvent->mMessage) {
case eMouseMove: {
if (DetachedFromEditor()) {
return NS_OK;
}
RefPtr<MouseEvent> mouseEvent = aEvent->AsMouseEvent();
if (NS_WARN_IF(!mouseEvent)) {
return NS_ERROR_FAILURE;
}
RefPtr<HTMLEditor> htmlEditor = mEditorBase->AsHTMLEditor();
MOZ_ASSERT(htmlEditor);
DebugOnly<nsresult> rvIgnored = htmlEditor->OnMouseMove(mouseEvent);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
"Resizers failed to handle mousemove events");
return NS_OK;
}
case eResize: {
if (DetachedFromEditor()) {
return NS_OK;
@ -80,6 +107,82 @@ HTMLEditorEventListener::HandleEvent(Event* aEvent)
return NS_OK;
}
}
nsresult
HTMLEditorEventListener::ListenToMouseMoveEventForResizersOrGrabber(
bool aListen,
bool aForGrabber)
{
MOZ_ASSERT(aForGrabber ?
mListeningToMouseMoveEventForGrabber != aListen :
mListeningToMouseMoveEventForResizers != aListen);
if (NS_WARN_IF(DetachedFromEditor())) {
return aListen ? NS_ERROR_FAILURE : NS_OK;
}
if (aListen) {
if (aForGrabber && mListeningToMouseMoveEventForResizers) {
// We've already added mousemove event listener for resizers.
mListeningToMouseMoveEventForGrabber = true;
return NS_OK;
}
if (!aForGrabber && mListeningToMouseMoveEventForGrabber) {
// We've already added mousemove event listener for grabber.
mListeningToMouseMoveEventForResizers = true;
return NS_OK;
}
} else {
if (aForGrabber && mListeningToMouseMoveEventForResizers) {
// We need to keep listening to mousemove event listener for resizers.
mListeningToMouseMoveEventForGrabber = false;
return NS_OK;
}
if (!aForGrabber && mListeningToMouseMoveEventForGrabber) {
// We need to keep listening to mousemove event listener for grabber.
mListeningToMouseMoveEventForResizers = false;
return NS_OK;
}
}
EventTarget* target = mEditorBase->AsHTMLEditor()->GetDOMEventTarget();
if (NS_WARN_IF(!target)) {
return NS_ERROR_FAILURE;
}
// Listen to mousemove events in the system group since web apps may stop
// propagation before we receive the events.
EventListenerManager* eventListenerManager =
target->GetOrCreateListenerManager();
if (NS_WARN_IF(!eventListenerManager)) {
return NS_ERROR_FAILURE;
}
if (aListen) {
eventListenerManager->AddEventListenerByType(
this,
NS_LITERAL_STRING("mousemove"),
TrustedEventsAtSystemGroupBubble());
if (aForGrabber) {
mListeningToMouseMoveEventForGrabber = true;
} else {
mListeningToMouseMoveEventForResizers = true;
}
return NS_OK;
}
eventListenerManager->RemoveEventListenerByType(
this,
NS_LITERAL_STRING("mousemove"),
TrustedEventsAtSystemGroupBubble());
if (aForGrabber) {
mListeningToMouseMoveEventForGrabber = false;
} else {
mListeningToMouseMoveEventForResizers = false;
}
return NS_OK;
}
nsresult
HTMLEditorEventListener::ListenToWindowResizeEvent(bool aListen)
{

Просмотреть файл

@ -18,6 +18,8 @@ class HTMLEditorEventListener final : public EditorEventListener
public:
HTMLEditorEventListener()
: EditorEventListener()
, mListeningToMouseMoveEventForResizers(false)
, mListeningToMouseMoveEventForGrabber(false)
, mListeningToResizeEvent(false)
{
}
@ -37,6 +39,41 @@ public:
virtual void Disconnect() override;
/**
* ListenToMouseMoveEventForResizers() starts to listen to or stop
* listening to "mousemove" events for resizers.
*/
nsresult ListenToMouseMoveEventForResizers(bool aListen)
{
if (aListen == mListeningToMouseMoveEventForResizers) {
return NS_OK;
}
nsresult rv =
ListenToMouseMoveEventForResizersOrGrabber(aListen, false);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
/**
* ListenToMouseMoveEventForResizers() starts to listen to or stop
* listening to "mousemove" events for grabber to move absolutely
* positioned element.
*/
nsresult ListenToMouseMoveEventForGrabber(bool aListen)
{
if (aListen == mListeningToMouseMoveEventForGrabber) {
return NS_OK;
}
nsresult rv =
ListenToMouseMoveEventForResizersOrGrabber(aListen, true);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
/**
* ListenToWindowResizeEvent() starts to listen to or stop listening to
* "resize" events of the document.
@ -50,6 +87,11 @@ protected:
MOZ_CAN_RUN_SCRIPT
virtual nsresult MouseClick(WidgetMouseEvent* aMouseClickEvent) override;
nsresult
ListenToMouseMoveEventForResizersOrGrabber(bool aListen, bool aForGrabber);
bool mListeningToMouseMoveEventForResizers;
bool mListeningToMouseMoveEventForGrabber;
bool mListeningToResizeEvent;
};

Просмотреть файл

@ -39,36 +39,6 @@ namespace mozilla {
using namespace dom;
/******************************************************************************
* mozilla::ResizerMouseMotionListener
******************************************************************************/
NS_IMPL_ISUPPORTS(ResizerMouseMotionListener, nsIDOMEventListener)
ResizerMouseMotionListener::ResizerMouseMotionListener(HTMLEditor& aHTMLEditor)
: mHTMLEditorWeak(&aHTMLEditor)
{
}
NS_IMETHODIMP
ResizerMouseMotionListener::HandleEvent(Event* aMouseEvent)
{
MouseEvent* mouseEvent = aMouseEvent->AsMouseEvent();
if (!mouseEvent) {
//non-ui event passed in. bad things.
return NS_OK;
}
// Don't do anything special if not an HTML object resizer editor
RefPtr<HTMLEditor> htmlEditor = mHTMLEditorWeak.get();
if (htmlEditor) {
// check if we have to redisplay a resizing shadow
htmlEditor->OnMouseMove(mouseEvent);
}
return NS_OK;
}
/******************************************************************************
* mozilla::HTMLEditor
******************************************************************************/
@ -529,8 +499,6 @@ HTMLEditor::HideResizersInternal()
ManualNACPtr resizingShadow(std::move(mResizingShadow));
ManualNACPtr resizingInfo(std::move(mResizingInfo));
RefPtr<Element> activatedHandle(std::move(mActivatedHandle));
nsCOMPtr<nsIDOMEventListener> mouseMotionListener(
std::move(mMouseMotionListenerP));
RefPtr<Element> resizedObject(std::move(mResizedObject));
// Remvoe all handles.
@ -573,13 +541,15 @@ HTMLEditor::HideResizersInternal()
// Remove resizing state of the target element.
resizedObject->UnsetAttr(kNameSpaceID_None, nsGkAtoms::_moz_resizing, true);
// Remove mousemove event listener from the event target.
nsCOMPtr<EventTarget> target = GetDOMEventTarget();
NS_WARNING_ASSERTION(target, "GetDOMEventTarget() returned nullptr");
if (!mEventListener) {
return NS_OK;
}
if (target && mouseMotionListener) {
target->RemoveEventListener(NS_LITERAL_STRING("mousemove"),
mouseMotionListener, true);
nsresult rv =
static_cast<HTMLEditorEventListener*>(mEventListener.get())->
ListenToMouseMoveEventForResizers(false);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// Remove resize event listener from the window.
@ -587,9 +557,8 @@ HTMLEditor::HideResizersInternal()
return NS_OK;
}
nsresult rv =
static_cast<HTMLEditorEventListener*>(mEventListener.get())->
ListenToWindowResizeEvent(false);
rv = static_cast<HTMLEditorEventListener*>(mEventListener.get())->
ListenToWindowResizeEvent(false);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
@ -654,22 +623,16 @@ HTMLEditor::StartResizing(Element* aHandle)
mResizedObjectHeight);
// add a mouse move listener to the editor
nsresult result = NS_OK;
if (!mMouseMotionListenerP) {
mMouseMotionListenerP = new ResizerMouseMotionListener(*this);
if (!mMouseMotionListenerP) {
return NS_ERROR_OUT_OF_MEMORY;
}
EventTarget* target = GetDOMEventTarget();
NS_ENSURE_TRUE(target, NS_ERROR_FAILURE);
result = target->AddEventListener(NS_LITERAL_STRING("mousemove"),
mMouseMotionListenerP, true);
NS_ASSERTION(NS_SUCCEEDED(result),
"failed to register mouse motion listener");
if (NS_WARN_IF(!mEventListener)) {
return NS_ERROR_NOT_INITIALIZED;
}
return result;
nsresult rv =
static_cast<HTMLEditorEventListener*>(mEventListener.get())->
ListenToMouseMoveEventForResizers(true);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
nsresult

Просмотреть файл

@ -26,23 +26,6 @@ namespace mozilla {
#define kBottom NS_LITERAL_STRING("s")
#define kBottomRight NS_LITERAL_STRING("se")
/******************************************************************************
* mozilla::ResizerMouseMotionListener
******************************************************************************/
class ResizerMouseMotionListener final : public nsIDOMEventListener
{
public:
explicit ResizerMouseMotionListener(HTMLEditor& aHTMLEditor);
NS_DECL_ISUPPORTS
NS_DECL_NSIDOMEVENTLISTENER
protected:
virtual ~ResizerMouseMotionListener() {}
CachedWeakPtr<HTMLEditor, nsIHTMLEditor> mHTMLEditorWeak;
};
} // namespace mozilla
#endif // #ifndef HTMLEditorObjectResizerUtils_h