summaryrefslogtreecommitdiffstats
path: root/editor/txmgr/TransactionItem.cpp
blob: 26d114dfc98612c057b36ae37bfee3a2a0347359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "TransactionItem.h"

#include "mozilla/mozalloc.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/IntegerRange.h"
#include "mozilla/OwningNonNull.h"
#include "mozilla/TransactionManager.h"
#include "mozilla/TransactionStack.h"
#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsError.h"
#include "nsISupportsImpl.h"
#include "nsITransaction.h"

namespace mozilla {

TransactionItem::TransactionItem(nsITransaction* aTransaction)
    : mTransaction(aTransaction), mUndoStack(0), mRedoStack(0) {}

TransactionItem::~TransactionItem() {
  delete mRedoStack;
  delete mUndoStack;
}

void TransactionItem::CleanUp() {
  mData.Clear();
  mTransaction = nullptr;
  if (mRedoStack) {
    mRedoStack->DoUnlink();
  }
  if (mUndoStack) {
    mUndoStack->DoUnlink();
  }
}

NS_IMPL_CYCLE_COLLECTING_NATIVE_ADDREF(TransactionItem)
NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE_WITH_LAST_RELEASE(TransactionItem,
                                                          CleanUp())

NS_IMPL_CYCLE_COLLECTION_CLASS(TransactionItem)

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(TransactionItem)
  tmp->CleanUp();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(TransactionItem)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mData)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTransaction)
  if (tmp->mRedoStack) {
    tmp->mRedoStack->DoTraverse(cb);
  }
  if (tmp->mUndoStack) {
    tmp->mUndoStack->DoTraverse(cb);
  }
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

nsresult TransactionItem::AddChild(TransactionItem& aTransactionItem) {
  if (!mUndoStack) {
    mUndoStack = new TransactionStack(TransactionStack::FOR_UNDO);
  }

  mUndoStack->Push(&aTransactionItem);
  return NS_OK;
}

already_AddRefed<nsITransaction> TransactionItem::GetTransaction() {
  return do_AddRef(mTransaction);
}

nsresult TransactionItem::DoTransaction() {
  if (!mTransaction) {
    return NS_OK;
  }
  OwningNonNull<nsITransaction> transaction = *mTransaction;
  nsresult rv = transaction->DoTransaction();
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                       "nsITransaction::DoTransaction() failed");
  return rv;
}

nsresult TransactionItem::UndoTransaction(
    TransactionManager* aTransactionManager) {
  nsresult rv = UndoChildren(aTransactionManager);
  if (NS_FAILED(rv)) {
    NS_WARNING("TransactionItem::UndoChildren() failed");
    DebugOnly<nsresult> rvIgnored = RecoverFromUndoError(aTransactionManager);
    NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
                         "TransactionItem::RecoverFromUndoError() failed");
    return rv;
  }

  if (!mTransaction) {
    return NS_OK;
  }

  OwningNonNull<nsITransaction> transaction = *mTransaction;
  rv = transaction->UndoTransaction();
  if (NS_SUCCEEDED(rv)) {
    return NS_OK;
  }

  NS_WARNING("TransactionItem::UndoTransaction() failed");
  DebugOnly<nsresult> rvIgnored = RecoverFromUndoError(aTransactionManager);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
                       "TransactionItem::RecoverFromUndoError() failed");
  return rv;
}

nsresult TransactionItem::UndoChildren(
    TransactionManager* aTransactionManager) {
  if (!mUndoStack) {
    return NS_OK;
  }

  if (!mRedoStack) {
    mRedoStack = new TransactionStack(TransactionStack::FOR_REDO);
  }

  const size_t undoStackSize = mUndoStack->GetSize();

  nsresult rv = NS_OK;
  for ([[maybe_unused]] const size_t undoneCount :
       IntegerRange(undoStackSize)) {
    RefPtr<TransactionItem> transactionItem = mUndoStack->Peek();
    if (!transactionItem) {
      return NS_ERROR_FAILURE;
    }

    nsCOMPtr<nsITransaction> transaction = transactionItem->GetTransaction();
    rv = transactionItem->UndoTransaction(aTransactionManager);
    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                         "TransactionItem::UndoTransaction() failed");
    if (NS_SUCCEEDED(rv)) {
      transactionItem = mUndoStack->Pop();
      mRedoStack->Push(transactionItem.forget());
    }

    if (transaction) {
      aTransactionManager->DidUndoNotify(*transaction, rv);
    }
  }
  // NS_OK if there is no Undo items or all methods work fine, otherwise,
  // the result of the last item's UndoTransaction().
  return rv;
}

