Bug 1866798 - Add QA tests r=taskgraph-reviewers,releng-reviewers,mboldan,benchatt...
[gecko.git] / image / imgLoader.cpp
blob737d99ff15125fb56e696d481d1600e87d91e211
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/dom/ContentParent.h"
35 #include "mozilla/dom/nsMixedContentBlocker.h"
36 #include "mozilla/image/ImageMemoryReporter.h"
37 #include "mozilla/layers/CompositorManagerChild.h"
38 #include "nsCOMPtr.h"
39 #include "nsCRT.h"
40 #include "nsComponentManagerUtils.h"
41 #include "nsContentPolicyUtils.h"
42 #include "nsContentSecurityManager.h"
43 #include "nsContentUtils.h"
44 #include "nsHttpChannel.h"
45 #include "nsIAsyncVerifyRedirectCallback.h"
46 #include "nsICacheInfoChannel.h"
47 #include "nsIChannelEventSink.h"
48 #include "nsIClassOfService.h"
49 #include "nsIEffectiveTLDService.h"
50 #include "nsIFile.h"
51 #include "nsIFileURL.h"
52 #include "nsIHttpChannel.h"
53 #include "nsIInterfaceRequestor.h"
54 #include "nsIInterfaceRequestorUtils.h"
55 #include "nsIMemoryReporter.h"
56 #include "nsINetworkPredictor.h"
57 #include "nsIProgressEventSink.h"
58 #include "nsIProtocolHandler.h"
59 #include "nsImageModule.h"
60 #include "nsMediaSniffer.h"
61 #include "nsMimeTypes.h"
62 #include "nsNetCID.h"
63 #include "nsNetUtil.h"
64 #include "nsProxyRelease.h"
65 #include "nsQueryObject.h"
66 #include "nsReadableUtils.h"
67 #include "nsStreamUtils.h"
68 #include "prtime.h"
70 // we want to explore making the document own the load group
71 // so we can associate the document URI with the load group.
72 // until this point, we have an evil hack:
73 #include "nsIHttpChannelInternal.h"
74 #include "nsILoadGroupChild.h"
75 #include "nsIDocShell.h"
77 using namespace mozilla;
78 using namespace mozilla::dom;
79 using namespace mozilla::image;
80 using namespace mozilla::net;
82 MOZ_DEFINE_MALLOC_SIZE_OF(ImagesMallocSizeOf)
84 class imgMemoryReporter final : public nsIMemoryReporter {
85 ~imgMemoryReporter() = default;
87 public:
88 NS_DECL_ISUPPORTS
90 NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
91 nsISupports* aData, bool aAnonymize) override {
92 MOZ_ASSERT(NS_IsMainThread());
94 layers::CompositorManagerChild* manager =
95 mozilla::layers::CompositorManagerChild::GetInstance();
96 if (!manager || !StaticPrefs::image_mem_debug_reporting()) {
97 layers::SharedSurfacesMemoryReport sharedSurfaces;
98 FinishCollectReports(aHandleReport, aData, aAnonymize, sharedSurfaces);
99 return NS_OK;
102 RefPtr<imgMemoryReporter> self(this);
103 nsCOMPtr<nsIHandleReportCallback> handleReport(aHandleReport);
104 nsCOMPtr<nsISupports> data(aData);
105 manager->SendReportSharedSurfacesMemory(
106 [=](layers::SharedSurfacesMemoryReport aReport) {
107 self->FinishCollectReports(handleReport, data, aAnonymize, aReport);
109 [=](mozilla::ipc::ResponseRejectReason&& aReason) {
110 layers::SharedSurfacesMemoryReport sharedSurfaces;
111 self->FinishCollectReports(handleReport, data, aAnonymize,
112 sharedSurfaces);
114 return NS_OK;
117 void FinishCollectReports(
118 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
119 bool aAnonymize, layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
120 nsTArray<ImageMemoryCounter> chrome;
121 nsTArray<ImageMemoryCounter> content;
122 nsTArray<ImageMemoryCounter> uncached;
124 for (uint32_t i = 0; i < mKnownLoaders.Length(); i++) {
125 for (imgCacheEntry* entry : mKnownLoaders[i]->mCache.Values()) {
126 RefPtr<imgRequest> req = entry->GetRequest();
127 RecordCounterForRequest(req, &content, !entry->HasNoProxies());
129 MutexAutoLock lock(mKnownLoaders[i]->mUncachedImagesMutex);
130 for (RefPtr<imgRequest> req : mKnownLoaders[i]->mUncachedImages) {
131 RecordCounterForRequest(req, &uncached, req->HasConsumers());
135 // Note that we only need to anonymize content image URIs.
137 ReportCounterArray(aHandleReport, aData, chrome, "images/chrome",
138 /* aAnonymize */ false, aSharedSurfaces);
140 ReportCounterArray(aHandleReport, aData, content, "images/content",
141 aAnonymize, aSharedSurfaces);
143 // Uncached images may be content or chrome, so anonymize them.
144 ReportCounterArray(aHandleReport, aData, uncached, "images/uncached",
145 aAnonymize, aSharedSurfaces);
147 // Report any shared surfaces that were not merged with the surface cache.
148 ImageMemoryReporter::ReportSharedSurfaces(aHandleReport, aData,
149 aSharedSurfaces);
151 nsCOMPtr<nsIMemoryReporterManager> imgr =
152 do_GetService("@mozilla.org/memory-reporter-manager;1");
153 if (imgr) {
154 imgr->EndReport();
158 static int64_t ImagesContentUsedUncompressedDistinguishedAmount() {
159 size_t n = 0;
160 for (uint32_t i = 0; i < imgLoader::sMemReporter->mKnownLoaders.Length();
161 i++) {
162 for (imgCacheEntry* entry :
163 imgLoader::sMemReporter->mKnownLoaders[i]->mCache.Values()) {
164 if (entry->HasNoProxies()) {
165 continue;
168 RefPtr<imgRequest> req = entry->GetRequest();
169 RefPtr<image::Image> image = req->GetImage();
170 if (!image) {
171 continue;
174 // Both this and EntryImageSizes measure
175 // images/content/raster/used/decoded memory. This function's
176 // measurement is secondary -- the result doesn't go in the "explicit"
177 // tree -- so we use moz_malloc_size_of instead of ImagesMallocSizeOf to
178 // prevent DMD from seeing it reported twice.
179 SizeOfState state(moz_malloc_size_of);
180 ImageMemoryCounter counter(req, image, state, /* aIsUsed = */ true);
182 n += counter.Values().DecodedHeap();
183 n += counter.Values().DecodedNonHeap();
184 n += counter.Values().DecodedUnknown();
187 return n;
190 void RegisterLoader(imgLoader* aLoader) {
191 mKnownLoaders.AppendElement(aLoader);
194 void UnregisterLoader(imgLoader* aLoader) {
195 mKnownLoaders.RemoveElement(aLoader);
198 private:
199 nsTArray<imgLoader*> mKnownLoaders;
201 struct MemoryTotal {
202 MemoryTotal& operator+=(const ImageMemoryCounter& aImageCounter) {
203 if (aImageCounter.Type() == imgIContainer::TYPE_RASTER) {
204 if (aImageCounter.IsUsed()) {
205 mUsedRasterCounter += aImageCounter.Values();
206 } else {
207 mUnusedRasterCounter += aImageCounter.Values();
209 } else if (aImageCounter.Type() == imgIContainer::TYPE_VECTOR) {
210 if (aImageCounter.IsUsed()) {
211 mUsedVectorCounter += aImageCounter.Values();
212 } else {
213 mUnusedVectorCounter += aImageCounter.Values();
215 } else if (aImageCounter.Type() == imgIContainer::TYPE_REQUEST) {
216 // Nothing to do, we did not get to the point of having an image.
217 } else {
218 MOZ_CRASH("Unexpected image type");
221 return *this;
224 const MemoryCounter& UsedRaster() const { return mUsedRasterCounter; }
225 const MemoryCounter& UnusedRaster() const { return mUnusedRasterCounter; }
226 const MemoryCounter& UsedVector() const { return mUsedVectorCounter; }
227 const MemoryCounter& UnusedVector() const { return mUnusedVectorCounter; }
229 private:
230 MemoryCounter mUsedRasterCounter;
231 MemoryCounter mUnusedRasterCounter;
232 MemoryCounter mUsedVectorCounter;
233 MemoryCounter mUnusedVectorCounter;
236 // Reports all images of a single kind, e.g. all used chrome images.
237 void ReportCounterArray(nsIHandleReportCallback* aHandleReport,
238 nsISupports* aData,
239 nsTArray<ImageMemoryCounter>& aCounterArray,
240 const char* aPathPrefix, bool aAnonymize,
241 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
242 MemoryTotal summaryTotal;
243 MemoryTotal nonNotableTotal;
245 // Report notable images, and compute total and non-notable aggregate sizes.
246 for (uint32_t i = 0; i < aCounterArray.Length(); i++) {
247 ImageMemoryCounter& counter = aCounterArray[i];
249 if (aAnonymize) {
250 counter.URI().Truncate();
251 counter.URI().AppendPrintf("<anonymized-%u>", i);
252 } else {
253 // The URI could be an extremely long data: URI. Truncate if needed.
254 static const size_t max = 256;
255 if (counter.URI().Length() > max) {
256 counter.URI().Truncate(max);
257 counter.URI().AppendLiteral(" (truncated)");
259 counter.URI().ReplaceChar('/', '\\');
262 summaryTotal += counter;
264 if (counter.IsNotable() || StaticPrefs::image_mem_debug_reporting()) {
265 ReportImage(aHandleReport, aData, aPathPrefix, counter,
266 aSharedSurfaces);
267 } else {
268 ImageMemoryReporter::TrimSharedSurfaces(counter, aSharedSurfaces);
269 nonNotableTotal += counter;
273 // Report non-notable images in aggregate.
274 ReportTotal(aHandleReport, aData, /* aExplicit = */ true, aPathPrefix,
275 "<non-notable images>/", nonNotableTotal);
277 // Report a summary in aggregate, outside of the explicit tree.
278 ReportTotal(aHandleReport, aData, /* aExplicit = */ false, aPathPrefix, "",
279 summaryTotal);
282 static void ReportImage(nsIHandleReportCallback* aHandleReport,
283 nsISupports* aData, const char* aPathPrefix,
284 const ImageMemoryCounter& aCounter,
285 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
286 nsAutoCString pathPrefix("explicit/"_ns);
287 pathPrefix.Append(aPathPrefix);
289 switch (aCounter.Type()) {
290 case imgIContainer::TYPE_RASTER:
291 pathPrefix.AppendLiteral("/raster/");
292 break;
293 case imgIContainer::TYPE_VECTOR:
294 pathPrefix.AppendLiteral("/vector/");
295 break;
296 case imgIContainer::TYPE_REQUEST:
297 pathPrefix.AppendLiteral("/request/");
298 break;
299 default:
300 pathPrefix.AppendLiteral("/unknown=");
301 pathPrefix.AppendInt(aCounter.Type());
302 pathPrefix.AppendLiteral("/");
303 break;
306 pathPrefix.Append(aCounter.IsUsed() ? "used/" : "unused/");
307 if (aCounter.IsValidating()) {
308 pathPrefix.AppendLiteral("validating/");
310 if (aCounter.HasError()) {
311 pathPrefix.AppendLiteral("err/");
314 pathPrefix.AppendLiteral("progress=");
315 pathPrefix.AppendInt(aCounter.Progress(), 16);
316 pathPrefix.AppendLiteral("/");
318 pathPrefix.AppendLiteral("image(");
319 pathPrefix.AppendInt(aCounter.IntrinsicSize().width);
320 pathPrefix.AppendLiteral("x");
321 pathPrefix.AppendInt(aCounter.IntrinsicSize().height);
322 pathPrefix.AppendLiteral(", ");
324 if (aCounter.URI().IsEmpty()) {
325 pathPrefix.AppendLiteral("<unknown URI>");
326 } else {
327 pathPrefix.Append(aCounter.URI());
330 pathPrefix.AppendLiteral(")/");
332 ReportSurfaces(aHandleReport, aData, pathPrefix, aCounter, aSharedSurfaces);
334 ReportSourceValue(aHandleReport, aData, pathPrefix, aCounter.Values());
337 static void ReportSurfaces(
338 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
339 const nsACString& aPathPrefix, const ImageMemoryCounter& aCounter,
340 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
341 for (const SurfaceMemoryCounter& counter : aCounter.Surfaces()) {
342 nsAutoCString surfacePathPrefix(aPathPrefix);
343 switch (counter.Type()) {
344 case SurfaceMemoryCounterType::NORMAL:
345 if (counter.IsLocked()) {
346 surfacePathPrefix.AppendLiteral("locked/");
347 } else {
348 surfacePathPrefix.AppendLiteral("unlocked/");
350 if (counter.IsFactor2()) {
351 surfacePathPrefix.AppendLiteral("factor2/");
353 if (counter.CannotSubstitute()) {
354 surfacePathPrefix.AppendLiteral("cannot_substitute/");
356 break;
357 case SurfaceMemoryCounterType::CONTAINER:
358 surfacePathPrefix.AppendLiteral("container/");
359 break;
360 default:
361 MOZ_ASSERT_UNREACHABLE("Unknown counter type");
362 break;
365 surfacePathPrefix.AppendLiteral("types=");
366 surfacePathPrefix.AppendInt(counter.Values().SurfaceTypes(), 16);
367 surfacePathPrefix.AppendLiteral("/surface(");
368 surfacePathPrefix.AppendInt(counter.Key().Size().width);
369 surfacePathPrefix.AppendLiteral("x");
370 surfacePathPrefix.AppendInt(counter.Key().Size().height);
372 if (!counter.IsFinished()) {
373 surfacePathPrefix.AppendLiteral(", incomplete");
376 if (counter.Values().ExternalHandles() > 0) {
377 surfacePathPrefix.AppendLiteral(", handles:");
378 surfacePathPrefix.AppendInt(
379 uint32_t(counter.Values().ExternalHandles()));
382 ImageMemoryReporter::AppendSharedSurfacePrefix(surfacePathPrefix, counter,
383 aSharedSurfaces);
385 PlaybackType playback = counter.Key().Playback();
386 if (playback == PlaybackType::eAnimated) {
387 if (StaticPrefs::image_mem_debug_reporting()) {
388 surfacePathPrefix.AppendPrintf(
389 " (animation %4u)", uint32_t(counter.Values().FrameIndex()));
390 } else {
391 surfacePathPrefix.AppendLiteral(" (animation)");
395 if (counter.Key().Flags() != DefaultSurfaceFlags()) {
396 surfacePathPrefix.AppendLiteral(", flags:");
397 surfacePathPrefix.AppendInt(uint32_t(counter.Key().Flags()),
398 /* aRadix = */ 16);
401 if (counter.Key().Region()) {
402 const ImageIntRegion& region = counter.Key().Region().ref();
403 const gfx::IntRect& rect = region.Rect();
404 surfacePathPrefix.AppendLiteral(", region:[ rect=(");
405 surfacePathPrefix.AppendInt(rect.x);
406 surfacePathPrefix.AppendLiteral(",");
407 surfacePathPrefix.AppendInt(rect.y);
408 surfacePathPrefix.AppendLiteral(") ");
409 surfacePathPrefix.AppendInt(rect.width);
410 surfacePathPrefix.AppendLiteral("x");
411 surfacePathPrefix.AppendInt(rect.height);
412 if (region.IsRestricted()) {
413 const gfx::IntRect& restrict = region.Restriction();
414 if (restrict == rect) {
415 surfacePathPrefix.AppendLiteral(", restrict=rect");
416 } else {
417 surfacePathPrefix.AppendLiteral(", restrict=(");
418 surfacePathPrefix.AppendInt(restrict.x);
419 surfacePathPrefix.AppendLiteral(",");
420 surfacePathPrefix.AppendInt(restrict.y);
421 surfacePathPrefix.AppendLiteral(") ");
422 surfacePathPrefix.AppendInt(restrict.width);
423 surfacePathPrefix.AppendLiteral("x");
424 surfacePathPrefix.AppendInt(restrict.height);
427 if (region.GetExtendMode() != gfx::ExtendMode::CLAMP) {
428 surfacePathPrefix.AppendLiteral(", extendMode=");
429 surfacePathPrefix.AppendInt(int32_t(region.GetExtendMode()));
431 surfacePathPrefix.AppendLiteral("]");
434 const SVGImageContext& context = counter.Key().SVGContext();
435 surfacePathPrefix.AppendLiteral(", svgContext:[ ");
436 if (context.GetViewportSize()) {
437 const CSSIntSize& size = context.GetViewportSize().ref();
438 surfacePathPrefix.AppendLiteral("viewport=(");
439 surfacePathPrefix.AppendInt(size.width);
440 surfacePathPrefix.AppendLiteral("x");
441 surfacePathPrefix.AppendInt(size.height);
442 surfacePathPrefix.AppendLiteral(") ");
444 if (context.GetPreserveAspectRatio()) {
445 nsAutoString aspect;
446 context.GetPreserveAspectRatio()->ToString(aspect);
447 surfacePathPrefix.AppendLiteral("preserveAspectRatio=(");
448 LossyAppendUTF16toASCII(aspect, surfacePathPrefix);
449 surfacePathPrefix.AppendLiteral(") ");
451 if (auto scheme = context.GetColorScheme()) {
452 surfacePathPrefix.AppendLiteral("colorScheme=");
453 surfacePathPrefix.AppendInt(int32_t(*scheme));
454 surfacePathPrefix.AppendLiteral(" ");
456 if (context.GetContextPaint()) {
457 const SVGEmbeddingContextPaint* paint = context.GetContextPaint();
458 surfacePathPrefix.AppendLiteral("contextPaint=(");
459 if (paint->GetFill()) {
460 surfacePathPrefix.AppendLiteral(" fill=");
461 surfacePathPrefix.AppendInt(paint->GetFill()->ToABGR(), 16);
463 if (paint->GetFillOpacity() != 1.0) {
464 surfacePathPrefix.AppendLiteral(" fillOpa=");
465 surfacePathPrefix.AppendFloat(paint->GetFillOpacity());
467 if (paint->GetStroke()) {
468 surfacePathPrefix.AppendLiteral(" stroke=");
469 surfacePathPrefix.AppendInt(paint->GetStroke()->ToABGR(), 16);
471 if (paint->GetStrokeOpacity() != 1.0) {
472 surfacePathPrefix.AppendLiteral(" strokeOpa=");
473 surfacePathPrefix.AppendFloat(paint->GetStrokeOpacity());
475 surfacePathPrefix.AppendLiteral(" ) ");
477 surfacePathPrefix.AppendLiteral("]");
479 surfacePathPrefix.AppendLiteral(")/");
481 ReportValues(aHandleReport, aData, surfacePathPrefix, counter.Values());
485 static void ReportTotal(nsIHandleReportCallback* aHandleReport,
486 nsISupports* aData, bool aExplicit,
487 const char* aPathPrefix, const char* aPathInfix,
488 const MemoryTotal& aTotal) {
489 nsAutoCString pathPrefix;
490 if (aExplicit) {
491 pathPrefix.AppendLiteral("explicit/");
493 pathPrefix.Append(aPathPrefix);
495 nsAutoCString rasterUsedPrefix(pathPrefix);
496 rasterUsedPrefix.AppendLiteral("/raster/used/");
497 rasterUsedPrefix.Append(aPathInfix);
498 ReportValues(aHandleReport, aData, rasterUsedPrefix, aTotal.UsedRaster());
500 nsAutoCString rasterUnusedPrefix(pathPrefix);
501 rasterUnusedPrefix.AppendLiteral("/raster/unused/");
502 rasterUnusedPrefix.Append(aPathInfix);
503 ReportValues(aHandleReport, aData, rasterUnusedPrefix,
504 aTotal.UnusedRaster());
506 nsAutoCString vectorUsedPrefix(pathPrefix);
507 vectorUsedPrefix.AppendLiteral("/vector/used/");
508 vectorUsedPrefix.Append(aPathInfix);
509 ReportValues(aHandleReport, aData, vectorUsedPrefix, aTotal.UsedVector());
511 nsAutoCString vectorUnusedPrefix(pathPrefix);
512 vectorUnusedPrefix.AppendLiteral("/vector/unused/");
513 vectorUnusedPrefix.Append(aPathInfix);
514 ReportValues(aHandleReport, aData, vectorUnusedPrefix,
515 aTotal.UnusedVector());
518 static void ReportValues(nsIHandleReportCallback* aHandleReport,
519 nsISupports* aData, const nsACString& aPathPrefix,
520 const MemoryCounter& aCounter) {
521 ReportSourceValue(aHandleReport, aData, aPathPrefix, aCounter);
523 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "decoded-heap",
524 "Decoded image data which is stored on the heap.",
525 aCounter.DecodedHeap());
527 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
528 "decoded-nonheap",
529 "Decoded image data which isn't stored on the heap.",
530 aCounter.DecodedNonHeap());
532 // We don't know for certain whether or not it is on the heap, so let's
533 // just report it as non-heap for reporting purposes.
534 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
535 "decoded-unknown",
536 "Decoded image data which is unknown to be on the heap or not.",
537 aCounter.DecodedUnknown());
540 static void ReportSourceValue(nsIHandleReportCallback* aHandleReport,
541 nsISupports* aData,
542 const nsACString& aPathPrefix,
543 const MemoryCounter& aCounter) {
544 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "source",
545 "Raster image source data and vector image documents.",
546 aCounter.Source());
549 static void ReportValue(nsIHandleReportCallback* aHandleReport,
550 nsISupports* aData, int32_t aKind,
551 const nsACString& aPathPrefix,
552 const char* aPathSuffix, const char* aDescription,
553 size_t aValue) {
554 if (aValue == 0) {
555 return;
558 nsAutoCString desc(aDescription);
559 nsAutoCString path(aPathPrefix);
560 path.Append(aPathSuffix);
562 aHandleReport->Callback(""_ns, path, aKind, UNITS_BYTES, aValue, desc,
563 aData);
566 static void RecordCounterForRequest(imgRequest* aRequest,
567 nsTArray<ImageMemoryCounter>* aArray,
568 bool aIsUsed) {
569 SizeOfState state(ImagesMallocSizeOf);
570 RefPtr<image::Image> image = aRequest->GetImage();
571 if (image) {
572 ImageMemoryCounter counter(aRequest, image, state, aIsUsed);
573 aArray->AppendElement(std::move(counter));
574 } else {
575 // We can at least record some information about the image from the
576 // request, and mark it as not knowing the image type yet.
577 ImageMemoryCounter counter(aRequest, state, aIsUsed);
578 aArray->AppendElement(std::move(counter));
583 NS_IMPL_ISUPPORTS(imgMemoryReporter, nsIMemoryReporter)
585 NS_IMPL_ISUPPORTS(nsProgressNotificationProxy, nsIProgressEventSink,
586 nsIChannelEventSink, nsIInterfaceRequestor)
588 NS_IMETHODIMP
589 nsProgressNotificationProxy::OnProgress(nsIRequest* request, int64_t progress,
590 int64_t progressMax) {
591 nsCOMPtr<nsILoadGroup> loadGroup;
592 request->GetLoadGroup(getter_AddRefs(loadGroup));
594 nsCOMPtr<nsIProgressEventSink> target;
595 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
596 NS_GET_IID(nsIProgressEventSink),
597 getter_AddRefs(target));
598 if (!target) {
599 return NS_OK;
601 return target->OnProgress(mImageRequest, progress, progressMax);
604 NS_IMETHODIMP
605 nsProgressNotificationProxy::OnStatus(nsIRequest* request, nsresult status,
606 const char16_t* statusArg) {
607 nsCOMPtr<nsILoadGroup> loadGroup;
608 request->GetLoadGroup(getter_AddRefs(loadGroup));
610 nsCOMPtr<nsIProgressEventSink> target;
611 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
612 NS_GET_IID(nsIProgressEventSink),
613 getter_AddRefs(target));
614 if (!target) {
615 return NS_OK;
617 return target->OnStatus(mImageRequest, status, statusArg);
620 NS_IMETHODIMP
621 nsProgressNotificationProxy::AsyncOnChannelRedirect(
622 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
623 nsIAsyncVerifyRedirectCallback* cb) {
624 // Tell the original original callbacks about it too
625 nsCOMPtr<nsILoadGroup> loadGroup;
626 newChannel->GetLoadGroup(getter_AddRefs(loadGroup));
627 nsCOMPtr<nsIChannelEventSink> target;
628 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
629 NS_GET_IID(nsIChannelEventSink),
630 getter_AddRefs(target));
631 if (!target) {
632 cb->OnRedirectVerifyCallback(NS_OK);
633 return NS_OK;
636 // Delegate to |target| if set, reusing |cb|
637 return target->AsyncOnChannelRedirect(oldChannel, newChannel, flags, cb);
640 NS_IMETHODIMP
641 nsProgressNotificationProxy::GetInterface(const nsIID& iid, void** result) {
642 if (iid.Equals(NS_GET_IID(nsIProgressEventSink))) {
643 *result = static_cast<nsIProgressEventSink*>(this);
644 NS_ADDREF_THIS();
645 return NS_OK;
647 if (iid.Equals(NS_GET_IID(nsIChannelEventSink))) {
648 *result = static_cast<nsIChannelEventSink*>(this);
649 NS_ADDREF_THIS();
650 return NS_OK;
652 if (mOriginalCallbacks) {
653 return mOriginalCallbacks->GetInterface(iid, result);
655 return NS_NOINTERFACE;
658 static void NewRequestAndEntry(bool aForcePrincipalCheckForCacheEntry,
659 imgLoader* aLoader, const ImageCacheKey& aKey,
660 imgRequest** aRequest, imgCacheEntry** aEntry) {
661 RefPtr<imgRequest> request = new imgRequest(aLoader, aKey);
662 RefPtr<imgCacheEntry> entry =
663 new imgCacheEntry(aLoader, request, aForcePrincipalCheckForCacheEntry);
664 aLoader->AddToUncachedImages(request);
665 request.forget(aRequest);
666 entry.forget(aEntry);
669 static bool ShouldRevalidateEntry(imgCacheEntry* aEntry, nsLoadFlags aFlags,
670 bool aHasExpired) {
671 if (aFlags & nsIRequest::LOAD_BYPASS_CACHE) {
672 return false;
674 if (aFlags & nsIRequest::VALIDATE_ALWAYS) {
675 return true;
677 if (aEntry->GetMustValidate()) {
678 return true;
680 if (aHasExpired) {
681 // The cache entry has expired... Determine whether the stale cache
682 // entry can be used without validation...
683 if (aFlags & (nsIRequest::LOAD_FROM_CACHE | nsIRequest::VALIDATE_NEVER |
684 nsIRequest::VALIDATE_ONCE_PER_SESSION)) {
685 // LOAD_FROM_CACHE, VALIDATE_NEVER and VALIDATE_ONCE_PER_SESSION allow
686 // stale cache entries to be used unless they have been explicitly marked
687 // to indicate that revalidation is necessary.
688 return false;
690 // Entry is expired, revalidate.
691 return true;
693 return false;
696 /* Call content policies on cached images that went through a redirect */
697 static bool ShouldLoadCachedImage(imgRequest* aImgRequest,
698 Document* aLoadingDocument,
699 nsIPrincipal* aTriggeringPrincipal,
700 nsContentPolicyType aPolicyType,
701 bool aSendCSPViolationReports) {
702 /* Call content policies on cached images - Bug 1082837
703 * Cached images are keyed off of the first uri in a redirect chain.
704 * Hence content policies don't get a chance to test the intermediate hops
705 * or the final destination. Here we test the final destination using
706 * mFinalURI off of the imgRequest and passing it into content policies.
707 * For Mixed Content Blocker, we do an additional check to determine if any
708 * of the intermediary hops went through an insecure redirect with the
709 * mHadInsecureRedirect flag
711 bool insecureRedirect = aImgRequest->HadInsecureRedirect();
712 nsCOMPtr<nsIURI> contentLocation;
713 aImgRequest->GetFinalURI(getter_AddRefs(contentLocation));
714 nsresult rv;
716 nsCOMPtr<nsIPrincipal> loadingPrincipal =
717 aLoadingDocument ? aLoadingDocument->NodePrincipal()
718 : aTriggeringPrincipal;
719 // If there is no context and also no triggeringPrincipal, then we use a fresh
720 // nullPrincipal as the loadingPrincipal because we can not create a loadinfo
721 // without a valid loadingPrincipal.
722 if (!loadingPrincipal) {
723 loadingPrincipal = NullPrincipal::CreateWithoutOriginAttributes();
726 nsCOMPtr<nsILoadInfo> secCheckLoadInfo = new LoadInfo(
727 loadingPrincipal, aTriggeringPrincipal, aLoadingDocument,
728 nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK, aPolicyType);
730 secCheckLoadInfo->SetSendCSPViolationEvents(aSendCSPViolationReports);
732 int16_t decision = nsIContentPolicy::REJECT_REQUEST;
733 rv = NS_CheckContentLoadPolicy(contentLocation, secCheckLoadInfo, &decision,
734 nsContentUtils::GetContentPolicy());
735 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
736 return false;
739 // We call all Content Policies above, but we also have to call mcb
740 // individually to check the intermediary redirect hops are secure.
741 if (insecureRedirect) {
742 // Bug 1314356: If the image ended up in the cache upgraded by HSTS and the
743 // page uses upgrade-inscure-requests it had an insecure redirect
744 // (http->https). We need to invalidate the image and reload it because
745 // mixed content blocker only bails if upgrade-insecure-requests is set on
746 // the doc and the resource load is http: which would result in an incorrect
747 // mixed content warning.
748 nsCOMPtr<nsIDocShell> docShell =
749 NS_CP_GetDocShellFromContext(ToSupports(aLoadingDocument));
750 if (docShell) {
751 Document* document = docShell->GetDocument();
752 if (document && document->GetUpgradeInsecureRequests(false)) {
753 return false;
757 if (!aTriggeringPrincipal || !aTriggeringPrincipal->IsSystemPrincipal()) {
758 // reset the decision for mixed content blocker check
759 decision = nsIContentPolicy::REJECT_REQUEST;
760 rv = nsMixedContentBlocker::ShouldLoad(insecureRedirect, contentLocation,
761 secCheckLoadInfo,
762 true, // aReportError
763 &decision);
764 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
765 return false;
770 return true;
773 // Returns true if this request is compatible with the given CORS mode on the
774 // given loading principal, and false if the request may not be reused due
775 // to CORS.
776 static bool ValidateCORSMode(imgRequest* aRequest, bool aForcePrincipalCheck,
777 CORSMode aCORSMode,
778 nsIPrincipal* aTriggeringPrincipal) {
779 // If the entry's CORS mode doesn't match, or the CORS mode matches but the
780 // document principal isn't the same, we can't use this request.
781 if (aRequest->GetCORSMode() != aCORSMode) {
782 return false;
785 if (aRequest->GetCORSMode() != CORS_NONE || aForcePrincipalCheck) {
786 nsCOMPtr<nsIPrincipal> otherprincipal = aRequest->GetTriggeringPrincipal();
788 // If we previously had a principal, but we don't now, we can't use this
789 // request.
790 if (otherprincipal && !aTriggeringPrincipal) {
791 return false;
794 if (otherprincipal && aTriggeringPrincipal &&
795 !otherprincipal->Equals(aTriggeringPrincipal)) {
796 return false;
800 return true;
803 static bool ValidateSecurityInfo(imgRequest* aRequest,
804 bool aForcePrincipalCheck, CORSMode aCORSMode,
805 nsIPrincipal* aTriggeringPrincipal,
806 Document* aLoadingDocument,
807 nsContentPolicyType aPolicyType) {
808 if (!ValidateCORSMode(aRequest, aForcePrincipalCheck, aCORSMode,
809 aTriggeringPrincipal)) {
810 return false;
812 // Content Policy Check on Cached Images
813 return ShouldLoadCachedImage(aRequest, aLoadingDocument, aTriggeringPrincipal,
814 aPolicyType,
815 /* aSendCSPViolationReports */ false);
818 static nsresult NewImageChannel(
819 nsIChannel** aResult,
820 // If aForcePrincipalCheckForCacheEntry is true, then we will
821 // force a principal check even when not using CORS before
822 // assuming we have a cache hit on a cache entry that we
823 // create for this channel. This is an out param that should
824 // be set to true if this channel ends up depending on
825 // aTriggeringPrincipal and false otherwise.
826 bool* aForcePrincipalCheckForCacheEntry, nsIURI* aURI,
827 nsIURI* aInitialDocumentURI, CORSMode aCORSMode,
828 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
829 nsLoadFlags aLoadFlags, nsContentPolicyType aPolicyType,
830 nsIPrincipal* aTriggeringPrincipal, nsINode* aRequestingNode,
831 bool aRespectPrivacy, uint64_t aEarlyHintPreloaderId) {
832 MOZ_ASSERT(aResult);
834 nsresult rv;
835 nsCOMPtr<nsIHttpChannel> newHttpChannel;
837 nsCOMPtr<nsIInterfaceRequestor> callbacks;
839 if (aLoadGroup) {
840 // Get the notification callbacks from the load group for the new channel.
842 // XXX: This is not exactly correct, because the network request could be
843 // referenced by multiple windows... However, the new channel needs
844 // something. So, using the 'first' notification callbacks is better
845 // than nothing...
847 aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
850 // Pass in a nullptr loadgroup because this is the underlying network
851 // request. This request may be referenced by several proxy image requests
852 // (possibly in different documents).
853 // If all of the proxy requests are canceled then this request should be
854 // canceled too.
857 nsSecurityFlags securityFlags =
858 nsContentSecurityManager::ComputeSecurityFlags(
859 aCORSMode, nsContentSecurityManager::CORSSecurityMapping::
860 CORS_NONE_MAPS_TO_INHERITED_CONTEXT);
862 securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
864 // Note we are calling NS_NewChannelWithTriggeringPrincipal() here with a
865 // node and a principal. This is for things like background images that are
866 // specified by user stylesheets, where the document is being styled, but
867 // the principal is that of the user stylesheet.
868 if (aRequestingNode && aTriggeringPrincipal) {
869 rv = NS_NewChannelWithTriggeringPrincipal(aResult, aURI, aRequestingNode,
870 aTriggeringPrincipal,
871 securityFlags, aPolicyType,
872 nullptr, // PerformanceStorage
873 nullptr, // loadGroup
874 callbacks, aLoadFlags);
876 if (NS_FAILED(rv)) {
877 return rv;
880 if (aPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
881 // If this is a favicon loading, we will use the originAttributes from the
882 // triggeringPrincipal as the channel's originAttributes. This allows the
883 // favicon loading from XUL will use the correct originAttributes.
885 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
886 rv = loadInfo->SetOriginAttributes(
887 aTriggeringPrincipal->OriginAttributesRef());
889 } else {
890 // either we are loading something inside a document, in which case
891 // we should always have a requestingNode, or we are loading something
892 // outside a document, in which case the triggeringPrincipal and
893 // triggeringPrincipal should always be the systemPrincipal.
894 // However, there are exceptions: one is Notifications which create a
895 // channel in the parent process in which case we can't get a
896 // requestingNode.
897 rv = NS_NewChannel(aResult, aURI, nsContentUtils::GetSystemPrincipal(),
898 securityFlags, aPolicyType,
899 nullptr, // nsICookieJarSettings
900 nullptr, // PerformanceStorage
901 nullptr, // loadGroup
902 callbacks, aLoadFlags);
904 if (NS_FAILED(rv)) {
905 return rv;
908 // Use the OriginAttributes from the loading principal, if one is available,
909 // and adjust the private browsing ID based on what kind of load the caller
910 // has asked us to perform.
911 OriginAttributes attrs;
912 if (aTriggeringPrincipal) {
913 attrs = aTriggeringPrincipal->OriginAttributesRef();
915 attrs.mPrivateBrowsingId = aRespectPrivacy ? 1 : 0;
917 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
918 rv = loadInfo->SetOriginAttributes(attrs);
921 if (NS_FAILED(rv)) {
922 return rv;
925 // only inherit if we have a principal
926 *aForcePrincipalCheckForCacheEntry =
927 aTriggeringPrincipal && nsContentUtils::ChannelShouldInheritPrincipal(
928 aTriggeringPrincipal, aURI,
929 /* aInheritForAboutBlank */ false,
930 /* aForceInherit */ false);
932 // Initialize HTTP-specific attributes
933 newHttpChannel = do_QueryInterface(*aResult);
934 if (newHttpChannel) {
935 nsCOMPtr<nsIHttpChannelInternal> httpChannelInternal =
936 do_QueryInterface(newHttpChannel);
937 NS_ENSURE_TRUE(httpChannelInternal, NS_ERROR_UNEXPECTED);
938 rv = httpChannelInternal->SetDocumentURI(aInitialDocumentURI);
939 MOZ_ASSERT(NS_SUCCEEDED(rv));
940 if (aReferrerInfo) {
941 DebugOnly<nsresult> rv = newHttpChannel->SetReferrerInfo(aReferrerInfo);
942 MOZ_ASSERT(NS_SUCCEEDED(rv));
945 if (aEarlyHintPreloaderId) {
946 rv = httpChannelInternal->SetEarlyHintPreloaderId(aEarlyHintPreloaderId);
947 NS_ENSURE_SUCCESS(rv, rv);
951 // Image channels are loaded by default with reduced priority.
952 nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(*aResult);
953 if (p) {
954 uint32_t priority = nsISupportsPriority::PRIORITY_LOW;
956 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
957 ++priority; // further reduce priority for background loads
960 p->AdjustPriority(priority);
963 // Create a new loadgroup for this new channel, using the old group as
964 // the parent. The indirection keeps the channel insulated from cancels,
965 // but does allow a way for this revalidation to be associated with at
966 // least one base load group for scheduling/caching purposes.
968 nsCOMPtr<nsILoadGroup> loadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID);
969 nsCOMPtr<nsILoadGroupChild> childLoadGroup = do_QueryInterface(loadGroup);
970 if (childLoadGroup) {
971 childLoadGroup->SetParentLoadGroup(aLoadGroup);
973 (*aResult)->SetLoadGroup(loadGroup);
975 return NS_OK;
978 static uint32_t SecondsFromPRTime(PRTime aTime) {
979 return nsContentUtils::SecondsFromPRTime(aTime);
982 /* static */
983 imgCacheEntry::imgCacheEntry(imgLoader* loader, imgRequest* request,
984 bool forcePrincipalCheck)
985 : mLoader(loader),
986 mRequest(request),
987 mDataSize(0),
988 mTouchedTime(SecondsFromPRTime(PR_Now())),
989 mLoadTime(SecondsFromPRTime(PR_Now())),
990 mExpiryTime(0),
991 mMustValidate(false),
992 // We start off as evicted so we don't try to update the cache.
993 // PutIntoCache will set this to false.
994 mEvicted(true),
995 mHasNoProxies(true),
996 mForcePrincipalCheck(forcePrincipalCheck),
997 mHasNotified(false) {}
999 imgCacheEntry::~imgCacheEntry() {
1000 LOG_FUNC(gImgLog, "imgCacheEntry::~imgCacheEntry()");
1003 void imgCacheEntry::Touch(bool updateTime /* = true */) {
1004 LOG_SCOPE(gImgLog, "imgCacheEntry::Touch");
1006 if (updateTime) {
1007 mTouchedTime = SecondsFromPRTime(PR_Now());
1010 UpdateCache();
1013 void imgCacheEntry::UpdateCache(int32_t diff /* = 0 */) {
1014 // Don't update the cache if we've been removed from it or it doesn't care
1015 // about our size or usage.
1016 if (!Evicted() && HasNoProxies()) {
1017 mLoader->CacheEntriesChanged(diff);
1021 void imgCacheEntry::UpdateLoadTime() {
1022 mLoadTime = SecondsFromPRTime(PR_Now());
1025 void imgCacheEntry::SetHasNoProxies(bool hasNoProxies) {
1026 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1027 if (hasNoProxies) {
1028 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies true", "uri",
1029 mRequest->CacheKey().URI());
1030 } else {
1031 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies false",
1032 "uri", mRequest->CacheKey().URI());
1036 mHasNoProxies = hasNoProxies;
1039 imgCacheQueue::imgCacheQueue() : mDirty(false), mSize(0) {}
1041 void imgCacheQueue::UpdateSize(int32_t diff) { mSize += diff; }
1043 uint32_t imgCacheQueue::GetSize() const { return mSize; }
1045 void imgCacheQueue::Remove(imgCacheEntry* entry) {
1046 uint64_t index = mQueue.IndexOf(entry);
1047 if (index == queueContainer::NoIndex) {
1048 return;
1051 mSize -= mQueue[index]->GetDataSize();
1053 // If the queue is clean and this is the first entry,
1054 // then we can efficiently remove the entry without
1055 // dirtying the sort order.
1056 if (!IsDirty() && index == 0) {
1057 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1058 mQueue.RemoveLastElement();
1059 return;
1062 // Remove from the middle of the list. This potentially
1063 // breaks the binary heap sort order.
1064 mQueue.RemoveElementAt(index);
1066 // If we only have one entry or the queue is empty, though,
1067 // then the sort order is still effectively good. Simply
1068 // refresh the list to clear the dirty flag.
1069 if (mQueue.Length() <= 1) {
1070 Refresh();
1071 return;
1074 // Otherwise we must mark the queue dirty and potentially
1075 // trigger an expensive sort later.
1076 MarkDirty();
1079 void imgCacheQueue::Push(imgCacheEntry* entry) {
1080 mSize += entry->GetDataSize();
1082 RefPtr<imgCacheEntry> refptr(entry);
1083 mQueue.AppendElement(std::move(refptr));
1084 // If we're not dirty already, then we can efficiently add this to the
1085 // binary heap immediately. This is only O(log n).
1086 if (!IsDirty()) {
1087 std::push_heap(mQueue.begin(), mQueue.end(),
1088 imgLoader::CompareCacheEntries);
1092 already_AddRefed<imgCacheEntry> imgCacheQueue::Pop() {
1093 if (mQueue.IsEmpty()) {
1094 return nullptr;
1096 if (IsDirty()) {
1097 Refresh();
1100 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1101 RefPtr<imgCacheEntry> entry = mQueue.PopLastElement();
1103 mSize -= entry->GetDataSize();
1104 return entry.forget();
1107 void imgCacheQueue::Refresh() {
1108 // Resort the list. This is an O(3 * n) operation and best avoided
1109 // if possible.
1110 std::make_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1111 mDirty = false;
1114 void imgCacheQueue::MarkDirty() { mDirty = true; }
1116 bool imgCacheQueue::IsDirty() { return mDirty; }
1118 uint32_t imgCacheQueue::GetNumElements() const { return mQueue.Length(); }
1120 bool imgCacheQueue::Contains(imgCacheEntry* aEntry) const {
1121 return mQueue.Contains(aEntry);
1124 imgCacheQueue::iterator imgCacheQueue::begin() { return mQueue.begin(); }
1126 imgCacheQueue::const_iterator imgCacheQueue::begin() const {
1127 return mQueue.begin();
1130 imgCacheQueue::iterator imgCacheQueue::end() { return mQueue.end(); }
1132 imgCacheQueue::const_iterator imgCacheQueue::end() const {
1133 return mQueue.end();
1136 nsresult imgLoader::CreateNewProxyForRequest(
1137 imgRequest* aRequest, nsIURI* aURI, nsILoadGroup* aLoadGroup,
1138 Document* aLoadingDocument, imgINotificationObserver* aObserver,
1139 nsLoadFlags aLoadFlags, imgRequestProxy** _retval) {
1140 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::CreateNewProxyForRequest",
1141 "imgRequest", aRequest);
1143 /* XXX If we move decoding onto separate threads, we should save off the
1144 calling thread here and pass it off to |proxyRequest| so that it call
1145 proxy calls to |aObserver|.
1148 RefPtr<imgRequestProxy> proxyRequest = new imgRequestProxy();
1150 /* It is important to call |SetLoadFlags()| before calling |Init()| because
1151 |Init()| adds the request to the loadgroup.
1153 proxyRequest->SetLoadFlags(aLoadFlags);
1155 // init adds itself to imgRequest's list of observers
1156 nsresult rv = proxyRequest->Init(aRequest, aLoadGroup, aURI, aObserver);
1157 if (NS_WARN_IF(NS_FAILED(rv))) {
1158 return rv;
1161 proxyRequest.forget(_retval);
1162 return NS_OK;
1165 class imgCacheExpirationTracker final
1166 : public nsExpirationTracker<imgCacheEntry, 3> {
1167 enum { TIMEOUT_SECONDS = 10 };
1169 public:
1170 imgCacheExpirationTracker();
1172 protected:
1173 void NotifyExpired(imgCacheEntry* entry) override;
1176 imgCacheExpirationTracker::imgCacheExpirationTracker()
1177 : nsExpirationTracker<imgCacheEntry, 3>(TIMEOUT_SECONDS * 1000,
1178 "imgCacheExpirationTracker") {}
1180 void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry* entry) {
1181 // Hold on to a reference to this entry, because the expiration tracker
1182 // mechanism doesn't.
1183 RefPtr<imgCacheEntry> kungFuDeathGrip(entry);
1185 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1186 RefPtr<imgRequest> req = entry->GetRequest();
1187 if (req) {
1188 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheExpirationTracker::NotifyExpired",
1189 "entry", req->CacheKey().URI());
1193 // We can be called multiple times on the same entry. Don't do work multiple
1194 // times.
1195 if (!entry->Evicted()) {
1196 entry->Loader()->RemoveFromCache(entry);
1199 entry->Loader()->VerifyCacheSizes();
1202 ///////////////////////////////////////////////////////////////////////////////
1203 // imgLoader
1204 ///////////////////////////////////////////////////////////////////////////////
1206 double imgLoader::sCacheTimeWeight;
1207 uint32_t imgLoader::sCacheMaxSize;
1208 imgMemoryReporter* imgLoader::sMemReporter;
1210 NS_IMPL_ISUPPORTS(imgLoader, imgILoader, nsIContentSniffer, imgICache,
1211 nsISupportsWeakReference, nsIObserver)
1213 static imgLoader* gNormalLoader = nullptr;
1214 static imgLoader* gPrivateBrowsingLoader = nullptr;
1216 /* static */
1217 already_AddRefed<imgLoader> imgLoader::CreateImageLoader() {
1218 // In some cases, such as xpctests, XPCOM modules are not automatically
1219 // initialized. We need to make sure that our module is initialized before
1220 // we hand out imgLoader instances and code starts using them.
1221 mozilla::image::EnsureModuleInitialized();
1223 RefPtr<imgLoader> loader = new imgLoader();
1224 loader->Init();
1226 return loader.forget();
1229 imgLoader* imgLoader::NormalLoader() {
1230 if (!gNormalLoader) {
1231 gNormalLoader = CreateImageLoader().take();
1233 return gNormalLoader;
1236 imgLoader* imgLoader::PrivateBrowsingLoader() {
1237 if (!gPrivateBrowsingLoader) {
1238 gPrivateBrowsingLoader = CreateImageLoader().take();
1239 gPrivateBrowsingLoader->RespectPrivacyNotifications();
1241 return gPrivateBrowsingLoader;
1244 imgLoader::imgLoader()
1245 : mUncachedImagesMutex("imgLoader::UncachedImages"),
1246 mRespectPrivacy(false) {
1247 sMemReporter->AddRef();
1248 sMemReporter->RegisterLoader(this);
1251 imgLoader::~imgLoader() {
1252 ClearImageCache();
1254 // If there are any of our imgRequest's left they are in the uncached
1255 // images set, so clear their pointer to us.
1256 MutexAutoLock lock(mUncachedImagesMutex);
1257 for (RefPtr<imgRequest> req : mUncachedImages) {
1258 req->ClearLoader();
1261 sMemReporter->UnregisterLoader(this);
1262 sMemReporter->Release();
1265 void imgLoader::VerifyCacheSizes() {
1266 #ifdef DEBUG
1267 if (!mCacheTracker) {
1268 return;
1271 uint32_t cachesize = mCache.Count();
1272 uint32_t queuesize = mCacheQueue.GetNumElements();
1273 uint32_t trackersize = 0;
1274 for (nsExpirationTracker<imgCacheEntry, 3>::Iterator it(mCacheTracker.get());
1275 it.Next();) {
1276 trackersize++;
1278 MOZ_ASSERT(queuesize == trackersize, "Queue and tracker sizes out of sync!");
1279 MOZ_ASSERT(queuesize <= cachesize, "Queue has more elements than cache!");
1280 #endif
1283 void imgLoader::GlobalInit() {
1284 sCacheTimeWeight = StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
1285 int32_t cachesize = StaticPrefs::image_cache_size_AtStartup();
1286 sCacheMaxSize = cachesize > 0 ? cachesize : 0;
1288 sMemReporter = new imgMemoryReporter();
1289 RegisterStrongAsyncMemoryReporter(sMemReporter);
1290 RegisterImagesContentUsedUncompressedDistinguishedAmount(
1291 imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount);
1294 void imgLoader::ShutdownMemoryReporter() {
1295 UnregisterImagesContentUsedUncompressedDistinguishedAmount();
1296 UnregisterStrongMemoryReporter(sMemReporter);
1299 nsresult imgLoader::InitCache() {
1300 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
1301 if (!os) {
1302 return NS_ERROR_FAILURE;
1305 os->AddObserver(this, "memory-pressure", false);
1306 os->AddObserver(this, "chrome-flush-caches", false);
1307 os->AddObserver(this, "last-pb-context-exited", false);
1308 os->AddObserver(this, "profile-before-change", false);
1309 os->AddObserver(this, "xpcom-shutdown", false);
1311 mCacheTracker = MakeUnique<imgCacheExpirationTracker>();
1313 return NS_OK;
1316 nsresult imgLoader::Init() {
1317 InitCache();
1319 return NS_OK;
1322 NS_IMETHODIMP
1323 imgLoader::RespectPrivacyNotifications() {
1324 mRespectPrivacy = true;
1325 return NS_OK;
1328 NS_IMETHODIMP
1329 imgLoader::Observe(nsISupports* aSubject, const char* aTopic,
1330 const char16_t* aData) {
1331 if (strcmp(aTopic, "memory-pressure") == 0) {
1332 MinimizeCache();
1333 } else if (strcmp(aTopic, "chrome-flush-caches") == 0) {
1334 MinimizeCache();
1335 ClearImageCache({ClearOption::ChromeOnly});
1336 } else if (strcmp(aTopic, "last-pb-context-exited") == 0) {
1337 if (mRespectPrivacy) {
1338 ClearImageCache();
1340 } else if (strcmp(aTopic, "profile-before-change") == 0) {
1341 mCacheTracker = nullptr;
1342 } else if (strcmp(aTopic, "xpcom-shutdown") == 0) {
1343 mCacheTracker = nullptr;
1344 ShutdownMemoryReporter();
1346 } else {
1347 // (Nothing else should bring us here)
1348 MOZ_ASSERT(0, "Invalid topic received");
1351 return NS_OK;
1354 NS_IMETHODIMP
1355 imgLoader::ClearCache(bool chrome) {
1356 if (XRE_IsParentProcess()) {
1357 bool privateLoader = this == gPrivateBrowsingLoader;
1358 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1359 Unused << cp->SendClearImageCache(privateLoader, chrome);
1362 ClearOptions options;
1363 if (chrome) {
1364 options += ClearOption::ChromeOnly;
1366 return ClearImageCache(options);
1369 NS_IMETHODIMP
1370 imgLoader::RemoveEntriesFromPrincipalInAllProcesses(nsIPrincipal* aPrincipal) {
1371 if (!XRE_IsParentProcess()) {
1372 return NS_ERROR_NOT_AVAILABLE;
1375 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1376 Unused << cp->SendClearImageCacheFromPrincipal(aPrincipal);
1379 imgLoader* loader;
1380 if (aPrincipal->OriginAttributesRef().mPrivateBrowsingId ==
1381 nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID) {
1382 loader = imgLoader::NormalLoader();
1383 } else {
1384 loader = imgLoader::PrivateBrowsingLoader();
1387 return loader->RemoveEntriesInternal(aPrincipal, nullptr);
1390 NS_IMETHODIMP
1391 imgLoader::RemoveEntriesFromBaseDomainInAllProcesses(
1392 const nsACString& aBaseDomain) {
1393 if (!XRE_IsParentProcess()) {
1394 return NS_ERROR_NOT_AVAILABLE;
1397 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1398 Unused << cp->SendClearImageCacheFromBaseDomain(aBaseDomain);
1401 return RemoveEntriesInternal(nullptr, &aBaseDomain);
1404 nsresult imgLoader::RemoveEntriesInternal(nsIPrincipal* aPrincipal,
1405 const nsACString* aBaseDomain) {
1406 // Can only clear by either principal or base domain.
1407 if ((!aPrincipal && !aBaseDomain) || (aPrincipal && aBaseDomain)) {
1408 return NS_ERROR_INVALID_ARG;
1411 nsCOMPtr<nsIEffectiveTLDService> tldService;
1412 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1414 // For base domain we only clear the non-chrome cache.
1415 for (const auto& entry : mCache) {
1416 const auto& key = entry.GetKey();
1418 const bool shouldRemove = [&] {
1419 if (aPrincipal) {
1420 nsCOMPtr<nsIPrincipal> keyPrincipal =
1421 BasePrincipal::CreateContentPrincipal(key.URI(),
1422 key.OriginAttributesRef());
1423 return keyPrincipal->Equals(aPrincipal);
1426 if (!aBaseDomain) {
1427 return false;
1429 // Clear by baseDomain.
1430 nsAutoCString host;
1431 nsresult rv = key.URI()->GetHost(host);
1432 if (NS_FAILED(rv) || host.IsEmpty()) {
1433 return false;
1436 if (!tldService) {
1437 tldService = do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
1439 if (NS_WARN_IF(!tldService)) {
1440 return false;
1443 bool hasRootDomain = false;
1444 rv = tldService->HasRootDomain(host, *aBaseDomain, &hasRootDomain);
1445 if (NS_SUCCEEDED(rv) && hasRootDomain) {
1446 return true;
1449 // If we don't get a direct base domain match, also check for cache of
1450 // third parties partitioned under aBaseDomain.
1452 // The isolation key is either just the base domain, or an origin suffix
1453 // which contains the partitionKey holding the baseDomain.
1455 if (key.IsolationKeyRef().Equals(*aBaseDomain)) {
1456 return true;
1459 // The isolation key does not match the given base domain. It may be an
1460 // origin suffix. Parse it into origin attributes.
1461 OriginAttributes attrs;
1462 if (!attrs.PopulateFromSuffix(key.IsolationKeyRef())) {
1463 // Key is not an origin suffix.
1464 return false;
1467 return StoragePrincipalHelper::PartitionKeyHasBaseDomain(
1468 attrs.mPartitionKey, *aBaseDomain);
1469 }();
1471 if (shouldRemove) {
1472 entriesToBeRemoved.AppendElement(entry.GetData());
1476 for (auto& entry : entriesToBeRemoved) {
1477 if (!RemoveFromCache(entry)) {
1478 NS_WARNING(
1479 "Couldn't remove an entry from the cache in "
1480 "RemoveEntriesInternal()\n");
1484 return NS_OK;
1487 constexpr auto AllCORSModes() {
1488 return MakeInclusiveEnumeratedRange(kFirstCORSMode, kLastCORSMode);
1491 NS_IMETHODIMP
1492 imgLoader::RemoveEntry(nsIURI* aURI, Document* aDoc) {
1493 if (!aURI) {
1494 return NS_OK;
1496 OriginAttributes attrs;
1497 if (aDoc) {
1498 attrs = aDoc->NodePrincipal()->OriginAttributesRef();
1500 for (auto corsMode : AllCORSModes()) {
1501 ImageCacheKey key(aURI, corsMode, attrs, aDoc);
1502 RemoveFromCache(key);
1504 return NS_OK;
1507 NS_IMETHODIMP
1508 imgLoader::FindEntryProperties(nsIURI* uri, Document* aDoc,
1509 nsIProperties** _retval) {
1510 *_retval = nullptr;
1512 OriginAttributes attrs;
1513 if (aDoc) {
1514 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1515 if (principal) {
1516 attrs = principal->OriginAttributesRef();
1520 for (auto corsMode : AllCORSModes()) {
1521 ImageCacheKey key(uri, corsMode, attrs, aDoc);
1522 RefPtr<imgCacheEntry> entry;
1523 if (!mCache.Get(key, getter_AddRefs(entry)) || !entry) {
1524 continue;
1526 if (mCacheTracker && entry->HasNoProxies()) {
1527 mCacheTracker->MarkUsed(entry);
1529 RefPtr<imgRequest> request = entry->GetRequest();
1530 if (request) {
1531 nsCOMPtr<nsIProperties> properties = request->Properties();
1532 properties.forget(_retval);
1533 return NS_OK;
1536 return NS_OK;
1539 NS_IMETHODIMP_(void)
1540 imgLoader::ClearCacheForControlledDocument(Document* aDoc) {
1541 MOZ_ASSERT(aDoc);
1542 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1543 for (const auto& entry : mCache) {
1544 const auto& key = entry.GetKey();
1545 if (key.ControlledDocument() == aDoc) {
1546 entriesToBeRemoved.AppendElement(entry.GetData());
1549 for (auto& entry : entriesToBeRemoved) {
1550 if (!RemoveFromCache(entry)) {
1551 NS_WARNING(
1552 "Couldn't remove an entry from the cache in "
1553 "ClearCacheForControlledDocument()\n");
1558 void imgLoader::Shutdown() {
1559 NS_IF_RELEASE(gNormalLoader);
1560 gNormalLoader = nullptr;
1561 NS_IF_RELEASE(gPrivateBrowsingLoader);
1562 gPrivateBrowsingLoader = nullptr;
1565 bool imgLoader::PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* entry) {
1566 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::PutIntoCache", "uri",
1567 aKey.URI());
1569 // Check to see if this request already exists in the cache. If so, we'll
1570 // replace the old version.
1571 RefPtr<imgCacheEntry> tmpCacheEntry;
1572 if (mCache.Get(aKey, getter_AddRefs(tmpCacheEntry)) && tmpCacheEntry) {
1573 MOZ_LOG(
1574 gImgLog, LogLevel::Debug,
1575 ("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
1576 nullptr));
1577 RefPtr<imgRequest> tmpRequest = tmpCacheEntry->GetRequest();
1579 // If it already exists, and we're putting the same key into the cache, we
1580 // should remove the old version.
1581 MOZ_LOG(gImgLog, LogLevel::Debug,
1582 ("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
1583 nullptr));
1585 RemoveFromCache(aKey);
1586 } else {
1587 MOZ_LOG(gImgLog, LogLevel::Debug,
1588 ("[this=%p] imgLoader::PutIntoCache --"
1589 " Element NOT already in the cache",
1590 nullptr));
1593 mCache.InsertOrUpdate(aKey, RefPtr{entry});
1595 // We can be called to resurrect an evicted entry.
1596 if (entry->Evicted()) {
1597 entry->SetEvicted(false);
1600 // If we're resurrecting an entry with no proxies, put it back in the
1601 // tracker and queue.
1602 if (entry->HasNoProxies()) {
1603 nsresult addrv = NS_OK;
1605 if (mCacheTracker) {
1606 addrv = mCacheTracker->AddObject(entry);
1609 if (NS_SUCCEEDED(addrv)) {
1610 mCacheQueue.Push(entry);
1614 RefPtr<imgRequest> request = entry->GetRequest();
1615 request->SetIsInCache(true);
1616 RemoveFromUncachedImages(request);
1618 return true;
1621 bool imgLoader::SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry) {
1622 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasNoProxies", "uri",
1623 aRequest->CacheKey().URI());
1625 aEntry->SetHasNoProxies(true);
1627 if (aEntry->Evicted()) {
1628 return false;
1631 nsresult addrv = NS_OK;
1633 if (mCacheTracker) {
1634 addrv = mCacheTracker->AddObject(aEntry);
1637 if (NS_SUCCEEDED(addrv)) {
1638 mCacheQueue.Push(aEntry);
1641 return true;
1644 bool imgLoader::SetHasProxies(imgRequest* aRequest) {
1645 VerifyCacheSizes();
1647 const ImageCacheKey& key = aRequest->CacheKey();
1649 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasProxies", "uri",
1650 key.URI());
1652 RefPtr<imgCacheEntry> entry;
1653 if (mCache.Get(key, getter_AddRefs(entry)) && entry) {
1654 // Make sure the cache entry is for the right request
1655 RefPtr<imgRequest> entryRequest = entry->GetRequest();
1656 if (entryRequest == aRequest && entry->HasNoProxies()) {
1657 mCacheQueue.Remove(entry);
1659 if (mCacheTracker) {
1660 mCacheTracker->RemoveObject(entry);
1663 entry->SetHasNoProxies(false);
1665 return true;
1669 return false;
1672 void imgLoader::CacheEntriesChanged(int32_t aSizeDiff /* = 0 */) {
1673 // We only need to dirty the queue if there is any sorting
1674 // taking place. Empty or single-entry lists can't become
1675 // dirty.
1676 if (mCacheQueue.GetNumElements() > 1) {
1677 mCacheQueue.MarkDirty();
1679 mCacheQueue.UpdateSize(aSizeDiff);
1682 void imgLoader::CheckCacheLimits() {
1683 if (mCacheQueue.GetNumElements() == 0) {
1684 NS_ASSERTION(mCacheQueue.GetSize() == 0,
1685 "imgLoader::CheckCacheLimits -- incorrect cache size");
1688 // Remove entries from the cache until we're back at our desired max size.
1689 while (mCacheQueue.GetSize() > sCacheMaxSize) {
1690 // Remove the first entry in the queue.
1691 RefPtr<imgCacheEntry> entry(mCacheQueue.Pop());
1693 NS_ASSERTION(entry, "imgLoader::CheckCacheLimits -- NULL entry pointer");
1695 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1696 RefPtr<imgRequest> req = entry->GetRequest();
1697 if (req) {
1698 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::CheckCacheLimits",
1699 "entry", req->CacheKey().URI());
1703 if (entry) {
1704 // We just popped this entry from the queue, so pass AlreadyRemoved
1705 // to avoid searching the queue again in RemoveFromCache.
1706 RemoveFromCache(entry, QueueState::AlreadyRemoved);
1711 bool imgLoader::ValidateRequestWithNewChannel(
1712 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1713 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1714 imgINotificationObserver* aObserver, Document* aLoadingDocument,
1715 uint64_t aInnerWindowId, nsLoadFlags aLoadFlags,
1716 nsContentPolicyType aLoadPolicyType, imgRequestProxy** aProxyRequest,
1717 nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode, bool aLinkPreload,
1718 uint64_t aEarlyHintPreloaderId, bool* aNewChannelCreated) {
1719 // now we need to insert a new channel request object in between the real
1720 // request and the proxy that basically delays loading the image until it
1721 // gets a 304 or figures out that this needs to be a new request
1723 nsresult rv;
1725 // If we're currently in the middle of validating this request, just hand
1726 // back a proxy to it; the required work will be done for us.
1727 if (imgCacheValidator* validator = request->GetValidator()) {
1728 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
1729 aObserver, aLoadFlags, aProxyRequest);
1730 if (NS_FAILED(rv)) {
1731 return false;
1734 if (*aProxyRequest) {
1735 imgRequestProxy* proxy = static_cast<imgRequestProxy*>(*aProxyRequest);
1737 // We will send notifications from imgCacheValidator::OnStartRequest().
1738 // In the mean time, we must defer notifications because we are added to
1739 // the imgRequest's proxy list, and we can get extra notifications
1740 // resulting from methods such as StartDecoding(). See bug 579122.
1741 proxy->MarkValidating();
1743 if (aLinkPreload) {
1744 MOZ_ASSERT(aLoadingDocument);
1745 auto preloadKey = PreloadHashKey::CreateAsImage(
1746 aURI, aTriggeringPrincipal, aCORSMode);
1747 proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
1750 // Attach the proxy without notifying
1751 validator->AddProxy(proxy);
1754 return true;
1756 // We will rely on Necko to cache this request when it's possible, and to
1757 // tell imgCacheValidator::OnStartRequest whether the request came from its
1758 // cache.
1759 nsCOMPtr<nsIChannel> newChannel;
1760 bool forcePrincipalCheck;
1761 rv =
1762 NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
1763 aInitialDocumentURI, aCORSMode, aReferrerInfo, aLoadGroup,
1764 aLoadFlags, aLoadPolicyType, aTriggeringPrincipal,
1765 aLoadingDocument, mRespectPrivacy, aEarlyHintPreloaderId);
1766 if (NS_FAILED(rv)) {
1767 return false;
1770 if (aNewChannelCreated) {
1771 *aNewChannelCreated = true;
1774 RefPtr<imgRequestProxy> req;
1775 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
1776 aObserver, aLoadFlags, getter_AddRefs(req));
1777 if (NS_FAILED(rv)) {
1778 return false;
1781 // Make sure that OnStatus/OnProgress calls have the right request set...
1782 RefPtr<nsProgressNotificationProxy> progressproxy =
1783 new nsProgressNotificationProxy(newChannel, req);
1784 if (!progressproxy) {
1785 return false;
1788 RefPtr<imgCacheValidator> hvc =
1789 new imgCacheValidator(progressproxy, this, request, aLoadingDocument,
1790 aInnerWindowId, forcePrincipalCheck);
1792 // Casting needed here to get past multiple inheritance.
1793 nsCOMPtr<nsIStreamListener> listener =
1794 static_cast<nsIThreadRetargetableStreamListener*>(hvc);
1795 NS_ENSURE_TRUE(listener, false);
1797 // We must set the notification callbacks before setting up the
1798 // CORS listener, because that's also interested inthe
1799 // notification callbacks.
1800 newChannel->SetNotificationCallbacks(hvc);
1802 request->SetValidator(hvc);
1804 // We will send notifications from imgCacheValidator::OnStartRequest().
1805 // In the mean time, we must defer notifications because we are added to
1806 // the imgRequest's proxy list, and we can get extra notifications
1807 // resulting from methods such as StartDecoding(). See bug 579122.
1808 req->MarkValidating();
1810 if (aLinkPreload) {
1811 MOZ_ASSERT(aLoadingDocument);
1812 auto preloadKey =
1813 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, aCORSMode);
1814 req->NotifyOpen(preloadKey, aLoadingDocument, true);
1817 // Add the proxy without notifying
1818 hvc->AddProxy(req);
1820 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
1821 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
1822 aLoadGroup);
1823 rv = newChannel->AsyncOpen(listener);
1824 if (NS_WARN_IF(NS_FAILED(rv))) {
1825 req->CancelAndForgetObserver(rv);
1826 // This will notify any current or future <link preload> tags. Pass the
1827 // non-open channel so that we can read loadinfo and referrer info of that
1828 // channel.
1829 req->NotifyStart(newChannel);
1830 // Use the non-channel overload of this method to force the notification to
1831 // happen. The preload request has not been assigned a channel.
1832 req->NotifyStop(rv);
1833 return false;
1836 req.forget(aProxyRequest);
1837 return true;
1840 void imgLoader::NotifyObserversForCachedImage(
1841 imgCacheEntry* aEntry, imgRequest* request, nsIURI* aURI,
1842 nsIReferrerInfo* aReferrerInfo, Document* aLoadingDocument,
1843 nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode,
1844 uint64_t aEarlyHintPreloaderId) {
1845 if (aEntry->HasNotified()) {
1846 return;
1849 nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
1851 if (!obsService->HasObservers("http-on-image-cache-response")) {
1852 return;
1855 aEntry->SetHasNotified();
1857 nsCOMPtr<nsIChannel> newChannel;
1858 bool forcePrincipalCheck;
1859 nsresult rv = NewImageChannel(
1860 getter_AddRefs(newChannel), &forcePrincipalCheck, aURI, nullptr,
1861 aCORSMode, aReferrerInfo, nullptr, 0,
1862 nsIContentPolicy::TYPE_INTERNAL_IMAGE, aTriggeringPrincipal,
1863 aLoadingDocument, mRespectPrivacy, aEarlyHintPreloaderId);
1864 if (NS_FAILED(rv)) {
1865 return;
1868 RefPtr<HttpBaseChannel> httpBaseChannel = do_QueryObject(newChannel);
1869 if (httpBaseChannel) {
1870 httpBaseChannel->SetDummyChannelForImageCache();
1871 newChannel->SetContentType(nsDependentCString(request->GetMimeType()));
1872 RefPtr<mozilla::image::Image> image = request->GetImage();
1873 if (image) {
1874 newChannel->SetContentLength(aEntry->GetDataSize());
1876 obsService->NotifyObservers(newChannel, "http-on-image-cache-response",
1877 nullptr);
1881 bool imgLoader::ValidateEntry(
1882 imgCacheEntry* aEntry, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1883 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1884 imgINotificationObserver* aObserver, Document* aLoadingDocument,
1885 nsLoadFlags aLoadFlags, nsContentPolicyType aLoadPolicyType,
1886 bool aCanMakeNewChannel, bool* aNewChannelCreated,
1887 imgRequestProxy** aProxyRequest, nsIPrincipal* aTriggeringPrincipal,
1888 CORSMode aCORSMode, bool aLinkPreload, uint64_t aEarlyHintPreloaderId) {
1889 LOG_SCOPE(gImgLog, "imgLoader::ValidateEntry");
1891 // If the expiration time is zero, then the request has not gotten far enough
1892 // to know when it will expire, or we know it will never expire (see
1893 // nsContentUtils::GetSubresourceCacheValidationInfo).
1894 uint32_t expiryTime = aEntry->GetExpiryTime();
1895 bool hasExpired = expiryTime && expiryTime <= SecondsFromPRTime(PR_Now());
1897 // Special treatment for file URLs - aEntry has expired if file has changed
1898 if (nsCOMPtr<nsIFileURL> fileUrl = do_QueryInterface(aURI)) {
1899 uint32_t lastModTime = aEntry->GetLoadTime();
1900 nsCOMPtr<nsIFile> theFile;
1901 if (NS_SUCCEEDED(fileUrl->GetFile(getter_AddRefs(theFile)))) {
1902 PRTime fileLastMod;
1903 if (NS_SUCCEEDED(theFile->GetLastModifiedTime(&fileLastMod))) {
1904 // nsIFile uses millisec, NSPR usec.
1905 fileLastMod *= 1000;
1906 hasExpired = SecondsFromPRTime((PRTime)fileLastMod) > lastModTime;
1911 RefPtr<imgRequest> request(aEntry->GetRequest());
1913 if (!request) {
1914 return false;
1917 if (!ValidateSecurityInfo(request, aEntry->ForcePrincipalCheck(), aCORSMode,
1918 aTriggeringPrincipal, aLoadingDocument,
1919 aLoadPolicyType)) {
1920 return false;
1923 // data URIs are immutable and by their nature can't leak data, so we can
1924 // just return true in that case. Doing so would mean that shift-reload
1925 // doesn't reload data URI documents/images though (which is handy for
1926 // debugging during gecko development) so we make an exception in that case.
1927 if (aURI->SchemeIs("data") && !(aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE)) {
1928 return true;
1931 bool validateRequest = false;
1933 if (!request->CanReuseWithoutValidation(aLoadingDocument)) {
1934 // If we would need to revalidate this entry, but we're being told to
1935 // bypass the cache, we don't allow this entry to be used.
1936 if (aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE) {
1937 return false;
1940 if (MOZ_UNLIKELY(ChaosMode::isActive(ChaosFeature::ImageCache))) {
1941 if (ChaosMode::randomUint32LessThan(4) < 1) {
1942 return false;
1946 // Determine whether the cache aEntry must be revalidated...
1947 validateRequest = ShouldRevalidateEntry(aEntry, aLoadFlags, hasExpired);
1949 MOZ_LOG(gImgLog, LogLevel::Debug,
1950 ("imgLoader::ValidateEntry validating cache entry. "
1951 "validateRequest = %d",
1952 validateRequest));
1953 } else if (!aLoadingDocument && MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1954 MOZ_LOG(gImgLog, LogLevel::Debug,
1955 ("imgLoader::ValidateEntry BYPASSING cache validation for %s "
1956 "because of NULL loading document",
1957 aURI->GetSpecOrDefault().get()));
1960 // If the original request is still transferring don't kick off a validation
1961 // network request because it is a bit silly to issue a validation request if
1962 // the original request hasn't even finished yet. So just return true
1963 // indicating the caller can create a new proxy for the request and use it as
1964 // is.
1965 // This is an optimization but it's also required for correctness. If we don't
1966 // do this then when firing the load complete notification for the original
1967 // request that can unblock load for the document and then spin the event loop
1968 // (see the stack in bug 1641682) which then the OnStartRequest for the
1969 // validation request can fire which can call UpdateProxies and can sync
1970 // notify on the progress tracker about all existing state, which includes
1971 // load complete, so we fire a second load complete notification for the
1972 // image.
1973 // In addition, we want to validate if the original request encountered
1974 // an error for two reasons. The first being if the error was a network error
1975 // then trying to re-fetch the image might succeed. The second is more
1976 // complicated. We decide if we should fire the load or error event for img
1977 // elements depending on if the image has error in its status at the time when
1978 // the load complete notification is received, and we set error status on an
1979 // image if it encounters a network error or a decode error with no real way
1980 // to tell them apart. So if we load an image that will produce a decode error
1981 // the first time we will usually fire the load event, and then decode enough
1982 // to encounter the decode error and set the error status on the image. The
1983 // next time we reference the image in the same document the load complete
1984 // notification is replayed and this time the error status from the decode is
1985 // already present so we fire the error event instead of the load event. This
1986 // is a bug (bug 1645576) that we should fix. In order to avoid that bug in
1987 // some cases (specifically the cases when we hit this code and try to
1988 // validate the request) we make sure to validate. This avoids the bug because
1989 // when the load complete notification arrives the proxy is marked as
1990 // validating so it lies about its status and returns nothing.
1991 const bool requestComplete = [&] {
1992 RefPtr<ProgressTracker> tracker;
1993 RefPtr<mozilla::image::Image> image = request->GetImage();
1994 if (image) {
1995 tracker = image->GetProgressTracker();
1996 } else {
1997 tracker = request->GetProgressTracker();
1999 return tracker &&
2000 tracker->GetProgress() & (FLAG_LOAD_COMPLETE | FLAG_HAS_ERROR);
2001 }();
2003 if (!requestComplete) {
2004 return true;
2007 if (validateRequest && aCanMakeNewChannel) {
2008 LOG_SCOPE(gImgLog, "imgLoader::ValidateRequest |cache hit| must validate");
2010 uint64_t innerWindowID =
2011 aLoadingDocument ? aLoadingDocument->InnerWindowID() : 0;
2012 return ValidateRequestWithNewChannel(
2013 request, aURI, aInitialDocumentURI, aReferrerInfo, aLoadGroup,
2014 aObserver, aLoadingDocument, innerWindowID, aLoadFlags, aLoadPolicyType,
2015 aProxyRequest, aTriggeringPrincipal, aCORSMode, aLinkPreload,
2016 aEarlyHintPreloaderId, aNewChannelCreated);
2019 if (!validateRequest) {
2020 NotifyObserversForCachedImage(aEntry, request, aURI, aReferrerInfo,
2021 aLoadingDocument, aTriggeringPrincipal,
2022 aCORSMode, aEarlyHintPreloaderId);
2025 return !validateRequest;
2028 bool imgLoader::RemoveFromCache(const ImageCacheKey& aKey) {
2029 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache", "uri",
2030 aKey.URI());
2031 RefPtr<imgCacheEntry> entry;
2032 mCache.Remove(aKey, getter_AddRefs(entry));
2033 if (entry) {
2034 MOZ_ASSERT(!entry->Evicted(), "Evicting an already-evicted cache entry!");
2036 // Entries with no proxies are in the tracker.
2037 if (entry->HasNoProxies()) {
2038 if (mCacheTracker) {
2039 mCacheTracker->RemoveObject(entry);
2041 mCacheQueue.Remove(entry);
2044 entry->SetEvicted(true);
2046 RefPtr<imgRequest> request = entry->GetRequest();
2047 request->SetIsInCache(false);
2048 AddToUncachedImages(request);
2050 return true;
2052 return false;
2055 bool imgLoader::RemoveFromCache(imgCacheEntry* entry, QueueState aQueueState) {
2056 LOG_STATIC_FUNC(gImgLog, "imgLoader::RemoveFromCache entry");
2058 RefPtr<imgRequest> request = entry->GetRequest();
2059 if (request) {
2060 const ImageCacheKey& key = request->CacheKey();
2061 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache",
2062 "entry's uri", key.URI());
2064 mCache.Remove(key);
2066 if (entry->HasNoProxies()) {
2067 LOG_STATIC_FUNC(gImgLog,
2068 "imgLoader::RemoveFromCache removing from tracker");
2069 if (mCacheTracker) {
2070 mCacheTracker->RemoveObject(entry);
2072 // Only search the queue to remove the entry if its possible it might
2073 // be in the queue. If we know its not in the queue this would be
2074 // wasted work.
2075 MOZ_ASSERT_IF(aQueueState == QueueState::AlreadyRemoved,
2076 !mCacheQueue.Contains(entry));
2077 if (aQueueState == QueueState::MaybeExists) {
2078 mCacheQueue.Remove(entry);
2082 entry->SetEvicted(true);
2083 request->SetIsInCache(false);
2084 AddToUncachedImages(request);
2086 return true;
2089 return false;
2092 nsresult imgLoader::ClearImageCache(ClearOptions aOptions) {
2093 const bool chromeOnly = aOptions.contains(ClearOption::ChromeOnly);
2094 const auto ShouldRemove = [&](imgCacheEntry* aEntry) {
2095 if (chromeOnly) {
2096 // TODO: Consider also removing "resource://" etc?
2097 RefPtr<imgRequest> request = aEntry->GetRequest();
2098 if (!request || !request->CacheKey().URI()->SchemeIs("chrome")) {
2099 return false;
2102 return true;
2104 if (aOptions.contains(ClearOption::UnusedOnly)) {
2105 LOG_STATIC_FUNC(gImgLog, "imgLoader::ClearImageCache queue");
2106 // We have to make a temporary, since RemoveFromCache removes the element
2107 // from the queue, invalidating iterators.
2108 nsTArray<RefPtr<imgCacheEntry>> entries(mCacheQueue.GetNumElements());
2109 for (auto& entry : mCacheQueue) {
2110 if (ShouldRemove(entry)) {
2111 entries.AppendElement(entry);
2115 // Iterate in reverse order to minimize array copying.
2116 for (auto& entry : entries) {
2117 if (!RemoveFromCache(entry)) {
2118 return NS_ERROR_FAILURE;
2122 MOZ_ASSERT(chromeOnly || mCacheQueue.GetNumElements() == 0);
2123 return NS_OK;
2126 LOG_STATIC_FUNC(gImgLog, "imgLoader::ClearImageCache table");
2127 // We have to make a temporary, since RemoveFromCache removes the element
2128 // from the queue, invalidating iterators.
2129 const auto entries =
2130 ToTArray<nsTArray<RefPtr<imgCacheEntry>>>(mCache.Values());
2131 for (const auto& entry : entries) {
2132 if (!ShouldRemove(entry)) {
2133 continue;
2135 if (!RemoveFromCache(entry)) {
2136 return NS_ERROR_FAILURE;
2139 MOZ_ASSERT(chromeOnly || mCache.IsEmpty());
2140 return NS_OK;
2143 void imgLoader::AddToUncachedImages(imgRequest* aRequest) {
2144 MutexAutoLock lock(mUncachedImagesMutex);
2145 mUncachedImages.Insert(aRequest);
2148 void imgLoader::RemoveFromUncachedImages(imgRequest* aRequest) {
2149 MutexAutoLock lock(mUncachedImagesMutex);
2150 mUncachedImages.Remove(aRequest);
2153 #define LOAD_FLAGS_CACHE_MASK \
2154 (nsIRequest::LOAD_BYPASS_CACHE | nsIRequest::LOAD_FROM_CACHE)
2156 #define LOAD_FLAGS_VALIDATE_MASK \
2157 (nsIRequest::VALIDATE_ALWAYS | nsIRequest::VALIDATE_NEVER | \
2158 nsIRequest::VALIDATE_ONCE_PER_SESSION)
2160 NS_IMETHODIMP
2161 imgLoader::LoadImageXPCOM(
2162 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2163 nsIPrincipal* aTriggeringPrincipal, nsILoadGroup* aLoadGroup,
2164 imgINotificationObserver* aObserver, Document* aLoadingDocument,
2165 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
2166 nsContentPolicyType aContentPolicyType, imgIRequest** _retval) {
2167 // Optional parameter, so defaults to 0 (== TYPE_INVALID)
2168 if (!aContentPolicyType) {
2169 aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE;
2171 imgRequestProxy* proxy;
2172 nsresult rv =
2173 LoadImage(aURI, aInitialDocumentURI, aReferrerInfo, aTriggeringPrincipal,
2174 0, aLoadGroup, aObserver, aLoadingDocument, aLoadingDocument,
2175 aLoadFlags, aCacheKey, aContentPolicyType, u""_ns,
2176 /* aUseUrgentStartForChannel */ false, /* aListPreload */ false,
2177 0, &proxy);
2178 *_retval = proxy;
2179 return rv;
2182 static void MakeRequestStaticIfNeeded(
2183 Document* aLoadingDocument, imgRequestProxy** aProxyAboutToGetReturned) {
2184 if (!aLoadingDocument || !aLoadingDocument->IsStaticDocument()) {
2185 return;
2188 if (!*aProxyAboutToGetReturned) {
2189 return;
2192 RefPtr<imgRequestProxy> proxy = dont_AddRef(*aProxyAboutToGetReturned);
2193 *aProxyAboutToGetReturned = nullptr;
2195 RefPtr<imgRequestProxy> staticProxy =
2196 proxy->GetStaticRequest(aLoadingDocument);
2197 if (staticProxy != proxy) {
2198 proxy->CancelAndForgetObserver(NS_BINDING_ABORTED);
2199 proxy = std::move(staticProxy);
2201 proxy.forget(aProxyAboutToGetReturned);
2204 bool imgLoader::IsImageAvailable(nsIURI* aURI,
2205 nsIPrincipal* aTriggeringPrincipal,
2206 CORSMode aCORSMode, Document* aDocument) {
2207 ImageCacheKey key(aURI, aCORSMode,
2208 aTriggeringPrincipal->OriginAttributesRef(), aDocument);
2209 RefPtr<imgCacheEntry> entry;
2210 if (!mCache.Get(key, getter_AddRefs(entry)) || !entry) {
2211 return false;
2213 RefPtr<imgRequest> request = entry->GetRequest();
2214 if (!request) {
2215 return false;
2217 if (nsCOMPtr<nsILoadGroup> docLoadGroup = aDocument->GetDocumentLoadGroup()) {
2218 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2219 docLoadGroup->GetLoadFlags(&requestFlags);
2220 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2221 // If we're bypassing the cache, treat the image as not available.
2222 return false;
2225 return ValidateCORSMode(request, false, aCORSMode, aTriggeringPrincipal);
2228 nsresult imgLoader::LoadImage(
2229 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2230 nsIPrincipal* aTriggeringPrincipal, uint64_t aRequestContextID,
2231 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
2232 nsINode* aContext, Document* aLoadingDocument, nsLoadFlags aLoadFlags,
2233 nsISupports* aCacheKey, nsContentPolicyType aContentPolicyType,
2234 const nsAString& initiatorType, bool aUseUrgentStartForChannel,
2235 bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
2236 imgRequestProxy** _retval) {
2237 VerifyCacheSizes();
2239 NS_ASSERTION(aURI, "imgLoader::LoadImage -- NULL URI pointer");
2241 if (!aURI) {
2242 return NS_ERROR_NULL_POINTER;
2245 auto makeStaticIfNeeded = mozilla::MakeScopeExit(
2246 [&] { MakeRequestStaticIfNeeded(aLoadingDocument, _retval); });
2248 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK,
2249 aURI->GetSpecOrDefault());
2251 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::LoadImage", "aURI", aURI);
2253 *_retval = nullptr;
2255 RefPtr<imgRequest> request;
2257 nsresult rv;
2258 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2260 #ifdef DEBUG
2261 bool isPrivate = false;
2263 if (aLoadingDocument) {
2264 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadingDocument);
2265 } else if (aLoadGroup) {
2266 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadGroup);
2268 MOZ_ASSERT(isPrivate == mRespectPrivacy);
2270 if (aLoadingDocument) {
2271 // The given load group should match that of the document if given. If
2272 // that isn't the case, then we need to add more plumbing to ensure we
2273 // block the document as well.
2274 nsCOMPtr<nsILoadGroup> docLoadGroup =
2275 aLoadingDocument->GetDocumentLoadGroup();
2276 MOZ_ASSERT(docLoadGroup == aLoadGroup);
2278 #endif
2280 // Get the default load flags from the loadgroup (if possible)...
2281 if (aLoadGroup) {
2282 aLoadGroup->GetLoadFlags(&requestFlags);
2285 // Merge the default load flags with those passed in via aLoadFlags.
2286 // Currently, *only* the caching, validation and background load flags
2287 // are merged...
2289 // The flags in aLoadFlags take precedence over the default flags!
2291 if (aLoadFlags & LOAD_FLAGS_CACHE_MASK) {
2292 // Override the default caching flags...
2293 requestFlags = (requestFlags & ~LOAD_FLAGS_CACHE_MASK) |
2294 (aLoadFlags & LOAD_FLAGS_CACHE_MASK);
2296 if (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK) {
2297 // Override the default validation flags...
2298 requestFlags = (requestFlags & ~LOAD_FLAGS_VALIDATE_MASK) |
2299 (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK);
2301 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
2302 // Propagate background loading...
2303 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2306 if (aLinkPreload) {
2307 // Set background loading if it is <link rel=preload>
2308 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2311 CORSMode corsmode = CORS_NONE;
2312 if (aLoadFlags & imgILoader::LOAD_CORS_ANONYMOUS) {
2313 corsmode = CORS_ANONYMOUS;
2314 } else if (aLoadFlags & imgILoader::LOAD_CORS_USE_CREDENTIALS) {
2315 corsmode = CORS_USE_CREDENTIALS;
2318 // Look in the preloaded images of loading document first.
2319 if (!aLinkPreload && aLoadingDocument) {
2320 // All Early Hints preloads are Link preloads, therefore we don't have a
2321 // Early Hints preload here
2322 MOZ_ASSERT(!aEarlyHintPreloaderId);
2323 auto key =
2324 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, corsmode);
2325 if (RefPtr<PreloaderBase> preload =
2326 aLoadingDocument->Preloads().LookupPreload(key)) {
2327 RefPtr<imgRequestProxy> proxy = do_QueryObject(preload);
2328 MOZ_ASSERT(proxy);
2330 MOZ_LOG(gImgLog, LogLevel::Debug,
2331 ("[this=%p] imgLoader::LoadImage -- preloaded [proxy=%p]"
2332 " [document=%p]\n",
2333 this, proxy.get(), aLoadingDocument));
2335 // Removing the preload for this image to be in parity with Chromium. Any
2336 // following regular image request will be reloaded using the regular
2337 // path: image cache, http cache, network. Any following `<link
2338 // rel=preload as=image>` will start a new image preload that can be
2339 // satisfied from http cache or network.
2341 // There is a spec discussion for "preload cache", see
2342 // https://github.com/w3c/preload/issues/97. And it is also not clear how
2343 // preload image interacts with list of available images, see
2344 // https://github.com/whatwg/html/issues/4474.
2345 proxy->RemoveSelf(aLoadingDocument);
2346 proxy->NotifyUsage(aLoadingDocument);
2348 imgRequest* request = proxy->GetOwner();
2349 nsresult rv =
2350 CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
2351 aObserver, requestFlags, _retval);
2352 NS_ENSURE_SUCCESS(rv, rv);
2354 imgRequestProxy* newProxy = *_retval;
2355 if (imgCacheValidator* validator = request->GetValidator()) {
2356 newProxy->MarkValidating();
2357 // Attach the proxy without notifying and this will add us to the load
2358 // group.
2359 validator->AddProxy(newProxy);
2360 } else {
2361 // It's OK to add here even if the request is done. If it is, it'll send
2362 // a OnStopRequest()and the proxy will be removed from the loadgroup in
2363 // imgRequestProxy::OnLoadComplete.
2364 newProxy->AddToLoadGroup();
2365 newProxy->NotifyListener();
2368 return NS_OK;
2372 RefPtr<imgCacheEntry> entry;
2374 // Look in the cache for our URI, and then validate it.
2375 // XXX For now ignore aCacheKey. We will need it in the future
2376 // for correctly dealing with image load requests that are a result
2377 // of post data.
2378 OriginAttributes attrs;
2379 if (aTriggeringPrincipal) {
2380 attrs = aTriggeringPrincipal->OriginAttributesRef();
2382 ImageCacheKey key(aURI, corsmode, attrs, aLoadingDocument);
2383 if (mCache.Get(key, getter_AddRefs(entry)) && entry) {
2384 bool newChannelCreated = false;
2385 if (ValidateEntry(entry, aURI, aInitialDocumentURI, aReferrerInfo,
2386 aLoadGroup, aObserver, aLoadingDocument, requestFlags,
2387 aContentPolicyType, true, &newChannelCreated, _retval,
2388 aTriggeringPrincipal, corsmode, aLinkPreload,
2389 aEarlyHintPreloaderId)) {
2390 request = entry->GetRequest();
2392 // If this entry has no proxies, its request has no reference to the
2393 // entry.
2394 if (entry->HasNoProxies()) {
2395 LOG_FUNC_WITH_PARAM(gImgLog,
2396 "imgLoader::LoadImage() adding proxyless entry",
2397 "uri", key.URI());
2398 MOZ_ASSERT(!request->HasCacheEntry(),
2399 "Proxyless entry's request has cache entry!");
2400 request->SetCacheEntry(entry);
2402 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2403 mCacheTracker->MarkUsed(entry);
2407 entry->Touch();
2409 if (!newChannelCreated) {
2410 // This is ugly but it's needed to report CSP violations. We have 3
2411 // scenarios:
2412 // - we don't have cache. We are not in this if() stmt. A new channel is
2413 // created and that triggers the CSP checks.
2414 // - We have a cache entry and this is blocked by CSP directives.
2415 DebugOnly<bool> shouldLoad = ShouldLoadCachedImage(
2416 request, aLoadingDocument, aTriggeringPrincipal, aContentPolicyType,
2417 /* aSendCSPViolationReports */ true);
2418 MOZ_ASSERT(shouldLoad);
2420 } else {
2421 // We can't use this entry. We'll try to load it off the network, and if
2422 // successful, overwrite the old entry in the cache with a new one.
2423 entry = nullptr;
2427 // Keep the channel in this scope, so we can adjust its notificationCallbacks
2428 // later when we create the proxy.
2429 nsCOMPtr<nsIChannel> newChannel;
2430 // If we didn't get a cache hit, we need to load from the network.
2431 if (!request) {
2432 LOG_SCOPE(gImgLog, "imgLoader::LoadImage |cache miss|");
2434 bool forcePrincipalCheck;
2435 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
2436 aInitialDocumentURI, corsmode, aReferrerInfo,
2437 aLoadGroup, requestFlags, aContentPolicyType,
2438 aTriggeringPrincipal, aContext, mRespectPrivacy,
2439 aEarlyHintPreloaderId);
2440 if (NS_FAILED(rv)) {
2441 return NS_ERROR_FAILURE;
2444 MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel) == mRespectPrivacy);
2446 NewRequestAndEntry(forcePrincipalCheck, this, key, getter_AddRefs(request),
2447 getter_AddRefs(entry));
2449 MOZ_LOG(gImgLog, LogLevel::Debug,
2450 ("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
2451 " [request=%p]\n",
2452 this, request.get()));
2454 nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(newChannel));
2455 if (cos) {
2456 if (aUseUrgentStartForChannel && !aLinkPreload) {
2457 cos->AddClassFlags(nsIClassOfService::UrgentStart);
2460 if (StaticPrefs::network_http_tailing_enabled() &&
2461 aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
2462 cos->AddClassFlags(nsIClassOfService::Throttleable |
2463 nsIClassOfService::Tail);
2464 nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(newChannel));
2465 if (httpChannel) {
2466 Unused << httpChannel->SetRequestContextID(aRequestContextID);
2471 nsCOMPtr<nsILoadGroup> channelLoadGroup;
2472 newChannel->GetLoadGroup(getter_AddRefs(channelLoadGroup));
2473 rv = request->Init(aURI, aURI, /* aHadInsecureRedirect = */ false,
2474 channelLoadGroup, newChannel, entry, aLoadingDocument,
2475 aTriggeringPrincipal, corsmode, aReferrerInfo);
2476 if (NS_FAILED(rv)) {
2477 return NS_ERROR_FAILURE;
2480 // Add the initiator type for this image load
2481 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(newChannel);
2482 if (timedChannel) {
2483 timedChannel->SetInitiatorType(initiatorType);
2486 // create the proxy listener
2487 nsCOMPtr<nsIStreamListener> listener = new ProxyListener(request.get());
2489 MOZ_LOG(gImgLog, LogLevel::Debug,
2490 ("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
2491 this));
2493 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
2494 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
2495 aLoadGroup);
2497 nsresult openRes;
2498 openRes = newChannel->AsyncOpen(listener);
2500 if (NS_FAILED(openRes)) {
2501 MOZ_LOG(
2502 gImgLog, LogLevel::Debug,
2503 ("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
2504 "\n",
2505 this, static_cast<uint32_t>(openRes)));
2506 request->CancelAndAbort(openRes);
2507 return openRes;
2510 // Try to add the new request into the cache.
2511 PutIntoCache(key, entry);
2512 } else {
2513 LOG_MSG_WITH_PARAM(gImgLog, "imgLoader::LoadImage |cache hit|", "request",
2514 request);
2517 // If we didn't get a proxy when validating the cache entry, we need to
2518 // create one.
2519 if (!*_retval) {
2520 // ValidateEntry() has three return values: "Is valid," "might be valid --
2521 // validating over network", and "not valid." If we don't have a _retval,
2522 // we know ValidateEntry is not validating over the network, so it's safe
2523 // to SetLoadId here because we know this request is valid for this context.
2525 // Note, however, that this doesn't guarantee the behaviour we want (one
2526 // URL maps to the same image on a page) if we load the same image in a
2527 // different tab (see bug 528003), because its load id will get re-set, and
2528 // that'll cause us to validate over the network.
2529 request->SetLoadId(aLoadingDocument);
2531 LOG_MSG(gImgLog, "imgLoader::LoadImage", "creating proxy request.");
2532 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
2533 aObserver, requestFlags, _retval);
2534 if (NS_FAILED(rv)) {
2535 return rv;
2538 imgRequestProxy* proxy = *_retval;
2540 // Make sure that OnStatus/OnProgress calls have the right request set, if
2541 // we did create a channel here.
2542 if (newChannel) {
2543 nsCOMPtr<nsIInterfaceRequestor> requestor(
2544 new nsProgressNotificationProxy(newChannel, proxy));
2545 if (!requestor) {
2546 return NS_ERROR_OUT_OF_MEMORY;
2548 newChannel->SetNotificationCallbacks(requestor);
2551 if (aLinkPreload) {
2552 MOZ_ASSERT(aLoadingDocument);
2553 auto preloadKey =
2554 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, corsmode);
2555 proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
2558 // Note that it's OK to add here even if the request is done. If it is,
2559 // it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
2560 // the proxy will be removed from the loadgroup.
2561 proxy->AddToLoadGroup();
2563 // If we're loading off the network, explicitly don't notify our proxy,
2564 // because necko (or things called from necko, such as imgCacheValidator)
2565 // are going to call our notifications asynchronously, and we can't make it
2566 // further asynchronous because observers might rely on imagelib completing
2567 // its work between the channel's OnStartRequest and OnStopRequest.
2568 if (!newChannel) {
2569 proxy->NotifyListener();
2572 return rv;
2575 NS_ASSERTION(*_retval, "imgLoader::LoadImage -- no return value");
2577 return NS_OK;
2580 NS_IMETHODIMP
2581 imgLoader::LoadImageWithChannelXPCOM(nsIChannel* channel,
2582 imgINotificationObserver* aObserver,
2583 Document* aLoadingDocument,
2584 nsIStreamListener** listener,
2585 imgIRequest** _retval) {
2586 nsresult result;
2587 imgRequestProxy* proxy;
2588 result = LoadImageWithChannel(channel, aObserver, aLoadingDocument, listener,
2589 &proxy);
2590 *_retval = proxy;
2591 return result;
2594 nsresult imgLoader::LoadImageWithChannel(nsIChannel* channel,
2595 imgINotificationObserver* aObserver,
2596 Document* aLoadingDocument,
2597 nsIStreamListener** listener,
2598 imgRequestProxy** _retval) {
2599 NS_ASSERTION(channel,
2600 "imgLoader::LoadImageWithChannel -- NULL channel pointer");
2602 MOZ_ASSERT(NS_UsePrivateBrowsing(channel) == mRespectPrivacy);
2604 auto makeStaticIfNeeded = mozilla::MakeScopeExit(
2605 [&] { MakeRequestStaticIfNeeded(aLoadingDocument, _retval); });
2607 LOG_SCOPE(gImgLog, "imgLoader::LoadImageWithChannel");
2608 RefPtr<imgRequest> request;
2610 nsCOMPtr<nsIURI> uri;
2611 channel->GetURI(getter_AddRefs(uri));
2613 NS_ENSURE_TRUE(channel, NS_ERROR_FAILURE);
2614 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2616 OriginAttributes attrs = loadInfo->GetOriginAttributes();
2618 // TODO: Get a meaningful cors mode from the caller probably?
2619 const auto corsMode = CORS_NONE;
2620 ImageCacheKey key(uri, corsMode, attrs, aLoadingDocument);
2622 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2623 channel->GetLoadFlags(&requestFlags);
2625 RefPtr<imgCacheEntry> entry;
2627 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2628 RemoveFromCache(key);
2629 } else {
2630 // Look in the cache for our URI, and then validate it.
2631 // XXX For now ignore aCacheKey. We will need it in the future
2632 // for correctly dealing with image load requests that are a result
2633 // of post data.
2634 if (mCache.Get(key, getter_AddRefs(entry)) && entry) {
2635 // We don't want to kick off another network load. So we ask
2636 // ValidateEntry to only do validation without creating a new proxy. If
2637 // it says that the entry isn't valid any more, we'll only use the entry
2638 // we're getting if the channel is loading from the cache anyways.
2640 // XXX -- should this be changed? it's pretty much verbatim from the old
2641 // code, but seems nonsensical.
2643 // Since aCanMakeNewChannel == false, we don't need to pass content policy
2644 // type/principal/etc
2646 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2647 // if there is a loadInfo, use the right contentType, otherwise
2648 // default to the internal image type
2649 nsContentPolicyType policyType = loadInfo->InternalContentPolicyType();
2651 if (ValidateEntry(entry, uri, nullptr, nullptr, nullptr, aObserver,
2652 aLoadingDocument, requestFlags, policyType, false,
2653 nullptr, nullptr, nullptr, corsMode, false, 0)) {
2654 request = entry->GetRequest();
2655 } else {
2656 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(channel));
2657 bool bUseCacheCopy;
2659 if (cacheChan) {
2660 cacheChan->IsFromCache(&bUseCacheCopy);
2661 } else {
2662 bUseCacheCopy = false;
2665 if (!bUseCacheCopy) {
2666 entry = nullptr;
2667 } else {
2668 request = entry->GetRequest();
2672 if (request && entry) {
2673 // If this entry has no proxies, its request has no reference to
2674 // the entry.
2675 if (entry->HasNoProxies()) {
2676 LOG_FUNC_WITH_PARAM(
2677 gImgLog,
2678 "imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
2679 key.URI());
2680 MOZ_ASSERT(!request->HasCacheEntry(),
2681 "Proxyless entry's request has cache entry!");
2682 request->SetCacheEntry(entry);
2684 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2685 mCacheTracker->MarkUsed(entry);
2692 nsCOMPtr<nsILoadGroup> loadGroup;
2693 channel->GetLoadGroup(getter_AddRefs(loadGroup));
2695 #ifdef DEBUG
2696 if (aLoadingDocument) {
2697 // The load group of the channel should always match that of the
2698 // document if given. If that isn't the case, then we need to add more
2699 // plumbing to ensure we block the document as well.
2700 nsCOMPtr<nsILoadGroup> docLoadGroup =
2701 aLoadingDocument->GetDocumentLoadGroup();
2702 MOZ_ASSERT(docLoadGroup == loadGroup);
2704 #endif
2706 // Filter out any load flags not from nsIRequest
2707 requestFlags &= nsIRequest::LOAD_REQUESTMASK;
2709 nsresult rv = NS_OK;
2710 if (request) {
2711 // we have this in our cache already.. cancel the current (document) load
2713 // this should fire an OnStopRequest
2714 channel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
2716 *listener = nullptr; // give them back a null nsIStreamListener
2718 rv = CreateNewProxyForRequest(request, uri, loadGroup, aLoadingDocument,
2719 aObserver, requestFlags, _retval);
2720 static_cast<imgRequestProxy*>(*_retval)->NotifyListener();
2721 } else {
2722 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2723 nsCOMPtr<nsIURI> originalURI;
2724 channel->GetOriginalURI(getter_AddRefs(originalURI));
2726 // XXX(seth): We should be able to just use |key| here, except that |key| is
2727 // constructed above with the *current URI* and not the *original URI*. I'm
2728 // pretty sure this is a bug, and it's preventing us from ever getting a
2729 // cache hit in LoadImageWithChannel when redirects are involved.
2730 ImageCacheKey originalURIKey(originalURI, corsMode, attrs,
2731 aLoadingDocument);
2733 // Default to doing a principal check because we don't know who
2734 // started that load and whether their principal ended up being
2735 // inherited on the channel.
2736 NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
2737 originalURIKey, getter_AddRefs(request),
2738 getter_AddRefs(entry));
2740 // No principal specified here, because we're not passed one.
2741 // In LoadImageWithChannel, the redirects that may have been
2742 // associated with this load would have gone through necko.
2743 // We only have the final URI in ImageLib and hence don't know
2744 // if the request went through insecure redirects. But if it did,
2745 // the necko cache should have handled that (since all necko cache hits
2746 // including the redirects will go through content policy). Hence, we
2747 // can set aHadInsecureRedirect to false here.
2748 rv = request->Init(originalURI, uri, /* aHadInsecureRedirect = */ false,
2749 channel, channel, entry, aLoadingDocument, nullptr,
2750 corsMode, nullptr);
2751 NS_ENSURE_SUCCESS(rv, rv);
2753 RefPtr<ProxyListener> pl =
2754 new ProxyListener(static_cast<nsIStreamListener*>(request.get()));
2755 pl.forget(listener);
2757 // Try to add the new request into the cache.
2758 PutIntoCache(originalURIKey, entry);
2760 rv = CreateNewProxyForRequest(request, originalURI, loadGroup,
2761 aLoadingDocument, aObserver, requestFlags,
2762 _retval);
2764 // Explicitly don't notify our proxy, because we're loading off the
2765 // network, and necko (or things called from necko, such as
2766 // imgCacheValidator) are going to call our notifications asynchronously,
2767 // and we can't make it further asynchronous because observers might rely
2768 // on imagelib completing its work between the channel's OnStartRequest and
2769 // OnStopRequest.
2772 if (NS_FAILED(rv)) {
2773 return rv;
2776 (*_retval)->AddToLoadGroup();
2777 return rv;
2780 bool imgLoader::SupportImageWithMimeType(const nsACString& aMimeType,
2781 AcceptedMimeTypes aAccept
2782 /* = AcceptedMimeTypes::IMAGES */) {
2783 nsAutoCString mimeType(aMimeType);
2784 ToLowerCase(mimeType);
2786 if (aAccept == AcceptedMimeTypes::IMAGES_AND_DOCUMENTS &&
2787 mimeType.EqualsLiteral("image/svg+xml")) {
2788 return true;
2791 DecoderType type = DecoderFactory::GetDecoderType(mimeType.get());
2792 return type != DecoderType::UNKNOWN;
2795 NS_IMETHODIMP
2796 imgLoader::GetMIMETypeFromContent(nsIRequest* aRequest,
2797 const uint8_t* aContents, uint32_t aLength,
2798 nsACString& aContentType) {
2799 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2800 if (channel) {
2801 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2802 if (loadInfo->GetSkipContentSniffing()) {
2803 return NS_ERROR_NOT_AVAILABLE;
2807 nsresult rv =
2808 GetMimeTypeFromContent((const char*)aContents, aLength, aContentType);
2809 if (NS_SUCCEEDED(rv) && channel && XRE_IsParentProcess()) {
2810 if (RefPtr<mozilla::net::nsHttpChannel> httpChannel =
2811 do_QueryObject(channel)) {
2812 // If the image type pattern matching algorithm given bytes does not
2813 // return undefined, then disable the further check and allow the
2814 // response.
2815 httpChannel->DisableIsOpaqueResponseAllowedAfterSniffCheck(
2816 mozilla::net::nsHttpChannel::SnifferType::Image);
2820 return rv;
2823 /* static */
2824 nsresult imgLoader::GetMimeTypeFromContent(const char* aContents,
2825 uint32_t aLength,
2826 nsACString& aContentType) {
2827 nsAutoCString detected;
2829 /* Is it a GIF? */
2830 if (aLength >= 6 &&
2831 (!strncmp(aContents, "GIF87a", 6) || !strncmp(aContents, "GIF89a", 6))) {
2832 aContentType.AssignLiteral(IMAGE_GIF);
2834 /* or a PNG? */
2835 } else if (aLength >= 8 && ((unsigned char)aContents[0] == 0x89 &&
2836 (unsigned char)aContents[1] == 0x50 &&
2837 (unsigned char)aContents[2] == 0x4E &&
2838 (unsigned char)aContents[3] == 0x47 &&
2839 (unsigned char)aContents[4] == 0x0D &&
2840 (unsigned char)aContents[5] == 0x0A &&
2841 (unsigned char)aContents[6] == 0x1A &&
2842 (unsigned char)aContents[7] == 0x0A)) {
2843 aContentType.AssignLiteral(IMAGE_PNG);
2845 /* maybe a JPEG (JFIF)? */
2846 /* JFIF files start with SOI APP0 but older files can start with SOI DQT
2847 * so we test for SOI followed by any marker, i.e. FF D8 FF
2848 * this will also work for SPIFF JPEG files if they appear in the future.
2850 * (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
2852 } else if (aLength >= 3 && ((unsigned char)aContents[0]) == 0xFF &&
2853 ((unsigned char)aContents[1]) == 0xD8 &&
2854 ((unsigned char)aContents[2]) == 0xFF) {
2855 aContentType.AssignLiteral(IMAGE_JPEG);
2857 /* or how about ART? */
2858 /* ART begins with JG (4A 47). Major version offset 2.
2859 * Minor version offset 3. Offset 4 must be nullptr.
2861 } else if (aLength >= 5 && ((unsigned char)aContents[0]) == 0x4a &&
2862 ((unsigned char)aContents[1]) == 0x47 &&
2863 ((unsigned char)aContents[4]) == 0x00) {
2864 aContentType.AssignLiteral(IMAGE_ART);
2866 } else if (aLength >= 2 && !strncmp(aContents, "BM", 2)) {
2867 aContentType.AssignLiteral(IMAGE_BMP);
2869 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
2870 // CURs begin with 2-byte 0 followed by 2-byte 2.
2871 } else if (aLength >= 4 && (!memcmp(aContents, "\000\000\001\000", 4) ||
2872 !memcmp(aContents, "\000\000\002\000", 4))) {
2873 aContentType.AssignLiteral(IMAGE_ICO);
2875 // WebPs always begin with RIFF, a 32-bit length, and WEBP.
2876 } else if (aLength >= 12 && !memcmp(aContents, "RIFF", 4) &&
2877 !memcmp(aContents + 8, "WEBP", 4)) {
2878 aContentType.AssignLiteral(IMAGE_WEBP);
2880 } else if (MatchesMP4(reinterpret_cast<const uint8_t*>(aContents), aLength,
2881 detected) &&
2882 detected.Equals(IMAGE_AVIF)) {
2883 aContentType.AssignLiteral(IMAGE_AVIF);
2884 } else if ((aLength >= 2 && !memcmp(aContents, "\xFF\x0A", 2)) ||
2885 (aLength >= 12 &&
2886 !memcmp(aContents, "\x00\x00\x00\x0CJXL \x0D\x0A\x87\x0A", 12))) {
2887 // Each version is for containerless and containerful files respectively.
2888 aContentType.AssignLiteral(IMAGE_JXL);
2889 } else {
2890 /* none of the above? I give up */
2891 return NS_ERROR_NOT_AVAILABLE;
2894 return NS_OK;
2898 * proxy stream listener class used to handle multipart/x-mixed-replace
2901 #include "nsIRequest.h"
2902 #include "nsIStreamConverterService.h"
2904 NS_IMPL_ISUPPORTS(ProxyListener, nsIStreamListener,
2905 nsIThreadRetargetableStreamListener, nsIRequestObserver)
2907 ProxyListener::ProxyListener(nsIStreamListener* dest) : mDestListener(dest) {}
2909 ProxyListener::~ProxyListener() = default;
2911 /** nsIRequestObserver methods **/
2913 NS_IMETHODIMP
2914 ProxyListener::OnStartRequest(nsIRequest* aRequest) {
2915 if (!mDestListener) {
2916 return NS_ERROR_FAILURE;
2919 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2920 if (channel) {
2921 // We need to set the initiator type for the image load
2922 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(channel);
2923 if (timedChannel) {
2924 nsAutoString type;
2925 timedChannel->GetInitiatorType(type);
2926 if (type.IsEmpty()) {
2927 timedChannel->SetInitiatorType(u"img"_ns);
2931 nsAutoCString contentType;
2932 nsresult rv = channel->GetContentType(contentType);
2934 if (!contentType.IsEmpty()) {
2935 /* If multipart/x-mixed-replace content, we'll insert a MIME decoder
2936 in the pipeline to handle the content and pass it along to our
2937 original listener.
2939 if ("multipart/x-mixed-replace"_ns.Equals(contentType)) {
2940 nsCOMPtr<nsIStreamConverterService> convServ(
2941 do_GetService("@mozilla.org/streamConverters;1", &rv));
2942 if (NS_SUCCEEDED(rv)) {
2943 nsCOMPtr<nsIStreamListener> toListener(mDestListener);
2944 nsCOMPtr<nsIStreamListener> fromListener;
2946 rv = convServ->AsyncConvertData("multipart/x-mixed-replace", "*/*",
2947 toListener, nullptr,
2948 getter_AddRefs(fromListener));
2949 if (NS_SUCCEEDED(rv)) {
2950 mDestListener = fromListener;
2957 return mDestListener->OnStartRequest(aRequest);
2960 NS_IMETHODIMP
2961 ProxyListener::OnStopRequest(nsIRequest* aRequest, nsresult status) {
2962 if (!mDestListener) {
2963 return NS_ERROR_FAILURE;
2966 return mDestListener->OnStopRequest(aRequest, status);
2969 /** nsIStreamListener methods **/
2971 NS_IMETHODIMP
2972 ProxyListener::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
2973 uint64_t sourceOffset, uint32_t count) {
2974 if (!mDestListener) {
2975 return NS_ERROR_FAILURE;
2978 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
2981 NS_IMETHODIMP
2982 ProxyListener::OnDataFinished(nsresult aStatus) {
2983 if (!mDestListener) {
2984 return NS_ERROR_FAILURE;
2986 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
2987 do_QueryInterface(mDestListener);
2988 if (retargetableListener) {
2989 return retargetableListener->OnDataFinished(aStatus);
2992 return NS_OK;
2995 /** nsThreadRetargetableStreamListener methods **/
2996 NS_IMETHODIMP
2997 ProxyListener::CheckListenerChain() {
2998 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
2999 nsresult rv = NS_OK;
3000 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3001 do_QueryInterface(mDestListener, &rv);
3002 if (retargetableListener) {
3003 rv = retargetableListener->CheckListenerChain();
3005 MOZ_LOG(
3006 gImgLog, LogLevel::Debug,
3007 ("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
3008 "]",
3009 (NS_SUCCEEDED(rv) ? "success" : "failure"), this,
3010 (nsIStreamListener*)mDestListener, static_cast<uint32_t>(rv)));
3011 return rv;
3015 * http validate class. check a channel for a 304
3018 NS_IMPL_ISUPPORTS(imgCacheValidator, nsIStreamListener, nsIRequestObserver,
3019 nsIThreadRetargetableStreamListener, nsIChannelEventSink,
3020 nsIInterfaceRequestor, nsIAsyncVerifyRedirectCallback)
3022 imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy* progress,
3023 imgLoader* loader, imgRequest* request,
3024 Document* aDocument,
3025 uint64_t aInnerWindowId,
3026 bool forcePrincipalCheckForCacheEntry)
3027 : mProgressProxy(progress),
3028 mRequest(request),
3029 mDocument(aDocument),
3030 mInnerWindowId(aInnerWindowId),
3031 mImgLoader(loader),
3032 mHadInsecureRedirect(false) {
3033 NewRequestAndEntry(forcePrincipalCheckForCacheEntry, loader,
3034 mRequest->CacheKey(), getter_AddRefs(mNewRequest),
3035 getter_AddRefs(mNewEntry));
3038 imgCacheValidator::~imgCacheValidator() {
3039 if (mRequest) {
3040 // If something went wrong, and we never unblocked the requests waiting on
3041 // validation, now is our last chance. We will cancel the new request and
3042 // switch the waiting proxies to it.
3043 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
3047 void imgCacheValidator::AddProxy(imgRequestProxy* aProxy) {
3048 // aProxy needs to be in the loadgroup since we're validating from
3049 // the network.
3050 aProxy->AddToLoadGroup();
3052 mProxies.AppendElement(aProxy);
3055 void imgCacheValidator::RemoveProxy(imgRequestProxy* aProxy) {
3056 mProxies.RemoveElement(aProxy);
3059 void imgCacheValidator::UpdateProxies(bool aCancelRequest, bool aSyncNotify) {
3060 MOZ_ASSERT(mRequest);
3062 // Clear the validator before updating the proxies. The notifications may
3063 // clone an existing request, and its state could be inconsistent.
3064 mRequest->SetValidator(nullptr);
3065 mRequest = nullptr;
3067 // If an error occurred, we will want to cancel the new request, and make the
3068 // validating proxies point to it. Any proxies still bound to the original
3069 // request which are not validating should remain untouched.
3070 if (aCancelRequest) {
3071 MOZ_ASSERT(mNewRequest);
3072 mNewRequest->CancelAndAbort(NS_BINDING_ABORTED);
3075 // We have finished validating the request, so we can safely take ownership
3076 // of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
3077 // if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
3078 // that any potential notifications should still be suppressed in
3079 // imgRequestProxy::ChangeOwner because we haven't cleared the validating
3080 // flag yet, and thus they will remain deferred.
3081 AutoTArray<RefPtr<imgRequestProxy>, 4> proxies(std::move(mProxies));
3083 for (auto& proxy : proxies) {
3084 // First update the state of all proxies before notifying any of them
3085 // to ensure a consistent state (e.g. in case the notification causes
3086 // other proxies to be touched indirectly.)
3087 MOZ_ASSERT(proxy->IsValidating());
3088 MOZ_ASSERT(proxy->NotificationsDeferred(),
3089 "Proxies waiting on cache validation should be "
3090 "deferring notifications!");
3091 if (mNewRequest) {
3092 proxy->ChangeOwner(mNewRequest);
3094 proxy->ClearValidating();
3097 mNewRequest = nullptr;
3098 mNewEntry = nullptr;
3100 for (auto& proxy : proxies) {
3101 if (aSyncNotify) {
3102 // Notify synchronously, because the caller knows we are already in an
3103 // asynchronously-called function (e.g. OnStartRequest).
3104 proxy->SyncNotifyListener();
3105 } else {
3106 // Notify asynchronously, because the caller does not know our current
3107 // call state (e.g. ~imgCacheValidator).
3108 proxy->NotifyListener();
3113 /** nsIRequestObserver methods **/
3115 NS_IMETHODIMP
3116 imgCacheValidator::OnStartRequest(nsIRequest* aRequest) {
3117 // We may be holding on to a document, so ensure that it's released.
3118 RefPtr<Document> document = mDocument.forget();
3120 // If for some reason we don't still have an existing request (probably
3121 // because OnStartRequest got delivered more than once), just bail.
3122 if (!mRequest) {
3123 MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
3124 aRequest->CancelWithReason(NS_BINDING_ABORTED,
3125 "OnStartRequest delivered more than once?"_ns);
3126 return NS_ERROR_FAILURE;
3129 // If this request is coming from cache and has the same URI as our
3130 // imgRequest, the request all our proxies are pointing at is valid, and all
3131 // we have to do is tell them to notify their listeners.
3132 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(aRequest));
3133 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
3134 if (cacheChan && channel) {
3135 bool isFromCache = false;
3136 cacheChan->IsFromCache(&isFromCache);
3138 nsCOMPtr<nsIURI> channelURI;
3139 channel->GetURI(getter_AddRefs(channelURI));
3141 nsCOMPtr<nsIURI> finalURI;
3142 mRequest->GetFinalURI(getter_AddRefs(finalURI));
3144 bool sameURI = false;
3145 if (channelURI && finalURI) {
3146 channelURI->Equals(finalURI, &sameURI);
3149 if (isFromCache && sameURI) {
3150 // We don't need to load this any more.
3151 aRequest->CancelWithReason(NS_BINDING_ABORTED,
3152 "imgCacheValidator::OnStartRequest"_ns);
3153 mNewRequest = nullptr;
3155 // Clear the validator before updating the proxies. The notifications may
3156 // clone an existing request, and its state could be inconsistent.
3157 mRequest->SetLoadId(document);
3158 mRequest->SetInnerWindowID(mInnerWindowId);
3159 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3160 return NS_OK;
3164 // We can't load out of cache. We have to create a whole new request for the
3165 // data that's coming in off the channel.
3166 nsCOMPtr<nsIURI> uri;
3167 mRequest->GetURI(getter_AddRefs(uri));
3169 LOG_MSG_WITH_PARAM(gImgLog,
3170 "imgCacheValidator::OnStartRequest creating new request",
3171 "uri", uri);
3173 CORSMode corsmode = mRequest->GetCORSMode();
3174 nsCOMPtr<nsIReferrerInfo> referrerInfo = mRequest->GetReferrerInfo();
3175 nsCOMPtr<nsIPrincipal> triggeringPrincipal =
3176 mRequest->GetTriggeringPrincipal();
3178 // Doom the old request's cache entry
3179 mRequest->RemoveFromCache();
3181 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
3182 nsCOMPtr<nsIURI> originalURI;
3183 channel->GetOriginalURI(getter_AddRefs(originalURI));
3184 nsresult rv = mNewRequest->Init(originalURI, uri, mHadInsecureRedirect,
3185 aRequest, channel, mNewEntry, document,
3186 triggeringPrincipal, corsmode, referrerInfo);
3187 if (NS_FAILED(rv)) {
3188 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
3189 return rv;
3192 mDestListener = new ProxyListener(mNewRequest);
3194 // Try to add the new request into the cache. Note that the entry must be in
3195 // the cache before the proxies' ownership changes, because adding a proxy
3196 // changes the caching behaviour for imgRequests.
3197 mImgLoader->PutIntoCache(mNewRequest->CacheKey(), mNewEntry);
3198 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3199 return mDestListener->OnStartRequest(aRequest);
3202 NS_IMETHODIMP
3203 imgCacheValidator::OnStopRequest(nsIRequest* aRequest, nsresult status) {
3204 // Be sure we've released the document that we may have been holding on to.
3205 mDocument = nullptr;
3207 if (!mDestListener) {
3208 return NS_OK;
3211 return mDestListener->OnStopRequest(aRequest, status);
3214 /** nsIStreamListener methods **/
3216 NS_IMETHODIMP
3217 imgCacheValidator::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
3218 uint64_t sourceOffset, uint32_t count) {
3219 if (!mDestListener) {
3220 // XXX see bug 113959
3221 uint32_t _retval;
3222 inStr->ReadSegments(NS_DiscardSegment, nullptr, count, &_retval);
3223 return NS_OK;
3226 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
3229 NS_IMETHODIMP
3230 imgCacheValidator::OnDataFinished(nsresult aStatus) {
3231 if (!mDestListener) {
3232 return NS_ERROR_FAILURE;
3234 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3235 do_QueryInterface(mDestListener);
3236 if (retargetableListener) {
3237 return retargetableListener->OnDataFinished(aStatus);
3240 return NS_OK;
3243 /** nsIThreadRetargetableStreamListener methods **/
3245 NS_IMETHODIMP
3246 imgCacheValidator::CheckListenerChain() {
3247 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3248 nsresult rv = NS_OK;
3249 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3250 do_QueryInterface(mDestListener, &rv);
3251 if (retargetableListener) {
3252 rv = retargetableListener->CheckListenerChain();
3254 MOZ_LOG(
3255 gImgLog, LogLevel::Debug,
3256 ("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32 "=%s",
3257 this, static_cast<uint32_t>(rv),
3258 NS_SUCCEEDED(rv) ? "succeeded" : "failed"));
3259 return rv;
3262 /** nsIInterfaceRequestor methods **/
3264 NS_IMETHODIMP
3265 imgCacheValidator::GetInterface(const nsIID& aIID, void** aResult) {
3266 if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
3267 return QueryInterface(aIID, aResult);
3270 return mProgressProxy->GetInterface(aIID, aResult);
3273 // These functions are materially the same as the same functions in imgRequest.
3274 // We duplicate them because we're verifying whether cache loads are necessary,
3275 // not unconditionally loading.
3277 /** nsIChannelEventSink methods **/
3278 NS_IMETHODIMP
3279 imgCacheValidator::AsyncOnChannelRedirect(
3280 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
3281 nsIAsyncVerifyRedirectCallback* callback) {
3282 // Note all cache information we get from the old channel.
3283 mNewRequest->SetCacheValidation(mNewEntry, oldChannel);
3285 // If the previous URI is a non-HTTPS URI, record that fact for later use by
3286 // security code, which needs to know whether there is an insecure load at any
3287 // point in the redirect chain.
3288 nsCOMPtr<nsIURI> oldURI;
3289 bool schemeLocal = false;
3290 if (NS_FAILED(oldChannel->GetURI(getter_AddRefs(oldURI))) ||
3291 NS_FAILED(NS_URIChainHasFlags(
3292 oldURI, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE, &schemeLocal)) ||
3293 (!oldURI->SchemeIs("https") && !oldURI->SchemeIs("chrome") &&
3294 !schemeLocal)) {
3295 mHadInsecureRedirect = true;
3298 // Prepare for callback
3299 mRedirectCallback = callback;
3300 mRedirectChannel = newChannel;
3302 return mProgressProxy->AsyncOnChannelRedirect(oldChannel, newChannel, flags,
3303 this);
3306 NS_IMETHODIMP
3307 imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult) {
3308 // If we've already been told to abort, just do so.
3309 if (NS_FAILED(aResult)) {
3310 mRedirectCallback->OnRedirectVerifyCallback(aResult);
3311 mRedirectCallback = nullptr;
3312 mRedirectChannel = nullptr;
3313 return NS_OK;
3316 // make sure we have a protocol that returns data rather than opens
3317 // an external application, e.g. mailto:
3318 nsCOMPtr<nsIURI> uri;
3319 mRedirectChannel->GetURI(getter_AddRefs(uri));
3321 nsresult result = NS_OK;
3323 if (nsContentUtils::IsExternalProtocol(uri)) {
3324 result = NS_ERROR_ABORT;
3327 mRedirectCallback->OnRedirectVerifyCallback(result);
3328 mRedirectCallback = nullptr;
3329 mRedirectChannel = nullptr;
3330 return NS_OK;