~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/xpcom/base/nsExceptionService.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Netscape Public License
 
6
 * Version 1.1 (the "License"); you may not use this file except in
 
7
 * compliance with the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/NPL/
 
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
 * ActiveState Tool Corp..
 
19
 * Portions created by the Initial Developer are Copyright (C) 2001
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *           Mark Hammond <MarkH@ActiveState.com>
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or 
 
27
 * 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 NPL, 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 NPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#include "nsISupports.h"
 
40
#include "nsExceptionService.h"
 
41
#include "nsIServiceManager.h"
 
42
#include "nsCOMPtr.h"
 
43
#include "prthread.h"
 
44
#include "prlock.h"
 
45
static const PRUintn BAD_TLS_INDEX = (PRUintn) -1;
 
46
 
 
47
#define CHECK_SERVICE_USE_OK() if (!lock) return NS_ERROR_NOT_INITIALIZED
 
48
#define CHECK_MANAGER_USE_OK() if (!mService || !nsExceptionService::lock) return NS_ERROR_NOT_INITIALIZED
 
49
 
 
50
// A key for our registered module providers hashtable
 
51
class nsProviderKey : public nsHashKey {
 
52
protected:
 
53
  PRUint32 mKey;
 
54
public:
 
55
  nsProviderKey(PRUint32 key) : mKey(key) {}
 
56
  PRUint32 HashCode(void) const {
 
57
    return mKey;
 
58
  }
 
59
  PRBool Equals(const nsHashKey *aKey) const {
 
60
    return mKey == ((const nsProviderKey *) aKey)->mKey;
 
61
  }
 
62
  nsHashKey *Clone() const {
 
63
    return new nsProviderKey(mKey);
 
64
  }
 
65
  PRUint32 GetValue() { return mKey; }
 
66
};
 
67
 
 
68
/** Exception Manager definition **/
 
69
class nsExceptionManager : public nsIExceptionManager
 
70
{
 
71
public:
 
72
  NS_DECL_ISUPPORTS
 
73
  NS_DECL_NSIEXCEPTIONMANAGER
 
74
 
 
75
  nsExceptionManager(nsExceptionService *svc);
 
76
  /* additional members */
 
77
  nsCOMPtr<nsIException> mCurrentException;
 
78
  nsExceptionManager *mNextThread; // not ref-counted.
 
79
  nsExceptionService *mService; // not ref-counted
 
80
#ifdef NS_DEBUG
 
81
  static PRInt32 totalInstances;
 
82
#endif
 
83
 
 
84
private:
 
85
  ~nsExceptionManager();
 
86
};
 
87
 
 
88
 
 
89
#ifdef NS_DEBUG
 
90
PRInt32 nsExceptionManager::totalInstances = 0;
 
91
#endif
 
92
 
 
93
// Note this object is single threaded - the service itself ensures
 
94
// one per thread.
 
95
// An exception if the destructor, which may be called on
 
96
// the thread shutting down xpcom
 
97
NS_IMPL_ISUPPORTS1(nsExceptionManager, nsIExceptionManager)
 
98
 
 
99
nsExceptionManager::nsExceptionManager(nsExceptionService *svc) :
 
100
  mNextThread(nsnull),
 
101
  mService(svc)
 
102
{
 
103
  /* member initializers and constructor code */
 
104
#ifdef NS_DEBUG
 
105
  PR_AtomicIncrement(&totalInstances);
 
106
#endif
 
107
}
 
108
 
 
109
nsExceptionManager::~nsExceptionManager()
 
110
{
 
111
  /* destructor code */
 
112
#ifdef NS_DEBUG
 
113
  PR_AtomicDecrement(&totalInstances);
 
114
#endif // NS_DEBUG
 
115
}
 
116
 
 
117
/* void setCurrentException (in nsIException error); */
 
118
NS_IMETHODIMP nsExceptionManager::SetCurrentException(nsIException *error)
 
119
{
 
120
    CHECK_MANAGER_USE_OK();
 
121
    mCurrentException = error;
 
122
    return NS_OK;
 
123
}
 
124
 
 
125
/* nsIException getCurrentException (); */
 
126
NS_IMETHODIMP nsExceptionManager::GetCurrentException(nsIException **_retval)
 
127
{
 
128
    CHECK_MANAGER_USE_OK();
 
129
    *_retval = mCurrentException;
 
130
    NS_IF_ADDREF(*_retval);
 
131
    return NS_OK;
 
132
}
 
133
 
 
134
/* nsIException getExceptionFromProvider( in nsresult rc, in nsIException defaultException); */
 
135
NS_IMETHODIMP nsExceptionManager::GetExceptionFromProvider(nsresult rc, nsIException * defaultException, nsIException **_retval)
 
136
{
 
137
    CHECK_MANAGER_USE_OK();
 
138
    // Just delegate back to the service with the provider map.
 
139
    return mService->GetExceptionFromProvider(rc, defaultException, _retval);
 
140
}
 
