Bug 1608150 [wpt PR 21112] - Add missing space in `./wpt lint` command line docs...
[gecko.git] / image / imgLoader.cpp
blob265780d1407174b92944f17e6d0f83241556903b
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 // Undefine windows version of LoadImage because our code uses that name.
8 #undef LoadImage
10 #include <algorithm>
12 #include "ImageLogging.h"
13 #include "imgLoader.h"
15 #include "mozilla/Attributes.h"
16 #include "mozilla/ClearOnShutdown.h"
17 #include "mozilla/Move.h"
18 #include "mozilla/NullPrincipal.h"
19 #include "mozilla/Preferences.h"
20 #include "mozilla/StaticPrefs_image.h"
21 #include "mozilla/StaticPrefs_network.h"
22 #include "mozilla/ChaosMode.h"
23 #include "mozilla/LoadInfo.h"
25 #include "nsImageModule.h"
26 #include "imgRequestProxy.h"
28 #include "nsCOMPtr.h"
30 #include "nsContentPolicyUtils.h"
31 #include "nsContentUtils.h"
32 #include "nsNetUtil.h"
33 #include "nsNetCID.h"
34 #include "nsIProtocolHandler.h"
35 #include "nsMimeTypes.h"
36 #include "nsStreamUtils.h"
37 #include "nsIHttpChannel.h"
38 #include "nsICacheInfoChannel.h"
39 #include "nsIClassOfService.h"
40 #include "nsIInterfaceRequestor.h"
41 #include "nsIInterfaceRequestorUtils.h"
42 #include "nsIProgressEventSink.h"
43 #include "nsIChannelEventSink.h"
44 #include "nsIAsyncVerifyRedirectCallback.h"
45 #include "nsIFileURL.h"
46 #include "nsIFile.h"
47 #include "nsCRT.h"
48 #include "nsINetworkPredictor.h"
49 #include "nsReadableUtils.h"
50 #include "mozilla/BasePrincipal.h"
51 #include "mozilla/dom/ContentParent.h"
52 #include "mozilla/dom/nsMixedContentBlocker.h"
53 #include "mozilla/image/ImageMemoryReporter.h"
54 #include "mozilla/layers/CompositorManagerChild.h"
56 #include "nsIApplicationCache.h"
57 #include "nsIApplicationCacheContainer.h"
59 #include "nsIMemoryReporter.h"
60 #include "DecoderFactory.h"
61 #include "Image.h"
62 #include "prtime.h"
63 #include "ReferrerInfo.h"
65 // we want to explore making the document own the load group
66 // so we can associate the document URI with the load group.
67 // until this point, we have an evil hack:
68 #include "nsIHttpChannelInternal.h"
69 #include "nsILoadGroupChild.h"
70 #include "nsIDocShell.h"
72 using namespace mozilla;
73 using namespace mozilla::dom;
74 using namespace mozilla::image;
75 using namespace mozilla::net;
77 MOZ_DEFINE_MALLOC_SIZE_OF(ImagesMallocSizeOf)
79 class imgMemoryReporter final : public nsIMemoryReporter {
80 ~imgMemoryReporter() = default;
82 public:
83 NS_DECL_ISUPPORTS
85 NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
86 nsISupports* aData, bool aAnonymize) override {
87 MOZ_ASSERT(NS_IsMainThread());
89 layers::CompositorManagerChild* manager =
90 CompositorManagerChild::GetInstance();
91 if (!manager || !StaticPrefs::image_mem_debug_reporting()) {
92 layers::SharedSurfacesMemoryReport sharedSurfaces;
93 FinishCollectReports(aHandleReport, aData, aAnonymize, sharedSurfaces);
94 return NS_OK;
97 RefPtr<imgMemoryReporter> self(this);
98 nsCOMPtr<nsIHandleReportCallback> handleReport(aHandleReport);
99 nsCOMPtr<nsISupports> data(aData);
100 manager->SendReportSharedSurfacesMemory(
101 [=](layers::SharedSurfacesMemoryReport aReport) {
102 self->FinishCollectReports(handleReport, data, aAnonymize, aReport);
104 [=](mozilla::ipc::ResponseRejectReason&& aReason) {
105 layers::SharedSurfacesMemoryReport sharedSurfaces;
106 self->FinishCollectReports(handleReport, data, aAnonymize,
107 sharedSurfaces);
109 return NS_OK;
112 void FinishCollectReports(
113 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
114 bool aAnonymize, layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
115 nsTArray<ImageMemoryCounter> chrome;
116 nsTArray<ImageMemoryCounter> content;
117 nsTArray<ImageMemoryCounter> uncached;
119 for (uint32_t i = 0; i < mKnownLoaders.Length(); i++) {
120 for (auto iter = mKnownLoaders[i]->mChromeCache.Iter(); !iter.Done();
121 iter.Next()) {
122 imgCacheEntry* entry = iter.UserData();
123 RefPtr<imgRequest> req = entry->GetRequest();
124 RecordCounterForRequest(req, &chrome, !entry->HasNoProxies());
126 for (auto iter = mKnownLoaders[i]->mCache.Iter(); !iter.Done();
127 iter.Next()) {
128 imgCacheEntry* entry = iter.UserData();
129 RefPtr<imgRequest> req = entry->GetRequest();
130 RecordCounterForRequest(req, &content, !entry->HasNoProxies());
132 MutexAutoLock lock(mKnownLoaders[i]->mUncachedImagesMutex);
133 for (auto iter = mKnownLoaders[i]->mUncachedImages.Iter(); !iter.Done();
134 iter.Next()) {
135 nsPtrHashKey<imgRequest>* entry = iter.Get();
136 RefPtr<imgRequest> req = entry->GetKey();
137 RecordCounterForRequest(req, &uncached, req->HasConsumers());
141 // Note that we only need to anonymize content image URIs.
143 ReportCounterArray(aHandleReport, aData, chrome, "images/chrome",
144 /* aAnonymize */ false, aSharedSurfaces);
146 ReportCounterArray(aHandleReport, aData, content, "images/content",
147 aAnonymize, aSharedSurfaces);
149 // Uncached images may be content or chrome, so anonymize them.
150 ReportCounterArray(aHandleReport, aData, uncached, "images/uncached",
151 aAnonymize, aSharedSurfaces);
153 // Report any shared surfaces that were not merged with the surface cache.
154 ImageMemoryReporter::ReportSharedSurfaces(aHandleReport, aData,
155 aSharedSurfaces);
157 nsCOMPtr<nsIMemoryReporterManager> imgr =
158 do_GetService("@mozilla.org/memory-reporter-manager;1");
159 if (imgr) {
160 imgr->EndReport();
164 static int64_t ImagesContentUsedUncompressedDistinguishedAmount() {
165 size_t n = 0;
166 for (uint32_t i = 0; i < imgLoader::sMemReporter->mKnownLoaders.Length();
167 i++) {
168 for (auto iter = imgLoader::sMemReporter->mKnownLoaders[i]->mCache.Iter();
169 !iter.Done(); iter.Next()) {
170 imgCacheEntry* entry = iter.UserData();
171 if (entry->HasNoProxies()) {
172 continue;
175 RefPtr<imgRequest> req = entry->GetRequest();
176 RefPtr<image::Image> image = req->GetImage();
177 if (!image) {
178 continue;
181 // Both this and EntryImageSizes measure
182 // images/content/raster/used/decoded memory. This function's
183 // measurement is secondary -- the result doesn't go in the "explicit"
184 // tree -- so we use moz_malloc_size_of instead of ImagesMallocSizeOf to
185 // prevent DMD from seeing it reported twice.
186 SizeOfState state(moz_malloc_size_of);
187 ImageMemoryCounter counter(image, state, /* aIsUsed = */ true);
189 n += counter.Values().DecodedHeap();
190 n += counter.Values().DecodedNonHeap();
193 return n;
196 void RegisterLoader(imgLoader* aLoader) {
197 mKnownLoaders.AppendElement(aLoader);
200 void UnregisterLoader(imgLoader* aLoader) {
201 mKnownLoaders.RemoveElement(aLoader);
204 private:
205 nsTArray<imgLoader*> mKnownLoaders;
207 struct MemoryTotal {
208 MemoryTotal& operator+=(const ImageMemoryCounter& aImageCounter) {
209 if (aImageCounter.Type() == imgIContainer::TYPE_RASTER) {
210 if (aImageCounter.IsUsed()) {
211 mUsedRasterCounter += aImageCounter.Values();
212 } else {
213 mUnusedRasterCounter += aImageCounter.Values();
215 } else if (aImageCounter.Type() == imgIContainer::TYPE_VECTOR) {
216 if (aImageCounter.IsUsed()) {
217 mUsedVectorCounter += aImageCounter.Values();
218 } else {
219 mUnusedVectorCounter += aImageCounter.Values();
221 } else {
222 MOZ_CRASH("Unexpected image type");
225 return *this;
228 const MemoryCounter& UsedRaster() const { return mUsedRasterCounter; }
229 const MemoryCounter& UnusedRaster() const { return mUnusedRasterCounter; }
230 const MemoryCounter& UsedVector() const { return mUsedVectorCounter; }
231 const MemoryCounter& UnusedVector() const { return mUnusedVectorCounter; }
233 private:
234 MemoryCounter mUsedRasterCounter;
235 MemoryCounter mUnusedRasterCounter;
236 MemoryCounter mUsedVectorCounter;
237 MemoryCounter mUnusedVectorCounter;
240 // Reports all images of a single kind, e.g. all used chrome images.
241 void ReportCounterArray(nsIHandleReportCallback* aHandleReport,
242 nsISupports* aData,
243 nsTArray<ImageMemoryCounter>& aCounterArray,
244 const char* aPathPrefix, bool aAnonymize,
245 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
246 MemoryTotal summaryTotal;
247 MemoryTotal nonNotableTotal;
249 // Report notable images, and compute total and non-notable aggregate sizes.
250 for (uint32_t i = 0; i < aCounterArray.Length(); i++) {
251 ImageMemoryCounter& counter = aCounterArray[i];
253 if (aAnonymize) {
254 counter.URI().Truncate();
255 counter.URI().AppendPrintf("<anonymized-%u>", i);
256 } else {
257 // The URI could be an extremely long data: URI. Truncate if needed.
258 static const size_t max = 256;
259 if (counter.URI().Length() > max) {
260 counter.URI().Truncate(max);
261 counter.URI().AppendLiteral(" (truncated)");
263 counter.URI().ReplaceChar('/', '\\');
266 summaryTotal += counter;
268 if (counter.IsNotable() || StaticPrefs::image_mem_debug_reporting()) {
269 ReportImage(aHandleReport, aData, aPathPrefix, counter,
270 aSharedSurfaces);
271 } else {
272 ImageMemoryReporter::TrimSharedSurfaces(counter, aSharedSurfaces);
273 nonNotableTotal += counter;
277 // Report non-notable images in aggregate.
278 ReportTotal(aHandleReport, aData, /* aExplicit = */ true, aPathPrefix,
279 "<non-notable images>/", nonNotableTotal);
281 // Report a summary in aggregate, outside of the explicit tree.
282 ReportTotal(aHandleReport, aData, /* aExplicit = */ false, aPathPrefix, "",
283 summaryTotal);
286 static void ReportImage(nsIHandleReportCallback* aHandleReport,
287 nsISupports* aData, const char* aPathPrefix,
288 const ImageMemoryCounter& aCounter,
289 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
290 nsAutoCString pathPrefix(NS_LITERAL_CSTRING("explicit/"));
291 pathPrefix.Append(aPathPrefix);
292 pathPrefix.Append(aCounter.Type() == imgIContainer::TYPE_RASTER
293 ? "/raster/"
294 : "/vector/");
295 pathPrefix.Append(aCounter.IsUsed() ? "used/" : "unused/");
296 pathPrefix.AppendLiteral("image(");
297 pathPrefix.AppendInt(aCounter.IntrinsicSize().width);
298 pathPrefix.AppendLiteral("x");
299 pathPrefix.AppendInt(aCounter.IntrinsicSize().height);
300 pathPrefix.AppendLiteral(", ");
302 if (aCounter.URI().IsEmpty()) {
303 pathPrefix.AppendLiteral("<unknown URI>");
304 } else {
305 pathPrefix.Append(aCounter.URI());
308 pathPrefix.AppendLiteral(")/");
310 ReportSurfaces(aHandleReport, aData, pathPrefix, aCounter, aSharedSurfaces);
312 ReportSourceValue(aHandleReport, aData, pathPrefix, aCounter.Values());
315 static void ReportSurfaces(
316 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
317 const nsACString& aPathPrefix, const ImageMemoryCounter& aCounter,
318 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
319 for (const SurfaceMemoryCounter& counter : aCounter.Surfaces()) {
320 nsAutoCString surfacePathPrefix(aPathPrefix);
321 if (counter.IsLocked()) {
322 surfacePathPrefix.AppendLiteral("locked/");
323 } else {
324 surfacePathPrefix.AppendLiteral("unlocked/");
326 if (counter.IsFactor2()) {
327 surfacePathPrefix.AppendLiteral("factor2/");
329 if (counter.CannotSubstitute()) {
330 surfacePathPrefix.AppendLiteral("cannot_substitute/");
332 surfacePathPrefix.AppendLiteral("surface(");
333 surfacePathPrefix.AppendInt(counter.Key().Size().width);
334 surfacePathPrefix.AppendLiteral("x");
335 surfacePathPrefix.AppendInt(counter.Key().Size().height);
337 if (counter.Values().ExternalHandles() > 0) {
338 surfacePathPrefix.AppendLiteral(", handles:");
339 surfacePathPrefix.AppendInt(
340 uint32_t(counter.Values().ExternalHandles()));
343 ImageMemoryReporter::AppendSharedSurfacePrefix(surfacePathPrefix, counter,
344 aSharedSurfaces);
346 if (counter.Type() == SurfaceMemoryCounterType::NORMAL) {
347 PlaybackType playback = counter.Key().Playback();
348 if (playback == PlaybackType::eAnimated) {
349 if (StaticPrefs::image_mem_debug_reporting()) {
350 surfacePathPrefix.AppendPrintf(
351 " (animation %4u)", uint32_t(counter.Values().FrameIndex()));
352 } else {
353 surfacePathPrefix.AppendLiteral(" (animation)");
357 if (counter.Key().Flags() != DefaultSurfaceFlags()) {
358 surfacePathPrefix.AppendLiteral(", flags:");
359 surfacePathPrefix.AppendInt(uint32_t(counter.Key().Flags()),
360 /* aRadix = */ 16);
363 if (counter.Key().SVGContext()) {
364 const SVGImageContext& context = counter.Key().SVGContext().ref();
365 surfacePathPrefix.AppendLiteral(", svgContext:[ ");
366 if (context.GetViewportSize()) {
367 const CSSIntSize& size = context.GetViewportSize().ref();
368 surfacePathPrefix.AppendLiteral("viewport=(");
369 surfacePathPrefix.AppendInt(size.width);
370 surfacePathPrefix.AppendLiteral("x");
371 surfacePathPrefix.AppendInt(size.height);
372 surfacePathPrefix.AppendLiteral(") ");
374 if (context.GetPreserveAspectRatio()) {
375 nsAutoString aspect;
376 context.GetPreserveAspectRatio()->ToString(aspect);
377 surfacePathPrefix.AppendLiteral("preserveAspectRatio=(");
378 LossyAppendUTF16toASCII(aspect, surfacePathPrefix);
379 surfacePathPrefix.AppendLiteral(") ");
381 if (context.GetContextPaint()) {
382 const SVGEmbeddingContextPaint* paint = context.GetContextPaint();
383 surfacePathPrefix.AppendLiteral("contextPaint=(");
384 if (paint->GetFill()) {
385 surfacePathPrefix.AppendLiteral(" fill=");
386 surfacePathPrefix.AppendInt(paint->GetFill()->ToABGR(), 16);
388 if (paint->GetFillOpacity()) {
389 surfacePathPrefix.AppendLiteral(" fillOpa=");
390 surfacePathPrefix.AppendFloat(paint->GetFillOpacity());
392 if (paint->GetStroke()) {
393 surfacePathPrefix.AppendLiteral(" stroke=");
394 surfacePathPrefix.AppendInt(paint->GetStroke()->ToABGR(), 16);
396 if (paint->GetStrokeOpacity()) {
397 surfacePathPrefix.AppendLiteral(" strokeOpa=");
398 surfacePathPrefix.AppendFloat(paint->GetStrokeOpacity());
400 surfacePathPrefix.AppendLiteral(" ) ");
402 surfacePathPrefix.AppendLiteral("]");
404 } else if (counter.Type() == SurfaceMemoryCounterType::COMPOSITING) {
405 surfacePathPrefix.AppendLiteral(", compositing frame");
406 } else if (counter.Type() == SurfaceMemoryCounterType::COMPOSITING_PREV) {
407 surfacePathPrefix.AppendLiteral(", compositing prev frame");
408 } else {
409 MOZ_ASSERT_UNREACHABLE("Unknown counter type");
412 surfacePathPrefix.AppendLiteral(")/");
414 ReportValues(aHandleReport, aData, surfacePathPrefix, counter.Values());
418 static void ReportTotal(nsIHandleReportCallback* aHandleReport,
419 nsISupports* aData, bool aExplicit,
420 const char* aPathPrefix, const char* aPathInfix,
421 const MemoryTotal& aTotal) {
422 nsAutoCString pathPrefix;
423 if (aExplicit) {
424 pathPrefix.AppendLiteral("explicit/");
426 pathPrefix.Append(aPathPrefix);
428 nsAutoCString rasterUsedPrefix(pathPrefix);
429 rasterUsedPrefix.AppendLiteral("/raster/used/");
430 rasterUsedPrefix.Append(aPathInfix);
431 ReportValues(aHandleReport, aData, rasterUsedPrefix, aTotal.UsedRaster());
433 nsAutoCString rasterUnusedPrefix(pathPrefix);
434 rasterUnusedPrefix.AppendLiteral("/raster/unused/");
435 rasterUnusedPrefix.Append(aPathInfix);
436 ReportValues(aHandleReport, aData, rasterUnusedPrefix,
437 aTotal.UnusedRaster());
439 nsAutoCString vectorUsedPrefix(pathPrefix);
440 vectorUsedPrefix.AppendLiteral("/vector/used/");
441 vectorUsedPrefix.Append(aPathInfix);
442 ReportValues(aHandleReport, aData, vectorUsedPrefix, aTotal.UsedVector());
444 nsAutoCString vectorUnusedPrefix(pathPrefix);
445 vectorUnusedPrefix.AppendLiteral("/vector/unused/");
446 vectorUnusedPrefix.Append(aPathInfix);
447 ReportValues(aHandleReport, aData, vectorUnusedPrefix,
448 aTotal.UnusedVector());
451 static void ReportValues(nsIHandleReportCallback* aHandleReport,
452 nsISupports* aData, const nsACString& aPathPrefix,
453 const MemoryCounter& aCounter) {
454 ReportSourceValue(aHandleReport, aData, aPathPrefix, aCounter);
456 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "decoded-heap",
457 "Decoded image data which is stored on the heap.",
458 aCounter.DecodedHeap());
460 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
461 "decoded-nonheap",
462 "Decoded image data which isn't stored on the heap.",
463 aCounter.DecodedNonHeap());
466 static void ReportSourceValue(nsIHandleReportCallback* aHandleReport,
467 nsISupports* aData,
468 const nsACString& aPathPrefix,
469 const MemoryCounter& aCounter) {
470 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "source",
471 "Raster image source data and vector image documents.",
472 aCounter.Source());
475 static void ReportValue(nsIHandleReportCallback* aHandleReport,
476 nsISupports* aData, int32_t aKind,
477 const nsACString& aPathPrefix,
478 const char* aPathSuffix, const char* aDescription,
479 size_t aValue) {
480 if (aValue == 0) {
481 return;
484 nsAutoCString desc(aDescription);
485 nsAutoCString path(aPathPrefix);
486 path.Append(aPathSuffix);
488 aHandleReport->Callback(EmptyCString(), path, aKind, UNITS_BYTES, aValue,
489 desc, aData);
492 static void RecordCounterForRequest(imgRequest* aRequest,
493 nsTArray<ImageMemoryCounter>* aArray,
494 bool aIsUsed) {
495 RefPtr<image::Image> image = aRequest->GetImage();
496 if (!image) {
497 return;
500 SizeOfState state(ImagesMallocSizeOf);
501 ImageMemoryCounter counter(image, state, aIsUsed);
503 aArray->AppendElement(std::move(counter));
507 NS_IMPL_ISUPPORTS(imgMemoryReporter, nsIMemoryReporter)
509 NS_IMPL_ISUPPORTS(nsProgressNotificationProxy, nsIProgressEventSink,
510 nsIChannelEventSink, nsIInterfaceRequestor)
512 NS_IMETHODIMP
513 nsProgressNotificationProxy::OnProgress(nsIRequest* request, nsISupports* ctxt,
514 int64_t progress, int64_t progressMax) {
515 nsCOMPtr<nsILoadGroup> loadGroup;
516 request->GetLoadGroup(getter_AddRefs(loadGroup));
518 nsCOMPtr<nsIProgressEventSink> target;
519 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
520 NS_GET_IID(nsIProgressEventSink),
521 getter_AddRefs(target));
522 if (!target) {
523 return NS_OK;
525 return target->OnProgress(mImageRequest, ctxt, progress, progressMax);
528 NS_IMETHODIMP
529 nsProgressNotificationProxy::OnStatus(nsIRequest* request, nsISupports* ctxt,
530 nsresult status,
531 const char16_t* statusArg) {
532 nsCOMPtr<nsILoadGroup> loadGroup;
533 request->GetLoadGroup(getter_AddRefs(loadGroup));
535 nsCOMPtr<nsIProgressEventSink> target;
536 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
537 NS_GET_IID(nsIProgressEventSink),
538 getter_AddRefs(target));
539 if (!target) {
540 return NS_OK;
542 return target->OnStatus(mImageRequest, ctxt, status, statusArg);
545 NS_IMETHODIMP
546 nsProgressNotificationProxy::AsyncOnChannelRedirect(
547 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
548 nsIAsyncVerifyRedirectCallback* cb) {
549 // Tell the original original callbacks about it too
550 nsCOMPtr<nsILoadGroup> loadGroup;
551 newChannel->GetLoadGroup(getter_AddRefs(loadGroup));
552 nsCOMPtr<nsIChannelEventSink> target;
553 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
554 NS_GET_IID(nsIChannelEventSink),
555 getter_AddRefs(target));
556 if (!target) {
557 cb->OnRedirectVerifyCallback(NS_OK);
558 return NS_OK;
561 // Delegate to |target| if set, reusing |cb|
562 return target->AsyncOnChannelRedirect(oldChannel, newChannel, flags, cb);
565 NS_IMETHODIMP
566 nsProgressNotificationProxy::GetInterface(const nsIID& iid, void** result) {
567 if (iid.Equals(NS_GET_IID(nsIProgressEventSink))) {
568 *result = static_cast<nsIProgressEventSink*>(this);
569 NS_ADDREF_THIS();
570 return NS_OK;
572 if (iid.Equals(NS_GET_IID(nsIChannelEventSink))) {
573 *result = static_cast<nsIChannelEventSink*>(this);
574 NS_ADDREF_THIS();
575 return NS_OK;
577 if (mOriginalCallbacks) {
578 return mOriginalCallbacks->GetInterface(iid, result);
580 return NS_NOINTERFACE;
583 static void NewRequestAndEntry(bool aForcePrincipalCheckForCacheEntry,
584 imgLoader* aLoader, const ImageCacheKey& aKey,
585 imgRequest** aRequest, imgCacheEntry** aEntry) {
586 RefPtr<imgRequest> request = new imgRequest(aLoader, aKey);
587 RefPtr<imgCacheEntry> entry =
588 new imgCacheEntry(aLoader, request, aForcePrincipalCheckForCacheEntry);
589 aLoader->AddToUncachedImages(request);
590 request.forget(aRequest);
591 entry.forget(aEntry);
594 static bool ShouldRevalidateEntry(imgCacheEntry* aEntry, nsLoadFlags aFlags,
595 bool aHasExpired) {
596 bool bValidateEntry = false;
598 if (aFlags & nsIRequest::LOAD_BYPASS_CACHE) {
599 return false;
602 if (aFlags & nsIRequest::VALIDATE_ALWAYS) {
603 bValidateEntry = true;
604 } else if (aEntry->GetMustValidate()) {
605 bValidateEntry = true;
606 } else if (aHasExpired) {
607 // The cache entry has expired... Determine whether the stale cache
608 // entry can be used without validation...
609 if (aFlags &
610 (nsIRequest::VALIDATE_NEVER | nsIRequest::VALIDATE_ONCE_PER_SESSION)) {
611 // VALIDATE_NEVER and VALIDATE_ONCE_PER_SESSION allow stale cache
612 // entries to be used unless they have been explicitly marked to
613 // indicate that revalidation is necessary.
614 bValidateEntry = false;
616 } else if (!(aFlags & nsIRequest::LOAD_FROM_CACHE)) {
617 // LOAD_FROM_CACHE allows a stale cache entry to be used... Otherwise,
618 // the entry must be revalidated.
619 bValidateEntry = true;
623 return bValidateEntry;
626 /* Call content policies on cached images that went through a redirect */
627 static bool ShouldLoadCachedImage(imgRequest* aImgRequest,
628 nsISupports* aLoadingContext,
629 nsIPrincipal* aTriggeringPrincipal,
630 nsContentPolicyType aPolicyType,
631 bool aSendCSPViolationReports) {
632 /* Call content policies on cached images - Bug 1082837
633 * Cached images are keyed off of the first uri in a redirect chain.
634 * Hence content policies don't get a chance to test the intermediate hops
635 * or the final desitnation. Here we test the final destination using
636 * mFinalURI off of the imgRequest and passing it into content policies.
637 * For Mixed Content Blocker, we do an additional check to determine if any
638 * of the intermediary hops went through an insecure redirect with the
639 * mHadInsecureRedirect flag
641 bool insecureRedirect = aImgRequest->HadInsecureRedirect();
642 nsCOMPtr<nsIURI> contentLocation;
643 aImgRequest->GetFinalURI(getter_AddRefs(contentLocation));
644 nsresult rv;
646 nsCOMPtr<nsINode> requestingNode = do_QueryInterface(aLoadingContext);
647 nsCOMPtr<nsIPrincipal> loadingPrincipal =
648 requestingNode ? requestingNode->NodePrincipal() : aTriggeringPrincipal;
649 // If there is no context and also no triggeringPrincipal, then we use a fresh
650 // nullPrincipal as the loadingPrincipal because we can not create a loadinfo
651 // without a valid loadingPrincipal.
652 if (!loadingPrincipal) {
653 loadingPrincipal = NullPrincipal::CreateWithoutOriginAttributes();
656 nsCOMPtr<nsILoadInfo> secCheckLoadInfo = new LoadInfo(
657 loadingPrincipal, aTriggeringPrincipal, requestingNode,
658 nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK, aPolicyType);
660 secCheckLoadInfo->SetSendCSPViolationEvents(aSendCSPViolationReports);
662 int16_t decision = nsIContentPolicy::REJECT_REQUEST;
663 rv = NS_CheckContentLoadPolicy(contentLocation, secCheckLoadInfo,
664 EmptyCString(), // mime guess
665 &decision, nsContentUtils::GetContentPolicy());
666 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
667 return false;
670 // We call all Content Policies above, but we also have to call mcb
671 // individually to check the intermediary redirect hops are secure.
672 if (insecureRedirect) {
673 // Bug 1314356: If the image ended up in the cache upgraded by HSTS and the
674 // page uses upgrade-inscure-requests it had an insecure redirect
675 // (http->https). We need to invalidate the image and reload it because
676 // mixed content blocker only bails if upgrade-insecure-requests is set on
677 // the doc and the resource load is http: which would result in an incorrect
678 // mixed content warning.
679 nsCOMPtr<nsIDocShell> docShell =
680 NS_CP_GetDocShellFromContext(aLoadingContext);
681 if (docShell) {
682 Document* document = docShell->GetDocument();
683 if (document && document->GetUpgradeInsecureRequests(false)) {
684 return false;
688 if (!aTriggeringPrincipal || !aTriggeringPrincipal->IsSystemPrincipal()) {
689 // Set the requestingLocation from the aTriggeringPrincipal.
690 nsCOMPtr<nsIURI> requestingLocation;
691 if (aTriggeringPrincipal) {
692 rv = aTriggeringPrincipal->GetURI(getter_AddRefs(requestingLocation));
693 NS_ENSURE_SUCCESS(rv, false);
696 // reset the decision for mixed content blocker check
697 decision = nsIContentPolicy::REJECT_REQUEST;
698 rv = nsMixedContentBlocker::ShouldLoad(
699 insecureRedirect, aPolicyType, contentLocation, requestingLocation,
700 aLoadingContext,
701 EmptyCString(), // mime guess
702 aTriggeringPrincipal, &decision);
703 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
704 return false;
709 return true;
712 // Returns true if this request is compatible with the given CORS mode on the
713 // given loading principal, and false if the request may not be reused due
714 // to CORS. Also checks the Referrer Policy, since requests with different
715 // referrers/policies may generate different responses.
716 static bool ValidateSecurityInfo(imgRequest* request, bool forcePrincipalCheck,
717 int32_t corsmode,
718 nsIPrincipal* triggeringPrincipal,
719 nsISupports* aCX,
720 nsContentPolicyType aPolicyType,
721 nsIReferrerInfo* aReferrerInfo) {
722 // If the referrer policy doesn't match, we can't use this request.
723 // XXX: Note that we only validate referrer policy, not referrerInfo object.
724 // We should do with referrerInfo object, but it will cause us to use more
725 // resources in the common case (the same policies but different original
726 // referrers).
727 // XXX: this will return false if an image has different referrer attributes,
728 // i.e. we currently don't use the cached image but reload the image with
729 // the new referrer policy bug 1174921
730 ReferrerPolicy referrerPolicy = ReferrerPolicy::_empty;
731 if (aReferrerInfo) {
732 referrerPolicy = aReferrerInfo->ReferrerPolicy();
735 ReferrerPolicy requestReferrerPolicy = ReferrerPolicy::_empty;
736 if (request->GetReferrerInfo()) {
737 requestReferrerPolicy = request->GetReferrerInfo()->ReferrerPolicy();
740 if (referrerPolicy != requestReferrerPolicy) {
741 return false;
744 // If the entry's CORS mode doesn't match, or the CORS mode matches but the
745 // document principal isn't the same, we can't use this request.
746 if (request->GetCORSMode() != corsmode) {
747 return false;
749 if (request->GetCORSMode() != imgIRequest::CORS_NONE || forcePrincipalCheck) {
750 nsCOMPtr<nsIPrincipal> otherprincipal = request->GetTriggeringPrincipal();
752 // If we previously had a principal, but we don't now, we can't use this
753 // request.
754 if (otherprincipal && !triggeringPrincipal) {
755 return false;
758 if (otherprincipal && triggeringPrincipal) {
759 bool equals = false;
760 otherprincipal->Equals(triggeringPrincipal, &equals);
761 if (!equals) {
762 return false;
767 // Content Policy Check on Cached Images
768 return ShouldLoadCachedImage(request, aCX, triggeringPrincipal, aPolicyType,
769 /* aSendCSPViolationReports */ false);
772 static nsresult NewImageChannel(
773 nsIChannel** aResult,
774 // If aForcePrincipalCheckForCacheEntry is true, then we will
775 // force a principal check even when not using CORS before
776 // assuming we have a cache hit on a cache entry that we
777 // create for this channel. This is an out param that should
778 // be set to true if this channel ends up depending on
779 // aTriggeringPrincipal and false otherwise.
780 bool* aForcePrincipalCheckForCacheEntry, nsIURI* aURI,
781 nsIURI* aInitialDocumentURI, int32_t aCORSMode,
782 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
783 const nsCString& aAcceptHeader, nsLoadFlags aLoadFlags,
784 nsContentPolicyType aPolicyType, nsIPrincipal* aTriggeringPrincipal,
785 nsISupports* aRequestingContext, bool aRespectPrivacy) {
786 MOZ_ASSERT(aResult);
788 nsresult rv;
789 nsCOMPtr<nsIHttpChannel> newHttpChannel;
791 nsCOMPtr<nsIInterfaceRequestor> callbacks;
793 if (aLoadGroup) {
794 // Get the notification callbacks from the load group for the new channel.
796 // XXX: This is not exactly correct, because the network request could be
797 // referenced by multiple windows... However, the new channel needs
798 // something. So, using the 'first' notification callbacks is better
799 // than nothing...
801 aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
804 // Pass in a nullptr loadgroup because this is the underlying network
805 // request. This request may be referenced by several proxy image requests
806 // (possibly in different documents).
807 // If all of the proxy requests are canceled then this request should be
808 // canceled too.
811 nsCOMPtr<nsINode> requestingNode = do_QueryInterface(aRequestingContext);
813 nsSecurityFlags securityFlags =
814 aCORSMode == imgIRequest::CORS_NONE
815 ? nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS
816 : nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS;
817 if (aCORSMode == imgIRequest::CORS_ANONYMOUS) {
818 securityFlags |= nsILoadInfo::SEC_COOKIES_SAME_ORIGIN;
819 } else if (aCORSMode == imgIRequest::CORS_USE_CREDENTIALS) {
820 securityFlags |= nsILoadInfo::SEC_COOKIES_INCLUDE;
822 securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
824 // Note we are calling NS_NewChannelWithTriggeringPrincipal() here with a
825 // node and a principal. This is for things like background images that are
826 // specified by user stylesheets, where the document is being styled, but
827 // the principal is that of the user stylesheet.
828 if (requestingNode && aTriggeringPrincipal) {
829 rv = NS_NewChannelWithTriggeringPrincipal(aResult, aURI, requestingNode,
830 aTriggeringPrincipal,
831 securityFlags, aPolicyType,
832 nullptr, // PerformanceStorage
833 nullptr, // loadGroup
834 callbacks, aLoadFlags);
836 if (NS_FAILED(rv)) {
837 return rv;
840 if (aPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
841 // If this is a favicon loading, we will use the originAttributes from the
842 // triggeringPrincipal as the channel's originAttributes. This allows the
843 // favicon loading from XUL will use the correct originAttributes.
845 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
846 rv = loadInfo->SetOriginAttributes(
847 aTriggeringPrincipal->OriginAttributesRef());
849 } else {
850 // either we are loading something inside a document, in which case
851 // we should always have a requestingNode, or we are loading something
852 // outside a document, in which case the triggeringPrincipal and
853 // triggeringPrincipal should always be the systemPrincipal.
854 // However, there are exceptions: one is Notifications which create a
855 // channel in the parent process in which case we can't get a
856 // requestingNode.
857 rv = NS_NewChannel(aResult, aURI, nsContentUtils::GetSystemPrincipal(),
858 securityFlags, aPolicyType,
859 nullptr, // nsICookieSettings
860 nullptr, // PerformanceStorage
861 nullptr, // loadGroup
862 callbacks, aLoadFlags);
864 if (NS_FAILED(rv)) {
865 return rv;
868 // Use the OriginAttributes from the loading principal, if one is available,
869 // and adjust the private browsing ID based on what kind of load the caller
870 // has asked us to perform.
871 OriginAttributes attrs;
872 if (aTriggeringPrincipal) {
873 attrs = aTriggeringPrincipal->OriginAttributesRef();
875 attrs.mPrivateBrowsingId = aRespectPrivacy ? 1 : 0;
877 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
878 rv = loadInfo->SetOriginAttributes(attrs);
881 if (NS_FAILED(rv)) {
882 return rv;
885 // only inherit if we have a principal
886 *aForcePrincipalCheckForCacheEntry =
887 aTriggeringPrincipal && nsContentUtils::ChannelShouldInheritPrincipal(
888 aTriggeringPrincipal, aURI,
889 /* aInheritForAboutBlank */ false,
890 /* aForceInherit */ false);
892 // Initialize HTTP-specific attributes
893 newHttpChannel = do_QueryInterface(*aResult);
894 if (newHttpChannel) {
895 rv = newHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
896 aAcceptHeader, false);
897 MOZ_ASSERT(NS_SUCCEEDED(rv));
899 nsCOMPtr<nsIHttpChannelInternal> httpChannelInternal =
900 do_QueryInterface(newHttpChannel);
901 NS_ENSURE_TRUE(httpChannelInternal, NS_ERROR_UNEXPECTED);
902 rv = httpChannelInternal->SetDocumentURI(aInitialDocumentURI);
903 MOZ_ASSERT(NS_SUCCEEDED(rv));
904 if (aReferrerInfo) {
905 DebugOnly<nsresult> rv = newHttpChannel->SetReferrerInfo(aReferrerInfo);
906 MOZ_ASSERT(NS_SUCCEEDED(rv));
910 // Image channels are loaded by default with reduced priority.
911 nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(*aResult);
912 if (p) {
913 uint32_t priority = nsISupportsPriority::PRIORITY_LOW;
915 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
916 ++priority; // further reduce priority for background loads
919 p->AdjustPriority(priority);
922 // Create a new loadgroup for this new channel, using the old group as
923 // the parent. The indirection keeps the channel insulated from cancels,
924 // but does allow a way for this revalidation to be associated with at
925 // least one base load group for scheduling/caching purposes.
927 nsCOMPtr<nsILoadGroup> loadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID);
928 nsCOMPtr<nsILoadGroupChild> childLoadGroup = do_QueryInterface(loadGroup);
929 if (childLoadGroup) {
930 childLoadGroup->SetParentLoadGroup(aLoadGroup);
932 (*aResult)->SetLoadGroup(loadGroup);
934 return NS_OK;
937 /* static */
938 uint32_t imgCacheEntry::SecondsFromPRTime(PRTime prTime) {
939 return uint32_t(int64_t(prTime) / int64_t(PR_USEC_PER_SEC));
942 imgCacheEntry::imgCacheEntry(imgLoader* loader, imgRequest* request,
943 bool forcePrincipalCheck)
944 : mLoader(loader),
945 mRequest(request),
946 mDataSize(0),
947 mTouchedTime(SecondsFromPRTime(PR_Now())),
948 mLoadTime(SecondsFromPRTime(PR_Now())),
949 mExpiryTime(0),
950 mMustValidate(false),
951 // We start off as evicted so we don't try to update the cache.
952 // PutIntoCache will set this to false.
953 mEvicted(true),
954 mHasNoProxies(true),
955 mForcePrincipalCheck(forcePrincipalCheck) {}
957 imgCacheEntry::~imgCacheEntry() {
958 LOG_FUNC(gImgLog, "imgCacheEntry::~imgCacheEntry()");
961 void imgCacheEntry::Touch(bool updateTime /* = true */) {
962 LOG_SCOPE(gImgLog, "imgCacheEntry::Touch");
964 if (updateTime) {
965 mTouchedTime = SecondsFromPRTime(PR_Now());
968 UpdateCache();
971 void imgCacheEntry::UpdateCache(int32_t diff /* = 0 */) {
972 // Don't update the cache if we've been removed from it or it doesn't care
973 // about our size or usage.
974 if (!Evicted() && HasNoProxies()) {
975 mLoader->CacheEntriesChanged(mRequest->IsChrome(), diff);
979 void imgCacheEntry::UpdateLoadTime() {
980 mLoadTime = SecondsFromPRTime(PR_Now());
983 void imgCacheEntry::SetHasNoProxies(bool hasNoProxies) {
984 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
985 if (hasNoProxies) {
986 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies true", "uri",
987 mRequest->CacheKey().URI());
988 } else {
989 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies false",
990 "uri", mRequest->CacheKey().URI());
994 mHasNoProxies = hasNoProxies;
997 imgCacheQueue::imgCacheQueue() : mDirty(false), mSize(0) {}
999 void imgCacheQueue::UpdateSize(int32_t diff) { mSize += diff; }
1001 uint32_t imgCacheQueue::GetSize() const { return mSize; }
1003 void imgCacheQueue::Remove(imgCacheEntry* entry) {
1004 uint64_t index = mQueue.IndexOf(entry);
1005 if (index == queueContainer::NoIndex) {
1006 return;
1009 mSize -= mQueue[index]->GetDataSize();
1011 // If the queue is clean and this is the first entry,
1012 // then we can efficiently remove the entry without
1013 // dirtying the sort order.
1014 if (!IsDirty() && index == 0) {
1015 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1016 mQueue.RemoveLastElement();
1017 return;
1020 // Remove from the middle of the list. This potentially
1021 // breaks the binary heap sort order.
1022 mQueue.RemoveElementAt(index);
1024 // If we only have one entry or the queue is empty, though,
1025 // then the sort order is still effectively good. Simply
1026 // refresh the list to clear the dirty flag.
1027 if (mQueue.Length() <= 1) {
1028 Refresh();
1029 return;
1032 // Otherwise we must mark the queue dirty and potentially
1033 // trigger an expensive sort later.
1034 MarkDirty();
1037 void imgCacheQueue::Push(imgCacheEntry* entry) {
1038 mSize += entry->GetDataSize();
1040 RefPtr<imgCacheEntry> refptr(entry);
1041 mQueue.AppendElement(std::move(refptr));
1042 // If we're not dirty already, then we can efficiently add this to the
1043 // binary heap immediately. This is only O(log n).
1044 if (!IsDirty()) {
1045 std::push_heap(mQueue.begin(), mQueue.end(),
1046 imgLoader::CompareCacheEntries);
1050 already_AddRefed<imgCacheEntry> imgCacheQueue::Pop() {
1051 if (mQueue.IsEmpty()) {
1052 return nullptr;
1054 if (IsDirty()) {
1055 Refresh();
1058 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1059 RefPtr<imgCacheEntry> entry = mQueue.PopLastElement();
1061 mSize -= entry->GetDataSize();
1062 return entry.forget();
1065 void imgCacheQueue::Refresh() {
1066 // Resort the list. This is an O(3 * n) operation and best avoided
1067 // if possible.
1068 std::make_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1069 mDirty = false;
1072 void imgCacheQueue::MarkDirty() { mDirty = true; }
1074 bool imgCacheQueue::IsDirty() { return mDirty; }
1076 uint32_t imgCacheQueue::GetNumElements() const { return mQueue.Length(); }
1078 bool imgCacheQueue::Contains(imgCacheEntry* aEntry) const {
1079 return mQueue.Contains(aEntry);
1082 imgCacheQueue::iterator imgCacheQueue::begin() { return mQueue.begin(); }
1084 imgCacheQueue::const_iterator imgCacheQueue::begin() const {
1085 return mQueue.begin();
1088 imgCacheQueue::iterator imgCacheQueue::end() { return mQueue.end(); }
1090 imgCacheQueue::const_iterator imgCacheQueue::end() const {
1091 return mQueue.end();
1094 nsresult imgLoader::CreateNewProxyForRequest(
1095 imgRequest* aRequest, nsILoadGroup* aLoadGroup, Document* aLoadingDocument,
1096 imgINotificationObserver* aObserver, nsLoadFlags aLoadFlags,
1097 imgRequestProxy** _retval) {
1098 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::CreateNewProxyForRequest",
1099 "imgRequest", aRequest);
1101 /* XXX If we move decoding onto separate threads, we should save off the
1102 calling thread here and pass it off to |proxyRequest| so that it call
1103 proxy calls to |aObserver|.
1106 RefPtr<imgRequestProxy> proxyRequest = new imgRequestProxy();
1108 /* It is important to call |SetLoadFlags()| before calling |Init()| because
1109 |Init()| adds the request to the loadgroup.
1111 proxyRequest->SetLoadFlags(aLoadFlags);
1113 nsCOMPtr<nsIURI> uri;
1114 aRequest->GetURI(getter_AddRefs(uri));
1116 // init adds itself to imgRequest's list of observers
1117 nsresult rv = proxyRequest->Init(aRequest, aLoadGroup, aLoadingDocument, uri,
1118 aObserver);
1119 if (NS_WARN_IF(NS_FAILED(rv))) {
1120 return rv;
1123 proxyRequest.forget(_retval);
1124 return NS_OK;
1127 class imgCacheExpirationTracker final
1128 : public nsExpirationTracker<imgCacheEntry, 3> {
1129 enum { TIMEOUT_SECONDS = 10 };
1131 public:
1132 imgCacheExpirationTracker();
1134 protected:
1135 void NotifyExpired(imgCacheEntry* entry) override;
1138 imgCacheExpirationTracker::imgCacheExpirationTracker()
1139 : nsExpirationTracker<imgCacheEntry, 3>(
1140 TIMEOUT_SECONDS * 1000, "imgCacheExpirationTracker",
1141 SystemGroup::EventTargetFor(TaskCategory::Other)) {}
1143 void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry* entry) {
1144 // Hold on to a reference to this entry, because the expiration tracker
1145 // mechanism doesn't.
1146 RefPtr<imgCacheEntry> kungFuDeathGrip(entry);
1148 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1149 RefPtr<imgRequest> req = entry->GetRequest();
1150 if (req) {
1151 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheExpirationTracker::NotifyExpired",
1152 "entry", req->CacheKey().URI());
1156 // We can be called multiple times on the same entry. Don't do work multiple
1157 // times.
1158 if (!entry->Evicted()) {
1159 entry->Loader()->RemoveFromCache(entry);
1162 entry->Loader()->VerifyCacheSizes();
1165 ///////////////////////////////////////////////////////////////////////////////
1166 // imgLoader
1167 ///////////////////////////////////////////////////////////////////////////////
1169 double imgLoader::sCacheTimeWeight;
1170 uint32_t imgLoader::sCacheMaxSize;
1171 imgMemoryReporter* imgLoader::sMemReporter;
1173 NS_IMPL_ISUPPORTS(imgLoader, imgILoader, nsIContentSniffer, imgICache,
1174 nsISupportsWeakReference, nsIObserver)
1176 static imgLoader* gNormalLoader = nullptr;
1177 static imgLoader* gPrivateBrowsingLoader = nullptr;
1179 /* static */
1180 already_AddRefed<imgLoader> imgLoader::CreateImageLoader() {
1181 // In some cases, such as xpctests, XPCOM modules are not automatically
1182 // initialized. We need to make sure that our module is initialized before
1183 // we hand out imgLoader instances and code starts using them.
1184 mozilla::image::EnsureModuleInitialized();
1186 RefPtr<imgLoader> loader = new imgLoader();
1187 loader->Init();
1189 return loader.forget();
1192 imgLoader* imgLoader::NormalLoader() {
1193 if (!gNormalLoader) {
1194 gNormalLoader = CreateImageLoader().take();
1196 return gNormalLoader;
1199 imgLoader* imgLoader::PrivateBrowsingLoader() {
1200 if (!gPrivateBrowsingLoader) {
1201 gPrivateBrowsingLoader = CreateImageLoader().take();
1202 gPrivateBrowsingLoader->RespectPrivacyNotifications();
1204 return gPrivateBrowsingLoader;
1207 imgLoader::imgLoader()
1208 : mUncachedImagesMutex("imgLoader::UncachedImages"),
1209 mRespectPrivacy(false) {
1210 sMemReporter->AddRef();
1211 sMemReporter->RegisterLoader(this);
1214 imgLoader::~imgLoader() {
1215 ClearChromeImageCache();
1216 ClearImageCache();
1218 // If there are any of our imgRequest's left they are in the uncached
1219 // images set, so clear their pointer to us.
1220 MutexAutoLock lock(mUncachedImagesMutex);
1221 for (auto iter = mUncachedImages.Iter(); !iter.Done(); iter.Next()) {
1222 nsPtrHashKey<imgRequest>* entry = iter.Get();
1223 RefPtr<imgRequest> req = entry->GetKey();
1224 req->ClearLoader();
1227 sMemReporter->UnregisterLoader(this);
1228 sMemReporter->Release();
1231 void imgLoader::VerifyCacheSizes() {
1232 #ifdef DEBUG
1233 if (!mCacheTracker) {
1234 return;
1237 uint32_t cachesize = mCache.Count() + mChromeCache.Count();
1238 uint32_t queuesize =
1239 mCacheQueue.GetNumElements() + mChromeCacheQueue.GetNumElements();
1240 uint32_t trackersize = 0;
1241 for (nsExpirationTracker<imgCacheEntry, 3>::Iterator it(mCacheTracker.get());
1242 it.Next();) {
1243 trackersize++;
1245 MOZ_ASSERT(queuesize == trackersize, "Queue and tracker sizes out of sync!");
1246 MOZ_ASSERT(queuesize <= cachesize, "Queue has more elements than cache!");
1247 #endif
1250 imgLoader::imgCacheTable& imgLoader::GetCache(bool aForChrome) {
1251 return aForChrome ? mChromeCache : mCache;
1254 imgLoader::imgCacheTable& imgLoader::GetCache(const ImageCacheKey& aKey) {
1255 return GetCache(aKey.IsChrome());
1258 imgCacheQueue& imgLoader::GetCacheQueue(bool aForChrome) {
1259 return aForChrome ? mChromeCacheQueue : mCacheQueue;
1262 imgCacheQueue& imgLoader::GetCacheQueue(const ImageCacheKey& aKey) {
1263 return GetCacheQueue(aKey.IsChrome());
1266 void imgLoader::GlobalInit() {
1267 sCacheTimeWeight = StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
1268 int32_t cachesize = StaticPrefs::image_cache_size_AtStartup();
1269 sCacheMaxSize = cachesize > 0 ? cachesize : 0;
1271 sMemReporter = new imgMemoryReporter();
1272 RegisterStrongAsyncMemoryReporter(sMemReporter);
1273 RegisterImagesContentUsedUncompressedDistinguishedAmount(
1274 imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount);
1277 void imgLoader::ShutdownMemoryReporter() {
1278 UnregisterImagesContentUsedUncompressedDistinguishedAmount();
1279 UnregisterStrongMemoryReporter(sMemReporter);
1282 nsresult imgLoader::InitCache() {
1283 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
1284 if (!os) {
1285 return NS_ERROR_FAILURE;
1288 os->AddObserver(this, "memory-pressure", false);
1289 os->AddObserver(this, "chrome-flush-caches", false);
1290 os->AddObserver(this, "last-pb-context-exited", false);
1291 os->AddObserver(this, "profile-before-change", false);
1292 os->AddObserver(this, "xpcom-shutdown", false);
1294 mCacheTracker = MakeUnique<imgCacheExpirationTracker>();
1296 return NS_OK;
1299 nsresult imgLoader::Init() {
1300 InitCache();
1302 ReadAcceptHeaderPref();
1304 Preferences::AddWeakObserver(this, "image.http.accept");
1306 return NS_OK;
1309 NS_IMETHODIMP
1310 imgLoader::RespectPrivacyNotifications() {
1311 mRespectPrivacy = true;
1312 return NS_OK;
1315 NS_IMETHODIMP
1316 imgLoader::Observe(nsISupports* aSubject, const char* aTopic,
1317 const char16_t* aData) {
1318 // We listen for pref change notifications...
1319 if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
1320 if (!NS_strcmp(aData, u"image.http.accept")) {
1321 ReadAcceptHeaderPref();
1324 } else if (strcmp(aTopic, "memory-pressure") == 0) {
1325 MinimizeCaches();
1326 } else if (strcmp(aTopic, "chrome-flush-caches") == 0) {
1327 MinimizeCaches();
1328 ClearChromeImageCache();
1329 } else if (strcmp(aTopic, "last-pb-context-exited") == 0) {
1330 if (mRespectPrivacy) {
1331 ClearImageCache();
1332 ClearChromeImageCache();
1334 } else if (strcmp(aTopic, "profile-before-change") == 0) {
1335 mCacheTracker = nullptr;
1336 } else if (strcmp(aTopic, "xpcom-shutdown") == 0) {
1337 mCacheTracker = nullptr;
1338 ShutdownMemoryReporter();
1340 } else {
1341 // (Nothing else should bring us here)
1342 MOZ_ASSERT(0, "Invalid topic received");
1345 return NS_OK;
1348 void imgLoader::ReadAcceptHeaderPref() {
1349 nsAutoCString accept;
1350 nsresult rv = Preferences::GetCString("image.http.accept", accept);
1351 if (NS_SUCCEEDED(rv)) {
1352 mAcceptHeader = accept;
1353 } else {
1354 mAcceptHeader =
1355 IMAGE_PNG "," IMAGE_WILDCARD ";q=0.8," ANY_WILDCARD ";q=0.5";
1359 NS_IMETHODIMP
1360 imgLoader::ClearCache(bool chrome) {
1361 if (XRE_IsParentProcess()) {
1362 bool privateLoader = this == gPrivateBrowsingLoader;
1363 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1364 Unused << cp->SendClearImageCache(privateLoader, chrome);
1368 if (chrome) {
1369 return ClearChromeImageCache();
1371 return ClearImageCache();
1374 NS_IMETHODIMP
1375 imgLoader::RemoveEntriesFromPrincipal(nsIPrincipal* aPrincipal) {
1376 nsAutoString origin;
1377 nsresult rv = nsContentUtils::GetUTFOrigin(aPrincipal, origin);
1378 if (NS_WARN_IF(NS_FAILED(rv))) {
1379 return rv;
1382 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1384 imgCacheTable& cache = GetCache(aPrincipal->IsSystemPrincipal());
1385 for (auto iter = cache.Iter(); !iter.Done(); iter.Next()) {
1386 auto& key = iter.Key();
1388 if (key.OriginAttributesRef() !=
1389 BasePrincipal::Cast(aPrincipal)->OriginAttributesRef()) {
1390 continue;
1393 nsAutoString imageOrigin;
1394 nsresult rv = nsContentUtils::GetUTFOrigin(key.URI(), imageOrigin);
1395 if (NS_WARN_IF(NS_FAILED(rv))) {
1396 continue;
1399 if (imageOrigin == origin) {
1400 entriesToBeRemoved.AppendElement(iter.Data());
1404 for (auto& entry : entriesToBeRemoved) {
1405 if (!RemoveFromCache(entry)) {
1406 NS_WARNING(
1407 "Couldn't remove an entry from the cache in "
1408 "RemoveEntriesFromPrincipal()\n");
1412 return NS_OK;
1415 NS_IMETHODIMP
1416 imgLoader::RemoveEntry(nsIURI* aURI, Document* aDoc) {
1417 if (aURI) {
1418 OriginAttributes attrs;
1419 if (aDoc) {
1420 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1421 if (principal) {
1422 attrs = principal->OriginAttributesRef();
1426 ImageCacheKey key(aURI, attrs, aDoc);
1427 if (RemoveFromCache(key)) {
1428 return NS_OK;
1431 return NS_ERROR_NOT_AVAILABLE;
1434 NS_IMETHODIMP
1435 imgLoader::FindEntryProperties(nsIURI* uri, Document* aDoc,
1436 nsIProperties** _retval) {
1437 *_retval = nullptr;
1439 OriginAttributes attrs;
1440 if (aDoc) {
1441 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1442 if (principal) {
1443 attrs = principal->OriginAttributesRef();
1447 ImageCacheKey key(uri, attrs, aDoc);
1448 imgCacheTable& cache = GetCache(key);
1450 RefPtr<imgCacheEntry> entry;
1451 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
1452 if (mCacheTracker && entry->HasNoProxies()) {
1453 mCacheTracker->MarkUsed(entry);
1456 RefPtr<imgRequest> request = entry->GetRequest();
1457 if (request) {
1458 nsCOMPtr<nsIProperties> properties = request->Properties();
1459 properties.forget(_retval);
1463 return NS_OK;
1466 NS_IMETHODIMP_(void)
1467 imgLoader::ClearCacheForControlledDocument(Document* aDoc) {
1468 MOZ_ASSERT(aDoc);
1469 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1470 imgCacheTable& cache = GetCache(false);
1471 for (auto iter = cache.Iter(); !iter.Done(); iter.Next()) {
1472 auto& key = iter.Key();
1473 if (key.ControlledDocument() == aDoc) {
1474 entriesToBeRemoved.AppendElement(iter.Data());
1477 for (auto& entry : entriesToBeRemoved) {
1478 if (!RemoveFromCache(entry)) {
1479 NS_WARNING(
1480 "Couldn't remove an entry from the cache in "
1481 "ClearCacheForControlledDocument()\n");
1486 void imgLoader::Shutdown() {
1487 NS_IF_RELEASE(gNormalLoader);
1488 gNormalLoader = nullptr;
1489 NS_IF_RELEASE(gPrivateBrowsingLoader);
1490 gPrivateBrowsingLoader = nullptr;
1493 nsresult imgLoader::ClearChromeImageCache() {
1494 return EvictEntries(mChromeCache);
1497 nsresult imgLoader::ClearImageCache() { return EvictEntries(mCache); }
1499 void imgLoader::MinimizeCaches() {
1500 EvictEntries(mCacheQueue);
1501 EvictEntries(mChromeCacheQueue);
1504 bool imgLoader::PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* entry) {
1505 imgCacheTable& cache = GetCache(aKey);
1507 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::PutIntoCache", "uri",
1508 aKey.URI());
1510 // Check to see if this request already exists in the cache. If so, we'll
1511 // replace the old version.
1512 RefPtr<imgCacheEntry> tmpCacheEntry;
1513 if (cache.Get(aKey, getter_AddRefs(tmpCacheEntry)) && tmpCacheEntry) {
1514 MOZ_LOG(
1515 gImgLog, LogLevel::Debug,
1516 ("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
1517 nullptr));
1518 RefPtr<imgRequest> tmpRequest = tmpCacheEntry->GetRequest();
1520 // If it already exists, and we're putting the same key into the cache, we
1521 // should remove the old version.
1522 MOZ_LOG(gImgLog, LogLevel::Debug,
1523 ("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
1524 nullptr));
1526 RemoveFromCache(aKey);
1527 } else {
1528 MOZ_LOG(gImgLog, LogLevel::Debug,
1529 ("[this=%p] imgLoader::PutIntoCache --"
1530 " Element NOT already in the cache",
1531 nullptr));
1534 cache.Put(aKey, entry);
1536 // We can be called to resurrect an evicted entry.
1537 if (entry->Evicted()) {
1538 entry->SetEvicted(false);
1541 // If we're resurrecting an entry with no proxies, put it back in the
1542 // tracker and queue.
1543 if (entry->HasNoProxies()) {
1544 nsresult addrv = NS_OK;
1546 if (mCacheTracker) {
1547 addrv = mCacheTracker->AddObject(entry);
1550 if (NS_SUCCEEDED(addrv)) {
1551 imgCacheQueue& queue = GetCacheQueue(aKey);
1552 queue.Push(entry);
1556 RefPtr<imgRequest> request = entry->GetRequest();
1557 request->SetIsInCache(true);
1558 RemoveFromUncachedImages(request);
1560 return true;
1563 bool imgLoader::SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry) {
1564 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasNoProxies", "uri",
1565 aRequest->CacheKey().URI());
1567 aEntry->SetHasNoProxies(true);
1569 if (aEntry->Evicted()) {
1570 return false;
1573 imgCacheQueue& queue = GetCacheQueue(aRequest->IsChrome());
1575 nsresult addrv = NS_OK;
1577 if (mCacheTracker) {
1578 addrv = mCacheTracker->AddObject(aEntry);
1581 if (NS_SUCCEEDED(addrv)) {
1582 queue.Push(aEntry);
1585 imgCacheTable& cache = GetCache(aRequest->IsChrome());
1586 CheckCacheLimits(cache, queue);
1588 return true;
1591 bool imgLoader::SetHasProxies(imgRequest* aRequest) {
1592 VerifyCacheSizes();
1594 const ImageCacheKey& key = aRequest->CacheKey();
1595 imgCacheTable& cache = GetCache(key);
1597 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasProxies", "uri",
1598 key.URI());
1600 RefPtr<imgCacheEntry> entry;
1601 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
1602 // Make sure the cache entry is for the right request
1603 RefPtr<imgRequest> entryRequest = entry->GetRequest();
1604 if (entryRequest == aRequest && entry->HasNoProxies()) {
1605 imgCacheQueue& queue = GetCacheQueue(key);
1606 queue.Remove(entry);
1608 if (mCacheTracker) {
1609 mCacheTracker->RemoveObject(entry);
1612 entry->SetHasNoProxies(false);
1614 return true;
1618 return false;
1621 void imgLoader::CacheEntriesChanged(bool aForChrome,
1622 int32_t aSizeDiff /* = 0 */) {
1623 imgCacheQueue& queue = GetCacheQueue(aForChrome);
1624 // We only need to dirty the queue if there is any sorting
1625 // taking place. Empty or single-entry lists can't become
1626 // dirty.
1627 if (queue.GetNumElements() > 1) {
1628 queue.MarkDirty();
1630 queue.UpdateSize(aSizeDiff);
1633 void imgLoader::CheckCacheLimits(imgCacheTable& cache, imgCacheQueue& queue) {
1634 if (queue.GetNumElements() == 0) {
1635 NS_ASSERTION(queue.GetSize() == 0,
1636 "imgLoader::CheckCacheLimits -- incorrect cache size");
1639 // Remove entries from the cache until we're back at our desired max size.
1640 while (queue.GetSize() > sCacheMaxSize) {
1641 // Remove the first entry in the queue.
1642 RefPtr<imgCacheEntry> entry(queue.Pop());
1644 NS_ASSERTION(entry, "imgLoader::CheckCacheLimits -- NULL entry pointer");
1646 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1647 RefPtr<imgRequest> req = entry->GetRequest();
1648 if (req) {
1649 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::CheckCacheLimits",
1650 "entry", req->CacheKey().URI());
1654 if (entry) {
1655 // We just popped this entry from the queue, so pass AlreadyRemoved
1656 // to avoid searching the queue again in RemoveFromCache.
1657 RemoveFromCache(entry, QueueState::AlreadyRemoved);
1662 bool imgLoader::ValidateRequestWithNewChannel(
1663 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1664 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1665 imgINotificationObserver* aObserver, nsISupports* aCX,
1666 Document* aLoadingDocument, uint64_t aInnerWindowId, nsLoadFlags aLoadFlags,
1667 nsContentPolicyType aLoadPolicyType, imgRequestProxy** aProxyRequest,
1668 nsIPrincipal* aTriggeringPrincipal, int32_t aCORSMode,
1669 bool* aNewChannelCreated) {
1670 // now we need to insert a new channel request object in between the real
1671 // request and the proxy that basically delays loading the image until it
1672 // gets a 304 or figures out that this needs to be a new request
1674 nsresult rv;
1676 // If we're currently in the middle of validating this request, just hand
1677 // back a proxy to it; the required work will be done for us.
1678 if (request->GetValidator()) {
1679 rv = CreateNewProxyForRequest(request, aLoadGroup, aLoadingDocument,
1680 aObserver, aLoadFlags, aProxyRequest);
1681 if (NS_FAILED(rv)) {
1682 return false;
1685 if (*aProxyRequest) {
1686 imgRequestProxy* proxy = static_cast<imgRequestProxy*>(*aProxyRequest);
1688 // We will send notifications from imgCacheValidator::OnStartRequest().
1689 // In the mean time, we must defer notifications because we are added to
1690 // the imgRequest's proxy list, and we can get extra notifications
1691 // resulting from methods such as StartDecoding(). See bug 579122.
1692 proxy->MarkValidating();
1694 // Attach the proxy without notifying
1695 request->GetValidator()->AddProxy(proxy);
1698 return NS_SUCCEEDED(rv);
1700 // We will rely on Necko to cache this request when it's possible, and to
1701 // tell imgCacheValidator::OnStartRequest whether the request came from its
1702 // cache.
1703 nsCOMPtr<nsIChannel> newChannel;
1704 bool forcePrincipalCheck;
1705 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
1706 aInitialDocumentURI, aCORSMode, aReferrerInfo,
1707 aLoadGroup, mAcceptHeader, aLoadFlags, aLoadPolicyType,
1708 aTriggeringPrincipal, aCX, mRespectPrivacy);
1709 if (NS_FAILED(rv)) {
1710 return false;
1713 if (aNewChannelCreated) {
1714 *aNewChannelCreated = true;
1717 RefPtr<imgRequestProxy> req;
1718 rv = CreateNewProxyForRequest(request, aLoadGroup, aLoadingDocument,
1719 aObserver, aLoadFlags, getter_AddRefs(req));
1720 if (NS_FAILED(rv)) {
1721 return false;
1724 // Make sure that OnStatus/OnProgress calls have the right request set...
1725 RefPtr<nsProgressNotificationProxy> progressproxy =
1726 new nsProgressNotificationProxy(newChannel, req);
1727 if (!progressproxy) {
1728 return false;
1731 RefPtr<imgCacheValidator> hvc = new imgCacheValidator(
1732 progressproxy, this, request, aCX, aInnerWindowId, forcePrincipalCheck);
1734 // Casting needed here to get past multiple inheritance.
1735 nsCOMPtr<nsIStreamListener> listener =
1736 do_QueryInterface(static_cast<nsIThreadRetargetableStreamListener*>(hvc));
1737 NS_ENSURE_TRUE(listener, false);
1739 // We must set the notification callbacks before setting up the
1740 // CORS listener, because that's also interested inthe
1741 // notification callbacks.
1742 newChannel->SetNotificationCallbacks(hvc);
1744 request->SetValidator(hvc);
1746 // We will send notifications from imgCacheValidator::OnStartRequest().
1747 // In the mean time, we must defer notifications because we are added to
1748 // the imgRequest's proxy list, and we can get extra notifications
1749 // resulting from methods such as StartDecoding(). See bug 579122.
1750 req->MarkValidating();
1752 // Add the proxy without notifying
1753 hvc->AddProxy(req);
1755 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
1756 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
1757 aLoadGroup);
1758 rv = newChannel->AsyncOpen(listener);
1759 if (NS_WARN_IF(NS_FAILED(rv))) {
1760 req->CancelAndForgetObserver(rv);
1761 return false;
1764 req.forget(aProxyRequest);
1765 return true;
1768 bool imgLoader::ValidateEntry(
1769 imgCacheEntry* aEntry, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1770 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1771 imgINotificationObserver* aObserver, nsISupports* aCX,
1772 Document* aLoadingDocument, nsLoadFlags aLoadFlags,
1773 nsContentPolicyType aLoadPolicyType, bool aCanMakeNewChannel,
1774 bool* aNewChannelCreated, imgRequestProxy** aProxyRequest,
1775 nsIPrincipal* aTriggeringPrincipal, int32_t aCORSMode) {
1776 LOG_SCOPE(gImgLog, "imgLoader::ValidateEntry");
1778 // If the expiration time is zero, then the request has not gotten far enough
1779 // to know when it will expire.
1780 uint32_t expiryTime = aEntry->GetExpiryTime();
1781 bool hasExpired = expiryTime != 0 &&
1782 expiryTime <= imgCacheEntry::SecondsFromPRTime(PR_Now());
1784 nsresult rv;
1786 // Special treatment for file URLs - aEntry has expired if file has changed
1787 nsCOMPtr<nsIFileURL> fileUrl(do_QueryInterface(aURI));
1788 if (fileUrl) {
1789 uint32_t lastModTime = aEntry->GetLoadTime();
1791 nsCOMPtr<nsIFile> theFile;
1792 rv = fileUrl->GetFile(getter_AddRefs(theFile));
1793 if (NS_SUCCEEDED(rv)) {
1794 PRTime fileLastMod;
1795 rv = theFile->GetLastModifiedTime(&fileLastMod);
1796 if (NS_SUCCEEDED(rv)) {
1797 // nsIFile uses millisec, NSPR usec
1798 fileLastMod *= 1000;
1799 hasExpired =
1800 imgCacheEntry::SecondsFromPRTime((PRTime)fileLastMod) > lastModTime;
1805 RefPtr<imgRequest> request(aEntry->GetRequest());
1807 if (!request) {
1808 return false;
1811 if (!ValidateSecurityInfo(request, aEntry->ForcePrincipalCheck(), aCORSMode,
1812 aTriggeringPrincipal, aCX, aLoadPolicyType,
1813 aReferrerInfo))
1814 return false;
1816 // data URIs are immutable and by their nature can't leak data, so we can
1817 // just return true in that case. Doing so would mean that shift-reload
1818 // doesn't reload data URI documents/images though (which is handy for
1819 // debugging during gecko development) so we make an exception in that case.
1820 nsAutoCString scheme;
1821 aURI->GetScheme(scheme);
1822 if (scheme.EqualsLiteral("data") &&
1823 !(aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE)) {
1824 return true;
1827 bool validateRequest = false;
1829 // If the request's loadId is the same as the aCX, then it is ok to use
1830 // this one because it has already been validated for this context.
1832 // XXX: nullptr seems to be a 'special' key value that indicates that NO
1833 // validation is required.
1834 // XXX: we also check the window ID because the loadID() can return a reused
1835 // pointer of a document. This can still happen for non-document image
1836 // cache entries.
1837 void* key = (void*)aCX;
1838 nsCOMPtr<Document> doc = do_QueryInterface(aCX);
1839 uint64_t innerWindowID = doc ? doc->InnerWindowID() : 0;
1840 if (request->LoadId() != key || request->InnerWindowID() != innerWindowID) {
1841 // If we would need to revalidate this entry, but we're being told to
1842 // bypass the cache, we don't allow this entry to be used.
1843 if (aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE) {
1844 return false;
1847 if (MOZ_UNLIKELY(ChaosMode::isActive(ChaosFeature::ImageCache))) {
1848 if (ChaosMode::randomUint32LessThan(4) < 1) {
1849 return false;
1853 // Determine whether the cache aEntry must be revalidated...
1854 validateRequest = ShouldRevalidateEntry(aEntry, aLoadFlags, hasExpired);
1856 MOZ_LOG(gImgLog, LogLevel::Debug,
1857 ("imgLoader::ValidateEntry validating cache entry. "
1858 "validateRequest = %d",
1859 validateRequest));
1860 } else if (!key && MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1861 MOZ_LOG(gImgLog, LogLevel::Debug,
1862 ("imgLoader::ValidateEntry BYPASSING cache validation for %s "
1863 "because of NULL LoadID",
1864 aURI->GetSpecOrDefault().get()));
1867 // We can't use a cached request if it comes from a different
1868 // application cache than this load is expecting.
1869 nsCOMPtr<nsIApplicationCacheContainer> appCacheContainer;
1870 nsCOMPtr<nsIApplicationCache> requestAppCache;
1871 nsCOMPtr<nsIApplicationCache> groupAppCache;
1872 if ((appCacheContainer = do_GetInterface(request->GetRequest()))) {
1873 appCacheContainer->GetApplicationCache(getter_AddRefs(requestAppCache));
1875 if ((appCacheContainer = do_QueryInterface(aLoadGroup))) {
1876 appCacheContainer->GetApplicationCache(getter_AddRefs(groupAppCache));
1879 if (requestAppCache != groupAppCache) {
1880 MOZ_LOG(gImgLog, LogLevel::Debug,
1881 ("imgLoader::ValidateEntry - Unable to use cached imgRequest "
1882 "[request=%p] because of mismatched application caches\n",
1883 address_of(request)));
1884 return false;
1887 if (validateRequest && aCanMakeNewChannel) {
1888 LOG_SCOPE(gImgLog, "imgLoader::ValidateRequest |cache hit| must validate");
1890 return ValidateRequestWithNewChannel(
1891 request, aURI, aInitialDocumentURI, aReferrerInfo, aLoadGroup,
1892 aObserver, aCX, aLoadingDocument, innerWindowID, aLoadFlags,
1893 aLoadPolicyType, aProxyRequest, aTriggeringPrincipal, aCORSMode,
1894 aNewChannelCreated);
1897 return !validateRequest;
1900 bool imgLoader::RemoveFromCache(const ImageCacheKey& aKey) {
1901 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache", "uri",
1902 aKey.URI());
1904 imgCacheTable& cache = GetCache(aKey);
1905 imgCacheQueue& queue = GetCacheQueue(aKey);
1907 RefPtr<imgCacheEntry> entry;
1908 cache.Remove(aKey, getter_AddRefs(entry));
1909 if (entry) {
1910 MOZ_ASSERT(!entry->Evicted(), "Evicting an already-evicted cache entry!");
1912 // Entries with no proxies are in the tracker.
1913 if (entry->HasNoProxies()) {
1914 if (mCacheTracker) {
1915 mCacheTracker->RemoveObject(entry);
1917 queue.Remove(entry);
1920 entry->SetEvicted(true);
1922 RefPtr<imgRequest> request = entry->GetRequest();
1923 request->SetIsInCache(false);
1924 AddToUncachedImages(request);
1926 return true;
1928 return false;
1931 bool imgLoader::RemoveFromCache(imgCacheEntry* entry, QueueState aQueueState) {
1932 LOG_STATIC_FUNC(gImgLog, "imgLoader::RemoveFromCache entry");
1934 RefPtr<imgRequest> request = entry->GetRequest();
1935 if (request) {
1936 const ImageCacheKey& key = request->CacheKey();
1937 imgCacheTable& cache = GetCache(key);
1938 imgCacheQueue& queue = GetCacheQueue(key);
1940 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache",
1941 "entry's uri", key.URI());
1943 cache.Remove(key);
1945 if (entry->HasNoProxies()) {
1946 LOG_STATIC_FUNC(gImgLog,
1947 "imgLoader::RemoveFromCache removing from tracker");
1948 if (mCacheTracker) {
1949 mCacheTracker->RemoveObject(entry);
1951 // Only search the queue to remove the entry if its possible it might
1952 // be in the queue. If we know its not in the queue this would be
1953 // wasted work.
1954 MOZ_ASSERT_IF(aQueueState == QueueState::AlreadyRemoved,
1955 !queue.Contains(entry));
1956 if (aQueueState == QueueState::MaybeExists) {
1957 queue.Remove(entry);
1961 entry->SetEvicted(true);
1962 request->SetIsInCache(false);
1963 AddToUncachedImages(request);
1965 return true;
1968 return false;
1971 nsresult imgLoader::EvictEntries(imgCacheTable& aCacheToClear) {
1972 LOG_STATIC_FUNC(gImgLog, "imgLoader::EvictEntries table");
1974 // We have to make a temporary, since RemoveFromCache removes the element
1975 // from the queue, invalidating iterators.
1976 nsTArray<RefPtr<imgCacheEntry> > entries;
1977 for (auto iter = aCacheToClear.Iter(); !iter.Done(); iter.Next()) {
1978 RefPtr<imgCacheEntry>& data = iter.Data();
1979 entries.AppendElement(data);
1982 for (uint32_t i = 0; i < entries.Length(); ++i) {
1983 if (!RemoveFromCache(entries[i])) {
1984 return NS_ERROR_FAILURE;
1988 MOZ_ASSERT(aCacheToClear.Count() == 0);
1990 return NS_OK;
1993 nsresult imgLoader::EvictEntries(imgCacheQueue& aQueueToClear) {
1994 LOG_STATIC_FUNC(gImgLog, "imgLoader::EvictEntries queue");
1996 // We have to make a temporary, since RemoveFromCache removes the element
1997 // from the queue, invalidating iterators.
1998 nsTArray<RefPtr<imgCacheEntry> > entries(aQueueToClear.GetNumElements());
1999 for (auto i = aQueueToClear.begin(); i != aQueueToClear.end(); ++i) {
2000 entries.AppendElement(*i);
2003 // Iterate in reverse order to minimize array copying.
2004 for (auto& entry : entries) {
2005 if (!RemoveFromCache(entry)) {
2006 return NS_ERROR_FAILURE;
2010 MOZ_ASSERT(aQueueToClear.GetNumElements() == 0);
2012 return NS_OK;
2015 void imgLoader::AddToUncachedImages(imgRequest* aRequest) {
2016 MutexAutoLock lock(mUncachedImagesMutex);
2017 mUncachedImages.PutEntry(aRequest);
2020 void imgLoader::RemoveFromUncachedImages(imgRequest* aRequest) {
2021 MutexAutoLock lock(mUncachedImagesMutex);
2022 mUncachedImages.RemoveEntry(aRequest);
2025 bool imgLoader::PreferLoadFromCache(nsIURI* aURI) const {
2026 // If we are trying to load an image from a protocol that doesn't support
2027 // caching (e.g. thumbnails via the moz-page-thumb:// protocol, or icons via
2028 // the moz-extension:// protocol), load it directly from the cache to prevent
2029 // re-decoding the image. See Bug 1373258.
2030 // TODO: Bug 1406134
2031 return aURI->SchemeIs("moz-page-thumb") || aURI->SchemeIs("moz-extension");
2034 #define LOAD_FLAGS_CACHE_MASK \
2035 (nsIRequest::LOAD_BYPASS_CACHE | nsIRequest::LOAD_FROM_CACHE)
2037 #define LOAD_FLAGS_VALIDATE_MASK \
2038 (nsIRequest::VALIDATE_ALWAYS | nsIRequest::VALIDATE_NEVER | \
2039 nsIRequest::VALIDATE_ONCE_PER_SESSION)
2041 NS_IMETHODIMP
2042 imgLoader::LoadImageXPCOM(nsIURI* aURI, nsIURI* aInitialDocumentURI,
2043 nsIReferrerInfo* aReferrerInfo,
2044 nsIPrincipal* aTriggeringPrincipal,
2045 nsILoadGroup* aLoadGroup,
2046 imgINotificationObserver* aObserver, nsISupports* aCX,
2047 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
2048 nsContentPolicyType aContentPolicyType,
2049 imgIRequest** _retval) {
2050 // Optional parameter, so defaults to 0 (== TYPE_INVALID)
2051 if (!aContentPolicyType) {
2052 aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE;
2054 imgRequestProxy* proxy;
2055 nsCOMPtr<nsINode> node = do_QueryInterface(aCX);
2056 nsCOMPtr<Document> doc = do_QueryInterface(aCX);
2057 nsresult rv =
2058 LoadImage(aURI, aInitialDocumentURI, aReferrerInfo, aTriggeringPrincipal,
2059 0, aLoadGroup, aObserver, node, doc, aLoadFlags, aCacheKey,
2060 aContentPolicyType, EmptyString(),
2061 /* aUseUrgentStartForChannel */ false, &proxy);
2062 *_retval = proxy;
2063 return rv;
2066 nsresult imgLoader::LoadImage(
2067 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2068 nsIPrincipal* aTriggeringPrincipal, uint64_t aRequestContextID,
2069 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
2070 nsINode* aContext, Document* aLoadingDocument, nsLoadFlags aLoadFlags,
2071 nsISupports* aCacheKey, nsContentPolicyType aContentPolicyType,
2072 const nsAString& initiatorType, bool aUseUrgentStartForChannel,
2073 imgRequestProxy** _retval) {
2074 VerifyCacheSizes();
2076 NS_ASSERTION(aURI, "imgLoader::LoadImage -- NULL URI pointer");
2078 if (!aURI) {
2079 return NS_ERROR_NULL_POINTER;
2082 #ifdef MOZ_GECKO_PROFILER
2083 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK,
2084 aURI->GetSpecOrDefault());
2085 #endif
2087 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::LoadImage", "aURI", aURI);
2089 *_retval = nullptr;
2091 RefPtr<imgRequest> request;
2093 nsresult rv;
2094 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2096 #ifdef DEBUG
2097 bool isPrivate = false;
2099 if (aLoadingDocument) {
2100 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadingDocument);
2101 } else if (aLoadGroup) {
2102 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadGroup);
2104 MOZ_ASSERT(isPrivate == mRespectPrivacy);
2106 if (aLoadingDocument) {
2107 // The given load group should match that of the document if given. If
2108 // that isn't the case, then we need to add more plumbing to ensure we
2109 // block the document as well.
2110 nsCOMPtr<nsILoadGroup> docLoadGroup =
2111 aLoadingDocument->GetDocumentLoadGroup();
2112 MOZ_ASSERT(docLoadGroup == aLoadGroup);
2114 #endif
2116 // Get the default load flags from the loadgroup (if possible)...
2117 if (aLoadGroup) {
2118 aLoadGroup->GetLoadFlags(&requestFlags);
2119 if (PreferLoadFromCache(aURI)) {
2120 requestFlags |= nsIRequest::LOAD_FROM_CACHE;
2124 // Merge the default load flags with those passed in via aLoadFlags.
2125 // Currently, *only* the caching, validation and background load flags
2126 // are merged...
2128 // The flags in aLoadFlags take precedence over the default flags!
2130 if (aLoadFlags & LOAD_FLAGS_CACHE_MASK) {
2131 // Override the default caching flags...
2132 requestFlags = (requestFlags & ~LOAD_FLAGS_CACHE_MASK) |
2133 (aLoadFlags & LOAD_FLAGS_CACHE_MASK);
2135 if (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK) {
2136 // Override the default validation flags...
2137 requestFlags = (requestFlags & ~LOAD_FLAGS_VALIDATE_MASK) |
2138 (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK);
2140 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
2141 // Propagate background loading...
2142 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2145 int32_t corsmode = imgIRequest::CORS_NONE;
2146 if (aLoadFlags & imgILoader::LOAD_CORS_ANONYMOUS) {
2147 corsmode = imgIRequest::CORS_ANONYMOUS;
2148 } else if (aLoadFlags & imgILoader::LOAD_CORS_USE_CREDENTIALS) {
2149 corsmode = imgIRequest::CORS_USE_CREDENTIALS;
2152 RefPtr<imgCacheEntry> entry;
2154 // Look in the cache for our URI, and then validate it.
2155 // XXX For now ignore aCacheKey. We will need it in the future
2156 // for correctly dealing with image load requests that are a result
2157 // of post data.
2158 OriginAttributes attrs;
2159 if (aTriggeringPrincipal) {
2160 attrs = aTriggeringPrincipal->OriginAttributesRef();
2162 ImageCacheKey key(aURI, attrs, aLoadingDocument);
2163 imgCacheTable& cache = GetCache(key);
2165 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
2166 bool newChannelCreated = false;
2167 if (ValidateEntry(entry, aURI, aInitialDocumentURI, aReferrerInfo,
2168 aLoadGroup, aObserver, ToSupports(aLoadingDocument),
2169 aLoadingDocument, requestFlags, aContentPolicyType, true,
2170 &newChannelCreated, _retval, aTriggeringPrincipal,
2171 corsmode)) {
2172 request = entry->GetRequest();
2174 // If this entry has no proxies, its request has no reference to the
2175 // entry.
2176 if (entry->HasNoProxies()) {
2177 LOG_FUNC_WITH_PARAM(gImgLog,
2178 "imgLoader::LoadImage() adding proxyless entry",
2179 "uri", key.URI());
2180 MOZ_ASSERT(!request->HasCacheEntry(),
2181 "Proxyless entry's request has cache entry!");
2182 request->SetCacheEntry(entry);
2184 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2185 mCacheTracker->MarkUsed(entry);
2189 entry->Touch();
2191 if (!newChannelCreated) {
2192 // This is ugly but it's needed to report CSP violations. We have 3
2193 // scenarios:
2194 // - we don't have cache. We are not in this if() stmt. A new channel is
2195 // created and that triggers the CSP checks.
2196 // - We have a cache entry and this is blocked by CSP directives.
2197 DebugOnly<bool> shouldLoad =
2198 ShouldLoadCachedImage(request, ToSupports(aLoadingDocument),
2199 aTriggeringPrincipal, aContentPolicyType,
2200 /* aSendCSPViolationReports */ true);
2201 MOZ_ASSERT(shouldLoad);
2203 } else {
2204 // We can't use this entry. We'll try to load it off the network, and if
2205 // successful, overwrite the old entry in the cache with a new one.
2206 entry = nullptr;
2210 // Keep the channel in this scope, so we can adjust its notificationCallbacks
2211 // later when we create the proxy.
2212 nsCOMPtr<nsIChannel> newChannel;
2213 // If we didn't get a cache hit, we need to load from the network.
2214 if (!request) {
2215 LOG_SCOPE(gImgLog, "imgLoader::LoadImage |cache miss|");
2217 bool forcePrincipalCheck;
2218 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
2219 aInitialDocumentURI, corsmode, aReferrerInfo,
2220 aLoadGroup, mAcceptHeader, requestFlags,
2221 aContentPolicyType, aTriggeringPrincipal, aContext,
2222 mRespectPrivacy);
2223 if (NS_FAILED(rv)) {
2224 return NS_ERROR_FAILURE;
2227 MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel) == mRespectPrivacy);
2229 NewRequestAndEntry(forcePrincipalCheck, this, key, getter_AddRefs(request),
2230 getter_AddRefs(entry));
2232 MOZ_LOG(gImgLog, LogLevel::Debug,
2233 ("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
2234 " [request=%p]\n",
2235 this, request.get()));
2237 nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(newChannel));
2238 if (cos) {
2239 if (aUseUrgentStartForChannel) {
2240 cos->AddClassFlags(nsIClassOfService::UrgentStart);
2243 if (StaticPrefs::network_http_tailing_enabled() &&
2244 aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
2245 cos->AddClassFlags(nsIClassOfService::Throttleable |
2246 nsIClassOfService::Tail);
2247 nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(newChannel));
2248 if (httpChannel) {
2249 Unused << httpChannel->SetRequestContextID(aRequestContextID);
2254 nsCOMPtr<nsILoadGroup> channelLoadGroup;
2255 newChannel->GetLoadGroup(getter_AddRefs(channelLoadGroup));
2256 rv = request->Init(aURI, aURI, /* aHadInsecureRedirect = */ false,
2257 channelLoadGroup, newChannel, entry,
2258 ToSupports(aLoadingDocument), aTriggeringPrincipal,
2259 corsmode, aReferrerInfo);
2260 if (NS_FAILED(rv)) {
2261 return NS_ERROR_FAILURE;
2264 // Add the initiator type for this image load
2265 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(newChannel);
2266 if (timedChannel) {
2267 timedChannel->SetInitiatorType(initiatorType);
2270 // create the proxy listener
2271 nsCOMPtr<nsIStreamListener> listener = new ProxyListener(request.get());
2273 MOZ_LOG(gImgLog, LogLevel::Debug,
2274 ("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
2275 this));
2277 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
2278 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
2279 aLoadGroup);
2281 nsresult openRes = newChannel->AsyncOpen(listener);
2283 if (NS_FAILED(openRes)) {
2284 MOZ_LOG(
2285 gImgLog, LogLevel::Debug,
2286 ("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
2287 "\n",
2288 this, static_cast<uint32_t>(openRes)));
2289 request->CancelAndAbort(openRes);
2290 return openRes;
2293 // Try to add the new request into the cache.
2294 PutIntoCache(key, entry);
2295 } else {
2296 LOG_MSG_WITH_PARAM(gImgLog, "imgLoader::LoadImage |cache hit|", "request",
2297 request);
2300 // If we didn't get a proxy when validating the cache entry, we need to
2301 // create one.
2302 if (!*_retval) {
2303 // ValidateEntry() has three return values: "Is valid," "might be valid --
2304 // validating over network", and "not valid." If we don't have a _retval,
2305 // we know ValidateEntry is not validating over the network, so it's safe
2306 // to SetLoadId here because we know this request is valid for this context.
2308 // Note, however, that this doesn't guarantee the behaviour we want (one
2309 // URL maps to the same image on a page) if we load the same image in a
2310 // different tab (see bug 528003), because its load id will get re-set, and
2311 // that'll cause us to validate over the network.
2312 request->SetLoadId(aLoadingDocument);
2314 LOG_MSG(gImgLog, "imgLoader::LoadImage", "creating proxy request.");
2315 rv = CreateNewProxyForRequest(request, aLoadGroup, aLoadingDocument,
2316 aObserver, requestFlags, _retval);
2317 if (NS_FAILED(rv)) {
2318 return rv;
2321 imgRequestProxy* proxy = *_retval;
2323 // Make sure that OnStatus/OnProgress calls have the right request set, if
2324 // we did create a channel here.
2325 if (newChannel) {
2326 nsCOMPtr<nsIInterfaceRequestor> requestor(
2327 new nsProgressNotificationProxy(newChannel, proxy));
2328 if (!requestor) {
2329 return NS_ERROR_OUT_OF_MEMORY;
2331 newChannel->SetNotificationCallbacks(requestor);
2334 // Note that it's OK to add here even if the request is done. If it is,
2335 // it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
2336 // the proxy will be removed from the loadgroup.
2337 proxy->AddToLoadGroup();
2339 // If we're loading off the network, explicitly don't notify our proxy,
2340 // because necko (or things called from necko, such as imgCacheValidator)
2341 // are going to call our notifications asynchronously, and we can't make it
2342 // further asynchronous because observers might rely on imagelib completing
2343 // its work between the channel's OnStartRequest and OnStopRequest.
2344 if (!newChannel) {
2345 proxy->NotifyListener();
2348 return rv;
2351 NS_ASSERTION(*_retval, "imgLoader::LoadImage -- no return value");
2353 return NS_OK;
2356 NS_IMETHODIMP
2357 imgLoader::LoadImageWithChannelXPCOM(nsIChannel* channel,
2358 imgINotificationObserver* aObserver,
2359 nsISupports* aCX,
2360 nsIStreamListener** listener,
2361 imgIRequest** _retval) {
2362 nsresult result;
2363 imgRequestProxy* proxy;
2364 result = LoadImageWithChannel(channel, aObserver, aCX, listener, &proxy);
2365 *_retval = proxy;
2366 return result;
2369 nsresult imgLoader::LoadImageWithChannel(nsIChannel* channel,
2370 imgINotificationObserver* aObserver,
2371 nsISupports* aCX,
2372 nsIStreamListener** listener,
2373 imgRequestProxy** _retval) {
2374 NS_ASSERTION(channel,
2375 "imgLoader::LoadImageWithChannel -- NULL channel pointer");
2377 MOZ_ASSERT(NS_UsePrivateBrowsing(channel) == mRespectPrivacy);
2379 LOG_SCOPE(gImgLog, "imgLoader::LoadImageWithChannel");
2380 RefPtr<imgRequest> request;
2382 nsCOMPtr<nsIURI> uri;
2383 channel->GetURI(getter_AddRefs(uri));
2384 nsCOMPtr<Document> doc = do_QueryInterface(aCX);
2386 NS_ENSURE_TRUE(channel, NS_ERROR_FAILURE);
2387 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2389 OriginAttributes attrs = loadInfo->GetOriginAttributes();
2391 ImageCacheKey key(uri, attrs, doc);
2393 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2394 channel->GetLoadFlags(&requestFlags);
2396 if (PreferLoadFromCache(uri)) {
2397 requestFlags |= nsIRequest::LOAD_FROM_CACHE;
2400 RefPtr<imgCacheEntry> entry;
2402 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2403 RemoveFromCache(key);
2404 } else {
2405 // Look in the cache for our URI, and then validate it.
2406 // XXX For now ignore aCacheKey. We will need it in the future
2407 // for correctly dealing with image load requests that are a result
2408 // of post data.
2409 imgCacheTable& cache = GetCache(key);
2410 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
2411 // We don't want to kick off another network load. So we ask
2412 // ValidateEntry to only do validation without creating a new proxy. If
2413 // it says that the entry isn't valid any more, we'll only use the entry
2414 // we're getting if the channel is loading from the cache anyways.
2416 // XXX -- should this be changed? it's pretty much verbatim from the old
2417 // code, but seems nonsensical.
2419 // Since aCanMakeNewChannel == false, we don't need to pass content policy
2420 // type/principal/etc
2422 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2423 // if there is a loadInfo, use the right contentType, otherwise
2424 // default to the internal image type
2425 nsContentPolicyType policyType = loadInfo->InternalContentPolicyType();
2427 if (ValidateEntry(entry, uri, nullptr, nullptr, nullptr, aObserver, aCX,
2428 doc, requestFlags, policyType, false, nullptr, nullptr,
2429 nullptr, imgIRequest::CORS_NONE)) {
2430 request = entry->GetRequest();
2431 } else {
2432 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(channel));
2433 bool bUseCacheCopy;
2435 if (cacheChan) {
2436 cacheChan->IsFromCache(&bUseCacheCopy);
2437 } else {
2438 bUseCacheCopy = false;
2441 if (!bUseCacheCopy) {
2442 entry = nullptr;
2443 } else {
2444 request = entry->GetRequest();
2448 if (request && entry) {
2449 // If this entry has no proxies, its request has no reference to
2450 // the entry.
2451 if (entry->HasNoProxies()) {
2452 LOG_FUNC_WITH_PARAM(
2453 gImgLog,
2454 "imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
2455 key.URI());
2456 MOZ_ASSERT(!request->HasCacheEntry(),
2457 "Proxyless entry's request has cache entry!");
2458 request->SetCacheEntry(entry);
2460 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2461 mCacheTracker->MarkUsed(entry);
2468 nsCOMPtr<nsILoadGroup> loadGroup;
2469 channel->GetLoadGroup(getter_AddRefs(loadGroup));
2471 #ifdef DEBUG
2472 if (doc) {
2473 // The load group of the channel should always match that of the
2474 // document if given. If that isn't the case, then we need to add more
2475 // plumbing to ensure we block the document as well.
2476 nsCOMPtr<nsILoadGroup> docLoadGroup = doc->GetDocumentLoadGroup();
2477 MOZ_ASSERT(docLoadGroup == loadGroup);
2479 #endif
2481 // Filter out any load flags not from nsIRequest
2482 requestFlags &= nsIRequest::LOAD_REQUESTMASK;
2484 nsresult rv = NS_OK;
2485 if (request) {
2486 // we have this in our cache already.. cancel the current (document) load
2488 // this should fire an OnStopRequest
2489 channel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
2491 *listener = nullptr; // give them back a null nsIStreamListener
2493 rv = CreateNewProxyForRequest(request, loadGroup, doc, aObserver,
2494 requestFlags, _retval);
2495 static_cast<imgRequestProxy*>(*_retval)->NotifyListener();
2496 } else {
2497 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2498 nsCOMPtr<nsIURI> originalURI;
2499 channel->GetOriginalURI(getter_AddRefs(originalURI));
2501 // XXX(seth): We should be able to just use |key| here, except that |key| is
2502 // constructed above with the *current URI* and not the *original URI*. I'm
2503 // pretty sure this is a bug, and it's preventing us from ever getting a
2504 // cache hit in LoadImageWithChannel when redirects are involved.
2505 ImageCacheKey originalURIKey(originalURI, attrs, doc);
2507 // Default to doing a principal check because we don't know who
2508 // started that load and whether their principal ended up being
2509 // inherited on the channel.
2510 NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
2511 originalURIKey, getter_AddRefs(request),
2512 getter_AddRefs(entry));
2514 // No principal specified here, because we're not passed one.
2515 // In LoadImageWithChannel, the redirects that may have been
2516 // associated with this load would have gone through necko.
2517 // We only have the final URI in ImageLib and hence don't know
2518 // if the request went through insecure redirects. But if it did,
2519 // the necko cache should have handled that (since all necko cache hits
2520 // including the redirects will go through content policy). Hence, we
2521 // can set aHadInsecureRedirect to false here.
2522 rv = request->Init(originalURI, uri, /* aHadInsecureRedirect = */ false,
2523 channel, channel, entry, aCX, nullptr,
2524 imgIRequest::CORS_NONE, nullptr);
2525 NS_ENSURE_SUCCESS(rv, rv);
2527 RefPtr<ProxyListener> pl =
2528 new ProxyListener(static_cast<nsIStreamListener*>(request.get()));
2529 pl.forget(listener);
2531 // Try to add the new request into the cache.
2532 PutIntoCache(originalURIKey, entry);
2534 rv = CreateNewProxyForRequest(request, loadGroup, doc, aObserver,
2535 requestFlags, _retval);
2537 // Explicitly don't notify our proxy, because we're loading off the
2538 // network, and necko (or things called from necko, such as
2539 // imgCacheValidator) are going to call our notifications asynchronously,
2540 // and we can't make it further asynchronous because observers might rely
2541 // on imagelib completing its work between the channel's OnStartRequest and
2542 // OnStopRequest.
2545 if (NS_FAILED(rv)) {
2546 return rv;
2549 (*_retval)->AddToLoadGroup();
2550 return rv;
2553 bool imgLoader::SupportImageWithMimeType(const char* aMimeType,
2554 AcceptedMimeTypes aAccept
2555 /* = AcceptedMimeTypes::IMAGES */) {
2556 nsAutoCString mimeType(aMimeType);
2557 ToLowerCase(mimeType);
2559 if (aAccept == AcceptedMimeTypes::IMAGES_AND_DOCUMENTS &&
2560 mimeType.EqualsLiteral("image/svg+xml")) {
2561 return true;
2564 DecoderType type = DecoderFactory::GetDecoderType(mimeType.get());
2565 return type != DecoderType::UNKNOWN;
2568 NS_IMETHODIMP
2569 imgLoader::GetMIMETypeFromContent(nsIRequest* aRequest,
2570 const uint8_t* aContents, uint32_t aLength,
2571 nsACString& aContentType) {
2572 return GetMimeTypeFromContent((const char*)aContents, aLength, aContentType);
2575 /* static */
2576 nsresult imgLoader::GetMimeTypeFromContent(const char* aContents,
2577 uint32_t aLength,
2578 nsACString& aContentType) {
2579 /* Is it a GIF? */
2580 if (aLength >= 6 &&
2581 (!strncmp(aContents, "GIF87a", 6) || !strncmp(aContents, "GIF89a", 6))) {
2582 aContentType.AssignLiteral(IMAGE_GIF);
2584 /* or a PNG? */
2585 } else if (aLength >= 8 && ((unsigned char)aContents[0] == 0x89 &&
2586 (unsigned char)aContents[1] == 0x50 &&
2587 (unsigned char)aContents[2] == 0x4E &&
2588 (unsigned char)aContents[3] == 0x47 &&
2589 (unsigned char)aContents[4] == 0x0D &&
2590 (unsigned char)aContents[5] == 0x0A &&
2591 (unsigned char)aContents[6] == 0x1A &&
2592 (unsigned char)aContents[7] == 0x0A)) {
2593 aContentType.AssignLiteral(IMAGE_PNG);
2595 /* maybe a JPEG (JFIF)? */
2596 /* JFIF files start with SOI APP0 but older files can start with SOI DQT
2597 * so we test for SOI followed by any marker, i.e. FF D8 FF
2598 * this will also work for SPIFF JPEG files if they appear in the future.
2600 * (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
2602 } else if (aLength >= 3 && ((unsigned char)aContents[0]) == 0xFF &&
2603 ((unsigned char)aContents[1]) == 0xD8 &&
2604 ((unsigned char)aContents[2]) == 0xFF) {
2605 aContentType.AssignLiteral(IMAGE_JPEG);
2607 /* or how about ART? */
2608 /* ART begins with JG (4A 47). Major version offset 2.
2609 * Minor version offset 3. Offset 4 must be nullptr.
2611 } else if (aLength >= 5 && ((unsigned char)aContents[0]) == 0x4a &&
2612 ((unsigned char)aContents[1]) == 0x47 &&
2613 ((unsigned char)aContents[4]) == 0x00) {
2614 aContentType.AssignLiteral(IMAGE_ART);
2616 } else if (aLength >= 2 && !strncmp(aContents, "BM", 2)) {
2617 aContentType.AssignLiteral(IMAGE_BMP);
2619 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
2620 // CURs begin with 2-byte 0 followed by 2-byte 2.
2621 } else if (aLength >= 4 && (!memcmp(aContents, "\000\000\001\000", 4) ||
2622 !memcmp(aContents, "\000\000\002\000", 4))) {
2623 aContentType.AssignLiteral(IMAGE_ICO);
2625 // WebPs always begin with RIFF, a 32-bit length, and WEBP.
2626 } else if (aLength >= 12 && !memcmp(aContents, "RIFF", 4) &&
2627 !memcmp(aContents + 8, "WEBP", 4)) {
2628 aContentType.AssignLiteral(IMAGE_WEBP);
2630 } else {
2631 /* none of the above? I give up */
2632 return NS_ERROR_NOT_AVAILABLE;
2635 return NS_OK;
2639 * proxy stream listener class used to handle multipart/x-mixed-replace
2642 #include "nsIRequest.h"
2643 #include "nsIStreamConverterService.h"
2645 NS_IMPL_ISUPPORTS(ProxyListener, nsIStreamListener,
2646 nsIThreadRetargetableStreamListener, nsIRequestObserver)
2648 ProxyListener::ProxyListener(nsIStreamListener* dest) : mDestListener(dest) {
2649 /* member initializers and constructor code */
2652 ProxyListener::~ProxyListener() { /* destructor code */
2655 /** nsIRequestObserver methods **/
2657 NS_IMETHODIMP
2658 ProxyListener::OnStartRequest(nsIRequest* aRequest) {
2659 if (!mDestListener) {
2660 return NS_ERROR_FAILURE;
2663 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2664 if (channel) {
2665 // We need to set the initiator type for the image load
2666 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(channel);
2667 if (timedChannel) {
2668 nsAutoString type;
2669 timedChannel->GetInitiatorType(type);
2670 if (type.IsEmpty()) {
2671 timedChannel->SetInitiatorType(NS_LITERAL_STRING("img"));
2675 nsAutoCString contentType;
2676 nsresult rv = channel->GetContentType(contentType);
2678 if (!contentType.IsEmpty()) {
2679 /* If multipart/x-mixed-replace content, we'll insert a MIME decoder
2680 in the pipeline to handle the content and pass it along to our
2681 original listener.
2683 if (NS_LITERAL_CSTRING("multipart/x-mixed-replace").Equals(contentType)) {
2684 nsCOMPtr<nsIStreamConverterService> convServ(
2685 do_GetService("@mozilla.org/streamConverters;1", &rv));
2686 if (NS_SUCCEEDED(rv)) {
2687 nsCOMPtr<nsIStreamListener> toListener(mDestListener);
2688 nsCOMPtr<nsIStreamListener> fromListener;
2690 rv = convServ->AsyncConvertData("multipart/x-mixed-replace", "*/*",
2691 toListener, nullptr,
2692 getter_AddRefs(fromListener));
2693 if (NS_SUCCEEDED(rv)) {
2694 mDestListener = fromListener;
2701 return mDestListener->OnStartRequest(aRequest);
2704 NS_IMETHODIMP
2705 ProxyListener::OnStopRequest(nsIRequest* aRequest, nsresult status) {
2706 if (!mDestListener) {
2707 return NS_ERROR_FAILURE;
2710 return mDestListener->OnStopRequest(aRequest, status);
2713 /** nsIStreamListener methods **/
2715 NS_IMETHODIMP
2716 ProxyListener::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
2717 uint64_t sourceOffset, uint32_t count) {
2718 if (!mDestListener) {
2719 return NS_ERROR_FAILURE;
2722 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
2725 /** nsThreadRetargetableStreamListener methods **/
2726 NS_IMETHODIMP
2727 ProxyListener::CheckListenerChain() {
2728 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
2729 nsresult rv = NS_OK;
2730 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
2731 do_QueryInterface(mDestListener, &rv);
2732 if (retargetableListener) {
2733 rv = retargetableListener->CheckListenerChain();
2735 MOZ_LOG(
2736 gImgLog, LogLevel::Debug,
2737 ("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
2738 "]",
2739 (NS_SUCCEEDED(rv) ? "success" : "failure"), this,
2740 (nsIStreamListener*)mDestListener, static_cast<uint32_t>(rv)));
2741 return rv;
2745 * http validate class. check a channel for a 304
2748 NS_IMPL_ISUPPORTS(imgCacheValidator, nsIStreamListener, nsIRequestObserver,
2749 nsIThreadRetargetableStreamListener, nsIChannelEventSink,
2750 nsIInterfaceRequestor, nsIAsyncVerifyRedirectCallback)
2752 imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy* progress,
2753 imgLoader* loader, imgRequest* request,
2754 nsISupports* aContext,
2755 uint64_t aInnerWindowId,
2756 bool forcePrincipalCheckForCacheEntry)
2757 : mProgressProxy(progress),
2758 mRequest(request),
2759 mContext(aContext),
2760 mInnerWindowId(aInnerWindowId),
2761 mImgLoader(loader),
2762 mHadInsecureRedirect(false) {
2763 NewRequestAndEntry(forcePrincipalCheckForCacheEntry, loader,
2764 mRequest->CacheKey(), getter_AddRefs(mNewRequest),
2765 getter_AddRefs(mNewEntry));
2768 imgCacheValidator::~imgCacheValidator() {
2769 if (mRequest) {
2770 // If something went wrong, and we never unblocked the requests waiting on
2771 // validation, now is our last chance. We will cancel the new request and
2772 // switch the waiting proxies to it.
2773 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
2777 void imgCacheValidator::AddProxy(imgRequestProxy* aProxy) {
2778 // aProxy needs to be in the loadgroup since we're validating from
2779 // the network.
2780 aProxy->AddToLoadGroup();
2782 mProxies.AppendElement(aProxy);
2785 void imgCacheValidator::RemoveProxy(imgRequestProxy* aProxy) {
2786 mProxies.RemoveElement(aProxy);
2789 void imgCacheValidator::UpdateProxies(bool aCancelRequest, bool aSyncNotify) {
2790 MOZ_ASSERT(mRequest);
2792 // Clear the validator before updating the proxies. The notifications may
2793 // clone an existing request, and its state could be inconsistent.
2794 mRequest->SetValidator(nullptr);
2795 mRequest = nullptr;
2797 // If an error occurred, we will want to cancel the new request, and make the
2798 // validating proxies point to it. Any proxies still bound to the original
2799 // request which are not validating should remain untouched.
2800 if (aCancelRequest) {
2801 MOZ_ASSERT(mNewRequest);
2802 mNewRequest->CancelAndAbort(NS_BINDING_ABORTED);
2805 // We have finished validating the request, so we can safely take ownership
2806 // of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
2807 // if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
2808 // that any potential notifications should still be suppressed in
2809 // imgRequestProxy::ChangeOwner because we haven't cleared the validating
2810 // flag yet, and thus they will remain deferred.
2811 AutoTArray<RefPtr<imgRequestProxy>, 4> proxies(std::move(mProxies));
2813 for (auto& proxy : proxies) {
2814 // First update the state of all proxies before notifying any of them
2815 // to ensure a consistent state (e.g. in case the notification causes
2816 // other proxies to be touched indirectly.)
2817 MOZ_ASSERT(proxy->IsValidating());
2818 MOZ_ASSERT(proxy->NotificationsDeferred(),
2819 "Proxies waiting on cache validation should be "
2820 "deferring notifications!");
2821 if (mNewRequest) {
2822 proxy->ChangeOwner(mNewRequest);
2824 proxy->ClearValidating();
2827 mNewRequest = nullptr;
2828 mNewEntry = nullptr;
2830 for (auto& proxy : proxies) {
2831 if (aSyncNotify) {
2832 // Notify synchronously, because the caller knows we are already in an
2833 // asynchronously-called function (e.g. OnStartRequest).
2834 proxy->SyncNotifyListener();
2835 } else {
2836 // Notify asynchronously, because the caller does not know our current
2837 // call state (e.g. ~imgCacheValidator).
2838 proxy->NotifyListener();
2843 /** nsIRequestObserver methods **/
2845 NS_IMETHODIMP
2846 imgCacheValidator::OnStartRequest(nsIRequest* aRequest) {
2847 // We may be holding on to a document, so ensure that it's released.
2848 nsCOMPtr<nsISupports> context = mContext.forget();
2850 // If for some reason we don't still have an existing request (probably
2851 // because OnStartRequest got delivered more than once), just bail.
2852 if (!mRequest) {
2853 MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
2854 aRequest->Cancel(NS_BINDING_ABORTED);
2855 return NS_ERROR_FAILURE;
2858 // If this request is coming from cache and has the same URI as our
2859 // imgRequest, the request all our proxies are pointing at is valid, and all
2860 // we have to do is tell them to notify their listeners.
2861 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(aRequest));
2862 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2863 if (cacheChan && channel && !mRequest->CacheChanged(aRequest)) {
2864 bool isFromCache = false;
2865 cacheChan->IsFromCache(&isFromCache);
2867 nsCOMPtr<nsIURI> channelURI;
2868 channel->GetURI(getter_AddRefs(channelURI));
2870 nsCOMPtr<nsIURI> finalURI;
2871 mRequest->GetFinalURI(getter_AddRefs(finalURI));
2873 bool sameURI = false;
2874 if (channelURI && finalURI) {
2875 channelURI->Equals(finalURI, &sameURI);
2878 if (isFromCache && sameURI) {
2879 // We don't need to load this any more.
2880 aRequest->Cancel(NS_BINDING_ABORTED);
2881 mNewRequest = nullptr;
2883 // Clear the validator before updating the proxies. The notifications may
2884 // clone an existing request, and its state could be inconsistent.
2885 mRequest->SetLoadId(context);
2886 mRequest->SetInnerWindowID(mInnerWindowId);
2887 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
2888 return NS_OK;
2892 // We can't load out of cache. We have to create a whole new request for the
2893 // data that's coming in off the channel.
2894 nsCOMPtr<nsIURI> uri;
2895 mRequest->GetURI(getter_AddRefs(uri));
2897 LOG_MSG_WITH_PARAM(gImgLog,
2898 "imgCacheValidator::OnStartRequest creating new request",
2899 "uri", uri);
2901 int32_t corsmode = mRequest->GetCORSMode();
2902 nsCOMPtr<nsIReferrerInfo> referrerInfo = mRequest->GetReferrerInfo();
2903 nsCOMPtr<nsIPrincipal> triggeringPrincipal =
2904 mRequest->GetTriggeringPrincipal();
2906 // Doom the old request's cache entry
2907 mRequest->RemoveFromCache();
2909 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2910 nsCOMPtr<nsIURI> originalURI;
2911 channel->GetOriginalURI(getter_AddRefs(originalURI));
2912 nsresult rv = mNewRequest->Init(originalURI, uri, mHadInsecureRedirect,
2913 aRequest, channel, mNewEntry, context,
2914 triggeringPrincipal, corsmode, referrerInfo);
2915 if (NS_FAILED(rv)) {
2916 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
2917 return rv;
2920 mDestListener = new ProxyListener(mNewRequest);
2922 // Try to add the new request into the cache. Note that the entry must be in
2923 // the cache before the proxies' ownership changes, because adding a proxy
2924 // changes the caching behaviour for imgRequests.
2925 mImgLoader->PutIntoCache(mNewRequest->CacheKey(), mNewEntry);
2926 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
2927 return mDestListener->OnStartRequest(aRequest);
2930 NS_IMETHODIMP
2931 imgCacheValidator::OnStopRequest(nsIRequest* aRequest, nsresult status) {
2932 // Be sure we've released the document that we may have been holding on to.
2933 mContext = nullptr;
2935 if (!mDestListener) {
2936 return NS_OK;
2939 return mDestListener->OnStopRequest(aRequest, status);
2942 /** nsIStreamListener methods **/
2944 NS_IMETHODIMP
2945 imgCacheValidator::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
2946 uint64_t sourceOffset, uint32_t count) {
2947 if (!mDestListener) {
2948 // XXX see bug 113959
2949 uint32_t _retval;
2950 inStr->ReadSegments(NS_DiscardSegment, nullptr, count, &_retval);
2951 return NS_OK;
2954 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
2957 /** nsIThreadRetargetableStreamListener methods **/
2959 NS_IMETHODIMP
2960 imgCacheValidator::CheckListenerChain() {
2961 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
2962 nsresult rv = NS_OK;
2963 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
2964 do_QueryInterface(mDestListener, &rv);
2965 if (retargetableListener) {
2966 rv = retargetableListener->CheckListenerChain();
2968 MOZ_LOG(
2969 gImgLog, LogLevel::Debug,
2970 ("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32 "=%s",
2971 this, static_cast<uint32_t>(rv),
2972 NS_SUCCEEDED(rv) ? "succeeded" : "failed"));
2973 return rv;
2976 /** nsIInterfaceRequestor methods **/
2978 NS_IMETHODIMP
2979 imgCacheValidator::GetInterface(const nsIID& aIID, void** aResult) {
2980 if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
2981 return QueryInterface(aIID, aResult);
2984 return mProgressProxy->GetInterface(aIID, aResult);
2987 // These functions are materially the same as the same functions in imgRequest.
2988 // We duplicate them because we're verifying whether cache loads are necessary,
2989 // not unconditionally loading.
2991 /** nsIChannelEventSink methods **/
2992 NS_IMETHODIMP
2993 imgCacheValidator::AsyncOnChannelRedirect(
2994 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
2995 nsIAsyncVerifyRedirectCallback* callback) {
2996 // Note all cache information we get from the old channel.
2997 mNewRequest->SetCacheValidation(mNewEntry, oldChannel);
2999 // If the previous URI is a non-HTTPS URI, record that fact for later use by
3000 // security code, which needs to know whether there is an insecure load at any
3001 // point in the redirect chain.
3002 nsCOMPtr<nsIURI> oldURI;
3003 bool schemeLocal = false;
3004 if (NS_FAILED(oldChannel->GetURI(getter_AddRefs(oldURI))) ||
3005 NS_FAILED(NS_URIChainHasFlags(
3006 oldURI, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE, &schemeLocal)) ||
3007 (!oldURI->SchemeIs("https") && !oldURI->SchemeIs("chrome") &&
3008 !schemeLocal)) {
3009 mHadInsecureRedirect = true;
3012 // Prepare for callback
3013 mRedirectCallback = callback;
3014 mRedirectChannel = newChannel;
3016 return mProgressProxy->AsyncOnChannelRedirect(oldChannel, newChannel, flags,
3017 this);
3020 NS_IMETHODIMP
3021 imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult) {
3022 // If we've already been told to abort, just do so.
3023 if (NS_FAILED(aResult)) {
3024 mRedirectCallback->OnRedirectVerifyCallback(aResult);
3025 mRedirectCallback = nullptr;
3026 mRedirectChannel = nullptr;
3027 return NS_OK;
3030 // make sure we have a protocol that returns data rather than opens
3031 // an external application, e.g. mailto:
3032 nsCOMPtr<nsIURI> uri;
3033 mRedirectChannel->GetURI(getter_AddRefs(uri));
3034 bool doesNotReturnData = false;
3035 NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA,
3036 &doesNotReturnData);
3038 nsresult result = NS_OK;
3040 if (doesNotReturnData) {
3041 result = NS_ERROR_ABORT;
3044 mRedirectCallback->OnRedirectVerifyCallback(result);
3045 mRedirectCallback = nullptr;
3046 mRedirectChannel = nullptr;
3047 return NS_OK;