Bumping manifests a=b2g-bump
[gecko.git] / image / src / imgRequestProxy.h
blob1cc347e2f06d7483d58a2849557f5f28ad8eff19
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"
17 #include "nsCOMPtr.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 */ \
26 0x20557898, \
27 0x1dd2, \
28 0x11b2, \
29 {0x8f, 0x65, 0x9c, 0x46, 0x2e, 0xe2, 0xbc, 0x95} \
32 class imgINotificationObserver;
33 class imgRequestNotifyRunnable;
34 class imgStatusNotifyRunnable;
35 struct nsIntRect;
36 class ProxyBehaviour;
38 namespace mozilla {
39 namespace image {
40 class Image;
41 class ImageURL;
42 } // namespace image
43 } // namespace mozilla
45 class imgRequestProxy : public imgIRequest,
46 public nsISupportsPriority,
47 public nsISecurityInfoProvider,
48 public nsITimedChannel,
49 public mozilla::SupportsWeakPtr<imgRequestProxy>
51 protected:
52 virtual ~imgRequestProxy();
54 public:
55 MOZ_DECLARE_REFCOUNTED_TYPENAME(imgRequestProxy)
56 typedef mozilla::image::ImageURL ImageURL;
57 NS_DECL_ISUPPORTS
58 NS_DECL_IMGIREQUEST
59 NS_DECL_NSIREQUEST
60 NS_DECL_NSISUPPORTSPRIORITY
61 NS_DECL_NSISECURITYINFOPROVIDER
62 // nsITimedChannel declared below
64 imgRequestProxy();
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,
70 ImageURL* aURI,
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.
106 void SetHasImage();
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);
119 protected:
120 friend class imgStatusTracker;
121 friend class imgStatusNotifyRunnable;
122 friend class imgRequestNotifyRunnable;
124 class imgCancelRunnable;
125 friend class imgCancelRunnable;
127 class imgCancelRunnable : public nsRunnable
129 public:
130 imgCancelRunnable(imgRequestProxy* owner, nsresult status)
131 : mOwner(owner), mStatus(status)
134 NS_IMETHOD Run() {
135 mOwner->DoCancel(mStatus);
136 return NS_OK;
139 private:
140 nsRefPtr<imgRequestProxy> mOwner;
141 nsresult mStatus;
144 // The following notification functions are protected to ensure that (friend
145 // class) imgStatusTracker is the only class allowed to send us
146 // notifications.
148 /* non-virtual imgDecoderObserver methods */
149 void OnStartDecode ();
150 void OnStartContainer ();
151 void OnFrameUpdate (const nsIntRect * aRect);
152 void OnStopFrame ();
153 void OnStopDecode ();
154 void OnDiscard ();
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 */
163 void BlockOnload();
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()
184 if (!GetOwner())
185 return nullptr;
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);
197 public:
198 NS_FORWARD_SAFE_NSITIMEDCHANNEL(TimedChannel())
200 protected:
201 nsAutoPtr<ProxyBehaviour> mBehaviour;
203 private:
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;
217 uint32_t mLockCount;
218 uint32_t mAnimationConsumers;
219 bool mCanceled;
220 bool mIsInLoadGroup;
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
238 public:
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;
249 protected:
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__