Bug 1460509 - part 10: Make TextEditRules::WillInsertText() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 11 May 2018 18:29:54 +0900
changeset 798728 8c820293c9b1e29b8f170c001f985920de16a060
parent 798727 36e927743abd7e646870751ac4126c5edc1d7bc6
child 798729 e53a1cb59fd67c40304bd568da3d3a7e2569b87f
push id110840
push usermasayuki@d-toybox.com
push dateWed, 23 May 2018 13:41:58 +0000
reviewersm_kato
bugs1460509
milestone62.0a1
Bug 1460509 - part 10: Make TextEditRules::WillInsertText() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato MozReview-Commit-ID: RT0Bi9TBwt
editor/libeditor/TextEditRules.cpp
editor/libeditor/TextEditRules.h
--- a/editor/libeditor/TextEditRules.cpp
+++ b/editor/libeditor/TextEditRules.cpp
@@ -676,17 +676,19 @@ TextEditRules::WillInsertText(EditAction
   *aCancel = false;
   *aHandled = true;
 
   // handle docs with a max length
   // NOTE, this function copies inString into outString for us.
   bool truncated = false;
   nsresult rv =
     TruncateInsertionIfNeeded(inString, outString, aMaxLength, &truncated);
-  NS_ENSURE_SUCCESS(rv, rv);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
   // If we're exceeding the maxlength when composing IME, we need to clean up
   // the composing text, so we shouldn't return early.
   if (truncated && outString->IsEmpty() &&
       aAction != EditAction::insertIMEText) {
     *aCancel = true;
     return NS_OK;
   }
 
@@ -699,16 +701,19 @@ TextEditRules::WillInsertText(EditAction
                                               TextEditorRef().GetRoot(),
                                               start, end);
   }
 
   // if the selection isn't collapsed, delete it.
   if (!SelectionRef().IsCollapsed()) {
     rv = TextEditorRef().DeleteSelectionAsAction(nsIEditor::eNone,
                                                  nsIEditor::eStrip);
+    if (NS_WARN_IF(!CanHandleEditAction())) {
+      return NS_ERROR_EDITOR_DESTROYED;
+    }
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
 
   WillInsert(aCancel);
   // initialize out param
   // we want to ignore result of WillInsert()
@@ -795,29 +800,35 @@ TextEditRules::WillInsertText(EditAction
       TextEditorRef().GetIMESelectionStartOffsetIn(
                         betterInsertionPoint.GetContainer());
     if (IMESelectionOffset >= 0) {
       betterInsertionPoint.Set(betterInsertionPoint.GetContainer(),
                                IMESelectionOffset);
     }
     rv = TextEditorRef().InsertTextWithTransaction(*doc, *outString,
                                                    betterInsertionPoint);
+    if (NS_WARN_IF(!CanHandleEditAction())) {
+      return NS_ERROR_EDITOR_DESTROYED;
+    }
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   } else {
     // aAction == EditAction::insertText
 
     // don't change my selection in subtransactions
     AutoTransactionsConserveSelection dontChangeMySelection(&TextEditorRef());
 
     EditorRawDOMPoint pointAfterStringInserted;
     rv = TextEditorRef().InsertTextWithTransaction(*doc, *outString,
                                                    atStartOfSelection,
                                                    &pointAfterStringInserted);
+    if (NS_WARN_IF(!CanHandleEditAction())) {
+      return NS_ERROR_EDITOR_DESTROYED;
+    }
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
     if (pointAfterStringInserted.IsSet()) {
       // Make the caret attach to the inserted text, unless this text ends with a LF,
       // in which case make the caret attach to the next line.
       bool endsWithLF =
@@ -827,16 +838,19 @@ TextEditRules::WillInsertText(EditAction
       NS_WARNING_ASSERTION(!error.Failed(),
         "Failed to set or unset interline position");
 
       MOZ_ASSERT(!pointAfterStringInserted.GetChild(),
         "After inserting text into a text node, pointAfterStringInserted."
         "GetChild() should be nullptr");
       error = IgnoredErrorResult();
       SelectionRef().Collapse(pointAfterStringInserted, error);
+      if (NS_WARN_IF(!CanHandleEditAction())) {
+        return NS_ERROR_EDITOR_DESTROYED;
+      }
       NS_WARNING_ASSERTION(!error.Failed(),
         "Failed to collapse selection after inserting string");
     }
   }
   ASSERT_PASSWORD_LENGTHS_EQUAL()
   return NS_OK;
 }
 
--- a/editor/libeditor/TextEditRules.h
+++ b/editor/libeditor/TextEditRules.h
@@ -125,22 +125,35 @@ public:
     return !!mBogusNode;
   }
 
 protected:
 
   void InitFields();
 
   // TextEditRules implementation methods
-  nsresult WillInsertText(EditAction aAction,
-                          bool* aCancel,
-                          bool* aHandled,
-                          const nsAString* inString,
-                          nsAString* outString,
-                          int32_t aMaxLength);
+
+  /**
+   * Called before inserting text.
+   * This method may actually inserts text into the editor.  Therefore, this
+   * might cause destroying the editor.
+   *
+   * @param aAction             Must be EditAction::insertIMEText or
+   *                            EditAction::insertText.
+   * @param aCancel             Returns true if the operation is canceled.
+   * @param aHandled            Returns true if the edit action is handled.
+   * @param inString            String to be inserted.
+   * @param outString           String actually inserted.
+   * @param aMaxLength          The maximum string length which the editor
+   *                            allows to set.
+   */
+  MOZ_MUST_USE nsresult
+  WillInsertText(EditAction aAction, bool* aCancel, bool* aHandled,
+                 const nsAString* inString, nsAString* outString,
+                 int32_t aMaxLength);
 
   nsresult WillInsertBreak(bool* aCancel, bool* aHandled, int32_t aMaxLength);
 
   /**
    * Called before setting text to the text editor.
    * This method may actually set text to it.  Therefore, this might cause
    * destroying the text editor.
    *