DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (290d570566d6)

VCS Links

Line Code
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
/* -*- 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/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

NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(TransactionItem, AddRef)
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(TransactionItem, Release)

nsresult TransactionItem::AddChild(TransactionItem* aTransactionItem) {
  NS_ENSURE_TRUE(aTransactionItem, NS_ERROR_NULL_POINTER);

  if (!mUndoStack) {
    mUndoStack = new TransactionStack(TransactionStack::FOR_UNDO);
  }

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

already_AddRefed<nsITransaction> TransactionItem::GetTransaction() {
  nsCOMPtr<nsITransaction> txn = mTransaction;
  return txn.forget();
}

nsresult TransactionItem::GetIsBatch(bool* aIsBatch) {
  NS_ENSURE_TRUE(aIsBatch, NS_ERROR_NULL_POINTER);
  *aIsBatch = !mTransaction;
  return NS_OK;
}

nsresult TransactionItem::GetNumberOfChildren(int32_t* aNumChildren) {
  NS_ENSURE_TRUE(aNumChildren, NS_ERROR_NULL_POINTER);

  *aNumChildren = 0;

  int32_t ui = 0;
  nsresult rv = GetNumberOfUndoItems(&ui);
  NS_ENSURE_SUCCESS(rv, rv);

  int32_t ri = 0;
  rv = GetNumberOfRedoItems(&ri);
  NS_ENSURE_SUCCESS(rv, rv);

  *aNumChildren = ui + ri;
  return NS_OK;
}

nsresult TransactionItem::GetChild(int32_t aIndex, TransactionItem** aChild) {
  NS_ENSURE_TRUE(aChild, NS_ERROR_NULL_POINTER);

  *aChild = 0;

  int32_t numItems = 0;
  nsresult rv = GetNumberOfChildren(&numItems);
  NS_ENSURE_SUCCESS(rv, rv);
  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.
  rv = GetNumberOfUndoItems(&numItems);
  NS_ENSURE_SUCCESS(rv, rv);

  if (numItems > 0 && aIndex < numItems) {
    NS_ENSURE_TRUE(mUndoStack, NS_ERROR_FAILURE);

    RefPtr<TransactionItem> child = mUndoStack->GetItem(aIndex);
    child.forget(aChild);
    return *aChild ? NS_OK : NS_ERROR_FAILURE;
  }

  // Adjust the index for the redo stack:
  aIndex -= numItems;

  rv = GetNumberOfRedoItems(&numItems);
  NS_ENSURE_SUCCESS(rv, rv);
  NS_ENSURE_TRUE(mRedoStack && numItems != 0 && aIndex < numItems,
                 NS_ERROR_FAILURE);

  RefPtr<TransactionItem> child = mRedoStack->GetItem(aIndex);
  child.forget(aChild);
  return *aChild ? NS_OK : NS_ERROR_FAILURE;
}

nsresult TransactionItem::DoTransaction() {
  if (mTransaction) {
    return mTransaction->DoTransaction();
  }
  return NS_OK;
}

nsresult TransactionItem::UndoTransaction(
    TransactionManager* aTransactionManager) {
  nsresult rv = UndoChildren(aTransactionManager);
  if (NS_FAILED(rv)) {
    RecoverFromUndoError(aTransactionManager);
    return rv;
  }

  if (!mTransaction) {
    return NS_OK;
  }

  rv = mTransaction->UndoTransaction();
  if (NS_FAILED(rv)) {
    RecoverFromUndoError(aTransactionManager);
    return rv;
  }

  return NS_OK;
}

nsresult TransactionItem::UndoChildren(
    TransactionManager* aTransactionManager) {
  if (mUndoStack) {
    if (!mRedoStack && mUndoStack) {
      mRedoStack = new TransactionStack(TransactionStack::FOR_REDO);
    }

    /* Undo all of the transaction items children! */
    int32_t sz = mUndoStack->GetSize();

    nsresult rv = NS_OK;
    while (sz-- > 0) {
      RefPtr<TransactionItem> transactionItem = mUndoStack->Peek();
      if (!transactionItem) {
        return NS_ERROR_FAILURE;
      }

      nsCOMPtr<nsITransaction> transaction = transactionItem->GetTransaction();
      bool doInterrupt = false;
      rv = aTransactionManager->WillUndoNotify(transaction, &doInterrupt);
      if (NS_FAILED(rv)) {
        return rv;
      }
      if (doInterrupt) {
        return NS_OK;
      }

      rv = transactionItem->UndoTransaction(aTransactionManager);
      if (NS_SUCCEEDED(rv)) {
        transactionItem = mUndoStack->Pop();
        mRedoStack->Push(transactionItem.forget());
      }

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

  return NS_OK;
}

nsresult TransactionItem::RedoTransaction(
    TransactionManager* aTransactionManager) {
  nsCOMPtr<nsITransaction> transaction(mTransaction);
  if (transaction) {
    nsresult rv = transaction->RedoTransaction();
    NS_ENSURE_SUCCESS(rv, rv);
  }

  nsresult rv = RedoChildren(aTransactionManager);
  if (NS_FAILED(rv)) {
    RecoverFromRedoError(aTransactionManager);
    return rv;
  }

  return NS_OK;
}

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

  /* Redo all of the transaction items children! */
  int32_t sz = mRedoStack->GetSize();

  nsresult rv = NS_OK;
  while (sz-- > 0) {
    RefPtr<TransactionItem> transactionItem = mRedoStack->Peek();
    if (!transactionItem) {
      return NS_ERROR_FAILURE;
    }

    nsCOMPtr<nsITransaction> transaction = transactionItem->GetTransaction();
    bool doInterrupt = false;
    rv = aTransactionManager->WillRedoNotify(transaction, &doInterrupt);
    if (NS_FAILED(rv)) {
      return rv;
    }
    if (doInterrupt) {
      return NS_OK;
    }

    rv = transactionItem->RedoTransaction(aTransactionManager);
    if (NS_SUCCEEDED(rv)) {
      transactionItem = mRedoStack->Pop();
      mUndoStack->Push(transactionItem.forget());
    }

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

nsresult TransactionItem::GetNumberOfUndoItems(int32_t* aNumItems) {
  NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER);

  if (!mUndoStack) {
    *aNumItems = 0;
    return NS_OK;
  }

  *aNumItems = mUndoStack->GetSize();
  return *aNumItems ? NS_OK : NS_ERROR_FAILURE;
}

nsresult TransactionItem::GetNumberOfRedoItems(int32_t* aNumItems) {
  NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER);

  if (!mRedoStack) {
    *aNumItems = 0;
    return NS_OK;
  }

  *aNumItems = mRedoStack->GetSize();
  return *aNumItems ? NS_OK : NS_ERROR_FAILURE;
}

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!
  return RedoChildren(aTransactionManager);
}

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)) {
    return rv;
  }

  if (!mTransaction) {
    return NS_OK;
  }

  return mTransaction->UndoTransaction();
}

}  // namespace mozilla