Bug 1837620 - Part 1.5: Rename IC flag to 'mayHaveFoldedStub' to make it clear that...
[gecko.git] / image / imgRequestProxy.h
blob950c78341ca4e890386783946e51ee980e1691e3
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,
70 Document* aLoadingDocument, nsIURI* aURI,
71 imgINotificationObserver* aObserver);
73 nsresult ChangeOwner(imgRequest* aNewOwner); // this will change mOwner.
74 // Do not call this if the
75 // previous owner has already
76 // sent notifications out!
78 // Add the request to the load group, if any. This should only be called once
79 // during initialization.
80 void AddToLoadGroup();
82 inline bool HasObserver() const { return mListener != nullptr; }
84 // Asynchronously notify this proxy's listener of the current state of the
85 // image, and, if we have an imgRequest mOwner, any status changes that
86 // happen between the time this function is called and the time the
87 // notification is scheduled.
88 void NotifyListener();
90 // Synchronously notify this proxy's listener of the current state of the
91 // image. Only use this function if you are currently servicing an
92 // asynchronously-called function.
93 void SyncNotifyListener();
95 // imgINotificationObserver methods:
96 virtual void Notify(int32_t aType,
97 const mozilla::gfx::IntRect* aRect = nullptr) override;
98 virtual void OnLoadComplete(bool aLastPart) override;
100 // Other, internal-only methods:
101 virtual void SetHasImage() override;
103 // Whether we want notifications from ProgressTracker to be deferred until
104 // an event it has scheduled has been fired and/or validation is complete.
105 virtual bool NotificationsDeferred() const override {
106 return IsValidating() || mPendingNotify;
108 virtual void MarkPendingNotify() override { mPendingNotify = true; }
109 virtual void ClearPendingNotify() override { mPendingNotify = false; }
110 bool IsValidating() const { return mValidating; }
111 void MarkValidating();
112 void ClearValidating();
114 // Flags this image load as not cancelable temporarily. This is needed so that
115 // stylesheets can be shared across documents properly, see bug 1800979.
116 void SetCancelable(bool);
118 already_AddRefed<nsIEventTarget> GetEventTarget() const override;
120 // Removes all animation consumers that were created with
121 // IncrementAnimationConsumers. This is necessary since we need
122 // to do it before the proxy itself is destroyed. See
123 // imgRequest::RemoveProxy
124 void ClearAnimationConsumers();
126 nsresult SyncClone(imgINotificationObserver* aObserver,
127 Document* aLoadingDocument, imgRequestProxy** aClone);
128 nsresult Clone(imgINotificationObserver* aObserver,
129 Document* aLoadingDocument, imgRequestProxy** aClone);
130 already_AddRefed<imgRequestProxy> GetStaticRequest(
131 Document* aLoadingDocument);
133 imgRequest* GetOwner() const;
135 // PreloaderBase
136 // We are using the default image loader prioritization for preloads.
137 virtual void PrioritizeAsPreload() override {}
139 protected:
140 friend class mozilla::image::ProgressTracker;
141 friend class imgStatusNotifyRunnable;
143 class imgCancelRunnable;
144 friend class imgCancelRunnable;
146 class imgCancelRunnable : public mozilla::Runnable {
147 public:
148 imgCancelRunnable(imgRequestProxy* owner, nsresult status)
149 : Runnable("imgCancelRunnable"), mOwner(owner), mStatus(status) {}
151 NS_IMETHOD Run() override {
152 mOwner->DoCancel(mStatus);
153 return NS_OK;
156 private:
157 RefPtr<imgRequestProxy> mOwner;
158 nsresult mStatus;
161 /* Remove from and forget the load group. */
162 void RemoveFromLoadGroup();
164 /* Remove from the load group and re-add as a background request. */
165 void MoveToBackgroundInLoadGroup();
167 /* Finish up canceling ourselves */
168 void DoCancel(nsresult status);
170 /* Do the proper refcount management to null out mListener */
171 void NullOutListener();
173 // Return the ProgressTracker associated with mOwner and/or mImage. It may
174 // live either on mOwner or mImage, depending on whether
175 // (a) we have an mOwner at all
176 // (b) whether mOwner has instantiated its image yet
177 already_AddRefed<ProgressTracker> GetProgressTracker() const;
179 nsITimedChannel* TimedChannel();
181 already_AddRefed<Image> GetImage() const;
182 bool HasImage() const;
183 imgCacheValidator* GetValidator() const;
185 nsresult PerformClone(imgINotificationObserver* aObserver,
186 Document* aLoadingDocument, bool aSyncNotify,
187 imgRequestProxy** aClone);
189 virtual imgRequestProxy* NewClonedProxy();
191 public:
192 NS_FORWARD_SAFE_NSITIMEDCHANNEL(TimedChannel())
194 protected:
195 mozilla::UniquePtr<ProxyBehaviour> mBehaviour;
197 private:
198 friend class imgCacheValidator;
200 void AddToOwner(Document* aLoadingDocument);
201 void RemoveFromOwner(nsresult aStatus);
203 nsresult DispatchWithTargetIfAvailable(already_AddRefed<nsIRunnable> aEvent);
205 // The URI of our request.
206 nsCOMPtr<nsIURI> mURI;
208 // mListener is only promised to be a weak ref (see imgILoader.idl),
209 // but we actually keep a strong ref to it until we've seen our
210 // first OnStopRequest.
211 imgINotificationObserver* MOZ_UNSAFE_REF(
212 "Observers must call Cancel() or "
213 "CancelAndForgetObserver() before "
214 "they are destroyed") mListener;
216 nsCOMPtr<nsILoadGroup> mLoadGroup;
217 nsCOMPtr<nsIEventTarget> mEventTarget;
219 nsLoadFlags mLoadFlags;
220 uint32_t mLockCount;
221 uint32_t mAnimationConsumers;
222 bool mCancelable : 1;
223 bool mCanceled : 1;
224 bool mIsInLoadGroup : 1;
225 bool mForceDispatchLoadGroup : 1;
226 bool mListenerIsStrongRef : 1;
227 bool mDecodeRequested : 1;
229 // Whether we want to defer our notifications by the non-virtual Observer
230 // interfaces as image loads proceed.
231 bool mPendingNotify : 1;
232 bool mValidating : 1;
233 bool mHadListener : 1;
234 bool mHadDispatch : 1;
237 NS_DEFINE_STATIC_IID_ACCESSOR(imgRequestProxy, NS_IMGREQUESTPROXY_CID)
239 // Used for static image proxies for which no requests are available, so
240 // certain behaviours must be overridden to compensate.
241 class imgRequestProxyStatic : public imgRequestProxy {
242 public:
243 imgRequestProxyStatic(Image* aImage, nsIPrincipal* aImagePrincipal,
244 nsIPrincipal* aTriggeringPrincipal,
245 bool hadCrossOriginRedirects);
247 NS_IMETHOD GetImagePrincipal(nsIPrincipal** aPrincipal) override;
248 NS_IMETHOD GetTriggeringPrincipal(nsIPrincipal** aPrincipal) override;
250 NS_IMETHOD GetHadCrossOriginRedirects(
251 bool* aHadCrossOriginRedirects) override;
253 protected:
254 imgRequestProxy* NewClonedProxy() override;
256 // Our principal. We have to cache it, rather than accessing the underlying
257 // request on-demand, because static proxies don't have an underlying request.
258 const nsCOMPtr<nsIPrincipal> mImagePrincipal;
259 const nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
260 const bool mHadCrossOriginRedirects;
263 #endif // mozilla_image_imgRequestProxy_h