Bug 1546578 - part 8: Make EditorCommand implement nsIControllerCommand::GetCommandStateParams() and call internal GetCommandStateParams() method which take TextEditor directly r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Sat, 27 Apr 2019 07:35:56 +0000
changeset 530501 a90af6c1b7d1bf34cc34928da7cf5169f37837b0
parent 530500 4e5609da308736145af7e211d3048a45df5c289b
child 530502 7c45189445aab81ca8032e963fd66ecf394157c2
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1546578
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1546578 - part 8: Make EditorCommand implement nsIControllerCommand::GetCommandStateParams() and call internal GetCommandStateParams() method which take TextEditor directly r=m_kato This patch makes `EditorCommand` implement `nsIControllerCommand::GetCommandStateParams()` and then, makes it call internal `GetCommandStateParams()` method which takes `TextEditor` or `nsIEditingSession` instead. This makes each implementation really simpler. Differential Revision: https://phabricator.services.mozilla.com/D28696
editor/libeditor/EditorCommands.cpp
editor/libeditor/EditorCommands.h
editor/libeditor/HTMLEditorCommands.cpp
editor/libeditor/HTMLEditorDocumentCommands.cpp
--- a/editor/libeditor/EditorCommands.cpp
+++ b/editor/libeditor/EditorCommands.cpp
@@ -4,26 +4,21 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "EditorCommands.h"
 
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/Assertions.h"
 #include "mozilla/FlushType.h"
 #include "mozilla/TextEditor.h"
+#include "mozilla/dom/Document.h"
 #include "mozilla/dom/Selection.h"
 #include "nsCommandParams.h"
-#include "nsCOMPtr.h"
-#include "nsCRT.h"
-#include "nsDebug.h"
-#include "nsError.h"
 #include "nsIClipboard.h"
-#include "nsID.h"
-#include "mozilla/dom/Document.h"
-#include "nsIEditor.h"
+#include "nsIEditingSession.h"
 #include "nsISelectionController.h"
 #include "nsITransferable.h"
 #include "nsString.h"
 #include "nsAString.h"
 
 class nsISupports;
 
 #define STATE_ENABLED "state_enabled"
@@ -82,16 +77,38 @@ EditorCommand::DoCommandParams(const cha
       DoCommandParams(aCommandName, MOZ_KnownLive(aParams->AsCommandParams()),
                       MOZ_KnownLive(*editor->AsTextEditor()));
   NS_WARNING_ASSERTION(
       NS_SUCCEEDED(rv),
       "Failed to do command from nsIControllerCommand::DoCommandParams()");
   return rv;
 }
 
