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"
11 #include "nsISecurityInfoProvider.h"
13 #include "nsILoadGroup.h"
14 #include "nsISupportsPriority.h"
15 #include "nsITimedChannel.h"
17 #include "nsThreadUtils.h"
18 #include "mozilla/TimeStamp.h"
19 #include "mozilla/UniquePtr.h"
20 #include "mozilla/gfx/Rect.h"
22 #include "imgRequest.h"
23 #include "IProgressObserver.h"
25 #define NS_IMGREQUESTPROXY_CID \
26 { /* 20557898-1dd2-11b2-8f65-9c462ee2bc95 */ \
30 {0x8f, 0x65, 0x9c, 0x46, 0x2e, 0xe2, 0xbc, 0x95} \
33 class imgCacheValidator
;
34 class imgINotificationObserver
;
35 class imgStatusNotifyRunnable
;
46 class ProgressTracker
;
48 } // namespace mozilla
50 class imgRequestProxy
: public imgIRequest
,
51 public mozilla::image::IProgressObserver
,
52 public nsISupportsPriority
,
53 public nsISecurityInfoProvider
,
54 public nsITimedChannel
57 virtual ~imgRequestProxy();
60 typedef mozilla::image::Image Image
;
61 typedef mozilla::image::ImageURL ImageURL
;
62 typedef mozilla::image::ProgressTracker ProgressTracker
;
64 MOZ_DECLARE_REFCOUNTED_TYPENAME(imgRequestProxy
)
68 NS_DECL_NSISUPPORTSPRIORITY
69 NS_DECL_NSISECURITYINFOPROVIDER
70 // nsITimedChannel declared below
74 // Callers to Init or ChangeOwner are required to call NotifyListener after
75 // (although not immediately after) doing so.
76 nsresult
Init(imgRequest
* aOwner
,
77 nsILoadGroup
* aLoadGroup
,
78 nsIDocument
* aLoadingDocument
,
80 imgINotificationObserver
* aObserver
);
82 nsresult
ChangeOwner(imgRequest
* aNewOwner
); // this will change mOwner.
83 // Do not call this if the
84 // previous owner has already
85 // sent notifications out!
87 // Add the request to the load group, if any. This should only be called once
88 // during initialization.
89 void AddToLoadGroup();
91 inline bool HasObserver() const {
92 return mListener
!= nullptr;
95 // Asynchronously notify this proxy's listener of the current state of the
96 // image, and, if we have an imgRequest mOwner, any status changes that
97 // happen between the time this function is called and the time the
98 // notification is scheduled.
99 void NotifyListener();
101 // Synchronously notify this proxy's listener of the current state of the
102 // image. Only use this function if you are currently servicing an
103 // asynchronously-called function.
104 void SyncNotifyListener();
106 // imgINotificationObserver methods:
107 virtual void Notify(int32_t aType
,
108 const mozilla::gfx::IntRect
* aRect
= nullptr) override
;
109 virtual void OnLoadComplete(bool aLastPart
) override
;
111 // Other, internal-only methods:
112 virtual void SetHasImage() override
;
114 // Whether we want notifications from ProgressTracker to be deferred until
115 // an event it has scheduled has been fired and/or validation is complete.
116 virtual bool NotificationsDeferred() const override
118 return IsValidating() || mPendingNotify
;
120 virtual void MarkPendingNotify() override
122 mPendingNotify
= true;
124 virtual void ClearPendingNotify() override
126 mPendingNotify
= false;
128 bool IsValidating() const
132 void MarkValidating();
133 void ClearValidating();
135 bool IsOnEventTarget() const;
136 already_AddRefed
<nsIEventTarget
> GetEventTarget() const override
;
138 // Removes all animation consumers that were created with
139 // IncrementAnimationConsumers. This is necessary since we need
140 // to do it before the proxy itself is destroyed. See
141 // imgRequest::RemoveProxy
142 void ClearAnimationConsumers();
144 nsresult
SyncClone(imgINotificationObserver
* aObserver
,
145 nsIDocument
* aLoadingDocument
,
146 imgRequestProxy
** aClone
);
147 nsresult
Clone(imgINotificationObserver
* aObserver
,
148 nsIDocument
* aLoadingDocument
,
149 imgRequestProxy
** aClone
);
150 nsresult
GetStaticRequest(nsIDocument
* aLoadingDocument
,
151 imgRequestProxy
** aReturn
);
153 nsresult
GetURI(ImageURL
** aURI
);
156 friend class mozilla::image::ProgressTracker
;
157 friend class imgStatusNotifyRunnable
;
159 class imgCancelRunnable
;
160 friend class imgCancelRunnable
;
162 class imgCancelRunnable
: public mozilla::Runnable
165 imgCancelRunnable(imgRequestProxy
* owner
, nsresult status
)
166 : Runnable("imgCancelRunnable"), mOwner(owner
), mStatus(status
)
169 NS_IMETHOD
Run() override
{
170 mOwner
->DoCancel(mStatus
);
175 RefPtr
<imgRequestProxy
> mOwner
;
179 /* Remove from and forget the load group. */
180 void RemoveFromLoadGroup();
182 /* Remove from the load group and readd as a background request. */
183 void MoveToBackgroundInLoadGroup();
185 /* Finish up canceling ourselves */
186 void DoCancel(nsresult status
);
188 /* Do the proper refcount management to null out mListener */
189 void NullOutListener();
191 // Return the ProgressTracker associated with mOwner and/or mImage. It may
192 // live either on mOwner or mImage, depending on whether
193 // (a) we have an mOwner at all
194 // (b) whether mOwner has instantiated its image yet
195 already_AddRefed
<ProgressTracker
> GetProgressTracker() const;
197 nsITimedChannel
* TimedChannel()
202 return GetOwner()->GetTimedChannel();
205 already_AddRefed
<Image
> GetImage() const;
206 bool HasImage() const;
207 imgRequest
* GetOwner() const;
208 imgCacheValidator
* GetValidator() const;
210 nsresult
PerformClone(imgINotificationObserver
* aObserver
,
211 nsIDocument
* aLoadingDocument
,
213 imgRequestProxy
** aClone
);
215 virtual imgRequestProxy
* NewClonedProxy();
218 NS_FORWARD_SAFE_NSITIMEDCHANNEL(TimedChannel())
221 mozilla::UniquePtr
<ProxyBehaviour
> mBehaviour
;
224 friend class imgCacheValidator
;
226 void AddToOwner(nsIDocument
* aLoadingDocument
);
227 void RemoveFromOwner(nsresult aStatus
);
229 nsresult
DispatchWithTargetIfAvailable(already_AddRefed
<nsIRunnable
> aEvent
);
230 void DispatchWithTarget(already_AddRefed
<nsIRunnable
> aEvent
);
232 // The URI of our request.
233 RefPtr
<ImageURL
> mURI
;
235 // mListener is only promised to be a weak ref (see imgILoader.idl),
236 // but we actually keep a strong ref to it until we've seen our
237 // first OnStopRequest.
238 imgINotificationObserver
* MOZ_UNSAFE_REF("Observers must call Cancel() or "
239 "CancelAndForgetObserver() before "
240 "they are destroyed") mListener
;
242 nsCOMPtr
<nsILoadGroup
> mLoadGroup
;
243 RefPtr
<mozilla::dom::TabGroup
> mTabGroup
;
244 nsCOMPtr
<nsIEventTarget
> mEventTarget
;
246 nsLoadFlags mLoadFlags
;
248 uint32_t mAnimationConsumers
;
250 bool mIsInLoadGroup
: 1;
251 bool mForceDispatchLoadGroup
: 1;
252 bool mListenerIsStrongRef
: 1;
253 bool mDecodeRequested
: 1;
255 // Whether we want to defer our notifications by the non-virtual Observer
256 // interfaces as image loads proceed.
257 bool mPendingNotify
: 1;
258 bool mValidating
: 1;
259 bool mHadListener
: 1;
260 bool mHadDispatch
: 1;
263 // Used for static image proxies for which no requests are available, so
264 // certain behaviours must be overridden to compensate.
265 class imgRequestProxyStatic
: public imgRequestProxy
269 imgRequestProxyStatic(Image
* aImage
, nsIPrincipal
* aPrincipal
);
271 NS_IMETHOD
GetImagePrincipal(nsIPrincipal
** aPrincipal
) override
;
274 imgRequestProxy
* NewClonedProxy() override
;
276 // Our principal. We have to cache it, rather than accessing the underlying
277 // request on-demand, because static proxies don't have an underlying request.
278 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
281 #endif // mozilla_image_imgRequestProxy_h