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 // Whether a given document is allowed to reuse this request without any
79 bool CanReuseWithoutValidation(mozilla::dom::Document
*) const;
81 nsresult
RemoveProxy(imgRequestProxy
* proxy
, nsresult aStatus
);
83 // Cancel, but also ensure that all work done in Init() is undone. Call this
84 // only when the channel has failed to open, and so calling Cancel() on it
85 // won't be sufficient.
86 void CancelAndAbort(nsresult aStatus
);
88 // Called or dispatched by cancel for main thread only execution.
89 void ContinueCancel(nsresult aStatus
);
91 // Called or dispatched by EvictFromCache for main thread only execution.
94 // Request that we start decoding the image as soon as data becomes available.
97 inline uint64_t InnerWindowID() const { return mInnerWindowId
; }
98 void SetInnerWindowID(uint64_t aInnerWindowId
) {
99 mInnerWindowId
= aInnerWindowId
;
102 // Set the cache validation information (expiry time, whether we must
103 // validate, etc) on the cache entry based on the request information.
104 // If this function is called multiple times, the information set earliest
106 static void SetCacheValidation(imgCacheEntry
* aEntry
, nsIRequest
* aRequest
);
108 // Check if application cache of the original load is different from
109 // application cache of the new load. Also lack of application cache
110 // on one of the loads is considered a change of a loading cache since
111 // HTTP cache may contain a different data then app cache.
112 bool CacheChanged(nsIRequest
* aNewRequest
);
114 bool GetMultipart() const;
116 // Returns whether we went through an insecure (non-HTTPS) redirect at some
117 // point during loading. This does not consider the final URI.
118 bool HadInsecureRedirect() const;
120 // The CORS mode for which we loaded this image.
121 int32_t GetCORSMode() const { return mCORSMode
; }
123 // The ReferrerInfo in effect when loading this image.
124 nsIReferrerInfo
* GetReferrerInfo() const { return mReferrerInfo
; }
126 // The principal for the document that loaded this image. Used when trying to
127 // validate a CORS image load.
128 already_AddRefed
<nsIPrincipal
> GetTriggeringPrincipal() const {
129 nsCOMPtr
<nsIPrincipal
> principal
= mTriggeringPrincipal
;
130 return principal
.forget();
133 // Return the ProgressTracker associated with this imgRequest. It may live
134 // in |mProgressTracker| or in |mImage.mProgressTracker|, depending on whether
135 // mImage has been instantiated yet.
136 already_AddRefed
<ProgressTracker
> GetProgressTracker() const;
138 /// Returns the Image associated with this imgRequest, if it's ready.
139 already_AddRefed
<Image
> GetImage() const;
141 // Get the current principal of the image. No AddRefing.
142 inline nsIPrincipal
* GetPrincipal() const { return mPrincipal
.get(); }
144 /// Get the ImageCacheKey associated with this request.
145 const ImageCacheKey
& CacheKey() const { return mCacheKey
; }
147 // Resize the cache entry to 0 if it exists
148 void ResetCacheEntry();
150 // OK to use on any thread.
151 nsresult
GetURI(nsIURI
** aURI
);
152 nsresult
GetFinalURI(nsIURI
** aURI
);
153 bool IsChrome() const;
156 nsresult
GetImageErrorCode(void);
158 /// Returns a non-owning pointer to this imgRequest's MIME type.
159 const char* GetMimeType() const { return mContentType
.get(); }
161 /// @return the priority of the underlying network request, or
162 /// PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
163 int32_t Priority() const;
165 /// Adjust the priority of the underlying network request by @aDelta on behalf
167 void AdjustPriority(imgRequestProxy
* aProxy
, int32_t aDelta
);
169 void BoostPriority(uint32_t aCategory
);
171 /// Returns a weak pointer to the underlying request.
172 nsIRequest
* GetRequest() const { return mRequest
; }
174 nsITimedChannel
* GetTimedChannel() const { return mTimedChannel
; }
176 imgCacheValidator
* GetValidator() const { return mValidator
; }
177 void SetValidator(imgCacheValidator
* aValidator
) { mValidator
= aValidator
; }
179 void* LoadId() const { return mLoadId
; }
180 void SetLoadId(void* aLoadId
) { mLoadId
= aLoadId
; }
182 /// Reset the cache entry after we've dropped our reference to it. Used by
183 /// imgLoader when our cache entry is re-requested after we've dropped our
185 void SetCacheEntry(imgCacheEntry
* aEntry
);
187 /// Returns whether we've got a reference to the cache entry.
188 bool HasCacheEntry() const;
190 /// Set whether this request is stored in the cache. If it isn't, regardless
191 /// of whether this request has a non-null mCacheEntry, this imgRequest won't
192 /// try to update or modify the image cache.
193 void SetIsInCache(bool aCacheable
);
195 void EvictFromCache();
196 void RemoveFromCache();
198 // Sets properties for this image; will dispatch to main thread if needed.
199 void SetProperties(const nsACString
& aContentType
,
200 const nsACString
& aContentDisposition
);
202 nsIProperties
* Properties() const { return mProperties
; }
204 bool HasConsumers() const;
206 bool ImageAvailable() const;
208 bool IsDeniedCrossSiteCORSRequest() const {
209 return mIsDeniedCrossSiteCORSRequest
;
212 bool IsCrossSiteNoCORSRequest() const { return mIsCrossSiteNoCORSRequest
; }
215 friend class FinishPreparingForNewPartRunnable
;
217 virtual ~imgRequest();
219 void FinishPreparingForNewPart(const NewPartResult
& aResult
);
221 void Cancel(nsresult aStatus
);
223 // Update the cache entry size based on the image container.
224 void UpdateCacheEntrySize();
226 /// Returns true if StartDecoding() was called.
227 bool IsDecodeRequested() const;
229 void AdjustPriorityInternal(int32_t aDelta
);
231 // Weak reference to parent loader; this request cannot outlive its owner.
233 nsCOMPtr
<nsIRequest
> mRequest
;
234 // The original URI we were loaded with. This is the same as the URI we are
235 // keyed on in the cache. We store a string here to avoid off main thread
236 // refcounting issues with nsStandardURL.
237 nsCOMPtr
<nsIURI
> mURI
;
238 // The URI of the resource we ended up loading after all redirects, etc.
239 nsCOMPtr
<nsIURI
> mFinalURI
;
240 // The principal which triggered the load of this image. Generally either
241 // the principal of the document the image is being loaded into, or of the
242 // stylesheet which specified the image to load. Used when validating for
244 nsCOMPtr
<nsIPrincipal
> mTriggeringPrincipal
;
245 // The principal of this image.
246 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
247 nsCOMPtr
<nsIProperties
> mProperties
;
248 nsCOMPtr
<nsIChannel
> mChannel
;
249 nsCOMPtr
<nsIInterfaceRequestor
> mPrevChannelSink
;
250 nsCOMPtr
<nsIApplicationCache
> mApplicationCache
;
252 nsCOMPtr
<nsITimedChannel
> mTimedChannel
;
254 nsCString mContentType
;
256 /* we hold on to this to this so long as we have observers */
257 RefPtr
<imgCacheEntry
> mCacheEntry
;
259 /// The key under which this imgRequest is stored in the image cache.
260 ImageCacheKey mCacheKey
;
264 /// Raw pointer to the first proxy that was added to this imgRequest. Use only
265 /// pointer comparisons; there's no guarantee this will remain valid.
268 imgCacheValidator
* mValidator
;
269 nsCOMPtr
<nsIAsyncVerifyRedirectCallback
> mRedirectCallback
;
270 nsCOMPtr
<nsIChannel
> mNewRedirectChannel
;
272 // The ID of the inner window origin, used for error reporting.
273 uint64_t mInnerWindowId
;
275 // The CORS mode (defined in imgIRequest) this image was loaded with. By
276 // default, imgIRequest::CORS_NONE.
279 // The ReferrerInfo used for this image.
280 nsCOMPtr
<nsIReferrerInfo
> mReferrerInfo
;
282 nsresult mImageErrorCode
;
284 // The categories of prioritization strategy that have been requested.
285 uint32_t mBoostCategoriesRequested
= 0;
287 // If we've called OnImageAvailable.
288 bool mImageAvailable
;
289 bool mIsDeniedCrossSiteCORSRequest
;
290 bool mIsCrossSiteNoCORSRequest
;
292 mutable mozilla::Mutex mMutex
;
294 // Member variables protected by mMutex. Note that *all* flags in our bitfield
295 // are protected by mMutex; if you're adding a new flag that isn'protected, it
296 // must not be a part of this bitfield.
297 RefPtr
<ProgressTracker
> mProgressTracker
;
298 RefPtr
<Image
> mImage
;
299 bool mIsMultiPartChannel
: 1;
301 bool mDecodeRequested
: 1;
302 bool mNewPartPending
: 1;
303 bool mHadInsecureRedirect
: 1;
306 #endif // mozilla_image_imgRequest_h