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/. */
5 
6 #include "TransactionStack.h"
7 
8 #include "nsCycleCollectionParticipant.h"
9 #include "nsDebug.h"
10 #include "nsISupportsUtils.h"
11 #include "nscore.h"
12 #include "TransactionItem.h"
13 
14 namespace mozilla {
15 
TransactionStack(Type aType)16 TransactionStack::TransactionStack(Type aType)
17     : nsRefPtrDeque<TransactionItem>(), mType(aType) {}
18 
~TransactionStack()19 TransactionStack::~TransactionStack() { Clear(); }
20 
Push(TransactionItem * aTransactionItem)21 void TransactionStack::Push(TransactionItem* aTransactionItem) {
22   if (NS_WARN_IF(!aTransactionItem)) {
23     return;
24   }
25   nsRefPtrDeque<TransactionItem>::Push(aTransactionItem);
26 }
27 
Push(already_AddRefed<TransactionItem> aTransactionItem)28 void TransactionStack::Push(
29     already_AddRefed<TransactionItem> aTransactionItem) {
30   TransactionItem* transactionItem = aTransactionItem.take();
31   if (NS_WARN_IF(!transactionItem)) {
32     return;
33   }
34   nsRefPtrDeque<TransactionItem>::Push(dont_AddRef(transactionItem));
35 }
36 
Pop()37 already_AddRefed<TransactionItem> TransactionStack::Pop() {
38   return nsRefPtrDeque<TransactionItem>::Pop();
39 }
40 
PopBottom()41 already_AddRefed<TransactionItem> TransactionStack::PopBottom() {
42   return nsRefPtrDeque<TransactionItem>::PopFront();
43 }
44 
Peek() const45 already_AddRefed<TransactionItem> TransactionStack::Peek() const {
46   RefPtr<TransactionItem> item = nsRefPtrDeque<TransactionItem>::Peek();
47   return item.forget();
48 }
49 
GetItemAt(size_t aIndex) const50 already_AddRefed<TransactionItem> TransactionStack::GetItemAt(
51     size_t aIndex) const {
52   RefPtr<TransactionItem> item =
53       nsRefPtrDeque<TransactionItem>::ObjectAt(aIndex);
54   return item.forget();
55 }
56 
Clear()57 void TransactionStack::Clear() {
58   while (GetSize()) {
59     RefPtr<TransactionItem> item = mType == FOR_UNDO ? Pop() : PopBottom();
60   }
61 }
62 
DoTraverse(nsCycleCollectionTraversalCallback & cb)63 void TransactionStack::DoTraverse(nsCycleCollectionTraversalCallback& cb) {
64   size_t size = GetSize();
65   for (size_t i = 0; i < size; ++i) {
66     auto* item = nsRefPtrDeque<TransactionItem>::ObjectAt(i);
67     if (item) {
68       NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "transaction stack mDeque[i]");
69       cb.NoteNativeChild(item,
70                          NS_CYCLE_COLLECTION_PARTICIPANT(TransactionItem));
71     }
72   }
73 }
74 
75 }  // namespace mozilla
76