VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsObserverService.cpp@ 101986

Last change on this file since 101986 was 101986, checked in by vboxsync, 14 months ago

libs/xpcom: Convert nsObserverService.cpp from PR_LOG to IPRT's Log, bugref:10545

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 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 * Pierre Phaneuf <[email protected]>
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 "nsIFactory.h"
40#include "nsIServiceManager.h"
41#include "nsIComponentManager.h"
42#include "nsIObserverService.h"
43#include "nsObserverService.h"
44#include "nsObserverList.h"
45#include "nsHashtable.h"
46#include "nsIWeakReference.h"
47
48#include <VBox/log.h>
49
50#define NS_WEAK_OBSERVERS
51
52
53
54////////////////////////////////////////////////////////////////////////////////
55// nsObserverService Implementation
56
57
58NS_IMPL_THREADSAFE_ISUPPORTS1(nsObserverService, nsIObserverService)
59
60nsObserverService::nsObserverService()
61 : mObserverTopicTable(nsnull)
62{
63}
64
65nsObserverService::~nsObserverService(void)
66{
67 if(mObserverTopicTable)
68 delete mObserverTopicTable;
69}
70
71NS_METHOD
72nsObserverService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
73{
74 nsresult rv;
75 nsObserverService* os = new nsObserverService();
76 if (os == nsnull)
77 return NS_ERROR_OUT_OF_MEMORY;
78 NS_ADDREF(os);
79 rv = os->QueryInterface(aIID, aInstancePtr);
80 NS_RELEASE(os);
81 return rv;
82}
83
84static PRBool PR_CALLBACK
85ReleaseObserverList(nsHashKey *aKey, void *aData, void* closure)
86{
87 nsObserverList* observerList = NS_STATIC_CAST(nsObserverList*, aData);
88 delete(observerList);
89 return PR_TRUE;
90}
91
92nsresult nsObserverService::GetObserverList(const char* aTopic, nsObserverList** anObserverList)
93{
94 if (anObserverList == nsnull)
95 return NS_ERROR_NULL_POINTER;
96
97 if(mObserverTopicTable == nsnull)
98 {
99 mObserverTopicTable = new nsObjectHashtable(nsnull,
100 nsnull, // should never be cloned
101 ReleaseObserverList,
102 nsnull,
103 256,
104 PR_TRUE);
105 if (mObserverTopicTable == nsnull)
106 return NS_ERROR_OUT_OF_MEMORY;
107 }
108
109
110 nsCStringKey key(aTopic);
111
112 nsObserverList *topicObservers;
113 topicObservers = (nsObserverList *) mObserverTopicTable->Get(&key);
114
115 if (topicObservers)
116 {
117 *anObserverList = topicObservers;
118 return NS_OK;
119 }
120
121 topicObservers = new nsObserverList();
122 if (!topicObservers)
123 return NS_ERROR_OUT_OF_MEMORY;
124
125 *anObserverList = topicObservers;
126 mObserverTopicTable->Put(&key, topicObservers);
127
128 return NS_OK;
129}
130
131NS_IMETHODIMP nsObserverService::AddObserver(nsIObserver* anObserver, const char* aTopic, PRBool ownsWeak)
132{
133 nsObserverList* anObserverList;
134 nsresult rv;
135
136 if (anObserver == nsnull || aTopic == nsnull)
137 return NS_ERROR_NULL_POINTER;
138
139 rv = GetObserverList(aTopic, &anObserverList);
140 if (NS_FAILED(rv)) return rv;
141
142 return anObserverList->AddObserver(anObserver, ownsWeak);
143}
144
145NS_IMETHODIMP nsObserverService::RemoveObserver(nsIObserver* anObserver, const char* aTopic)
146{
147 nsObserverList* anObserverList;
148 nsresult rv;
149
150 if (anObserver == nsnull || aTopic == nsnull)
151 return NS_ERROR_NULL_POINTER;
152
153 rv = GetObserverList(aTopic, &anObserverList);
154 if (NS_FAILED(rv)) return rv;
155
156 return anObserverList->RemoveObserver(anObserver);
157}
158
159NS_IMETHODIMP nsObserverService::EnumerateObservers(const char* aTopic, nsISimpleEnumerator** anEnumerator)
160{
161 nsObserverList* anObserverList;
162 nsresult rv;
163
164 if (anEnumerator == nsnull || aTopic == nsnull)
165 return NS_ERROR_NULL_POINTER;
166
167 rv = GetObserverList(aTopic, &anObserverList);
168 if (NS_FAILED(rv)) return rv;
169
170 return anObserverList->GetObserverList(anEnumerator);
171}
172
173// Enumerate observers of aTopic and call Observe on each.
174NS_IMETHODIMP nsObserverService::NotifyObservers( nsISupports *aSubject,
175 const char *aTopic,
176 const PRUnichar *someData ) {
177 nsresult rv = NS_OK;
178 nsCOMPtr<nsISimpleEnumerator> observers;
179 nsCOMPtr<nsISupports> observerRef;
180
181 rv = EnumerateObservers( aTopic, getter_AddRefs(observers) );
182 if ( NS_FAILED( rv ) )
183 return rv;
184 PRBool loop = PR_TRUE;
185 while( NS_SUCCEEDED(observers->HasMoreElements(&loop)) && loop)
186 {
187 observers->GetNext(getter_AddRefs(observerRef));
188 nsCOMPtr<nsIObserver> observer = do_QueryInterface(observerRef);
189 if ( observer )
190 observer->Observe( aSubject, aTopic, someData );
191#ifdef NS_WEAK_OBSERVERS
192 else
193 { // check for weak reference.
194 nsCOMPtr<nsIWeakReference> weakRef = do_QueryInterface(observerRef);
195 if ( weakRef )
196 weakRef->QueryReferent(NS_GET_IID(nsIObserver), getter_AddRefs(observer));
197
198 if ( observer )
199 observer->Observe( aSubject, aTopic, someData );
200
201 Log(("Notification - %s\n", aTopic ? aTopic : "undefined"));
202 }
203#endif
204 }
205 return NS_OK;
206}
207////////////////////////////////////////////////////////////////////////////////
208
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