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 txXPathTreeWalker_h__ |
michael@0 | 7 | #define txXPathTreeWalker_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "txCore.h" |
michael@0 | 10 | #include "txXPathNode.h" |
michael@0 | 11 | #include "nsIContentInlines.h" |
michael@0 | 12 | #include "nsINodeInfo.h" |
michael@0 | 13 | #include "nsTArray.h" |
michael@0 | 14 | |
michael@0 | 15 | class nsIAtom; |
michael@0 | 16 | class nsIDOMDocument; |
michael@0 | 17 | |
michael@0 | 18 | class txUint32Array : public nsTArray<uint32_t> |
michael@0 | 19 | { |
michael@0 | 20 | public: |
michael@0 | 21 | bool AppendValue(uint32_t aValue) |
michael@0 | 22 | { |
michael@0 | 23 | return AppendElement(aValue) != nullptr; |
michael@0 | 24 | } |
michael@0 | 25 | bool RemoveValueAt(uint32_t aIndex) |
michael@0 | 26 | { |
michael@0 | 27 | if (aIndex < Length()) { |
michael@0 | 28 | RemoveElementAt(aIndex); |
michael@0 | 29 | } |
michael@0 | 30 | return true; |
michael@0 | 31 | } |
michael@0 | 32 | uint32_t ValueAt(uint32_t aIndex) const |
michael@0 | 33 | { |
michael@0 | 34 | return (aIndex < Length()) ? ElementAt(aIndex) : 0; |
michael@0 | 35 | } |
michael@0 | 36 | }; |
michael@0 | 37 | |
michael@0 | 38 | class txXPathTreeWalker |
michael@0 | 39 | { |
michael@0 | 40 | public: |
michael@0 | 41 | txXPathTreeWalker(const txXPathTreeWalker& aOther); |
michael@0 | 42 | explicit txXPathTreeWalker(const txXPathNode& aNode); |
michael@0 | 43 | |
michael@0 | 44 | bool getAttr(nsIAtom* aLocalName, int32_t aNSID, nsAString& aValue) const; |
michael@0 | 45 | int32_t getNamespaceID() const; |
michael@0 | 46 | uint16_t getNodeType() const; |
michael@0 | 47 | void appendNodeValue(nsAString& aResult) const; |
michael@0 | 48 | void getNodeName(nsAString& aName) const; |
michael@0 | 49 | |
michael@0 | 50 | void moveTo(const txXPathTreeWalker& aWalker); |
michael@0 | 51 | |
michael@0 | 52 | void moveToRoot(); |
michael@0 | 53 | bool moveToParent(); |
michael@0 | 54 | bool moveToElementById(const nsAString& aID); |
michael@0 | 55 | bool moveToFirstAttribute(); |
michael@0 | 56 | bool moveToNextAttribute(); |
michael@0 | 57 | bool moveToNamedAttribute(nsIAtom* aLocalName, int32_t aNSID); |
michael@0 | 58 | bool moveToFirstChild(); |
michael@0 | 59 | bool moveToLastChild(); |
michael@0 | 60 | bool moveToNextSibling(); |
michael@0 | 61 | bool moveToPreviousSibling(); |
michael@0 | 62 | |
michael@0 | 63 | bool isOnNode(const txXPathNode& aNode) const; |
michael@0 | 64 | |
michael@0 | 65 | const txXPathNode& getCurrentPosition() const; |
michael@0 | 66 | |
michael@0 | 67 | private: |
michael@0 | 68 | txXPathNode mPosition; |
michael@0 | 69 | |
michael@0 | 70 | bool moveToValidAttribute(uint32_t aStartIndex); |
michael@0 | 71 | bool moveToSibling(int32_t aDir); |
michael@0 | 72 | |
michael@0 | 73 | uint32_t mCurrentIndex; |
michael@0 | 74 | txUint32Array mDescendants; |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | class txXPathNodeUtils |
michael@0 | 78 | { |
michael@0 | 79 | public: |
michael@0 | 80 | static bool getAttr(const txXPathNode& aNode, nsIAtom* aLocalName, |
michael@0 | 81 | int32_t aNSID, nsAString& aValue); |
michael@0 | 82 | static already_AddRefed<nsIAtom> getLocalName(const txXPathNode& aNode); |
michael@0 | 83 | static nsIAtom* getPrefix(const txXPathNode& aNode); |
michael@0 | 84 | static void getLocalName(const txXPathNode& aNode, nsAString& aLocalName); |
michael@0 | 85 | static void getNodeName(const txXPathNode& aNode, |
michael@0 | 86 | nsAString& aName); |
michael@0 | 87 | static int32_t getNamespaceID(const txXPathNode& aNode); |
michael@0 | 88 | static void getNamespaceURI(const txXPathNode& aNode, nsAString& aURI); |
michael@0 | 89 | static uint16_t getNodeType(const txXPathNode& aNode); |
michael@0 | 90 | static void appendNodeValue(const txXPathNode& aNode, nsAString& aResult); |
michael@0 | 91 | static bool isWhitespace(const txXPathNode& aNode); |
michael@0 | 92 | static txXPathNode* getOwnerDocument(const txXPathNode& aNode); |
michael@0 | 93 | static int32_t getUniqueIdentifier(const txXPathNode& aNode); |
michael@0 | 94 | static nsresult getXSLTId(const txXPathNode& aNode, |
michael@0 | 95 | const txXPathNode& aBase, nsAString& aResult); |
michael@0 | 96 | static void release(txXPathNode* aNode); |
michael@0 | 97 | static void getBaseURI(const txXPathNode& aNode, nsAString& aURI); |
michael@0 | 98 | static int comparePosition(const txXPathNode& aNode, |
michael@0 | 99 | const txXPathNode& aOtherNode); |
michael@0 | 100 | static bool localNameEquals(const txXPathNode& aNode, |
michael@0 | 101 | nsIAtom* aLocalName); |
michael@0 | 102 | static bool isRoot(const txXPathNode& aNode); |
michael@0 | 103 | static bool isElement(const txXPathNode& aNode); |
michael@0 | 104 | static bool isAttribute(const txXPathNode& aNode); |
michael@0 | 105 | static bool isProcessingInstruction(const txXPathNode& aNode); |
michael@0 | 106 | static bool isComment(const txXPathNode& aNode); |
michael@0 | 107 | static bool isText(const txXPathNode& aNode); |
michael@0 | 108 | static inline bool isHTMLElementInHTMLDocument(const txXPathNode& aNode) |
michael@0 | 109 | { |
michael@0 | 110 | if (!aNode.isContent()) { |
michael@0 | 111 | return false; |
michael@0 | 112 | } |
michael@0 | 113 | nsIContent* content = aNode.Content(); |
michael@0 | 114 | return content->IsHTML() && content->IsInHTMLDocument(); |
michael@0 | 115 | } |
michael@0 | 116 | }; |
michael@0 | 117 | |
michael@0 | 118 | class txXPathNativeNode |
michael@0 | 119 | { |
michael@0 | 120 | public: |
michael@0 | 121 | static txXPathNode* createXPathNode(nsIDOMNode* aNode, |
michael@0 | 122 | bool aKeepRootAlive = false); |
michael@0 | 123 | static txXPathNode* createXPathNode(nsIContent* aContent, |
michael@0 | 124 | bool aKeepRootAlive = false); |
michael@0 | 125 | static txXPathNode* createXPathNode(nsIDOMDocument* aDocument); |
michael@0 | 126 | static nsresult getNode(const txXPathNode& aNode, nsIDOMNode** aResult); |
michael@0 | 127 | static nsIContent* getContent(const txXPathNode& aNode); |
michael@0 | 128 | static nsIDocument* getDocument(const txXPathNode& aNode); |
michael@0 | 129 | static void addRef(const txXPathNode& aNode) |
michael@0 | 130 | { |
michael@0 | 131 | NS_ADDREF(aNode.mNode); |
michael@0 | 132 | } |
michael@0 | 133 | static void release(const txXPathNode& aNode) |
michael@0 | 134 | { |
michael@0 | 135 | nsINode *node = aNode.mNode; |
michael@0 | 136 | NS_RELEASE(node); |
michael@0 | 137 | } |
michael@0 | 138 | }; |
michael@0 | 139 | |
michael@0 | 140 | inline const txXPathNode& |
michael@0 | 141 | txXPathTreeWalker::getCurrentPosition() const |
michael@0 | 142 | { |
michael@0 | 143 | return mPosition; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | inline bool |
michael@0 | 147 | txXPathTreeWalker::getAttr(nsIAtom* aLocalName, int32_t aNSID, |
michael@0 | 148 | nsAString& aValue) const |
michael@0 | 149 | { |
michael@0 | 150 | return txXPathNodeUtils::getAttr(mPosition, aLocalName, aNSID, aValue); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | inline int32_t |
michael@0 | 154 | txXPathTreeWalker::getNamespaceID() const |
michael@0 | 155 | { |
michael@0 | 156 | return txXPathNodeUtils::getNamespaceID(mPosition); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | inline void |
michael@0 | 160 | txXPathTreeWalker::appendNodeValue(nsAString& aResult) const |
michael@0 | 161 | { |
michael@0 | 162 | txXPathNodeUtils::appendNodeValue(mPosition, aResult); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | inline void |
michael@0 | 166 | txXPathTreeWalker::getNodeName(nsAString& aName) const |
michael@0 | 167 | { |
michael@0 | 168 | txXPathNodeUtils::getNodeName(mPosition, aName); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | inline void |
michael@0 | 172 | txXPathTreeWalker::moveTo(const txXPathTreeWalker& aWalker) |
michael@0 | 173 | { |
michael@0 | 174 | nsINode *root = nullptr; |
michael@0 | 175 | if (mPosition.mRefCountRoot) { |
michael@0 | 176 | root = mPosition.Root(); |
michael@0 | 177 | } |
michael@0 | 178 | mPosition.mIndex = aWalker.mPosition.mIndex; |
michael@0 | 179 | mPosition.mRefCountRoot = aWalker.mPosition.mRefCountRoot; |
michael@0 | 180 | mPosition.mNode = aWalker.mPosition.mNode; |
michael@0 | 181 | nsINode *newRoot = nullptr; |
michael@0 | 182 | if (mPosition.mRefCountRoot) { |
michael@0 | 183 | newRoot = mPosition.Root(); |
michael@0 | 184 | } |
michael@0 | 185 | if (root != newRoot) { |
michael@0 | 186 | NS_IF_ADDREF(newRoot); |
michael@0 | 187 | NS_IF_RELEASE(root); |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | mCurrentIndex = aWalker.mCurrentIndex; |
michael@0 | 191 | mDescendants.Clear(); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | inline bool |
michael@0 | 195 | txXPathTreeWalker::isOnNode(const txXPathNode& aNode) const |
michael@0 | 196 | { |
michael@0 | 197 | return (mPosition == aNode); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | /* static */ |
michael@0 | 201 | inline int32_t |
michael@0 | 202 | txXPathNodeUtils::getUniqueIdentifier(const txXPathNode& aNode) |
michael@0 | 203 | { |
michael@0 | 204 | NS_PRECONDITION(!aNode.isAttribute(), |
michael@0 | 205 | "Not implemented for attributes."); |
michael@0 | 206 | return NS_PTR_TO_INT32(aNode.mNode); |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | /* static */ |
michael@0 | 210 | inline void |
michael@0 | 211 | txXPathNodeUtils::release(txXPathNode* aNode) |
michael@0 | 212 | { |
michael@0 | 213 | NS_RELEASE(aNode->mNode); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | /* static */ |
michael@0 | 217 | inline bool |
michael@0 | 218 | txXPathNodeUtils::localNameEquals(const txXPathNode& aNode, |
michael@0 | 219 | nsIAtom* aLocalName) |
michael@0 | 220 | { |
michael@0 | 221 | if (aNode.isContent() && |
michael@0 | 222 | aNode.Content()->IsElement()) { |
michael@0 | 223 | return aNode.Content()->NodeInfo()->Equals(aLocalName); |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | nsCOMPtr<nsIAtom> localName = txXPathNodeUtils::getLocalName(aNode); |
michael@0 | 227 | |
michael@0 | 228 | return localName == aLocalName; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | /* static */ |
michael@0 | 232 | inline bool |
michael@0 | 233 | txXPathNodeUtils::isRoot(const txXPathNode& aNode) |
michael@0 | 234 | { |
michael@0 | 235 | return !aNode.isAttribute() && !aNode.mNode->GetParentNode(); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | /* static */ |
michael@0 | 239 | inline bool |
michael@0 | 240 | txXPathNodeUtils::isElement(const txXPathNode& aNode) |
michael@0 | 241 | { |
michael@0 | 242 | return aNode.isContent() && |
michael@0 | 243 | aNode.Content()->IsElement(); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | |
michael@0 | 247 | /* static */ |
michael@0 | 248 | inline bool |
michael@0 | 249 | txXPathNodeUtils::isAttribute(const txXPathNode& aNode) |
michael@0 | 250 | { |
michael@0 | 251 | return aNode.isAttribute(); |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | /* static */ |
michael@0 | 255 | inline bool |
michael@0 | 256 | txXPathNodeUtils::isProcessingInstruction(const txXPathNode& aNode) |
michael@0 | 257 | { |
michael@0 | 258 | return aNode.isContent() && |
michael@0 | 259 | aNode.Content()->IsNodeOfType(nsINode::ePROCESSING_INSTRUCTION); |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | /* static */ |
michael@0 | 263 | inline bool |
michael@0 | 264 | txXPathNodeUtils::isComment(const txXPathNode& aNode) |
michael@0 | 265 | { |
michael@0 | 266 | return aNode.isContent() && |
michael@0 | 267 | aNode.Content()->IsNodeOfType(nsINode::eCOMMENT); |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | /* static */ |
michael@0 | 271 | inline bool |
michael@0 | 272 | txXPathNodeUtils::isText(const txXPathNode& aNode) |
michael@0 | 273 | { |
michael@0 | 274 | return aNode.isContent() && |
michael@0 | 275 | aNode.Content()->IsNodeOfType(nsINode::eTEXT); |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | #endif /* txXPathTreeWalker_h__ */ |