xpcom/glue/nsCategoryCache.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 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef nsCategoryCache_h_
     6 #define nsCategoryCache_h_
     8 #include "mozilla/Attributes.h"
    10 #include "nsICategoryManager.h"
    11 #include "nsIObserver.h"
    12 #include "nsISimpleEnumerator.h"
    13 #include "nsISupportsPrimitives.h"
    15 #include "nsServiceManagerUtils.h"
    17 #include "nsAutoPtr.h"
    18 #include "nsCOMArray.h"
    19 #include "nsInterfaceHashtable.h"
    21 #include "nsXPCOM.h"
    23 class NS_COM_GLUE nsCategoryObserver MOZ_FINAL : public nsIObserver
    24 {
    25   public:
    26     nsCategoryObserver(const char* aCategory);
    27     ~nsCategoryObserver();
    29     void ListenerDied();
    30     nsInterfaceHashtable<nsCStringHashKey, nsISupports>& GetHash()
    31     {
    32       return mHash;
    33     }
    35     NS_DECL_ISUPPORTS
    36     NS_DECL_NSIOBSERVER
    37   private:
    38     void RemoveObservers();
    40     nsInterfaceHashtable<nsCStringHashKey, nsISupports> mHash;
    41     nsCString mCategory;
    42     bool mObserversRemoved;
    43 };
    45 /**
    46  * This is a helper class that caches services that are registered in a certain
    47  * category. The intended usage is that a service stores a variable of type
    48  * nsCategoryCache<nsIFoo> in a member variable, where nsIFoo is the interface
    49  * that these services should implement. The constructor of this class should
    50  * then get the name of the category.
    51  */
    52 template<class T>
    53 class nsCategoryCache MOZ_FINAL
    54 {
    55   public:
    56     explicit nsCategoryCache(const char* aCategory)
    57       : mCategoryName(aCategory)
    58     {
    59     }
    60     ~nsCategoryCache() {
    61       if (mObserver)
    62 	mObserver->ListenerDied();
    63     }
    65     void GetEntries(nsCOMArray<T>& result) {
    66       // Lazy initialization, so that services in this category can't
    67       // cause reentrant getService (bug 386376)
    68       if (!mObserver)
    69         mObserver = new nsCategoryObserver(mCategoryName.get());
    71       mObserver->GetHash().EnumerateRead(EntriesToArray, &result);
    72     }
    74   private:
    75     // Not to be implemented
    76     nsCategoryCache(const nsCategoryCache<T>&);
    78     static PLDHashOperator EntriesToArray(const nsACString& key,
    79 					  nsISupports* entry, void* arg)
    80     {
    81       nsCOMArray<T>& entries = *static_cast<nsCOMArray<T>*>(arg);
    83       nsCOMPtr<T> service = do_QueryInterface(entry);
    84       if (service) {
    85 	entries.AppendObject(service);
    86       }
    87       return PL_DHASH_NEXT;
    88     }
    90     nsCString mCategoryName;
    91     nsRefPtr<nsCategoryObserver> mObserver;
    93 };
    95 #endif

mercurial