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 nsDataHashtable
<nsCStringHashKey
, nsCString
> mHash
;
77 nsCategoryListener
* mListener
;
82 * This is a helper class that caches services that are registered in a certain
83 * category. The intended usage is that a service stores a variable of type
84 * nsCategoryCache<nsIFoo> in a member variable, where nsIFoo is the interface
85 * that these services should implement. The constructor of this class should
86 * then get the name of the category.
89 class nsCategoryCache
: protected nsCategoryListener
{
91 explicit nsCategoryCache(const char* aCategory
);
92 ~nsCategoryCache() { if (mObserver
) mObserver
->ListenerDied(); }
94 const nsCOMArray
<T
>& GetEntries() {
95 // Lazy initialization, so that services in this category can't
96 // cause reentrant getService (bug 386376)
98 mObserver
= new nsCategoryObserver(mCategoryName
.get(), this);
102 virtual void EntryAdded(const nsCString
& aValue
);
103 virtual void EntryRemoved(const nsCString
& aValue
);
104 virtual void CategoryCleared();
106 friend class CategoryObserver
;
108 // Not to be implemented
109 nsCategoryCache(const nsCategoryCache
<T
>&);
111 nsCString mCategoryName
;
112 nsCOMArray
<T
> mEntries
;
113 nsRefPtr
<nsCategoryObserver
> mObserver
;
116 // -----------------------------------
120 nsCategoryCache
<T
>::nsCategoryCache(const char* aCategory
)
121 : mCategoryName(aCategory
)
126 void nsCategoryCache
<T
>::EntryAdded(const nsCString
& aValue
) {
127 nsCOMPtr
<T
> catEntry
= do_GetService(aValue
.get());
129 mEntries
.AppendObject(catEntry
);
133 void nsCategoryCache
<T
>::EntryRemoved(const nsCString
& aValue
) {
134 nsCOMPtr
<T
> catEntry
= do_GetService(aValue
.get());
136 mEntries
.RemoveObject(catEntry
);
140 void nsCategoryCache
<T
>::CategoryCleared() {