Bug 449749 nsWindowWatcher.cpp failed to compile with Sun Studio 12 r+sr=roc
[wine-gecko.git] / xpcom / glue / nsCategoryCache.cpp
blob1ce55aefa6523faa1befc06208eeb73060947672
1 /* vim:set st=2 sts=2 ts=2 et cin: */
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
13 * License.
15 * The Original Code is a cache for services in a category.
17 * The Initial Developer of the Original Code is
18 * Christian Biesinger <cbiesinger@web.de>.
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * 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.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsIObserverService.h"
39 #include "nsISupportsPrimitives.h"
41 #include "nsXPCOMCID.h"
43 #include "nsCategoryCache.h"
45 nsCategoryObserver::nsCategoryObserver(const char* aCategory,
46 nsCategoryListener* aListener)
47 : mListener(nsnull), mCategory(aCategory)
49 if (!mHash.Init()) {
50 // OOM
51 return;
54 mListener = aListener;
56 // First, enumerate the currently existing entries
57 nsCOMPtr<nsICategoryManager> catMan =
58 do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
59 if (!catMan)
60 return;
62 nsCOMPtr<nsISimpleEnumerator> enumerator;
63 nsresult rv = catMan->EnumerateCategory(aCategory,
64 getter_AddRefs(enumerator));
65 if (NS_FAILED(rv))
66 return;
68 nsCOMPtr<nsISupports> entry;
69 while (NS_SUCCEEDED(enumerator->GetNext(getter_AddRefs(entry)))) {
70 nsCOMPtr<nsISupportsCString> entryName = do_QueryInterface(entry, &rv);
72 if (NS_SUCCEEDED(rv)) {
73 nsCAutoString categoryEntry;
74 rv = entryName->GetData(categoryEntry);
76 nsCString entryValue;
77 catMan->GetCategoryEntry(aCategory,
78 categoryEntry.get(),
79 getter_Copies(entryValue));
81 if (NS_SUCCEEDED(rv)) {
82 mHash.Put(categoryEntry, entryValue);
83 mListener->EntryAdded(entryValue);
88 // Now, listen for changes
89 nsCOMPtr<nsIObserverService> serv =
90 do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
91 if (!serv)
92 return;
94 serv->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
96 serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID, PR_FALSE);
97 serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID, PR_FALSE);
98 serv->AddObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID, PR_FALSE);
101 nsCategoryObserver::~nsCategoryObserver() {
104 NS_IMPL_ISUPPORTS1(nsCategoryObserver, nsIObserver)
106 void
107 nsCategoryObserver::ListenerDied() {
108 mListener = nsnull;
110 nsCOMPtr<nsIObserverService> serv =
111 do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
112 if (!serv)
113 return;
115 serv->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
117 serv->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID);
118 serv->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID);
119 serv->RemoveObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID);
122 NS_IMETHODIMP
123 nsCategoryObserver::Observe(nsISupports* aSubject, const char* aTopic,
124 const PRUnichar* aData) {
125 if (!mListener)
126 return NS_OK;
128 if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
129 mHash.Clear();
130 mListener->CategoryCleared();
131 return NS_OK;
134 if (!aData ||
135 !nsDependentString(aData).Equals(NS_ConvertASCIItoUTF16(mCategory)))
136 return NS_OK;
138 nsCAutoString str;
139 nsCOMPtr<nsISupportsCString> strWrapper(do_QueryInterface(aSubject));
140 if (strWrapper)
141 strWrapper->GetData(str);
143 if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID) == 0) {
144 nsCOMPtr<nsICategoryManager> catMan =
145 do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
146 if (!catMan)
147 return NS_OK;
149 nsCString entryValue;
150 catMan->GetCategoryEntry(mCategory.get(),
151 str.get(),
152 getter_Copies(entryValue));
154 mHash.Put(str, entryValue);
155 mListener->EntryAdded(entryValue);
156 } else if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID) == 0) {
157 nsCAutoString val;
158 if (mHash.Get(str, &val)) {
159 mHash.Remove(str);
160 mListener->EntryRemoved(val);
162 } else if (strcmp(aTopic, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID) == 0) {
163 mHash.Clear();
164 mListener->CategoryCleared();
166 return NS_OK;