OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/svg/SVGTreeScopeResources.h" |
| 6 |
| 7 #include "core/dom/Element.h" |
| 8 #include "core/dom/TreeScope.h" |
| 9 #include "core/layout/svg/LayoutSVGResourceContainer.h" |
| 10 #include "wtf/text/AtomicString.h" |
| 11 |
| 12 namespace blink { |
| 13 |
| 14 SVGTreeScopeResources::SVGTreeScopeResources(TreeScope* treeScope) { |
| 15 // Whenever an object of SVGTreeScopeResources is created, to keep the code |
| 16 // behave as before, |
| 17 // the document should also have an instance of SVGDocumentExtensions created. |
| 18 // Thus below line is added. |
| 19 treeScope->document().accessSVGExtensions(); |
| 20 } |
| 21 |
| 22 SVGTreeScopeResources::~SVGTreeScopeResources() = default; |
| 23 |
| 24 void SVGTreeScopeResources::addResource(const AtomicString& id, |
| 25 LayoutSVGResourceContainer* resource) { |
| 26 DCHECK(resource); |
| 27 if (id.isEmpty()) |
| 28 return; |
| 29 // Replaces resource if already present, to handle potential id changes |
| 30 m_resources.set(id, resource); |
| 31 } |
| 32 |
| 33 void SVGTreeScopeResources::removeResource(const AtomicString& id) { |
| 34 if (id.isEmpty()) |
| 35 return; |
| 36 m_resources.remove(id); |
| 37 } |
| 38 |
| 39 LayoutSVGResourceContainer* SVGTreeScopeResources::resourceById( |
| 40 const AtomicString& id) const { |
| 41 if (id.isEmpty()) |
| 42 return nullptr; |
| 43 return m_resources.get(id); |
| 44 } |
| 45 |
| 46 void SVGTreeScopeResources::addPendingResource(const AtomicString& id, |
| 47 Element* element) { |
| 48 DCHECK(element); |
| 49 DCHECK(element->isConnected()); |
| 50 |
| 51 if (id.isEmpty()) |
| 52 return; |
| 53 |
| 54 HeapHashMap<AtomicString, Member<SVGPendingElements>>::AddResult result = |
| 55 m_pendingResources.add(id, nullptr); |
| 56 if (result.isNewEntry) |
| 57 result.storedValue->value = new SVGPendingElements; |
| 58 result.storedValue->value->add(element); |
| 59 |
| 60 element->setHasPendingResources(); |
| 61 } |
| 62 |
| 63 bool SVGTreeScopeResources::hasPendingResource(const AtomicString& id) const { |
| 64 if (id.isEmpty()) |
| 65 return false; |
| 66 return m_pendingResources.contains(id); |
| 67 } |
| 68 |
| 69 bool SVGTreeScopeResources::isElementPendingResources(Element* element) const { |
| 70 // This algorithm takes time proportional to the number of pending resources |
| 71 // and need not. |
| 72 // If performance becomes an issue we can keep a counted set of elements and |
| 73 // answer the question efficiently. |
| 74 DCHECK(element); |
| 75 |
| 76 for (const auto& entry : m_pendingResources) { |
| 77 SVGPendingElements* elements = entry.value.get(); |
| 78 DCHECK(elements); |
| 79 if (elements->contains(element)) |
| 80 return true; |
| 81 } |
| 82 return false; |
| 83 } |
| 84 |
| 85 bool SVGTreeScopeResources::isElementPendingResource( |
| 86 Element* element, |
| 87 const AtomicString& id) const { |
| 88 DCHECK(element); |
| 89 if (!hasPendingResource(id)) |
| 90 return false; |
| 91 return m_pendingResources.get(id)->contains(element); |
| 92 } |
| 93 |
| 94 void SVGTreeScopeResources::clearHasPendingResourcesIfPossible( |
| 95 Element* element) { |
| 96 if (!isElementPendingResources(element)) |
| 97 element->clearHasPendingResources(); |
| 98 } |
| 99 |
| 100 void SVGTreeScopeResources::removeElementFromPendingResources( |
| 101 Element* element) { |
| 102 DCHECK(element); |
| 103 |
| 104 // Remove the element from pending resources. |
| 105 if (!m_pendingResources.isEmpty() && element->hasPendingResources()) { |
| 106 Vector<AtomicString> toBeRemoved; |
| 107 for (const auto& entry : m_pendingResources) { |
| 108 SVGPendingElements* elements = entry.value.get(); |
| 109 DCHECK(elements); |
| 110 DCHECK(!elements->isEmpty()); |
| 111 |
| 112 elements->remove(element); |
| 113 if (elements->isEmpty()) |
| 114 toBeRemoved.append(entry.key); |
| 115 } |
| 116 |
| 117 clearHasPendingResourcesIfPossible(element); |
| 118 |
| 119 // We use the removePendingResource function here because it deals with set |
| 120 // lifetime correctly. |
| 121 for (const AtomicString& id : toBeRemoved) |
| 122 removePendingResource(id); |
| 123 } |
| 124 } |
| 125 |
| 126 SVGTreeScopeResources::SVGPendingElements* |
| 127 SVGTreeScopeResources::removePendingResource(const AtomicString& id) { |
| 128 DCHECK(m_pendingResources.contains(id)); |
| 129 return m_pendingResources.take(id); |
| 130 } |
| 131 |
| 132 DEFINE_TRACE(SVGTreeScopeResources) { |
| 133 visitor->trace(m_pendingResources); |
| 134 } |
| 135 } |
OLD | NEW |