Source code

Revision control

Other Tools

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
#ifndef mozilla_TransactionManager_h
7
#define mozilla_TransactionManager_h
8
9
#include "mozilla/TransactionStack.h"
10
11
#include "nsCOMArray.h"
12
#include "nsCOMPtr.h"
13
#include "nsCycleCollectionParticipant.h"
14
#include "nsISupportsImpl.h"
15
#include "nsITransactionManager.h"
16
#include "nsWeakReference.h"
17
#include "nscore.h"
18
19
class nsITransaction;
20
class nsITransactionListener;
21
22
namespace mozilla {
23
24
class TransactionManager final : public nsITransactionManager,
25
public nsSupportsWeakReference {
26
public:
27
explicit TransactionManager(int32_t aMaxTransactionCount = -1);
28
29
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
30
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(TransactionManager,
31
nsITransactionManager)
32
33
NS_DECL_NSITRANSACTIONMANAGER
34
35
already_AddRefed<nsITransaction> PeekUndoStack();
36
already_AddRefed<nsITransaction> PeekRedoStack();
37
38
nsresult Undo();
39
nsresult Redo();
40
41
size_t NumberOfUndoItems() const { return mUndoStack.GetSize(); }
42
size_t NumberOfRedoItems() const { return mRedoStack.GetSize(); }
43
44
int32_t NumberOfMaximumTransactions() const { return mMaxTransactionCount; }
45
46
bool EnableUndoRedo(int32_t aMaxTransactionCount = -1);
47
bool DisableUndoRedo() { return EnableUndoRedo(0); }
48
bool ClearUndoRedo() {
49
if (NS_WARN_IF(!mDoStack.IsEmpty())) {
50
return false;
51
}
52
mUndoStack.Clear();
53
mRedoStack.Clear();
54
return true;
55
}
56
57
bool AddTransactionListener(nsITransactionListener& aListener) {
58
// XXX Shouldn't we check if aListener has already been in mListeners?
59
return mListeners.AppendObject(&aListener);
60
}
61
bool RemoveTransactionListener(nsITransactionListener& aListener) {
62
return mListeners.RemoveObject(&aListener);
63
}
64
65
nsresult WillDoNotify(nsITransaction* aTransaction, bool* aInterrupt);
66
nsresult DidDoNotify(nsITransaction* aTransaction, nsresult aExecuteResult);
67
nsresult WillUndoNotify(nsITransaction* aTransaction, bool* aInterrupt);
68
nsresult DidUndoNotify(nsITransaction* aTransaction, nsresult aUndoResult);
69
nsresult WillRedoNotify(nsITransaction* aTransaction, bool* aInterrupt);
70
nsresult DidRedoNotify(nsITransaction* aTransaction, nsresult aRedoResult);
71
nsresult WillBeginBatchNotify(bool* aInterrupt);
72
nsresult DidBeginBatchNotify(nsresult aResult);
73
nsresult WillEndBatchNotify(bool* aInterrupt);
74
nsresult DidEndBatchNotify(nsresult aResult);
75
nsresult WillMergeNotify(nsITransaction* aTop, nsITransaction* aTransaction,
76
bool* aInterrupt);
77
nsresult DidMergeNotify(nsITransaction* aTop, nsITransaction* aTransaction,
78
bool aDidMerge, nsresult aMergeResult);
79
80
/**
81
* Exposing non-virtual methods of nsITransactionManager methods.
82
*/
83
nsresult BeginBatchInternal(nsISupports* aData);
84
nsresult EndBatchInternal(bool aAllowEmpty);
85
86
private:
87
virtual ~TransactionManager() = default;
88
89
nsresult BeginTransaction(nsITransaction* aTransaction, nsISupports* aData);
90
nsresult EndTransaction(bool aAllowEmpty);
91
92
int32_t mMaxTransactionCount;
93
TransactionStack mDoStack;
94
TransactionStack mUndoStack;
95
TransactionStack mRedoStack;
96
nsCOMArray<nsITransactionListener> mListeners;
97
};
98
99
} // namespace mozilla
100
101
mozilla::TransactionManager* nsITransactionManager::AsTransactionManager() {
102
return static_cast<mozilla::TransactionManager*>(this);
103
}
104
105
#endif // #ifndef mozilla_TransactionManager_h