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 NS_INLINE_DECL_REFCOUNTING(imgCacheEntry
)
47 imgCacheEntry(imgLoader
* loader
, imgRequest
* request
,
48 bool aForcePrincipalCheck
);
50 uint32_t GetDataSize() const { return mDataSize
; }
51 void SetDataSize(uint32_t aDataSize
) {
52 int32_t oldsize
= mDataSize
;
53 mDataSize
= aDataSize
;
54 UpdateCache(mDataSize
- oldsize
);
57 int32_t GetTouchedTime() const { return mTouchedTime
; }
58 void SetTouchedTime(int32_t time
) {
60 Touch(/* updateTime = */ false);
63 uint32_t GetLoadTime() const { return mLoadTime
; }
65 void UpdateLoadTime();
67 uint32_t GetExpiryTime() const { return mExpiryTime
; }
68 void SetExpiryTime(uint32_t aExpiryTime
) {
69 mExpiryTime
= aExpiryTime
;
73 bool GetMustValidate() const { return mMustValidate
; }
74 void SetMustValidate(bool aValidate
) {
75 mMustValidate
= aValidate
;
79 already_AddRefed
<imgRequest
> GetRequest() const {
80 RefPtr
<imgRequest
> req
= mRequest
;
84 bool Evicted() const { return mEvicted
; }
86 nsExpirationState
* GetExpirationState() { return &mExpirationState
; }
88 bool HasNoProxies() const { return mHasNoProxies
; }
90 bool ForcePrincipalCheck() const { return mForcePrincipalCheck
; }
92 bool HasNotified() const { return mHasNotified
; }
93 void SetHasNotified() {
94 MOZ_ASSERT(!mHasNotified
);
98 imgLoader
* Loader() const { return mLoader
; }
101 friend class imgLoader
;
102 friend class imgCacheQueue
;
103 void Touch(bool updateTime
= true);
104 void UpdateCache(int32_t diff
= 0);
105 void SetEvicted(bool evict
) { mEvicted
= evict
; }
106 void SetHasNoProxies(bool hasNoProxies
);
108 // Private, unimplemented copy constructor.
109 imgCacheEntry(const imgCacheEntry
&);
114 RefPtr
<imgRequest
> mRequest
;
116 int32_t mTouchedTime
;
118 uint32_t mExpiryTime
;
119 nsExpirationState mExpirationState
;
120 bool mMustValidate
: 1;
122 bool mHasNoProxies
: 1;
123 bool mForcePrincipalCheck
: 1;
124 bool mHasNotified
: 1;
129 #define NS_IMGLOADER_CID \
130 { /* c1354898-e3fe-4602-88a7-c4520c21cb4e */ \
131 0xc1354898, 0xe3fe, 0x4602, { \
132 0x88, 0xa7, 0xc4, 0x52, 0x0c, 0x21, 0xcb, 0x4e \
136 class imgCacheQueue
{
139 void Remove(imgCacheEntry
*);
140 void Push(imgCacheEntry
*);
143 already_AddRefed
<imgCacheEntry
> Pop();
145 uint32_t GetSize() const;
146 void UpdateSize(int32_t diff
);
147 uint32_t GetNumElements() const;
148 bool Contains(imgCacheEntry
* aEntry
) const;
149 typedef nsTArray
<RefPtr
<imgCacheEntry
>> queueContainer
;
150 typedef queueContainer::iterator iterator
;
151 typedef queueContainer::const_iterator const_iterator
;
154 const_iterator
begin() const;
156 const_iterator
end() const;
159 queueContainer mQueue
;
164 enum class AcceptedMimeTypes
: uint8_t {
166 IMAGES_AND_DOCUMENTS
,
169 class imgLoader final
: public imgILoader
,
170 public nsIContentSniffer
,
172 public nsSupportsWeakReference
,
174 virtual ~imgLoader();
177 using ImageCacheKey
= mozilla::image::ImageCacheKey
;
178 using imgCacheTable
=
179 nsRefPtrHashtable
<nsGenericHashKey
<ImageCacheKey
>, imgCacheEntry
>;
180 using imgSet
= nsTHashSet
<imgRequest
*>;
181 using Mutex
= mozilla::Mutex
;
185 NS_DECL_NSICONTENTSNIFFER
190 * Get the normal image loader instance that is used by gecko code, creating
193 static imgLoader
* NormalLoader();
196 * Get the Private Browsing image loader instance that is used by gecko code,
197 * creating it if necessary.
199 static imgLoader
* PrivateBrowsingLoader();
202 * Gecko code should use NormalLoader() or PrivateBrowsingLoader() to get the
203 * appropriate image loader.
205 * This constructor is public because the XPCOM module code that creates
206 * instances of "@mozilla.org/image/loader;1" / "@mozilla.org/image/cache;1"
207 * for nsIComponentManager.createInstance()/nsIServiceManager.getService()
208 * calls (now only made by add-ons) needs access to it.
210 * XXX We would like to get rid of the nsIServiceManager.getService (and
211 * nsIComponentManager.createInstance) method of creating imgLoader objects,
212 * but there are add-ons that are still using it. These add-ons don't
213 * actually do anything useful with the loaders that they create since nobody
214 * who creates an imgLoader using this method actually QIs to imgILoader and
215 * loads images. They all just QI to imgICache and either call clearCache()
216 * or findEntryProperties(). Since they're doing this on an imgLoader that
217 * has never loaded images, these calls are useless. It seems likely that
218 * the code that is doing this is just legacy code left over from a time when
219 * there was only one imgLoader instance for the entire process. (Nowadays
220 * the correct method to get an imgILoader/imgICache is to call
221 * imgITools::getImgCacheForDocument/imgITools::getImgLoaderForDocument.)
222 * All the same, even though what these add-ons are doing is a no-op,
223 * removing the nsIServiceManager.getService method of creating/getting an
224 * imgLoader objects would cause an exception in these add-ons that could
230 bool IsImageAvailable(nsIURI
*, nsIPrincipal
* aTriggeringPrincipal
,
231 mozilla::CORSMode
, mozilla::dom::Document
*);
233 [[nodiscard
]] nsresult
LoadImage(
234 nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
, nsIReferrerInfo
* aReferrerInfo
,
235 nsIPrincipal
* aLoadingPrincipal
, uint64_t aRequestContextID
,
236 nsILoadGroup
* aLoadGroup
, imgINotificationObserver
* aObserver
,
237 nsINode
* aContext
, mozilla::dom::Document
* aLoadingDocument
,
238 nsLoadFlags aLoadFlags
, nsISupports
* aCacheKey
,
239 nsContentPolicyType aContentPolicyType
, const nsAString
& initiatorType
,
240 bool aUseUrgentStartForChannel
, bool aLinkPreload
,
241 uint64_t aEarlyHintPreloaderId
, imgRequestProxy
** _retval
);
243 [[nodiscard
]] nsresult
LoadImageWithChannel(
244 nsIChannel
* channel
, imgINotificationObserver
* aObserver
,
245 mozilla::dom::Document
* aLoadingDocument
, nsIStreamListener
** listener
,
246 imgRequestProxy
** _retval
);
248 static nsresult
GetMimeTypeFromContent(const char* aContents
,
250 nsACString
& aContentType
);
253 * Returns true if the given mime type may be interpreted as an image.
255 * Some MIME types may be interpreted as both images and documents. (At the
256 * moment only "image/svg+xml" falls into this category, but there may be more
257 * in the future.) Callers which want this function to return true for such
258 * MIME types should pass AcceptedMimeTypes::IMAGES_AND_DOCUMENTS for
261 * @param aMimeType The MIME type to evaluate.
262 * @param aAcceptedMimeTypes Which kinds of MIME types to treat as images.
264 static bool SupportImageWithMimeType(
265 const nsACString
&, AcceptedMimeTypes aAccept
= AcceptedMimeTypes::IMAGES
);
267 static void GlobalInit(); // for use by the factory
268 static void Shutdown(); // for use by the factory
269 static void ShutdownMemoryReporter();
271 enum class ClearOption
{
275 using ClearOptions
= mozilla::EnumSet
<ClearOption
>;
276 nsresult
ClearImageCache(ClearOptions
= {});
277 void MinimizeCache() { ClearImageCache({ClearOption::UnusedOnly
}); }
279 nsresult
InitCache();
281 bool RemoveFromCache(const ImageCacheKey
& aKey
);
283 // Enumeration describing if a given entry is in the cache queue or not.
284 // There are some cases we know the entry is definitely not in the queue.
285 enum class QueueState
{ MaybeExists
, AlreadyRemoved
};
287 bool RemoveFromCache(imgCacheEntry
* entry
,
288 QueueState aQueueState
= QueueState::MaybeExists
);
290 bool PutIntoCache(const ImageCacheKey
& aKey
, imgCacheEntry
* aEntry
);
292 void AddToUncachedImages(imgRequest
* aRequest
);
293 void RemoveFromUncachedImages(imgRequest
* aRequest
);
295 // Returns true if we should prefer evicting cache entry |two| over cache
297 // This mixes units in the worst way, but provides reasonable results.
298 inline static bool CompareCacheEntries(const RefPtr
<imgCacheEntry
>& one
,
299 const RefPtr
<imgCacheEntry
>& two
) {
307 const double sizeweight
= 1.0 - sCacheTimeWeight
;
309 // We want large, old images to be evicted first (depending on their
310 // relative weights). Since a larger time is actually newer, we subtract
311 // time's weight, so an older image has a larger weight.
312 double oneweight
= double(one
->GetDataSize()) * sizeweight
-
313 double(one
->GetTouchedTime()) * sCacheTimeWeight
;
314 double twoweight
= double(two
->GetDataSize()) * sizeweight
-
315 double(two
->GetTouchedTime()) * sCacheTimeWeight
;
317 return oneweight
< twoweight
;
320 void VerifyCacheSizes();
322 nsresult
RemoveEntriesInternal(nsIPrincipal
* aPrincipal
,
323 const nsACString
* aBaseDomain
);
325 // The image loader maintains a hash table of all imgCacheEntries. However,
326 // only some of them will be evicted from the cache: those who have no
327 // imgRequestProxies watching their imgRequests.
329 // Once an imgRequest has no imgRequestProxies, it should notify us by
330 // calling HasNoObservers(), and null out its cache entry pointer.
332 // Upon having a proxy start observing again, it should notify us by calling
333 // HasObservers(). The request's cache entry will be re-set before this
334 // happens, by calling imgRequest::SetCacheEntry() when an entry with no
335 // observers is re-requested.
336 bool SetHasNoProxies(imgRequest
* aRequest
, imgCacheEntry
* aEntry
);
337 bool SetHasProxies(imgRequest
* aRequest
);
340 static already_AddRefed
<imgLoader
> CreateImageLoader();
342 bool ValidateEntry(imgCacheEntry
* aEntry
, nsIURI
* aURI
,
343 nsIURI
* aInitialDocumentURI
,
344 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
345 imgINotificationObserver
* aObserver
,
346 mozilla::dom::Document
* aLoadingDocument
,
347 nsLoadFlags aLoadFlags
,
348 nsContentPolicyType aLoadPolicyType
,
349 bool aCanMakeNewChannel
, bool* aNewChannelCreated
,
350 imgRequestProxy
** aProxyRequest
,
351 nsIPrincipal
* aTriggeringPrincipal
, mozilla::CORSMode
,
352 bool aLinkPreload
, uint64_t aEarlyHintPreloaderId
);
354 bool ValidateRequestWithNewChannel(
355 imgRequest
* request
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
356 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
357 imgINotificationObserver
* aObserver
,
358 mozilla::dom::Document
* aLoadingDocument
, uint64_t aInnerWindowId
,
359 nsLoadFlags aLoadFlags
, nsContentPolicyType aContentPolicyType
,
360 imgRequestProxy
** aProxyRequest
, nsIPrincipal
* aLoadingPrincipal
,
361 mozilla::CORSMode
, bool aLinkPreload
, uint64_t aEarlyHintPreloaderId
,
362 bool* aNewChannelCreated
);
364 void NotifyObserversForCachedImage(imgCacheEntry
* aEntry
, imgRequest
* request
,
366 nsIReferrerInfo
* aReferrerInfo
,
367 mozilla::dom::Document
* aLoadingDocument
,
368 nsIPrincipal
* aLoadingPrincipal
,
370 uint64_t aEarlyHintPreloaderId
);
371 // aURI may be different from imgRequest's URI in the case of blob URIs, as we
372 // can share requests with different URIs.
373 nsresult
CreateNewProxyForRequest(imgRequest
* aRequest
, nsIURI
* aURI
,
374 nsILoadGroup
* aLoadGroup
,
375 mozilla::dom::Document
* aLoadingDocument
,
376 imgINotificationObserver
* aObserver
,
377 nsLoadFlags aLoadFlags
,
378 imgRequestProxy
** _retval
);
380 nsresult
EvictEntries(bool aChromeOnly
);
382 void CacheEntriesChanged(int32_t aSizeDiff
);
383 void CheckCacheLimits();
386 friend class imgCacheEntry
;
387 friend class imgMemoryReporter
;
389 imgCacheTable mCache
;
390 imgCacheQueue mCacheQueue
;
392 // Hash set of every imgRequest for this loader that isn't in mCache or
393 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
394 // mUncachedImages should be every imgRequest that is alive. These are weak
395 // pointers so we rely on the imgRequest destructor to remove itself.
396 imgSet mUncachedImages
MOZ_GUARDED_BY(mUncachedImagesMutex
);
397 // The imgRequest can have refs to them held on non-main thread, so we need
398 // a mutex because we modify the uncached images set from the imgRequest
400 Mutex mUncachedImagesMutex
;
402 static double sCacheTimeWeight
;
403 static uint32_t sCacheMaxSize
;
404 static imgMemoryReporter
* sMemReporter
;
406 mozilla::UniquePtr
<imgCacheExpirationTracker
> mCacheTracker
;
407 bool mRespectPrivacy
;
411 * proxy stream listener class used to handle multipart/x-mixed-replace
414 #include "nsCOMPtr.h"
415 #include "nsIStreamListener.h"
416 #include "nsIThreadRetargetableStreamListener.h"
418 class ProxyListener
: public nsIStreamListener
,
419 public nsIThreadRetargetableStreamListener
{
421 explicit ProxyListener(nsIStreamListener
* dest
);
423 /* additional members */
424 NS_DECL_THREADSAFE_ISUPPORTS
425 NS_DECL_NSISTREAMLISTENER
426 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
427 NS_DECL_NSIREQUESTOBSERVER
430 virtual ~ProxyListener();
432 nsCOMPtr
<nsIStreamListener
> mDestListener
;
436 * A class that implements nsIProgressEventSink and forwards all calls to it to
437 * the original notification callbacks of the channel. Also implements
438 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
439 * and forwards everything else to the channel's notification callbacks.
441 class nsProgressNotificationProxy final
: public nsIProgressEventSink
,
442 public nsIChannelEventSink
,
443 public nsIInterfaceRequestor
{
445 nsProgressNotificationProxy(nsIChannel
* channel
, imgIRequest
* proxy
)
446 : mImageRequest(proxy
) {
447 channel
->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks
));
451 NS_DECL_NSIPROGRESSEVENTSINK
452 NS_DECL_NSICHANNELEVENTSINK
453 NS_DECL_NSIINTERFACEREQUESTOR
455 ~nsProgressNotificationProxy() = default;
457 nsCOMPtr
<nsIInterfaceRequestor
> mOriginalCallbacks
;
458 nsCOMPtr
<nsIRequest
> mImageRequest
;
465 #include "nsCOMArray.h"
467 class imgCacheValidator
: public nsIStreamListener
,
468 public nsIThreadRetargetableStreamListener
,
469 public nsIChannelEventSink
,
470 public nsIInterfaceRequestor
,
471 public nsIAsyncVerifyRedirectCallback
{
473 imgCacheValidator(nsProgressNotificationProxy
* progress
, imgLoader
* loader
,
474 imgRequest
* aRequest
, mozilla::dom::Document
* aDocument
,
475 uint64_t aInnerWindowId
,
476 bool forcePrincipalCheckForCacheEntry
);
478 void AddProxy(imgRequestProxy
* aProxy
);
479 void RemoveProxy(imgRequestProxy
* aProxy
);
481 NS_DECL_THREADSAFE_ISUPPORTS
482 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
483 NS_DECL_NSISTREAMLISTENER
484 NS_DECL_NSIREQUESTOBSERVER
485 NS_DECL_NSICHANNELEVENTSINK
486 NS_DECL_NSIINTERFACEREQUESTOR
487 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
490 void UpdateProxies(bool aCancelRequest
, bool aSyncNotify
);
491 virtual ~imgCacheValidator();
493 nsCOMPtr
<nsIStreamListener
> mDestListener
;
494 RefPtr
<nsProgressNotificationProxy
> mProgressProxy
;
495 nsCOMPtr
<nsIAsyncVerifyRedirectCallback
> mRedirectCallback
;
496 nsCOMPtr
<nsIChannel
> mRedirectChannel
;
498 RefPtr
<imgRequest
> mRequest
;
499 AutoTArray
<RefPtr
<imgRequestProxy
>, 4> mProxies
;
501 RefPtr
<imgRequest
> mNewRequest
;
502 RefPtr
<imgCacheEntry
> mNewEntry
;
504 RefPtr
<mozilla::dom::Document
> mDocument
;
505 uint64_t mInnerWindowId
;
507 imgLoader
* mImgLoader
;
509 bool mHadInsecureRedirect
;
512 #endif // mozilla_image_imgLoader_h