Bug 1834520 [wpt PR 40165] - [scroll-animations] Support ranges for non-view timeline...
[gecko.git] / image / imgLoader.h
blob168b473333662db8816a3ec5ce79ba31c63ce93a
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;
41 } // namespace mozilla
43 class imgCacheEntry {
44 public:
45 imgCacheEntry(imgLoader* loader, imgRequest* request,
46 bool aForcePrincipalCheck);
47 ~imgCacheEntry();
49 nsrefcnt AddRef() {
50 MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt");
51 NS_ASSERT_OWNINGTHREAD(imgCacheEntry);
52 ++mRefCnt;
53 NS_LOG_ADDREF(this, mRefCnt, "imgCacheEntry", sizeof(*this));
54 return mRefCnt;
57 nsrefcnt Release() {
58 MOZ_ASSERT(0 != mRefCnt, "dup release");
59 NS_ASSERT_OWNINGTHREAD(imgCacheEntry);
60 --mRefCnt;
61 NS_LOG_RELEASE(this, mRefCnt, "imgCacheEntry");
62 if (mRefCnt == 0) {
63 mRefCnt = 1; /* stabilize */
64 delete this;
65 return 0;
67 return mRefCnt;
70 uint32_t GetDataSize() const { return mDataSize; }
71 void SetDataSize(uint32_t aDataSize) {
72 int32_t oldsize = mDataSize;
73 mDataSize = aDataSize;
74 UpdateCache(mDataSize - oldsize);
77 int32_t GetTouchedTime() const { return mTouchedTime; }
78 void SetTouchedTime(int32_t time) {
79 mTouchedTime = time;
80 Touch(/* updateTime = */ false);
83 uint32_t GetLoadTime() const { return mLoadTime; }
85 void UpdateLoadTime();
87 uint32_t GetExpiryTime() const { return mExpiryTime; }
88 void SetExpiryTime(uint32_t aExpiryTime) {
89 mExpiryTime = aExpiryTime;
90 Touch();
93 bool GetMustValidate() const { return mMustValidate; }
94 void SetMustValidate(bool aValidate) {
95 mMustValidate = aValidate;
96 Touch();
99 already_AddRefed<imgRequest> GetRequest() const {
100 RefPtr<imgRequest> req = mRequest;
101 return req.forget();
104 bool Evicted() const { return mEvicted; }
106 nsExpirationState* GetExpirationState() { return &mExpirationState; }
108 bool HasNoProxies() const { return mHasNoProxies; }
110 bool ForcePrincipalCheck() const { return mForcePrincipalCheck; }
112 bool HasNotified() const { return mHasNotified; }
113 void SetHasNotified() {
114 MOZ_ASSERT(!mHasNotified);
115 mHasNotified = true;
118 imgLoader* Loader() const { return mLoader; }
120 private: // methods
121 friend class imgLoader;
122 friend class imgCacheQueue;
123 void Touch(bool updateTime = true);
124 void UpdateCache(int32_t diff = 0);
125 void SetEvicted(bool evict) { mEvicted = evict; }
126 void SetHasNoProxies(bool hasNoProxies);
128 // Private, unimplemented copy constructor.
129 imgCacheEntry(const imgCacheEntry&);
131 private: // data
132 nsAutoRefCnt mRefCnt;
133 NS_DECL_OWNINGTHREAD
135 imgLoader* mLoader;
136 RefPtr<imgRequest> mRequest;
137 uint32_t mDataSize;
138 int32_t mTouchedTime;
139 uint32_t mLoadTime;
140 uint32_t mExpiryTime;
141 nsExpirationState mExpirationState;
142 bool mMustValidate : 1;
143 bool mEvicted : 1;
144 bool mHasNoProxies : 1;
145 bool mForcePrincipalCheck : 1;
146 bool mHasNotified : 1;
149 #include <vector>
151 #define NS_IMGLOADER_CID \
152 { /* c1354898-e3fe-4602-88a7-c4520c21cb4e */ \
153 0xc1354898, 0xe3fe, 0x4602, { \
154 0x88, 0xa7, 0xc4, 0x52, 0x0c, 0x21, 0xcb, 0x4e \
158 class imgCacheQueue {
159 public:
160 imgCacheQueue();
161 void Remove(imgCacheEntry*);
162 void Push(imgCacheEntry*);
163 void MarkDirty();
164 bool IsDirty();
165 already_AddRefed<imgCacheEntry> Pop();
166 void Refresh();
167 uint32_t GetSize() const;
168 void UpdateSize(int32_t diff);
169 uint32_t GetNumElements() const;
170 bool Contains(imgCacheEntry* aEntry) const;
171 typedef nsTArray<RefPtr<imgCacheEntry>> queueContainer;
172 typedef queueContainer::iterator iterator;
173 typedef queueContainer::const_iterator const_iterator;
175 iterator begin();
176 const_iterator begin() const;
177 iterator end();
178 const_iterator end() const;
180 private:
181 queueContainer mQueue;
182 bool mDirty;
183 uint32_t mSize;
186 enum class AcceptedMimeTypes : uint8_t {
187 IMAGES,
188 IMAGES_AND_DOCUMENTS,
191 class imgLoader final : public imgILoader,
192 public nsIContentSniffer,
193 public imgICache,
194 public nsSupportsWeakReference,
195 public nsIObserver {
196 virtual ~imgLoader();
198 public:
199 using ImageCacheKey = mozilla::image::ImageCacheKey;
200 using imgCacheTable =
201 nsRefPtrHashtable<nsGenericHashKey<ImageCacheKey>, imgCacheEntry>;
202 using imgSet = nsTHashSet<imgRequest*>;
203 using Mutex = mozilla::Mutex;
205 NS_DECL_ISUPPORTS
206 NS_DECL_IMGILOADER
207 NS_DECL_NSICONTENTSNIFFER
208 NS_DECL_IMGICACHE
209 NS_DECL_NSIOBSERVER
212 * Get the normal image loader instance that is used by gecko code, creating
213 * it if necessary.
215 static imgLoader* NormalLoader();
218 * Get the Private Browsing image loader instance that is used by gecko code,
219 * creating it if necessary.
221 static imgLoader* PrivateBrowsingLoader();
224 * Gecko code should use NormalLoader() or PrivateBrowsingLoader() to get the
225 * appropriate image loader.
227 * This constructor is public because the XPCOM module code that creates
228 * instances of "@mozilla.org/image/loader;1" / "@mozilla.org/image/cache;1"
229 * for nsIComponentManager.createInstance()/nsIServiceManager.getService()
230 * calls (now only made by add-ons) needs access to it.
232 * XXX We would like to get rid of the nsIServiceManager.getService (and
233 * nsIComponentManager.createInstance) method of creating imgLoader objects,
234 * but there are add-ons that are still using it. These add-ons don't
235 * actually do anything useful with the loaders that they create since nobody
236 * who creates an imgLoader using this method actually QIs to imgILoader and
237 * loads images. They all just QI to imgICache and either call clearCache()
238 * or findEntryProperties(). Since they're doing this on an imgLoader that
239 * has never loaded images, these calls are useless. It seems likely that
240 * the code that is doing this is just legacy code left over from a time when
241 * there was only one imgLoader instance for the entire process. (Nowadays
242 * the correct method to get an imgILoader/imgICache is to call
243 * imgITools::getImgCacheForDocument/imgITools::getImgLoaderForDocument.)
244 * All the same, even though what these add-ons are doing is a no-op,
245 * removing the nsIServiceManager.getService method of creating/getting an
246 * imgLoader objects would cause an exception in these add-ons that could
247 * break things.
249 imgLoader();
250 nsresult Init();
252 bool IsImageAvailable(nsIURI*, nsIPrincipal* aTriggeringPrincipal,
253 mozilla::CORSMode, mozilla::dom::Document*);
255 [[nodiscard]] nsresult LoadImage(
256 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
257 nsIPrincipal* aLoadingPrincipal, uint64_t aRequestContextID,
258 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
259 nsINode* aContext, mozilla::dom::Document* aLoadingDocument,
260 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
261 nsContentPolicyType aContentPolicyType, const nsAString& initiatorType,
262 bool aUseUrgentStartForChannel, bool aLinkPreload,
263 uint64_t aEarlyHintPreloaderId, imgRequestProxy** _retval);
265 [[nodiscard]] nsresult LoadImageWithChannel(
266 nsIChannel* channel, imgINotificationObserver* aObserver,
267 mozilla::dom::Document* aLoadingDocument, nsIStreamListener** listener,
268 imgRequestProxy** _retval);
270 static nsresult GetMimeTypeFromContent(const char* aContents,
271 uint32_t aLength,
272 nsACString& aContentType);
275 * Returns true if the given mime type may be interpreted as an image.
277 * Some MIME types may be interpreted as both images and documents. (At the
278 * moment only "image/svg+xml" falls into this category, but there may be more
279 * in the future.) Callers which want this function to return true for such
280 * MIME types should pass AcceptedMimeTypes::IMAGES_AND_DOCUMENTS for
281 * @aAccept.
283 * @param aMimeType The MIME type to evaluate.
284 * @param aAcceptedMimeTypes Which kinds of MIME types to treat as images.
286 static bool SupportImageWithMimeType(
287 const nsACString&, AcceptedMimeTypes aAccept = AcceptedMimeTypes::IMAGES);
289 static void GlobalInit(); // for use by the factory
290 static void Shutdown(); // for use by the factory
291 static void ShutdownMemoryReporter();
293 enum class ClearOption {
294 ChromeOnly,
295 UnusedOnly,
297 using ClearOptions = mozilla::EnumSet<ClearOption>;
298 nsresult ClearImageCache(ClearOptions = {});
299 void MinimizeCache() { ClearImageCache({ClearOption::UnusedOnly}); }
301 nsresult InitCache();
303 bool RemoveFromCache(const ImageCacheKey& aKey);
305 // Enumeration describing if a given entry is in the cache queue or not.
306 // There are some cases we know the entry is definitely not in the queue.
307 enum class QueueState { MaybeExists, AlreadyRemoved };
309 bool RemoveFromCache(imgCacheEntry* entry,
310 QueueState aQueueState = QueueState::MaybeExists);
312 bool PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* aEntry);
314 void AddToUncachedImages(imgRequest* aRequest);
315 void RemoveFromUncachedImages(imgRequest* aRequest);
317 // Returns true if we should prefer evicting cache entry |two| over cache
318 // entry |one|.
319 // This mixes units in the worst way, but provides reasonable results.
320 inline static bool CompareCacheEntries(const RefPtr<imgCacheEntry>& one,
321 const RefPtr<imgCacheEntry>& two) {
322 if (!one) {
323 return false;
325 if (!two) {
326 return true;
329 const double sizeweight = 1.0 - sCacheTimeWeight;
331 // We want large, old images to be evicted first (depending on their
332 // relative weights). Since a larger time is actually newer, we subtract
333 // time's weight, so an older image has a larger weight.
334 double oneweight = double(one->GetDataSize()) * sizeweight -
335 double(one->GetTouchedTime()) * sCacheTimeWeight;
336 double twoweight = double(two->GetDataSize()) * sizeweight -
337 double(two->GetTouchedTime()) * sCacheTimeWeight;
339 return oneweight < twoweight;
342 void VerifyCacheSizes();
344 nsresult RemoveEntriesInternal(nsIPrincipal* aPrincipal,
345 const nsACString* aBaseDomain);
347 // The image loader maintains a hash table of all imgCacheEntries. However,
348 // only some of them will be evicted from the cache: those who have no
349 // imgRequestProxies watching their imgRequests.
351 // Once an imgRequest has no imgRequestProxies, it should notify us by
352 // calling HasNoObservers(), and null out its cache entry pointer.
354 // Upon having a proxy start observing again, it should notify us by calling
355 // HasObservers(). The request's cache entry will be re-set before this
356 // happens, by calling imgRequest::SetCacheEntry() when an entry with no
357 // observers is re-requested.
358 bool SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry);
359 bool SetHasProxies(imgRequest* aRequest);
361 private: // methods
362 static already_AddRefed<imgLoader> CreateImageLoader();
364 bool ValidateEntry(imgCacheEntry* aEntry, nsIURI* aURI,
365 nsIURI* aInitialDocumentURI,
366 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
367 imgINotificationObserver* aObserver,
368 mozilla::dom::Document* aLoadingDocument,
369 nsLoadFlags aLoadFlags,
370 nsContentPolicyType aLoadPolicyType,
371 bool aCanMakeNewChannel, bool* aNewChannelCreated,
372 imgRequestProxy** aProxyRequest,
373 nsIPrincipal* aTriggeringPrincipal, mozilla::CORSMode,
374 bool aLinkPreload, uint64_t aEarlyHintPreloaderId);
376 bool ValidateRequestWithNewChannel(
377 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
378 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
379 imgINotificationObserver* aObserver,
380 mozilla::dom::Document* aLoadingDocument, uint64_t aInnerWindowId,
381 nsLoadFlags aLoadFlags, nsContentPolicyType aContentPolicyType,
382 imgRequestProxy** aProxyRequest, nsIPrincipal* aLoadingPrincipal,
383 mozilla::CORSMode, bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
384 bool* aNewChannelCreated);
386 void NotifyObserversForCachedImage(imgCacheEntry* aEntry, imgRequest* request,
387 nsIURI* aURI,
388 nsIReferrerInfo* aReferrerInfo,
389 mozilla::dom::Document* aLoadingDocument,
390 nsIPrincipal* aLoadingPrincipal,
391 mozilla::CORSMode,
392 uint64_t aEarlyHintPreloaderId);
393 // aURI may be different from imgRequest's URI in the case of blob URIs, as we
394 // can share requests with different URIs.
395 nsresult CreateNewProxyForRequest(imgRequest* aRequest, nsIURI* aURI,
396 nsILoadGroup* aLoadGroup,
397 mozilla::dom::Document* aLoadingDocument,
398 imgINotificationObserver* aObserver,
399 nsLoadFlags aLoadFlags,
400 imgRequestProxy** _retval);
402 nsresult EvictEntries(bool aChromeOnly);
404 void CacheEntriesChanged(int32_t aSizeDiff);
405 void CheckCacheLimits();
407 private: // data
408 friend class imgCacheEntry;
409 friend class imgMemoryReporter;
411 imgCacheTable mCache;
412 imgCacheQueue mCacheQueue;
414 // Hash set of every imgRequest for this loader that isn't in mCache or
415 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
416 // mUncachedImages should be every imgRequest that is alive. These are weak
417 // pointers so we rely on the imgRequest destructor to remove itself.
418 imgSet mUncachedImages MOZ_GUARDED_BY(mUncachedImagesMutex);
419 // The imgRequest can have refs to them held on non-main thread, so we need
420 // a mutex because we modify the uncached images set from the imgRequest
421 // destructor.
422 Mutex mUncachedImagesMutex;
424 static double sCacheTimeWeight;
425 static uint32_t sCacheMaxSize;
426 static imgMemoryReporter* sMemReporter;
428 mozilla::UniquePtr<imgCacheExpirationTracker> mCacheTracker;
429 bool mRespectPrivacy;
433 * proxy stream listener class used to handle multipart/x-mixed-replace
436 #include "nsCOMPtr.h"
437 #include "nsIStreamListener.h"
438 #include "nsIThreadRetargetableStreamListener.h"
440 class ProxyListener : public nsIStreamListener,
441 public nsIThreadRetargetableStreamListener {
442 public:
443 explicit ProxyListener(nsIStreamListener* dest);
445 /* additional members */
446 NS_DECL_THREADSAFE_ISUPPORTS
447 NS_DECL_NSISTREAMLISTENER
448 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
449 NS_DECL_NSIREQUESTOBSERVER
451 private:
452 virtual ~ProxyListener();
454 nsCOMPtr<nsIStreamListener> mDestListener;
458 * A class that implements nsIProgressEventSink and forwards all calls to it to
459 * the original notification callbacks of the channel. Also implements
460 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
461 * and forwards everything else to the channel's notification callbacks.
463 class nsProgressNotificationProxy final : public nsIProgressEventSink,
464 public nsIChannelEventSink,
465 public nsIInterfaceRequestor {
466 public:
467 nsProgressNotificationProxy(nsIChannel* channel, imgIRequest* proxy)
468 : mImageRequest(proxy) {
469 channel->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks));
472 NS_DECL_ISUPPORTS
473 NS_DECL_NSIPROGRESSEVENTSINK
474 NS_DECL_NSICHANNELEVENTSINK
475 NS_DECL_NSIINTERFACEREQUESTOR
476 private:
477 ~nsProgressNotificationProxy() = default;
479 nsCOMPtr<nsIInterfaceRequestor> mOriginalCallbacks;
480 nsCOMPtr<nsIRequest> mImageRequest;
484 * validate checker
487 #include "nsCOMArray.h"
489 class imgCacheValidator : public nsIStreamListener,
490 public nsIThreadRetargetableStreamListener,
491 public nsIChannelEventSink,
492 public nsIInterfaceRequestor,
493 public nsIAsyncVerifyRedirectCallback {
494 public:
495 imgCacheValidator(nsProgressNotificationProxy* progress, imgLoader* loader,
496 imgRequest* aRequest, mozilla::dom::Document* aDocument,
497 uint64_t aInnerWindowId,
498 bool forcePrincipalCheckForCacheEntry);
500 void AddProxy(imgRequestProxy* aProxy);
501 void RemoveProxy(imgRequestProxy* aProxy);
503 NS_DECL_THREADSAFE_ISUPPORTS
504 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
505 NS_DECL_NSISTREAMLISTENER
506 NS_DECL_NSIREQUESTOBSERVER
507 NS_DECL_NSICHANNELEVENTSINK
508 NS_DECL_NSIINTERFACEREQUESTOR
509 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
511 private:
512 void UpdateProxies(bool aCancelRequest, bool aSyncNotify);
513 virtual ~imgCacheValidator();
515 nsCOMPtr<nsIStreamListener> mDestListener;
516 RefPtr<nsProgressNotificationProxy> mProgressProxy;
517 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
518 nsCOMPtr<nsIChannel> mRedirectChannel;
520 RefPtr<imgRequest> mRequest;
521 AutoTArray<RefPtr<imgRequestProxy>, 4> mProxies;
523 RefPtr<imgRequest> mNewRequest;
524 RefPtr<imgCacheEntry> mNewEntry;
526 RefPtr<mozilla::dom::Document> mDocument;
527 uint64_t mInnerWindowId;
529 imgLoader* mImgLoader;
531 bool mHadInsecureRedirect;
534 #endif // mozilla_image_imgLoader_h