141
 
 
142
/* The Exception Service */
 
143
 
 
144
PRUintn nsExceptionService::tlsIndex = BAD_TLS_INDEX;
 
145
PRLock *nsExceptionService::lock = PR_FALSE;
 
146
nsExceptionManager *nsExceptionService::firstThread = nsnull;
 
147
 
 
148
#ifdef NS_DEBUG
 
149
PRInt32 nsExceptionService::totalInstances = 0;
 
150
#endif
 
151
 
 
152
NS_IMPL_THREADSAFE_ISUPPORTS2(nsExceptionService, nsIExceptionService, nsIObserver)
 
153
 
 
154
nsExceptionService::nsExceptionService()
 
155
  : mProviders(4, PR_TRUE) /* small, thread-safe hashtable */
 
156
{
 
157
#ifdef NS_DEBUG
 
158
  if (PR_AtomicIncrement(&totalInstances)!=1) {
 
159
    NS_ERROR("The nsExceptionService is a singleton!");
 
160
  }
 
161
#endif
 
162
  /* member initializers and constructor code */
 
163
  if (tlsIndex == BAD_TLS_INDEX) {
 
164
    PRStatus status;
 
165
    status = PR_NewThreadPrivateIndex( &tlsIndex, ThreadDestruct );
 
166
    NS_WARN_IF_FALSE(status==0, "ScriptErrorService could not allocate TLS storage.");
 
167
  }
 
168
  lock = PR_NewLock();
 
169
  NS_WARN_IF_FALSE(lock, "Error allocating ExceptionService lock");
 
170
 
 
171
  // observe XPCOM shutdown.
 
172
  nsCOMPtr<nsIObserverService> observerService = do_GetService("@mozilla.org/observer-service;1");
 
173
  NS_WARN_IF_FALSE(observerService, "Could not get observer service!");
 
174
  if (observerService)
 
175
    observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
 
176
}
 
177
 
 
178
nsExceptionService::~nsExceptionService()
 
179
{
 
180
  Shutdown();
 
181
  /* destructor code */
 
182
#ifdef NS_DEBUG
 
183
  PR_AtomicDecrement(&totalInstances);
 
184
#endif
 
185
}
 
186
 
 
187
/*static*/
 
188
void nsExceptionService::ThreadDestruct( void *data )
 
189
{
 
190
  if (!lock) {
 
191
    NS_WARNING("nsExceptionService ignoring thread destruction after shutdown");
 
192
    return;
 
193
  }
 
194
  DropThread( (nsExceptionManager *)data );
 
195
}
 
196
 
 
197
 
 
198
void nsExceptionService::Shutdown()
 
199
{
 
200
  mProviders.Reset();
 
201
  if (lock) {
 
202
    DropAllThreads();
 
203
    PR_DestroyLock(lock);
 
204
    lock = nsnull;
 
205
  }
 
206
  PR_SetThreadPrivate(tlsIndex, nsnull);
 
207
}
 
208
 
 
209
/* void setCurrentException (in nsIException error); */
 
210
NS_IMETHODIMP nsExceptionService::SetCurrentException(nsIException *error)
 
211
{
 
212
    CHECK_SERVICE_USE_OK();
 
213
    nsCOMPtr<nsIExceptionManager> sm;
 
214
    nsresult nr = GetCurrentExceptionManager(getter_AddRefs(sm));
 
215
    if (NS_FAILED(nr))
 
216
        return nr;
 
217
    return sm->SetCurrentException(error);
 
218
}
 
219
 
 
220
/* nsIException getCurrentException (); */
 
221
NS_IMETHODIMP nsExceptionService::GetCurrentException(nsIException **_retval)
 
222
{
 
223
    CHECK_SERVICE_USE_OK();
 
224
    nsCOMPtr<nsIExceptionManager> sm;
 
225
    nsresult nr = GetCurrentExceptionManager(getter_AddRefs(sm));
 
226
    if (NS_FAILED(nr))
 
227
        return nr;
 
228
    return sm->GetCurrentException(_retval);
 
229
}
 
230
 
 
231
/* nsIException getExceptionFromProvider( in nsresult rc, in nsIException defaultException); */
 
232
NS_IMETHODIMP nsExceptionService::GetExceptionFromProvider(nsresult rc, 
 
233
    nsIException * defaultException, nsIException **_retval)
 
234
{
 
235
    CHECK_SERVICE_USE_OK();
 
236
    return DoGetExceptionFromProvider(rc, defaultException, _retval);
 
237
}
 
238
 
 
239
/* readonly attribute nsIExceptionManager currentExceptionManager; */
 
240
NS_IMETHODIMP nsExceptionService::GetCurrentExceptionManager(nsIExceptionManager * *aCurrentScriptManager)
 
