Tue, 06 Jan 2015 21:39:09 +0100
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: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
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 TRANSFRMX_EXPANDEDNAMEMAP_H |
michael@0 | 7 | #define TRANSFRMX_EXPANDEDNAMEMAP_H |
michael@0 | 8 | |
michael@0 | 9 | #include "nsError.h" |
michael@0 | 10 | #include "txXMLUtils.h" |
michael@0 | 11 | #include "nsTArray.h" |
michael@0 | 12 | |
michael@0 | 13 | class txExpandedNameMap_base { |
michael@0 | 14 | protected: |
michael@0 | 15 | /** |
michael@0 | 16 | * Adds an item, if an item with this key already exists an error is |
michael@0 | 17 | * returned |
michael@0 | 18 | * @param aKey key for item to add |
michael@0 | 19 | * @param aValue value of item to add |
michael@0 | 20 | * @return errorcode |
michael@0 | 21 | */ |
michael@0 | 22 | nsresult addItem(const txExpandedName& aKey, void* aValue); |
michael@0 | 23 | |
michael@0 | 24 | /** |
michael@0 | 25 | * Sets an item, if an item with this key already exists it is overwritten |
michael@0 | 26 | * with the new value |
michael@0 | 27 | * @param aKey key for item to set |
michael@0 | 28 | * @param aValue value of item to set |
michael@0 | 29 | * @return errorcode |
michael@0 | 30 | */ |
michael@0 | 31 | nsresult setItem(const txExpandedName& aKey, void* aValue, |
michael@0 | 32 | void** aOldValue); |
michael@0 | 33 | |
michael@0 | 34 | /** |
michael@0 | 35 | * Gets an item |
michael@0 | 36 | * @param aKey key for item to get |
michael@0 | 37 | * @return item with specified key, or null if no such item exists |
michael@0 | 38 | */ |
michael@0 | 39 | void* getItem(const txExpandedName& aKey) const; |
michael@0 | 40 | |
michael@0 | 41 | /** |
michael@0 | 42 | * Removes an item, deleting it if the map owns the values |
michael@0 | 43 | * @param aKey key for item to remove |
michael@0 | 44 | * @return item with specified key, or null if it has been deleted |
michael@0 | 45 | * or no such item exists |
michael@0 | 46 | */ |
michael@0 | 47 | void* removeItem(const txExpandedName& aKey); |
michael@0 | 48 | |
michael@0 | 49 | /** |
michael@0 | 50 | * Clears the items |
michael@0 | 51 | */ |
michael@0 | 52 | void clearItems() |
michael@0 | 53 | { |
michael@0 | 54 | mItems.Clear(); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | class iterator_base { |
michael@0 | 58 | public: |
michael@0 | 59 | iterator_base(txExpandedNameMap_base& aMap) |
michael@0 | 60 | : mMap(aMap), |
michael@0 | 61 | mCurrentPos(uint32_t(-1)) |
michael@0 | 62 | { |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | bool next() |
michael@0 | 66 | { |
michael@0 | 67 | return ++mCurrentPos < mMap.mItems.Length(); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | const txExpandedName key() |
michael@0 | 71 | { |
michael@0 | 72 | NS_ASSERTION(mCurrentPos < mMap.mItems.Length(), |
michael@0 | 73 | "invalid position in txExpandedNameMap::iterator"); |
michael@0 | 74 | return txExpandedName(mMap.mItems[mCurrentPos].mNamespaceID, |
michael@0 | 75 | mMap.mItems[mCurrentPos].mLocalName); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | protected: |
michael@0 | 79 | void* itemValue() |
michael@0 | 80 | { |
michael@0 | 81 | NS_ASSERTION(mCurrentPos < mMap.mItems.Length(), |
michael@0 | 82 | "invalid position in txExpandedNameMap::iterator"); |
michael@0 | 83 | return mMap.mItems[mCurrentPos].mValue; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | private: |
michael@0 | 87 | txExpandedNameMap_base& mMap; |
michael@0 | 88 | uint32_t mCurrentPos; |
michael@0 | 89 | }; |
michael@0 | 90 | |
michael@0 | 91 | friend class iterator_base; |
michael@0 | 92 | |
michael@0 | 93 | friend class txMapItemComparator; |
michael@0 | 94 | struct MapItem { |
michael@0 | 95 | int32_t mNamespaceID; |
michael@0 | 96 | nsCOMPtr<nsIAtom> mLocalName; |
michael@0 | 97 | void* mValue; |
michael@0 | 98 | }; |
michael@0 | 99 | |
michael@0 | 100 | nsTArray<MapItem> mItems; |
michael@0 | 101 | }; |
michael@0 | 102 | |
michael@0 | 103 | template<class E> |
michael@0 | 104 | class txExpandedNameMap : public txExpandedNameMap_base |
michael@0 | 105 | { |
michael@0 | 106 | public: |
michael@0 | 107 | nsresult add(const txExpandedName& aKey, E* aValue) |
michael@0 | 108 | { |
michael@0 | 109 | return addItem(aKey, (void*)aValue); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | nsresult set(const txExpandedName& aKey, E* aValue) |
michael@0 | 113 | { |
michael@0 | 114 | void* oldValue; |
michael@0 | 115 | return setItem(aKey, (void*)aValue, &oldValue); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | E* get(const txExpandedName& aKey) const |
michael@0 | 119 | { |
michael@0 | 120 | return (E*)getItem(aKey); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | E* remove(const txExpandedName& aKey) |
michael@0 | 124 | { |
michael@0 | 125 | return (E*)removeItem(aKey); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | void clear() |
michael@0 | 129 | { |
michael@0 | 130 | clearItems(); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | class iterator : public iterator_base |
michael@0 | 134 | { |
michael@0 | 135 | public: |
michael@0 | 136 | iterator(txExpandedNameMap& aMap) |
michael@0 | 137 | : iterator_base(aMap) |
michael@0 | 138 | { |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | E* value() |
michael@0 | 142 | { |
michael@0 | 143 | return (E*)itemValue(); |
michael@0 | 144 | } |
michael@0 | 145 | }; |
michael@0 | 146 | }; |
michael@0 | 147 | |
michael@0 | 148 | template<class E> |
michael@0 | 149 | class txOwningExpandedNameMap : public txExpandedNameMap_base |
michael@0 | 150 | { |
michael@0 | 151 | public: |
michael@0 | 152 | ~txOwningExpandedNameMap() |
michael@0 | 153 | { |
michael@0 | 154 | clear(); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | nsresult add(const txExpandedName& aKey, E* aValue) |
michael@0 | 158 | { |
michael@0 | 159 | return addItem(aKey, (void*)aValue); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | nsresult set(const txExpandedName& aKey, E* aValue) |
michael@0 | 163 | { |
michael@0 | 164 | nsAutoPtr<E> oldValue; |
michael@0 | 165 | return setItem(aKey, (void*)aValue, getter_Transfers(oldValue)); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | E* get(const txExpandedName& aKey) const |
michael@0 | 169 | { |
michael@0 | 170 | return (E*)getItem(aKey); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | void remove(const txExpandedName& aKey) |
michael@0 | 174 | { |
michael@0 | 175 | delete (E*)removeItem(aKey); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | void clear() |
michael@0 | 179 | { |
michael@0 | 180 | uint32_t i, len = mItems.Length(); |
michael@0 | 181 | for (i = 0; i < len; ++i) { |
michael@0 | 182 | delete (E*)mItems[i].mValue; |
michael@0 | 183 | } |
michael@0 | 184 | clearItems(); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | class iterator : public iterator_base |
michael@0 | 188 | { |
michael@0 | 189 | public: |
michael@0 | 190 | iterator(txOwningExpandedNameMap& aMap) |
michael@0 | 191 | : iterator_base(aMap) |
michael@0 | 192 | { |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | E* value() |
michael@0 | 196 | { |
michael@0 | 197 | return (E*)itemValue(); |
michael@0 | 198 | } |
michael@0 | 199 | }; |
michael@0 | 200 | }; |
michael@0 | 201 | |
michael@0 | 202 | #endif //TRANSFRMX_EXPANDEDNAMEMAP_H |