Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/HTMLImageElement.h
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
#ifndef mozilla_dom_HTMLImageElement_h
8
#define mozilla_dom_HTMLImageElement_h
9
10
#include "mozilla/Attributes.h"
11
#include "nsGenericHTMLElement.h"
12
#include "nsImageLoadingContent.h"
13
#include "Units.h"
14
#include "nsCycleCollectionParticipant.h"
15
16
namespace mozilla {
17
class EventChainPreVisitor;
18
namespace dom {
19
20
class ImageLoadTask;
21
22
class ResponsiveImageSelector;
23
class HTMLImageElement final : public nsGenericHTMLElement,
24
                               public nsImageLoadingContent
25
{
26
  friend class HTMLSourceElement;
27
  friend class HTMLPictureElement;
28
  friend class ImageLoadTask;
29
public:
30
  explicit HTMLImageElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
31
32
  static already_AddRefed<HTMLImageElement>
33
    Image(const GlobalObject& aGlobal,
34
          const Optional<uint32_t>& aWidth,
35
          const Optional<uint32_t>& aHeight,
36
          ErrorResult& aError);
37
38
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLImageElement,
39
                                           nsGenericHTMLElement)
40
41
  // nsISupports
42
  NS_DECL_ISUPPORTS_INHERITED
43
44
  virtual bool Draggable() const override;
45
46
  ResponsiveImageSelector* GetResponsiveImageSelector()
47
0
  {
48
0
    return mResponsiveSelector.get();
49
0
  }
50
51
  // Element
52
  virtual bool IsInteractiveHTMLContent(bool aIgnoreTabindex) const override;
53
54
  // EventTarget
55
  virtual void AsyncEventRunning(AsyncEventDispatcher* aEvent) override;
56
57
  NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLImageElement, img)
58
59
  // override from nsImageLoadingContent
60
  CORSMode GetCORSMode() override;
61
62
  // nsIContent
63
  virtual bool ParseAttribute(int32_t aNamespaceID,
64
                                nsAtom* aAttribute,
65
                                const nsAString& aValue,
66
                                nsIPrincipal* aMaybeScriptedPrincipal,
67
                                nsAttrValue& aResult) override;
68
  virtual nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute,
69
                                              int32_t aModType) const override;
70
  NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const override;
71
  virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const override;
72
73
  void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
74
75
  bool IsHTMLFocusable(bool aWithMouse, bool *aIsFocusable, int32_t *aTabIndex) override;
76
77
  virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
78
                              nsIContent* aBindingParent) override;
79
  virtual void UnbindFromTree(bool aDeep, bool aNullParent) override;
80
81
  virtual EventStates IntrinsicState() const override;
82
  virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
83
84
  virtual void NodeInfoChanged(nsIDocument* aOldDoc) override;
85
86
  nsresult CopyInnerTo(HTMLImageElement* aDest);
87
88
  void MaybeLoadImage(bool aAlwaysForceLoad);
89
90
  // Overrides for nsImageLoadingContent's GetNaturalHeight/Width, since we
91
  // handle responsive scaling in the element's version of these methods.
92
  NS_IMETHOD GetNaturalHeight(uint32_t* aNaturalHeight) override;
93
  NS_IMETHOD GetNaturalWidth(uint32_t* aNaturalWidth) override;
94
95
  bool IsMap()
96
0
  {
97
0
    return GetBoolAttr(nsGkAtoms::ismap);
98
0
  }
99
  void SetIsMap(bool aIsMap, ErrorResult& aError)
100
0
  {
101
0
    SetHTMLBoolAttr(nsGkAtoms::ismap, aIsMap, aError);
102
0
  }
103
  MOZ_CAN_RUN_SCRIPT uint32_t Width()
104
0
  {
105
0
    return GetWidthHeightForImage(mCurrentRequest).width;
106
0
  }
107
  void SetWidth(uint32_t aWidth, ErrorResult& aError)
108
0
  {
109
0
    SetUnsignedIntAttr(nsGkAtoms::width, aWidth, 0, aError);
110
0
  }
