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