1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
---|
2 | /* ***** BEGIN LICENSE BLOCK ***** |
---|
3 | * Version: NPL 1.1/GPL 2.0/LGPL 2.1 |
---|
4 | * |
---|
5 | * The contents of this file are subject to the Netscape Public License |
---|
6 | * Version 1.1 (the "License"); you may not use this file except in |
---|
7 | * compliance with the License. You may obtain a copy of the License at |
---|
8 | * http://www.mozilla.org/NPL/ |
---|
9 | * |
---|
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. |
---|
14 | * |
---|
15 | * The Original Code is mozilla.org code. |
---|
16 | * |
---|
17 | * The Initial Developer of the Original Code is |
---|
18 | * Netscape Communications Corporation. |
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998 |
---|
20 | * the Initial Developer. All Rights Reserved. |
---|
21 | * |
---|
22 | * Contributor(s): |
---|
23 | * |
---|
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 NPL, 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 NPL, the GPL or the LGPL. |
---|
35 | * |
---|
36 | * ***** END LICENSE BLOCK ***** */ |
---|
37 | |
---|
38 | |
---|
39 | #ifndef NSCATEGORYMANAGER_H |
---|
40 | #define NSCATEGORYMANAGER_H |
---|
41 | |
---|
42 | #include "prio.h" |
---|
43 | #include "prlock.h" |
---|
44 | #include "plarena.h" |
---|
45 | #include "nsClassHashtable.h" |
---|
46 | #include "nsICategoryManager.h" |
---|
47 | |
---|
48 | #define NS_CATEGORYMANAGER_CLASSNAME "Category Manager" |
---|
49 | |
---|
50 | /* 16d222a6-1dd2-11b2-b693-f38b02c021b2 */ |
---|
51 | #define NS_CATEGORYMANAGER_CID \ |
---|
52 | { 0x16d222a6, 0x1dd2, 0x11b2, \ |
---|
53 | {0xb6, 0x93, 0xf3, 0x8b, 0x02, 0xc0, 0x21, 0xb2} } |
---|
54 | |
---|
55 | /** |
---|
56 | * a "leaf-node", managed by the nsCategoryNode hashtable. |
---|
57 | * |
---|
58 | * we need to keep a "persistent value" (which will be written to the registry) |
---|
59 | * and a non-persistent value (for the current runtime): these are usually |
---|
60 | * the same, except when aPersist==PR_FALSE. The strings are permanently arena- |
---|
61 | * allocated, and will never go away. |
---|
62 | */ |
---|
63 | class CategoryLeaf : public nsDepCharHashKey |
---|
64 | { |
---|
65 | public: |
---|
66 | CategoryLeaf(const char* aKey) |
---|
67 | : nsDepCharHashKey(aKey), |
---|
68 | pValue(nsnull), |
---|
69 | nonpValue(nsnull) { } |
---|
70 | const char* pValue; |
---|
71 | const char* nonpValue; |
---|
72 | }; |
---|
73 | |
---|
74 | |
---|
75 | /** |
---|
76 | * CategoryNode keeps a hashtable of it's entries. |
---|
77 | * the CategoryNode itself is permanently allocated in |
---|
78 | * the arena. |
---|
79 | */ |
---|
80 | class CategoryNode |
---|
81 | { |
---|
82 | public: |
---|
83 | NS_METHOD GetLeaf(const char* aEntryName, |
---|
84 | char** _retval); |
---|
85 | |
---|
86 | NS_METHOD AddLeaf(const char* aEntryName, |
---|
87 | const char* aValue, |
---|
88 | PRBool aPersist, |
---|
89 | PRBool aReplace, |
---|
90 | char** _retval, |
---|
91 | PLArenaPool* aArena); |
---|
92 | |
---|
93 | NS_METHOD DeleteLeaf(const char* aEntryName, |
---|
94 | PRBool aDontPersist); |
---|
95 | |
---|
96 | void Clear() { |
---|
97 | PR_Lock(mLock); |
---|
98 | mTable.Clear(); |
---|
99 | PR_Unlock(mLock); |
---|
100 | } |
---|
101 | |
---|
102 | PRUint32 Count() { |
---|
103 | PR_Lock(mLock); |
---|
104 | PRUint32 tCount = mTable.Count(); |
---|
105 | PR_Unlock(mLock); |
---|
106 | return tCount; |
---|
107 | } |
---|
108 | |
---|
109 | NS_METHOD Enumerate(nsISimpleEnumerator** _retval); |
---|
110 | |
---|
111 | PRBool WritePersistentEntries(PRFileDesc* fd, const char* aCategoryName); |
---|
112 | |
---|
113 | // CategoryNode is arena-allocated, with the strings |
---|
114 | static CategoryNode* Create(PLArenaPool* aArena); |
---|
115 | ~CategoryNode(); |
---|
116 | void operator delete(void*) { } |
---|
117 | |
---|
118 | private: |
---|
119 | CategoryNode() { } |
---|
120 | void* operator new(size_t aSize, PLArenaPool* aArena); |
---|
121 | |
---|
122 | nsTHashtable<CategoryLeaf> mTable; |
---|
123 | PRLock* mLock; |
---|
124 | }; |
---|
125 | |
---|
126 | |
---|
127 | /** |
---|
128 | * The main implementation of nsICategoryManager. |
---|
129 | * |
---|
130 | * This implementation is thread-safe. |
---|
131 | */ |
---|
132 | class nsCategoryManager |
---|
133 | : public nsICategoryManager |
---|
134 | { |
---|
135 | public: |
---|
136 | NS_DECL_ISUPPORTS |
---|
137 | NS_DECL_NSICATEGORYMANAGER |
---|
138 | |
---|
139 | /** |
---|
140 | * Write the categories to the XPCOM persistent registry. |
---|
141 | * This is to be used by nsComponentManagerImpl (and NO ONE ELSE). |
---|
142 | */ |
---|
143 | NS_METHOD WriteCategoryManagerToRegistry(PRFileDesc* fd); |
---|
144 | |
---|
145 | nsCategoryManager() { } |
---|
146 | private: |
---|
147 | friend class nsCategoryManagerFactory; |
---|
148 | static nsCategoryManager* Create(); |
---|
149 | |
---|
150 | ~nsCategoryManager(); |
---|
151 | |
---|
152 | CategoryNode* get_category(const char* aName); |
---|
153 | |
---|
154 | PLArenaPool mArena; |
---|
155 | nsClassHashtable<nsDepCharHashKey, CategoryNode> mTable; |
---|
156 | PRLock* mLock; |
---|
157 | }; |
---|
158 | |
---|
159 | #endif |
---|