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_imgRequest_h
8 #define mozilla_image_imgRequest_h
10 #include "nsIChannelEventSink.h"
11 #include "nsIInterfaceRequestor.h"
12 #include "nsIStreamListener.h"
13 #include "nsIThreadRetargetableStreamListener.h"
14 #include "nsIPrincipal.h"
17 #include "nsProxyRelease.h"
20 #include "nsIAsyncVerifyRedirectCallback.h"
21 #include "mozilla/Mutex.h"
22 #include "ImageCacheKey.h"
24 class imgCacheValidator
;
26 class imgRequestProxy
;
30 class nsITimedChannel
;
32 class nsIReferrerInfo
;
35 enum CORSMode
: uint8_t;
38 class ProgressTracker
;
40 } // namespace mozilla
44 class imgRequest final
: public nsIStreamListener
,
45 public nsIThreadRetargetableStreamListener
,
46 public nsIChannelEventSink
,
47 public nsIInterfaceRequestor
,
48 public nsIAsyncVerifyRedirectCallback
{
49 typedef mozilla::image::Image Image
;
50 typedef mozilla::image::ImageCacheKey ImageCacheKey
;
51 typedef mozilla::image::ProgressTracker ProgressTracker
;
52 typedef mozilla::dom::ReferrerPolicy ReferrerPolicy
;
55 imgRequest(imgLoader
* aLoader
, const ImageCacheKey
& aCacheKey
);
57 NS_DECL_THREADSAFE_ISUPPORTS
58 NS_DECL_NSISTREAMLISTENER
59 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
60 NS_DECL_NSIREQUESTOBSERVER
61 NS_DECL_NSICHANNELEVENTSINK
62 NS_DECL_NSIINTERFACEREQUESTOR
63 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
65 [[nodiscard
]] nsresult
Init(nsIURI
* aURI
, nsIURI
* aFinalURI
,
66 bool aHadInsecureRedirect
, nsIRequest
* aRequest
,
67 nsIChannel
* aChannel
, imgCacheEntry
* aCacheEntry
,
68 mozilla::dom::Document
* aLoadingDocument
,
69 nsIPrincipal
* aTriggeringPrincipal
,
70 mozilla::CORSMode aCORSMode
,
71 nsIReferrerInfo
* aReferrerInfo
);
75 // Callers must call imgRequestProxy::Notify later.
76 void AddProxy(imgRequestProxy
* proxy
);
78 // Whether a given document is allowed to reuse this request without any
80 bool CanReuseWithoutValidation(mozilla::dom::Document
*) const;
82 nsresult
RemoveProxy(imgRequestProxy
* proxy
, nsresult aStatus
);
84 // Cancel, but also ensure that all work done in Init() is undone. Call this
85 // only when the channel has failed to open, and so calling Cancel() on it
86 // won't be sufficient.
87 void CancelAndAbort(nsresult aStatus
);
89 // Called or dispatched by cancel for main thread only execution.
90 void ContinueCancel(nsresult aStatus
);
92 // Called or dispatched by EvictFromCache for main thread only execution.
95 // Request that we start decoding the image as soon as data becomes available.
98 uint64_t InnerWindowID() const;
99 void SetInnerWindowID(uint64_t aInnerWindowId
);
101 // Set the cache validation information (expiry time, whether we must
102 // validate, etc) on the cache entry based on the request information.
103 // If this function is called multiple times, the information set earliest
105 static void SetCacheValidation(imgCacheEntry
* aEntry
, nsIRequest
* aRequest
);
107 bool GetMultipart() const;
109 // Returns whether we went through an insecure (non-HTTPS) redirect at some
110 // point during loading. This does not consider the final URI.
111 bool HadInsecureRedirect() const;
113 // The CORS mode for which we loaded this image.
114 mozilla::CORSMode
GetCORSMode() const { return mCORSMode
; }
116 // The ReferrerInfo in effect when loading this image.
117 nsIReferrerInfo
* GetReferrerInfo() const { return mReferrerInfo
; }
119 // The principal for the document that loaded this image. Used when trying to
120 // validate a CORS image load.
121 already_AddRefed
<nsIPrincipal
> GetTriggeringPrincipal() const;
123 // Return the ProgressTracker associated with this imgRequest. It may live
124 // in |mProgressTracker| or in |mImage.mProgressTracker|, depending on whether
125 // mImage has been instantiated yet.
126 already_AddRefed
<ProgressTracker
> GetProgressTracker() const;
128 /// Returns the Image associated with this imgRequest, if it's ready.
129 already_AddRefed
<Image
> GetImage() const;
131 // Get the current principal of the image. No AddRefing.
132 inline nsIPrincipal
* GetPrincipal() const { return mPrincipal
.get(); }
134 /// Get the ImageCacheKey associated with this request.
135 const ImageCacheKey
& CacheKey() const { return mCacheKey
; }
137 // Resize the cache entry to 0 if it exists
138 void ResetCacheEntry();
140 // OK to use on any thread.
141 nsresult
GetURI(nsIURI
** aURI
);
142 nsresult
GetFinalURI(nsIURI
** aURI
);
143 bool IsChrome() const;
146 nsresult
GetImageErrorCode(void);
148 /// Returns a non-owning pointer to this imgRequest's MIME type.
149 const char* GetMimeType() const { return mContentType
.get(); }
151 void GetFileName(nsACString
& aFileName
);
153 /// @return the priority of the underlying network request, or
154 /// PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
155 int32_t Priority() const;
157 /// Adjust the priority of the underlying network request by @aDelta on behalf
159 void AdjustPriority(imgRequestProxy
* aProxy
, int32_t aDelta
);
161 void BoostPriority(uint32_t aCategory
);
163 /// Returns a weak pointer to the underlying request.
164 nsIRequest
* GetRequest() const { return mRequest
; }
166 nsITimedChannel
* GetTimedChannel() const { return mTimedChannel
; }
168 imgCacheValidator
* GetValidator() const { return mValidator
; }
169 void SetValidator(imgCacheValidator
* aValidator
) { mValidator
= aValidator
; }
171 void* LoadId() const { return mLoadId
; }
172 void SetLoadId(void* aLoadId
) { mLoadId
= aLoadId
; }
174 /// Reset the cache entry after we've dropped our reference to it. Used by
175 /// imgLoader when our cache entry is re-requested after we've dropped our
177 void SetCacheEntry(imgCacheEntry
* aEntry
);
179 /// Returns whether we've got a reference to the cache entry.
180 bool HasCacheEntry() const;
182 /// Set whether this request is stored in the cache. If it isn't, regardless
183 /// of whether this request has a non-null mCacheEntry, this imgRequest won't
184 /// try to update or modify the image cache.
185 void SetIsInCache(bool aCacheable
);
187 void EvictFromCache();
188 void RemoveFromCache();
190 // Sets properties for this image; will dispatch to main thread if needed.
191 void SetProperties(const nsACString
& aContentType
,
192 const nsACString
& aContentDisposition
);
194 nsIProperties
* Properties() const { return mProperties
; }
196 bool HasConsumers() const;
198 bool ImageAvailable() const;
200 bool IsDeniedCrossSiteCORSRequest() const {
201 return mIsDeniedCrossSiteCORSRequest
;
204 bool IsCrossSiteNoCORSRequest() const { return mIsCrossSiteNoCORSRequest
; }
207 friend class FinishPreparingForNewPartRunnable
;
209 virtual ~imgRequest();
211 void FinishPreparingForNewPart(const NewPartResult
& aResult
);
213 void Cancel(nsresult aStatus
);
215 // Update the cache entry size based on the image container.
216 void UpdateCacheEntrySize();
218 /// Returns true if StartDecoding() was called.
219 bool IsDecodeRequested() const;
221 void AdjustPriorityInternal(int32_t aDelta
);
223 // Weak reference to parent loader; this request cannot outlive its owner.
225 nsCOMPtr
<nsIRequest
> mRequest
;
226 // The original URI we were loaded with. This is the same as the URI we are
227 // keyed on in the cache. We store a string here to avoid off main thread
228 // refcounting issues with nsStandardURL.
229 nsCOMPtr
<nsIURI
> mURI
;
230 // The URI of the resource we ended up loading after all redirects, etc.
231 nsCOMPtr
<nsIURI
> mFinalURI
;
232 // The principal which triggered the load of this image. Generally either
233 // the principal of the document the image is being loaded into, or of the
234 // stylesheet which specified the image to load. Used when validating for
236 nsCOMPtr
<nsIPrincipal
> mTriggeringPrincipal
;
237 // The principal of this image.
238 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
239 nsCOMPtr
<nsIProperties
> mProperties
;
240 nsCOMPtr
<nsIChannel
> mChannel
;
241 nsCOMPtr
<nsIInterfaceRequestor
> mPrevChannelSink
;
243 nsCOMPtr
<nsITimedChannel
> mTimedChannel
;
245 nsCString mContentType
;
247 /* we hold on to this to this so long as we have observers */
248 RefPtr
<imgCacheEntry
> mCacheEntry
;
250 /// The key under which this imgRequest is stored in the image cache.
251 ImageCacheKey mCacheKey
;
255 /// Raw pointer to the first proxy that was added to this imgRequest. Use only
256 /// pointer comparisons; there's no guarantee this will remain valid.
259 imgCacheValidator
* mValidator
;
260 nsCOMPtr
<nsIAsyncVerifyRedirectCallback
> mRedirectCallback
;
261 nsCOMPtr
<nsIChannel
> mNewRedirectChannel
;
263 // The CORS mode (defined in imgIRequest) this image was loaded with. By
264 // default, CORS_NONE.
265 mozilla::CORSMode mCORSMode
;
267 // The ReferrerInfo used for this image.
268 nsCOMPtr
<nsIReferrerInfo
> mReferrerInfo
;
270 nsresult mImageErrorCode
;
272 // The categories of prioritization strategy that have been requested.
273 uint32_t mBoostCategoriesRequested
= 0;
275 // If we've called OnImageAvailable.
276 bool mImageAvailable
;
277 bool mIsDeniedCrossSiteCORSRequest
;
278 bool mIsCrossSiteNoCORSRequest
;
280 mutable mozilla::Mutex mMutex
;
282 // Member variables protected by mMutex. Note that *all* flags in our bitfield
283 // are protected by mMutex; if you're adding a new flag that isn'protected, it
284 // must not be a part of this bitfield.
285 RefPtr
<ProgressTracker
> mProgressTracker
MOZ_GUARDED_BY(mMutex
);
286 RefPtr
<Image
> mImage
MOZ_GUARDED_BY(mMutex
);
287 bool mIsMultiPartChannel
: 1 MOZ_GUARDED_BY(mMutex
);
288 bool mIsInCache
: 1 MOZ_GUARDED_BY(mMutex
);
289 bool mDecodeRequested
: 1 MOZ_GUARDED_BY(mMutex
);
290 bool mNewPartPending
: 1 MOZ_GUARDED_BY(mMutex
);
291 bool mHadInsecureRedirect
: 1 MOZ_GUARDED_BY(mMutex
);
292 // The ID of the inner window origin, used for error reporting.
293 uint64_t mInnerWindowId
MOZ_GUARDED_BY(mMutex
);
296 #endif // mozilla_image_imgRequest_h