Bug 1549661 - part 6: Create `EditorCommand::DoCommandParam(Command aCommand, nsITransferable* aTransferableParam, TextEditor& aTextEditor)` r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 21 May 2019 07:47:49 +0000
changeset 474697 bd77dd67175d25744b092e18f4a074bb15971fe4
parent 474696 ed3667be6075fb724afc34a3294285ec8d484e52
child 474698 476139ca9d4a5802cfd786fccb1fd87d088e78fa
push id113168
push userrmaries@mozilla.com
push dateTue, 21 May 2019 16:39:23 +0000
treeherdermozilla-inbound@3c0f78074b72 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1549661
milestone69.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 1549661 - part 6: Create `EditorCommand::DoCommandParam(Command aCommand, nsITransferable* aTransferableParam, TextEditor& aTextEditor)` r=m_kato If `nsIControllerCommand::DoCommandParams()` is called without aParams or `nsITransferable` pointer, this patch sets nullptr to `aTransferableParam` for `DoCommandParam()`. This allows each implementation to choose ignore or return error. Differential Revision: https://phabricator.services.mozilla.com/D30500
editor/libeditor/EditorCommands.cpp
editor/libeditor/EditorCommands.h
widget/CommandList.h
--- a/editor/libeditor/EditorCommands.cpp
+++ b/editor/libeditor/EditorCommands.cpp
@@ -214,22 +214,32 @@ EditorCommand::DoCommandParams(const cha
     nsresult rv = DoCommandParam(command, stringParam,
                                  MOZ_KnownLive(*editor->AsTextEditor()));
     NS_WARNING_ASSERTION(
         NS_SUCCEEDED(rv),
         "Failed to do command from nsIControllerCommand::DoCommandParams()");
     return rv;
   }
 
-  nsresult rv = DoCommandParams(command, MOZ_KnownLive(params),
-                                MOZ_KnownLive(*editor->AsTextEditor()));
-  NS_WARNING_ASSERTION(
-      NS_SUCCEEDED(rv),
-      "Failed to do command from nsIControllerCommand::DoCommandParams()");
-  return rv;
+  if (Any(paramType & EditorCommandParamType::Transferable)) {
+    nsCOMPtr<nsITransferable> transferable;
+    if (params) {
+      nsCOMPtr<nsISupports> supports = params->GetISupports("transferable");
+      transferable = do_QueryInterface(supports);
+    }
+    nsresult rv = DoCommandParam(command, transferable,
+                                 MOZ_KnownLive(*editor->AsTextEditor()));
+    NS_WARNING_ASSERTION(
+        NS_SUCCEEDED(rv),
+        "Failed to do command from nsIControllerCommand::DoCommandParams()");
+    return rv;
+  }
+
+  MOZ_ASSERT_UNREACHABLE("Unexpected param type");
+  return NS_ERROR_NOT_IMPLEMENTED;
 }
 
 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;
@@ -469,39 +479,25 @@ bool PasteTransferableCommand::IsCommand
          aTextEditor->CanPasteTransferable(nullptr);
 }
 
 nsresult PasteTransferableCommand::DoCommand(Command aCommand,
                                              TextEditor& aTextEditor) const {
   return NS_ERROR_FAILURE;
 }
 