111
  MOZ_CAN_RUN_SCRIPT uint32_t Height()
112
0
  {
113
0
    return GetWidthHeightForImage(mCurrentRequest).height;
114
0
  }
115
  void SetHeight(uint32_t aHeight, ErrorResult& aError)
116
0
  {
117
0
    SetUnsignedIntAttr(nsGkAtoms::height, aHeight, 0, aError);
118
0
  }
119
  uint32_t NaturalWidth();
120
  uint32_t NaturalHeight();
121
  bool Complete();
122
  uint32_t Hspace()
123
0
  {
124
0
    return GetUnsignedIntAttr(nsGkAtoms::hspace, 0);
125
0
  }
126
  void SetHspace(uint32_t aHspace, ErrorResult& aError)
127
0
  {
128
0
    SetUnsignedIntAttr(nsGkAtoms::hspace, aHspace, 0, aError);
129
0
  }
130
  uint32_t Vspace()
131
0
  {
132
0
    return GetUnsignedIntAttr(nsGkAtoms::vspace, 0);
133
0
  }
134
  void SetVspace(uint32_t aVspace, ErrorResult& aError)
135
0
  {
136
0
    SetUnsignedIntAttr(nsGkAtoms::vspace, aVspace, 0, aError);
137
0
  }
138
139
  void GetAlt(nsAString& aAlt)
140
0
  {
141
0
    GetHTMLAttr(nsGkAtoms::alt, aAlt);
142
0
  }
143
  void SetAlt(const nsAString& aAlt, ErrorResult& aError)
144
0
  {
145
0
    SetHTMLAttr(nsGkAtoms::alt, aAlt, aError);
146
0
  }
147
  void GetSrc(nsAString& aSrc)
148
0
  {
149
0
    GetURIAttr(nsGkAtoms::src, nullptr, aSrc);
150
0
  }
151
  void SetSrc(const nsAString& aSrc, ErrorResult& aError)
152
0
  {
153
0
    SetHTMLAttr(nsGkAtoms::src, aSrc, aError);
154
0
  }
155
  void SetSrc(const nsAString& aSrc, nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError)
156
0
  {
157
0
    SetHTMLAttr(nsGkAtoms::src, aSrc, aTriggeringPrincipal, aError);
158
0
  }
159
  void GetSrcset(nsAString& aSrcset)
160
0
  {
161
0
    GetHTMLAttr(nsGkAtoms::srcset, aSrcset);
162
0
  }
163
  void SetSrcset(const nsAString& aSrcset, nsIPrincipal* aTriggeringPrincipal, ErrorResult& aError)
164
0
  {
165
0
    SetHTMLAttr(nsGkAtoms::srcset, aSrcset, aTriggeringPrincipal, aError);
166
0
  }
167
  void GetCrossOrigin(nsAString& aResult)
168
0
  {
169
0
    // Null for both missing and invalid defaults is ok, since we
170
0
    // always parse to an enum value, so we don't need an invalid
171
0
    // default, and we _want_ the missing default to be null.
172
0
    GetEnumAttr(nsGkAtoms::crossorigin, nullptr, aResult);
173
0
  }
174
  void SetCrossOrigin(const nsAString& aCrossOrigin, ErrorResult& aError)
175
0
  {
176
0
    SetOrRemoveNullableStringAttr(nsGkAtoms::crossorigin, aCrossOrigin, aError);
177
0
  }
178
  void GetUseMap(nsAString& aUseMap)
179
0
  {
180
0
    GetHTMLAttr(nsGkAtoms::usemap, aUseMap);
181
0
  }
182
  void SetUseMap(const nsAString& aUseMap, ErrorResult& aError)
183
0
  {
184
0
    SetHTMLAttr(nsGkAtoms::usemap, aUseMap, aError);
185
0
  }
186
  void GetName(nsAString& aName)
187
0
  {
188
0
    GetHTMLAttr(nsGkAtoms::name, aName);
189
0
  }
190
  void SetName(const nsAString& aName, ErrorResult& aError)
191
0
  {
192
0
    SetHTMLAttr(nsGkAtoms::name, aName, aError);
193
0
  }
