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
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
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.
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 ***** */
40 #include "nsAutoPtr.h"
41 #include "nsIFactory.h"
42 #include "nsIServiceManager.h"
43 #include "nsIComponentManager.h"
44 #include "nsIObserverService.h"
45 #include "nsIObserver.h"
46 #include "nsISimpleEnumerator.h"
47 #include "nsObserverService.h"
48 #include "nsObserverList.h"
49 #include "nsHashtable.h"
50 #include "nsThreadUtils.h"
51 #include "nsIWeakReference.h"
52 #include "nsEnumeratorUtils.h"
54 #define NOTIFY_GLOBAL_OBSERVERS
56 #if defined(PR_LOGGING)
57 // Log module for nsObserverService logging...
59 // To enable logging (see prlog.h for full details):
61 // set NSPR_LOG_MODULES=ObserverService:5
62 // set NSPR_LOG_FILE=nspr.log
64 // this enables PR_LOG_DEBUG level information and places all output in
66 PRLogModuleInfo
* observerServiceLog
= PR_NewLogModule("ObserverService");
67 #define LOG(x) PR_LOG(observerServiceLog, PR_LOG_DEBUG, x)
70 #endif /* PR_LOGGING */
72 ////////////////////////////////////////////////////////////////////////////////
73 // nsObserverService Implementation
76 NS_IMPL_THREADSAFE_ISUPPORTS2(nsObserverService
, nsIObserverService
, nsObserverService
)
78 nsObserverService::nsObserverService() :
79 mShuttingDown(PR_FALSE
)
81 mObserverTopicTable
.Init();
84 nsObserverService::~nsObserverService(void)
90 nsObserverService::Shutdown()
92 mShuttingDown
= PR_TRUE
;
94 if (mObserverTopicTable
.IsInitialized())
95 mObserverTopicTable
.Clear();
99 nsObserverService::Create(nsISupports
* outer
, const nsIID
& aIID
, void* *aInstancePtr
)
101 LOG(("nsObserverService::Create()"));
103 nsRefPtr
<nsObserverService
> os
= new nsObserverService();
105 // The cast is required for MSVC6, otherwise it complains about calling
106 // a private function.
107 if (!os
|| !((nsObserverService
*) os
)->mObserverTopicTable
.IsInitialized())
108 return NS_ERROR_OUT_OF_MEMORY
;
110 return os
->QueryInterface(aIID
, aInstancePtr
);
113 #define NS_ENSURE_VALIDCALL \
114 if (!NS_IsMainThread()) { \
115 NS_ERROR("Using observer service off the main thread!"); \
116 return NS_ERROR_UNEXPECTED; \
118 if (mShuttingDown) { \
119 NS_ERROR("Using observer service after XPCOM shutdown!"); \
120 return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; \
124 nsObserverService::AddObserver(nsIObserver
* anObserver
, const char* aTopic
,
127 LOG(("nsObserverService::AddObserver(%p: %s)",
128 (void*) anObserver
, aTopic
));
131 NS_ENSURE_ARG(anObserver
&& aTopic
);
133 nsObserverList
*observerList
= mObserverTopicTable
.PutEntry(aTopic
);
135 return NS_ERROR_OUT_OF_MEMORY
;
137 return observerList
->AddObserver(anObserver
, ownsWeak
);
141 nsObserverService::RemoveObserver(nsIObserver
* anObserver
, const char* aTopic
)
143 LOG(("nsObserverService::RemoveObserver(%p: %s)",
144 (void*) anObserver
, aTopic
));
146 NS_ENSURE_ARG(anObserver
&& aTopic
);
148 nsObserverList
*observerList
= mObserverTopicTable
.GetEntry(aTopic
);
150 return NS_ERROR_FAILURE
;
152 return observerList
->RemoveObserver(anObserver
);
156 nsObserverService::EnumerateObservers(const char* aTopic
,
157 nsISimpleEnumerator
** anEnumerator
)
160 NS_ENSURE_ARG(aTopic
&& anEnumerator
);
162 nsObserverList
*observerList
= mObserverTopicTable
.GetEntry(aTopic
);
164 return NS_NewEmptyEnumerator(anEnumerator
);
166 return observerList
->GetObserverList(anEnumerator
);
169 // Enumerate observers of aTopic and call Observe on each.
170 NS_IMETHODIMP
nsObserverService::NotifyObservers(nsISupports
*aSubject
,
172 const PRUnichar
*someData
)
174 LOG(("nsObserverService::NotifyObservers(%s)", aTopic
));
177 NS_ENSURE_ARG(aTopic
);
179 nsObserverList
*observerList
= mObserverTopicTable
.GetEntry(aTopic
);
181 observerList
->NotifyObservers(aSubject
, aTopic
, someData
);
183 #ifdef NOTIFY_GLOBAL_OBSERVERS
184 observerList
= mObserverTopicTable
.GetEntry("*");
186 observerList
->NotifyObservers(aSubject
, aTopic
, someData
);