c3fcf1ce0a0a19c13c14df7aff457e4bf7ff2c58
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFEImageElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
4  * Copyright (C) 2010 Dirk Schulze <krit@webkit.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 #include "core/svg/SVGFEImageElement.h"
25
26 #include "XLinkNames.h"
27 #include "core/dom/Document.h"
28 #include "core/fetch/FetchRequest.h"
29 #include "core/fetch/ResourceFetcher.h"
30 #include "core/rendering/svg/RenderSVGResource.h"
31 #include "core/svg/SVGElementInstance.h"
32 #include "core/svg/SVGPreserveAspectRatio.h"
33 #include "platform/graphics/Image.h"
34
35 namespace WebCore {
36
37 // Animated property definitions
38 DEFINE_ANIMATED_STRING(SVGFEImageElement, XLinkNames::hrefAttr, Href, href)
39
40 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEImageElement)
41     REGISTER_LOCAL_ANIMATED_PROPERTY(href)
42     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
43 END_REGISTER_ANIMATED_PROPERTIES
44
45 inline SVGFEImageElement::SVGFEImageElement(Document& document)
46     : SVGFilterPrimitiveStandardAttributes(SVGNames::feImageTag, document)
47     , m_preserveAspectRatio(SVGAnimatedPreserveAspectRatio::create(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create()))
48 {
49     ScriptWrappable::init(this);
50     addToPropertyMap(m_preserveAspectRatio);
51     registerAnimatedPropertiesForSVGFEImageElement();
52 }
53
54 PassRefPtr<SVGFEImageElement> SVGFEImageElement::create(Document& document)
55 {
56     return adoptRef(new SVGFEImageElement(document));
57 }
58
59 SVGFEImageElement::~SVGFEImageElement()
60 {
61     clearResourceReferences();
62 }
63
64 bool SVGFEImageElement::currentFrameHasSingleSecurityOrigin() const
65 {
66     if (m_cachedImage && m_cachedImage->image())
67         return m_cachedImage->image()->currentFrameHasSingleSecurityOrigin();
68
69     return true;
70 }
71
72 void SVGFEImageElement::clearResourceReferences()
73 {
74     if (m_cachedImage) {
75         m_cachedImage->removeClient(this);
76         m_cachedImage = 0;
77     }
78
79     document().accessSVGExtensions()->removeAllTargetReferencesForElement(this);
80 }
81
82 void SVGFEImageElement::fetchImageResource()
83 {
84     FetchRequest request(ResourceRequest(ownerDocument()->completeURL(hrefCurrentValue())), localName());
85     m_cachedImage = document().fetcher()->fetchImage(request);
86
87     if (m_cachedImage)
88         m_cachedImage->addClient(this);
89 }
90
91 void SVGFEImageElement::buildPendingResource()
92 {
93     clearResourceReferences();
94     if (!inDocument())
95         return;
96
97     AtomicString id;
98     Element* target = SVGURIReference::targetElementFromIRIString(hrefCurrentValue(), document(), &id);
99     if (!target) {
100         if (id.isEmpty())
101             fetchImageResource();
102         else {
103             document().accessSVGExtensions()->addPendingResource(id, this);
104             ASSERT(hasPendingResources());
105         }
106     } else if (target->isSVGElement()) {
107         // Register us with the target in the dependencies map. Any change of hrefElement
108         // that leads to relayout/repainting now informs us, so we can react to it.
109         document().accessSVGExtensions()->addElementReferencingTarget(this, toSVGElement(target));
110     }
111
112     invalidate();
113 }
114
115 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
116 {
117     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
118     if (supportedAttributes.isEmpty()) {
119         SVGURIReference::addSupportedAttributes(supportedAttributes);
120         supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
121     }
122     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
123 }
124
125 void SVGFEImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
126 {
127     if (!isSupportedAttribute(name)) {
128         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
129         return;
130     }
131
132     SVGParsingError parseError = NoError;
133
134     if (name == SVGNames::preserveAspectRatioAttr) {
135         m_preserveAspectRatio->setBaseValueAsString(value, parseError);
136     } else if (SVGURIReference::parseAttribute(name, value)) {
137     } else {
138         ASSERT_NOT_REACHED();
139     }
140
141     reportAttributeParsingError(parseError, name, value);
142 }
143
144 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName)
145 {
146     if (!isSupportedAttribute(attrName)) {
147         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
148         return;
149     }
150
151     SVGElementInstance::InvalidationGuard invalidationGuard(this);
152
153     if (attrName == SVGNames::preserveAspectRatioAttr) {
154         invalidate();
155         return;
156     }
157
158     if (SVGURIReference::isKnownAttribute(attrName)) {
159         buildPendingResource();
160         return;
161     }
162
163     ASSERT_NOT_REACHED();
164 }
165
166 Node::InsertionNotificationRequest SVGFEImageElement::insertedInto(ContainerNode* rootParent)
167 {
168     SVGFilterPrimitiveStandardAttributes::insertedInto(rootParent);
169     buildPendingResource();
170     return InsertionDone;
171 }
172
173 void SVGFEImageElement::removedFrom(ContainerNode* rootParent)
174 {
175     SVGFilterPrimitiveStandardAttributes::removedFrom(rootParent);
176     if (rootParent->inDocument())
177         clearResourceReferences();
178 }
179
180 void SVGFEImageElement::notifyFinished(Resource*)
181 {
182     if (!inDocument())
183         return;
184
185     Element* parent = parentElement();
186     ASSERT(parent);
187
188     if (!parent->hasTagName(SVGNames::filterTag) || !parent->renderer())
189         return;
190
191     if (RenderObject* renderer = this->renderer())
192         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
193 }
194
195 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
196 {
197     if (m_cachedImage)
198         return FEImage::createWithImage(filter, m_cachedImage->imageForRenderer(renderer()), m_preserveAspectRatio->currentValue());
199     return FEImage::createWithIRIReference(filter, document(), hrefCurrentValue(), m_preserveAspectRatio->currentValue());
200 }
201
202 }