CSSVariableImageTable.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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/. */
  5. /* A global table that tracks images referenced by CSS variables. */
  6. #ifndef mozilla_CSSVariableImageTable_h
  7. #define mozilla_CSSVariableImageTable_h
  8. #include "nsClassHashtable.h"
  9. #include "nsCSSPropertyID.h"
  10. #include "nsCSSValue.h"
  11. #include "nsStyleContext.h"
  12. #include "nsTArray.h"
  13. /**
  14. * CSSVariableImageTable maintains a global mapping
  15. * (nsStyleContext, nsCSSPropertyID) -> nsTArray<ImageValue>
  16. * which allows us to track the relationship between CSS property values
  17. * involving variables and any images they may reference.
  18. *
  19. * When properties like background-image contain a normal url(), the
  20. * Declaration's data block will hold a reference to the ImageValue. When a
  21. * token stream is used, the Declaration only holds on to an
  22. * nsCSSValueTokenStream object, and the ImageValue would only exist for the
  23. * duration of nsRuleNode::WalkRuleTree, in the AutoCSSValueArray. So instead
  24. * when we re-parse a token stream and get an ImageValue, we record it in the
  25. * CSSVariableImageTable to keep the ImageValue alive. Such ImageValues are
  26. * eventually freed the next time the token stream is re-parsed, or when the
  27. * associated style context is destroyed.
  28. *
  29. * To add ImageValues to the CSSVariableImageTable, callers should pass a lambda
  30. * to CSSVariableImageTable::ReplaceAll() that calls
  31. * CSSVariableImageTable::Add() for each ImageValue that needs to be added to
  32. * the table. When callers are sure that the ImageValues for a given
  33. * nsStyleContext won't be needed anymore, they can call
  34. * CSSVariableImageTable::RemoveAll() to release them.
  35. */
  36. namespace mozilla {
  37. namespace CSSVariableImageTable {
  38. namespace detail {
  39. typedef nsTArray<RefPtr<css::ImageValue>> ImageValueArray;
  40. typedef nsClassHashtable<nsGenericHashKey<nsCSSPropertyID>, ImageValueArray>
  41. PerPropertyImageHashtable;
  42. typedef nsClassHashtable<nsPtrHashKey<nsStyleContext>, PerPropertyImageHashtable>
  43. CSSVariableImageHashtable;
  44. inline CSSVariableImageHashtable& GetTable()
  45. {
  46. static CSSVariableImageHashtable imageTable;
  47. return imageTable;
  48. }
  49. #ifdef DEBUG
  50. inline bool& IsReplacing()
  51. {
  52. static bool isReplacing = false;
  53. return isReplacing;
  54. }
  55. #endif
  56. } // namespace detail
  57. /**
  58. * ReplaceAll() allows callers to replace the ImageValues associated with a
  59. * (nsStyleContext, nsCSSPropertyID) pair. The memory used by the previous list of
  60. * ImageValues is automatically released.
  61. *
  62. * @param aContext The style context the ImageValues are associated with.
  63. * @param aProp The CSS property the ImageValues are associated with.
  64. * @param aFunc A lambda that calls CSSVariableImageTable::Add() to add new
  65. * ImageValues which will replace the old ones.
  66. */
  67. template <typename Lambda>
  68. inline void ReplaceAll(nsStyleContext* aContext,
  69. nsCSSPropertyID aProp,
  70. Lambda aFunc)
  71. {
  72. MOZ_ASSERT(aContext);
  73. auto& imageTable = detail::GetTable();
  74. // Clear the existing image array, if any, for this property.
  75. {
  76. auto* perPropertyImageTable = imageTable.Get(aContext);
  77. auto* imageList = perPropertyImageTable ? perPropertyImageTable->Get(aProp)
  78. : nullptr;
  79. if (imageList) {
  80. imageList->ClearAndRetainStorage();
  81. }
  82. }
  83. #ifdef DEBUG
  84. MOZ_ASSERT(!detail::IsReplacing());
  85. detail::IsReplacing() = true;
  86. #endif
  87. aFunc();
  88. #ifdef DEBUG
  89. detail::IsReplacing() = false;
  90. #endif
  91. // Clean up.
  92. auto* perPropertyImageTable = imageTable.Get(aContext);
  93. auto* imageList = perPropertyImageTable ? perPropertyImageTable->Get(aProp)
  94. : nullptr;
  95. if (imageList) {
  96. if (imageList->IsEmpty()) {
  97. // We used to have an image array for this property, but now we don't.
  98. // Remove the entry in the per-property image table for this property.
  99. // That may then allow us to remove the entire per-property image table.
  100. perPropertyImageTable->Remove(aProp);
  101. if (perPropertyImageTable->Count() == 0) {
  102. imageTable.Remove(aContext);
  103. }
  104. } else {
  105. // We still have a non-empty image array for this property. Compact the
  106. // storage it's using if possible.
  107. imageList->Compact();
  108. }
  109. }
  110. }
  111. /**
  112. * Adds a new ImageValue @aValue to the CSSVariableImageTable, which will be
  113. * associated with @aContext and @aProp.
  114. *
  115. * It's illegal to call this function outside of a lambda passed to
  116. * CSSVariableImageTable::ReplaceAll().
  117. */
  118. inline void
  119. Add(nsStyleContext* aContext, nsCSSPropertyID aProp, css::ImageValue* aValue)
  120. {
  121. MOZ_ASSERT(aValue);
  122. MOZ_ASSERT(aContext);
  123. MOZ_ASSERT(detail::IsReplacing());
  124. auto& imageTable = detail::GetTable();
  125. // Ensure there's a per-property image table for this style context.
  126. auto* perPropertyImageTable = imageTable.Get(aContext);
  127. if (!perPropertyImageTable) {
  128. perPropertyImageTable = new detail::PerPropertyImageHashtable();
  129. imageTable.Put(aContext, perPropertyImageTable);
  130. }
  131. // Ensure there's an image array for this property.
  132. auto* imageList = perPropertyImageTable->Get(aProp);
  133. if (!imageList) {
  134. imageList = new detail::ImageValueArray();
  135. perPropertyImageTable->Put(aProp, imageList);
  136. }
  137. // Append the provided ImageValue to the list.
  138. imageList->AppendElement(aValue);
  139. }
  140. /**
  141. * Removes all ImageValues stored in the CSSVariableImageTable for the provided
  142. * @aContext.
  143. */
  144. inline void
  145. RemoveAll(nsStyleContext* aContext)
  146. {
  147. // Move all ImageValue references into removedImageList so that we can
  148. // release them outside of any hashtable methods. (If we just call
  149. // Remove(aContext) on the table then we can end up calling back
  150. // re-entrantly into hashtable methods, as other style contexts
  151. // are released.)
  152. detail::ImageValueArray removedImages;
  153. auto& imageTable = detail::GetTable();
  154. auto* perPropertyImageTable = imageTable.Get(aContext);
  155. if (perPropertyImageTable) {
  156. for (auto it = perPropertyImageTable->Iter(); !it.Done(); it.Next()) {
  157. auto* imageList = it.UserData();
  158. removedImages.AppendElements(Move(*imageList));
  159. }
  160. }
  161. imageTable.Remove(aContext);
  162. }
  163. } // namespace CSSVariableImageTable
  164. } // namespace mozilla
  165. #endif // mozilla_CSSVariableImageTable_h