Bug 1586798 - Use WalkerFront from the currently selected element in onTagEdit()...
[gecko.git] / image / imgLoader.h
blob06bbf312f8d4fdeb084fa0d66d5d482e39513377
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/Mutex.h"
12 #include "mozilla/UniquePtr.h"
14 #include "imgILoader.h"
15 #include "imgICache.h"
16 #include "nsWeakReference.h"
17 #include "nsIContentSniffer.h"
18 #include "nsRefPtrHashtable.h"
19 #include "nsExpirationTracker.h"
20 #include "ImageCacheKey.h"
21 #include "imgRequest.h"
22 #include "nsIProgressEventSink.h"
23 #include "nsIChannel.h"
24 #include "nsIThreadRetargetableStreamListener.h"
25 #include "imgIRequest.h"
27 class imgLoader;
28 class imgRequestProxy;
29 class imgINotificationObserver;
30 class nsILoadGroup;
31 class imgCacheExpirationTracker;
32 class imgMemoryReporter;
34 namespace mozilla {
35 namespace image {} // namespace image
36 } // namespace mozilla
38 class imgCacheEntry {
39 public:
40 static uint32_t SecondsFromPRTime(PRTime prTime);
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 MOZ_MUST_USE 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, imgRequestProxy** _retval);
251 MOZ_MUST_USE nsresult
252 LoadImageWithChannel(nsIChannel* channel, imgINotificationObserver* aObserver,
253 nsISupports* aCX, nsIStreamListener** listener,
254 imgRequestProxy** _retval);
256 static nsresult GetMimeTypeFromContent(const char* aContents,
257 uint32_t aLength,
258 nsACString& aContentType);
261 * Returns true if the given mime type may be interpreted as an image.
263 * Some MIME types may be interpreted as both images and documents. (At the
264 * moment only "image/svg+xml" falls into this category, but there may be more
265 * in the future.) Callers which want this function to return true for such
266 * MIME types should pass AcceptedMimeTypes::IMAGES_AND_DOCUMENTS for
267 * @aAccept.
269 * @param aMimeType The MIME type to evaluate.
270 * @param aAcceptedMimeTypes Which kinds of MIME types to treat as images.
272 static bool SupportImageWithMimeType(
273 const char* aMimeType,
274 AcceptedMimeTypes aAccept = AcceptedMimeTypes::IMAGES);
276 static void GlobalInit(); // for use by the factory
277 static void Shutdown(); // for use by the factory
278 static void ShutdownMemoryReporter();
280 nsresult ClearChromeImageCache();
281 nsresult ClearImageCache();
282 void MinimizeCaches();
284 nsresult InitCache();
286 bool RemoveFromCache(const ImageCacheKey& aKey);
288 // Enumeration describing if a given entry is in the cache queue or not.
289 // There are some cases we know the entry is definitely not in the queue.
290 enum class QueueState { MaybeExists, AlreadyRemoved };
292 bool RemoveFromCache(imgCacheEntry* entry,
293 QueueState aQueueState = QueueState::MaybeExists);
295 bool PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* aEntry);
297 void AddToUncachedImages(imgRequest* aRequest);
298 void RemoveFromUncachedImages(imgRequest* aRequest);
300 // Returns true if we should prefer evicting cache entry |two| over cache
301 // entry |one|.
302 // This mixes units in the worst way, but provides reasonable results.
303 inline static bool CompareCacheEntries(const RefPtr<imgCacheEntry>& one,
304 const RefPtr<imgCacheEntry>& two) {
305 if (!one) {
306 return false;
308 if (!two) {
309 return true;
312 const double sizeweight = 1.0 - sCacheTimeWeight;
314 // We want large, old images to be evicted first (depending on their
315 // relative weights). Since a larger time is actually newer, we subtract
316 // time's weight, so an older image has a larger weight.
317 double oneweight = double(one->GetDataSize()) * sizeweight -
318 double(one->GetTouchedTime()) * sCacheTimeWeight;
319 double twoweight = double(two->GetDataSize()) * sizeweight -
320 double(two->GetTouchedTime()) * sCacheTimeWeight;
322 return oneweight < twoweight;
325 void VerifyCacheSizes();
327 // The image loader maintains a hash table of all imgCacheEntries. However,
328 // only some of them will be evicted from the cache: those who have no
329 // imgRequestProxies watching their imgRequests.
331 // Once an imgRequest has no imgRequestProxies, it should notify us by
332 // calling HasNoObservers(), and null out its cache entry pointer.
334 // Upon having a proxy start observing again, it should notify us by calling
335 // HasObservers(). The request's cache entry will be re-set before this
336 // happens, by calling imgRequest::SetCacheEntry() when an entry with no
337 // observers is re-requested.
338 bool SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry);
339 bool SetHasProxies(imgRequest* aRequest);
341 private: // methods
342 static already_AddRefed<imgLoader> CreateImageLoader();
344 bool PreferLoadFromCache(nsIURI* aURI) const;
346 bool ValidateEntry(imgCacheEntry* aEntry, nsIURI* aKey,
347 nsIURI* aInitialDocumentURI,
348 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
349 imgINotificationObserver* aObserver, nsISupports* aCX,
350 mozilla::dom::Document* aLoadingDocument,
351 nsLoadFlags aLoadFlags,
352 nsContentPolicyType aContentPolicyType,
353 bool aCanMakeNewChannel, bool* aNewChannelCreated,
354 imgRequestProxy** aProxyRequest,
355 nsIPrincipal* aLoadingPrincipal, int32_t aCORSMode);
357 bool ValidateRequestWithNewChannel(
358 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
359 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
360 imgINotificationObserver* aObserver, nsISupports* aCX,
361 mozilla::dom::Document* aLoadingDocument, uint64_t aInnerWindowId,
362 nsLoadFlags aLoadFlags, nsContentPolicyType aContentPolicyType,
363 imgRequestProxy** aProxyRequest, nsIPrincipal* aLoadingPrincipal,
364 int32_t aCORSMode, bool* aNewChannelCreated);
366 nsresult CreateNewProxyForRequest(imgRequest* aRequest,
367 nsILoadGroup* aLoadGroup,
368 mozilla::dom::Document* aLoadingDocument,
369 imgINotificationObserver* aObserver,
370 nsLoadFlags aLoadFlags,
371 imgRequestProxy** _retval);
373 void ReadAcceptHeaderPref();
375 nsresult EvictEntries(imgCacheTable& aCacheToClear);
376 nsresult EvictEntries(imgCacheQueue& aQueueToClear);
378 imgCacheTable& GetCache(bool aForChrome);
379 imgCacheTable& GetCache(const ImageCacheKey& aKey);
380 imgCacheQueue& GetCacheQueue(bool aForChrome);
381 imgCacheQueue& GetCacheQueue(const ImageCacheKey& aKey);
382 void CacheEntriesChanged(bool aForChrome, int32_t aSizeDiff = 0);
383 void CheckCacheLimits(imgCacheTable& cache, imgCacheQueue& queue);
385 private: // data
386 friend class imgCacheEntry;
387 friend class imgMemoryReporter;
389 imgCacheTable mCache;
390 imgCacheQueue mCacheQueue;
392 imgCacheTable mChromeCache;
393 imgCacheQueue mChromeCacheQueue;
395 // Hash set of every imgRequest for this loader that isn't in mCache or
396 // mChromeCache. The union over all imgLoader's of mCache, mChromeCache, and
397 // mUncachedImages should be every imgRequest that is alive. These are weak
398 // pointers so we rely on the imgRequest destructor to remove itself.
399 imgSet mUncachedImages;
400 // The imgRequest can have refs to them held on non-main thread, so we need
401 // a mutex because we modify the uncached images set from the imgRequest
402 // destructor.
403 Mutex mUncachedImagesMutex;
405 static double sCacheTimeWeight;
406 static uint32_t sCacheMaxSize;
407 static imgMemoryReporter* sMemReporter;
409 nsCString mAcceptHeader;
411 mozilla::UniquePtr<imgCacheExpirationTracker> mCacheTracker;
412 bool mRespectPrivacy;
416 * proxy stream listener class used to handle multipart/x-mixed-replace
419 #include "nsCOMPtr.h"
420 #include "nsIStreamListener.h"
421 #include "nsIThreadRetargetableStreamListener.h"
423 class ProxyListener : public nsIStreamListener,
424 public nsIThreadRetargetableStreamListener {
425 public:
426 explicit ProxyListener(nsIStreamListener* dest);
428 /* additional members */
429 NS_DECL_THREADSAFE_ISUPPORTS
430 NS_DECL_NSISTREAMLISTENER
431 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
432 NS_DECL_NSIREQUESTOBSERVER
434 private:
435 virtual ~ProxyListener();
437 nsCOMPtr<nsIStreamListener> mDestListener;
441 * A class that implements nsIProgressEventSink and forwards all calls to it to
442 * the original notification callbacks of the channel. Also implements
443 * nsIInterfaceRequestor and gives out itself for nsIProgressEventSink calls,
444 * and forwards everything else to the channel's notification callbacks.
446 class nsProgressNotificationProxy final : public nsIProgressEventSink,
447 public nsIChannelEventSink,
448 public nsIInterfaceRequestor {
449 public:
450 nsProgressNotificationProxy(nsIChannel* channel, imgIRequest* proxy)
451 : mImageRequest(proxy) {
452 channel->GetNotificationCallbacks(getter_AddRefs(mOriginalCallbacks));
455 NS_DECL_ISUPPORTS
456 NS_DECL_NSIPROGRESSEVENTSINK
457 NS_DECL_NSICHANNELEVENTSINK
458 NS_DECL_NSIINTERFACEREQUESTOR
459 private:
460 ~nsProgressNotificationProxy() {}
462 nsCOMPtr<nsIInterfaceRequestor> mOriginalCallbacks;
463 nsCOMPtr<nsIRequest> mImageRequest;
467 * validate checker
470 #include "nsCOMArray.h"
472 class imgCacheValidator : public nsIStreamListener,
473 public nsIThreadRetargetableStreamListener,
474 public nsIChannelEventSink,
475 public nsIInterfaceRequestor,
476 public nsIAsyncVerifyRedirectCallback {
477 public:
478 imgCacheValidator(nsProgressNotificationProxy* progress, imgLoader* loader,
479 imgRequest* aRequest, nsISupports* aContext,
480 uint64_t aInnerWindowId,
481 bool forcePrincipalCheckForCacheEntry);
483 void AddProxy(imgRequestProxy* aProxy);
484 void RemoveProxy(imgRequestProxy* aProxy);
486 NS_DECL_THREADSAFE_ISUPPORTS
487 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
488 NS_DECL_NSISTREAMLISTENER
489 NS_DECL_NSIREQUESTOBSERVER
490 NS_DECL_NSICHANNELEVENTSINK
491 NS_DECL_NSIINTERFACEREQUESTOR
492 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
494 private:
495 void UpdateProxies(bool aCancelRequest, bool aSyncNotify);
496 virtual ~imgCacheValidator();
498 nsCOMPtr<nsIStreamListener> mDestListener;
499 RefPtr<nsProgressNotificationProxy> mProgressProxy;
500 nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
501 nsCOMPtr<nsIChannel> mRedirectChannel;
503 RefPtr<imgRequest> mRequest;
504 AutoTArray<RefPtr<imgRequestProxy>, 4> mProxies;
506 RefPtr<imgRequest> mNewRequest;
507 RefPtr<imgCacheEntry> mNewEntry;
509 nsCOMPtr<nsISupports> mContext;
510 uint64_t mInnerWindowId;
512 imgLoader* mImgLoader;
514 bool mHadInsecureRedirect;
517 #endif // mozilla_image_imgLoader_h