nsCategoryManager.h 3.8 KB

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