Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / base / nsImageLoadingContent.h
blobec6f55c9febadfde824459b20a748ac00d38f639
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/. */
7 /*
8 * A base class which implements nsIImageLoadingContent and can be
9 * subclassed by various content nodes that want to provide image
10 * loading functionality (eg <img>, <object>, etc).
13 #ifndef nsImageLoadingContent_h__
14 #define nsImageLoadingContent_h__
16 #include "imgINotificationObserver.h"
17 #include "mozilla/CORSMode.h"
18 #include "mozilla/TimeStamp.h"
19 #include "nsCOMPtr.h"
20 #include "nsIContentPolicy.h"
21 #include "nsIImageLoadingContent.h"
22 #include "nsIRequest.h"
23 #include "mozilla/dom/BindingDeclarations.h"
24 #include "mozilla/dom/Promise.h"
25 #include "mozilla/dom/RustTypes.h"
26 #include "nsAttrValue.h"
27 #include "Units.h"
29 class nsINode;
30 class nsIURI;
31 class nsPresContext;
32 class nsIContent;
33 class imgRequestProxy;
35 namespace mozilla {
36 class AsyncEventDispatcher;
37 class ErrorResult;
39 namespace dom {
40 struct BindContext;
41 class Document;
42 class Element;
43 enum class FetchPriority : uint8_t;
44 } // namespace dom
45 } // namespace mozilla
47 #ifdef LoadImage
48 // Undefine LoadImage to prevent naming conflict with Windows.
49 # undef LoadImage
50 #endif
52 class nsImageLoadingContent : public nsIImageLoadingContent {
53 protected:
54 template <typename T>
55 using Maybe = mozilla::Maybe<T>;
56 using Nothing = mozilla::Nothing;
57 using OnNonvisible = mozilla::OnNonvisible;
58 using Visibility = mozilla::Visibility;
60 /* METHODS */
61 public:
62 nsImageLoadingContent();
63 virtual ~nsImageLoadingContent();
65 NS_DECL_IMGINOTIFICATIONOBSERVER
66 NS_DECL_NSIIMAGELOADINGCONTENT
68 // Web IDL binding methods.
69 // Note that the XPCOM SetLoadingEnabled method is OK for Web IDL bindings
70 // to use as well, since it does not throw when called via the Web IDL
71 // bindings.
73 bool LoadingEnabled() const { return mLoadingEnabled; }
74 void AddObserver(imgINotificationObserver* aObserver);
75 void RemoveObserver(imgINotificationObserver* aObserver);
76 already_AddRefed<imgIRequest> GetRequest(int32_t aRequestType,
77 mozilla::ErrorResult& aError);
78 int32_t GetRequestType(imgIRequest* aRequest, mozilla::ErrorResult& aError);
79 already_AddRefed<nsIURI> GetCurrentURI();
80 already_AddRefed<nsIURI> GetCurrentRequestFinalURI();
81 void ForceReload(bool aNotify, mozilla::ErrorResult& aError);
83 mozilla::dom::Element* FindImageMap();
84 static mozilla::dom::Element* FindImageMap(mozilla::dom::Element*);
86 /**
87 * Toggle whether or not to synchronously decode an image on draw.
89 void SetSyncDecodingHint(bool aHint);
91 /**
92 * Notify us that the document state has changed. Called by nsDocument so that
93 * we may reject any promises which require the document to be active.
95 void NotifyOwnerDocumentActivityChanged();
97 // Trigger text recognition for the current image request.
98 already_AddRefed<mozilla::dom::Promise> RecognizeCurrentImageText(
99 mozilla::ErrorResult&);
101 protected:
102 enum ImageLoadType {
103 // Most normal image loads
104 eImageLoadType_Normal,
105 // From a <img srcset> or <picture> context. Affects type given to content
106 // policy.
107 eImageLoadType_Imageset
111 * LoadImage is called by subclasses when the appropriate
112 * attributes (eg 'src' for <img> tags) change. The string passed
113 * in is the new uri string; this consolidates the code for getting
114 * the charset, constructing URI objects, and any other incidentals
115 * into this superclass.
117 * @param aNewURI the URI spec to be loaded (may be a relative URI)
118 * @param aForce If true, make sure to load the URI. If false, only
119 * load if the URI is different from the currently loaded URI.
120 * @param aNotify If true, nsIDocumentObserver state change notifications
121 * will be sent as needed.
122 * @param aImageLoadType The ImageLoadType for this request
123 * @param aTriggeringPrincipal Optional parameter specifying the triggering
124 * principal to use for the image load
126 nsresult LoadImage(const nsAString& aNewURI, bool aForce, bool aNotify,
127 ImageLoadType aImageLoadType,
128 nsIPrincipal* aTriggeringPrincipal = nullptr);
131 * ImageState is called by subclasses that are computing their content state.
132 * The return value will have the ElementState::BROKEN bit set as needed.
134 * Note that this state assumes that this node is "trying" to be an
135 * image (so for example complete lack of attempt to load an image will lead
136 * to ElementState::BROKEN being set). Subclasses that are not "trying" to
137 * be an image (eg an HTML <input> of type other than "image") should just
138 * not call this method when computing their intrinsic state.
140 mozilla::dom::ElementState ImageState() const;
143 * LoadImage is called by subclasses when the appropriate
144 * attributes (eg 'src' for <img> tags) change. If callers have an
145 * URI object already available, they should use this method.
147 * @param aNewURI the URI to be loaded
148 * @param aForce If true, make sure to load the URI. If false, only
149 * load if the URI is different from the currently loaded URI.
150 * @param aNotify If true, nsIDocumentObserver state change notifications
151 * will be sent as needed.
152 * @param aImageLoadType The ImageLoadType for this request
153 * @param aDocument Optional parameter giving the document this node is in.
154 * This is purely a performance optimization.
155 * @param aLoadFlags Optional parameter specifying load flags to use for
156 * the image load
157 * @param aTriggeringPrincipal Optional parameter specifying the triggering
158 * principal to use for the image load
160 nsresult LoadImage(nsIURI* aNewURI, bool aForce, bool aNotify,
161 ImageLoadType aImageLoadType, nsLoadFlags aLoadFlags,
162 mozilla::dom::Document* aDocument = nullptr,
163 nsIPrincipal* aTriggeringPrincipal = nullptr);
165 nsresult LoadImage(nsIURI* aNewURI, bool aForce, bool aNotify,
166 ImageLoadType aImageLoadType,
167 nsIPrincipal* aTriggeringPrincipal) {
168 return LoadImage(aNewURI, aForce, aNotify, aImageLoadType, LoadFlags(),
169 nullptr, aTriggeringPrincipal);
173 * helpers to get the document for this content (from the nodeinfo
174 * and such). Not named GetOwnerDoc/GetCurrentDoc to prevent ambiguous
175 * method names in subclasses
177 * @return the document we belong to
179 mozilla::dom::Document* GetOurOwnerDoc();
180 mozilla::dom::Document* GetOurCurrentDoc();
183 * Helper function to get the frame associated with this content. Not named
184 * GetPrimaryFrame to prevent ambiguous method names in subclasses.
186 * @return The frame we own, or nullptr if it doesn't exist, or isn't
187 * associated with any of our requests.
189 nsIFrame* GetOurPrimaryImageFrame();
192 * Helper function to get the PresContext associated with this content's
193 * frame. Not named GetPresContext to prevent ambiguous method names in
194 * subclasses.
196 * @return The nsPresContext associated with our frame, or nullptr if either
197 * the frame doesn't exist, or the frame's prescontext doesn't exist.
199 nsPresContext* GetFramePresContext();
202 * CancelImageRequests is called by subclasses when they want to
203 * cancel all image requests (for example when the subclass is
204 * somehow not an image anymore).
206 void CancelImageRequests(bool aNotify);
209 * Derived classes of nsImageLoadingContent MUST call Destroy from their
210 * destructor, or earlier. It does things that cannot be done in
211 * ~nsImageLoadingContent because they rely on being able to QueryInterface to
212 * other derived classes, which cannot happen once the derived class
213 * destructor has started calling the base class destructors.
215 void Destroy();
218 * Returns the CORS mode that will be used for all future image loads. The
219 * default implementation returns CORS_NONE unconditionally.
221 virtual mozilla::CORSMode GetCORSMode();
223 // Subclasses are *required* to call BindToTree/UnbindFromTree.
224 void BindToTree(mozilla::dom::BindContext&, nsINode& aParent);
225 void UnbindFromTree();
227 void OnLoadComplete(imgIRequest* aRequest, uint32_t aImageStatus);
228 void OnUnlockedDraw();
229 void OnImageIsAnimated(imgIRequest* aRequest);
231 // The nsContentPolicyType we would use for this ImageLoadType
232 static nsContentPolicyType PolicyTypeForLoad(ImageLoadType aImageLoadType);
234 void AsyncEventRunning(mozilla::AsyncEventDispatcher* aEvent);
236 // Get ourselves as an nsIContent*. Not const because some of the callers
237 // want a non-const nsIContent.
238 virtual nsIContent* AsContent() = 0;
240 virtual mozilla::dom::FetchPriority GetFetchPriorityForImage() const;
243 * Get width and height of the current request, using given image request if
244 * attributes are unset.
246 MOZ_CAN_RUN_SCRIPT mozilla::CSSIntSize GetWidthHeightForImage();
249 * Create a promise and queue a microtask which will ensure the current
250 * request (after any pending loads are applied) has requested a full decode.
251 * The promise is fulfilled once the request has a fully decoded surface that
252 * is available for drawing, or an error condition occurrs (e.g. broken image,
253 * current request is updated, etc).
255 * https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-decode
257 already_AddRefed<mozilla::dom::Promise> QueueDecodeAsync(
258 mozilla::ErrorResult& aRv);
260 enum class ImageDecodingType : uint8_t {
261 Auto,
262 Async,
263 Sync,
266 static const nsAttrValue::EnumTable kDecodingTable[];
267 static const nsAttrValue::EnumTable* kDecodingTableDefault;
269 private:
271 * Enqueue and/or fulfill a promise created by QueueDecodeAsync.
273 void DecodeAsync(RefPtr<mozilla::dom::Promise>&& aPromise,
274 uint32_t aRequestGeneration);
277 * Attempt to resolve all queued promises based on the state of the current
278 * request. If the current request does not yet have all of the encoded data,
279 * or the decoding has not yet completed, it will return without changing the
280 * promise states.
282 void MaybeResolveDecodePromises();
285 * Reject all queued promises with the given status.
287 void RejectDecodePromises(nsresult aStatus);
290 * Age the generation counter if we have a new current request with a
291 * different URI. If the generation counter is aged, then all queued promises
292 * will also be rejected.
294 void MaybeAgeRequestGeneration(nsIURI* aNewURI);
297 * Deregister as an observer for the owner document's activity notifications
298 * if we have no outstanding decode promises.
300 void MaybeDeregisterActivityObserver();
303 * Struct used to manage the native image observers.
305 struct ImageObserver {
306 explicit ImageObserver(imgINotificationObserver* aObserver);
307 ~ImageObserver();
309 nsCOMPtr<imgINotificationObserver> mObserver;
310 ImageObserver* mNext;
314 * Struct used to manage the scripted/XPCOM image observers.
316 class ScriptedImageObserver final {
317 public:
318 NS_INLINE_DECL_REFCOUNTING(ScriptedImageObserver)
320 ScriptedImageObserver(imgINotificationObserver* aObserver,
321 RefPtr<imgRequestProxy>&& aCurrentRequest,
322 RefPtr<imgRequestProxy>&& aPendingRequest);
323 bool CancelRequests();
325 nsCOMPtr<imgINotificationObserver> mObserver;
326 RefPtr<imgRequestProxy> mCurrentRequest;
327 RefPtr<imgRequestProxy> mPendingRequest;
329 private:
330 ~ScriptedImageObserver();
334 * Method to fire an event once we know what's going on with the image load.
336 * @param aEventType "load", or "error" depending on how things went
337 * @param aIsCancelable true if event is cancelable.
339 nsresult FireEvent(const nsAString& aEventType, bool aIsCancelable = false);
342 * Method to cancel and null-out pending event if they exist.
344 void CancelPendingEvent();
346 RefPtr<mozilla::AsyncEventDispatcher> mPendingEvent;
348 protected:
350 * UpdateImageState recomputes the current state of this image loading
351 * content and updates what ImageState() returns accordingly. It will also
352 * fire a ContentStatesChanged() notification as needed if aNotify is true.
354 void UpdateImageState(bool aNotify);
357 * Method to create an nsIURI object from the given string (will
358 * handle getting the right charset, base, etc). You MUST pass in a
359 * non-null document to this function.
361 * @param aSpec the string spec (from an HTML attribute, eg)
362 * @param aDocument the document we belong to
363 * @return the URI we want to be loading
365 nsresult StringToURI(const nsAString& aSpec,
366 mozilla::dom::Document* aDocument, nsIURI** aURI);
369 * Prepare and returns a reference to the "next request". If there's already
370 * a _usable_ current request (one with SIZE_AVAILABLE), this request is
371 * "pending" until it becomes usable. Otherwise, this becomes the current
372 * request.
374 * @param aImageLoadType The ImageLoadType for this request
375 * @param aNewURI The uri that we're going to load
377 RefPtr<imgRequestProxy>& PrepareNextRequest(ImageLoadType, nsIURI* aNewURI);
380 * Returns a COMPtr reference to the current/pending image requests, cleaning
381 * up and canceling anything that was there before. Note that if you just want
382 * to get rid of one of the requests, you should call
383 * Clear*Request(NS_BINDING_ABORTED) instead.
385 * @param aImageLoadType The ImageLoadType for this request
386 * @param aNewURI The uri that we're going to load
388 RefPtr<imgRequestProxy>& PrepareCurrentRequest(ImageLoadType,
389 nsIURI* aNewURI);
390 RefPtr<imgRequestProxy>& PreparePendingRequest(ImageLoadType);
393 * Switch our pending request to be our current request.
394 * mPendingRequest must be non-null!
396 void MakePendingRequestCurrent();
399 * Cancels and nulls-out the "current" and "pending" requests if they exist.
401 * @param aNonvisibleAction An action to take if the image is no longer
402 * visible as a result; see |UntrackImage|.
404 void ClearCurrentRequest(
405 nsresult aReason,
406 const Maybe<OnNonvisible>& aNonvisibleAction = Nothing());
407 void ClearPendingRequest(
408 nsresult aReason,
409 const Maybe<OnNonvisible>& aNonvisibleAction = Nothing());
412 * Static helper method to tell us if we have the size of a request. The
413 * image may be null.
415 static bool HaveSize(imgIRequest* aImage);
418 * Adds/Removes a given imgIRequest from our document's tracker.
420 * No-op if aImage is null.
422 * @param aFrame If called from FrameCreated the frame passed to FrameCreated.
423 * This is our frame, but at the time of the FrameCreated call
424 * our primary frame pointer hasn't been set yet, so this is
425 * only way to get our frame.
427 * @param aNonvisibleAction A requested action if the frame has become
428 * nonvisible. If Nothing(), no action is
429 * requested. If DISCARD_IMAGES is specified, the
430 * frame is requested to ask any images it's
431 * associated with to discard their surfaces if
432 * possible.
434 void TrackImage(imgIRequest* aImage, nsIFrame* aFrame = nullptr);
435 void UntrackImage(imgIRequest* aImage,
436 const Maybe<OnNonvisible>& aNonvisibleAction = Nothing());
438 nsLoadFlags LoadFlags();
440 /* MEMBERS */
441 RefPtr<imgRequestProxy> mCurrentRequest;
442 RefPtr<imgRequestProxy> mPendingRequest;
443 uint8_t mCurrentRequestFlags = 0;
444 uint8_t mPendingRequestFlags = 0;
446 enum {
447 // Set if the request is currently tracked with the document.
448 REQUEST_IS_TRACKED = 1 << 0,
449 // Set if this is an imageset request, such as from <img srcset> or
450 // <picture>
451 REQUEST_IS_IMAGESET = 1 << 1,
454 // If the image was blocked or if there was an error loading, it's nice to
455 // still keep track of what the URI was despite not having an imgIRequest.
456 // We only maintain this in those situations (in the common case, this is
457 // always null).
458 nsCOMPtr<nsIURI> mCurrentURI;
460 private:
462 * Clones the given "current" or "pending" request for each scripted observer.
464 void CloneScriptedRequests(imgRequestProxy* aRequest);
467 * Cancels and nulls-out the "current" or "pending" requests if they exist
468 * for each scripted observer.
470 void ClearScriptedRequests(int32_t aRequestType, nsresult aReason);
473 * Moves the "pending" request into the "current" request for each scripted
474 * observer. If there is an existing "current" request, it will cancel it
475 * first.
477 void MakePendingScriptedRequestsCurrent();
480 * Depending on the configured decoding hint, and/or how recently we updated
481 * the image request, force or stop the frame from decoding the image
482 * synchronously when it is drawn.
483 * @param aPrepareNextRequest True if this is when updating the image request.
484 * @param aFrame If called from FrameCreated the frame passed to FrameCreated.
485 * This is our frame, but at the time of the FrameCreated call
486 * our primary frame pointer hasn't been set yet, so this is
487 * only way to get our frame.
489 void MaybeForceSyncDecoding(bool aPrepareNextRequest,
490 nsIFrame* aFrame = nullptr);
493 * Typically we will have only one observer (our frame in the screen
494 * prescontext), so we want to only make space for one and to
495 * heap-allocate anything past that (saves memory and malloc churn
496 * in the common case). The storage is a linked list, we just
497 * happen to actually hold the first observer instead of a pointer
498 * to it.
500 ImageObserver mObserverList;
503 * Typically we will have no scripted observers, as this is only used by
504 * chrome, legacy extensions, and some mochitests. An empty array reserves
505 * minimal memory.
507 nsTArray<RefPtr<ScriptedImageObserver>> mScriptedObservers;
510 * Promises created by QueueDecodeAsync that are still waiting to be
511 * fulfilled by the image being fully decoded.
513 nsTArray<RefPtr<mozilla::dom::Promise>> mDecodePromises;
515 mozilla::TimeStamp mMostRecentRequestChange;
518 * Total number of outstanding decode promises, including those stored in
519 * mDecodePromises and those embedded in runnables waiting to be enqueued.
520 * This is used to determine whether we need to register as an observer for
521 * document activity notifications.
523 size_t mOutstandingDecodePromises;
526 * An incrementing counter representing the current request generation;
527 * Each time mCurrentRequest is modified with a different URI, this will
528 * be incremented. Each QueueDecodeAsync call will cache the generation
529 * of the current request so that when it is processed, it knows if it
530 * should have rejected because the request changed.
532 uint32_t mRequestGeneration;
534 protected:
535 bool mLoadingEnabled : 1;
537 * Flag to indicate whether the channel should be mark as urgent-start.
538 * It should be set in *Element and passed to nsContentUtils::LoadImage.
539 * True if we want to set nsIClassOfService::UrgentStart to the channel to
540 * get the response ASAP for better user responsiveness.
542 bool mUseUrgentStartForChannel : 1;
544 // Represents the image is deferred loading until this element gets visible.
545 bool mLazyLoading : 1;
547 // Whether we have a pending load task scheduled (HTMLImageElement only).
548 bool mHasPendingLoadTask : 1;
550 // If true, force frames to synchronously decode images on draw.
551 bool mSyncDecodingHint : 1;
553 // Whether we're in the doc responsive content set (HTMLImageElement only).
554 bool mInDocResponsiveContent : 1;
556 private:
557 // Flags to indicate whether each of the current and pending requests are
558 // registered with the refresh driver.
559 bool mCurrentRequestRegistered;
560 bool mPendingRequestRegistered;
563 #endif // nsImageLoadingContent_h__