Bug 1482012 - part 2: Create TextEditor::PasteAsAction() as non-virtual method for outer C++ code r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 13 Aug 2018 04:37:56 +0000
changeset 488911 a415ada9fdae85a9162a898928a8d7e854bbd338
parent 488910 b11bd5b080a2cd7feac3c7be175bd4e4b7b0dd2a
child 488912 50e4f06ac01a1f6dc187ef6ea23226e551f6f2a0
push id1815
push userffxbld-merge
push dateMon, 15 Oct 2018 10:40:45 +0000
treeherdermozilla-release@18d4c09e9378 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1482012
milestone63.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 1482012 - part 2: Create TextEditor::PasteAsAction() as non-virtual method for outer C++ code r=m_kato User may paste a lot with pressing Accel+V for a while (i.e., with auto repeat). So, calling nsIEditor::Paste() may be in a hot path and we can now make non-virtual public method with AsHTMLEditor(). Differential Revision: https://phabricator.services.mozilla.com/D2993
accessible/generic/HyperTextAccessible-inl.h
editor/libeditor/EditorBase.cpp
editor/libeditor/EditorCommands.cpp
editor/libeditor/EditorEventListener.cpp
editor/libeditor/HTMLEditor.h
editor/libeditor/HTMLEditorDataTransfer.cpp
editor/libeditor/TextEditor.h
editor/libeditor/TextEditorDataTransfer.cpp
--- a/accessible/generic/HyperTextAccessible-inl.h
+++ b/accessible/generic/HyperTextAccessible-inl.h
@@ -119,17 +119,17 @@ HyperTextAccessible::DeleteText(int32_t 
 }
 
 inline void
 HyperTextAccessible::PasteText(int32_t aPosition)
 {
   RefPtr<TextEditor> textEditor = GetEditor();
   if (textEditor) {
     SetSelectionRange(aPosition, aPosition);
-    textEditor->Paste(nsIClipboard::kGlobalClipboard);
+    textEditor->PasteAsAction(nsIClipboard::kGlobalClipboard);
   }
 }
 
 inline index_t
 HyperTextAccessible::ConvertMagicOffset(int32_t aOffset) const
 {
   if (aOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT)
     return CharacterCount();
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -1193,19 +1193,23 @@ EditorBase::CanCopy(bool* aCanCut)
 
 NS_IMETHODIMP
 EditorBase::CanDelete(bool* aCanDelete)
 {
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
 NS_IMETHODIMP
-EditorBase::Paste(int32_t aSelectionType)
+EditorBase::Paste(int32_t aClipboardType)
 {
-  return NS_ERROR_NOT_IMPLEMENTED;
+  nsresult rv = AsTextEditor()->PasteAsAction(aClipboardType);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 EditorBase::PasteTransferable(nsITransferable* aTransferable)
 {
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
--- a/editor/libeditor/EditorCommands.cpp
+++ b/editor/libeditor/EditorCommands.cpp
@@ -554,17 +554,17 @@ PasteCommand::DoCommand(const char* aCom
                         nsISupports* aCommandRefCon)
 {
   nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
   if (NS_WARN_IF(!editor)) {
     return NS_ERROR_FAILURE;
   }
   TextEditor* textEditor = editor->AsTextEditor();
   MOZ_ASSERT(textEditor);
-  return textEditor->Paste(nsIClipboard::kGlobalClipboard);
+  return textEditor->PasteAsAction(nsIClipboard::kGlobalClipboard);
 }
 
 NS_IMETHODIMP
 PasteCommand::DoCommandParams(const char* aCommandName,
                               nsICommandParams* aParams,
                               nsISupports* aCommandRefCon)
 {
   return DoCommand(aCommandName, aCommandRefCon);
--- a/editor/libeditor/EditorEventListener.cpp
+++ b/editor/libeditor/EditorEventListener.cpp
@@ -714,17 +714,17 @@ EditorEventListener::HandleMiddleClickPa
     }
   }
 
   // If the ctrl key is pressed, we'll do paste as quotation.
   // Would've used the alt key, but the kde wmgr treats alt-middle specially.
   if (clickEvent->IsControl()) {
     textEditor->PasteAsQuotationAsAction(clipboard);
   } else {
-    textEditor->Paste(clipboard);
+    textEditor->PasteAsAction(clipboard);
   }
 
   // Prevent the event from propagating up to be possibly handled
   // again by the containing window:
   clickEvent->StopPropagation();
   clickEvent->PreventDefault();
 
   // We processed the event, whether drop/paste succeeded or not
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -131,17 +131,16 @@ public:
 
   // TextEditor overrides
   virtual nsresult Init(nsIDocument& aDoc, Element* aRoot,
                         nsISelectionController* aSelCon, uint32_t aFlags,
                         const nsAString& aValue) override;
   NS_IMETHOD BeginningOfDocument() override;
   NS_IMETHOD SetFlags(uint32_t aFlags) override;
 
-  NS_IMETHOD Paste(int32_t aSelectionType) override;
   NS_IMETHOD CanPaste(int32_t aSelectionType, bool* aCanPaste) override;
 
   NS_IMETHOD PasteTransferable(nsITransferable* aTransferable) override;
 
   NS_IMETHOD DeleteNode(nsINode* aNode) override;
 
   NS_IMETHOD DebugUnitTests(int32_t* outNumTests,
                             int32_t* outNumTestsFailed) override;
@@ -1540,16 +1539,17 @@ protected:
   nsCOMPtr<nsILinkHandler> mLinkHandler;
 
   ParagraphSeparator mDefaultParagraphSeparator;
 
   friend class AutoSelectionSetterAfterTableEdit;
   friend class CSSEditUtils;
   friend class EmptyEditableFunctor;
   friend class HTMLEditRules;
+  friend class TextEditor;
   friend class WSRunObject;
 };
 
 } // namespace mozilla
 
 mozilla::HTMLEditor*
 nsIEditor::AsHTMLEditor()
 {
--- a/editor/libeditor/HTMLEditorDataTransfer.cpp
+++ b/editor/libeditor/HTMLEditorDataTransfer.cpp
@@ -1341,27 +1341,16 @@ HTMLEditor::HavePrivateHTMLFlavor(nsICli
                                            nsIClipboard::kGlobalClipboard,
                                            &bHavePrivateHTMLFlavor))) {
     return bHavePrivateHTMLFlavor;
   }
 
   return false;
 }
 
