Bug 1526591 - Remove devtools.inspector.shapesHighlighter.enabled pref. r=rcaliman
[gecko.git] / image / imgRequestProxy.h
blobd355409bfcaa34a1d9dd2804d814c175f62c0263
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 "nsILoadGroup.h"
13 #include "nsISupportsPriority.h"
14 #include "nsITimedChannel.h"
15 #include "nsCOMPtr.h"
16 #include "nsThreadUtils.h"
17 #include "mozilla/TimeStamp.h"
18 #include "mozilla/UniquePtr.h"
19 #include "mozilla/gfx/Rect.h"
21 #include "imgRequest.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 imgStatusNotifyRunnable;
34 class ProxyBehaviour;
36 namespace mozilla {
37 namespace dom {
38 class TabGroup;
41 namespace image {
42 class Image;
43 class ProgressTracker;
44 } // namespace image
45 } // namespace mozilla
47 class imgRequestProxy : public imgIRequest,
48 public mozilla::image::IProgressObserver,
49 public nsISupportsPriority,
50 public nsITimedChannel {
51 protected:
52 virtual ~imgRequestProxy();
54 public:
55 typedef mozilla::dom::Document Document;
56 typedef mozilla::image::Image Image;
57 typedef mozilla::image::ProgressTracker ProgressTracker;
59 MOZ_DECLARE_REFCOUNTED_TYPENAME(imgRequestProxy)
60 NS_DECL_ISUPPORTS
61 NS_DECL_IMGIREQUEST
62 NS_DECL_NSIREQUEST
63 NS_DECL_NSISUPPORTSPRIORITY
64 // nsITimedChannel declared below
66 imgRequestProxy();
68 // Callers to Init or ChangeOwner are required to call NotifyListener after
69 // (although not immediately after) doing so.
70 nsresult Init(imgRequest* aOwner, nsILoadGroup* aLoadGroup,
71 Document* aLoadingDocument, nsIURI* aURI,
72 imgINotificationObserver* aObserver);
74 nsresult ChangeOwner(imgRequest* aNewOwner); // this will change mOwner.
75 // Do not call this if the
76 // previous owner has already
77 // sent notifications out!
79 // Add the request to the load group, if any. This should only be called once
80 // during initialization.
81 void AddToLoadGroup();
83 inline bool HasObserver() const { return mListener != nullptr; }
85 // Asynchronously notify this proxy's listener of the current state of the
86 // image, and, if we have an imgRequest mOwner, any status changes that
87 // happen between the time this function is called and the time the
88 // notification is scheduled.
89 void NotifyListener();
91 // Synchronously notify this proxy's listener of the current state of the
92 // image. Only use this function if you are currently servicing an
93 // asynchronously-called function.
94 void SyncNotifyListener();
96 // imgINotificationObserver methods:
97 virtual void Notify(int32_t aType,
98 const mozilla::gfx::IntRect* aRect = nullptr) override;
99 virtual void OnLoadComplete(bool aLastPart) override;
101 // Other, internal-only methods:
102 virtual void SetHasImage() override;
104 // Whether we want notifications from ProgressTracker to be deferred until
105 // an event it has scheduled has been fired and/or validation is complete.
106 virtual bool NotificationsDeferred() const override {
107 return IsValidating() || mPendingNotify;
109 virtual void MarkPendingNotify() override { mPendingNotify = true; }
110 virtual void ClearPendingNotify() override { mPendingNotify = false; }
111 bool IsValidating() const { return mValidating; }
112 void MarkValidating();
113 void ClearValidating();
115 bool IsOnEventTarget() const;
116 already_AddRefed<nsIEventTarget> GetEventTarget() const override;
118 // Removes all animation consumers that were created with
119 // IncrementAnimationConsumers. This is necessary since we need
120 // to do it before the proxy itself is destroyed. See
121 // imgRequest::RemoveProxy
122 void ClearAnimationConsumers();
124 nsresult SyncClone(imgINotificationObserver* aObserver,
125 Document* aLoadingDocument, imgRequestProxy** aClone);
126 nsresult Clone(imgINotificationObserver* aObserver,
127 Document* aLoadingDocument, imgRequestProxy** aClone);
128 nsresult GetStaticRequest(Document* aLoadingDocument,
129 imgRequestProxy** aReturn);
131 protected:
132 friend class mozilla::image::ProgressTracker;
133 friend class imgStatusNotifyRunnable;
135 class imgCancelRunnable;
136 friend class imgCancelRunnable;
138 class imgCancelRunnable : public mozilla::Runnable {
139 public:
140 imgCancelRunnable(imgRequestProxy* owner, nsresult status)
141 : Runnable("imgCancelRunnable"), mOwner(owner), mStatus(status) {}
143 NS_IMETHOD Run() override {
144 mOwner->DoCancel(mStatus);
145 return NS_OK;
148 private:
149 RefPtr<imgRequestProxy> mOwner;
150 nsresult mStatus;
153 /* Remove from and forget the load group. */
154 void RemoveFromLoadGroup();
156 /* Remove from the load group and readd as a background request. */
157 void MoveToBackgroundInLoadGroup();
159 /* Finish up canceling ourselves */
160 void DoCancel(nsresult status);
162 /* Do the proper refcount management to null out mListener */
163 void NullOutListener();
165 // Return the ProgressTracker associated with mOwner and/or mImage. It may
166 // live either on mOwner or mImage, depending on whether
167 // (a) we have an mOwner at all
168 // (b) whether mOwner has instantiated its image yet
169 already_AddRefed<ProgressTracker> GetProgressTracker() const;
171 nsITimedChannel* TimedChannel() {
172 if (!GetOwner()) {
173 return nullptr;
175 return GetOwner()->GetTimedChannel();
178 already_AddRefed<Image> GetImage() const;
179 bool HasImage() const;
180 imgRequest* GetOwner() const;
181 imgCacheValidator* GetValidator() const;
183 nsresult PerformClone(imgINotificationObserver* aObserver,
184 Document* aLoadingDocument, bool aSyncNotify,
185 imgRequestProxy** aClone);
187 virtual imgRequestProxy* NewClonedProxy();
189 public:
190 NS_FORWARD_SAFE_NSITIMEDCHANNEL(TimedChannel())
192 protected:
193 mozilla::UniquePtr<ProxyBehaviour> mBehaviour;
195 private:
196 friend class imgCacheValidator;
198 void AddToOwner(Document* aLoadingDocument);
199 void RemoveFromOwner(nsresult aStatus);
201 nsresult DispatchWithTargetIfAvailable(already_AddRefed<nsIRunnable> aEvent);
202 void DispatchWithTarget(already_AddRefed<nsIRunnable> aEvent);
204 // The URI of our request.
205 nsCOMPtr<nsIURI> mURI;
207 // mListener is only promised to be a weak ref (see imgILoader.idl),
208 // but we actually keep a strong ref to it until we've seen our
209 // first OnStopRequest.
210 imgINotificationObserver* MOZ_UNSAFE_REF(
211 "Observers must call Cancel() or "
212 "CancelAndForgetObserver() before "
213 "they are destroyed") mListener;
215 nsCOMPtr<nsILoadGroup> mLoadGroup;
216 RefPtr<mozilla::dom::TabGroup> mTabGroup;
217 nsCOMPtr<nsIEventTarget> mEventTarget;
219 nsLoadFlags mLoadFlags;
220 uint32_t mLockCount;
221 uint32_t mAnimationConsumers;
222 bool mCanceled : 1;
223 bool mIsInLoadGroup : 1;
224 bool mForceDispatchLoadGroup : 1;
225 bool mListenerIsStrongRef : 1;
226 bool mDecodeRequested : 1;
228 // Whether we want to defer our notifications by the non-virtual Observer
229 // interfaces as image loads proceed.
230 bool mPendingNotify : 1;
231 bool mValidating : 1;
232 bool mHadListener : 1;
233 bool mHadDispatch : 1;
236 // Used for static image proxies for which no requests are available, so
237 // certain behaviours must be overridden to compensate.
238 class imgRequestProxyStatic : public imgRequestProxy {
239 public:
240 imgRequestProxyStatic(Image* aImage, nsIPrincipal* aPrincipal);
242 NS_IMETHOD GetImagePrincipal(nsIPrincipal** aPrincipal) override;
244 protected:
245 imgRequestProxy* NewClonedProxy() override;
247 // Our principal. We have to cache it, rather than accessing the underlying
248 // request on-demand, because static proxies don't have an underlying request.
249 nsCOMPtr<nsIPrincipal> mPrincipal;
252 #endif // mozilla_image_imgRequestProxy_h