Coverage Report

Created: 2018-09-25 14:53

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