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"
29 #include "mozilla/dom/CacheExpirationTime.h"
32 class imgRequestProxy
;
33 class imgINotificationObserver
;
35 class imgCacheExpirationTracker
;
36 class imgMemoryReporter
;
41 enum class FetchPriority
: uint8_t;
43 } // namespace mozilla
47 NS_INLINE_DECL_REFCOUNTING(imgCacheEntry
)
49 imgCacheEntry(imgLoader
* loader
, imgRequest
* request
,
50 bool aForcePrincipalCheck
);
52 uint32_t GetDataSize() const { return mDataSize
; }
53 void SetDataSize(uint32_t aDataSize
) {
54 int32_t oldsize
= mDataSize
;
55 mDataSize
= aDataSize
;
56 UpdateCache(mDataSize
- oldsize
);
59 int32_t GetTouchedTime() const { return mTouchedTime
; }
60 void SetTouchedTime(int32_t time
) {
62 Touch(/* updateTime = */ false);
65 uint32_t GetLoadTime() const { return mLoadTime
; }
67 void UpdateLoadTime();
69 const CacheExpirationTime
& GetExpiryTime() const { return mExpiryTime
; }
71 void AccumulateExpiryTime(const CacheExpirationTime
& aExpiryTime
,
72 bool aForceTouch
= false) {
73 if (aExpiryTime
.IsNever()) {
79 if (mExpiryTime
.IsNever() || aExpiryTime
.IsShorterThan(mExpiryTime
)) {
80 mExpiryTime
= aExpiryTime
;
89 bool GetMustValidate() const { return mMustValidate
; }
90 void SetMustValidate(bool aValidate
) {
91 mMustValidate
= aValidate
;
95 already_AddRefed
<imgRequest
> GetRequest() const {
96 RefPtr
<imgRequest
> req
= mRequest
;
100 bool Evicted() const { return mEvicted
; }
102 nsExpirationState
* GetExpirationState() { return &mExpirationState
; }
104 bool HasNoProxies() const { return mHasNoProxies
; }
106 bool ForcePrincipalCheck() const { return mForcePrincipalCheck
; }
108 bool HasNotified() const { return mHasNotified
; }
109 void SetHasNotified() {
110 MOZ_ASSERT(!mHasNotified
);
114 imgLoader
* Loader() const { return mLoader
; }
117 friend class imgLoader
;
118 friend class imgCacheQueue
;
119 void Touch(bool updateTime
= true);
120 void UpdateCache(int32_t diff
= 0);
121 void SetEvicted(bool evict
) { mEvicted
= evict
; }
122 void SetHasNoProxies(bool hasNoProxies
);
124 // Private, unimplemented copy constructor.
125 imgCacheEntry(const imgCacheEntry
&);
130 RefPtr
<imgRequest
> mRequest
;
132 int32_t mTouchedTime
;
134 CacheExpirationTime mExpiryTime
;
135 nsExpirationState mExpirationState
;
136 bool mMustValidate
: 1;
138 bool mHasNoProxies
: 1;
139 bool mForcePrincipalCheck
: 1;
140 bool mHasNotified
: 1;
145 #define NS_IMGLOADER_CID \
146 { /* c1354898-e3fe-4602-88a7-c4520c21cb4e */ \
147 0xc1354898, 0xe3fe, 0x4602, { \
148 0x88, 0xa7, 0xc4, 0x52, 0x0c, 0x21, 0xcb, 0x4e \
152 class imgCacheQueue
{
155 void Remove(imgCacheEntry
*);
156 void Push(imgCacheEntry
*);
159 already_AddRefed
<imgCacheEntry
> Pop();
161 uint32_t GetSize() const;
162 void UpdateSize(int32_t diff
);
163 uint32_t GetNumElements() const;
164 bool Contains(imgCacheEntry
* aEntry
) const;
165 typedef nsTArray
<RefPtr
<imgCacheEntry
>> queueContainer
;
166 typedef queueContainer::iterator iterator
;
167 typedef queueContainer::const_iterator const_iterator
;
170 const_iterator
begin() const;
172 const_iterator
end() const;
175 queueContainer mQueue
;
180 enum class AcceptedMimeTypes
: uint8_t {
182 IMAGES_AND_DOCUMENTS
,
185 class imgLoader final
: public imgILoader
,
186 public nsIContentSniffer
,
188 public nsSupportsWeakReference
,
190 virtual ~imgLoader();
193 using ImageCacheKey
= mozilla::image::ImageCacheKey
;
194 using imgCacheTable
=
195 nsRefPtrHashtable
<nsGenericHashKey
<ImageCacheKey
>, imgCacheEntry
>;
196 using imgSet
= nsTHashSet
<imgRequest
*>;
197 using Mutex
= mozilla::Mutex
;
201 NS_DECL_NSICONTENTSNIFFER
206 * Get the normal image loader instance that is used by gecko code, creating
209 static imgLoader
* NormalLoader();
212 * Get the Private Browsing image loader instance that is used by gecko code,
213 * creating it if necessary.
215 static imgLoader
* PrivateBrowsingLoader();
218 * Gecko code should use NormalLoader() or PrivateBrowsingLoader() to get the
219 * appropriate image loader.
221 * This constructor is public because the XPCOM module code that creates
222 * instances of "@mozilla.org/image/loader;1" / "@mozilla.org/image/cache;1"
223 * for nsIComponentManager.createInstance()/nsIServiceManager.getService()
224 * calls (now only made by add-ons) needs access to it.
226 * XXX We would like to get rid of the nsIServiceManager.getService (and
227 * nsIComponentManager.createInstance) method of creating imgLoader objects,
228 * but there are add-ons that are still using it. These add-ons don't
229 * actually do anything useful with the loaders that they create since nobody
230 * who creates an imgLoader using this method actually QIs to imgILoader and
231 * loads images. They all just QI to imgICache and either call clearCache()
232 * or findEntryProperties(). Since they're doing this on an imgLoader that
233 * has never loaded images, these calls are useless. It seems likely that
234 * the code that is doing this is just legacy code left over from a time when
235 * there was only one imgLoader instance for the entire process. (Nowadays
236 * the correct method to get an imgILoader/imgICache is to call
237 * imgITools::getImgCacheForDocument/imgITools::getImgLoaderForDocument.)
238 * All the same, even though what these add-ons are doing is a no-op,
239 * removing the nsIServiceManager.getService method of creating/getting an
240 * imgLoader objects would cause an exception in these add-ons that could
246 bool IsImageAvailable(nsIURI
*, nsIPrincipal
* aTriggeringPrincipal
,
247 mozilla::CORSMode
, mozilla::dom::Document
*);
249 [[nodiscard
]] nsresult
LoadImage(
250 nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
, nsIReferrerInfo
* aReferrerInfo
,
251 nsIPrincipal
* aLoadingPrincipal
, uint64_t aRequestContextID
,
252 nsILoadGroup
* aLoadGroup
, imgINotificationObserver
* aObserver
,
253 nsINode
* aContext
, mozilla::dom::Document
* aLoadingDocument
,
254 nsLoadFlags aLoadFlags
, nsISupports
* aCacheKey
,
255 nsContentPolicyType aContentPolicyType
, const nsAString
& initiatorType
,
256 bool aUseUrgentStartForChannel
, bool aLinkPreload
,
257 uint64_t aEarlyHintPreloaderId
,
258 mozilla::dom::FetchPriority aFetchPriority
, imgRequestProxy
** _retval
);
260 [[nodiscard
]] nsresult
LoadImageWithChannel(
261 nsIChannel
* channel
, imgINotificationObserver
* aObserver
,
262 mozilla::dom::Document
* aLoadingDocument
, nsIStreamListener
** listener
,
263 imgRequestProxy
** _retval
);
265 static nsresult
GetMimeTypeFromContent(const char* aContents
,
267 nsACString
& aContentType
);
270 * Returns true if the given mime type may be interpreted as an image.
272 * Some MIME types may be interpreted as both images and documents. (At the
273 * moment only "image/svg+xml" falls into this category, but there may be more
274 * in the future.) Callers which want this function to return true for such
275 * MIME types should pass AcceptedMimeTypes::IMAGES_AND_DOCUMENTS for
278 * @param aMimeType The MIME type to evaluate.
279 * @param aAcceptedMimeTypes Which kinds of MIME types to treat as images.
281 static bool SupportImageWithMimeType(
282 const nsACString
&, AcceptedMimeTypes aAccept
= AcceptedMimeTypes::IMAGES
);
284 static void GlobalInit(); // for use by the factory
285 static void Shutdown(); // for use by the factory
286 static void ShutdownMemoryReporter();
288 enum class ClearOption
{
292 using ClearOptions
= mozilla::EnumSet
<ClearOption
>;
293 nsresult
ClearImageCache(ClearOptions
= {});
294 void MinimizeCache() { ClearImageCache({ClearOption::UnusedOnly
}); }
296 nsresult
InitCache();
298 bool RemoveFromCache(const ImageCacheKey
& aKey
);
300 // Enumeration describing if a given entry is in the cache queue or not.
301 // There are some cases we know the entry is definitely not in the queue.
302 enum class QueueState
{ MaybeExists
, AlreadyRemoved
};
304 bool RemoveFromCache(imgCacheEntry
* entry
,
305 QueueState aQueueState
= QueueState::MaybeExists
);
307 bool PutIntoCache(const ImageCacheKey
& aKey
, imgCacheEntry
* aEntry
);
309 void AddToUncachedImages(imgRequest
* aRequest
);
310 void RemoveFromUncachedImages(imgRequest
* aRequest
);
312 // Returns true if we should prefer evicting cache entry |two| over cache
314 // This mixes units in the worst way, but provides reasonable results.
315 inline static bool CompareCacheEntries(const RefPtr
<imgCacheEntry
>& one
,
316 const RefPtr
<imgCacheEntry
>& two
) {
324 const double sizeweight
= 1.0 - sCacheTimeWeight
;
326 // We want large, old images to be evicted first (depending on their
327 // relative weights). Since a larger time is actually newer, we subtract
328 // time's weight, so an older image has a larger weight.
329 double oneweight
= double(one
->GetDataSize()) * sizeweight
-
330 double(one
->GetTouchedTime()) * sCacheTimeWeight
;
331 double twoweight
= double(two
->GetDataSize()) * sizeweight
-
332 double(two
->GetTouchedTime()) * sCacheTimeWeight
;
334 return oneweight
< twoweight
;
337 void VerifyCacheSizes();
339 nsresult
RemoveEntriesInternal(
340 const mozilla::Maybe
<nsCOMPtr
<nsIPrincipal
>>& aPrincipal
,
341 const mozilla::Maybe
<nsCString
>& aSchemelessSite
,
342 const mozilla::Maybe
<mozilla::OriginAttributesPattern
>& aPattern
);
344 // The image loader maintains a hash table of all imgCacheEntries. However,
345 // only some of them will be evicted from the cache: those who have no
346 // imgRequestProxies watching their imgRequests.
348 // Once an imgRequest has no imgRequestProxies, it should notify us by
349 // calling HasNoObservers(), and null out its cache entry pointer.
351 // Upon having a proxy start observing again, it should notify us by calling
352 // HasObservers(). The request's cache entry will be re-set before this
353 // happens, by calling imgRequest::SetCacheEntry() when an entry with no
354 // observers is re-requested.
355 bool SetHasNoProxies(imgRequest
* aRequest
, imgCacheEntry
* aEntry
);
356 bool SetHasProxies(imgRequest
* aRequest
);
359 static already_AddRefed
<imgLoader
> CreateImageLoader();
361 bool ValidateEntry(imgCacheEntry
* aEntry
, nsIURI
* aURI
,
362 nsIURI
* aInitialDocumentURI
,
363 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
364 imgINotificationObserver
* aObserver
,
365 mozilla::dom::Document
* aLoadingDocument
,
366 nsLoadFlags aLoadFlags
,
367 nsContentPolicyType aLoadPolicyType
,
368 bool aCanMakeNewChannel
, bool* aNewChannelCreated
,
369 imgRequestProxy
** aProxyRequest
,
370 nsIPrincipal
* aTriggeringPrincipal
, mozilla::CORSMode
,
371 bool aLinkPreload
, uint64_t aEarlyHintPreloaderId
,
372 mozilla::dom::FetchPriority aFetchPriority
);
374 bool ValidateRequestWithNewChannel(
375 imgRequest
* request
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
376 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
377 imgINotificationObserver
* aObserver
,
378 mozilla::dom::Document
* aLoadingDocument
, uint64_t aInnerWindowId
,
379 nsLoadFlags aLoadFlags
, nsContentPolicyType aContentPolicyType
,
380 imgRequestProxy
** aProxyRequest
, nsIPrincipal
* aLoadingPrincipal
,
381 mozilla::CORSMode
, bool aLinkPreload
, uint64_t aEarlyHintPreloaderId
,
382 mozilla::dom::FetchPriority aFetchPriority
, bool* aNewChannelCreated
);
384 void NotifyObserversForCachedImage(
385 imgCacheEntry
* aEntry
, imgRequest
* request
, nsIURI
* aURI
,
386 nsIReferrerInfo
* aReferrerInfo
, mozilla::dom::Document
* aLoadingDocument
,
387 nsIPrincipal
* aTriggeringPrincipal
, mozilla::CORSMode
,
388 uint64_t aEarlyHintPreloaderId
,
389 mozilla::dom::FetchPriority aFetchPriority
);
390 // aURI may be different from imgRequest's URI in the case of blob URIs, as we
391 // can share requests with different URIs.
392 nsresult
CreateNewProxyForRequest(imgRequest
* aRequest
, nsIURI
* aURI
,
393 nsILoadGroup
* aLoadGroup
,
394 mozilla::dom::Document
* aLoadingDocument
,
395 imgINotificationObserver
* aObserver
,
396 nsLoadFlags aLoadFlags
,
397 imgRequestProxy
** _retval
);
399 nsresult
EvictEntries(bool aChromeOnly
);
401 void CacheEntriesChanged(int32_t aSizeDiff
);
402 void CheckCacheLimits();
405 friend class imgCacheEntry
;
406 friend class imgMemoryReporter
;
408 imgCacheTable mCache
;
409 imgCacheQueue mCacheQueue
;
411 // Hash set of every imgRequest for this loader that isn't in mCache or
412 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
413 // mUncachedImages should be every imgRequest that is alive. These are weak
414 // pointers so we rely on the imgRequest destructor to remove itself.
415 imgSet mUncachedImages
MOZ_GUARDED_BY(mUncachedImagesMutex
);
416 // The imgRequest can have refs to them held on non-main thread, so we need
417 // a mutex because we modify the uncached images set from the imgRequest
419 Mutex mUncachedImagesMutex
;
421 static double sCacheTimeWeight
;
422 static uint32_t sCacheMaxSize
;
423 static imgMemoryReporter
* sMemReporter
;
425 mozilla::UniquePtr
<imgCacheExpirationTracker
> mCacheTracker
;
426 bool mRespectPrivacy
;
430 * proxy stream listener class used to handle multipart/x-mixed-replace
433 #include "nsCOMPtr.h"
434 #include "nsIStreamListener.h"
435 #include "nsIThreadRetargetableStreamListener.h"
437 class ProxyListener
: public nsIThreadRetargetableStreamListener
{
439 explicit ProxyListener(nsIStreamListener
* dest
);
441 /* additional members */
442 NS_DECL_THREADSAFE_ISUPPORTS
443 NS_DECL_NSISTREAMLISTENER
444 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
445 NS_DECL_NSIREQUESTOBSERVER
448 virtual ~ProxyListener();
450 nsCOMPtr
<nsIStreamListener
> mDestListener
;
454 * A class that implements nsIProgressEventSink and forwards all calls to it to
455 * the original notification callbacks of the channel. Also implements
456 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
457 * and forwards everything else to the channel's notification callbacks.
459 class nsProgressNotificationProxy final
: public nsIProgressEventSink
,
460 public nsIChannelEventSink
,
461 public nsIInterfaceRequestor
{
463 nsProgressNotificationProxy(nsIChannel
* channel
, imgIRequest
* proxy
)
464 : mImageRequest(proxy
) {
465 channel
->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks
));
469 NS_DECL_NSIPROGRESSEVENTSINK
470 NS_DECL_NSICHANNELEVENTSINK
471 NS_DECL_NSIINTERFACEREQUESTOR
473 ~nsProgressNotificationProxy() = default;
475 nsCOMPtr
<nsIInterfaceRequestor
> mOriginalCallbacks
;
476 nsCOMPtr
<nsIRequest
> mImageRequest
;
483 #include "nsCOMArray.h"
485 class imgCacheValidator
: public nsIThreadRetargetableStreamListener
,
486 public nsIChannelEventSink
,
487 public nsIInterfaceRequestor
,
488 public nsIAsyncVerifyRedirectCallback
{
490 imgCacheValidator(nsProgressNotificationProxy
* progress
, imgLoader
* loader
,
491 imgRequest
* aRequest
, mozilla::dom::Document
* aDocument
,
492 uint64_t aInnerWindowId
,
493 bool forcePrincipalCheckForCacheEntry
);
495 void AddProxy(imgRequestProxy
* aProxy
);
496 void RemoveProxy(imgRequestProxy
* aProxy
);
498 NS_DECL_THREADSAFE_ISUPPORTS
499 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
500 NS_DECL_NSISTREAMLISTENER
501 NS_DECL_NSIREQUESTOBSERVER
502 NS_DECL_NSICHANNELEVENTSINK
503 NS_DECL_NSIINTERFACEREQUESTOR
504 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
507 void UpdateProxies(bool aCancelRequest
, bool aSyncNotify
);
508 virtual ~imgCacheValidator();
510 nsCOMPtr
<nsIStreamListener
> mDestListener
;
511 RefPtr
<nsProgressNotificationProxy
> mProgressProxy
;
512 nsCOMPtr
<nsIAsyncVerifyRedirectCallback
> mRedirectCallback
;
513 nsCOMPtr
<nsIChannel
> mRedirectChannel
;
515 RefPtr
<imgRequest
> mRequest
;
516 AutoTArray
<RefPtr
<imgRequestProxy
>, 4> mProxies
;
518 RefPtr
<imgRequest
> mNewRequest
;
519 RefPtr
<imgCacheEntry
> mNewEntry
;
521 RefPtr
<mozilla::dom::Document
> mDocument
;
522 uint64_t mInnerWindowId
;
524 imgLoader
* mImgLoader
;
526 bool mHadInsecureRedirect
;
529 #endif // mozilla_image_imgLoader_h