Bug 441473. Move nsSameOriginChecker into nsContentUtils. r+sr=jonas
[wine-gecko.git] / editor / libeditor / html / nsEditorTxnLog.cpp
blobbe6d8b08f5011733a801bdc94e23810ada4fff1c
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include <stdio.h>
40 #include "nsHTMLEditorLog.h"
41 #include "nsEditorTxnLog.h"
42 #include "nsPIEditorTransaction.h"
44 #define LOCK_LOG(doc)
45 #define UNLOCK_LOG(doc)
48 nsEditorTxnLog::nsEditorTxnLog(nsHTMLEditorLog *aEditorLog)
50 mRefCnt = 0;
51 mIndentLevel = 0;
52 mBatchCount = 0;
53 mEditorLog = aEditorLog;
56 nsEditorTxnLog::~nsEditorTxnLog()
60 // #define DEBUG_EDITOR_TXN_LOG_REFCNT 1
62 #ifdef DEBUG_EDITOR_TXN_LOG_REFCNT
64 nsrefcnt nsEditorTxnLog::AddRef(void)
66 return ++mRefCnt;
69 nsrefcnt nsEditorTxnLog::Release(void)
71 NS_PRECONDITION(0 != mRefCnt, "dup release");
72 if (--mRefCnt == 0) {
73 NS_DELETEXPCOM(this);
74 return 0;
76 return mRefCnt;
79 #else
81 NS_IMPL_ADDREF(nsEditorTxnLog)
82 NS_IMPL_RELEASE(nsEditorTxnLog)
84 #endif
86 NS_IMPL_QUERY_INTERFACE1(nsEditorTxnLog, nsITransactionListener)
88 NS_IMETHODIMP
89 nsEditorTxnLog::WillDo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, PRBool *aInterrupt)
91 LOCK_LOG(this);
93 PrintIndent(mIndentLevel++);
94 Write("WillDo: ");
95 WriteTransaction(aTransaction);
96 Write("\n");
97 Flush();
99 UNLOCK_LOG(this);
101 return NS_OK;
104 NS_IMETHODIMP
105 nsEditorTxnLog::DidDo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aDoResult)
107 LOCK_LOG(this);
109 PrintIndent(--mIndentLevel);
110 Write("DidDo: ");
111 WriteTransaction(aTransaction);
112 Write("(");
113 WriteInt(aDoResult);
114 Write(")\n");
115 Flush();
117 UNLOCK_LOG(this);
119 return NS_OK;
122 NS_IMETHODIMP
123 nsEditorTxnLog::WillUndo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, PRBool *aInterrupt)
125 LOCK_LOG(this);
127 PrintIndent(mIndentLevel++);
129 if (aTransaction)
131 Write("WillUndo: ");
132 WriteTransaction(aTransaction);
133 Write("\n");
135 else
136 Write("WillUndoBatch\n");
138 Flush();
140 UNLOCK_LOG(this);
142 return NS_OK;
145 NS_IMETHODIMP
146 nsEditorTxnLog::DidUndo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aUndoResult)
148 LOCK_LOG(this);
150 PrintIndent(--mIndentLevel);
152 if (aTransaction)
154 Write("DidUndo: ");
155 WriteTransaction(aTransaction);
156 Write("(");
157 WriteInt(aUndoResult);
158 Write(")\n");
160 else
162 Write("EndUndoBatch (");
163 WriteInt(aUndoResult);
164 Write(")\n");
167 Flush();
169 UNLOCK_LOG(this);
171 return NS_OK;
174 NS_IMETHODIMP
175 nsEditorTxnLog::WillRedo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, PRBool *aInterrupt)
177 LOCK_LOG(this);
179 PrintIndent(mIndentLevel++);
181 if (aTransaction)
183 Write("WillRedo: ");
184 WriteTransaction(aTransaction);
185 Write("\n");
187 else
188 Write("WillRedoBatch\n");
190 Flush();
192 UNLOCK_LOG(this);
194 return NS_OK;
197 NS_IMETHODIMP
198 nsEditorTxnLog::DidRedo(nsITransactionManager *aTxMgr, nsITransaction *aTransaction, nsresult aRedoResult)
200 LOCK_LOG(this);
202 PrintIndent(--mIndentLevel);
204 if (aTransaction)
206 Write("DidRedo: ");
207 WriteTransaction(aTransaction);
208 Write(" (");
209 WriteInt(aRedoResult);
210 Write(")\n");
212 else
214 Write("DidRedoBatch (");
215 WriteInt(aRedoResult);
216 Write(")\n");
219 Flush();
221 UNLOCK_LOG(this);
223 return NS_OK;
226 NS_IMETHODIMP
227 nsEditorTxnLog::WillBeginBatch(nsITransactionManager *aTxMgr, PRBool *aInterrupt)
229 LOCK_LOG(this);
231 PrintIndent(mIndentLevel);
232 Write("WillBeginBatch: ");
233 WriteInt(mBatchCount);
234 Write("\n");
235 Flush();
237 UNLOCK_LOG(this);
239 return NS_OK;
242 NS_IMETHODIMP
243 nsEditorTxnLog::DidBeginBatch(nsITransactionManager *aTxMgr, nsresult aResult)
245 LOCK_LOG(this);
247 PrintIndent(mIndentLevel++);
248 Write("DidBeginBatch: ");
249 WriteInt(mBatchCount++);
250 Write(" (");
251 WriteInt(aResult);
252 Write(")\n");
253 Flush();
255 UNLOCK_LOG(this);
257 return NS_OK;
260 NS_IMETHODIMP
261 nsEditorTxnLog::WillEndBatch(nsITransactionManager *aTxMgr, PRBool *aInterrupt)
263 LOCK_LOG(this);
265 PrintIndent(--mIndentLevel);
266 Write("WillEndBatch: ");
267 WriteInt(--mBatchCount);
268 Write("\n");
269 Flush();
271 UNLOCK_LOG(this);
273 return NS_OK;
276 NS_IMETHODIMP
277 nsEditorTxnLog::DidEndBatch(nsITransactionManager *aTxMgr, nsresult aResult)
279 LOCK_LOG(this);
281 PrintIndent(mIndentLevel);
282 Write("DidEndBatch: ");
283 WriteInt(mBatchCount);
284 Write(" (");
285 WriteInt(aResult);
286 Write(")\n");
287 Flush();
289 UNLOCK_LOG(this);
291 return NS_OK;
294 NS_IMETHODIMP
295 nsEditorTxnLog::WillMerge(nsITransactionManager *aTxMgr, nsITransaction *aTopTransaction, nsITransaction *aTransaction, PRBool *aInterrupt)
297 LOCK_LOG(this);
299 PrintIndent(mIndentLevel);
300 Write("WillMerge: ");
301 WriteTransaction(aTopTransaction);
302 Write(" <-- ");
303 WriteTransaction(aTransaction);
304 Write("\n");
305 Flush();
307 UNLOCK_LOG(this);
309 return NS_OK;
312 NS_IMETHODIMP
313 nsEditorTxnLog::DidMerge(nsITransactionManager *aTxMgr, nsITransaction *aTopTransaction, nsITransaction *aTransaction, PRBool aDidMerge, nsresult aMergeResult)
315 LOCK_LOG(this);
317 PrintIndent(mIndentLevel);
318 Write("DidMerge: ");
319 WriteTransaction(aTopTransaction);
320 Write(" <-- ");
321 WriteTransaction(aTransaction);
322 Write(" (");
323 Write(aDidMerge ? "TRUE" : "FALSE");
324 Write(", ");
325 WriteInt(aMergeResult);
326 Write(")\n");
327 Flush();
329 UNLOCK_LOG(this);
331 return NS_OK;
334 nsresult
335 nsEditorTxnLog::WriteTransaction(nsITransaction *aTransaction)
337 nsString str;
339 nsCOMPtr<nsPIEditorTransaction> txn = do_QueryInterface(aTransaction);
340 if (txn) {
341 txn->GetTxnDescription(str);
342 if (str.IsEmpty())
343 str.AssignLiteral("<NULL>");
346 return Write(NS_LossyConvertUTF16toASCII(str).get());
349 nsresult
350 nsEditorTxnLog::PrintIndent(PRInt32 aIndentLevel)
352 PRInt32 i;
354 Write(" // ");
356 for (i = 0; i < aIndentLevel; i++)
357 Write(" ");
359 return NS_OK;
362 nsresult
363 nsEditorTxnLog::Write(const char *aBuffer)
365 if (!aBuffer)
366 return NS_ERROR_NULL_POINTER;
368 if (mEditorLog)
369 mEditorLog->Write(aBuffer);
370 else
372 PRInt32 len = strlen(aBuffer);
373 if (len > 0)
374 fwrite(aBuffer, 1, len, stdout);
377 return NS_OK;
380 nsresult
381 nsEditorTxnLog::WriteInt(PRInt32 aInt)
383 if (mEditorLog)
384 mEditorLog->WriteInt(aInt);
385 else
386 printf("%d", aInt);
388 return NS_OK;
391 nsresult
392 nsEditorTxnLog::Flush()
394 nsresult result = NS_OK;
396 #ifdef SLOWS_THINGS_WAY_DOWN
398 if (mEditorLog)
399 result = mEditorLog->Flush();
400 else
401 fflush(stdout);
403 #endif // SLOWS_THINGS_WAY_DOWN
405 return result;