241
{
 
242
    CHECK_SERVICE_USE_OK();
 
243
    nsExceptionManager *mgr = (nsExceptionManager *)PR_GetThreadPrivate(tlsIndex);
 
244
    if (mgr == nsnull) {
 
245
        // Stick the new exception object in with no reference count.
 
246
        mgr = new nsExceptionManager(this);
 
247
        if (mgr == nsnull)
 
248
            return NS_ERROR_OUT_OF_MEMORY;
 
249
        PR_SetThreadPrivate(tlsIndex, mgr);
 
250
        // The reference count is held in the thread-list
 
251
        AddThread(mgr);
 
252
    }
 
253
    *aCurrentScriptManager = mgr;
 
254
    NS_ADDREF(*aCurrentScriptManager);
 
255
    return NS_OK;
 
256
}
 
257
 
 
258
/* void registerErrorProvider (in nsIExceptionProvider provider, in PRUint32 moduleCode); */
 
259
NS_IMETHODIMP nsExceptionService::RegisterExceptionProvider(nsIExceptionProvider *provider, PRUint32 errorModule)
 
260
{
 
261
    CHECK_SERVICE_USE_OK();
 
262
 
 
263
    nsProviderKey key(errorModule);
 
264
    if (mProviders.Put(&key, provider)) {
 
265
        NS_WARNING("Registration of exception provider overwrote another provider with the same module code!");
 
266
    }
 
267
    return NS_OK;
 
268
}
 
269
 
 
270
/* void unregisterErrorProvider (in nsIExceptionProvider provider, in PRUint32 errorModule); */
 
271
NS_IMETHODIMP nsExceptionService::UnregisterExceptionProvider(nsIExceptionProvider *provider, PRUint32 errorModule)
 
272
{
 
273
    CHECK_SERVICE_USE_OK();
 
274
    nsProviderKey key(errorModule);
 
275
    if (!mProviders.Remove(&key)) {
 
276
        NS_WARNING("Attempt to unregister an unregistered exception provider!");
 
277
        return NS_ERROR_UNEXPECTED;
 
278
    }
 
279
    return NS_OK;
 
280
}
 
281
 
 
282
// nsIObserver
 
283
NS_IMETHODIMP nsExceptionService::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
 
284
{
 
285
     Shutdown();
 
286
     return NS_OK;
 
287
}
 
288
 
 
289
nsresult
 
290
nsExceptionService::DoGetExceptionFromProvider(nsresult errCode, 
 
291
                                               nsIException * defaultException,
 
292
                                               nsIException **_exc)
 
293
{
 
294
    // Check for an existing exception
 
295
    nsresult nr = GetCurrentException(_exc);
 
296
    if (NS_SUCCEEDED(nr) && *_exc) {
 
297
        (*_exc)->GetResult(&nr);
 
298
        // If it matches our result then use it
 
299
        if (nr == errCode)
 
300
            return NS_OK;
 
301
        NS_RELEASE(*_exc);
 
302
    }
 
303
    nsProviderKey key(NS_ERROR_GET_MODULE(errCode));
 
304
    nsCOMPtr<nsIExceptionProvider> provider =
 
305
        dont_AddRef((nsIExceptionProvider *)mProviders.Get(&key));
 
306
 
 
307
    // No provider so we'll return the default exception
 
308
    if (!provider) {
 
309
        *_exc = defaultException;
 
310
        NS_IF_ADDREF(*_exc);
 
311
        return NS_OK;
 
312
    }
 
313
 
 
314
    return provider->GetException(errCode, defaultException, _exc);
 
315
}
 
316
 
 
317
// thread management
 
318
/*static*/ void nsExceptionService::AddThread(nsExceptionManager *thread)
 
319
{
 
320
    PR_Lock(lock);
 
321
    thread->mNextThread = firstThread;
 
322
    firstThread = thread;
 
323
    NS_ADDREF(thread);
 
324
    PR_Unlock(lock);
 
325
}
 
326
 
 
327
/*static*/ void nsExceptionService::DoDropThread(nsExceptionManager *thread)
 
328
{
 
329
    nsExceptionManager **emp = &firstThread;
 
330
    while (*emp != thread) {
 
331
        NS_ABORT_IF_FALSE(*emp, "Could not find the thread to drop!");
 
332
        emp = &(*emp)->mNextThread;
 
333
    }
 
334
    *emp = thread->mNextThread;
 
335
    NS_RELEASE(thread);
 
336
}
 
337
 
 
338
/*static*/ void nsExceptionService::DropThread(nsExceptionManager *thread)
 
339
{
 
340
    PR_Lock(lock);
 
341
    DoDropThread(thread);
 
342
    PR_Unlock(lock);
 
343
}
 
344
 
 
345
/*static*/ void nsExceptionService::DropAllThreads()
 
346
{
 
347
    PR_Lock(lock);
 
348
    while (firstThread)
 
349
        DoDropThread(firstThread);
 
350
    PR_Unlock(lock);
 
351
}