tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsPropertyTable.h (6618B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /**
      8 * nsPropertyTable allows a set of arbitrary key/value pairs to be stored
      9 * for any number of nodes, in a global hashtable rather than on the nodes
     10 * themselves.  Nodes can be any type of object; the hashtable keys are
     11 * nsAtom pointers, and the values are void pointers.
     12 */
     13 
     14 #ifndef nsPropertyTable_h_
     15 #define nsPropertyTable_h_
     16 
     17 #include "mozilla/MemoryReporting.h"
     18 #include "nscore.h"
     19 
     20 class nsAtom;
     21 
     22 using NSPropertyFunc = void (*)(void* aObject, nsAtom* aPropertyName,
     23                                void* aPropertyValue, void* aData);
     24 
     25 /**
     26 * Callback type for property destructors.  |aObject| is the object
     27 * the property is being removed for, |aPropertyName| is the property
     28 * being removed, |aPropertyValue| is the value of the property, and |aData|
     29 * is the opaque destructor data that was passed to SetProperty().
     30 **/
     31 using NSPropertyDtorFunc = NSPropertyFunc;
     32 class nsINode;
     33 class nsIFrame;
     34 
     35 class nsPropertyOwner {
     36 public:
     37  nsPropertyOwner(const nsPropertyOwner& aOther) = default;
     38 
     39  // These are the types of objects that can own properties. No object should
     40  // inherit more then one of these classes.
     41  // To add support for more types just add to this list.
     42  MOZ_IMPLICIT nsPropertyOwner(const nsINode* aObject) : mObject(aObject) {}
     43  MOZ_IMPLICIT nsPropertyOwner(const nsIFrame* aObject) : mObject(aObject) {}
     44 
     45  operator const void*() { return mObject; }
     46  const void* get() { return mObject; }
     47 
     48 private:
     49  const void* mObject;
     50 };
     51 
     52 class nsPropertyTable {
     53 public:
     54  /**
     55   * Get the value of the property |aPropertyName| for node |aObject|.
     56   * |aResult|, if supplied, is filled in with a return status code.
     57   **/
     58  void* GetProperty(const nsPropertyOwner& aObject, const nsAtom* aPropertyName,
     59                    nsresult* aResult = nullptr) {
     60    return GetPropertyInternal(aObject, aPropertyName, false, aResult);
     61  }
     62 
     63  /**
     64   * Set the value of the property |aPropertyName| to
     65   * |aPropertyValue| for node |aObject|.  |aDtor| is a destructor for the
     66   * property value to be called if the property is removed.  It can be null
     67   * if no destructor is required.  |aDtorData| is an optional pointer to an
     68   * opaque context to be passed to the property destructor.  Note that the
     69   * destructor is global for each property name regardless of node; it is an
     70   * error to set a given property with a different destructor than was used
     71   * before (this will return NS_ERROR_INVALID_ARG). If |aTransfer| is true
     72   * the property will be transfered to the new table when the property table
     73   * for |aObject| changes (currently the tables for nodes are owned by their
     74   * ownerDocument, so if the ownerDocument for a node changes, its property
     75   * table changes too). If |aTransfer| is false the property will just be
     76   * deleted instead.
     77   */
     78  nsresult SetProperty(const nsPropertyOwner& aObject, nsAtom* aPropertyName,
     79                       void* aPropertyValue, NSPropertyDtorFunc aDtor,
     80                       void* aDtorData, bool aTransfer = false) {
     81    return SetPropertyInternal(aObject, aPropertyName, aPropertyValue, aDtor,
     82                               aDtorData, aTransfer);
     83  }
     84 
     85  /**
     86   * Remove the property |aPropertyName| in the global category for object
     87   * |aObject|. The property's destructor function will be called.
     88   */
     89  nsresult RemoveProperty(nsPropertyOwner aObject, const nsAtom* aPropertyName);
     90 
     91  /**
     92   * Remove the property |aPropertyName| in the global category for object
     93   * |aObject|, but do not call the property's destructor function.  The
     94   * property value is returned.
     95   */
     96  void* TakeProperty(const nsPropertyOwner& aObject,
     97                     const nsAtom* aPropertyName, nsresult* aStatus = nullptr) {
     98    return GetPropertyInternal(aObject, aPropertyName, true, aStatus);
     99  }
    100 
    101  /**
    102   * Removes all of the properties for object |aObject|, calling the destructor
    103   * function for each property.
    104   */
    105  void RemoveAllPropertiesFor(nsPropertyOwner aObject);
    106 
    107  /**
    108   * Transfers all properties for object |aObject| that were set with the
    109   * |aTransfer| argument as true to |aTable|. Removes the other properties for
    110   * object |aObject|, calling the destructor function for each property.
    111   * If transfering a property fails, this deletes all the properties for object
    112   * |aObject|.
    113   */
    114  nsresult TransferOrRemoveAllPropertiesFor(nsPropertyOwner aObject,
    115                                            nsPropertyTable& aOtherTable);
    116 
    117  /**
    118   * Enumerate the properties for object |aObject|.
    119   * For every property |aCallback| will be called with as arguments |aObject|,
    120   * the property name, the property value and |aData|.
    121   */
    122  void Enumerate(nsPropertyOwner aObject, NSPropertyFunc aCallback,
    123                 void* aData);
    124 
    125  /**
    126   * Enumerate all the properties.
    127   * For every property |aCallback| will be called with arguments the owner,
    128   * the property name, the property value and |aData|.
    129   */
    130  void EnumerateAll(NSPropertyFunc aCallback, void* aData);
    131 
    132  /**
    133   * Removes all of the properties for all objects in the property table,
    134   * calling the destructor function for each property.
    135   */
    136  void RemoveAllProperties();
    137 
    138  nsPropertyTable() : mPropertyList(nullptr) {}
    139  ~nsPropertyTable() { RemoveAllProperties(); }
    140 
    141  /**
    142   * Function useable as destructor function for property data that is
    143   * XPCOM objects. The function will call NS_IF_RELASE on the value
    144   * to destroy it.
    145   */
    146  static void SupportsDtorFunc(void* aObject, nsAtom* aPropertyName,
    147                               void* aPropertyValue, void* aData);
    148 
    149  class PropertyList;
    150 
    151  size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
    152  size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
    153 
    154 private:
    155  void DestroyPropertyList();
    156  PropertyList* GetPropertyListFor(const nsAtom* aPropertyName) const;
    157  void* GetPropertyInternal(nsPropertyOwner aObject,
    158                            const nsAtom* aPropertyName, bool aRemove,
    159                            nsresult* aStatus);
    160  nsresult SetPropertyInternal(nsPropertyOwner aObject, nsAtom* aPropertyName,
    161                               void* aPropertyValue, NSPropertyDtorFunc aDtor,
    162                               void* aDtorData, bool aTransfer);
    163 
    164  PropertyList* mPropertyList;
    165 };
    166 #endif