dom/xslt/base/txExpandedNameMap.h

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.

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

mercurial