Bug 1516095 [wpt PR 14643] - Update wpt metadata, a=testonly
[gecko.git] / image / imgRequest.h
blob56bf9e670537c93e0b304cd97b0ee4cc51a37b3f
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 "mozilla/net/ReferrerPolicy.h"
23 #include "ImageCacheKey.h"
25 class imgCacheValidator;
26 class imgLoader;
27 class imgRequestProxy;
28 class imgCacheEntry;
29 class nsIApplicationCache;
30 class nsIProperties;
31 class nsIRequest;
32 class nsITimedChannel;
33 class nsIURI;
35 namespace mozilla {
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::net::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 MOZ_MUST_USE nsresult Init(nsIURI* aURI, nsIURI* aFinalURI,
66 bool aHadInsecureRedirect, nsIRequest* aRequest,
67 nsIChannel* aChannel, imgCacheEntry* aCacheEntry,
68 nsISupports* aCX,
69 nsIPrincipal* aTriggeringPrincipal,
70 int32_t aCORSMode, ReferrerPolicy aReferrerPolicy);
72 void ClearLoader();
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.
88 void ContinueEvict();
90 // Request that we start decoding the image as soon as data becomes available.
91 void StartDecoding();
93 inline uint64_t InnerWindowID() const { return mInnerWindowId; }
95 // Set the cache validation information (expiry time, whether we must
96 // validate, etc) on the cache entry based on the request information.
97 // If this function is called multiple times, the information set earliest
98 // wins.
99 static void SetCacheValidation(imgCacheEntry* aEntry, nsIRequest* aRequest);
101 // Check if application cache of the original load is different from
102 // application cache of the new load. Also lack of application cache
103 // on one of the loads is considered a change of a loading cache since
104 // HTTP cache may contain a different data then app cache.
105 bool CacheChanged(nsIRequest* aNewRequest);
107 bool GetMultipart() const;
109 // Returns whether we went through an insecure (non-HTTPS) redirect at some
110 // point during loading. This does not consider the final URI.
111 bool HadInsecureRedirect() const;
113 // The CORS mode for which we loaded this image.
114 int32_t GetCORSMode() const { return mCORSMode; }
116 // The Referrer Policy in effect when loading this image.
117 ReferrerPolicy GetReferrerPolicy() const { return mReferrerPolicy; }
119 // The principal for the document that loaded this image. Used when trying to
120 // validate a CORS image load.
121 already_AddRefed<nsIPrincipal> GetTriggeringPrincipal() const {
122 nsCOMPtr<nsIPrincipal> principal = mTriggeringPrincipal;
123 return principal.forget();
126 // Return the ProgressTracker associated with this imgRequest. It may live
127 // in |mProgressTracker| or in |mImage.mProgressTracker|, depending on whether
128 // mImage has been instantiated yet.
129 already_AddRefed<ProgressTracker> GetProgressTracker() const;
131 /// Returns the Image associated with this imgRequest, if it's ready.
132 already_AddRefed<Image> GetImage() const;
134 // Get the current principal of the image. No AddRefing.
135 inline nsIPrincipal* GetPrincipal() const { return mPrincipal.get(); }
137 /// Get the ImageCacheKey associated with this request.
138 const ImageCacheKey& CacheKey() const { return mCacheKey; }
140 // Resize the cache entry to 0 if it exists
141 void ResetCacheEntry();
143 // OK to use on any thread.
144 nsresult GetURI(nsIURI** aURI);
145 nsresult GetFinalURI(nsIURI** aURI);
146 bool IsScheme(const char* aScheme) const;
147 bool IsChrome() const;
148 bool IsData() const;
150 nsresult GetImageErrorCode(void);
152 /// Returns a non-owning pointer to this imgRequest's MIME type.
153 const char* GetMimeType() const { return mContentType.get(); }
155 /// @return the priority of the underlying network request, or
156 /// PRIORITY_NORMAL if it doesn't support nsISupportsPriority.
157 int32_t Priority() const;
159 /// Adjust the priority of the underlying network request by @aDelta on behalf
160 /// of @aProxy.
161 void AdjustPriority(imgRequestProxy* aProxy, int32_t aDelta);
163 void BoostPriority(uint32_t aCategory);
165 /// Returns a weak pointer to the underlying request.
166 nsIRequest* GetRequest() const { return mRequest; }
168 nsITimedChannel* GetTimedChannel() const { return mTimedChannel; }
170 imgCacheValidator* GetValidator() const { return mValidator; }
171 void SetValidator(imgCacheValidator* aValidator) { mValidator = aValidator; }
173 void* LoadId() const { return mLoadId; }
174 void SetLoadId(void* aLoadId) { mLoadId = aLoadId; }
176 /// Reset the cache entry after we've dropped our reference to it. Used by
177 /// imgLoader when our cache entry is re-requested after we've dropped our
178 /// reference to it.
179 void SetCacheEntry(imgCacheEntry* aEntry);
181 /// Returns whether we've got a reference to the cache entry.
182 bool HasCacheEntry() const;
184 /// Set whether this request is stored in the cache. If it isn't, regardless
185 /// of whether this request has a non-null mCacheEntry, this imgRequest won't
186 /// try to update or modify the image cache.
187 void SetIsInCache(bool aCacheable);
189 void EvictFromCache();
190 void RemoveFromCache();
192 // Sets properties for this image; will dispatch to main thread if needed.
193 void SetProperties(const nsACString& aContentType,
194 const nsACString& aContentDisposition);
196 nsIProperties* Properties() const { return mProperties; }
198 bool HasConsumers() const;
200 private:
201 friend class FinishPreparingForNewPartRunnable;
203 virtual ~imgRequest();
205 void FinishPreparingForNewPart(const NewPartResult& aResult);
207 void Cancel(nsresult aStatus);
209 // Update the cache entry size based on the image container.
210 void UpdateCacheEntrySize();
212 /// Returns true if StartDecoding() was called.
213 bool IsDecodeRequested() const;
215 void AdjustPriorityInternal(int32_t aDelta);
217 // Weak reference to parent loader; this request cannot outlive its owner.
218 imgLoader* mLoader;
219 nsCOMPtr<nsIRequest> mRequest;
220 // The original URI we were loaded with. This is the same as the URI we are
221 // keyed on in the cache. We store a string here to avoid off main thread
222 // refcounting issues with nsStandardURL.
223 nsCOMPtr<nsIURI> mURI;
224 // The URI of the resource we ended up loading after all redirects, etc.
225 nsCOMPtr<nsIURI> mFinalURI;
226 // The principal which triggered the load of this image. Generally either
227 // the principal of the document the image is being loaded into, or of the
228 // stylesheet which specified the image to load. Used when validating for
229 // CORS.
230 nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
231 // The principal of this image.
232 nsCOMPtr<nsIPrincipal> mPrincipal;
233 nsCOMPtr<nsIProperties> mProperties;
234 nsCOMPtr<nsIChannel> mChannel;
235 nsCOMPtr<nsIInterfaceRequestor> mPrevChannelSink;
236 nsCOMPtr<nsIApplicationCache> mApplicationCache;
238 nsCOMPtr<nsITimedChannel> mTimedChannel;
240 nsCString mContentType;
242 /* we hold on to this to this so long as we have observers */
243 RefPtr<imgCacheEntry> mCacheEntry;
245 /// The key under which this imgRequest is stored in the image cache.
246 ImageCacheKey mCacheKey;
248 void* mLoadId;
250 /// Raw pointer to the first proxy that was added to this imgRequest. Use only
251 /// pointer comparisons; there's no guarantee this will remain valid.
252 void* mFirstProxy;
254 imgCacheValidator* mValidator;
255 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
256 nsCOMPtr<nsIChannel> mNewRedirectChannel;
258 // The ID of the inner window origin, used for error reporting.
259 uint64_t mInnerWindowId;
261 // The CORS mode (defined in imgIRequest) this image was loaded with. By
262 // default, imgIRequest::CORS_NONE.
263 int32_t mCORSMode;
265 // The Referrer Policy (defined in ReferrerPolicy.h) used for this image.
266 ReferrerPolicy mReferrerPolicy;
268 nsresult mImageErrorCode;
270 // The categories of prioritization strategy that have been requested.
271 uint32_t mBoostCategoriesRequested = 0;
273 mutable mozilla::Mutex mMutex;
275 // Member variables protected by mMutex. Note that *all* flags in our bitfield
276 // are protected by mMutex; if you're adding a new flag that isn'protected, it
277 // must not be a part of this bitfield.
278 RefPtr<ProgressTracker> mProgressTracker;
279 RefPtr<Image> mImage;
280 bool mIsMultiPartChannel : 1;
281 bool mIsInCache : 1;
282 bool mDecodeRequested : 1;
283 bool mNewPartPending : 1;
284 bool mHadInsecureRedirect : 1;
287 #endif // mozilla_image_imgRequest_h