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.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "txNamespaceMap.h"
7 #include "nsGkAtoms.h"
8 #include "txXPathNode.h"
10 txNamespaceMap::txNamespaceMap()
11 {
12 }
14 txNamespaceMap::txNamespaceMap(const txNamespaceMap& aOther)
15 : mPrefixes(aOther.mPrefixes)
16 {
17 mNamespaces = aOther.mNamespaces; //bah! I want a copy-constructor!
18 }
20 nsresult
21 txNamespaceMap::mapNamespace(nsIAtom* aPrefix, const nsAString& aNamespaceURI)
22 {
23 nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? nullptr : aPrefix;
25 int32_t nsId;
26 if (prefix && aNamespaceURI.IsEmpty()) {
27 // Remove the mapping
28 int32_t index = mPrefixes.IndexOf(prefix);
29 if (index >= 0) {
30 mPrefixes.RemoveObjectAt(index);
31 mNamespaces.RemoveElementAt(index);
32 }
34 return NS_OK;
35 }
37 if (aNamespaceURI.IsEmpty()) {
38 // Set default to empty namespace
39 nsId = kNameSpaceID_None;
40 }
41 else {
42 nsId = txNamespaceManager::getNamespaceID(aNamespaceURI);
43 NS_ENSURE_FALSE(nsId == kNameSpaceID_Unknown, NS_ERROR_FAILURE);
44 }
46 // Check if the mapping already exists
47 int32_t index = mPrefixes.IndexOf(prefix);
48 if (index >= 0) {
49 mNamespaces.ElementAt(index) = nsId;
51 return NS_OK;
52 }
54 // New mapping
55 if (!mPrefixes.AppendObject(prefix)) {
56 return NS_ERROR_OUT_OF_MEMORY;
57 }
59 if (mNamespaces.AppendElement(nsId) == nullptr) {
60 mPrefixes.RemoveObjectAt(mPrefixes.Count() - 1);
62 return NS_ERROR_OUT_OF_MEMORY;
63 }
65 return NS_OK;
66 }
68 int32_t
69 txNamespaceMap::lookupNamespace(nsIAtom* aPrefix)
70 {
71 if (aPrefix == nsGkAtoms::xml) {
72 return kNameSpaceID_XML;
73 }
75 nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? 0 : aPrefix;
77 int32_t index = mPrefixes.IndexOf(prefix);
78 if (index >= 0) {
79 return mNamespaces.SafeElementAt(index, kNameSpaceID_Unknown);
80 }
82 if (!prefix) {
83 return kNameSpaceID_None;
84 }
86 return kNameSpaceID_Unknown;
87 }
89 int32_t
90 txNamespaceMap::lookupNamespaceWithDefault(const nsAString& aPrefix)
91 {
92 nsCOMPtr<nsIAtom> prefix = do_GetAtom(aPrefix);
93 if (prefix != nsGkAtoms::_poundDefault) {
94 return lookupNamespace(prefix);
95 }
97 return lookupNamespace(nullptr);
98 }