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