|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 #ifndef NSCATEGORYMANAGER_H |
|
8 #define NSCATEGORYMANAGER_H |
|
9 |
|
10 #include "prio.h" |
|
11 #include "plarena.h" |
|
12 #include "nsClassHashtable.h" |
|
13 #include "nsICategoryManager.h" |
|
14 #include "nsIMemoryReporter.h" |
|
15 #include "mozilla/MemoryReporting.h" |
|
16 #include "mozilla/Mutex.h" |
|
17 #include "mozilla/Attributes.h" |
|
18 |
|
19 class nsIMemoryReporter; |
|
20 |
|
21 /* 16d222a6-1dd2-11b2-b693-f38b02c021b2 */ |
|
22 #define NS_CATEGORYMANAGER_CID \ |
|
23 { 0x16d222a6, 0x1dd2, 0x11b2, \ |
|
24 {0xb6, 0x93, 0xf3, 0x8b, 0x02, 0xc0, 0x21, 0xb2} } |
|
25 |
|
26 /** |
|
27 * a "leaf-node", managed by the nsCategoryNode hashtable. |
|
28 * |
|
29 * we need to keep a "persistent value" (which will be written to the registry) |
|
30 * and a non-persistent value (for the current runtime): these are usually |
|
31 * the same, except when aPersist==false. The strings are permanently arena- |
|
32 * allocated, and will never go away. |
|
33 */ |
|
34 class CategoryLeaf : public nsDepCharHashKey |
|
35 { |
|
36 public: |
|
37 CategoryLeaf(const char* aKey) |
|
38 : nsDepCharHashKey(aKey), |
|
39 value(nullptr) { } |
|
40 const char* value; |
|
41 }; |
|
42 |
|
43 |
|
44 /** |
|
45 * CategoryNode keeps a hashtable of its entries. |
|
46 * the CategoryNode itself is permanently allocated in |
|
47 * the arena. |
|
48 */ |
|
49 class CategoryNode |
|
50 { |
|
51 public: |
|
52 NS_METHOD GetLeaf(const char* aEntryName, |
|
53 char** _retval); |
|
54 |
|
55 NS_METHOD AddLeaf(const char* aEntryName, |
|
56 const char* aValue, |
|
57 bool aReplace, |
|
58 char** _retval, |
|
59 PLArenaPool* aArena); |
|
60 |
|
61 void DeleteLeaf(const char* aEntryName); |
|
62 |
|
63 void Clear() { |
|
64 mozilla::MutexAutoLock lock(mLock); |
|
65 mTable.Clear(); |
|
66 } |
|
67 |
|
68 uint32_t Count() { |
|
69 mozilla::MutexAutoLock lock(mLock); |
|
70 uint32_t tCount = mTable.Count(); |
|
71 return tCount; |
|
72 } |
|
73 |
|
74 NS_METHOD Enumerate(nsISimpleEnumerator** _retval); |
|
75 |
|
76 // CategoryNode is arena-allocated, with the strings |
|
77 static CategoryNode* Create(PLArenaPool* aArena); |
|
78 ~CategoryNode(); |
|
79 void operator delete(void*) { } |
|
80 |
|
81 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf); |
|
82 |
|
83 private: |
|
84 CategoryNode() |
|
85 : mLock("CategoryLeaf") |
|
86 { } |
|
87 |
|
88 void* operator new(size_t aSize, PLArenaPool* aArena); |
|
89 |
|
90 nsTHashtable<CategoryLeaf> mTable; |
|
91 mozilla::Mutex mLock; |
|
92 }; |
|
93 |
|
94 |
|
95 /** |
|
96 * The main implementation of nsICategoryManager. |
|
97 * |
|
98 * This implementation is thread-safe. |
|
99 */ |
|
100 class nsCategoryManager MOZ_FINAL |
|
101 : public nsICategoryManager |
|
102 , public nsIMemoryReporter |
|
103 { |
|
104 public: |
|
105 NS_DECL_ISUPPORTS |
|
106 NS_DECL_NSICATEGORYMANAGER |
|
107 NS_DECL_NSIMEMORYREPORTER |
|
108 |
|
109 /** |
|
110 * Suppress or unsuppress notifications of category changes to the |
|
111 * observer service. This is to be used by nsComponentManagerImpl |
|
112 * on startup while reading the stored category list. |
|
113 */ |
|
114 NS_METHOD SuppressNotifications(bool aSuppress); |
|
115 |
|
116 void AddCategoryEntry(const char* aCategory, |
|
117 const char* aKey, |
|
118 const char* aValue, |
|
119 bool aReplace = true, |
|
120 char** aOldValue = nullptr); |
|
121 |
|
122 static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult); |
|
123 void InitMemoryReporter(); |
|
124 |
|
125 static nsCategoryManager* GetSingleton(); |
|
126 static void Destroy(); |
|
127 |
|
128 private: |
|
129 static nsCategoryManager* gCategoryManager; |
|
130 |
|
131 nsCategoryManager(); |
|
132 ~nsCategoryManager(); |
|
133 |
|
134 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf); |
|
135 |
|
136 CategoryNode* get_category(const char* aName); |
|
137 void NotifyObservers(const char* aTopic, |
|
138 const char* aCategoryName, // must be a static string |
|
139 const char* aEntryName); |
|
140 |
|
141 PLArenaPool mArena; |
|
142 nsClassHashtable<nsDepCharHashKey, CategoryNode> mTable; |
|
143 mozilla::Mutex mLock; |
|
144 bool mSuppressNotifications; |
|
145 }; |
|
146 |
|
147 #endif |