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
Init(nsIURI
* aURI
, nsIURI
* aFinalURI
,
66 bool aHadInsecureRedirect
, nsIRequest
* aRequest
,
67 nsIChannel
* aChannel
, imgCacheEntry
* aCacheEntry
,
68 mozilla::dom::Document
* aLoadingDocument
,
69 nsIPrincipal
* aTriggeringPrincipal
,
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 inline uint64_t InnerWindowID() const { return mInnerWindowId
; }
99 void SetInnerWindowID(uint64_t aInnerWindowId
) {
100 mInnerWindowId
= aInnerWindowId
;
103 // Set the cache validation information (expiry time, whether we must
104 // validate, etc) on the cache entry based on the request information.
105 // If this function is called multiple times, the information set earliest
107 static void SetCacheValidation(imgCacheEntry
* aEntry
, nsIRequest
* aRequest
);
109 // Check if application cache of the original load is different from
110 // application cache of the new load. Also lack of application cache
111 // on one of the loads is considered a change of a loading cache since
112 // HTTP cache may contain a different data then app cache.
113 bool CacheChanged(nsIRequest
* aNewRequest
);
115 bool GetMultipart() const;
117 // Returns whether we went through an insecure (non-HTTPS) redirect at some
118 // point during loading. This does not consider the final URI.
119 bool HadInsecureRedirect() const;
121 // The CORS mode for which we loaded this image.
122 int32_t GetCORSMode() const { return mCORSMode
; }
124 // The ReferrerInfo in effect when loading this image.
125 nsIReferrerInfo
* GetReferrerInfo() const { return mReferrerInfo
; }
127 // The principal for the document that loaded this image. Used when trying to
128 // validate a CORS image load.
129 already_AddRefed
<nsIPrincipal
> GetTriggeringPrincipal() const {
130 nsCOMPtr
<nsIPrincipal
> principal
= mTriggeringPrincipal
;
131 return principal
.forget();
134 // Return the ProgressTracker associated with this imgRequest. It may live
135 // in |mProgressTracker| or in |mImage.mProgressTracker|, depending on whether
136 // mImage has been instantiated yet.
137 already_AddRefed
<ProgressTracker
> GetProgressTracker() const;
139 /// Returns the Image associated with this imgRequest, if it's ready.
140 already_AddRefed
<Image
> GetImage() const;
142 // Get the current principal of the image. No AddRefing.
143 inline nsIPrincipal
* GetPrincipal() const { return mPrincipal
.get(); }
145 /// Get the ImageCacheKey associated with this request.
146 const ImageCacheKey
& CacheKey() const { return mCacheKey
; }
148 // Resize the cache entry to 0 if it exists
149 void ResetCacheEntry();
151 // OK to use on any thread.
152 nsresult
GetURI(nsIURI
** aURI
);
153 nsresult
GetFinalURI(nsIURI
** aURI
);
154 bool IsChrome() const;
157 nsresult
GetImageErrorCode(void);
159 /// Returns a non-owning pointer to this imgRequest's MIME type.
160 const char* GetMimeType() const { return mContentType
.get(); }
162 /// @return the priority of the underlying network request, or
163 /// PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
164 int32_t Priority() const;
166 /// Adjust the priority of the underlying network request by @aDelta on behalf
168 void AdjustPriority(imgRequestProxy
* aProxy
, int32_t aDelta
);
170 void BoostPriority(uint32_t aCategory
);
172 /// Returns a weak pointer to the underlying request.
173 nsIRequest
* GetRequest() const { return mRequest
; }
175 nsITimedChannel
* GetTimedChannel() const { return mTimedChannel
; }
177 imgCacheValidator
* GetValidator() const { return mValidator
; }
178 void SetValidator(imgCacheValidator
* aValidator
) { mValidator
= aValidator
; }
180 void* LoadId() const { return mLoadId
; }
181 void SetLoadId(void* aLoadId
) { mLoadId
= aLoadId
; }
183 /// Reset the cache entry after we've dropped our reference to it. Used by
184 /// imgLoader when our cache entry is re-requested after we've dropped our
186 void SetCacheEntry(imgCacheEntry
* aEntry
);
188 /// Returns whether we've got a reference to the cache entry.
189 bool HasCacheEntry() const;
191 /// Set whether this request is stored in the cache. If it isn't, regardless
192 /// of whether this request has a non-null mCacheEntry, this imgRequest won't
193 /// try to update or modify the image cache.
194 void SetIsInCache(bool aCacheable
);
196 void EvictFromCache();
197 void RemoveFromCache();
199 // Sets properties for this image; will dispatch to main thread if needed.
200 void SetProperties(const nsACString
& aContentType
,
201 const nsACString
& aContentDisposition
);
203 nsIProperties
* Properties() const { return mProperties
; }
205 bool HasConsumers() const;
207 bool ImageAvailable() const;
209 bool IsDeniedCrossSiteCORSRequest() const {
210 return mIsDeniedCrossSiteCORSRequest
;
213 bool IsCrossSiteNoCORSRequest() const { return mIsCrossSiteNoCORSRequest
; }
216 friend class FinishPreparingForNewPartRunnable
;
218 virtual ~imgRequest();
220 void FinishPreparingForNewPart(const NewPartResult
& aResult
);
222 void Cancel(nsresult aStatus
);
224 // Update the cache entry size based on the image container.
225 void UpdateCacheEntrySize();
227 /// Returns true if StartDecoding() was called.
228 bool IsDecodeRequested() const;
230 void AdjustPriorityInternal(int32_t aDelta
);
232 // Weak reference to parent loader; this request cannot outlive its owner.
234 nsCOMPtr
<nsIRequest
> mRequest
;
235 // The original URI we were loaded with. This is the same as the URI we are
236 // keyed on in the cache. We store a string here to avoid off main thread
237 // refcounting issues with nsStandardURL.
238 nsCOMPtr
<nsIURI
> mURI
;
239 // The URI of the resource we ended up loading after all redirects, etc.
240 nsCOMPtr
<nsIURI
> mFinalURI
;
241 // The principal which triggered the load of this image. Generally either
242 // the principal of the document the image is being loaded into, or of the
243 // stylesheet which specified the image to load. Used when validating for
245 nsCOMPtr
<nsIPrincipal
> mTriggeringPrincipal
;
246 // The principal of this image.
247 nsCOMPtr
<nsIPrincipal
> mPrincipal
;
248 nsCOMPtr
<nsIProperties
> mProperties
;
249 nsCOMPtr
<nsIChannel
> mChannel
;
250 nsCOMPtr
<nsIInterfaceRequestor
> mPrevChannelSink
;
251 nsCOMPtr
<nsIApplicationCache
> mApplicationCache
;
253 nsCOMPtr
<nsITimedChannel
> mTimedChannel
;
255 nsCString mContentType
;
257 /* we hold on to this to this so long as we have observers */
258 RefPtr
<imgCacheEntry
> mCacheEntry
;
260 /// The key under which this imgRequest is stored in the image cache.
261 ImageCacheKey mCacheKey
;
265 /// Raw pointer to the first proxy that was added to this imgRequest. Use only
266 /// pointer comparisons; there's no guarantee this will remain valid.
269 imgCacheValidator
* mValidator
;
270 nsCOMPtr
<nsIAsyncVerifyRedirectCallback
> mRedirectCallback
;
271 nsCOMPtr
<nsIChannel
> mNewRedirectChannel
;
273 // The ID of the inner window origin, used for error reporting.
274 uint64_t mInnerWindowId
;
276 // The CORS mode (defined in imgIRequest) this image was loaded with. By
277 // default, imgIRequest::CORS_NONE.
280 // The ReferrerInfo used for this image.
281 nsCOMPtr
<nsIReferrerInfo
> mReferrerInfo
;
283 nsresult mImageErrorCode
;
285 // The categories of prioritization strategy that have been requested.
286 uint32_t mBoostCategoriesRequested
= 0;
288 // If we've called OnImageAvailable.
289 bool mImageAvailable
;
290 bool mIsDeniedCrossSiteCORSRequest
;
291 bool mIsCrossSiteNoCORSRequest
;
293 mutable mozilla::Mutex mMutex
;
295 // Member variables protected by mMutex. Note that *all* flags in our bitfield
296 // are protected by mMutex; if you're adding a new flag that isn'protected, it
297 // must not be a part of this bitfield.
298 RefPtr
<ProgressTracker
> mProgressTracker
;
299 RefPtr
<Image
> mImage
;
300 bool mIsMultiPartChannel
: 1;
302 bool mDecodeRequested
: 1;
303 bool mNewPartPending
: 1;
304 bool mHadInsecureRedirect
: 1;
307 #endif // mozilla_image_imgRequest_h