Bug 1497746 - part 3: Move EditorBase::mSavedSel into AutoEditActionDataSetter r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 26 Nov 2018 06:31:13 +0000
changeset 507166 465ebb044ee1494d7c2dca9ff04839cc45772b35
parent 507165 097a6db494774ab5b7311e10e40bd2948009e46e
child 507167 ceb75e2e0421b36d6d9e680830902d4196e94717
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1497746
milestone65.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 1497746 - part 3: Move EditorBase::mSavedSel into AutoEditActionDataSetter r=m_kato EditorBase::mSavedSel is used only by EditorBase methods which are called only by AutoSelectionRestorer. Additionally, AutoSelectionRestorer requires AutoEditActionDataSetter instance. So, we don't need to keep create for editor instance anymore. And also we don't need to keep it in the cycle collection. Note that SelectionState class is also used by PlaceholderTransaction. Therefore, we cannot make it MOZ_STACK_CLASS. Differential Revision: https://phabricator.services.mozilla.com/D12401
editor/libeditor/EditorBase.cpp
editor/libeditor/EditorBase.h
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -206,17 +206,16 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Ed
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mTextServicesDocument)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mTextInputListener)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mTransactionManager)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mActionListeners)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mEditorObservers)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocStateListeners)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mEventTarget)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mPlaceholderTransaction)
- NS_IMPL_CYCLE_COLLECTION_UNLINK(mSavedSel);
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mRangeUpdater);
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(EditorBase)
  nsIDocument* currentDoc =
    tmp->mRootElement ? tmp->mRootElement->GetUncomposedDoc() : nullptr;
  if (currentDoc &&
      nsCCUncollectableMarker::InGeneration(cb, currentDoc->GetMarkedCCGeneration())) {
@@ -231,17 +230,16 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTextInputListener)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTransactionManager)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mActionListeners)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEditorObservers)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocStateListeners)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEventTarget)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEventListener)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPlaceholderTransaction)
- NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSavedSel);
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRangeUpdater);
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(EditorBase)
  NS_INTERFACE_MAP_ENTRY(nsISelectionListener)
  NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
  NS_INTERFACE_MAP_ENTRY(nsIEditor)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIEditor)
