Bug 1635673 [wpt PR 23412] - [ResourceTiming] Dispatch entry for 404 scripts, a=testonly
[gecko.git] / image / imgLoader.h
blob9b4fdaee5deb3b00bf1d7f4962643af6e4acbe9a
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 static uint32_t SecondsFromPRTime(PRTime prTime);
44 imgCacheEntry(imgLoader* loader, imgRequest* request,
45 bool aForcePrincipalCheck);
46 ~imgCacheEntry();
48 nsrefcnt AddRef() {
49 MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt");
50 NS_ASSERT_OWNINGTHREAD(imgCacheEntry);
51 ++mRefCnt;
52 NS_LOG_ADDREF(this, mRefCnt, "imgCacheEntry", sizeof(*this));
53 return mRefCnt;
56 nsrefcnt Release() {
57 MOZ_ASSERT(0 != mRefCnt, "dup release");
58 NS_ASSERT_OWNINGTHREAD(imgCacheEntry);
59 --mRefCnt;
60 NS_LOG_RELEASE(this, mRefCnt, "imgCacheEntry");
61 if (mRefCnt == 0) {
62 mRefCnt = 1; /* stabilize */
63 delete this;
64 return 0;
66 return mRefCnt;
69 uint32_t GetDataSize() const { return mDataSize; }
70 void SetDataSize(uint32_t aDataSize) {
71 int32_t oldsize = mDataSize;
72 mDataSize = aDataSize;
73 UpdateCache(mDataSize - oldsize);
76 int32_t GetTouchedTime() const { return mTouchedTime; }
77 void SetTouchedTime(int32_t time) {
78 mTouchedTime = time;
79 Touch(/* updateTime = */ false);
82 uint32_t GetLoadTime() const { return mLoadTime; }
84 void UpdateLoadTime();
86 int32_t GetExpiryTime() const { return mExpiryTime; }
87 void SetExpiryTime(int32_t aExpiryTime) {
88 mExpiryTime = aExpiryTime;
89 Touch();
92 bool GetMustValidate() const { return mMustValidate; }
93 void SetMustValidate(bool aValidate) {
94 mMustValidate = aValidate;
95 Touch();
98 already_AddRefed<imgRequest> GetRequest() const {
99 RefPtr<imgRequest> req = mRequest;
100 return req.forget();
103 bool Evicted() const { return mEvicted; }
105 nsExpirationState* GetExpirationState() { return &mExpirationState; }
107 bool HasNoProxies() const { return mHasNoProxies; }
109 bool ForcePrincipalCheck() const { return mForcePrincipalCheck; }
111 imgLoader* Loader() const { return mLoader; }
113 private: // methods
114 friend class imgLoader;
115 friend class imgCacheQueue;
116 void Touch(bool updateTime = true);
117 void UpdateCache(int32_t diff = 0);
118 void SetEvicted(bool evict) { mEvicted = evict; }
119 void SetHasNoProxies(bool hasNoProxies);
121 // Private, unimplemented copy constructor.
122 imgCacheEntry(const imgCacheEntry&);
124 private: // data
125 nsAutoRefCnt mRefCnt;
126 NS_DECL_OWNINGTHREAD
128 imgLoader* mLoader;
129 RefPtr<imgRequest> mRequest;
130 uint32_t mDataSize;
131 int32_t mTouchedTime;
132 uint32_t mLoadTime;
133 int32_t mExpiryTime;
134 nsExpirationState mExpirationState;
135 bool mMustValidate : 1;
136 bool mEvicted : 1;
137 bool mHasNoProxies : 1;
138 bool mForcePrincipalCheck : 1;
141 #include <vector>
143 #define NS_IMGLOADER_CID \
144 { /* c1354898-e3fe-4602-88a7-c4520c21cb4e */ \
145 0xc1354898, 0xe3fe, 0x4602, { \
146 0x88, 0xa7, 0xc4, 0x52, 0x0c, 0x21, 0xcb, 0x4e \
150 class imgCacheQueue {
151 public:
152 imgCacheQueue();
153 void Remove(imgCacheEntry*);
154 void Push(imgCacheEntry*);
155 void MarkDirty();
156 bool IsDirty();
157 already_AddRefed<imgCacheEntry> Pop();
158 void Refresh();
159 uint32_t GetSize() const;
160 void UpdateSize(int32_t diff);
161 uint32_t GetNumElements() const;
162 bool Contains(imgCacheEntry* aEntry) const;
163 typedef nsTArray<RefPtr<imgCacheEntry>> queueContainer;
164 typedef queueContainer::iterator iterator;
165 typedef queueContainer::const_iterator const_iterator;
167 iterator begin();
168 const_iterator begin() const;
169 iterator end();
170 const_iterator end() const;
172 private:
173 queueContainer mQueue;
174 bool mDirty;
175 uint32_t mSize;
178 enum class AcceptedMimeTypes : uint8_t {
179 IMAGES,
180 IMAGES_AND_DOCUMENTS,
183 class imgLoader final : public imgILoader,
184 public nsIContentSniffer,
185 public imgICache,
186 public nsSupportsWeakReference,
187 public nsIObserver {
188 virtual ~imgLoader();
190 public:
191 typedef mozilla::image::ImageCacheKey ImageCacheKey;
192 typedef nsRefPtrHashtable<nsGenericHashKey<ImageCacheKey>, imgCacheEntry>
193 imgCacheTable;
194 typedef nsTHashtable<nsPtrHashKey<imgRequest>> imgSet;
195 typedef mozilla::Mutex Mutex;
197 NS_DECL_ISUPPORTS
198 NS_DECL_IMGILOADER
199 NS_DECL_NSICONTENTSNIFFER
200 NS_DECL_IMGICACHE
201 NS_DECL_NSIOBSERVER
204 * Get the normal image loader instance that is used by gecko code, creating
205 * it if necessary.
207 static imgLoader* NormalLoader();
210 * Get the Private Browsing image loader instance that is used by gecko code,
211 * creating it if necessary.
213 static imgLoader* PrivateBrowsingLoader();
216 * Gecko code should use NormalLoader() or PrivateBrowsingLoader() to get the
217 * appropriate image loader.
219 * This constructor is public because the XPCOM module code that creates
220 * instances of "@mozilla.org/image/loader;1" / "@mozilla.org/image/cache;1"
221 * for nsIComponentManager.createInstance()/nsIServiceManager.getService()
222 * calls (now only made by add-ons) needs access to it.
224 * XXX We would like to get rid of the nsIServiceManager.getService (and
225 * nsIComponentManager.createInstance) method of creating imgLoader objects,
226 * but there are add-ons that are still using it. These add-ons don't
227 * actually do anything useful with the loaders that they create since nobody
228 * who creates an imgLoader using this method actually QIs to imgILoader and
229 * loads images. They all just QI to imgICache and either call clearCache()
230 * or findEntryProperties(). Since they're doing this on an imgLoader that
231 * has never loaded images, these calls are useless. It seems likely that
232 * the code that is doing this is just legacy code left over from a time when
233 * there was only one imgLoader instance for the entire process. (Nowadays
234 * the correct method to get an imgILoader/imgICache is to call
235 * imgITools::getImgCacheForDocument/imgITools::getImgLoaderForDocument.)
236 * All the same, even though what these add-ons are doing is a no-op,
237 * removing the nsIServiceManager.getService method of creating/getting an
238 * imgLoader objects would cause an exception in these add-ons that could
239 * break things.
241 imgLoader();
242 nsresult Init();
244 [[nodiscard]] nsresult LoadImage(
245 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
246 nsIPrincipal* aLoadingPrincipal, uint64_t aRequestContextID,
247 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
248 nsINode* aContext, mozilla::dom::Document* aLoadingDocument,
249 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
250 nsContentPolicyType aContentPolicyType, const nsAString& initiatorType,
251 bool aUseUrgentStartForChannel, bool aLinkPreload,
252 imgRequestProxy** _retval);
254 [[nodiscard]] nsresult LoadImageWithChannel(
255 nsIChannel* channel, imgINotificationObserver* aObserver,
256 mozilla::dom::Document* aLoadingDocument, nsIStreamListener** listener,
257 imgRequestProxy** _retval);
259 static nsresult GetMimeTypeFromContent(const char* aContents,
260 uint32_t aLength,
261 nsACString& aContentType);
264 * Returns true if the given mime type may be interpreted as an image.
266 * Some MIME types may be interpreted as both images and documents. (At the
267 * moment only "image/svg+xml" falls into this category, but there may be more
268 * in the future.) Callers which want this function to return true for such
269 * MIME types should pass AcceptedMimeTypes::IMAGES_AND_DOCUMENTS for
270 * @aAccept.
272 * @param aMimeType The MIME type to evaluate.
273 * @param aAcceptedMimeTypes Which kinds of MIME types to treat as images.
275 static bool SupportImageWithMimeType(
276 const char* aMimeType,
277 AcceptedMimeTypes aAccept = AcceptedMimeTypes::IMAGES);
279 static void GlobalInit(); // for use by the factory
280 static void Shutdown(); // for use by the factory
281 static void ShutdownMemoryReporter();
283 nsresult ClearChromeImageCache();
284 nsresult ClearImageCache();
285 void MinimizeCaches();
287 nsresult InitCache();
289 bool RemoveFromCache(const ImageCacheKey& aKey);
291 // Enumeration describing if a given entry is in the cache queue or not.
292 // There are some cases we know the entry is definitely not in the queue.
293 enum class QueueState { MaybeExists, AlreadyRemoved };
295 bool RemoveFromCache(imgCacheEntry* entry,
296 QueueState aQueueState = QueueState::MaybeExists);
298 bool PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* aEntry);
300 void AddToUncachedImages(imgRequest* aRequest);
301 void RemoveFromUncachedImages(imgRequest* aRequest);
303 // Returns true if we should prefer evicting cache entry |two| over cache
304 // entry |one|.
305 // This mixes units in the worst way, but provides reasonable results.
306 inline static bool CompareCacheEntries(const RefPtr<imgCacheEntry>& one,
307 const RefPtr<imgCacheEntry>& two) {
308 if (!one) {
309 return false;
311 if (!two) {
312 return true;
315 const double sizeweight = 1.0 - sCacheTimeWeight;
317 // We want large, old images to be evicted first (depending on their
318 // relative weights). Since a larger time is actually newer, we subtract
319 // time's weight, so an older image has a larger weight.
320 double oneweight = double(one->GetDataSize()) * sizeweight -
321 double(one->GetTouchedTime()) * sCacheTimeWeight;
322 double twoweight = double(two->GetDataSize()) * sizeweight -
323 double(two->GetTouchedTime()) * sCacheTimeWeight;
325 return oneweight < twoweight;
328 void VerifyCacheSizes();
330 // The image loader maintains a hash table of all imgCacheEntries. However,
331 // only some of them will be evicted from the cache: those who have no
332 // imgRequestProxies watching their imgRequests.
334 // Once an imgRequest has no imgRequestProxies, it should notify us by
335 // calling HasNoObservers(), and null out its cache entry pointer.
337 // Upon having a proxy start observing again, it should notify us by calling
338 // HasObservers(). The request's cache entry will be re-set before this
339 // happens, by calling imgRequest::SetCacheEntry() when an entry with no
340 // observers is re-requested.
341 bool SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry);
342 bool SetHasProxies(imgRequest* aRequest);
344 // This method converts imgIRequest::CORS_* values to mozilla::CORSMode
345 // values.
346 static mozilla::CORSMode ConvertToCORSMode(uint32_t aImgCORS);
348 private: // methods
349 static already_AddRefed<imgLoader> CreateImageLoader();
351 bool PreferLoadFromCache(nsIURI* aURI) const;
353 bool ValidateEntry(
354 imgCacheEntry* aEntry, nsIURI* aURI, nsIURI* aInitialDocumentURI,
355 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
356 imgINotificationObserver* aObserver,
357 mozilla::dom::Document* aLoadingDocument, nsLoadFlags aLoadFlags,
358 nsContentPolicyType aLoadPolicyType, bool aCanMakeNewChannel,
359 bool* aNewChannelCreated, imgRequestProxy** aProxyRequest,
360 nsIPrincipal* aTriggeringPrincipal, int32_t aCORSMode, bool aLinkPreload);
362 bool ValidateRequestWithNewChannel(
363 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
364 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
365 imgINotificationObserver* aObserver,
366 mozilla::dom::Document* aLoadingDocument, uint64_t aInnerWindowId,
367 nsLoadFlags aLoadFlags, nsContentPolicyType aContentPolicyType,
368 imgRequestProxy** aProxyRequest, nsIPrincipal* aLoadingPrincipal,
369 int32_t aCORSMode, bool aLinkPreload, bool* aNewChannelCreated);
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(imgCacheTable& aCacheToClear);
381 nsresult EvictEntries(imgCacheQueue& aQueueToClear);
383 imgCacheTable& GetCache(bool aForChrome);
384 imgCacheTable& GetCache(const ImageCacheKey& aKey);
385 imgCacheQueue& GetCacheQueue(bool aForChrome);
386 imgCacheQueue& GetCacheQueue(const ImageCacheKey& aKey);
387 void CacheEntriesChanged(bool aForChrome, int32_t aSizeDiff = 0);
388 void CheckCacheLimits(imgCacheTable& cache, imgCacheQueue& queue);
390 private: // data
391 friend class imgCacheEntry;
392 friend class imgMemoryReporter;
394 imgCacheTable mCache;
395 imgCacheQueue mCacheQueue;
397 imgCacheTable mChromeCache;
398 imgCacheQueue mChromeCacheQueue;
400 // Hash set of every imgRequest for this loader that isn't in mCache or
401 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
402 // mUncachedImages should be every imgRequest that is alive. These are weak
403 // pointers so we rely on the imgRequest destructor to remove itself.
404 imgSet mUncachedImages;
405 // The imgRequest can have refs to them held on non-main thread, so we need
406 // a mutex because we modify the uncached images set from the imgRequest
407 // destructor.
408 Mutex mUncachedImagesMutex;
410 static double sCacheTimeWeight;
411 static uint32_t sCacheMaxSize;
412 static imgMemoryReporter* sMemReporter;
414 mozilla::UniquePtr<imgCacheExpirationTracker> mCacheTracker;
415 bool mRespectPrivacy;
419 * proxy stream listener class used to handle multipart/x-mixed-replace
422 #include "nsCOMPtr.h"
423 #include "nsIStreamListener.h"
424 #include "nsIThreadRetargetableStreamListener.h"
426 class ProxyListener : public nsIStreamListener,
427 public nsIThreadRetargetableStreamListener {
428 public:
429 explicit ProxyListener(nsIStreamListener* dest);
431 /* additional members */
432 NS_DECL_THREADSAFE_ISUPPORTS
433 NS_DECL_NSISTREAMLISTENER
434 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
435 NS_DECL_NSIREQUESTOBSERVER
437 private:
438 virtual ~ProxyListener();
440 nsCOMPtr<nsIStreamListener> mDestListener;
444 * A class that implements nsIProgressEventSink and forwards all calls to it to
445 * the original notification callbacks of the channel. Also implements
446 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
447 * and forwards everything else to the channel's notification callbacks.
449 class nsProgressNotificationProxy final : public nsIProgressEventSink,
450 public nsIChannelEventSink,
451 public nsIInterfaceRequestor {
452 public:
453 nsProgressNotificationProxy(nsIChannel* channel, imgIRequest* proxy)
454 : mImageRequest(proxy) {
455 channel->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks));
458 NS_DECL_ISUPPORTS
459 NS_DECL_NSIPROGRESSEVENTSINK
460 NS_DECL_NSICHANNELEVENTSINK
461 NS_DECL_NSIINTERFACEREQUESTOR
462 private:
463 ~nsProgressNotificationProxy() = default;
465 nsCOMPtr<nsIInterfaceRequestor> mOriginalCallbacks;
466 nsCOMPtr<nsIRequest> mImageRequest;
470 * validate checker
473 #include "nsCOMArray.h"
475 class imgCacheValidator : public nsIStreamListener,
476 public nsIThreadRetargetableStreamListener,
477 public nsIChannelEventSink,
478 public nsIInterfaceRequestor,
479 public nsIAsyncVerifyRedirectCallback {
480 public:
481 imgCacheValidator(nsProgressNotificationProxy* progress, imgLoader* loader,
482 imgRequest* aRequest, mozilla::dom::Document* aDocument,
483 uint64_t aInnerWindowId,
484 bool forcePrincipalCheckForCacheEntry);
486 void AddProxy(imgRequestProxy* aProxy);
487 void RemoveProxy(imgRequestProxy* aProxy);
489 void PrioritizeAsPreload();
491 NS_DECL_THREADSAFE_ISUPPORTS
492 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
493 NS_DECL_NSISTREAMLISTENER
494 NS_DECL_NSIREQUESTOBSERVER
495 NS_DECL_NSICHANNELEVENTSINK
496 NS_DECL_NSIINTERFACEREQUESTOR
497 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
499 private:
500 void UpdateProxies(bool aCancelRequest, bool aSyncNotify);
501 virtual ~imgCacheValidator();
503 nsCOMPtr<nsIStreamListener> mDestListener;
504 RefPtr<nsProgressNotificationProxy> mProgressProxy;
505 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
506 nsCOMPtr<nsIChannel> mRedirectChannel;
508 RefPtr<imgRequest> mRequest;
509 AutoTArray<RefPtr<imgRequestProxy>, 4> mProxies;
511 RefPtr<imgRequest> mNewRequest;
512 RefPtr<imgCacheEntry> mNewEntry;
514 RefPtr<mozilla::dom::Document> mDocument;
515 uint64_t mInnerWindowId;
517 imgLoader* mImgLoader;
519 bool mHadInsecureRedirect;
522 #endif // mozilla_image_imgLoader_h