dom/xslt/base/txExpandedNameMap.cpp

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: 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 #include "txExpandedNameMap.h"
michael@0 7 #include "txCore.h"
michael@0 8
michael@0 9 class txMapItemComparator
michael@0 10 {
michael@0 11 public:
michael@0 12 bool Equals(const txExpandedNameMap_base::MapItem& aItem,
michael@0 13 const txExpandedName& aKey) const {
michael@0 14 return aItem.mNamespaceID == aKey.mNamespaceID &&
michael@0 15 aItem.mLocalName == aKey.mLocalName;
michael@0 16 }
michael@0 17 };
michael@0 18
michael@0 19 /**
michael@0 20 * Adds an item, if an item with this key already exists an error is
michael@0 21 * returned
michael@0 22 * @param aKey key for item to add
michael@0 23 * @param aValue value of item to add
michael@0 24 * @return errorcode
michael@0 25 */
michael@0 26 nsresult txExpandedNameMap_base::addItem(const txExpandedName& aKey,
michael@0 27 void* aValue)
michael@0 28 {
michael@0 29 uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
michael@0 30 if (pos != mItems.NoIndex) {
michael@0 31 return NS_ERROR_XSLT_ALREADY_SET;
michael@0 32 }
michael@0 33
michael@0 34 MapItem* item = mItems.AppendElement();
michael@0 35 NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
michael@0 36
michael@0 37 item->mNamespaceID = aKey.mNamespaceID;
michael@0 38 item->mLocalName = aKey.mLocalName;
michael@0 39 item->mValue = aValue;
michael@0 40
michael@0 41 return NS_OK;
michael@0 42 }
michael@0 43
michael@0 44 /**
michael@0 45 * Sets an item, if an item with this key already exists it is overwritten
michael@0 46 * with the new value
michael@0 47 * @param aKey key for item to set
michael@0 48 * @param aValue value of item to set
michael@0 49 * @return errorcode
michael@0 50 */
michael@0 51 nsresult txExpandedNameMap_base::setItem(const txExpandedName& aKey,
michael@0 52 void* aValue,
michael@0 53 void** aOldValue)
michael@0 54 {
michael@0 55 *aOldValue = nullptr;
michael@0 56 uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
michael@0 57 if (pos != mItems.NoIndex) {
michael@0 58 *aOldValue = mItems[pos].mValue;
michael@0 59 mItems[pos].mValue = aValue;
michael@0 60
michael@0 61 return NS_OK;
michael@0 62 }
michael@0 63
michael@0 64 MapItem* item = mItems.AppendElement();
michael@0 65 NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
michael@0 66
michael@0 67 item->mNamespaceID = aKey.mNamespaceID;
michael@0 68 item->mLocalName = aKey.mLocalName;
michael@0 69 item->mValue = aValue;
michael@0 70
michael@0 71 return NS_OK;
michael@0 72 }
michael@0 73
michael@0 74 /**
michael@0 75 * Gets an item
michael@0 76 * @param aKey key for item to get
michael@0 77 * @return item with specified key, or null if no such item exists
michael@0 78 */
michael@0 79 void* txExpandedNameMap_base::getItem(const txExpandedName& aKey) const
michael@0 80 {
michael@0 81 uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
michael@0 82 if (pos != mItems.NoIndex) {
michael@0 83 return mItems[pos].mValue;
michael@0 84 }
michael@0 85
michael@0 86 return nullptr;
michael@0 87 }
michael@0 88
michael@0 89 /**
michael@0 90 * Removes an item, deleting it if the map owns the values
michael@0 91 * @param aKey key for item to remove
michael@0 92 * @return item with specified key, or null if it has been deleted
michael@0 93 * or no such item exists
michael@0 94 */
michael@0 95 void* txExpandedNameMap_base::removeItem(const txExpandedName& aKey)
michael@0 96 {
michael@0 97 void* value = nullptr;
michael@0 98 uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
michael@0 99 if (pos != mItems.NoIndex) {
michael@0 100 value = mItems[pos].mValue;
michael@0 101 mItems.RemoveElementAt(pos);
michael@0 102 }
michael@0 103
michael@0 104 return value;
michael@0 105 }

mercurial