Back to index

lightning-sunbird  0.9+nobinonly
nsCategoryCache.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is a cache for services in a category.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Christian Biesinger <cbiesinger@web.de>.
00018  * Portions created by the Initial Developer are Copyright (C) 2005
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #ifndef nsCategoryCache_h_
00038 #define nsCategoryCache_h_
00039 
00040 #include "nsICategoryManager.h"
00041 #include "nsIObserver.h"
00042 #include "nsISimpleEnumerator.h"
00043 #include "nsISupportsPrimitives.h"
00044 
00045 #include "nsServiceManagerUtils.h"
00046 
00047 #include "nsAutoPtr.h"
00048 #include "nsCOMArray.h"
00049 #include "nsDataHashtable.h"
00050 
00051 #include "nsXPCOM.h"
00052 
00053 class NS_NO_VTABLE nsCategoryListener {
00054   protected:
00055     // no virtual destructor (people shouldn't delete through an
00056     // nsCategoryListener pointer)
00057     ~nsCategoryListener() {}
00058 
00059   public:
00060     virtual void EntryAdded(const nsCString& aValue) = 0;
00061     virtual void EntryRemoved(const nsCString& aValue) = 0;
00062     virtual void CategoryCleared() = 0;
00063 };
00064 
00065 class NS_COM nsCategoryObserver : public nsIObserver {
00066   public:
00067     nsCategoryObserver(const char* aCategory,
00068                        nsCategoryListener* aCategoryListener);
00069     ~nsCategoryObserver();
00070 
00071     void ListenerDied();
00072 
00073     NS_DECL_ISUPPORTS
00074     NS_DECL_NSIOBSERVER
00075   private:
00076     nsDataHashtable<nsCStringHashKey, nsCString> mHash;
00077     nsCategoryListener*                          mListener;
00078     nsCString                                    mCategory;
00079 };
00080 
00088 template<class T>
00089 class nsCategoryCache : protected nsCategoryListener {
00090   public:
00091     explicit nsCategoryCache(const char* aCategory);
00092     ~nsCategoryCache() { if (mObserver) mObserver->ListenerDied(); }
00093 
00094     const nsCOMArray<T>& GetEntries() {
00095       // Lazy initialization, so that services in this category can't
00096       // cause reentrant getService (bug 386376)
00097       if (!mObserver)
00098         mObserver = new nsCategoryObserver(mCategoryName.get(), this);
00099       return mEntries;
00100     }
00101   protected:
00102     virtual void EntryAdded(const nsCString& aValue);
00103     virtual void EntryRemoved(const nsCString& aValue);
00104     virtual void CategoryCleared();
00105   private:
00106     friend class CategoryObserver;
00107 
00108     // Not to be implemented
00109     nsCategoryCache(const nsCategoryCache<T>&);
00110 
00111     nsCString mCategoryName;
00112     nsCOMArray<T> mEntries;
00113     nsRefPtr<nsCategoryObserver> mObserver;
00114 };
00115 
00116 // -----------------------------------
00117 // Implementation
00118 
00119 template<class T>
00120 nsCategoryCache<T>::nsCategoryCache(const char* aCategory)
00121 : mCategoryName(aCategory)
00122 {
00123 }
00124 
00125 template<class T>
00126 void nsCategoryCache<T>::EntryAdded(const nsCString& aValue) {
00127   nsCOMPtr<T> catEntry = do_GetService(aValue.get());
00128   if (catEntry)
00129     mEntries.AppendObject(catEntry);
00130 }
00131 
00132 template<class T>
00133 void nsCategoryCache<T>::EntryRemoved(const nsCString& aValue) {
00134   nsCOMPtr<T> catEntry = do_GetService(aValue.get());
00135   if (catEntry)
00136     mEntries.RemoveObject(catEntry);
00137 }
00138 
00139 template<class T>
00140 void nsCategoryCache<T>::CategoryCleared() {
00141   mEntries.Clear();
00142 }
00143 
00144 #endif