Bug 1882593 [wpt PR 44836] - Update wpt metadata, a=testonly
[gecko.git] / image / imgLoader.h
blob7423517925336ae9c5b419ab5e116e9768dda940
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_imgLoader_h
8 #define mozilla_image_imgLoader_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/CORSMode.h"
12 #include "mozilla/Mutex.h"
13 #include "mozilla/EnumSet.h"
14 #include "mozilla/UniquePtr.h"
16 #include "imgILoader.h"
17 #include "imgICache.h"
18 #include "nsWeakReference.h"
19 #include "nsIContentSniffer.h"
20 #include "nsRefPtrHashtable.h"
21 #include "nsTHashSet.h"
22 #include "nsExpirationTracker.h"
23 #include "ImageCacheKey.h"
24 #include "imgRequest.h"
25 #include "nsIProgressEventSink.h"
26 #include "nsIChannel.h"
27 #include "nsIThreadRetargetableStreamListener.h"
28 #include "imgIRequest.h"
30 class imgLoader;
31 class imgRequestProxy;
32 class imgINotificationObserver;
33 class nsILoadGroup;
34 class imgCacheExpirationTracker;
35 class imgMemoryReporter;
37 namespace mozilla {
38 namespace dom {
39 class Document;
40 enum class FetchPriority : uint8_t;
42 } // namespace mozilla
44 class imgCacheEntry {
45 public:
46 NS_INLINE_DECL_REFCOUNTING(imgCacheEntry)
48 imgCacheEntry(imgLoader* loader, imgRequest* request,
49 bool aForcePrincipalCheck);
51 uint32_t GetDataSize() const { return mDataSize; }
52 void SetDataSize(uint32_t aDataSize) {
53 int32_t oldsize = mDataSize;
54 mDataSize = aDataSize;
55 UpdateCache(mDataSize - oldsize);
58 int32_t GetTouchedTime() const { return mTouchedTime; }
59 void SetTouchedTime(int32_t time) {
60 mTouchedTime = time;
61 Touch(/* updateTime = */ false);
64 uint32_t GetLoadTime() const { return mLoadTime; }
66 void UpdateLoadTime();
68 uint32_t GetExpiryTime() const { return mExpiryTime; }
69 void SetExpiryTime(uint32_t aExpiryTime) {
70 mExpiryTime = aExpiryTime;
71 Touch();
74 bool GetMustValidate() const { return mMustValidate; }
75 void SetMustValidate(bool aValidate) {
76 mMustValidate = aValidate;
77 Touch();
80 already_AddRefed<imgRequest> GetRequest() const {
81 RefPtr<imgRequest> req = mRequest;
82 return req.forget();
85 bool Evicted() const { return mEvicted; }
87 nsExpirationState* GetExpirationState() { return &mExpirationState; }
89 bool HasNoProxies() const { return mHasNoProxies; }
91 bool ForcePrincipalCheck() const { return mForcePrincipalCheck; }
93 bool HasNotified() const { return mHasNotified; }
94 void SetHasNotified() {
95 MOZ_ASSERT(!mHasNotified);
96 mHasNotified = true;
99 imgLoader* Loader() const { return mLoader; }
101 private: // methods
102 friend class imgLoader;
103 friend class imgCacheQueue;
104 void Touch(bool updateTime = true);
105 void UpdateCache(int32_t diff = 0);
106 void SetEvicted(bool evict) { mEvicted = evict; }
107 void SetHasNoProxies(bool hasNoProxies);
109 // Private, unimplemented copy constructor.
110 imgCacheEntry(const imgCacheEntry&);
111 ~imgCacheEntry();
113 private: // data
114 imgLoader* mLoader;
115 RefPtr<imgRequest> mRequest;
116 uint32_t mDataSize;
117 int32_t mTouchedTime;
118 uint32_t mLoadTime;
119 uint32_t mExpiryTime;
120 nsExpirationState mExpirationState;
121 bool mMustValidate : 1;
122 bool mEvicted : 1;
123 bool mHasNoProxies : 1;
124 bool mForcePrincipalCheck : 1;
125 bool mHasNotified : 1;
128 #include <vector>
130 #define NS_IMGLOADER_CID \
131 { /* c1354898-e3fe-4602-88a7-c4520c21cb4e */ \
132 0xc1354898, 0xe3fe, 0x4602, { \
133 0x88, 0xa7, 0xc4, 0x52, 0x0c, 0x21, 0xcb, 0x4e \
137 class imgCacheQueue {
138 public:
139 imgCacheQueue();
140 void Remove(imgCacheEntry*);
141 void Push(imgCacheEntry*);
142 void MarkDirty();
143 bool IsDirty();
144 already_AddRefed<imgCacheEntry> Pop();
145 void Refresh();
146 uint32_t GetSize() const;
147 void UpdateSize(int32_t diff);
148 uint32_t GetNumElements() const;
149 bool Contains(imgCacheEntry* aEntry) const;
150 typedef nsTArray<RefPtr<imgCacheEntry>> queueContainer;
151 typedef queueContainer::iterator iterator;
152 typedef queueContainer::const_iterator const_iterator;
154 iterator begin();
155 const_iterator begin() const;
156 iterator end();
157 const_iterator end() const;
159 private:
160 queueContainer mQueue;
161 bool mDirty;
162 uint32_t mSize;
165 enum class AcceptedMimeTypes : uint8_t {
166 IMAGES,
167 IMAGES_AND_DOCUMENTS,
170 class imgLoader final : public imgILoader,
171 public nsIContentSniffer,
172 public imgICache,
173 public nsSupportsWeakReference,
174 public nsIObserver {
175 virtual ~imgLoader();
177 public:
178 using ImageCacheKey = mozilla::image::ImageCacheKey;
179 using imgCacheTable =
180 nsRefPtrHashtable<nsGenericHashKey<ImageCacheKey>, imgCacheEntry>;
181 using imgSet = nsTHashSet<imgRequest*>;
182 using Mutex = mozilla::Mutex;
184 NS_DECL_ISUPPORTS
185 NS_DECL_IMGILOADER
186 NS_DECL_NSICONTENTSNIFFER
187 NS_DECL_IMGICACHE
188 NS_DECL_NSIOBSERVER
191 * Get the normal image loader instance that is used by gecko code, creating
192 * it if necessary.
194 static imgLoader* NormalLoader();
197 * Get the Private Browsing image loader instance that is used by gecko code,
198 * creating it if necessary.
200 static imgLoader* PrivateBrowsingLoader();
203 * Gecko code should use NormalLoader() or PrivateBrowsingLoader() to get the
204 * appropriate image loader.
206 * This constructor is public because the XPCOM module code that creates
207 * instances of "@mozilla.org/image/loader;1" / "@mozilla.org/image/cache;1"
208 * for nsIComponentManager.createInstance()/nsIServiceManager.getService()
209 * calls (now only made by add-ons) needs access to it.
211 * XXX We would like to get rid of the nsIServiceManager.getService (and
212 * nsIComponentManager.createInstance) method of creating imgLoader objects,
213 * but there are add-ons that are still using it. These add-ons don't
214 * actually do anything useful with the loaders that they create since nobody
215 * who creates an imgLoader using this method actually QIs to imgILoader and
216 * loads images. They all just QI to imgICache and either call clearCache()
217 * or findEntryProperties(). Since they're doing this on an imgLoader that
218 * has never loaded images, these calls are useless. It seems likely that
219 * the code that is doing this is just legacy code left over from a time when
220 * there was only one imgLoader instance for the entire process. (Nowadays
221 * the correct method to get an imgILoader/imgICache is to call
222 * imgITools::getImgCacheForDocument/imgITools::getImgLoaderForDocument.)
223 * All the same, even though what these add-ons are doing is a no-op,
224 * removing the nsIServiceManager.getService method of creating/getting an
225 * imgLoader objects would cause an exception in these add-ons that could
226 * break things.
228 imgLoader();
229 nsresult Init();
231 bool IsImageAvailable(nsIURI*, nsIPrincipal* aTriggeringPrincipal,
232 mozilla::CORSMode, mozilla::dom::Document*);
234 [[nodiscard]] nsresult LoadImage(
235 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
236 nsIPrincipal* aLoadingPrincipal, uint64_t aRequestContextID,
237 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
238 nsINode* aContext, mozilla::dom::Document* aLoadingDocument,
239 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
240 nsContentPolicyType aContentPolicyType, const nsAString& initiatorType,
241 bool aUseUrgentStartForChannel, bool aLinkPreload,
242 uint64_t aEarlyHintPreloaderId,
243 mozilla::dom::FetchPriority aFetchPriority, imgRequestProxy** _retval);
245 [[nodiscard]] nsresult LoadImageWithChannel(
246 nsIChannel* channel, imgINotificationObserver* aObserver,
247 mozilla::dom::Document* aLoadingDocument, nsIStreamListener** listener,
248 imgRequestProxy** _retval);
250 static nsresult GetMimeTypeFromContent(const char* aContents,
251 uint32_t aLength,
252 nsACString& aContentType);
255 * Returns true if the given mime type may be interpreted as an image.
257 * Some MIME types may be interpreted as both images and documents. (At the
258 * moment only "image/svg+xml" falls into this category, but there may be more
259 * in the future.) Callers which want this function to return true for such
260 * MIME types should pass AcceptedMimeTypes::IMAGES_AND_DOCUMENTS for
261 * @aAccept.
263 * @param aMimeType The MIME type to evaluate.
264 * @param aAcceptedMimeTypes Which kinds of MIME types to treat as images.
266 static bool SupportImageWithMimeType(
267 const nsACString&, AcceptedMimeTypes aAccept = AcceptedMimeTypes::IMAGES);
269 static void GlobalInit(); // for use by the factory
270 static void Shutdown(); // for use by the factory
271 static void ShutdownMemoryReporter();
273 enum class ClearOption {
274 ChromeOnly,
275 UnusedOnly,
277 using ClearOptions = mozilla::EnumSet<ClearOption>;
278 nsresult ClearImageCache(ClearOptions = {});
279 void MinimizeCache() { ClearImageCache({ClearOption::UnusedOnly}); }
281 nsresult InitCache();
283 bool RemoveFromCache(const ImageCacheKey& aKey);
285 // Enumeration describing if a given entry is in the cache queue or not.
286 // There are some cases we know the entry is definitely not in the queue.
287 enum class QueueState { MaybeExists, AlreadyRemoved };
289 bool RemoveFromCache(imgCacheEntry* entry,
290 QueueState aQueueState = QueueState::MaybeExists);
292 bool PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* aEntry);
294 void AddToUncachedImages(imgRequest* aRequest);
295 void RemoveFromUncachedImages(imgRequest* aRequest);
297 // Returns true if we should prefer evicting cache entry |two| over cache
298 // entry |one|.
299 // This mixes units in the worst way, but provides reasonable results.
300 inline static bool CompareCacheEntries(const RefPtr<imgCacheEntry>& one,
301 const RefPtr<imgCacheEntry>& two) {
302 if (!one) {
303 return false;
305 if (!two) {
306 return true;
309 const double sizeweight = 1.0 - sCacheTimeWeight;
311 // We want large, old images to be evicted first (depending on their
312 // relative weights). Since a larger time is actually newer, we subtract
313 // time's weight, so an older image has a larger weight.
314 double oneweight = double(one->GetDataSize()) * sizeweight -
315 double(one->GetTouchedTime()) * sCacheTimeWeight;
316 double twoweight = double(two->GetDataSize()) * sizeweight -
317 double(two->GetTouchedTime()) * sCacheTimeWeight;
319 return oneweight < twoweight;
322 void VerifyCacheSizes();
324 nsresult RemoveEntriesInternal(nsIPrincipal* aPrincipal,
325 const nsACString* aBaseDomain);
327 // The image loader maintains a hash table of all imgCacheEntries. However,
328 // only some of them will be evicted from the cache: those who have no
329 // imgRequestProxies watching their imgRequests.
331 // Once an imgRequest has no imgRequestProxies, it should notify us by
332 // calling HasNoObservers(), and null out its cache entry pointer.
334 // Upon having a proxy start observing again, it should notify us by calling
335 // HasObservers(). The request's cache entry will be re-set before this
336 // happens, by calling imgRequest::SetCacheEntry() when an entry with no
337 // observers is re-requested.
338 bool SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry);
339 bool SetHasProxies(imgRequest* aRequest);
341 private: // methods
342 static already_AddRefed<imgLoader> CreateImageLoader();
344 bool ValidateEntry(imgCacheEntry* aEntry, nsIURI* aURI,
345 nsIURI* aInitialDocumentURI,
346 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
347 imgINotificationObserver* aObserver,
348 mozilla::dom::Document* aLoadingDocument,
349 nsLoadFlags aLoadFlags,
350 nsContentPolicyType aLoadPolicyType,
351 bool aCanMakeNewChannel, bool* aNewChannelCreated,
352 imgRequestProxy** aProxyRequest,
353 nsIPrincipal* aTriggeringPrincipal, mozilla::CORSMode,
354 bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
355 mozilla::dom::FetchPriority aFetchPriority);
357 bool ValidateRequestWithNewChannel(
358 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
359 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
360 imgINotificationObserver* aObserver,
361 mozilla::dom::Document* aLoadingDocument, uint64_t aInnerWindowId,
362 nsLoadFlags aLoadFlags, nsContentPolicyType aContentPolicyType,
363 imgRequestProxy** aProxyRequest, nsIPrincipal* aLoadingPrincipal,
364 mozilla::CORSMode, bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
365 mozilla::dom::FetchPriority aFetchPriority, bool* aNewChannelCreated);
367 void NotifyObserversForCachedImage(
368 imgCacheEntry* aEntry, imgRequest* request, nsIURI* aURI,
369 nsIReferrerInfo* aReferrerInfo, mozilla::dom::Document* aLoadingDocument,
370 nsIPrincipal* aTriggeringPrincipal, mozilla::CORSMode,
371 uint64_t aEarlyHintPreloaderId,
372 mozilla::dom::FetchPriority aFetchPriority);
373 // aURI may be different from imgRequest's URI in the case of blob URIs, as we
374 // can share requests with different URIs.
375 nsresult CreateNewProxyForRequest(imgRequest* aRequest, nsIURI* aURI,
376 nsILoadGroup* aLoadGroup,
377 mozilla::dom::Document* aLoadingDocument,
378 imgINotificationObserver* aObserver,
379 nsLoadFlags aLoadFlags,
380 imgRequestProxy** _retval);
382 nsresult EvictEntries(bool aChromeOnly);
384 void CacheEntriesChanged(int32_t aSizeDiff);
385 void CheckCacheLimits();
387 private: // data
388 friend class imgCacheEntry;
389 friend class imgMemoryReporter;
391 imgCacheTable mCache;
392 imgCacheQueue mCacheQueue;
394 // Hash set of every imgRequest for this loader that isn't in mCache or
395 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
396 // mUncachedImages should be every imgRequest that is alive. These are weak
397 // pointers so we rely on the imgRequest destructor to remove itself.
398 imgSet mUncachedImages MOZ_GUARDED_BY(mUncachedImagesMutex);
399 // The imgRequest can have refs to them held on non-main thread, so we need
400 // a mutex because we modify the uncached images set from the imgRequest
401 // destructor.
402 Mutex mUncachedImagesMutex;
404 static double sCacheTimeWeight;
405 static uint32_t sCacheMaxSize;
406 static imgMemoryReporter* sMemReporter;
408 mozilla::UniquePtr<imgCacheExpirationTracker> mCacheTracker;
409 bool mRespectPrivacy;
413 * proxy stream listener class used to handle multipart/x-mixed-replace
416 #include "nsCOMPtr.h"
417 #include "nsIStreamListener.h"
418 #include "nsIThreadRetargetableStreamListener.h"
420 class ProxyListener : public nsIThreadRetargetableStreamListener {
421 public:
422 explicit ProxyListener(nsIStreamListener* dest);
424 /* additional members */
425 NS_DECL_THREADSAFE_ISUPPORTS
426 NS_DECL_NSISTREAMLISTENER
427 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
428 NS_DECL_NSIREQUESTOBSERVER
430 private:
431 virtual ~ProxyListener();
433 nsCOMPtr<nsIStreamListener> mDestListener;
437 * A class that implements nsIProgressEventSink and forwards all calls to it to
438 * the original notification callbacks of the channel. Also implements
439 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
440 * and forwards everything else to the channel's notification callbacks.
442 class nsProgressNotificationProxy final : public nsIProgressEventSink,
443 public nsIChannelEventSink,
444 public nsIInterfaceRequestor {
445 public:
446 nsProgressNotificationProxy(nsIChannel* channel, imgIRequest* proxy)
447 : mImageRequest(proxy) {
448 channel->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks));
451 NS_DECL_ISUPPORTS
452 NS_DECL_NSIPROGRESSEVENTSINK
453 NS_DECL_NSICHANNELEVENTSINK
454 NS_DECL_NSIINTERFACEREQUESTOR
455 private:
456 ~nsProgressNotificationProxy() = default;
458 nsCOMPtr<nsIInterfaceRequestor> mOriginalCallbacks;
459 nsCOMPtr<nsIRequest> mImageRequest;
463 * validate checker
466 #include "nsCOMArray.h"
468 class imgCacheValidator : public nsIThreadRetargetableStreamListener,
469 public nsIChannelEventSink,
470 public nsIInterfaceRequestor,
471 public nsIAsyncVerifyRedirectCallback {
472 public:
473 imgCacheValidator(nsProgressNotificationProxy* progress, imgLoader* loader,
474 imgRequest* aRequest, mozilla::dom::Document* aDocument,
475 uint64_t aInnerWindowId,
476 bool forcePrincipalCheckForCacheEntry);
478 void AddProxy(imgRequestProxy* aProxy);
479 void RemoveProxy(imgRequestProxy* aProxy);
481 NS_DECL_THREADSAFE_ISUPPORTS
482 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
483 NS_DECL_NSISTREAMLISTENER
484 NS_DECL_NSIREQUESTOBSERVER
485 NS_DECL_NSICHANNELEVENTSINK
486 NS_DECL_NSIINTERFACEREQUESTOR
487 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
489 private:
490 void UpdateProxies(bool aCancelRequest, bool aSyncNotify);
491 virtual ~imgCacheValidator();
493 nsCOMPtr<nsIStreamListener> mDestListener;
494 RefPtr<nsProgressNotificationProxy> mProgressProxy;
495 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
496 nsCOMPtr<nsIChannel> mRedirectChannel;
498 RefPtr<imgRequest> mRequest;
499 AutoTArray<RefPtr<imgRequestProxy>, 4> mProxies;
501 RefPtr<imgRequest> mNewRequest;
502 RefPtr<imgCacheEntry> mNewEntry;
504 RefPtr<mozilla::dom::Document> mDocument;
505 uint64_t mInnerWindowId;
507 imgLoader* mImgLoader;
509 bool mHadInsecureRedirect;
512 #endif // mozilla_image_imgLoader_h