VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/base/nsConsoleService.cpp@ 78394

Last change on this file since 78394 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
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/
9 *
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.
14 *
15 * The Original Code is mozilla.org code.
16 *
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.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/*
39 * Maintains a circular buffer of recent messages, and notifies
40 * listeners when new messages are logged.
41 */
42
43/* Threadsafe. */
44
45#include "nsMemory.h"
46#include "nsIServiceManager.h"
47#include "nsIProxyObjectManager.h"
48#include "nsSupportsArray.h"
49
50#include "nsConsoleService.h"
51#include "nsConsoleMessage.h"
52
53NS_IMPL_THREADSAFE_ISUPPORTS1(nsConsoleService, nsIConsoleService)
54
55nsConsoleService::nsConsoleService()
56 : mCurrent(0), mFull(PR_FALSE), mListening(PR_FALSE), mLock(nsnull)
57{
58 // XXX grab this from a pref!
59 // hm, but worry about circularity, bc we want to be able to report
60 // prefs errs...
61 mBufferSize = 250;
62
63 // XXX deal with these two allocations by detecting null mLock in factory?
64 mMessages = (nsIConsoleMessage **)
65 nsMemory::Alloc(mBufferSize * sizeof(nsIConsoleMessage *));
66
67 mLock = PR_NewLock();
68
69 // Array elements should be 0 initially for circular buffer algorithm.
70 for (PRUint32 i = 0; i < mBufferSize; i++) {
71 mMessages[i] = nsnull;
72 }
73
74}
75
76nsConsoleService::~nsConsoleService()
77{
78 PRUint32 i = 0;
79 while (i < mBufferSize && mMessages[i] != nsnull) {
80 NS_RELEASE(mMessages[i]);
81 i++;
82 }
83
84#ifdef DEBUG_mccabe
85 if (mListeners.Count() != 0) {
86 fprintf(stderr,
87 "WARNING - %d console error listeners still registered!\n"
88 "More calls to nsIConsoleService::UnregisterListener needed.\n",
89 mListeners.Count());
90 }
91
92#endif
93
94 nsMemory::Free(mMessages);
95 if (mLock)
96 PR_DestroyLock(mLock);
97}
98
99static PRBool PR_CALLBACK snapshot_enum_func(nsHashKey *key, void *data, void* closure)
100{
101 nsISupportsArray *array = (nsISupportsArray *)closure;
102
103 // Copy each element into the temporary nsSupportsArray...
104 array->AppendElement((nsISupports*)data);
105 return PR_TRUE;
106}
107
108// nsIConsoleService methods
109NS_IMETHODIMP
110nsConsoleService::LogMessage(nsIConsoleMessage *message)
111{
112 if (message == nsnull)
113 return NS_ERROR_INVALID_ARG;
114
115 nsSupportsArray listenersSnapshot;
116 nsIConsoleMessage *retiredMessage;
117
118 NS_ADDREF(message); // early, in case it's same as replaced below.
119
120 /*
121 * Lock while updating buffer, and while taking snapshot of
122 * listeners array.
123 */
124 {
125 nsAutoLock lock(mLock);
126
127 /*
128 * If there's already a message in the slot we're about to replace,
129 * we've wrapped around, and we need to release the old message. We
130 * save a pointer to it, so we can release below outside the lock.
131 */
132 retiredMessage = mMessages[mCurrent];
133
134 mMessages[mCurrent++] = message;
135 if (mCurrent == mBufferSize) {
136 mCurrent = 0; // wrap around.
137 mFull = PR_TRUE;
138 }
139
140 /*
141 * Copy the listeners into the snapshot array - in case a listener
142 * is removed during an Observe(...) notification...
143 */
144 mListeners.Enumerate(snapshot_enum_func, &listenersSnapshot);
145 }
146 if (retiredMessage != nsnull)
147 NS_RELEASE(retiredMessage);
148
149 /*
150 * Iterate through any registered listeners and tell them about
151 * the message. We use the mListening flag to guard against
152 * recursive message logs. This could sometimes result in
153 * listeners being skipped because of activity on other threads,
154 * when we only care about the recursive case.
155 */
156 nsCOMPtr<nsIConsoleListener> listener;
157 nsresult rv;
158 nsresult returned_rv;
159 PRUint32 snapshotCount;
160 rv = listenersSnapshot.Count(&snapshotCount);
161 if (NS_FAILED(rv))
162 return rv;
163
164 {
165 nsAutoLock lock(mLock);
166 if (mListening)
167 return NS_OK;
168 mListening = PR_TRUE;
169 }
170
171 returned_rv = NS_OK;
172 for (PRUint32 i = 0; i < snapshotCount; i++) {
173 rv = listenersSnapshot.GetElementAt(i, getter_AddRefs(listener));
174 if (NS_FAILED(rv)) {
175 returned_rv = rv;
176 break; // fall thru to mListening restore code below.
177 }
178 listener->Observe(message);
179 }
180
181 {
182 nsAutoLock lock(mLock);
183 mListening = PR_FALSE;
184 }
185
186 return returned_rv;
187}
188
189NS_IMETHODIMP
190nsConsoleService::LogStringMessage(const PRUnichar *message)
191{
192 nsConsoleMessage *msg = new nsConsoleMessage(message);
193 return this->LogMessage(msg);
194}
195
196NS_IMETHODIMP
197nsConsoleService::GetMessageArray(nsIConsoleMessage ***messages, PRUint32 *count)
198{
199 nsIConsoleMessage **messageArray;
200
201 /*
202 * Lock the whole method, as we don't want anyone mucking with mCurrent or
203 * mFull while we're copying out the buffer.
204 */
205 nsAutoLock lock(mLock);
206
207 if (mCurrent == 0 && !mFull) {
208 /*
209 * Make a 1-length output array so that nobody gets confused,
210 * and return a count of 0. This should result in a 0-length
211 * array object when called from script.
212 */
213 messageArray = (nsIConsoleMessage **)
214 nsMemory::Alloc(sizeof (nsIConsoleMessage *));
215 *messageArray = nsnull;
216 *messages = messageArray;
217 *count = 0;
218
219 return NS_OK;
220 }
221
222 PRUint32 resultSize = mFull ? mBufferSize : mCurrent;
223 messageArray =
224 (nsIConsoleMessage **)nsMemory::Alloc((sizeof (nsIConsoleMessage *))
225 * resultSize);
226
227 if (messageArray == nsnull) {
228 *messages = nsnull;
229 *count = 0;
230 return NS_ERROR_FAILURE;
231 }
232
233 PRUint32 i;
234 if (mFull) {
235 for (i = 0; i < mBufferSize; i++) {
236 // if full, fill the buffer starting from mCurrent (which'll be
237 // oldest) wrapping around the buffer to the most recent.
238 messageArray[i] = mMessages[(mCurrent + i) % mBufferSize];
239 NS_ADDREF(messageArray[i]);
240 }
241 } else {
242 for (i = 0; i < mCurrent; i++) {
243 messageArray[i] = mMessages[i];
244 NS_ADDREF(messageArray[i]);
245 }
246 }
247 *count = resultSize;
248 *messages = messageArray;
249
250 return NS_OK;
251}
252
253NS_IMETHODIMP
254nsConsoleService::RegisterListener(nsIConsoleListener *listener) {
255 nsresult rv;
256
257 /*
258 * Store a threadsafe proxy to the listener rather than the
259 * listener itself; we want the console service to be callable
260 * from any thread, but listeners can be implemented in
261 * thread-specific ways, and we always want to call them on their
262 * originating thread. JavaScript is the motivating example.
263 */
264 nsCOMPtr<nsIConsoleListener> proxiedListener;
265
266 rv = GetProxyForListener(listener, getter_AddRefs(proxiedListener));
267 if (NS_FAILED(rv))
268 return rv;
269
270 {
271 nsAutoLock lock(mLock);
272 nsISupportsKey key(listener);
273
274 /*
275 * Put the proxy event listener into a hashtable using the *real*
276 * listener as the key.
277 *
278 * This is necessary because proxy objects do *not* maintain
279 * nsISupports identity. Therefore, since GetProxyForListener(...)
280 * can return different proxies for the same object (see bug #85831)
281 * we need to use the real object as the unique key...
282 */
283 mListeners.Put(&key, proxiedListener);
284 }
285 return NS_OK;
286}
287
288NS_IMETHODIMP
289nsConsoleService::UnregisterListener(nsIConsoleListener *listener) {
290 nsAutoLock lock(mLock);
291
292 nsISupportsKey key(listener);
293 mListeners.Remove(&key);
294 return NS_OK;
295}
296
297nsresult
298nsConsoleService::GetProxyForListener(nsIConsoleListener* aListener,
299 nsIConsoleListener** aProxy)
300{
301 nsresult rv;
302 *aProxy = nsnull;
303
304 nsCOMPtr<nsIProxyObjectManager> proxyManager =
305 (do_GetService(NS_XPCOMPROXY_CONTRACTID));
306
307 if (proxyManager == nsnull)
308 return NS_ERROR_NOT_AVAILABLE;
309
310 /*
311 * NOTE this will fail if the calling thread doesn't have an eventQ.
312 *
313 * Would it be better to catch that case and leave the listener unproxied?
314 */
315 rv = proxyManager->GetProxyForObject(NS_CURRENT_EVENTQ,
316 NS_GET_IID(nsIConsoleListener),
317 aListener,
318 PROXY_ASYNC | PROXY_ALWAYS,
319 (void**) aProxy);
320 return rv;
321}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette