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.

     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/. */
     7 #ifndef NSCATEGORYMANAGER_H
     8 #define NSCATEGORYMANAGER_H
    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"
    19 class nsIMemoryReporter;
    21 /* 16d222a6-1dd2-11b2-b693-f38b02c021b2 */
    22 #define NS_CATEGORYMANAGER_CID \
    23 { 0x16d222a6, 0x1dd2, 0x11b2, \
    24   {0xb6, 0x93, 0xf3, 0x8b, 0x02, 0xc0, 0x21, 0xb2} }
    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 };
    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);
    55   NS_METHOD AddLeaf(const char* aEntryName,
    56                     const char* aValue,
    57                     bool aReplace,
    58                     char** _retval,
    59                     PLArenaPool* aArena);
    61   void DeleteLeaf(const char* aEntryName);
    63   void Clear() {
    64     mozilla::MutexAutoLock lock(mLock);
    65     mTable.Clear();
    66   }
    68   uint32_t Count() {
    69     mozilla::MutexAutoLock lock(mLock);
    70     uint32_t tCount = mTable.Count();
    71     return tCount;
    72   }
    74   NS_METHOD Enumerate(nsISimpleEnumerator** _retval);
    76   // CategoryNode is arena-allocated, with the strings
    77   static CategoryNode* Create(PLArenaPool* aArena);
    78   ~CategoryNode();
    79   void operator delete(void*) { }
    81   size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf);
    83 private:
    84   CategoryNode()
    85     : mLock("CategoryLeaf")
    86   { }
    88   void* operator new(size_t aSize, PLArenaPool* aArena);
    90   nsTHashtable<CategoryLeaf> mTable;
    91   mozilla::Mutex mLock;
    92 };
    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
   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);
   116   void AddCategoryEntry(const char* aCategory,
   117                         const char* aKey,
   118                         const char* aValue,
   119                         bool aReplace = true,
   120                         char** aOldValue = nullptr);
   122   static nsresult Create(nsISupports* aOuter, REFNSIID aIID, void** aResult);
   123   void InitMemoryReporter();
   125   static nsCategoryManager* GetSingleton();
   126   static void Destroy();
   128 private:
   129   static nsCategoryManager* gCategoryManager;
   131   nsCategoryManager();
   132   ~nsCategoryManager();
   134   size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
   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);
   141   PLArenaPool mArena;
   142   nsClassHashtable<nsDepCharHashKey, CategoryNode> mTable;
   143   mozilla::Mutex mLock;
   144   bool mSuppressNotifications;
   145 };
   147 #endif

mercurial