/src/mozilla-central/dom/svg/SVGFEImageElement.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "mozilla/dom/SVGFEImageElement.h" |
8 | | |
9 | | #include "mozilla/EventStateManager.h" |
10 | | #include "mozilla/EventStates.h" |
11 | | #include "mozilla/dom/SVGFEImageElementBinding.h" |
12 | | #include "mozilla/dom/SVGFilterElement.h" |
13 | | #include "mozilla/gfx/2D.h" |
14 | | #include "mozilla/RefPtr.h" |
15 | | #include "nsContentUtils.h" |
16 | | #include "nsLayoutUtils.h" |
17 | | #include "nsSVGUtils.h" |
18 | | #include "nsNetUtil.h" |
19 | | #include "imgIContainer.h" |
20 | | #include "gfx2DGlue.h" |
21 | | |
22 | | NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(FEImage) |
23 | | |
24 | | using namespace mozilla::gfx; |
25 | | |
26 | | namespace mozilla { |
27 | | namespace dom { |
28 | | |
29 | | JSObject* |
30 | | SVGFEImageElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto) |
31 | 0 | { |
32 | 0 | return SVGFEImageElement_Binding::Wrap(aCx, this, aGivenProto); |
33 | 0 | } |
34 | | |
35 | | nsSVGElement::StringInfo SVGFEImageElement::sStringInfo[3] = |
36 | | { |
37 | | { &nsGkAtoms::result, kNameSpaceID_None, true }, |
38 | | { &nsGkAtoms::href, kNameSpaceID_None, true }, |
39 | | { &nsGkAtoms::href, kNameSpaceID_XLink, true } |
40 | | }; |
41 | | |
42 | | //---------------------------------------------------------------------- |
43 | | // nsISupports methods |
44 | | |
45 | | NS_IMPL_ISUPPORTS_INHERITED(SVGFEImageElement, SVGFEImageElementBase, |
46 | | imgINotificationObserver, nsIImageLoadingContent) |
47 | | |
48 | | //---------------------------------------------------------------------- |
49 | | // Implementation |
50 | | |
51 | | SVGFEImageElement::SVGFEImageElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) |
52 | | : SVGFEImageElementBase(std::move(aNodeInfo)) |
53 | 0 | { |
54 | 0 | // We start out broken |
55 | 0 | AddStatesSilently(NS_EVENT_STATE_BROKEN); |
56 | 0 | } |
57 | | |
58 | | SVGFEImageElement::~SVGFEImageElement() |
59 | 0 | { |
60 | 0 | DestroyImageLoadingContent(); |
61 | 0 | } |
62 | | |
63 | | //---------------------------------------------------------------------- |
64 | | |
65 | | nsresult |
66 | | SVGFEImageElement::LoadSVGImage(bool aForce, bool aNotify) |
67 | 0 | { |
68 | 0 | // resolve href attribute |
69 | 0 | nsCOMPtr<nsIURI> baseURI = GetBaseURI(); |
70 | 0 |
|
71 | 0 | nsAutoString href; |
72 | 0 | if (mStringAttributes[HREF].IsExplicitlySet()) { |
73 | 0 | mStringAttributes[HREF].GetAnimValue(href, this); |
74 | 0 | } else { |
75 | 0 | mStringAttributes[XLINK_HREF].GetAnimValue(href, this); |
76 | 0 | } |
77 | 0 | href.Trim(" \t\n\r"); |
78 | 0 |
|
79 | 0 | if (baseURI && !href.IsEmpty()) |
80 | 0 | NS_MakeAbsoluteURI(href, href, baseURI); |
81 | 0 |
|
82 | 0 | // Make sure we don't get in a recursive death-spiral |
83 | 0 | nsIDocument* doc = OwnerDoc(); |
84 | 0 | nsCOMPtr<nsIURI> hrefAsURI; |
85 | 0 | if (NS_SUCCEEDED(StringToURI(href, doc, getter_AddRefs(hrefAsURI)))) { |
86 | 0 | bool isEqual; |
87 | 0 | if (NS_SUCCEEDED(hrefAsURI->Equals(baseURI, &isEqual)) && isEqual) { |
88 | 0 | // Image URI matches our URI exactly! Bail out. |
89 | 0 | return NS_OK; |
90 | 0 | } |
91 | 0 | } |
92 | 0 | |
93 | 0 | // Mark channel as urgent-start before load image if the image load is |
94 | 0 | // initaiated by a user interaction. |
95 | 0 | mUseUrgentStartForChannel = EventStateManager::IsHandlingUserInput(); |
96 | 0 | return LoadImage(href, aForce, aNotify, eImageLoadType_Normal); |
97 | 0 | } |
98 | | |
99 | | //---------------------------------------------------------------------- |
100 | | // EventTarget methods: |
101 | | |
102 | | void |
103 | | SVGFEImageElement::AsyncEventRunning(AsyncEventDispatcher* aEvent) |
104 | 0 | { |
105 | 0 | nsImageLoadingContent::AsyncEventRunning(aEvent); |
106 | 0 | } |
107 | | |
108 | | //---------------------------------------------------------------------- |
109 | | // nsIContent methods: |
110 | | |
111 | | NS_IMETHODIMP_(bool) |
112 | | SVGFEImageElement::IsAttributeMapped(const nsAtom* name) const |
113 | 0 | { |
114 | 0 | static const MappedAttributeEntry* const map[] = { |
115 | 0 | sGraphicsMap |
116 | 0 | }; |
117 | 0 |
|
118 | 0 | return FindAttributeDependence(name, map) || |
119 | 0 | SVGFEImageElementBase::IsAttributeMapped(name); |
120 | 0 | } |
121 | | |
122 | | nsresult |
123 | | SVGFEImageElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName, |
124 | | const nsAttrValue* aValue, |
125 | | const nsAttrValue* aOldValue, |
126 | | nsIPrincipal* aSubjectPrincipal, |
127 | | bool aNotify) |
128 | 0 | { |
129 | 0 | if (aName == nsGkAtoms::href && |
130 | 0 | (aNamespaceID == kNameSpaceID_XLink || |
131 | 0 | aNamespaceID == kNameSpaceID_None)) { |
132 | 0 |
|
133 | 0 | if (aValue) { |
134 | 0 | LoadSVGImage(true, aNotify); |
135 | 0 | } else { |
136 | 0 | CancelImageRequests(aNotify); |
137 | 0 | } |
138 | 0 | } |
139 | 0 |
|
140 | 0 | return SVGFEImageElementBase::AfterSetAttr(aNamespaceID, aName, |
141 | 0 | aValue, aOldValue, |
142 | 0 | aSubjectPrincipal, |
143 | 0 | aNotify); |
144 | 0 | } |
145 | | |
146 | | void |
147 | | SVGFEImageElement::MaybeLoadSVGImage() |
148 | 0 | { |
149 | 0 | if ((mStringAttributes[HREF].IsExplicitlySet() || |
150 | 0 | mStringAttributes[XLINK_HREF].IsExplicitlySet() ) && |
151 | 0 | (NS_FAILED(LoadSVGImage(false, true)) || |
152 | 0 | !LoadingEnabled())) { |
153 | 0 | CancelImageRequests(true); |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | | nsresult |
158 | | SVGFEImageElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent, |
159 | | nsIContent* aBindingParent) |
160 | 0 | { |
161 | 0 | nsresult rv = SVGFEImageElementBase::BindToTree(aDocument, aParent, |
162 | 0 | aBindingParent); |
163 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
164 | 0 |
|
165 | 0 | nsImageLoadingContent::BindToTree(aDocument, aParent, aBindingParent); |
166 | 0 |
|
167 | 0 | if (mStringAttributes[HREF].IsExplicitlySet() || |
168 | 0 | mStringAttributes[XLINK_HREF].IsExplicitlySet()) { |
169 | 0 | // FIXME: Bug 660963 it would be nice if we could just have |
170 | 0 | // ClearBrokenState update our state and do it fast... |
171 | 0 | ClearBrokenState(); |
172 | 0 | RemoveStatesSilently(NS_EVENT_STATE_BROKEN); |
173 | 0 | nsContentUtils::AddScriptRunner( |
174 | 0 | NewRunnableMethod("dom::SVGFEImageElement::MaybeLoadSVGImage", |
175 | 0 | this, |
176 | 0 | &SVGFEImageElement::MaybeLoadSVGImage)); |
177 | 0 | } |
178 | 0 |
|
179 | 0 | return rv; |
180 | 0 | } |
181 | | |
182 | | void |
183 | | SVGFEImageElement::UnbindFromTree(bool aDeep, bool aNullParent) |
184 | 0 | { |
185 | 0 | nsImageLoadingContent::UnbindFromTree(aDeep, aNullParent); |
186 | 0 | SVGFEImageElementBase::UnbindFromTree(aDeep, aNullParent); |
187 | 0 | } |
188 | | |
189 | | EventStates |
190 | | SVGFEImageElement::IntrinsicState() const |
191 | 0 | { |
192 | 0 | return SVGFEImageElementBase::IntrinsicState() | |
193 | 0 | nsImageLoadingContent::ImageState(); |
194 | 0 | } |
195 | | |
196 | | //---------------------------------------------------------------------- |
197 | | // nsINode methods |
198 | | |
199 | | NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFEImageElement) |
200 | | |
201 | | already_AddRefed<SVGAnimatedString> |
202 | | SVGFEImageElement::Href() |
203 | 0 | { |
204 | 0 | return mStringAttributes[HREF].IsExplicitlySet() |
205 | 0 | ? mStringAttributes[HREF].ToDOMAnimatedString(this) |
206 | 0 | : mStringAttributes[XLINK_HREF].ToDOMAnimatedString(this); |
207 | 0 | } |
208 | | |
209 | | //---------------------------------------------------------------------- |
210 | | // nsIDOMSVGFEImageElement methods |
211 | | |
212 | | FilterPrimitiveDescription |
213 | | SVGFEImageElement::GetPrimitiveDescription(nsSVGFilterInstance* aInstance, |
214 | | const IntRect& aFilterSubregion, |
215 | | const nsTArray<bool>& aInputsAreTainted, |
216 | | nsTArray<RefPtr<SourceSurface>>& aInputImages) |
217 | 0 | { |
218 | 0 | nsIFrame* frame = GetPrimaryFrame(); |
219 | 0 | if (!frame) { |
220 | 0 | return FilterPrimitiveDescription(); |
221 | 0 | } |
222 | 0 | |
223 | 0 | nsCOMPtr<imgIRequest> currentRequest; |
224 | 0 | GetRequest(nsIImageLoadingContent::CURRENT_REQUEST, |
225 | 0 | getter_AddRefs(currentRequest)); |
226 | 0 |
|
227 | 0 | nsCOMPtr<imgIContainer> imageContainer; |
228 | 0 | if (currentRequest) { |
229 | 0 | currentRequest->GetImage(getter_AddRefs(imageContainer)); |
230 | 0 | } |
231 | 0 |
|
232 | 0 | RefPtr<SourceSurface> image; |
233 | 0 | if (imageContainer) { |
234 | 0 | uint32_t flags = imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY; |
235 | 0 | image = imageContainer->GetFrame(imgIContainer::FRAME_CURRENT, flags); |
236 | 0 | } |
237 | 0 |
|
238 | 0 | if (!image) { |
239 | 0 | return FilterPrimitiveDescription(); |
240 | 0 | } |
241 | 0 | |
242 | 0 | IntSize nativeSize; |
243 | 0 | imageContainer->GetWidth(&nativeSize.width); |
244 | 0 | imageContainer->GetHeight(&nativeSize.height); |
245 | 0 |
|
246 | 0 | Matrix viewBoxTM = |
247 | 0 | SVGContentUtils::GetViewBoxTransform(aFilterSubregion.width, aFilterSubregion.height, |
248 | 0 | 0, 0, nativeSize.width, nativeSize.height, |
249 | 0 | mPreserveAspectRatio); |
250 | 0 | Matrix TM = viewBoxTM; |
251 | 0 | TM.PostTranslate(aFilterSubregion.x, aFilterSubregion.y); |
252 | 0 |
|
253 | 0 | SamplingFilter samplingFilter = nsLayoutUtils::GetSamplingFilterForFrame(frame); |
254 | 0 |
|
255 | 0 | ImageAttributes atts; |
256 | 0 | atts.mFilter = (uint32_t)samplingFilter; |
257 | 0 | atts.mTransform = TM; |
258 | 0 |
|
259 | 0 | // Append the image to aInputImages and store its index in the description. |
260 | 0 | size_t imageIndex = aInputImages.Length(); |
261 | 0 | aInputImages.AppendElement(image); |
262 | 0 | atts.mInputIndex = (uint32_t)imageIndex; |
263 | 0 | return FilterPrimitiveDescription(AsVariant(std::move(atts))); |
264 | 0 | } |
265 | | |
266 | | bool |
267 | | SVGFEImageElement::AttributeAffectsRendering(int32_t aNameSpaceID, |
268 | | nsAtom* aAttribute) const |
269 | 0 | { |
270 | 0 | // nsGkAtoms::href is deliberately omitted as the frame has special |
271 | 0 | // handling to load the image |
272 | 0 | return SVGFEImageElementBase::AttributeAffectsRendering(aNameSpaceID, aAttribute) || |
273 | 0 | (aNameSpaceID == kNameSpaceID_None && |
274 | 0 | aAttribute == nsGkAtoms::preserveAspectRatio); |
275 | 0 | } |
276 | | |
277 | | bool |
278 | | SVGFEImageElement::OutputIsTainted(const nsTArray<bool>& aInputsAreTainted, |
279 | | nsIPrincipal* aReferencePrincipal) |
280 | 0 | { |
281 | 0 | nsresult rv; |
282 | 0 | nsCOMPtr<imgIRequest> currentRequest; |
283 | 0 | GetRequest(nsIImageLoadingContent::CURRENT_REQUEST, |
284 | 0 | getter_AddRefs(currentRequest)); |
285 | 0 |
|
286 | 0 | if (!currentRequest) { |
287 | 0 | return false; |
288 | 0 | } |
289 | 0 | |
290 | 0 | uint32_t status; |
291 | 0 | currentRequest->GetImageStatus(&status); |
292 | 0 | if ((status & imgIRequest::STATUS_LOAD_COMPLETE) == 0) { |
293 | 0 | // The load has not completed yet. |
294 | 0 | return false; |
295 | 0 | } |
296 | 0 | |
297 | 0 | nsCOMPtr<nsIPrincipal> principal; |
298 | 0 | rv = currentRequest->GetImagePrincipal(getter_AddRefs(principal)); |
299 | 0 | if (NS_FAILED(rv) || !principal) { |
300 | 0 | return true; |
301 | 0 | } |
302 | 0 | |
303 | 0 | int32_t corsmode; |
304 | 0 | if (NS_SUCCEEDED(currentRequest->GetCORSMode(&corsmode)) && |
305 | 0 | corsmode != imgIRequest::CORS_NONE) { |
306 | 0 | // If CORS was used to load the image, the page is allowed to read from it. |
307 | 0 | return false; |
308 | 0 | } |
309 | 0 | |
310 | 0 | if (aReferencePrincipal->Subsumes(principal)) { |
311 | 0 | // The page is allowed to read from the image. |
312 | 0 | return false; |
313 | 0 | } |
314 | 0 | |
315 | 0 | return true; |
316 | 0 | } |
317 | | |
318 | | //---------------------------------------------------------------------- |
319 | | // nsSVGElement methods |
320 | | |
321 | | already_AddRefed<DOMSVGAnimatedPreserveAspectRatio> |
322 | | SVGFEImageElement::PreserveAspectRatio() |
323 | 0 | { |
324 | 0 | return mPreserveAspectRatio.ToDOMAnimatedPreserveAspectRatio(this); |
325 | 0 | } |
326 | | |
327 | | SVGAnimatedPreserveAspectRatio * |
328 | | SVGFEImageElement::GetPreserveAspectRatio() |
329 | 0 | { |
330 | 0 | return &mPreserveAspectRatio; |
331 | 0 | } |
332 | | |
333 | | nsSVGElement::StringAttributesInfo |
334 | | SVGFEImageElement::GetStringInfo() |
335 | 0 | { |
336 | 0 | return StringAttributesInfo(mStringAttributes, sStringInfo, |
337 | 0 | ArrayLength(sStringInfo)); |
338 | 0 | } |
339 | | |
340 | | //---------------------------------------------------------------------- |
341 | | // imgINotificationObserver methods |
342 | | |
343 | | NS_IMETHODIMP |
344 | | SVGFEImageElement::Notify(imgIRequest* aRequest, int32_t aType, const nsIntRect* aData) |
345 | 0 | { |
346 | 0 | nsresult rv = nsImageLoadingContent::Notify(aRequest, aType, aData); |
347 | 0 |
|
348 | 0 | if (aType == imgINotificationObserver::SIZE_AVAILABLE) { |
349 | 0 | // Request a decode |
350 | 0 | nsCOMPtr<imgIContainer> container; |
351 | 0 | aRequest->GetImage(getter_AddRefs(container)); |
352 | 0 | MOZ_ASSERT(container, "who sent the notification then?"); |
353 | 0 | container->StartDecoding(imgIContainer::FLAG_NONE); |
354 | 0 | } |
355 | 0 |
|
356 | 0 | if (aType == imgINotificationObserver::LOAD_COMPLETE || |
357 | 0 | aType == imgINotificationObserver::FRAME_UPDATE || |
358 | 0 | aType == imgINotificationObserver::SIZE_AVAILABLE) { |
359 | 0 | Invalidate(); |
360 | 0 | } |
361 | 0 |
|
362 | 0 | return rv; |
363 | 0 | } |
364 | | |
365 | | //---------------------------------------------------------------------- |
366 | | // helper methods |
367 | | |
368 | | void |
369 | | SVGFEImageElement::Invalidate() |
370 | 0 | { |
371 | 0 | if (GetParent() && GetParent()->IsSVGElement(nsGkAtoms::filter)) { |
372 | 0 | static_cast<SVGFilterElement*>(GetParent())->Invalidate(); |
373 | 0 | } |
374 | 0 | } |
375 | | |
376 | | } // namespace dom |
377 | | } // namespace mozilla |