Bug 1425412 - part 10: Create factory methods for AddStyleSheetTransaction and RemoveStyleSheetTransaction, and remove EditorBase::CreateTxnForAddStyleSheet() and EditorBase::CreateTxnForRemoveStyleSheet() r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Mon, 18 Dec 2017 17:29:32 +0900
changeset 449023 bfbe1d3bf3776836a8d08c2fb490f152777301a0
parent 449022 d4351f088a6e6ace7c52838b45a17694513b12de
child 449024 98e9140f291df5b7e2f69f53a7311627cc1e8929
push id8527
push userCallek@gmail.com
push dateThu, 11 Jan 2018 21:05:50 +0000
treeherdermozilla-beta@95342d212a7a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1425412
milestone59.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 1425412 - part 10: Create factory methods for AddStyleSheetTransaction and RemoveStyleSheetTransaction, and remove EditorBase::CreateTxnForAddStyleSheet() and EditorBase::CreateTxnForRemoveStyleSheet() r=m_kato This patch creates factory methods for AddStyleSheetTransaction and RemoveStyleSheetTransaction, and removes EditorBase::CreateTxnForAddStyleSheet() and EditorBase::CreateTxnForRemoveStyleSheet() instead. MozReview-Commit-ID: 6dnZctDtNik
editor/libeditor/EditorBase.cpp
editor/libeditor/EditorBase.h
editor/libeditor/HTMLEditor.cpp
editor/libeditor/StyleSheetTransactions.cpp
editor/libeditor/StyleSheetTransactions.h
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -4578,34 +4578,16 @@ EditorBase::DoAfterUndoTransaction()
 
 void
 EditorBase::DoAfterRedoTransaction()
 {
   // all redoable transactions are non-transient
   MOZ_ALWAYS_SUCCEEDS(IncrementModificationCount(1));
 }
 
-already_AddRefed<AddStyleSheetTransaction>
-EditorBase::CreateTxnForAddStyleSheet(StyleSheet* aSheet)
-{
-  RefPtr<AddStyleSheetTransaction> transaction =
-    new AddStyleSheetTransaction(*this, aSheet);
-
-  return transaction.forget();
-}
-
-already_AddRefed<RemoveStyleSheetTransaction>
-EditorBase::CreateTxnForRemoveStyleSheet(StyleSheet* aSheet)
-{
-  RefPtr<RemoveStyleSheetTransaction> transaction =
-    new RemoveStyleSheetTransaction(*this, aSheet);
-
-  return transaction.forget();
-}
-
 already_AddRefed<EditAggregateTransaction>
 EditorBase::CreateTxnForDeleteSelection(EDirection aAction,
                                         nsINode** aRemovingNode,
                                         int32_t* aOffset,
                                         int32_t* aLength)
 {
   RefPtr<Selection> selection = GetSelection();
   if (NS_WARN_IF(!selection)) {
--- a/editor/libeditor/EditorBase.h
+++ b/editor/libeditor/EditorBase.h
@@ -555,28 +555,16 @@ protected:
    */
   already_AddRefed<EditTransactionBase>
     CreateTxnForDeleteRange(nsRange* aRangeToDelete,
                             EDirection aAction,
                             nsINode** aRemovingNode,
                             int32_t* aOffset,
                             int32_t* aLength);
 
-  /**
-   * Create a transaction for adding a style sheet.
-   */
-  already_AddRefed<mozilla::AddStyleSheetTransaction>
-    CreateTxnForAddStyleSheet(StyleSheet* aSheet);
-
-  /**
-   * Create a transaction for removing a style sheet.
-   */
-  already_AddRefed<mozilla::RemoveStyleSheetTransaction>
-    CreateTxnForRemoveStyleSheet(StyleSheet* aSheet);
-
   nsresult DeleteText(nsGenericDOMDataNode& aElement,
                       uint32_t aOffset, uint32_t aLength);
 
   /**
    * This method first deletes the selection, if it's not collapsed.  Then if
    * the selection lies in a CharacterData node, it splits it.  If the
    * selection is at this point collapsed in a CharacterData node, it's
    * adjusted to be collapsed right before or after the node instead (which is
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -2797,21 +2797,17 @@ HTMLEditor::ReplaceStyleSheet(const nsAS
 
 NS_IMETHODIMP
 HTMLEditor::RemoveStyleSheet(const nsAString& aURL)
 {
   RefPtr<StyleSheet> sheet = GetStyleSheetForURL(aURL);
   NS_ENSURE_TRUE(sheet, NS_ERROR_UNEXPECTED);
 
   RefPtr<RemoveStyleSheetTransaction> transaction =
-    CreateTxnForRemoveStyleSheet(sheet);
-  if (!transaction) {
-    return NS_ERROR_NULL_POINTER;
-  }
-
+    RemoveStyleSheetTransaction::Create(*this, *sheet);
   nsresult rv = DoTransaction(transaction);
   if (NS_SUCCEEDED(rv)) {
     mLastStyleSheetURL.Truncate();        // forget it
   }
   // Remove it from our internal list
   return RemoveStyleSheetFromList(aURL);
 }
 
@@ -3377,21 +3373,17 @@ HTMLEditor::StyleSheetLoaded(StyleSheet*
                              nsresult aStatus)
 {
   AutoPlaceholderBatch batchIt(this);
 
   if (!mLastStyleSheetURL.IsEmpty())
     RemoveStyleSheet(mLastStyleSheetURL);
 
   RefPtr<AddStyleSheetTransaction> transaction =
-    CreateTxnForAddStyleSheet(aSheet);
-  if (!transaction) {
-    return NS_OK;
-  }
-
+    AddStyleSheetTransaction::Create(*this, *aSheet);
   nsresult rv = DoTransaction(transaction);
   if (NS_SUCCEEDED(rv)) {
     // Get the URI, then url spec from the sheet
     nsAutoCString spec;
     rv = aSheet->GetSheetURI()->GetSpec(spec);
 
     if (NS_SUCCEEDED(rv)) {
       // Save it so we can remove before applying the next one
--- a/editor/libeditor/StyleSheetTransactions.cpp
+++ b/editor/libeditor/StyleSheetTransactions.cpp
@@ -40,21 +40,20 @@ RemoveStyleSheet(EditorBase& aEditor, St
   }
 }
 
 /******************************************************************************
  * AddStyleSheetTransaction
  ******************************************************************************/
 
 AddStyleSheetTransaction::AddStyleSheetTransaction(EditorBase& aEditorBase,
-                                                   StyleSheet* aSheet)
+                                                   StyleSheet& aStyleSheet)
   : mEditorBase(&aEditorBase)
-  , mSheet(aSheet)
+  , mSheet(&aStyleSheet)
 {
-  MOZ_ASSERT(aSheet);
 }
 
 NS_IMPL_CYCLE_COLLECTION_INHERITED(AddStyleSheetTransaction,
                                    EditTransactionBase,
                                    mEditorBase,
                                    mSheet)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AddStyleSheetTransaction)
