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 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();
360 imgCacheEntry
* aEntry
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
361 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
362 imgINotificationObserver
* aObserver
,
363 mozilla::dom::Document
* aLoadingDocument
, nsLoadFlags aLoadFlags
,
364 nsContentPolicyType aLoadPolicyType
, bool aCanMakeNewChannel
,
365 bool* aNewChannelCreated
, imgRequestProxy
** aProxyRequest
,
366 nsIPrincipal
* aTriggeringPrincipal
, mozilla::CORSMode
, bool aLinkPreload
);
368 bool ValidateRequestWithNewChannel(
369 imgRequest
* request
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
370 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
371 imgINotificationObserver
* aObserver
,
372 mozilla::dom::Document
* aLoadingDocument
, uint64_t aInnerWindowId
,
373 nsLoadFlags aLoadFlags
, nsContentPolicyType aContentPolicyType
,
374 imgRequestProxy
** aProxyRequest
, nsIPrincipal
* aLoadingPrincipal
,
375 mozilla::CORSMode
, bool aLinkPreload
, bool* aNewChannelCreated
);
377 void NotifyObserversForCachedImage(imgCacheEntry
* aEntry
, imgRequest
* request
,
379 nsIReferrerInfo
* aReferrerInfo
,
380 mozilla::dom::Document
* aLoadingDocument
,
381 nsIPrincipal
* aLoadingPrincipal
,
383 // aURI may be different from imgRequest's URI in the case of blob URIs, as we
384 // can share requests with different URIs.
385 nsresult
CreateNewProxyForRequest(imgRequest
* aRequest
, nsIURI
* aURI
,
386 nsILoadGroup
* aLoadGroup
,
387 mozilla::dom::Document
* aLoadingDocument
,
388 imgINotificationObserver
* aObserver
,
389 nsLoadFlags aLoadFlags
,
390 imgRequestProxy
** _retval
);
392 nsresult
EvictEntries(imgCacheTable
& aCacheToClear
);
393 nsresult
EvictEntries(imgCacheQueue
& aQueueToClear
);
395 imgCacheTable
& GetCache(bool aForChrome
);
396 imgCacheTable
& GetCache(const ImageCacheKey
& aKey
);
397 imgCacheQueue
& GetCacheQueue(bool aForChrome
);
398 imgCacheQueue
& GetCacheQueue(const ImageCacheKey
& aKey
);
399 void CacheEntriesChanged(bool aForChrome
, int32_t aSizeDiff
= 0);
400 void CheckCacheLimits(imgCacheTable
& cache
, imgCacheQueue
& queue
);
403 friend class imgCacheEntry
;
404 friend class imgMemoryReporter
;
406 imgCacheTable mCache
;
407 imgCacheQueue mCacheQueue
;
409 imgCacheTable mChromeCache
;
410 imgCacheQueue mChromeCacheQueue
;
412 // Hash set of every imgRequest for this loader that isn't in mCache or
413 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
414 // mUncachedImages should be every imgRequest that is alive. These are weak
415 // pointers so we rely on the imgRequest destructor to remove itself.
416 imgSet mUncachedImages
;
417 // The imgRequest can have refs to them held on non-main thread, so we need
418 // a mutex because we modify the uncached images set from the imgRequest
420 Mutex mUncachedImagesMutex MOZ_UNANNOTATED
;
422 static double sCacheTimeWeight
;
423 static uint32_t sCacheMaxSize
;
424 static imgMemoryReporter
* sMemReporter
;
426 mozilla::UniquePtr
<imgCacheExpirationTracker
> mCacheTracker
;
427 bool mRespectPrivacy
;
431 * proxy stream listener class used to handle multipart/x-mixed-replace
434 #include "nsCOMPtr.h"
435 #include "nsIStreamListener.h"
436 #include "nsIThreadRetargetableStreamListener.h"
438 class ProxyListener
: public nsIStreamListener
,
439 public nsIThreadRetargetableStreamListener
{
441 explicit ProxyListener(nsIStreamListener
* dest
);
443 /* additional members */
444 NS_DECL_THREADSAFE_ISUPPORTS
445 NS_DECL_NSISTREAMLISTENER
446 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
447 NS_DECL_NSIREQUESTOBSERVER
450 virtual ~ProxyListener();
452 nsCOMPtr
<nsIStreamListener
> mDestListener
;
456 * A class that implements nsIProgressEventSink and forwards all calls to it to
457 * the original notification callbacks of the channel. Also implements
458 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
459 * and forwards everything else to the channel's notification callbacks.
461 class nsProgressNotificationProxy final
: public nsIProgressEventSink
,
462 public nsIChannelEventSink
,
463 public nsIInterfaceRequestor
{
465 nsProgressNotificationProxy(nsIChannel
* channel
, imgIRequest
* proxy
)
466 : mImageRequest(proxy
) {
467 channel
->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks
));
471 NS_DECL_NSIPROGRESSEVENTSINK
472 NS_DECL_NSICHANNELEVENTSINK
473 NS_DECL_NSIINTERFACEREQUESTOR
475 ~nsProgressNotificationProxy() = default;
477 nsCOMPtr
<nsIInterfaceRequestor
> mOriginalCallbacks
;
478 nsCOMPtr
<nsIRequest
> mImageRequest
;
485 #include "nsCOMArray.h"
487 class imgCacheValidator
: public nsIStreamListener
,
488 public nsIThreadRetargetableStreamListener
,
489 public nsIChannelEventSink
,
490 public nsIInterfaceRequestor
,
491 public nsIAsyncVerifyRedirectCallback
{
493 imgCacheValidator(nsProgressNotificationProxy
* progress
, imgLoader
* loader
,
494 imgRequest
* aRequest
, mozilla::dom::Document
* aDocument
,
495 uint64_t aInnerWindowId
,
496 bool forcePrincipalCheckForCacheEntry
);
498 void AddProxy(imgRequestProxy
* aProxy
);
499 void RemoveProxy(imgRequestProxy
* aProxy
);
501 NS_DECL_THREADSAFE_ISUPPORTS
502 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
503 NS_DECL_NSISTREAMLISTENER
504 NS_DECL_NSIREQUESTOBSERVER
505 NS_DECL_NSICHANNELEVENTSINK
506 NS_DECL_NSIINTERFACEREQUESTOR
507 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
510 void UpdateProxies(bool aCancelRequest
, bool aSyncNotify
);
511 virtual ~imgCacheValidator();
513 nsCOMPtr
<nsIStreamListener
> mDestListener
;
514 RefPtr
<nsProgressNotificationProxy
> mProgressProxy
;
515 nsCOMPtr
<nsIAsyncVerifyRedirectCallback
> mRedirectCallback
;
516 nsCOMPtr
<nsIChannel
> mRedirectChannel
;
518 RefPtr
<imgRequest
> mRequest
;
519 AutoTArray
<RefPtr
<imgRequestProxy
>, 4> mProxies
;
521 RefPtr
<imgRequest
> mNewRequest
;
522 RefPtr
<imgCacheEntry
> mNewEntry
;
524 RefPtr
<mozilla::dom::Document
> mDocument
;
525 uint64_t mInnerWindowId
;
527 imgLoader
* mImgLoader
;
529 bool mHadInsecureRedirect
;
532 #endif // mozilla_image_imgLoader_h