+NS_IMETHODIMP
+EditorCommand::GetCommandStateParams(const char* aCommandName,
+                                     nsICommandParams* aParams,
+                                     nsISupports* aCommandRefCon) {
+  if (NS_WARN_IF(!aCommandName) || NS_WARN_IF(!aParams)) {
+    return NS_ERROR_INVALID_ARG;
+  }
+  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
+  if (editor) {
+    return GetCommandStateParams(aCommandName, *aParams->AsCommandParams(),
+                                 editor->AsTextEditor(), nullptr);
+  }
+  nsCOMPtr<nsIEditingSession> editingSession =
+      do_QueryInterface(aCommandRefCon);
+  if (editingSession) {
+    return GetCommandStateParams(aCommandName, *aParams->AsCommandParams(),
+                                 nullptr, editingSession);
+  }
+  return GetCommandStateParams(aCommandName, *aParams->AsCommandParams(),
+                               nullptr, nullptr);
+}
+
 /******************************************************************************
  * mozilla::UndoCommand
  ******************************************************************************/
 
 StaticRefPtr<UndoCommand> UndoCommand::sInstance;
 
 bool UndoCommand::IsCommandEnabled(const char* aCommandName,
                                    TextEditor* aTextEditor) const {
@@ -107,23 +124,21 @@ nsresult UndoCommand::DoCommand(const ch
 }
 
 nsresult UndoCommand::DoCommandParams(const char* aCommandName,
                                       nsCommandParams* aParams,
                                       TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-UndoCommand::GetCommandStateParams(const char* aCommandName,
-                                   nsICommandParams* aParams,
-                                   nsISupports* aCommandRefCon) {
-  bool canUndo;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
+nsresult UndoCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::RedoCommand
  ******************************************************************************/
 
 StaticRefPtr<RedoCommand> RedoCommand::sInstance;
 
@@ -141,23 +156,21 @@ nsresult RedoCommand::DoCommand(const ch
 }
 
 nsresult RedoCommand::DoCommandParams(const char* aCommandName,
                                       nsCommandParams* aParams,
                                       TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-RedoCommand::GetCommandStateParams(const char* aCommandName,
-                                   nsICommandParams* aParams,
-                                   nsISupports* aCommandRefCon) {
-  bool canUndo;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
+nsresult RedoCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::CutCommand
  ******************************************************************************/
 
 StaticRefPtr<CutCommand> CutCommand::sInstance;
 
@@ -175,23 +188,21 @@ nsresult CutCommand::DoCommand(const cha
 }
 
 nsresult CutCommand::DoCommandParams(const char* aCommandName,
                                      nsCommandParams* aParams,
                                      TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-CutCommand::GetCommandStateParams(const char* aCommandName,
-                                  nsICommandParams* aParams,
-                                  nsISupports* aCommandRefCon) {
-  bool canUndo;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
+nsresult CutCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::CutOrDeleteCommand
  ******************************************************************************/
 
 StaticRefPtr<CutOrDeleteCommand> CutOrDeleteCommand::sInstance;
 
@@ -218,23 +229,21 @@ nsresult CutOrDeleteCommand::DoCommand(c
 }
 
 nsresult CutOrDeleteCommand::DoCommandParams(const char* aCommandName,
                                              nsCommandParams* aParams,
                                              TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-CutOrDeleteCommand::GetCommandStateParams(const char* aCommandName,
-                                          nsICommandParams* aParams,
-                                          nsISupports* aCommandRefCon) {
-  bool canUndo;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
+nsresult CutOrDeleteCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::CopyCommand
  ******************************************************************************/
 
 StaticRefPtr<CopyCommand> CopyCommand::sInstance;
 
@@ -252,23 +261,21 @@ nsresult CopyCommand::DoCommand(const ch
 }
 
 nsresult CopyCommand::DoCommandParams(const char* aCommandName,
                                       nsCommandParams* aParams,
                                       TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-CopyCommand::GetCommandStateParams(const char* aCommandName,
-                                   nsICommandParams* aParams,
-                                   nsISupports* aCommandRefCon) {
-  bool canUndo;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
+nsresult CopyCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::CopyOrDeleteCommand
  ******************************************************************************/
 
 StaticRefPtr<CopyOrDeleteCommand> CopyOrDeleteCommand::sInstance;
 
@@ -295,23 +302,21 @@ nsresult CopyOrDeleteCommand::DoCommand(
 }
 
 nsresult CopyOrDeleteCommand::DoCommandParams(const char* aCommandName,
                                               nsCommandParams* aParams,
                                               TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-CopyOrDeleteCommand::GetCommandStateParams(const char* aCommandName,
-                                           nsICommandParams* aParams,
-                                           nsISupports* aCommandRefCon) {
-  bool canUndo;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
+nsresult CopyOrDeleteCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::PasteCommand
  ******************************************************************************/
 
 StaticRefPtr<PasteCommand> PasteCommand::sInstance;
 
@@ -330,23 +335,21 @@ nsresult PasteCommand::DoCommand(const c
 }
 
 nsresult PasteCommand::DoCommandParams(const char* aCommandName,
                                        nsCommandParams* aParams,
                                        TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-PasteCommand::GetCommandStateParams(const char* aCommandName,
-                                    nsICommandParams* aParams,
-                                    nsISupports* aCommandRefCon) {
-  bool canUndo;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
+nsresult PasteCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::PasteTransferableCommand
  ******************************************************************************/
 
 StaticRefPtr<PasteTransferableCommand> PasteTransferableCommand::sInstance;
 
@@ -385,42 +388,36 @@ nsresult PasteTransferableCommand::DoCom
   // via "editor".
   nsresult rv = aTextEditor.PasteTransferable(trans);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
   return NS_OK;
 }
 
-NS_IMETHODIMP
-PasteTransferableCommand::GetCommandStateParams(const char* aCommandName,
-                                                nsICommandParams* aParams,
-                                                nsISupports* aCommandRefCon) {
-  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
-  if (NS_WARN_IF(!editor)) {
-    return NS_ERROR_FAILURE;
+nsresult PasteTransferableCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  if (NS_WARN_IF(!aTextEditor)) {
+    return NS_ERROR_INVALID_ARG;
   }
 
-  nsCOMPtr<nsISupports> supports =
-      aParams->AsCommandParams()->GetISupports("transferable");
+  nsCOMPtr<nsISupports> supports = aParams.GetISupports("transferable");
   if (NS_WARN_IF(!supports)) {
     return NS_ERROR_FAILURE;
   }
 
   nsCOMPtr<nsITransferable> trans;
   trans = do_QueryInterface(supports);
   if (NS_WARN_IF(!trans)) {
     return NS_ERROR_FAILURE;
   }
 
-  TextEditor* textEditor = editor->AsTextEditor();
-  MOZ_ASSERT(textEditor);
-
-  return aParams->AsCommandParams()->SetBool(
-      STATE_ENABLED, textEditor->CanPasteTransferable(trans));
+  return aParams.SetBool(STATE_ENABLED,
+                         aTextEditor->CanPasteTransferable(trans));
 }
 
 /******************************************************************************
  * mozilla::SwitchTextDirectionCommand
  ******************************************************************************/
 
 StaticRefPtr<SwitchTextDirectionCommand> SwitchTextDirectionCommand::sInstance;
 
@@ -438,24 +435,21 @@ nsresult SwitchTextDirectionCommand::DoC
 }
 
 nsresult SwitchTextDirectionCommand::DoCommandParams(
     const char* aCommandName, nsCommandParams* aParams,
     TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-SwitchTextDirectionCommand::GetCommandStateParams(const char* aCommandName,
-                                                  nsICommandParams* aParams,
-                                                  nsISupports* aCommandRefCon) {
-  bool canSwitchTextDirection = true;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &canSwitchTextDirection);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED,
-                                             canSwitchTextDirection);
+nsresult SwitchTextDirectionCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::DeleteCommand
  ******************************************************************************/
 
 StaticRefPtr<DeleteCommand> DeleteCommand::sInstance;
 
@@ -507,23 +501,21 @@ nsresult DeleteCommand::DoCommand(const 
 }
 
 nsresult DeleteCommand::DoCommandParams(const char* aCommandName,
                                         nsCommandParams* aParams,
                                         TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-DeleteCommand::GetCommandStateParams(const char* aCommandName,
-                                     nsICommandParams* aParams,
-                                     nsISupports* aCommandRefCon) {
-  bool canUndo;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
+nsresult DeleteCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::SelectAllCommand
  ******************************************************************************/
 
 StaticRefPtr<SelectAllCommand> SelectAllCommand::sInstance;
 
@@ -549,23 +541,21 @@ nsresult SelectAllCommand::DoCommand(con
 }
 
 nsresult SelectAllCommand::DoCommandParams(const char* aCommandName,
                                            nsCommandParams* aParams,
                                            TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-SelectAllCommand::GetCommandStateParams(const char* aCommandName,
-                                        nsICommandParams* aParams,
-                                        nsISupports* aCommandRefCon) {
-  bool canUndo;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
+nsresult SelectAllCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::SelectionMoveCommands
  ******************************************************************************/
 
 StaticRefPtr<SelectionMoveCommands> SelectionMoveCommands::sInstance;
 
@@ -678,23 +668,21 @@ nsresult SelectionMoveCommands::DoComman
 }
 
 nsresult SelectionMoveCommands::DoCommandParams(const char* aCommandName,
                                                 nsCommandParams* aParams,
                                                 TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-SelectionMoveCommands::GetCommandStateParams(const char* aCommandName,
-                                             nsICommandParams* aParams,
-                                             nsISupports* aCommandRefCon) {
-  bool canUndo;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &canUndo);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, canUndo);
+nsresult SelectionMoveCommands::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::InsertPlaintextCommand
  ******************************************************************************/
 
 StaticRefPtr<InsertPlaintextCommand> InsertPlaintextCommand::sInstance;
 
@@ -739,27 +727,21 @@ nsresult InsertPlaintextCommand::DoComma
   //     better, however, this may not cause two or more placeholder
   //     transactions to the top transaction since its name may not be
   //     nsGkAtoms::TypingTxnName.
   rv = aTextEditor.InsertTextAsAction(text);
   NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to insert the text");
   return NS_OK;
 }
 
-NS_IMETHODIMP
-InsertPlaintextCommand::GetCommandStateParams(const char* aCommandName,
-                                              nsICommandParams* aParams,
-                                              nsISupports* aCommandRefCon) {
-  if (NS_WARN_IF(!aParams)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-
-  bool aIsEnabled = false;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &aIsEnabled);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, aIsEnabled);
+nsresult InsertPlaintextCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::InsertParagraphCommand
  ******************************************************************************/
 
 StaticRefPtr<InsertParagraphCommand> InsertParagraphCommand::sInstance;
 
@@ -781,27 +763,21 @@ nsresult InsertParagraphCommand::DoComma
 }
 
 nsresult InsertParagraphCommand::DoCommandParams(
     const char* aCommandName, nsCommandParams* aParams,
     TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-InsertParagraphCommand::GetCommandStateParams(const char* aCommandName,
-                                              nsICommandParams* aParams,
-                                              nsISupports* aCommandRefCon) {
-  if (NS_WARN_IF(!aParams)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-
-  bool aIsEnabled = false;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &aIsEnabled);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, aIsEnabled);
+nsresult InsertParagraphCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::InsertLineBreakCommand
  ******************************************************************************/
 
 StaticRefPtr<InsertLineBreakCommand> InsertLineBreakCommand::sInstance;
 
@@ -823,27 +799,21 @@ nsresult InsertLineBreakCommand::DoComma
 }
 
 nsresult InsertLineBreakCommand::DoCommandParams(
     const char* aCommandName, nsCommandParams* aParams,
     TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-InsertLineBreakCommand::GetCommandStateParams(const char* aCommandName,
-                                              nsICommandParams* aParams,
-                                              nsISupports* aCommandRefCon) {
-  if (NS_WARN_IF(!aParams)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-
-  bool aIsEnabled = false;
-  IsCommandEnabled(aCommandName, aCommandRefCon, &aIsEnabled);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, aIsEnabled);
+nsresult InsertLineBreakCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /******************************************************************************
  * mozilla::PasteQuotationCommand
  ******************************************************************************/
 
 StaticRefPtr<PasteQuotationCommand> PasteQuotationCommand::sInstance;
 
@@ -867,24 +837,20 @@ nsresult PasteQuotationCommand::DoComman
 }
 
 nsresult PasteQuotationCommand::DoCommandParams(const char* aCommandName,
                                                 nsCommandParams* aParams,
                                                 TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-PasteQuotationCommand::GetCommandStateParams(const char* aCommandName,
-                                             nsICommandParams* aParams,
-                                             nsISupports* aCommandRefCon) {
-  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
-  if (!editor) {
+nsresult PasteQuotationCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  if (!aTextEditor) {
     return NS_OK;
   }
-  TextEditor* textEditor = editor->AsTextEditor();
-  MOZ_ASSERT(textEditor);
-  aParams->AsCommandParams()->SetBool(
-      STATE_ENABLED, textEditor->CanPaste(nsIClipboard::kGlobalClipboard));
+  aParams.SetBool(STATE_ENABLED,
+                  aTextEditor->CanPaste(nsIClipboard::kGlobalClipboard));
   return NS_OK;
 }
 
 }  // namespace mozilla
--- a/editor/libeditor/EditorCommands.h
+++ b/editor/libeditor/EditorCommands.h
@@ -10,16 +10,17 @@
 #include "nsIControllerCommand.h"
 #include "nsISupportsImpl.h"
 #include "nsRefPtrHashtable.h"
 #include "nsStringFwd.h"
 
 class nsAtom;
 class nsCommandParams;
 class nsICommandParams;
+class nsIEditingSession;
 
 namespace mozilla {
 
 class HTMLEditor;
 class TextEditor;
 
 /**
  * This is a base class for commands registered with the editor controller.
@@ -38,26 +39,40 @@ class EditorCommand : public nsIControll
                               bool* aIsEnabled) final;
   MOZ_CAN_RUN_SCRIPT
   NS_IMETHOD DoCommand(const char* aCommandName,
                        nsISupports* aCommandRefCon) final;
   MOZ_CAN_RUN_SCRIPT
   NS_IMETHOD DoCommandParams(const char* aCommandName,
                              nsICommandParams* aParams,
                              nsISupports* aCommandRefCon) final;
+  NS_IMETHOD GetCommandStateParams(const char* aCommandName,
+                                   nsICommandParams* aParams,
+                                   nsISupports* aCommandRefCon) final;
 
   virtual bool IsCommandEnabled(const char* aCommandName,
                                 TextEditor* aTextEditor) const = 0;
   MOZ_CAN_RUN_SCRIPT
   virtual nsresult DoCommand(const char* aCommandName,
                              TextEditor& aTextEditor) const = 0;
   MOZ_CAN_RUN_SCRIPT
   virtual nsresult DoCommandParams(const char* aCommandName,
                                    nsCommandParams* aParams,
                                    TextEditor& aTextEditor) const = 0;
+  /**
+   * @param aTextEditor         If the context is an editor, should be set to
+   *                            it.  Otherwise, nullptr.
+   * @param aEditingSession     If the context is an editing session, should be
+   *                            set to it.  This usually occurs if editor has
+   *                            not been created yet during initialization.
+   *                            Otherwise, nullptr.
+   */
+  virtual nsresult GetCommandStateParams(
+      const char* aCommandName, nsCommandParams& aParams,
+      TextEditor* aTextEditor, nsIEditingSession* aEditingSession) const = 0;
 
  protected:
   EditorCommand() = default;
   virtual ~EditorCommand() = default;
 };
 
 #define NS_DECL_EDITOR_COMMAND_METHODS(_cmd)                             \
  public:                                                                 \
@@ -68,19 +83,21 @@ class EditorCommand : public nsIControll
   virtual nsresult DoCommand(const char* aCommandName,                   \
                              TextEditor& aTextEditor) const final;       \
   using EditorCommand::DoCommand;                                        \
   MOZ_CAN_RUN_SCRIPT                                                     \
   virtual nsresult DoCommandParams(const char* aCommandName,             \
                                    nsCommandParams* aParams,             \
                                    TextEditor& aTextEditor) const final; \
   using EditorCommand::DoCommandParams;                                  \
-  NS_IMETHOD GetCommandStateParams(const char* aCommandName,             \
-                                   nsICommandParams* aParams,            \
-                                   nsISupports* aCommandRefCon) final;
+  virtual nsresult GetCommandStateParams(                                \
+      const char* aCommandName, nsCommandParams& aParams,                \
+      TextEditor* aTextEditor, nsIEditingSession* aEditingSession)       \
+      const final;                                                       \
+  using EditorCommand::GetCommandStateParams;
 
 #define NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \
  public:                                                   \
   static _cmd* GetInstance() {                             \
     if (!sInstance) {                                      \
       sInstance = new _cmd();                              \
     }                                                      \
     return sInstance;                                      \
--- a/editor/libeditor/HTMLEditorCommands.cpp
+++ b/editor/libeditor/HTMLEditorCommands.cpp
@@ -7,26 +7,21 @@
 
 #include "mozilla/Assertions.h"  // for MOZ_ASSERT, etc
 #include "mozilla/EditorBase.h"  // for EditorBase
 #include "mozilla/ErrorResult.h"
 #include "mozilla/HTMLEditor.h"  // for HTMLEditor
 #include "mozilla/dom/Element.h"
 #include "nsAString.h"
 #include "nsCommandParams.h"          // for nsCommandParams, etc
-#include "nsCOMPtr.h"                 // for nsCOMPtr, do_QueryInterface, etc
 #include "nsComponentManagerUtils.h"  // for do_CreateInstance
-#include "nsDebug.h"                  // for NS_ENSURE_TRUE, etc
-#include "nsError.h"                  // for NS_OK, NS_ERROR_FAILURE, etc
 #include "nsGkAtoms.h"                // for nsGkAtoms, nsGkAtoms::font, etc
 #include "nsAtom.h"                   // for nsAtom, etc
 #include "nsIClipboard.h"             // for nsIClipboard, etc
-#include "nsID.h"
-#include "nsIEditor.h"        // for nsIEditor
-#include "nsIHTMLEditor.h"    // for nsIHTMLEditor, etc
+#include "nsIEditingSession.h"
 #include "nsLiteralString.h"  // for NS_LITERAL_STRING
 #include "nsReadableUtils.h"  // for EmptyString
 #include "nsString.h"         // for nsAutoString, nsString, etc
 #include "nsStringFwd.h"      // for nsString
 
 class nsISupports;
 
 namespace mozilla {
@@ -83,33 +78,31 @@ nsresult StateUpdatingCommandBase::DoCom
 }
 
 nsresult StateUpdatingCommandBase::DoCommandParams(
     const char* aCommandName, nsCommandParams* aParams,
     TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-StateUpdatingCommandBase::GetCommandStateParams(const char* aCommandName,
-                                                nsICommandParams* aParams,
-                                                nsISupports* refCon) {
-  nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
-  if (!editor) {
+nsresult StateUpdatingCommandBase::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  if (!aTextEditor) {
     return NS_OK;
   }
-  mozilla::HTMLEditor* htmlEditor = editor->AsHTMLEditor();
+  HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
   if (NS_WARN_IF(!htmlEditor)) {
     return NS_ERROR_FAILURE;
   }
   RefPtr<nsAtom> tagName = TagName(aCommandName);
   if (NS_WARN_IF(!tagName)) {
     return NS_ERROR_UNEXPECTED;
   }
-  return GetCurrentState(tagName, htmlEditor, aParams);
+  return GetCurrentState(tagName, htmlEditor, &aParams);
 }
 
 /*****************************************************************************
  * mozilla::PasteNoFormattingCommand
  *****************************************************************************/
 
 StaticRefPtr<PasteNoFormattingCommand> PasteNoFormattingCommand::sInstance;
 
@@ -141,27 +134,21 @@ nsresult PasteNoFormattingCommand::DoCom
 }
 
 nsresult PasteNoFormattingCommand::DoCommandParams(
     const char* aCommandName, nsCommandParams* aParams,
     TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-PasteNoFormattingCommand::GetCommandStateParams(const char* aCommandName,
-                                                nsICommandParams* aParams,
-                                                nsISupports* refCon) {
-  NS_ENSURE_ARG_POINTER(aParams);
-
-  bool enabled = false;
-  nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, enabled);
+nsresult PasteNoFormattingCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /*****************************************************************************
  * mozilla::StyleUpdatingCommand
  *****************************************************************************/
 
 StaticRefPtr<StyleUpdatingCommand> StyleUpdatingCommand::sInstance;
 
@@ -424,23 +411,21 @@ nsresult RemoveListCommand::DoCommand(co
 }
 
 nsresult RemoveListCommand::DoCommandParams(const char* aCommandName,
                                             nsCommandParams* aParams,
                                             TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-RemoveListCommand::GetCommandStateParams(const char* aCommandName,
-                                         nsICommandParams* aParams,
-                                         nsISupports* refCon) {
-  bool outCmdEnabled = false;
-  IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, outCmdEnabled);
+nsresult RemoveListCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /*****************************************************************************
  * mozilla::IndentCommand
  *****************************************************************************/
 
 StaticRefPtr<IndentCommand> IndentCommand::sInstance;
 
@@ -466,23 +451,21 @@ nsresult IndentCommand::DoCommand(const 
 }
 
 nsresult IndentCommand::DoCommandParams(const char* aCommandName,
                                         nsCommandParams* aParams,
                                         TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-IndentCommand::GetCommandStateParams(const char* aCommandName,
-                                     nsICommandParams* aParams,
-                                     nsISupports* refCon) {
-  bool outCmdEnabled = false;
-  IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, outCmdEnabled);
+nsresult IndentCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /*****************************************************************************
  * mozilla::OutdentCommand
  *****************************************************************************/
 
 StaticRefPtr<OutdentCommand> OutdentCommand::sInstance;
 
@@ -508,23 +491,21 @@ nsresult OutdentCommand::DoCommand(const
 }
 
 nsresult OutdentCommand::DoCommandParams(const char* aCommandName,
                                          nsCommandParams* aParams,
                                          TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-OutdentCommand::GetCommandStateParams(const char* aCommandName,
-                                      nsICommandParams* aParams,
-                                      nsISupports* refCon) {
-  bool outCmdEnabled = false;
-  IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, outCmdEnabled);
+nsresult OutdentCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /*****************************************************************************
  * mozilla::MultiStateCommandBase
  *****************************************************************************/
 
 bool MultiStateCommandBase::IsCommandEnabled(const char* aCommandName,
                                              TextEditor* aTextEditor) const {
@@ -558,29 +539,27 @@ nsresult MultiStateCommandBase::DoComman
       CopyASCIItoUTF16(asciiAttribute, attribute);
     } else {
       aParams->GetString(STATE_ATTRIBUTE, attribute);
     }
   }
   return SetState(MOZ_KnownLive(htmlEditor), attribute);
 }
 
-NS_IMETHODIMP
-MultiStateCommandBase::GetCommandStateParams(const char* aCommandName,
-                                             nsICommandParams* aParams,
-                                             nsISupports* refCon) {
-  nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
-  if (!editor) {
+nsresult MultiStateCommandBase::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  if (!aTextEditor) {
     return NS_OK;
   }
-  mozilla::HTMLEditor* htmlEditor = editor->AsHTMLEditor();
+  HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
   if (NS_WARN_IF(!htmlEditor)) {
     return NS_ERROR_FAILURE;
   }
-  return GetCurrentState(MOZ_KnownLive(htmlEditor), aParams);
+  return GetCurrentState(MOZ_KnownLive(htmlEditor), &aParams);
 }
 
 /*****************************************************************************
  * mozilla::ParagraphStateCommand
  *****************************************************************************/
 
 StaticRefPtr<ParagraphStateCommand> ParagraphStateCommand::sInstance;
 
@@ -1012,27 +991,21 @@ nsresult DecreaseZIndexCommand::DoComman
 }
 
 nsresult DecreaseZIndexCommand::DoCommandParams(const char* aCommandName,
                                                 nsCommandParams* aParams,
                                                 TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-DecreaseZIndexCommand::GetCommandStateParams(const char* aCommandName,
-                                             nsICommandParams* aParams,
-                                             nsISupports* refCon) {
-  NS_ENSURE_ARG_POINTER(aParams);
-
-  bool enabled = false;
-  nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, enabled);
+nsresult DecreaseZIndexCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /*****************************************************************************
  * mozilla::IncreaseZIndexCommand
  *****************************************************************************/
 
 StaticRefPtr<IncreaseZIndexCommand> IncreaseZIndexCommand::sInstance;
 
@@ -1061,27 +1034,21 @@ nsresult IncreaseZIndexCommand::DoComman
 }
 
 nsresult IncreaseZIndexCommand::DoCommandParams(const char* aCommandName,
                                                 nsCommandParams* aParams,
                                                 TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-IncreaseZIndexCommand::GetCommandStateParams(const char* aCommandName,
-                                             nsICommandParams* aParams,
-                                             nsISupports* refCon) {
-  NS_ENSURE_ARG_POINTER(aParams);
-
-  bool enabled = false;
-  nsresult rv = IsCommandEnabled(aCommandName, refCon, &enabled);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, enabled);
+nsresult IncreaseZIndexCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /*****************************************************************************
  * mozilla::RemoveStylesCommand
  *****************************************************************************/
 
 StaticRefPtr<RemoveStylesCommand> RemoveStylesCommand::sInstance;
 
@@ -1104,23 +1071,21 @@ nsresult RemoveStylesCommand::DoCommand(
 }
 
 nsresult RemoveStylesCommand::DoCommandParams(const char* aCommandName,
                                               nsCommandParams* aParams,
                                               TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-RemoveStylesCommand::GetCommandStateParams(const char* aCommandName,
-                                           nsICommandParams* aParams,
-                                           nsISupports* refCon) {
-  bool outCmdEnabled = false;
-  IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, outCmdEnabled);
+nsresult RemoveStylesCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /*****************************************************************************
  * mozilla::IncreaseFontSizeCommand
  *****************************************************************************/
 
 StaticRefPtr<IncreaseFontSizeCommand> IncreaseFontSizeCommand::sInstance;
 
@@ -1143,23 +1108,21 @@ nsresult IncreaseFontSizeCommand::DoComm
 }
 
 nsresult IncreaseFontSizeCommand::DoCommandParams(
     const char* aCommandName, nsCommandParams* aParams,
     TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-IncreaseFontSizeCommand::GetCommandStateParams(const char* aCommandName,
-                                               nsICommandParams* aParams,
-                                               nsISupports* refCon) {
-  bool outCmdEnabled = false;
-  IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, outCmdEnabled);
+nsresult IncreaseFontSizeCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /*****************************************************************************
  * mozilla::DecreaseFontSizeCommand
  *****************************************************************************/
 
 StaticRefPtr<DecreaseFontSizeCommand> DecreaseFontSizeCommand::sInstance;
 
@@ -1182,23 +1145,21 @@ nsresult DecreaseFontSizeCommand::DoComm
 }
 
 nsresult DecreaseFontSizeCommand::DoCommandParams(
     const char* aCommandName, nsCommandParams* aParams,
     TextEditor& aTextEditor) const {
   return DoCommand(aCommandName, aTextEditor);
 }
 
-NS_IMETHODIMP
-DecreaseFontSizeCommand::GetCommandStateParams(const char* aCommandName,
-                                               nsICommandParams* aParams,
-                                               nsISupports* refCon) {
-  bool outCmdEnabled = false;
-  IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, outCmdEnabled);
+nsresult DecreaseFontSizeCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /*****************************************************************************
  * mozilla::InsertHTMLCommand
  *****************************************************************************/
 
 StaticRefPtr<InsertHTMLCommand> InsertHTMLCommand::sInstance;
 
@@ -1239,26 +1200,21 @@ nsresult InsertHTMLCommand::DoCommandPar
   nsAutoString html;
   nsresult rv = aParams->GetString(STATE_DATA, html);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
   return MOZ_KnownLive(htmlEditor)->InsertHTML(html);
 }
 
-NS_IMETHODIMP
-InsertHTMLCommand::GetCommandStateParams(const char* aCommandName,
-                                         nsICommandParams* aParams,
-                                         nsISupports* refCon) {
-  NS_ENSURE_ARG_POINTER(aParams);
-  NS_ENSURE_ARG_POINTER(refCon);
-
-  bool outCmdEnabled = false;
-  IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, outCmdEnabled);
+nsresult InsertHTMLCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /*****************************************************************************
  * mozilla::InsertTagCommand
  *****************************************************************************/
 
 StaticRefPtr<InsertTagCommand> InsertTagCommand::sInstance;
 nsRefPtrHashtable<nsCharPtrHashKey, nsAtom> InsertTagCommand::sTagNameTable;
@@ -1363,26 +1319,21 @@ nsresult InsertTagCommand::DoCommandPara
 
   rv = MOZ_KnownLive(htmlEditor)->InsertElementAtSelection(newElement, true);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
   return NS_OK;
 }
 
-NS_IMETHODIMP
-InsertTagCommand::GetCommandStateParams(const char* aCommandName,
-                                        nsICommandParams* aParams,
-                                        nsISupports* refCon) {
-  NS_ENSURE_ARG_POINTER(aParams);
-  NS_ENSURE_ARG_POINTER(refCon);
-
-  bool outCmdEnabled = false;
-  IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
-  return aParams->AsCommandParams()->SetBool(STATE_ENABLED, outCmdEnabled);
+nsresult InsertTagCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  return aParams.SetBool(STATE_ENABLED,
+                         IsCommandEnabled(aCommandName, aTextEditor));
 }
 
 /*****************************************************************************
  * Helper methods
  *****************************************************************************/
 
 static nsresult GetListState(HTMLEditor* aHTMLEditor, bool* aMixed,
                              nsAString& aLocalName)
--- a/editor/libeditor/HTMLEditorDocumentCommands.cpp
+++ b/editor/libeditor/HTMLEditorDocumentCommands.cpp
@@ -2,34 +2,25 @@
 /* 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/EditorCommands.h"
 
 #include "mozilla/HTMLEditor.h"          // for HTMLEditor
 #include "mozilla/TextEditor.h"          // for TextEditor
+#include "mozilla/dom/Document.h"        // for Document
 #include "nsCommandParams.h"             // for nsCommandParams
-#include "nsCOMPtr.h"                    // for nsCOMPtr, do_QueryInterface, etc
-#include "nsCRT.h"                       // for nsCRT
-#include "nsDebug.h"                     // for NS_ENSURE_ARG_POINTER, etc
-#include "nsError.h"                     // for NS_ERROR_INVALID_ARG, etc
 #include "nsIDocShell.h"                 // for nsIDocShell
-#include "mozilla/dom/Document.h"        // for Document
 #include "nsIEditingSession.h"           // for nsIEditingSession, etc
-#include "nsIEditor.h"                   // for nsIEditor
-#include "nsIPlaintextEditor.h"          // for nsIPlaintextEditor, etc
 #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
-
-class nsISupports;
 
 // defines
 #define STATE_ENABLED "state_enabled"
 #define STATE_ALL "state_all"
 #define STATE_ATTRIBUTE "state_attribute"
 #define STATE_DATA "state_data"
 
 namespace mozilla {
@@ -214,181 +205,168 @@ nsresult SetDocumentStateCommand::DoComm
     }
     htmlEditor->EnableAbsolutePositionEditor(enabled);
     return NS_OK;
   }
 
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
-NS_IMETHODIMP
-SetDocumentStateCommand::GetCommandStateParams(const char* aCommandName,
-                                               nsICommandParams* aParams,
-                                               nsISupports* refCon) {
-  if (NS_WARN_IF(!aParams) || NS_WARN_IF(!refCon)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-
+nsresult SetDocumentStateCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
   // If the result is set to STATE_ALL as bool value, queryCommandState()
   // returns the bool value.
   // If the result is set to STATE_ATTRIBUTE as CString value,
   // queryCommandValue() returns the string value.
   // Otherwise, ignored.
 
   // The base editor owns most state info
-  nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
-  if (NS_WARN_IF(!editor)) {
+  if (NS_WARN_IF(!aTextEditor)) {
     return NS_ERROR_INVALID_ARG;
   }
-  TextEditor* textEditor = editor->AsTextEditor();
-  MOZ_ASSERT(textEditor);
-
-  nsCommandParams* params = aParams->AsCommandParams();
-
   // Always get the enabled state
-  bool outCmdEnabled = false;
-  IsCommandEnabled(aCommandName, refCon, &outCmdEnabled);
-  nsresult rv = params->SetBool(STATE_ENABLED, outCmdEnabled);
+  nsresult rv = aParams.SetBool(STATE_ENABLED,
+                                IsCommandEnabled(aCommandName, aTextEditor));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   // cmd_setDocumentModified is an internal command.
-  if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentModified")) {
+  if (!strcmp(aCommandName, "cmd_setDocumentModified")) {
     bool modified;
-    rv = textEditor->GetDocumentModified(&modified);
+    rv = aTextEditor->GetDocumentModified(&modified);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     // XXX Nobody refers this result due to wrong type.
-    rv = params->SetBool(STATE_ATTRIBUTE, modified);
+    rv = aParams.SetBool(STATE_ATTRIBUTE, modified);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     return NS_OK;
   }
 
   // cmd_setDocumentReadOnly is a Gecko specific command, "contentReadOnly".
-  if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentReadOnly")) {
+  if (!strcmp(aCommandName, "cmd_setDocumentReadOnly")) {
     // XXX Nobody refers this result due to wrong type.
-    rv = params->SetBool(STATE_ATTRIBUTE, textEditor->IsReadonly());
+    rv = aParams.SetBool(STATE_ATTRIBUTE, aTextEditor->IsReadonly());
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     return NS_OK;
   }
 
   // cmd_setDocumentUseCSS is a common command, "styleWithCSS".
-  if (!nsCRT::strcmp(aCommandName, "cmd_setDocumentUseCSS")) {
-    HTMLEditor* htmlEditor = textEditor->AsHTMLEditor();
+  if (!strcmp(aCommandName, "cmd_setDocumentUseCSS")) {
+    HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
     if (NS_WARN_IF(!htmlEditor)) {
       return NS_ERROR_INVALID_ARG;
     }
-    rv = params->SetBool(STATE_ALL, htmlEditor->IsCSSEnabled());
+    rv = aParams.SetBool(STATE_ALL, htmlEditor->IsCSSEnabled());
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     return NS_OK;
   }
 
   // cmd_insertBrOnReturn is a Gecko specific command, "insertBrOrReturn".
-  if (!nsCRT::strcmp(aCommandName, "cmd_insertBrOnReturn")) {
-    HTMLEditor* htmlEditor = textEditor->AsHTMLEditor();
+  if (!strcmp(aCommandName, "cmd_insertBrOnReturn")) {
+    HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
     if (NS_WARN_IF(!htmlEditor)) {
       return NS_ERROR_INVALID_ARG;
     }
     bool createPOnReturn;
     htmlEditor->GetReturnInParagraphCreatesNewParagraph(&createPOnReturn);
     // XXX Nobody refers this result due to wrong type.
-    rv = params->SetBool(STATE_ATTRIBUTE, !createPOnReturn);
+    rv = aParams.SetBool(STATE_ATTRIBUTE, !createPOnReturn);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     return NS_OK;
   }
 
   // cmd_defaultParagraphSeparator is a common command,
   // "defaultParagraphSeparator".
-  if (!nsCRT::strcmp(aCommandName, "cmd_defaultParagraphSeparator")) {
-    HTMLEditor* htmlEditor = textEditor->AsHTMLEditor();
+  if (!strcmp(aCommandName, "cmd_defaultParagraphSeparator")) {
+    HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
     if (NS_WARN_IF(!htmlEditor)) {
       return NS_ERROR_INVALID_ARG;
     }
 
     switch (htmlEditor->GetDefaultParagraphSeparator()) {
       case ParagraphSeparator::div: {
         DebugOnly<nsresult> rv =
-            params->SetCString(STATE_ATTRIBUTE, NS_LITERAL_CSTRING("div"));
+            aParams.SetCString(STATE_ATTRIBUTE, NS_LITERAL_CSTRING("div"));
         NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                              "Failed to set command params to return \"div\"");
         return NS_OK;
       }
       case ParagraphSeparator::p: {
         DebugOnly<nsresult> rv =
-            params->SetCString(STATE_ATTRIBUTE, NS_LITERAL_CSTRING("p"));
+            aParams.SetCString(STATE_ATTRIBUTE, NS_LITERAL_CSTRING("p"));
         NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                              "Failed to set command params to return \"p\"");
         return NS_OK;
       }
       case ParagraphSeparator::br: {
         DebugOnly<nsresult> rv =
-            params->SetCString(STATE_ATTRIBUTE, NS_LITERAL_CSTRING("br"));
+            aParams.SetCString(STATE_ATTRIBUTE, NS_LITERAL_CSTRING("br"));
         NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                              "Failed to set command params to return \"br\"");
         return NS_OK;
       }
       default:
         MOZ_ASSERT_UNREACHABLE("Invalid paragraph separator value");
         return NS_ERROR_UNEXPECTED;
     }
   }
 
   // cmd_enableObjectResizing is a Gecko specific command,
   // "enableObjectResizing".
-  if (!nsCRT::strcmp(aCommandName, "cmd_enableObjectResizing")) {
-    HTMLEditor* htmlEditor = textEditor->AsHTMLEditor();
+  if (!strcmp(aCommandName, "cmd_enableObjectResizing")) {
+    HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
     if (NS_WARN_IF(!htmlEditor)) {
       return NS_ERROR_INVALID_ARG;
     }
     // We returned the result as STATE_ATTRIBUTE with bool value 60 or earlier.
     // So, the result was ignored by both nsHTMLDocument::QueryCommandValue()
     // and nsHTMLDocument::QueryCommandState().
-    rv = params->SetBool(STATE_ALL, htmlEditor->IsObjectResizerEnabled());
+    rv = aParams.SetBool(STATE_ALL, htmlEditor->IsObjectResizerEnabled());
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     return NS_OK;
   }
 
   // cmd_enableInlineTableEditing is a Gecko specific command,
   // "enableInlineTableEditing".
-  if (!nsCRT::strcmp(aCommandName, "cmd_enableInlineTableEditing")) {
-    HTMLEditor* htmlEditor = textEditor->AsHTMLEditor();
+  if (!strcmp(aCommandName, "cmd_enableInlineTableEditing")) {
+    HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
     if (NS_WARN_IF(!htmlEditor)) {
       return NS_ERROR_INVALID_ARG;
     }
     // We returned the result as STATE_ATTRIBUTE with bool value 60 or earlier.
     // So, the result was ignored by both nsHTMLDocument::QueryCommandValue()
     // and nsHTMLDocument::QueryCommandState().
-    rv = params->SetBool(STATE_ALL, htmlEditor->IsInlineTableEditorEnabled());
+    rv = aParams.SetBool(STATE_ALL, htmlEditor->IsInlineTableEditorEnabled());
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     return NS_OK;
   }
 
   // cmd_enableAbsolutePositionEditing is a Gecko specific command,
   // "cenableAbsolutePositionEditing".
-  if (!nsCRT::strcmp(aCommandName, "cmd_enableAbsolutePositionEditing")) {
-    NS_ENSURE_ARG_POINTER(aParams);
-    HTMLEditor* htmlEditor = textEditor->AsHTMLEditor();
+  if (!strcmp(aCommandName, "cmd_enableAbsolutePositionEditing")) {
+    HTMLEditor* htmlEditor = aTextEditor->AsHTMLEditor();
     if (NS_WARN_IF(!htmlEditor)) {
       return NS_ERROR_INVALID_ARG;
     }
-    return params->SetBool(STATE_ALL,
+    return aParams.SetBool(STATE_ALL,
                            htmlEditor->IsAbsolutePositionEditorEnabled());
   }
 
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
 /*****************************************************************************
  * mozilla::DocumentStateCommand
@@ -441,72 +419,55 @@ nsresult DocumentStateCommand::DoCommand
 }
 
 nsresult DocumentStateCommand::DoCommandParams(const char* aCommandName,
                                                nsCommandParams* aParams,
                                                TextEditor& aTextEditor) const {
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
-NS_IMETHODIMP
-DocumentStateCommand::GetCommandStateParams(const char* aCommandName,
-                                            nsICommandParams* aParams,
-                                            nsISupports* refCon) {
-  if (NS_WARN_IF(!aParams) || NS_WARN_IF(!aCommandName)) {
-    return NS_ERROR_INVALID_ARG;
-  }
-
-  nsCommandParams* params = aParams->AsCommandParams();
-
-  if (!nsCRT::strcmp(aCommandName, "obs_documentCreated")) {
+nsresult DocumentStateCommand::GetCommandStateParams(
+    const char* aCommandName, nsCommandParams& aParams, TextEditor* aTextEditor,
+    nsIEditingSession* aEditingSession) const {
+  if (!strcmp(aCommandName, "obs_documentCreated")) {
     uint32_t editorStatus = nsIEditingSession::eEditorErrorUnknown;
-
-    nsCOMPtr<nsIEditingSession> editingSession = do_QueryInterface(refCon);
-    if (editingSession) {
-      // refCon is initially set to nsIEditingSession until editor
-      //  is successfully created and source doc is loaded
-      // Embedder gets error status if this fails
-      // If called before startup is finished,
-      //    status = eEditorCreationInProgress
-      nsresult rv = editingSession->GetEditorStatus(&editorStatus);
+    if (aEditingSession) {
+      // Current context is initially set to nsIEditingSession until editor is
+      // successfully created and source doc is loaded.  Embedder gets error
+      // status if this fails.  If called before startup is finished,
+      // status will be eEditorCreationInProgress.
+      nsresult rv = aEditingSession->GetEditorStatus(&editorStatus);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
-    } else {
-      // If refCon is an editor, then everything started up OK!
-      nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
-      if (editor) {
-        editorStatus = nsIEditingSession::eEditorOK;
-      }
+    } else if (aTextEditor) {
+      // If current context is an editor, then everything started up OK!
+      editorStatus = nsIEditingSession::eEditorOK;
     }
 
     // Note that if refCon is not-null, but is neither
     // an nsIEditingSession or nsIEditor, we return "eEditorErrorUnknown"
-    DebugOnly<nsresult> rv = params->SetInt(STATE_DATA, editorStatus);
+    DebugOnly<nsresult> rv = aParams.SetInt(STATE_DATA, editorStatus);
     NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to set editor status");
     return NS_OK;
   }
 
-  if (!nsCRT::strcmp(aCommandName, "obs_documentLocationChanged")) {
-    nsCOMPtr<nsIEditor> editor = do_QueryInterface(refCon);
-    if (!editor) {
+  if (!strcmp(aCommandName, "obs_documentLocationChanged")) {
+    if (!aTextEditor) {
       return NS_OK;
     }
-    TextEditor* textEditor = editor->AsTextEditor();
-    MOZ_ASSERT(textEditor);
-
-    RefPtr<Document> doc = textEditor->GetDocument();
-    if (NS_WARN_IF(!doc)) {
+    Document* document = aTextEditor->GetDocument();
+    if (NS_WARN_IF(!document)) {
       return NS_ERROR_FAILURE;
     }
-    nsIURI* uri = doc->GetDocumentURI();
+    nsIURI* uri = document->GetDocumentURI();
     if (NS_WARN_IF(!uri)) {
       return NS_ERROR_FAILURE;
     }
-    nsresult rv = params->SetISupports(STATE_DATA, uri);
+    nsresult rv = aParams.SetISupports(STATE_DATA, uri);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     return NS_OK;
   }
 
   return NS_ERROR_NOT_IMPLEMENTED;
 }