xpcom/glue/nsInterfaceHashtable.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 #ifndef nsInterfaceHashtable_h__
michael@0 7 #define nsInterfaceHashtable_h__
michael@0 8
michael@0 9 #include "nsBaseHashtable.h"
michael@0 10 #include "nsHashKeys.h"
michael@0 11 #include "nsCOMPtr.h"
michael@0 12
michael@0 13 /**
michael@0 14 * templated hashtable class maps keys to interface pointers.
michael@0 15 * See nsBaseHashtable for complete declaration.
michael@0 16 * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
michael@0 17 * for a complete specification.
michael@0 18 * @param Interface the interface-type being wrapped
michael@0 19 * @see nsDataHashtable, nsClassHashtable
michael@0 20 */
michael@0 21 template<class KeyClass,class Interface>
michael@0 22 class nsInterfaceHashtable :
michael@0 23 public nsBaseHashtable< KeyClass, nsCOMPtr<Interface> , Interface* >
michael@0 24 {
michael@0 25 public:
michael@0 26 typedef typename KeyClass::KeyType KeyType;
michael@0 27 typedef Interface* UserDataType;
michael@0 28 typedef nsBaseHashtable< KeyClass, nsCOMPtr<Interface> , Interface* >
michael@0 29 base_type;
michael@0 30
michael@0 31 nsInterfaceHashtable()
michael@0 32 {
michael@0 33 }
michael@0 34 explicit nsInterfaceHashtable(uint32_t aInitSize)
michael@0 35 : nsBaseHashtable<KeyClass,nsCOMPtr<Interface>,Interface*>(aInitSize)
michael@0 36 {
michael@0 37 }
michael@0 38
michael@0 39 /**
michael@0 40 * @copydoc nsBaseHashtable::Get
michael@0 41 * @param pData This is an XPCOM getter, so pData is already_addrefed.
michael@0 42 * If the key doesn't exist, pData will be set to nullptr.
michael@0 43 */
michael@0 44 bool Get(KeyType aKey, UserDataType* pData) const;
michael@0 45
michael@0 46 /**
michael@0 47 * @copydoc nsBaseHashtable::Get
michael@0 48 */
michael@0 49 already_AddRefed<Interface> Get(KeyType aKey) const;
michael@0 50
michael@0 51 /**
michael@0 52 * Gets a weak reference to the hashtable entry.
michael@0 53 * @param aFound If not nullptr, will be set to true if the entry is found,
michael@0 54 * to false otherwise.
michael@0 55 * @return The entry, or nullptr if not found. Do not release this pointer!
michael@0 56 */
michael@0 57 Interface* GetWeak(KeyType aKey, bool* aFound = nullptr) const;
michael@0 58 };
michael@0 59
michael@0 60 template <typename K, typename T>
michael@0 61 inline void
michael@0 62 ImplCycleCollectionUnlink(nsInterfaceHashtable<K, T>& aField)
michael@0 63 {
michael@0 64 aField.Clear();
michael@0 65 }
michael@0 66
michael@0 67 template <typename K, typename T>
michael@0 68 inline void
michael@0 69 ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
michael@0 70 const nsInterfaceHashtable<K, T>& aField,
michael@0 71 const char* aName,
michael@0 72 uint32_t aFlags = 0)
michael@0 73 {
michael@0 74 nsBaseHashtableCCTraversalData userData(aCallback, aName, aFlags);
michael@0 75
michael@0 76 aField.EnumerateRead(ImplCycleCollectionTraverse_EnumFunc<typename K::KeyType,T*>,
michael@0 77 &userData);
michael@0 78 }
michael@0 79
michael@0 80 //
michael@0 81 // nsInterfaceHashtable definitions
michael@0 82 //
michael@0 83
michael@0 84 template<class KeyClass,class Interface>
michael@0 85 bool
michael@0 86 nsInterfaceHashtable<KeyClass,Interface>::Get
michael@0 87 (KeyType aKey, UserDataType* pInterface) const
michael@0 88 {
michael@0 89 typename base_type::EntryType* ent = this->GetEntry(aKey);
michael@0 90
michael@0 91 if (ent)
michael@0 92 {
michael@0 93 if (pInterface)
michael@0 94 {
michael@0 95 *pInterface = ent->mData;
michael@0 96
michael@0 97 NS_IF_ADDREF(*pInterface);
michael@0 98 }
michael@0 99
michael@0 100 return true;
michael@0 101 }
michael@0 102
michael@0 103 // if the key doesn't exist, set *pInterface to null
michael@0 104 // so that it is a valid XPCOM getter
michael@0 105 if (pInterface)
michael@0 106 *pInterface = nullptr;
michael@0 107
michael@0 108 return false;
michael@0 109 }
michael@0 110
michael@0 111 template<class KeyClass, class Interface>
michael@0 112 already_AddRefed<Interface>
michael@0 113 nsInterfaceHashtable<KeyClass,Interface>::Get(KeyType aKey) const
michael@0 114 {
michael@0 115 typename base_type::EntryType* ent = this->GetEntry(aKey);
michael@0 116 if (!ent)
michael@0 117 return nullptr;
michael@0 118
michael@0 119 nsCOMPtr<Interface> copy = ent->mData;
michael@0 120 return copy.forget();
michael@0 121 }
michael@0 122
michael@0 123 template<class KeyClass,class Interface>
michael@0 124 Interface*
michael@0 125 nsInterfaceHashtable<KeyClass,Interface>::GetWeak
michael@0 126 (KeyType aKey, bool* aFound) const
michael@0 127 {
michael@0 128 typename base_type::EntryType* ent = this->GetEntry(aKey);
michael@0 129
michael@0 130 if (ent)
michael@0 131 {
michael@0 132 if (aFound)
michael@0 133 *aFound = true;
michael@0 134
michael@0 135 return ent->mData;
michael@0 136 }
michael@0 137
michael@0 138 // Key does not exist, return nullptr and set aFound to false
michael@0 139 if (aFound)
michael@0 140 *aFound = false;
michael@0 141 return nullptr;
michael@0 142 }
michael@0 143
michael@0 144 #endif // nsInterfaceHashtable_h__

mercurial