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"
31 class imgRequestProxy
;
32 class imgINotificationObserver
;
34 class imgCacheExpirationTracker
;
35 class imgMemoryReporter
;
41 } // namespace mozilla
45 imgCacheEntry(imgLoader
* loader
, imgRequest
* request
,
46 bool aForcePrincipalCheck
);
50 MOZ_ASSERT(int32_t(mRefCnt
) >= 0, "illegal refcnt");
51 NS_ASSERT_OWNINGTHREAD(imgCacheEntry
);
53 NS_LOG_ADDREF(this, mRefCnt
, "imgCacheEntry", sizeof(*this));
58 MOZ_ASSERT(0 != mRefCnt
, "dup release");
59 NS_ASSERT_OWNINGTHREAD(imgCacheEntry
);
61 NS_LOG_RELEASE(this, mRefCnt
, "imgCacheEntry");
63 mRefCnt
= 1; /* stabilize */
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
) {
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
;
93 bool GetMustValidate() const { return mMustValidate
; }
94 void SetMustValidate(bool aValidate
) {
95 mMustValidate
= aValidate
;
99 already_AddRefed
<imgRequest
> GetRequest() const {
100 RefPtr
<imgRequest
> req
= mRequest
;
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
);
118 imgLoader
* Loader() const { return mLoader
; }
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
&);
132 nsAutoRefCnt mRefCnt
;
136 RefPtr
<imgRequest
> mRequest
;
138 int32_t mTouchedTime
;
140 uint32_t mExpiryTime
;
141 nsExpirationState mExpirationState
;
142 bool mMustValidate
: 1;
144 bool mHasNoProxies
: 1;
145 bool mForcePrincipalCheck
: 1;
146 bool mHasNotified
: 1;
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
{
161 void Remove(imgCacheEntry
*);
162 void Push(imgCacheEntry
*);
165 already_AddRefed
<imgCacheEntry
> Pop();
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
;
176 const_iterator
begin() const;
178 const_iterator
end() const;
181 queueContainer mQueue
;
186 enum class AcceptedMimeTypes
: uint8_t {
188 IMAGES_AND_DOCUMENTS
,
191 class imgLoader final
: public imgILoader
,
192 public nsIContentSniffer
,
194 public nsSupportsWeakReference
,
196 virtual ~imgLoader();
199 using ImageCacheKey
= mozilla::image::ImageCacheKey
;
200 using imgCacheTable
=
201 nsRefPtrHashtable
<nsGenericHashKey
<ImageCacheKey
>, imgCacheEntry
>;
202 using imgSet
= nsTHashSet
<imgRequest
*>;
203 using Mutex
= mozilla::Mutex
;
207 NS_DECL_NSICONTENTSNIFFER
212 * Get the normal image loader instance that is used by gecko code, creating
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
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
,
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
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
{
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
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
) {
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
);
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
,
388 nsIReferrerInfo
* aReferrerInfo
,
389 mozilla::dom::Document
* aLoadingDocument
,
390 nsIPrincipal
* aLoadingPrincipal
,
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();
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
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
{
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
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
{
467 nsProgressNotificationProxy(nsIChannel
* channel
, imgIRequest
* proxy
)
468 : mImageRequest(proxy
) {
469 channel
->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks
));
473 NS_DECL_NSIPROGRESSEVENTSINK
474 NS_DECL_NSICHANNELEVENTSINK
475 NS_DECL_NSIINTERFACEREQUESTOR
477 ~nsProgressNotificationProxy() = default;
479 nsCOMPtr
<nsIInterfaceRequestor
> mOriginalCallbacks
;
480 nsCOMPtr
<nsIRequest
> mImageRequest
;
487 #include "nsCOMArray.h"
489 class imgCacheValidator
: public nsIStreamListener
,
490 public nsIThreadRetargetableStreamListener
,
491 public nsIChannelEventSink
,
492 public nsIInterfaceRequestor
,
493 public nsIAsyncVerifyRedirectCallback
{
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
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