194
  void GetAlign(nsAString& aAlign)
195
0
  {
196
0
    GetHTMLAttr(nsGkAtoms::align, aAlign);
197
0
  }
198
  void SetAlign(const nsAString& aAlign, ErrorResult& aError)
199
0
  {
200
0
    SetHTMLAttr(nsGkAtoms::align, aAlign, aError);
201
0
  }
202
  void GetLongDesc(nsAString& aLongDesc)
203
0
  {
204
0
    GetURIAttr(nsGkAtoms::longdesc, nullptr, aLongDesc);
205
0
  }
206
  void SetLongDesc(const nsAString& aLongDesc, ErrorResult& aError)
207
0
  {
208
0
    SetHTMLAttr(nsGkAtoms::longdesc, aLongDesc, aError);
209
0
  }
210
  void GetSizes(nsAString& aSizes)
211
0
  {
212
0
    GetHTMLAttr(nsGkAtoms::sizes, aSizes);
213
0
  }
214
  void SetSizes(const nsAString& aSizes, ErrorResult& aError)
215
0
  {
216
0
    SetHTMLAttr(nsGkAtoms::sizes, aSizes, aError);
217
0
  }
218
  void GetCurrentSrc(nsAString& aValue);
219
  void GetBorder(nsAString& aBorder)
220
0
  {
221
0
    GetHTMLAttr(nsGkAtoms::border, aBorder);
222
0
  }
223
  void SetBorder(const nsAString& aBorder, ErrorResult& aError)
224
0
  {
225
0
    SetHTMLAttr(nsGkAtoms::border, aBorder, aError);
226
0
  }
227
  void SetReferrerPolicy(const nsAString& aReferrer, ErrorResult& aError)
228
0
  {
229
0
    SetHTMLAttr(nsGkAtoms::referrerpolicy, aReferrer, aError);
230
0
  }
231
  void GetReferrerPolicy(nsAString& aReferrer)
232
0
  {
233
0
    GetEnumAttr(nsGkAtoms::referrerpolicy, EmptyCString().get(), aReferrer);
234
0
  }
235
  void SetDecoding(const nsAString& aDecoding, ErrorResult& aError)
236
0
  {
237
0
    SetHTMLAttr(nsGkAtoms::decoding, aDecoding, aError);
238
0
  }
239
  void GetDecoding(nsAString& aValue);
240
241
  net::ReferrerPolicy
242
  GetImageReferrerPolicy() override
243
0
  {
244
0
    return GetReferrerPolicyAsEnum();
245
0
  }
246
247
  MOZ_CAN_RUN_SCRIPT int32_t X();
248
  MOZ_CAN_RUN_SCRIPT int32_t Y();
249
  void GetLowsrc(nsAString& aLowsrc)
250
0
  {
251
0
    GetURIAttr(nsGkAtoms::lowsrc, nullptr, aLowsrc);
252
0
  }
253
  void SetLowsrc(const nsAString& aLowsrc, ErrorResult& aError)
254
0
  {
255
0
    SetHTMLAttr(nsGkAtoms::lowsrc, aLowsrc, aError);
256
0
  }
257
258
#ifdef DEBUG
259
  HTMLFormElement* GetForm() const;
260
#endif
261
  void SetForm(HTMLFormElement* aForm);
262
  void ClearForm(bool aRemoveFromForm);
263
264
  virtual void DestroyContent() override;
265
266
  void MediaFeatureValuesChanged();
