no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / image / imgRequest.h
blob7a751e6505243ee0c201c1098d125d2b75b9798e
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 nsIThreadRetargetableStreamListener,
45 public nsIChannelEventSink,
46 public nsIInterfaceRequestor,
47 public nsIAsyncVerifyRedirectCallback {
48 typedef mozilla::image::Image Image;
49 typedef mozilla::image::ImageCacheKey ImageCacheKey;
50 typedef mozilla::image::ProgressTracker ProgressTracker;
51 typedef mozilla::dom::ReferrerPolicy ReferrerPolicy;
53 public:
54 imgRequest(imgLoader* aLoader, const ImageCacheKey& aCacheKey);
56 NS_DECL_THREADSAFE_ISUPPORTS
57 NS_DECL_NSISTREAMLISTENER
58 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
59 NS_DECL_NSIREQUESTOBSERVER
60 NS_DECL_NSICHANNELEVENTSINK
61 NS_DECL_NSIINTERFACEREQUESTOR
62 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
64 [[nodiscard]] nsresult Init(nsIURI* aURI, nsIURI* aFinalURI,
65 bool aHadInsecureRedirect, nsIRequest* aRequest,
66 nsIChannel* aChannel, imgCacheEntry* aCacheEntry,
67 mozilla::dom::Document* aLoadingDocument,
68 nsIPrincipal* aTriggeringPrincipal,
69 mozilla::CORSMode aCORSMode,
70 nsIReferrerInfo* aReferrerInfo);
72 void ClearLoader();
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
78 // revalidation.
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.
92 void ContinueEvict();
94 // Request that we start decoding the image as soon as data becomes available.
95 void StartDecoding();
97 uint64_t InnerWindowID() const;
98 void SetInnerWindowID(uint64_t aInnerWindowId);
100 // Set the cache validation information (expiry time, whether we must
101 // validate, etc) on the cache entry based on the request information.
102 // If this function is called multiple times, the information set earliest
103 // wins.
104 static void SetCacheValidation(imgCacheEntry* aEntry, nsIRequest* aRequest);
106 bool GetMultipart() const;
108 // Returns whether we went through an insecure (non-HTTPS) redirect at some
109 // point during loading. This does not consider the final URI.
110 bool HadInsecureRedirect() const;
112 // The CORS mode for which we loaded this image.
113 mozilla::CORSMode GetCORSMode() const { return mCORSMode; }
115 // The ReferrerInfo in effect when loading this image.
116 nsIReferrerInfo* GetReferrerInfo() const { return mReferrerInfo; }
118 // The principal for the document that loaded this image. Used when trying to
119 // validate a CORS image load.
120 already_AddRefed<nsIPrincipal> GetTriggeringPrincipal() const;
122 // Return the ProgressTracker associated with this imgRequest. It may live
123 // in |mProgressTracker| or in |mImage.mProgressTracker|, depending on whether
124 // mImage has been instantiated yet.
125 already_AddRefed<ProgressTracker> GetProgressTracker() const;
127 /// Returns the Image associated with this imgRequest, if it's ready.
128 already_AddRefed<Image> GetImage() const;
130 // Get the current principal of the image. No AddRefing.
131 inline nsIPrincipal* GetPrincipal() const { return mPrincipal.get(); }
133 /// Get the ImageCacheKey associated with this request.
134 const ImageCacheKey& CacheKey() const { return mCacheKey; }
136 // Resize the cache entry to 0 if it exists
137 void ResetCacheEntry();
139 // OK to use on any thread.
140 nsresult GetURI(nsIURI** aURI);
141 nsresult GetFinalURI(nsIURI** aURI);
142 bool IsChrome() const;
143 bool IsData() const;
145 nsresult GetImageErrorCode(void);
147 /// Returns a non-owning pointer to this imgRequest's MIME type.
148 const char* GetMimeType() const { return mContentType.get(); }
150 void GetFileName(nsACString& aFileName);
152 /// @return the priority of the underlying network request, or
153 /// PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
154 int32_t Priority() const;
156 /// Adjust the priority of the underlying network request by @aDelta on behalf
157 /// of @aProxy.
158 void AdjustPriority(imgRequestProxy* aProxy, int32_t aDelta);
160 void BoostPriority(uint32_t aCategory);
162 /// Returns a weak pointer to the underlying request.
163 nsIRequest* GetRequest() const { return mRequest; }
165 nsITimedChannel* GetTimedChannel() const { return mTimedChannel; }
167 imgCacheValidator* GetValidator() const { return mValidator; }
168 void SetValidator(imgCacheValidator* aValidator) { mValidator = aValidator; }
170 void* LoadId() const { return mLoadId; }
171 void SetLoadId(void* aLoadId) { mLoadId = aLoadId; }
173 /// Reset the cache entry after we've dropped our reference to it. Used by
174 /// imgLoader when our cache entry is re-requested after we've dropped our
175 /// reference to it.
176 void SetCacheEntry(imgCacheEntry* aEntry);
178 /// Returns whether we've got a reference to the cache entry.
179 bool HasCacheEntry() const;
181 /// Set whether this request is stored in the cache. If it isn't, regardless
182 /// of whether this request has a non-null mCacheEntry, this imgRequest won't
183 /// try to update or modify the image cache.
184 void SetIsInCache(bool aCacheable);
186 void EvictFromCache();
187 void RemoveFromCache();
189 // Sets properties for this image; will dispatch to main thread if needed.
190 void SetProperties(const nsACString& aContentType,
191 const nsACString& aContentDisposition);
193 nsIProperties* Properties() const { return mProperties; }
195 bool HasConsumers() const;
197 bool ImageAvailable() const;
199 bool IsDeniedCrossSiteCORSRequest() const {
200 return mIsDeniedCrossSiteCORSRequest;
203 bool IsCrossSiteNoCORSRequest() const { return mIsCrossSiteNoCORSRequest; }
205 private:
206 friend class FinishPreparingForNewPartRunnable;
208 virtual ~imgRequest();
210 void FinishPreparingForNewPart(const NewPartResult& aResult);
212 void Cancel(nsresult aStatus);
214 // Update the cache entry size based on the image container.
215 void UpdateCacheEntrySize();
217 /// Returns true if StartDecoding() was called.
218 bool IsDecodeRequested() const;
220 void AdjustPriorityInternal(int32_t aDelta);
222 // Weak reference to parent loader; this request cannot outlive its owner.
223 imgLoader* mLoader;
224 nsCOMPtr<nsIRequest> mRequest;
225 // The original URI we were loaded with. This is the same as the URI we are
226 // keyed on in the cache. We store a string here to avoid off main thread
227 // refcounting issues with nsStandardURL.
228 nsCOMPtr<nsIURI> mURI;
229 // The URI of the resource we ended up loading after all redirects, etc.
230 nsCOMPtr<nsIURI> mFinalURI;
231 // The principal which triggered the load of this image. Generally either
232 // the principal of the document the image is being loaded into, or of the
233 // stylesheet which specified the image to load. Used when validating for
234 // CORS.
235 nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
236 // The principal of this image.
237 nsCOMPtr<nsIPrincipal> mPrincipal;
238 nsCOMPtr<nsIProperties> mProperties;
239 nsCOMPtr<nsIChannel> mChannel;
240 nsCOMPtr<nsIInterfaceRequestor> mPrevChannelSink;
242 nsCOMPtr<nsITimedChannel> mTimedChannel;
244 nsCString mContentType;
246 /* we hold on to this to this so long as we have observers */
247 RefPtr<imgCacheEntry> mCacheEntry;
249 /// The key under which this imgRequest is stored in the image cache.
250 ImageCacheKey mCacheKey;
252 void* mLoadId;
254 /// Raw pointer to the first proxy that was added to this imgRequest. Use only
255 /// pointer comparisons; there's no guarantee this will remain valid.
256 void* mFirstProxy;
258 imgCacheValidator* mValidator;
259 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
260 nsCOMPtr<nsIChannel> mNewRedirectChannel;
262 // The CORS mode (defined in imgIRequest) this image was loaded with. By
263 // default, CORS_NONE.
264 mozilla::CORSMode mCORSMode;
266 // The ReferrerInfo used for this image.
267 nsCOMPtr<nsIReferrerInfo> mReferrerInfo;
269 nsresult mImageErrorCode;
271 // The categories of prioritization strategy that have been requested.
272 uint32_t mBoostCategoriesRequested = 0;
274 // If we've called OnImageAvailable.
275 bool mImageAvailable;
276 bool mIsDeniedCrossSiteCORSRequest;
277 bool mIsCrossSiteNoCORSRequest;
279 mutable mozilla::Mutex mMutex;
281 // Member variables protected by mMutex. Note that *all* flags in our bitfield
282 // are protected by mMutex; if you're adding a new flag that isn'protected, it
283 // must not be a part of this bitfield.
284 RefPtr<ProgressTracker> mProgressTracker MOZ_GUARDED_BY(mMutex);
285 RefPtr<Image> mImage MOZ_GUARDED_BY(mMutex);
286 bool mIsMultiPartChannel : 1 MOZ_GUARDED_BY(mMutex);
287 bool mIsInCache : 1 MOZ_GUARDED_BY(mMutex);
288 bool mDecodeRequested : 1 MOZ_GUARDED_BY(mMutex);
289 bool mNewPartPending : 1 MOZ_GUARDED_BY(mMutex);
290 bool mHadInsecureRedirect : 1 MOZ_GUARDED_BY(mMutex);
291 // The ID of the inner window origin, used for error reporting.
292 uint64_t mInnerWindowId MOZ_GUARDED_BY(mMutex);
295 #endif // mozilla_image_imgRequest_h