1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
6 
7 #ifndef mozilla_image_imgRequestProxy_h
8 #define mozilla_image_imgRequestProxy_h
9 
10 #include "imgIRequest.h"
11 
12 #include "nsILoadGroup.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"
21 
22 #include "imgRequest.h"
23 #include "IProgressObserver.h"
24 
25 #define NS_IMGREQUESTPROXY_CID                       \
26   { /* 20557898-1dd2-11b2-8f65-9c462ee2bc95 */       \
27     0x20557898, 0x1dd2, 0x11b2, {                    \
28       0x8f, 0x65, 0x9c, 0x46, 0x2e, 0xe2, 0xbc, 0x95 \
29     }                                                \
30   }
31 
32 class imgCacheValidator;
33 class imgINotificationObserver;
34 class imgStatusNotifyRunnable;
35 class ProxyBehaviour;
36 
37 namespace mozilla {
38 namespace image {
39 class Image;
40 class ProgressTracker;
41 }  // namespace image
42 }  // namespace mozilla
43 
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();
51 
52  public:
53   typedef mozilla::dom::Document Document;
54   typedef mozilla::image::Image Image;
55   typedef mozilla::image::ProgressTracker ProgressTracker;
56 
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
64 
65   imgRequestProxy();
66 
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);
72 
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!
77 
78   // Add the request to the load group, if any. This should only be called once
79   // during initialization.
80   void AddToLoadGroup();
81 
HasObserver()82   inline bool HasObserver() const { return mListener != nullptr; }
83 
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();
89 
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();
94 
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;
99 
100   // Other, internal-only methods:
101   virtual void SetHasImage() override;
102 
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.
NotificationsDeferred()105   virtual bool NotificationsDeferred() const override {
106     return IsValidating() || mPendingNotify;
107   }
MarkPendingNotify()108   virtual void MarkPendingNotify() override { mPendingNotify = true; }
ClearPendingNotify()109   virtual void ClearPendingNotify() override { mPendingNotify = false; }
IsValidating()110   bool IsValidating() const { return mValidating; }
111   void MarkValidating();
112   void ClearValidating();
113 
114   bool IsOnEventTarget() const;
115   already_AddRefed<nsIEventTarget> GetEventTarget() const override;
116 
117   // Removes all animation consumers that were created with
118   // IncrementAnimationConsumers. This is necessary since we need
119   // to do it before the proxy itself is destroyed. See
120   // imgRequest::RemoveProxy
121   void ClearAnimationConsumers();
122 
123   nsresult SyncClone(imgINotificationObserver* aObserver,
124                      Document* aLoadingDocument, imgRequestProxy** aClone);
125   nsresult Clone(imgINotificationObserver* aObserver,
126                  Document* aLoadingDocument, imgRequestProxy** aClone);
127   nsresult GetStaticRequest(Document* aLoadingDocument,
128                             imgRequestProxy** aReturn);
129 
130   imgRequest* GetOwner() const;
131 
132   // PreloaderBase
133   virtual void PrioritizeAsPreload() override;
134 
135  protected:
136   friend class mozilla::image::ProgressTracker;
137   friend class imgStatusNotifyRunnable;
138 
139   class imgCancelRunnable;
140   friend class imgCancelRunnable;
141 
142   class imgCancelRunnable : public mozilla::Runnable {
143    public:
imgCancelRunnable(imgRequestProxy * owner,nsresult status)144     imgCancelRunnable(imgRequestProxy* owner, nsresult status)
145         : Runnable("imgCancelRunnable"), mOwner(owner), mStatus(status) {}
146 
Run()147     NS_IMETHOD Run() override {
148       mOwner->DoCancel(mStatus);
149       return NS_OK;
150     }
151 
152    private:
153     RefPtr<imgRequestProxy> mOwner;
154     nsresult mStatus;
155   };
156 
157   /* Remove from and forget the load group. */
158   void RemoveFromLoadGroup();
159 
160   /* Remove from the load group and re-add as a background request. */
161   void MoveToBackgroundInLoadGroup();
162 
163   /* Finish up canceling ourselves */
164   void DoCancel(nsresult status);
165 
166   /* Do the proper refcount management to null out mListener */
167   void NullOutListener();
168 
169   // Return the ProgressTracker associated with mOwner and/or mImage. It may
170   // live either on mOwner or mImage, depending on whether
171   //   (a) we have an mOwner at all
172   //   (b) whether mOwner has instantiated its image yet
173   already_AddRefed<ProgressTracker> GetProgressTracker() const;
174 
TimedChannel()175   nsITimedChannel* TimedChannel() {
176     if (!GetOwner()) {
177       return nullptr;
178     }
179     return GetOwner()->GetTimedChannel();
180   }
181 
182   already_AddRefed<Image> GetImage() const;
183   bool HasImage() const;
184   imgCacheValidator* GetValidator() const;
185 
186   nsresult PerformClone(imgINotificationObserver* aObserver,
187                         Document* aLoadingDocument, bool aSyncNotify,
188                         imgRequestProxy** aClone);
189 
190   virtual imgRequestProxy* NewClonedProxy();
191 
192  public:
193   NS_FORWARD_SAFE_NSITIMEDCHANNEL(TimedChannel())
194 
195  protected:
196   mozilla::UniquePtr<ProxyBehaviour> mBehaviour;
197 
198  private:
199   friend class imgCacheValidator;
200 
201   void AddToOwner(Document* aLoadingDocument);
202   void RemoveFromOwner(nsresult aStatus);
203 
204   nsresult DispatchWithTargetIfAvailable(already_AddRefed<nsIRunnable> aEvent);
205   void DispatchWithTarget(already_AddRefed<nsIRunnable> aEvent);
206 
207   // The URI of our request.
208   nsCOMPtr<nsIURI> mURI;
209 
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* MOZ_UNSAFE_REF(
214       "Observers must call Cancel() or "
215       "CancelAndForgetObserver() before "
216       "they are destroyed") mListener;
217 
218   nsCOMPtr<nsILoadGroup> mLoadGroup;
219   nsCOMPtr<nsIEventTarget> mEventTarget;
220 
221   nsLoadFlags mLoadFlags;
222   uint32_t mLockCount;
223   uint32_t mAnimationConsumers;
224   bool mCanceled : 1;
225   bool mIsInLoadGroup : 1;
226   bool mForceDispatchLoadGroup : 1;
227   bool mListenerIsStrongRef : 1;
228   bool mDecodeRequested : 1;
229 
230   // Whether we want to defer our notifications by the non-virtual Observer
231   // interfaces as image loads proceed.
232   bool mPendingNotify : 1;
233   bool mValidating : 1;
234   bool mHadListener : 1;
235   bool mHadDispatch : 1;
236 };
237 
NS_DEFINE_STATIC_IID_ACCESSOR(imgRequestProxy,NS_IMGREQUESTPROXY_CID)238 NS_DEFINE_STATIC_IID_ACCESSOR(imgRequestProxy, NS_IMGREQUESTPROXY_CID)
239 
240 // Used for static image proxies for which no requests are available, so
241 // certain behaviours must be overridden to compensate.
242 class imgRequestProxyStatic : public imgRequestProxy {
243  public:
244   imgRequestProxyStatic(Image* aImage, nsIPrincipal* aPrincipal,
245                         bool hadCrossOriginRedirects);
246 
247   NS_IMETHOD GetImagePrincipal(nsIPrincipal** aPrincipal) override;
248 
249   NS_IMETHOD GetHadCrossOriginRedirects(
250       bool* aHadCrossOriginRedirects) override;
251 
252  protected:
253   imgRequestProxy* NewClonedProxy() override;
254 
255   // Our principal. We have to cache it, rather than accessing the underlying
256   // request on-demand, because static proxies don't have an underlying request.
257   nsCOMPtr<nsIPrincipal> mPrincipal;
258   const bool mHadCrossOriginRedirects;
259 };
260 
261 #endif  // mozilla_image_imgRequestProxy_h
262