1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
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/
8 : *
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
12 : * License.
13 : *
14 : * The Original Code is a cache for services in a category.
15 : *
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.
20 : *
21 : * Contributor(s):
22 : *
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.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #ifndef nsCategoryCache_h_
38 : #define nsCategoryCache_h_
39 :
40 : #include "mozilla/Attributes.h"
41 :
42 : #include "nsICategoryManager.h"
43 : #include "nsIObserver.h"
44 : #include "nsISimpleEnumerator.h"
45 : #include "nsISupportsPrimitives.h"
46 :
47 : #include "nsServiceManagerUtils.h"
48 :
49 : #include "nsAutoPtr.h"
50 : #include "nsCOMArray.h"
51 : #include "nsDataHashtable.h"
52 :
53 : #include "nsXPCOM.h"
54 :
55 3419 : class NS_NO_VTABLE nsCategoryListener {
56 : protected:
57 : // no virtual destructor (people shouldn't delete through an
58 : // nsCategoryListener pointer)
59 3413 : ~nsCategoryListener() {}
60 :
61 : public:
62 : virtual void EntryAdded(const nsCString& aValue) = 0;
63 : virtual void EntryRemoved(const nsCString& aValue) = 0;
64 : virtual void CategoryCleared() = 0;
65 : };
66 :
67 : class NS_COM_GLUE nsCategoryObserver MOZ_FINAL : public nsIObserver {
68 : public:
69 : nsCategoryObserver(const char* aCategory,
70 : nsCategoryListener* aCategoryListener);
71 : ~nsCategoryObserver();
72 :
73 : void ListenerDied();
74 :
75 : NS_DECL_ISUPPORTS
76 : NS_DECL_NSIOBSERVER
77 : private:
78 : NS_HIDDEN_(void) RemoveObservers();
79 :
80 : nsDataHashtable<nsCStringHashKey, nsCString> mHash;
81 : nsCategoryListener* mListener;
82 : nsCString mCategory;
83 : bool mObserversRemoved;
84 : };
85 :
86 : /**
87 : * This is a helper class that caches services that are registered in a certain
88 : * category. The intended usage is that a service stores a variable of type
89 : * nsCategoryCache<nsIFoo> in a member variable, where nsIFoo is the interface
90 : * that these services should implement. The constructor of this class should
91 : * then get the name of the category.
92 : */
93 : template<class T>
94 : class nsCategoryCache : protected nsCategoryListener {
95 : public:
96 : explicit nsCategoryCache(const char* aCategory);
97 3413 : ~nsCategoryCache() { if (mObserver) mObserver->ListenerDied(); }
98 :
99 11293 : const nsCOMArray<T>& GetEntries() {
100 : // Lazy initialization, so that services in this category can't
101 : // cause reentrant getService (bug 386376)
102 11293 : if (!mObserver)
103 778 : mObserver = new nsCategoryObserver(mCategoryName.get(), this);
104 11293 : return mEntries;
105 : }
106 : protected:
107 : virtual void EntryAdded(const nsCString& aValue);
108 : virtual void EntryRemoved(const nsCString& aValue);
109 : virtual void CategoryCleared();
110 : private:
111 : friend class CategoryObserver;
112 :
113 : // Not to be implemented
114 : nsCategoryCache(const nsCategoryCache<T>&);
115 :
116 : nsCString mCategoryName;
117 : nsCOMArray<T> mEntries;
118 : nsRefPtr<nsCategoryObserver> mObserver;
119 : };
120 :
121 : // -----------------------------------
122 : // Implementation
123 :
124 : template<class T>
125 3419 : nsCategoryCache<T>::nsCategoryCache(const char* aCategory)
126 3419 : : mCategoryName(aCategory)
127 : {
128 3419 : }
129 :
130 : template<class T>
131 402 : void nsCategoryCache<T>::EntryAdded(const nsCString& aValue) {
132 804 : nsCOMPtr<T> catEntry = do_GetService(aValue.get());
133 402 : if (catEntry)
134 402 : mEntries.AppendObject(catEntry);
135 402 : }
136 :
137 : template<class T>
138 1 : void nsCategoryCache<T>::EntryRemoved(const nsCString& aValue) {
139 2 : nsCOMPtr<T> catEntry = do_GetService(aValue.get());
140 1 : if (catEntry)
141 1 : mEntries.RemoveObject(catEntry);
142 1 : }
143 :
144 : template<class T>
145 385 : void nsCategoryCache<T>::CategoryCleared() {
146 385 : mEntries.Clear();
147 385 : }
148 :
149 : #endif
|