Bug 1374207 - part3: Editor classes should use concrete classes instead of nsI*Editor r=m_kato

HTMLEditor inherits some interface classes.  Therefore, CachedWeakPtr is confused at ambiguous conversion from HTMLEditor to nsISupports.  Therefore, this patch adds second parameter to the template class.

MozReview-Commit-ID: KGSYJHfp1L5

--HG--
extra : rebase_source : 7189372c4c06218c3040701196eac51ded85d3fa
This commit is contained in:
Masayuki Nakano 2017-06-22 15:02:59 +09:00
parent 583dafd5d4
commit c5fbe1617e
12 changed files with 83 additions and 61 deletions

View File

@ -141,32 +141,50 @@ struct IMEState;
* CachedWeakPtr stores a pointer to a class which inherits nsIWeakReference.
* If the instance of the class has already been destroyed, this returns
* nullptr. Otherwise, returns cached pointer.
* If class T inherits nsISupports a lot, specify Base explicitly for avoiding
* ambiguous conversion to nsISupports.
*/
template<class T>
template<class T, class Base = nsISupports>
class CachedWeakPtr final
{
public:
CachedWeakPtr<T>()
CachedWeakPtr<T, Base>()
: mCache(nullptr)
{
}
CachedWeakPtr<T>& operator=(T* aObject)
explicit CachedWeakPtr<T, Base>(T* aObject)
{
mWeakPtr = do_GetWeakReference(aObject);
mWeakPtr = do_GetWeakReference(static_cast<Base*>(aObject));
mCache = aObject;
}
explicit CachedWeakPtr<T, Base>(const nsCOMPtr<T>& aOther)
{
mWeakPtr = do_GetWeakReference(static_cast<Base*>(aOther.get()));
mCache = aOther;
}
explicit CachedWeakPtr<T, Base>(already_AddRefed<T>& aOther)
{
RefPtr<T> other = aOther;
mWeakPtr = do_GetWeakReference(static_cast<Base*>(other.get()));
mCache = other;
}
CachedWeakPtr<T, Base>& operator=(T* aObject)
{
mWeakPtr = do_GetWeakReference(static_cast<Base*>(aObject));
mCache = aObject;
return *this;
}
CachedWeakPtr<T>& operator=(const nsCOMPtr<T>& aOther)
CachedWeakPtr<T, Base>& operator=(const nsCOMPtr<T>& aOther)
{
mWeakPtr = do_GetWeakReference(aOther);
mWeakPtr = do_GetWeakReference(static_cast<Base*>(aOther.get()));
mCache = aOther;
return *this;
}
CachedWeakPtr<T>& operator=(already_AddRefed<T>& aOther)
CachedWeakPtr<T, Base>& operator=(already_AddRefed<T>& aOther)
{
nsCOMPtr<T> other = aOther;
mWeakPtr = do_GetWeakReference(other);
RefPtr<T> other = aOther;
mWeakPtr = do_GetWeakReference(static_cast<Base*>(other.get()));
mCache = other;
return *this;
}
@ -178,7 +196,7 @@ public:
T* get() const
{
if (mCache && !mWeakPtr->IsAlive()) {
const_cast<CachedWeakPtr<T>*>(this)->mCache = nullptr;
const_cast<CachedWeakPtr<T, Base>*>(this)->mCache = nullptr;
}
return mCache;
}

View File

@ -1238,8 +1238,7 @@ EditorEventListener::ShouldHandleNativeKeyBindings(
}
RefPtr<EditorBase> editorBase(mEditorBase);
nsCOMPtr<nsIHTMLEditor> htmlEditor =
do_QueryInterface(static_cast<nsIEditor*>(editorBase));
HTMLEditor* htmlEditor = editorBase->AsHTMLEditor();
if (!htmlEditor) {
return false;
}

View File

@ -144,8 +144,10 @@ EditActionCanceled(nsresult aRv = NS_OK)
}
/***************************************************************************
* stack based helper class for batching a collection of txns inside a
* placeholder txn.
* stack based helper class for batching a collection of transactions inside a
* placeholder transaction.
* XXX This is used by mozInlineSpellChecker. Therefore, cannot use concrete
* editor class.
*/
class MOZ_RAII AutoPlaceHolderBatch
{

View File

@ -31,9 +31,7 @@
#include "nsIDOMNode.h"
#include "nsDOMCSSRGBColor.h"
#include "nsIDOMWindow.h"
#include "nsIEditor.h"
#include "nsIEditRules.h"
#include "nsIHTMLEditor.h"
#include "nsIHTMLObjectResizer.h"
#include "nsINode.h"
#include "nsIPresShell.h"
@ -373,7 +371,7 @@ HTMLEditor::GrabberClicked()
// add a mouse move listener to the editor
nsresult rv = NS_OK;
if (!mMouseMotionListenerP) {
mMouseMotionListenerP = new ResizerMouseMotionListener(this);
mMouseMotionListenerP = new ResizerMouseMotionListener(*this);
if (!mMouseMotionListenerP) {return NS_ERROR_NULL_POINTER;}
nsCOMPtr<nsIDOMEventTarget> piTarget = GetDOMEventTarget();

View File

@ -295,7 +295,7 @@ HTMLEditor::Init(nsIDOMDocument* aDoc,
mTypeInState = new TypeInState();
// init the selection listener for image resizing
mSelectionListenerP = new ResizerSelectionListener(this);
mSelectionListenerP = new ResizerSelectionListener(*this);
if (!IsInteractionAllowed()) {
// ignore any errors from this in case the file is missing

View File

@ -21,7 +21,6 @@
#include "nsIDocumentObserver.h"
#include "nsIDOMElement.h"
#include "nsIDOMEventListener.h"
#include "nsIEditor.h"
#include "nsIEditorMailSupport.h"
#include "nsIEditorStyleSheets.h"
#include "nsIEditorUtils.h"
@ -39,6 +38,7 @@
class nsDocumentFragment;
class nsITransferable;
class nsIClipboard;
class nsIDOMMouseEvent;
class nsILinkHandler;
class nsTableWrapperFrame;
class nsIDOMRange;
@ -135,6 +135,8 @@ public:
using EditorBase::RemoveAttributeOrEquivalent;
using EditorBase::SetAttributeOrEquivalent;
nsresult MouseMove(nsIDOMMouseEvent* aMouseEvent);
// nsStubMutationObserver overrides
NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED

View File

@ -50,8 +50,6 @@
#include "nsIDOMHTMLScriptElement.h"
#include "nsIDOMNode.h"
#include "nsIDocument.h"
#include "nsIEditor.h"
#include "nsIEditorMailSupport.h"
#include "nsIEditRules.h"
#include "nsIFile.h"
#include "nsIInputStream.h"
@ -59,7 +57,6 @@
#include "nsNameSpaceManager.h"
#include "nsINode.h"
#include "nsIParserUtils.h"
#include "nsIPlaintextEditor.h"
#include "nsISupportsImpl.h"
#include "nsISupportsPrimitives.h"
#include "nsISupportsUtils.h"

View File

@ -18,7 +18,6 @@
#include "nsIDOMEventTarget.h"
#include "nsIDOMMouseEvent.h"
#include "nsIDOMNode.h"
#include "nsIEditor.h"
#include "nsIHTMLInlineTableEditor.h"
#include "nsIHTMLObjectResizer.h"
#include "nsISupportsImpl.h"

View File

@ -29,8 +29,6 @@
#include "nsIDOMMouseEvent.h"
#include "nsIDOMNode.h"
#include "nsIDocument.h"
#include "nsIEditor.h"
#include "nsIHTMLObjectResizer.h"
#include "nsIPresShell.h"
#include "nsISupportsUtils.h"
#include "nsPIDOMWindow.h"
@ -53,17 +51,18 @@ using namespace dom;
NS_IMPL_ISUPPORTS(DocumentResizeEventListener, nsIDOMEventListener)
DocumentResizeEventListener::DocumentResizeEventListener(nsIHTMLEditor* aEditor)
DocumentResizeEventListener::DocumentResizeEventListener(
HTMLEditor& aHTMLEditor)
: mHTMLEditorWeak(&aHTMLEditor)
{
mEditor = do_GetWeakReference(aEditor);
}
NS_IMETHODIMP
DocumentResizeEventListener::HandleEvent(nsIDOMEvent* aMouseEvent)
{
nsCOMPtr<nsIHTMLObjectResizer> objectResizer = do_QueryReferent(mEditor);
if (objectResizer) {
return objectResizer->RefreshResizers();
RefPtr<HTMLEditor> htmlEditor = mHTMLEditorWeak.get();
if (htmlEditor) {
return htmlEditor->RefreshResizers();
}
return NS_OK;
}
@ -74,9 +73,9 @@ DocumentResizeEventListener::HandleEvent(nsIDOMEvent* aMouseEvent)
NS_IMPL_ISUPPORTS(ResizerSelectionListener, nsISelectionListener)
ResizerSelectionListener::ResizerSelectionListener(nsIHTMLEditor* aEditor)
ResizerSelectionListener::ResizerSelectionListener(HTMLEditor& aHTMLEditor)
: mHTMLEditorWeak(&aHTMLEditor)
{
mEditor = do_GetWeakReference(aEditor);
}
NS_IMETHODIMP
@ -89,9 +88,9 @@ ResizerSelectionListener::NotifySelectionChanged(nsIDOMDocument* aDOMDocument,
nsISelectionListener::SELECTALL_REASON)) && aSelection) {
// the selection changed and we need to check if we have to
// hide and/or redisplay resizing handles
nsCOMPtr<nsIHTMLEditor> editor = do_QueryReferent(mEditor);
if (editor) {
editor->CheckSelectionStateForAnonymousButtons(aSelection);
RefPtr<HTMLEditor> htmlEditor = mHTMLEditorWeak.get();
if (htmlEditor) {
htmlEditor->CheckSelectionStateForAnonymousButtons(aSelection);
}
}
@ -104,25 +103,25 @@ ResizerSelectionListener::NotifySelectionChanged(nsIDOMDocument* aDOMDocument,
NS_IMPL_ISUPPORTS(ResizerMouseMotionListener, nsIDOMEventListener)
ResizerMouseMotionListener::ResizerMouseMotionListener(nsIHTMLEditor* aEditor)
ResizerMouseMotionListener::ResizerMouseMotionListener(HTMLEditor& aHTMLEditor)
: mHTMLEditorWeak(&aHTMLEditor)
{
mEditor = do_GetWeakReference(aEditor);
}
NS_IMETHODIMP
ResizerMouseMotionListener::HandleEvent(nsIDOMEvent* aMouseEvent)
{
nsCOMPtr<nsIDOMMouseEvent> mouseEvent ( do_QueryInterface(aMouseEvent) );
nsCOMPtr<nsIDOMMouseEvent> mouseEvent = do_QueryInterface(aMouseEvent);
if (!mouseEvent) {
//non-ui event passed in. bad things.
return NS_OK;
}
// Don't do anything special if not an HTML object resizer editor
nsCOMPtr<nsIHTMLObjectResizer> objectResizer = do_QueryReferent(mEditor);
if (objectResizer) {
RefPtr<HTMLEditor> htmlEditor = mHTMLEditorWeak.get();
if (htmlEditor) {
// check if we have to redisplay a resizing shadow
objectResizer->MouseMove(aMouseEvent);
htmlEditor->MouseMove(mouseEvent);
}
return NS_OK;
@ -361,7 +360,7 @@ HTMLEditor::ShowResizersInner(nsIDOMElement* aResizedElement)
return NS_ERROR_NULL_POINTER;
}
mResizeEventListenerP = new DocumentResizeEventListener(this);
mResizeEventListenerP = new DocumentResizeEventListener(*this);
if (!mResizeEventListenerP) {
return NS_ERROR_OUT_OF_MEMORY;
}
@ -533,7 +532,7 @@ HTMLEditor::StartResizing(nsIDOMElement* aHandle)
// add a mouse move listener to the editor
nsresult result = NS_OK;
if (!mMouseMotionListenerP) {
mMouseMotionListenerP = new ResizerMouseMotionListener(this);
mMouseMotionListenerP = new ResizerMouseMotionListener(*this);
if (!mMouseMotionListenerP) {
return NS_ERROR_OUT_OF_MEMORY;
}
@ -809,16 +808,27 @@ HTMLEditor::GetNewResizingHeight(int32_t aX,
NS_IMETHODIMP
HTMLEditor::MouseMove(nsIDOMEvent* aMouseEvent)
{
nsCOMPtr<nsIDOMMouseEvent> mouseEvent = do_QueryInterface(aMouseEvent);
if (NS_WARN_IF(!mouseEvent)) {
return NS_OK;
}
return MouseMove(mouseEvent);
}
nsresult
HTMLEditor::MouseMove(nsIDOMMouseEvent* aMouseEvent)
{
MOZ_ASSERT(aMouseEvent);
NS_NAMED_LITERAL_STRING(leftStr, "left");
NS_NAMED_LITERAL_STRING(topStr, "top");
if (mIsResizing) {
// we are resizing and the mouse pointer's position has changed
// we have to resdisplay the shadow
nsCOMPtr<nsIDOMMouseEvent> mouseEvent ( do_QueryInterface(aMouseEvent) );
int32_t clientX, clientY;
mouseEvent->GetClientX(&clientX);
mouseEvent->GetClientY(&clientY);
aMouseEvent->GetClientX(&clientX);
aMouseEvent->GetClientY(&clientY);
int32_t newX = GetNewResizingX(clientX, clientY);
int32_t newY = GetNewResizingY(clientX, clientY);
@ -838,10 +848,9 @@ HTMLEditor::MouseMove(nsIDOMEvent* aMouseEvent)
}
if (mGrabberClicked) {
nsCOMPtr<nsIDOMMouseEvent> mouseEvent ( do_QueryInterface(aMouseEvent) );
int32_t clientX, clientY;
mouseEvent->GetClientX(&clientX);
mouseEvent->GetClientY(&clientY);
aMouseEvent->GetClientX(&clientX);
aMouseEvent->GetClientY(&clientY);
int32_t xThreshold =
LookAndFeel::GetInt(LookAndFeel::eIntID_DragThresholdX, 1);
@ -855,10 +864,9 @@ HTMLEditor::MouseMove(nsIDOMEvent* aMouseEvent)
}
}
if (mIsMoving) {
nsCOMPtr<nsIDOMMouseEvent> mouseEvent ( do_QueryInterface(aMouseEvent) );
int32_t clientX, clientY;
mouseEvent->GetClientX(&clientX);
mouseEvent->GetClientY(&clientY);
aMouseEvent->GetClientX(&clientX);
aMouseEvent->GetClientY(&clientY);
int32_t newX = mPositionedObjectX + clientX - mOriginalX;
int32_t newY = mPositionedObjectY + clientY - mOriginalY;

View File

@ -6,6 +6,7 @@
#ifndef HTMLEditorObjectResizerUtils_h
#define HTMLEditorObjectResizerUtils_h
#include "mozilla/HTMLEditor.h"
#include "nsIDOMEventListener.h"
#include "nsISelectionListener.h"
#include "nsISupportsImpl.h"
@ -32,7 +33,7 @@ namespace mozilla {
class ResizerSelectionListener final : public nsISelectionListener
{
public:
explicit ResizerSelectionListener(nsIHTMLEditor* aEditor);
explicit ResizerSelectionListener(HTMLEditor& aHTMLEditor);
void Reset();
NS_DECL_ISUPPORTS
@ -40,7 +41,7 @@ public:
protected:
virtual ~ResizerSelectionListener() {}
nsWeakPtr mEditor;
CachedWeakPtr<HTMLEditor, nsIHTMLEditor> mHTMLEditorWeak;
};
/******************************************************************************
@ -50,14 +51,14 @@ protected:
class ResizerMouseMotionListener final : public nsIDOMEventListener
{
public:
explicit ResizerMouseMotionListener(nsIHTMLEditor* aEditor);
explicit ResizerMouseMotionListener(HTMLEditor& aHTMLEditor);
NS_DECL_ISUPPORTS
NS_DECL_NSIDOMEVENTLISTENER
protected:
virtual ~ResizerMouseMotionListener() {}
nsWeakPtr mEditor;
CachedWeakPtr<HTMLEditor, nsIHTMLEditor> mHTMLEditorWeak;
};
/******************************************************************************
@ -67,14 +68,14 @@ protected:
class DocumentResizeEventListener final : public nsIDOMEventListener
{
public:
explicit DocumentResizeEventListener(nsIHTMLEditor* aEditor);
explicit DocumentResizeEventListener(HTMLEditor& aHTMLEditor);
NS_DECL_ISUPPORTS
NS_DECL_NSIDOMEVENTLISTENER
protected:
virtual ~DocumentResizeEventListener() {}
nsWeakPtr mEditor;
CachedWeakPtr<HTMLEditor, nsIHTMLEditor> mHTMLEditorWeak;
};
} // namespace mozilla

View File

@ -26,7 +26,6 @@
#include "nsIContent.h"
#include "nsIContentIterator.h"
#include "nsIDOMElement.h"
#include "nsIEditor.h"
#include "nsIEditRules.h"
#include "nsNameSpaceManager.h"
#include "nsINode.h"

View File

@ -22,7 +22,6 @@
#include "nsIContent.h"
#include "nsIDOMElement.h"
#include "nsIDOMNode.h"
#include "nsIEditor.h"
#include "nsIFrame.h"
#include "nsINode.h"
#include "nsIPresShell.h"