@@ -88,21 +87,20 @@ AddStyleSheetTransaction::GetTxnDescript
 }
 
 /******************************************************************************
  * RemoveStyleSheetTransaction
  ******************************************************************************/
 
 RemoveStyleSheetTransaction::RemoveStyleSheetTransaction(
                                EditorBase& aEditorBase,
-                               StyleSheet* aSheet)
+                               StyleSheet& aStyleSheet)
   : mEditorBase(&aEditorBase)
-  , mSheet(aSheet)
+  , mSheet(&aStyleSheet)
 {
-  MOZ_ASSERT(aSheet);
 }
 
 NS_IMPL_CYCLE_COLLECTION_INHERITED(RemoveStyleSheetTransaction,
                                    EditTransactionBase,
                                    mEditorBase,
                                    mSheet)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(RemoveStyleSheetTransaction)
--- a/editor/libeditor/StyleSheetTransactions.h
+++ b/editor/libeditor/StyleSheetTransactions.h
@@ -12,22 +12,33 @@
 #include "nsCycleCollectionParticipant.h"
 #include "nsID.h"                       // for REFNSIID
 #include "nscore.h"                     // for NS_IMETHOD
 
 namespace mozilla {
 
 class AddStyleSheetTransaction final : public EditTransactionBase
 {
+protected:
+  AddStyleSheetTransaction(EditorBase& aEditor, StyleSheet& aStyleSheet);
+
 public:
   /**
-   * @param aEditor     The object providing core editing operations
-   * @param aSheet      The stylesheet to add
-    */
-  AddStyleSheetTransaction(EditorBase& aEditor, StyleSheet* aSheet);
+   * Creates an add style sheet transaction.  This never returns nullptr.
+   *
+   * @param aEditorBase The editor.
+   * @param aSheet      The style sheet to add.
+   */
+  static already_AddRefed<AddStyleSheetTransaction>
+  Create(EditorBase& aEditorBase, StyleSheet& aStyleSheet)
+  {
+    RefPtr<AddStyleSheetTransaction> transaction =
+      new AddStyleSheetTransaction(aEditorBase, aStyleSheet);
+    return transaction.forget();
+  }
 
   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AddStyleSheetTransaction,
                                            EditTransactionBase)
   NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
 
   NS_DECL_EDITTRANSACTIONBASE
 
 protected:
@@ -35,32 +46,42 @@ protected:
   RefPtr<EditorBase> mEditorBase;
   // The style sheet to add.
   RefPtr<mozilla::StyleSheet> mSheet;
 };
 
 
 class RemoveStyleSheetTransaction final : public EditTransactionBase
 {
+protected:
+  RemoveStyleSheetTransaction(EditorBase& aEditor, StyleSheet& aStyleSheet);
+
 public:
   /**
+   * Creates a remove style sheet transaction.  This never returns nullptr.
+   *
    * @param aEditor     The object providing core editing operations.
    * @param aSheet      The stylesheet to remove.
    */
-  RemoveStyleSheetTransaction(EditorBase& aEditor, StyleSheet* aSheet);
+  static already_AddRefed<RemoveStyleSheetTransaction>
+  Create(EditorBase& aEditorBase, StyleSheet& aStyleSheet)
+  {
+    RefPtr<RemoveStyleSheetTransaction> transaction =
+      new RemoveStyleSheetTransaction(aEditorBase, aStyleSheet);
+    return transaction.forget();
+  }
 
   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(RemoveStyleSheetTransaction,
                                            EditTransactionBase)
   NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
 
   NS_DECL_EDITTRANSACTIONBASE
 
 protected:
   // The editor that created this transaction.
   RefPtr<EditorBase> mEditorBase;
   // The style sheet to remove.
   RefPtr<StyleSheet> mSheet;
-
 };
 
 } // namespace mozilla
 
 #endif // #ifndef StylesheetTransactions_h