Backed out 9 changesets (bug 1901851, bug 1728331) for causing remote worker crashes...
[gecko.git] / image / imgLoader.h
blobaafc80f12e206efd046dc05fd6d2367149e4a243
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"
30 class imgLoader;
31 class imgRequestProxy;
32 class imgINotificationObserver;
33 class nsILoadGroup;
34 class imgCacheExpirationTracker;
35 class imgMemoryReporter;
37 namespace mozilla {
38 namespace dom {
39 class Document;
40 enum class FetchPriority : uint8_t;
41 } // namespace dom
42 } // namespace mozilla
44 class imgCacheEntry {
45 public:
46 NS_INLINE_DECL_REFCOUNTING(imgCacheEntry)
48 imgCacheEntry(imgLoader* loader, imgRequest* request,
49 bool aForcePrincipalCheck);
51 uint32_t GetDataSize() const { return mDataSize; }
52 void SetDataSize(uint32_t aDataSize) {
53 int32_t oldsize = mDataSize;
54 mDataSize = aDataSize;
55 UpdateCache(mDataSize - oldsize);
58 int32_t GetTouchedTime() const { return mTouchedTime; }
59 void SetTouchedTime(int32_t time) {
60 mTouchedTime = time;
61 Touch(/* updateTime = */ false);
64 uint32_t GetLoadTime() const { return mLoadTime; }
66 void UpdateLoadTime();
68 uint32_t GetExpiryTime() const { return mExpiryTime; }
69 void AccumulateExpiryTime(uint32_t aExpiryTime, bool aForceTouch = false) {
70 // 0 means "doesn't expire".
71 // Otherwise, calculate the minimum value.
72 if (aExpiryTime == 0) {
73 if (aForceTouch) {
74 Touch();
76 return;
78 if (mExpiryTime == 0 || aExpiryTime < mExpiryTime) {
79 mExpiryTime = aExpiryTime;
80 Touch();
81 } else {
82 if (aForceTouch) {
83 Touch();
88 bool GetMustValidate() const { return mMustValidate; }
89 void SetMustValidate(bool aValidate) {
90 mMustValidate = aValidate;
91 Touch();
94 already_AddRefed<imgRequest> GetRequest() const {
95 RefPtr<imgRequest> req = mRequest;
96 return req.forget();
99 bool Evicted() const { return mEvicted; }
101 nsExpirationState* GetExpirationState() { return &mExpirationState; }
103 bool HasNoProxies() const { return mHasNoProxies; }
105 bool ForcePrincipalCheck() const { return mForcePrincipalCheck; }
107 bool HasNotified() const { return mHasNotified; }
108 void SetHasNotified() {
109 MOZ_ASSERT(!mHasNotified);
110 mHasNotified = true;
113 imgLoader* Loader() const { return mLoader; }
115 private: // methods
116 friend class imgLoader;
117 friend class imgCacheQueue;
118 void Touch(bool updateTime = true);
119 void UpdateCache(int32_t diff = 0);
120 void SetEvicted(bool evict) { mEvicted = evict; }
121 void SetHasNoProxies(bool hasNoProxies);
123 // Private, unimplemented copy constructor.
124 imgCacheEntry(const imgCacheEntry&);
125 ~imgCacheEntry();
127 private: // data
128 imgLoader* mLoader;
129 RefPtr<imgRequest> mRequest;
130 uint32_t mDataSize;
131 int32_t mTouchedTime;
132 uint32_t mLoadTime;
133 uint32_t mExpiryTime;
134 nsExpirationState mExpirationState;
135 bool mMustValidate : 1;
136 bool mEvicted : 1;
137 bool mHasNoProxies : 1;
138 bool mForcePrincipalCheck : 1;
139 bool mHasNotified : 1;
142 #include <vector>
144 #define NS_IMGLOADER_CID \
145 { /* c1354898-e3fe-4602-88a7-c4520c21cb4e */ \
146 0xc1354898, 0xe3fe, 0x4602, { \
147 0x88, 0xa7, 0xc4, 0x52, 0x0c, 0x21, 0xcb, 0x4e \
151 class imgCacheQueue {
152 public:
153 imgCacheQueue();
154 void Remove(imgCacheEntry*);
155 void Push(imgCacheEntry*);
156 void MarkDirty();
157 bool IsDirty();
158 already_AddRefed<imgCacheEntry> Pop();
159 void Refresh();
160 uint32_t GetSize() const;
161 void UpdateSize(int32_t diff);
162 uint32_t GetNumElements() const;
163 bool Contains(imgCacheEntry* aEntry) const;
164 typedef nsTArray<RefPtr<imgCacheEntry>> queueContainer;
165 typedef queueContainer::iterator iterator;
166 typedef queueContainer::const_iterator const_iterator;
168 iterator begin();
169 const_iterator begin() const;
170 iterator end();
171 const_iterator end() const;
173 private:
174 queueContainer mQueue;
175 bool mDirty;
176 uint32_t mSize;
179 enum class AcceptedMimeTypes : uint8_t {
180 IMAGES,
181 IMAGES_AND_DOCUMENTS,
184 class imgLoader final : public imgILoader,
185 public nsIContentSniffer,
186 public imgICache,
187 public nsSupportsWeakReference,
188 public nsIObserver {
189 virtual ~imgLoader();
191 public:
192 using ImageCacheKey = mozilla::image::ImageCacheKey;
193 using imgCacheTable =
194 nsRefPtrHashtable<nsGenericHashKey<ImageCacheKey>, imgCacheEntry>;
195 using imgSet = nsTHashSet<imgRequest*>;
196 using Mutex = mozilla::Mutex;
198 NS_DECL_ISUPPORTS
199 NS_DECL_IMGILOADER
200 NS_DECL_NSICONTENTSNIFFER
201 NS_DECL_IMGICACHE
202 NS_DECL_NSIOBSERVER
205 * Get the normal image loader instance that is used by gecko code, creating
206 * it if necessary.
208 static imgLoader* NormalLoader();
211 * Get the Private Browsing image loader instance that is used by gecko code,
212 * creating it if necessary.
214 static imgLoader* PrivateBrowsingLoader();
217 * Gecko code should use NormalLoader() or PrivateBrowsingLoader() to get the
218 * appropriate image loader.
220 * This constructor is public because the XPCOM module code that creates
221 * instances of "@mozilla.org/image/loader;1" / "@mozilla.org/image/cache;1"
222 * for nsIComponentManager.createInstance()/nsIServiceManager.getService()
223 * calls (now only made by add-ons) needs access to it.
225 * XXX We would like to get rid of the nsIServiceManager.getService (and
226 * nsIComponentManager.createInstance) method of creating imgLoader objects,
227 * but there are add-ons that are still using it. These add-ons don't
228 * actually do anything useful with the loaders that they create since nobody
229 * who creates an imgLoader using this method actually QIs to imgILoader and
230 * loads images. They all just QI to imgICache and either call clearCache()
231 * or findEntryProperties(). Since they're doing this on an imgLoader that
232 * has never loaded images, these calls are useless. It seems likely that
233 * the code that is doing this is just legacy code left over from a time when
234 * there was only one imgLoader instance for the entire process. (Nowadays
235 * the correct method to get an imgILoader/imgICache is to call
236 * imgITools::getImgCacheForDocument/imgITools::getImgLoaderForDocument.)
237 * All the same, even though what these add-ons are doing is a no-op,
238 * removing the nsIServiceManager.getService method of creating/getting an
239 * imgLoader objects would cause an exception in these add-ons that could
240 * break things.
242 imgLoader();
243 nsresult Init();
245 bool IsImageAvailable(nsIURI*, nsIPrincipal* aTriggeringPrincipal,
246 mozilla::CORSMode, mozilla::dom::Document*);
248 [[nodiscard]] nsresult LoadImage(
249 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
250 nsIPrincipal* aLoadingPrincipal, uint64_t aRequestContextID,
251 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
252 nsINode* aContext, mozilla::dom::Document* aLoadingDocument,
253 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
254 nsContentPolicyType aContentPolicyType, const nsAString& initiatorType,
255 bool aUseUrgentStartForChannel, bool aLinkPreload,
256 uint64_t aEarlyHintPreloaderId,
257 mozilla::dom::FetchPriority aFetchPriority, imgRequestProxy** _retval);
259 [[nodiscard]] nsresult LoadImageWithChannel(
260 nsIChannel* channel, imgINotificationObserver* aObserver,
261 mozilla::dom::Document* aLoadingDocument, nsIStreamListener** listener,
262 imgRequestProxy** _retval);
264 static nsresult GetMimeTypeFromContent(const char* aContents,
265 uint32_t aLength,
266 nsACString& aContentType);
269 * Returns true if the given mime type may be interpreted as an image.
271 * Some MIME types may be interpreted as both images and documents. (At the
272 * moment only "image/svg+xml" falls into this category, but there may be more
273 * in the future.) Callers which want this function to return true for such
274 * MIME types should pass AcceptedMimeTypes::IMAGES_AND_DOCUMENTS for
275 * @aAccept.
277 * @param aMimeType The MIME type to evaluate.
278 * @param aAcceptedMimeTypes Which kinds of MIME types to treat as images.
280 static bool SupportImageWithMimeType(
281 const nsACString&, AcceptedMimeTypes aAccept = AcceptedMimeTypes::IMAGES);
283 static void GlobalInit(); // for use by the factory
284 static void Shutdown(); // for use by the factory
285 static void ShutdownMemoryReporter();
287 enum class ClearOption {
288 ChromeOnly,
289 UnusedOnly,
291 using ClearOptions = mozilla::EnumSet<ClearOption>;
292 nsresult ClearImageCache(ClearOptions = {});
293 void MinimizeCache() { ClearImageCache({ClearOption::UnusedOnly}); }
295 nsresult InitCache();
297 bool RemoveFromCache(const ImageCacheKey& aKey);
299 // Enumeration describing if a given entry is in the cache queue or not.
300 // There are some cases we know the entry is definitely not in the queue.
301 enum class QueueState { MaybeExists, AlreadyRemoved };
303 bool RemoveFromCache(imgCacheEntry* entry,
304 QueueState aQueueState = QueueState::MaybeExists);
306 bool PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* aEntry);
308 void AddToUncachedImages(imgRequest* aRequest);
309 void RemoveFromUncachedImages(imgRequest* aRequest);
311 // Returns true if we should prefer evicting cache entry |two| over cache
312 // entry |one|.
313 // This mixes units in the worst way, but provides reasonable results.
314 inline static bool CompareCacheEntries(const RefPtr<imgCacheEntry>& one,
315 const RefPtr<imgCacheEntry>& two) {
316 if (!one) {
317 return false;
319 if (!two) {
320 return true;
323 const double sizeweight = 1.0 - sCacheTimeWeight;
325 // We want large, old images to be evicted first (depending on their
326 // relative weights). Since a larger time is actually newer, we subtract
327 // time's weight, so an older image has a larger weight.
328 double oneweight = double(one->GetDataSize()) * sizeweight -
329 double(one->GetTouchedTime()) * sCacheTimeWeight;
330 double twoweight = double(two->GetDataSize()) * sizeweight -
331 double(two->GetTouchedTime()) * sCacheTimeWeight;
333 return oneweight < twoweight;
336 void VerifyCacheSizes();
338 nsresult RemoveEntriesInternal(nsIPrincipal* aPrincipal,
339 const nsACString* aBaseDomain);
341 // The image loader maintains a hash table of all imgCacheEntries. However,
342 // only some of them will be evicted from the cache: those who have no
343 // imgRequestProxies watching their imgRequests.
345 // Once an imgRequest has no imgRequestProxies, it should notify us by
346 // calling HasNoObservers(), and null out its cache entry pointer.
348 // Upon having a proxy start observing again, it should notify us by calling
349 // HasObservers(). The request's cache entry will be re-set before this
350 // happens, by calling imgRequest::SetCacheEntry() when an entry with no
351 // observers is re-requested.
352 bool SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry);
353 bool SetHasProxies(imgRequest* aRequest);
355 private: // methods
356 static already_AddRefed<imgLoader> CreateImageLoader();
358 bool ValidateEntry(imgCacheEntry* aEntry, nsIURI* aURI,
359 nsIURI* aInitialDocumentURI,
360 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
361 imgINotificationObserver* aObserver,
362 mozilla::dom::Document* aLoadingDocument,
363 nsLoadFlags aLoadFlags,
364 nsContentPolicyType aLoadPolicyType,
365 bool aCanMakeNewChannel, bool* aNewChannelCreated,
366 imgRequestProxy** aProxyRequest,
367 nsIPrincipal* aTriggeringPrincipal, mozilla::CORSMode,
368 bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
369 mozilla::dom::FetchPriority aFetchPriority);
371 bool ValidateRequestWithNewChannel(
372 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
373 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
374 imgINotificationObserver* aObserver,
375 mozilla::dom::Document* aLoadingDocument, uint64_t aInnerWindowId,
376 nsLoadFlags aLoadFlags, nsContentPolicyType aContentPolicyType,
377 imgRequestProxy** aProxyRequest, nsIPrincipal* aLoadingPrincipal,
378 mozilla::CORSMode, bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
379 mozilla::dom::FetchPriority aFetchPriority, bool* aNewChannelCreated);
381 void NotifyObserversForCachedImage(
382 imgCacheEntry* aEntry, imgRequest* request, nsIURI* aURI,
383 nsIReferrerInfo* aReferrerInfo, mozilla::dom::Document* aLoadingDocument,
384 nsIPrincipal* aTriggeringPrincipal, mozilla::CORSMode,
385 uint64_t aEarlyHintPreloaderId,
386 mozilla::dom::FetchPriority aFetchPriority);
387 // aURI may be different from imgRequest's URI in the case of blob URIs, as we
388 // can share requests with different URIs.
389 nsresult CreateNewProxyForRequest(imgRequest* aRequest, nsIURI* aURI,
390 nsILoadGroup* aLoadGroup,
391 mozilla::dom::Document* aLoadingDocument,
392 imgINotificationObserver* aObserver,
393 nsLoadFlags aLoadFlags,
394 imgRequestProxy** _retval);
396 nsresult EvictEntries(bool aChromeOnly);
398 void CacheEntriesChanged(int32_t aSizeDiff);
399 void CheckCacheLimits();
401 private: // data
402 friend class imgCacheEntry;
403 friend class imgMemoryReporter;
405 imgCacheTable mCache;
406 imgCacheQueue mCacheQueue;
408 // Hash set of every imgRequest for this loader that isn't in mCache or
409 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
410 // mUncachedImages should be every imgRequest that is alive. These are weak
411 // pointers so we rely on the imgRequest destructor to remove itself.
412 imgSet mUncachedImages MOZ_GUARDED_BY(mUncachedImagesMutex);
413 // The imgRequest can have refs to them held on non-main thread, so we need
414 // a mutex because we modify the uncached images set from the imgRequest
415 // destructor.
416 Mutex mUncachedImagesMutex;
418 static double sCacheTimeWeight;
419 static uint32_t sCacheMaxSize;
420 static imgMemoryReporter* sMemReporter;
422 mozilla::UniquePtr<imgCacheExpirationTracker> mCacheTracker;
423 bool mRespectPrivacy;
427 * proxy stream listener class used to handle multipart/x-mixed-replace
430 #include "nsCOMPtr.h"
431 #include "nsIStreamListener.h"
432 #include "nsIThreadRetargetableStreamListener.h"
434 class ProxyListener : public nsIThreadRetargetableStreamListener {
435 public:
436 explicit ProxyListener(nsIStreamListener* dest);
438 /* additional members */
439 NS_DECL_THREADSAFE_ISUPPORTS
440 NS_DECL_NSISTREAMLISTENER
441 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
442 NS_DECL_NSIREQUESTOBSERVER
444 private:
445 virtual ~ProxyListener();
447 nsCOMPtr<nsIStreamListener> mDestListener;
451 * A class that implements nsIProgressEventSink and forwards all calls to it to
452 * the original notification callbacks of the channel. Also implements
453 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
454 * and forwards everything else to the channel's notification callbacks.
456 class nsProgressNotificationProxy final : public nsIProgressEventSink,
457 public nsIChannelEventSink,
458 public nsIInterfaceRequestor {
459 public:
460 nsProgressNotificationProxy(nsIChannel* channel, imgIRequest* proxy)
461 : mImageRequest(proxy) {
462 channel->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks));
465 NS_DECL_ISUPPORTS
466 NS_DECL_NSIPROGRESSEVENTSINK
467 NS_DECL_NSICHANNELEVENTSINK
468 NS_DECL_NSIINTERFACEREQUESTOR
469 private:
470 ~nsProgressNotificationProxy() = default;
472 nsCOMPtr<nsIInterfaceRequestor> mOriginalCallbacks;
473 nsCOMPtr<nsIRequest> mImageRequest;
477 * validate checker
480 #include "nsCOMArray.h"
482 class imgCacheValidator : public nsIThreadRetargetableStreamListener,
483 public nsIChannelEventSink,
484 public nsIInterfaceRequestor,
485 public nsIAsyncVerifyRedirectCallback {
486 public:
487 imgCacheValidator(nsProgressNotificationProxy* progress, imgLoader* loader,
488 imgRequest* aRequest, mozilla::dom::Document* aDocument,
489 uint64_t aInnerWindowId,
490 bool forcePrincipalCheckForCacheEntry);
492 void AddProxy(imgRequestProxy* aProxy);
493 void RemoveProxy(imgRequestProxy* aProxy);
495 NS_DECL_THREADSAFE_ISUPPORTS
496 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
497 NS_DECL_NSISTREAMLISTENER
498 NS_DECL_NSIREQUESTOBSERVER
499 NS_DECL_NSICHANNELEVENTSINK
500 NS_DECL_NSIINTERFACEREQUESTOR
501 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
503 private:
504 void UpdateProxies(bool aCancelRequest, bool aSyncNotify);
505 virtual ~imgCacheValidator();
507 nsCOMPtr<nsIStreamListener> mDestListener;
508 RefPtr<nsProgressNotificationProxy> mProgressProxy;
509 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
510 nsCOMPtr<nsIChannel> mRedirectChannel;
512 RefPtr<imgRequest> mRequest;
513 AutoTArray<RefPtr<imgRequestProxy>, 4> mProxies;
515 RefPtr<imgRequest> mNewRequest;
516 RefPtr<imgCacheEntry> mNewEntry;
518 RefPtr<mozilla::dom::Document> mDocument;
519 uint64_t mInnerWindowId;
521 imgLoader* mImgLoader;
523 bool mHadInsecureRedirect;
526 #endif // mozilla_image_imgLoader_h