~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/svg/SVGDocumentExtensions.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "SVGResourcesCache.h"
 
37
#include "SVGSMILElement.h"
 
38
#include "SVGSVGElement.h"
 
39
#include "ScriptableDocumentParser.h"
 
40
#include "XLinkNames.h"
 
41
#include <wtf/text/AtomicString.h>
 
42
 
 
43
namespace WebCore {
 
44
 
 
45
SVGDocumentExtensions::SVGDocumentExtensions(Document* document)
 
46
    : m_document(document)
 
47
    , m_resourcesCache(adoptPtr(new SVGResourcesCache))
 
48
{
 
49
}
 
50
 
 
51
SVGDocumentExtensions::~SVGDocumentExtensions()
 
52
{
 
53
    deleteAllValues(m_animatedElements);
 
54
    deleteAllValues(m_pendingResources);
 
55
    deleteAllValues(m_pendingResourcesForRemoval);
 
56
}
 
57
 
 
58
void SVGDocumentExtensions::addTimeContainer(SVGSVGElement* element)
 
59
{
 
60
    m_timeContainers.add(element);
 
61
}
 
62
 
 
63
void SVGDocumentExtensions::removeTimeContainer(SVGSVGElement* element)
 
64
{
 
65
    m_timeContainers.remove(element);
 
66
}
 
67
 
 
68
void SVGDocumentExtensions::addResource(const AtomicString& id, RenderSVGResourceContainer* resource)
 
69
{
 
70
    ASSERT(resource);
 
71
 
 
72
    if (id.isEmpty())
 
73
        return;
 
74
 
 
75
    // Replaces resource if already present, to handle potential id changes
 
76
    m_resources.set(id, resource);
 
77
}
 
78
 
 
79
void SVGDocumentExtensions::removeResource(const AtomicString& id)
 
80
{
 
81
    if (id.isEmpty() || !m_resources.contains(id))
 
82
        return;
 
83
 
 
84
    m_resources.remove(id);
 
85
}
 
86
 
 
87
RenderSVGResourceContainer* SVGDocumentExtensions::resourceById(const AtomicString& id) const
 
88
{
 
89
    if (id.isEmpty())
 
90
        return 0;
 
91
 
 
92
    return m_resources.get(id);
 
93
}
 
94
 
 
95
void SVGDocumentExtensions::startAnimations()
 
96
{
 
97
    // FIXME: Eventually every "Time Container" will need a way to latch on to some global timer
 
98
    // starting animations for a document will do this "latching"
 
99
    // FIXME: We hold a ref pointers to prevent a shadow tree from getting removed out from underneath us.
 
100
    // In the future we should refactor the use-element to avoid this. See https://webkit.org/b/53704
 
101
    Vector<RefPtr<SVGSVGElement> > timeContainers;
 
102
    timeContainers.appendRange(m_timeContainers.begin(), m_timeContainers.end());
 
103
    Vector<RefPtr<SVGSVGElement> >::iterator end = timeContainers.end();
 
104
    for (Vector<RefPtr<SVGSVGElement> >::iterator itr = timeContainers.begin(); itr != end; ++itr)
 
105
        (*itr)->timeContainer()->begin();
 
106
}
 
107
    
 
108
void SVGDocumentExtensions::pauseAnimations()
 
109
{
 
110
    HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
 
111
    for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
 
112
        (*itr)->pauseAnimations();
 
113
}
 
114
 
 
115
void SVGDocumentExtensions::unpauseAnimations()
 
116
{
 
117
    HashSet<SVGSVGElement*>::iterator end = m_timeContainers.end();
 
118
    for (HashSet<SVGSVGElement*>::iterator itr = m_timeContainers.begin(); itr != end; ++itr)
 
119
        (*itr)->unpauseAnimations();
 
120
}
 
121
 
 
122
void SVGDocumentExtensions::dispatchSVGLoadEventToOutermostSVGElements()
 
123
{
 
124
    Vector<RefPtr<SVGSVGElement> > timeContainers;
 
125
    timeContainers.appendRange(m_timeContainers.begin(), m_timeContainers.end());
 
126
 
 
127
    Vector<RefPtr<SVGSVGElement> >::iterator end = timeContainers.end();
 
128
    for (Vector<RefPtr<SVGSVGElement> >::iterator it = timeContainers.begin(); it != end; ++it) {
 
129
        SVGSVGElement* outerSVG = (*it).get();
 
130
        if (!outerSVG->isOutermostSVGSVGElement())
 
131
            continue;
 
132
        outerSVG->sendSVGLoadEventIfPossible();
 
133
    }
 
134
}
 
135
 
 
136
void SVGDocumentExtensions::addAnimationElementToTarget(SVGSMILElement* animationElement, SVGElement* targetElement)
 
137
{
 
138
    ASSERT(targetElement);
 
139
    ASSERT(animationElement);
 
140
 
 
141
    if (HashSet<SVGSMILElement*>* animationElementsForTarget = m_animatedElements.get(targetElement)) {
 
142
        animationElementsForTarget->add(animationElement);
 
143
        return;
 
144
    }
 
145
 
 
146
    HashSet<SVGSMILElement*>* animationElementsForTarget = new HashSet<SVGSMILElement*>;
 
147
    animationElementsForTarget->add(animationElement);
 
148
    m_animatedElements.set(targetElement, animationElementsForTarget);
 
149
}
 
150
 
 
151
void SVGDocumentExtensions::removeAnimationElementFromTarget(SVGSMILElement* animationElement, SVGElement* targetElement)
 
152
{
 
153
    ASSERT(targetElement);
 
154
    ASSERT(animationElement);
 
155
 
 
156
    HashMap<SVGElement*, HashSet<SVGSMILElement*>* >::iterator it = m_animatedElements.find(targetElement);
 
157
    ASSERT(it != m_animatedElements.end());
 
158
    
 
159
    HashSet<SVGSMILElement*>* animationElementsForTarget = it->value;
 
160
    ASSERT(!animationElementsForTarget->isEmpty());
 
161
 
 
162
    animationElementsForTarget->remove(animationElement);
 
163
    if (animationElementsForTarget->isEmpty()) {
 
164
        m_animatedElements.remove(it);
 
165
        delete animationElementsForTarget;
 
166
    }
 
167
}
 
168
 
 
169
void SVGDocumentExtensions::removeAllAnimationElementsFromTarget(SVGElement* targetElement)
 
170
{
 
171
    ASSERT(targetElement);
 
172
    HashMap<SVGElement*, HashSet<SVGSMILElement*>* >::iterator it = m_animatedElements.find(targetElement);
 
173
    if (it == m_animatedElements.end())
 
174
        return;
 
175
 
 
176
    HashSet<SVGSMILElement*>* animationElementsForTarget = it->value;
 
177
    Vector<SVGSMILElement*> toBeReset;
 
178
 
 
179
    HashSet<SVGSMILElement*>::iterator end = animationElementsForTarget->end();
 
180
    for (HashSet<SVGSMILElement*>::iterator it = animationElementsForTarget->begin(); it != end; ++it)
 
181
        toBeReset.append(*it);
 
182
 
 
183
    Vector<SVGSMILElement*>::iterator vectorEnd = toBeReset.end();
 
184
    for (Vector<SVGSMILElement*>::iterator vectorIt = toBeReset.begin(); vectorIt != vectorEnd; ++vectorIt)
 
185
        (*vectorIt)->resetTargetElement();
 
186
}
 
187
 
 
188
// FIXME: Callers should probably use ScriptController::eventHandlerLineNumber()
 
189
static int parserLineNumber(Document* document)
 
190
{
 
191
    ScriptableDocumentParser* parser = document->scriptableDocumentParser();
 
192
    if (!parser)
 
193
        return 1;
 
194
    return parser->lineNumber().oneBasedInt();
 
195
}
 
196
 
 
197
static void reportMessage(Document* document, MessageLevel level, const String& message)
 
198
{
 
199
    if (document->frame())
 
200
        document->domWindow()->console()->addMessage(JSMessageSource, LogMessageType, level, message, document->documentURI(), parserLineNumber(document));
 
201
}
 
202
 
 
203
void SVGDocumentExtensions::reportWarning(const String& message)
 
204
{
 
205
    reportMessage(m_document, WarningMessageLevel, "Warning: " + message);
 
206
}
 
207
 
 
208
void SVGDocumentExtensions::reportError(const String& message)
 
209
{
 
210
    reportMessage(m_document, ErrorMessageLevel, "Error: " + message);
 
211
}
 
212
 
 
213
void SVGDocumentExtensions::addPendingResource(const AtomicString& id, SVGElement* element)
 
214
{
 
215
    ASSERT(element);
 
216
 
 
217
    if (id.isEmpty())
 
218
        return;
 
219
 
 
220
    // The HashMap add function leaves the map alone and returns a pointer to the element in the
 
221
    // map if the element already exists. So we add with a value of 0, and it either finds the
 
222
    // existing element or adds a new one in a single operation. The ".iterator->value" idiom gets
 
223
    // us to the iterator from add's result, and then to the value inside the hash table.
 
224
    SVGPendingElements*& set = m_pendingResources.add(id, 0).iterator->value;
 
225
    if (!set)
 
226
        set = new SVGPendingElements;
 
227
    set->add(element);
 
228
 
 
229
    element->setHasPendingResources();
 
230
}
 
231
 
 
232
bool SVGDocumentExtensions::hasPendingResource(const AtomicString& id) const
 
233
{
 
234
    if (id.isEmpty())
 
235
        return false;
 
236
 
 
237
    return m_pendingResources.contains(id);
 
238
}
 
239
 
 
240
bool SVGDocumentExtensions::isElementPendingResources(SVGElement* element) const
 
241
{
 
242
    // This algorithm takes time proportional to the number of pending resources and need not.
 
243
    // If performance becomes an issue we can keep a counted set of elements and answer the question efficiently.
 
244
 
 
245
    ASSERT(element);
 
246
 
 
247
    HashMap<AtomicString, SVGPendingElements*>::const_iterator end = m_pendingResources.end();
 
248
    for (HashMap<AtomicString, SVGPendingElements*>::const_iterator it = m_pendingResources.begin(); it != end; ++it) {
 
249
        SVGPendingElements* elements = it->value;
 
250
        ASSERT(elements);
 
251
 
 
252
        if (elements->contains(element))
 
253
            return true;
 
254
    }
 
255
    return false;
 
256
}
 
257
 
 
258
bool SVGDocumentExtensions::isElementPendingResource(SVGElement* element, const AtomicString& id) const
 
259
{
 
260
    ASSERT(element);
 
261
 
 
262
    if (!hasPendingResource(id))
 
263
        return false;
 
264
 
 
265
    return m_pendingResources.get(id)->contains(element);
 
266
}
 
267
 
 
268
void SVGDocumentExtensions::removeElementFromPendingResources(SVGElement* element)
 
269
{
 
270
    ASSERT(element);
 
271
 
 
272
    // Remove the element from pending resources.
 
273
    if (!m_pendingResources.isEmpty() && element->hasPendingResources()) {
 
274
        Vector<AtomicString> toBeRemoved;
 
275
        HashMap<AtomicString, SVGPendingElements*>::iterator end = m_pendingResources.end();
 
276
        for (HashMap<AtomicString, SVGPendingElements*>::iterator it = m_pendingResources.begin(); it != end; ++it) {
 
277
            SVGPendingElements* elements = it->value;
 
278
            ASSERT(elements);
 
279
            ASSERT(!elements->isEmpty());
 
280
 
 
281
            elements->remove(element);
 
282
            if (elements->isEmpty())
 
283
                toBeRemoved.append(it->key);
 
284
        }
 
285
 
 
286
        element->clearHasPendingResourcesIfPossible();
 
287
 
 
288
        // We use the removePendingResource function here because it deals with set lifetime correctly.
 
289
        Vector<AtomicString>::iterator vectorEnd = toBeRemoved.end();
 
290
        for (Vector<AtomicString>::iterator it = toBeRemoved.begin(); it != vectorEnd; ++it)
 
291
            removePendingResource(*it);
 
292
    }
 
293
 
 
294
    // Remove the element from pending resources that were scheduled for removal.
 
295
    if (!m_pendingResourcesForRemoval.isEmpty()) {
 
296
        Vector<AtomicString> toBeRemoved;
 
297
        HashMap<AtomicString, SVGPendingElements*>::iterator end = m_pendingResourcesForRemoval.end();
 
298
        for (HashMap<AtomicString, SVGPendingElements*>::iterator it = m_pendingResourcesForRemoval.begin(); it != end; ++it) {
 
299
            SVGPendingElements* elements = it->value;
 
300
            ASSERT(elements);
 
301
            ASSERT(!elements->isEmpty());
 
302
 
 
303
            elements->remove(element);
 
304
            if (elements->isEmpty())
 
305
                toBeRemoved.append(it->key);
 
306
        }
 
307
 
 
308
        // We use the removePendingResourceForRemoval function here because it deals with set lifetime correctly.
 
309
        Vector<AtomicString>::iterator vectorEnd = toBeRemoved.end();
 
310
        for (Vector<AtomicString>::iterator it = toBeRemoved.begin(); it != vectorEnd; ++it)
 
311
            removePendingResourceForRemoval(*it);
 
312
    }
 
313
}
 
314
 
 
315
PassOwnPtr<SVGDocumentExtensions::SVGPendingElements> SVGDocumentExtensions::removePendingResource(const AtomicString& id)
 
316
{
 
317
    ASSERT(m_pendingResources.contains(id));
 
318
    return adoptPtr(m_pendingResources.take(id));
 
319
}
 
320
 
 
321
PassOwnPtr<SVGDocumentExtensions::SVGPendingElements> SVGDocumentExtensions::removePendingResourceForRemoval(const AtomicString& id)
 
322
{
 
323
    ASSERT(m_pendingResourcesForRemoval.contains(id));
 
324
    return adoptPtr(m_pendingResourcesForRemoval.take(id));
 
325
}
 
326
 
 
327
void SVGDocumentExtensions::markPendingResourcesForRemoval(const AtomicString& id)
 
328
{
 
329
    if (id.isEmpty())
 
330
        return;
 
331
 
 
332
    ASSERT(!m_pendingResourcesForRemoval.contains(id));
 
333
 
 
334
    SVGPendingElements* existing = m_pendingResources.take(id);
 
335
    if (existing && !existing->isEmpty())
 
336
        m_pendingResourcesForRemoval.add(id, existing);
 
337
}
 
338
 
 
339
SVGElement* SVGDocumentExtensions::removeElementFromPendingResourcesForRemoval(const AtomicString& id)
 
340
{
 
341
    if (id.isEmpty())
 
342
        return 0;
 
343
 
 
344
    SVGPendingElements* resourceSet = m_pendingResourcesForRemoval.get(id);
 
345
    if (!resourceSet || resourceSet->isEmpty())
 
346
        return 0;
 
347
 
 
348
    SVGPendingElements::iterator firstElement = resourceSet->begin();
 
349
    SVGElement* element = *firstElement;
 
350
 
 
351
    resourceSet->remove(firstElement);
 
352
 
 
353
    if (resourceSet->isEmpty())
 
354
        removePendingResourceForRemoval(id);
 
355
 
 
356
    return element;
 
357
}
 
358
 
 
359
HashSet<SVGElement*>* SVGDocumentExtensions::setOfElementsReferencingTarget(SVGElement* referencedElement) const
 
360
{
 
361
    ASSERT(referencedElement);
 
362
    const HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > >::const_iterator it = m_elementDependencies.find(referencedElement);
 
363
    if (it == m_elementDependencies.end())
 
364
        return 0;
 
365
    return it->value.get();
 
366
}
 
367
 
 
368
void SVGDocumentExtensions::addElementReferencingTarget(SVGElement* referencingElement, SVGElement* referencedElement)
 
369
{
 
370
    ASSERT(referencingElement);
 
371
    ASSERT(referencedElement);
 
372
 
 
373
    if (HashSet<SVGElement*>* elements = m_elementDependencies.get(referencedElement)) {
 
374
        elements->add(referencingElement);
 
375
        return;
 
376
    }
 
377
 
 
378
    OwnPtr<HashSet<SVGElement*> > elements = adoptPtr(new HashSet<SVGElement*>);
 
379
    elements->add(referencingElement);
 
380
    m_elementDependencies.set(referencedElement, elements.release());
 
381
}
 
382
 
 
383
void SVGDocumentExtensions::removeAllTargetReferencesForElement(SVGElement* referencingElement)
 
384
{
 
385
    Vector<SVGElement*> toBeRemoved;
 
386
 
 
387
    HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > >::iterator end = m_elementDependencies.end();
 
388
    for (HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > >::iterator it = m_elementDependencies.begin(); it != end; ++it) {
 
389
        SVGElement* referencedElement = it->key;
 
390
        HashSet<SVGElement*>* referencingElements = it->value.get();
 
391
        HashSet<SVGElement*>::iterator setIt = referencingElements->find(referencingElement);
 
392
        if (setIt == referencingElements->end())
 
393
            continue;
 
394
 
 
395
        referencingElements->remove(setIt);
 
396
        if (referencingElements->isEmpty())
 
397
            toBeRemoved.append(referencedElement);
 
398
    }
 
399
 
 
400
    Vector<SVGElement*>::iterator vectorEnd = toBeRemoved.end();
 
401
    for (Vector<SVGElement*>::iterator it = toBeRemoved.begin(); it != vectorEnd; ++it)
 
402
        m_elementDependencies.remove(*it);
 
403
}
 
404
 
 
405
void SVGDocumentExtensions::removeAllElementReferencesForTarget(SVGElement* referencedElement)
 
406
{
 
407
    ASSERT(referencedElement);
 
408
    HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > >::iterator it = m_elementDependencies.find(referencedElement);
 
409
    if (it == m_elementDependencies.end())
 
410
        return;
 
411
    ASSERT(it->key == referencedElement);
 
412
    Vector<SVGElement*> toBeNotified;
 
413
 
 
414
    HashSet<SVGElement*>* referencingElements = it->value.get();
 
415
    HashSet<SVGElement*>::iterator setEnd = referencingElements->end();
 
416
    for (HashSet<SVGElement*>::iterator setIt = referencingElements->begin(); setIt != setEnd; ++setIt)
 
417
        toBeNotified.append(*setIt);
 
418
 
 
419
    // Force rebuilding the referencingElement so it knows about this change.
 
420
    Vector<SVGElement*>::iterator vectorEnd = toBeNotified.end();
 
421
    for (Vector<SVGElement*>::iterator vectorIt = toBeNotified.begin(); vectorIt != vectorEnd; ++vectorIt) {
 
422
        // Before rebuilding referencingElement ensure it was not removed from under us.
 
423
        if (HashSet<SVGElement*>* referencingElements = setOfElementsReferencingTarget(referencedElement)) {
 
424
            if (referencingElements->contains(*vectorIt))
 
425
                (*vectorIt)->svgAttributeChanged(XLinkNames::hrefAttr);
 
426
        }
 
427
    }
 
428
 
 
429
    m_elementDependencies.remove(referencedElement);
 
430
}
 
431
 
 
432
#if ENABLE(SVG_FONTS)
 
433
void SVGDocumentExtensions::registerSVGFontFaceElement(SVGFontFaceElement* element)
 
434
{
 
435
    m_svgFontFaceElements.add(element);
 
436
}
 
437
 
 
438
void SVGDocumentExtensions::unregisterSVGFontFaceElement(SVGFontFaceElement* element)
 
439
{
 
440
    ASSERT(m_svgFontFaceElements.contains(element));
 
441
    m_svgFontFaceElements.remove(element);
 
442
}
 
443
#endif
 
444
 
 
445
}
 
446
 
 
447
#endif