2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "mozilla/mozalloc.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
2014-02-26 13:36:35 -08:00
|
|
|
#include "nsISupportsImpl.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsITransaction.h"
|
|
|
|
#include "nsTransactionItem.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsTransactionManager.h"
|
|
|
|
#include "nsTransactionStack.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsTransactionItem::nsTransactionItem(nsITransaction *aTransaction)
|
|
|
|
: mTransaction(aTransaction), mUndoStack(0), mRedoStack(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTransactionItem::~nsTransactionItem()
|
|
|
|
{
|
2011-05-17 07:01:36 -07:00
|
|
|
delete mRedoStack;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-17 07:01:36 -07:00
|
|
|
delete mUndoStack;
|
2008-12-10 08:46:36 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 10:30:58 -07:00
|
|
|
void
|
|
|
|
nsTransactionItem::CleanUp()
|
|
|
|
{
|
|
|
|
mData.Clear();
|
|
|
|
mTransaction = nullptr;
|
|
|
|
if (mRedoStack) {
|
|
|
|
mRedoStack->DoUnlink();
|
|
|
|
}
|
|
|
|
if (mUndoStack) {
|
|
|
|
mUndoStack->DoUnlink();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 09:50:06 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_NATIVE_ADDREF(nsTransactionItem)
|
2013-07-09 10:30:58 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE_WITH_LAST_RELEASE(nsTransactionItem,
|
|
|
|
CleanUp())
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsTransactionItem)
|
|
|
|
|
2012-11-22 09:15:38 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsTransactionItem)
|
2013-07-09 10:30:58 -07:00
|
|
|
tmp->CleanUp();
|
2009-05-08 21:59:25 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2012-11-22 09:15:38 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsTransactionItem)
|
2013-01-03 22:54:26 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mData)
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTransaction)
|
2009-05-08 21:59:25 -07:00
|
|
|
if (tmp->mRedoStack) {
|
|
|
|
tmp->mRedoStack->DoTraverse(cb);
|
|
|
|
}
|
|
|
|
if (tmp->mUndoStack) {
|
|
|
|
tmp->mUndoStack->DoTraverse(cb);
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(nsTransactionItem, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(nsTransactionItem, Release)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsTransactionItem::AddChild(nsTransactionItem *aTransactionItem)
|
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aTransactionItem, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mUndoStack) {
|
2012-03-02 12:08:40 -08:00
|
|
|
mUndoStack = new nsTransactionStack(nsTransactionStack::FOR_UNDO);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mUndoStack->Push(aTransactionItem);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
already_AddRefed<nsITransaction>
|
|
|
|
nsTransactionItem::GetTransaction()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-06-19 06:23:36 -07:00
|
|
|
nsCOMPtr<nsITransaction> txn = mTransaction;
|
|
|
|
return txn.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsTransactionItem::GetIsBatch(bool *aIsBatch)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aIsBatch, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aIsBatch = !mTransaction;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTransactionItem::GetNumberOfChildren(int32_t *aNumChildren)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aNumChildren, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aNumChildren = 0;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t ui = 0;
|
|
|
|
int32_t ri = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = GetNumberOfUndoItems(&ui);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = GetNumberOfRedoItems(&ri);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aNumChildren = ui + ri;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTransactionItem::GetChild(int32_t aIndex, nsTransactionItem **aChild)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aChild, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aChild = 0;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t numItems = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult result = GetNumberOfChildren(&numItems);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aIndex < 0 || aIndex >= numItems)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Children are expected to be in the order they were added,
|
|
|
|
// so the child first added would be at the bottom of the undo
|
|
|
|
// stack, or if there are no items on the undo stack, it would
|
|
|
|
// be at the top of the redo stack.
|
|
|
|
|
|
|
|
result = GetNumberOfUndoItems(&numItems);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (numItems > 0 && aIndex < numItems) {
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mUndoStack, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-02 12:08:40 -08:00
|
|
|
nsRefPtr<nsTransactionItem> child = mUndoStack->GetItem(aIndex);
|
|
|
|
child.forget(aChild);
|
|
|
|
return *aChild ? NS_OK : NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the index for the redo stack:
|
|
|
|
|
|
|
|
aIndex -= numItems;
|
|
|
|
|
|
|
|
result = GetNumberOfRedoItems(&numItems);
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(mRedoStack && numItems != 0 && aIndex < numItems, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-02 12:08:40 -08:00
|
|
|
nsRefPtr<nsTransactionItem> child = mRedoStack->GetItem(aIndex);
|
|
|
|
child.forget(aChild);
|
|
|
|
return *aChild ? NS_OK : NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTransactionItem::DoTransaction()
|
|
|
|
{
|
|
|
|
if (mTransaction)
|
|
|
|
return mTransaction->DoTransaction();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTransactionItem::UndoTransaction(nsTransactionManager *aTxMgr)
|
|
|
|
{
|
|
|
|
nsresult result = UndoChildren(aTxMgr);
|
|
|
|
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
RecoverFromUndoError(aTxMgr);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-07-31 10:58:35 -07:00
|
|
|
if (!mTransaction)
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = mTransaction->UndoTransaction();
|
|
|
|
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
RecoverFromUndoError(aTxMgr);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTransactionItem::UndoChildren(nsTransactionManager *aTxMgr)
|
|
|
|
{
|
2008-12-10 08:46:36 -08:00
|
|
|
nsRefPtr<nsTransactionItem> item;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult result = NS_OK;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t sz = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mUndoStack) {
|
|
|
|
if (!mRedoStack && mUndoStack) {
|
2012-03-02 12:08:40 -08:00
|
|
|
mRedoStack = new nsTransactionStack(nsTransactionStack::FOR_REDO);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Undo all of the transaction items children! */
|
2012-03-02 12:08:40 -08:00
|
|
|
sz = mUndoStack->GetSize();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
while (sz-- > 0) {
|
2012-03-02 12:08:40 -08:00
|
|
|
item = mUndoStack->Peek();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-02 12:08:40 -08:00
|
|
|
if (!item) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
nsCOMPtr<nsITransaction> t = item->GetTransaction();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool doInterrupt = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = aTxMgr->WillUndoNotify(t, &doInterrupt);
|
|
|
|
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doInterrupt) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = item->UndoTransaction(aTxMgr);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
2012-03-02 12:08:40 -08:00
|
|
|
item = mUndoStack->Pop();
|
|
|
|
mRedoStack->Push(item);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult result2 = aTxMgr->DidUndoNotify(t, result);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = result2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTransactionItem::RedoTransaction(nsTransactionManager *aTxMgr)
|
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
2008-12-10 08:46:36 -08:00
|
|
|
nsCOMPtr<nsITransaction> kungfuDeathGrip(mTransaction);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mTransaction) {
|
|
|
|
result = mTransaction->RedoTransaction();
|
|
|
|
|
2010-06-17 13:44:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
result = RedoChildren(aTxMgr);
|
|
|
|
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
RecoverFromRedoError(aTxMgr);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTransactionItem::RedoChildren(nsTransactionManager *aTxMgr)
|
|
|
|
{
|
2008-12-10 08:46:36 -08:00
|
|
|
nsRefPtr<nsTransactionItem> item;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
2010-06-18 16:29:16 -07:00
|
|
|
if (!mRedoStack)
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Redo all of the transaction items children! */
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t sz = mRedoStack->GetSize();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
while (sz-- > 0) {
|
2012-03-02 12:08:40 -08:00
|
|
|
item = mRedoStack->Peek();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-02 12:08:40 -08:00
|
|
|
if (!item) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-19 06:23:36 -07:00
|
|
|
nsCOMPtr<nsITransaction> t = item->GetTransaction();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool doInterrupt = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = aTxMgr->WillRedoNotify(t, &doInterrupt);
|
|
|
|
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doInterrupt) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = item->RedoTransaction(aTxMgr);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
2012-03-02 12:08:40 -08:00
|
|
|
item = mRedoStack->Pop();
|
|
|
|
mUndoStack->Push(item);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult result2 = aTxMgr->DidUndoNotify(t, result);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = result2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTransactionItem::GetNumberOfUndoItems(int32_t *aNumItems)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mUndoStack) {
|
|
|
|
*aNumItems = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-02 12:08:40 -08:00
|
|
|
*aNumItems = mUndoStack->GetSize();
|
|
|
|
return *aNumItems ? NS_OK : NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsTransactionItem::GetNumberOfRedoItems(int32_t *aNumItems)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 13:40:48 -07:00
|
|
|
NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mRedoStack) {
|
|
|
|
*aNumItems = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-02 12:08:40 -08:00
|
|
|
*aNumItems = mRedoStack->GetSize();
|
|
|
|
return *aNumItems ? NS_OK : NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTransactionItem::RecoverFromUndoError(nsTransactionManager *aTxMgr)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// 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!
|
|
|
|
//
|
|
|
|
return RedoChildren(aTxMgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTransactionItem::RecoverFromRedoError(nsTransactionManager *aTxMgr)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// 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 result;
|
|
|
|
|
|
|
|
result = UndoChildren(aTxMgr);
|
|
|
|
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-08-05 19:13:07 -07:00
|
|
|
if (!mTransaction)
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return mTransaction->UndoTransaction();
|
|
|
|
}
|
|
|
|
|