267
268
  /**
269
   * Given a hypothetical <img> or <source> tag with the given parameters,
270
   * return what URI we would attempt to use, if any.  Used by the preloader to
271
   * resolve sources prior to DOM creation.
272
   *
273
   * @param aDocument The document this image would be for, for referencing
274
   *        viewport width and DPI/zoom
275
   * @param aIsSourceTag If these parameters are for a <source> tag (as in a
276
   *        <picture>) rather than an <img> tag. Note that some attrs are unused
277
   *        when this is true an vice versa
278
   * @param aSrcAttr [ignored if aIsSourceTag] The src attr for this image.
279
   * @param aSrcsetAttr The srcset attr for this image/source
280
   * @param aSizesAttr The sizes attr for this image/source
281
   * @param aTypeAttr [ignored if !aIsSourceTag] The type attr for this source.
282
   *                  Should be a void string to differentiate no type attribute
283
   *                  from an empty one.
284
   * @param aMediaAttr [ignored if !aIsSourceTag] The media attr for this
285
   *                   source.  Should be a void string to differentiate no
286
   *                   media attribute from an empty one.
287
   * @param aResult A reference to store the resulting URL spec in if we
288
   *                selected a source.  This value is not guaranteed to parse to
289
   *                a valid URL, merely the URL that the tag would attempt to
290
   *                resolve and load (which may be the empty string).  This
291
   *                parameter is not modified if return value is false.
292
   * @return True if we were able to select a final source, false if further
293
   *         sources would be considered.  It follows that this always returns
294
   *         true if !aIsSourceTag.
295
   *
296
   * Note that the return value may be true with an empty string as the result,
297
   * which implies that the parameters provided describe a tag that would select
298
   * no source.  This is distinct from a return of false which implies that
299
   * further <source> or <img> tags would be considered.
300
   */
301
  static bool
302
    SelectSourceForTagWithAttrs(nsIDocument *aDocument,
303
                                bool aIsSourceTag,
304
                                const nsAString& aSrcAttr,
305
                                const nsAString& aSrcsetAttr,
306
                                const nsAString& aSizesAttr,
307
                                const nsAString& aTypeAttr,
308
                                const nsAString& aMediaAttr,
309
                                nsAString& aResult);
310
311
  /**
312
   * If this image's src pointers to an SVG document, flush the SVG document's
313
   * use counters to telemetry.  Only used for testing purposes.
314
   */
315
  void FlushUseCounters();
316
317
protected:
318
  virtual ~HTMLImageElement();
319
320
  // Queues a task to run LoadSelectedImage pending stable state.
321
  //
322
  // Pending Bug 1076583 this is only used by the responsive image
323
  // algorithm (InResponsiveMode()) -- synchronous actions when just
324
  // using img.src will bypass this, and update source and kick off
325
  // image load synchronously.
326
  void QueueImageLoadTask(bool aAlwaysLoad);
327
328
  // True if we have a srcset attribute or a <picture> parent, regardless of if
329
  // any valid responsive sources were parsed from either.
330
  bool HaveSrcsetOrInPicture();
331
332
  // True if we are using the newer image loading algorithm. This will be the
333
  // only mode after Bug 1076583
334
  bool InResponsiveMode();
335
336
  // True if the given URL equals the last URL that was loaded by this element.
337
  bool SelectedSourceMatchesLast(nsIURI* aSelectedSource);
338
339
  // Resolve and load the current mResponsiveSelector (responsive mode) or src
340
  // attr image.
341
  nsresult LoadSelectedImage(bool aForce, bool aNotify, bool aAlwaysLoad);
342
343
  // True if this string represents a type we would support on <source type>
344
  static bool SupportedPictureSourceType(const nsAString& aType);
345
346
  // Update/create/destroy mResponsiveSelector
347
  void PictureSourceSrcsetChanged(nsIContent *aSourceNode,
348
                                  const nsAString& aNewValue, bool aNotify);
349
  void PictureSourceSizesChanged(nsIContent *aSourceNode,
350
                                 const nsAString& aNewValue, bool aNotify);
351
  // As we re-run the source selection on these mutations regardless,
352
  // we don't actually care which changed or to what
353
  void PictureSourceMediaOrTypeChanged(nsIContent *aSourceNode, bool aNotify);
354
355
  void PictureSourceAdded(nsIContent *aSourceNode);
356
  // This should be called prior to the unbind, such that nextsibling works
357
  void PictureSourceRemoved(nsIContent *aSourceNode);
358
359
  // Re-evaluates all source nodes (picture <source>,<img>) and finds
360
  // the best source set for mResponsiveSelector. If a better source
361
  // is found, creates a new selector and feeds the source to it. If
