Bug 1708243 - Part 2: stop using sender data from the child process r=robwu,agi
[gecko.git] / image / imgRequest.h
blob51aec078cbc8f3c64c64458f5671f1f1836283d2
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"
16 #include "nsCOMPtr.h"
17 #include "nsProxyRelease.h"
18 #include "nsString.h"
19 #include "nsError.h"
20 #include "nsIAsyncVerifyRedirectCallback.h"
21 #include "mozilla/Mutex.h"
22 #include "ImageCacheKey.h"
24 class imgCacheValidator;
25 class imgLoader;
26 class imgRequestProxy;
27 class imgCacheEntry;
28 class nsIProperties;
29 class nsIRequest;
30 class nsITimedChannel;
31 class nsIURI;
32 class nsIReferrerInfo;
34 namespace mozilla {
35 enum CORSMode : uint8_t;
36 namespace image {
37 class Image;
38 class ProgressTracker;
39 } // namespace image
40 } // namespace mozilla
42 struct NewPartResult;
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;
54 public:
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);
73 void ClearLoader();
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
79 // revalidation.
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.
93 void ContinueEvict();
95 // Request that we start decoding the image as soon as data becomes available.
96 void StartDecoding();
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
106 // wins.
107 static void SetCacheValidation(imgCacheEntry* aEntry, nsIRequest* aRequest);
109 bool GetMultipart() const;
111 // Returns whether we went through an insecure (non-HTTPS) redirect at some
112 // point during loading. This does not consider the final URI.
113 bool HadInsecureRedirect() const;
115 // The CORS mode for which we loaded this image.
116 mozilla::CORSMode GetCORSMode() const { return mCORSMode; }
118 // The ReferrerInfo in effect when loading this image.
119 nsIReferrerInfo* GetReferrerInfo() const { return mReferrerInfo; }
121 // The principal for the document that loaded this image. Used when trying to
122 // validate a CORS image load.
123 already_AddRefed<nsIPrincipal> GetTriggeringPrincipal() const {
124 nsCOMPtr<nsIPrincipal> principal = mTriggeringPrincipal;
125 return principal.forget();
128 // Return the ProgressTracker associated with this imgRequest. It may live
129 // in |mProgressTracker| or in |mImage.mProgressTracker|, depending on whether
130 // mImage has been instantiated yet.
131 already_AddRefed<ProgressTracker> GetProgressTracker() const;
133 /// Returns the Image associated with this imgRequest, if it's ready.
134 already_AddRefed<Image> GetImage() const;
136 // Get the current principal of the image. No AddRefing.
137 inline nsIPrincipal* GetPrincipal() const { return mPrincipal.get(); }
139 /// Get the ImageCacheKey associated with this request.
140 const ImageCacheKey& CacheKey() const { return mCacheKey; }
142 // Resize the cache entry to 0 if it exists
143 void ResetCacheEntry();
145 // OK to use on any thread.
146 nsresult GetURI(nsIURI** aURI);
147 nsresult GetFinalURI(nsIURI** aURI);
148 bool IsChrome() const;
149 bool IsData() const;
151 nsresult GetImageErrorCode(void);
153 /// Returns a non-owning pointer to this imgRequest's MIME type.
154 const char* GetMimeType() const { return mContentType.get(); }
156 /// @return the priority of the underlying network request, or
157 /// PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
158 int32_t Priority() const;
160 /// Adjust the priority of the underlying network request by @aDelta on behalf
161 /// of @aProxy.
162 void AdjustPriority(imgRequestProxy* aProxy, int32_t aDelta);
164 void BoostPriority(uint32_t aCategory);
166 /// Returns a weak pointer to the underlying request.
167 nsIRequest* GetRequest() const { return mRequest; }
169 nsITimedChannel* GetTimedChannel() const { return mTimedChannel; }
171 imgCacheValidator* GetValidator() const { return mValidator; }
172 void SetValidator(imgCacheValidator* aValidator) { mValidator = aValidator; }
174 void* LoadId() const { return mLoadId; }
175 void SetLoadId(void* aLoadId) { mLoadId = aLoadId; }
177 /// Reset the cache entry after we've dropped our reference to it. Used by
178 /// imgLoader when our cache entry is re-requested after we've dropped our
179 /// reference to it.
180 void SetCacheEntry(imgCacheEntry* aEntry);
182 /// Returns whether we've got a reference to the cache entry.
183 bool HasCacheEntry() const;
185 /// Set whether this request is stored in the cache. If it isn't, regardless
186 /// of whether this request has a non-null mCacheEntry, this imgRequest won't
187 /// try to update or modify the image cache.
188 void SetIsInCache(bool aCacheable);
190 void EvictFromCache();
191 void RemoveFromCache();
193 // Sets properties for this image; will dispatch to main thread if needed.
194 void SetProperties(const nsACString& aContentType,
195 const nsACString& aContentDisposition);
197 nsIProperties* Properties() const { return mProperties; }
199 bool HasConsumers() const;
201 bool ImageAvailable() const;
203 bool IsDeniedCrossSiteCORSRequest() const {
204 return mIsDeniedCrossSiteCORSRequest;
207 bool IsCrossSiteNoCORSRequest() const { return mIsCrossSiteNoCORSRequest; }
209 private:
210 friend class FinishPreparingForNewPartRunnable;
212 virtual ~imgRequest();
214 void FinishPreparingForNewPart(const NewPartResult& aResult);
216 void Cancel(nsresult aStatus);
218 // Update the cache entry size based on the image container.
219 void UpdateCacheEntrySize();
221 /// Returns true if StartDecoding() was called.
222 bool IsDecodeRequested() const;
224 void AdjustPriorityInternal(int32_t aDelta);
226 // Weak reference to parent loader; this request cannot outlive its owner.
227 imgLoader* mLoader;
228 nsCOMPtr<nsIRequest> mRequest;
229 // The original URI we were loaded with. This is the same as the URI we are
230 // keyed on in the cache. We store a string here to avoid off main thread
231 // refcounting issues with nsStandardURL.
232 nsCOMPtr<nsIURI> mURI;
233 // The URI of the resource we ended up loading after all redirects, etc.
234 nsCOMPtr<nsIURI> mFinalURI;
235 // The principal which triggered the load of this image. Generally either
236 // the principal of the document the image is being loaded into, or of the
237 // stylesheet which specified the image to load. Used when validating for
238 // CORS.
239 nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
240 // The principal of this image.
241 nsCOMPtr<nsIPrincipal> mPrincipal;
242 nsCOMPtr<nsIProperties> mProperties;
243 nsCOMPtr<nsIChannel> mChannel;
244 nsCOMPtr<nsIInterfaceRequestor> mPrevChannelSink;
246 nsCOMPtr<nsITimedChannel> mTimedChannel;
248 nsCString mContentType;
250 /* we hold on to this to this so long as we have observers */
251 RefPtr<imgCacheEntry> mCacheEntry;
253 /// The key under which this imgRequest is stored in the image cache.
254 ImageCacheKey mCacheKey;
256 void* mLoadId;
258 /// Raw pointer to the first proxy that was added to this imgRequest. Use only
259 /// pointer comparisons; there's no guarantee this will remain valid.
260 void* mFirstProxy;
262 imgCacheValidator* mValidator;
263 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
264 nsCOMPtr<nsIChannel> mNewRedirectChannel;
266 // The ID of the inner window origin, used for error reporting.
267 uint64_t mInnerWindowId;
269 // The CORS mode (defined in imgIRequest) this image was loaded with. By
270 // default, CORS_NONE.
271 mozilla::CORSMode mCORSMode;
273 // The ReferrerInfo used for this image.
274 nsCOMPtr<nsIReferrerInfo> mReferrerInfo;
276 nsresult mImageErrorCode;
278 // The categories of prioritization strategy that have been requested.
279 uint32_t mBoostCategoriesRequested = 0;
281 // If we've called OnImageAvailable.
282 bool mImageAvailable;
283 bool mIsDeniedCrossSiteCORSRequest;
284 bool mIsCrossSiteNoCORSRequest;
286 mutable mozilla::Mutex mMutex;
288 // Member variables protected by mMutex. Note that *all* flags in our bitfield
289 // are protected by mMutex; if you're adding a new flag that isn'protected, it
290 // must not be a part of this bitfield.
291 RefPtr<ProgressTracker> mProgressTracker;
292 RefPtr<Image> mImage;
293 bool mIsMultiPartChannel : 1;
294 bool mIsInCache : 1;
295 bool mDecodeRequested : 1;
296 bool mNewPartPending : 1;
297 bool mHadInsecureRedirect : 1;
300 #endif // mozilla_image_imgRequest_h