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/UniquePtr.h"
15 #include "imgILoader.h"
16 #include "imgICache.h"
17 #include "nsWeakReference.h"
18 #include "nsIContentSniffer.h"
19 #include "nsRefPtrHashtable.h"
20 #include "nsTHashSet.h"
21 #include "nsExpirationTracker.h"
22 #include "ImageCacheKey.h"
23 #include "imgRequest.h"
24 #include "nsIProgressEventSink.h"
25 #include "nsIChannel.h"
26 #include "nsIThreadRetargetableStreamListener.h"
27 #include "imgIRequest.h"
30 class imgRequestProxy
;
31 class imgINotificationObserver
;
33 class imgCacheExpirationTracker
;
34 class imgMemoryReporter
;
40 } // namespace mozilla
44 imgCacheEntry(imgLoader
* loader
, imgRequest
* request
,
45 bool aForcePrincipalCheck
);
49 MOZ_ASSERT(int32_t(mRefCnt
) >= 0, "illegal refcnt");
50 NS_ASSERT_OWNINGTHREAD(imgCacheEntry
);
52 NS_LOG_ADDREF(this, mRefCnt
, "imgCacheEntry", sizeof(*this));
57 MOZ_ASSERT(0 != mRefCnt
, "dup release");
58 NS_ASSERT_OWNINGTHREAD(imgCacheEntry
);
60 NS_LOG_RELEASE(this, mRefCnt
, "imgCacheEntry");
62 mRefCnt
= 1; /* stabilize */
69 uint32_t GetDataSize() const { return mDataSize
; }
70 void SetDataSize(uint32_t aDataSize
) {
71 int32_t oldsize
= mDataSize
;
72 mDataSize
= aDataSize
;
73 UpdateCache(mDataSize
- oldsize
);
76 int32_t GetTouchedTime() const { return mTouchedTime
; }
77 void SetTouchedTime(int32_t time
) {
79 Touch(/* updateTime = */ false);
82 uint32_t GetLoadTime() const { return mLoadTime
; }
84 void UpdateLoadTime();
86 uint32_t GetExpiryTime() const { return mExpiryTime
; }
87 void SetExpiryTime(uint32_t aExpiryTime
) {
88 mExpiryTime
= aExpiryTime
;
92 bool GetMustValidate() const { return mMustValidate
; }
93 void SetMustValidate(bool aValidate
) {
94 mMustValidate
= aValidate
;
98 already_AddRefed
<imgRequest
> GetRequest() const {
99 RefPtr
<imgRequest
> req
= mRequest
;
103 bool Evicted() const { return mEvicted
; }
105 nsExpirationState
* GetExpirationState() { return &mExpirationState
; }
107 bool HasNoProxies() const { return mHasNoProxies
; }
109 bool ForcePrincipalCheck() const { return mForcePrincipalCheck
; }
111 bool HasNotified() const { return mHasNotified
; }
112 void SetHasNotified() {
113 MOZ_ASSERT(!mHasNotified
);
117 imgLoader
* Loader() const { return mLoader
; }
120 friend class imgLoader
;
121 friend class imgCacheQueue
;
122 void Touch(bool updateTime
= true);
123 void UpdateCache(int32_t diff
= 0);
124 void SetEvicted(bool evict
) { mEvicted
= evict
; }
125 void SetHasNoProxies(bool hasNoProxies
);
127 // Private, unimplemented copy constructor.
128 imgCacheEntry(const imgCacheEntry
&);
131 nsAutoRefCnt mRefCnt
;
135 RefPtr
<imgRequest
> mRequest
;
137 int32_t mTouchedTime
;
139 uint32_t mExpiryTime
;
140 nsExpirationState mExpirationState
;
141 bool mMustValidate
: 1;
143 bool mHasNoProxies
: 1;
144 bool mForcePrincipalCheck
: 1;
145 bool mHasNotified
: 1;
150 #define NS_IMGLOADER_CID \
151 { /* c1354898-e3fe-4602-88a7-c4520c21cb4e */ \
152 0xc1354898, 0xe3fe, 0x4602, { \
153 0x88, 0xa7, 0xc4, 0x52, 0x0c, 0x21, 0xcb, 0x4e \
157 class imgCacheQueue
{
160 void Remove(imgCacheEntry
*);
161 void Push(imgCacheEntry
*);
164 already_AddRefed
<imgCacheEntry
> Pop();
166 uint32_t GetSize() const;
167 void UpdateSize(int32_t diff
);
168 uint32_t GetNumElements() const;
169 bool Contains(imgCacheEntry
* aEntry
) const;
170 typedef nsTArray
<RefPtr
<imgCacheEntry
>> queueContainer
;
171 typedef queueContainer::iterator iterator
;
172 typedef queueContainer::const_iterator const_iterator
;
175 const_iterator
begin() const;
177 const_iterator
end() const;
180 queueContainer mQueue
;
185 enum class AcceptedMimeTypes
: uint8_t {
187 IMAGES_AND_DOCUMENTS
,
190 class imgLoader final
: public imgILoader
,
191 public nsIContentSniffer
,
193 public nsSupportsWeakReference
,
195 virtual ~imgLoader();
198 using ImageCacheKey
= mozilla::image::ImageCacheKey
;
199 using imgCacheTable
=
200 nsRefPtrHashtable
<nsGenericHashKey
<ImageCacheKey
>, imgCacheEntry
>;
201 using imgSet
= nsTHashSet
<imgRequest
*>;
202 using Mutex
= mozilla::Mutex
;
206 NS_DECL_NSICONTENTSNIFFER
211 * Get the normal image loader instance that is used by gecko code, creating
214 static imgLoader
* NormalLoader();
217 * Get the Private Browsing image loader instance that is used by gecko code,
218 * creating it if necessary.
220 static imgLoader
* PrivateBrowsingLoader();
223 * Gecko code should use NormalLoader() or PrivateBrowsingLoader() to get the
224 * appropriate image loader.
226 * This constructor is public because the XPCOM module code that creates
227 * instances of "@mozilla.org/image/loader;1" / "@mozilla.org/image/cache;1"
228 * for nsIComponentManager.createInstance()/nsIServiceManager.getService()
229 * calls (now only made by add-ons) needs access to it.
231 * XXX We would like to get rid of the nsIServiceManager.getService (and
232 * nsIComponentManager.createInstance) method of creating imgLoader objects,
233 * but there are add-ons that are still using it. These add-ons don't
234 * actually do anything useful with the loaders that they create since nobody
235 * who creates an imgLoader using this method actually QIs to imgILoader and
236 * loads images. They all just QI to imgICache and either call clearCache()
237 * or findEntryProperties(). Since they're doing this on an imgLoader that
238 * has never loaded images, these calls are useless. It seems likely that
239 * the code that is doing this is just legacy code left over from a time when
240 * there was only one imgLoader instance for the entire process. (Nowadays
241 * the correct method to get an imgILoader/imgICache is to call
242 * imgITools::getImgCacheForDocument/imgITools::getImgLoaderForDocument.)
243 * All the same, even though what these add-ons are doing is a no-op,
244 * removing the nsIServiceManager.getService method of creating/getting an
245 * imgLoader objects would cause an exception in these add-ons that could
251 bool IsImageAvailable(nsIURI
*, nsIPrincipal
* aTriggeringPrincipal
,
252 mozilla::CORSMode
, mozilla::dom::Document
*);
254 [[nodiscard
]] nsresult
LoadImage(
255 nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
, nsIReferrerInfo
* aReferrerInfo
,
256 nsIPrincipal
* aLoadingPrincipal
, uint64_t aRequestContextID
,
257 nsILoadGroup
* aLoadGroup
, imgINotificationObserver
* aObserver
,
258 nsINode
* aContext
, mozilla::dom::Document
* aLoadingDocument
,
259 nsLoadFlags aLoadFlags
, nsISupports
* aCacheKey
,
260 nsContentPolicyType aContentPolicyType
, const nsAString
& initiatorType
,
261 bool aUseUrgentStartForChannel
, bool aLinkPreload
,
262 uint64_t aEarlyHintPreloaderId
, imgRequestProxy
** _retval
);
264 [[nodiscard
]] nsresult
LoadImageWithChannel(
265 nsIChannel
* channel
, imgINotificationObserver
* aObserver
,
266 mozilla::dom::Document
* aLoadingDocument
, nsIStreamListener
** listener
,
267 imgRequestProxy
** _retval
);
269 static nsresult
GetMimeTypeFromContent(const char* aContents
,
271 nsACString
& aContentType
);
274 * Returns true if the given mime type may be interpreted as an image.
276 * Some MIME types may be interpreted as both images and documents. (At the
277 * moment only "image/svg+xml" falls into this category, but there may be more
278 * in the future.) Callers which want this function to return true for such
279 * MIME types should pass AcceptedMimeTypes::IMAGES_AND_DOCUMENTS for
282 * @param aMimeType The MIME type to evaluate.
283 * @param aAcceptedMimeTypes Which kinds of MIME types to treat as images.
285 static bool SupportImageWithMimeType(
286 const nsACString
&, AcceptedMimeTypes aAccept
= AcceptedMimeTypes::IMAGES
);
288 static void GlobalInit(); // for use by the factory
289 static void Shutdown(); // for use by the factory
290 static void ShutdownMemoryReporter();
292 nsresult
ClearChromeImageCache();
293 nsresult
ClearImageCache();
294 void MinimizeCaches();
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(nsIPrincipal
* aPrincipal
,
340 const nsACString
* aBaseDomain
);
342 // The image loader maintains a hash table of all imgCacheEntries. However,
343 // only some of them will be evicted from the cache: those who have no
344 // imgRequestProxies watching their imgRequests.
346 // Once an imgRequest has no imgRequestProxies, it should notify us by
347 // calling HasNoObservers(), and null out its cache entry pointer.
349 // Upon having a proxy start observing again, it should notify us by calling
350 // HasObservers(). The request's cache entry will be re-set before this
351 // happens, by calling imgRequest::SetCacheEntry() when an entry with no
352 // observers is re-requested.
353 bool SetHasNoProxies(imgRequest
* aRequest
, imgCacheEntry
* aEntry
);
354 bool SetHasProxies(imgRequest
* aRequest
);
357 static already_AddRefed
<imgLoader
> CreateImageLoader();
359 bool ValidateEntry(imgCacheEntry
* aEntry
, nsIURI
* aURI
,
360 nsIURI
* aInitialDocumentURI
,
361 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
362 imgINotificationObserver
* aObserver
,
363 mozilla::dom::Document
* aLoadingDocument
,
364 nsLoadFlags aLoadFlags
,
365 nsContentPolicyType aLoadPolicyType
,
366 bool aCanMakeNewChannel
, bool* aNewChannelCreated
,
367 imgRequestProxy
** aProxyRequest
,
368 nsIPrincipal
* aTriggeringPrincipal
, mozilla::CORSMode
,
369 bool aLinkPreload
, uint64_t aEarlyHintPreloaderId
);
371 bool ValidateRequestWithNewChannel(
372 imgRequest
* request
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
373 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
374 imgINotificationObserver
* aObserver
,
375 mozilla::dom::Document
* aLoadingDocument
, uint64_t aInnerWindowId
,
376 nsLoadFlags aLoadFlags
, nsContentPolicyType aContentPolicyType
,
377 imgRequestProxy
** aProxyRequest
, nsIPrincipal
* aLoadingPrincipal
,
378 mozilla::CORSMode
, bool aLinkPreload
, uint64_t aEarlyHintPreloaderId
,
379 bool* aNewChannelCreated
);
381 void NotifyObserversForCachedImage(imgCacheEntry
* aEntry
, imgRequest
* request
,
383 nsIReferrerInfo
* aReferrerInfo
,
384 mozilla::dom::Document
* aLoadingDocument
,
385 nsIPrincipal
* aLoadingPrincipal
,
387 uint64_t aEarlyHintPreloaderId
);
388 // aURI may be different from imgRequest's URI in the case of blob URIs, as we
389 // can share requests with different URIs.
390 nsresult
CreateNewProxyForRequest(imgRequest
* aRequest
, nsIURI
* aURI
,
391 nsILoadGroup
* aLoadGroup
,
392 mozilla::dom::Document
* aLoadingDocument
,
393 imgINotificationObserver
* aObserver
,
394 nsLoadFlags aLoadFlags
,
395 imgRequestProxy
** _retval
);
397 nsresult
EvictEntries(imgCacheTable
& aCacheToClear
);
398 nsresult
EvictEntries(imgCacheQueue
& aQueueToClear
);
400 imgCacheTable
& GetCache(bool aForChrome
);
401 imgCacheTable
& GetCache(const ImageCacheKey
& aKey
);
402 imgCacheQueue
& GetCacheQueue(bool aForChrome
);
403 imgCacheQueue
& GetCacheQueue(const ImageCacheKey
& aKey
);
404 void CacheEntriesChanged(bool aForChrome
, int32_t aSizeDiff
= 0);
405 void CheckCacheLimits(imgCacheTable
& cache
, imgCacheQueue
& queue
);
408 friend class imgCacheEntry
;
409 friend class imgMemoryReporter
;
411 imgCacheTable mCache
;
412 imgCacheQueue mCacheQueue
;
414 imgCacheTable mChromeCache
;
415 imgCacheQueue mChromeCacheQueue
;
417 // Hash set of every imgRequest for this loader that isn't in mCache or
418 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
419 // mUncachedImages should be every imgRequest that is alive. These are weak
420 // pointers so we rely on the imgRequest destructor to remove itself.
421 imgSet mUncachedImages
MOZ_GUARDED_BY(mUncachedImagesMutex
);
422 // The imgRequest can have refs to them held on non-main thread, so we need
423 // a mutex because we modify the uncached images set from the imgRequest
425 Mutex mUncachedImagesMutex
;
427 static double sCacheTimeWeight
;
428 static uint32_t sCacheMaxSize
;
429 static imgMemoryReporter
* sMemReporter
;
431 mozilla::UniquePtr
<imgCacheExpirationTracker
> mCacheTracker
;
432 bool mRespectPrivacy
;
436 * proxy stream listener class used to handle multipart/x-mixed-replace
439 #include "nsCOMPtr.h"
440 #include "nsIStreamListener.h"
441 #include "nsIThreadRetargetableStreamListener.h"
443 class ProxyListener
: public nsIStreamListener
,
444 public nsIThreadRetargetableStreamListener
{
446 explicit ProxyListener(nsIStreamListener
* dest
);
448 /* additional members */
449 NS_DECL_THREADSAFE_ISUPPORTS
450 NS_DECL_NSISTREAMLISTENER
451 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
452 NS_DECL_NSIREQUESTOBSERVER
455 virtual ~ProxyListener();
457 nsCOMPtr
<nsIStreamListener
> mDestListener
;
461 * A class that implements nsIProgressEventSink and forwards all calls to it to
462 * the original notification callbacks of the channel. Also implements
463 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
464 * and forwards everything else to the channel's notification callbacks.
466 class nsProgressNotificationProxy final
: public nsIProgressEventSink
,
467 public nsIChannelEventSink
,
468 public nsIInterfaceRequestor
{
470 nsProgressNotificationProxy(nsIChannel
* channel
, imgIRequest
* proxy
)
471 : mImageRequest(proxy
) {
472 channel
->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks
));
476 NS_DECL_NSIPROGRESSEVENTSINK
477 NS_DECL_NSICHANNELEVENTSINK
478 NS_DECL_NSIINTERFACEREQUESTOR
480 ~nsProgressNotificationProxy() = default;
482 nsCOMPtr
<nsIInterfaceRequestor
> mOriginalCallbacks
;
483 nsCOMPtr
<nsIRequest
> mImageRequest
;
490 #include "nsCOMArray.h"
492 class imgCacheValidator
: public nsIStreamListener
,
493 public nsIThreadRetargetableStreamListener
,
494 public nsIChannelEventSink
,
495 public nsIInterfaceRequestor
,
496 public nsIAsyncVerifyRedirectCallback
{
498 imgCacheValidator(nsProgressNotificationProxy
* progress
, imgLoader
* loader
,
499 imgRequest
* aRequest
, mozilla::dom::Document
* aDocument
,
500 uint64_t aInnerWindowId
,
501 bool forcePrincipalCheckForCacheEntry
);
503 void AddProxy(imgRequestProxy
* aProxy
);
504 void RemoveProxy(imgRequestProxy
* aProxy
);
506 NS_DECL_THREADSAFE_ISUPPORTS
507 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
508 NS_DECL_NSISTREAMLISTENER
509 NS_DECL_NSIREQUESTOBSERVER
510 NS_DECL_NSICHANNELEVENTSINK
511 NS_DECL_NSIINTERFACEREQUESTOR
512 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
515 void UpdateProxies(bool aCancelRequest
, bool aSyncNotify
);
516 virtual ~imgCacheValidator();
518 nsCOMPtr
<nsIStreamListener
> mDestListener
;
519 RefPtr
<nsProgressNotificationProxy
> mProgressProxy
;
520 nsCOMPtr
<nsIAsyncVerifyRedirectCallback
> mRedirectCallback
;
521 nsCOMPtr
<nsIChannel
> mRedirectChannel
;
523 RefPtr
<imgRequest
> mRequest
;
524 AutoTArray
<RefPtr
<imgRequestProxy
>, 4> mProxies
;
526 RefPtr
<imgRequest
> mNewRequest
;
527 RefPtr
<imgCacheEntry
> mNewEntry
;
529 RefPtr
<mozilla::dom::Document
> mDocument
;
530 uint64_t mInnerWindowId
;
532 imgLoader
* mImgLoader
;
534 bool mHadInsecureRedirect
;
537 #endif // mozilla_image_imgLoader_h