362
  // the current ResponsiveSelector is not changed, runs
363
  // SelectImage(true) to re-evaluate its candidates.
364
  //
365
  // Because keeping the existing selector is the common case (and we
366
  // often do no-op reselections), this does not re-parse values for
367
  // the existing mResponsiveSelector, meaning you need to update its
368
  // parameters as appropriate before calling (or null it out to force
369
  // recreation)
370
  //
371
  // Returns true if the source has changed, and false otherwise.
372
  bool UpdateResponsiveSource();
373
374
  // Given a <source> node that is a previous sibling *or* ourselves, try to
375
  // create a ResponsiveSelector.
376
377
  // If the node's srcset/sizes make for an invalid selector, returns
378
  // false. This does not guarantee the resulting selector matches an image,
379
  // only that it is valid.
380
  bool TryCreateResponsiveSelector(Element* aSourceElement);
381
382
  MOZ_CAN_RUN_SCRIPT CSSIntPoint GetXY();
383
  virtual JSObject* WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto) override;
384
  void UpdateFormOwner();
385
386
  virtual nsresult BeforeSetAttr(int32_t aNameSpaceID, nsAtom* aName,
387
                                 const nsAttrValueOrString* aValue,
388
                                 bool aNotify) override;
389
390
  virtual nsresult AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
391
                                const nsAttrValue* aValue,
392
                                const nsAttrValue* aOldValue,
393
                                nsIPrincipal* aMaybeScriptedPrincipal,
394
                                bool aNotify) override;
395
  virtual nsresult OnAttrSetButNotChanged(int32_t aNamespaceID, nsAtom* aName,
396
                                          const nsAttrValueOrString& aValue,
397
                                          bool aNotify) override;
398
399
  // Override for nsImageLoadingContent.
400
0
  nsIContent* AsContent() override { return this; }
401
402
  // This is a weak reference that this element and the HTMLFormElement
403
  // cooperate in maintaining.
404
  HTMLFormElement* mForm;
405
406
  // Created when we're tracking responsive image state
407
  RefPtr<ResponsiveImageSelector> mResponsiveSelector;
408
409
private:
410
  bool SourceElementMatches(Element* aSourceElement);
411
412
  static void MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
413
                                    MappedDeclarations&);
414
  /**
415
   * This function is called by AfterSetAttr and OnAttrSetButNotChanged.
416
   * It will not be called if the value is being unset.
417
   *
418
   * @param aNamespaceID the namespace of the attr being set
419
   * @param aName the localname of the attribute being set
420
   * @param aValue the value it's being set to represented as either a string or
421
   *        a parsed nsAttrValue.
422
   * @param aOldValue the value previously set. Will be null if no value was
423
   *        previously set. This value should only be used when
424
   *        aValueMaybeChanged is true; when aValueMaybeChanged is false,
425
   *        aOldValue should be considered unreliable.
426
   * @param aValueMaybeChanged will be false when this function is called from
427
   *        OnAttrSetButNotChanged to indicate that the value was not changed.
428
   * @param aNotify Whether we plan to notify document observers.
429
   */
430
  void AfterMaybeChangeAttr(int32_t aNamespaceID, nsAtom* aName,
431
                            const nsAttrValueOrString& aValue,
432
                            const nsAttrValue* aOldValue,
433
                            nsIPrincipal* aMaybeScriptedPrincipal,
434
                            bool aValueMaybeChanged, bool aNotify);
435
436
  bool mInDocResponsiveContent;
437
  RefPtr<ImageLoadTask> mPendingImageLoadTask;
438
  nsCOMPtr<nsIPrincipal> mSrcTriggeringPrincipal;
439
  nsCOMPtr<nsIPrincipal> mSrcsetTriggeringPrincipal;
440
441
  // Last URL that was attempted to load by this element.
442
  nsCOMPtr<nsIURI> mLastSelectedSource;
443
  // Last pixel density that was selected.
444
  double mCurrentDensity;
445
};
446
447
} // namespace dom
448
} // namespace mozilla
449
450
#endif /* mozilla_dom_HTMLImageElement_h */