nsresult TransactionItem::RedoTransaction(
    TransactionManager* aTransactionManager) {
  nsCOMPtr<nsITransaction> transaction(mTransaction);
  if (transaction) {
    nsresult rv = transaction->RedoTransaction();
    if (NS_FAILED(rv)) {
      NS_WARNING("nsITransaction::RedoTransaction() failed");
      return rv;
    }
  }

  nsresult rv = RedoChildren(aTransactionManager);
  if (NS_SUCCEEDED(rv)) {
    return NS_OK;
  }

  NS_WARNING("TransactionItem::RedoChildren() failed");
  DebugOnly<nsresult> rvIgnored = RecoverFromRedoError(aTransactionManager);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
                       "TransactionItem::RecoverFromRedoError() failed");
  return rv;
}

nsresult TransactionItem::RedoChildren(
    TransactionManager* aTransactionManager) {
  if (!mRedoStack) {
    return NS_OK;
  }

  /* Redo all of the transaction items children! */
  const size_t redoStackSize = mRedoStack->GetSize();

  nsresult rv = NS_OK;
  for ([[maybe_unused]] const size_t redoneCount :
       IntegerRange(redoStackSize)) {
    RefPtr<TransactionItem> transactionItem = mRedoStack->Peek();
    if (NS_WARN_IF(!transactionItem)) {
      return NS_ERROR_FAILURE;
    }

    nsCOMPtr<nsITransaction> transaction = transactionItem->GetTransaction();
    rv = transactionItem->RedoTransaction(aTransactionManager);
    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                         "TransactionItem::RedoTransaction() failed");
    if (NS_SUCCEEDED(rv)) {
      transactionItem = mRedoStack->Pop();
      mUndoStack->Push(transactionItem.forget());
    }

    // XXX Shouldn't this DidRedoNotify()? (bug 1311626)
    if (transaction) {
      aTransactionManager->DidUndoNotify(*transaction, rv);
    }
  }
  // NS_OK if there is no Redo items or all methods work fine, otherwise,
  // the result of the last item's RedoTransaction().
  return rv;
}

size_t TransactionItem::NumberOfUndoItems() const {
  NS_WARNING_ASSERTION(!mUndoStack || mUndoStack->GetSize() > 0,
                       "UndoStack cannot have no children");
  return mUndoStack ? mUndoStack->GetSize() : 0;
}

size_t TransactionItem::NumberOfRedoItems() const {
  NS_WARNING_ASSERTION(!mRedoStack || mRedoStack->GetSize() > 0,
                       "UndoStack cannot have no children");
  return mRedoStack ? mRedoStack->GetSize() : 0;
}

nsresult TransactionItem::RecoverFromUndoError(
    TransactionManager* aTransactionManager) {
  // If this method gets called, we never got to the point where we
  // successfully called UndoTransaction() for the transaction item itself.
  // Just redo any children that successfully called undo!
  nsresult rv = RedoChildren(aTransactionManager);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                       "TransactionItem::RedoChildren() failed");
  return rv;
}

nsresult TransactionItem::RecoverFromRedoError(
    TransactionManager* aTransactionManager) {
  // If this method gets called, we already successfully called
  // RedoTransaction() for the transaction item itself. Undo all
  // the children that successfully called RedoTransaction(),
  // then undo the transaction item itself.
  nsresult rv = UndoChildren(aTransactionManager);
  if (NS_FAILED(rv)) {
    NS_WARNING("TransactionItem::UndoChildren() failed");
    return rv;
  }

  if (!mTransaction) {
    return NS_OK;
  }

  OwningNonNull<nsITransaction> transaction = *mTransaction;
  rv = transaction->UndoTransaction();
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                       "nsITransaction::UndoTransaction() failed");
  return rv;
}

}  // namespace mozilla