Skip to content

Commit

Permalink
Bug 1549661 - part 6: Create `EditorCommand::DoCommandParam(Command a…
Browse files Browse the repository at this point in the history
…Command, 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
  • Loading branch information
masayuki-nakano committed May 21, 2019
1 parent f1a9ccd commit 3df9a71
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 48 deletions.
50 changes: 23 additions & 27 deletions editor/libeditor/EditorCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,22 @@ EditorCommand::DoCommandParams(const char* aCommandName,
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
Expand Down Expand Up @@ -474,29 +484,15 @@ nsresult PasteTransferableCommand::DoCommand(Command aCommand,
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(
Expand Down
60 changes: 39 additions & 21 deletions editor/libeditor/EditorCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class nsAtom;
class nsCommandParams;
class nsICommandParams;
class nsIEditingSession;
class nsITransferable;

namespace mozilla {

Expand Down Expand Up @@ -94,6 +95,8 @@ class EditorCommand : public nsIControllerCommand {
// PasteCommand
case Command::Paste:
return EditorCommandParamType::None;
case Command::PasteTransferable:
return EditorCommandParamType::Transferable;
// SwitchTextDirectionCommand
case Command::FormatSetBlockTextDirection:
return EditorCommandParamType::None;
Expand Down Expand Up @@ -298,8 +301,8 @@ class EditorCommand : public nsIControllerCommand {
return EditorCommandParamType::None;

default:
// XXX Treat as Unknown for now
return EditorCommandParamType::Transferable;
MOZ_ASSERT_UNREACHABLE("Unknown Command");
return EditorCommandParamType::None;
}
}

Expand Down Expand Up @@ -327,12 +330,6 @@ class EditorCommand : public nsIControllerCommand {
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
Expand Down Expand Up @@ -397,6 +394,19 @@ class EditorCommand : public nsIControllerCommand {
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;
Expand Down Expand Up @@ -454,6 +464,13 @@ class EditorCommand : public nsIControllerCommand {
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() { \
Expand All @@ -468,17 +485,6 @@ class EditorCommand : public nsIControllerCommand {
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 \
Expand Down Expand Up @@ -523,6 +529,17 @@ class EditorCommand : public nsIControllerCommand {
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)
Expand All @@ -532,7 +549,7 @@ 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)
Expand Down Expand Up @@ -893,17 +910,18 @@ 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
Expand Down
1 change: 1 addition & 0 deletions widget/CommandList.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ 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)
Expand Down

0 comments on commit 3df9a71

Please sign in to comment.