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
;
28 class nsIApplicationCache
;
31 class nsITimedChannel
;
33 class nsIReferrerInfo
;
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
66 Init(nsIURI
* aURI
, nsIURI
* aFinalURI
, bool aHadInsecureRedirect
,
67 nsIRequest
* aRequest
, nsIChannel
* aChannel
,
68 imgCacheEntry
* aCacheEntry
, mozilla::dom::Document
* aLoadingDocument
,
69 nsIPrincipal
* aTriggeringPrincipal
, int32_t aCORSMode
,
70 nsIReferrerInfo
* aReferrerInfo
);
74 // Callers must call imgRequestProxy::Notify later.
75 void AddProxy(imgRequestProxy
* proxy
);
77 nsresult
RemoveProxy(imgRequestProxy
* proxy
, nsresult aStatus
);
79 // Cancel, but also ensure that all work done in Init() is undone. Call this
80 // only when the channel has failed to open, and so calling Cancel() on it
81 // won't be sufficient.
82 void CancelAndAbort(nsresult aStatus
);
84 // Called or dispatched by cancel for main thread only execution.
85 void ContinueCancel(nsresult aStatus
);
87 // Called or dispatched by EvictFromCache for main thread only execution.
90 // Request that we start decoding the image as soon as data becomes available.
93 inline uint64_t InnerWindowID() const { return mInnerWindowId
; }
94 void SetInnerWindowID(uint64_t aInnerWindowId
) {
95 mInnerWindowId
= aInnerWindowId
;
98 // Set the cache validation information (expiry time, whether we must
99 // validate, etc) on the cache entry based on the request information.
100 // If this function is called multiple times, the information set earliest
102 static void SetCacheValidation(imgCacheEntry
* aEntry
, nsIRequest
* aRequest
);
104 // Check if application cache of the original load is different from
105 // application cache of the new load. Also lack of application cache
106 // on one of the loads is considered a change of a loading cache since
107 // HTTP cache may contain a different data then app cache.
108 bool CacheChanged(nsIRequest
* aNewRequest
);
110 bool GetMultipart() const;
112 // Returns whether we went through an insecure (non-HTTPS) redirect at some
113 // point during loading. This does not consider the final URI.
114 bool HadInsecureRedirect() const;
116 // The CORS mode for which we loaded this image.
117 int32_t GetCORSMode() const { return mCORSMode
; }
119 // The ReferrerInfo in effect when loading this image.
120 nsIReferrerInfo
* GetReferrerInfo() const { return mReferrerInfo
; }
122 // The principal for the document that loaded this image. Used when trying to
123 // validate a CORS image load.
124 already_AddRefed
<nsIPrincipal
> GetTriggeringPrincipal() const {
125 nsCOMPtr
<nsIPrincipal
> principal
= mTriggeringPrincipal
;
126 return principal
.forget();
129 // Return the ProgressTracker associated with this imgRequest. It may live
130 // in |mProgressTracker| or in |mImage.mProgressTracker|, depending on whether
131 // mImage has been instantiated yet.
132 already_AddRefed
<ProgressTracker
> GetProgressTracker() const;
134 /// Returns the Image associated with this imgRequest, if it's ready.
135 already_AddRefed
<Image
> GetImage() const;
137 // Get the current principal of the image. No AddRefing.
138 inline nsIPrincipal
* GetPrincipal() const { return mPrincipal
.get(); }
140 /// Get the ImageCacheKey associated with this request.
141 const ImageCacheKey
& CacheKey() const { return mCacheKey
; }
143 // Resize the cache entry to 0 if it exists
144 void ResetCacheEntry();
146 // OK to use on any thread.
147 nsresult
GetURI(nsIURI
** aURI
);
148 nsresult
GetFinalURI(nsIURI
** aURI
);
149 bool IsChrome() const;
152 nsresult
GetImageErrorCode(void);
154 /// Returns a non-owning pointer to this imgRequest's MIME type.
155 const char* GetMimeType() const { return mContentType
.get(); }
157 /// @return the priority of the underlying network request, or
158 /// PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
159 int32_t Priority() const;
161 /// Adjust the priority of the underlying network request by @aDelta on behalf
163 void AdjustPriority(imgRequestProxy
* aProxy
, int32_t aDelta
);
165 void BoostPriority(uint32_t aCategory
);
167 /// Returns a weak pointer to the underlying request.
168 nsIRequest
* GetRequest() const { return mRequest
; }
170 nsITimedChannel
* GetTimedChannel() const { return mTimedChannel
; }
172 imgCacheValidator
* GetValidator() const { return mValidator
; }
173 void SetValidator(imgCacheValidator
* aValidator
) { mValidator
= aValidator
; }
175 void* LoadId() const { return mLoadId
; }
176 void SetLoadId(void* aLoadId
) { mLoadId
= aLoadId
; }
178 /// Reset the cache entry after we've dropped our reference to it. Used by
179 /// imgLoader when our cache entry is re-requested after we've dropped our
181 void SetCacheEntry(imgCacheEntry
* aEntry
);
183 /// Returns whether we've got a reference to the cache entry.
184 bool HasCacheEntry() const;
186 /// Set whether this request is stored in the cache. If it isn't, regardless
187 /// of whether this request has a non-null mCacheEntry, this imgRequest won't
188 /// try to update or modify the image cache.
189 void SetIsInCache(bool aCacheable
);
191 void EvictFromCache();
192 void RemoveFromCache();
194 // Sets properties for this image; will dispatch to main thread if needed.
195 void SetProperties(const nsACString
& aContentType
,
196 const nsACString
& aContentDisposition
);
198 nsIProperties
* Properties() const { return mProperties
; }
200 bool HasConsumers() const;
202 bool ImageAvailable() const;
204 bool IsDeniedCrossSiteCORSRequest() const {
205 return mIsDeniedCrossSiteCORSRequest
;
208 bool IsCrossSiteNoCORSRequest() const { return mIsCrossSiteNoCORSRequest
; }
211 friend class FinishPreparingForNewPartRunnable
;
213 virtual ~imgRequest();
215 void FinishPreparingForNewPart(const NewPartResult
& aResult
);
217 void Cancel(nsresult aStatus
);
219 // Update the cache entry size based on the image container.
220 void UpdateCacheEntrySize();
222 /// Returns true if StartDecoding() was called.
223 bool IsDecodeRequested() const;
225 void AdjustPriorityInternal(int32_t aDelta
);
227 // Weak reference to parent loader; this request cannot outlive its owner.
229 nsCOMPtr
<nsIRequest
> mRequest
;
230 // The original URI we were loaded with. This is the same as the URI we are
231 // keyed on in the cache. We store a string here to avoid off main thread
232 // refcounting issues with nsStandardURL.
233 nsCOMPtr
<nsIURI
> mURI
;
234 // The URI of the resource we ended up loading after all redirects, etc.
235 nsCOMPtr
<nsIURI
> mFinalURI
;
236 // The principal which triggered the load of this image. Generally either
237 // the principal of the document the image is being loaded into, or of the
238 // stylesheet which specified the image to load. Used when validating for
240 nsCOMPtr
<nsIPrincipal
> mTriggeringPrincipal
;
241 // The principal of this image.
242 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
243 nsCOMPtr
<nsIProperties
> mProperties
;
244 nsCOMPtr
<nsIChannel
> mChannel
;
245 nsCOMPtr
<nsIInterfaceRequestor
> mPrevChannelSink
;
246 nsCOMPtr
<nsIApplicationCache
> mApplicationCache
;
248 nsCOMPtr
<nsITimedChannel
> mTimedChannel
;
250 nsCString mContentType
;
252 /* we hold on to this to this so long as we have observers */
253 RefPtr
<imgCacheEntry
> mCacheEntry
;
255 /// The key under which this imgRequest is stored in the image cache.
256 ImageCacheKey mCacheKey
;
260 /// Raw pointer to the first proxy that was added to this imgRequest. Use only
261 /// pointer comparisons; there's no guarantee this will remain valid.
264 imgCacheValidator
* mValidator
;
265 nsCOMPtr
<nsIAsyncVerifyRedirectCallback
> mRedirectCallback
;
266 nsCOMPtr
<nsIChannel
> mNewRedirectChannel
;
268 // The ID of the inner window origin, used for error reporting.
269 uint64_t mInnerWindowId
;
271 // The CORS mode (defined in imgIRequest) this image was loaded with. By
272 // default, imgIRequest::CORS_NONE.
275 // The ReferrerInfo used for this image.
276 nsCOMPtr
<nsIReferrerInfo
> mReferrerInfo
;
278 nsresult mImageErrorCode
;
280 // The categories of prioritization strategy that have been requested.
281 uint32_t mBoostCategoriesRequested
= 0;
283 // If we've called OnImageAvailable.
284 bool mImageAvailable
;
285 bool mIsDeniedCrossSiteCORSRequest
;
286 bool mIsCrossSiteNoCORSRequest
;
288 mutable mozilla::Mutex mMutex
;
290 // Member variables protected by mMutex. Note that *all* flags in our bitfield
291 // are protected by mMutex; if you're adding a new flag that isn'protected, it
292 // must not be a part of this bitfield.
293 RefPtr
<ProgressTracker
> mProgressTracker
;
294 RefPtr
<Image
> mImage
;
295 bool mIsMultiPartChannel
: 1;
297 bool mDecodeRequested
: 1;
298 bool mNewPartPending
: 1;
299 bool mHadInsecureRedirect
: 1;
302 #endif // mozilla_image_imgRequest_h