Bug 1319340 - part9: Make nsComposerDocumentCommands use concrete class when calling methods of editor r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 07 Aug 2017 18:27:16 +0900
changeset 644055 7025d650f9fc18345c95478c8776da84259e4550
parent 644054 0400c6764803e5879be0d903fdc7201045e7c1e0
child 725475 6191b4d03a17eb451e2b87cf9a1bca6044019510
push id73292
push usermasayuki@d-toybox.com
push dateThu, 10 Aug 2017 11:23:45 +0000
reviewersm_kato
bugs1319340
milestone57.0a1
Bug 1319340 - part9: Make nsComposerDocumentCommands use concrete class when calling methods of editor r?m_kato MozReview-Commit-ID: 15WBfUpfo6L
editor/composer/nsComposerDocumentCommands.cpp
--- a/editor/composer/nsComposerDocumentCommands.cpp
+++ b/editor/composer/nsComposerDocumentCommands.cpp
@@ -1,29 +1,27 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 
 #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
 #include "nsDebug.h"                    // for NS_ENSURE_ARG_POINTER, etc
 #include "nsError.h"                    // for NS_ERROR_INVALID_ARG, etc
 #include "nsICommandParams.h"           // for nsICommandParams
 #include "nsIDOMDocument.h"             // for nsIDOMDocument
 #include "nsIDocShell.h"                // for nsIDocShell
 #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
 #include "nsISupportsImpl.h"            // for nsPresContext::Release
 #include "nsISupportsUtils.h"           // for NS_IF_ADDREF
 #include "nsIURI.h"                     // for nsIURI
 #include "nsPresContext.h"              // for nsPresContext
 #include "nscore.h"                     // for NS_IMETHODIMP, nsresult, etc
@@ -35,46 +33,45 @@ class nsISupports;
 //defines
 #define STATE_ENABLED  "state_enabled"
 #define STATE_ALL "state_all"
 #define STATE_ATTRIBUTE "state_attribute"
 #define STATE_DATA "state_data"
 
 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<nsISelectionController> selCon;