-nsresult PasteTransferableCommand::DoCommandParams(
-    Command aCommand, nsCommandParams* aParams, TextEditor& aTextEditor) const {
-  if (NS_WARN_IF(!aParams)) {
+nsresult PasteTransferableCommand::DoCommandParam(
+    Command aCommand, nsITransferable* aTransferableParam,
+    TextEditor& aTextEditor) const {
+  if (NS_WARN_IF(!aTransferableParam)) {
     return NS_ERROR_INVALID_ARG;
   }
-
-  nsCOMPtr<nsISupports> supports = aParams->GetISupports("transferable");
-  if (NS_WARN_IF(!supports)) {
-    return NS_ERROR_FAILURE;
-  }
-
-  nsCOMPtr<nsITransferable> trans = do_QueryInterface(supports);
-  if (NS_WARN_IF(!trans)) {
-    return NS_ERROR_FAILURE;
-  }
-
-  // We know textEditor is known-live here because we are holding a ref to it
-  // via "editor".
-  nsresult rv = aTextEditor.PasteTransferable(trans);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
-  return NS_OK;
+  nsresult rv = aTextEditor.PasteTransferable(aTransferableParam);
+  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "PasteTransferable() failed");
+  return rv;
 }
 
 nsresult PasteTransferableCommand::GetCommandStateParams(
     Command aCommand, nsCommandParams& aParams, TextEditor* aTextEditor,
     nsIEditingSession* aEditingSession) const {
   if (NS_WARN_IF(!aTextEditor)) {
     return NS_ERROR_INVALID_ARG;
   }
--- a/editor/libeditor/EditorCommands.h
+++ b/editor/libeditor/EditorCommands.h
@@ -13,16 +13,17 @@
 #include "nsISupportsImpl.h"
 #include "nsRefPtrHashtable.h"
 #include "nsStringFwd.h"
 
 class nsAtom;
 class nsCommandParams;
 class nsICommandParams;
 class nsIEditingSession;
+class nsITransferable;
 
 namespace mozilla {
 
 class HTMLEditor;
 class TextEditor;
 
 /**
  * EditorCommandParamType tells you that EditorCommand subclasses refer
@@ -89,16 +90,18 @@ class EditorCommand : public nsIControll
       case Command::CopyOrDelete:
         return EditorCommandParamType::None;
       // SelectAllCommand
       case Command::SelectAll:
         return EditorCommandParamType::None;
       // PasteCommand
       case Command::Paste:
         return EditorCommandParamType::None;
+      case Command::PasteTransferable:
+        return EditorCommandParamType::Transferable;
       // SwitchTextDirectionCommand
       case Command::FormatSetBlockTextDirection:
         return EditorCommandParamType::None;
       // DeleteCommand
       case Command::Delete:
       case Command::DeleteCharBackward:
       case Command::DeleteCharForward:
       case Command::DeleteWordBackward:
@@ -293,18 +296,18 @@ class EditorCommand : public nsIControll
       // DecreaseZIndexCommand
       case Command::FormatDecreaseZIndex:
         return EditorCommandParamType::None;
       // IncreaseZIndexCommand
       case Command::FormatIncreaseZIndex:
         return EditorCommandParamType::None;
 
       default:
-        // XXX Treat as Unknown for now
-        return EditorCommandParamType::Transferable;
+        MOZ_ASSERT_UNREACHABLE("Unknown Command");
+        return EditorCommandParamType::None;
     }
   }
 
   // nsIControllerCommand methods.  Use EditorCommand specific methods instead
   // for internal use.
   MOZ_CAN_RUN_SCRIPT_BOUNDARY
   NS_IMETHOD IsCommandEnabled(const char* aCommandName,
                               nsISupports* aCommandRefCon,
@@ -322,22 +325,16 @@ class EditorCommand : public nsIControll
                                    nsISupports* aCommandRefCon) final;
 
   MOZ_CAN_RUN_SCRIPT
   virtual bool IsCommandEnabled(Command aCommand,
                                 TextEditor* aTextEditor) const = 0;
   MOZ_CAN_RUN_SCRIPT
   virtual nsresult DoCommand(Command aCommand,
                              TextEditor& aTextEditor) const = 0;
-  MOZ_CAN_RUN_SCRIPT
-  virtual nsresult DoCommandParams(Command aCommand, nsCommandParams* aParams,
-                                   TextEditor& aTextEditor) const {
-    MOZ_ASSERT_UNREACHABLE("Wrong method is called");
-    return NS_ERROR_NOT_IMPLEMENTED;
-  }
 
   /**
    * @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.
@@ -392,16 +389,29 @@ class EditorCommand : public nsIControll
   MOZ_CAN_RUN_SCRIPT
   virtual nsresult DoCommandParam(Command aCommand,
                                   const nsAString& aStringParam,
                                   TextEditor& aTextEditor) const {
     MOZ_ASSERT_UNREACHABLE("Wrong overload is called");
     return NS_ERROR_NOT_IMPLEMENTED;
   }
 
+  /**
+   * Called only when the result of EditorCommand::GetParamType(aCommand) is
+   * EditorCommandParamType::Transferable.  If aTransferableParam may be
+   * nullptr.
+   */
+  MOZ_CAN_RUN_SCRIPT
+  virtual nsresult DoCommandParam(Command aCommand,
+                                  nsITransferable* aTransferableParam,
+                                  TextEditor& aTextEditor) const {
+    MOZ_ASSERT_UNREACHABLE("Wrong overload is called");
+    return NS_ERROR_NOT_IMPLEMENTED;
+  }
+
  protected:
   EditorCommand() = default;
   virtual ~EditorCommand() = default;
 };
 
 #define NS_DECL_EDITOR_COMMAND_COMMON_METHODS                              \
  public:                                                                   \
   MOZ_CAN_RUN_SCRIPT                                                       \
@@ -449,41 +459,37 @@ class EditorCommand : public nsIControll
 
 #define NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM                \
  public:                                                         \
   MOZ_CAN_RUN_SCRIPT                                             \
   virtual nsresult DoCommandParam(Command aCommand,              \
                                   const nsAString& aStringParam, \
                                   TextEditor& aTextEditor) const final;
 
+#define NS_DECL_DO_COMMAND_PARAM_FOR_TRANSFERABLE_PARAM                \
+ public:                                                               \
+  MOZ_CAN_RUN_SCRIPT                                                   \
+  virtual nsresult DoCommandParam(Command aCommand,                    \
+                                  nsITransferable* aTransferableParam, \
+                                  TextEditor& aTextEditor) const final;
+
 #define NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \
  public:                                                   \
   static _cmd* GetInstance() {                             \
     if (!sInstance) {                                      \
       sInstance = new _cmd();                              \
     }                                                      \
     return sInstance;                                      \
   }                                                        \
                                                            \
   static void Shutdown() { sInstance = nullptr; }          \
                                                            \
  private:                                                  \
   static StaticRefPtr<_cmd> sInstance;
 
-#define NS_DECL_EDITOR_COMMAND(_cmd)                   \
-  class _cmd final : public EditorCommand {            \
-    NS_DECL_EDITOR_COMMAND_COMMON_METHODS              \
-    NS_DECL_DO_COMMAND_PARAMS                          \
-    NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \
-                                                       \
-   protected:                                          \
-    _cmd() = default;                                  \
-    virtual ~_cmd() = default;                         \
-  };
-
 #define NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(_cmd) \
   class _cmd final : public EditorCommand {                     \
     NS_DECL_EDITOR_COMMAND_COMMON_METHODS                       \
     NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND             \
     NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd)          \
                                                                 \
    protected:                                                   \
     _cmd() = default;                                           \
@@ -518,26 +524,37 @@ class EditorCommand : public nsIControll
     NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM          \
     NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd) \
                                                        \
    protected:                                          \
     _cmd() = default;                                  \
     virtual ~_cmd() = default;                         \
   };
 
