From 19dc3194d7e2c48a1b177ab4365d84efb65ac66b Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Mon, 7 Aug 2017 18:27:16 +0900 Subject: [PATCH] Bug 1319340 - part9: Make nsComposerDocumentCommands use concrete class when calling methods of editor r=m_kato MozReview-Commit-ID: 15WBfUpfo6L --HG-- extra : rebase_source : 0e729ea9f2a37555836cca869ea1da4174895960 --- .../composer/nsComposerDocumentCommands.cpp | 162 ++++++++++-------- 1 file changed, 93 insertions(+), 69 deletions(-) diff --git a/editor/composer/nsComposerDocumentCommands.cpp b/editor/composer/nsComposerDocumentCommands.cpp index 8c72d8be9c27..b9de8550bc74 100644 --- a/editor/composer/nsComposerDocumentCommands.cpp +++ b/editor/composer/nsComposerDocumentCommands.cpp @@ -5,6 +5,7 @@ #include "mozilla/HTMLEditor.h" // for HTMLEditor +#include "mozilla/TextEditor.h" // for TextEditor #include "nsCOMPtr.h" // for nsCOMPtr, do_QueryInterface, etc #include "nsCRT.h" // for nsCRT #include "nsComposerCommands.h" // for nsSetDocumentOptionsCommand, etc @@ -16,9 +17,6 @@ #include "nsIDocument.h" // for nsIDocument #include "nsIEditingSession.h" // for nsIEditingSession, etc #include "nsIEditor.h" // for nsIEditor -#include "nsIHTMLEditor.h" // for nsIHTMLEditor -#include "nsIHTMLInlineTableEditor.h" // for nsIHTMLInlineTableEditor -#include "nsIHTMLObjectResizer.h" // for nsIHTMLObjectResizer #include "nsIPlaintextEditor.h" // for nsIPlaintextEditor, etc #include "nsIPresShell.h" // for nsIPresShell #include "nsISelectionController.h" // for nsISelectionController @@ -40,21 +38,18 @@ class nsISupports; static nsresult -GetPresContextFromEditor(nsIEditor *aEditor, nsPresContext **aResult) +GetPresContextFromEditor(TextEditor* aTextEditor, nsPresContext** aResult) { - NS_ENSURE_ARG_POINTER(aResult); + if (NS_WARN_IF(!aResult) || NS_WARN_IF(!aTextEditor)) { + return NS_ERROR_INVALID_ARG; + } *aResult = nullptr; - NS_ENSURE_ARG_POINTER(aEditor); - - nsCOMPtr selCon; - nsresult rv = aEditor->GetSelectionController(getter_AddRefs(selCon)); - NS_ENSURE_SUCCESS(rv, rv); - NS_ENSURE_TRUE(selCon, NS_ERROR_FAILURE); - - nsCOMPtr presShell = do_QueryInterface(selCon); - NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE); - - NS_IF_ADDREF(*aResult = presShell->GetPresContext()); + nsCOMPtr presShell = aTextEditor->GetPresShell(); + if (NS_WARN_IF(!presShell)) { + return NS_ERROR_FAILURE; + } + RefPtr presContext = presShell->GetPresContext(); + presContext.forget(aResult); return NS_OK; } @@ -65,11 +60,13 @@ nsSetDocumentOptionsCommand::IsCommandEnabled(const char * aCommandName, { NS_ENSURE_ARG_POINTER(outCmdEnabled); nsCOMPtr editor = do_QueryInterface(refCon); - if (editor) { - return editor->GetIsSelectionEditable(outCmdEnabled); + if (!editor) { + *outCmdEnabled = false; + return NS_OK; } - - *outCmdEnabled = false; + TextEditor* textEditor = editor->AsTextEditor(); + MOZ_ASSERT(textEditor); + *outCmdEnabled = textEditor->IsSelectionEditable(); return NS_OK; } @@ -88,10 +85,15 @@ nsSetDocumentOptionsCommand::DoCommandParams(const char *aCommandName, NS_ENSURE_ARG_POINTER(aParams); nsCOMPtr editor = do_QueryInterface(refCon); - NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG); + if (NS_WARN_IF(!editor)) { + return NS_ERROR_INVALID_ARG; + } + TextEditor* textEditor = editor->AsTextEditor(); + MOZ_ASSERT(textEditor); RefPtr presContext; - nsresult rv = GetPresContextFromEditor(editor, getter_AddRefs(presContext)); + nsresult rv = + GetPresContextFromEditor(textEditor, getter_AddRefs(presContext)); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(presContext, NS_ERROR_FAILURE); @@ -126,7 +128,11 @@ nsSetDocumentOptionsCommand::GetCommandStateParams(const char *aCommandName, // The base editor owns most state info nsCOMPtr editor = do_QueryInterface(refCon); - NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG); + if (NS_WARN_IF(!editor)) { + return NS_ERROR_INVALID_ARG; + } + TextEditor* textEditor = editor->AsTextEditor(); + MOZ_ASSERT(textEditor); // Always get the enabled state bool outCmdEnabled = false; @@ -136,7 +142,7 @@ nsSetDocumentOptionsCommand::GetCommandStateParams(const char *aCommandName, // get pres context RefPtr presContext; - rv = GetPresContextFromEditor(editor, getter_AddRefs(presContext)); + rv = GetPresContextFromEditor(textEditor, getter_AddRefs(presContext)); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(presContext, NS_ERROR_FAILURE); @@ -198,7 +204,11 @@ nsSetDocumentStateCommand::DoCommandParams(const char *aCommandName, nsISupports *refCon) { nsCOMPtr editor = do_QueryInterface(refCon); - NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG); + if (NS_WARN_IF(!editor)) { + return NS_ERROR_INVALID_ARG; + } + TextEditor* textEditor = editor->AsTextEditor(); + MOZ_ASSERT(textEditor); if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentModified")) { NS_ENSURE_ARG_POINTER(aParams); @@ -211,10 +221,10 @@ nsSetDocumentStateCommand::DoCommandParams(const char *aCommandName, NS_ENSURE_SUCCESS(rv, rv); if (modified) { - return editor->IncrementModificationCount(1); + return textEditor->IncrementModificationCount(1); } - return editor->ResetModificationCount(); + return textEditor->ResetModificationCount(); } if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentReadOnly")) { @@ -224,50 +234,53 @@ nsSetDocumentStateCommand::DoCommandParams(const char *aCommandName, NS_ENSURE_SUCCESS(rvRO, rvRO); uint32_t flags; - editor->GetFlags(&flags); + textEditor->GetFlags(&flags); if (isReadOnly) { flags |= nsIPlaintextEditor::eEditorReadonlyMask; } else { flags &= ~(nsIPlaintextEditor::eEditorReadonlyMask); } - return editor->SetFlags(flags); + return textEditor->SetFlags(flags); } if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentUseCSS")) { NS_ENSURE_ARG_POINTER(aParams); - nsCOMPtr htmleditor = do_QueryInterface(refCon); - NS_ENSURE_TRUE(htmleditor, NS_ERROR_INVALID_ARG); + HTMLEditor* htmlEditor = textEditor->AsHTMLEditor(); + if (NS_WARN_IF(!htmlEditor)) { + return NS_ERROR_INVALID_ARG; + } bool desireCSS; nsresult rvCSS = aParams->GetBooleanValue(STATE_ATTRIBUTE, &desireCSS); NS_ENSURE_SUCCESS(rvCSS, rvCSS); - return htmleditor->SetIsCSSEnabled(desireCSS); + return htmlEditor->SetIsCSSEnabled(desireCSS); } if (!nsCRT::strcmp(aCommandName, "cmd_insertBrOnReturn")) { NS_ENSURE_ARG_POINTER(aParams); - nsCOMPtr htmleditor = do_QueryInterface(refCon); - NS_ENSURE_TRUE(htmleditor, NS_ERROR_INVALID_ARG); + HTMLEditor* htmlEditor = textEditor->AsHTMLEditor(); + if (NS_WARN_IF(!htmlEditor)) { + return NS_ERROR_INVALID_ARG; + } bool insertBrOnReturn; nsresult rvBR = aParams->GetBooleanValue(STATE_ATTRIBUTE, &insertBrOnReturn); NS_ENSURE_SUCCESS(rvBR, rvBR); - return htmleditor->SetReturnInParagraphCreatesNewParagraph(!insertBrOnReturn); + return htmlEditor->SetReturnInParagraphCreatesNewParagraph(!insertBrOnReturn); } if (!nsCRT::strcmp(aCommandName, "cmd_defaultParagraphSeparator")) { if (NS_WARN_IF(!aParams)) { return NS_ERROR_NULL_POINTER; } - nsCOMPtr editor = do_QueryInterface(refCon); - if (NS_WARN_IF(!editor)) { + HTMLEditor* htmlEditor = textEditor->AsHTMLEditor(); + if (NS_WARN_IF(!htmlEditor)) { return NS_ERROR_INVALID_ARG; } - auto htmlEditor = static_cast(editor.get()); nsXPIDLCString newValue; nsresult rv = aParams->GetCStringValue(STATE_ATTRIBUTE, @@ -297,26 +310,30 @@ nsSetDocumentStateCommand::DoCommandParams(const char *aCommandName, if (!nsCRT::strcmp(aCommandName, "cmd_enableObjectResizing")) { NS_ENSURE_ARG_POINTER(aParams); - nsCOMPtr resizer = do_QueryInterface(refCon); - NS_ENSURE_TRUE(resizer, NS_ERROR_INVALID_ARG); + HTMLEditor* htmlEditor = textEditor->AsHTMLEditor(); + if (NS_WARN_IF(!htmlEditor)) { + return NS_ERROR_INVALID_ARG; + } bool enabled; nsresult rvOR = aParams->GetBooleanValue(STATE_ATTRIBUTE, &enabled); NS_ENSURE_SUCCESS(rvOR, rvOR); - return resizer->SetObjectResizingEnabled(enabled); + return htmlEditor->SetObjectResizingEnabled(enabled); } if (!nsCRT::strcmp(aCommandName, "cmd_enableInlineTableEditing")) { NS_ENSURE_ARG_POINTER(aParams); - nsCOMPtr editor = do_QueryInterface(refCon); - NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG); + HTMLEditor* htmlEditor = textEditor->AsHTMLEditor(); + if (NS_WARN_IF(!htmlEditor)) { + return NS_ERROR_INVALID_ARG; + } bool enabled; nsresult rvOR = aParams->GetBooleanValue(STATE_ATTRIBUTE, &enabled); NS_ENSURE_SUCCESS(rvOR, rvOR); - return editor->SetInlineTableEditingEnabled(enabled); + return htmlEditor->SetInlineTableEditingEnabled(enabled); } return NS_ERROR_NOT_IMPLEMENTED; @@ -332,7 +349,11 @@ nsSetDocumentStateCommand::GetCommandStateParams(const char *aCommandName, // The base editor owns most state info nsCOMPtr editor = do_QueryInterface(refCon); - NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG); + if (NS_WARN_IF(!editor)) { + return NS_ERROR_INVALID_ARG; + } + TextEditor* textEditor = editor->AsTextEditor(); + MOZ_ASSERT(textEditor); // Always get the enabled state bool outCmdEnabled = false; @@ -342,7 +363,7 @@ nsSetDocumentStateCommand::GetCommandStateParams(const char *aCommandName, if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentModified")) { bool modified; - rv = editor->GetDocumentModified(&modified); + rv = textEditor->GetDocumentModified(&modified); NS_ENSURE_SUCCESS(rv, rv); return aParams->SetBooleanValue(STATE_ATTRIBUTE, modified); @@ -350,30 +371,30 @@ nsSetDocumentStateCommand::GetCommandStateParams(const char *aCommandName, if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentReadOnly")) { NS_ENSURE_ARG_POINTER(aParams); - - uint32_t flags; - editor->GetFlags(&flags); - bool isReadOnly = flags & nsIPlaintextEditor::eEditorReadonlyMask; - return aParams->SetBooleanValue(STATE_ATTRIBUTE, isReadOnly); + return aParams->SetBooleanValue(STATE_ATTRIBUTE, textEditor->IsReadonly()); } if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentUseCSS")) { NS_ENSURE_ARG_POINTER(aParams); - nsCOMPtr htmleditor = do_QueryInterface(refCon); - NS_ENSURE_TRUE(htmleditor, NS_ERROR_INVALID_ARG); + HTMLEditor* htmlEditor = textEditor->AsHTMLEditor(); + if (NS_WARN_IF(!htmlEditor)) { + return NS_ERROR_INVALID_ARG; + } bool isCSS; - htmleditor->GetIsCSSEnabled(&isCSS); + htmlEditor->GetIsCSSEnabled(&isCSS); return aParams->SetBooleanValue(STATE_ALL, isCSS); } if (!nsCRT::strcmp(aCommandName, "cmd_insertBrOnReturn")) { NS_ENSURE_ARG_POINTER(aParams); - nsCOMPtr htmleditor = do_QueryInterface(refCon); - NS_ENSURE_TRUE(htmleditor, NS_ERROR_INVALID_ARG); + HTMLEditor* htmlEditor = textEditor->AsHTMLEditor(); + if (NS_WARN_IF(!htmlEditor)) { + return NS_ERROR_INVALID_ARG; + } bool createPOnReturn; - htmleditor->GetReturnInParagraphCreatesNewParagraph(&createPOnReturn); + htmlEditor->GetReturnInParagraphCreatesNewParagraph(&createPOnReturn); return aParams->SetBooleanValue(STATE_ATTRIBUTE, !createPOnReturn); } @@ -381,11 +402,10 @@ nsSetDocumentStateCommand::GetCommandStateParams(const char *aCommandName, if (NS_WARN_IF(!aParams)) { return NS_ERROR_NULL_POINTER; } - nsCOMPtr editor = do_QueryInterface(refCon); - if (NS_WARN_IF(!editor)) { + HTMLEditor* htmlEditor = textEditor->AsHTMLEditor(); + if (NS_WARN_IF(!htmlEditor)) { return NS_ERROR_INVALID_ARG; } - auto htmlEditor = static_cast(editor.get()); switch (htmlEditor->GetDefaultParagraphSeparator()) { case ParagraphSeparator::div: @@ -408,21 +428,25 @@ nsSetDocumentStateCommand::GetCommandStateParams(const char *aCommandName, if (!nsCRT::strcmp(aCommandName, "cmd_enableObjectResizing")) { NS_ENSURE_ARG_POINTER(aParams); - nsCOMPtr resizer = do_QueryInterface(refCon); - NS_ENSURE_TRUE(resizer, NS_ERROR_INVALID_ARG); + HTMLEditor* htmlEditor = textEditor->AsHTMLEditor(); + if (NS_WARN_IF(!htmlEditor)) { + return NS_ERROR_INVALID_ARG; + } bool enabled; - resizer->GetObjectResizingEnabled(&enabled); + htmlEditor->GetObjectResizingEnabled(&enabled); return aParams->SetBooleanValue(STATE_ATTRIBUTE, enabled); } if (!nsCRT::strcmp(aCommandName, "cmd_enableInlineTableEditing")) { NS_ENSURE_ARG_POINTER(aParams); - nsCOMPtr editor = do_QueryInterface(refCon); - NS_ENSURE_TRUE(editor, NS_ERROR_INVALID_ARG); + HTMLEditor* htmlEditor = textEditor->AsHTMLEditor(); + if (NS_WARN_IF(!htmlEditor)) { + return NS_ERROR_INVALID_ARG; + } bool enabled; - editor->GetInlineTableEditingEnabled(&enabled); + htmlEditor->GetInlineTableEditingEnabled(&enabled); return aParams->SetBooleanValue(STATE_ATTRIBUTE, enabled); } @@ -532,10 +556,10 @@ nsDocumentStateCommand::GetCommandStateParams(const char *aCommandName, if (!editor) { return NS_OK; } + TextEditor* textEditor = editor->AsTextEditor(); + MOZ_ASSERT(textEditor); - nsCOMPtr domDoc; - editor->GetDocument(getter_AddRefs(domDoc)); - nsCOMPtr doc = do_QueryInterface(domDoc); + nsCOMPtr doc = textEditor->GetDocument(); NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE); nsIURI *uri = doc->GetDocumentURI();