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/Mutex.h"
12 #include "mozilla/UniquePtr.h"
14 #include "imgILoader.h"
15 #include "imgICache.h"
16 #include "nsWeakReference.h"
17 #include "nsIContentSniffer.h"
18 #include "nsRefPtrHashtable.h"
19 #include "nsExpirationTracker.h"
20 #include "ImageCacheKey.h"
21 #include "imgRequest.h"
22 #include "nsIProgressEventSink.h"
23 #include "nsIChannel.h"
24 #include "nsIThreadRetargetableStreamListener.h"
25 #include "imgIRequest.h"
28 class imgRequestProxy
;
29 class imgINotificationObserver
;
31 class imgCacheExpirationTracker
;
32 class imgMemoryReporter
;
35 namespace image
{} // namespace image
36 } // namespace mozilla
40 static uint32_t SecondsFromPRTime(PRTime prTime
);
42 imgCacheEntry(imgLoader
* loader
, imgRequest
* request
,
43 bool aForcePrincipalCheck
);
47 MOZ_ASSERT(int32_t(mRefCnt
) >= 0, "illegal refcnt");
48 NS_ASSERT_OWNINGTHREAD(imgCacheEntry
);
50 NS_LOG_ADDREF(this, mRefCnt
, "imgCacheEntry", sizeof(*this));
55 MOZ_ASSERT(0 != mRefCnt
, "dup release");
56 NS_ASSERT_OWNINGTHREAD(imgCacheEntry
);
58 NS_LOG_RELEASE(this, mRefCnt
, "imgCacheEntry");
60 mRefCnt
= 1; /* stabilize */
67 uint32_t GetDataSize() const { return mDataSize
; }
68 void SetDataSize(uint32_t aDataSize
) {
69 int32_t oldsize
= mDataSize
;
70 mDataSize
= aDataSize
;
71 UpdateCache(mDataSize
- oldsize
);
74 int32_t GetTouchedTime() const { return mTouchedTime
; }
75 void SetTouchedTime(int32_t time
) {
77 Touch(/* updateTime = */ false);
80 uint32_t GetLoadTime() const { return mLoadTime
; }
82 void UpdateLoadTime();
84 int32_t GetExpiryTime() const { return mExpiryTime
; }
85 void SetExpiryTime(int32_t aExpiryTime
) {
86 mExpiryTime
= aExpiryTime
;
90 bool GetMustValidate() const { return mMustValidate
; }
91 void SetMustValidate(bool aValidate
) {
92 mMustValidate
= aValidate
;
96 already_AddRefed
<imgRequest
> GetRequest() const {
97 RefPtr
<imgRequest
> req
= mRequest
;
101 bool Evicted() const { return mEvicted
; }
103 nsExpirationState
* GetExpirationState() { return &mExpirationState
; }
105 bool HasNoProxies() const { return mHasNoProxies
; }
107 bool ForcePrincipalCheck() const { return mForcePrincipalCheck
; }
109 imgLoader
* Loader() const { return mLoader
; }
112 friend class imgLoader
;
113 friend class imgCacheQueue
;
114 void Touch(bool updateTime
= true);
115 void UpdateCache(int32_t diff
= 0);
116 void SetEvicted(bool evict
) { mEvicted
= evict
; }
117 void SetHasNoProxies(bool hasNoProxies
);
119 // Private, unimplemented copy constructor.
120 imgCacheEntry(const imgCacheEntry
&);
123 nsAutoRefCnt mRefCnt
;
127 RefPtr
<imgRequest
> mRequest
;
129 int32_t mTouchedTime
;
132 nsExpirationState mExpirationState
;
133 bool mMustValidate
: 1;
135 bool mHasNoProxies
: 1;
136 bool mForcePrincipalCheck
: 1;
141 #define NS_IMGLOADER_CID \
142 { /* c1354898-e3fe-4602-88a7-c4520c21cb4e */ \
143 0xc1354898, 0xe3fe, 0x4602, { \
144 0x88, 0xa7, 0xc4, 0x52, 0x0c, 0x21, 0xcb, 0x4e \
148 class imgCacheQueue
{
151 void Remove(imgCacheEntry
*);
152 void Push(imgCacheEntry
*);
155 already_AddRefed
<imgCacheEntry
> Pop();
157 uint32_t GetSize() const;
158 void UpdateSize(int32_t diff
);
159 uint32_t GetNumElements() const;
160 bool Contains(imgCacheEntry
* aEntry
) const;
161 typedef nsTArray
<RefPtr
<imgCacheEntry
>> queueContainer
;
162 typedef queueContainer::iterator iterator
;
163 typedef queueContainer::const_iterator const_iterator
;
166 const_iterator
begin() const;
168 const_iterator
end() const;
171 queueContainer mQueue
;
176 enum class AcceptedMimeTypes
: uint8_t {
178 IMAGES_AND_DOCUMENTS
,
181 class imgLoader final
: public imgILoader
,
182 public nsIContentSniffer
,
184 public nsSupportsWeakReference
,
186 virtual ~imgLoader();
189 typedef mozilla::image::ImageCacheKey ImageCacheKey
;
190 typedef nsRefPtrHashtable
<nsGenericHashKey
<ImageCacheKey
>, imgCacheEntry
>
192 typedef nsTHashtable
<nsPtrHashKey
<imgRequest
>> imgSet
;
193 typedef mozilla::Mutex Mutex
;
197 NS_DECL_NSICONTENTSNIFFER
202 * Get the normal image loader instance that is used by gecko code, creating
205 static imgLoader
* NormalLoader();
208 * Get the Private Browsing image loader instance that is used by gecko code,
209 * creating it if necessary.
211 static imgLoader
* PrivateBrowsingLoader();
214 * Gecko code should use NormalLoader() or PrivateBrowsingLoader() to get the
215 * appropriate image loader.
217 * This constructor is public because the XPCOM module code that creates
218 * instances of "@mozilla.org/image/loader;1" / "@mozilla.org/image/cache;1"
219 * for nsIComponentManager.createInstance()/nsIServiceManager.getService()
220 * calls (now only made by add-ons) needs access to it.
222 * XXX We would like to get rid of the nsIServiceManager.getService (and
223 * nsIComponentManager.createInstance) method of creating imgLoader objects,
224 * but there are add-ons that are still using it. These add-ons don't
225 * actually do anything useful with the loaders that they create since nobody
226 * who creates an imgLoader using this method actually QIs to imgILoader and
227 * loads images. They all just QI to imgICache and either call clearCache()
228 * or findEntryProperties(). Since they're doing this on an imgLoader that
229 * has never loaded images, these calls are useless. It seems likely that
230 * the code that is doing this is just legacy code left over from a time when
231 * there was only one imgLoader instance for the entire process. (Nowadays
232 * the correct method to get an imgILoader/imgICache is to call
233 * imgITools::getImgCacheForDocument/imgITools::getImgLoaderForDocument.)
234 * All the same, even though what these add-ons are doing is a no-op,
235 * removing the nsIServiceManager.getService method of creating/getting an
236 * imgLoader objects would cause an exception in these add-ons that could
242 MOZ_MUST_USE nsresult
LoadImage(
243 nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
, nsIReferrerInfo
* aReferrerInfo
,
244 nsIPrincipal
* aLoadingPrincipal
, uint64_t aRequestContextID
,
245 nsILoadGroup
* aLoadGroup
, imgINotificationObserver
* aObserver
,
246 nsINode
* aContext
, mozilla::dom::Document
* aLoadingDocument
,
247 nsLoadFlags aLoadFlags
, nsISupports
* aCacheKey
,
248 nsContentPolicyType aContentPolicyType
, const nsAString
& initiatorType
,
249 bool aUseUrgentStartForChannel
, imgRequestProxy
** _retval
);
251 MOZ_MUST_USE nsresult
252 LoadImageWithChannel(nsIChannel
* channel
, imgINotificationObserver
* aObserver
,
253 nsISupports
* aCX
, nsIStreamListener
** listener
,
254 imgRequestProxy
** _retval
);
256 static nsresult
GetMimeTypeFromContent(const char* aContents
,
258 nsACString
& aContentType
);
261 * Returns true if the given mime type may be interpreted as an image.
263 * Some MIME types may be interpreted as both images and documents. (At the
264 * moment only "image/svg+xml" falls into this category, but there may be more
265 * in the future.) Callers which want this function to return true for such
266 * MIME types should pass AcceptedMimeTypes::IMAGES_AND_DOCUMENTS for
269 * @param aMimeType The MIME type to evaluate.
270 * @param aAcceptedMimeTypes Which kinds of MIME types to treat as images.
272 static bool SupportImageWithMimeType(
273 const char* aMimeType
,
274 AcceptedMimeTypes aAccept
= AcceptedMimeTypes::IMAGES
);
276 static void GlobalInit(); // for use by the factory
277 static void Shutdown(); // for use by the factory
278 static void ShutdownMemoryReporter();
280 nsresult
ClearChromeImageCache();
281 nsresult
ClearImageCache();
282 void MinimizeCaches();
284 nsresult
InitCache();
286 bool RemoveFromCache(const ImageCacheKey
& aKey
);
288 // Enumeration describing if a given entry is in the cache queue or not.
289 // There are some cases we know the entry is definitely not in the queue.
290 enum class QueueState
{ MaybeExists
, AlreadyRemoved
};
292 bool RemoveFromCache(imgCacheEntry
* entry
,
293 QueueState aQueueState
= QueueState::MaybeExists
);
295 bool PutIntoCache(const ImageCacheKey
& aKey
, imgCacheEntry
* aEntry
);
297 void AddToUncachedImages(imgRequest
* aRequest
);
298 void RemoveFromUncachedImages(imgRequest
* aRequest
);
300 // Returns true if we should prefer evicting cache entry |two| over cache
302 // This mixes units in the worst way, but provides reasonable results.
303 inline static bool CompareCacheEntries(const RefPtr
<imgCacheEntry
>& one
,
304 const RefPtr
<imgCacheEntry
>& two
) {
312 const double sizeweight
= 1.0 - sCacheTimeWeight
;
314 // We want large, old images to be evicted first (depending on their
315 // relative weights). Since a larger time is actually newer, we subtract
316 // time's weight, so an older image has a larger weight.
317 double oneweight
= double(one
->GetDataSize()) * sizeweight
-
318 double(one
->GetTouchedTime()) * sCacheTimeWeight
;
319 double twoweight
= double(two
->GetDataSize()) * sizeweight
-
320 double(two
->GetTouchedTime()) * sCacheTimeWeight
;
322 return oneweight
< twoweight
;
325 void VerifyCacheSizes();
327 // The image loader maintains a hash table of all imgCacheEntries. However,
328 // only some of them will be evicted from the cache: those who have no
329 // imgRequestProxies watching their imgRequests.
331 // Once an imgRequest has no imgRequestProxies, it should notify us by
332 // calling HasNoObservers(), and null out its cache entry pointer.
334 // Upon having a proxy start observing again, it should notify us by calling
335 // HasObservers(). The request's cache entry will be re-set before this
336 // happens, by calling imgRequest::SetCacheEntry() when an entry with no
337 // observers is re-requested.
338 bool SetHasNoProxies(imgRequest
* aRequest
, imgCacheEntry
* aEntry
);
339 bool SetHasProxies(imgRequest
* aRequest
);
342 static already_AddRefed
<imgLoader
> CreateImageLoader();
344 bool PreferLoadFromCache(nsIURI
* aURI
) const;
346 bool ValidateEntry(imgCacheEntry
* aEntry
, nsIURI
* aKey
,
347 nsIURI
* aInitialDocumentURI
,
348 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
349 imgINotificationObserver
* aObserver
, nsISupports
* aCX
,
350 mozilla::dom::Document
* aLoadingDocument
,
351 nsLoadFlags aLoadFlags
,
352 nsContentPolicyType aContentPolicyType
,
353 bool aCanMakeNewChannel
, bool* aNewChannelCreated
,
354 imgRequestProxy
** aProxyRequest
,
355 nsIPrincipal
* aLoadingPrincipal
, int32_t aCORSMode
);
357 bool ValidateRequestWithNewChannel(
358 imgRequest
* request
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
359 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
360 imgINotificationObserver
* aObserver
, nsISupports
* aCX
,
361 mozilla::dom::Document
* aLoadingDocument
, uint64_t aInnerWindowId
,
362 nsLoadFlags aLoadFlags
, nsContentPolicyType aContentPolicyType
,
363 imgRequestProxy
** aProxyRequest
, nsIPrincipal
* aLoadingPrincipal
,
364 int32_t aCORSMode
, bool* aNewChannelCreated
);
366 nsresult
CreateNewProxyForRequest(imgRequest
* aRequest
,
367 nsILoadGroup
* aLoadGroup
,
368 mozilla::dom::Document
* aLoadingDocument
,
369 imgINotificationObserver
* aObserver
,
370 nsLoadFlags aLoadFlags
,
371 imgRequestProxy
** _retval
);
373 nsresult
EvictEntries(imgCacheTable
& aCacheToClear
);
374 nsresult
EvictEntries(imgCacheQueue
& aQueueToClear
);
376 imgCacheTable
& GetCache(bool aForChrome
);
377 imgCacheTable
& GetCache(const ImageCacheKey
& aKey
);
378 imgCacheQueue
& GetCacheQueue(bool aForChrome
);
379 imgCacheQueue
& GetCacheQueue(const ImageCacheKey
& aKey
);
380 void CacheEntriesChanged(bool aForChrome
, int32_t aSizeDiff
= 0);
381 void CheckCacheLimits(imgCacheTable
& cache
, imgCacheQueue
& queue
);
384 friend class imgCacheEntry
;
385 friend class imgMemoryReporter
;
387 imgCacheTable mCache
;
388 imgCacheQueue mCacheQueue
;
390 imgCacheTable mChromeCache
;
391 imgCacheQueue mChromeCacheQueue
;
393 // Hash set of every imgRequest for this loader that isn't in mCache or
394 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
395 // mUncachedImages should be every imgRequest that is alive. These are weak
396 // pointers so we rely on the imgRequest destructor to remove itself.
397 imgSet mUncachedImages
;
398 // The imgRequest can have refs to them held on non-main thread, so we need
399 // a mutex because we modify the uncached images set from the imgRequest
401 Mutex mUncachedImagesMutex
;
403 static double sCacheTimeWeight
;
404 static uint32_t sCacheMaxSize
;
405 static imgMemoryReporter
* sMemReporter
;
407 mozilla::UniquePtr
<imgCacheExpirationTracker
> mCacheTracker
;
408 bool mRespectPrivacy
;
412 * proxy stream listener class used to handle multipart/x-mixed-replace
415 #include "nsCOMPtr.h"
416 #include "nsIStreamListener.h"
417 #include "nsIThreadRetargetableStreamListener.h"
419 class ProxyListener
: public nsIStreamListener
,
420 public nsIThreadRetargetableStreamListener
{
422 explicit ProxyListener(nsIStreamListener
* dest
);
424 /* additional members */
425 NS_DECL_THREADSAFE_ISUPPORTS
426 NS_DECL_NSISTREAMLISTENER
427 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
428 NS_DECL_NSIREQUESTOBSERVER
431 virtual ~ProxyListener();
433 nsCOMPtr
<nsIStreamListener
> mDestListener
;
437 * A class that implements nsIProgressEventSink and forwards all calls to it to
438 * the original notification callbacks of the channel. Also implements
439 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
440 * and forwards everything else to the channel's notification callbacks.
442 class nsProgressNotificationProxy final
: public nsIProgressEventSink
,
443 public nsIChannelEventSink
,
444 public nsIInterfaceRequestor
{
446 nsProgressNotificationProxy(nsIChannel
* channel
, imgIRequest
* proxy
)
447 : mImageRequest(proxy
) {
448 channel
->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks
));
452 NS_DECL_NSIPROGRESSEVENTSINK
453 NS_DECL_NSICHANNELEVENTSINK
454 NS_DECL_NSIINTERFACEREQUESTOR
456 ~nsProgressNotificationProxy() {}
458 nsCOMPtr
<nsIInterfaceRequestor
> mOriginalCallbacks
;
459 nsCOMPtr
<nsIRequest
> mImageRequest
;
466 #include "nsCOMArray.h"
468 class imgCacheValidator
: public nsIStreamListener
,
469 public nsIThreadRetargetableStreamListener
,
470 public nsIChannelEventSink
,
471 public nsIInterfaceRequestor
,
472 public nsIAsyncVerifyRedirectCallback
{
474 imgCacheValidator(nsProgressNotificationProxy
* progress
, imgLoader
* loader
,
475 imgRequest
* aRequest
, nsISupports
* aContext
,
476 uint64_t aInnerWindowId
,
477 bool forcePrincipalCheckForCacheEntry
);
479 void AddProxy(imgRequestProxy
* aProxy
);
480 void RemoveProxy(imgRequestProxy
* aProxy
);
482 NS_DECL_THREADSAFE_ISUPPORTS
483 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
484 NS_DECL_NSISTREAMLISTENER
485 NS_DECL_NSIREQUESTOBSERVER
486 NS_DECL_NSICHANNELEVENTSINK
487 NS_DECL_NSIINTERFACEREQUESTOR
488 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
491 void UpdateProxies(bool aCancelRequest
, bool aSyncNotify
);
492 virtual ~imgCacheValidator();
494 nsCOMPtr
<nsIStreamListener
> mDestListener
;
495 RefPtr
<nsProgressNotificationProxy
> mProgressProxy
;
496 nsCOMPtr
<nsIAsyncVerifyRedirectCallback
> mRedirectCallback
;
497 nsCOMPtr
<nsIChannel
> mRedirectChannel
;
499 RefPtr
<imgRequest
> mRequest
;
500 AutoTArray
<RefPtr
<imgRequestProxy
>, 4> mProxies
;
502 RefPtr
<imgRequest
> mNewRequest
;
503 RefPtr
<imgCacheEntry
> mNewEntry
;
505 nsCOMPtr
<nsISupports
> mContext
;
506 uint64_t mInnerWindowId
;
508 imgLoader
* mImgLoader
;
510 bool mHadInsecureRedirect
;
513 #endif // mozilla_image_imgLoader_h