Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / image / imgLoader.cpp
blobe94cf0836896ef994961f2008e9c3c241b7b2699
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 #include "nsIThreadRetargetableStreamListener.h"
11 #undef LoadImage
13 #include "imgLoader.h"
15 #include <algorithm>
16 #include <utility>
18 #include "DecoderFactory.h"
19 #include "Image.h"
20 #include "ImageLogging.h"
21 #include "ReferrerInfo.h"
22 #include "imgRequestProxy.h"
23 #include "mozilla/Attributes.h"
24 #include "mozilla/BasePrincipal.h"
25 #include "mozilla/ChaosMode.h"
26 #include "mozilla/ClearOnShutdown.h"
27 #include "mozilla/LoadInfo.h"
28 #include "mozilla/NullPrincipal.h"
29 #include "mozilla/Preferences.h"
30 #include "mozilla/ProfilerLabels.h"
31 #include "mozilla/StaticPrefs_image.h"
32 #include "mozilla/StaticPrefs_network.h"
33 #include "mozilla/StoragePrincipalHelper.h"
34 #include "mozilla/Maybe.h"
35 #include "mozilla/dom/CacheExpirationTime.h"
36 #include "mozilla/dom/ContentParent.h"
37 #include "mozilla/dom/FetchPriority.h"
38 #include "mozilla/dom/nsMixedContentBlocker.h"
39 #include "mozilla/image/ImageMemoryReporter.h"
40 #include "mozilla/layers/CompositorManagerChild.h"
41 #include "nsCOMPtr.h"
42 #include "nsCRT.h"
43 #include "nsComponentManagerUtils.h"
44 #include "nsContentPolicyUtils.h"
45 #include "nsContentSecurityManager.h"
46 #include "nsContentUtils.h"
47 #include "nsHttpChannel.h"
48 #include "nsIAsyncVerifyRedirectCallback.h"
49 #include "nsICacheInfoChannel.h"
50 #include "nsIChannelEventSink.h"
51 #include "nsIClassOfService.h"
52 #include "nsIEffectiveTLDService.h"
53 #include "nsIFile.h"
54 #include "nsIFileURL.h"
55 #include "nsIHttpChannel.h"
56 #include "nsIInterfaceRequestor.h"
57 #include "nsIInterfaceRequestorUtils.h"
58 #include "nsIMemoryReporter.h"
59 #include "nsINetworkPredictor.h"
60 #include "nsIProgressEventSink.h"
61 #include "nsIProtocolHandler.h"
62 #include "nsImageModule.h"
63 #include "nsMediaSniffer.h"
64 #include "nsMimeTypes.h"
65 #include "nsNetCID.h"
66 #include "nsNetUtil.h"
67 #include "nsProxyRelease.h"
68 #include "nsQueryObject.h"
69 #include "nsReadableUtils.h"
70 #include "nsStreamUtils.h"
71 #include "prtime.h"
73 // we want to explore making the document own the load group
74 // so we can associate the document URI with the load group.
75 // until this point, we have an evil hack:
76 #include "nsIHttpChannelInternal.h"
77 #include "nsILoadGroupChild.h"
78 #include "nsIDocShell.h"
80 using namespace mozilla;
81 using namespace mozilla::dom;
82 using namespace mozilla::image;
83 using namespace mozilla::net;
85 MOZ_DEFINE_MALLOC_SIZE_OF(ImagesMallocSizeOf)
87 class imgMemoryReporter final : public nsIMemoryReporter {
88 ~imgMemoryReporter() = default;
90 public:
91 NS_DECL_ISUPPORTS
93 NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
94 nsISupports* aData, bool aAnonymize) override {
95 MOZ_ASSERT(NS_IsMainThread());
97 layers::CompositorManagerChild* manager =
98 mozilla::layers::CompositorManagerChild::GetInstance();
99 if (!manager || !StaticPrefs::image_mem_debug_reporting()) {
100 layers::SharedSurfacesMemoryReport sharedSurfaces;
101 FinishCollectReports(aHandleReport, aData, aAnonymize, sharedSurfaces);
102 return NS_OK;
105 RefPtr<imgMemoryReporter> self(this);
106 nsCOMPtr<nsIHandleReportCallback> handleReport(aHandleReport);
107 nsCOMPtr<nsISupports> data(aData);
108 manager->SendReportSharedSurfacesMemory(
109 [=](layers::SharedSurfacesMemoryReport aReport) {
110 self->FinishCollectReports(handleReport, data, aAnonymize, aReport);
112 [=](mozilla::ipc::ResponseRejectReason&& aReason) {
113 layers::SharedSurfacesMemoryReport sharedSurfaces;
114 self->FinishCollectReports(handleReport, data, aAnonymize,
115 sharedSurfaces);
117 return NS_OK;
120 void FinishCollectReports(
121 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
122 bool aAnonymize, layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
123 nsTArray<ImageMemoryCounter> chrome;
124 nsTArray<ImageMemoryCounter> content;
125 nsTArray<ImageMemoryCounter> uncached;
127 for (uint32_t i = 0; i < mKnownLoaders.Length(); i++) {
128 for (imgCacheEntry* entry : mKnownLoaders[i]->mCache.Values()) {
129 RefPtr<imgRequest> req = entry->GetRequest();
130 RecordCounterForRequest(req, &content, !entry->HasNoProxies());
132 MutexAutoLock lock(mKnownLoaders[i]->mUncachedImagesMutex);
133 for (RefPtr<imgRequest> req : mKnownLoaders[i]->mUncachedImages) {
134 RecordCounterForRequest(req, &uncached, req->HasConsumers());
138 // Note that we only need to anonymize content image URIs.
140 ReportCounterArray(aHandleReport, aData, chrome, "images/chrome",
141 /* aAnonymize */ false, aSharedSurfaces);
143 ReportCounterArray(aHandleReport, aData, content, "images/content",
144 aAnonymize, aSharedSurfaces);
146 // Uncached images may be content or chrome, so anonymize them.
147 ReportCounterArray(aHandleReport, aData, uncached, "images/uncached",
148 aAnonymize, aSharedSurfaces);
150 // Report any shared surfaces that were not merged with the surface cache.
151 ImageMemoryReporter::ReportSharedSurfaces(aHandleReport, aData,
152 aSharedSurfaces);
154 nsCOMPtr<nsIMemoryReporterManager> imgr =
155 do_GetService("@mozilla.org/memory-reporter-manager;1");
156 if (imgr) {
157 imgr->EndReport();
161 static int64_t ImagesContentUsedUncompressedDistinguishedAmount() {
162 size_t n = 0;
163 for (uint32_t i = 0; i < imgLoader::sMemReporter->mKnownLoaders.Length();
164 i++) {
165 for (imgCacheEntry* entry :
166 imgLoader::sMemReporter->mKnownLoaders[i]->mCache.Values()) {
167 if (entry->HasNoProxies()) {
168 continue;
171 RefPtr<imgRequest> req = entry->GetRequest();
172 RefPtr<image::Image> image = req->GetImage();
173 if (!image) {
174 continue;
177 // Both this and EntryImageSizes measure
178 // images/content/raster/used/decoded memory. This function's
179 // measurement is secondary -- the result doesn't go in the "explicit"
180 // tree -- so we use moz_malloc_size_of instead of ImagesMallocSizeOf to
181 // prevent DMD from seeing it reported twice.
182 SizeOfState state(moz_malloc_size_of);
183 ImageMemoryCounter counter(req, image, state, /* aIsUsed = */ true);
185 n += counter.Values().DecodedHeap();
186 n += counter.Values().DecodedNonHeap();
187 n += counter.Values().DecodedUnknown();
190 return n;
193 void RegisterLoader(imgLoader* aLoader) {
194 mKnownLoaders.AppendElement(aLoader);
197 void UnregisterLoader(imgLoader* aLoader) {
198 mKnownLoaders.RemoveElement(aLoader);
201 private:
202 nsTArray<imgLoader*> mKnownLoaders;
204 struct MemoryTotal {
205 MemoryTotal& operator+=(const ImageMemoryCounter& aImageCounter) {
206 if (aImageCounter.Type() == imgIContainer::TYPE_RASTER) {
207 if (aImageCounter.IsUsed()) {
208 mUsedRasterCounter += aImageCounter.Values();
209 } else {
210 mUnusedRasterCounter += aImageCounter.Values();
212 } else if (aImageCounter.Type() == imgIContainer::TYPE_VECTOR) {
213 if (aImageCounter.IsUsed()) {
214 mUsedVectorCounter += aImageCounter.Values();
215 } else {
216 mUnusedVectorCounter += aImageCounter.Values();
218 } else if (aImageCounter.Type() == imgIContainer::TYPE_REQUEST) {
219 // Nothing to do, we did not get to the point of having an image.
220 } else {
221 MOZ_CRASH("Unexpected image type");
224 return *this;
227 const MemoryCounter& UsedRaster() const { return mUsedRasterCounter; }
228 const MemoryCounter& UnusedRaster() const { return mUnusedRasterCounter; }
229 const MemoryCounter& UsedVector() const { return mUsedVectorCounter; }
230 const MemoryCounter& UnusedVector() const { return mUnusedVectorCounter; }
232 private:
233 MemoryCounter mUsedRasterCounter;
234 MemoryCounter mUnusedRasterCounter;
235 MemoryCounter mUsedVectorCounter;
236 MemoryCounter mUnusedVectorCounter;
239 // Reports all images of a single kind, e.g. all used chrome images.
240 void ReportCounterArray(nsIHandleReportCallback* aHandleReport,
241 nsISupports* aData,
242 nsTArray<ImageMemoryCounter>& aCounterArray,
243 const char* aPathPrefix, bool aAnonymize,
244 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
245 MemoryTotal summaryTotal;
246 MemoryTotal nonNotableTotal;
248 // Report notable images, and compute total and non-notable aggregate sizes.
249 for (uint32_t i = 0; i < aCounterArray.Length(); i++) {
250 ImageMemoryCounter& counter = aCounterArray[i];
252 if (aAnonymize) {
253 counter.URI().Truncate();
254 counter.URI().AppendPrintf("<anonymized-%u>", i);
255 } else {
256 // The URI could be an extremely long data: URI. Truncate if needed.
257 static const size_t max = 256;
258 if (counter.URI().Length() > max) {
259 counter.URI().Truncate(max);
260 counter.URI().AppendLiteral(" (truncated)");
262 counter.URI().ReplaceChar('/', '\\');
265 summaryTotal += counter;
267 if (counter.IsNotable() || StaticPrefs::image_mem_debug_reporting()) {
268 ReportImage(aHandleReport, aData, aPathPrefix, counter,
269 aSharedSurfaces);
270 } else {
271 ImageMemoryReporter::TrimSharedSurfaces(counter, aSharedSurfaces);
272 nonNotableTotal += counter;
276 // Report non-notable images in aggregate.
277 ReportTotal(aHandleReport, aData, /* aExplicit = */ true, aPathPrefix,
278 "<non-notable images>/", nonNotableTotal);
280 // Report a summary in aggregate, outside of the explicit tree.
281 ReportTotal(aHandleReport, aData, /* aExplicit = */ false, aPathPrefix, "",
282 summaryTotal);
285 static void ReportImage(nsIHandleReportCallback* aHandleReport,
286 nsISupports* aData, const char* aPathPrefix,
287 const ImageMemoryCounter& aCounter,
288 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
289 nsAutoCString pathPrefix("explicit/"_ns);
290 pathPrefix.Append(aPathPrefix);
292 switch (aCounter.Type()) {
293 case imgIContainer::TYPE_RASTER:
294 pathPrefix.AppendLiteral("/raster/");
295 break;
296 case imgIContainer::TYPE_VECTOR:
297 pathPrefix.AppendLiteral("/vector/");
298 break;
299 case imgIContainer::TYPE_REQUEST:
300 pathPrefix.AppendLiteral("/request/");
301 break;
302 default:
303 pathPrefix.AppendLiteral("/unknown=");
304 pathPrefix.AppendInt(aCounter.Type());
305 pathPrefix.AppendLiteral("/");
306 break;
309 pathPrefix.Append(aCounter.IsUsed() ? "used/" : "unused/");
310 if (aCounter.IsValidating()) {
311 pathPrefix.AppendLiteral("validating/");
313 if (aCounter.HasError()) {
314 pathPrefix.AppendLiteral("err/");
317 pathPrefix.AppendLiteral("progress=");
318 pathPrefix.AppendInt(aCounter.Progress(), 16);
319 pathPrefix.AppendLiteral("/");
321 pathPrefix.AppendLiteral("image(");
322 pathPrefix.AppendInt(aCounter.IntrinsicSize().width);
323 pathPrefix.AppendLiteral("x");
324 pathPrefix.AppendInt(aCounter.IntrinsicSize().height);
325 pathPrefix.AppendLiteral(", ");
327 if (aCounter.URI().IsEmpty()) {
328 pathPrefix.AppendLiteral("<unknown URI>");
329 } else {
330 pathPrefix.Append(aCounter.URI());
333 pathPrefix.AppendLiteral(")/");
335 ReportSurfaces(aHandleReport, aData, pathPrefix, aCounter, aSharedSurfaces);
337 ReportSourceValue(aHandleReport, aData, pathPrefix, aCounter.Values());
340 static void ReportSurfaces(
341 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
342 const nsACString& aPathPrefix, const ImageMemoryCounter& aCounter,
343 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
344 for (const SurfaceMemoryCounter& counter : aCounter.Surfaces()) {
345 nsAutoCString surfacePathPrefix(aPathPrefix);
346 switch (counter.Type()) {
347 case SurfaceMemoryCounterType::NORMAL:
348 if (counter.IsLocked()) {
349 surfacePathPrefix.AppendLiteral("locked/");
350 } else {
351 surfacePathPrefix.AppendLiteral("unlocked/");
353 if (counter.IsFactor2()) {
354 surfacePathPrefix.AppendLiteral("factor2/");
356 if (counter.CannotSubstitute()) {
357 surfacePathPrefix.AppendLiteral("cannot_substitute/");
359 break;
360 case SurfaceMemoryCounterType::CONTAINER:
361 surfacePathPrefix.AppendLiteral("container/");
362 break;
363 default:
364 MOZ_ASSERT_UNREACHABLE("Unknown counter type");
365 break;
368 surfacePathPrefix.AppendLiteral("types=");
369 surfacePathPrefix.AppendInt(counter.Values().SurfaceTypes(), 16);
370 surfacePathPrefix.AppendLiteral("/surface(");
371 surfacePathPrefix.AppendInt(counter.Key().Size().width);
372 surfacePathPrefix.AppendLiteral("x");
373 surfacePathPrefix.AppendInt(counter.Key().Size().height);
375 if (!counter.IsFinished()) {
376 surfacePathPrefix.AppendLiteral(", incomplete");
379 if (counter.Values().ExternalHandles() > 0) {
380 surfacePathPrefix.AppendLiteral(", handles:");
381 surfacePathPrefix.AppendInt(
382 uint32_t(counter.Values().ExternalHandles()));
385 ImageMemoryReporter::AppendSharedSurfacePrefix(surfacePathPrefix, counter,
386 aSharedSurfaces);
388 PlaybackType playback = counter.Key().Playback();
389 if (playback == PlaybackType::eAnimated) {
390 if (StaticPrefs::image_mem_debug_reporting()) {
391 surfacePathPrefix.AppendPrintf(
392 " (animation %4u)", uint32_t(counter.Values().FrameIndex()));
393 } else {
394 surfacePathPrefix.AppendLiteral(" (animation)");
398 if (counter.Key().Flags() != DefaultSurfaceFlags()) {
399 surfacePathPrefix.AppendLiteral(", flags:");
400 surfacePathPrefix.AppendInt(uint32_t(counter.Key().Flags()),
401 /* aRadix = */ 16);
404 if (counter.Key().Region()) {
405 const ImageIntRegion& region = counter.Key().Region().ref();
406 const gfx::IntRect& rect = region.Rect();
407 surfacePathPrefix.AppendLiteral(", region:[ rect=(");
408 surfacePathPrefix.AppendInt(rect.x);
409 surfacePathPrefix.AppendLiteral(",");
410 surfacePathPrefix.AppendInt(rect.y);
411 surfacePathPrefix.AppendLiteral(") ");
412 surfacePathPrefix.AppendInt(rect.width);
413 surfacePathPrefix.AppendLiteral("x");
414 surfacePathPrefix.AppendInt(rect.height);
415 if (region.IsRestricted()) {
416 const gfx::IntRect& restrict = region.Restriction();
417 if (restrict == rect) {
418 surfacePathPrefix.AppendLiteral(", restrict=rect");
419 } else {
420 surfacePathPrefix.AppendLiteral(", restrict=(");
421 surfacePathPrefix.AppendInt(restrict.x);
422 surfacePathPrefix.AppendLiteral(",");
423 surfacePathPrefix.AppendInt(restrict.y);
424 surfacePathPrefix.AppendLiteral(") ");
425 surfacePathPrefix.AppendInt(restrict.width);
426 surfacePathPrefix.AppendLiteral("x");
427 surfacePathPrefix.AppendInt(restrict.height);
430 if (region.GetExtendMode() != gfx::ExtendMode::CLAMP) {
431 surfacePathPrefix.AppendLiteral(", extendMode=");
432 surfacePathPrefix.AppendInt(int32_t(region.GetExtendMode()));
434 surfacePathPrefix.AppendLiteral("]");
437 const SVGImageContext& context = counter.Key().SVGContext();
438 surfacePathPrefix.AppendLiteral(", svgContext:[ ");
439 if (context.GetViewportSize()) {
440 const CSSIntSize& size = context.GetViewportSize().ref();
441 surfacePathPrefix.AppendLiteral("viewport=(");
442 surfacePathPrefix.AppendInt(size.width);
443 surfacePathPrefix.AppendLiteral("x");
444 surfacePathPrefix.AppendInt(size.height);
445 surfacePathPrefix.AppendLiteral(") ");
447 if (context.GetPreserveAspectRatio()) {
448 nsAutoString aspect;
449 context.GetPreserveAspectRatio()->ToString(aspect);
450 surfacePathPrefix.AppendLiteral("preserveAspectRatio=(");
451 LossyAppendUTF16toASCII(aspect, surfacePathPrefix);
452 surfacePathPrefix.AppendLiteral(") ");
454 if (auto scheme = context.GetColorScheme()) {
455 surfacePathPrefix.AppendLiteral("colorScheme=");
456 surfacePathPrefix.AppendInt(int32_t(*scheme));
457 surfacePathPrefix.AppendLiteral(" ");
459 if (context.GetContextPaint()) {
460 const SVGEmbeddingContextPaint* paint = context.GetContextPaint();
461 surfacePathPrefix.AppendLiteral("contextPaint=(");
462 if (paint->GetFill()) {
463 surfacePathPrefix.AppendLiteral(" fill=");
464 surfacePathPrefix.AppendInt(paint->GetFill()->ToABGR(), 16);
466 if (paint->GetFillOpacity() != 1.0) {
467 surfacePathPrefix.AppendLiteral(" fillOpa=");
468 surfacePathPrefix.AppendFloat(paint->GetFillOpacity());
470 if (paint->GetStroke()) {
471 surfacePathPrefix.AppendLiteral(" stroke=");
472 surfacePathPrefix.AppendInt(paint->GetStroke()->ToABGR(), 16);
474 if (paint->GetStrokeOpacity() != 1.0) {
475 surfacePathPrefix.AppendLiteral(" strokeOpa=");
476 surfacePathPrefix.AppendFloat(paint->GetStrokeOpacity());
478 surfacePathPrefix.AppendLiteral(" ) ");
480 surfacePathPrefix.AppendLiteral("]");
482 surfacePathPrefix.AppendLiteral(")/");
484 ReportValues(aHandleReport, aData, surfacePathPrefix, counter.Values());
488 static void ReportTotal(nsIHandleReportCallback* aHandleReport,
489 nsISupports* aData, bool aExplicit,
490 const char* aPathPrefix, const char* aPathInfix,
491 const MemoryTotal& aTotal) {
492 nsAutoCString pathPrefix;
493 if (aExplicit) {
494 pathPrefix.AppendLiteral("explicit/");
496 pathPrefix.Append(aPathPrefix);
498 nsAutoCString rasterUsedPrefix(pathPrefix);
499 rasterUsedPrefix.AppendLiteral("/raster/used/");
500 rasterUsedPrefix.Append(aPathInfix);
501 ReportValues(aHandleReport, aData, rasterUsedPrefix, aTotal.UsedRaster());
503 nsAutoCString rasterUnusedPrefix(pathPrefix);
504 rasterUnusedPrefix.AppendLiteral("/raster/unused/");
505 rasterUnusedPrefix.Append(aPathInfix);
506 ReportValues(aHandleReport, aData, rasterUnusedPrefix,
507 aTotal.UnusedRaster());
509 nsAutoCString vectorUsedPrefix(pathPrefix);
510 vectorUsedPrefix.AppendLiteral("/vector/used/");
511 vectorUsedPrefix.Append(aPathInfix);
512 ReportValues(aHandleReport, aData, vectorUsedPrefix, aTotal.UsedVector());
514 nsAutoCString vectorUnusedPrefix(pathPrefix);
515 vectorUnusedPrefix.AppendLiteral("/vector/unused/");
516 vectorUnusedPrefix.Append(aPathInfix);
517 ReportValues(aHandleReport, aData, vectorUnusedPrefix,
518 aTotal.UnusedVector());
521 static void ReportValues(nsIHandleReportCallback* aHandleReport,
522 nsISupports* aData, const nsACString& aPathPrefix,
523 const MemoryCounter& aCounter) {
524 ReportSourceValue(aHandleReport, aData, aPathPrefix, aCounter);
526 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "decoded-heap",
527 "Decoded image data which is stored on the heap.",
528 aCounter.DecodedHeap());
530 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
531 "decoded-nonheap",
532 "Decoded image data which isn't stored on the heap.",
533 aCounter.DecodedNonHeap());
535 // We don't know for certain whether or not it is on the heap, so let's
536 // just report it as non-heap for reporting purposes.
537 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
538 "decoded-unknown",
539 "Decoded image data which is unknown to be on the heap or not.",
540 aCounter.DecodedUnknown());
543 static void ReportSourceValue(nsIHandleReportCallback* aHandleReport,
544 nsISupports* aData,
545 const nsACString& aPathPrefix,
546 const MemoryCounter& aCounter) {
547 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "source",
548 "Raster image source data and vector image documents.",
549 aCounter.Source());
552 static void ReportValue(nsIHandleReportCallback* aHandleReport,
553 nsISupports* aData, int32_t aKind,
554 const nsACString& aPathPrefix,
555 const char* aPathSuffix, const char* aDescription,
556 size_t aValue) {
557 if (aValue == 0) {
558 return;
561 nsAutoCString desc(aDescription);
562 nsAutoCString path(aPathPrefix);
563 path.Append(aPathSuffix);
565 aHandleReport->Callback(""_ns, path, aKind, UNITS_BYTES, aValue, desc,
566 aData);
569 static void RecordCounterForRequest(imgRequest* aRequest,
570 nsTArray<ImageMemoryCounter>* aArray,
571 bool aIsUsed) {
572 SizeOfState state(ImagesMallocSizeOf);
573 RefPtr<image::Image> image = aRequest->GetImage();
574 if (image) {
575 ImageMemoryCounter counter(aRequest, image, state, aIsUsed);
576 aArray->AppendElement(std::move(counter));
577 } else {
578 // We can at least record some information about the image from the
579 // request, and mark it as not knowing the image type yet.
580 ImageMemoryCounter counter(aRequest, state, aIsUsed);
581 aArray->AppendElement(std::move(counter));
586 NS_IMPL_ISUPPORTS(imgMemoryReporter, nsIMemoryReporter)
588 NS_IMPL_ISUPPORTS(nsProgressNotificationProxy, nsIProgressEventSink,
589 nsIChannelEventSink, nsIInterfaceRequestor)
591 NS_IMETHODIMP
592 nsProgressNotificationProxy::OnProgress(nsIRequest* request, int64_t progress,
593 int64_t progressMax) {
594 nsCOMPtr<nsILoadGroup> loadGroup;
595 request->GetLoadGroup(getter_AddRefs(loadGroup));
597 nsCOMPtr<nsIProgressEventSink> target;
598 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
599 NS_GET_IID(nsIProgressEventSink),
600 getter_AddRefs(target));
601 if (!target) {
602 return NS_OK;
604 return target->OnProgress(mImageRequest, progress, progressMax);
607 NS_IMETHODIMP
608 nsProgressNotificationProxy::OnStatus(nsIRequest* request, nsresult status,
609 const char16_t* statusArg) {
610 nsCOMPtr<nsILoadGroup> loadGroup;
611 request->GetLoadGroup(getter_AddRefs(loadGroup));
613 nsCOMPtr<nsIProgressEventSink> target;
614 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
615 NS_GET_IID(nsIProgressEventSink),
616 getter_AddRefs(target));
617 if (!target) {
618 return NS_OK;
620 return target->OnStatus(mImageRequest, status, statusArg);
623 NS_IMETHODIMP
624 nsProgressNotificationProxy::AsyncOnChannelRedirect(
625 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
626 nsIAsyncVerifyRedirectCallback* cb) {
627 // Tell the original original callbacks about it too
628 nsCOMPtr<nsILoadGroup> loadGroup;
629 newChannel->GetLoadGroup(getter_AddRefs(loadGroup));
630 nsCOMPtr<nsIChannelEventSink> target;
631 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
632 NS_GET_IID(nsIChannelEventSink),
633 getter_AddRefs(target));
634 if (!target) {
635 cb->OnRedirectVerifyCallback(NS_OK);
636 return NS_OK;
639 // Delegate to |target| if set, reusing |cb|
640 return target->AsyncOnChannelRedirect(oldChannel, newChannel, flags, cb);
643 NS_IMETHODIMP
644 nsProgressNotificationProxy::GetInterface(const nsIID& iid, void** result) {
645 if (iid.Equals(NS_GET_IID(nsIProgressEventSink))) {
646 *result = static_cast<nsIProgressEventSink*>(this);
647 NS_ADDREF_THIS();
648 return NS_OK;
650 if (iid.Equals(NS_GET_IID(nsIChannelEventSink))) {
651 *result = static_cast<nsIChannelEventSink*>(this);
652 NS_ADDREF_THIS();
653 return NS_OK;
655 if (mOriginalCallbacks) {
656 return mOriginalCallbacks->GetInterface(iid, result);
658 return NS_NOINTERFACE;
661 static void NewRequestAndEntry(bool aForcePrincipalCheckForCacheEntry,
662 imgLoader* aLoader, const ImageCacheKey& aKey,
663 imgRequest** aRequest, imgCacheEntry** aEntry) {
664 RefPtr<imgRequest> request = new imgRequest(aLoader, aKey);
665 RefPtr<imgCacheEntry> entry =
666 new imgCacheEntry(aLoader, request, aForcePrincipalCheckForCacheEntry);
667 aLoader->AddToUncachedImages(request);
668 request.forget(aRequest);
669 entry.forget(aEntry);
672 static bool ShouldRevalidateEntry(imgCacheEntry* aEntry, nsLoadFlags aFlags,
673 bool aHasExpired) {
674 if (aFlags & nsIRequest::LOAD_BYPASS_CACHE) {
675 return false;
677 if (aFlags & nsIRequest::VALIDATE_ALWAYS) {
678 return true;
680 if (aEntry->GetMustValidate()) {
681 return true;
683 if (aHasExpired) {
684 // The cache entry has expired... Determine whether the stale cache
685 // entry can be used without validation...
686 if (aFlags & (nsIRequest::LOAD_FROM_CACHE | nsIRequest::VALIDATE_NEVER |
687 nsIRequest::VALIDATE_ONCE_PER_SESSION)) {
688 // LOAD_FROM_CACHE, VALIDATE_NEVER and VALIDATE_ONCE_PER_SESSION allow
689 // stale cache entries to be used unless they have been explicitly marked
690 // to indicate that revalidation is necessary.
691 return false;
693 // Entry is expired, revalidate.
694 return true;
696 return false;
699 /* Call content policies on cached images that went through a redirect */
700 static bool ShouldLoadCachedImage(imgRequest* aImgRequest,
701 Document* aLoadingDocument,
702 nsIPrincipal* aTriggeringPrincipal,
703 nsContentPolicyType aPolicyType,
704 bool aSendCSPViolationReports) {
705 /* Call content policies on cached images - Bug 1082837
706 * Cached images are keyed off of the first uri in a redirect chain.
707 * Hence content policies don't get a chance to test the intermediate hops
708 * or the final destination. Here we test the final destination using
709 * mFinalURI off of the imgRequest and passing it into content policies.
710 * For Mixed Content Blocker, we do an additional check to determine if any
711 * of the intermediary hops went through an insecure redirect with the
712 * mHadInsecureRedirect flag
714 bool insecureRedirect = aImgRequest->HadInsecureRedirect();
715 nsCOMPtr<nsIURI> contentLocation;
716 aImgRequest->GetFinalURI(getter_AddRefs(contentLocation));
717 nsresult rv;
719 nsCOMPtr<nsIPrincipal> loadingPrincipal =
720 aLoadingDocument ? aLoadingDocument->NodePrincipal()
721 : aTriggeringPrincipal;
722 // If there is no context and also no triggeringPrincipal, then we use a fresh
723 // nullPrincipal as the loadingPrincipal because we can not create a loadinfo
724 // without a valid loadingPrincipal.
725 if (!loadingPrincipal) {
726 loadingPrincipal = NullPrincipal::CreateWithoutOriginAttributes();
729 nsCOMPtr<nsILoadInfo> secCheckLoadInfo = new LoadInfo(
730 loadingPrincipal, aTriggeringPrincipal, aLoadingDocument,
731 nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK, aPolicyType);
733 secCheckLoadInfo->SetSendCSPViolationEvents(aSendCSPViolationReports);
735 int16_t decision = nsIContentPolicy::REJECT_REQUEST;
736 rv = NS_CheckContentLoadPolicy(contentLocation, secCheckLoadInfo, &decision,
737 nsContentUtils::GetContentPolicy());
738 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
739 return false;
742 // We call all Content Policies above, but we also have to call mcb
743 // individually to check the intermediary redirect hops are secure.
744 if (insecureRedirect) {
745 // Bug 1314356: If the image ended up in the cache upgraded by HSTS and the
746 // page uses upgrade-inscure-requests it had an insecure redirect
747 // (http->https). We need to invalidate the image and reload it because
748 // mixed content blocker only bails if upgrade-insecure-requests is set on
749 // the doc and the resource load is http: which would result in an incorrect
750 // mixed content warning.
751 nsCOMPtr<nsIDocShell> docShell =
752 NS_CP_GetDocShellFromContext(ToSupports(aLoadingDocument));
753 if (docShell) {
754 Document* document = docShell->GetDocument();
755 if (document && document->GetUpgradeInsecureRequests(false)) {
756 return false;
760 if (!aTriggeringPrincipal || !aTriggeringPrincipal->IsSystemPrincipal()) {
761 // reset the decision for mixed content blocker check
762 decision = nsIContentPolicy::REJECT_REQUEST;
763 rv = nsMixedContentBlocker::ShouldLoad(insecureRedirect, contentLocation,
764 secCheckLoadInfo,
765 true, // aReportError
766 &decision);
767 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
768 return false;
773 return true;
776 // Returns true if this request is compatible with the given CORS mode on the
777 // given loading principal, and false if the request may not be reused due
778 // to CORS.
779 static bool ValidateCORSMode(imgRequest* aRequest, bool aForcePrincipalCheck,
780 CORSMode aCORSMode,
781 nsIPrincipal* aTriggeringPrincipal) {
782 // If the entry's CORS mode doesn't match, or the CORS mode matches but the
783 // document principal isn't the same, we can't use this request.
784 if (aRequest->GetCORSMode() != aCORSMode) {
785 return false;
788 if (aRequest->GetCORSMode() != CORS_NONE || aForcePrincipalCheck) {
789 nsCOMPtr<nsIPrincipal> otherprincipal = aRequest->GetTriggeringPrincipal();
791 // If we previously had a principal, but we don't now, we can't use this
792 // request.
793 if (otherprincipal && !aTriggeringPrincipal) {
794 return false;
797 if (otherprincipal && aTriggeringPrincipal &&
798 !otherprincipal->Equals(aTriggeringPrincipal)) {
799 return false;
803 return true;
806 static bool ValidateSecurityInfo(imgRequest* aRequest,
807 bool aForcePrincipalCheck, CORSMode aCORSMode,
808 nsIPrincipal* aTriggeringPrincipal,
809 Document* aLoadingDocument,
810 nsContentPolicyType aPolicyType) {
811 if (!ValidateCORSMode(aRequest, aForcePrincipalCheck, aCORSMode,
812 aTriggeringPrincipal)) {
813 return false;
815 // Content Policy Check on Cached Images
816 return ShouldLoadCachedImage(aRequest, aLoadingDocument, aTriggeringPrincipal,
817 aPolicyType,
818 /* aSendCSPViolationReports */ false);
821 static void AdjustPriorityForImages(nsIChannel* aChannel,
822 nsLoadFlags aLoadFlags,
823 FetchPriority aFetchPriority) {
824 // Image channels are loaded by default with reduced priority.
825 if (nsCOMPtr<nsISupportsPriority> supportsPriority =
826 do_QueryInterface(aChannel)) {
827 int32_t priority = nsISupportsPriority::PRIORITY_LOW;
829 // Adjust priority according to fetchpriorty attribute.
830 if (StaticPrefs::network_fetchpriority_enabled()) {
831 priority += FETCH_PRIORITY_ADJUSTMENT_FOR(images, aFetchPriority);
834 // Further reduce priority for background loads
835 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
836 ++priority;
839 supportsPriority->AdjustPriority(priority);
842 if (nsCOMPtr<nsIClassOfService> cos = do_QueryInterface(aChannel)) {
843 cos->SetFetchPriorityDOM(aFetchPriority);
847 static nsresult NewImageChannel(
848 nsIChannel** aResult,
849 // If aForcePrincipalCheckForCacheEntry is true, then we will
850 // force a principal check even when not using CORS before
851 // assuming we have a cache hit on a cache entry that we
852 // create for this channel. This is an out param that should
853 // be set to true if this channel ends up depending on
854 // aTriggeringPrincipal and false otherwise.
855 bool* aForcePrincipalCheckForCacheEntry, nsIURI* aURI,
856 nsIURI* aInitialDocumentURI, CORSMode aCORSMode,
857 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
858 nsLoadFlags aLoadFlags, nsContentPolicyType aPolicyType,
859 nsIPrincipal* aTriggeringPrincipal, nsINode* aRequestingNode,
860 bool aRespectPrivacy, uint64_t aEarlyHintPreloaderId,
861 FetchPriority aFetchPriority) {
862 MOZ_ASSERT(aResult);
864 nsresult rv;
865 nsCOMPtr<nsIHttpChannel> newHttpChannel;
867 nsCOMPtr<nsIInterfaceRequestor> callbacks;
869 if (aLoadGroup) {
870 // Get the notification callbacks from the load group for the new channel.
872 // XXX: This is not exactly correct, because the network request could be
873 // referenced by multiple windows... However, the new channel needs
874 // something. So, using the 'first' notification callbacks is better
875 // than nothing...
877 aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
880 // Pass in a nullptr loadgroup because this is the underlying network
881 // request. This request may be referenced by several proxy image requests
882 // (possibly in different documents).
883 // If all of the proxy requests are canceled then this request should be
884 // canceled too.
887 nsSecurityFlags securityFlags =
888 nsContentSecurityManager::ComputeSecurityFlags(
889 aCORSMode, nsContentSecurityManager::CORSSecurityMapping::
890 CORS_NONE_MAPS_TO_INHERITED_CONTEXT);
892 securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
894 // Note we are calling NS_NewChannelWithTriggeringPrincipal() here with a
895 // node and a principal. This is for things like background images that are
896 // specified by user stylesheets, where the document is being styled, but
897 // the principal is that of the user stylesheet.
898 if (aRequestingNode && aTriggeringPrincipal) {
899 rv = NS_NewChannelWithTriggeringPrincipal(aResult, aURI, aRequestingNode,
900 aTriggeringPrincipal,
901 securityFlags, aPolicyType,
902 nullptr, // PerformanceStorage
903 nullptr, // loadGroup
904 callbacks, aLoadFlags);
906 if (NS_FAILED(rv)) {
907 return rv;
910 if (aPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
911 // If this is a favicon loading, we will use the originAttributes from the
912 // triggeringPrincipal as the channel's originAttributes. This allows the
913 // favicon loading from XUL will use the correct originAttributes.
915 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
916 rv = loadInfo->SetOriginAttributes(
917 aTriggeringPrincipal->OriginAttributesRef());
919 } else {
920 // either we are loading something inside a document, in which case
921 // we should always have a requestingNode, or we are loading something
922 // outside a document, in which case the triggeringPrincipal and
923 // triggeringPrincipal should always be the systemPrincipal.
924 // However, there are exceptions: one is Notifications which create a
925 // channel in the parent process in which case we can't get a
926 // requestingNode.
927 rv = NS_NewChannel(aResult, aURI, nsContentUtils::GetSystemPrincipal(),
928 securityFlags, aPolicyType,
929 nullptr, // nsICookieJarSettings
930 nullptr, // PerformanceStorage
931 nullptr, // loadGroup
932 callbacks, aLoadFlags);
934 if (NS_FAILED(rv)) {
935 return rv;
938 // Use the OriginAttributes from the loading principal, if one is available,
939 // and adjust the private browsing ID based on what kind of load the caller
940 // has asked us to perform.
941 OriginAttributes attrs;
942 if (aTriggeringPrincipal) {
943 attrs = aTriggeringPrincipal->OriginAttributesRef();
945 attrs.mPrivateBrowsingId = aRespectPrivacy ? 1 : 0;
947 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
948 rv = loadInfo->SetOriginAttributes(attrs);
951 if (NS_FAILED(rv)) {
952 return rv;
955 // only inherit if we have a principal
956 *aForcePrincipalCheckForCacheEntry =
957 aTriggeringPrincipal && nsContentUtils::ChannelShouldInheritPrincipal(
958 aTriggeringPrincipal, aURI,
959 /* aInheritForAboutBlank */ false,
960 /* aForceInherit */ false);
962 // Initialize HTTP-specific attributes
963 newHttpChannel = do_QueryInterface(*aResult);
964 if (newHttpChannel) {
965 nsCOMPtr<nsIHttpChannelInternal> httpChannelInternal =
966 do_QueryInterface(newHttpChannel);
967 NS_ENSURE_TRUE(httpChannelInternal, NS_ERROR_UNEXPECTED);
968 rv = httpChannelInternal->SetDocumentURI(aInitialDocumentURI);
969 MOZ_ASSERT(NS_SUCCEEDED(rv));
970 if (aReferrerInfo) {
971 DebugOnly<nsresult> rv = newHttpChannel->SetReferrerInfo(aReferrerInfo);
972 MOZ_ASSERT(NS_SUCCEEDED(rv));
975 if (aEarlyHintPreloaderId) {
976 rv = httpChannelInternal->SetEarlyHintPreloaderId(aEarlyHintPreloaderId);
977 NS_ENSURE_SUCCESS(rv, rv);
981 AdjustPriorityForImages(*aResult, aLoadFlags, aFetchPriority);
983 // Create a new loadgroup for this new channel, using the old group as
984 // the parent. The indirection keeps the channel insulated from cancels,
985 // but does allow a way for this revalidation to be associated with at
986 // least one base load group for scheduling/caching purposes.
988 nsCOMPtr<nsILoadGroup> loadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID);
989 nsCOMPtr<nsILoadGroupChild> childLoadGroup = do_QueryInterface(loadGroup);
990 if (childLoadGroup) {
991 childLoadGroup->SetParentLoadGroup(aLoadGroup);
993 (*aResult)->SetLoadGroup(loadGroup);
995 return NS_OK;
998 static uint32_t SecondsFromPRTime(PRTime aTime) {
999 return nsContentUtils::SecondsFromPRTime(aTime);
1002 /* static */
1003 imgCacheEntry::imgCacheEntry(imgLoader* loader, imgRequest* request,
1004 bool forcePrincipalCheck)
1005 : mLoader(loader),
1006 mRequest(request),
1007 mDataSize(0),
1008 mTouchedTime(SecondsFromPRTime(PR_Now())),
1009 mLoadTime(SecondsFromPRTime(PR_Now())),
1010 mExpiryTime(CacheExpirationTime::Never()),
1011 mMustValidate(false),
1012 // We start off as evicted so we don't try to update the cache.
1013 // PutIntoCache will set this to false.
1014 mEvicted(true),
1015 mHasNoProxies(true),
1016 mForcePrincipalCheck(forcePrincipalCheck),
1017 mHasNotified(false) {}
1019 imgCacheEntry::~imgCacheEntry() {
1020 LOG_FUNC(gImgLog, "imgCacheEntry::~imgCacheEntry()");
1023 void imgCacheEntry::Touch(bool updateTime /* = true */) {
1024 LOG_SCOPE(gImgLog, "imgCacheEntry::Touch");
1026 if (updateTime) {
1027 mTouchedTime = SecondsFromPRTime(PR_Now());
1030 UpdateCache();
1033 void imgCacheEntry::UpdateCache(int32_t diff /* = 0 */) {
1034 // Don't update the cache if we've been removed from it or it doesn't care
1035 // about our size or usage.
1036 if (!Evicted() && HasNoProxies()) {
1037 mLoader->CacheEntriesChanged(diff);
1041 void imgCacheEntry::UpdateLoadTime() {
1042 mLoadTime = SecondsFromPRTime(PR_Now());
1045 void imgCacheEntry::SetHasNoProxies(bool hasNoProxies) {
1046 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1047 if (hasNoProxies) {
1048 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies true", "uri",
1049 mRequest->CacheKey().URI());
1050 } else {
1051 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies false",
1052 "uri", mRequest->CacheKey().URI());
1056 mHasNoProxies = hasNoProxies;
1059 imgCacheQueue::imgCacheQueue() : mDirty(false), mSize(0) {}
1061 void imgCacheQueue::UpdateSize(int32_t diff) { mSize += diff; }
1063 uint32_t imgCacheQueue::GetSize() const { return mSize; }
1065 void imgCacheQueue::Remove(imgCacheEntry* entry) {
1066 uint64_t index = mQueue.IndexOf(entry);
1067 if (index == queueContainer::NoIndex) {
1068 return;
1071 mSize -= mQueue[index]->GetDataSize();
1073 // If the queue is clean and this is the first entry,
1074 // then we can efficiently remove the entry without
1075 // dirtying the sort order.
1076 if (!IsDirty() && index == 0) {
1077 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1078 mQueue.RemoveLastElement();
1079 return;
1082 // Remove from the middle of the list. This potentially
1083 // breaks the binary heap sort order.
1084 mQueue.RemoveElementAt(index);
1086 // If we only have one entry or the queue is empty, though,
1087 // then the sort order is still effectively good. Simply
1088 // refresh the list to clear the dirty flag.
1089 if (mQueue.Length() <= 1) {
1090 Refresh();
1091 return;
1094 // Otherwise we must mark the queue dirty and potentially
1095 // trigger an expensive sort later.
1096 MarkDirty();
1099 void imgCacheQueue::Push(imgCacheEntry* entry) {
1100 mSize += entry->GetDataSize();
1102 RefPtr<imgCacheEntry> refptr(entry);
1103 mQueue.AppendElement(std::move(refptr));
1104 // If we're not dirty already, then we can efficiently add this to the
1105 // binary heap immediately. This is only O(log n).
1106 if (!IsDirty()) {
1107 std::push_heap(mQueue.begin(), mQueue.end(),
1108 imgLoader::CompareCacheEntries);
1112 already_AddRefed<imgCacheEntry> imgCacheQueue::Pop() {
1113 if (mQueue.IsEmpty()) {
1114 return nullptr;
1116 if (IsDirty()) {
1117 Refresh();
1120 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1121 RefPtr<imgCacheEntry> entry = mQueue.PopLastElement();
1123 mSize -= entry->GetDataSize();
1124 return entry.forget();
1127 void imgCacheQueue::Refresh() {
1128 // Resort the list. This is an O(3 * n) operation and best avoided
1129 // if possible.
1130 std::make_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1131 mDirty = false;
1134 void imgCacheQueue::MarkDirty() { mDirty = true; }
1136 bool imgCacheQueue::IsDirty() { return mDirty; }
1138 uint32_t imgCacheQueue::GetNumElements() const { return mQueue.Length(); }
1140 bool imgCacheQueue::Contains(imgCacheEntry* aEntry) const {
1141 return mQueue.Contains(aEntry);
1144 imgCacheQueue::iterator imgCacheQueue::begin() { return mQueue.begin(); }
1146 imgCacheQueue::const_iterator imgCacheQueue::begin() const {
1147 return mQueue.begin();
1150 imgCacheQueue::iterator imgCacheQueue::end() { return mQueue.end(); }
1152 imgCacheQueue::const_iterator imgCacheQueue::end() const {
1153 return mQueue.end();
1156 nsresult imgLoader::CreateNewProxyForRequest(
1157 imgRequest* aRequest, nsIURI* aURI, nsILoadGroup* aLoadGroup,
1158 Document* aLoadingDocument, imgINotificationObserver* aObserver,
1159 nsLoadFlags aLoadFlags, imgRequestProxy** _retval) {
1160 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::CreateNewProxyForRequest",
1161 "imgRequest", aRequest);
1163 /* XXX If we move decoding onto separate threads, we should save off the
1164 calling thread here and pass it off to |proxyRequest| so that it call
1165 proxy calls to |aObserver|.
1168 RefPtr<imgRequestProxy> proxyRequest = new imgRequestProxy();
1170 /* It is important to call |SetLoadFlags()| before calling |Init()| because
1171 |Init()| adds the request to the loadgroup.
1173 proxyRequest->SetLoadFlags(aLoadFlags);
1175 // init adds itself to imgRequest's list of observers
1176 nsresult rv = proxyRequest->Init(aRequest, aLoadGroup, aURI, aObserver);
1177 if (NS_WARN_IF(NS_FAILED(rv))) {
1178 return rv;
1181 proxyRequest.forget(_retval);
1182 return NS_OK;
1185 class imgCacheExpirationTracker final
1186 : public nsExpirationTracker<imgCacheEntry, 3> {
1187 enum { TIMEOUT_SECONDS = 10 };
1189 public:
1190 imgCacheExpirationTracker();
1192 protected:
1193 void NotifyExpired(imgCacheEntry* entry) override;
1196 imgCacheExpirationTracker::imgCacheExpirationTracker()
1197 : nsExpirationTracker<imgCacheEntry, 3>(TIMEOUT_SECONDS * 1000,
1198 "imgCacheExpirationTracker") {}
1200 void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry* entry) {
1201 // Hold on to a reference to this entry, because the expiration tracker
1202 // mechanism doesn't.
1203 RefPtr<imgCacheEntry> kungFuDeathGrip(entry);
1205 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1206 RefPtr<imgRequest> req = entry->GetRequest();
1207 if (req) {
1208 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheExpirationTracker::NotifyExpired",
1209 "entry", req->CacheKey().URI());
1213 // We can be called multiple times on the same entry. Don't do work multiple
1214 // times.
1215 if (!entry->Evicted()) {
1216 entry->Loader()->RemoveFromCache(entry);
1219 entry->Loader()->VerifyCacheSizes();
1222 ///////////////////////////////////////////////////////////////////////////////
1223 // imgLoader
1224 ///////////////////////////////////////////////////////////////////////////////
1226 double imgLoader::sCacheTimeWeight;
1227 uint32_t imgLoader::sCacheMaxSize;
1228 imgMemoryReporter* imgLoader::sMemReporter;
1230 NS_IMPL_ISUPPORTS(imgLoader, imgILoader, nsIContentSniffer, imgICache,
1231 nsISupportsWeakReference, nsIObserver)
1233 static imgLoader* gNormalLoader = nullptr;
1234 static imgLoader* gPrivateBrowsingLoader = nullptr;
1236 /* static */
1237 already_AddRefed<imgLoader> imgLoader::CreateImageLoader() {
1238 // In some cases, such as xpctests, XPCOM modules are not automatically
1239 // initialized. We need to make sure that our module is initialized before
1240 // we hand out imgLoader instances and code starts using them.
1241 mozilla::image::EnsureModuleInitialized();
1243 RefPtr<imgLoader> loader = new imgLoader();
1244 loader->Init();
1246 return loader.forget();
1249 imgLoader* imgLoader::NormalLoader() {
1250 if (!gNormalLoader) {
1251 gNormalLoader = CreateImageLoader().take();
1253 return gNormalLoader;
1256 imgLoader* imgLoader::PrivateBrowsingLoader() {
1257 if (!gPrivateBrowsingLoader) {
1258 gPrivateBrowsingLoader = CreateImageLoader().take();
1259 gPrivateBrowsingLoader->RespectPrivacyNotifications();
1261 return gPrivateBrowsingLoader;
1264 imgLoader::imgLoader()
1265 : mUncachedImagesMutex("imgLoader::UncachedImages"),
1266 mRespectPrivacy(false) {
1267 sMemReporter->AddRef();
1268 sMemReporter->RegisterLoader(this);
1271 imgLoader::~imgLoader() {
1272 ClearImageCache();
1274 // If there are any of our imgRequest's left they are in the uncached
1275 // images set, so clear their pointer to us.
1276 MutexAutoLock lock(mUncachedImagesMutex);
1277 for (RefPtr<imgRequest> req : mUncachedImages) {
1278 req->ClearLoader();
1281 sMemReporter->UnregisterLoader(this);
1282 sMemReporter->Release();
1285 void imgLoader::VerifyCacheSizes() {
1286 #ifdef DEBUG
1287 if (!mCacheTracker) {
1288 return;
1291 uint32_t cachesize = mCache.Count();
1292 uint32_t queuesize = mCacheQueue.GetNumElements();
1293 uint32_t trackersize = 0;
1294 for (nsExpirationTracker<imgCacheEntry, 3>::Iterator it(mCacheTracker.get());
1295 it.Next();) {
1296 trackersize++;
1298 MOZ_ASSERT(queuesize == trackersize, "Queue and tracker sizes out of sync!");
1299 MOZ_ASSERT(queuesize <= cachesize, "Queue has more elements than cache!");
1300 #endif
1303 void imgLoader::GlobalInit() {
1304 sCacheTimeWeight = StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
1305 int32_t cachesize = StaticPrefs::image_cache_size_AtStartup();
1306 sCacheMaxSize = cachesize > 0 ? cachesize : 0;
1308 sMemReporter = new imgMemoryReporter();
1309 RegisterStrongAsyncMemoryReporter(sMemReporter);
1310 RegisterImagesContentUsedUncompressedDistinguishedAmount(
1311 imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount);
1314 void imgLoader::ShutdownMemoryReporter() {
1315 UnregisterImagesContentUsedUncompressedDistinguishedAmount();
1316 UnregisterStrongMemoryReporter(sMemReporter);
1319 nsresult imgLoader::InitCache() {
1320 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
1321 if (!os) {
1322 return NS_ERROR_FAILURE;
1325 os->AddObserver(this, "memory-pressure", false);
1326 os->AddObserver(this, "chrome-flush-caches", false);
1327 os->AddObserver(this, "last-pb-context-exited", false);
1328 os->AddObserver(this, "profile-before-change", false);
1329 os->AddObserver(this, "xpcom-shutdown", false);
1331 mCacheTracker = MakeUnique<imgCacheExpirationTracker>();
1333 return NS_OK;
1336 nsresult imgLoader::Init() {
1337 InitCache();
1339 return NS_OK;
1342 NS_IMETHODIMP
1343 imgLoader::RespectPrivacyNotifications() {
1344 mRespectPrivacy = true;
1345 return NS_OK;
1348 NS_IMETHODIMP
1349 imgLoader::Observe(nsISupports* aSubject, const char* aTopic,
1350 const char16_t* aData) {
1351 if (strcmp(aTopic, "memory-pressure") == 0) {
1352 MinimizeCache();
1353 } else if (strcmp(aTopic, "chrome-flush-caches") == 0) {
1354 MinimizeCache();
1355 ClearImageCache({ClearOption::ChromeOnly});
1356 } else if (strcmp(aTopic, "last-pb-context-exited") == 0) {
1357 if (mRespectPrivacy) {
1358 ClearImageCache();
1360 } else if (strcmp(aTopic, "profile-before-change") == 0) {
1361 mCacheTracker = nullptr;
1362 } else if (strcmp(aTopic, "xpcom-shutdown") == 0) {
1363 mCacheTracker = nullptr;
1364 ShutdownMemoryReporter();
1366 } else {
1367 // (Nothing else should bring us here)
1368 MOZ_ASSERT(0, "Invalid topic received");
1371 return NS_OK;
1374 NS_IMETHODIMP
1375 imgLoader::ClearCache(bool chrome) {
1376 if (XRE_IsParentProcess()) {
1377 bool privateLoader = this == gPrivateBrowsingLoader;
1378 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1379 Unused << cp->SendClearImageCache(privateLoader, chrome);
1382 ClearOptions options;
1383 if (chrome) {
1384 options += ClearOption::ChromeOnly;
1386 return ClearImageCache(options);
1389 NS_IMETHODIMP
1390 imgLoader::RemoveEntriesFromPrincipalInAllProcesses(nsIPrincipal* aPrincipal) {
1391 if (!XRE_IsParentProcess()) {
1392 return NS_ERROR_NOT_AVAILABLE;
1395 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1396 Unused << cp->SendClearImageCacheFromPrincipal(aPrincipal);
1399 imgLoader* loader;
1400 if (aPrincipal->OriginAttributesRef().IsPrivateBrowsing()) {
1401 loader = imgLoader::PrivateBrowsingLoader();
1402 } else {
1403 loader = imgLoader::NormalLoader();
1406 return loader->RemoveEntriesInternal(Some(aPrincipal), Nothing(), Nothing());
1409 NS_IMETHODIMP
1410 imgLoader::RemoveEntriesFromSiteInAllProcesses(
1411 const nsACString& aSchemelessSite,
1412 JS::Handle<JS::Value> aOriginAttributesPattern, JSContext* aCx) {
1413 if (!XRE_IsParentProcess()) {
1414 return NS_ERROR_NOT_AVAILABLE;
1417 OriginAttributesPattern pattern;
1418 if (!aOriginAttributesPattern.isObject() ||
1419 !pattern.Init(aCx, aOriginAttributesPattern)) {
1420 return NS_ERROR_INVALID_ARG;
1423 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1424 Unused << cp->SendClearImageCacheFromSite(aSchemelessSite, pattern);
1427 return RemoveEntriesInternal(Nothing(), Some(nsCString(aSchemelessSite)),
1428 Some(pattern));
1431 nsresult imgLoader::RemoveEntriesInternal(
1432 const Maybe<nsCOMPtr<nsIPrincipal>>& aPrincipal,
1433 const Maybe<nsCString>& aSchemelessSite,
1434 const Maybe<OriginAttributesPattern>& aPattern) {
1435 // Can only clear by either principal or site + pattern.
1436 if ((!aPrincipal && !aSchemelessSite) || (aPrincipal && aSchemelessSite) ||
1437 aSchemelessSite.isSome() != aPattern.isSome()) {
1438 return NS_ERROR_INVALID_ARG;
1441 nsCOMPtr<nsIEffectiveTLDService> tldService;
1442 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1444 Maybe<OriginAttributesPattern> patternWithPartitionKey = Nothing();
1445 if (aPattern) {
1446 // Used for checking for cache entries partitioned under aSchemelessSite.
1447 OriginAttributesPattern pattern(aPattern.ref());
1448 pattern.mPartitionKeyPattern.Construct();
1449 pattern.mPartitionKeyPattern.Value().mBaseDomain.Construct(
1450 NS_ConvertUTF8toUTF16(aSchemelessSite.ref()));
1452 patternWithPartitionKey.emplace(std::move(pattern));
1455 // For base domain we only clear the non-chrome cache.
1456 for (const auto& entry : mCache) {
1457 const auto& key = entry.GetKey();
1459 const bool shouldRemove = [&] {
1460 // The isolation key is either just the site, or an origin suffix
1461 // which contains the partitionKey holding the baseDomain.
1463 if (aPrincipal) {
1464 nsCOMPtr<nsIPrincipal> keyPrincipal =
1465 BasePrincipal::CreateContentPrincipal(key.URI(),
1466 key.OriginAttributesRef());
1467 return keyPrincipal->Equals(aPrincipal.ref());
1470 if (!aSchemelessSite) {
1471 return false;
1473 // Clear by site and pattern.
1474 nsAutoCString host;
1475 nsresult rv = key.URI()->GetHost(host);
1476 if (NS_FAILED(rv) || host.IsEmpty()) {
1477 return false;
1480 if (!tldService) {
1481 tldService = do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
1483 if (NS_WARN_IF(!tldService)) {
1484 return false;
1487 bool hasRootDomain = false;
1488 rv = tldService->HasRootDomain(host, aSchemelessSite.ref(),
1489 &hasRootDomain);
1490 if (NS_WARN_IF(NS_FAILED(rv))) {
1491 return false;
1494 if (hasRootDomain && aPattern->Matches(key.OriginAttributesRef())) {
1495 return true;
1498 // Attempt to parse isolation key into origin attributes.
1499 Maybe<OriginAttributes> originAttributesWithPartitionKey;
1501 OriginAttributes attrs;
1502 if (attrs.PopulateFromSuffix(key.IsolationKeyRef())) {
1503 OriginAttributes attrsWithPartitionKey(key.OriginAttributesRef());
1504 attrsWithPartitionKey.mPartitionKey = attrs.mPartitionKey;
1505 originAttributesWithPartitionKey.emplace(
1506 std::move(attrsWithPartitionKey));
1510 // Match it against the pattern that contains the partition key and any
1511 // fields set by the caller pattern.
1512 if (originAttributesWithPartitionKey.isSome()) {
1513 nsAutoCString oaSuffixForPrinting;
1514 originAttributesWithPartitionKey->CreateSuffix(oaSuffixForPrinting);
1516 nsAutoString patternForPrinting;
1517 patternWithPartitionKey->ToJSON(patternForPrinting);
1519 return patternWithPartitionKey.ref().Matches(
1520 originAttributesWithPartitionKey.ref());
1523 // The isolation key is the site.
1524 return aSchemelessSite->Equals(key.IsolationKeyRef());
1525 }();
1527 if (shouldRemove) {
1528 entriesToBeRemoved.AppendElement(entry.GetData());
1532 for (auto& entry : entriesToBeRemoved) {
1533 if (!RemoveFromCache(entry)) {
1534 NS_WARNING(
1535 "Couldn't remove an entry from the cache in "
1536 "RemoveEntriesInternal()\n");
1540 return NS_OK;
1543 constexpr auto AllCORSModes() {
1544 return MakeInclusiveEnumeratedRange(kFirstCORSMode, kLastCORSMode);
1547 NS_IMETHODIMP
1548 imgLoader::RemoveEntry(nsIURI* aURI, Document* aDoc) {
1549 if (!aURI) {
1550 return NS_OK;
1552 OriginAttributes attrs;
1553 if (aDoc) {
1554 attrs = aDoc->NodePrincipal()->OriginAttributesRef();
1556 for (auto corsMode : AllCORSModes()) {
1557 ImageCacheKey key(aURI, corsMode, attrs, aDoc);
1558 RemoveFromCache(key);
1560 return NS_OK;
1563 NS_IMETHODIMP
1564 imgLoader::FindEntryProperties(nsIURI* uri, Document* aDoc,
1565 nsIProperties** _retval) {
1566 *_retval = nullptr;
1568 OriginAttributes attrs;
1569 if (aDoc) {
1570 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1571 if (principal) {
1572 attrs = principal->OriginAttributesRef();
1576 for (auto corsMode : AllCORSModes()) {
1577 ImageCacheKey key(uri, corsMode, attrs, aDoc);
1578 RefPtr<imgCacheEntry> entry;
1579 if (!mCache.Get(key, getter_AddRefs(entry)) || !entry) {
1580 continue;
1582 if (mCacheTracker && entry->HasNoProxies()) {
1583 mCacheTracker->MarkUsed(entry);
1585 RefPtr<imgRequest> request = entry->GetRequest();
1586 if (request) {
1587 nsCOMPtr<nsIProperties> properties = request->Properties();
1588 properties.forget(_retval);
1589 return NS_OK;
1592 return NS_OK;
1595 NS_IMETHODIMP_(void)
1596 imgLoader::ClearCacheForControlledDocument(Document* aDoc) {
1597 MOZ_ASSERT(aDoc);
1598 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1599 for (const auto& entry : mCache) {
1600 const auto& key = entry.GetKey();
1601 if (key.ControlledDocument() == aDoc) {
1602 entriesToBeRemoved.AppendElement(entry.GetData());
1605 for (auto& entry : entriesToBeRemoved) {
1606 if (!RemoveFromCache(entry)) {
1607 NS_WARNING(
1608 "Couldn't remove an entry from the cache in "
1609 "ClearCacheForControlledDocument()\n");
1614 void imgLoader::Shutdown() {
1615 NS_IF_RELEASE(gNormalLoader);
1616 gNormalLoader = nullptr;
1617 NS_IF_RELEASE(gPrivateBrowsingLoader);
1618 gPrivateBrowsingLoader = nullptr;
1621 bool imgLoader::PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* entry) {
1622 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::PutIntoCache", "uri",
1623 aKey.URI());
1625 // Check to see if this request already exists in the cache. If so, we'll
1626 // replace the old version.
1627 RefPtr<imgCacheEntry> tmpCacheEntry;
1628 if (mCache.Get(aKey, getter_AddRefs(tmpCacheEntry)) && tmpCacheEntry) {
1629 MOZ_LOG(
1630 gImgLog, LogLevel::Debug,
1631 ("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
1632 nullptr));
1633 RefPtr<imgRequest> tmpRequest = tmpCacheEntry->GetRequest();
1635 // If it already exists, and we're putting the same key into the cache, we
1636 // should remove the old version.
1637 MOZ_LOG(gImgLog, LogLevel::Debug,
1638 ("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
1639 nullptr));
1641 RemoveFromCache(aKey);
1642 } else {
1643 MOZ_LOG(gImgLog, LogLevel::Debug,
1644 ("[this=%p] imgLoader::PutIntoCache --"
1645 " Element NOT already in the cache",
1646 nullptr));
1649 mCache.InsertOrUpdate(aKey, RefPtr{entry});
1651 // We can be called to resurrect an evicted entry.
1652 if (entry->Evicted()) {
1653 entry->SetEvicted(false);
1656 // If we're resurrecting an entry with no proxies, put it back in the
1657 // tracker and queue.
1658 if (entry->HasNoProxies()) {
1659 nsresult addrv = NS_OK;
1661 if (mCacheTracker) {
1662 addrv = mCacheTracker->AddObject(entry);
1665 if (NS_SUCCEEDED(addrv)) {
1666 mCacheQueue.Push(entry);
1670 RefPtr<imgRequest> request = entry->GetRequest();
1671 request->SetIsInCache(true);
1672 RemoveFromUncachedImages(request);
1674 return true;
1677 bool imgLoader::SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry) {
1678 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasNoProxies", "uri",
1679 aRequest->CacheKey().URI());
1681 aEntry->SetHasNoProxies(true);
1683 if (aEntry->Evicted()) {
1684 return false;
1687 nsresult addrv = NS_OK;
1689 if (mCacheTracker) {
1690 addrv = mCacheTracker->AddObject(aEntry);
1693 if (NS_SUCCEEDED(addrv)) {
1694 mCacheQueue.Push(aEntry);
1697 return true;
1700 bool imgLoader::SetHasProxies(imgRequest* aRequest) {
1701 VerifyCacheSizes();
1703 const ImageCacheKey& key = aRequest->CacheKey();
1705 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasProxies", "uri",
1706 key.URI());
1708 RefPtr<imgCacheEntry> entry;
1709 if (mCache.Get(key, getter_AddRefs(entry)) && entry) {
1710 // Make sure the cache entry is for the right request
1711 RefPtr<imgRequest> entryRequest = entry->GetRequest();
1712 if (entryRequest == aRequest && entry->HasNoProxies()) {
1713 mCacheQueue.Remove(entry);
1715 if (mCacheTracker) {
1716 mCacheTracker->RemoveObject(entry);
1719 entry->SetHasNoProxies(false);
1721 return true;
1725 return false;
1728 void imgLoader::CacheEntriesChanged(int32_t aSizeDiff /* = 0 */) {
1729 // We only need to dirty the queue if there is any sorting
1730 // taking place. Empty or single-entry lists can't become
1731 // dirty.
1732 if (mCacheQueue.GetNumElements() > 1) {
1733 mCacheQueue.MarkDirty();
1735 mCacheQueue.UpdateSize(aSizeDiff);
1738 void imgLoader::CheckCacheLimits() {
1739 if (mCacheQueue.GetNumElements() == 0) {
1740 NS_ASSERTION(mCacheQueue.GetSize() == 0,
1741 "imgLoader::CheckCacheLimits -- incorrect cache size");
1744 // Remove entries from the cache until we're back at our desired max size.
1745 while (mCacheQueue.GetSize() > sCacheMaxSize) {
1746 // Remove the first entry in the queue.
1747 RefPtr<imgCacheEntry> entry(mCacheQueue.Pop());
1749 NS_ASSERTION(entry, "imgLoader::CheckCacheLimits -- NULL entry pointer");
1751 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1752 RefPtr<imgRequest> req = entry->GetRequest();
1753 if (req) {
1754 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::CheckCacheLimits",
1755 "entry", req->CacheKey().URI());
1759 if (entry) {
1760 // We just popped this entry from the queue, so pass AlreadyRemoved
1761 // to avoid searching the queue again in RemoveFromCache.
1762 RemoveFromCache(entry, QueueState::AlreadyRemoved);
1767 bool imgLoader::ValidateRequestWithNewChannel(
1768 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1769 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1770 imgINotificationObserver* aObserver, Document* aLoadingDocument,
1771 uint64_t aInnerWindowId, nsLoadFlags aLoadFlags,
1772 nsContentPolicyType aLoadPolicyType, imgRequestProxy** aProxyRequest,
1773 nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode, bool aLinkPreload,
1774 uint64_t aEarlyHintPreloaderId, FetchPriority aFetchPriority,
1775 bool* aNewChannelCreated) {
1776 // now we need to insert a new channel request object in between the real
1777 // request and the proxy that basically delays loading the image until it
1778 // gets a 304 or figures out that this needs to be a new request
1780 nsresult rv;
1782 // If we're currently in the middle of validating this request, just hand
1783 // back a proxy to it; the required work will be done for us.
1784 if (imgCacheValidator* validator = request->GetValidator()) {
1785 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
1786 aObserver, aLoadFlags, aProxyRequest);
1787 if (NS_FAILED(rv)) {
1788 return false;
1791 if (*aProxyRequest) {
1792 imgRequestProxy* proxy = static_cast<imgRequestProxy*>(*aProxyRequest);
1794 // We will send notifications from imgCacheValidator::OnStartRequest().
1795 // In the mean time, we must defer notifications because we are added to
1796 // the imgRequest's proxy list, and we can get extra notifications
1797 // resulting from methods such as StartDecoding(). See bug 579122.
1798 proxy->MarkValidating();
1800 if (aLinkPreload) {
1801 MOZ_ASSERT(aLoadingDocument);
1802 auto preloadKey = PreloadHashKey::CreateAsImage(
1803 aURI, aTriggeringPrincipal, aCORSMode);
1804 proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
1807 // Attach the proxy without notifying
1808 validator->AddProxy(proxy);
1811 return true;
1813 // We will rely on Necko to cache this request when it's possible, and to
1814 // tell imgCacheValidator::OnStartRequest whether the request came from its
1815 // cache.
1816 nsCOMPtr<nsIChannel> newChannel;
1817 bool forcePrincipalCheck;
1818 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
1819 aInitialDocumentURI, aCORSMode, aReferrerInfo,
1820 aLoadGroup, aLoadFlags, aLoadPolicyType,
1821 aTriggeringPrincipal, aLoadingDocument, mRespectPrivacy,
1822 aEarlyHintPreloaderId, aFetchPriority);
1823 if (NS_FAILED(rv)) {
1824 return false;
1827 if (aNewChannelCreated) {
1828 *aNewChannelCreated = true;
1831 RefPtr<imgRequestProxy> req;
1832 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
1833 aObserver, aLoadFlags, getter_AddRefs(req));
1834 if (NS_FAILED(rv)) {
1835 return false;
1838 // Make sure that OnStatus/OnProgress calls have the right request set...
1839 RefPtr<nsProgressNotificationProxy> progressproxy =
1840 new nsProgressNotificationProxy(newChannel, req);
1841 if (!progressproxy) {
1842 return false;
1845 RefPtr<imgCacheValidator> hvc =
1846 new imgCacheValidator(progressproxy, this, request, aLoadingDocument,
1847 aInnerWindowId, forcePrincipalCheck);
1849 // Casting needed here to get past multiple inheritance.
1850 nsCOMPtr<nsIStreamListener> listener =
1851 static_cast<nsIThreadRetargetableStreamListener*>(hvc);
1852 NS_ENSURE_TRUE(listener, false);
1854 // We must set the notification callbacks before setting up the
1855 // CORS listener, because that's also interested inthe
1856 // notification callbacks.
1857 newChannel->SetNotificationCallbacks(hvc);
1859 request->SetValidator(hvc);
1861 // We will send notifications from imgCacheValidator::OnStartRequest().
1862 // In the mean time, we must defer notifications because we are added to
1863 // the imgRequest's proxy list, and we can get extra notifications
1864 // resulting from methods such as StartDecoding(). See bug 579122.
1865 req->MarkValidating();
1867 if (aLinkPreload) {
1868 MOZ_ASSERT(aLoadingDocument);
1869 auto preloadKey =
1870 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, aCORSMode);
1871 req->NotifyOpen(preloadKey, aLoadingDocument, true);
1874 // Add the proxy without notifying
1875 hvc->AddProxy(req);
1877 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
1878 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
1879 aLoadGroup);
1880 rv = newChannel->AsyncOpen(listener);
1881 if (NS_WARN_IF(NS_FAILED(rv))) {
1882 req->CancelAndForgetObserver(rv);
1883 // This will notify any current or future <link preload> tags. Pass the
1884 // non-open channel so that we can read loadinfo and referrer info of that
1885 // channel.
1886 req->NotifyStart(newChannel);
1887 // Use the non-channel overload of this method to force the notification to
1888 // happen. The preload request has not been assigned a channel.
1889 req->NotifyStop(rv);
1890 return false;
1893 req.forget(aProxyRequest);
1894 return true;
1897 void imgLoader::NotifyObserversForCachedImage(
1898 imgCacheEntry* aEntry, imgRequest* request, nsIURI* aURI,
1899 nsIReferrerInfo* aReferrerInfo, Document* aLoadingDocument,
1900 nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode,
1901 uint64_t aEarlyHintPreloaderId, FetchPriority aFetchPriority) {
1902 if (aEntry->HasNotified()) {
1903 return;
1906 nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
1908 if (!obsService->HasObservers("http-on-resource-cache-response")) {
1909 return;
1912 aEntry->SetHasNotified();
1914 nsCOMPtr<nsIChannel> newChannel;
1915 bool forcePrincipalCheck;
1916 nsresult rv = NewImageChannel(
1917 getter_AddRefs(newChannel), &forcePrincipalCheck, aURI, nullptr,
1918 aCORSMode, aReferrerInfo, nullptr, 0,
1919 nsIContentPolicy::TYPE_INTERNAL_IMAGE, aTriggeringPrincipal,
1920 aLoadingDocument, mRespectPrivacy, aEarlyHintPreloaderId, aFetchPriority);
1921 if (NS_FAILED(rv)) {
1922 return;
1925 RefPtr<HttpBaseChannel> httpBaseChannel = do_QueryObject(newChannel);
1926 if (httpBaseChannel) {
1927 httpBaseChannel->SetDummyChannelForCachedResource();
1928 newChannel->SetContentType(nsDependentCString(request->GetMimeType()));
1929 RefPtr<mozilla::image::Image> image = request->GetImage();
1930 if (image) {
1931 newChannel->SetContentLength(aEntry->GetDataSize());
1933 obsService->NotifyObservers(newChannel, "http-on-resource-cache-response",
1934 nullptr);
1938 bool imgLoader::ValidateEntry(
1939 imgCacheEntry* aEntry, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1940 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1941 imgINotificationObserver* aObserver, Document* aLoadingDocument,
1942 nsLoadFlags aLoadFlags, nsContentPolicyType aLoadPolicyType,
1943 bool aCanMakeNewChannel, bool* aNewChannelCreated,
1944 imgRequestProxy** aProxyRequest, nsIPrincipal* aTriggeringPrincipal,
1945 CORSMode aCORSMode, bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
1946 FetchPriority aFetchPriority) {
1947 LOG_SCOPE(gImgLog, "imgLoader::ValidateEntry");
1949 // If the expiration time is zero, then the request has not gotten far enough
1950 // to know when it will expire, or we know it will never expire (see
1951 // nsContentUtils::GetSubresourceCacheValidationInfo).
1952 bool hasExpired = aEntry->GetExpiryTime().IsExpired();
1954 // Special treatment for file URLs - aEntry has expired if file has changed
1955 if (nsCOMPtr<nsIFileURL> fileUrl = do_QueryInterface(aURI)) {
1956 uint32_t lastModTime = aEntry->GetLoadTime();
1957 nsCOMPtr<nsIFile> theFile;
1958 if (NS_SUCCEEDED(fileUrl->GetFile(getter_AddRefs(theFile)))) {
1959 PRTime fileLastMod;
1960 if (NS_SUCCEEDED(theFile->GetLastModifiedTime(&fileLastMod))) {
1961 // nsIFile uses millisec, NSPR usec.
1962 fileLastMod *= 1000;
1963 hasExpired = SecondsFromPRTime((PRTime)fileLastMod) > lastModTime;
1968 RefPtr<imgRequest> request(aEntry->GetRequest());
1970 if (!request) {
1971 return false;
1974 if (!ValidateSecurityInfo(request, aEntry->ForcePrincipalCheck(), aCORSMode,
1975 aTriggeringPrincipal, aLoadingDocument,
1976 aLoadPolicyType)) {
1977 return false;
1980 // data URIs are immutable and by their nature can't leak data, so we can
1981 // just return true in that case. Doing so would mean that shift-reload
1982 // doesn't reload data URI documents/images though (which is handy for
1983 // debugging during gecko development) so we make an exception in that case.
1984 if (aURI->SchemeIs("data") && !(aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE)) {
1985 return true;
1988 bool validateRequest = false;
1990 if (!request->CanReuseWithoutValidation(aLoadingDocument)) {
1991 // If we would need to revalidate this entry, but we're being told to
1992 // bypass the cache, we don't allow this entry to be used.
1993 if (aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE) {
1994 return false;
1997 if (MOZ_UNLIKELY(ChaosMode::isActive(ChaosFeature::ImageCache))) {
1998 if (ChaosMode::randomUint32LessThan(4) < 1) {
1999 return false;
2003 // Determine whether the cache aEntry must be revalidated...
2004 validateRequest = ShouldRevalidateEntry(aEntry, aLoadFlags, hasExpired);
2006 MOZ_LOG(gImgLog, LogLevel::Debug,
2007 ("imgLoader::ValidateEntry validating cache entry. "
2008 "validateRequest = %d",
2009 validateRequest));
2010 } else if (!aLoadingDocument && MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
2011 MOZ_LOG(gImgLog, LogLevel::Debug,
2012 ("imgLoader::ValidateEntry BYPASSING cache validation for %s "
2013 "because of NULL loading document",
2014 aURI->GetSpecOrDefault().get()));
2017 // If the original request is still transferring don't kick off a validation
2018 // network request because it is a bit silly to issue a validation request if
2019 // the original request hasn't even finished yet. So just return true
2020 // indicating the caller can create a new proxy for the request and use it as
2021 // is.
2022 // This is an optimization but it's also required for correctness. If we don't
2023 // do this then when firing the load complete notification for the original
2024 // request that can unblock load for the document and then spin the event loop
2025 // (see the stack in bug 1641682) which then the OnStartRequest for the
2026 // validation request can fire which can call UpdateProxies and can sync
2027 // notify on the progress tracker about all existing state, which includes
2028 // load complete, so we fire a second load complete notification for the
2029 // image.
2030 // In addition, we want to validate if the original request encountered
2031 // an error for two reasons. The first being if the error was a network error
2032 // then trying to re-fetch the image might succeed. The second is more
2033 // complicated. We decide if we should fire the load or error event for img
2034 // elements depending on if the image has error in its status at the time when
2035 // the load complete notification is received, and we set error status on an
2036 // image if it encounters a network error or a decode error with no real way
2037 // to tell them apart. So if we load an image that will produce a decode error
2038 // the first time we will usually fire the load event, and then decode enough
2039 // to encounter the decode error and set the error status on the image. The
2040 // next time we reference the image in the same document the load complete
2041 // notification is replayed and this time the error status from the decode is
2042 // already present so we fire the error event instead of the load event. This
2043 // is a bug (bug 1645576) that we should fix. In order to avoid that bug in
2044 // some cases (specifically the cases when we hit this code and try to
2045 // validate the request) we make sure to validate. This avoids the bug because
2046 // when the load complete notification arrives the proxy is marked as
2047 // validating so it lies about its status and returns nothing.
2048 const bool requestComplete = [&] {
2049 RefPtr<ProgressTracker> tracker;
2050 RefPtr<mozilla::image::Image> image = request->GetImage();
2051 if (image) {
2052 tracker = image->GetProgressTracker();
2053 } else {
2054 tracker = request->GetProgressTracker();
2056 return tracker &&
2057 tracker->GetProgress() & (FLAG_LOAD_COMPLETE | FLAG_HAS_ERROR);
2058 }();
2060 if (!requestComplete) {
2061 return true;
2064 if (validateRequest && aCanMakeNewChannel) {
2065 LOG_SCOPE(gImgLog, "imgLoader::ValidateRequest |cache hit| must validate");
2067 uint64_t innerWindowID =
2068 aLoadingDocument ? aLoadingDocument->InnerWindowID() : 0;
2069 return ValidateRequestWithNewChannel(
2070 request, aURI, aInitialDocumentURI, aReferrerInfo, aLoadGroup,
2071 aObserver, aLoadingDocument, innerWindowID, aLoadFlags, aLoadPolicyType,
2072 aProxyRequest, aTriggeringPrincipal, aCORSMode, aLinkPreload,
2073 aEarlyHintPreloaderId, aFetchPriority, aNewChannelCreated);
2076 if (!validateRequest) {
2077 NotifyObserversForCachedImage(
2078 aEntry, request, aURI, aReferrerInfo, aLoadingDocument,
2079 aTriggeringPrincipal, aCORSMode, aEarlyHintPreloaderId, aFetchPriority);
2082 return !validateRequest;
2085 bool imgLoader::RemoveFromCache(const ImageCacheKey& aKey) {
2086 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache", "uri",
2087 aKey.URI());
2088 RefPtr<imgCacheEntry> entry;
2089 mCache.Remove(aKey, getter_AddRefs(entry));
2090 if (entry) {
2091 MOZ_ASSERT(!entry->Evicted(), "Evicting an already-evicted cache entry!");
2093 // Entries with no proxies are in the tracker.
2094 if (entry->HasNoProxies()) {
2095 if (mCacheTracker) {
2096 mCacheTracker->RemoveObject(entry);
2098 mCacheQueue.Remove(entry);
2101 entry->SetEvicted(true);
2103 RefPtr<imgRequest> request = entry->GetRequest();
2104 request->SetIsInCache(false);
2105 AddToUncachedImages(request);
2107 return true;
2109 return false;
2112 bool imgLoader::RemoveFromCache(imgCacheEntry* entry, QueueState aQueueState) {
2113 LOG_STATIC_FUNC(gImgLog, "imgLoader::RemoveFromCache entry");
2115 RefPtr<imgRequest> request = entry->GetRequest();
2116 if (request) {
2117 const ImageCacheKey& key = request->CacheKey();
2118 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache",
2119 "entry's uri", key.URI());
2121 mCache.Remove(key);
2123 if (entry->HasNoProxies()) {
2124 LOG_STATIC_FUNC(gImgLog,
2125 "imgLoader::RemoveFromCache removing from tracker");
2126 if (mCacheTracker) {
2127 mCacheTracker->RemoveObject(entry);
2129 // Only search the queue to remove the entry if its possible it might
2130 // be in the queue. If we know its not in the queue this would be
2131 // wasted work.
2132 MOZ_ASSERT_IF(aQueueState == QueueState::AlreadyRemoved,
2133 !mCacheQueue.Contains(entry));
2134 if (aQueueState == QueueState::MaybeExists) {
2135 mCacheQueue.Remove(entry);
2139 entry->SetEvicted(true);
2140 request->SetIsInCache(false);
2141 AddToUncachedImages(request);
2143 return true;
2146 return false;
2149 nsresult imgLoader::ClearImageCache(ClearOptions aOptions) {
2150 const bool chromeOnly = aOptions.contains(ClearOption::ChromeOnly);
2151 const auto ShouldRemove = [&](imgCacheEntry* aEntry) {
2152 if (chromeOnly) {
2153 // TODO: Consider also removing "resource://" etc?
2154 RefPtr<imgRequest> request = aEntry->GetRequest();
2155 if (!request || !request->CacheKey().URI()->SchemeIs("chrome")) {
2156 return false;
2159 return true;
2161 if (aOptions.contains(ClearOption::UnusedOnly)) {
2162 LOG_STATIC_FUNC(gImgLog, "imgLoader::ClearImageCache queue");
2163 // We have to make a temporary, since RemoveFromCache removes the element
2164 // from the queue, invalidating iterators.
2165 nsTArray<RefPtr<imgCacheEntry>> entries(mCacheQueue.GetNumElements());
2166 for (auto& entry : mCacheQueue) {
2167 if (ShouldRemove(entry)) {
2168 entries.AppendElement(entry);
2172 // Iterate in reverse order to minimize array copying.
2173 for (auto& entry : entries) {
2174 if (!RemoveFromCache(entry)) {
2175 return NS_ERROR_FAILURE;
2179 MOZ_ASSERT(chromeOnly || mCacheQueue.GetNumElements() == 0);
2180 return NS_OK;
2183 LOG_STATIC_FUNC(gImgLog, "imgLoader::ClearImageCache table");
2184 // We have to make a temporary, since RemoveFromCache removes the element
2185 // from the queue, invalidating iterators.
2186 const auto entries =
2187 ToTArray<nsTArray<RefPtr<imgCacheEntry>>>(mCache.Values());
2188 for (const auto& entry : entries) {
2189 if (!ShouldRemove(entry)) {
2190 continue;
2192 if (!RemoveFromCache(entry)) {
2193 return NS_ERROR_FAILURE;
2196 MOZ_ASSERT(chromeOnly || mCache.IsEmpty());
2197 return NS_OK;
2200 void imgLoader::AddToUncachedImages(imgRequest* aRequest) {
2201 MutexAutoLock lock(mUncachedImagesMutex);
2202 mUncachedImages.Insert(aRequest);
2205 void imgLoader::RemoveFromUncachedImages(imgRequest* aRequest) {
2206 MutexAutoLock lock(mUncachedImagesMutex);
2207 mUncachedImages.Remove(aRequest);
2210 #define LOAD_FLAGS_CACHE_MASK \
2211 (nsIRequest::LOAD_BYPASS_CACHE | nsIRequest::LOAD_FROM_CACHE)
2213 #define LOAD_FLAGS_VALIDATE_MASK \
2214 (nsIRequest::VALIDATE_ALWAYS | nsIRequest::VALIDATE_NEVER | \
2215 nsIRequest::VALIDATE_ONCE_PER_SESSION)
2217 NS_IMETHODIMP
2218 imgLoader::LoadImageXPCOM(
2219 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2220 nsIPrincipal* aTriggeringPrincipal, nsILoadGroup* aLoadGroup,
2221 imgINotificationObserver* aObserver, Document* aLoadingDocument,
2222 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
2223 nsContentPolicyType aContentPolicyType, imgIRequest** _retval) {
2224 // Optional parameter, so defaults to 0 (== TYPE_INVALID)
2225 if (!aContentPolicyType) {
2226 aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE;
2228 imgRequestProxy* proxy;
2229 nsresult rv =
2230 LoadImage(aURI, aInitialDocumentURI, aReferrerInfo, aTriggeringPrincipal,
2231 0, aLoadGroup, aObserver, aLoadingDocument, aLoadingDocument,
2232 aLoadFlags, aCacheKey, aContentPolicyType, u""_ns,
2233 /* aUseUrgentStartForChannel */ false, /* aListPreload */ false,
2234 0, FetchPriority::Auto, &proxy);
2235 *_retval = proxy;
2236 return rv;
2239 static void MakeRequestStaticIfNeeded(
2240 Document* aLoadingDocument, imgRequestProxy** aProxyAboutToGetReturned) {
2241 if (!aLoadingDocument || !aLoadingDocument->IsStaticDocument()) {
2242 return;
2245 if (!*aProxyAboutToGetReturned) {
2246 return;
2249 RefPtr<imgRequestProxy> proxy = dont_AddRef(*aProxyAboutToGetReturned);
2250 *aProxyAboutToGetReturned = nullptr;
2252 RefPtr<imgRequestProxy> staticProxy =
2253 proxy->GetStaticRequest(aLoadingDocument);
2254 if (staticProxy != proxy) {
2255 proxy->CancelAndForgetObserver(NS_BINDING_ABORTED);
2256 proxy = std::move(staticProxy);
2258 proxy.forget(aProxyAboutToGetReturned);
2261 bool imgLoader::IsImageAvailable(nsIURI* aURI,
2262 nsIPrincipal* aTriggeringPrincipal,
2263 CORSMode aCORSMode, Document* aDocument) {
2264 ImageCacheKey key(aURI, aCORSMode,
2265 aTriggeringPrincipal->OriginAttributesRef(), aDocument);
2266 RefPtr<imgCacheEntry> entry;
2267 if (!mCache.Get(key, getter_AddRefs(entry)) || !entry) {
2268 return false;
2270 RefPtr<imgRequest> request = entry->GetRequest();
2271 if (!request) {
2272 return false;
2274 if (nsCOMPtr<nsILoadGroup> docLoadGroup = aDocument->GetDocumentLoadGroup()) {
2275 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2276 docLoadGroup->GetLoadFlags(&requestFlags);
2277 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2278 // If we're bypassing the cache, treat the image as not available.
2279 return false;
2282 return ValidateCORSMode(request, false, aCORSMode, aTriggeringPrincipal);
2285 nsresult imgLoader::LoadImage(
2286 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2287 nsIPrincipal* aTriggeringPrincipal, uint64_t aRequestContextID,
2288 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
2289 nsINode* aContext, Document* aLoadingDocument, nsLoadFlags aLoadFlags,
2290 nsISupports* aCacheKey, nsContentPolicyType aContentPolicyType,
2291 const nsAString& initiatorType, bool aUseUrgentStartForChannel,
2292 bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
2293 FetchPriority aFetchPriority, imgRequestProxy** _retval) {
2294 VerifyCacheSizes();
2296 NS_ASSERTION(aURI, "imgLoader::LoadImage -- NULL URI pointer");
2298 if (!aURI) {
2299 return NS_ERROR_NULL_POINTER;
2302 auto makeStaticIfNeeded = mozilla::MakeScopeExit(
2303 [&] { MakeRequestStaticIfNeeded(aLoadingDocument, _retval); });
2305 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK,
2306 aURI->GetSpecOrDefault());
2308 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::LoadImage", "aURI", aURI);
2310 *_retval = nullptr;
2312 RefPtr<imgRequest> request;
2314 nsresult rv;
2315 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2317 #ifdef DEBUG
2318 bool isPrivate = false;
2320 if (aLoadingDocument) {
2321 isPrivate = aLoadingDocument->IsInPrivateBrowsing();
2322 } else if (aLoadGroup) {
2323 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadGroup);
2325 MOZ_ASSERT(isPrivate == mRespectPrivacy);
2327 if (aLoadingDocument) {
2328 // The given load group should match that of the document if given. If
2329 // that isn't the case, then we need to add more plumbing to ensure we
2330 // block the document as well.
2331 nsCOMPtr<nsILoadGroup> docLoadGroup =
2332 aLoadingDocument->GetDocumentLoadGroup();
2333 MOZ_ASSERT(docLoadGroup == aLoadGroup);
2335 #endif
2337 // Get the default load flags from the loadgroup (if possible)...
2338 if (aLoadGroup) {
2339 aLoadGroup->GetLoadFlags(&requestFlags);
2342 // Merge the default load flags with those passed in via aLoadFlags.
2343 // Currently, *only* the caching, validation and background load flags
2344 // are merged...
2346 // The flags in aLoadFlags take precedence over the default flags!
2348 if (aLoadFlags & LOAD_FLAGS_CACHE_MASK) {
2349 // Override the default caching flags...
2350 requestFlags = (requestFlags & ~LOAD_FLAGS_CACHE_MASK) |
2351 (aLoadFlags & LOAD_FLAGS_CACHE_MASK);
2353 if (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK) {
2354 // Override the default validation flags...
2355 requestFlags = (requestFlags & ~LOAD_FLAGS_VALIDATE_MASK) |
2356 (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK);
2358 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
2359 // Propagate background loading...
2360 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2363 if (aLinkPreload) {
2364 // Set background loading if it is <link rel=preload>
2365 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2368 CORSMode corsmode = CORS_NONE;
2369 if (aLoadFlags & imgILoader::LOAD_CORS_ANONYMOUS) {
2370 corsmode = CORS_ANONYMOUS;
2371 } else if (aLoadFlags & imgILoader::LOAD_CORS_USE_CREDENTIALS) {
2372 corsmode = CORS_USE_CREDENTIALS;
2375 // Look in the preloaded images of loading document first.
2376 if (!aLinkPreload && aLoadingDocument) {
2377 // All Early Hints preloads are Link preloads, therefore we don't have a
2378 // Early Hints preload here
2379 MOZ_ASSERT(!aEarlyHintPreloaderId);
2380 auto key =
2381 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, corsmode);
2382 if (RefPtr<PreloaderBase> preload =
2383 aLoadingDocument->Preloads().LookupPreload(key)) {
2384 RefPtr<imgRequestProxy> proxy = do_QueryObject(preload);
2385 MOZ_ASSERT(proxy);
2387 MOZ_LOG(gImgLog, LogLevel::Debug,
2388 ("[this=%p] imgLoader::LoadImage -- preloaded [proxy=%p]"
2389 " [document=%p]\n",
2390 this, proxy.get(), aLoadingDocument));
2392 // Removing the preload for this image to be in parity with Chromium. Any
2393 // following regular image request will be reloaded using the regular
2394 // path: image cache, http cache, network. Any following `<link
2395 // rel=preload as=image>` will start a new image preload that can be
2396 // satisfied from http cache or network.
2398 // There is a spec discussion for "preload cache", see
2399 // https://github.com/w3c/preload/issues/97. And it is also not clear how
2400 // preload image interacts with list of available images, see
2401 // https://github.com/whatwg/html/issues/4474.
2402 proxy->RemoveSelf(aLoadingDocument);
2403 proxy->NotifyUsage(aLoadingDocument);
2405 imgRequest* request = proxy->GetOwner();
2406 nsresult rv =
2407 CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
2408 aObserver, requestFlags, _retval);
2409 NS_ENSURE_SUCCESS(rv, rv);
2411 imgRequestProxy* newProxy = *_retval;
2412 if (imgCacheValidator* validator = request->GetValidator()) {
2413 newProxy->MarkValidating();
2414 // Attach the proxy without notifying and this will add us to the load
2415 // group.
2416 validator->AddProxy(newProxy);
2417 } else {
2418 // It's OK to add here even if the request is done. If it is, it'll send
2419 // a OnStopRequest()and the proxy will be removed from the loadgroup in
2420 // imgRequestProxy::OnLoadComplete.
2421 newProxy->AddToLoadGroup();
2422 newProxy->NotifyListener();
2425 return NS_OK;
2429 RefPtr<imgCacheEntry> entry;
2431 // Look in the cache for our URI, and then validate it.
2432 // XXX For now ignore aCacheKey. We will need it in the future
2433 // for correctly dealing with image load requests that are a result
2434 // of post data.
2435 OriginAttributes attrs;
2436 if (aTriggeringPrincipal) {
2437 attrs = aTriggeringPrincipal->OriginAttributesRef();
2439 ImageCacheKey key(aURI, corsmode, attrs, aLoadingDocument);
2440 if (mCache.Get(key, getter_AddRefs(entry)) && entry) {
2441 bool newChannelCreated = false;
2442 if (ValidateEntry(entry, aURI, aInitialDocumentURI, aReferrerInfo,
2443 aLoadGroup, aObserver, aLoadingDocument, requestFlags,
2444 aContentPolicyType, true, &newChannelCreated, _retval,
2445 aTriggeringPrincipal, corsmode, aLinkPreload,
2446 aEarlyHintPreloaderId, aFetchPriority)) {
2447 request = entry->GetRequest();
2449 // If this entry has no proxies, its request has no reference to the
2450 // entry.
2451 if (entry->HasNoProxies()) {
2452 LOG_FUNC_WITH_PARAM(gImgLog,
2453 "imgLoader::LoadImage() adding proxyless entry",
2454 "uri", key.URI());
2455 MOZ_ASSERT(!request->HasCacheEntry(),
2456 "Proxyless entry's request has cache entry!");
2457 request->SetCacheEntry(entry);
2459 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2460 mCacheTracker->MarkUsed(entry);
2464 entry->Touch();
2466 if (!newChannelCreated) {
2467 // This is ugly but it's needed to report CSP violations. We have 3
2468 // scenarios:
2469 // - we don't have cache. We are not in this if() stmt. A new channel is
2470 // created and that triggers the CSP checks.
2471 // - We have a cache entry and this is blocked by CSP directives.
2472 DebugOnly<bool> shouldLoad = ShouldLoadCachedImage(
2473 request, aLoadingDocument, aTriggeringPrincipal, aContentPolicyType,
2474 /* aSendCSPViolationReports */ true);
2475 MOZ_ASSERT(shouldLoad);
2477 } else {
2478 // We can't use this entry. We'll try to load it off the network, and if
2479 // successful, overwrite the old entry in the cache with a new one.
2480 entry = nullptr;
2484 // Keep the channel in this scope, so we can adjust its notificationCallbacks
2485 // later when we create the proxy.
2486 nsCOMPtr<nsIChannel> newChannel;
2487 // If we didn't get a cache hit, we need to load from the network.
2488 if (!request) {
2489 LOG_SCOPE(gImgLog, "imgLoader::LoadImage |cache miss|");
2491 bool forcePrincipalCheck;
2492 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
2493 aInitialDocumentURI, corsmode, aReferrerInfo,
2494 aLoadGroup, requestFlags, aContentPolicyType,
2495 aTriggeringPrincipal, aContext, mRespectPrivacy,
2496 aEarlyHintPreloaderId, aFetchPriority);
2497 if (NS_FAILED(rv)) {
2498 return NS_ERROR_FAILURE;
2501 MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel) == mRespectPrivacy);
2503 NewRequestAndEntry(forcePrincipalCheck, this, key, getter_AddRefs(request),
2504 getter_AddRefs(entry));
2506 MOZ_LOG(gImgLog, LogLevel::Debug,
2507 ("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
2508 " [request=%p]\n",
2509 this, request.get()));
2511 nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(newChannel));
2512 if (cos) {
2513 if (aUseUrgentStartForChannel && !aLinkPreload) {
2514 cos->AddClassFlags(nsIClassOfService::UrgentStart);
2516 if (StaticPrefs::image_priority_incremental()) {
2517 cos->SetIncremental(true);
2520 if (StaticPrefs::network_http_tailing_enabled() &&
2521 aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
2522 cos->AddClassFlags(nsIClassOfService::Throttleable |
2523 nsIClassOfService::Tail);
2524 nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(newChannel));
2525 if (httpChannel) {
2526 Unused << httpChannel->SetRequestContextID(aRequestContextID);
2531 nsCOMPtr<nsILoadGroup> channelLoadGroup;
2532 newChannel->GetLoadGroup(getter_AddRefs(channelLoadGroup));
2533 rv = request->Init(aURI, aURI, /* aHadInsecureRedirect = */ false,
2534 channelLoadGroup, newChannel, entry, aLoadingDocument,
2535 aTriggeringPrincipal, corsmode, aReferrerInfo);
2536 if (NS_FAILED(rv)) {
2537 return NS_ERROR_FAILURE;
2540 // Add the initiator type for this image load
2541 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(newChannel);
2542 if (timedChannel) {
2543 timedChannel->SetInitiatorType(initiatorType);
2546 // create the proxy listener
2547 nsCOMPtr<nsIStreamListener> listener = new ProxyListener(request.get());
2549 MOZ_LOG(gImgLog, LogLevel::Debug,
2550 ("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
2551 this));
2553 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
2554 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
2555 aLoadGroup);
2557 nsresult openRes;
2558 openRes = newChannel->AsyncOpen(listener);
2560 if (NS_FAILED(openRes)) {
2561 MOZ_LOG(
2562 gImgLog, LogLevel::Debug,
2563 ("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
2564 "\n",
2565 this, static_cast<uint32_t>(openRes)));
2566 request->CancelAndAbort(openRes);
2567 return openRes;
2570 // Try to add the new request into the cache.
2571 PutIntoCache(key, entry);
2572 } else {
2573 LOG_MSG_WITH_PARAM(gImgLog, "imgLoader::LoadImage |cache hit|", "request",
2574 request);
2577 // If we didn't get a proxy when validating the cache entry, we need to
2578 // create one.
2579 if (!*_retval) {
2580 // ValidateEntry() has three return values: "Is valid," "might be valid --
2581 // validating over network", and "not valid." If we don't have a _retval,
2582 // we know ValidateEntry is not validating over the network, so it's safe
2583 // to SetLoadId here because we know this request is valid for this context.
2585 // Note, however, that this doesn't guarantee the behaviour we want (one
2586 // URL maps to the same image on a page) if we load the same image in a
2587 // different tab (see bug 528003), because its load id will get re-set, and
2588 // that'll cause us to validate over the network.
2589 request->SetLoadId(aLoadingDocument);
2591 LOG_MSG(gImgLog, "imgLoader::LoadImage", "creating proxy request.");
2592 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
2593 aObserver, requestFlags, _retval);
2594 if (NS_FAILED(rv)) {
2595 return rv;
2598 imgRequestProxy* proxy = *_retval;
2600 // Make sure that OnStatus/OnProgress calls have the right request set, if
2601 // we did create a channel here.
2602 if (newChannel) {
2603 nsCOMPtr<nsIInterfaceRequestor> requestor(
2604 new nsProgressNotificationProxy(newChannel, proxy));
2605 if (!requestor) {
2606 return NS_ERROR_OUT_OF_MEMORY;
2608 newChannel->SetNotificationCallbacks(requestor);
2611 if (aLinkPreload) {
2612 MOZ_ASSERT(aLoadingDocument);
2613 auto preloadKey =
2614 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, corsmode);
2615 proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
2618 // Note that it's OK to add here even if the request is done. If it is,
2619 // it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
2620 // the proxy will be removed from the loadgroup.
2621 proxy->AddToLoadGroup();
2623 // If we're loading off the network, explicitly don't notify our proxy,
2624 // because necko (or things called from necko, such as imgCacheValidator)
2625 // are going to call our notifications asynchronously, and we can't make it
2626 // further asynchronous because observers might rely on imagelib completing
2627 // its work between the channel's OnStartRequest and OnStopRequest.
2628 if (!newChannel) {
2629 proxy->NotifyListener();
2632 return rv;
2635 NS_ASSERTION(*_retval, "imgLoader::LoadImage -- no return value");
2637 return NS_OK;
2640 NS_IMETHODIMP
2641 imgLoader::LoadImageWithChannelXPCOM(nsIChannel* channel,
2642 imgINotificationObserver* aObserver,
2643 Document* aLoadingDocument,
2644 nsIStreamListener** listener,
2645 imgIRequest** _retval) {
2646 nsresult result;
2647 imgRequestProxy* proxy;
2648 result = LoadImageWithChannel(channel, aObserver, aLoadingDocument, listener,
2649 &proxy);
2650 *_retval = proxy;
2651 return result;
2654 nsresult imgLoader::LoadImageWithChannel(nsIChannel* channel,
2655 imgINotificationObserver* aObserver,
2656 Document* aLoadingDocument,
2657 nsIStreamListener** listener,
2658 imgRequestProxy** _retval) {
2659 NS_ASSERTION(channel,
2660 "imgLoader::LoadImageWithChannel -- NULL channel pointer");
2662 MOZ_ASSERT(NS_UsePrivateBrowsing(channel) == mRespectPrivacy);
2664 auto makeStaticIfNeeded = mozilla::MakeScopeExit(
2665 [&] { MakeRequestStaticIfNeeded(aLoadingDocument, _retval); });
2667 LOG_SCOPE(gImgLog, "imgLoader::LoadImageWithChannel");
2668 RefPtr<imgRequest> request;
2670 nsCOMPtr<nsIURI> uri;
2671 channel->GetURI(getter_AddRefs(uri));
2673 NS_ENSURE_TRUE(channel, NS_ERROR_FAILURE);
2674 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2676 OriginAttributes attrs = loadInfo->GetOriginAttributes();
2678 // TODO: Get a meaningful cors mode from the caller probably?
2679 const auto corsMode = CORS_NONE;
2680 ImageCacheKey key(uri, corsMode, attrs, aLoadingDocument);
2682 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2683 channel->GetLoadFlags(&requestFlags);
2685 RefPtr<imgCacheEntry> entry;
2687 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2688 RemoveFromCache(key);
2689 } else {
2690 // Look in the cache for our URI, and then validate it.
2691 // XXX For now ignore aCacheKey. We will need it in the future
2692 // for correctly dealing with image load requests that are a result
2693 // of post data.
2694 if (mCache.Get(key, getter_AddRefs(entry)) && entry) {
2695 // We don't want to kick off another network load. So we ask
2696 // ValidateEntry to only do validation without creating a new proxy. If
2697 // it says that the entry isn't valid any more, we'll only use the entry
2698 // we're getting if the channel is loading from the cache anyways.
2700 // XXX -- should this be changed? it's pretty much verbatim from the old
2701 // code, but seems nonsensical.
2703 // Since aCanMakeNewChannel == false, we don't need to pass content policy
2704 // type/principal/etc
2706 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2707 // if there is a loadInfo, use the right contentType, otherwise
2708 // default to the internal image type
2709 nsContentPolicyType policyType = loadInfo->InternalContentPolicyType();
2711 if (ValidateEntry(entry, uri, nullptr, nullptr, nullptr, aObserver,
2712 aLoadingDocument, requestFlags, policyType, false,
2713 nullptr, nullptr, nullptr, corsMode, false, 0,
2714 FetchPriority::Auto)) {
2715 request = entry->GetRequest();
2716 } else {
2717 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(channel));
2718 bool bUseCacheCopy;
2720 if (cacheChan) {
2721 cacheChan->IsFromCache(&bUseCacheCopy);
2722 } else {
2723 bUseCacheCopy = false;
2726 if (!bUseCacheCopy) {
2727 entry = nullptr;
2728 } else {
2729 request = entry->GetRequest();
2733 if (request && entry) {
2734 // If this entry has no proxies, its request has no reference to
2735 // the entry.
2736 if (entry->HasNoProxies()) {
2737 LOG_FUNC_WITH_PARAM(
2738 gImgLog,
2739 "imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
2740 key.URI());
2741 MOZ_ASSERT(!request->HasCacheEntry(),
2742 "Proxyless entry's request has cache entry!");
2743 request->SetCacheEntry(entry);
2745 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2746 mCacheTracker->MarkUsed(entry);
2753 nsCOMPtr<nsILoadGroup> loadGroup;
2754 channel->GetLoadGroup(getter_AddRefs(loadGroup));
2756 #ifdef DEBUG
2757 if (aLoadingDocument) {
2758 // The load group of the channel should always match that of the
2759 // document if given. If that isn't the case, then we need to add more
2760 // plumbing to ensure we block the document as well.
2761 nsCOMPtr<nsILoadGroup> docLoadGroup =
2762 aLoadingDocument->GetDocumentLoadGroup();
2763 MOZ_ASSERT(docLoadGroup == loadGroup);
2765 #endif
2767 // Filter out any load flags not from nsIRequest
2768 requestFlags &= nsIRequest::LOAD_REQUESTMASK;
2770 nsresult rv = NS_OK;
2771 if (request) {
2772 // we have this in our cache already.. cancel the current (document) load
2774 // this should fire an OnStopRequest
2775 channel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
2777 *listener = nullptr; // give them back a null nsIStreamListener
2779 rv = CreateNewProxyForRequest(request, uri, loadGroup, aLoadingDocument,
2780 aObserver, requestFlags, _retval);
2781 static_cast<imgRequestProxy*>(*_retval)->NotifyListener();
2782 } else {
2783 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2784 nsCOMPtr<nsIURI> originalURI;
2785 channel->GetOriginalURI(getter_AddRefs(originalURI));
2787 // XXX(seth): We should be able to just use |key| here, except that |key| is
2788 // constructed above with the *current URI* and not the *original URI*. I'm
2789 // pretty sure this is a bug, and it's preventing us from ever getting a
2790 // cache hit in LoadImageWithChannel when redirects are involved.
2791 ImageCacheKey originalURIKey(originalURI, corsMode, attrs,
2792 aLoadingDocument);
2794 // Default to doing a principal check because we don't know who
2795 // started that load and whether their principal ended up being
2796 // inherited on the channel.
2797 NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
2798 originalURIKey, getter_AddRefs(request),
2799 getter_AddRefs(entry));
2801 // No principal specified here, because we're not passed one.
2802 // In LoadImageWithChannel, the redirects that may have been
2803 // associated with this load would have gone through necko.
2804 // We only have the final URI in ImageLib and hence don't know
2805 // if the request went through insecure redirects. But if it did,
2806 // the necko cache should have handled that (since all necko cache hits
2807 // including the redirects will go through content policy). Hence, we
2808 // can set aHadInsecureRedirect to false here.
2809 rv = request->Init(originalURI, uri, /* aHadInsecureRedirect = */ false,
2810 channel, channel, entry, aLoadingDocument, nullptr,
2811 corsMode, nullptr);
2812 NS_ENSURE_SUCCESS(rv, rv);
2814 RefPtr<ProxyListener> pl =
2815 new ProxyListener(static_cast<nsIStreamListener*>(request.get()));
2816 pl.forget(listener);
2818 // Try to add the new request into the cache.
2819 PutIntoCache(originalURIKey, entry);
2821 rv = CreateNewProxyForRequest(request, originalURI, loadGroup,
2822 aLoadingDocument, aObserver, requestFlags,
2823 _retval);
2825 // Explicitly don't notify our proxy, because we're loading off the
2826 // network, and necko (or things called from necko, such as
2827 // imgCacheValidator) are going to call our notifications asynchronously,
2828 // and we can't make it further asynchronous because observers might rely
2829 // on imagelib completing its work between the channel's OnStartRequest and
2830 // OnStopRequest.
2833 if (NS_FAILED(rv)) {
2834 return rv;
2837 (*_retval)->AddToLoadGroup();
2838 return rv;
2841 bool imgLoader::SupportImageWithMimeType(const nsACString& aMimeType,
2842 AcceptedMimeTypes aAccept
2843 /* = AcceptedMimeTypes::IMAGES */) {
2844 nsAutoCString mimeType(aMimeType);
2845 ToLowerCase(mimeType);
2847 if (aAccept == AcceptedMimeTypes::IMAGES_AND_DOCUMENTS &&
2848 mimeType.EqualsLiteral(IMAGE_SVG_XML)) {
2849 return true;
2852 DecoderType type = DecoderFactory::GetDecoderType(mimeType.get());
2853 return type != DecoderType::UNKNOWN;
2856 NS_IMETHODIMP
2857 imgLoader::GetMIMETypeFromContent(nsIRequest* aRequest,
2858 const uint8_t* aContents, uint32_t aLength,
2859 nsACString& aContentType) {
2860 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2861 if (channel) {
2862 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2863 if (loadInfo->GetSkipContentSniffing()) {
2864 return NS_ERROR_NOT_AVAILABLE;
2868 nsresult rv =
2869 GetMimeTypeFromContent((const char*)aContents, aLength, aContentType);
2870 if (NS_SUCCEEDED(rv) && channel && XRE_IsParentProcess()) {
2871 if (RefPtr<mozilla::net::nsHttpChannel> httpChannel =
2872 do_QueryObject(channel)) {
2873 // If the image type pattern matching algorithm given bytes does not
2874 // return undefined, then disable the further check and allow the
2875 // response.
2876 httpChannel->DisableIsOpaqueResponseAllowedAfterSniffCheck(
2877 mozilla::net::nsHttpChannel::SnifferType::Image);
2881 return rv;
2884 /* static */
2885 nsresult imgLoader::GetMimeTypeFromContent(const char* aContents,
2886 uint32_t aLength,
2887 nsACString& aContentType) {
2888 nsAutoCString detected;
2890 /* Is it a GIF? */
2891 if (aLength >= 6 &&
2892 (!strncmp(aContents, "GIF87a", 6) || !strncmp(aContents, "GIF89a", 6))) {
2893 aContentType.AssignLiteral(IMAGE_GIF);
2895 /* or a PNG? */
2896 } else if (aLength >= 8 && ((unsigned char)aContents[0] == 0x89 &&
2897 (unsigned char)aContents[1] == 0x50 &&
2898 (unsigned char)aContents[2] == 0x4E &&
2899 (unsigned char)aContents[3] == 0x47 &&
2900 (unsigned char)aContents[4] == 0x0D &&
2901 (unsigned char)aContents[5] == 0x0A &&
2902 (unsigned char)aContents[6] == 0x1A &&
2903 (unsigned char)aContents[7] == 0x0A)) {
2904 aContentType.AssignLiteral(IMAGE_PNG);
2906 /* maybe a JPEG (JFIF)? */
2907 /* JFIF files start with SOI APP0 but older files can start with SOI DQT
2908 * so we test for SOI followed by any marker, i.e. FF D8 FF
2909 * this will also work for SPIFF JPEG files if they appear in the future.
2911 * (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
2913 } else if (aLength >= 3 && ((unsigned char)aContents[0]) == 0xFF &&
2914 ((unsigned char)aContents[1]) == 0xD8 &&
2915 ((unsigned char)aContents[2]) == 0xFF) {
2916 aContentType.AssignLiteral(IMAGE_JPEG);
2918 /* or how about ART? */
2919 /* ART begins with JG (4A 47). Major version offset 2.
2920 * Minor version offset 3. Offset 4 must be nullptr.
2922 } else if (aLength >= 5 && ((unsigned char)aContents[0]) == 0x4a &&
2923 ((unsigned char)aContents[1]) == 0x47 &&
2924 ((unsigned char)aContents[4]) == 0x00) {
2925 aContentType.AssignLiteral(IMAGE_ART);
2927 } else if (aLength >= 2 && !strncmp(aContents, "BM", 2)) {
2928 aContentType.AssignLiteral(IMAGE_BMP);
2930 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
2931 // CURs begin with 2-byte 0 followed by 2-byte 2.
2932 } else if (aLength >= 4 && (!memcmp(aContents, "\000\000\001\000", 4) ||
2933 !memcmp(aContents, "\000\000\002\000", 4))) {
2934 aContentType.AssignLiteral(IMAGE_ICO);
2936 // WebPs always begin with RIFF, a 32-bit length, and WEBP.
2937 } else if (aLength >= 12 && !memcmp(aContents, "RIFF", 4) &&
2938 !memcmp(aContents + 8, "WEBP", 4)) {
2939 aContentType.AssignLiteral(IMAGE_WEBP);
2941 } else if (MatchesMP4(reinterpret_cast<const uint8_t*>(aContents), aLength,
2942 detected) &&
2943 detected.Equals(IMAGE_AVIF)) {
2944 aContentType.AssignLiteral(IMAGE_AVIF);
2945 } else if ((aLength >= 2 && !memcmp(aContents, "\xFF\x0A", 2)) ||
2946 (aLength >= 12 &&
2947 !memcmp(aContents, "\x00\x00\x00\x0CJXL \x0D\x0A\x87\x0A", 12))) {
2948 // Each version is for containerless and containerful files respectively.
2949 aContentType.AssignLiteral(IMAGE_JXL);
2950 } else {
2951 /* none of the above? I give up */
2952 return NS_ERROR_NOT_AVAILABLE;
2955 return NS_OK;
2959 * proxy stream listener class used to handle multipart/x-mixed-replace
2962 #include "nsIRequest.h"
2963 #include "nsIStreamConverterService.h"
2965 NS_IMPL_ISUPPORTS(ProxyListener, nsIStreamListener,
2966 nsIThreadRetargetableStreamListener, nsIRequestObserver)
2968 ProxyListener::ProxyListener(nsIStreamListener* dest) : mDestListener(dest) {}
2970 ProxyListener::~ProxyListener() = default;
2972 /** nsIRequestObserver methods **/
2974 NS_IMETHODIMP
2975 ProxyListener::OnStartRequest(nsIRequest* aRequest) {
2976 if (!mDestListener) {
2977 return NS_ERROR_FAILURE;
2980 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2981 if (channel) {
2982 // We need to set the initiator type for the image load
2983 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(channel);
2984 if (timedChannel) {
2985 nsAutoString type;
2986 timedChannel->GetInitiatorType(type);
2987 if (type.IsEmpty()) {
2988 timedChannel->SetInitiatorType(u"img"_ns);
2992 nsAutoCString contentType;
2993 nsresult rv = channel->GetContentType(contentType);
2995 if (!contentType.IsEmpty()) {
2996 /* If multipart/x-mixed-replace content, we'll insert a MIME decoder
2997 in the pipeline to handle the content and pass it along to our
2998 original listener.
3000 if ("multipart/x-mixed-replace"_ns.Equals(contentType)) {
3001 nsCOMPtr<nsIStreamConverterService> convServ(
3002 do_GetService("@mozilla.org/streamConverters;1", &rv));
3003 if (NS_SUCCEEDED(rv)) {
3004 nsCOMPtr<nsIStreamListener> toListener(mDestListener);
3005 nsCOMPtr<nsIStreamListener> fromListener;
3007 rv = convServ->AsyncConvertData("multipart/x-mixed-replace", "*/*",
3008 toListener, nullptr,
3009 getter_AddRefs(fromListener));
3010 if (NS_SUCCEEDED(rv)) {
3011 mDestListener = fromListener;
3018 return mDestListener->OnStartRequest(aRequest);
3021 NS_IMETHODIMP
3022 ProxyListener::OnStopRequest(nsIRequest* aRequest, nsresult status) {
3023 if (!mDestListener) {
3024 return NS_ERROR_FAILURE;
3027 return mDestListener->OnStopRequest(aRequest, status);
3030 /** nsIStreamListener methods **/
3032 NS_IMETHODIMP
3033 ProxyListener::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
3034 uint64_t sourceOffset, uint32_t count) {
3035 if (!mDestListener) {
3036 return NS_ERROR_FAILURE;
3039 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
3042 NS_IMETHODIMP
3043 ProxyListener::OnDataFinished(nsresult aStatus) {
3044 if (!mDestListener) {
3045 return NS_ERROR_FAILURE;
3047 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3048 do_QueryInterface(mDestListener);
3049 if (retargetableListener) {
3050 return retargetableListener->OnDataFinished(aStatus);
3053 return NS_OK;
3056 /** nsThreadRetargetableStreamListener methods **/
3057 NS_IMETHODIMP
3058 ProxyListener::CheckListenerChain() {
3059 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3060 nsresult rv = NS_OK;
3061 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3062 do_QueryInterface(mDestListener, &rv);
3063 if (retargetableListener) {
3064 rv = retargetableListener->CheckListenerChain();
3066 MOZ_LOG(
3067 gImgLog, LogLevel::Debug,
3068 ("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
3069 "]",
3070 (NS_SUCCEEDED(rv) ? "success" : "failure"), this,
3071 (nsIStreamListener*)mDestListener, static_cast<uint32_t>(rv)));
3072 return rv;
3076 * http validate class. check a channel for a 304
3079 NS_IMPL_ISUPPORTS(imgCacheValidator, nsIStreamListener, nsIRequestObserver,
3080 nsIThreadRetargetableStreamListener, nsIChannelEventSink,
3081 nsIInterfaceRequestor, nsIAsyncVerifyRedirectCallback)
3083 imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy* progress,
3084 imgLoader* loader, imgRequest* request,
3085 Document* aDocument,
3086 uint64_t aInnerWindowId,
3087 bool forcePrincipalCheckForCacheEntry)
3088 : mProgressProxy(progress),
3089 mRequest(request),
3090 mDocument(aDocument),
3091 mInnerWindowId(aInnerWindowId),
3092 mImgLoader(loader),
3093 mHadInsecureRedirect(false) {
3094 NewRequestAndEntry(forcePrincipalCheckForCacheEntry, loader,
3095 mRequest->CacheKey(), getter_AddRefs(mNewRequest),
3096 getter_AddRefs(mNewEntry));
3099 imgCacheValidator::~imgCacheValidator() {
3100 if (mRequest) {
3101 // If something went wrong, and we never unblocked the requests waiting on
3102 // validation, now is our last chance. We will cancel the new request and
3103 // switch the waiting proxies to it.
3104 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
3108 void imgCacheValidator::AddProxy(imgRequestProxy* aProxy) {
3109 // aProxy needs to be in the loadgroup since we're validating from
3110 // the network.
3111 aProxy->AddToLoadGroup();
3113 mProxies.AppendElement(aProxy);
3116 void imgCacheValidator::RemoveProxy(imgRequestProxy* aProxy) {
3117 mProxies.RemoveElement(aProxy);
3120 void imgCacheValidator::UpdateProxies(bool aCancelRequest, bool aSyncNotify) {
3121 MOZ_ASSERT(mRequest);
3123 // Clear the validator before updating the proxies. The notifications may
3124 // clone an existing request, and its state could be inconsistent.
3125 mRequest->SetValidator(nullptr);
3126 mRequest = nullptr;
3128 // If an error occurred, we will want to cancel the new request, and make the
3129 // validating proxies point to it. Any proxies still bound to the original
3130 // request which are not validating should remain untouched.
3131 if (aCancelRequest) {
3132 MOZ_ASSERT(mNewRequest);
3133 mNewRequest->CancelAndAbort(NS_BINDING_ABORTED);
3136 // We have finished validating the request, so we can safely take ownership
3137 // of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
3138 // if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
3139 // that any potential notifications should still be suppressed in
3140 // imgRequestProxy::ChangeOwner because we haven't cleared the validating
3141 // flag yet, and thus they will remain deferred.
3142 AutoTArray<RefPtr<imgRequestProxy>, 4> proxies(std::move(mProxies));
3144 for (auto& proxy : proxies) {
3145 // First update the state of all proxies before notifying any of them
3146 // to ensure a consistent state (e.g. in case the notification causes
3147 // other proxies to be touched indirectly.)
3148 MOZ_ASSERT(proxy->IsValidating());
3149 MOZ_ASSERT(proxy->NotificationsDeferred(),
3150 "Proxies waiting on cache validation should be "
3151 "deferring notifications!");
3152 if (mNewRequest) {
3153 proxy->ChangeOwner(mNewRequest);
3155 proxy->ClearValidating();
3158 mNewRequest = nullptr;
3159 mNewEntry = nullptr;
3161 for (auto& proxy : proxies) {
3162 if (aSyncNotify) {
3163 // Notify synchronously, because the caller knows we are already in an
3164 // asynchronously-called function (e.g. OnStartRequest).
3165 proxy->SyncNotifyListener();
3166 } else {
3167 // Notify asynchronously, because the caller does not know our current
3168 // call state (e.g. ~imgCacheValidator).
3169 proxy->NotifyListener();
3174 /** nsIRequestObserver methods **/
3176 NS_IMETHODIMP
3177 imgCacheValidator::OnStartRequest(nsIRequest* aRequest) {
3178 // We may be holding on to a document, so ensure that it's released.
3179 RefPtr<Document> document = mDocument.forget();
3181 // If for some reason we don't still have an existing request (probably
3182 // because OnStartRequest got delivered more than once), just bail.
3183 if (!mRequest) {
3184 MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
3185 aRequest->CancelWithReason(NS_BINDING_ABORTED,
3186 "OnStartRequest delivered more than once?"_ns);
3187 return NS_ERROR_FAILURE;
3190 // If this request is coming from cache and has the same URI as our
3191 // imgRequest, the request all our proxies are pointing at is valid, and all
3192 // we have to do is tell them to notify their listeners.
3193 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(aRequest));
3194 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
3195 if (cacheChan && channel) {
3196 bool isFromCache = false;
3197 cacheChan->IsFromCache(&isFromCache);
3199 nsCOMPtr<nsIURI> channelURI;
3200 channel->GetURI(getter_AddRefs(channelURI));
3202 nsCOMPtr<nsIURI> finalURI;
3203 mRequest->GetFinalURI(getter_AddRefs(finalURI));
3205 bool sameURI = false;
3206 if (channelURI && finalURI) {
3207 channelURI->Equals(finalURI, &sameURI);
3210 if (isFromCache && sameURI) {
3211 // We don't need to load this any more.
3212 aRequest->CancelWithReason(NS_BINDING_ABORTED,
3213 "imgCacheValidator::OnStartRequest"_ns);
3214 mNewRequest = nullptr;
3216 // Clear the validator before updating the proxies. The notifications may
3217 // clone an existing request, and its state could be inconsistent.
3218 mRequest->SetLoadId(document);
3219 mRequest->SetInnerWindowID(mInnerWindowId);
3220 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3221 return NS_OK;
3225 // We can't load out of cache. We have to create a whole new request for the
3226 // data that's coming in off the channel.
3227 nsCOMPtr<nsIURI> uri;
3228 mRequest->GetURI(getter_AddRefs(uri));
3230 LOG_MSG_WITH_PARAM(gImgLog,
3231 "imgCacheValidator::OnStartRequest creating new request",
3232 "uri", uri);
3234 CORSMode corsmode = mRequest->GetCORSMode();
3235 nsCOMPtr<nsIReferrerInfo> referrerInfo = mRequest->GetReferrerInfo();
3236 nsCOMPtr<nsIPrincipal> triggeringPrincipal =
3237 mRequest->GetTriggeringPrincipal();
3239 // Doom the old request's cache entry
3240 mRequest->RemoveFromCache();
3242 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
3243 nsCOMPtr<nsIURI> originalURI;
3244 channel->GetOriginalURI(getter_AddRefs(originalURI));
3245 nsresult rv = mNewRequest->Init(originalURI, uri, mHadInsecureRedirect,
3246 aRequest, channel, mNewEntry, document,
3247 triggeringPrincipal, corsmode, referrerInfo);
3248 if (NS_FAILED(rv)) {
3249 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
3250 return rv;
3253 mDestListener = new ProxyListener(mNewRequest);
3255 // Try to add the new request into the cache. Note that the entry must be in
3256 // the cache before the proxies' ownership changes, because adding a proxy
3257 // changes the caching behaviour for imgRequests.
3258 mImgLoader->PutIntoCache(mNewRequest->CacheKey(), mNewEntry);
3259 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3260 return mDestListener->OnStartRequest(aRequest);
3263 NS_IMETHODIMP
3264 imgCacheValidator::OnStopRequest(nsIRequest* aRequest, nsresult status) {
3265 // Be sure we've released the document that we may have been holding on to.
3266 mDocument = nullptr;
3268 if (!mDestListener) {
3269 return NS_OK;
3272 return mDestListener->OnStopRequest(aRequest, status);
3275 /** nsIStreamListener methods **/
3277 NS_IMETHODIMP
3278 imgCacheValidator::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
3279 uint64_t sourceOffset, uint32_t count) {
3280 if (!mDestListener) {
3281 // XXX see bug 113959
3282 uint32_t _retval;
3283 inStr->ReadSegments(NS_DiscardSegment, nullptr, count, &_retval);
3284 return NS_OK;
3287 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
3290 NS_IMETHODIMP
3291 imgCacheValidator::OnDataFinished(nsresult aStatus) {
3292 if (!mDestListener) {
3293 return NS_ERROR_FAILURE;
3295 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3296 do_QueryInterface(mDestListener);
3297 if (retargetableListener) {
3298 return retargetableListener->OnDataFinished(aStatus);
3301 return NS_OK;
3304 /** nsIThreadRetargetableStreamListener methods **/
3306 NS_IMETHODIMP
3307 imgCacheValidator::CheckListenerChain() {
3308 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3309 nsresult rv = NS_OK;
3310 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3311 do_QueryInterface(mDestListener, &rv);
3312 if (retargetableListener) {
3313 rv = retargetableListener->CheckListenerChain();
3315 MOZ_LOG(
3316 gImgLog, LogLevel::Debug,
3317 ("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32 "=%s",
3318 this, static_cast<uint32_t>(rv),
3319 NS_SUCCEEDED(rv) ? "succeeded" : "failed"));
3320 return rv;
3323 /** nsIInterfaceRequestor methods **/
3325 NS_IMETHODIMP
3326 imgCacheValidator::GetInterface(const nsIID& aIID, void** aResult) {
3327 if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
3328 return QueryInterface(aIID, aResult);
3331 return mProgressProxy->GetInterface(aIID, aResult);
3334 // These functions are materially the same as the same functions in imgRequest.
3335 // We duplicate them because we're verifying whether cache loads are necessary,
3336 // not unconditionally loading.
3338 /** nsIChannelEventSink methods **/
3339 NS_IMETHODIMP
3340 imgCacheValidator::AsyncOnChannelRedirect(
3341 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
3342 nsIAsyncVerifyRedirectCallback* callback) {
3343 // Note all cache information we get from the old channel.
3344 mNewRequest->SetCacheValidation(mNewEntry, oldChannel);
3346 // If the previous URI is a non-HTTPS URI, record that fact for later use by
3347 // security code, which needs to know whether there is an insecure load at any
3348 // point in the redirect chain.
3349 nsCOMPtr<nsIURI> oldURI;
3350 bool schemeLocal = false;
3351 if (NS_FAILED(oldChannel->GetURI(getter_AddRefs(oldURI))) ||
3352 NS_FAILED(NS_URIChainHasFlags(
3353 oldURI, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE, &schemeLocal)) ||
3354 (!oldURI->SchemeIs("https") && !oldURI->SchemeIs("chrome") &&
3355 !schemeLocal)) {
3356 mHadInsecureRedirect = true;
3359 // Prepare for callback
3360 mRedirectCallback = callback;
3361 mRedirectChannel = newChannel;
3363 return mProgressProxy->AsyncOnChannelRedirect(oldChannel, newChannel, flags,
3364 this);
3367 NS_IMETHODIMP
3368 imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult) {
3369 // If we've already been told to abort, just do so.
3370 if (NS_FAILED(aResult)) {
3371 mRedirectCallback->OnRedirectVerifyCallback(aResult);
3372 mRedirectCallback = nullptr;
3373 mRedirectChannel = nullptr;
3374 return NS_OK;
3377 // make sure we have a protocol that returns data rather than opens
3378 // an external application, e.g. mailto:
3379 nsCOMPtr<nsIURI> uri;
3380 mRedirectChannel->GetURI(getter_AddRefs(uri));
3382 nsresult result = NS_OK;
3384 if (nsContentUtils::IsExternalProtocol(uri)) {
3385 result = NS_ERROR_ABORT;
3388 mRedirectCallback->OnRedirectVerifyCallback(result);
3389 mRedirectCallback = nullptr;
3390 mRedirectChannel = nullptr;
3391 return NS_OK;