Bug 1646700 [wpt PR 24235] - Update picture-in-picture idlharness test, a=testonly
[gecko.git] / image / imgLoader.h
blob28ff373e4c358a3a648cb1da47e7b4b2759ac758
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/dom/Document.h"
13 #include "mozilla/Mutex.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 "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"
29 class imgLoader;
30 class imgRequestProxy;
31 class imgINotificationObserver;
32 class nsILoadGroup;
33 class imgCacheExpirationTracker;
34 class imgMemoryReporter;
36 namespace mozilla {
37 namespace image {} // namespace image
38 } // namespace mozilla
40 class imgCacheEntry {
41 public:
42 imgCacheEntry(imgLoader* loader, imgRequest* request,
43 bool aForcePrincipalCheck);
44 ~imgCacheEntry();
46 nsrefcnt AddRef() {
47 MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt");
48 NS_ASSERT_OWNINGTHREAD(imgCacheEntry);
49 ++mRefCnt;
50 NS_LOG_ADDREF(this, mRefCnt, "imgCacheEntry", sizeof(*this));
51 return mRefCnt;
54 nsrefcnt Release() {
55 MOZ_ASSERT(0 != mRefCnt, "dup release");
56 NS_ASSERT_OWNINGTHREAD(imgCacheEntry);
57 --mRefCnt;
58 NS_LOG_RELEASE(this, mRefCnt, "imgCacheEntry");
59 if (mRefCnt == 0) {
60 mRefCnt = 1; /* stabilize */
61 delete this;
62 return 0;
64 return mRefCnt;
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) {
76 mTouchedTime = 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;
87 Touch();
90 bool GetMustValidate() const { return mMustValidate; }
91 void SetMustValidate(bool aValidate) {
92 mMustValidate = aValidate;
93 Touch();
96 already_AddRefed<imgRequest> GetRequest() const {
97 RefPtr<imgRequest> req = mRequest;
98 return req.forget();
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; }
111 private: // methods
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&);
122 private: // data
123 nsAutoRefCnt mRefCnt;
124 NS_DECL_OWNINGTHREAD
126 imgLoader* mLoader;
127 RefPtr<imgRequest> mRequest;
128 uint32_t mDataSize;
129 int32_t mTouchedTime;
130 uint32_t mLoadTime;
131 int32_t mExpiryTime;
132 nsExpirationState mExpirationState;
133 bool mMustValidate : 1;
134 bool mEvicted : 1;
135 bool mHasNoProxies : 1;
136 bool mForcePrincipalCheck : 1;
139 #include <vector>
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 {
149 public:
150 imgCacheQueue();
151 void Remove(imgCacheEntry*);
152 void Push(imgCacheEntry*);
153 void MarkDirty();
154 bool IsDirty();
155 already_AddRefed<imgCacheEntry> Pop();
156 void Refresh();
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;
165 iterator begin();
166 const_iterator begin() const;
167 iterator end();
168 const_iterator end() const;
170 private:
171 queueContainer mQueue;
172 bool mDirty;
173 uint32_t mSize;
176 enum class AcceptedMimeTypes : uint8_t {
177 IMAGES,
178 IMAGES_AND_DOCUMENTS,
181 class imgLoader final : public imgILoader,
182 public nsIContentSniffer,
183 public imgICache,
184 public nsSupportsWeakReference,
185 public nsIObserver {
186 virtual ~imgLoader();
188 public:
189 typedef mozilla::image::ImageCacheKey ImageCacheKey;
190 typedef nsRefPtrHashtable<nsGenericHashKey<ImageCacheKey>, imgCacheEntry>
191 imgCacheTable;
192 typedef nsTHashtable<nsPtrHashKey<imgRequest>> imgSet;
193 typedef mozilla::Mutex Mutex;
195 NS_DECL_ISUPPORTS
196 NS_DECL_IMGILOADER
197 NS_DECL_NSICONTENTSNIFFER
198 NS_DECL_IMGICACHE
199 NS_DECL_NSIOBSERVER
202 * Get the normal image loader instance that is used by gecko code, creating
203 * it if necessary.
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
237 * break things.
239 imgLoader();
240 nsresult Init();
242 [[nodiscard]] 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, bool aLinkPreload,
250 imgRequestProxy** _retval);
252 [[nodiscard]] nsresult LoadImageWithChannel(
253 nsIChannel* channel, imgINotificationObserver* aObserver,
254 mozilla::dom::Document* aLoadingDocument, nsIStreamListener** listener,
255 imgRequestProxy** _retval);
257 static nsresult GetMimeTypeFromContent(const char* aContents,
258 uint32_t aLength,
259 nsACString& aContentType);
262 * Returns true if the given mime type may be interpreted as an image.
264 * Some MIME types may be interpreted as both images and documents. (At the
265 * moment only "image/svg+xml" falls into this category, but there may be more
266 * in the future.) Callers which want this function to return true for such
267 * MIME types should pass AcceptedMimeTypes::IMAGES_AND_DOCUMENTS for
268 * @aAccept.
270 * @param aMimeType The MIME type to evaluate.
271 * @param aAcceptedMimeTypes Which kinds of MIME types to treat as images.
273 static bool SupportImageWithMimeType(
274 const char* aMimeType,
275 AcceptedMimeTypes aAccept = AcceptedMimeTypes::IMAGES);
277 static void GlobalInit(); // for use by the factory
278 static void Shutdown(); // for use by the factory
279 static void ShutdownMemoryReporter();
281 nsresult ClearChromeImageCache();
282 nsresult ClearImageCache();
283 void MinimizeCaches();
285 nsresult InitCache();
287 bool RemoveFromCache(const ImageCacheKey& aKey);
289 // Enumeration describing if a given entry is in the cache queue or not.
290 // There are some cases we know the entry is definitely not in the queue.
291 enum class QueueState { MaybeExists, AlreadyRemoved };
293 bool RemoveFromCache(imgCacheEntry* entry,
294 QueueState aQueueState = QueueState::MaybeExists);
296 bool PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* aEntry);
298 void AddToUncachedImages(imgRequest* aRequest);
299 void RemoveFromUncachedImages(imgRequest* aRequest);
301 // Returns true if we should prefer evicting cache entry |two| over cache
302 // entry |one|.
303 // This mixes units in the worst way, but provides reasonable results.
304 inline static bool CompareCacheEntries(const RefPtr<imgCacheEntry>& one,
305 const RefPtr<imgCacheEntry>& two) {
306 if (!one) {
307 return false;
309 if (!two) {
310 return true;
313 const double sizeweight = 1.0 - sCacheTimeWeight;
315 // We want large, old images to be evicted first (depending on their
316 // relative weights). Since a larger time is actually newer, we subtract
317 // time's weight, so an older image has a larger weight.
318 double oneweight = double(one->GetDataSize()) * sizeweight -
319 double(one->GetTouchedTime()) * sCacheTimeWeight;
320 double twoweight = double(two->GetDataSize()) * sizeweight -
321 double(two->GetTouchedTime()) * sCacheTimeWeight;
323 return oneweight < twoweight;
326 void VerifyCacheSizes();
328 // The image loader maintains a hash table of all imgCacheEntries. However,
329 // only some of them will be evicted from the cache: those who have no
330 // imgRequestProxies watching their imgRequests.
332 // Once an imgRequest has no imgRequestProxies, it should notify us by
333 // calling HasNoObservers(), and null out its cache entry pointer.
335 // Upon having a proxy start observing again, it should notify us by calling
336 // HasObservers(). The request's cache entry will be re-set before this
337 // happens, by calling imgRequest::SetCacheEntry() when an entry with no
338 // observers is re-requested.
339 bool SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry);
340 bool SetHasProxies(imgRequest* aRequest);
342 // This method converts imgIRequest::CORS_* values to mozilla::CORSMode
343 // values.
344 static mozilla::CORSMode ConvertToCORSMode(uint32_t aImgCORS);
346 private: // methods
347 static already_AddRefed<imgLoader> CreateImageLoader();
349 bool PreferLoadFromCache(nsIURI* aURI) const;
351 bool ValidateEntry(
352 imgCacheEntry* aEntry, nsIURI* aURI, nsIURI* aInitialDocumentURI,
353 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
354 imgINotificationObserver* aObserver,
355 mozilla::dom::Document* aLoadingDocument, nsLoadFlags aLoadFlags,
356 nsContentPolicyType aLoadPolicyType, bool aCanMakeNewChannel,
357 bool* aNewChannelCreated, imgRequestProxy** aProxyRequest,
358 nsIPrincipal* aTriggeringPrincipal, int32_t aCORSMode, bool aLinkPreload);
360 bool ValidateRequestWithNewChannel(
361 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
362 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
363 imgINotificationObserver* aObserver,
364 mozilla::dom::Document* aLoadingDocument, uint64_t aInnerWindowId,
365 nsLoadFlags aLoadFlags, nsContentPolicyType aContentPolicyType,
366 imgRequestProxy** aProxyRequest, nsIPrincipal* aLoadingPrincipal,
367 int32_t aCORSMode, bool aLinkPreload, bool* aNewChannelCreated);
369 // aURI may be different from imgRequest's URI in the case of blob URIs, as we
370 // can share requests with different URIs.
371 nsresult CreateNewProxyForRequest(imgRequest* aRequest, nsIURI* aURI,
372 nsILoadGroup* aLoadGroup,
373 mozilla::dom::Document* aLoadingDocument,
374 imgINotificationObserver* aObserver,
375 nsLoadFlags aLoadFlags,
376 imgRequestProxy** _retval);
378 nsresult EvictEntries(imgCacheTable& aCacheToClear);
379 nsresult EvictEntries(imgCacheQueue& aQueueToClear);
381 imgCacheTable& GetCache(bool aForChrome);
382 imgCacheTable& GetCache(const ImageCacheKey& aKey);
383 imgCacheQueue& GetCacheQueue(bool aForChrome);
384 imgCacheQueue& GetCacheQueue(const ImageCacheKey& aKey);
385 void CacheEntriesChanged(bool aForChrome, int32_t aSizeDiff = 0);
386 void CheckCacheLimits(imgCacheTable& cache, imgCacheQueue& queue);
388 private: // data
389 friend class imgCacheEntry;
390 friend class imgMemoryReporter;
392 imgCacheTable mCache;
393 imgCacheQueue mCacheQueue;
395 imgCacheTable mChromeCache;
396 imgCacheQueue mChromeCacheQueue;
398 // Hash set of every imgRequest for this loader that isn't in mCache or
399 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
400 // mUncachedImages should be every imgRequest that is alive. These are weak
401 // pointers so we rely on the imgRequest destructor to remove itself.
402 imgSet mUncachedImages;
403 // The imgRequest can have refs to them held on non-main thread, so we need
404 // a mutex because we modify the uncached images set from the imgRequest
405 // destructor.
406 Mutex mUncachedImagesMutex;
408 static double sCacheTimeWeight;
409 static uint32_t sCacheMaxSize;
410 static imgMemoryReporter* sMemReporter;
412 mozilla::UniquePtr<imgCacheExpirationTracker> mCacheTracker;
413 bool mRespectPrivacy;
417 * proxy stream listener class used to handle multipart/x-mixed-replace
420 #include "nsCOMPtr.h"
421 #include "nsIStreamListener.h"
422 #include "nsIThreadRetargetableStreamListener.h"
424 class ProxyListener : public nsIStreamListener,
425 public nsIThreadRetargetableStreamListener {
426 public:
427 explicit ProxyListener(nsIStreamListener* dest);
429 /* additional members */
430 NS_DECL_THREADSAFE_ISUPPORTS
431 NS_DECL_NSISTREAMLISTENER
432 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
433 NS_DECL_NSIREQUESTOBSERVER
435 private:
436 virtual ~ProxyListener();
438 nsCOMPtr<nsIStreamListener> mDestListener;
442 * A class that implements nsIProgressEventSink and forwards all calls to it to
443 * the original notification callbacks of the channel. Also implements
444 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
445 * and forwards everything else to the channel's notification callbacks.
447 class nsProgressNotificationProxy final : public nsIProgressEventSink,
448 public nsIChannelEventSink,
449 public nsIInterfaceRequestor {
450 public:
451 nsProgressNotificationProxy(nsIChannel* channel, imgIRequest* proxy)
452 : mImageRequest(proxy) {
453 channel->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks));
456 NS_DECL_ISUPPORTS
457 NS_DECL_NSIPROGRESSEVENTSINK
458 NS_DECL_NSICHANNELEVENTSINK
459 NS_DECL_NSIINTERFACEREQUESTOR
460 private:
461 ~nsProgressNotificationProxy() = default;
463 nsCOMPtr<nsIInterfaceRequestor> mOriginalCallbacks;
464 nsCOMPtr<nsIRequest> mImageRequest;
468 * validate checker
471 #include "nsCOMArray.h"
473 class imgCacheValidator : public nsIStreamListener,
474 public nsIThreadRetargetableStreamListener,
475 public nsIChannelEventSink,
476 public nsIInterfaceRequestor,
477 public nsIAsyncVerifyRedirectCallback {
478 public:
479 imgCacheValidator(nsProgressNotificationProxy* progress, imgLoader* loader,
480 imgRequest* aRequest, mozilla::dom::Document* aDocument,
481 uint64_t aInnerWindowId,
482 bool forcePrincipalCheckForCacheEntry);
484 void AddProxy(imgRequestProxy* aProxy);
485 void RemoveProxy(imgRequestProxy* aProxy);
487 NS_DECL_THREADSAFE_ISUPPORTS
488 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
489 NS_DECL_NSISTREAMLISTENER
490 NS_DECL_NSIREQUESTOBSERVER
491 NS_DECL_NSICHANNELEVENTSINK
492 NS_DECL_NSIINTERFACEREQUESTOR
493 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
495 private:
496 void UpdateProxies(bool aCancelRequest, bool aSyncNotify);
497 virtual ~imgCacheValidator();
499 nsCOMPtr<nsIStreamListener> mDestListener;
500 RefPtr<nsProgressNotificationProxy> mProgressProxy;
501 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
502 nsCOMPtr<nsIChannel> mRedirectChannel;
504 RefPtr<imgRequest> mRequest;
505 AutoTArray<RefPtr<imgRequestProxy>, 4> mProxies;
507 RefPtr<imgRequest> mNewRequest;
508 RefPtr<imgCacheEntry> mNewEntry;
510 RefPtr<mozilla::dom::Document> mDocument;
511 uint64_t mInnerWindowId;
513 imgLoader* mImgLoader;
515 bool mHadInsecureRedirect;
518 #endif // mozilla_image_imgLoader_h