Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/svg/SVGImageElement.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/ArrayUtils.h"
8
#include "mozilla/EventStateManager.h"
9
#include "mozilla/EventStates.h"
10
11
#include "mozilla/dom/SVGImageElement.h"
12
#include "mozilla/gfx/2D.h"
13
#include "nsCOMPtr.h"
14
#include "nsIURI.h"
15
#include "nsNetUtil.h"
16
#include "imgINotificationObserver.h"
17
#include "mozilla/dom/SVGImageElementBinding.h"
18
#include "mozilla/dom/SVGLengthBinding.h"
19
#include "nsContentUtils.h"
20
21
NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(Image)
22
23
using namespace mozilla::gfx;
24
25
namespace mozilla {
26
namespace dom {
27
28
JSObject*
29
SVGImageElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto)
30
0
{
31
0
  return SVGImageElement_Binding::Wrap(aCx, this, aGivenProto);
32
0
}
33
34
nsSVGElement::LengthInfo SVGImageElement::sLengthInfo[4] =
35
{
36
  { &nsGkAtoms::x, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::X },
37
  { &nsGkAtoms::y, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::Y },
38
  { &nsGkAtoms::width, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::X },
39
  { &nsGkAtoms::height, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::Y },
40
};
41
42
nsSVGElement::StringInfo SVGImageElement::sStringInfo[2] =
43
{
44
  { &nsGkAtoms::href, kNameSpaceID_None, true },
45
  { &nsGkAtoms::href, kNameSpaceID_XLink, true }
46
};
47
48
//----------------------------------------------------------------------
49
// nsISupports methods
50
51
NS_IMPL_ISUPPORTS_INHERITED(SVGImageElement, SVGImageElementBase,
52
                            imgINotificationObserver,
53
                            nsIImageLoadingContent)
54
55
//----------------------------------------------------------------------
56
// Implementation
57
58
SVGImageElement::SVGImageElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
59
  : SVGImageElementBase(std::move(aNodeInfo))
60
0
{
61
0
  // We start out broken
62
0
  AddStatesSilently(NS_EVENT_STATE_BROKEN);
63
0
}
64
65
SVGImageElement::~SVGImageElement()
66
0
{
67
0
  DestroyImageLoadingContent();
68
0
}
69
70
//----------------------------------------------------------------------
71
// nsINode methods
72
73
74
NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGImageElement)
75
76
77
//----------------------------------------------------------------------
78
79
already_AddRefed<SVGAnimatedLength>
80
SVGImageElement::X()
81
0
{
82
0
  return mLengthAttributes[ATTR_X].ToDOMAnimatedLength(this);
83
0
}
84
85
already_AddRefed<SVGAnimatedLength>
86
SVGImageElement::Y()
87
0
{
88
0
  return mLengthAttributes[ATTR_Y].ToDOMAnimatedLength(this);
89
0
}
90
91
already_AddRefed<SVGAnimatedLength>
92
SVGImageElement::Width()
93
0
{
94
0
  return mLengthAttributes[ATTR_WIDTH].ToDOMAnimatedLength(this);
95
0
}
96
97
already_AddRefed<SVGAnimatedLength>
98
SVGImageElement::Height()
99
0
{
100
0
  return mLengthAttributes[ATTR_HEIGHT].ToDOMAnimatedLength(this);
101
0
}
102
103
already_AddRefed<DOMSVGAnimatedPreserveAspectRatio>
104
SVGImageElement::PreserveAspectRatio()
105
0
{
106
0
  return mPreserveAspectRatio.ToDOMAnimatedPreserveAspectRatio(this);
107
0
}
108
109
already_AddRefed<SVGAnimatedString>
110
SVGImageElement::Href()
111
0
{
112
0
  return mStringAttributes[HREF].IsExplicitlySet()
113
0
         ? mStringAttributes[HREF].ToDOMAnimatedString(this)
114
0
         : mStringAttributes[XLINK_HREF].ToDOMAnimatedString(this);
115
0
}
116
117
void
118
SVGImageElement::GetDecoding(nsAString& aValue)
119
0
{
120
0
  GetEnumAttr(nsGkAtoms::decoding, kDecodingTableDefault->tag, aValue);
121
0
}
122
123
//----------------------------------------------------------------------
124
125
nsresult
126
SVGImageElement::LoadSVGImage(bool aForce, bool aNotify)
127
0
{
128
0
  // resolve href attribute
129
0
  nsCOMPtr<nsIURI> baseURI = GetBaseURI();
130
0
131
0
  nsAutoString href;
132
0
  if (mStringAttributes[HREF].IsExplicitlySet()) {
133
0
    mStringAttributes[HREF].GetAnimValue(href, this);
134
0
  } else {
135
0
    mStringAttributes[XLINK_HREF].GetAnimValue(href, this);
136
0
  }
137
0
  href.Trim(" \t\n\r");
138
0
139
0
  if (baseURI && !href.IsEmpty())
140
0
    NS_MakeAbsoluteURI(href, href, baseURI);
141
0
142
0
  // Mark channel as urgent-start before load image if the image load is
143
0
  // initaiated by a user interaction.
144
0
  mUseUrgentStartForChannel = EventStateManager::IsHandlingUserInput();
145
0
146
0
  return LoadImage(href, aForce, aNotify, eImageLoadType_Normal);
147
0
}
148
149
//----------------------------------------------------------------------
150
// EventTarget methods:
151
152
void
153
SVGImageElement::AsyncEventRunning(AsyncEventDispatcher* aEvent)
154
0
{
155
0
  nsImageLoadingContent::AsyncEventRunning(aEvent);
156
0
}
157
158
//----------------------------------------------------------------------
159
// nsIContent methods:
160
161
bool
162
SVGImageElement::ParseAttribute(int32_t aNamespaceID,
163
                                 nsAtom* aAttribute,
164
                                 const nsAString& aValue,
165
                                 nsIPrincipal* aMaybeScriptedPrincipal,
166
                                 nsAttrValue& aResult)
