michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/mozalloc.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsDebug.h" michael@0: #include "nsError.h" michael@0: #include "nsISupportsImpl.h" michael@0: #include "nsITransaction.h" michael@0: #include "nsTransactionItem.h" michael@0: #include "nsTransactionManager.h" michael@0: #include "nsTransactionStack.h" michael@0: michael@0: nsTransactionItem::nsTransactionItem(nsITransaction *aTransaction) michael@0: : mTransaction(aTransaction), mUndoStack(0), mRedoStack(0) michael@0: { michael@0: } michael@0: michael@0: nsTransactionItem::~nsTransactionItem() michael@0: { michael@0: delete mRedoStack; michael@0: michael@0: delete mUndoStack; michael@0: } michael@0: michael@0: void michael@0: nsTransactionItem::CleanUp() michael@0: { michael@0: mData.Clear(); michael@0: mTransaction = nullptr; michael@0: if (mRedoStack) { michael@0: mRedoStack->DoUnlink(); michael@0: } michael@0: if (mUndoStack) { michael@0: mUndoStack->DoUnlink(); michael@0: } michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_NATIVE_ADDREF(nsTransactionItem) michael@0: NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE_WITH_LAST_RELEASE(nsTransactionItem, michael@0: CleanUp()) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CLASS(nsTransactionItem) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsTransactionItem) michael@0: tmp->CleanUp(); michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsTransactionItem) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mData) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTransaction) michael@0: if (tmp->mRedoStack) { michael@0: tmp->mRedoStack->DoTraverse(cb); michael@0: } michael@0: if (tmp->mUndoStack) { michael@0: tmp->mUndoStack->DoTraverse(cb); michael@0: } michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(nsTransactionItem, AddRef) michael@0: NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(nsTransactionItem, Release) michael@0: michael@0: nsresult michael@0: nsTransactionItem::AddChild(nsTransactionItem *aTransactionItem) michael@0: { michael@0: NS_ENSURE_TRUE(aTransactionItem, NS_ERROR_NULL_POINTER); michael@0: michael@0: if (!mUndoStack) { michael@0: mUndoStack = new nsTransactionStack(nsTransactionStack::FOR_UNDO); michael@0: } michael@0: michael@0: mUndoStack->Push(aTransactionItem); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsTransactionItem::GetTransaction() michael@0: { michael@0: nsCOMPtr txn = mTransaction; michael@0: return txn.forget(); michael@0: } michael@0: michael@0: nsresult michael@0: nsTransactionItem::GetIsBatch(bool *aIsBatch) michael@0: { michael@0: NS_ENSURE_TRUE(aIsBatch, NS_ERROR_NULL_POINTER); michael@0: michael@0: *aIsBatch = !mTransaction; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsTransactionItem::GetNumberOfChildren(int32_t *aNumChildren) michael@0: { michael@0: nsresult result; michael@0: michael@0: NS_ENSURE_TRUE(aNumChildren, NS_ERROR_NULL_POINTER); michael@0: michael@0: *aNumChildren = 0; michael@0: michael@0: int32_t ui = 0; michael@0: int32_t ri = 0; michael@0: michael@0: result = GetNumberOfUndoItems(&ui); michael@0: michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: michael@0: result = GetNumberOfRedoItems(&ri); michael@0: michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: michael@0: *aNumChildren = ui + ri; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsTransactionItem::GetChild(int32_t aIndex, nsTransactionItem **aChild) michael@0: { michael@0: NS_ENSURE_TRUE(aChild, NS_ERROR_NULL_POINTER); michael@0: michael@0: *aChild = 0; michael@0: michael@0: int32_t numItems = 0; michael@0: nsresult result = GetNumberOfChildren(&numItems); michael@0: michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: michael@0: if (aIndex < 0 || aIndex >= numItems) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: // Children are expected to be in the order they were added, michael@0: // so the child first added would be at the bottom of the undo michael@0: // stack, or if there are no items on the undo stack, it would michael@0: // be at the top of the redo stack. michael@0: michael@0: result = GetNumberOfUndoItems(&numItems); michael@0: michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: michael@0: if (numItems > 0 && aIndex < numItems) { michael@0: NS_ENSURE_TRUE(mUndoStack, NS_ERROR_FAILURE); michael@0: michael@0: nsRefPtr child = mUndoStack->GetItem(aIndex); michael@0: child.forget(aChild); michael@0: return *aChild ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // Adjust the index for the redo stack: michael@0: michael@0: aIndex -= numItems; michael@0: michael@0: result = GetNumberOfRedoItems(&numItems); michael@0: michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: michael@0: NS_ENSURE_TRUE(mRedoStack && numItems != 0 && aIndex < numItems, NS_ERROR_FAILURE); michael@0: michael@0: nsRefPtr child = mRedoStack->GetItem(aIndex); michael@0: child.forget(aChild); michael@0: return *aChild ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsresult michael@0: nsTransactionItem::DoTransaction() michael@0: { michael@0: if (mTransaction) michael@0: return mTransaction->DoTransaction(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsTransactionItem::UndoTransaction(nsTransactionManager *aTxMgr) michael@0: { michael@0: nsresult result = UndoChildren(aTxMgr); michael@0: michael@0: if (NS_FAILED(result)) { michael@0: RecoverFromUndoError(aTxMgr); michael@0: return result; michael@0: } michael@0: michael@0: if (!mTransaction) michael@0: return NS_OK; michael@0: michael@0: result = mTransaction->UndoTransaction(); michael@0: michael@0: if (NS_FAILED(result)) { michael@0: RecoverFromUndoError(aTxMgr); michael@0: return result; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsTransactionItem::UndoChildren(nsTransactionManager *aTxMgr) michael@0: { michael@0: nsRefPtr item; michael@0: nsresult result = NS_OK; michael@0: int32_t sz = 0; michael@0: michael@0: if (mUndoStack) { michael@0: if (!mRedoStack && mUndoStack) { michael@0: mRedoStack = new nsTransactionStack(nsTransactionStack::FOR_REDO); michael@0: } michael@0: michael@0: /* Undo all of the transaction items children! */ michael@0: sz = mUndoStack->GetSize(); michael@0: michael@0: while (sz-- > 0) { michael@0: item = mUndoStack->Peek(); michael@0: michael@0: if (!item) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsCOMPtr t = item->GetTransaction(); michael@0: michael@0: bool doInterrupt = false; michael@0: michael@0: result = aTxMgr->WillUndoNotify(t, &doInterrupt); michael@0: michael@0: if (NS_FAILED(result)) { michael@0: return result; michael@0: } michael@0: michael@0: if (doInterrupt) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: result = item->UndoTransaction(aTxMgr); michael@0: michael@0: if (NS_SUCCEEDED(result)) { michael@0: item = mUndoStack->Pop(); michael@0: mRedoStack->Push(item); michael@0: } michael@0: michael@0: nsresult result2 = aTxMgr->DidUndoNotify(t, result); michael@0: michael@0: if (NS_SUCCEEDED(result)) { michael@0: result = result2; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: nsresult michael@0: nsTransactionItem::RedoTransaction(nsTransactionManager *aTxMgr) michael@0: { michael@0: nsresult result; michael@0: michael@0: nsCOMPtr kungfuDeathGrip(mTransaction); michael@0: if (mTransaction) { michael@0: result = mTransaction->RedoTransaction(); michael@0: michael@0: NS_ENSURE_SUCCESS(result, result); michael@0: } michael@0: michael@0: result = RedoChildren(aTxMgr); michael@0: michael@0: if (NS_FAILED(result)) { michael@0: RecoverFromRedoError(aTxMgr); michael@0: return result; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsTransactionItem::RedoChildren(nsTransactionManager *aTxMgr) michael@0: { michael@0: nsRefPtr item; michael@0: nsresult result = NS_OK; michael@0: michael@0: if (!mRedoStack) michael@0: return NS_OK; michael@0: michael@0: /* Redo all of the transaction items children! */ michael@0: int32_t sz = mRedoStack->GetSize(); michael@0: michael@0: while (sz-- > 0) { michael@0: item = mRedoStack->Peek(); michael@0: michael@0: if (!item) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsCOMPtr t = item->GetTransaction(); michael@0: michael@0: bool doInterrupt = false; michael@0: michael@0: result = aTxMgr->WillRedoNotify(t, &doInterrupt); michael@0: michael@0: if (NS_FAILED(result)) { michael@0: return result; michael@0: } michael@0: michael@0: if (doInterrupt) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: result = item->RedoTransaction(aTxMgr); michael@0: michael@0: if (NS_SUCCEEDED(result)) { michael@0: item = mRedoStack->Pop(); michael@0: mUndoStack->Push(item); michael@0: } michael@0: michael@0: nsresult result2 = aTxMgr->DidUndoNotify(t, result); michael@0: michael@0: if (NS_SUCCEEDED(result)) { michael@0: result = result2; michael@0: } michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: nsresult michael@0: nsTransactionItem::GetNumberOfUndoItems(int32_t *aNumItems) michael@0: { michael@0: NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER); michael@0: michael@0: if (!mUndoStack) { michael@0: *aNumItems = 0; michael@0: return NS_OK; michael@0: } michael@0: michael@0: *aNumItems = mUndoStack->GetSize(); michael@0: return *aNumItems ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsresult michael@0: nsTransactionItem::GetNumberOfRedoItems(int32_t *aNumItems) michael@0: { michael@0: NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER); michael@0: michael@0: if (!mRedoStack) { michael@0: *aNumItems = 0; michael@0: return NS_OK; michael@0: } michael@0: michael@0: *aNumItems = mRedoStack->GetSize(); michael@0: return *aNumItems ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsresult michael@0: nsTransactionItem::RecoverFromUndoError(nsTransactionManager *aTxMgr) michael@0: { michael@0: // michael@0: // If this method gets called, we never got to the point where we michael@0: // successfully called UndoTransaction() for the transaction item itself. michael@0: // Just redo any children that successfully called undo! michael@0: // michael@0: return RedoChildren(aTxMgr); michael@0: } michael@0: michael@0: nsresult michael@0: nsTransactionItem::RecoverFromRedoError(nsTransactionManager *aTxMgr) michael@0: { michael@0: // michael@0: // If this method gets called, we already successfully called michael@0: // RedoTransaction() for the transaction item itself. Undo all michael@0: // the children that successfully called RedoTransaction(), michael@0: // then undo the transaction item itself. michael@0: // michael@0: michael@0: nsresult result; michael@0: michael@0: result = UndoChildren(aTxMgr); michael@0: michael@0: if (NS_FAILED(result)) { michael@0: return result; michael@0: } michael@0: michael@0: if (!mTransaction) michael@0: return NS_OK; michael@0: michael@0: return mTransaction->UndoTransaction(); michael@0: } michael@0: