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 | #include "txNamespaceMap.h" |
michael@0 | 7 | #include "nsGkAtoms.h" |
michael@0 | 8 | #include "txXPathNode.h" |
michael@0 | 9 | |
michael@0 | 10 | txNamespaceMap::txNamespaceMap() |
michael@0 | 11 | { |
michael@0 | 12 | } |
michael@0 | 13 | |
michael@0 | 14 | txNamespaceMap::txNamespaceMap(const txNamespaceMap& aOther) |
michael@0 | 15 | : mPrefixes(aOther.mPrefixes) |
michael@0 | 16 | { |
michael@0 | 17 | mNamespaces = aOther.mNamespaces; //bah! I want a copy-constructor! |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | nsresult |
michael@0 | 21 | txNamespaceMap::mapNamespace(nsIAtom* aPrefix, const nsAString& aNamespaceURI) |
michael@0 | 22 | { |
michael@0 | 23 | nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? nullptr : aPrefix; |
michael@0 | 24 | |
michael@0 | 25 | int32_t nsId; |
michael@0 | 26 | if (prefix && aNamespaceURI.IsEmpty()) { |
michael@0 | 27 | // Remove the mapping |
michael@0 | 28 | int32_t index = mPrefixes.IndexOf(prefix); |
michael@0 | 29 | if (index >= 0) { |
michael@0 | 30 | mPrefixes.RemoveObjectAt(index); |
michael@0 | 31 | mNamespaces.RemoveElementAt(index); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | return NS_OK; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | if (aNamespaceURI.IsEmpty()) { |
michael@0 | 38 | // Set default to empty namespace |
michael@0 | 39 | nsId = kNameSpaceID_None; |
michael@0 | 40 | } |
michael@0 | 41 | else { |
michael@0 | 42 | nsId = txNamespaceManager::getNamespaceID(aNamespaceURI); |
michael@0 | 43 | NS_ENSURE_FALSE(nsId == kNameSpaceID_Unknown, NS_ERROR_FAILURE); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | // Check if the mapping already exists |
michael@0 | 47 | int32_t index = mPrefixes.IndexOf(prefix); |
michael@0 | 48 | if (index >= 0) { |
michael@0 | 49 | mNamespaces.ElementAt(index) = nsId; |
michael@0 | 50 | |
michael@0 | 51 | return NS_OK; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | // New mapping |
michael@0 | 55 | if (!mPrefixes.AppendObject(prefix)) { |
michael@0 | 56 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | if (mNamespaces.AppendElement(nsId) == nullptr) { |
michael@0 | 60 | mPrefixes.RemoveObjectAt(mPrefixes.Count() - 1); |
michael@0 | 61 | |
michael@0 | 62 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | return NS_OK; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | int32_t |
michael@0 | 69 | txNamespaceMap::lookupNamespace(nsIAtom* aPrefix) |
michael@0 | 70 | { |
michael@0 | 71 | if (aPrefix == nsGkAtoms::xml) { |
michael@0 | 72 | return kNameSpaceID_XML; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? 0 : aPrefix; |
michael@0 | 76 | |
michael@0 | 77 | int32_t index = mPrefixes.IndexOf(prefix); |
michael@0 | 78 | if (index >= 0) { |
michael@0 | 79 | return mNamespaces.SafeElementAt(index, kNameSpaceID_Unknown); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | if (!prefix) { |
michael@0 | 83 | return kNameSpaceID_None; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | return kNameSpaceID_Unknown; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | int32_t |
michael@0 | 90 | txNamespaceMap::lookupNamespaceWithDefault(const nsAString& aPrefix) |
michael@0 | 91 | { |
michael@0 | 92 | nsCOMPtr<nsIAtom> prefix = do_GetAtom(aPrefix); |
michael@0 | 93 | if (prefix != nsGkAtoms::_poundDefault) { |
michael@0 | 94 | return lookupNamespace(prefix); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | return lookupNamespace(nullptr); |
michael@0 | 98 | } |