Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / image / imgLoader.h
blobb83adb3786e103c3f063a32bbc4a01b00179dce5
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"
29 #include "mozilla/dom/CacheExpirationTime.h"
31 class imgLoader;
32 class imgRequestProxy;
33 class imgINotificationObserver;
34 class nsILoadGroup;
35 class imgCacheExpirationTracker;
36 class imgMemoryReporter;
38 namespace mozilla {
39 namespace dom {
40 class Document;
41 enum class FetchPriority : uint8_t;
42 } // namespace dom
43 } // namespace mozilla
45 class imgCacheEntry {
46 public:
47 NS_INLINE_DECL_REFCOUNTING(imgCacheEntry)
49 imgCacheEntry(imgLoader* loader, imgRequest* request,
50 bool aForcePrincipalCheck);
52 uint32_t GetDataSize() const { return mDataSize; }
53 void SetDataSize(uint32_t aDataSize) {
54 int32_t oldsize = mDataSize;
55 mDataSize = aDataSize;
56 UpdateCache(mDataSize - oldsize);
59 int32_t GetTouchedTime() const { return mTouchedTime; }
60 void SetTouchedTime(int32_t time) {
61 mTouchedTime = time;
62 Touch(/* updateTime = */ false);
65 uint32_t GetLoadTime() const { return mLoadTime; }
67 void UpdateLoadTime();
69 const CacheExpirationTime& GetExpiryTime() const { return mExpiryTime; }
71 void AccumulateExpiryTime(const CacheExpirationTime& aExpiryTime,
72 bool aForceTouch = false) {
73 if (aExpiryTime.IsNever()) {
74 if (aForceTouch) {
75 Touch();
77 return;
79 if (mExpiryTime.IsNever() || aExpiryTime.IsShorterThan(mExpiryTime)) {
80 mExpiryTime = aExpiryTime;
81 Touch();
82 } else {
83 if (aForceTouch) {
84 Touch();
89 bool GetMustValidate() const { return mMustValidate; }
90 void SetMustValidate(bool aValidate) {
91 mMustValidate = aValidate;
92 Touch();
95 already_AddRefed<imgRequest> GetRequest() const {
96 RefPtr<imgRequest> req = mRequest;
97 return req.forget();
100 bool Evicted() const { return mEvicted; }
102 nsExpirationState* GetExpirationState() { return &mExpirationState; }
104 bool HasNoProxies() const { return mHasNoProxies; }
106 bool ForcePrincipalCheck() const { return mForcePrincipalCheck; }
108 bool HasNotified() const { return mHasNotified; }
109 void SetHasNotified() {
110 MOZ_ASSERT(!mHasNotified);
111 mHasNotified = true;
114 imgLoader* Loader() const { return mLoader; }
116 private: // methods
117 friend class imgLoader;
118 friend class imgCacheQueue;
119 void Touch(bool updateTime = true);
120 void UpdateCache(int32_t diff = 0);
121 void SetEvicted(bool evict) { mEvicted = evict; }
122 void SetHasNoProxies(bool hasNoProxies);
124 // Private, unimplemented copy constructor.
125 imgCacheEntry(const imgCacheEntry&);
126 ~imgCacheEntry();
128 private: // data
129 imgLoader* mLoader;
130 RefPtr<imgRequest> mRequest;
131 uint32_t mDataSize;
132 int32_t mTouchedTime;
133 uint32_t mLoadTime;
134 CacheExpirationTime mExpiryTime;
135 nsExpirationState mExpirationState;
136 bool mMustValidate : 1;
137 bool mEvicted : 1;
138 bool mHasNoProxies : 1;
139 bool mForcePrincipalCheck : 1;
140 bool mHasNotified : 1;
143 #include <vector>
145 #define NS_IMGLOADER_CID \
146 { /* c1354898-e3fe-4602-88a7-c4520c21cb4e */ \
147 0xc1354898, 0xe3fe, 0x4602, { \
148 0x88, 0xa7, 0xc4, 0x52, 0x0c, 0x21, 0xcb, 0x4e \
152 class imgCacheQueue {
153 public:
154 imgCacheQueue();
155 void Remove(imgCacheEntry*);
156 void Push(imgCacheEntry*);
157 void MarkDirty();
158 bool IsDirty();
159 already_AddRefed<imgCacheEntry> Pop();
160 void Refresh();
161 uint32_t GetSize() const;
162 void UpdateSize(int32_t diff);
163 uint32_t GetNumElements() const;
164 bool Contains(imgCacheEntry* aEntry) const;
165 typedef nsTArray<RefPtr<imgCacheEntry>> queueContainer;
166 typedef queueContainer::iterator iterator;
167 typedef queueContainer::const_iterator const_iterator;
169 iterator begin();
170 const_iterator begin() const;
171 iterator end();
172 const_iterator end() const;
174 private:
175 queueContainer mQueue;
176 bool mDirty;
177 uint32_t mSize;
180 enum class AcceptedMimeTypes : uint8_t {
181 IMAGES,
182 IMAGES_AND_DOCUMENTS,
185 class imgLoader final : public imgILoader,
186 public nsIContentSniffer,
187 public imgICache,
188 public nsSupportsWeakReference,
189 public nsIObserver {
190 virtual ~imgLoader();
192 public:
193 using ImageCacheKey = mozilla::image::ImageCacheKey;
194 using imgCacheTable =
195 nsRefPtrHashtable<nsGenericHashKey<ImageCacheKey>, imgCacheEntry>;
196 using imgSet = nsTHashSet<imgRequest*>;
197 using Mutex = mozilla::Mutex;
199 NS_DECL_ISUPPORTS
200 NS_DECL_IMGILOADER
201 NS_DECL_NSICONTENTSNIFFER
202 NS_DECL_IMGICACHE
203 NS_DECL_NSIOBSERVER
206 * Get the normal image loader instance that is used by gecko code, creating
207 * it if necessary.
209 static imgLoader* NormalLoader();
212 * Get the Private Browsing image loader instance that is used by gecko code,
213 * creating it if necessary.
215 static imgLoader* PrivateBrowsingLoader();
218 * Gecko code should use NormalLoader() or PrivateBrowsingLoader() to get the
219 * appropriate image loader.
221 * This constructor is public because the XPCOM module code that creates
222 * instances of "@mozilla.org/image/loader;1" / "@mozilla.org/image/cache;1"
223 * for nsIComponentManager.createInstance()/nsIServiceManager.getService()
224 * calls (now only made by add-ons) needs access to it.
226 * XXX We would like to get rid of the nsIServiceManager.getService (and
227 * nsIComponentManager.createInstance) method of creating imgLoader objects,
228 * but there are add-ons that are still using it. These add-ons don't
229 * actually do anything useful with the loaders that they create since nobody
230 * who creates an imgLoader using this method actually QIs to imgILoader and
231 * loads images. They all just QI to imgICache and either call clearCache()
232 * or findEntryProperties(). Since they're doing this on an imgLoader that
233 * has never loaded images, these calls are useless. It seems likely that
234 * the code that is doing this is just legacy code left over from a time when
235 * there was only one imgLoader instance for the entire process. (Nowadays
236 * the correct method to get an imgILoader/imgICache is to call
237 * imgITools::getImgCacheForDocument/imgITools::getImgLoaderForDocument.)
238 * All the same, even though what these add-ons are doing is a no-op,
239 * removing the nsIServiceManager.getService method of creating/getting an
240 * imgLoader objects would cause an exception in these add-ons that could
241 * break things.
243 imgLoader();
244 nsresult Init();
246 bool IsImageAvailable(nsIURI*, nsIPrincipal* aTriggeringPrincipal,
247 mozilla::CORSMode, mozilla::dom::Document*);
249 [[nodiscard]] nsresult LoadImage(
250 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
251 nsIPrincipal* aLoadingPrincipal, uint64_t aRequestContextID,
252 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
253 nsINode* aContext, mozilla::dom::Document* aLoadingDocument,
254 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
255 nsContentPolicyType aContentPolicyType, const nsAString& initiatorType,
256 bool aUseUrgentStartForChannel, bool aLinkPreload,
257 uint64_t aEarlyHintPreloaderId,
258 mozilla::dom::FetchPriority aFetchPriority, imgRequestProxy** _retval);
260 [[nodiscard]] nsresult LoadImageWithChannel(
261 nsIChannel* channel, imgINotificationObserver* aObserver,
262 mozilla::dom::Document* aLoadingDocument, nsIStreamListener** listener,
263 imgRequestProxy** _retval);
265 static nsresult GetMimeTypeFromContent(const char* aContents,
266 uint32_t aLength,
267 nsACString& aContentType);
270 * Returns true if the given mime type may be interpreted as an image.
272 * Some MIME types may be interpreted as both images and documents. (At the
273 * moment only "image/svg+xml" falls into this category, but there may be more
274 * in the future.) Callers which want this function to return true for such
275 * MIME types should pass AcceptedMimeTypes::IMAGES_AND_DOCUMENTS for
276 * @aAccept.
278 * @param aMimeType The MIME type to evaluate.
279 * @param aAcceptedMimeTypes Which kinds of MIME types to treat as images.
281 static bool SupportImageWithMimeType(
282 const nsACString&, AcceptedMimeTypes aAccept = AcceptedMimeTypes::IMAGES);
284 static void GlobalInit(); // for use by the factory
285 static void Shutdown(); // for use by the factory
286 static void ShutdownMemoryReporter();
288 enum class ClearOption {
289 ChromeOnly,
290 UnusedOnly,
292 using ClearOptions = mozilla::EnumSet<ClearOption>;
293 nsresult ClearImageCache(ClearOptions = {});
294 void MinimizeCache() { ClearImageCache({ClearOption::UnusedOnly}); }
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
313 // entry |one|.
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) {
317 if (!one) {
318 return false;
320 if (!two) {
321 return true;
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(
340 const mozilla::Maybe<nsCOMPtr<nsIPrincipal>>& aPrincipal,
341 const mozilla::Maybe<nsCString>& aSchemelessSite,
342 const mozilla::Maybe<mozilla::OriginAttributesPattern>& aPattern);
344 // The image loader maintains a hash table of all imgCacheEntries. However,
345 // only some of them will be evicted from the cache: those who have no
346 // imgRequestProxies watching their imgRequests.
348 // Once an imgRequest has no imgRequestProxies, it should notify us by
349 // calling HasNoObservers(), and null out its cache entry pointer.
351 // Upon having a proxy start observing again, it should notify us by calling
352 // HasObservers(). The request's cache entry will be re-set before this
353 // happens, by calling imgRequest::SetCacheEntry() when an entry with no
354 // observers is re-requested.
355 bool SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry);
356 bool SetHasProxies(imgRequest* aRequest);
358 private: // methods
359 static already_AddRefed<imgLoader> CreateImageLoader();
361 bool ValidateEntry(imgCacheEntry* aEntry, nsIURI* aURI,
362 nsIURI* aInitialDocumentURI,
363 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
364 imgINotificationObserver* aObserver,
365 mozilla::dom::Document* aLoadingDocument,
366 nsLoadFlags aLoadFlags,
367 nsContentPolicyType aLoadPolicyType,
368 bool aCanMakeNewChannel, bool* aNewChannelCreated,
369 imgRequestProxy** aProxyRequest,
370 nsIPrincipal* aTriggeringPrincipal, mozilla::CORSMode,
371 bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
372 mozilla::dom::FetchPriority aFetchPriority);
374 bool ValidateRequestWithNewChannel(
375 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
376 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
377 imgINotificationObserver* aObserver,
378 mozilla::dom::Document* aLoadingDocument, uint64_t aInnerWindowId,
379 nsLoadFlags aLoadFlags, nsContentPolicyType aContentPolicyType,
380 imgRequestProxy** aProxyRequest, nsIPrincipal* aLoadingPrincipal,
381 mozilla::CORSMode, bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
382 mozilla::dom::FetchPriority aFetchPriority, bool* aNewChannelCreated);
384 void NotifyObserversForCachedImage(
385 imgCacheEntry* aEntry, imgRequest* request, nsIURI* aURI,
386 nsIReferrerInfo* aReferrerInfo, mozilla::dom::Document* aLoadingDocument,
387 nsIPrincipal* aTriggeringPrincipal, mozilla::CORSMode,
388 uint64_t aEarlyHintPreloaderId,
389 mozilla::dom::FetchPriority aFetchPriority);
390 // aURI may be different from imgRequest's URI in the case of blob URIs, as we
391 // can share requests with different URIs.
392 nsresult CreateNewProxyForRequest(imgRequest* aRequest, nsIURI* aURI,
393 nsILoadGroup* aLoadGroup,
394 mozilla::dom::Document* aLoadingDocument,
395 imgINotificationObserver* aObserver,
396 nsLoadFlags aLoadFlags,
397 imgRequestProxy** _retval);
399 nsresult EvictEntries(bool aChromeOnly);
401 void CacheEntriesChanged(int32_t aSizeDiff);
402 void CheckCacheLimits();
404 private: // data
405 friend class imgCacheEntry;
406 friend class imgMemoryReporter;
408 imgCacheTable mCache;
409 imgCacheQueue mCacheQueue;
411 // Hash set of every imgRequest for this loader that isn't in mCache or
412 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
413 // mUncachedImages should be every imgRequest that is alive. These are weak
414 // pointers so we rely on the imgRequest destructor to remove itself.
415 imgSet mUncachedImages MOZ_GUARDED_BY(mUncachedImagesMutex);
416 // The imgRequest can have refs to them held on non-main thread, so we need
417 // a mutex because we modify the uncached images set from the imgRequest
418 // destructor.
419 Mutex mUncachedImagesMutex;
421 static double sCacheTimeWeight;
422 static uint32_t sCacheMaxSize;
423 static imgMemoryReporter* sMemReporter;
425 mozilla::UniquePtr<imgCacheExpirationTracker> mCacheTracker;
426 bool mRespectPrivacy;
430 * proxy stream listener class used to handle multipart/x-mixed-replace
433 #include "nsCOMPtr.h"
434 #include "nsIStreamListener.h"
435 #include "nsIThreadRetargetableStreamListener.h"
437 class ProxyListener : public nsIThreadRetargetableStreamListener {
438 public:
439 explicit ProxyListener(nsIStreamListener* dest);
441 /* additional members */
442 NS_DECL_THREADSAFE_ISUPPORTS
443 NS_DECL_NSISTREAMLISTENER
444 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
445 NS_DECL_NSIREQUESTOBSERVER
447 private:
448 virtual ~ProxyListener();
450 nsCOMPtr<nsIStreamListener> mDestListener;
454 * A class that implements nsIProgressEventSink and forwards all calls to it to
455 * the original notification callbacks of the channel. Also implements
456 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
457 * and forwards everything else to the channel's notification callbacks.
459 class nsProgressNotificationProxy final : public nsIProgressEventSink,
460 public nsIChannelEventSink,
461 public nsIInterfaceRequestor {
462 public:
463 nsProgressNotificationProxy(nsIChannel* channel, imgIRequest* proxy)
464 : mImageRequest(proxy) {
465 channel->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks));
468 NS_DECL_ISUPPORTS
469 NS_DECL_NSIPROGRESSEVENTSINK
470 NS_DECL_NSICHANNELEVENTSINK
471 NS_DECL_NSIINTERFACEREQUESTOR
472 private:
473 ~nsProgressNotificationProxy() = default;
475 nsCOMPtr<nsIInterfaceRequestor> mOriginalCallbacks;
476 nsCOMPtr<nsIRequest> mImageRequest;
480 * validate checker
483 #include "nsCOMArray.h"
485 class imgCacheValidator : public nsIThreadRetargetableStreamListener,
486 public nsIChannelEventSink,
487 public nsIInterfaceRequestor,
488 public nsIAsyncVerifyRedirectCallback {
489 public:
490 imgCacheValidator(nsProgressNotificationProxy* progress, imgLoader* loader,
491 imgRequest* aRequest, mozilla::dom::Document* aDocument,
492 uint64_t aInnerWindowId,
493 bool forcePrincipalCheckForCacheEntry);
495 void AddProxy(imgRequestProxy* aProxy);
496 void RemoveProxy(imgRequestProxy* aProxy);
498 NS_DECL_THREADSAFE_ISUPPORTS
499 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
500 NS_DECL_NSISTREAMLISTENER
501 NS_DECL_NSIREQUESTOBSERVER
502 NS_DECL_NSICHANNELEVENTSINK
503 NS_DECL_NSIINTERFACEREQUESTOR
504 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
506 private:
507 void UpdateProxies(bool aCancelRequest, bool aSyncNotify);
508 virtual ~imgCacheValidator();
510 nsCOMPtr<nsIStreamListener> mDestListener;
511 RefPtr<nsProgressNotificationProxy> mProgressProxy;
512 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
513 nsCOMPtr<nsIChannel> mRedirectChannel;
515 RefPtr<imgRequest> mRequest;
516 AutoTArray<RefPtr<imgRequestProxy>, 4> mProxies;
518 RefPtr<imgRequest> mNewRequest;
519 RefPtr<imgCacheEntry> mNewEntry;
521 RefPtr<mozilla::dom::Document> mDocument;
522 uint64_t mInnerWindowId;
524 imgLoader* mImgLoader;
526 bool mHadInsecureRedirect;
529 #endif // mozilla_image_imgLoader_h