167
0
{
168
0
  if (aNamespaceID == kNameSpaceID_None) {
169
0
    if (aAttribute == nsGkAtoms::decoding) {
170
0
      return aResult.ParseEnumValue(aValue, kDecodingTable, false,
171
0
                                    kDecodingTableDefault);
172
0
    }
173
0
  }
174
0
175
0
  return SVGImageElementBase::ParseAttribute(aNamespaceID, aAttribute, aValue,
176
0
                                             aMaybeScriptedPrincipal, aResult);
177
0
}
178
179
nsresult
180
SVGImageElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
181
                              const nsAttrValue* aValue,
182
                              const nsAttrValue* aOldValue,
183
                              nsIPrincipal* aSubjectPrincipal,
184
                              bool aNotify)
185
0
{
186
0
  if (aName == nsGkAtoms::href &&
187
0
      (aNamespaceID == kNameSpaceID_None ||
188
0
       aNamespaceID == kNameSpaceID_XLink)) {
189
0
190
0
    if (aValue) {
191
0
      LoadSVGImage(true, aNotify);
192
0
    } else {
193
0
      CancelImageRequests(aNotify);
194
0
    }
195
0
  } else if (aName == nsGkAtoms::decoding &&
196
0
             aNamespaceID == kNameSpaceID_None) {
197
0
    // Request sync or async image decoding.
198
0
    SetSyncDecodingHint(aValue &&
199
0
                        static_cast<ImageDecodingType>(aValue->GetEnumValue())
200
0
                          == ImageDecodingType::Sync);
201
0
  }
202
0
  return SVGImageElementBase::AfterSetAttr(aNamespaceID, aName,
203
0
                                           aValue, aOldValue,
204
0
                                           aSubjectPrincipal, aNotify);
205
0
}
206
207
void
208
SVGImageElement::MaybeLoadSVGImage()
209
0
{
210
0
  if ((mStringAttributes[HREF].IsExplicitlySet() ||
211
0
       mStringAttributes[XLINK_HREF].IsExplicitlySet()) &&
212
0
      (NS_FAILED(LoadSVGImage(false, true)) ||
213
0
       !LoadingEnabled())) {
214
0
    CancelImageRequests(true);
215
0
  }
216
0
}
217
218
nsresult
219
SVGImageElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
220
                            nsIContent* aBindingParent)