-
-NS_IMETHODIMP
-HTMLEditor::Paste(int32_t aClipboardType)
-{
-  nsresult rv = PasteInternal(aClipboardType);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
-  return NS_OK;
-}
-
 nsresult
 HTMLEditor::PasteInternal(int32_t aClipboardType)
 {
   if (!FireClipboardEvent(ePaste, aClipboardType)) {
     return NS_OK;
   }
 
   // Get Clipboard Service
--- a/editor/libeditor/TextEditor.h
+++ b/editor/libeditor/TextEditor.h
@@ -67,17 +67,16 @@ public:
   NS_IMETHOD Undo(uint32_t aCount) final;
   NS_IMETHOD Redo(uint32_t aCount) final;
 
   NS_IMETHOD Cut() override;
   NS_IMETHOD CanCut(bool* aCanCut) override;
   NS_IMETHOD Copy() override;
   NS_IMETHOD CanCopy(bool* aCanCopy) override;
   NS_IMETHOD CanDelete(bool* aCanDelete) override;
-  NS_IMETHOD Paste(int32_t aSelectionType) override;
   NS_IMETHOD CanPaste(int32_t aSelectionType, bool* aCanPaste) override;
   NS_IMETHOD PasteTransferable(nsITransferable* aTransferable) override;
 
   NS_IMETHOD OutputToString(const nsAString& aFormatType,
                             uint32_t aFlags,
                             nsAString& aOutputString) override;
 
   /** Can we paste |aTransferable| or, if |aTransferable| is null, will a call
@@ -108,16 +107,26 @@ public:
   }
 
   virtual nsresult HandleKeyPressEvent(
                      WidgetKeyboardEvent* aKeyboardEvent) override;
 
   virtual dom::EventTarget* GetDOMEventTarget() override;
 
   /**
+   * PasteAsAction() pastes clipboard content to Selection.  This method
+   * may dispatch ePaste event first.  If its defaultPrevent() is called,
+   * this does nothing but returns NS_OK.
+   *
+   * @param aClipboardType      nsIClipboard::kGlobalClipboard or
+   *                            nsIClipboard::kSelectionClipboard.
+   */
+  nsresult PasteAsAction(int32_t aClipboardType);
+
+  /**
    * InsertTextAsAction() inserts aStringToInsert at selection.
    * Although this method is implementation of nsIPlaintextEditor.insertText(),
    * this treats the input is an edit action.  If you'd like to insert text
    * as part of edit action, you probably should use InsertTextAsSubAction().
    *
    * @param aStringToInsert     The string to insert.
    */
   nsresult InsertTextAsAction(const nsAString& aStringToInsert);
--- a/editor/libeditor/TextEditorDataTransfer.cpp
+++ b/editor/libeditor/TextEditorDataTransfer.cpp
@@ -289,42 +289,62 @@ TextEditor::OnDrop(DragEvent* aDropEvent
                            newSelectionOffset, deleteSelection);
   }
 
   ScrollSelectionIntoView(false);
 
   return NS_OK;
 }
 
-NS_IMETHODIMP
-TextEditor::Paste(int32_t aSelectionType)
+nsresult
+TextEditor::PasteAsAction(int32_t aClipboardType)
 {
-  if (!FireClipboardEvent(ePaste, aSelectionType)) {
+  if (AsHTMLEditor()) {
+    nsresult rv = AsHTMLEditor()->PasteInternal(aClipboardType);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+    return NS_OK;
+  }
+
+  if (!FireClipboardEvent(ePaste, aClipboardType)) {
     return NS_OK;
   }
 
   // Get Clipboard Service
   nsresult rv;
-  nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
-  if (NS_FAILED(rv)) {
+  nsCOMPtr<nsIClipboard> clipboard =
+    do_GetService("@mozilla.org/widget/clipboard;1", &rv);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   // Get the nsITransferable interface for getting the data from the clipboard
-  nsCOMPtr<nsITransferable> trans;
-  rv = PrepareTransferable(getter_AddRefs(trans));
-  if (NS_SUCCEEDED(rv) && trans) {
-    // Get the Data from the clipboard
-    if (NS_SUCCEEDED(clipboard->GetData(trans, aSelectionType)) &&
-        IsModifiable()) {
-      rv = InsertTextFromTransferable(trans);
-    }
+  nsCOMPtr<nsITransferable> transferable;
+  rv = PrepareTransferable(getter_AddRefs(transferable));
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+  if (NS_WARN_IF(!transferable)) {
+    return NS_OK; // XXX Why?
   }
-
-  return rv;
+  // Get the Data from the clipboard.
+  rv = clipboard->GetData(transferable, aClipboardType);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return NS_OK; // XXX Why?
+  }
+  // XXX Why don't we check this first?
+  if (!IsModifiable()) {
+    return NS_OK;
+  }
+  rv = InsertTextFromTransferable(transferable);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 TextEditor::PasteTransferable(nsITransferable* aTransferable)
 {
   // Use an invalid value for the clipboard type as data comes from aTransferable
   // and we don't currently implement a way to put that in the data transfer yet.
   if (!FireClipboardEvent(ePaste, -1)) {