Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/nsPropertyTable.h
Line
Count
Source (jump to first uncovered line)
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
typedef void
23
(*NSPropertyFunc)(void           *aObject,
24
                  nsAtom        *aPropertyName,
25
                  void           *aPropertyValue,
26
                  void           *aData);
27
28
/**
29
 * Callback type for property destructors.  |aObject| is the object
30
 * the property is being removed for, |aPropertyName| is the property
31
 * being removed, |aPropertyValue| is the value of the property, and |aData|
32
 * is the opaque destructor data that was passed to SetProperty().
33
 **/
34
typedef NSPropertyFunc NSPropertyDtorFunc;
35
class nsINode;
36
class nsIFrame;
37
38
class nsPropertyOwner
39
{
40
public:
41
  nsPropertyOwner(const nsPropertyOwner& aOther) : mObject(aOther.mObject) {}
42
43
  // These are the types of objects that can own properties. No object should
44
  // inherit more then one of these classes.
45
  // To add support for more types just add to this list.
46
  MOZ_IMPLICIT nsPropertyOwner(const nsINode* aObject) : mObject(aObject) {}
47
0
  MOZ_IMPLICIT nsPropertyOwner(const nsIFrame* aObject) : mObject(aObject) {}
48
49
  operator const void*() { return mObject; }
50
  const void* get() { return mObject; }
51
52
private:
53
  const void* mObject;
54
};
55
56
class nsPropertyTable
57
{
58
 public:
59
  /**
60
   * Get the value of the property |aPropertyName| for node |aObject|.
61
   * |aResult|, if supplied, is filled in with a return status code.
62
   **/
63
  void* GetProperty(const nsPropertyOwner& aObject,
64
                    nsAtom    *aPropertyName,
65
                    nsresult   *aResult = nullptr)
66
  {
67
    return GetPropertyInternal(aObject, aPropertyName, false, aResult);
68
  }
69
70
  /**
71
   * Set the value of the property |aPropertyName| to
72
   * |aPropertyValue| for node |aObject|.  |aDtor| is a destructor for the
73
   * property value to be called if the property is removed.  It can be null
74
   * if no destructor is required.  |aDtorData| is an optional pointer to an
75
   * opaque context to be passed to the property destructor.  Note that the
76
   * destructor is global for each property name regardless of node; it is an
77
   * error to set a given property with a different destructor than was used
78
   * before (this will return NS_ERROR_INVALID_ARG). If |aTransfer| is true
79
   * the property will be transfered to the new table when the property table
80
   * for |aObject| changes (currently the tables for nodes are owned by their
81
   * ownerDocument, so if the ownerDocument for a node changes, its property
82
   * table changes too). If |aTransfer| is false the property will just be
83
   * deleted instead.
84
   */
85
  nsresult SetProperty(const nsPropertyOwner& aObject,
86
                       nsAtom* aPropertyName,
87
                       void* aPropertyValue,
88
                       NSPropertyDtorFunc aDtor,
89
                       void* aDtorData,
90
                       bool aTransfer = false)
91
  {
92
    return SetPropertyInternal(aObject, aPropertyName, aPropertyValue,
93
                               aDtor, aDtorData, aTransfer);
94
  }
95
96
  /**
97
   * Delete the property |aPropertyName| in the global category for object
98
   * |aObject|. The property's destructor function will be called.
99
   */
100
  nsresult DeleteProperty(nsPropertyOwner aObject, nsAtom* aPropertyName);
101
102
  /**
103
   * Unset the property |aPropertyName| in the global category for object
104
   * |aObject|, but do not call the property's destructor function.  The
105
   * property value is returned.
106
   */
107
  void* UnsetProperty(const nsPropertyOwner& aObject,
108
                      nsAtom* aPropertyName,
109
                      nsresult* aStatus = nullptr)
110
  {
111
    return GetPropertyInternal(aObject, aPropertyName, true, aStatus);
112
  }
113
114
  /**
115
   * Deletes all of the properties for object |aObject|, calling the
116
   * destructor function for each property.
117
   */
118
  void DeleteAllPropertiesFor(nsPropertyOwner aObject);
119
120
  /**
121
   * Transfers all properties for object |aObject| that were set with the
122
   * |aTransfer| argument as true to |aTable|. Deletes the other properties
123
   * for object |aObject|, calling the destructor function for each property.
124
   * If transfering a property fails, this deletes all the properties for
125
   * object |aObject|.
126
   */
127
  nsresult TransferOrDeleteAllPropertiesFor(nsPropertyOwner aObject,
128
                                            nsPropertyTable& aOtherTable);
129
130
  /**
131
   * Enumerate the properties for object |aObject|.
132
   * For every property |aCallback| will be called with as arguments |aObject|,
133
   * the property name, the property value and |aData|.
134
   */
135
  void Enumerate(nsPropertyOwner aObject, NSPropertyFunc aCallback, void* aData);
136
137
  /**
138
   * Enumerate all the properties.
139
   * For every property |aCallback| will be called with arguments the owner,
140
   * the property name, the property value and |aData|.
141
   */
142
  void EnumerateAll(NSPropertyFunc aCallback, void *aData);
143
144
  /**
145
   * Deletes all of the properties for all objects in the property
146
   * table, calling the destructor function for each property.
147
   */
148
  void DeleteAllProperties();
149
150
  nsPropertyTable() : mPropertyList(nullptr) {}
151
  ~nsPropertyTable() {
152
    DeleteAllProperties();
153
  }
154
155
  /**
156
   * Function useable as destructor function for property data that is
157
   * XPCOM objects. The function will call NS_IF_RELASE on the value
158
   * to destroy it.
159
   */
160
  static void SupportsDtorFunc(void *aObject, nsAtom *aPropertyName,
161
                               void *aPropertyValue, void *aData);
162
163
  class PropertyList;
164
165
  size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
166
  size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
167
168
 private:
169
  void DestroyPropertyList();
170
  PropertyList* GetPropertyListFor(nsAtom* aPropertyName) const;
171
  void* GetPropertyInternal(nsPropertyOwner aObject,
172
                            nsAtom* aPropertyName,
173
                            bool aRemove,
174
                            nsresult* aStatus);
175
  nsresult SetPropertyInternal(nsPropertyOwner aObject,
176
                               nsAtom* aPropertyName,
177
                               void* aPropertyValue,
178
                               NSPropertyDtorFunc  aDtor,
179
                               void* aDtorData,
180
                               bool aTransfer);
181
182
  PropertyList *mPropertyList;
183
};
184
#endif