1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/SVGFEImageElement.h"
#include "imgIContainer.h"
#include "mozilla/RefPtr.h"
#include "mozilla/SVGObserverUtils.h"
#include "mozilla/dom/BindContext.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/FetchPriority.h"
#include "mozilla/dom/SVGFEImageElementBinding.h"
#include "mozilla/dom/SVGFilterElement.h"
#include "mozilla/dom/UserActivation.h"
#include "mozilla/gfx/2D.h"
#include "nsContentUtils.h"
#include "nsLayoutUtils.h"
#include "nsNetUtil.h"
NS_IMPL_NS_NEW_SVG_ELEMENT(FEImage)
using namespace mozilla::gfx;
namespace mozilla::dom {
JSObject* SVGFEImageElement::WrapNode(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return SVGFEImageElement_Binding::Wrap(aCx, this, aGivenProto);
}
SVGElement::StringInfo SVGFEImageElement::sStringInfo[3] = {
{nsGkAtoms::result, kNameSpaceID_None, true},
{nsGkAtoms::href, kNameSpaceID_None, true},
{nsGkAtoms::href, kNameSpaceID_XLink, true}};
// Cycle collection magic -- based on SVGUseElement
NS_IMPL_CYCLE_COLLECTION_CLASS(SVGFEImageElement)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SVGFEImageElement,
SVGFEImageElementBase)
tmp->mImageContentObserver = nullptr;
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SVGFEImageElement,
SVGFEImageElementBase)
SVGObserverUtils::TraverseFEImageObserver(tmp, &cb);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
//----------------------------------------------------------------------
// nsISupports methods
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(SVGFEImageElement,
SVGFEImageElementBase,
imgINotificationObserver,
nsIImageLoadingContent)
//----------------------------------------------------------------------
// Implementation
SVGFEImageElement::SVGFEImageElement(
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
: SVGFEImageElementBase(std::move(aNodeInfo)) {
// We start out broken
AddStatesSilently(ElementState::BROKEN);
}
SVGFEImageElement::~SVGFEImageElement() { nsImageLoadingContent::Destroy(); }
//----------------------------------------------------------------------
void SVGFEImageElement::UpdateSrcURI() {
nsAutoString href;
HrefAsString(href);
mImageContentObserver = nullptr;
mSrcURI = nullptr;
if (!href.IsEmpty()) {
StringToURI(href, OwnerDoc(), getter_AddRefs(mSrcURI));
}
}
void SVGFEImageElement::LoadSelectedImage(bool aAlwaysLoad,
bool aStopLazyLoading) {
// Make sure we don't get in a recursive death-spiral
bool isEqual;
if (mSrcURI && NS_SUCCEEDED(mSrcURI->Equals(GetBaseURI(), &isEqual)) &&
isEqual) {
// Image URI matches our URI exactly! Bail out.
return;
}
const bool kNotify = true;
if (SVGObserverUtils::GetAndObserveFEImageContent(this)) {
// We have a local target, don't try to load an image.
CancelImageRequests(kNotify);
return;
}
nsresult rv = NS_ERROR_FAILURE;
if (mSrcURI || (mStringAttributes[HREF].IsExplicitlySet() ||
mStringAttributes[XLINK_HREF].IsExplicitlySet())) {
rv = LoadImage(mSrcURI, /* aForce = */ true, kNotify, eImageLoadType_Normal,
LoadFlags(), OwnerDoc());
}
if (NS_FAILED(rv)) {
CancelImageRequests(kNotify);
}
}
//----------------------------------------------------------------------
// EventTarget methods:
void SVGFEImageElement::AsyncEventRunning(AsyncEventDispatcher* aEvent) {
nsImageLoadingContent::AsyncEventRunning(aEvent);
}
//----------------------------------------------------------------------
// nsIContent methods:
bool SVGFEImageElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
const nsAString& aValue,
nsIPrincipal* aMaybeScriptedPrincipal,
nsAttrValue& aResult) {
if (aNamespaceID == kNameSpaceID_None) {
if (aAttribute == nsGkAtoms::crossorigin) {
ParseCORSValue(aValue, aResult);
return true;
}
if (aAttribute == nsGkAtoms::fetchpriority) {
ParseFetchPriority(aValue, aResult);
return true;
}
}
return SVGFEImageElementBase::ParseAttribute(
aNamespaceID, aAttribute, aValue, aMaybeScriptedPrincipal, aResult);
}
void SVGFEImageElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
const nsAttrValue* aValue,
const nsAttrValue* aOldValue,
nsIPrincipal* aSubjectPrincipal,
bool aNotify) {
bool forceReload = false;
if (aName == nsGkAtoms::href && (aNamespaceID == kNameSpaceID_XLink ||
aNamespaceID == kNameSpaceID_None)) {
if (aNamespaceID == kNameSpaceID_XLink &&
mStringAttributes[HREF].IsExplicitlySet()) {
// href overrides xlink:href
return;
}
UpdateSrcURI();
forceReload = true;
} else if (aNamespaceID == kNameSpaceID_None &&
aName == nsGkAtoms::crossorigin) {
forceReload = GetCORSMode() != AttrValueToCORSMode(aOldValue);
}
if (forceReload) {
QueueImageTask(mSrcURI, /* aAlwaysLoad = */ true, aNotify);
}
return SVGFEImageElementBase::AfterSetAttr(
aNamespaceID, aName, aValue, aOldValue, aSubjectPrincipal, aNotify);
}
nsresult SVGFEImageElement::BindToTree(BindContext& aContext,
nsINode& aParent) {
nsresult rv = SVGFEImageElementBase::BindToTree(aContext, aParent);
NS_ENSURE_SUCCESS(rv, rv);
nsImageLoadingContent::BindToTree(aContext, aParent);
if (aContext.InComposedDoc()) {
aContext.OwnerDoc().SetUseCounter(eUseCounter_custom_feImage);
}
return rv;
}
void SVGFEImageElement::UnbindFromTree(UnbindContext& aContext) {
mImageContentObserver = nullptr;
nsImageLoadingContent::UnbindFromTree();
SVGFEImageElementBase::UnbindFromTree(aContext);
}
void SVGFEImageElement::DestroyContent() {
ClearImageLoadTask();
nsImageLoadingContent::Destroy();
SVGFEImageElementBase::DestroyContent();
}
//----------------------------------------------------------------------
// nsINode methods
NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFEImageElement)
void SVGFEImageElement::NodeInfoChanged(Document* aOldDoc) {
SVGFEImageElementBase::NodeInfoChanged(aOldDoc);
// Reparse the URI if needed. Note that we can't check whether we already have
// a parsed URI, because it might be null even if we have a valid href
// attribute, if we tried to parse with a different base.
UpdateSrcURI();
QueueImageTask(mSrcURI, /* aAlwaysLoad = */ true, /* aNotify = */ false);
}
//----------------------------------------------------------------------
// nsImageLoadingContent methods:
CORSMode SVGFEImageElement::GetCORSMode() {
return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin));
}
void SVGFEImageElement::GetFetchPriority(nsAString& aFetchPriority) const {
GetEnumAttr(nsGkAtoms::fetchpriority, kFetchPriorityAttributeValueAuto,
aFetchPriority);
}
//----------------------------------------------------------------------
// nsIDOMSVGFEImageElement methods
FilterPrimitiveDescription SVGFEImageElement::GetPrimitiveDescription(
SVGFilterInstance* aInstance, const IntRect& aFilterSubregion,
const nsTArray<bool>& aInputsAreTainted,
nsTArray<RefPtr<SourceSurface>>& aInputImages) {
nsIFrame* frame = GetPrimaryFrame();
if (!frame) {
return FilterPrimitiveDescription();
}
nsCOMPtr<imgIRequest> currentRequest;
GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
getter_AddRefs(currentRequest));
nsCOMPtr<imgIContainer> imageContainer;
if (currentRequest) {
currentRequest->GetImage(getter_AddRefs(imageContainer));
}
RefPtr<SourceSurface> image;
nsIntSize nativeSize;
if (imageContainer) {
if (NS_FAILED(imageContainer->GetWidth(&nativeSize.width))) {
nativeSize.width = kFallbackIntrinsicWidthInPixels;
}
if (NS_FAILED(imageContainer->GetHeight(&nativeSize.height))) {
nativeSize.height = kFallbackIntrinsicHeightInPixels;
}
uint32_t flags =
imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY;
image = imageContainer->GetFrameAtSize(nativeSize,
imgIContainer::FRAME_CURRENT, flags);
}
if (!image) {
return FilterPrimitiveDescription();
}
Matrix viewBoxTM = SVGContentUtils::GetViewBoxTransform(
aFilterSubregion.width, aFilterSubregion.height, 0, 0, nativeSize.width,
nativeSize.height, mPreserveAspectRatio);
Matrix TM = viewBoxTM;
TM.PostTranslate(aFilterSubregion.x, aFilterSubregion.y);
SamplingFilter samplingFilter =
nsLayoutUtils::GetSamplingFilterForFrame(frame);
ImageAttributes atts;
atts.mFilter = (uint32_t)samplingFilter;
atts.mTransform = TM;
// Append the image to aInputImages and store its index in the description.
size_t imageIndex = aInputImages.Length();
aInputImages.AppendElement(image);
atts.mInputIndex = (uint32_t)imageIndex;
return FilterPrimitiveDescription(AsVariant(std::move(atts)));
}
bool SVGFEImageElement::AttributeAffectsRendering(int32_t aNameSpaceID,
nsAtom* aAttribute) const {
// nsGkAtoms::href is deliberately omitted as the frame has special
// handling to load the image
return SVGFEImageElementBase::AttributeAffectsRendering(aNameSpaceID,
aAttribute) ||
(aNameSpaceID == kNameSpaceID_None &&
aAttribute == nsGkAtoms::preserveAspectRatio);
}
bool SVGFEImageElement::OutputIsTainted(const nsTArray<bool>& aInputsAreTainted,
nsIPrincipal* aReferencePrincipal) {
nsresult rv;
nsCOMPtr<imgIRequest> currentRequest;
GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
getter_AddRefs(currentRequest));
if (!currentRequest) {
return false;
}
nsCOMPtr<nsIPrincipal> principal;
rv = currentRequest->GetImagePrincipal(getter_AddRefs(principal));
if (NS_FAILED(rv) || !principal) {
return true;
}
// If CORS was used to load the image, the page is allowed to read from it.
if (nsLayoutUtils::ImageRequestUsesCORS(currentRequest)) {
return false;
}
if (aReferencePrincipal->Subsumes(principal)) {
// The page is allowed to read from the image.
return false;
}
return true;
}
//----------------------------------------------------------------------
// SVGElement methods
already_AddRefed<DOMSVGAnimatedString> SVGFEImageElement::Href() {
return mStringAttributes[HREF].IsExplicitlySet()
? mStringAttributes[HREF].ToDOMAnimatedString(this)
: mStringAttributes[XLINK_HREF].ToDOMAnimatedString(this);
}
already_AddRefed<DOMSVGAnimatedPreserveAspectRatio>
SVGFEImageElement::PreserveAspectRatio() {
return mPreserveAspectRatio.ToDOMAnimatedPreserveAspectRatio(this);
}
SVGAnimatedPreserveAspectRatio*
SVGFEImageElement::GetAnimatedPreserveAspectRatio() {
return &mPreserveAspectRatio;
}
SVGElement::StringAttributesInfo SVGFEImageElement::GetStringInfo() {
return StringAttributesInfo(mStringAttributes, sStringInfo,
std::size(sStringInfo));
}
//----------------------------------------------------------------------
// nsIImageLoadingContent methods
NS_IMETHODIMP_(void)
SVGFEImageElement::FrameCreated(nsIFrame* aFrame) {
nsImageLoadingContent::FrameCreated(aFrame);
auto mode = aFrame->PresContext()->ImageAnimationMode();
if (mode == mImageAnimationMode) {
return;
}
mImageAnimationMode = mode;
if (mPendingRequest) {
nsCOMPtr<imgIContainer> container;
mPendingRequest->GetImage(getter_AddRefs(container));
if (container) {
container->SetAnimationMode(mode);
}
}
if (mCurrentRequest) {
nsCOMPtr<imgIContainer> container;
mCurrentRequest->GetImage(getter_AddRefs(container));
if (container) {
container->SetAnimationMode(mode);
}
}
}
//----------------------------------------------------------------------
// imgINotificationObserver methods
void SVGFEImageElement::Notify(imgIRequest* aRequest, int32_t aType,
const nsIntRect* aData) {
nsImageLoadingContent::Notify(aRequest, aType, aData);
if (aType == imgINotificationObserver::SIZE_AVAILABLE) {
// Request a decode
nsCOMPtr<imgIContainer> container;
aRequest->GetImage(getter_AddRefs(container));
MOZ_ASSERT(container, "who sent the notification then?");
container->StartDecoding(imgIContainer::FLAG_NONE);
container->SetAnimationMode(mImageAnimationMode);
}
if (aType == imgINotificationObserver::LOAD_COMPLETE ||
aType == imgINotificationObserver::FRAME_UPDATE ||
aType == imgINotificationObserver::SIZE_AVAILABLE) {
if (auto* filter = SVGFilterElement::FromNodeOrNull(GetParent())) {
SVGObserverUtils::InvalidateDirectRenderingObservers(filter);
}
}
}
void SVGFEImageElement::DidAnimateAttribute(int32_t aNameSpaceID,
nsAtom* aAttribute) {
if ((aNameSpaceID == kNameSpaceID_None ||
aNameSpaceID == kNameSpaceID_XLink) &&
aAttribute == nsGkAtoms::href) {
UpdateSrcURI();
QueueImageTask(mSrcURI, /* aAlwaysLoad = */ true, /* aNotify */ true);
}
SVGFEImageElementBase::DidAnimateAttribute(aNameSpaceID, aAttribute);
}
//----------------------------------------------------------------------
// Public helper methods
void SVGFEImageElement::HrefAsString(nsAString& aHref) {
if (mStringAttributes[HREF].IsExplicitlySet()) {
mStringAttributes[HREF].GetBaseValue(aHref, this);
} else {
mStringAttributes[XLINK_HREF].GetBaseValue(aHref, this);
}
}
void SVGFEImageElement::NotifyImageContentChanged() {
// We don't support rendering fragments yet (bug 455986)
}
} // namespace mozilla::dom
|