@@ -2292,46 +2290,48 @@ EditorBase::OutputToString(const nsAStri
 {
   // these should be implemented by derived classes.
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
 bool
 EditorBase::ArePreservingSelection()
 {
-  return !(mSavedSel.IsEmpty());
+  return IsEditActionDataAvailable() && !SavedSelectionRef().IsEmpty();
 }
 
 void
 EditorBase::PreserveSelectionAcrossActions()
 {
   MOZ_ASSERT(IsEditActionDataAvailable());
 
-  mSavedSel.SaveSelection(SelectionRefPtr());
-  mRangeUpdater.RegisterSelectionState(mSavedSel);
+  SavedSelectionRef().SaveSelection(SelectionRefPtr());
+  mRangeUpdater.RegisterSelectionState(SavedSelectionRef());
 }
 
 nsresult
 EditorBase::RestorePreservedSelection()
 {
   MOZ_ASSERT(IsEditActionDataAvailable());
 
-  if (mSavedSel.IsEmpty()) {
+  if (SavedSelectionRef().IsEmpty()) {
     return NS_ERROR_FAILURE;
   }
-  mSavedSel.RestoreSelection(SelectionRefPtr());
+  SavedSelectionRef().RestoreSelection(SelectionRefPtr());
   StopPreservingSelection();
   return NS_OK;
 }
 
 void
 EditorBase::StopPreservingSelection()
 {
-  mRangeUpdater.DropSelectionState(mSavedSel);
-  mSavedSel.MakeEmpty();
+  MOZ_ASSERT(IsEditActionDataAvailable());
+
+  mRangeUpdater.DropSelectionState(SavedSelectionRef());
+  SavedSelectionRef().MakeEmpty();
 }
 
 NS_IMETHODIMP
 EditorBase::ForceCompositionEnd()
 {
   return CommitComposition();
 }
 
@@ -5184,17 +5184,18 @@ EditorBase::HideCaret(bool aHide)
 
 EditorBase::AutoSelectionRestorer::AutoSelectionRestorer(
                                      EditorBase& aEditorBase
                                      MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
   : mEditorBase(nullptr)
 {
   MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   if (aEditorBase.ArePreservingSelection()) {
-    // We already have initialized mSavedSel, so this must be nested call.
+    // We already have initialized mParentData::mSavedSelection, so this must
+    // be nested call.
     return;
   }
   MOZ_ASSERT(aEditorBase.IsEditActionDataAvailable());
   mEditorBase = &aEditorBase;
   mEditorBase->PreserveSelectionAcrossActions();
 }
 
 EditorBase::AutoSelectionRestorer::~AutoSelectionRestorer()
--- a/editor/libeditor/EditorBase.h
+++ b/editor/libeditor/EditorBase.h
@@ -782,23 +782,36 @@ protected: // AutoEditActionDataSetter, 
       MOZ_ASSERT(CanHandle());
       return mTopLevelEditSubAction;
     }
     EDirection GetDirectionOfTopLevelEditSubAction() const
     {
       return mDirectionOfTopLevelEditSubAction;
     }
 
+    SelectionState& SavedSelectionRef()
+    {
+      return mParentData ? mParentData->SavedSelectionRef() : mSavedSelection;
+    }
+    const SelectionState& SavedSelectionRef() const
+    {
+      return mParentData ? mParentData->SavedSelectionRef() : mSavedSelection;
+    }
+
   private:
     EditorBase& mEditorBase;
     RefPtr<Selection> mSelection;
     // EditAction may be nested, for example, a command may be executed
     // from mutation event listener which is run while editor changes
     // the DOM tree.  In such case, we need to handle edit action separately.
     AutoEditActionDataSetter* mParentData;
+
+    // Cached selection for AutoSelectionRestorer.
+    SelectionState mSavedSelection;
+
     EditAction mEditAction;
     EditSubAction mTopLevelEditSubAction;
     EDirection mDirectionOfTopLevelEditSubAction;
 
     AutoEditActionDataSetter() = delete;
     AutoEditActionDataSetter(const AutoEditActionDataSetter& aOther) = delete;
   };
 
@@ -864,16 +877,31 @@ protected: // May be called by friends.
    */
   EDirection GetDirectionOfTopLevelEditSubAction() const
   {
     return mEditActionData ?
        mEditActionData->GetDirectionOfTopLevelEditSubAction() : eNone;
   }
 
   /**
+   * SavedSelection() returns reference to saved selection which are
+   * stored by AutoSelectionRestorer.
+   */
+  SelectionState& SavedSelectionRef()
+  {
+    MOZ_ASSERT(IsEditActionDataAvailable());
+    return mEditActionData->SavedSelectionRef();
+  }
+  const SelectionState& SavedSelectionRef() const
+  {
+    MOZ_ASSERT(IsEditActionDataAvailable());
+    return mEditActionData->SavedSelectionRef();
+  }
+
+  /**
    * InsertTextWithTransaction() inserts aStringToInsert to aPointToInsert or
    * better insertion point around it.  If aPointToInsert isn't in a text node,
    * this method looks for the nearest point in a text node with
    * FindBetterInsertionPoint().  If there is no text node, this creates
    * new text node and put aStringToInsert to it.
    *
    * @param aDocument       The document of this editor.
    * @param aStringToInsert The string to insert.
@@ -2300,18 +2328,16 @@ protected:
   typedef AutoTArray<OwningNonNull<nsIEditorObserver>, 3>
             AutoEditorObserverArray;
   AutoEditorObserverArray mEditorObservers;
   // Listen to overall doc state (dirty or not, just created, etc.).
   typedef AutoTArray<OwningNonNull<nsIDocumentStateListener>, 1>
             AutoDocumentStateListenerArray;
   AutoDocumentStateListenerArray mDocStateListeners;
 
-  // Cached selection for AutoSelectionRestorer.
-  SelectionState mSavedSel;
   // Utility class object for maintaining preserved ranges.
   RangeUpdater mRangeUpdater;
 
   // Number of modifications (for undo/redo stack).
   uint32_t mModCount;
   // Behavior flags. See nsIPlaintextEditor.idl for the flags we use.
   uint32_t mFlags;