Line data Source code
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 "mozilla/mozalloc.h"
7 : #include "nsCOMPtr.h"
8 : #include "nsDebug.h"
9 : #include "nsError.h"
10 : #include "nsID.h"
11 : #include "nsISupportsUtils.h"
12 : #include "nsITransactionManager.h"
13 : #include "nsTransactionItem.h"
14 : #include "nsTransactionList.h"
15 : #include "nsTransactionStack.h"
16 : #include "nscore.h"
17 :
18 0 : NS_IMPL_ISUPPORTS(nsTransactionList, nsITransactionList)
19 :
20 0 : nsTransactionList::nsTransactionList(nsITransactionManager *aTxnMgr, nsTransactionStack *aTxnStack)
21 : : mTxnStack(aTxnStack)
22 0 : , mTxnItem(nullptr)
23 : {
24 0 : if (aTxnMgr)
25 0 : mTxnMgr = do_GetWeakReference(aTxnMgr);
26 0 : }
27 :
28 0 : nsTransactionList::nsTransactionList(nsITransactionManager *aTxnMgr, nsTransactionItem *aTxnItem)
29 : : mTxnStack(0)
30 0 : , mTxnItem(aTxnItem)
31 : {
32 0 : if (aTxnMgr)
33 0 : mTxnMgr = do_GetWeakReference(aTxnMgr);
34 0 : }
35 :
36 0 : nsTransactionList::~nsTransactionList()
37 : {
38 0 : mTxnStack = 0;
39 0 : mTxnItem = nullptr;
40 0 : }
41 :
42 0 : NS_IMETHODIMP nsTransactionList::GetNumItems(int32_t *aNumItems)
43 : {
44 0 : NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER);
45 :
46 0 : *aNumItems = 0;
47 :
48 0 : nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
49 0 : NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
50 :
51 0 : if (mTxnStack) {
52 0 : *aNumItems = mTxnStack->GetSize();
53 0 : } else if (mTxnItem) {
54 0 : return mTxnItem->GetNumberOfChildren(aNumItems);
55 : }
56 :
57 0 : return NS_OK;
58 : }
59 :
60 0 : NS_IMETHODIMP nsTransactionList::ItemIsBatch(int32_t aIndex, bool *aIsBatch)
61 : {
62 0 : NS_ENSURE_TRUE(aIsBatch, NS_ERROR_NULL_POINTER);
63 :
64 0 : *aIsBatch = false;
65 :
66 0 : nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
67 0 : NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
68 :
69 0 : RefPtr<nsTransactionItem> item;
70 0 : if (mTxnStack) {
71 0 : item = mTxnStack->GetItem(aIndex);
72 0 : } else if (mTxnItem) {
73 0 : nsresult rv = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
74 0 : NS_ENSURE_SUCCESS(rv, rv);
75 : }
76 0 : NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
77 :
78 0 : return item->GetIsBatch(aIsBatch);
79 : }
80 :
81 0 : NS_IMETHODIMP nsTransactionList::GetData(int32_t aIndex,
82 : uint32_t *aLength,
83 : nsISupports ***aData)
84 : {
85 0 : nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
86 0 : NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
87 :
88 0 : RefPtr<nsTransactionItem> item;
89 0 : if (mTxnStack) {
90 0 : item = mTxnStack->GetItem(aIndex);
91 0 : } else if (mTxnItem) {
92 0 : nsresult rv = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
93 0 : NS_ENSURE_SUCCESS(rv, rv);
94 : }
95 :
96 0 : nsCOMArray<nsISupports>& data = item->GetData();
97 0 : nsISupports** ret = static_cast<nsISupports**>(moz_xmalloc(data.Count() *
98 0 : sizeof(nsISupports*)));
99 :
100 0 : for (int32_t i = 0; i < data.Count(); i++) {
101 0 : NS_ADDREF(ret[i] = data[i]);
102 : }
103 0 : *aLength = data.Count();
104 0 : *aData = ret;
105 0 : return NS_OK;
106 : }
107 :
108 0 : NS_IMETHODIMP nsTransactionList::GetItem(int32_t aIndex, nsITransaction **aItem)
109 : {
110 0 : NS_ENSURE_TRUE(aItem, NS_ERROR_NULL_POINTER);
111 :
112 0 : *aItem = 0;
113 :
114 0 : nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
115 0 : NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
116 :
117 0 : RefPtr<nsTransactionItem> item;
118 0 : if (mTxnStack) {
119 0 : item = mTxnStack->GetItem(aIndex);
120 0 : } else if (mTxnItem) {
121 0 : nsresult rv = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
122 0 : NS_ENSURE_SUCCESS(rv, rv);
123 : }
124 0 : NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
125 :
126 0 : *aItem = item->GetTransaction().take();
127 0 : return NS_OK;
128 : }
129 :
130 0 : NS_IMETHODIMP nsTransactionList::GetNumChildrenForItem(int32_t aIndex, int32_t *aNumChildren)
131 : {
132 0 : NS_ENSURE_TRUE(aNumChildren, NS_ERROR_NULL_POINTER);
133 :
134 0 : *aNumChildren = 0;
135 :
136 0 : nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
137 0 : NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
138 :
139 0 : RefPtr<nsTransactionItem> item;
140 0 : if (mTxnStack) {
141 0 : item = mTxnStack->GetItem(aIndex);
142 0 : } else if (mTxnItem) {
143 0 : nsresult rv = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
144 0 : NS_ENSURE_SUCCESS(rv, rv);
145 : }
146 0 : NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
147 :
148 0 : return item->GetNumberOfChildren(aNumChildren);
149 : }
150 :
151 0 : NS_IMETHODIMP nsTransactionList::GetChildListForItem(int32_t aIndex, nsITransactionList **aTxnList)
152 : {
153 0 : NS_ENSURE_TRUE(aTxnList, NS_ERROR_NULL_POINTER);
154 :
155 0 : *aTxnList = 0;
156 :
157 0 : nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
158 0 : NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
159 :
160 0 : RefPtr<nsTransactionItem> item;
161 0 : if (mTxnStack) {
162 0 : item = mTxnStack->GetItem(aIndex);
163 0 : } else if (mTxnItem) {
164 0 : nsresult rv = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
165 0 : NS_ENSURE_SUCCESS(rv, rv);
166 : }
167 0 : NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
168 :
169 0 : *aTxnList = (nsITransactionList *)new nsTransactionList(txMgr, item);
170 0 : NS_ENSURE_TRUE(*aTxnList, NS_ERROR_OUT_OF_MEMORY);
171 :
172 0 : NS_ADDREF(*aTxnList);
173 0 : return NS_OK;
174 : }
|