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/mozalloc.h"
11 #include "nsISupportsUtils.h"
12 #include "nsITransactionManager.h"
13 #include "nsTransactionItem.h"
14 #include "nsTransactionList.h"
15 #include "nsTransactionStack.h"
18 NS_IMPL_ISUPPORTS(nsTransactionList
, nsITransactionList
)
20 nsTransactionList::nsTransactionList(nsITransactionManager
*aTxnMgr
, nsTransactionStack
*aTxnStack
)
21 : mTxnStack(aTxnStack
)
25 mTxnMgr
= do_GetWeakReference(aTxnMgr
);
28 nsTransactionList::nsTransactionList(nsITransactionManager
*aTxnMgr
, nsTransactionItem
*aTxnItem
)
33 mTxnMgr
= do_GetWeakReference(aTxnMgr
);
36 nsTransactionList::~nsTransactionList()
42 /* readonly attribute long numItems; */
43 NS_IMETHODIMP
nsTransactionList::GetNumItems(int32_t *aNumItems
)
45 NS_ENSURE_TRUE(aNumItems
, NS_ERROR_NULL_POINTER
);
49 nsCOMPtr
<nsITransactionManager
> txMgr
= do_QueryReferent(mTxnMgr
);
51 NS_ENSURE_TRUE(txMgr
, NS_ERROR_FAILURE
);
53 nsresult result
= NS_OK
;
56 *aNumItems
= mTxnStack
->GetSize();
58 result
= mTxnItem
->GetNumberOfChildren(aNumItems
);
63 /* boolean itemIsBatch (in long aIndex); */
64 NS_IMETHODIMP
nsTransactionList::ItemIsBatch(int32_t aIndex
, bool *aIsBatch
)
66 NS_ENSURE_TRUE(aIsBatch
, NS_ERROR_NULL_POINTER
);
70 nsCOMPtr
<nsITransactionManager
> txMgr
= do_QueryReferent(mTxnMgr
);
72 NS_ENSURE_TRUE(txMgr
, NS_ERROR_FAILURE
);
74 nsRefPtr
<nsTransactionItem
> item
;
76 nsresult result
= NS_OK
;
79 item
= mTxnStack
->GetItem(aIndex
);
81 result
= mTxnItem
->GetChild(aIndex
, getter_AddRefs(item
));
83 NS_ENSURE_SUCCESS(result
, result
);
85 NS_ENSURE_TRUE(item
, NS_ERROR_FAILURE
);
87 return item
->GetIsBatch(aIsBatch
);
90 /* void getData (in long aIndex,
91 [optional] out unsigned long aLength,
92 [array, size_is (aLength), retval]
93 out nsISupports aData); */
94 NS_IMETHODIMP
nsTransactionList::GetData(int32_t aIndex
,
98 nsCOMPtr
<nsITransactionManager
> txMgr
= do_QueryReferent(mTxnMgr
);
100 NS_ENSURE_TRUE(txMgr
, NS_ERROR_FAILURE
);
102 nsRefPtr
<nsTransactionItem
> item
;
105 item
= mTxnStack
->GetItem(aIndex
);
106 } else if (mTxnItem
) {
107 nsresult result
= mTxnItem
->GetChild(aIndex
, getter_AddRefs(item
));
108 NS_ENSURE_SUCCESS(result
, result
);
111 nsCOMArray
<nsISupports
>& data
= item
->GetData();
113 nsISupports
** ret
= static_cast<nsISupports
**>(NS_Alloc(data
.Count() *
114 sizeof(nsISupports
*)));
116 for (int32_t i
= 0; i
< data
.Count(); i
++) {
117 NS_ADDREF(ret
[i
] = data
[i
]);
120 *aLength
= data
.Count();
126 /* nsITransaction getItem (in long aIndex); */
127 NS_IMETHODIMP
nsTransactionList::GetItem(int32_t aIndex
, nsITransaction
**aItem
)
129 NS_ENSURE_TRUE(aItem
, NS_ERROR_NULL_POINTER
);
133 nsCOMPtr
<nsITransactionManager
> txMgr
= do_QueryReferent(mTxnMgr
);
135 NS_ENSURE_TRUE(txMgr
, NS_ERROR_FAILURE
);
137 nsRefPtr
<nsTransactionItem
> item
;
139 nsresult result
= NS_OK
;
142 item
= mTxnStack
->GetItem(aIndex
);
144 result
= mTxnItem
->GetChild(aIndex
, getter_AddRefs(item
));
146 NS_ENSURE_SUCCESS(result
, result
);
148 NS_ENSURE_TRUE(item
, NS_ERROR_FAILURE
);
150 *aItem
= item
->GetTransaction().take();
155 /* long getNumChildrenForItem (in long aIndex); */
156 NS_IMETHODIMP
nsTransactionList::GetNumChildrenForItem(int32_t aIndex
, int32_t *aNumChildren
)
158 NS_ENSURE_TRUE(aNumChildren
, NS_ERROR_NULL_POINTER
);
162 nsCOMPtr
<nsITransactionManager
> txMgr
= do_QueryReferent(mTxnMgr
);
164 NS_ENSURE_TRUE(txMgr
, NS_ERROR_FAILURE
);
166 nsRefPtr
<nsTransactionItem
> item
;
168 nsresult result
= NS_OK
;
171 item
= mTxnStack
->GetItem(aIndex
);
173 result
= mTxnItem
->GetChild(aIndex
, getter_AddRefs(item
));
175 NS_ENSURE_SUCCESS(result
, result
);
177 NS_ENSURE_TRUE(item
, NS_ERROR_FAILURE
);
179 return item
->GetNumberOfChildren(aNumChildren
);
182 /* nsITransactionList getChildListForItem (in long aIndex); */
183 NS_IMETHODIMP
nsTransactionList::GetChildListForItem(int32_t aIndex
, nsITransactionList
**aTxnList
)
185 NS_ENSURE_TRUE(aTxnList
, NS_ERROR_NULL_POINTER
);
189 nsCOMPtr
<nsITransactionManager
> txMgr
= do_QueryReferent(mTxnMgr
);
191 NS_ENSURE_TRUE(txMgr
, NS_ERROR_FAILURE
);
193 nsRefPtr
<nsTransactionItem
> item
;
195 nsresult result
= NS_OK
;
198 item
= mTxnStack
->GetItem(aIndex
);
200 result
= mTxnItem
->GetChild(aIndex
, getter_AddRefs(item
));
202 NS_ENSURE_SUCCESS(result
, result
);
204 NS_ENSURE_TRUE(item
, NS_ERROR_FAILURE
);
206 *aTxnList
= (nsITransactionList
*)new nsTransactionList(txMgr
, item
);
208 NS_ENSURE_TRUE(*aTxnList
, NS_ERROR_OUT_OF_MEMORY
);
210 NS_ADDREF(*aTxnList
);