Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/nsMappedAttributes.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
 * A unique per-element set of attributes that is used as an
9
 * nsIStyleRule; used to implement presentational attributes.
10
 */
11
12
#ifndef nsMappedAttributes_h___
13
#define nsMappedAttributes_h___
14
15
#include "AttrArray.h"
16
#include "nsMappedAttributeElement.h"
17
#include "mozilla/Attributes.h"
18
#include "mozilla/ServoBindingTypes.h"
19
#include "mozilla/MemoryReporting.h"
20
21
class nsAtom;
22
class nsHTMLStyleSheet;
23
24
class nsMappedAttributes final
25
{
26
public:
27
  nsMappedAttributes(nsHTMLStyleSheet* aSheet,
28
                     nsMapRuleToAttributesFunc aMapRuleFunc);
29
30
  // Do not return null.
31
  void* operator new(size_t size, uint32_t aAttrCount = 1) CPP_THROW_NEW;
32
  nsMappedAttributes* Clone(bool aWillAddAttr);
33
34
  NS_INLINE_DECL_REFCOUNTING_WITH_DESTROY(nsMappedAttributes, LastRelease())
35
36
  void SetAndSwapAttr(nsAtom* aAttrName, nsAttrValue& aValue,
37
                      bool* aValueWasSet);
38
  const nsAttrValue* GetAttr(nsAtom* aAttrName) const;
39
  const nsAttrValue* GetAttr(const nsAString& aAttrName) const;
40
41
  uint32_t Count() const
42
0
  {
43
0
    return mAttrCount;
44
0
  }
45
46
  bool Equals(const nsMappedAttributes* aAttributes) const;
47
  PLDHashNumber HashValue() const;
48
49
  void DropStyleSheetReference()
50
0
  {
51
0
    mSheet = nullptr;
52
0
  }
53
  void SetStyleSheet(nsHTMLStyleSheet* aSheet);
54
  nsHTMLStyleSheet* GetStyleSheet()
55
0
  {
56
0
    return mSheet;
57
0
  }
58
59
  void SetRuleMapper(nsMapRuleToAttributesFunc aRuleMapper)
60
0
  {
61
0
    mRuleMapper = aRuleMapper;
62
0
  }
63
64
  const nsAttrName* NameAt(uint32_t aPos) const
65
0
  {
66
0
    NS_ASSERTION(aPos < mAttrCount, "out-of-bounds");
67
0
    return &Attrs()[aPos].mName;
68
0
  }
69
  const nsAttrValue* AttrAt(uint32_t aPos) const
70
0
  {
71
0
    NS_ASSERTION(aPos < mAttrCount, "out-of-bounds");
72
0
    return &Attrs()[aPos].mValue;
73
0
  }
74
  // Remove the attr at position aPos.  The value of the attr is placed in
75
  // aValue; any value that was already in aValue is destroyed.
76
  void RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue);
77
  const nsAttrName* GetExistingAttrNameFromQName(const nsAString& aName) const;
78
  int32_t IndexOfAttr(nsAtom* aLocalName) const;
79
80
  // Apply the contained mapper to the contained set of servo rules,
81
  // unless the servo rules have already been initialized.
82
  void LazilyResolveServoDeclaration(nsIDocument* aDocument);
83
84
  // Obtain the contained servo declaration block
85
  // May return null if called before the inner block
86
  // has been (lazily) resolved
87
  const RefPtr<RawServoDeclarationBlock>& GetServoStyle() const
88
0
  {
89
0
    return mServoStyle;
90
0
  }
91
92
0
  void ClearServoStyle() {
93
0
    MOZ_ASSERT(NS_IsMainThread());
94
0
    mServoStyle = nullptr;
95
0
  }
96
97
98
  size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
99
100
  static void Shutdown();
101
private:
102
103
  void LastRelease();
104
105
  nsMappedAttributes(const nsMappedAttributes& aCopy);
106
  ~nsMappedAttributes();
107
108
  struct InternalAttr
109
  {
110
    nsAttrName mName;
111
    nsAttrValue mValue;
112
  };
113
114
  /**
115
   * Due to a compiler bug in VisualAge C++ for AIX, we need to return the
116
   * address of the first index into mAttrs here, instead of simply
117
   * returning mAttrs itself.
118
   *
119
   * See Bug 231104 for more information.
120
   */
121
  const InternalAttr* Attrs() const
122
0
  {
123
0
    return reinterpret_cast<const InternalAttr*>(&(mAttrs[0]));
124
0
  }
125
  InternalAttr* Attrs()
126
0
  {
127
0
    return reinterpret_cast<InternalAttr*>(&(mAttrs[0]));
128
0
  }
129
130
  uint16_t mAttrCount;
131
#ifdef DEBUG
132
  uint16_t mBufferSize;
133
#endif
134
  nsHTMLStyleSheet* mSheet; //weak
135
  nsMapRuleToAttributesFunc mRuleMapper;
136
  RefPtr<RawServoDeclarationBlock> mServoStyle;
137
  void* mAttrs[1];
138
139
  static bool sShuttingDown;
140
141
  // We're caching some memory to avoid trashing the allocator.
142
  // The memory stored at index N can hold N attribute values.
143
  static nsTArray<void*>* sCachedMappedAttributeAllocations;
144
};
145
146
#endif /* nsMappedAttributes_h___ */