1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/components/nsCategoryManager.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,147 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +#ifndef NSCATEGORYMANAGER_H 1.11 +#define NSCATEGORYMANAGER_H 1.12 + 1.13 +#include "prio.h" 1.14 +#include "plarena.h" 1.15 +#include "nsClassHashtable.h" 1.16 +#include "nsICategoryManager.h" 1.17 +#include "nsIMemoryReporter.h" 1.18 +#include "mozilla/MemoryReporting.h" 1.19 +#include "mozilla/Mutex.h" 1.20 +#include "mozilla/Attributes.h" 1.21 + 1.22 +class nsIMemoryReporter; 1.23 + 1.24 +/* 16d222a6-1dd2-11b2-b693-f38b02c021b2 */ 1.25 +#define NS_CATEGORYMANAGER_CID \ 1.26 +{ 0x16d222a6, 0x1dd2, 0x11b2, \ 1.27 + {0xb6, 0x93, 0xf3, 0x8b, 0x02, 0xc0, 0x21, 0xb2} } 1.28 + 1.29 +/** 1.30 + * a "leaf-node", managed by the nsCategoryNode hashtable. 1.31 + * 1.32 + * we need to keep a "persistent value" (which will be written to the registry) 1.33 + * and a non-persistent value (for the current runtime): these are usually 1.34 + * the same, except when aPersist==false. The strings are permanently arena- 1.35 + * allocated, and will never go away. 1.36 + */ 1.37 +class CategoryLeaf : public nsDepCharHashKey 1.38 +{ 1.39 +public: 1.40 + CategoryLeaf(const char* aKey) 1.41 + : nsDepCharHashKey(aKey), 1.42 + value(nullptr) { } 1.43 + const char* value; 1.44 +}; 1.45 + 1.46 + 1.47 +/** 1.48 + * CategoryNode keeps a hashtable of its entries. 1.49 + * the CategoryNode itself is permanently allocated in 1.50 + * the arena. 1.51 + */ 1.52 +class CategoryNode 1.53 +{ 1.54 +public: 1.55 + NS_METHOD GetLeaf(const char* aEntryName, 1.56 + char** _retval); 1.57 + 1.58 + NS_METHOD AddLeaf(const char* aEntryName, 1.59 + const char* aValue, 1.60 + bool aReplace, 1.61 + char** _retval, 1.62 + PLArenaPool* aArena); 1.63 + 1.64 + void DeleteLeaf(const char* aEntryName); 1.65 + 1.66 + void Clear() { 1.67 + mozilla::MutexAutoLock lock(mLock); 1.68 + mTable.Clear(); 1.69 + } 1.70 + 1.71 + uint32_t Count() { 1.72 + mozilla::MutexAutoLock lock(mLock); 1.73 + uint32_t tCount = mTable.Count(); 1.74 + return tCount; 1.75 + } 1.76 + 1.77 + NS_METHOD Enumerate(nsISimpleEnumerator** _retval); 1.78 + 1.79 + // CategoryNode is arena-allocated, with the strings 1.80 + static CategoryNode* Create(PLArenaPool* aArena); 1.81 + ~CategoryNode(); 1.82 + void operator delete(void*) { } 1.83 + 1.84 + size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf); 1.85 + 1.86 +private: 1.87 + CategoryNode() 1.88 + : mLock("CategoryLeaf") 1.89 + { } 1.90 + 1.91 + void* operator new(size_t aSize, PLArenaPool* aArena); 1.92 + 1.93 + nsTHashtable<CategoryLeaf> mTable; 1.94 + mozilla::Mutex mLock; 1.95 +}; 1.96 + 1.97 + 1.98 +/** 1.99 + * The main implementation of nsICategoryManager. 1.100 + * 1.101 + * This implementation is thread-safe. 1.102 + */ 1.103 +class nsCategoryManager MOZ_FINAL 1.104 + : public nsICategoryManager 1.105 + , public nsIMemoryReporter 1.106 +{ 1.107 +public: 1.108 + NS_DECL_ISUPPORTS 1.109 + NS_DECL_NSICATEGORYMANAGER 1.110 + NS_DECL_NSIMEMORYREPORTER 1.111 + 1.112 + /** 1.113 + * Suppress or unsuppress notifications of category changes to the 1.114 + * observer service. This is to be used by nsComponentManagerImpl 1.115 + * on startup while reading the stored category list. 1.116 + */ 1.117 + NS_METHOD SuppressNotifications(bool aSuppress); 1.118 + 1.119 + void AddCategoryEntry(const char* aCategory, 1.120 + const char* aKey, 1.121 + const char* aValue, 1.122 + bool aReplace = true, 1.123 + char** aOldValue = nullptr); 1.124 + 1.125 + static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult); 1.126 + void InitMemoryReporter(); 1.127 + 1.128 + static nsCategoryManager* GetSingleton(); 1.129 + static void Destroy(); 1.130 + 1.131 +private: 1.132 + static nsCategoryManager* gCategoryManager; 1.133 + 1.134 + nsCategoryManager(); 1.135 + ~nsCategoryManager(); 1.136 + 1.137 + size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf); 1.138 + 1.139 + CategoryNode* get_category(const char* aName); 1.140 + void NotifyObservers(const char* aTopic, 1.141 + const char* aCategoryName, // must be a static string 1.142 + const char* aEntryName); 1.143 + 1.144 + PLArenaPool mArena; 1.145 + nsClassHashtable<nsDepCharHashKey, CategoryNode> mTable; 1.146 + mozilla::Mutex mLock; 1.147 + bool mSuppressNotifications; 1.148 +}; 1.149 + 1.150 +#endif