1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is a cache for services in a category.
16 * The Initial Developer of the Original Code is
17 * Christian Biesinger <cbiesinger@web.de>.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #ifndef nsCategoryCache_h_
38 #define nsCategoryCache_h_
40 #include "nsICategoryManager.h"
41 #include "nsIObserver.h"
42 #include "nsISimpleEnumerator.h"
43 #include "nsISupportsPrimitives.h"
45 #include "nsServiceManagerUtils.h"
47 #include "nsAutoPtr.h"
48 #include "nsCOMArray.h"
49 #include "nsDataHashtable.h"
53 class NS_NO_VTABLE nsCategoryListener
{
55 // no virtual destructor (people shouldn't delete through an
56 // nsCategoryListener pointer)
57 ~nsCategoryListener() {}
60 virtual void EntryAdded(const nsCString
& aValue
) = 0;
61 virtual void EntryRemoved(const nsCString
& aValue
) = 0;
62 virtual void CategoryCleared() = 0;
65 class NS_COM_GLUE nsCategoryObserver
: public nsIObserver
{
67 nsCategoryObserver(const char* aCategory
,
68 nsCategoryListener
* aCategoryListener
);
69 ~nsCategoryObserver();
76 NS_HIDDEN_(void) RemoveObservers();
78 nsDataHashtable
<nsCStringHashKey
, nsCString
> mHash
;
79 nsCategoryListener
* mListener
;
81 bool mObserversRemoved
;
85 * This is a helper class that caches services that are registered in a certain
86 * category. The intended usage is that a service stores a variable of type
87 * nsCategoryCache<nsIFoo> in a member variable, where nsIFoo is the interface
88 * that these services should implement. The constructor of this class should
89 * then get the name of the category.
92 class nsCategoryCache
: protected nsCategoryListener
{
94 explicit nsCategoryCache(const char* aCategory
);
95 ~nsCategoryCache() { if (mObserver
) mObserver
->ListenerDied(); }
97 const nsCOMArray
<T
>& GetEntries() {
98 // Lazy initialization, so that services in this category can't
99 // cause reentrant getService (bug 386376)
101 mObserver
= new nsCategoryObserver(mCategoryName
.get(), this);
105 virtual void EntryAdded(const nsCString
& aValue
);
106 virtual void EntryRemoved(const nsCString
& aValue
);
107 virtual void CategoryCleared();
109 friend class CategoryObserver
;
111 // Not to be implemented
112 nsCategoryCache(const nsCategoryCache
<T
>&);
114 nsCString mCategoryName
;
115 nsCOMArray
<T
> mEntries
;
116 nsRefPtr
<nsCategoryObserver
> mObserver
;
119 // -----------------------------------
123 nsCategoryCache
<T
>::nsCategoryCache(const char* aCategory
)
124 : mCategoryName(aCategory
)
129 void nsCategoryCache
<T
>::EntryAdded(const nsCString
& aValue
) {
130 nsCOMPtr
<T
> catEntry
= do_GetService(aValue
.get());
132 mEntries
.AppendObject(catEntry
);
136 void nsCategoryCache
<T
>::EntryRemoved(const nsCString
& aValue
) {
137 nsCOMPtr
<T
> catEntry
= do_GetService(aValue
.get());
139 mEntries
.RemoveObject(catEntry
);
143 void nsCategoryCache
<T
>::CategoryCleared() {