221
0
{
222
0
  nsresult rv = SVGImageElementBase::BindToTree(aDocument, aParent,
223
0
                                                aBindingParent);
224
0
  NS_ENSURE_SUCCESS(rv, rv);
225
0
226
0
  nsImageLoadingContent::BindToTree(aDocument, aParent, aBindingParent);
227
0
228
0
  if (mStringAttributes[HREF].IsExplicitlySet() ||
229
0
      mStringAttributes[XLINK_HREF].IsExplicitlySet()) {
230
0
    // FIXME: Bug 660963 it would be nice if we could just have
231
0
    // ClearBrokenState update our state and do it fast...
232
0
    ClearBrokenState();
233
0
    RemoveStatesSilently(NS_EVENT_STATE_BROKEN);
234
0
    nsContentUtils::AddScriptRunner(
235
0
      NewRunnableMethod("dom::SVGImageElement::MaybeLoadSVGImage",
236
0
                        this,
237
0
                        &SVGImageElement::MaybeLoadSVGImage));
238
0
  }
239
0
240
0
  return rv;
241
0
}
242
243
void
244
SVGImageElement::UnbindFromTree(bool aDeep, bool aNullParent)
245
0
{
246
0
  nsImageLoadingContent::UnbindFromTree(aDeep, aNullParent);
247
0
  SVGImageElementBase::UnbindFromTree(aDeep, aNullParent);
248
0
}
249
250
EventStates
251
SVGImageElement::IntrinsicState() const
252
0
{
253
0
  return SVGImageElementBase::IntrinsicState() |
254
0
    nsImageLoadingContent::ImageState();
255
0
}
256
257
NS_IMETHODIMP_(bool)
258
SVGImageElement::IsAttributeMapped(const nsAtom* name) const
259
0
{
260
0
  static const MappedAttributeEntry* const map[] = {
261
0
    sViewportsMap,
262
0
  };
263
0
264
0
  return FindAttributeDependence(name, map) ||
265
0
    SVGImageElementBase::IsAttributeMapped(name);
266
0
}
267
268
//----------------------------------------------------------------------
269
// SVGGeometryElement methods
270
271
/* For the purposes of the update/invalidation logic pretend to
272
   be a rectangle. */
273
bool
274
SVGImageElement::GetGeometryBounds(Rect* aBounds,
275
                                   const StrokeOptions& aStrokeOptions,
276
                                   const Matrix& aToBoundsSpace,
277
                                   const Matrix* aToNonScalingStrokeSpace)
278
0
{
279
0
  Rect rect;
280
0
  GetAnimatedLengthValues(&rect.x, &rect.y, &rect.width,
281
0
                          &rect.height, nullptr);
282
0
283
0
  if (rect.IsEmpty()) {
284
0
    // Rendering of the element disabled
285
0
    rect.SetEmpty(); // Make sure width/height are zero and not negative
286
0
  }
287
0
288
0
  *aBounds = aToBoundsSpace.TransformBounds(rect);
289
0
  return true;
290
0
}
291
292
already_AddRefed<Path>
293
SVGImageElement::BuildPath(PathBuilder* aBuilder)
294
0
{
295
0
  // We get called in order to get bounds for this element, and for
296
0
  // hit-testing against it. For that we just pretend to be a rectangle.
297
0
298
0
  float x, y, width, height;
299
0
  GetAnimatedLengthValues(&x, &y, &width, &height, nullptr);
300
0
301
0
  if (width <= 0 || height <= 0) {
302
0
    return nullptr;
303
0
  }
304
0
305
0
  Rect r(x, y, width, height);
306
0
  aBuilder->MoveTo(r.TopLeft());
307
0
  aBuilder->LineTo(r.TopRight());
308
0
  aBuilder->LineTo(r.BottomRight());
309
0
  aBuilder->LineTo(r.BottomLeft());
310
0
  aBuilder->Close();
311
0
312
0
  return aBuilder->Finish();
313
0
}
314
315
//----------------------------------------------------------------------
316
// nsSVGElement methods
317
318
/* virtual */ bool
319
SVGImageElement::HasValidDimensions() const
320
0
{
321
0
  return mLengthAttributes[ATTR_WIDTH].IsExplicitlySet() &&
322
0
         mLengthAttributes[ATTR_WIDTH].GetAnimValInSpecifiedUnits() > 0 &&
323
0
         mLengthAttributes[ATTR_HEIGHT].IsExplicitlySet() &&
324
0
         mLengthAttributes[ATTR_HEIGHT].GetAnimValInSpecifiedUnits() > 0;
325
0
}
326
327
nsSVGElement::LengthAttributesInfo
328
SVGImageElement::GetLengthInfo()
329
0
{
330
0
  return LengthAttributesInfo(mLengthAttributes, sLengthInfo,
331
0
                              ArrayLength(sLengthInfo));
332
0
}
333
334
SVGAnimatedPreserveAspectRatio *
335
SVGImageElement::GetPreserveAspectRatio()
336
0
{
337
0
  return &mPreserveAspectRatio;
338
0
}
339
340
nsSVGElement::StringAttributesInfo
341
SVGImageElement::GetStringInfo()
342
0
{
343
0
  return StringAttributesInfo(mStringAttributes, sStringInfo,
344
0
                              ArrayLength(sStringInfo));
345
0
}
346
347
nsresult
348
SVGImageElement::CopyInnerTo(Element* aDest)
349
0
{
350
0
  if (aDest->OwnerDoc()->IsStaticDocument()) {
351
0
    CreateStaticImageClone(static_cast<SVGImageElement*>(aDest));
352
0
  }
353
0
  return SVGImageElementBase::CopyInnerTo(aDest);
354
0
}
355
356
} // namespace dom
357
} // namespace mozilla
358