Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / image / imgRequestProxy.h
blobcb4853540d9f47998cf515e0f298accdf45dd702
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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 #ifndef mozilla_image_imgRequestProxy_h
8 #define mozilla_image_imgRequestProxy_h
10 #include "imgIRequest.h"
12 #include "nsIPrincipal.h"
13 #include "nsISupportsPriority.h"
14 #include "nsITimedChannel.h"
15 #include "nsCOMPtr.h"
16 #include "nsThreadUtils.h"
17 #include "mozilla/PreloaderBase.h"
18 #include "mozilla/TimeStamp.h"
19 #include "mozilla/UniquePtr.h"
20 #include "mozilla/gfx/Rect.h"
22 #include "IProgressObserver.h"
24 #define NS_IMGREQUESTPROXY_CID \
25 { /* 20557898-1dd2-11b2-8f65-9c462ee2bc95 */ \
26 0x20557898, 0x1dd2, 0x11b2, { \
27 0x8f, 0x65, 0x9c, 0x46, 0x2e, 0xe2, 0xbc, 0x95 \
28 } \
31 class imgCacheValidator;
32 class imgINotificationObserver;
33 class imgRequest;
34 class imgStatusNotifyRunnable;
35 class ProxyBehaviour;
37 namespace mozilla {
38 namespace image {
39 class Image;
40 class ProgressTracker;
41 } // namespace image
42 } // namespace mozilla
44 class imgRequestProxy : public mozilla::PreloaderBase,
45 public imgIRequest,
46 public mozilla::image::IProgressObserver,
47 public nsISupportsPriority,
48 public nsITimedChannel {
49 protected:
50 virtual ~imgRequestProxy();
52 public:
53 typedef mozilla::dom::Document Document;
54 typedef mozilla::image::Image Image;
55 typedef mozilla::image::ProgressTracker ProgressTracker;
57 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IMGREQUESTPROXY_CID)
58 MOZ_DECLARE_REFCOUNTED_TYPENAME(imgRequestProxy)
59 NS_DECL_ISUPPORTS
60 NS_DECL_IMGIREQUEST
61 NS_DECL_NSIREQUEST
62 NS_DECL_NSISUPPORTSPRIORITY
63 // nsITimedChannel declared below
65 imgRequestProxy();
67 // Callers to Init or ChangeOwner are required to call NotifyListener after
68 // (although not immediately after) doing so.
69 nsresult Init(imgRequest* aOwner, nsILoadGroup* aLoadGroup, nsIURI* aURI,
70 imgINotificationObserver* aObserver);
72 nsresult ChangeOwner(imgRequest* aNewOwner); // this will change mOwner.
73 // Do not call this if the
74 // previous owner has already
75 // sent notifications out!
77 // Add the request to the load group, if any. This should only be called once
78 // during initialization.
79 void AddToLoadGroup();
81 inline bool HasObserver() const { return mListener != nullptr; }
83 // Asynchronously notify this proxy's listener of the current state of the
84 // image, and, if we have an imgRequest mOwner, any status changes that
85 // happen between the time this function is called and the time the
86 // notification is scheduled.
87 void NotifyListener();
89 // Synchronously notify this proxy's listener of the current state of the
90 // image. Only use this function if you are currently servicing an
91 // asynchronously-called function.
92 void SyncNotifyListener();
94 // imgINotificationObserver methods:
95 virtual void Notify(int32_t aType,
96 const mozilla::gfx::IntRect* aRect = nullptr) override;
97 virtual void OnLoadComplete(bool aLastPart) override;
99 // Other, internal-only methods:
100 virtual void SetHasImage() override;
102 // Whether we want notifications from ProgressTracker to be deferred until
103 // an event it has scheduled has been fired and/or validation is complete.
104 virtual bool NotificationsDeferred() const override {
105 return IsValidating() || mPendingNotify;
107 virtual void MarkPendingNotify() override { mPendingNotify = true; }
108 virtual void ClearPendingNotify() override { mPendingNotify = false; }
109 bool IsValidating() const { return mValidating; }
110 void MarkValidating();
111 void ClearValidating();
113 // Flags this image load as not cancelable temporarily. This is needed so that
114 // stylesheets can be shared across documents properly, see bug 1800979.
115 void SetCancelable(bool);
117 // Removes all animation consumers that were created with
118 // IncrementAnimationConsumers. This is necessary since we need
119 // to do it before the proxy itself is destroyed. See
120 // imgRequest::RemoveProxy
121 void ClearAnimationConsumers();
123 nsresult SyncClone(imgINotificationObserver* aObserver,
124 Document* aLoadingDocument, imgRequestProxy** aClone);
125 nsresult Clone(imgINotificationObserver* aObserver,
126 Document* aLoadingDocument, imgRequestProxy** aClone);
127 already_AddRefed<imgRequestProxy> GetStaticRequest(
128 Document* aLoadingDocument);
130 imgRequest* GetOwner() const;
132 // PreloaderBase
133 // We are using the default image loader prioritization for preloads.
134 virtual void PrioritizeAsPreload() override {}
136 protected:
137 friend class mozilla::image::ProgressTracker;
138 friend class imgStatusNotifyRunnable;
140 class imgCancelRunnable;
141 friend class imgCancelRunnable;
143 class imgCancelRunnable : public mozilla::Runnable {
144 public:
145 imgCancelRunnable(imgRequestProxy* owner, nsresult status)
146 : Runnable("imgCancelRunnable"), mOwner(owner), mStatus(status) {}
148 NS_IMETHOD Run() override {
149 mOwner->DoCancel(mStatus);
150 return NS_OK;
153 private:
154 RefPtr<imgRequestProxy> mOwner;
155 nsresult mStatus;
158 /* Remove from and forget the load group. */
159 void RemoveFromLoadGroup();
161 /* Remove from the load group and re-add as a background request. */
162 void MoveToBackgroundInLoadGroup();
164 /* Finish up canceling ourselves */
165 void DoCancel(nsresult status);
167 /* Do the proper refcount management to null out mListener */
168 void NullOutListener();
170 // Return the ProgressTracker associated with mOwner and/or mImage. It may
171 // live either on mOwner or mImage, depending on whether
172 // (a) we have an mOwner at all
173 // (b) whether mOwner has instantiated its image yet
174 already_AddRefed<ProgressTracker> GetProgressTracker() const;
176 nsITimedChannel* TimedChannel();
178 already_AddRefed<Image> GetImage() const;
179 bool HasImage() const;
180 imgCacheValidator* GetValidator() const;
182 nsresult PerformClone(imgINotificationObserver* aObserver,
183 Document* aLoadingDocument, bool aSyncNotify,
184 imgRequestProxy** aClone);
186 virtual imgRequestProxy* NewClonedProxy();
188 public:
189 NS_FORWARD_SAFE_NSITIMEDCHANNEL(TimedChannel())
191 protected:
192 mozilla::UniquePtr<ProxyBehaviour> mBehaviour;
194 private:
195 friend class imgCacheValidator;
197 void AddToOwner();
198 void RemoveFromOwner(nsresult aStatus);
200 nsresult DispatchWithTargetIfAvailable(already_AddRefed<nsIRunnable> aEvent);
202 // The URI of our request.
203 nsCOMPtr<nsIURI> mURI;
205 // mListener is only promised to be a weak ref (see imgILoader.idl),
206 // but we actually keep a strong ref to it until we've seen our
207 // first OnStopRequest.
208 imgINotificationObserver* MOZ_UNSAFE_REF(
209 "Observers must call Cancel() or "
210 "CancelAndForgetObserver() before "
211 "they are destroyed") mListener;
213 nsCOMPtr<nsILoadGroup> mLoadGroup;
215 nsLoadFlags mLoadFlags;
216 uint32_t mLockCount;
217 uint32_t mAnimationConsumers;
218 bool mCancelable : 1;
219 bool mCanceled : 1;
220 bool mIsInLoadGroup : 1;
221 bool mForceDispatchLoadGroup : 1;
222 bool mListenerIsStrongRef : 1;
223 bool mDecodeRequested : 1;
225 // Whether we want to defer our notifications by the non-virtual Observer
226 // interfaces as image loads proceed.
227 bool mPendingNotify : 1;
228 bool mValidating : 1;
229 bool mHadListener : 1;
232 NS_DEFINE_STATIC_IID_ACCESSOR(imgRequestProxy, NS_IMGREQUESTPROXY_CID)
234 // Used for static image proxies for which no requests are available, so
235 // certain behaviours must be overridden to compensate.
236 class imgRequestProxyStatic : public imgRequestProxy {
237 public:
238 imgRequestProxyStatic(Image* aImage, nsIPrincipal* aImagePrincipal,
239 nsIPrincipal* aTriggeringPrincipal,
240 bool hadCrossOriginRedirects);
242 NS_IMETHOD GetImagePrincipal(nsIPrincipal** aPrincipal) override;
243 NS_IMETHOD GetTriggeringPrincipal(nsIPrincipal** aPrincipal) override;
245 NS_IMETHOD GetHadCrossOriginRedirects(
246 bool* aHadCrossOriginRedirects) override;
248 protected:
249 imgRequestProxy* NewClonedProxy() override;
251 // Our principal. We have to cache it, rather than accessing the underlying
252 // request on-demand, because static proxies don't have an underlying request.
253 const nsCOMPtr<nsIPrincipal> mImagePrincipal;
254 const nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
255 const bool mHadCrossOriginRedirects;
258 #endif // mozilla_image_imgRequestProxy_h