Backed out changeset f020820671c4 (bug 1833403) for causing reftest failures on resiz...
[gecko.git] / image / imgLoader.cpp
blob37b41c68de7307a8a6f08b17270ddc27d31c03a7
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 #include "mozilla/ScopeExit.h"
9 #include "nsIChildChannel.h"
10 #undef LoadImage
12 #include "imgLoader.h"
14 #include <algorithm>
15 #include <utility>
17 #include "DecoderFactory.h"
18 #include "Image.h"
19 #include "ImageLogging.h"
20 #include "ReferrerInfo.h"
21 #include "imgRequestProxy.h"
22 #include "mozilla/Attributes.h"
23 #include "mozilla/BasePrincipal.h"
24 #include "mozilla/ChaosMode.h"
25 #include "mozilla/ClearOnShutdown.h"
26 #include "mozilla/LoadInfo.h"
27 #include "mozilla/NullPrincipal.h"
28 #include "mozilla/Preferences.h"
29 #include "mozilla/ProfilerLabels.h"
30 #include "mozilla/StaticPrefs_image.h"
31 #include "mozilla/StaticPrefs_network.h"
32 #include "mozilla/StoragePrincipalHelper.h"
33 #include "mozilla/dom/ContentParent.h"
34 #include "mozilla/dom/nsMixedContentBlocker.h"
35 #include "mozilla/image/ImageMemoryReporter.h"
36 #include "mozilla/layers/CompositorManagerChild.h"
37 #include "nsCOMPtr.h"
38 #include "nsCRT.h"
39 #include "nsComponentManagerUtils.h"
40 #include "nsContentPolicyUtils.h"
41 #include "nsContentSecurityManager.h"
42 #include "nsContentUtils.h"
43 #include "nsHttpChannel.h"
44 #include "nsIAsyncVerifyRedirectCallback.h"
45 #include "nsICacheInfoChannel.h"
46 #include "nsIChannelEventSink.h"
47 #include "nsIClassOfService.h"
48 #include "nsIEffectiveTLDService.h"
49 #include "nsIFile.h"
50 #include "nsIFileURL.h"
51 #include "nsIHttpChannel.h"
52 #include "nsIInterfaceRequestor.h"
53 #include "nsIInterfaceRequestorUtils.h"
54 #include "nsIMemoryReporter.h"
55 #include "nsINetworkPredictor.h"
56 #include "nsIProgressEventSink.h"
57 #include "nsIProtocolHandler.h"
58 #include "nsImageModule.h"
59 #include "nsMediaSniffer.h"
60 #include "nsMimeTypes.h"
61 #include "nsNetCID.h"
62 #include "nsNetUtil.h"
63 #include "nsProxyRelease.h"
64 #include "nsQueryObject.h"
65 #include "nsReadableUtils.h"
66 #include "nsStreamUtils.h"
67 #include "prtime.h"
69 // we want to explore making the document own the load group
70 // so we can associate the document URI with the load group.
71 // until this point, we have an evil hack:
72 #include "nsIHttpChannelInternal.h"
73 #include "nsILoadGroupChild.h"
74 #include "nsIDocShell.h"
76 using namespace mozilla;
77 using namespace mozilla::dom;
78 using namespace mozilla::image;
79 using namespace mozilla::net;
81 MOZ_DEFINE_MALLOC_SIZE_OF(ImagesMallocSizeOf)
83 class imgMemoryReporter final : public nsIMemoryReporter {
84 ~imgMemoryReporter() = default;
86 public:
87 NS_DECL_ISUPPORTS
89 NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
90 nsISupports* aData, bool aAnonymize) override {
91 MOZ_ASSERT(NS_IsMainThread());
93 layers::CompositorManagerChild* manager =
94 mozilla::layers::CompositorManagerChild::GetInstance();
95 if (!manager || !StaticPrefs::image_mem_debug_reporting()) {
96 layers::SharedSurfacesMemoryReport sharedSurfaces;
97 FinishCollectReports(aHandleReport, aData, aAnonymize, sharedSurfaces);
98 return NS_OK;
101 RefPtr<imgMemoryReporter> self(this);
102 nsCOMPtr<nsIHandleReportCallback> handleReport(aHandleReport);
103 nsCOMPtr<nsISupports> data(aData);
104 manager->SendReportSharedSurfacesMemory(
105 [=](layers::SharedSurfacesMemoryReport aReport) {
106 self->FinishCollectReports(handleReport, data, aAnonymize, aReport);
108 [=](mozilla::ipc::ResponseRejectReason&& aReason) {
109 layers::SharedSurfacesMemoryReport sharedSurfaces;
110 self->FinishCollectReports(handleReport, data, aAnonymize,
111 sharedSurfaces);
113 return NS_OK;
116 void FinishCollectReports(
117 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
118 bool aAnonymize, layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
119 nsTArray<ImageMemoryCounter> chrome;
120 nsTArray<ImageMemoryCounter> content;
121 nsTArray<ImageMemoryCounter> uncached;
123 for (uint32_t i = 0; i < mKnownLoaders.Length(); i++) {
124 for (imgCacheEntry* entry : mKnownLoaders[i]->mCache.Values()) {
125 RefPtr<imgRequest> req = entry->GetRequest();
126 RecordCounterForRequest(req, &content, !entry->HasNoProxies());
128 MutexAutoLock lock(mKnownLoaders[i]->mUncachedImagesMutex);
129 for (RefPtr<imgRequest> req : mKnownLoaders[i]->mUncachedImages) {
130 RecordCounterForRequest(req, &uncached, req->HasConsumers());
134 // Note that we only need to anonymize content image URIs.
136 ReportCounterArray(aHandleReport, aData, chrome, "images/chrome",
137 /* aAnonymize */ false, aSharedSurfaces);
139 ReportCounterArray(aHandleReport, aData, content, "images/content",
140 aAnonymize, aSharedSurfaces);
142 // Uncached images may be content or chrome, so anonymize them.
143 ReportCounterArray(aHandleReport, aData, uncached, "images/uncached",
144 aAnonymize, aSharedSurfaces);
146 // Report any shared surfaces that were not merged with the surface cache.
147 ImageMemoryReporter::ReportSharedSurfaces(aHandleReport, aData,
148 aSharedSurfaces);
150 nsCOMPtr<nsIMemoryReporterManager> imgr =
151 do_GetService("@mozilla.org/memory-reporter-manager;1");
152 if (imgr) {
153 imgr->EndReport();
157 static int64_t ImagesContentUsedUncompressedDistinguishedAmount() {
158 size_t n = 0;
159 for (uint32_t i = 0; i < imgLoader::sMemReporter->mKnownLoaders.Length();
160 i++) {
161 for (imgCacheEntry* entry :
162 imgLoader::sMemReporter->mKnownLoaders[i]->mCache.Values()) {
163 if (entry->HasNoProxies()) {
164 continue;
167 RefPtr<imgRequest> req = entry->GetRequest();
168 RefPtr<image::Image> image = req->GetImage();
169 if (!image) {
170 continue;
173 // Both this and EntryImageSizes measure
174 // images/content/raster/used/decoded memory. This function's
175 // measurement is secondary -- the result doesn't go in the "explicit"
176 // tree -- so we use moz_malloc_size_of instead of ImagesMallocSizeOf to
177 // prevent DMD from seeing it reported twice.
178 SizeOfState state(moz_malloc_size_of);
179 ImageMemoryCounter counter(req, image, state, /* aIsUsed = */ true);
181 n += counter.Values().DecodedHeap();
182 n += counter.Values().DecodedNonHeap();
183 n += counter.Values().DecodedUnknown();
186 return n;
189 void RegisterLoader(imgLoader* aLoader) {
190 mKnownLoaders.AppendElement(aLoader);
193 void UnregisterLoader(imgLoader* aLoader) {
194 mKnownLoaders.RemoveElement(aLoader);
197 private:
198 nsTArray<imgLoader*> mKnownLoaders;
200 struct MemoryTotal {
201 MemoryTotal& operator+=(const ImageMemoryCounter& aImageCounter) {
202 if (aImageCounter.Type() == imgIContainer::TYPE_RASTER) {
203 if (aImageCounter.IsUsed()) {
204 mUsedRasterCounter += aImageCounter.Values();
205 } else {
206 mUnusedRasterCounter += aImageCounter.Values();
208 } else if (aImageCounter.Type() == imgIContainer::TYPE_VECTOR) {
209 if (aImageCounter.IsUsed()) {
210 mUsedVectorCounter += aImageCounter.Values();
211 } else {
212 mUnusedVectorCounter += aImageCounter.Values();
214 } else if (aImageCounter.Type() == imgIContainer::TYPE_REQUEST) {
215 // Nothing to do, we did not get to the point of having an image.
216 } else {
217 MOZ_CRASH("Unexpected image type");
220 return *this;
223 const MemoryCounter& UsedRaster() const { return mUsedRasterCounter; }
224 const MemoryCounter& UnusedRaster() const { return mUnusedRasterCounter; }
225 const MemoryCounter& UsedVector() const { return mUsedVectorCounter; }
226 const MemoryCounter& UnusedVector() const { return mUnusedVectorCounter; }
228 private:
229 MemoryCounter mUsedRasterCounter;
230 MemoryCounter mUnusedRasterCounter;
231 MemoryCounter mUsedVectorCounter;
232 MemoryCounter mUnusedVectorCounter;
235 // Reports all images of a single kind, e.g. all used chrome images.
236 void ReportCounterArray(nsIHandleReportCallback* aHandleReport,
237 nsISupports* aData,
238 nsTArray<ImageMemoryCounter>& aCounterArray,
239 const char* aPathPrefix, bool aAnonymize,
240 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
241 MemoryTotal summaryTotal;
242 MemoryTotal nonNotableTotal;
244 // Report notable images, and compute total and non-notable aggregate sizes.
245 for (uint32_t i = 0; i < aCounterArray.Length(); i++) {
246 ImageMemoryCounter& counter = aCounterArray[i];
248 if (aAnonymize) {
249 counter.URI().Truncate();
250 counter.URI().AppendPrintf("<anonymized-%u>", i);
251 } else {
252 // The URI could be an extremely long data: URI. Truncate if needed.
253 static const size_t max = 256;
254 if (counter.URI().Length() > max) {
255 counter.URI().Truncate(max);
256 counter.URI().AppendLiteral(" (truncated)");
258 counter.URI().ReplaceChar('/', '\\');
261 summaryTotal += counter;
263 if (counter.IsNotable() || StaticPrefs::image_mem_debug_reporting()) {
264 ReportImage(aHandleReport, aData, aPathPrefix, counter,
265 aSharedSurfaces);
266 } else {
267 ImageMemoryReporter::TrimSharedSurfaces(counter, aSharedSurfaces);
268 nonNotableTotal += counter;
272 // Report non-notable images in aggregate.
273 ReportTotal(aHandleReport, aData, /* aExplicit = */ true, aPathPrefix,
274 "<non-notable images>/", nonNotableTotal);
276 // Report a summary in aggregate, outside of the explicit tree.
277 ReportTotal(aHandleReport, aData, /* aExplicit = */ false, aPathPrefix, "",
278 summaryTotal);
281 static void ReportImage(nsIHandleReportCallback* aHandleReport,
282 nsISupports* aData, const char* aPathPrefix,
283 const ImageMemoryCounter& aCounter,
284 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
285 nsAutoCString pathPrefix("explicit/"_ns);
286 pathPrefix.Append(aPathPrefix);
288 switch (aCounter.Type()) {
289 case imgIContainer::TYPE_RASTER:
290 pathPrefix.AppendLiteral("/raster/");
291 break;
292 case imgIContainer::TYPE_VECTOR:
293 pathPrefix.AppendLiteral("/vector/");
294 break;
295 case imgIContainer::TYPE_REQUEST:
296 pathPrefix.AppendLiteral("/request/");
297 break;
298 default:
299 pathPrefix.AppendLiteral("/unknown=");
300 pathPrefix.AppendInt(aCounter.Type());
301 pathPrefix.AppendLiteral("/");
302 break;
305 pathPrefix.Append(aCounter.IsUsed() ? "used/" : "unused/");
306 if (aCounter.IsValidating()) {
307 pathPrefix.AppendLiteral("validating/");
309 if (aCounter.HasError()) {
310 pathPrefix.AppendLiteral("err/");
313 pathPrefix.AppendLiteral("progress=");
314 pathPrefix.AppendInt(aCounter.Progress(), 16);
315 pathPrefix.AppendLiteral("/");
317 pathPrefix.AppendLiteral("image(");
318 pathPrefix.AppendInt(aCounter.IntrinsicSize().width);
319 pathPrefix.AppendLiteral("x");
320 pathPrefix.AppendInt(aCounter.IntrinsicSize().height);
321 pathPrefix.AppendLiteral(", ");
323 if (aCounter.URI().IsEmpty()) {
324 pathPrefix.AppendLiteral("<unknown URI>");
325 } else {
326 pathPrefix.Append(aCounter.URI());
329 pathPrefix.AppendLiteral(")/");
331 ReportSurfaces(aHandleReport, aData, pathPrefix, aCounter, aSharedSurfaces);
333 ReportSourceValue(aHandleReport, aData, pathPrefix, aCounter.Values());
336 static void ReportSurfaces(
337 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
338 const nsACString& aPathPrefix, const ImageMemoryCounter& aCounter,
339 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
340 for (const SurfaceMemoryCounter& counter : aCounter.Surfaces()) {
341 nsAutoCString surfacePathPrefix(aPathPrefix);
342 switch (counter.Type()) {
343 case SurfaceMemoryCounterType::NORMAL:
344 if (counter.IsLocked()) {
345 surfacePathPrefix.AppendLiteral("locked/");
346 } else {
347 surfacePathPrefix.AppendLiteral("unlocked/");
349 if (counter.IsFactor2()) {
350 surfacePathPrefix.AppendLiteral("factor2/");
352 if (counter.CannotSubstitute()) {
353 surfacePathPrefix.AppendLiteral("cannot_substitute/");
355 break;
356 case SurfaceMemoryCounterType::CONTAINER:
357 surfacePathPrefix.AppendLiteral("container/");
358 break;
359 default:
360 MOZ_ASSERT_UNREACHABLE("Unknown counter type");
361 break;
364 surfacePathPrefix.AppendLiteral("types=");
365 surfacePathPrefix.AppendInt(counter.Values().SurfaceTypes(), 16);
366 surfacePathPrefix.AppendLiteral("/surface(");
367 surfacePathPrefix.AppendInt(counter.Key().Size().width);
368 surfacePathPrefix.AppendLiteral("x");
369 surfacePathPrefix.AppendInt(counter.Key().Size().height);
371 if (!counter.IsFinished()) {
372 surfacePathPrefix.AppendLiteral(", incomplete");
375 if (counter.Values().ExternalHandles() > 0) {
376 surfacePathPrefix.AppendLiteral(", handles:");
377 surfacePathPrefix.AppendInt(
378 uint32_t(counter.Values().ExternalHandles()));
381 ImageMemoryReporter::AppendSharedSurfacePrefix(surfacePathPrefix, counter,
382 aSharedSurfaces);
384 PlaybackType playback = counter.Key().Playback();
385 if (playback == PlaybackType::eAnimated) {
386 if (StaticPrefs::image_mem_debug_reporting()) {
387 surfacePathPrefix.AppendPrintf(
388 " (animation %4u)", uint32_t(counter.Values().FrameIndex()));
389 } else {
390 surfacePathPrefix.AppendLiteral(" (animation)");
394 if (counter.Key().Flags() != DefaultSurfaceFlags()) {
395 surfacePathPrefix.AppendLiteral(", flags:");
396 surfacePathPrefix.AppendInt(uint32_t(counter.Key().Flags()),
397 /* aRadix = */ 16);
400 if (counter.Key().Region()) {
401 const ImageIntRegion& region = counter.Key().Region().ref();
402 const gfx::IntRect& rect = region.Rect();
403 surfacePathPrefix.AppendLiteral(", region:[ rect=(");
404 surfacePathPrefix.AppendInt(rect.x);
405 surfacePathPrefix.AppendLiteral(",");
406 surfacePathPrefix.AppendInt(rect.y);
407 surfacePathPrefix.AppendLiteral(") ");
408 surfacePathPrefix.AppendInt(rect.width);
409 surfacePathPrefix.AppendLiteral("x");
410 surfacePathPrefix.AppendInt(rect.height);
411 if (region.IsRestricted()) {
412 const gfx::IntRect& restrict = region.Restriction();
413 if (restrict == rect) {
414 surfacePathPrefix.AppendLiteral(", restrict=rect");
415 } else {
416 surfacePathPrefix.AppendLiteral(", restrict=(");
417 surfacePathPrefix.AppendInt(restrict.x);
418 surfacePathPrefix.AppendLiteral(",");
419 surfacePathPrefix.AppendInt(restrict.y);
420 surfacePathPrefix.AppendLiteral(") ");
421 surfacePathPrefix.AppendInt(restrict.width);
422 surfacePathPrefix.AppendLiteral("x");
423 surfacePathPrefix.AppendInt(restrict.height);
426 if (region.GetExtendMode() != gfx::ExtendMode::CLAMP) {
427 surfacePathPrefix.AppendLiteral(", extendMode=");
428 surfacePathPrefix.AppendInt(int32_t(region.GetExtendMode()));
430 surfacePathPrefix.AppendLiteral("]");
433 const SVGImageContext& context = counter.Key().SVGContext();
434 surfacePathPrefix.AppendLiteral(", svgContext:[ ");
435 if (context.GetViewportSize()) {
436 const CSSIntSize& size = context.GetViewportSize().ref();
437 surfacePathPrefix.AppendLiteral("viewport=(");
438 surfacePathPrefix.AppendInt(size.width);
439 surfacePathPrefix.AppendLiteral("x");
440 surfacePathPrefix.AppendInt(size.height);
441 surfacePathPrefix.AppendLiteral(") ");
443 if (context.GetPreserveAspectRatio()) {
444 nsAutoString aspect;
445 context.GetPreserveAspectRatio()->ToString(aspect);
446 surfacePathPrefix.AppendLiteral("preserveAspectRatio=(");
447 LossyAppendUTF16toASCII(aspect, surfacePathPrefix);
448 surfacePathPrefix.AppendLiteral(") ");
450 if (auto scheme = context.GetColorScheme()) {
451 surfacePathPrefix.AppendLiteral("colorScheme=");
452 surfacePathPrefix.AppendInt(int32_t(*scheme));
453 surfacePathPrefix.AppendLiteral(" ");
455 if (context.GetContextPaint()) {
456 const SVGEmbeddingContextPaint* paint = context.GetContextPaint();
457 surfacePathPrefix.AppendLiteral("contextPaint=(");
458 if (paint->GetFill()) {
459 surfacePathPrefix.AppendLiteral(" fill=");
460 surfacePathPrefix.AppendInt(paint->GetFill()->ToABGR(), 16);
462 if (paint->GetFillOpacity() != 1.0) {
463 surfacePathPrefix.AppendLiteral(" fillOpa=");
464 surfacePathPrefix.AppendFloat(paint->GetFillOpacity());
466 if (paint->GetStroke()) {
467 surfacePathPrefix.AppendLiteral(" stroke=");
468 surfacePathPrefix.AppendInt(paint->GetStroke()->ToABGR(), 16);
470 if (paint->GetStrokeOpacity() != 1.0) {
471 surfacePathPrefix.AppendLiteral(" strokeOpa=");
472 surfacePathPrefix.AppendFloat(paint->GetStrokeOpacity());
474 surfacePathPrefix.AppendLiteral(" ) ");
476 surfacePathPrefix.AppendLiteral("]");
478 surfacePathPrefix.AppendLiteral(")/");
480 ReportValues(aHandleReport, aData, surfacePathPrefix, counter.Values());
484 static void ReportTotal(nsIHandleReportCallback* aHandleReport,
485 nsISupports* aData, bool aExplicit,
486 const char* aPathPrefix, const char* aPathInfix,
487 const MemoryTotal& aTotal) {
488 nsAutoCString pathPrefix;
489 if (aExplicit) {
490 pathPrefix.AppendLiteral("explicit/");
492 pathPrefix.Append(aPathPrefix);
494 nsAutoCString rasterUsedPrefix(pathPrefix);
495 rasterUsedPrefix.AppendLiteral("/raster/used/");
496 rasterUsedPrefix.Append(aPathInfix);
497 ReportValues(aHandleReport, aData, rasterUsedPrefix, aTotal.UsedRaster());
499 nsAutoCString rasterUnusedPrefix(pathPrefix);
500 rasterUnusedPrefix.AppendLiteral("/raster/unused/");
501 rasterUnusedPrefix.Append(aPathInfix);
502 ReportValues(aHandleReport, aData, rasterUnusedPrefix,
503 aTotal.UnusedRaster());
505 nsAutoCString vectorUsedPrefix(pathPrefix);
506 vectorUsedPrefix.AppendLiteral("/vector/used/");
507 vectorUsedPrefix.Append(aPathInfix);
508 ReportValues(aHandleReport, aData, vectorUsedPrefix, aTotal.UsedVector());
510 nsAutoCString vectorUnusedPrefix(pathPrefix);
511 vectorUnusedPrefix.AppendLiteral("/vector/unused/");
512 vectorUnusedPrefix.Append(aPathInfix);
513 ReportValues(aHandleReport, aData, vectorUnusedPrefix,
514 aTotal.UnusedVector());
517 static void ReportValues(nsIHandleReportCallback* aHandleReport,
518 nsISupports* aData, const nsACString& aPathPrefix,
519 const MemoryCounter& aCounter) {
520 ReportSourceValue(aHandleReport, aData, aPathPrefix, aCounter);
522 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "decoded-heap",
523 "Decoded image data which is stored on the heap.",
524 aCounter.DecodedHeap());
526 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
527 "decoded-nonheap",
528 "Decoded image data which isn't stored on the heap.",
529 aCounter.DecodedNonHeap());
531 // We don't know for certain whether or not it is on the heap, so let's
532 // just report it as non-heap for reporting purposes.
533 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
534 "decoded-unknown",
535 "Decoded image data which is unknown to be on the heap or not.",
536 aCounter.DecodedUnknown());
539 static void ReportSourceValue(nsIHandleReportCallback* aHandleReport,
540 nsISupports* aData,
541 const nsACString& aPathPrefix,
542 const MemoryCounter& aCounter) {
543 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "source",
544 "Raster image source data and vector image documents.",
545 aCounter.Source());
548 static void ReportValue(nsIHandleReportCallback* aHandleReport,
549 nsISupports* aData, int32_t aKind,
550 const nsACString& aPathPrefix,
551 const char* aPathSuffix, const char* aDescription,
552 size_t aValue) {
553 if (aValue == 0) {
554 return;
557 nsAutoCString desc(aDescription);
558 nsAutoCString path(aPathPrefix);
559 path.Append(aPathSuffix);
561 aHandleReport->Callback(""_ns, path, aKind, UNITS_BYTES, aValue, desc,
562 aData);
565 static void RecordCounterForRequest(imgRequest* aRequest,
566 nsTArray<ImageMemoryCounter>* aArray,
567 bool aIsUsed) {
568 SizeOfState state(ImagesMallocSizeOf);
569 RefPtr<image::Image> image = aRequest->GetImage();
570 if (image) {
571 ImageMemoryCounter counter(aRequest, image, state, aIsUsed);
572 aArray->AppendElement(std::move(counter));
573 } else {
574 // We can at least record some information about the image from the
575 // request, and mark it as not knowing the image type yet.
576 ImageMemoryCounter counter(aRequest, state, aIsUsed);
577 aArray->AppendElement(std::move(counter));
582 NS_IMPL_ISUPPORTS(imgMemoryReporter, nsIMemoryReporter)
584 NS_IMPL_ISUPPORTS(nsProgressNotificationProxy, nsIProgressEventSink,
585 nsIChannelEventSink, nsIInterfaceRequestor)
587 NS_IMETHODIMP
588 nsProgressNotificationProxy::OnProgress(nsIRequest* request, int64_t progress,
589 int64_t progressMax) {
590 nsCOMPtr<nsILoadGroup> loadGroup;
591 request->GetLoadGroup(getter_AddRefs(loadGroup));
593 nsCOMPtr<nsIProgressEventSink> target;
594 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
595 NS_GET_IID(nsIProgressEventSink),
596 getter_AddRefs(target));
597 if (!target) {
598 return NS_OK;
600 return target->OnProgress(mImageRequest, progress, progressMax);
603 NS_IMETHODIMP
604 nsProgressNotificationProxy::OnStatus(nsIRequest* request, nsresult status,
605 const char16_t* statusArg) {
606 nsCOMPtr<nsILoadGroup> loadGroup;
607 request->GetLoadGroup(getter_AddRefs(loadGroup));
609 nsCOMPtr<nsIProgressEventSink> target;
610 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
611 NS_GET_IID(nsIProgressEventSink),
612 getter_AddRefs(target));
613 if (!target) {
614 return NS_OK;
616 return target->OnStatus(mImageRequest, status, statusArg);
619 NS_IMETHODIMP
620 nsProgressNotificationProxy::AsyncOnChannelRedirect(
621 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
622 nsIAsyncVerifyRedirectCallback* cb) {
623 // Tell the original original callbacks about it too
624 nsCOMPtr<nsILoadGroup> loadGroup;
625 newChannel->GetLoadGroup(getter_AddRefs(loadGroup));
626 nsCOMPtr<nsIChannelEventSink> target;
627 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
628 NS_GET_IID(nsIChannelEventSink),
629 getter_AddRefs(target));
630 if (!target) {
631 cb->OnRedirectVerifyCallback(NS_OK);
632 return NS_OK;
635 // Delegate to |target| if set, reusing |cb|
636 return target->AsyncOnChannelRedirect(oldChannel, newChannel, flags, cb);
639 NS_IMETHODIMP
640 nsProgressNotificationProxy::GetInterface(const nsIID& iid, void** result) {
641 if (iid.Equals(NS_GET_IID(nsIProgressEventSink))) {
642 *result = static_cast<nsIProgressEventSink*>(this);
643 NS_ADDREF_THIS();
644 return NS_OK;
646 if (iid.Equals(NS_GET_IID(nsIChannelEventSink))) {
647 *result = static_cast<nsIChannelEventSink*>(this);
648 NS_ADDREF_THIS();
649 return NS_OK;
651 if (mOriginalCallbacks) {
652 return mOriginalCallbacks->GetInterface(iid, result);
654 return NS_NOINTERFACE;
657 static void NewRequestAndEntry(bool aForcePrincipalCheckForCacheEntry,
658 imgLoader* aLoader, const ImageCacheKey& aKey,
659 imgRequest** aRequest, imgCacheEntry** aEntry) {
660 RefPtr<imgRequest> request = new imgRequest(aLoader, aKey);
661 RefPtr<imgCacheEntry> entry =
662 new imgCacheEntry(aLoader, request, aForcePrincipalCheckForCacheEntry);
663 aLoader->AddToUncachedImages(request);
664 request.forget(aRequest);
665 entry.forget(aEntry);
668 static bool ShouldRevalidateEntry(imgCacheEntry* aEntry, nsLoadFlags aFlags,
669 bool aHasExpired) {
670 if (aFlags & nsIRequest::LOAD_BYPASS_CACHE) {
671 return false;
673 if (aFlags & nsIRequest::VALIDATE_ALWAYS) {
674 return true;
676 if (aEntry->GetMustValidate()) {
677 return true;
679 if (aHasExpired) {
680 // The cache entry has expired... Determine whether the stale cache
681 // entry can be used without validation...
682 if (aFlags & (nsIRequest::LOAD_FROM_CACHE | nsIRequest::VALIDATE_NEVER |
683 nsIRequest::VALIDATE_ONCE_PER_SESSION)) {
684 // LOAD_FROM_CACHE, VALIDATE_NEVER and VALIDATE_ONCE_PER_SESSION allow
685 // stale cache entries to be used unless they have been explicitly marked
686 // to indicate that revalidation is necessary.
687 return false;
689 // Entry is expired, revalidate.
690 return true;
692 return false;
695 /* Call content policies on cached images that went through a redirect */
696 static bool ShouldLoadCachedImage(imgRequest* aImgRequest,
697 Document* aLoadingDocument,
698 nsIPrincipal* aTriggeringPrincipal,
699 nsContentPolicyType aPolicyType,
700 bool aSendCSPViolationReports) {
701 /* Call content policies on cached images - Bug 1082837
702 * Cached images are keyed off of the first uri in a redirect chain.
703 * Hence content policies don't get a chance to test the intermediate hops
704 * or the final destination. Here we test the final destination using
705 * mFinalURI off of the imgRequest and passing it into content policies.
706 * For Mixed Content Blocker, we do an additional check to determine if any
707 * of the intermediary hops went through an insecure redirect with the
708 * mHadInsecureRedirect flag
710 bool insecureRedirect = aImgRequest->HadInsecureRedirect();
711 nsCOMPtr<nsIURI> contentLocation;
712 aImgRequest->GetFinalURI(getter_AddRefs(contentLocation));
713 nsresult rv;
715 nsCOMPtr<nsIPrincipal> loadingPrincipal =
716 aLoadingDocument ? aLoadingDocument->NodePrincipal()
717 : aTriggeringPrincipal;
718 // If there is no context and also no triggeringPrincipal, then we use a fresh
719 // nullPrincipal as the loadingPrincipal because we can not create a loadinfo
720 // without a valid loadingPrincipal.
721 if (!loadingPrincipal) {
722 loadingPrincipal = NullPrincipal::CreateWithoutOriginAttributes();
725 nsCOMPtr<nsILoadInfo> secCheckLoadInfo = new LoadInfo(
726 loadingPrincipal, aTriggeringPrincipal, aLoadingDocument,
727 nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK, aPolicyType);
729 secCheckLoadInfo->SetSendCSPViolationEvents(aSendCSPViolationReports);
731 int16_t decision = nsIContentPolicy::REJECT_REQUEST;
732 rv = NS_CheckContentLoadPolicy(contentLocation, secCheckLoadInfo,
733 ""_ns, // mime guess
734 &decision, nsContentUtils::GetContentPolicy());
735 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
736 return false;
739 // We call all Content Policies above, but we also have to call mcb
740 // individually to check the intermediary redirect hops are secure.
741 if (insecureRedirect) {
742 // Bug 1314356: If the image ended up in the cache upgraded by HSTS and the
743 // page uses upgrade-inscure-requests it had an insecure redirect
744 // (http->https). We need to invalidate the image and reload it because
745 // mixed content blocker only bails if upgrade-insecure-requests is set on
746 // the doc and the resource load is http: which would result in an incorrect
747 // mixed content warning.
748 nsCOMPtr<nsIDocShell> docShell =
749 NS_CP_GetDocShellFromContext(ToSupports(aLoadingDocument));
750 if (docShell) {
751 Document* document = docShell->GetDocument();
752 if (document && document->GetUpgradeInsecureRequests(false)) {
753 return false;
757 if (!aTriggeringPrincipal || !aTriggeringPrincipal->IsSystemPrincipal()) {
758 // reset the decision for mixed content blocker check
759 decision = nsIContentPolicy::REJECT_REQUEST;
760 rv = nsMixedContentBlocker::ShouldLoad(insecureRedirect, contentLocation,
761 secCheckLoadInfo,
762 ""_ns, // mime guess
763 true, // aReportError
764 &decision);
765 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
766 return false;
771 return true;
774 // Returns true if this request is compatible with the given CORS mode on the
775 // given loading principal, and false if the request may not be reused due
776 // to CORS.
777 static bool ValidateCORSMode(imgRequest* aRequest, bool aForcePrincipalCheck,
778 CORSMode aCORSMode,
779 nsIPrincipal* aTriggeringPrincipal) {
780 // If the entry's CORS mode doesn't match, or the CORS mode matches but the
781 // document principal isn't the same, we can't use this request.
782 if (aRequest->GetCORSMode() != aCORSMode) {
783 return false;
786 if (aRequest->GetCORSMode() != CORS_NONE || aForcePrincipalCheck) {
787 nsCOMPtr<nsIPrincipal> otherprincipal = aRequest->GetTriggeringPrincipal();
789 // If we previously had a principal, but we don't now, we can't use this
790 // request.
791 if (otherprincipal && !aTriggeringPrincipal) {
792 return false;
795 if (otherprincipal && aTriggeringPrincipal &&
796 !otherprincipal->Equals(aTriggeringPrincipal)) {
797 return false;
801 return true;
804 static bool ValidateSecurityInfo(imgRequest* aRequest,
805 bool aForcePrincipalCheck, CORSMode aCORSMode,
806 nsIPrincipal* aTriggeringPrincipal,
807 Document* aLoadingDocument,
808 nsContentPolicyType aPolicyType) {
809 if (!ValidateCORSMode(aRequest, aForcePrincipalCheck, aCORSMode,
810 aTriggeringPrincipal)) {
811 return false;
813 // Content Policy Check on Cached Images
814 return ShouldLoadCachedImage(aRequest, aLoadingDocument, aTriggeringPrincipal,
815 aPolicyType,
816 /* aSendCSPViolationReports */ false);
819 static nsresult NewImageChannel(
820 nsIChannel** aResult,
821 // If aForcePrincipalCheckForCacheEntry is true, then we will
822 // force a principal check even when not using CORS before
823 // assuming we have a cache hit on a cache entry that we
824 // create for this channel. This is an out param that should
825 // be set to true if this channel ends up depending on
826 // aTriggeringPrincipal and false otherwise.
827 bool* aForcePrincipalCheckForCacheEntry, nsIURI* aURI,
828 nsIURI* aInitialDocumentURI, CORSMode aCORSMode,
829 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
830 nsLoadFlags aLoadFlags, nsContentPolicyType aPolicyType,
831 nsIPrincipal* aTriggeringPrincipal, nsINode* aRequestingNode,
832 bool aRespectPrivacy, uint64_t aEarlyHintPreloaderId) {
833 MOZ_ASSERT(aResult);
835 nsresult rv;
836 nsCOMPtr<nsIHttpChannel> newHttpChannel;
838 nsCOMPtr<nsIInterfaceRequestor> callbacks;
840 if (aLoadGroup) {
841 // Get the notification callbacks from the load group for the new channel.
843 // XXX: This is not exactly correct, because the network request could be
844 // referenced by multiple windows... However, the new channel needs
845 // something. So, using the 'first' notification callbacks is better
846 // than nothing...
848 aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
851 // Pass in a nullptr loadgroup because this is the underlying network
852 // request. This request may be referenced by several proxy image requests
853 // (possibly in different documents).
854 // If all of the proxy requests are canceled then this request should be
855 // canceled too.
858 nsSecurityFlags securityFlags =
859 nsContentSecurityManager::ComputeSecurityFlags(
860 aCORSMode, nsContentSecurityManager::CORSSecurityMapping::
861 CORS_NONE_MAPS_TO_INHERITED_CONTEXT);
863 securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
865 // Note we are calling NS_NewChannelWithTriggeringPrincipal() here with a
866 // node and a principal. This is for things like background images that are
867 // specified by user stylesheets, where the document is being styled, but
868 // the principal is that of the user stylesheet.
869 if (aRequestingNode && aTriggeringPrincipal) {
870 rv = NS_NewChannelWithTriggeringPrincipal(aResult, aURI, aRequestingNode,
871 aTriggeringPrincipal,
872 securityFlags, aPolicyType,
873 nullptr, // PerformanceStorage
874 nullptr, // loadGroup
875 callbacks, aLoadFlags);
877 if (NS_FAILED(rv)) {
878 return rv;
881 if (aPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
882 // If this is a favicon loading, we will use the originAttributes from the
883 // triggeringPrincipal as the channel's originAttributes. This allows the
884 // favicon loading from XUL will use the correct originAttributes.
886 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
887 rv = loadInfo->SetOriginAttributes(
888 aTriggeringPrincipal->OriginAttributesRef());
890 } else {
891 // either we are loading something inside a document, in which case
892 // we should always have a requestingNode, or we are loading something
893 // outside a document, in which case the triggeringPrincipal and
894 // triggeringPrincipal should always be the systemPrincipal.
895 // However, there are exceptions: one is Notifications which create a
896 // channel in the parent process in which case we can't get a
897 // requestingNode.
898 rv = NS_NewChannel(aResult, aURI, nsContentUtils::GetSystemPrincipal(),
899 securityFlags, aPolicyType,
900 nullptr, // nsICookieJarSettings
901 nullptr, // PerformanceStorage
902 nullptr, // loadGroup
903 callbacks, aLoadFlags);
905 if (NS_FAILED(rv)) {
906 return rv;
909 // Use the OriginAttributes from the loading principal, if one is available,
910 // and adjust the private browsing ID based on what kind of load the caller
911 // has asked us to perform.
912 OriginAttributes attrs;
913 if (aTriggeringPrincipal) {
914 attrs = aTriggeringPrincipal->OriginAttributesRef();
916 attrs.mPrivateBrowsingId = aRespectPrivacy ? 1 : 0;
918 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
919 rv = loadInfo->SetOriginAttributes(attrs);
922 if (NS_FAILED(rv)) {
923 return rv;
926 // only inherit if we have a principal
927 *aForcePrincipalCheckForCacheEntry =
928 aTriggeringPrincipal && nsContentUtils::ChannelShouldInheritPrincipal(
929 aTriggeringPrincipal, aURI,
930 /* aInheritForAboutBlank */ false,
931 /* aForceInherit */ false);
933 // Initialize HTTP-specific attributes
934 newHttpChannel = do_QueryInterface(*aResult);
935 if (newHttpChannel) {
936 nsCOMPtr<nsIHttpChannelInternal> httpChannelInternal =
937 do_QueryInterface(newHttpChannel);
938 NS_ENSURE_TRUE(httpChannelInternal, NS_ERROR_UNEXPECTED);
939 rv = httpChannelInternal->SetDocumentURI(aInitialDocumentURI);
940 MOZ_ASSERT(NS_SUCCEEDED(rv));
941 if (aReferrerInfo) {
942 DebugOnly<nsresult> rv = newHttpChannel->SetReferrerInfo(aReferrerInfo);
943 MOZ_ASSERT(NS_SUCCEEDED(rv));
946 if (aEarlyHintPreloaderId) {
947 rv = httpChannelInternal->SetEarlyHintPreloaderId(aEarlyHintPreloaderId);
948 NS_ENSURE_SUCCESS(rv, rv);
952 // Image channels are loaded by default with reduced priority.
953 nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(*aResult);
954 if (p) {
955 uint32_t priority = nsISupportsPriority::PRIORITY_LOW;
957 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
958 ++priority; // further reduce priority for background loads
961 p->AdjustPriority(priority);
964 // Create a new loadgroup for this new channel, using the old group as
965 // the parent. The indirection keeps the channel insulated from cancels,
966 // but does allow a way for this revalidation to be associated with at
967 // least one base load group for scheduling/caching purposes.
969 nsCOMPtr<nsILoadGroup> loadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID);
970 nsCOMPtr<nsILoadGroupChild> childLoadGroup = do_QueryInterface(loadGroup);
971 if (childLoadGroup) {
972 childLoadGroup->SetParentLoadGroup(aLoadGroup);
974 (*aResult)->SetLoadGroup(loadGroup);
976 return NS_OK;
979 static uint32_t SecondsFromPRTime(PRTime aTime) {
980 return nsContentUtils::SecondsFromPRTime(aTime);
983 /* static */
984 imgCacheEntry::imgCacheEntry(imgLoader* loader, imgRequest* request,
985 bool forcePrincipalCheck)
986 : mLoader(loader),
987 mRequest(request),
988 mDataSize(0),
989 mTouchedTime(SecondsFromPRTime(PR_Now())),
990 mLoadTime(SecondsFromPRTime(PR_Now())),
991 mExpiryTime(0),
992 mMustValidate(false),
993 // We start off as evicted so we don't try to update the cache.
994 // PutIntoCache will set this to false.
995 mEvicted(true),
996 mHasNoProxies(true),
997 mForcePrincipalCheck(forcePrincipalCheck),
998 mHasNotified(false) {}
1000 imgCacheEntry::~imgCacheEntry() {
1001 LOG_FUNC(gImgLog, "imgCacheEntry::~imgCacheEntry()");
1004 void imgCacheEntry::Touch(bool updateTime /* = true */) {
1005 LOG_SCOPE(gImgLog, "imgCacheEntry::Touch");
1007 if (updateTime) {
1008 mTouchedTime = SecondsFromPRTime(PR_Now());
1011 UpdateCache();
1014 void imgCacheEntry::UpdateCache(int32_t diff /* = 0 */) {
1015 // Don't update the cache if we've been removed from it or it doesn't care
1016 // about our size or usage.
1017 if (!Evicted() && HasNoProxies()) {
1018 mLoader->CacheEntriesChanged(diff);
1022 void imgCacheEntry::UpdateLoadTime() {
1023 mLoadTime = SecondsFromPRTime(PR_Now());
1026 void imgCacheEntry::SetHasNoProxies(bool hasNoProxies) {
1027 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1028 if (hasNoProxies) {
1029 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies true", "uri",
1030 mRequest->CacheKey().URI());
1031 } else {
1032 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies false",
1033 "uri", mRequest->CacheKey().URI());
1037 mHasNoProxies = hasNoProxies;
1040 imgCacheQueue::imgCacheQueue() : mDirty(false), mSize(0) {}
1042 void imgCacheQueue::UpdateSize(int32_t diff) { mSize += diff; }
1044 uint32_t imgCacheQueue::GetSize() const { return mSize; }
1046 void imgCacheQueue::Remove(imgCacheEntry* entry) {
1047 uint64_t index = mQueue.IndexOf(entry);
1048 if (index == queueContainer::NoIndex) {
1049 return;
1052 mSize -= mQueue[index]->GetDataSize();
1054 // If the queue is clean and this is the first entry,
1055 // then we can efficiently remove the entry without
1056 // dirtying the sort order.
1057 if (!IsDirty() && index == 0) {
1058 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1059 mQueue.RemoveLastElement();
1060 return;
1063 // Remove from the middle of the list. This potentially
1064 // breaks the binary heap sort order.
1065 mQueue.RemoveElementAt(index);
1067 // If we only have one entry or the queue is empty, though,
1068 // then the sort order is still effectively good. Simply
1069 // refresh the list to clear the dirty flag.
1070 if (mQueue.Length() <= 1) {
1071 Refresh();
1072 return;
1075 // Otherwise we must mark the queue dirty and potentially
1076 // trigger an expensive sort later.
1077 MarkDirty();
1080 void imgCacheQueue::Push(imgCacheEntry* entry) {
1081 mSize += entry->GetDataSize();
1083 RefPtr<imgCacheEntry> refptr(entry);
1084 mQueue.AppendElement(std::move(refptr));
1085 // If we're not dirty already, then we can efficiently add this to the
1086 // binary heap immediately. This is only O(log n).
1087 if (!IsDirty()) {
1088 std::push_heap(mQueue.begin(), mQueue.end(),
1089 imgLoader::CompareCacheEntries);
1093 already_AddRefed<imgCacheEntry> imgCacheQueue::Pop() {
1094 if (mQueue.IsEmpty()) {
1095 return nullptr;
1097 if (IsDirty()) {
1098 Refresh();
1101 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1102 RefPtr<imgCacheEntry> entry = mQueue.PopLastElement();
1104 mSize -= entry->GetDataSize();
1105 return entry.forget();
1108 void imgCacheQueue::Refresh() {
1109 // Resort the list. This is an O(3 * n) operation and best avoided
1110 // if possible.
1111 std::make_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1112 mDirty = false;
1115 void imgCacheQueue::MarkDirty() { mDirty = true; }
1117 bool imgCacheQueue::IsDirty() { return mDirty; }
1119 uint32_t imgCacheQueue::GetNumElements() const { return mQueue.Length(); }
1121 bool imgCacheQueue::Contains(imgCacheEntry* aEntry) const {
1122 return mQueue.Contains(aEntry);
1125 imgCacheQueue::iterator imgCacheQueue::begin() { return mQueue.begin(); }
1127 imgCacheQueue::const_iterator imgCacheQueue::begin() const {
1128 return mQueue.begin();
1131 imgCacheQueue::iterator imgCacheQueue::end() { return mQueue.end(); }
1133 imgCacheQueue::const_iterator imgCacheQueue::end() const {
1134 return mQueue.end();
1137 nsresult imgLoader::CreateNewProxyForRequest(
1138 imgRequest* aRequest, nsIURI* aURI, nsILoadGroup* aLoadGroup,
1139 Document* aLoadingDocument, imgINotificationObserver* aObserver,
1140 nsLoadFlags aLoadFlags, imgRequestProxy** _retval) {
1141 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::CreateNewProxyForRequest",
1142 "imgRequest", aRequest);
1144 /* XXX If we move decoding onto separate threads, we should save off the
1145 calling thread here and pass it off to |proxyRequest| so that it call
1146 proxy calls to |aObserver|.
1149 RefPtr<imgRequestProxy> proxyRequest = new imgRequestProxy();
1151 /* It is important to call |SetLoadFlags()| before calling |Init()| because
1152 |Init()| adds the request to the loadgroup.
1154 proxyRequest->SetLoadFlags(aLoadFlags);
1156 // init adds itself to imgRequest's list of observers
1157 nsresult rv = proxyRequest->Init(aRequest, aLoadGroup, aLoadingDocument, aURI,
1158 aObserver);
1159 if (NS_WARN_IF(NS_FAILED(rv))) {
1160 return rv;
1163 proxyRequest.forget(_retval);
1164 return NS_OK;
1167 class imgCacheExpirationTracker final
1168 : public nsExpirationTracker<imgCacheEntry, 3> {
1169 enum { TIMEOUT_SECONDS = 10 };
1171 public:
1172 imgCacheExpirationTracker();
1174 protected:
1175 void NotifyExpired(imgCacheEntry* entry) override;
1178 imgCacheExpirationTracker::imgCacheExpirationTracker()
1179 : nsExpirationTracker<imgCacheEntry, 3>(TIMEOUT_SECONDS * 1000,
1180 "imgCacheExpirationTracker") {}
1182 void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry* entry) {
1183 // Hold on to a reference to this entry, because the expiration tracker
1184 // mechanism doesn't.
1185 RefPtr<imgCacheEntry> kungFuDeathGrip(entry);
1187 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1188 RefPtr<imgRequest> req = entry->GetRequest();
1189 if (req) {
1190 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheExpirationTracker::NotifyExpired",
1191 "entry", req->CacheKey().URI());
1195 // We can be called multiple times on the same entry. Don't do work multiple
1196 // times.
1197 if (!entry->Evicted()) {
1198 entry->Loader()->RemoveFromCache(entry);
1201 entry->Loader()->VerifyCacheSizes();
1204 ///////////////////////////////////////////////////////////////////////////////
1205 // imgLoader
1206 ///////////////////////////////////////////////////////////////////////////////
1208 double imgLoader::sCacheTimeWeight;
1209 uint32_t imgLoader::sCacheMaxSize;
1210 imgMemoryReporter* imgLoader::sMemReporter;
1212 NS_IMPL_ISUPPORTS(imgLoader, imgILoader, nsIContentSniffer, imgICache,
1213 nsISupportsWeakReference, nsIObserver)
1215 static imgLoader* gNormalLoader = nullptr;
1216 static imgLoader* gPrivateBrowsingLoader = nullptr;
1218 /* static */
1219 already_AddRefed<imgLoader> imgLoader::CreateImageLoader() {
1220 // In some cases, such as xpctests, XPCOM modules are not automatically
1221 // initialized. We need to make sure that our module is initialized before
1222 // we hand out imgLoader instances and code starts using them.
1223 mozilla::image::EnsureModuleInitialized();
1225 RefPtr<imgLoader> loader = new imgLoader();
1226 loader->Init();
1228 return loader.forget();
1231 imgLoader* imgLoader::NormalLoader() {
1232 if (!gNormalLoader) {
1233 gNormalLoader = CreateImageLoader().take();
1235 return gNormalLoader;
1238 imgLoader* imgLoader::PrivateBrowsingLoader() {
1239 if (!gPrivateBrowsingLoader) {
1240 gPrivateBrowsingLoader = CreateImageLoader().take();
1241 gPrivateBrowsingLoader->RespectPrivacyNotifications();
1243 return gPrivateBrowsingLoader;
1246 imgLoader::imgLoader()
1247 : mUncachedImagesMutex("imgLoader::UncachedImages"),
1248 mRespectPrivacy(false) {
1249 sMemReporter->AddRef();
1250 sMemReporter->RegisterLoader(this);
1253 imgLoader::~imgLoader() {
1254 ClearImageCache();
1256 // If there are any of our imgRequest's left they are in the uncached
1257 // images set, so clear their pointer to us.
1258 MutexAutoLock lock(mUncachedImagesMutex);
1259 for (RefPtr<imgRequest> req : mUncachedImages) {
1260 req->ClearLoader();
1263 sMemReporter->UnregisterLoader(this);
1264 sMemReporter->Release();
1267 void imgLoader::VerifyCacheSizes() {
1268 #ifdef DEBUG
1269 if (!mCacheTracker) {
1270 return;
1273 uint32_t cachesize = mCache.Count();
1274 uint32_t queuesize = mCacheQueue.GetNumElements();
1275 uint32_t trackersize = 0;
1276 for (nsExpirationTracker<imgCacheEntry, 3>::Iterator it(mCacheTracker.get());
1277 it.Next();) {
1278 trackersize++;
1280 MOZ_ASSERT(queuesize == trackersize, "Queue and tracker sizes out of sync!");
1281 MOZ_ASSERT(queuesize <= cachesize, "Queue has more elements than cache!");
1282 #endif
1285 void imgLoader::GlobalInit() {
1286 sCacheTimeWeight = StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
1287 int32_t cachesize = StaticPrefs::image_cache_size_AtStartup();
1288 sCacheMaxSize = cachesize > 0 ? cachesize : 0;
1290 sMemReporter = new imgMemoryReporter();
1291 RegisterStrongAsyncMemoryReporter(sMemReporter);
1292 RegisterImagesContentUsedUncompressedDistinguishedAmount(
1293 imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount);
1296 void imgLoader::ShutdownMemoryReporter() {
1297 UnregisterImagesContentUsedUncompressedDistinguishedAmount();
1298 UnregisterStrongMemoryReporter(sMemReporter);
1301 nsresult imgLoader::InitCache() {
1302 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
1303 if (!os) {
1304 return NS_ERROR_FAILURE;
1307 os->AddObserver(this, "memory-pressure", false);
1308 os->AddObserver(this, "chrome-flush-caches", false);
1309 os->AddObserver(this, "last-pb-context-exited", false);
1310 os->AddObserver(this, "profile-before-change", false);
1311 os->AddObserver(this, "xpcom-shutdown", false);
1313 mCacheTracker = MakeUnique<imgCacheExpirationTracker>();
1315 return NS_OK;
1318 nsresult imgLoader::Init() {
1319 InitCache();
1321 return NS_OK;
1324 NS_IMETHODIMP
1325 imgLoader::RespectPrivacyNotifications() {
1326 mRespectPrivacy = true;
1327 return NS_OK;
1330 NS_IMETHODIMP
1331 imgLoader::Observe(nsISupports* aSubject, const char* aTopic,
1332 const char16_t* aData) {
1333 if (strcmp(aTopic, "memory-pressure") == 0) {
1334 MinimizeCache();
1335 } else if (strcmp(aTopic, "chrome-flush-caches") == 0) {
1336 MinimizeCache();
1337 ClearImageCache({ClearOption::ChromeOnly});
1338 } else if (strcmp(aTopic, "last-pb-context-exited") == 0) {
1339 if (mRespectPrivacy) {
1340 ClearImageCache();
1342 } else if (strcmp(aTopic, "profile-before-change") == 0) {
1343 mCacheTracker = nullptr;
1344 } else if (strcmp(aTopic, "xpcom-shutdown") == 0) {
1345 mCacheTracker = nullptr;
1346 ShutdownMemoryReporter();
1348 } else {
1349 // (Nothing else should bring us here)
1350 MOZ_ASSERT(0, "Invalid topic received");
1353 return NS_OK;
1356 NS_IMETHODIMP
1357 imgLoader::ClearCache(bool chrome) {
1358 if (XRE_IsParentProcess()) {
1359 bool privateLoader = this == gPrivateBrowsingLoader;
1360 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1361 Unused << cp->SendClearImageCache(privateLoader, chrome);
1364 ClearOptions options;
1365 if (chrome) {
1366 options += ClearOption::ChromeOnly;
1368 return ClearImageCache(options);
1371 NS_IMETHODIMP
1372 imgLoader::RemoveEntriesFromPrincipalInAllProcesses(nsIPrincipal* aPrincipal) {
1373 if (!XRE_IsParentProcess()) {
1374 return NS_ERROR_NOT_AVAILABLE;
1377 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1378 Unused << cp->SendClearImageCacheFromPrincipal(aPrincipal);
1381 imgLoader* loader;
1382 if (aPrincipal->OriginAttributesRef().mPrivateBrowsingId ==
1383 nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID) {
1384 loader = imgLoader::NormalLoader();
1385 } else {
1386 loader = imgLoader::PrivateBrowsingLoader();
1389 return loader->RemoveEntriesInternal(aPrincipal, nullptr);
1392 NS_IMETHODIMP
1393 imgLoader::RemoveEntriesFromBaseDomainInAllProcesses(
1394 const nsACString& aBaseDomain) {
1395 if (!XRE_IsParentProcess()) {
1396 return NS_ERROR_NOT_AVAILABLE;
1399 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1400 Unused << cp->SendClearImageCacheFromBaseDomain(aBaseDomain);
1403 return RemoveEntriesInternal(nullptr, &aBaseDomain);
1406 nsresult imgLoader::RemoveEntriesInternal(nsIPrincipal* aPrincipal,
1407 const nsACString* aBaseDomain) {
1408 // Can only clear by either principal or base domain.
1409 if ((!aPrincipal && !aBaseDomain) || (aPrincipal && aBaseDomain)) {
1410 return NS_ERROR_INVALID_ARG;
1413 nsAutoString origin;
1414 if (aPrincipal) {
1415 nsresult rv = nsContentUtils::GetUTFOrigin(aPrincipal, origin);
1416 if (NS_WARN_IF(NS_FAILED(rv))) {
1417 return rv;
1421 nsCOMPtr<nsIEffectiveTLDService> tldService;
1422 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1424 // For base domain we only clear the non-chrome cache.
1425 for (const auto& entry : mCache) {
1426 const auto& key = entry.GetKey();
1428 const bool shouldRemove = [&] {
1429 if (aPrincipal) {
1430 if (key.OriginAttributesRef() !=
1431 BasePrincipal::Cast(aPrincipal)->OriginAttributesRef()) {
1432 return false;
1435 nsAutoString imageOrigin;
1436 nsresult rv = nsContentUtils::GetUTFOrigin(key.URI(), imageOrigin);
1437 if (NS_WARN_IF(NS_FAILED(rv))) {
1438 return false;
1441 return imageOrigin == origin;
1444 if (!aBaseDomain) {
1445 return false;
1447 // Clear by baseDomain.
1448 nsAutoCString host;
1449 nsresult rv = key.URI()->GetHost(host);
1450 if (NS_FAILED(rv) || host.IsEmpty()) {
1451 return false;
1454 if (!tldService) {
1455 tldService = do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
1457 if (NS_WARN_IF(!tldService)) {
1458 return false;
1461 bool hasRootDomain = false;
1462 rv = tldService->HasRootDomain(host, *aBaseDomain, &hasRootDomain);
1463 if (NS_SUCCEEDED(rv) && hasRootDomain) {
1464 return true;
1467 // If we don't get a direct base domain match, also check for cache of
1468 // third parties partitioned under aBaseDomain.
1470 // The isolation key is either just the base domain, or an origin suffix
1471 // which contains the partitionKey holding the baseDomain.
1473 if (key.IsolationKeyRef().Equals(*aBaseDomain)) {
1474 return true;
1477 // The isolation key does not match the given base domain. It may be an
1478 // origin suffix. Parse it into origin attributes.
1479 OriginAttributes attrs;
1480 if (!attrs.PopulateFromSuffix(key.IsolationKeyRef())) {
1481 // Key is not an origin suffix.
1482 return false;
1485 return StoragePrincipalHelper::PartitionKeyHasBaseDomain(
1486 attrs.mPartitionKey, *aBaseDomain);
1487 }();
1489 if (shouldRemove) {
1490 entriesToBeRemoved.AppendElement(entry.GetData());
1494 for (auto& entry : entriesToBeRemoved) {
1495 if (!RemoveFromCache(entry)) {
1496 NS_WARNING(
1497 "Couldn't remove an entry from the cache in "
1498 "RemoveEntriesInternal()\n");
1502 return NS_OK;
1505 NS_IMETHODIMP
1506 imgLoader::RemoveEntry(nsIURI* aURI, Document* aDoc) {
1507 if (aURI) {
1508 OriginAttributes attrs;
1509 if (aDoc) {
1510 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1511 if (principal) {
1512 attrs = principal->OriginAttributesRef();
1516 ImageCacheKey key(aURI, attrs, aDoc);
1517 if (RemoveFromCache(key)) {
1518 return NS_OK;
1521 return NS_ERROR_NOT_AVAILABLE;
1524 NS_IMETHODIMP
1525 imgLoader::FindEntryProperties(nsIURI* uri, Document* aDoc,
1526 nsIProperties** _retval) {
1527 *_retval = nullptr;
1529 OriginAttributes attrs;
1530 if (aDoc) {
1531 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1532 if (principal) {
1533 attrs = principal->OriginAttributesRef();
1537 ImageCacheKey key(uri, attrs, aDoc);
1538 RefPtr<imgCacheEntry> entry;
1539 if (mCache.Get(key, getter_AddRefs(entry)) && entry) {
1540 if (mCacheTracker && entry->HasNoProxies()) {
1541 mCacheTracker->MarkUsed(entry);
1544 RefPtr<imgRequest> request = entry->GetRequest();
1545 if (request) {
1546 nsCOMPtr<nsIProperties> properties = request->Properties();
1547 properties.forget(_retval);
1551 return NS_OK;
1554 NS_IMETHODIMP_(void)
1555 imgLoader::ClearCacheForControlledDocument(Document* aDoc) {
1556 MOZ_ASSERT(aDoc);
1557 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1558 for (const auto& entry : mCache) {
1559 const auto& key = entry.GetKey();
1560 if (key.ControlledDocument() == aDoc) {
1561 entriesToBeRemoved.AppendElement(entry.GetData());
1564 for (auto& entry : entriesToBeRemoved) {
1565 if (!RemoveFromCache(entry)) {
1566 NS_WARNING(
1567 "Couldn't remove an entry from the cache in "
1568 "ClearCacheForControlledDocument()\n");
1573 void imgLoader::Shutdown() {
1574 NS_IF_RELEASE(gNormalLoader);
1575 gNormalLoader = nullptr;
1576 NS_IF_RELEASE(gPrivateBrowsingLoader);
1577 gPrivateBrowsingLoader = nullptr;
1580 bool imgLoader::PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* entry) {
1581 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::PutIntoCache", "uri",
1582 aKey.URI());
1584 // Check to see if this request already exists in the cache. If so, we'll
1585 // replace the old version.
1586 RefPtr<imgCacheEntry> tmpCacheEntry;
1587 if (mCache.Get(aKey, getter_AddRefs(tmpCacheEntry)) && tmpCacheEntry) {
1588 MOZ_LOG(
1589 gImgLog, LogLevel::Debug,
1590 ("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
1591 nullptr));
1592 RefPtr<imgRequest> tmpRequest = tmpCacheEntry->GetRequest();
1594 // If it already exists, and we're putting the same key into the cache, we
1595 // should remove the old version.
1596 MOZ_LOG(gImgLog, LogLevel::Debug,
1597 ("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
1598 nullptr));
1600 RemoveFromCache(aKey);
1601 } else {
1602 MOZ_LOG(gImgLog, LogLevel::Debug,
1603 ("[this=%p] imgLoader::PutIntoCache --"
1604 " Element NOT already in the cache",
1605 nullptr));
1608 mCache.InsertOrUpdate(aKey, RefPtr{entry});
1610 // We can be called to resurrect an evicted entry.
1611 if (entry->Evicted()) {
1612 entry->SetEvicted(false);
1615 // If we're resurrecting an entry with no proxies, put it back in the
1616 // tracker and queue.
1617 if (entry->HasNoProxies()) {
1618 nsresult addrv = NS_OK;
1620 if (mCacheTracker) {
1621 addrv = mCacheTracker->AddObject(entry);
1624 if (NS_SUCCEEDED(addrv)) {
1625 mCacheQueue.Push(entry);
1629 RefPtr<imgRequest> request = entry->GetRequest();
1630 request->SetIsInCache(true);
1631 RemoveFromUncachedImages(request);
1633 return true;
1636 bool imgLoader::SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry) {
1637 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasNoProxies", "uri",
1638 aRequest->CacheKey().URI());
1640 aEntry->SetHasNoProxies(true);
1642 if (aEntry->Evicted()) {
1643 return false;
1646 nsresult addrv = NS_OK;
1648 if (mCacheTracker) {
1649 addrv = mCacheTracker->AddObject(aEntry);
1652 if (NS_SUCCEEDED(addrv)) {
1653 mCacheQueue.Push(aEntry);
1656 return true;
1659 bool imgLoader::SetHasProxies(imgRequest* aRequest) {
1660 VerifyCacheSizes();
1662 const ImageCacheKey& key = aRequest->CacheKey();
1664 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasProxies", "uri",
1665 key.URI());
1667 RefPtr<imgCacheEntry> entry;
1668 if (mCache.Get(key, getter_AddRefs(entry)) && entry) {
1669 // Make sure the cache entry is for the right request
1670 RefPtr<imgRequest> entryRequest = entry->GetRequest();
1671 if (entryRequest == aRequest && entry->HasNoProxies()) {
1672 mCacheQueue.Remove(entry);
1674 if (mCacheTracker) {
1675 mCacheTracker->RemoveObject(entry);
1678 entry->SetHasNoProxies(false);
1680 return true;
1684 return false;
1687 void imgLoader::CacheEntriesChanged(int32_t aSizeDiff /* = 0 */) {
1688 // We only need to dirty the queue if there is any sorting
1689 // taking place. Empty or single-entry lists can't become
1690 // dirty.
1691 if (mCacheQueue.GetNumElements() > 1) {
1692 mCacheQueue.MarkDirty();
1694 mCacheQueue.UpdateSize(aSizeDiff);
1697 void imgLoader::CheckCacheLimits() {
1698 if (mCacheQueue.GetNumElements() == 0) {
1699 NS_ASSERTION(mCacheQueue.GetSize() == 0,
1700 "imgLoader::CheckCacheLimits -- incorrect cache size");
1703 // Remove entries from the cache until we're back at our desired max size.
1704 while (mCacheQueue.GetSize() > sCacheMaxSize) {
1705 // Remove the first entry in the queue.
1706 RefPtr<imgCacheEntry> entry(mCacheQueue.Pop());
1708 NS_ASSERTION(entry, "imgLoader::CheckCacheLimits -- NULL entry pointer");
1710 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1711 RefPtr<imgRequest> req = entry->GetRequest();
1712 if (req) {
1713 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::CheckCacheLimits",
1714 "entry", req->CacheKey().URI());
1718 if (entry) {
1719 // We just popped this entry from the queue, so pass AlreadyRemoved
1720 // to avoid searching the queue again in RemoveFromCache.
1721 RemoveFromCache(entry, QueueState::AlreadyRemoved);
1726 bool imgLoader::ValidateRequestWithNewChannel(
1727 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1728 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1729 imgINotificationObserver* aObserver, Document* aLoadingDocument,
1730 uint64_t aInnerWindowId, nsLoadFlags aLoadFlags,
1731 nsContentPolicyType aLoadPolicyType, imgRequestProxy** aProxyRequest,
1732 nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode, bool aLinkPreload,
1733 uint64_t aEarlyHintPreloaderId, bool* aNewChannelCreated) {
1734 // now we need to insert a new channel request object in between the real
1735 // request and the proxy that basically delays loading the image until it
1736 // gets a 304 or figures out that this needs to be a new request
1738 nsresult rv;
1740 // If we're currently in the middle of validating this request, just hand
1741 // back a proxy to it; the required work will be done for us.
1742 if (imgCacheValidator* validator = request->GetValidator()) {
1743 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
1744 aObserver, aLoadFlags, aProxyRequest);
1745 if (NS_FAILED(rv)) {
1746 return false;
1749 if (*aProxyRequest) {
1750 imgRequestProxy* proxy = static_cast<imgRequestProxy*>(*aProxyRequest);
1752 // We will send notifications from imgCacheValidator::OnStartRequest().
1753 // In the mean time, we must defer notifications because we are added to
1754 // the imgRequest's proxy list, and we can get extra notifications
1755 // resulting from methods such as StartDecoding(). See bug 579122.
1756 proxy->MarkValidating();
1758 if (aLinkPreload) {
1759 MOZ_ASSERT(aLoadingDocument);
1760 proxy->PrioritizeAsPreload();
1761 auto preloadKey = PreloadHashKey::CreateAsImage(
1762 aURI, aTriggeringPrincipal, aCORSMode);
1763 proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
1766 // Attach the proxy without notifying
1767 validator->AddProxy(proxy);
1770 return true;
1772 // We will rely on Necko to cache this request when it's possible, and to
1773 // tell imgCacheValidator::OnStartRequest whether the request came from its
1774 // cache.
1775 nsCOMPtr<nsIChannel> newChannel;
1776 bool forcePrincipalCheck;
1777 rv =
1778 NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
1779 aInitialDocumentURI, aCORSMode, aReferrerInfo, aLoadGroup,
1780 aLoadFlags, aLoadPolicyType, aTriggeringPrincipal,
1781 aLoadingDocument, mRespectPrivacy, aEarlyHintPreloaderId);
1782 if (NS_FAILED(rv)) {
1783 return false;
1786 if (aNewChannelCreated) {
1787 *aNewChannelCreated = true;
1790 RefPtr<imgRequestProxy> req;
1791 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
1792 aObserver, aLoadFlags, getter_AddRefs(req));
1793 if (NS_FAILED(rv)) {
1794 return false;
1797 // Make sure that OnStatus/OnProgress calls have the right request set...
1798 RefPtr<nsProgressNotificationProxy> progressproxy =
1799 new nsProgressNotificationProxy(newChannel, req);
1800 if (!progressproxy) {
1801 return false;
1804 RefPtr<imgCacheValidator> hvc =
1805 new imgCacheValidator(progressproxy, this, request, aLoadingDocument,
1806 aInnerWindowId, forcePrincipalCheck);
1808 // Casting needed here to get past multiple inheritance.
1809 nsCOMPtr<nsIStreamListener> listener =
1810 do_QueryInterface(static_cast<nsIThreadRetargetableStreamListener*>(hvc));
1811 NS_ENSURE_TRUE(listener, false);
1813 // We must set the notification callbacks before setting up the
1814 // CORS listener, because that's also interested inthe
1815 // notification callbacks.
1816 newChannel->SetNotificationCallbacks(hvc);
1818 request->SetValidator(hvc);
1820 // We will send notifications from imgCacheValidator::OnStartRequest().
1821 // In the mean time, we must defer notifications because we are added to
1822 // the imgRequest's proxy list, and we can get extra notifications
1823 // resulting from methods such as StartDecoding(). See bug 579122.
1824 req->MarkValidating();
1826 if (aLinkPreload) {
1827 MOZ_ASSERT(aLoadingDocument);
1828 req->PrioritizeAsPreload();
1829 auto preloadKey =
1830 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, aCORSMode);
1831 req->NotifyOpen(preloadKey, aLoadingDocument, true);
1834 // Add the proxy without notifying
1835 hvc->AddProxy(req);
1837 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
1838 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
1839 aLoadGroup);
1840 rv = newChannel->AsyncOpen(listener);
1841 if (NS_WARN_IF(NS_FAILED(rv))) {
1842 req->CancelAndForgetObserver(rv);
1843 // This will notify any current or future <link preload> tags. Pass the
1844 // non-open channel so that we can read loadinfo and referrer info of that
1845 // channel.
1846 req->NotifyStart(newChannel);
1847 // Use the non-channel overload of this method to force the notification to
1848 // happen. The preload request has not been assigned a channel.
1849 req->NotifyStop(rv);
1850 return false;
1853 req.forget(aProxyRequest);
1854 return true;
1857 void imgLoader::NotifyObserversForCachedImage(
1858 imgCacheEntry* aEntry, imgRequest* request, nsIURI* aURI,
1859 nsIReferrerInfo* aReferrerInfo, Document* aLoadingDocument,
1860 nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode,
1861 uint64_t aEarlyHintPreloaderId) {
1862 if (aEntry->HasNotified()) {
1863 return;
1866 nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
1868 if (!obsService->HasObservers("http-on-image-cache-response")) {
1869 return;
1872 aEntry->SetHasNotified();
1874 nsCOMPtr<nsIChannel> newChannel;
1875 bool forcePrincipalCheck;
1876 nsresult rv = NewImageChannel(
1877 getter_AddRefs(newChannel), &forcePrincipalCheck, aURI, nullptr,
1878 aCORSMode, aReferrerInfo, nullptr, 0,
1879 nsIContentPolicy::TYPE_INTERNAL_IMAGE, aTriggeringPrincipal,
1880 aLoadingDocument, mRespectPrivacy, aEarlyHintPreloaderId);
1881 if (NS_FAILED(rv)) {
1882 return;
1885 RefPtr<HttpBaseChannel> httpBaseChannel = do_QueryObject(newChannel);
1886 if (httpBaseChannel) {
1887 httpBaseChannel->SetDummyChannelForImageCache();
1888 newChannel->SetContentType(nsDependentCString(request->GetMimeType()));
1889 RefPtr<mozilla::image::Image> image = request->GetImage();
1890 if (image) {
1891 newChannel->SetContentLength(aEntry->GetDataSize());
1893 obsService->NotifyObservers(newChannel, "http-on-image-cache-response",
1894 nullptr);
1898 bool imgLoader::ValidateEntry(
1899 imgCacheEntry* aEntry, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1900 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1901 imgINotificationObserver* aObserver, Document* aLoadingDocument,
1902 nsLoadFlags aLoadFlags, nsContentPolicyType aLoadPolicyType,
1903 bool aCanMakeNewChannel, bool* aNewChannelCreated,
1904 imgRequestProxy** aProxyRequest, nsIPrincipal* aTriggeringPrincipal,
1905 CORSMode aCORSMode, bool aLinkPreload, uint64_t aEarlyHintPreloaderId) {
1906 LOG_SCOPE(gImgLog, "imgLoader::ValidateEntry");
1908 // If the expiration time is zero, then the request has not gotten far enough
1909 // to know when it will expire, or we know it will never expire (see
1910 // nsContentUtils::GetSubresourceCacheValidationInfo).
1911 uint32_t expiryTime = aEntry->GetExpiryTime();
1912 bool hasExpired = expiryTime && expiryTime <= SecondsFromPRTime(PR_Now());
1914 nsresult rv;
1916 // Special treatment for file URLs - aEntry has expired if file has changed
1917 nsCOMPtr<nsIFileURL> fileUrl(do_QueryInterface(aURI));
1918 if (fileUrl) {
1919 uint32_t lastModTime = aEntry->GetLoadTime();
1921 nsCOMPtr<nsIFile> theFile;
1922 rv = fileUrl->GetFile(getter_AddRefs(theFile));
1923 if (NS_SUCCEEDED(rv)) {
1924 PRTime fileLastMod;
1925 rv = theFile->GetLastModifiedTime(&fileLastMod);
1926 if (NS_SUCCEEDED(rv)) {
1927 // nsIFile uses millisec, NSPR usec
1928 fileLastMod *= 1000;
1929 hasExpired = SecondsFromPRTime((PRTime)fileLastMod) > lastModTime;
1934 RefPtr<imgRequest> request(aEntry->GetRequest());
1936 if (!request) {
1937 return false;
1940 if (!ValidateSecurityInfo(request, aEntry->ForcePrincipalCheck(), aCORSMode,
1941 aTriggeringPrincipal, aLoadingDocument,
1942 aLoadPolicyType)) {
1943 return false;
1946 // data URIs are immutable and by their nature can't leak data, so we can
1947 // just return true in that case. Doing so would mean that shift-reload
1948 // doesn't reload data URI documents/images though (which is handy for
1949 // debugging during gecko development) so we make an exception in that case.
1950 if (aURI->SchemeIs("data") && !(aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE)) {
1951 return true;
1954 bool validateRequest = false;
1956 if (!request->CanReuseWithoutValidation(aLoadingDocument)) {
1957 // If we would need to revalidate this entry, but we're being told to
1958 // bypass the cache, we don't allow this entry to be used.
1959 if (aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE) {
1960 return false;
1963 if (MOZ_UNLIKELY(ChaosMode::isActive(ChaosFeature::ImageCache))) {
1964 if (ChaosMode::randomUint32LessThan(4) < 1) {
1965 return false;
1969 // Determine whether the cache aEntry must be revalidated...
1970 validateRequest = ShouldRevalidateEntry(aEntry, aLoadFlags, hasExpired);
1972 MOZ_LOG(gImgLog, LogLevel::Debug,
1973 ("imgLoader::ValidateEntry validating cache entry. "
1974 "validateRequest = %d",
1975 validateRequest));
1976 } else if (!aLoadingDocument && MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1977 MOZ_LOG(gImgLog, LogLevel::Debug,
1978 ("imgLoader::ValidateEntry BYPASSING cache validation for %s "
1979 "because of NULL loading document",
1980 aURI->GetSpecOrDefault().get()));
1983 // If the original request is still transferring don't kick off a validation
1984 // network request because it is a bit silly to issue a validation request if
1985 // the original request hasn't even finished yet. So just return true
1986 // indicating the caller can create a new proxy for the request and use it as
1987 // is.
1988 // This is an optimization but it's also required for correctness. If we don't
1989 // do this then when firing the load complete notification for the original
1990 // request that can unblock load for the document and then spin the event loop
1991 // (see the stack in bug 1641682) which then the OnStartRequest for the
1992 // validation request can fire which can call UpdateProxies and can sync
1993 // notify on the progress tracker about all existing state, which includes
1994 // load complete, so we fire a second load complete notification for the
1995 // image.
1996 // In addition, we want to validate if the original request encountered
1997 // an error for two reasons. The first being if the error was a network error
1998 // then trying to re-fetch the image might succeed. The second is more
1999 // complicated. We decide if we should fire the load or error event for img
2000 // elements depending on if the image has error in its status at the time when
2001 // the load complete notification is received, and we set error status on an
2002 // image if it encounters a network error or a decode error with no real way
2003 // to tell them apart. So if we load an image that will produce a decode error
2004 // the first time we will usually fire the load event, and then decode enough
2005 // to encounter the decode error and set the error status on the image. The
2006 // next time we reference the image in the same document the load complete
2007 // notification is replayed and this time the error status from the decode is
2008 // already present so we fire the error event instead of the load event. This
2009 // is a bug (bug 1645576) that we should fix. In order to avoid that bug in
2010 // some cases (specifically the cases when we hit this code and try to
2011 // validate the request) we make sure to validate. This avoids the bug because
2012 // when the load complete notification arrives the proxy is marked as
2013 // validating so it lies about its status and returns nothing.
2014 bool requestComplete = false;
2015 RefPtr<ProgressTracker> tracker;
2016 RefPtr<mozilla::image::Image> image = request->GetImage();
2017 if (image) {
2018 tracker = image->GetProgressTracker();
2019 } else {
2020 tracker = request->GetProgressTracker();
2022 if (tracker) {
2023 if (tracker->GetProgress() & (FLAG_LOAD_COMPLETE | FLAG_HAS_ERROR)) {
2024 requestComplete = true;
2027 if (!requestComplete) {
2028 return true;
2031 if (validateRequest && aCanMakeNewChannel) {
2032 LOG_SCOPE(gImgLog, "imgLoader::ValidateRequest |cache hit| must validate");
2034 uint64_t innerWindowID =
2035 aLoadingDocument ? aLoadingDocument->InnerWindowID() : 0;
2036 return ValidateRequestWithNewChannel(
2037 request, aURI, aInitialDocumentURI, aReferrerInfo, aLoadGroup,
2038 aObserver, aLoadingDocument, innerWindowID, aLoadFlags, aLoadPolicyType,
2039 aProxyRequest, aTriggeringPrincipal, aCORSMode, aLinkPreload,
2040 aEarlyHintPreloaderId, aNewChannelCreated);
2043 if (!validateRequest) {
2044 NotifyObserversForCachedImage(aEntry, request, aURI, aReferrerInfo,
2045 aLoadingDocument, aTriggeringPrincipal,
2046 aCORSMode, aEarlyHintPreloaderId);
2049 return !validateRequest;
2052 bool imgLoader::RemoveFromCache(const ImageCacheKey& aKey) {
2053 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache", "uri",
2054 aKey.URI());
2055 RefPtr<imgCacheEntry> entry;
2056 mCache.Remove(aKey, getter_AddRefs(entry));
2057 if (entry) {
2058 MOZ_ASSERT(!entry->Evicted(), "Evicting an already-evicted cache entry!");
2060 // Entries with no proxies are in the tracker.
2061 if (entry->HasNoProxies()) {
2062 if (mCacheTracker) {
2063 mCacheTracker->RemoveObject(entry);
2065 mCacheQueue.Remove(entry);
2068 entry->SetEvicted(true);
2070 RefPtr<imgRequest> request = entry->GetRequest();
2071 request->SetIsInCache(false);
2072 AddToUncachedImages(request);
2074 return true;
2076 return false;
2079 bool imgLoader::RemoveFromCache(imgCacheEntry* entry, QueueState aQueueState) {
2080 LOG_STATIC_FUNC(gImgLog, "imgLoader::RemoveFromCache entry");
2082 RefPtr<imgRequest> request = entry->GetRequest();
2083 if (request) {
2084 const ImageCacheKey& key = request->CacheKey();
2085 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache",
2086 "entry's uri", key.URI());
2088 mCache.Remove(key);
2090 if (entry->HasNoProxies()) {
2091 LOG_STATIC_FUNC(gImgLog,
2092 "imgLoader::RemoveFromCache removing from tracker");
2093 if (mCacheTracker) {
2094 mCacheTracker->RemoveObject(entry);
2096 // Only search the queue to remove the entry if its possible it might
2097 // be in the queue. If we know its not in the queue this would be
2098 // wasted work.
2099 MOZ_ASSERT_IF(aQueueState == QueueState::AlreadyRemoved,
2100 !mCacheQueue.Contains(entry));
2101 if (aQueueState == QueueState::MaybeExists) {
2102 mCacheQueue.Remove(entry);
2106 entry->SetEvicted(true);
2107 request->SetIsInCache(false);
2108 AddToUncachedImages(request);
2110 return true;
2113 return false;
2116 nsresult imgLoader::ClearImageCache(ClearOptions aOptions) {
2117 const bool chromeOnly = aOptions.contains(ClearOption::ChromeOnly);
2118 const auto ShouldRemove = [&](imgCacheEntry* aEntry) {
2119 if (chromeOnly) {
2120 // TODO: Consider also removing "resource://" etc?
2121 RefPtr<imgRequest> request = aEntry->GetRequest();
2122 if (!request || !request->CacheKey().URI()->SchemeIs("chrome")) {
2123 return false;
2126 return true;
2128 if (aOptions.contains(ClearOption::UnusedOnly)) {
2129 LOG_STATIC_FUNC(gImgLog, "imgLoader::ClearImageCache queue");
2130 // We have to make a temporary, since RemoveFromCache removes the element
2131 // from the queue, invalidating iterators.
2132 nsTArray<RefPtr<imgCacheEntry>> entries(mCacheQueue.GetNumElements());
2133 for (auto& entry : mCacheQueue) {
2134 if (ShouldRemove(entry)) {
2135 entries.AppendElement(entry);
2139 // Iterate in reverse order to minimize array copying.
2140 for (auto& entry : entries) {
2141 if (!RemoveFromCache(entry)) {
2142 return NS_ERROR_FAILURE;
2146 MOZ_ASSERT(chromeOnly || mCacheQueue.GetNumElements() == 0);
2147 return NS_OK;
2150 LOG_STATIC_FUNC(gImgLog, "imgLoader::ClearImageCache table");
2151 // We have to make a temporary, since RemoveFromCache removes the element
2152 // from the queue, invalidating iterators.
2153 const auto entries =
2154 ToTArray<nsTArray<RefPtr<imgCacheEntry>>>(mCache.Values());
2155 for (const auto& entry : entries) {
2156 if (!ShouldRemove(entry)) {
2157 continue;
2159 if (!RemoveFromCache(entry)) {
2160 return NS_ERROR_FAILURE;
2163 MOZ_ASSERT(chromeOnly || mCache.IsEmpty());
2164 return NS_OK;
2167 void imgLoader::AddToUncachedImages(imgRequest* aRequest) {
2168 MutexAutoLock lock(mUncachedImagesMutex);
2169 mUncachedImages.Insert(aRequest);
2172 void imgLoader::RemoveFromUncachedImages(imgRequest* aRequest) {
2173 MutexAutoLock lock(mUncachedImagesMutex);
2174 mUncachedImages.Remove(aRequest);
2177 #define LOAD_FLAGS_CACHE_MASK \
2178 (nsIRequest::LOAD_BYPASS_CACHE | nsIRequest::LOAD_FROM_CACHE)
2180 #define LOAD_FLAGS_VALIDATE_MASK \
2181 (nsIRequest::VALIDATE_ALWAYS | nsIRequest::VALIDATE_NEVER | \
2182 nsIRequest::VALIDATE_ONCE_PER_SESSION)
2184 NS_IMETHODIMP
2185 imgLoader::LoadImageXPCOM(
2186 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2187 nsIPrincipal* aTriggeringPrincipal, nsILoadGroup* aLoadGroup,
2188 imgINotificationObserver* aObserver, Document* aLoadingDocument,
2189 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
2190 nsContentPolicyType aContentPolicyType, imgIRequest** _retval) {
2191 // Optional parameter, so defaults to 0 (== TYPE_INVALID)
2192 if (!aContentPolicyType) {
2193 aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE;
2195 imgRequestProxy* proxy;
2196 nsresult rv =
2197 LoadImage(aURI, aInitialDocumentURI, aReferrerInfo, aTriggeringPrincipal,
2198 0, aLoadGroup, aObserver, aLoadingDocument, aLoadingDocument,
2199 aLoadFlags, aCacheKey, aContentPolicyType, u""_ns,
2200 /* aUseUrgentStartForChannel */ false, /* aListPreload */ false,
2201 0, &proxy);
2202 *_retval = proxy;
2203 return rv;
2206 static void MakeRequestStaticIfNeeded(
2207 Document* aLoadingDocument, imgRequestProxy** aProxyAboutToGetReturned) {
2208 if (!aLoadingDocument || !aLoadingDocument->IsStaticDocument()) {
2209 return;
2212 if (!*aProxyAboutToGetReturned) {
2213 return;
2216 RefPtr<imgRequestProxy> proxy = dont_AddRef(*aProxyAboutToGetReturned);
2217 *aProxyAboutToGetReturned = nullptr;
2219 RefPtr<imgRequestProxy> staticProxy =
2220 proxy->GetStaticRequest(aLoadingDocument);
2221 if (staticProxy != proxy) {
2222 proxy->CancelAndForgetObserver(NS_BINDING_ABORTED);
2223 proxy = std::move(staticProxy);
2225 proxy.forget(aProxyAboutToGetReturned);
2228 bool imgLoader::IsImageAvailable(nsIURI* aURI,
2229 nsIPrincipal* aTriggeringPrincipal,
2230 CORSMode aCORSMode, Document* aDocument) {
2231 ImageCacheKey key(aURI, aTriggeringPrincipal->OriginAttributesRef(),
2232 aDocument);
2233 RefPtr<imgCacheEntry> entry;
2234 if (!mCache.Get(key, getter_AddRefs(entry)) || !entry) {
2235 return false;
2237 RefPtr<imgRequest> request = entry->GetRequest();
2238 if (!request) {
2239 return false;
2241 if (nsCOMPtr<nsILoadGroup> docLoadGroup = aDocument->GetDocumentLoadGroup()) {
2242 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2243 docLoadGroup->GetLoadFlags(&requestFlags);
2244 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2245 // If we're bypassing the cache, treat the image as not available.
2246 return false;
2249 return ValidateCORSMode(request, false, aCORSMode, aTriggeringPrincipal);
2252 nsresult imgLoader::LoadImage(
2253 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2254 nsIPrincipal* aTriggeringPrincipal, uint64_t aRequestContextID,
2255 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
2256 nsINode* aContext, Document* aLoadingDocument, nsLoadFlags aLoadFlags,
2257 nsISupports* aCacheKey, nsContentPolicyType aContentPolicyType,
2258 const nsAString& initiatorType, bool aUseUrgentStartForChannel,
2259 bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
2260 imgRequestProxy** _retval) {
2261 VerifyCacheSizes();
2263 NS_ASSERTION(aURI, "imgLoader::LoadImage -- NULL URI pointer");
2265 if (!aURI) {
2266 return NS_ERROR_NULL_POINTER;
2269 auto makeStaticIfNeeded = mozilla::MakeScopeExit(
2270 [&] { MakeRequestStaticIfNeeded(aLoadingDocument, _retval); });
2272 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK,
2273 aURI->GetSpecOrDefault());
2275 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::LoadImage", "aURI", aURI);
2277 *_retval = nullptr;
2279 RefPtr<imgRequest> request;
2281 nsresult rv;
2282 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2284 #ifdef DEBUG
2285 bool isPrivate = false;
2287 if (aLoadingDocument) {
2288 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadingDocument);
2289 } else if (aLoadGroup) {
2290 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadGroup);
2292 MOZ_ASSERT(isPrivate == mRespectPrivacy);
2294 if (aLoadingDocument) {
2295 // The given load group should match that of the document if given. If
2296 // that isn't the case, then we need to add more plumbing to ensure we
2297 // block the document as well.
2298 nsCOMPtr<nsILoadGroup> docLoadGroup =
2299 aLoadingDocument->GetDocumentLoadGroup();
2300 MOZ_ASSERT(docLoadGroup == aLoadGroup);
2302 #endif
2304 // Get the default load flags from the loadgroup (if possible)...
2305 if (aLoadGroup) {
2306 aLoadGroup->GetLoadFlags(&requestFlags);
2309 // Merge the default load flags with those passed in via aLoadFlags.
2310 // Currently, *only* the caching, validation and background load flags
2311 // are merged...
2313 // The flags in aLoadFlags take precedence over the default flags!
2315 if (aLoadFlags & LOAD_FLAGS_CACHE_MASK) {
2316 // Override the default caching flags...
2317 requestFlags = (requestFlags & ~LOAD_FLAGS_CACHE_MASK) |
2318 (aLoadFlags & LOAD_FLAGS_CACHE_MASK);
2320 if (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK) {
2321 // Override the default validation flags...
2322 requestFlags = (requestFlags & ~LOAD_FLAGS_VALIDATE_MASK) |
2323 (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK);
2325 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
2326 // Propagate background loading...
2327 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2329 if (aLoadFlags & nsIRequest::LOAD_RECORD_START_REQUEST_DELAY) {
2330 requestFlags |= nsIRequest::LOAD_RECORD_START_REQUEST_DELAY;
2333 if (aLinkPreload) {
2334 // Set background loading if it is <link rel=preload>
2335 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2338 CORSMode corsmode = CORS_NONE;
2339 if (aLoadFlags & imgILoader::LOAD_CORS_ANONYMOUS) {
2340 corsmode = CORS_ANONYMOUS;
2341 } else if (aLoadFlags & imgILoader::LOAD_CORS_USE_CREDENTIALS) {
2342 corsmode = CORS_USE_CREDENTIALS;
2345 // Look in the preloaded images of loading document first.
2346 if (StaticPrefs::network_preload() && !aLinkPreload && aLoadingDocument) {
2347 // All Early Hints preloads are Link preloads, therefore we don't have a
2348 // Early Hints preload here
2349 MOZ_ASSERT(!aEarlyHintPreloaderId);
2350 auto key =
2351 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, corsmode);
2352 if (RefPtr<PreloaderBase> preload =
2353 aLoadingDocument->Preloads().LookupPreload(key)) {
2354 RefPtr<imgRequestProxy> proxy = do_QueryObject(preload);
2355 MOZ_ASSERT(proxy);
2357 MOZ_LOG(gImgLog, LogLevel::Debug,
2358 ("[this=%p] imgLoader::LoadImage -- preloaded [proxy=%p]"
2359 " [document=%p]\n",
2360 this, proxy.get(), aLoadingDocument));
2362 // Removing the preload for this image to be in parity with Chromium. Any
2363 // following regular image request will be reloaded using the regular
2364 // path: image cache, http cache, network. Any following `<link
2365 // rel=preload as=image>` will start a new image preload that can be
2366 // satisfied from http cache or network.
2368 // There is a spec discussion for "preload cache", see
2369 // https://github.com/w3c/preload/issues/97. And it is also not clear how
2370 // preload image interacts with list of available images, see
2371 // https://github.com/whatwg/html/issues/4474.
2372 proxy->RemoveSelf(aLoadingDocument);
2373 proxy->NotifyUsage(aLoadingDocument);
2375 imgRequest* request = proxy->GetOwner();
2376 nsresult rv =
2377 CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
2378 aObserver, requestFlags, _retval);
2379 NS_ENSURE_SUCCESS(rv, rv);
2381 imgRequestProxy* newProxy = *_retval;
2382 if (imgCacheValidator* validator = request->GetValidator()) {
2383 newProxy->MarkValidating();
2384 // Attach the proxy without notifying and this will add us to the load
2385 // group.
2386 validator->AddProxy(newProxy);
2387 } else {
2388 // It's OK to add here even if the request is done. If it is, it'll send
2389 // a OnStopRequest()and the proxy will be removed from the loadgroup in
2390 // imgRequestProxy::OnLoadComplete.
2391 newProxy->AddToLoadGroup();
2392 newProxy->NotifyListener();
2395 return NS_OK;
2399 RefPtr<imgCacheEntry> entry;
2401 // Look in the cache for our URI, and then validate it.
2402 // XXX For now ignore aCacheKey. We will need it in the future
2403 // for correctly dealing with image load requests that are a result
2404 // of post data.
2405 OriginAttributes attrs;
2406 if (aTriggeringPrincipal) {
2407 attrs = aTriggeringPrincipal->OriginAttributesRef();
2409 ImageCacheKey key(aURI, attrs, aLoadingDocument);
2410 if (mCache.Get(key, getter_AddRefs(entry)) && entry) {
2411 bool newChannelCreated = false;
2412 if (ValidateEntry(entry, aURI, aInitialDocumentURI, aReferrerInfo,
2413 aLoadGroup, aObserver, aLoadingDocument, requestFlags,
2414 aContentPolicyType, true, &newChannelCreated, _retval,
2415 aTriggeringPrincipal, corsmode, aLinkPreload,
2416 aEarlyHintPreloaderId)) {
2417 request = entry->GetRequest();
2419 // If this entry has no proxies, its request has no reference to the
2420 // entry.
2421 if (entry->HasNoProxies()) {
2422 LOG_FUNC_WITH_PARAM(gImgLog,
2423 "imgLoader::LoadImage() adding proxyless entry",
2424 "uri", key.URI());
2425 MOZ_ASSERT(!request->HasCacheEntry(),
2426 "Proxyless entry's request has cache entry!");
2427 request->SetCacheEntry(entry);
2429 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2430 mCacheTracker->MarkUsed(entry);
2434 entry->Touch();
2436 if (!newChannelCreated) {
2437 // This is ugly but it's needed to report CSP violations. We have 3
2438 // scenarios:
2439 // - we don't have cache. We are not in this if() stmt. A new channel is
2440 // created and that triggers the CSP checks.
2441 // - We have a cache entry and this is blocked by CSP directives.
2442 DebugOnly<bool> shouldLoad = ShouldLoadCachedImage(
2443 request, aLoadingDocument, aTriggeringPrincipal, aContentPolicyType,
2444 /* aSendCSPViolationReports */ true);
2445 MOZ_ASSERT(shouldLoad);
2447 } else {
2448 // We can't use this entry. We'll try to load it off the network, and if
2449 // successful, overwrite the old entry in the cache with a new one.
2450 entry = nullptr;
2454 // Keep the channel in this scope, so we can adjust its notificationCallbacks
2455 // later when we create the proxy.
2456 nsCOMPtr<nsIChannel> newChannel;
2457 // If we didn't get a cache hit, we need to load from the network.
2458 if (!request) {
2459 LOG_SCOPE(gImgLog, "imgLoader::LoadImage |cache miss|");
2461 bool forcePrincipalCheck;
2462 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
2463 aInitialDocumentURI, corsmode, aReferrerInfo,
2464 aLoadGroup, requestFlags, aContentPolicyType,
2465 aTriggeringPrincipal, aContext, mRespectPrivacy,
2466 aEarlyHintPreloaderId);
2467 if (NS_FAILED(rv)) {
2468 return NS_ERROR_FAILURE;
2471 MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel) == mRespectPrivacy);
2473 NewRequestAndEntry(forcePrincipalCheck, this, key, getter_AddRefs(request),
2474 getter_AddRefs(entry));
2476 MOZ_LOG(gImgLog, LogLevel::Debug,
2477 ("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
2478 " [request=%p]\n",
2479 this, request.get()));
2481 nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(newChannel));
2482 if (cos) {
2483 if (aUseUrgentStartForChannel && !aLinkPreload) {
2484 cos->AddClassFlags(nsIClassOfService::UrgentStart);
2487 if (StaticPrefs::network_http_tailing_enabled() &&
2488 aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
2489 cos->AddClassFlags(nsIClassOfService::Throttleable |
2490 nsIClassOfService::Tail);
2491 nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(newChannel));
2492 if (httpChannel) {
2493 Unused << httpChannel->SetRequestContextID(aRequestContextID);
2498 nsCOMPtr<nsILoadGroup> channelLoadGroup;
2499 newChannel->GetLoadGroup(getter_AddRefs(channelLoadGroup));
2500 rv = request->Init(aURI, aURI, /* aHadInsecureRedirect = */ false,
2501 channelLoadGroup, newChannel, entry, aLoadingDocument,
2502 aTriggeringPrincipal, corsmode, aReferrerInfo);
2503 if (NS_FAILED(rv)) {
2504 return NS_ERROR_FAILURE;
2507 // Add the initiator type for this image load
2508 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(newChannel);
2509 if (timedChannel) {
2510 timedChannel->SetInitiatorType(initiatorType);
2513 // create the proxy listener
2514 nsCOMPtr<nsIStreamListener> listener = new ProxyListener(request.get());
2516 MOZ_LOG(gImgLog, LogLevel::Debug,
2517 ("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
2518 this));
2520 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
2521 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
2522 aLoadGroup);
2524 nsresult openRes;
2525 openRes = newChannel->AsyncOpen(listener);
2527 if (NS_FAILED(openRes)) {
2528 MOZ_LOG(
2529 gImgLog, LogLevel::Debug,
2530 ("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
2531 "\n",
2532 this, static_cast<uint32_t>(openRes)));
2533 request->CancelAndAbort(openRes);
2534 return openRes;
2537 // Try to add the new request into the cache.
2538 PutIntoCache(key, entry);
2539 } else {
2540 LOG_MSG_WITH_PARAM(gImgLog, "imgLoader::LoadImage |cache hit|", "request",
2541 request);
2544 // If we didn't get a proxy when validating the cache entry, we need to
2545 // create one.
2546 if (!*_retval) {
2547 // ValidateEntry() has three return values: "Is valid," "might be valid --
2548 // validating over network", and "not valid." If we don't have a _retval,
2549 // we know ValidateEntry is not validating over the network, so it's safe
2550 // to SetLoadId here because we know this request is valid for this context.
2552 // Note, however, that this doesn't guarantee the behaviour we want (one
2553 // URL maps to the same image on a page) if we load the same image in a
2554 // different tab (see bug 528003), because its load id will get re-set, and
2555 // that'll cause us to validate over the network.
2556 request->SetLoadId(aLoadingDocument);
2558 LOG_MSG(gImgLog, "imgLoader::LoadImage", "creating proxy request.");
2559 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
2560 aObserver, requestFlags, _retval);
2561 if (NS_FAILED(rv)) {
2562 return rv;
2565 imgRequestProxy* proxy = *_retval;
2567 // Make sure that OnStatus/OnProgress calls have the right request set, if
2568 // we did create a channel here.
2569 if (newChannel) {
2570 nsCOMPtr<nsIInterfaceRequestor> requestor(
2571 new nsProgressNotificationProxy(newChannel, proxy));
2572 if (!requestor) {
2573 return NS_ERROR_OUT_OF_MEMORY;
2575 newChannel->SetNotificationCallbacks(requestor);
2578 if (aLinkPreload) {
2579 MOZ_ASSERT(aLoadingDocument);
2580 proxy->PrioritizeAsPreload();
2581 auto preloadKey =
2582 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, corsmode);
2583 proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
2586 // Note that it's OK to add here even if the request is done. If it is,
2587 // it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
2588 // the proxy will be removed from the loadgroup.
2589 proxy->AddToLoadGroup();
2591 // If we're loading off the network, explicitly don't notify our proxy,
2592 // because necko (or things called from necko, such as imgCacheValidator)
2593 // are going to call our notifications asynchronously, and we can't make it
2594 // further asynchronous because observers might rely on imagelib completing
2595 // its work between the channel's OnStartRequest and OnStopRequest.
2596 if (!newChannel) {
2597 proxy->NotifyListener();
2600 return rv;
2603 NS_ASSERTION(*_retval, "imgLoader::LoadImage -- no return value");
2605 return NS_OK;
2608 NS_IMETHODIMP
2609 imgLoader::LoadImageWithChannelXPCOM(nsIChannel* channel,
2610 imgINotificationObserver* aObserver,
2611 Document* aLoadingDocument,
2612 nsIStreamListener** listener,
2613 imgIRequest** _retval) {
2614 nsresult result;
2615 imgRequestProxy* proxy;
2616 result = LoadImageWithChannel(channel, aObserver, aLoadingDocument, listener,
2617 &proxy);
2618 *_retval = proxy;
2619 return result;
2622 nsresult imgLoader::LoadImageWithChannel(nsIChannel* channel,
2623 imgINotificationObserver* aObserver,
2624 Document* aLoadingDocument,
2625 nsIStreamListener** listener,
2626 imgRequestProxy** _retval) {
2627 NS_ASSERTION(channel,
2628 "imgLoader::LoadImageWithChannel -- NULL channel pointer");
2630 MOZ_ASSERT(NS_UsePrivateBrowsing(channel) == mRespectPrivacy);
2632 auto makeStaticIfNeeded = mozilla::MakeScopeExit(
2633 [&] { MakeRequestStaticIfNeeded(aLoadingDocument, _retval); });
2635 LOG_SCOPE(gImgLog, "imgLoader::LoadImageWithChannel");
2636 RefPtr<imgRequest> request;
2638 nsCOMPtr<nsIURI> uri;
2639 channel->GetURI(getter_AddRefs(uri));
2641 NS_ENSURE_TRUE(channel, NS_ERROR_FAILURE);
2642 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2644 OriginAttributes attrs = loadInfo->GetOriginAttributes();
2646 ImageCacheKey key(uri, attrs, aLoadingDocument);
2648 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2649 channel->GetLoadFlags(&requestFlags);
2651 RefPtr<imgCacheEntry> entry;
2653 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2654 RemoveFromCache(key);
2655 } else {
2656 // Look in the cache for our URI, and then validate it.
2657 // XXX For now ignore aCacheKey. We will need it in the future
2658 // for correctly dealing with image load requests that are a result
2659 // of post data.
2660 if (mCache.Get(key, getter_AddRefs(entry)) && entry) {
2661 // We don't want to kick off another network load. So we ask
2662 // ValidateEntry to only do validation without creating a new proxy. If
2663 // it says that the entry isn't valid any more, we'll only use the entry
2664 // we're getting if the channel is loading from the cache anyways.
2666 // XXX -- should this be changed? it's pretty much verbatim from the old
2667 // code, but seems nonsensical.
2669 // Since aCanMakeNewChannel == false, we don't need to pass content policy
2670 // type/principal/etc
2672 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2673 // if there is a loadInfo, use the right contentType, otherwise
2674 // default to the internal image type
2675 nsContentPolicyType policyType = loadInfo->InternalContentPolicyType();
2677 if (ValidateEntry(entry, uri, nullptr, nullptr, nullptr, aObserver,
2678 aLoadingDocument, requestFlags, policyType, false,
2679 nullptr, nullptr, nullptr, CORS_NONE, false, 0)) {
2680 request = entry->GetRequest();
2681 } else {
2682 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(channel));
2683 bool bUseCacheCopy;
2685 if (cacheChan) {
2686 cacheChan->IsFromCache(&bUseCacheCopy);
2687 } else {
2688 bUseCacheCopy = false;
2691 if (!bUseCacheCopy) {
2692 entry = nullptr;
2693 } else {
2694 request = entry->GetRequest();
2698 if (request && entry) {
2699 // If this entry has no proxies, its request has no reference to
2700 // the entry.
2701 if (entry->HasNoProxies()) {
2702 LOG_FUNC_WITH_PARAM(
2703 gImgLog,
2704 "imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
2705 key.URI());
2706 MOZ_ASSERT(!request->HasCacheEntry(),
2707 "Proxyless entry's request has cache entry!");
2708 request->SetCacheEntry(entry);
2710 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2711 mCacheTracker->MarkUsed(entry);
2718 nsCOMPtr<nsILoadGroup> loadGroup;
2719 channel->GetLoadGroup(getter_AddRefs(loadGroup));
2721 #ifdef DEBUG
2722 if (aLoadingDocument) {
2723 // The load group of the channel should always match that of the
2724 // document if given. If that isn't the case, then we need to add more
2725 // plumbing to ensure we block the document as well.
2726 nsCOMPtr<nsILoadGroup> docLoadGroup =
2727 aLoadingDocument->GetDocumentLoadGroup();
2728 MOZ_ASSERT(docLoadGroup == loadGroup);
2730 #endif
2732 // Filter out any load flags not from nsIRequest
2733 requestFlags &= nsIRequest::LOAD_REQUESTMASK;
2735 nsresult rv = NS_OK;
2736 if (request) {
2737 // we have this in our cache already.. cancel the current (document) load
2739 // this should fire an OnStopRequest
2740 channel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
2742 *listener = nullptr; // give them back a null nsIStreamListener
2744 rv = CreateNewProxyForRequest(request, uri, loadGroup, aLoadingDocument,
2745 aObserver, requestFlags, _retval);
2746 static_cast<imgRequestProxy*>(*_retval)->NotifyListener();
2747 } else {
2748 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2749 nsCOMPtr<nsIURI> originalURI;
2750 channel->GetOriginalURI(getter_AddRefs(originalURI));
2752 // XXX(seth): We should be able to just use |key| here, except that |key| is
2753 // constructed above with the *current URI* and not the *original URI*. I'm
2754 // pretty sure this is a bug, and it's preventing us from ever getting a
2755 // cache hit in LoadImageWithChannel when redirects are involved.
2756 ImageCacheKey originalURIKey(originalURI, attrs, aLoadingDocument);
2758 // Default to doing a principal check because we don't know who
2759 // started that load and whether their principal ended up being
2760 // inherited on the channel.
2761 NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
2762 originalURIKey, getter_AddRefs(request),
2763 getter_AddRefs(entry));
2765 // No principal specified here, because we're not passed one.
2766 // In LoadImageWithChannel, the redirects that may have been
2767 // associated with this load would have gone through necko.
2768 // We only have the final URI in ImageLib and hence don't know
2769 // if the request went through insecure redirects. But if it did,
2770 // the necko cache should have handled that (since all necko cache hits
2771 // including the redirects will go through content policy). Hence, we
2772 // can set aHadInsecureRedirect to false here.
2773 rv = request->Init(originalURI, uri, /* aHadInsecureRedirect = */ false,
2774 channel, channel, entry, aLoadingDocument, nullptr,
2775 CORS_NONE, nullptr);
2776 NS_ENSURE_SUCCESS(rv, rv);
2778 RefPtr<ProxyListener> pl =
2779 new ProxyListener(static_cast<nsIStreamListener*>(request.get()));
2780 pl.forget(listener);
2782 // Try to add the new request into the cache.
2783 PutIntoCache(originalURIKey, entry);
2785 rv = CreateNewProxyForRequest(request, originalURI, loadGroup,
2786 aLoadingDocument, aObserver, requestFlags,
2787 _retval);
2789 // Explicitly don't notify our proxy, because we're loading off the
2790 // network, and necko (or things called from necko, such as
2791 // imgCacheValidator) are going to call our notifications asynchronously,
2792 // and we can't make it further asynchronous because observers might rely
2793 // on imagelib completing its work between the channel's OnStartRequest and
2794 // OnStopRequest.
2797 if (NS_FAILED(rv)) {
2798 return rv;
2801 (*_retval)->AddToLoadGroup();
2802 return rv;
2805 bool imgLoader::SupportImageWithMimeType(const nsACString& aMimeType,
2806 AcceptedMimeTypes aAccept
2807 /* = AcceptedMimeTypes::IMAGES */) {
2808 nsAutoCString mimeType(aMimeType);
2809 ToLowerCase(mimeType);
2811 if (aAccept == AcceptedMimeTypes::IMAGES_AND_DOCUMENTS &&
2812 mimeType.EqualsLiteral("image/svg+xml")) {
2813 return true;
2816 DecoderType type = DecoderFactory::GetDecoderType(mimeType.get());
2817 return type != DecoderType::UNKNOWN;
2820 NS_IMETHODIMP
2821 imgLoader::GetMIMETypeFromContent(nsIRequest* aRequest,
2822 const uint8_t* aContents, uint32_t aLength,
2823 nsACString& aContentType) {
2824 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2825 if (channel) {
2826 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2827 if (loadInfo->GetSkipContentSniffing()) {
2828 return NS_ERROR_NOT_AVAILABLE;
2832 nsresult rv =
2833 GetMimeTypeFromContent((const char*)aContents, aLength, aContentType);
2834 if (NS_SUCCEEDED(rv) && channel && XRE_IsParentProcess()) {
2835 if (RefPtr<mozilla::net::nsHttpChannel> httpChannel =
2836 do_QueryObject(channel)) {
2837 // If the image type pattern matching algorithm given bytes does not
2838 // return undefined, then disable the further check and allow the
2839 // response.
2840 httpChannel->DisableIsOpaqueResponseAllowedAfterSniffCheck(
2841 mozilla::net::nsHttpChannel::SnifferType::Image);
2845 return rv;
2848 /* static */
2849 nsresult imgLoader::GetMimeTypeFromContent(const char* aContents,
2850 uint32_t aLength,
2851 nsACString& aContentType) {
2852 nsAutoCString detected;
2854 /* Is it a GIF? */
2855 if (aLength >= 6 &&
2856 (!strncmp(aContents, "GIF87a", 6) || !strncmp(aContents, "GIF89a", 6))) {
2857 aContentType.AssignLiteral(IMAGE_GIF);
2859 /* or a PNG? */
2860 } else if (aLength >= 8 && ((unsigned char)aContents[0] == 0x89 &&
2861 (unsigned char)aContents[1] == 0x50 &&
2862 (unsigned char)aContents[2] == 0x4E &&
2863 (unsigned char)aContents[3] == 0x47 &&
2864 (unsigned char)aContents[4] == 0x0D &&
2865 (unsigned char)aContents[5] == 0x0A &&
2866 (unsigned char)aContents[6] == 0x1A &&
2867 (unsigned char)aContents[7] == 0x0A)) {
2868 aContentType.AssignLiteral(IMAGE_PNG);
2870 /* maybe a JPEG (JFIF)? */
2871 /* JFIF files start with SOI APP0 but older files can start with SOI DQT
2872 * so we test for SOI followed by any marker, i.e. FF D8 FF
2873 * this will also work for SPIFF JPEG files if they appear in the future.
2875 * (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
2877 } else if (aLength >= 3 && ((unsigned char)aContents[0]) == 0xFF &&
2878 ((unsigned char)aContents[1]) == 0xD8 &&
2879 ((unsigned char)aContents[2]) == 0xFF) {
2880 aContentType.AssignLiteral(IMAGE_JPEG);
2882 /* or how about ART? */
2883 /* ART begins with JG (4A 47). Major version offset 2.
2884 * Minor version offset 3. Offset 4 must be nullptr.
2886 } else if (aLength >= 5 && ((unsigned char)aContents[0]) == 0x4a &&
2887 ((unsigned char)aContents[1]) == 0x47 &&
2888 ((unsigned char)aContents[4]) == 0x00) {
2889 aContentType.AssignLiteral(IMAGE_ART);
2891 } else if (aLength >= 2 && !strncmp(aContents, "BM", 2)) {
2892 aContentType.AssignLiteral(IMAGE_BMP);
2894 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
2895 // CURs begin with 2-byte 0 followed by 2-byte 2.
2896 } else if (aLength >= 4 && (!memcmp(aContents, "\000\000\001\000", 4) ||
2897 !memcmp(aContents, "\000\000\002\000", 4))) {
2898 aContentType.AssignLiteral(IMAGE_ICO);
2900 // WebPs always begin with RIFF, a 32-bit length, and WEBP.
2901 } else if (aLength >= 12 && !memcmp(aContents, "RIFF", 4) &&
2902 !memcmp(aContents + 8, "WEBP", 4)) {
2903 aContentType.AssignLiteral(IMAGE_WEBP);
2905 } else if (MatchesMP4(reinterpret_cast<const uint8_t*>(aContents), aLength,
2906 detected) &&
2907 detected.Equals(IMAGE_AVIF)) {
2908 aContentType.AssignLiteral(IMAGE_AVIF);
2909 } else if ((aLength >= 2 && !memcmp(aContents, "\xFF\x0A", 2)) ||
2910 (aLength >= 12 &&
2911 !memcmp(aContents, "\x00\x00\x00\x0CJXL \x0D\x0A\x87\x0A", 12))) {
2912 // Each version is for containerless and containerful files respectively.
2913 aContentType.AssignLiteral(IMAGE_JXL);
2914 } else {
2915 /* none of the above? I give up */
2916 return NS_ERROR_NOT_AVAILABLE;
2919 return NS_OK;
2923 * proxy stream listener class used to handle multipart/x-mixed-replace
2926 #include "nsIRequest.h"
2927 #include "nsIStreamConverterService.h"
2929 NS_IMPL_ISUPPORTS(ProxyListener, nsIStreamListener,
2930 nsIThreadRetargetableStreamListener, nsIRequestObserver)
2932 ProxyListener::ProxyListener(nsIStreamListener* dest) : mDestListener(dest) {}
2934 ProxyListener::~ProxyListener() = default;
2936 /** nsIRequestObserver methods **/
2938 NS_IMETHODIMP
2939 ProxyListener::OnStartRequest(nsIRequest* aRequest) {
2940 if (!mDestListener) {
2941 return NS_ERROR_FAILURE;
2944 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2945 if (channel) {
2946 // We need to set the initiator type for the image load
2947 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(channel);
2948 if (timedChannel) {
2949 nsAutoString type;
2950 timedChannel->GetInitiatorType(type);
2951 if (type.IsEmpty()) {
2952 timedChannel->SetInitiatorType(u"img"_ns);
2956 nsAutoCString contentType;
2957 nsresult rv = channel->GetContentType(contentType);
2959 if (!contentType.IsEmpty()) {
2960 /* If multipart/x-mixed-replace content, we'll insert a MIME decoder
2961 in the pipeline to handle the content and pass it along to our
2962 original listener.
2964 if ("multipart/x-mixed-replace"_ns.Equals(contentType)) {
2965 nsCOMPtr<nsIStreamConverterService> convServ(
2966 do_GetService("@mozilla.org/streamConverters;1", &rv));
2967 if (NS_SUCCEEDED(rv)) {
2968 nsCOMPtr<nsIStreamListener> toListener(mDestListener);
2969 nsCOMPtr<nsIStreamListener> fromListener;
2971 rv = convServ->AsyncConvertData("multipart/x-mixed-replace", "*/*",
2972 toListener, nullptr,
2973 getter_AddRefs(fromListener));
2974 if (NS_SUCCEEDED(rv)) {
2975 mDestListener = fromListener;
2982 return mDestListener->OnStartRequest(aRequest);
2985 NS_IMETHODIMP
2986 ProxyListener::OnStopRequest(nsIRequest* aRequest, nsresult status) {
2987 if (!mDestListener) {
2988 return NS_ERROR_FAILURE;
2991 return mDestListener->OnStopRequest(aRequest, status);
2994 /** nsIStreamListener methods **/
2996 NS_IMETHODIMP
2997 ProxyListener::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
2998 uint64_t sourceOffset, uint32_t count) {
2999 if (!mDestListener) {
3000 return NS_ERROR_FAILURE;
3003 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
3006 /** nsThreadRetargetableStreamListener methods **/
3007 NS_IMETHODIMP
3008 ProxyListener::CheckListenerChain() {
3009 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3010 nsresult rv = NS_OK;
3011 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3012 do_QueryInterface(mDestListener, &rv);
3013 if (retargetableListener) {
3014 rv = retargetableListener->CheckListenerChain();
3016 MOZ_LOG(
3017 gImgLog, LogLevel::Debug,
3018 ("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
3019 "]",
3020 (NS_SUCCEEDED(rv) ? "success" : "failure"), this,
3021 (nsIStreamListener*)mDestListener, static_cast<uint32_t>(rv)));
3022 return rv;
3026 * http validate class. check a channel for a 304
3029 NS_IMPL_ISUPPORTS(imgCacheValidator, nsIStreamListener, nsIRequestObserver,
3030 nsIThreadRetargetableStreamListener, nsIChannelEventSink,
3031 nsIInterfaceRequestor, nsIAsyncVerifyRedirectCallback)
3033 imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy* progress,
3034 imgLoader* loader, imgRequest* request,
3035 Document* aDocument,
3036 uint64_t aInnerWindowId,
3037 bool forcePrincipalCheckForCacheEntry)
3038 : mProgressProxy(progress),
3039 mRequest(request),
3040 mDocument(aDocument),
3041 mInnerWindowId(aInnerWindowId),
3042 mImgLoader(loader),
3043 mHadInsecureRedirect(false) {
3044 NewRequestAndEntry(forcePrincipalCheckForCacheEntry, loader,
3045 mRequest->CacheKey(), getter_AddRefs(mNewRequest),
3046 getter_AddRefs(mNewEntry));
3049 imgCacheValidator::~imgCacheValidator() {
3050 if (mRequest) {
3051 // If something went wrong, and we never unblocked the requests waiting on
3052 // validation, now is our last chance. We will cancel the new request and
3053 // switch the waiting proxies to it.
3054 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
3058 void imgCacheValidator::AddProxy(imgRequestProxy* aProxy) {
3059 // aProxy needs to be in the loadgroup since we're validating from
3060 // the network.
3061 aProxy->AddToLoadGroup();
3063 mProxies.AppendElement(aProxy);
3066 void imgCacheValidator::RemoveProxy(imgRequestProxy* aProxy) {
3067 mProxies.RemoveElement(aProxy);
3070 void imgCacheValidator::UpdateProxies(bool aCancelRequest, bool aSyncNotify) {
3071 MOZ_ASSERT(mRequest);
3073 // Clear the validator before updating the proxies. The notifications may
3074 // clone an existing request, and its state could be inconsistent.
3075 mRequest->SetValidator(nullptr);
3076 mRequest = nullptr;
3078 // If an error occurred, we will want to cancel the new request, and make the
3079 // validating proxies point to it. Any proxies still bound to the original
3080 // request which are not validating should remain untouched.
3081 if (aCancelRequest) {
3082 MOZ_ASSERT(mNewRequest);
3083 mNewRequest->CancelAndAbort(NS_BINDING_ABORTED);
3086 // We have finished validating the request, so we can safely take ownership
3087 // of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
3088 // if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
3089 // that any potential notifications should still be suppressed in
3090 // imgRequestProxy::ChangeOwner because we haven't cleared the validating
3091 // flag yet, and thus they will remain deferred.
3092 AutoTArray<RefPtr<imgRequestProxy>, 4> proxies(std::move(mProxies));
3094 for (auto& proxy : proxies) {
3095 // First update the state of all proxies before notifying any of them
3096 // to ensure a consistent state (e.g. in case the notification causes
3097 // other proxies to be touched indirectly.)
3098 MOZ_ASSERT(proxy->IsValidating());
3099 MOZ_ASSERT(proxy->NotificationsDeferred(),
3100 "Proxies waiting on cache validation should be "
3101 "deferring notifications!");
3102 if (mNewRequest) {
3103 proxy->ChangeOwner(mNewRequest);
3105 proxy->ClearValidating();
3108 mNewRequest = nullptr;
3109 mNewEntry = nullptr;
3111 for (auto& proxy : proxies) {
3112 if (aSyncNotify) {
3113 // Notify synchronously, because the caller knows we are already in an
3114 // asynchronously-called function (e.g. OnStartRequest).
3115 proxy->SyncNotifyListener();
3116 } else {
3117 // Notify asynchronously, because the caller does not know our current
3118 // call state (e.g. ~imgCacheValidator).
3119 proxy->NotifyListener();
3124 /** nsIRequestObserver methods **/
3126 NS_IMETHODIMP
3127 imgCacheValidator::OnStartRequest(nsIRequest* aRequest) {
3128 // We may be holding on to a document, so ensure that it's released.
3129 RefPtr<Document> document = mDocument.forget();
3131 // If for some reason we don't still have an existing request (probably
3132 // because OnStartRequest got delivered more than once), just bail.
3133 if (!mRequest) {
3134 MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
3135 aRequest->CancelWithReason(NS_BINDING_ABORTED,
3136 "OnStartRequest delivered more than once?"_ns);
3137 return NS_ERROR_FAILURE;
3140 // If this request is coming from cache and has the same URI as our
3141 // imgRequest, the request all our proxies are pointing at is valid, and all
3142 // we have to do is tell them to notify their listeners.
3143 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(aRequest));
3144 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
3145 if (cacheChan && channel) {
3146 bool isFromCache = false;
3147 cacheChan->IsFromCache(&isFromCache);
3149 nsCOMPtr<nsIURI> channelURI;
3150 channel->GetURI(getter_AddRefs(channelURI));
3152 nsCOMPtr<nsIURI> finalURI;
3153 mRequest->GetFinalURI(getter_AddRefs(finalURI));
3155 bool sameURI = false;
3156 if (channelURI && finalURI) {
3157 channelURI->Equals(finalURI, &sameURI);
3160 if (isFromCache && sameURI) {
3161 // We don't need to load this any more.
3162 aRequest->CancelWithReason(NS_BINDING_ABORTED,
3163 "imgCacheValidator::OnStartRequest"_ns);
3164 mNewRequest = nullptr;
3166 // Clear the validator before updating the proxies. The notifications may
3167 // clone an existing request, and its state could be inconsistent.
3168 mRequest->SetLoadId(document);
3169 mRequest->SetInnerWindowID(mInnerWindowId);
3170 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3171 return NS_OK;
3175 // We can't load out of cache. We have to create a whole new request for the
3176 // data that's coming in off the channel.
3177 nsCOMPtr<nsIURI> uri;
3178 mRequest->GetURI(getter_AddRefs(uri));
3180 LOG_MSG_WITH_PARAM(gImgLog,
3181 "imgCacheValidator::OnStartRequest creating new request",
3182 "uri", uri);
3184 CORSMode corsmode = mRequest->GetCORSMode();
3185 nsCOMPtr<nsIReferrerInfo> referrerInfo = mRequest->GetReferrerInfo();
3186 nsCOMPtr<nsIPrincipal> triggeringPrincipal =
3187 mRequest->GetTriggeringPrincipal();
3189 // Doom the old request's cache entry
3190 mRequest->RemoveFromCache();
3192 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
3193 nsCOMPtr<nsIURI> originalURI;
3194 channel->GetOriginalURI(getter_AddRefs(originalURI));
3195 nsresult rv = mNewRequest->Init(originalURI, uri, mHadInsecureRedirect,
3196 aRequest, channel, mNewEntry, document,
3197 triggeringPrincipal, corsmode, referrerInfo);
3198 if (NS_FAILED(rv)) {
3199 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
3200 return rv;
3203 mDestListener = new ProxyListener(mNewRequest);
3205 // Try to add the new request into the cache. Note that the entry must be in
3206 // the cache before the proxies' ownership changes, because adding a proxy
3207 // changes the caching behaviour for imgRequests.
3208 mImgLoader->PutIntoCache(mNewRequest->CacheKey(), mNewEntry);
3209 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3210 return mDestListener->OnStartRequest(aRequest);
3213 NS_IMETHODIMP
3214 imgCacheValidator::OnStopRequest(nsIRequest* aRequest, nsresult status) {
3215 // Be sure we've released the document that we may have been holding on to.
3216 mDocument = nullptr;
3218 if (!mDestListener) {
3219 return NS_OK;
3222 return mDestListener->OnStopRequest(aRequest, status);
3225 /** nsIStreamListener methods **/
3227 NS_IMETHODIMP
3228 imgCacheValidator::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
3229 uint64_t sourceOffset, uint32_t count) {
3230 if (!mDestListener) {
3231 // XXX see bug 113959
3232 uint32_t _retval;
3233 inStr->ReadSegments(NS_DiscardSegment, nullptr, count, &_retval);
3234 return NS_OK;
3237 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
3240 /** nsIThreadRetargetableStreamListener methods **/
3242 NS_IMETHODIMP
3243 imgCacheValidator::CheckListenerChain() {
3244 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3245 nsresult rv = NS_OK;
3246 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3247 do_QueryInterface(mDestListener, &rv);
3248 if (retargetableListener) {
3249 rv = retargetableListener->CheckListenerChain();
3251 MOZ_LOG(
3252 gImgLog, LogLevel::Debug,
3253 ("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32 "=%s",
3254 this, static_cast<uint32_t>(rv),
3255 NS_SUCCEEDED(rv) ? "succeeded" : "failed"));
3256 return rv;
3259 /** nsIInterfaceRequestor methods **/
3261 NS_IMETHODIMP
3262 imgCacheValidator::GetInterface(const nsIID& aIID, void** aResult) {
3263 if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
3264 return QueryInterface(aIID, aResult);
3267 return mProgressProxy->GetInterface(aIID, aResult);
3270 // These functions are materially the same as the same functions in imgRequest.
3271 // We duplicate them because we're verifying whether cache loads are necessary,
3272 // not unconditionally loading.
3274 /** nsIChannelEventSink methods **/
3275 NS_IMETHODIMP
3276 imgCacheValidator::AsyncOnChannelRedirect(
3277 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
3278 nsIAsyncVerifyRedirectCallback* callback) {
3279 // Note all cache information we get from the old channel.
3280 mNewRequest->SetCacheValidation(mNewEntry, oldChannel);
3282 // If the previous URI is a non-HTTPS URI, record that fact for later use by
3283 // security code, which needs to know whether there is an insecure load at any
3284 // point in the redirect chain.
3285 nsCOMPtr<nsIURI> oldURI;
3286 bool schemeLocal = false;
3287 if (NS_FAILED(oldChannel->GetURI(getter_AddRefs(oldURI))) ||
3288 NS_FAILED(NS_URIChainHasFlags(
3289 oldURI, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE, &schemeLocal)) ||
3290 (!oldURI->SchemeIs("https") && !oldURI->SchemeIs("chrome") &&
3291 !schemeLocal)) {
3292 mHadInsecureRedirect = true;
3295 // Prepare for callback
3296 mRedirectCallback = callback;
3297 mRedirectChannel = newChannel;
3299 return mProgressProxy->AsyncOnChannelRedirect(oldChannel, newChannel, flags,
3300 this);
3303 NS_IMETHODIMP
3304 imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult) {
3305 // If we've already been told to abort, just do so.
3306 if (NS_FAILED(aResult)) {
3307 mRedirectCallback->OnRedirectVerifyCallback(aResult);
3308 mRedirectCallback = nullptr;
3309 mRedirectChannel = nullptr;
3310 return NS_OK;
3313 // make sure we have a protocol that returns data rather than opens
3314 // an external application, e.g. mailto:
3315 nsCOMPtr<nsIURI> uri;
3316 mRedirectChannel->GetURI(getter_AddRefs(uri));
3318 nsresult result = NS_OK;
3320 if (nsContentUtils::IsExternalProtocol(uri)) {
3321 result = NS_ERROR_ABORT;
3324 mRedirectCallback->OnRedirectVerifyCallback(result);
3325 mRedirectCallback = nullptr;
3326 mRedirectChannel = nullptr;
3327 return NS_OK;