xpcom/components/nsCategoryManager.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial