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 imgRequestProxy_h__
8 #define imgRequestProxy_h__
10 #include "mozilla/WeakPtr.h"
11 #include "imgIRequest.h"
12 #include "nsISecurityInfoProvider.h"
14 #include "nsILoadGroup.h"
15 #include "nsISupportsPriority.h"
16 #include "nsITimedChannel.h"
18 #include "nsAutoPtr.h"
19 #include "nsThreadUtils.h"
20 #include "mozilla/TimeStamp.h"
22 #include "imgRequest.h"
24 #define NS_IMGREQUESTPROXY_CID \
25 { /* 20557898-1dd2-11b2-8f65-9c462ee2bc95 */ \
29 {0x8f, 0x65, 0x9c, 0x46, 0x2e, 0xe2, 0xbc, 0x95} \
32 class imgINotificationObserver
;
33 class imgRequestNotifyRunnable
;
34 class imgStatusNotifyRunnable
;
43 } // namespace mozilla
45 class imgRequestProxy
: public imgIRequest
,
46 public nsISupportsPriority
,
47 public nsISecurityInfoProvider
,
48 public nsITimedChannel
,
49 public mozilla::SupportsWeakPtr
<imgRequestProxy
>
52 virtual ~imgRequestProxy();
55 MOZ_DECLARE_REFCOUNTED_TYPENAME(imgRequestProxy
)
56 typedef mozilla::image::ImageURL ImageURL
;
60 NS_DECL_NSISUPPORTSPRIORITY
61 NS_DECL_NSISECURITYINFOPROVIDER
62 // nsITimedChannel declared below
66 // Callers to Init or ChangeOwner are required to call NotifyListener after
67 // (although not immediately after) doing so.
68 nsresult
Init(imgRequest
* aOwner
,
69 nsILoadGroup
*aLoadGroup
,
71 imgINotificationObserver
*aObserver
);
73 nsresult
ChangeOwner(imgRequest
*aNewOwner
); // this will change mOwner. Do not call this if the previous
74 // owner has already sent notifications out!
76 void AddToLoadGroup();
77 void RemoveFromLoadGroup(bool releaseLoadGroup
);
79 inline bool HasObserver() const {
80 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 // Whether we want notifications from imgStatusTracker to be deferred until
95 // an event it has scheduled has been fired.
96 bool NotificationsDeferred() const
98 return mDeferNotifications
;
100 void SetNotificationsDeferred(bool aDeferNotifications
)
102 mDeferNotifications
= aDeferNotifications
;
105 // XXXbholley - This eventually gets folded into the new notification API.
108 // Removes all animation consumers that were created with
109 // IncrementAnimationConsumers. This is necessary since we need
110 // to do it before the proxy itself is destroyed. See
111 // imgRequest::RemoveProxy
112 void ClearAnimationConsumers();
114 virtual nsresult
Clone(imgINotificationObserver
* aObserver
, imgRequestProxy
** aClone
);
115 nsresult
GetStaticRequest(imgRequestProxy
** aReturn
);
117 nsresult
GetURI(ImageURL
**aURI
);
120 friend class imgStatusTracker
;
121 friend class imgStatusNotifyRunnable
;
122 friend class imgRequestNotifyRunnable
;
124 class imgCancelRunnable
;
125 friend class imgCancelRunnable
;
127 class imgCancelRunnable
: public nsRunnable
130 imgCancelRunnable(imgRequestProxy
* owner
, nsresult status
)
131 : mOwner(owner
), mStatus(status
)
135 mOwner
->DoCancel(mStatus
);
140 nsRefPtr
<imgRequestProxy
> mOwner
;
144 // The following notification functions are protected to ensure that (friend
145 // class) imgStatusTracker is the only class allowed to send us
148 /* non-virtual imgDecoderObserver methods */
149 void OnStartDecode ();
150 void OnStartContainer ();
151 void OnFrameUpdate (const nsIntRect
* aRect
);
153 void OnStopDecode ();
155 void OnUnlockedDraw ();
156 void OnImageIsAnimated ();
158 /* non-virtual sort-of-nsIRequestObserver methods */
159 void OnStartRequest();
160 void OnStopRequest(bool aLastPart
);
162 /* non-virtual imgIOnloadBlocker methods */
164 void UnblockOnload();
166 /* Finish up canceling ourselves */
167 void DoCancel(nsresult status
);
169 /* Do the proper refcount management to null out mListener */
170 void NullOutListener();
172 void DoRemoveFromLoadGroup() {
173 RemoveFromLoadGroup(true);
176 // Return the imgStatusTracker associated with mOwner and/or mImage. It may
177 // live either on mOwner or mImage, depending on whether
178 // (a) we have an mOwner at all
179 // (b) whether mOwner has instantiated its image yet
180 already_AddRefed
<imgStatusTracker
> GetStatusTracker() const;
182 nsITimedChannel
* TimedChannel()
186 return GetOwner()->mTimedChannel
;
189 already_AddRefed
<mozilla::image::Image
> GetImage() const;
190 bool HasImage() const;
191 imgRequest
* GetOwner() const;
193 nsresult
PerformClone(imgINotificationObserver
* aObserver
,
194 imgRequestProxy
* (aAllocFn
)(imgRequestProxy
*),
195 imgRequestProxy
** aClone
);
198 NS_FORWARD_SAFE_NSITIMEDCHANNEL(TimedChannel())
201 nsAutoPtr
<ProxyBehaviour
> mBehaviour
;
204 friend class imgCacheValidator
;
205 friend imgRequestProxy
* NewStaticProxy(imgRequestProxy
* aThis
);
207 // The URI of our request.
208 nsRefPtr
<ImageURL
> mURI
;
210 // mListener is only promised to be a weak ref (see imgILoader.idl),
211 // but we actually keep a strong ref to it until we've seen our
212 // first OnStopRequest.
213 imgINotificationObserver
* mListener
;
214 nsCOMPtr
<nsILoadGroup
> mLoadGroup
;
216 nsLoadFlags mLoadFlags
;
218 uint32_t mAnimationConsumers
;
221 bool mListenerIsStrongRef
;
222 bool mDecodeRequested
;
224 // Whether we want to defer our notifications by the non-virtual Observer
225 // interfaces as image loads proceed.
226 bool mDeferNotifications
;
228 // We only want to send OnStartContainer once for each proxy, but we might
229 // get multiple OnStartContainer calls.
230 bool mSentStartContainer
;
233 // Used for static image proxies for which no requests are available, so
234 // certain behaviours must be overridden to compensate.
235 class imgRequestProxyStatic
: public imgRequestProxy
239 imgRequestProxyStatic(mozilla::image::Image
* aImage
,
240 nsIPrincipal
* aPrincipal
);
242 NS_IMETHOD
GetImagePrincipal(nsIPrincipal
** aPrincipal
) MOZ_OVERRIDE
;
244 using imgRequestProxy::Clone
;
246 virtual nsresult
Clone(imgINotificationObserver
* aObserver
,
247 imgRequestProxy
** aClone
) MOZ_OVERRIDE
;
250 friend imgRequestProxy
* NewStaticProxy(imgRequestProxy
*);
252 // Our principal. We have to cache it, rather than accessing the underlying
253 // request on-demand, because static proxies don't have an underlying request.
254 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
257 #endif // imgRequestProxy_h__