Backed out changeset 88fbb17e3c20 (bug 1865637) for causing animation related mochite...
[gecko.git] / editor / txmgr / TransactionManager.cpp
blobad4d641d5678024e3b26931713406cc1afa3ecb3
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/TransactionManager.h"
8 #include "mozilla/Assertions.h"
9 #include "mozilla/DebugOnly.h"
10 #include "mozilla/HTMLEditor.h"
11 #include "mozilla/mozalloc.h"
12 #include "mozilla/TransactionStack.h"
13 #include "nsCOMPtr.h"
14 #include "nsDebug.h"
15 #include "nsError.h"
16 #include "nsISupports.h"
17 #include "nsISupportsUtils.h"
18 #include "nsITransaction.h"
19 #include "nsIWeakReference.h"
20 #include "TransactionItem.h"
22 namespace mozilla {
24 TransactionManager::TransactionManager(int32_t aMaxTransactionCount)
25 : mMaxTransactionCount(aMaxTransactionCount),
26 mDoStack(TransactionStack::FOR_UNDO),
27 mUndoStack(TransactionStack::FOR_UNDO),
28 mRedoStack(TransactionStack::FOR_REDO) {}
30 NS_IMPL_CYCLE_COLLECTION_CLASS(TransactionManager)
32 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(TransactionManager)
33 NS_IMPL_CYCLE_COLLECTION_UNLINK(mHTMLEditor)
34 tmp->mDoStack.DoUnlink();
35 tmp->mUndoStack.DoUnlink();
36 tmp->mRedoStack.DoUnlink();
37 NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE
38 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
40 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(TransactionManager)
41 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mHTMLEditor)
42 tmp->mDoStack.DoTraverse(cb);
43 tmp->mUndoStack.DoTraverse(cb);
44 tmp->mRedoStack.DoTraverse(cb);
45 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
47 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TransactionManager)
48 NS_INTERFACE_MAP_ENTRY(nsITransactionManager)
49 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
50 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsITransactionManager)
51 NS_INTERFACE_MAP_END
53 NS_IMPL_CYCLE_COLLECTING_ADDREF(TransactionManager)
54 NS_IMPL_CYCLE_COLLECTING_RELEASE(TransactionManager)
56 void TransactionManager::Attach(HTMLEditor& aHTMLEditor) {
57 mHTMLEditor = &aHTMLEditor;
60 void TransactionManager::Detach(const HTMLEditor& aHTMLEditor) {
61 MOZ_DIAGNOSTIC_ASSERT_IF(mHTMLEditor, &aHTMLEditor == mHTMLEditor);
62 if (mHTMLEditor == &aHTMLEditor) {
63 mHTMLEditor = nullptr;
67 MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHODIMP
68 TransactionManager::DoTransaction(nsITransaction* aTransaction) {
69 if (NS_WARN_IF(!aTransaction)) {
70 return NS_ERROR_INVALID_ARG;
72 OwningNonNull<nsITransaction> transaction = *aTransaction;
74 nsresult rv = BeginTransaction(transaction, nullptr);
75 if (NS_FAILED(rv)) {
76 NS_WARNING("TransactionManager::BeginTransaction() failed");
77 DidDoNotify(transaction, rv);
78 return rv;
81 rv = EndTransaction(false);
82 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
83 "TransactionManager::EndTransaction() failed");
85 DidDoNotify(transaction, rv);
86 return rv;
89 MOZ_CAN_RUN_SCRIPT NS_IMETHODIMP TransactionManager::UndoTransaction() {
90 return Undo();
93 nsresult TransactionManager::Undo() {
94 // It's possible to be called Undo() again while the transaction manager is
95 // executing a transaction's DoTransaction() method. If this happens,
96 // the Undo() request is ignored, and we return NS_ERROR_FAILURE. This
97 // may occur if a mutation event listener calls document.execCommand("undo").
98 if (NS_WARN_IF(!mDoStack.IsEmpty())) {
99 return NS_ERROR_FAILURE;
102 // Peek at the top of the undo stack. Don't remove the transaction
103 // until it has successfully completed.
104 RefPtr<TransactionItem> transactionItem = mUndoStack.Peek();
105 if (!transactionItem) {
106 // Bail if there's nothing on the stack.
107 return NS_OK;
110 nsCOMPtr<nsITransaction> transaction = transactionItem->GetTransaction();
111 nsresult rv = transactionItem->UndoTransaction(this);
112 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
113 "TransactionItem::UndoTransaction() failed");
114 if (NS_SUCCEEDED(rv)) {
115 transactionItem = mUndoStack.Pop();
116 mRedoStack.Push(transactionItem.forget());
119 if (transaction) {
120 DidUndoNotify(*transaction, rv);
122 return rv;
125 MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHODIMP
126 TransactionManager::RedoTransaction() {
127 return Redo();
130 nsresult TransactionManager::Redo() {
131 // It's possible to be called Redo() again while the transaction manager is
132 // executing a transaction's DoTransaction() method. If this happens,
133 // the Redo() request is ignored, and we return NS_ERROR_FAILURE. This
134 // may occur if a mutation event listener calls document.execCommand("redo").
135 if (NS_WARN_IF(!mDoStack.IsEmpty())) {
136 return NS_ERROR_FAILURE;
139 // Peek at the top of the redo stack. Don't remove the transaction
140 // until it has successfully completed.
141 RefPtr<TransactionItem> transactionItem = mRedoStack.Peek();
142 if (!transactionItem) {
143 // Bail if there's nothing on the stack.
144 return NS_OK;
147 nsCOMPtr<nsITransaction> transaction = transactionItem->GetTransaction();
148 nsresult rv = transactionItem->RedoTransaction(this);
149 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
150 "TransactionItem::RedoTransaction() failed");
151 if (NS_SUCCEEDED(rv)) {
152 transactionItem = mRedoStack.Pop();
153 mUndoStack.Push(transactionItem.forget());
156 if (transaction) {
157 DidRedoNotify(*transaction, rv);
159 return rv;
162 NS_IMETHODIMP TransactionManager::Clear() {
163 return NS_WARN_IF(!ClearUndoRedo()) ? NS_ERROR_FAILURE : NS_OK;
166 NS_IMETHODIMP TransactionManager::BeginBatch(nsISupports* aData) {
167 nsresult rv = BeginBatchInternal(aData);
168 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
169 "TransactionManager::BeginBatchInternal() failed");
170 return rv;
173 nsresult TransactionManager::BeginBatchInternal(nsISupports* aData) {
174 // We can batch independent transactions together by simply pushing
175 // a dummy transaction item on the do stack. This dummy transaction item
176 // will be popped off the do stack, and then pushed on the undo stack
177 // in EndBatch().
178 nsresult rv = BeginTransaction(nullptr, aData);
179 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
180 "TransactionManager::BeginTransaction() failed");
181 return rv;
184 NS_IMETHODIMP TransactionManager::EndBatch(bool aAllowEmpty) {
185 nsresult rv = EndBatchInternal(aAllowEmpty);
186 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
187 "TransactionManager::EndBatchInternal() failed");
188 return rv;
191 nsresult TransactionManager::EndBatchInternal(bool aAllowEmpty) {
192 // XXX: Need to add some mechanism to detect the case where the transaction
193 // at the top of the do stack isn't the dummy transaction, so we can
194 // throw an error!! This can happen if someone calls EndBatch() within
195 // the DoTransaction() method of a transaction.
197 // For now, we can detect this case by checking the value of the
198 // dummy transaction's mTransaction field. If it is our dummy
199 // transaction, it should be nullptr. This may not be true in the
200 // future when we allow users to execute a transaction when beginning
201 // a batch!!!!
202 RefPtr<TransactionItem> transactionItem = mDoStack.Peek();
203 if (NS_WARN_IF(!transactionItem)) {
204 return NS_ERROR_FAILURE;
206 nsCOMPtr<nsITransaction> transaction = transactionItem->GetTransaction();
207 if (NS_WARN_IF(transaction)) {
208 return NS_ERROR_FAILURE;
211 nsresult rv = EndTransaction(aAllowEmpty);
212 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
213 "TransactionManager::EndTransaction() failed");
214 return rv;
217 NS_IMETHODIMP TransactionManager::GetNumberOfUndoItems(int32_t* aNumItems) {
218 *aNumItems = static_cast<int32_t>(NumberOfUndoItems());
219 MOZ_ASSERT(*aNumItems >= 0);
220 return NS_OK;
223 NS_IMETHODIMP TransactionManager::GetNumberOfRedoItems(int32_t* aNumItems) {
224 *aNumItems = static_cast<int32_t>(NumberOfRedoItems());
225 MOZ_ASSERT(*aNumItems >= 0);
226 return NS_OK;
229 NS_IMETHODIMP TransactionManager::GetMaxTransactionCount(int32_t* aMaxCount) {
230 if (NS_WARN_IF(!aMaxCount)) {
231 return NS_ERROR_INVALID_ARG;
233 *aMaxCount = mMaxTransactionCount;
234 return NS_OK;
237 NS_IMETHODIMP TransactionManager::SetMaxTransactionCount(int32_t aMaxCount) {
238 return NS_WARN_IF(!EnableUndoRedo(aMaxCount)) ? NS_ERROR_FAILURE : NS_OK;
241 bool TransactionManager::EnableUndoRedo(int32_t aMaxTransactionCount) {
242 // It is illegal to call EnableUndoRedo() while the transaction manager is
243 // executing a transaction's DoTransaction() method because the undo and redo
244 // stacks might get pruned. If this happens, the EnableUndoRedo() request is
245 // ignored, and we return false.
246 if (NS_WARN_IF(!mDoStack.IsEmpty())) {
247 return false;
250 // If aMaxTransactionCount is 0, it means to disable undo/redo.
251 if (!aMaxTransactionCount) {
252 mUndoStack.Clear();
253 mRedoStack.Clear();
254 mMaxTransactionCount = 0;
255 return true;
258 // If aMaxTransactionCount is less than zero, the user wants unlimited
259 // levels of undo! No need to prune the undo or redo stacks.
260 if (aMaxTransactionCount < 0) {
261 mMaxTransactionCount = -1;
262 return true;
265 // If new max transaction count is greater than or equal to current max
266 // transaction count, we don't need to remove any transactions.
267 if (mMaxTransactionCount >= 0 &&
268 mMaxTransactionCount <= aMaxTransactionCount) {
269 mMaxTransactionCount = aMaxTransactionCount;
270 return true;
273 // If aMaxTransactionCount is greater than the number of transactions that
274 // currently exist on the undo and redo stack, there is no need to prune the
275 // undo or redo stacks.
276 size_t numUndoItems = NumberOfUndoItems();
277 size_t numRedoItems = NumberOfRedoItems();
278 size_t total = numUndoItems + numRedoItems;
279 size_t newMaxTransactionCount = static_cast<size_t>(aMaxTransactionCount);
280 if (newMaxTransactionCount > total) {
281 mMaxTransactionCount = aMaxTransactionCount;
282 return true;
285 // Try getting rid of some transactions on the undo stack! Start at
286 // the bottom of the stack and pop towards the top.
287 for (; numUndoItems && (numRedoItems + numUndoItems) > newMaxTransactionCount;
288 numUndoItems--) {
289 RefPtr<TransactionItem> transactionItem = mUndoStack.PopBottom();
290 MOZ_ASSERT(transactionItem);
293 // If necessary, get rid of some transactions on the redo stack! Start at
294 // the bottom of the stack and pop towards the top.
295 for (; numRedoItems && (numRedoItems + numUndoItems) > newMaxTransactionCount;
296 numRedoItems--) {
297 RefPtr<TransactionItem> transactionItem = mRedoStack.PopBottom();
298 MOZ_ASSERT(transactionItem);
301 mMaxTransactionCount = aMaxTransactionCount;
302 return true;
305 NS_IMETHODIMP TransactionManager::PeekUndoStack(nsITransaction** aTransaction) {
306 MOZ_ASSERT(aTransaction);
307 *aTransaction = PeekUndoStack().take();
308 return NS_OK;
311 already_AddRefed<nsITransaction> TransactionManager::PeekUndoStack() {
312 RefPtr<TransactionItem> transactionItem = mUndoStack.Peek();
313 if (!transactionItem) {
314 return nullptr;
316 return transactionItem->GetTransaction();
319 NS_IMETHODIMP TransactionManager::PeekRedoStack(nsITransaction** aTransaction) {
320 MOZ_ASSERT(aTransaction);
321 *aTransaction = PeekRedoStack().take();
322 return NS_OK;
325 already_AddRefed<nsITransaction> TransactionManager::PeekRedoStack() {
326 RefPtr<TransactionItem> transactionItem = mRedoStack.Peek();
327 if (!transactionItem) {
328 return nullptr;
330 return transactionItem->GetTransaction();
333 nsresult TransactionManager::BatchTopUndo() {
334 if (mUndoStack.GetSize() < 2) {
335 // Not enough transactions to merge into one batch.
336 return NS_OK;
339 RefPtr<TransactionItem> lastUndo = mUndoStack.Pop();
340 MOZ_ASSERT(lastUndo, "There should be at least two transactions.");
342 RefPtr<TransactionItem> previousUndo = mUndoStack.Peek();
343 MOZ_ASSERT(previousUndo, "There should be at least two transactions.");
345 nsresult rv = previousUndo->AddChild(*lastUndo);
346 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "TransactionItem::AddChild() failed");
348 // Transfer data from the transactions that is going to be
349 // merged to the transaction that it is being merged with.
350 nsCOMArray<nsISupports>& lastData = lastUndo->GetData();
351 nsCOMArray<nsISupports>& previousData = previousUndo->GetData();
352 if (!previousData.AppendObjects(lastData)) {
353 NS_WARNING("nsISupports::AppendObjects() failed");
354 return NS_ERROR_FAILURE;
356 lastData.Clear();
357 return rv;
360 nsresult TransactionManager::RemoveTopUndo() {
361 if (mUndoStack.IsEmpty()) {
362 return NS_OK;
365 RefPtr<TransactionItem> lastUndo = mUndoStack.Pop();
366 return NS_OK;
369 NS_IMETHODIMP TransactionManager::ClearUndoStack() {
370 if (NS_WARN_IF(!mDoStack.IsEmpty())) {
371 return NS_ERROR_FAILURE;
373 mUndoStack.Clear();
374 return NS_OK;
377 NS_IMETHODIMP TransactionManager::ClearRedoStack() {
378 if (NS_WARN_IF(!mDoStack.IsEmpty())) {
379 return NS_ERROR_FAILURE;
381 mRedoStack.Clear();
382 return NS_OK;
385 void TransactionManager::DidDoNotify(nsITransaction& aTransaction,
386 nsresult aDoResult) {
387 if (mHTMLEditor) {
388 RefPtr<HTMLEditor> htmlEditor(mHTMLEditor);
389 htmlEditor->DidDoTransaction(*this, aTransaction, aDoResult);
393 void TransactionManager::DidUndoNotify(nsITransaction& aTransaction,
394 nsresult aUndoResult) {
395 if (mHTMLEditor) {
396 RefPtr<HTMLEditor> htmlEditor(mHTMLEditor);
397 htmlEditor->DidUndoTransaction(*this, aTransaction, aUndoResult);
401 void TransactionManager::DidRedoNotify(nsITransaction& aTransaction,
402 nsresult aRedoResult) {
403 if (mHTMLEditor) {
404 RefPtr<HTMLEditor> htmlEditor(mHTMLEditor);
405 htmlEditor->DidRedoTransaction(*this, aTransaction, aRedoResult);
409 nsresult TransactionManager::BeginTransaction(nsITransaction* aTransaction,
410 nsISupports* aData) {
411 // XXX: POSSIBLE OPTIMIZATION
412 // We could use a factory that pre-allocates/recycles transaction items.
413 RefPtr<TransactionItem> transactionItem = new TransactionItem(aTransaction);
415 if (aData) {
416 nsCOMArray<nsISupports>& data = transactionItem->GetData();
417 data.AppendObject(aData);
420 mDoStack.Push(transactionItem);
422 nsresult rv = transactionItem->DoTransaction();
423 if (NS_FAILED(rv)) {
424 NS_WARNING("TransactionItem::DoTransaction() failed");
425 transactionItem = mDoStack.Pop();
427 return rv;
430 nsresult TransactionManager::EndTransaction(bool aAllowEmpty) {
431 RefPtr<TransactionItem> transactionItem = mDoStack.Pop();
432 if (NS_WARN_IF(!transactionItem)) {
433 return NS_ERROR_FAILURE;
436 nsCOMPtr<nsITransaction> transaction = transactionItem->GetTransaction();
437 if (!transaction && !aAllowEmpty) {
438 // If we get here, the transaction must be a dummy batch transaction
439 // created by BeginBatch(). If it contains no children, get rid of it!
440 if (!transactionItem->NumberOfChildren()) {
441 return NS_OK;
445 // Check if the transaction is transient. If it is, there's nothing
446 // more to do, just return.
447 if (transaction) {
448 bool isTransient = false;
449 nsresult rv = transaction->GetIsTransient(&isTransient);
450 if (NS_FAILED(rv)) {
451 NS_WARNING("nsITransaction::GetIsTransient() failed");
452 return rv;
454 // XXX: Should we be clearing the redo stack if the transaction
455 // is transient and there is nothing on the do stack?
456 if (isTransient) {
457 return NS_OK;
461 if (!mMaxTransactionCount) {
462 return NS_OK;
465 // Check if there is a transaction on the do stack. If there is,
466 // the current transaction is a "sub" transaction, and should
467 // be added to the transaction at the top of the do stack.
468 RefPtr<TransactionItem> topTransactionItem = mDoStack.Peek();
469 if (topTransactionItem) {
470 // XXX: What do we do if this fails?
471 nsresult rv = topTransactionItem->AddChild(*transactionItem);
472 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
473 "TransactionItem::AddChild() failed");
474 return rv;
477 // The transaction succeeded, so clear the redo stack.
478 mRedoStack.Clear();
480 // Check if we can coalesce this transaction with the one at the top
481 // of the undo stack.
482 topTransactionItem = mUndoStack.Peek();
483 if (transaction && topTransactionItem) {
484 bool didMerge = false;
485 nsCOMPtr<nsITransaction> topTransaction =
486 topTransactionItem->GetTransaction();
487 if (topTransaction) {
488 nsresult rv = topTransaction->Merge(transaction, &didMerge);
489 if (didMerge) {
490 return rv;
492 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
493 "nsITransaction::Merge() failed, but ignored");
497 // Check to see if we've hit the max level of undo. If so,
498 // pop the bottom transaction off the undo stack and release it!
499 int32_t sz = mUndoStack.GetSize();
500 if (mMaxTransactionCount > 0 && sz >= mMaxTransactionCount) {
501 RefPtr<TransactionItem> overflow = mUndoStack.PopBottom();
504 // Push the transaction on the undo stack:
505 mUndoStack.Push(transactionItem.forget());
506 return NS_OK;
509 } // namespace mozilla