initial import
[vuplus_webkit] / Source / WebCore / svg / SVGDocumentExtensions.cpp
1 /*
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23
24 #if ENABLE(SVG)
25 #include "SVGDocumentExtensions.h"
26
27 #include "Console.h"
28 #include "DOMWindow.h"
29 #include "Document.h"
30 #include "EventListener.h"
31 #include "Frame.h"
32 #include "FrameLoader.h"
33 #include "Page.h"
34 #include "SMILTimeContainer.h"
35 #include "SVGElement.h"
36 #include "SVGSMILElement.h"
37 #include "SVGSVGElement.h"
38 #include "ScriptableDocumentParser.h"
39 #include <wtf/text/AtomicString.h>
40
41 namespace WebCore {
42
43 SVGDocumentExtensions::SVGDocumentExtensions(Document* document)
44     : m_document(document)
45     , m_resourcesCache(adoptPtr(new SVGResourcesCache))
46 {
47 }
48
49 SVGDocumentExtensions::~SVGDocumentExtensions()
50 {
51     deleteAllValues(m_animatedElements);
52     deleteAllValues(m_pendingResources);
53 }
54
55 void SVGDocumentExtensions::addTimeContainer(SVGSVGElement* element)
56 {
57     m_timeContainers.add(element);
58 }
59
60 void SVGDocumentExtensions::removeTimeContainer(SVGSVGElement* element)
61 {
62     m_timeContainers.remove(element);
63 }
64
65 void SVGDocumentExtensions::addResource(const AtomicString& id, RenderSVGResourceContainer* resource)
66 {
67     ASSERT(resource);
68
69     if (id.isEmpty())
70         return;
71
72     // Replaces resource if already present, to handle potential id changes
73     m_resources.set(id, resource);
74 }
75
76 void SVGDocumentExtensions::removeResource(const AtomicString& id)
77 {
78     if (id.isEmpty() || !m_resources.contains(id))
79         return;
80
81     m_resources.remove(id);
82 }
83
84 RenderSVGResourceContainer* SVGDocumentExtensions::resourceById(const AtomicString& id) const
85 {
86     if (id.isEmpty())
87         return 0;
88
89     return m_resources.get(id);
90 }
91
92 void SVGDocumentExtensions::startAnimations()
93 {
94     // FIXME: Eventually every "Time Container" will need a way to latch on to some global timer
95     // starting animations for a document will do this "latching"
96 #if ENABLE(SVG_ANIMATION)    
97     // FIXME: We hold a ref pointers to prevent a shadow tree from getting removed out from underneath us.
98     // In the future we should refactor the use-element to avoid this. See https://webkit.org/b/53704
99     Vector<RefPtr<SVGSVGElement> > timeContainers;
100     timeContainers.appendRange(m_timeContainers.begin(), m_timeContainers.end());
101     Vector<RefPtr<SVGSVGElement> >::iterator end = timeContainers.end();
102     for (Vector<RefPtr<SVGSVGElement> >::iterator itr = timeContainers.begin(); itr != end; ++itr)
103         (*itr)->timeContainer()->begin();
104 #endif
105 }
106     
107 void SVGDocumentExtensions::pauseAnimations()
108 {
109     HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
110     for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
111         (*itr)->pauseAnimations();
112 }
113
114 void SVGDocumentExtensions::unpauseAnimations()
115 {
116     HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
117     for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
118         (*itr)->unpauseAnimations();
119 }
120
121 bool SVGDocumentExtensions::sampleAnimationAtTime(const String& elementId, SVGSMILElement* element, double time)
122 {
123 #if !ENABLE(SVG_ANIMATION)
124     UNUSED_PARAM(elementId);
125     UNUSED_PARAM(element);
126     UNUSED_PARAM(time);
127     return false;
128 #else
129     ASSERT(element);
130     SMILTimeContainer* container = element->timeContainer();
131     if (!container || container->isPaused())
132         return false;
133
134     container->sampleAnimationAtTime(elementId, time);
135     return true;
136 #endif
137 }
138
139 void SVGDocumentExtensions::addAnimationElementToTarget(SVGSMILElement* animationElement, SVGElement* targetElement)
140 {
141     ASSERT(targetElement);
142     ASSERT(animationElement);
143
144     if (HashSet<SVGSMILElement*>* animationElementsForTarget = m_animatedElements.get(targetElement)) {
145         animationElementsForTarget->add(animationElement);
146         return;
147     }
148
149     HashSet<SVGSMILElement*>* animationElementsForTarget = new HashSet<SVGSMILElement*>;
150     animationElementsForTarget->add(animationElement);
151     m_animatedElements.set(targetElement, animationElementsForTarget);
152 }
153
154 void SVGDocumentExtensions::removeAnimationElementFromTarget(SVGSMILElement* animationElement, SVGElement* targetElement)
155 {
156     ASSERT(targetElement);
157     ASSERT(animationElement);
158
159     HashMap<SVGElement*, HashSet<SVGSMILElement*>* >::iterator it = m_animatedElements.find(targetElement);
160     ASSERT(it != m_animatedElements.end());
161     
162     HashSet<SVGSMILElement*>* animationElementsForTarget = it->second;
163     ASSERT(!animationElementsForTarget->isEmpty());
164
165     animationElementsForTarget->remove(animationElement);
166     if (animationElementsForTarget->isEmpty()) {
167         m_animatedElements.remove(it);
168         delete animationElementsForTarget;
169     }
170 }
171
172 void SVGDocumentExtensions::removeAllAnimationElementsFromTarget(SVGElement* targetElement)
173 {
174     ASSERT(targetElement);
175     HashSet<SVGSMILElement*>* animationElementsForTarget = m_animatedElements.take(targetElement);
176     if (!animationElementsForTarget)
177         return;
178 #if ENABLE(SVG_ANIMATION)
179     HashSet<SVGSMILElement*>::iterator it = animationElementsForTarget->begin();
180     HashSet<SVGSMILElement*>::iterator end = animationElementsForTarget->end();
181     for (; it != end; ++it)
182         (*it)->resetTargetElement();
183     delete animationElementsForTarget;
184 #else
185     ASSERT_NOT_REACHED();
186 #endif
187 }
188
189 // FIXME: Callers should probably use ScriptController::eventHandlerLineNumber()
190 static int parserLineNumber(Document* document)
191 {
192     ScriptableDocumentParser* parser = document->scriptableDocumentParser();
193     if (!parser)
194         return 1;
195     return parser->lineNumber();
196 }
197
198 static void reportMessage(Document* document, MessageLevel level, const String& message)
199 {
200     if (Frame* frame = document->frame())
201         frame->domWindow()->console()->addMessage(JSMessageSource, LogMessageType, level, message, parserLineNumber(document), document->documentURI());
202 }
203
204 void SVGDocumentExtensions::reportWarning(const String& message)
205 {
206     reportMessage(m_document, WarningMessageLevel, "Warning: " + message);
207 }
208
209 void SVGDocumentExtensions::reportError(const String& message)
210 {
211     reportMessage(m_document, ErrorMessageLevel, "Error: " + message);
212 }
213
214 void SVGDocumentExtensions::addPendingResource(const AtomicString& id, SVGStyledElement* element)
215 {
216     ASSERT(element);
217
218     if (id.isEmpty())
219         return;
220
221     if (m_pendingResources.contains(id))
222         m_pendingResources.get(id)->add(element);
223     else {
224         SVGPendingElements* set = new SVGPendingElements;
225         set->add(element);
226
227         m_pendingResources.add(id, set);
228     }
229
230     element->setHasPendingResources();
231 }
232
233 bool SVGDocumentExtensions::hasPendingResources(const AtomicString& id) const
234 {
235     if (id.isEmpty())
236         return false;
237
238     return m_pendingResources.contains(id);
239 }
240
241 bool SVGDocumentExtensions::isElementInPendingResources(SVGStyledElement* element) const
242 {
243     ASSERT(element);
244
245     if (m_pendingResources.isEmpty())
246         return false;
247
248     HashMap<AtomicString, SVGPendingElements*>::const_iterator end = m_pendingResources.end();
249     for (HashMap<AtomicString, SVGPendingElements*>::const_iterator it = m_pendingResources.begin(); it != end; ++it) {
250         SVGPendingElements* elements = it->second;
251         ASSERT(elements);
252
253         if (elements->contains(element))
254             return true;
255     }
256     return false;
257 }
258
259 void SVGDocumentExtensions::removeElementFromPendingResources(SVGStyledElement* element)
260 {
261     ASSERT(element);
262
263     if (m_pendingResources.isEmpty() || !element->hasPendingResources())
264         return;
265
266     Vector<AtomicString> toBeRemoved;
267     HashMap<AtomicString, SVGPendingElements*>::iterator end = m_pendingResources.end();
268     for (HashMap<AtomicString, SVGPendingElements*>::iterator it = m_pendingResources.begin(); it != end; ++it) {
269         SVGPendingElements* elements = it->second;
270         ASSERT(elements);
271         ASSERT(!elements->isEmpty());
272
273         elements->remove(element);
274         if (elements->isEmpty())
275             toBeRemoved.append(it->first);
276     }
277
278     element->clearHasPendingResourcesIfPossible();
279
280     if (toBeRemoved.isEmpty())
281         return;
282
283     Vector<AtomicString>::iterator endVector = toBeRemoved.end();
284     for (Vector<AtomicString>::iterator it = toBeRemoved.begin(); it != endVector; ++it)
285         m_pendingResources.remove(*it);
286 }
287
288 PassOwnPtr<SVGDocumentExtensions::SVGPendingElements> SVGDocumentExtensions::removePendingResource(const AtomicString& id)
289 {
290     ASSERT(m_pendingResources.contains(id));
291
292     OwnPtr<SVGPendingElements> set = adoptPtr(m_pendingResources.get(id));
293     m_pendingResources.remove(id);
294     return set.release();
295 }
296
297 }
298
299 #endif