+#define NS_DECL_EDITOR_COMMAND_FOR_TRANSFERABLE_PARAM(_cmd) \
+  class _cmd final : public EditorCommand {                 \
+    NS_DECL_EDITOR_COMMAND_COMMON_METHODS                   \
+    NS_DECL_DO_COMMAND_PARAM_FOR_TRANSFERABLE_PARAM         \
+    NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON(_cmd)      \
+                                                            \
+   protected:                                               \
+    _cmd() = default;                                       \
+    virtual ~_cmd() = default;                              \
+  };
+
 // basic editor commands
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(UndoCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(RedoCommand)
 
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CutCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CutOrDeleteCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CopyCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(CopyOrDeleteCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(PasteCommand)
-NS_DECL_EDITOR_COMMAND(PasteTransferableCommand)
+NS_DECL_EDITOR_COMMAND_FOR_TRANSFERABLE_PARAM(PasteTransferableCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(SwitchTextDirectionCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DeleteCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(SelectAllCommand)
 
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(SelectionMoveCommands)
 
 // Insert content commands
 NS_DECL_EDITOR_COMMAND_FOR_STRING_PARAM(InsertPlaintextCommand)
@@ -888,24 +905,25 @@ NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(RemoveListCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(RemoveStylesCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(IncreaseFontSizeCommand)
 NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE(DecreaseFontSizeCommand)
 
 // Insert content commands
 NS_DECL_EDITOR_COMMAND_FOR_STRING_PARAM(InsertHTMLCommand)
 
-#undef NS_DECL_EDITOR_COMMAND
 #undef NS_DECL_EDITOR_COMMAND_FOR_NO_PARAM_WITH_DELEGATE
 #undef NS_DECL_EDITOR_COMMAND_FOR_BOOL_PARAM
 #undef NS_DECL_EDITOR_COMMAND_FOR_CSTRING_PARAM
 #undef NS_DECL_EDITOR_COMMAND_FOR_STRING_PARAM
+#undef NS_DECL_EDITOR_COMMAND_FOR_TRANSFERABLE_PARAM
 #undef NS_DECL_EDITOR_COMMAND_COMMON_METHODS
 #undef NS_DECL_DO_COMMAND_PARAMS
 #undef NS_DECL_DO_COMMAND_PARAM_DELEGATE_TO_DO_COMMAND
 #undef NS_DECL_DO_COMMAND_PARAM_FOR_BOOL_PARAM
 #undef NS_DECL_DO_COMMAND_PARAM_FOR_CSTRING_PARAM
 #undef NS_DECL_DO_COMMAND_PARAM_FOR_STRING_PARAM
+#undef NS_DECL_DO_COMMAND_PARAM_FOR_TRANSFERABLE_PARAM
 #undef NS_INLINE_DECL_EDITOR_COMMAND_MAKE_SINGLETON
 
 }  // namespace mozilla
 
 #endif  // #ifndef mozilla_EditorCommands_h
--- a/widget/CommandList.h
+++ b/widget/CommandList.h
@@ -147,16 +147,17 @@ NS_DEFINE_COMMAND(MoveDown, cmd_moveDown
 NS_DEFINE_COMMAND(MoveDown2, cmd_moveDown2)
 NS_DEFINE_COMMAND(MoveLeft, cmd_moveLeft)
 NS_DEFINE_COMMAND(MoveLeft2, cmd_moveLeft2)
 NS_DEFINE_COMMAND(MoveRight, cmd_moveRight)
 NS_DEFINE_COMMAND(MoveRight2, cmd_moveRight2)
 NS_DEFINE_COMMAND(MoveUp, cmd_moveUp)
 NS_DEFINE_COMMAND(MoveUp2, cmd_moveUp2)
 NS_DEFINE_COMMAND(PasteAsQuotation, cmd_pasteQuote)
+NS_DEFINE_COMMAND(PasteTransferable, cmd_pasteTransferable)
 NS_DEFINE_COMMAND(PasteWithoutFormat, cmd_pasteNoFormatting)
 NS_DEFINE_COMMAND(SelectDown, cmd_selectDown)
 NS_DEFINE_COMMAND(SelectDown2, cmd_selectDown2)
 NS_DEFINE_COMMAND(SelectLeft, cmd_selectLeft)
 NS_DEFINE_COMMAND(SelectLeft2, cmd_selectLeft2)
 NS_DEFINE_COMMAND(SelectRight, cmd_selectRight)
 NS_DEFINE_COMMAND(SelectRight2, cmd_selectRight2)
 NS_DEFINE_COMMAND(SelectUp, cmd_selectUp)