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 : * Pierre Phaneuf <pp@ludusdesign.com>
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "prlog.h"
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"
53 :
54 : #define NOTIFY_GLOBAL_OBSERVERS
55 :
56 : #if defined(PR_LOGGING)
57 : // Log module for nsObserverService logging...
58 : //
59 : // To enable logging (see prlog.h for full details):
60 : //
61 : // set NSPR_LOG_MODULES=ObserverService:5
62 : // set NSPR_LOG_FILE=nspr.log
63 : //
64 : // this enables PR_LOG_DEBUG level information and places all output in
65 : // the file nspr.log
66 1396 : PRLogModuleInfo* observerServiceLog = PR_NewLogModule("ObserverService");
67 : #define LOG(x) PR_LOG(observerServiceLog, PR_LOG_DEBUG, x)
68 : #else
69 : #define LOG(x)
70 : #endif /* PR_LOGGING */
71 :
72 : ////////////////////////////////////////////////////////////////////////////////
73 : // nsObserverService Implementation
74 :
75 :
76 445403 : NS_IMPL_THREADSAFE_ISUPPORTS2(nsObserverService, nsIObserverService, nsObserverService)
77 :
78 1365 : nsObserverService::nsObserverService() :
79 1365 : mShuttingDown(false)
80 : {
81 1365 : mObserverTopicTable.Init();
82 1365 : }
83 :
84 2712 : nsObserverService::~nsObserverService(void)
85 : {
86 1356 : Shutdown();
87 1356 : }
88 :
89 : void
90 2721 : nsObserverService::Shutdown()
91 : {
92 2721 : mShuttingDown = true;
93 :
94 2721 : if (mObserverTopicTable.IsInitialized())
95 2721 : mObserverTopicTable.Clear();
96 2721 : }
97 :
98 : nsresult
99 1365 : nsObserverService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
100 : {
101 1365 : LOG(("nsObserverService::Create()"));
102 :
103 2730 : nsRefPtr<nsObserverService> os = new nsObserverService();
104 :
105 1365 : if (!os || !os->mObserverTopicTable.IsInitialized())
106 0 : return NS_ERROR_OUT_OF_MEMORY;
107 :
108 1365 : return os->QueryInterface(aIID, aInstancePtr);
109 : }
110 :
111 : #define NS_ENSURE_VALIDCALL \
112 : if (!NS_IsMainThread()) { \
113 : NS_ERROR("Using observer service off the main thread!"); \
114 : return NS_ERROR_UNEXPECTED; \
115 : } \
116 : if (mShuttingDown) { \
117 : NS_ERROR("Using observer service after XPCOM shutdown!"); \
118 : return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; \
119 : }
120 :
121 : NS_IMETHODIMP
122 95492 : nsObserverService::AddObserver(nsIObserver* anObserver, const char* aTopic,
123 : bool ownsWeak)
124 : {
125 95492 : LOG(("nsObserverService::AddObserver(%p: %s)",
126 : (void*) anObserver, aTopic));
127 :
128 95492 : NS_ENSURE_VALIDCALL
129 95492 : NS_ENSURE_ARG(anObserver && aTopic);
130 :
131 95492 : nsObserverList *observerList = mObserverTopicTable.PutEntry(aTopic);
132 95492 : if (!observerList)
133 0 : return NS_ERROR_OUT_OF_MEMORY;
134 :
135 95492 : return observerList->AddObserver(anObserver, ownsWeak);
136 : }
137 :
138 : NS_IMETHODIMP
139 19540 : nsObserverService::RemoveObserver(nsIObserver* anObserver, const char* aTopic)
140 : {
141 19540 : LOG(("nsObserverService::RemoveObserver(%p: %s)",
142 : (void*) anObserver, aTopic));
143 19540 : NS_ENSURE_VALIDCALL
144 19540 : NS_ENSURE_ARG(anObserver && aTopic);
145 :
146 19540 : nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
147 19540 : if (!observerList)
148 13 : return NS_ERROR_FAILURE;
149 :
150 : /* This death grip is to protect against stupid consumers who call
151 : RemoveObserver from their Destructor, see bug 485834/bug 325392. */
152 39054 : nsCOMPtr<nsIObserver> kungFuDeathGrip(anObserver);
153 19527 : return observerList->RemoveObserver(anObserver);
154 : }
155 :
156 : NS_IMETHODIMP
157 1629 : nsObserverService::EnumerateObservers(const char* aTopic,
158 : nsISimpleEnumerator** anEnumerator)
159 : {
160 1629 : NS_ENSURE_VALIDCALL
161 1629 : NS_ENSURE_ARG(aTopic && anEnumerator);
162 :
163 1629 : nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
164 1629 : if (!observerList)
165 657 : return NS_NewEmptyEnumerator(anEnumerator);
166 :
167 972 : return observerList->GetObserverList(anEnumerator);
168 : }
169 :
170 : // Enumerate observers of aTopic and call Observe on each.
171 66009 : NS_IMETHODIMP nsObserverService::NotifyObservers(nsISupports *aSubject,
172 : const char *aTopic,
173 : const PRUnichar *someData)
174 : {
175 66009 : LOG(("nsObserverService::NotifyObservers(%s)", aTopic));
176 :
177 66009 : NS_ENSURE_VALIDCALL
178 66009 : NS_ENSURE_ARG(aTopic);
179 :
180 66009 : nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
181 66009 : if (observerList)
182 30279 : observerList->NotifyObservers(aSubject, aTopic, someData);
183 :
184 : #ifdef NOTIFY_GLOBAL_OBSERVERS
185 66009 : observerList = mObserverTopicTable.GetEntry("*");
186 66009 : if (observerList)
187 0 : observerList->NotifyObservers(aSubject, aTopic, someData);
188 : #endif
189 :
190 66009 : return NS_OK;
191 4188 : }
192 :
|