2 * Copyright (C) 2006 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
4 * Copyright (C) 2007 Rob Buis <buis@kde.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
25 #include "SVGDocumentExtensions.h"
28 #include "DOMWindow.h"
30 #include "EventListener.h"
32 #include "FrameLoader.h"
34 #include "SMILTimeContainer.h"
35 #include "SVGElement.h"
36 #include "SVGResourcesCache.h"
37 #include "SVGSMILElement.h"
38 #include "SVGSVGElement.h"
39 #include "ScriptableDocumentParser.h"
40 #include "XLinkNames.h"
41 #include <wtf/text/AtomicString.h>
45 SVGDocumentExtensions::SVGDocumentExtensions(Document* document)
46 : m_document(document)
47 , m_resourcesCache(adoptPtr(new SVGResourcesCache))
51 SVGDocumentExtensions::~SVGDocumentExtensions()
53 deleteAllValues(m_pendingResources);
54 deleteAllValues(m_pendingResourcesForRemoval);
57 void SVGDocumentExtensions::addTimeContainer(SVGSVGElement* element)
59 m_timeContainers.add(element);
62 void SVGDocumentExtensions::removeTimeContainer(SVGSVGElement* element)
64 m_timeContainers.remove(element);
67 void SVGDocumentExtensions::addResource(const AtomicString& id, RenderSVGResourceContainer* resource)
74 // Replaces resource if already present, to handle potential id changes
75 m_resources.set(id, resource);
78 void SVGDocumentExtensions::removeResource(const AtomicString& id)
80 if (id.isEmpty() || !m_resources.contains(id))
83 m_resources.remove(id);
86 RenderSVGResourceContainer* SVGDocumentExtensions::resourceById(const AtomicString& id) const
91 return m_resources.get(id);
94 void SVGDocumentExtensions::startAnimations()
96 // FIXME: Eventually every "Time Container" will need a way to latch on to some global timer
97 // starting animations for a document will do this "latching"
98 // FIXME: We hold a ref pointers to prevent a shadow tree from getting removed out from underneath us.
99 // In the future we should refactor the use-element to avoid this. See https://webkit.org/b/53704
100 Vector<RefPtr<SVGSVGElement> > timeContainers;
101 timeContainers.appendRange(m_timeContainers.begin(), m_timeContainers.end());
102 Vector<RefPtr<SVGSVGElement> >::iterator end = timeContainers.end();
103 for (Vector<RefPtr<SVGSVGElement> >::iterator itr = timeContainers.begin(); itr != end; ++itr)
104 (*itr)->timeContainer()->begin();
107 void SVGDocumentExtensions::pauseAnimations()
109 HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
110 for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
111 (*itr)->pauseAnimations();
114 void SVGDocumentExtensions::unpauseAnimations()
116 HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
117 for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
118 (*itr)->unpauseAnimations();
121 void SVGDocumentExtensions::dispatchSVGLoadEventToOutermostSVGElements()
123 Vector<RefPtr<SVGSVGElement> > timeContainers;
124 timeContainers.appendRange(m_timeContainers.begin(), m_timeContainers.end());
126 Vector<RefPtr<SVGSVGElement> >::iterator end = timeContainers.end();
127 for (Vector<RefPtr<SVGSVGElement> >::iterator it = timeContainers.begin(); it != end; ++it) {
128 SVGSVGElement* outerSVG = (*it).get();
129 if (!outerSVG->isOutermostSVGSVGElement())
131 outerSVG->sendSVGLoadEventIfPossible();
135 static void reportMessage(Document* document, MessageLevel level, const String& message)
137 if (document->frame())
138 document->addConsoleMessage(JSMessageSource, LogMessageType, level, message);
141 void SVGDocumentExtensions::reportWarning(const String& message)
143 reportMessage(m_document, WarningMessageLevel, "Warning: " + message);
146 void SVGDocumentExtensions::reportError(const String& message)
148 reportMessage(m_document, ErrorMessageLevel, "Error: " + message);
151 void SVGDocumentExtensions::addPendingResource(const AtomicString& id, SVGElement* element)
158 // The HashMap add function leaves the map alone and returns a pointer to the element in the
159 // map if the element already exists. So we add with a value of 0, and it either finds the
160 // existing element or adds a new one in a single operation. The ".iterator->value" idiom gets
161 // us to the iterator from add's result, and then to the value inside the hash table.
162 SVGPendingElements*& set = m_pendingResources.add(id, 0).iterator->value;
164 set = new SVGPendingElements;
167 element->setHasPendingResources();
170 bool SVGDocumentExtensions::hasPendingResource(const AtomicString& id) const
175 return m_pendingResources.contains(id);
178 bool SVGDocumentExtensions::isElementPendingResources(SVGElement* element) const
180 // This algorithm takes time proportional to the number of pending resources and need not.
181 // If performance becomes an issue we can keep a counted set of elements and answer the question efficiently.
185 HashMap<AtomicString, SVGPendingElements*>::const_iterator end = m_pendingResources.end();
186 for (HashMap<AtomicString, SVGPendingElements*>::const_iterator it = m_pendingResources.begin(); it != end; ++it) {
187 SVGPendingElements* elements = it->value;
190 if (elements->contains(element))
196 bool SVGDocumentExtensions::isElementPendingResource(SVGElement* element, const AtomicString& id) const
200 if (!hasPendingResource(id))
203 return m_pendingResources.get(id)->contains(element);
206 void SVGDocumentExtensions::removeElementFromPendingResources(SVGElement* element)
210 // Remove the element from pending resources.
211 if (!m_pendingResources.isEmpty() && element->hasPendingResources()) {
212 Vector<AtomicString> toBeRemoved;
213 HashMap<AtomicString, SVGPendingElements*>::iterator end = m_pendingResources.end();
214 for (HashMap<AtomicString, SVGPendingElements*>::iterator it = m_pendingResources.begin(); it != end; ++it) {
215 SVGPendingElements* elements = it->value;
217 ASSERT(!elements->isEmpty());
219 elements->remove(element);
220 if (elements->isEmpty())
221 toBeRemoved.append(it->key);
224 element->clearHasPendingResourcesIfPossible();
226 // We use the removePendingResource function here because it deals with set lifetime correctly.
227 Vector<AtomicString>::iterator vectorEnd = toBeRemoved.end();
228 for (Vector<AtomicString>::iterator it = toBeRemoved.begin(); it != vectorEnd; ++it)
229 removePendingResource(*it);
232 // Remove the element from pending resources that were scheduled for removal.
233 if (!m_pendingResourcesForRemoval.isEmpty()) {
234 Vector<AtomicString> toBeRemoved;
235 HashMap<AtomicString, SVGPendingElements*>::iterator end = m_pendingResourcesForRemoval.end();
236 for (HashMap<AtomicString, SVGPendingElements*>::iterator it = m_pendingResourcesForRemoval.begin(); it != end; ++it) {
237 SVGPendingElements* elements = it->value;
239 ASSERT(!elements->isEmpty());
241 elements->remove(element);
242 if (elements->isEmpty())
243 toBeRemoved.append(it->key);
246 // We use the removePendingResourceForRemoval function here because it deals with set lifetime correctly.
247 Vector<AtomicString>::iterator vectorEnd = toBeRemoved.end();
248 for (Vector<AtomicString>::iterator it = toBeRemoved.begin(); it != vectorEnd; ++it)
249 removePendingResourceForRemoval(*it);
253 PassOwnPtr<SVGDocumentExtensions::SVGPendingElements> SVGDocumentExtensions::removePendingResource(const AtomicString& id)
255 ASSERT(m_pendingResources.contains(id));
256 return adoptPtr(m_pendingResources.take(id));
259 PassOwnPtr<SVGDocumentExtensions::SVGPendingElements> SVGDocumentExtensions::removePendingResourceForRemoval(const AtomicString& id)
261 ASSERT(m_pendingResourcesForRemoval.contains(id));
262 return adoptPtr(m_pendingResourcesForRemoval.take(id));
265 void SVGDocumentExtensions::markPendingResourcesForRemoval(const AtomicString& id)
270 ASSERT(!m_pendingResourcesForRemoval.contains(id));
272 SVGPendingElements* existing = m_pendingResources.take(id);
273 if (existing && !existing->isEmpty())
274 m_pendingResourcesForRemoval.add(id, existing);
277 SVGElement* SVGDocumentExtensions::removeElementFromPendingResourcesForRemoval(const AtomicString& id)
282 SVGPendingElements* resourceSet = m_pendingResourcesForRemoval.get(id);
283 if (!resourceSet || resourceSet->isEmpty())
286 SVGPendingElements::iterator firstElement = resourceSet->begin();
287 SVGElement* element = *firstElement;
289 resourceSet->remove(firstElement);
291 if (resourceSet->isEmpty())
292 removePendingResourceForRemoval(id);
297 HashSet<SVGElement*>* SVGDocumentExtensions::setOfElementsReferencingTarget(SVGElement* referencedElement) const
299 ASSERT(referencedElement);
300 const HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > >::const_iterator it = m_elementDependencies.find(referencedElement);
301 if (it == m_elementDependencies.end())
303 return it->value.get();
306 void SVGDocumentExtensions::addElementReferencingTarget(SVGElement* referencingElement, SVGElement* referencedElement)
308 ASSERT(referencingElement);
309 ASSERT(referencedElement);
311 if (HashSet<SVGElement*>* elements = m_elementDependencies.get(referencedElement)) {
312 elements->add(referencingElement);
316 OwnPtr<HashSet<SVGElement*> > elements = adoptPtr(new HashSet<SVGElement*>);
317 elements->add(referencingElement);
318 m_elementDependencies.set(referencedElement, elements.release());
321 void SVGDocumentExtensions::removeAllTargetReferencesForElement(SVGElement* referencingElement)
323 Vector<SVGElement*> toBeRemoved;
325 HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > >::iterator end = m_elementDependencies.end();
326 for (HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > >::iterator it = m_elementDependencies.begin(); it != end; ++it) {
327 SVGElement* referencedElement = it->key;
328 HashSet<SVGElement*>* referencingElements = it->value.get();
329 HashSet<SVGElement*>::iterator setIt = referencingElements->find(referencingElement);
330 if (setIt == referencingElements->end())
333 referencingElements->remove(setIt);
334 if (referencingElements->isEmpty())
335 toBeRemoved.append(referencedElement);
338 Vector<SVGElement*>::iterator vectorEnd = toBeRemoved.end();
339 for (Vector<SVGElement*>::iterator it = toBeRemoved.begin(); it != vectorEnd; ++it)
340 m_elementDependencies.remove(*it);
343 void SVGDocumentExtensions::removeAllElementReferencesForTarget(SVGElement* referencedElement)
345 ASSERT(referencedElement);
346 HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > >::iterator it = m_elementDependencies.find(referencedElement);
347 if (it == m_elementDependencies.end())
349 ASSERT(it->key == referencedElement);
350 Vector<SVGElement*> toBeNotified;
352 HashSet<SVGElement*>* referencingElements = it->value.get();
353 HashSet<SVGElement*>::iterator setEnd = referencingElements->end();
354 for (HashSet<SVGElement*>::iterator setIt = referencingElements->begin(); setIt != setEnd; ++setIt)
355 toBeNotified.append(*setIt);
357 // Force rebuilding the referencingElement so it knows about this change.
358 Vector<SVGElement*>::iterator vectorEnd = toBeNotified.end();
359 for (Vector<SVGElement*>::iterator vectorIt = toBeNotified.begin(); vectorIt != vectorEnd; ++vectorIt) {
360 // Before rebuilding referencingElement ensure it was not removed from under us.
361 if (HashSet<SVGElement*>* referencingElements = setOfElementsReferencingTarget(referencedElement)) {
362 if (referencingElements->contains(*vectorIt))
363 (*vectorIt)->svgAttributeChanged(XLinkNames::hrefAttr);
367 m_elementDependencies.remove(referencedElement);
370 #if ENABLE(SVG_FONTS)
371 void SVGDocumentExtensions::registerSVGFontFaceElement(SVGFontFaceElement* element)
373 m_svgFontFaceElements.add(element);
376 void SVGDocumentExtensions::unregisterSVGFontFaceElement(SVGFontFaceElement* element)
378 ASSERT(m_svgFontFaceElements.contains(element));
379 m_svgFontFaceElements.remove(element);