OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/SVGElementProxy.h" |
| 6 |
| 7 #include "core/dom/IdTargetObserver.h" |
| 8 #include "core/fetch/FetchInitiatorTypeNames.h" |
| 9 #include "core/fetch/FetchRequest.h" |
| 10 #include "core/fetch/ResourceFetcher.h" |
| 11 #include "core/svg/SVGElement.h" |
| 12 #include "core/svg/SVGFilterElement.h" |
| 13 #include "core/svg/SVGResourceClient.h" |
| 14 |
| 15 namespace blink { |
| 16 |
| 17 class SVGElementProxy::IdObserver : public IdTargetObserver { |
| 18 public: |
| 19 IdObserver(TreeScope& treeScope, SVGElementProxy& proxy) |
| 20 : IdTargetObserver(treeScope.idTargetObserverRegistry(), proxy.id()), |
| 21 m_treeScope(&treeScope) {} |
| 22 |
| 23 void addClient(SVGResourceClient* client) { m_clients.add(client); } |
| 24 bool removeClient(SVGResourceClient* client) { |
| 25 return m_clients.remove(client); |
| 26 } |
| 27 bool hasClients() const { return !m_clients.isEmpty(); } |
| 28 |
| 29 TreeScope* treeScope() const { return m_treeScope; } |
| 30 void transferClients(IdObserver& observer) { |
| 31 for (const auto& client : m_clients) |
| 32 observer.m_clients.add(client.key, client.value); |
| 33 m_clients.clear(); |
| 34 } |
| 35 |
| 36 DEFINE_INLINE_VIRTUAL_TRACE() { |
| 37 visitor->trace(m_clients); |
| 38 visitor->trace(m_treeScope); |
| 39 IdTargetObserver::trace(visitor); |
| 40 } |
| 41 |
| 42 void contentChanged() { |
| 43 DCHECK(lifecycle().state() <= DocumentLifecycle::CompositingClean || |
| 44 lifecycle().state() >= DocumentLifecycle::PaintClean); |
| 45 HeapVector<Member<SVGResourceClient>> clients; |
| 46 copyToVector(m_clients, clients); |
| 47 for (SVGResourceClient* client : clients) |
| 48 client->resourceContentChanged(); |
| 49 } |
| 50 |
| 51 private: |
| 52 const DocumentLifecycle& lifecycle() const { |
| 53 return m_treeScope->document().lifecycle(); |
| 54 } |
| 55 void idTargetChanged() override { |
| 56 DCHECK(lifecycle().stateAllowsTreeMutations()); |
| 57 HeapVector<Member<SVGResourceClient>> clients; |
| 58 copyToVector(m_clients, clients); |
| 59 for (SVGResourceClient* client : clients) |
| 60 client->resourceElementChanged(); |
| 61 } |
| 62 HeapHashCountedSet<Member<SVGResourceClient>> m_clients; |
| 63 Member<TreeScope> m_treeScope; |
| 64 }; |
| 65 |
| 66 SVGElementProxy::SVGElementProxy(const AtomicString& id) |
| 67 : m_id(id), m_isLocal(true) {} |
| 68 |
| 69 SVGElementProxy::SVGElementProxy(const String& url, const AtomicString& id) |
| 70 : m_id(id), m_url(url), m_isLocal(false) {} |
| 71 |
| 72 SVGElementProxy::~SVGElementProxy() {} |
| 73 |
| 74 void SVGElementProxy::addClient(SVGResourceClient* client) { |
| 75 // An empty id will never be a valid element reference. |
| 76 if (m_id.isEmpty()) |
| 77 return; |
| 78 if (!m_isLocal) { |
| 79 if (m_document) |
| 80 m_document->addClient(client); |
| 81 return; |
| 82 } |
| 83 TreeScope* clientScope = client->treeScope(); |
| 84 if (!clientScope) |
| 85 return; |
| 86 // Ensure sure we have an observer registered for this tree scope. |
| 87 auto& scopeObserver = |
| 88 m_observers.add(clientScope, nullptr).storedValue->value; |
| 89 if (!scopeObserver) |
| 90 scopeObserver = new IdObserver(*clientScope, *this); |
| 91 |
| 92 auto& observer = m_clients.add(client, nullptr).storedValue->value; |
| 93 if (!observer) |
| 94 observer = scopeObserver; |
| 95 |
| 96 DCHECK(observer && scopeObserver); |
| 97 |
| 98 // If the client moved to a different scope, we need to unregister the old |
| 99 // observer and transfer any clients from it before replacing it. Thus any |
| 100 // clients that remain to be removed will be transferred to the new observer, |
| 101 // and hence removed from it instead. |
| 102 if (observer != scopeObserver) { |
| 103 observer->unregister(); |
| 104 observer->transferClients(*scopeObserver); |
| 105 observer = scopeObserver; |
| 106 } |
| 107 observer->addClient(client); |
| 108 } |
| 109 |
| 110 void SVGElementProxy::removeClient(SVGResourceClient* client) { |
| 111 // An empty id will never be a valid element reference. |
| 112 if (m_id.isEmpty()) |
| 113 return; |
| 114 if (!m_isLocal) { |
| 115 if (m_document) |
| 116 m_document->removeClient(client); |
| 117 return; |
| 118 } |
| 119 auto entry = m_clients.find(client); |
| 120 if (entry == m_clients.end()) |
| 121 return; |
| 122 IdObserver* observer = entry->value; |
| 123 DCHECK(observer); |
| 124 // If the client is not the last client in the scope, then no further action |
| 125 // needs to be taken. |
| 126 if (!observer->removeClient(client)) |
| 127 return; |
| 128 // Unregister and drop the scope association, then drop the client. |
| 129 if (!observer->hasClients()) { |
| 130 observer->unregister(); |
| 131 m_observers.remove(observer->treeScope()); |
| 132 } |
| 133 m_clients.remove(entry); |
| 134 } |
| 135 |
| 136 void SVGElementProxy::resolve(Document& document) { |
| 137 if (m_isLocal || m_id.isEmpty() || m_url.isEmpty()) |
| 138 return; |
| 139 FetchRequest request(ResourceRequest(m_url), FetchInitiatorTypeNames::css); |
| 140 m_document = DocumentResource::fetchSVGDocument(request, document.fetcher()); |
| 141 m_url = String(); |
| 142 } |
| 143 |
| 144 TreeScope* SVGElementProxy::treeScopeForLookup(TreeScope& treeScope) const { |
| 145 if (m_isLocal) |
| 146 return &treeScope; |
| 147 if (!m_document) |
| 148 return nullptr; |
| 149 return m_document->document(); |
| 150 } |
| 151 |
| 152 SVGElement* SVGElementProxy::findElement(TreeScope& treeScope) { |
| 153 // An empty id will never be a valid element reference. |
| 154 if (m_id.isEmpty()) |
| 155 return nullptr; |
| 156 TreeScope* lookupScope = treeScopeForLookup(treeScope); |
| 157 if (!lookupScope) |
| 158 return nullptr; |
| 159 if (Element* targetElement = lookupScope->getElementById(m_id)) { |
| 160 if (isSVGFilterElement(*targetElement)) { |
| 161 toSVGFilterElement(*targetElement).elementProxySet().add(*this); |
| 162 return toSVGElement(targetElement); |
| 163 } |
| 164 } |
| 165 return nullptr; |
| 166 } |
| 167 |
| 168 void SVGElementProxy::contentChanged(TreeScope& treeScope) { |
| 169 if (auto* observer = m_observers.get(&treeScope)) |
| 170 observer->contentChanged(); |
| 171 } |
| 172 |
| 173 DEFINE_TRACE(SVGElementProxy) { |
| 174 visitor->trace(m_clients); |
| 175 visitor->trace(m_observers); |
| 176 visitor->trace(m_document); |
| 177 } |
| 178 |
| 179 void SVGElementProxySet::add(SVGElementProxy& elementProxy) { |
| 180 m_elementProxies.add(&elementProxy); |
| 181 } |
| 182 |
| 183 bool SVGElementProxySet::isEmpty() const { |
| 184 return m_elementProxies.isEmpty(); |
| 185 } |
| 186 |
| 187 void SVGElementProxySet::notifyContentChanged(TreeScope& treeScope) { |
| 188 for (SVGElementProxy* proxy : m_elementProxies) |
| 189 proxy->contentChanged(treeScope); |
| 190 } |
| 191 |
| 192 DEFINE_TRACE(SVGElementProxySet) { |
| 193 visitor->trace(m_elementProxies); |
| 194 } |
| 195 |
| 196 } // namespace blink |
OLD | NEW |