|
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/Assertions.h" |
|
7 #include "mozilla/mozalloc.h" |
|
8 #include "nsAutoPtr.h" |
|
9 #include "nsCOMPtr.h" |
|
10 #include "nsDebug.h" |
|
11 #include "nsError.h" |
|
12 #include "nsISupportsBase.h" |
|
13 #include "nsISupportsUtils.h" |
|
14 #include "nsITransaction.h" |
|
15 #include "nsITransactionList.h" |
|
16 #include "nsITransactionListener.h" |
|
17 #include "nsIWeakReference.h" |
|
18 #include "nsTransactionItem.h" |
|
19 #include "nsTransactionList.h" |
|
20 #include "nsTransactionManager.h" |
|
21 #include "nsTransactionStack.h" |
|
22 |
|
23 nsTransactionManager::nsTransactionManager(int32_t aMaxTransactionCount) |
|
24 : mMaxTransactionCount(aMaxTransactionCount) |
|
25 , mDoStack(nsTransactionStack::FOR_UNDO) |
|
26 , mUndoStack(nsTransactionStack::FOR_UNDO) |
|
27 , mRedoStack(nsTransactionStack::FOR_REDO) |
|
28 { |
|
29 } |
|
30 |
|
31 nsTransactionManager::~nsTransactionManager() |
|
32 { |
|
33 } |
|
34 |
|
35 NS_IMPL_CYCLE_COLLECTION_CLASS(nsTransactionManager) |
|
36 |
|
37 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsTransactionManager) |
|
38 NS_IMPL_CYCLE_COLLECTION_UNLINK(mListeners) |
|
39 tmp->mDoStack.DoUnlink(); |
|
40 tmp->mUndoStack.DoUnlink(); |
|
41 tmp->mRedoStack.DoUnlink(); |
|
42 NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
|
43 |
|
44 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsTransactionManager) |
|
45 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mListeners) |
|
46 tmp->mDoStack.DoTraverse(cb); |
|
47 tmp->mUndoStack.DoTraverse(cb); |
|
48 tmp->mRedoStack.DoTraverse(cb); |
|
49 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
|
50 |
|
51 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsTransactionManager) |
|
52 NS_INTERFACE_MAP_ENTRY(nsITransactionManager) |
|
53 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) |
|
54 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsITransactionManager) |
|
55 NS_INTERFACE_MAP_END |
|
56 |
|
57 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsTransactionManager) |
|
58 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsTransactionManager) |
|
59 |
|
60 NS_IMETHODIMP |
|
61 nsTransactionManager::DoTransaction(nsITransaction *aTransaction) |
|
62 { |
|
63 nsresult result; |
|
64 |
|
65 NS_ENSURE_TRUE(aTransaction, NS_ERROR_NULL_POINTER); |
|
66 |
|
67 bool doInterrupt = false; |
|
68 |
|
69 result = WillDoNotify(aTransaction, &doInterrupt); |
|
70 |
|
71 if (NS_FAILED(result)) { |
|
72 return result; |
|
73 } |
|
74 |
|
75 if (doInterrupt) { |
|
76 return NS_OK; |
|
77 } |
|
78 |
|
79 result = BeginTransaction(aTransaction, nullptr); |
|
80 |
|
81 if (NS_FAILED(result)) { |
|
82 DidDoNotify(aTransaction, result); |
|
83 return result; |
|
84 } |
|
85 |
|
86 result = EndTransaction(false); |
|
87 |
|
88 nsresult result2 = DidDoNotify(aTransaction, result); |
|
89 |
|
90 if (NS_SUCCEEDED(result)) |
|
91 result = result2; |
|
92 |
|
93 return result; |
|
94 } |
|
95 |
|
96 NS_IMETHODIMP |
|
97 nsTransactionManager::UndoTransaction() |
|
98 { |
|
99 nsresult result = NS_OK; |
|
100 |
|
101 // It is illegal to call UndoTransaction() while the transaction manager is |
|
102 // executing a transaction's DoTransaction() method! If this happens, |
|
103 // the UndoTransaction() request is ignored, and we return NS_ERROR_FAILURE. |
|
104 |
|
105 nsRefPtr<nsTransactionItem> tx = mDoStack.Peek(); |
|
106 |
|
107 if (tx) { |
|
108 return NS_ERROR_FAILURE; |
|
109 } |
|
110 |
|
111 // Peek at the top of the undo stack. Don't remove the transaction |
|
112 // until it has successfully completed. |
|
113 tx = mUndoStack.Peek(); |
|
114 |
|
115 // Bail if there's nothing on the stack. |
|
116 if (!tx) { |
|
117 return NS_OK; |
|
118 } |
|
119 |
|
120 nsCOMPtr<nsITransaction> t = tx->GetTransaction(); |
|
121 |
|
122 bool doInterrupt = false; |
|
123 |
|
124 result = WillUndoNotify(t, &doInterrupt); |
|
125 |
|
126 if (NS_FAILED(result)) { |
|
127 return result; |
|
128 } |
|
129 |
|
130 if (doInterrupt) { |
|
131 return NS_OK; |
|
132 } |
|
133 |
|
134 result = tx->UndoTransaction(this); |
|
135 |
|
136 if (NS_SUCCEEDED(result)) { |
|
137 tx = mUndoStack.Pop(); |
|
138 mRedoStack.Push(tx); |
|
139 } |
|
140 |
|
141 nsresult result2 = DidUndoNotify(t, result); |
|
142 |
|
143 if (NS_SUCCEEDED(result)) |
|
144 result = result2; |
|
145 |
|
146 return result; |
|
147 } |
|
148 |
|
149 NS_IMETHODIMP |
|
150 nsTransactionManager::RedoTransaction() |
|
151 { |
|
152 nsresult result = NS_OK; |
|
153 |
|
154 // It is illegal to call RedoTransaction() while the transaction manager is |
|
155 // executing a transaction's DoTransaction() method! If this happens, |
|
156 // the RedoTransaction() request is ignored, and we return NS_ERROR_FAILURE. |
|
157 |
|
158 nsRefPtr<nsTransactionItem> tx = mDoStack.Peek(); |
|
159 |
|
160 if (tx) { |
|
161 return NS_ERROR_FAILURE; |
|
162 } |
|
163 |
|
164 // Peek at the top of the redo stack. Don't remove the transaction |
|
165 // until it has successfully completed. |
|
166 tx = mRedoStack.Peek(); |
|
167 |
|
168 // Bail if there's nothing on the stack. |
|
169 if (!tx) { |
|
170 return NS_OK; |
|
171 } |
|
172 |
|
173 nsCOMPtr<nsITransaction> t = tx->GetTransaction(); |
|
174 |
|
175 bool doInterrupt = false; |
|
176 |
|
177 result = WillRedoNotify(t, &doInterrupt); |
|
178 |
|
179 if (NS_FAILED(result)) { |
|
180 return result; |
|
181 } |
|
182 |
|
183 if (doInterrupt) { |
|
184 return NS_OK; |
|
185 } |
|
186 |
|
187 result = tx->RedoTransaction(this); |
|
188 |
|
189 if (NS_SUCCEEDED(result)) { |
|
190 tx = mRedoStack.Pop(); |
|
191 mUndoStack.Push(tx); |
|
192 } |
|
193 |
|
194 nsresult result2 = DidRedoNotify(t, result); |
|
195 |
|
196 if (NS_SUCCEEDED(result)) |
|
197 result = result2; |
|
198 |
|
199 return result; |
|
200 } |
|
201 |
|
202 NS_IMETHODIMP |
|
203 nsTransactionManager::Clear() |
|
204 { |
|
205 nsresult result; |
|
206 |
|
207 result = ClearRedoStack(); |
|
208 |
|
209 if (NS_FAILED(result)) { |
|
210 return result; |
|
211 } |
|
212 |
|
213 result = ClearUndoStack(); |
|
214 |
|
215 return result; |
|
216 } |
|
217 |
|
218 NS_IMETHODIMP |
|
219 nsTransactionManager::BeginBatch(nsISupports* aData) |
|
220 { |
|
221 nsresult result; |
|
222 |
|
223 // We can batch independent transactions together by simply pushing |
|
224 // a dummy transaction item on the do stack. This dummy transaction item |
|
225 // will be popped off the do stack, and then pushed on the undo stack |
|
226 // in EndBatch(). |
|
227 |
|
228 bool doInterrupt = false; |
|
229 |
|
230 result = WillBeginBatchNotify(&doInterrupt); |
|
231 |
|
232 if (NS_FAILED(result)) { |
|
233 return result; |
|
234 } |
|
235 |
|
236 if (doInterrupt) { |
|
237 return NS_OK; |
|
238 } |
|
239 |
|
240 result = BeginTransaction(0, aData); |
|
241 |
|
242 nsresult result2 = DidBeginBatchNotify(result); |
|
243 |
|
244 if (NS_SUCCEEDED(result)) |
|
245 result = result2; |
|
246 |
|
247 return result; |
|
248 } |
|
249 |
|
250 NS_IMETHODIMP |
|
251 nsTransactionManager::EndBatch(bool aAllowEmpty) |
|
252 { |
|
253 nsCOMPtr<nsITransaction> ti; |
|
254 nsresult result; |
|
255 |
|
256 // XXX: Need to add some mechanism to detect the case where the transaction |
|
257 // at the top of the do stack isn't the dummy transaction, so we can |
|
258 // throw an error!! This can happen if someone calls EndBatch() within |
|
259 // the DoTransaction() method of a transaction. |
|
260 // |
|
261 // For now, we can detect this case by checking the value of the |
|
262 // dummy transaction's mTransaction field. If it is our dummy |
|
263 // transaction, it should be nullptr. This may not be true in the |
|
264 // future when we allow users to execute a transaction when beginning |
|
265 // a batch!!!! |
|
266 |
|
267 nsRefPtr<nsTransactionItem> tx = mDoStack.Peek(); |
|
268 |
|
269 if (tx) { |
|
270 ti = tx->GetTransaction(); |
|
271 } |
|
272 |
|
273 if (!tx || ti) { |
|
274 return NS_ERROR_FAILURE; |
|
275 } |
|
276 |
|
277 bool doInterrupt = false; |
|
278 |
|
279 result = WillEndBatchNotify(&doInterrupt); |
|
280 |
|
281 if (NS_FAILED(result)) { |
|
282 return result; |
|
283 } |
|
284 |
|
285 if (doInterrupt) { |
|
286 return NS_OK; |
|
287 } |
|
288 |
|
289 result = EndTransaction(aAllowEmpty); |
|
290 |
|
291 nsresult result2 = DidEndBatchNotify(result); |
|
292 |
|
293 if (NS_SUCCEEDED(result)) |
|
294 result = result2; |
|
295 |
|
296 return result; |
|
297 } |
|
298 |
|
299 NS_IMETHODIMP |
|
300 nsTransactionManager::GetNumberOfUndoItems(int32_t *aNumItems) |
|
301 { |
|
302 *aNumItems = mUndoStack.GetSize(); |
|
303 return NS_OK; |
|
304 } |
|
305 |
|
306 NS_IMETHODIMP |
|
307 nsTransactionManager::GetNumberOfRedoItems(int32_t *aNumItems) |
|
308 { |
|
309 *aNumItems = mRedoStack.GetSize(); |
|
310 return NS_OK; |
|
311 } |
|
312 |
|
313 NS_IMETHODIMP |
|
314 nsTransactionManager::GetMaxTransactionCount(int32_t *aMaxCount) |
|
315 { |
|
316 NS_ENSURE_TRUE(aMaxCount, NS_ERROR_NULL_POINTER); |
|
317 |
|
318 *aMaxCount = mMaxTransactionCount; |
|
319 |
|
320 return NS_OK; |
|
321 } |
|
322 |
|
323 NS_IMETHODIMP |
|
324 nsTransactionManager::SetMaxTransactionCount(int32_t aMaxCount) |
|
325 { |
|
326 int32_t numUndoItems = 0, numRedoItems = 0, total = 0; |
|
327 |
|
328 // It is illegal to call SetMaxTransactionCount() while the transaction |
|
329 // manager is executing a transaction's DoTransaction() method because |
|
330 // the undo and redo stacks might get pruned! If this happens, the |
|
331 // SetMaxTransactionCount() request is ignored, and we return |
|
332 // NS_ERROR_FAILURE. |
|
333 |
|
334 nsRefPtr<nsTransactionItem> tx = mDoStack.Peek(); |
|
335 |
|
336 if (tx) { |
|
337 return NS_ERROR_FAILURE; |
|
338 } |
|
339 |
|
340 // If aMaxCount is less than zero, the user wants unlimited |
|
341 // levels of undo! No need to prune the undo or redo stacks! |
|
342 |
|
343 if (aMaxCount < 0) { |
|
344 mMaxTransactionCount = -1; |
|
345 return NS_OK; |
|
346 } |
|
347 |
|
348 numUndoItems = mUndoStack.GetSize(); |
|
349 |
|
350 numRedoItems = mRedoStack.GetSize(); |
|
351 |
|
352 total = numUndoItems + numRedoItems; |
|
353 |
|
354 // If aMaxCount is greater than the number of transactions that currently |
|
355 // exist on the undo and redo stack, there is no need to prune the |
|
356 // undo or redo stacks! |
|
357 |
|
358 if (aMaxCount > total ) { |
|
359 mMaxTransactionCount = aMaxCount; |
|
360 return NS_OK; |
|
361 } |
|
362 |
|
363 // Try getting rid of some transactions on the undo stack! Start at |
|
364 // the bottom of the stack and pop towards the top. |
|
365 |
|
366 while (numUndoItems > 0 && (numRedoItems + numUndoItems) > aMaxCount) { |
|
367 tx = mUndoStack.PopBottom(); |
|
368 |
|
369 if (!tx) { |
|
370 return NS_ERROR_FAILURE; |
|
371 } |
|
372 |
|
373 --numUndoItems; |
|
374 } |
|
375 |
|
376 // If necessary, get rid of some transactions on the redo stack! Start at |
|
377 // the bottom of the stack and pop towards the top. |
|
378 |
|
379 while (numRedoItems > 0 && (numRedoItems + numUndoItems) > aMaxCount) { |
|
380 tx = mRedoStack.PopBottom(); |
|
381 |
|
382 if (!tx) { |
|
383 return NS_ERROR_FAILURE; |
|
384 } |
|
385 |
|
386 --numRedoItems; |
|
387 } |
|
388 |
|
389 mMaxTransactionCount = aMaxCount; |
|
390 |
|
391 return NS_OK; |
|
392 } |
|
393 |
|
394 NS_IMETHODIMP |
|
395 nsTransactionManager::PeekUndoStack(nsITransaction **aTransaction) |
|
396 { |
|
397 MOZ_ASSERT(aTransaction); |
|
398 *aTransaction = PeekUndoStack().take(); |
|
399 return NS_OK; |
|
400 } |
|
401 |
|
402 already_AddRefed<nsITransaction> |
|
403 nsTransactionManager::PeekUndoStack() |
|
404 { |
|
405 nsRefPtr<nsTransactionItem> tx = mUndoStack.Peek(); |
|
406 |
|
407 if (!tx) { |
|
408 return nullptr; |
|
409 } |
|
410 |
|
411 return tx->GetTransaction(); |
|
412 } |
|
413 |
|
414 NS_IMETHODIMP |
|
415 nsTransactionManager::PeekRedoStack(nsITransaction** aTransaction) |
|
416 { |
|
417 MOZ_ASSERT(aTransaction); |
|
418 *aTransaction = PeekRedoStack().take(); |
|
419 return NS_OK; |
|
420 } |
|
421 |
|
422 already_AddRefed<nsITransaction> |
|
423 nsTransactionManager::PeekRedoStack() |
|
424 { |
|
425 nsRefPtr<nsTransactionItem> tx = mRedoStack.Peek(); |
|
426 |
|
427 if (!tx) { |
|
428 return nullptr; |
|
429 } |
|
430 |
|
431 return tx->GetTransaction(); |
|
432 } |
|
433 |
|
434 NS_IMETHODIMP |
|
435 nsTransactionManager::GetUndoList(nsITransactionList **aTransactionList) |
|
436 { |
|
437 NS_ENSURE_TRUE(aTransactionList, NS_ERROR_NULL_POINTER); |
|
438 |
|
439 *aTransactionList = (nsITransactionList *)new nsTransactionList(this, &mUndoStack); |
|
440 |
|
441 NS_IF_ADDREF(*aTransactionList); |
|
442 |
|
443 return (! *aTransactionList) ? NS_ERROR_OUT_OF_MEMORY : NS_OK; |
|
444 } |
|
445 |
|
446 NS_IMETHODIMP |
|
447 nsTransactionManager::GetRedoList(nsITransactionList **aTransactionList) |
|
448 { |
|
449 NS_ENSURE_TRUE(aTransactionList, NS_ERROR_NULL_POINTER); |
|
450 |
|
451 *aTransactionList = (nsITransactionList *)new nsTransactionList(this, &mRedoStack); |
|
452 |
|
453 NS_IF_ADDREF(*aTransactionList); |
|
454 |
|
455 return (! *aTransactionList) ? NS_ERROR_OUT_OF_MEMORY : NS_OK; |
|
456 } |
|
457 |
|
458 nsresult |
|
459 nsTransactionManager::BatchTopUndo() |
|
460 { |
|
461 if (mUndoStack.GetSize() < 2) { |
|
462 // Not enough transactions to merge into one batch. |
|
463 return NS_OK; |
|
464 } |
|
465 |
|
466 nsRefPtr<nsTransactionItem> lastUndo; |
|
467 nsRefPtr<nsTransactionItem> previousUndo; |
|
468 |
|
469 lastUndo = mUndoStack.Pop(); |
|
470 MOZ_ASSERT(lastUndo, "There should be at least two transactions."); |
|
471 |
|
472 previousUndo = mUndoStack.Peek(); |
|
473 MOZ_ASSERT(previousUndo, "There should be at least two transactions."); |
|
474 |
|
475 nsresult result = previousUndo->AddChild(lastUndo); |
|
476 |
|
477 // Transfer data from the transactions that is going to be |
|
478 // merged to the transaction that it is being merged with. |
|
479 nsCOMArray<nsISupports>& lastData = lastUndo->GetData(); |
|
480 nsCOMArray<nsISupports>& previousData = previousUndo->GetData(); |
|
481 NS_ENSURE_TRUE(previousData.AppendObjects(lastData), NS_ERROR_UNEXPECTED); |
|
482 lastData.Clear(); |
|
483 |
|
484 return result; |
|
485 } |
|
486 |
|
487 nsresult |
|
488 nsTransactionManager::RemoveTopUndo() |
|
489 { |
|
490 nsRefPtr<nsTransactionItem> lastUndo; |
|
491 |
|
492 lastUndo = mUndoStack.Peek(); |
|
493 if (!lastUndo) { |
|
494 return NS_OK; |
|
495 } |
|
496 |
|
497 lastUndo = mUndoStack.Pop(); |
|
498 |
|
499 return NS_OK; |
|
500 } |
|
501 |
|
502 NS_IMETHODIMP |
|
503 nsTransactionManager::AddListener(nsITransactionListener *aListener) |
|
504 { |
|
505 NS_ENSURE_TRUE(aListener, NS_ERROR_NULL_POINTER); |
|
506 |
|
507 return mListeners.AppendObject(aListener) ? NS_OK : NS_ERROR_FAILURE; |
|
508 } |
|
509 |
|
510 NS_IMETHODIMP |
|
511 nsTransactionManager::RemoveListener(nsITransactionListener *aListener) |
|
512 { |
|
513 NS_ENSURE_TRUE(aListener, NS_ERROR_NULL_POINTER); |
|
514 |
|
515 return mListeners.RemoveObject(aListener) ? NS_OK : NS_ERROR_FAILURE; |
|
516 } |
|
517 |
|
518 NS_IMETHODIMP |
|
519 nsTransactionManager::ClearUndoStack() |
|
520 { |
|
521 mUndoStack.Clear(); |
|
522 return NS_OK; |
|
523 } |
|
524 |
|
525 NS_IMETHODIMP |
|
526 nsTransactionManager::ClearRedoStack() |
|
527 { |
|
528 mRedoStack.Clear(); |
|
529 return NS_OK; |
|
530 } |
|
531 |
|
532 nsresult |
|
533 nsTransactionManager::WillDoNotify(nsITransaction *aTransaction, bool *aInterrupt) |
|
534 { |
|
535 nsresult result = NS_OK; |
|
536 for (int32_t i = 0, lcount = mListeners.Count(); i < lcount; i++) |
|
537 { |
|
538 nsITransactionListener *listener = mListeners[i]; |
|
539 |
|
540 NS_ENSURE_TRUE(listener, NS_ERROR_FAILURE); |
|
541 |
|
542 result = listener->WillDo(this, aTransaction, aInterrupt); |
|
543 |
|
544 if (NS_FAILED(result) || *aInterrupt) |
|
545 break; |
|
546 } |
|
547 |
|
548 return result; |
|
549 } |
|
550 |
|
551 nsresult |
|
552 nsTransactionManager::DidDoNotify(nsITransaction *aTransaction, nsresult aDoResult) |
|
553 { |
|
554 nsresult result = NS_OK; |
|
555 for (int32_t i = 0, lcount = mListeners.Count(); i < lcount; i++) |
|
556 { |
|
557 nsITransactionListener *listener = mListeners[i]; |
|
558 |
|
559 NS_ENSURE_TRUE(listener, NS_ERROR_FAILURE); |
|
560 |
|
561 result = listener->DidDo(this, aTransaction, aDoResult); |
|
562 |
|
563 if (NS_FAILED(result)) |
|
564 break; |
|
565 } |
|
566 |
|
567 return result; |
|
568 } |
|
569 |
|
570 nsresult |
|
571 nsTransactionManager::WillUndoNotify(nsITransaction *aTransaction, bool *aInterrupt) |
|
572 { |
|
573 nsresult result = NS_OK; |
|
574 for (int32_t i = 0, lcount = mListeners.Count(); i < lcount; i++) |
|
575 { |
|
576 nsITransactionListener *listener = mListeners[i]; |
|
577 |
|
578 NS_ENSURE_TRUE(listener, NS_ERROR_FAILURE); |
|
579 |
|
580 result = listener->WillUndo(this, aTransaction, aInterrupt); |
|
581 |
|
582 if (NS_FAILED(result) || *aInterrupt) |
|
583 break; |
|
584 } |
|
585 |
|
586 return result; |
|
587 } |
|
588 |
|
589 nsresult |
|
590 nsTransactionManager::DidUndoNotify(nsITransaction *aTransaction, nsresult aUndoResult) |
|
591 { |
|
592 nsresult result = NS_OK; |
|
593 for (int32_t i = 0, lcount = mListeners.Count(); i < lcount; i++) |
|
594 { |
|
595 nsITransactionListener *listener = mListeners[i]; |
|
596 |
|
597 NS_ENSURE_TRUE(listener, NS_ERROR_FAILURE); |
|
598 |
|
599 result = listener->DidUndo(this, aTransaction, aUndoResult); |
|
600 |
|
601 if (NS_FAILED(result)) |
|
602 break; |
|
603 } |
|
604 |
|
605 return result; |
|
606 } |
|
607 |
|
608 nsresult |
|
609 nsTransactionManager::WillRedoNotify(nsITransaction *aTransaction, bool *aInterrupt) |
|
610 { |
|
611 nsresult result = NS_OK; |
|
612 for (int32_t i = 0, lcount = mListeners.Count(); i < lcount; i++) |
|
613 { |
|
614 nsITransactionListener *listener = mListeners[i]; |
|
615 |
|
616 NS_ENSURE_TRUE(listener, NS_ERROR_FAILURE); |
|
617 |
|
618 result = listener->WillRedo(this, aTransaction, aInterrupt); |
|
619 |
|
620 if (NS_FAILED(result) || *aInterrupt) |
|
621 break; |
|
622 } |
|
623 |
|
624 return result; |
|
625 } |
|
626 |
|
627 nsresult |
|
628 nsTransactionManager::DidRedoNotify(nsITransaction *aTransaction, nsresult aRedoResult) |
|
629 { |
|
630 nsresult result = NS_OK; |
|
631 for (int32_t i = 0, lcount = mListeners.Count(); i < lcount; i++) |
|
632 { |
|
633 nsITransactionListener *listener = mListeners[i]; |
|
634 |
|
635 NS_ENSURE_TRUE(listener, NS_ERROR_FAILURE); |
|
636 |
|
637 result = listener->DidRedo(this, aTransaction, aRedoResult); |
|
638 |
|
639 if (NS_FAILED(result)) |
|
640 break; |
|
641 } |
|
642 |
|
643 return result; |
|
644 } |
|
645 |
|
646 nsresult |
|
647 nsTransactionManager::WillBeginBatchNotify(bool *aInterrupt) |
|
648 { |
|
649 nsresult result = NS_OK; |
|
650 for (int32_t i = 0, lcount = mListeners.Count(); i < lcount; i++) |
|
651 { |
|
652 nsITransactionListener *listener = mListeners[i]; |
|
653 |
|
654 NS_ENSURE_TRUE(listener, NS_ERROR_FAILURE); |
|
655 |
|
656 result = listener->WillBeginBatch(this, aInterrupt); |
|
657 |
|
658 if (NS_FAILED(result) || *aInterrupt) |
|
659 break; |
|
660 } |
|
661 |
|
662 return result; |
|
663 } |
|
664 |
|
665 nsresult |
|
666 nsTransactionManager::DidBeginBatchNotify(nsresult aResult) |
|
667 { |
|
668 nsresult result = NS_OK; |
|
669 for (int32_t i = 0, lcount = mListeners.Count(); i < lcount; i++) |
|
670 { |
|
671 nsITransactionListener *listener = mListeners[i]; |
|
672 |
|
673 NS_ENSURE_TRUE(listener, NS_ERROR_FAILURE); |
|
674 |
|
675 result = listener->DidBeginBatch(this, aResult); |
|
676 |
|
677 if (NS_FAILED(result)) |
|
678 break; |
|
679 } |
|
680 |
|
681 return result; |
|
682 } |
|
683 |
|
684 nsresult |
|
685 nsTransactionManager::WillEndBatchNotify(bool *aInterrupt) |
|
686 { |
|
687 nsresult result = NS_OK; |
|
688 for (int32_t i = 0, lcount = mListeners.Count(); i < lcount; i++) |
|
689 { |
|
690 nsITransactionListener *listener = mListeners[i]; |
|
691 |
|
692 NS_ENSURE_TRUE(listener, NS_ERROR_FAILURE); |
|
693 |
|
694 result = listener->WillEndBatch(this, aInterrupt); |
|
695 |
|
696 if (NS_FAILED(result) || *aInterrupt) |
|
697 break; |
|
698 } |
|
699 |
|
700 return result; |
|
701 } |
|
702 |
|
703 nsresult |
|
704 nsTransactionManager::DidEndBatchNotify(nsresult aResult) |
|
705 { |
|
706 nsresult result = NS_OK; |
|
707 for (int32_t i = 0, lcount = mListeners.Count(); i < lcount; i++) |
|
708 { |
|
709 nsITransactionListener *listener = mListeners[i]; |
|
710 |
|
711 NS_ENSURE_TRUE(listener, NS_ERROR_FAILURE); |
|
712 |
|
713 result = listener->DidEndBatch(this, aResult); |
|
714 |
|
715 if (NS_FAILED(result)) |
|
716 break; |
|
717 } |
|
718 |
|
719 return result; |
|
720 } |
|
721 |
|
722 nsresult |
|
723 nsTransactionManager::WillMergeNotify(nsITransaction *aTop, nsITransaction *aTransaction, bool *aInterrupt) |
|
724 { |
|
725 nsresult result = NS_OK; |
|
726 for (int32_t i = 0, lcount = mListeners.Count(); i < lcount; i++) |
|
727 { |
|
728 nsITransactionListener *listener = mListeners[i]; |
|
729 |
|
730 NS_ENSURE_TRUE(listener, NS_ERROR_FAILURE); |
|
731 |
|
732 result = listener->WillMerge(this, aTop, aTransaction, aInterrupt); |
|
733 |
|
734 if (NS_FAILED(result) || *aInterrupt) |
|
735 break; |
|
736 } |
|
737 |
|
738 return result; |
|
739 } |
|
740 |
|
741 nsresult |
|
742 nsTransactionManager::DidMergeNotify(nsITransaction *aTop, |
|
743 nsITransaction *aTransaction, |
|
744 bool aDidMerge, |
|
745 nsresult aMergeResult) |
|
746 { |
|
747 nsresult result = NS_OK; |
|
748 for (int32_t i = 0, lcount = mListeners.Count(); i < lcount; i++) |
|
749 { |
|
750 nsITransactionListener *listener = mListeners[i]; |
|
751 |
|
752 NS_ENSURE_TRUE(listener, NS_ERROR_FAILURE); |
|
753 |
|
754 result = listener->DidMerge(this, aTop, aTransaction, aDidMerge, aMergeResult); |
|
755 |
|
756 if (NS_FAILED(result)) |
|
757 break; |
|
758 } |
|
759 |
|
760 return result; |
|
761 } |
|
762 |
|
763 nsresult |
|
764 nsTransactionManager::BeginTransaction(nsITransaction *aTransaction, |
|
765 nsISupports *aData) |
|
766 { |
|
767 nsresult result = NS_OK; |
|
768 |
|
769 // XXX: POSSIBLE OPTIMIZATION |
|
770 // We could use a factory that pre-allocates/recycles transaction items. |
|
771 nsRefPtr<nsTransactionItem> tx = new nsTransactionItem(aTransaction); |
|
772 |
|
773 if (aData) { |
|
774 nsCOMArray<nsISupports>& data = tx->GetData(); |
|
775 data.AppendObject(aData); |
|
776 } |
|
777 |
|
778 if (!tx) { |
|
779 return NS_ERROR_OUT_OF_MEMORY; |
|
780 } |
|
781 |
|
782 mDoStack.Push(tx); |
|
783 |
|
784 result = tx->DoTransaction(); |
|
785 |
|
786 if (NS_FAILED(result)) { |
|
787 tx = mDoStack.Pop(); |
|
788 return result; |
|
789 } |
|
790 |
|
791 return NS_OK; |
|
792 } |
|
793 |
|
794 nsresult |
|
795 nsTransactionManager::EndTransaction(bool aAllowEmpty) |
|
796 { |
|
797 nsresult result = NS_OK; |
|
798 |
|
799 nsRefPtr<nsTransactionItem> tx = mDoStack.Pop(); |
|
800 |
|
801 if (!tx) |
|
802 return NS_ERROR_FAILURE; |
|
803 |
|
804 nsCOMPtr<nsITransaction> tint = tx->GetTransaction(); |
|
805 |
|
806 if (!tint && !aAllowEmpty) { |
|
807 int32_t nc = 0; |
|
808 |
|
809 // If we get here, the transaction must be a dummy batch transaction |
|
810 // created by BeginBatch(). If it contains no children, get rid of it! |
|
811 |
|
812 tx->GetNumberOfChildren(&nc); |
|
813 |
|
814 if (!nc) { |
|
815 return result; |
|
816 } |
|
817 } |
|
818 |
|
819 // Check if the transaction is transient. If it is, there's nothing |
|
820 // more to do, just return. |
|
821 |
|
822 bool isTransient = false; |
|
823 |
|
824 if (tint) |
|
825 result = tint->GetIsTransient(&isTransient); |
|
826 |
|
827 if (NS_FAILED(result) || isTransient || !mMaxTransactionCount) { |
|
828 // XXX: Should we be clearing the redo stack if the transaction |
|
829 // is transient and there is nothing on the do stack? |
|
830 return result; |
|
831 } |
|
832 |
|
833 // Check if there is a transaction on the do stack. If there is, |
|
834 // the current transaction is a "sub" transaction, and should |
|
835 // be added to the transaction at the top of the do stack. |
|
836 |
|
837 nsRefPtr<nsTransactionItem> top = mDoStack.Peek(); |
|
838 if (top) { |
|
839 result = top->AddChild(tx); |
|
840 |
|
841 // XXX: What do we do if this fails? |
|
842 |
|
843 return result; |
|
844 } |
|
845 |
|
846 // The transaction succeeded, so clear the redo stack. |
|
847 |
|
848 result = ClearRedoStack(); |
|
849 |
|
850 if (NS_FAILED(result)) { |
|
851 // XXX: What do we do if this fails? |
|
852 } |
|
853 |
|
854 // Check if we can coalesce this transaction with the one at the top |
|
855 // of the undo stack. |
|
856 |
|
857 top = mUndoStack.Peek(); |
|
858 |
|
859 if (tint && top) { |
|
860 bool didMerge = false; |
|
861 nsCOMPtr<nsITransaction> topTransaction = top->GetTransaction(); |
|
862 |
|
863 if (topTransaction) { |
|
864 |
|
865 bool doInterrupt = false; |
|
866 |
|
867 result = WillMergeNotify(topTransaction, tint, &doInterrupt); |
|
868 |
|
869 NS_ENSURE_SUCCESS(result, result); |
|
870 |
|
871 if (!doInterrupt) { |
|
872 result = topTransaction->Merge(tint, &didMerge); |
|
873 |
|
874 nsresult result2 = DidMergeNotify(topTransaction, tint, didMerge, result); |
|
875 |
|
876 if (NS_SUCCEEDED(result)) |
|
877 result = result2; |
|
878 |
|
879 if (NS_FAILED(result)) { |
|
880 // XXX: What do we do if this fails? |
|
881 } |
|
882 |
|
883 if (didMerge) { |
|
884 return result; |
|
885 } |
|
886 } |
|
887 } |
|
888 } |
|
889 |
|
890 // Check to see if we've hit the max level of undo. If so, |
|
891 // pop the bottom transaction off the undo stack and release it! |
|
892 |
|
893 int32_t sz = mUndoStack.GetSize(); |
|
894 |
|
895 if (mMaxTransactionCount > 0 && sz >= mMaxTransactionCount) { |
|
896 nsRefPtr<nsTransactionItem> overflow = mUndoStack.PopBottom(); |
|
897 } |
|
898 |
|
899 // Push the transaction on the undo stack: |
|
900 |
|
901 mUndoStack.Push(tx); |
|
902 |
|
903 return NS_OK; |
|
904 } |
|
905 |