1/*
2 * Copyright (C) 2006 Apple Inc. All rights reserved.
3 * Copyright (C) 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef SVGDocumentExtensions_h
22#define SVGDocumentExtensions_h
23
24#if ENABLE(SVG)
25#include <wtf/Forward.h>
26#include <wtf/HashMap.h>
27#include <wtf/HashSet.h>
28#include <wtf/PassOwnPtr.h>
29#include <wtf/text/AtomicStringHash.h>
30#include <wtf/text/StringImpl.h>
31
32namespace WebCore {
33
34class Document;
35class RenderSVGResourceContainer;
36class SVGElement;
37#if ENABLE(SVG_FONTS)
38class SVGFontFaceElement;
39#endif
40class SVGResourcesCache;
41class SVGSMILElement;
42class SVGSVGElement;
43class Element;
44
45class SVGDocumentExtensions {
46    WTF_MAKE_NONCOPYABLE(SVGDocumentExtensions); WTF_MAKE_FAST_ALLOCATED;
47public:
48    typedef HashSet<Element*> SVGPendingElements;
49    SVGDocumentExtensions(Document*);
50    ~SVGDocumentExtensions();
51
52    void addTimeContainer(SVGSVGElement*);
53    void removeTimeContainer(SVGSVGElement*);
54
55    void addResource(const AtomicString& id, RenderSVGResourceContainer*);
56    void removeResource(const AtomicString& id);
57    RenderSVGResourceContainer* resourceById(const AtomicString& id) const;
58
59    void startAnimations();
60    void pauseAnimations();
61    void unpauseAnimations();
62    void dispatchSVGLoadEventToOutermostSVGElements();
63
64    void reportWarning(const String&);
65    void reportError(const String&);
66
67    SVGResourcesCache* resourcesCache() const { return m_resourcesCache.get(); }
68
69    HashSet<SVGElement*>* setOfElementsReferencingTarget(SVGElement* referencedElement) const;
70    void addElementReferencingTarget(SVGElement* referencingElement, SVGElement* referencedElement);
71    void removeAllTargetReferencesForElement(SVGElement*);
72    void rebuildAllElementReferencesForTarget(SVGElement*);
73    void removeAllElementReferencesForTarget(SVGElement*);
74
75#if ENABLE(SVG_FONTS)
76    const HashSet<SVGFontFaceElement*>& svgFontFaceElements() const { return m_svgFontFaceElements; }
77    void registerSVGFontFaceElement(SVGFontFaceElement*);
78    void unregisterSVGFontFaceElement(SVGFontFaceElement*);
79#endif
80
81private:
82    Document* m_document; // weak reference
83    HashSet<SVGSVGElement*> m_timeContainers; // For SVG 1.2 support this will need to be made more general.
84#if ENABLE(SVG_FONTS)
85    HashSet<SVGFontFaceElement*> m_svgFontFaceElements;
86#endif
87    HashMap<AtomicString, RenderSVGResourceContainer*> m_resources;
88    HashMap<AtomicString, OwnPtr<SVGPendingElements> > m_pendingResources; // Resources that are pending.
89    HashMap<AtomicString, OwnPtr<SVGPendingElements> > m_pendingResourcesForRemoval; // Resources that are pending and scheduled for removal.
90    HashMap<SVGElement*, OwnPtr<HashSet<SVGElement*> > > m_elementDependencies;
91    OwnPtr<SVGResourcesCache> m_resourcesCache;
92
93public:
94    // This HashMap contains a list of pending resources. Pending resources, are such
95    // which are referenced by any object in the SVG document, but do NOT exist yet.
96    // For instance, dynamically build gradients / patterns / clippers...
97    void addPendingResource(const AtomicString& id, Element*);
98    bool hasPendingResource(const AtomicString& id) const;
99    bool isElementPendingResources(Element*) const;
100    bool isElementPendingResource(Element*, const AtomicString& id) const;
101    void clearHasPendingResourcesIfPossible(Element*);
102    void removeElementFromPendingResources(Element*);
103    PassOwnPtr<SVGPendingElements> removePendingResource(const AtomicString& id);
104
105    // The following two functions are used for scheduling a pending resource to be removed.
106    void markPendingResourcesForRemoval(const AtomicString&);
107    Element* removeElementFromPendingResourcesForRemoval(const AtomicString&);
108
109private:
110    PassOwnPtr<SVGPendingElements> removePendingResourceForRemoval(const AtomicString&);
111};
112
113}
114
115#endif
116#endif
117