-  nsresult rv = aEditor->GetSelectionController(getter_AddRefs(selCon));
-  NS_ENSURE_SUCCESS(rv, rv);
-  NS_ENSURE_TRUE(selCon, NS_ERROR_FAILURE);
-
-  nsCOMPtr<nsIPresShell> presShell = do_QueryInterface(selCon);
-  NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
-
-  NS_IF_ADDREF(*aResult = presShell->GetPresContext());
+  nsCOMPtr<nsIPresShell> presShell = aTextEditor->GetPresShell();
+  if (NS_WARN_IF(!presShell)) {
+    return NS_ERROR_FAILURE;
+  }
+  RefPtr<nsPresContext> presContext = presShell->GetPresContext();
+  presContext.forget(aResult);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsSetDocumentOptionsCommand::IsCommandEnabled(const char * aCommandName,
                                               nsISupports *refCon,
                                               bool *outCmdEnabled)
 {
   NS_ENSURE_ARG_POINTER(outCmdEnabled);
   nsCOMPtr<nsIEditor> 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;
 }
 
 NS_IMETHODIMP
 nsSetDocumentOptionsCommand::DoCommand(const char *aCommandName,
                                        nsISupports *refCon)
 {
   return NS_ERROR_NOT_IMPLEMENTED;
@@ -83,20 +80,25 @@ nsSetDocumentOptionsCommand::DoCommand(c
 NS_IMETHODIMP
 nsSetDocumentOptionsCommand::DoCommandParams(const char *aCommandName,
                                              nsICommandParams *aParams,
                                              nsISupports *refCon)
 {
   NS_ENSURE_ARG_POINTER(aParams);
 
   nsCOMPtr<nsIEditor> 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<nsPresContext> 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);
 
   int32_t animationMode;
   rv = aParams->GetLongValue("imageAnimation", &animationMode);
   if (NS_SUCCEEDED(rv)) {
     // for possible values of animation mode, see:
     // http://lxr.mozilla.org/seamonkey/source/image/public/imgIContainer.idl
@@ -121,27 +123,31 @@ nsSetDocumentOptionsCommand::GetCommandS
                                                    nsICommandParams *aParams,
                                                    nsISupports *refCon)
 {
   NS_ENSURE_ARG_POINTER(aParams);
   NS_ENSURE_ARG_POINTER(refCon);
 
   // The base editor owns most state info
   nsCOMPtr<nsIEditor> 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;
   IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
   nsresult rv = aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // get pres context
   RefPtr<nsPresContext> 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);
 
   int32_t animationMode;
   rv = aParams->GetLongValue("imageAnimation", &animationMode);
   if (NS_SUCCEEDED(rv)) {
     // for possible values of animation mode, see
     // http://lxr.mozilla.org/seamonkey/source/image/public/imgIContainer.idl
@@ -193,86 +199,93 @@ nsSetDocumentStateCommand::DoCommand(con
 }
 
 NS_IMETHODIMP
 nsSetDocumentStateCommand::DoCommandParams(const char *aCommandName,
                                            nsICommandParams *aParams,
                                            nsISupports *refCon)
 {
   nsCOMPtr<nsIEditor> 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);
 
     bool modified;
     nsresult rv = aParams->GetBooleanValue(STATE_ATTRIBUTE, &modified);
 
     // Should we fail if this param wasn't set?
     // I'm not sure we should be that strict
     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")) {
     NS_ENSURE_ARG_POINTER(aParams);
     bool isReadOnly;
     nsresult rvRO = aParams->GetBooleanValue(STATE_ATTRIBUTE, &isReadOnly);
     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<nsIHTMLEditor> 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<nsIHTMLEditor> 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<nsIHTMLEditor> 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<HTMLEditor*>(editor.get());
 
     nsXPIDLCString newValue;
     nsresult rv = aParams->GetCStringValue(STATE_ATTRIBUTE,
                                            getter_Copies(newValue));
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
@@ -292,105 +305,112 @@ nsSetDocumentStateCommand::DoCommandPara
 
     // This should not be reachable from nsHTMLDocument::ExecCommand
     NS_WARNING("Invalid default paragraph separator");
     return NS_ERROR_UNEXPECTED;
   }
 
   if (!nsCRT::strcmp(aCommandName, "cmd_enableObjectResizing")) {
     NS_ENSURE_ARG_POINTER(aParams);
-    nsCOMPtr<nsIHTMLObjectResizer> 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<nsIHTMLInlineTableEditor> 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;
 }
 
 NS_IMETHODIMP
 nsSetDocumentStateCommand::GetCommandStateParams(const char *aCommandName,
                                                  nsICommandParams *aParams,
                                                  nsISupports *refCon)
 {
   NS_ENSURE_ARG_POINTER(aParams);
   NS_ENSURE_ARG_POINTER(refCon);
 
   // The base editor owns most state info
   nsCOMPtr<nsIEditor> 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;
   IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
   nsresult rv = aParams->SetBooleanValue(STATE_ENABLED, outCmdEnabled);
   NS_ENSURE_SUCCESS(rv, rv);
 
   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);
   }
 
   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<nsIHTMLEditor> 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<nsIHTMLEditor> 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);
   }
 
   if (!nsCRT::strcmp(aCommandName, "cmd_defaultParagraphSeparator")) {
     if (NS_WARN_IF(!aParams)) {
       return NS_ERROR_NULL_POINTER;
     }
-    nsCOMPtr<nsIHTMLEditor> 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<HTMLEditor*>(editor.get());
 
     switch (htmlEditor->GetDefaultParagraphSeparator()) {
       case ParagraphSeparator::div:
         aParams->SetCStringValue(STATE_ATTRIBUTE, "div");
         return NS_OK;
 
       case ParagraphSeparator::p:
         aParams->SetCStringValue(STATE_ATTRIBUTE, "p");
@@ -403,31 +423,35 @@ nsSetDocumentStateCommand::GetCommandSta
       default:
         MOZ_ASSERT_UNREACHABLE("Invalid paragraph separator value");
         return NS_ERROR_UNEXPECTED;
     }
   }
 
   if (!nsCRT::strcmp(aCommandName, "cmd_enableObjectResizing")) {
     NS_ENSURE_ARG_POINTER(aParams);
-    nsCOMPtr<nsIHTMLObjectResizer> 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<nsIHTMLInlineTableEditor> 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);
   }
 
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
 /**
  * Commands just for state notification
@@ -527,20 +551,20 @@ nsDocumentStateCommand::GetCommandStateP
     return NS_OK;
   }
 
   if (!nsCRT::strcmp(aCommandName, "obs_documentLocationChanged")) {
     nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
     if (!editor) {
       return NS_OK;
     }
+    TextEditor* textEditor = editor->AsTextEditor();
+    MOZ_ASSERT(textEditor);
 
-    nsCOMPtr<nsIDOMDocument> domDoc;
-    editor->GetDocument(getter_AddRefs(domDoc));
-    nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
+    nsCOMPtr<nsIDocument> doc = textEditor->GetDocument();
     NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
 
     nsIURI *uri = doc->GetDocumentURI();
     NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
 
     return aParams->SetISupportsValue(STATE_DATA, (nsISupports*)uri);
   }