Bug 1873777 - Remove [function] attribute from interfaces which are not meant to...
[gecko.git] / image / imgLoader.h
blobc5155067903ddda1153ac0798f26afdb69cde2e4
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;
41 } // namespace mozilla
43 class imgCacheEntry {
44 public:
45 NS_INLINE_DECL_REFCOUNTING(imgCacheEntry)
47 imgCacheEntry(imgLoader* loader, imgRequest* request,
48 bool aForcePrincipalCheck);
50 uint32_t GetDataSize() const { return mDataSize; }
51 void SetDataSize(uint32_t aDataSize) {
52 int32_t oldsize = mDataSize;
53 mDataSize = aDataSize;
54 UpdateCache(mDataSize - oldsize);
57 int32_t GetTouchedTime() const { return mTouchedTime; }
58 void SetTouchedTime(int32_t time) {
59 mTouchedTime = time;
60 Touch(/* updateTime = */ false);
63 uint32_t GetLoadTime() const { return mLoadTime; }
65 void UpdateLoadTime();
67 uint32_t GetExpiryTime() const { return mExpiryTime; }
68 void SetExpiryTime(uint32_t aExpiryTime) {
69 mExpiryTime = aExpiryTime;
70 Touch();
73 bool GetMustValidate() const { return mMustValidate; }
74 void SetMustValidate(bool aValidate) {
75 mMustValidate = aValidate;
76 Touch();
79 already_AddRefed<imgRequest> GetRequest() const {
80 RefPtr<imgRequest> req = mRequest;
81 return req.forget();
84 bool Evicted() const { return mEvicted; }
86 nsExpirationState* GetExpirationState() { return &mExpirationState; }
88 bool HasNoProxies() const { return mHasNoProxies; }
90 bool ForcePrincipalCheck() const { return mForcePrincipalCheck; }
92 bool HasNotified() const { return mHasNotified; }
93 void SetHasNotified() {
94 MOZ_ASSERT(!mHasNotified);
95 mHasNotified = true;
98 imgLoader* Loader() const { return mLoader; }
100 private: // methods
101 friend class imgLoader;
102 friend class imgCacheQueue;
103 void Touch(bool updateTime = true);
104 void UpdateCache(int32_t diff = 0);
105 void SetEvicted(bool evict) { mEvicted = evict; }
106 void SetHasNoProxies(bool hasNoProxies);
108 // Private, unimplemented copy constructor.
109 imgCacheEntry(const imgCacheEntry&);
110 ~imgCacheEntry();
112 private: // data
113 imgLoader* mLoader;
114 RefPtr<imgRequest> mRequest;
115 uint32_t mDataSize;
116 int32_t mTouchedTime;
117 uint32_t mLoadTime;
118 uint32_t mExpiryTime;
119 nsExpirationState mExpirationState;
120 bool mMustValidate : 1;
121 bool mEvicted : 1;
122 bool mHasNoProxies : 1;
123 bool mForcePrincipalCheck : 1;
124 bool mHasNotified : 1;
127 #include <vector>
129 #define NS_IMGLOADER_CID \
130 { /* c1354898-e3fe-4602-88a7-c4520c21cb4e */ \
131 0xc1354898, 0xe3fe, 0x4602, { \
132 0x88, 0xa7, 0xc4, 0x52, 0x0c, 0x21, 0xcb, 0x4e \
136 class imgCacheQueue {
137 public:
138 imgCacheQueue();
139 void Remove(imgCacheEntry*);
140 void Push(imgCacheEntry*);
141 void MarkDirty();
142 bool IsDirty();
143 already_AddRefed<imgCacheEntry> Pop();
144 void Refresh();
145 uint32_t GetSize() const;
146 void UpdateSize(int32_t diff);
147 uint32_t GetNumElements() const;
148 bool Contains(imgCacheEntry* aEntry) const;
149 typedef nsTArray<RefPtr<imgCacheEntry>> queueContainer;
150 typedef queueContainer::iterator iterator;
151 typedef queueContainer::const_iterator const_iterator;
153 iterator begin();
154 const_iterator begin() const;
155 iterator end();
156 const_iterator end() const;
158 private:
159 queueContainer mQueue;
160 bool mDirty;
161 uint32_t mSize;
164 enum class AcceptedMimeTypes : uint8_t {
165 IMAGES,
166 IMAGES_AND_DOCUMENTS,
169 class imgLoader final : public imgILoader,
170 public nsIContentSniffer,
171 public imgICache,
172 public nsSupportsWeakReference,
173 public nsIObserver {
174 virtual ~imgLoader();
176 public:
177 using ImageCacheKey = mozilla::image::ImageCacheKey;
178 using imgCacheTable =
179 nsRefPtrHashtable<nsGenericHashKey<ImageCacheKey>, imgCacheEntry>;
180 using imgSet = nsTHashSet<imgRequest*>;
181 using Mutex = mozilla::Mutex;
183 NS_DECL_ISUPPORTS
184 NS_DECL_IMGILOADER
185 NS_DECL_NSICONTENTSNIFFER
186 NS_DECL_IMGICACHE
187 NS_DECL_NSIOBSERVER
190 * Get the normal image loader instance that is used by gecko code, creating
191 * it if necessary.
193 static imgLoader* NormalLoader();
196 * Get the Private Browsing image loader instance that is used by gecko code,
197 * creating it if necessary.
199 static imgLoader* PrivateBrowsingLoader();
202 * Gecko code should use NormalLoader() or PrivateBrowsingLoader() to get the
203 * appropriate image loader.
205 * This constructor is public because the XPCOM module code that creates
206 * instances of "@mozilla.org/image/loader;1" / "@mozilla.org/image/cache;1"
207 * for nsIComponentManager.createInstance()/nsIServiceManager.getService()
208 * calls (now only made by add-ons) needs access to it.
210 * XXX We would like to get rid of the nsIServiceManager.getService (and
211 * nsIComponentManager.createInstance) method of creating imgLoader objects,
212 * but there are add-ons that are still using it. These add-ons don't
213 * actually do anything useful with the loaders that they create since nobody
214 * who creates an imgLoader using this method actually QIs to imgILoader and
215 * loads images. They all just QI to imgICache and either call clearCache()
216 * or findEntryProperties(). Since they're doing this on an imgLoader that
217 * has never loaded images, these calls are useless. It seems likely that
218 * the code that is doing this is just legacy code left over from a time when
219 * there was only one imgLoader instance for the entire process. (Nowadays
220 * the correct method to get an imgILoader/imgICache is to call
221 * imgITools::getImgCacheForDocument/imgITools::getImgLoaderForDocument.)
222 * All the same, even though what these add-ons are doing is a no-op,
223 * removing the nsIServiceManager.getService method of creating/getting an
224 * imgLoader objects would cause an exception in these add-ons that could
225 * break things.
227 imgLoader();
228 nsresult Init();
230 bool IsImageAvailable(nsIURI*, nsIPrincipal* aTriggeringPrincipal,
231 mozilla::CORSMode, mozilla::dom::Document*);
233 [[nodiscard]] nsresult LoadImage(
234 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
235 nsIPrincipal* aLoadingPrincipal, uint64_t aRequestContextID,
236 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
237 nsINode* aContext, mozilla::dom::Document* aLoadingDocument,
238 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
239 nsContentPolicyType aContentPolicyType, const nsAString& initiatorType,
240 bool aUseUrgentStartForChannel, bool aLinkPreload,
241 uint64_t aEarlyHintPreloaderId, imgRequestProxy** _retval);
243 [[nodiscard]] nsresult LoadImageWithChannel(
244 nsIChannel* channel, imgINotificationObserver* aObserver,
245 mozilla::dom::Document* aLoadingDocument, nsIStreamListener** listener,
246 imgRequestProxy** _retval);
248 static nsresult GetMimeTypeFromContent(const char* aContents,
249 uint32_t aLength,
250 nsACString& aContentType);
253 * Returns true if the given mime type may be interpreted as an image.
255 * Some MIME types may be interpreted as both images and documents. (At the
256 * moment only "image/svg+xml" falls into this category, but there may be more
257 * in the future.) Callers which want this function to return true for such
258 * MIME types should pass AcceptedMimeTypes::IMAGES_AND_DOCUMENTS for
259 * @aAccept.
261 * @param aMimeType The MIME type to evaluate.
262 * @param aAcceptedMimeTypes Which kinds of MIME types to treat as images.
264 static bool SupportImageWithMimeType(
265 const nsACString&, AcceptedMimeTypes aAccept = AcceptedMimeTypes::IMAGES);
267 static void GlobalInit(); // for use by the factory
268 static void Shutdown(); // for use by the factory
269 static void ShutdownMemoryReporter();
271 enum class ClearOption {
272 ChromeOnly,
273 UnusedOnly,
275 using ClearOptions = mozilla::EnumSet<ClearOption>;
276 nsresult ClearImageCache(ClearOptions = {});
277 void MinimizeCache() { ClearImageCache({ClearOption::UnusedOnly}); }
279 nsresult InitCache();
281 bool RemoveFromCache(const ImageCacheKey& aKey);
283 // Enumeration describing if a given entry is in the cache queue or not.
284 // There are some cases we know the entry is definitely not in the queue.
285 enum class QueueState { MaybeExists, AlreadyRemoved };
287 bool RemoveFromCache(imgCacheEntry* entry,
288 QueueState aQueueState = QueueState::MaybeExists);
290 bool PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* aEntry);
292 void AddToUncachedImages(imgRequest* aRequest);
293 void RemoveFromUncachedImages(imgRequest* aRequest);
295 // Returns true if we should prefer evicting cache entry |two| over cache
296 // entry |one|.
297 // This mixes units in the worst way, but provides reasonable results.
298 inline static bool CompareCacheEntries(const RefPtr<imgCacheEntry>& one,
299 const RefPtr<imgCacheEntry>& two) {
300 if (!one) {
301 return false;
303 if (!two) {
304 return true;
307 const double sizeweight = 1.0 - sCacheTimeWeight;
309 // We want large, old images to be evicted first (depending on their
310 // relative weights). Since a larger time is actually newer, we subtract
311 // time's weight, so an older image has a larger weight.
312 double oneweight = double(one->GetDataSize()) * sizeweight -
313 double(one->GetTouchedTime()) * sCacheTimeWeight;
314 double twoweight = double(two->GetDataSize()) * sizeweight -
315 double(two->GetTouchedTime()) * sCacheTimeWeight;
317 return oneweight < twoweight;
320 void VerifyCacheSizes();
322 nsresult RemoveEntriesInternal(nsIPrincipal* aPrincipal,
323 const nsACString* aBaseDomain);
325 // The image loader maintains a hash table of all imgCacheEntries. However,
326 // only some of them will be evicted from the cache: those who have no
327 // imgRequestProxies watching their imgRequests.
329 // Once an imgRequest has no imgRequestProxies, it should notify us by
330 // calling HasNoObservers(), and null out its cache entry pointer.
332 // Upon having a proxy start observing again, it should notify us by calling
333 // HasObservers(). The request's cache entry will be re-set before this
334 // happens, by calling imgRequest::SetCacheEntry() when an entry with no
335 // observers is re-requested.
336 bool SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry);
337 bool SetHasProxies(imgRequest* aRequest);
339 private: // methods
340 static already_AddRefed<imgLoader> CreateImageLoader();
342 bool ValidateEntry(imgCacheEntry* aEntry, nsIURI* aURI,
343 nsIURI* aInitialDocumentURI,
344 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
345 imgINotificationObserver* aObserver,
346 mozilla::dom::Document* aLoadingDocument,
347 nsLoadFlags aLoadFlags,
348 nsContentPolicyType aLoadPolicyType,
349 bool aCanMakeNewChannel, bool* aNewChannelCreated,
350 imgRequestProxy** aProxyRequest,
351 nsIPrincipal* aTriggeringPrincipal, mozilla::CORSMode,
352 bool aLinkPreload, uint64_t aEarlyHintPreloaderId);
354 bool ValidateRequestWithNewChannel(
355 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
356 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
357 imgINotificationObserver* aObserver,
358 mozilla::dom::Document* aLoadingDocument, uint64_t aInnerWindowId,
359 nsLoadFlags aLoadFlags, nsContentPolicyType aContentPolicyType,
360 imgRequestProxy** aProxyRequest, nsIPrincipal* aLoadingPrincipal,
361 mozilla::CORSMode, bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
362 bool* aNewChannelCreated);
364 void NotifyObserversForCachedImage(imgCacheEntry* aEntry, imgRequest* request,
365 nsIURI* aURI,
366 nsIReferrerInfo* aReferrerInfo,
367 mozilla::dom::Document* aLoadingDocument,
368 nsIPrincipal* aLoadingPrincipal,
369 mozilla::CORSMode,
370 uint64_t aEarlyHintPreloaderId);
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(bool aChromeOnly);
382 void CacheEntriesChanged(int32_t aSizeDiff);
383 void CheckCacheLimits();
385 private: // data
386 friend class imgCacheEntry;
387 friend class imgMemoryReporter;
389 imgCacheTable mCache;
390 imgCacheQueue mCacheQueue;
392 // Hash set of every imgRequest for this loader that isn't in mCache or
393 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
394 // mUncachedImages should be every imgRequest that is alive. These are weak
395 // pointers so we rely on the imgRequest destructor to remove itself.
396 imgSet mUncachedImages MOZ_GUARDED_BY(mUncachedImagesMutex);
397 // The imgRequest can have refs to them held on non-main thread, so we need
398 // a mutex because we modify the uncached images set from the imgRequest
399 // destructor.
400 Mutex mUncachedImagesMutex;
402 static double sCacheTimeWeight;
403 static uint32_t sCacheMaxSize;
404 static imgMemoryReporter* sMemReporter;
406 mozilla::UniquePtr<imgCacheExpirationTracker> mCacheTracker;
407 bool mRespectPrivacy;
411 * proxy stream listener class used to handle multipart/x-mixed-replace
414 #include "nsCOMPtr.h"
415 #include "nsIStreamListener.h"
416 #include "nsIThreadRetargetableStreamListener.h"
418 class ProxyListener : public nsIThreadRetargetableStreamListener {
419 public:
420 explicit ProxyListener(nsIStreamListener* dest);
422 /* additional members */
423 NS_DECL_THREADSAFE_ISUPPORTS
424 NS_DECL_NSISTREAMLISTENER
425 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
426 NS_DECL_NSIREQUESTOBSERVER
428 private:
429 virtual ~ProxyListener();
431 nsCOMPtr<nsIStreamListener> mDestListener;
435 * A class that implements nsIProgressEventSink and forwards all calls to it to
436 * the original notification callbacks of the channel. Also implements
437 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
438 * and forwards everything else to the channel's notification callbacks.
440 class nsProgressNotificationProxy final : public nsIProgressEventSink,
441 public nsIChannelEventSink,
442 public nsIInterfaceRequestor {
443 public:
444 nsProgressNotificationProxy(nsIChannel* channel, imgIRequest* proxy)
445 : mImageRequest(proxy) {
446 channel->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks));
449 NS_DECL_ISUPPORTS
450 NS_DECL_NSIPROGRESSEVENTSINK
451 NS_DECL_NSICHANNELEVENTSINK
452 NS_DECL_NSIINTERFACEREQUESTOR
453 private:
454 ~nsProgressNotificationProxy() = default;
456 nsCOMPtr<nsIInterfaceRequestor> mOriginalCallbacks;
457 nsCOMPtr<nsIRequest> mImageRequest;
461 * validate checker
464 #include "nsCOMArray.h"
466 class imgCacheValidator : public nsIThreadRetargetableStreamListener,
467 public nsIChannelEventSink,
468 public nsIInterfaceRequestor,
469 public nsIAsyncVerifyRedirectCallback {
470 public:
471 imgCacheValidator(nsProgressNotificationProxy* progress, imgLoader* loader,
472 imgRequest* aRequest, mozilla::dom::Document* aDocument,
473 uint64_t aInnerWindowId,
474 bool forcePrincipalCheckForCacheEntry);
476 void AddProxy(imgRequestProxy* aProxy);
477 void RemoveProxy(imgRequestProxy* aProxy);
479 NS_DECL_THREADSAFE_ISUPPORTS
480 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
481 NS_DECL_NSISTREAMLISTENER
482 NS_DECL_NSIREQUESTOBSERVER
483 NS_DECL_NSICHANNELEVENTSINK
484 NS_DECL_NSIINTERFACEREQUESTOR
485 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
487 private:
488 void UpdateProxies(bool aCancelRequest, bool aSyncNotify);
489 virtual ~imgCacheValidator();
491 nsCOMPtr<nsIStreamListener> mDestListener;
492 RefPtr<nsProgressNotificationProxy> mProgressProxy;
493 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
494 nsCOMPtr<nsIChannel> mRedirectChannel;
496 RefPtr<imgRequest> mRequest;
497 AutoTArray<RefPtr<imgRequestProxy>, 4> mProxies;
499 RefPtr<imgRequest> mNewRequest;
500 RefPtr<imgCacheEntry> mNewEntry;
502 RefPtr<mozilla::dom::Document> mDocument;
503 uint64_t mInnerWindowId;
505 imgLoader* mImgLoader;
507 bool mHadInsecureRedirect;
510 #endif // mozilla_image_imgLoader_h