Bug 1799258 - Support outByIn.size()<2 in SampleOutByIn. r=bradwerth
[gecko.git] / image / imgLoader.cpp
blob0ff72f35b631e71a526c32219b59a97732259e0c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // Undefine windows version of LoadImage because our code uses that name.
8 #include "mozilla/ScopeExit.h"
9 #include "nsIChildChannel.h"
10 #undef LoadImage
12 #include "imgLoader.h"
14 #include <algorithm>
15 #include <utility>
17 #include "DecoderFactory.h"
18 #include "Image.h"
19 #include "ImageLogging.h"
20 #include "ReferrerInfo.h"
21 #include "imgRequestProxy.h"
22 #include "mozilla/Attributes.h"
23 #include "mozilla/BasePrincipal.h"
24 #include "mozilla/ChaosMode.h"
25 #include "mozilla/ClearOnShutdown.h"
26 #include "mozilla/LoadInfo.h"
27 #include "mozilla/NullPrincipal.h"
28 #include "mozilla/Preferences.h"
29 #include "mozilla/ProfilerLabels.h"
30 #include "mozilla/StaticPrefs_image.h"
31 #include "mozilla/StaticPrefs_network.h"
32 #include "mozilla/StoragePrincipalHelper.h"
33 #include "mozilla/dom/ContentParent.h"
34 #include "mozilla/dom/nsMixedContentBlocker.h"
35 #include "mozilla/image/ImageMemoryReporter.h"
36 #include "mozilla/layers/CompositorManagerChild.h"
37 #include "nsCOMPtr.h"
38 #include "nsCRT.h"
39 #include "nsComponentManagerUtils.h"
40 #include "nsContentPolicyUtils.h"
41 #include "nsContentSecurityManager.h"
42 #include "nsContentUtils.h"
43 #include "nsHttpChannel.h"
44 #include "nsIAsyncVerifyRedirectCallback.h"
45 #include "nsICacheInfoChannel.h"
46 #include "nsIChannelEventSink.h"
47 #include "nsIClassOfService.h"
48 #include "nsIEffectiveTLDService.h"
49 #include "nsIFile.h"
50 #include "nsIFileURL.h"
51 #include "nsIHttpChannel.h"
52 #include "nsIInterfaceRequestor.h"
53 #include "nsIInterfaceRequestorUtils.h"
54 #include "nsIMemoryReporter.h"
55 #include "nsINetworkPredictor.h"
56 #include "nsIProgressEventSink.h"
57 #include "nsIProtocolHandler.h"
58 #include "nsImageModule.h"
59 #include "nsMediaSniffer.h"
60 #include "nsMimeTypes.h"
61 #include "nsNetCID.h"
62 #include "nsNetUtil.h"
63 #include "nsProxyRelease.h"
64 #include "nsQueryObject.h"
65 #include "nsReadableUtils.h"
66 #include "nsStreamUtils.h"
67 #include "prtime.h"
69 // we want to explore making the document own the load group
70 // so we can associate the document URI with the load group.
71 // until this point, we have an evil hack:
72 #include "nsIHttpChannelInternal.h"
73 #include "nsILoadGroupChild.h"
74 #include "nsIDocShell.h"
76 using namespace mozilla;
77 using namespace mozilla::dom;
78 using namespace mozilla::image;
79 using namespace mozilla::net;
81 MOZ_DEFINE_MALLOC_SIZE_OF(ImagesMallocSizeOf)
83 class imgMemoryReporter final : public nsIMemoryReporter {
84 ~imgMemoryReporter() = default;
86 public:
87 NS_DECL_ISUPPORTS
89 NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
90 nsISupports* aData, bool aAnonymize) override {
91 MOZ_ASSERT(NS_IsMainThread());
93 layers::CompositorManagerChild* manager =
94 mozilla::layers::CompositorManagerChild::GetInstance();
95 if (!manager || !StaticPrefs::image_mem_debug_reporting()) {
96 layers::SharedSurfacesMemoryReport sharedSurfaces;
97 FinishCollectReports(aHandleReport, aData, aAnonymize, sharedSurfaces);
98 return NS_OK;
101 RefPtr<imgMemoryReporter> self(this);
102 nsCOMPtr<nsIHandleReportCallback> handleReport(aHandleReport);
103 nsCOMPtr<nsISupports> data(aData);
104 manager->SendReportSharedSurfacesMemory(
105 [=](layers::SharedSurfacesMemoryReport aReport) {
106 self->FinishCollectReports(handleReport, data, aAnonymize, aReport);
108 [=](mozilla::ipc::ResponseRejectReason&& aReason) {
109 layers::SharedSurfacesMemoryReport sharedSurfaces;
110 self->FinishCollectReports(handleReport, data, aAnonymize,
111 sharedSurfaces);
113 return NS_OK;
116 void FinishCollectReports(
117 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
118 bool aAnonymize, layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
119 nsTArray<ImageMemoryCounter> chrome;
120 nsTArray<ImageMemoryCounter> content;
121 nsTArray<ImageMemoryCounter> uncached;
123 for (uint32_t i = 0; i < mKnownLoaders.Length(); i++) {
124 for (imgCacheEntry* entry : mKnownLoaders[i]->mChromeCache.Values()) {
125 RefPtr<imgRequest> req = entry->GetRequest();
126 RecordCounterForRequest(req, &chrome, !entry->HasNoProxies());
128 for (imgCacheEntry* entry : mKnownLoaders[i]->mCache.Values()) {
129 RefPtr<imgRequest> req = entry->GetRequest();
130 RecordCounterForRequest(req, &content, !entry->HasNoProxies());
132 MutexAutoLock lock(mKnownLoaders[i]->mUncachedImagesMutex);
133 for (RefPtr<imgRequest> req : mKnownLoaders[i]->mUncachedImages) {
134 RecordCounterForRequest(req, &uncached, req->HasConsumers());
138 // Note that we only need to anonymize content image URIs.
140 ReportCounterArray(aHandleReport, aData, chrome, "images/chrome",
141 /* aAnonymize */ false, aSharedSurfaces);
143 ReportCounterArray(aHandleReport, aData, content, "images/content",
144 aAnonymize, aSharedSurfaces);
146 // Uncached images may be content or chrome, so anonymize them.
147 ReportCounterArray(aHandleReport, aData, uncached, "images/uncached",
148 aAnonymize, aSharedSurfaces);
150 // Report any shared surfaces that were not merged with the surface cache.
151 ImageMemoryReporter::ReportSharedSurfaces(aHandleReport, aData,
152 aSharedSurfaces);
154 nsCOMPtr<nsIMemoryReporterManager> imgr =
155 do_GetService("@mozilla.org/memory-reporter-manager;1");
156 if (imgr) {
157 imgr->EndReport();
161 static int64_t ImagesContentUsedUncompressedDistinguishedAmount() {
162 size_t n = 0;
163 for (uint32_t i = 0; i < imgLoader::sMemReporter->mKnownLoaders.Length();
164 i++) {
165 for (imgCacheEntry* entry :
166 imgLoader::sMemReporter->mKnownLoaders[i]->mCache.Values()) {
167 if (entry->HasNoProxies()) {
168 continue;
171 RefPtr<imgRequest> req = entry->GetRequest();
172 RefPtr<image::Image> image = req->GetImage();
173 if (!image) {
174 continue;
177 // Both this and EntryImageSizes measure
178 // images/content/raster/used/decoded memory. This function's
179 // measurement is secondary -- the result doesn't go in the "explicit"
180 // tree -- so we use moz_malloc_size_of instead of ImagesMallocSizeOf to
181 // prevent DMD from seeing it reported twice.
182 SizeOfState state(moz_malloc_size_of);
183 ImageMemoryCounter counter(req, image, state, /* aIsUsed = */ true);
185 n += counter.Values().DecodedHeap();
186 n += counter.Values().DecodedNonHeap();
187 n += counter.Values().DecodedUnknown();
190 return n;
193 void RegisterLoader(imgLoader* aLoader) {
194 mKnownLoaders.AppendElement(aLoader);
197 void UnregisterLoader(imgLoader* aLoader) {
198 mKnownLoaders.RemoveElement(aLoader);
201 private:
202 nsTArray<imgLoader*> mKnownLoaders;
204 struct MemoryTotal {
205 MemoryTotal& operator+=(const ImageMemoryCounter& aImageCounter) {
206 if (aImageCounter.Type() == imgIContainer::TYPE_RASTER) {
207 if (aImageCounter.IsUsed()) {
208 mUsedRasterCounter += aImageCounter.Values();
209 } else {
210 mUnusedRasterCounter += aImageCounter.Values();
212 } else if (aImageCounter.Type() == imgIContainer::TYPE_VECTOR) {
213 if (aImageCounter.IsUsed()) {
214 mUsedVectorCounter += aImageCounter.Values();
215 } else {
216 mUnusedVectorCounter += aImageCounter.Values();
218 } else if (aImageCounter.Type() == imgIContainer::TYPE_REQUEST) {
219 // Nothing to do, we did not get to the point of having an image.
220 } else {
221 MOZ_CRASH("Unexpected image type");
224 return *this;
227 const MemoryCounter& UsedRaster() const { return mUsedRasterCounter; }
228 const MemoryCounter& UnusedRaster() const { return mUnusedRasterCounter; }
229 const MemoryCounter& UsedVector() const { return mUsedVectorCounter; }
230 const MemoryCounter& UnusedVector() const { return mUnusedVectorCounter; }
232 private:
233 MemoryCounter mUsedRasterCounter;
234 MemoryCounter mUnusedRasterCounter;
235 MemoryCounter mUsedVectorCounter;
236 MemoryCounter mUnusedVectorCounter;
239 // Reports all images of a single kind, e.g. all used chrome images.
240 void ReportCounterArray(nsIHandleReportCallback* aHandleReport,
241 nsISupports* aData,
242 nsTArray<ImageMemoryCounter>& aCounterArray,
243 const char* aPathPrefix, bool aAnonymize,
244 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
245 MemoryTotal summaryTotal;
246 MemoryTotal nonNotableTotal;
248 // Report notable images, and compute total and non-notable aggregate sizes.
249 for (uint32_t i = 0; i < aCounterArray.Length(); i++) {
250 ImageMemoryCounter& counter = aCounterArray[i];
252 if (aAnonymize) {
253 counter.URI().Truncate();
254 counter.URI().AppendPrintf("<anonymized-%u>", i);
255 } else {
256 // The URI could be an extremely long data: URI. Truncate if needed.
257 static const size_t max = 256;
258 if (counter.URI().Length() > max) {
259 counter.URI().Truncate(max);
260 counter.URI().AppendLiteral(" (truncated)");
262 counter.URI().ReplaceChar('/', '\\');
265 summaryTotal += counter;
267 if (counter.IsNotable() || StaticPrefs::image_mem_debug_reporting()) {
268 ReportImage(aHandleReport, aData, aPathPrefix, counter,
269 aSharedSurfaces);
270 } else {
271 ImageMemoryReporter::TrimSharedSurfaces(counter, aSharedSurfaces);
272 nonNotableTotal += counter;
276 // Report non-notable images in aggregate.
277 ReportTotal(aHandleReport, aData, /* aExplicit = */ true, aPathPrefix,
278 "<non-notable images>/", nonNotableTotal);
280 // Report a summary in aggregate, outside of the explicit tree.
281 ReportTotal(aHandleReport, aData, /* aExplicit = */ false, aPathPrefix, "",
282 summaryTotal);
285 static void ReportImage(nsIHandleReportCallback* aHandleReport,
286 nsISupports* aData, const char* aPathPrefix,
287 const ImageMemoryCounter& aCounter,
288 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
289 nsAutoCString pathPrefix("explicit/"_ns);
290 pathPrefix.Append(aPathPrefix);
292 switch (aCounter.Type()) {
293 case imgIContainer::TYPE_RASTER:
294 pathPrefix.AppendLiteral("/raster/");
295 break;
296 case imgIContainer::TYPE_VECTOR:
297 pathPrefix.AppendLiteral("/vector/");
298 break;
299 case imgIContainer::TYPE_REQUEST:
300 pathPrefix.AppendLiteral("/request/");
301 break;
302 default:
303 pathPrefix.AppendLiteral("/unknown=");
304 pathPrefix.AppendInt(aCounter.Type());
305 pathPrefix.AppendLiteral("/");
306 break;
309 pathPrefix.Append(aCounter.IsUsed() ? "used/" : "unused/");
310 if (aCounter.IsValidating()) {
311 pathPrefix.AppendLiteral("validating/");
313 if (aCounter.HasError()) {
314 pathPrefix.AppendLiteral("err/");
317 pathPrefix.AppendLiteral("progress=");
318 pathPrefix.AppendInt(aCounter.Progress(), 16);
319 pathPrefix.AppendLiteral("/");
321 pathPrefix.AppendLiteral("image(");
322 pathPrefix.AppendInt(aCounter.IntrinsicSize().width);
323 pathPrefix.AppendLiteral("x");
324 pathPrefix.AppendInt(aCounter.IntrinsicSize().height);
325 pathPrefix.AppendLiteral(", ");
327 if (aCounter.URI().IsEmpty()) {
328 pathPrefix.AppendLiteral("<unknown URI>");
329 } else {
330 pathPrefix.Append(aCounter.URI());
333 pathPrefix.AppendLiteral(")/");
335 ReportSurfaces(aHandleReport, aData, pathPrefix, aCounter, aSharedSurfaces);
337 ReportSourceValue(aHandleReport, aData, pathPrefix, aCounter.Values());
340 static void ReportSurfaces(
341 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
342 const nsACString& aPathPrefix, const ImageMemoryCounter& aCounter,
343 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
344 for (const SurfaceMemoryCounter& counter : aCounter.Surfaces()) {
345 nsAutoCString surfacePathPrefix(aPathPrefix);
346 switch (counter.Type()) {
347 case SurfaceMemoryCounterType::NORMAL:
348 if (counter.IsLocked()) {
349 surfacePathPrefix.AppendLiteral("locked/");
350 } else {
351 surfacePathPrefix.AppendLiteral("unlocked/");
353 if (counter.IsFactor2()) {
354 surfacePathPrefix.AppendLiteral("factor2/");
356 if (counter.CannotSubstitute()) {
357 surfacePathPrefix.AppendLiteral("cannot_substitute/");
359 break;
360 case SurfaceMemoryCounterType::CONTAINER:
361 surfacePathPrefix.AppendLiteral("container/");
362 break;
363 default:
364 MOZ_ASSERT_UNREACHABLE("Unknown counter type");
365 break;
368 surfacePathPrefix.AppendLiteral("types=");
369 surfacePathPrefix.AppendInt(counter.Values().SurfaceTypes(), 16);
370 surfacePathPrefix.AppendLiteral("/surface(");
371 surfacePathPrefix.AppendInt(counter.Key().Size().width);
372 surfacePathPrefix.AppendLiteral("x");
373 surfacePathPrefix.AppendInt(counter.Key().Size().height);
375 if (!counter.IsFinished()) {
376 surfacePathPrefix.AppendLiteral(", incomplete");
379 if (counter.Values().ExternalHandles() > 0) {
380 surfacePathPrefix.AppendLiteral(", handles:");
381 surfacePathPrefix.AppendInt(
382 uint32_t(counter.Values().ExternalHandles()));
385 ImageMemoryReporter::AppendSharedSurfacePrefix(surfacePathPrefix, counter,
386 aSharedSurfaces);
388 PlaybackType playback = counter.Key().Playback();
389 if (playback == PlaybackType::eAnimated) {
390 if (StaticPrefs::image_mem_debug_reporting()) {
391 surfacePathPrefix.AppendPrintf(
392 " (animation %4u)", uint32_t(counter.Values().FrameIndex()));
393 } else {
394 surfacePathPrefix.AppendLiteral(" (animation)");
398 if (counter.Key().Flags() != DefaultSurfaceFlags()) {
399 surfacePathPrefix.AppendLiteral(", flags:");
400 surfacePathPrefix.AppendInt(uint32_t(counter.Key().Flags()),
401 /* aRadix = */ 16);
404 if (counter.Key().Region()) {
405 const ImageIntRegion& region = counter.Key().Region().ref();
406 const gfx::IntRect& rect = region.Rect();
407 surfacePathPrefix.AppendLiteral(", region:[ rect=(");
408 surfacePathPrefix.AppendInt(rect.x);
409 surfacePathPrefix.AppendLiteral(",");
410 surfacePathPrefix.AppendInt(rect.y);
411 surfacePathPrefix.AppendLiteral(") ");
412 surfacePathPrefix.AppendInt(rect.width);
413 surfacePathPrefix.AppendLiteral("x");
414 surfacePathPrefix.AppendInt(rect.height);
415 if (region.IsRestricted()) {
416 const gfx::IntRect& restrict = region.Restriction();
417 if (restrict == rect) {
418 surfacePathPrefix.AppendLiteral(", restrict=rect");
419 } else {
420 surfacePathPrefix.AppendLiteral(", restrict=(");
421 surfacePathPrefix.AppendInt(restrict.x);
422 surfacePathPrefix.AppendLiteral(",");
423 surfacePathPrefix.AppendInt(restrict.y);
424 surfacePathPrefix.AppendLiteral(") ");
425 surfacePathPrefix.AppendInt(restrict.width);
426 surfacePathPrefix.AppendLiteral("x");
427 surfacePathPrefix.AppendInt(restrict.height);
430 if (region.GetExtendMode() != gfx::ExtendMode::CLAMP) {
431 surfacePathPrefix.AppendLiteral(", extendMode=");
432 surfacePathPrefix.AppendInt(int32_t(region.GetExtendMode()));
434 surfacePathPrefix.AppendLiteral("]");
437 const SVGImageContext& context = counter.Key().SVGContext();
438 surfacePathPrefix.AppendLiteral(", svgContext:[ ");
439 if (context.GetViewportSize()) {
440 const CSSIntSize& size = context.GetViewportSize().ref();
441 surfacePathPrefix.AppendLiteral("viewport=(");
442 surfacePathPrefix.AppendInt(size.width);
443 surfacePathPrefix.AppendLiteral("x");
444 surfacePathPrefix.AppendInt(size.height);
445 surfacePathPrefix.AppendLiteral(") ");
447 if (context.GetPreserveAspectRatio()) {
448 nsAutoString aspect;
449 context.GetPreserveAspectRatio()->ToString(aspect);
450 surfacePathPrefix.AppendLiteral("preserveAspectRatio=(");
451 LossyAppendUTF16toASCII(aspect, surfacePathPrefix);
452 surfacePathPrefix.AppendLiteral(") ");
454 if (auto scheme = context.GetColorScheme()) {
455 surfacePathPrefix.AppendLiteral("colorScheme=");
456 surfacePathPrefix.AppendInt(int32_t(*scheme));
457 surfacePathPrefix.AppendLiteral(" ");
459 if (context.GetContextPaint()) {
460 const SVGEmbeddingContextPaint* paint = context.GetContextPaint();
461 surfacePathPrefix.AppendLiteral("contextPaint=(");
462 if (paint->GetFill()) {
463 surfacePathPrefix.AppendLiteral(" fill=");
464 surfacePathPrefix.AppendInt(paint->GetFill()->ToABGR(), 16);
466 if (paint->GetFillOpacity() != 1.0) {
467 surfacePathPrefix.AppendLiteral(" fillOpa=");
468 surfacePathPrefix.AppendFloat(paint->GetFillOpacity());
470 if (paint->GetStroke()) {
471 surfacePathPrefix.AppendLiteral(" stroke=");
472 surfacePathPrefix.AppendInt(paint->GetStroke()->ToABGR(), 16);
474 if (paint->GetStrokeOpacity() != 1.0) {
475 surfacePathPrefix.AppendLiteral(" strokeOpa=");
476 surfacePathPrefix.AppendFloat(paint->GetStrokeOpacity());
478 surfacePathPrefix.AppendLiteral(" ) ");
480 surfacePathPrefix.AppendLiteral("]");
482 surfacePathPrefix.AppendLiteral(")/");
484 ReportValues(aHandleReport, aData, surfacePathPrefix, counter.Values());
488 static void ReportTotal(nsIHandleReportCallback* aHandleReport,
489 nsISupports* aData, bool aExplicit,
490 const char* aPathPrefix, const char* aPathInfix,
491 const MemoryTotal& aTotal) {
492 nsAutoCString pathPrefix;
493 if (aExplicit) {
494 pathPrefix.AppendLiteral("explicit/");
496 pathPrefix.Append(aPathPrefix);
498 nsAutoCString rasterUsedPrefix(pathPrefix);
499 rasterUsedPrefix.AppendLiteral("/raster/used/");
500 rasterUsedPrefix.Append(aPathInfix);
501 ReportValues(aHandleReport, aData, rasterUsedPrefix, aTotal.UsedRaster());
503 nsAutoCString rasterUnusedPrefix(pathPrefix);
504 rasterUnusedPrefix.AppendLiteral("/raster/unused/");
505 rasterUnusedPrefix.Append(aPathInfix);
506 ReportValues(aHandleReport, aData, rasterUnusedPrefix,
507 aTotal.UnusedRaster());
509 nsAutoCString vectorUsedPrefix(pathPrefix);
510 vectorUsedPrefix.AppendLiteral("/vector/used/");
511 vectorUsedPrefix.Append(aPathInfix);
512 ReportValues(aHandleReport, aData, vectorUsedPrefix, aTotal.UsedVector());
514 nsAutoCString vectorUnusedPrefix(pathPrefix);
515 vectorUnusedPrefix.AppendLiteral("/vector/unused/");
516 vectorUnusedPrefix.Append(aPathInfix);
517 ReportValues(aHandleReport, aData, vectorUnusedPrefix,
518 aTotal.UnusedVector());
521 static void ReportValues(nsIHandleReportCallback* aHandleReport,
522 nsISupports* aData, const nsACString& aPathPrefix,
523 const MemoryCounter& aCounter) {
524 ReportSourceValue(aHandleReport, aData, aPathPrefix, aCounter);
526 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "decoded-heap",
527 "Decoded image data which is stored on the heap.",
528 aCounter.DecodedHeap());
530 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
531 "decoded-nonheap",
532 "Decoded image data which isn't stored on the heap.",
533 aCounter.DecodedNonHeap());
535 // We don't know for certain whether or not it is on the heap, so let's
536 // just report it as non-heap for reporting purposes.
537 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
538 "decoded-unknown",
539 "Decoded image data which is unknown to be on the heap or not.",
540 aCounter.DecodedUnknown());
543 static void ReportSourceValue(nsIHandleReportCallback* aHandleReport,
544 nsISupports* aData,
545 const nsACString& aPathPrefix,
546 const MemoryCounter& aCounter) {
547 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "source",
548 "Raster image source data and vector image documents.",
549 aCounter.Source());
552 static void ReportValue(nsIHandleReportCallback* aHandleReport,
553 nsISupports* aData, int32_t aKind,
554 const nsACString& aPathPrefix,
555 const char* aPathSuffix, const char* aDescription,
556 size_t aValue) {
557 if (aValue == 0) {
558 return;
561 nsAutoCString desc(aDescription);
562 nsAutoCString path(aPathPrefix);
563 path.Append(aPathSuffix);
565 aHandleReport->Callback(""_ns, path, aKind, UNITS_BYTES, aValue, desc,
566 aData);
569 static void RecordCounterForRequest(imgRequest* aRequest,
570 nsTArray<ImageMemoryCounter>* aArray,
571 bool aIsUsed) {
572 SizeOfState state(ImagesMallocSizeOf);
573 RefPtr<image::Image> image = aRequest->GetImage();
574 if (image) {
575 ImageMemoryCounter counter(aRequest, image, state, aIsUsed);
576 aArray->AppendElement(std::move(counter));
577 } else {
578 // We can at least record some information about the image from the
579 // request, and mark it as not knowing the image type yet.
580 ImageMemoryCounter counter(aRequest, state, aIsUsed);
581 aArray->AppendElement(std::move(counter));
586 NS_IMPL_ISUPPORTS(imgMemoryReporter, nsIMemoryReporter)
588 NS_IMPL_ISUPPORTS(nsProgressNotificationProxy, nsIProgressEventSink,
589 nsIChannelEventSink, nsIInterfaceRequestor)
591 NS_IMETHODIMP
592 nsProgressNotificationProxy::OnProgress(nsIRequest* request, int64_t progress,
593 int64_t progressMax) {
594 nsCOMPtr<nsILoadGroup> loadGroup;
595 request->GetLoadGroup(getter_AddRefs(loadGroup));
597 nsCOMPtr<nsIProgressEventSink> target;
598 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
599 NS_GET_IID(nsIProgressEventSink),
600 getter_AddRefs(target));
601 if (!target) {
602 return NS_OK;
604 return target->OnProgress(mImageRequest, progress, progressMax);
607 NS_IMETHODIMP
608 nsProgressNotificationProxy::OnStatus(nsIRequest* request, nsresult status,
609 const char16_t* statusArg) {
610 nsCOMPtr<nsILoadGroup> loadGroup;
611 request->GetLoadGroup(getter_AddRefs(loadGroup));
613 nsCOMPtr<nsIProgressEventSink> target;
614 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
615 NS_GET_IID(nsIProgressEventSink),
616 getter_AddRefs(target));
617 if (!target) {
618 return NS_OK;
620 return target->OnStatus(mImageRequest, status, statusArg);
623 NS_IMETHODIMP
624 nsProgressNotificationProxy::AsyncOnChannelRedirect(
625 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
626 nsIAsyncVerifyRedirectCallback* cb) {
627 // Tell the original original callbacks about it too
628 nsCOMPtr<nsILoadGroup> loadGroup;
629 newChannel->GetLoadGroup(getter_AddRefs(loadGroup));
630 nsCOMPtr<nsIChannelEventSink> target;
631 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
632 NS_GET_IID(nsIChannelEventSink),
633 getter_AddRefs(target));
634 if (!target) {
635 cb->OnRedirectVerifyCallback(NS_OK);
636 return NS_OK;
639 // Delegate to |target| if set, reusing |cb|
640 return target->AsyncOnChannelRedirect(oldChannel, newChannel, flags, cb);
643 NS_IMETHODIMP
644 nsProgressNotificationProxy::GetInterface(const nsIID& iid, void** result) {
645 if (iid.Equals(NS_GET_IID(nsIProgressEventSink))) {
646 *result = static_cast<nsIProgressEventSink*>(this);
647 NS_ADDREF_THIS();
648 return NS_OK;
650 if (iid.Equals(NS_GET_IID(nsIChannelEventSink))) {
651 *result = static_cast<nsIChannelEventSink*>(this);
652 NS_ADDREF_THIS();
653 return NS_OK;
655 if (mOriginalCallbacks) {
656 return mOriginalCallbacks->GetInterface(iid, result);
658 return NS_NOINTERFACE;
661 static void NewRequestAndEntry(bool aForcePrincipalCheckForCacheEntry,
662 imgLoader* aLoader, const ImageCacheKey& aKey,
663 imgRequest** aRequest, imgCacheEntry** aEntry) {
664 RefPtr<imgRequest> request = new imgRequest(aLoader, aKey);
665 RefPtr<imgCacheEntry> entry =
666 new imgCacheEntry(aLoader, request, aForcePrincipalCheckForCacheEntry);
667 aLoader->AddToUncachedImages(request);
668 request.forget(aRequest);
669 entry.forget(aEntry);
672 static bool ShouldRevalidateEntry(imgCacheEntry* aEntry, nsLoadFlags aFlags,
673 bool aHasExpired) {
674 if (aFlags & nsIRequest::LOAD_BYPASS_CACHE) {
675 return false;
677 if (aFlags & nsIRequest::VALIDATE_ALWAYS) {
678 return true;
680 if (aEntry->GetMustValidate()) {
681 return true;
683 if (aHasExpired) {
684 // The cache entry has expired... Determine whether the stale cache
685 // entry can be used without validation...
686 if (aFlags & (nsIRequest::LOAD_FROM_CACHE | nsIRequest::VALIDATE_NEVER |
687 nsIRequest::VALIDATE_ONCE_PER_SESSION)) {
688 // LOAD_FROM_CACHE, VALIDATE_NEVER and VALIDATE_ONCE_PER_SESSION allow
689 // stale cache entries to be used unless they have been explicitly marked
690 // to indicate that revalidation is necessary.
691 return false;
693 // Entry is expired, revalidate.
694 return true;
696 return false;
699 /* Call content policies on cached images that went through a redirect */
700 static bool ShouldLoadCachedImage(imgRequest* aImgRequest,
701 Document* aLoadingDocument,
702 nsIPrincipal* aTriggeringPrincipal,
703 nsContentPolicyType aPolicyType,
704 bool aSendCSPViolationReports) {
705 /* Call content policies on cached images - Bug 1082837
706 * Cached images are keyed off of the first uri in a redirect chain.
707 * Hence content policies don't get a chance to test the intermediate hops
708 * or the final destination. Here we test the final destination using
709 * mFinalURI off of the imgRequest and passing it into content policies.
710 * For Mixed Content Blocker, we do an additional check to determine if any
711 * of the intermediary hops went through an insecure redirect with the
712 * mHadInsecureRedirect flag
714 bool insecureRedirect = aImgRequest->HadInsecureRedirect();
715 nsCOMPtr<nsIURI> contentLocation;
716 aImgRequest->GetFinalURI(getter_AddRefs(contentLocation));
717 nsresult rv;
719 nsCOMPtr<nsIPrincipal> loadingPrincipal =
720 aLoadingDocument ? aLoadingDocument->NodePrincipal()
721 : aTriggeringPrincipal;
722 // If there is no context and also no triggeringPrincipal, then we use a fresh
723 // nullPrincipal as the loadingPrincipal because we can not create a loadinfo
724 // without a valid loadingPrincipal.
725 if (!loadingPrincipal) {
726 loadingPrincipal = NullPrincipal::CreateWithoutOriginAttributes();
729 nsCOMPtr<nsILoadInfo> secCheckLoadInfo = new LoadInfo(
730 loadingPrincipal, aTriggeringPrincipal, aLoadingDocument,
731 nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK, aPolicyType);
733 secCheckLoadInfo->SetSendCSPViolationEvents(aSendCSPViolationReports);
735 int16_t decision = nsIContentPolicy::REJECT_REQUEST;
736 rv = NS_CheckContentLoadPolicy(contentLocation, secCheckLoadInfo,
737 ""_ns, // mime guess
738 &decision, nsContentUtils::GetContentPolicy());
739 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
740 return false;
743 // We call all Content Policies above, but we also have to call mcb
744 // individually to check the intermediary redirect hops are secure.
745 if (insecureRedirect) {
746 // Bug 1314356: If the image ended up in the cache upgraded by HSTS and the
747 // page uses upgrade-inscure-requests it had an insecure redirect
748 // (http->https). We need to invalidate the image and reload it because
749 // mixed content blocker only bails if upgrade-insecure-requests is set on
750 // the doc and the resource load is http: which would result in an incorrect
751 // mixed content warning.
752 nsCOMPtr<nsIDocShell> docShell =
753 NS_CP_GetDocShellFromContext(ToSupports(aLoadingDocument));
754 if (docShell) {
755 Document* document = docShell->GetDocument();
756 if (document && document->GetUpgradeInsecureRequests(false)) {
757 return false;
761 if (!aTriggeringPrincipal || !aTriggeringPrincipal->IsSystemPrincipal()) {
762 // reset the decision for mixed content blocker check
763 decision = nsIContentPolicy::REJECT_REQUEST;
764 rv = nsMixedContentBlocker::ShouldLoad(insecureRedirect, contentLocation,
765 secCheckLoadInfo,
766 ""_ns, // mime guess
767 true, // aReportError
768 &decision);
769 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
770 return false;
775 return true;
778 // Returns true if this request is compatible with the given CORS mode on the
779 // given loading principal, and false if the request may not be reused due
780 // to CORS.
781 static bool ValidateCORSMode(imgRequest* aRequest, bool aForcePrincipalCheck,
782 CORSMode aCORSMode,
783 nsIPrincipal* aTriggeringPrincipal) {
784 // If the entry's CORS mode doesn't match, or the CORS mode matches but the
785 // document principal isn't the same, we can't use this request.
786 if (aRequest->GetCORSMode() != aCORSMode) {
787 return false;
790 if (aRequest->GetCORSMode() != CORS_NONE || aForcePrincipalCheck) {
791 nsCOMPtr<nsIPrincipal> otherprincipal = aRequest->GetTriggeringPrincipal();
793 // If we previously had a principal, but we don't now, we can't use this
794 // request.
795 if (otherprincipal && !aTriggeringPrincipal) {
796 return false;
799 if (otherprincipal && aTriggeringPrincipal &&
800 !otherprincipal->Equals(aTriggeringPrincipal)) {
801 return false;
805 return true;
808 static bool ValidateSecurityInfo(imgRequest* aRequest,
809 bool aForcePrincipalCheck, CORSMode aCORSMode,
810 nsIPrincipal* aTriggeringPrincipal,
811 Document* aLoadingDocument,
812 nsContentPolicyType aPolicyType) {
813 if (!ValidateCORSMode(aRequest, aForcePrincipalCheck, aCORSMode,
814 aTriggeringPrincipal)) {
815 return false;
817 // Content Policy Check on Cached Images
818 return ShouldLoadCachedImage(aRequest, aLoadingDocument, aTriggeringPrincipal,
819 aPolicyType,
820 /* aSendCSPViolationReports */ false);
823 static nsresult NewImageChannel(
824 nsIChannel** aResult,
825 // If aForcePrincipalCheckForCacheEntry is true, then we will
826 // force a principal check even when not using CORS before
827 // assuming we have a cache hit on a cache entry that we
828 // create for this channel. This is an out param that should
829 // be set to true if this channel ends up depending on
830 // aTriggeringPrincipal and false otherwise.
831 bool* aForcePrincipalCheckForCacheEntry, nsIURI* aURI,
832 nsIURI* aInitialDocumentURI, CORSMode aCORSMode,
833 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
834 nsLoadFlags aLoadFlags, nsContentPolicyType aPolicyType,
835 nsIPrincipal* aTriggeringPrincipal, nsINode* aRequestingNode,
836 bool aRespectPrivacy) {
837 MOZ_ASSERT(aResult);
839 nsresult rv;
840 nsCOMPtr<nsIHttpChannel> newHttpChannel;
842 nsCOMPtr<nsIInterfaceRequestor> callbacks;
844 if (aLoadGroup) {
845 // Get the notification callbacks from the load group for the new channel.
847 // XXX: This is not exactly correct, because the network request could be
848 // referenced by multiple windows... However, the new channel needs
849 // something. So, using the 'first' notification callbacks is better
850 // than nothing...
852 aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
855 // Pass in a nullptr loadgroup because this is the underlying network
856 // request. This request may be referenced by several proxy image requests
857 // (possibly in different documents).
858 // If all of the proxy requests are canceled then this request should be
859 // canceled too.
862 nsSecurityFlags securityFlags =
863 nsContentSecurityManager::ComputeSecurityFlags(
864 aCORSMode, nsContentSecurityManager::CORSSecurityMapping::
865 CORS_NONE_MAPS_TO_INHERITED_CONTEXT);
867 securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
869 // Note we are calling NS_NewChannelWithTriggeringPrincipal() here with a
870 // node and a principal. This is for things like background images that are
871 // specified by user stylesheets, where the document is being styled, but
872 // the principal is that of the user stylesheet.
873 if (aRequestingNode && aTriggeringPrincipal) {
874 rv = NS_NewChannelWithTriggeringPrincipal(aResult, aURI, aRequestingNode,
875 aTriggeringPrincipal,
876 securityFlags, aPolicyType,
877 nullptr, // PerformanceStorage
878 nullptr, // loadGroup
879 callbacks, aLoadFlags);
881 if (NS_FAILED(rv)) {
882 return rv;
885 if (aPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
886 // If this is a favicon loading, we will use the originAttributes from the
887 // triggeringPrincipal as the channel's originAttributes. This allows the
888 // favicon loading from XUL will use the correct originAttributes.
890 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
891 rv = loadInfo->SetOriginAttributes(
892 aTriggeringPrincipal->OriginAttributesRef());
894 } else {
895 // either we are loading something inside a document, in which case
896 // we should always have a requestingNode, or we are loading something
897 // outside a document, in which case the triggeringPrincipal and
898 // triggeringPrincipal should always be the systemPrincipal.
899 // However, there are exceptions: one is Notifications which create a
900 // channel in the parent process in which case we can't get a
901 // requestingNode.
902 rv = NS_NewChannel(aResult, aURI, nsContentUtils::GetSystemPrincipal(),
903 securityFlags, aPolicyType,
904 nullptr, // nsICookieJarSettings
905 nullptr, // PerformanceStorage
906 nullptr, // loadGroup
907 callbacks, aLoadFlags);
909 if (NS_FAILED(rv)) {
910 return rv;
913 // Use the OriginAttributes from the loading principal, if one is available,
914 // and adjust the private browsing ID based on what kind of load the caller
915 // has asked us to perform.
916 OriginAttributes attrs;
917 if (aTriggeringPrincipal) {
918 attrs = aTriggeringPrincipal->OriginAttributesRef();
920 attrs.mPrivateBrowsingId = aRespectPrivacy ? 1 : 0;
922 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
923 rv = loadInfo->SetOriginAttributes(attrs);
926 if (NS_FAILED(rv)) {
927 return rv;
930 // only inherit if we have a principal
931 *aForcePrincipalCheckForCacheEntry =
932 aTriggeringPrincipal && nsContentUtils::ChannelShouldInheritPrincipal(
933 aTriggeringPrincipal, aURI,
934 /* aInheritForAboutBlank */ false,
935 /* aForceInherit */ false);
937 // Initialize HTTP-specific attributes
938 newHttpChannel = do_QueryInterface(*aResult);
939 if (newHttpChannel) {
940 nsCOMPtr<nsIHttpChannelInternal> httpChannelInternal =
941 do_QueryInterface(newHttpChannel);
942 NS_ENSURE_TRUE(httpChannelInternal, NS_ERROR_UNEXPECTED);
943 rv = httpChannelInternal->SetDocumentURI(aInitialDocumentURI);
944 MOZ_ASSERT(NS_SUCCEEDED(rv));
945 if (aReferrerInfo) {
946 DebugOnly<nsresult> rv = newHttpChannel->SetReferrerInfo(aReferrerInfo);
947 MOZ_ASSERT(NS_SUCCEEDED(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(mRequest->IsChrome(), 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, aLoadingDocument, aURI,
1157 aObserver);
1158 if (NS_WARN_IF(NS_FAILED(rv))) {
1159 return rv;
1162 proxyRequest.forget(_retval);
1163 return NS_OK;
1166 class imgCacheExpirationTracker final
1167 : public nsExpirationTracker<imgCacheEntry, 3> {
1168 enum { TIMEOUT_SECONDS = 10 };
1170 public:
1171 imgCacheExpirationTracker();
1173 protected:
1174 void NotifyExpired(imgCacheEntry* entry) override;
1177 imgCacheExpirationTracker::imgCacheExpirationTracker()
1178 : nsExpirationTracker<imgCacheEntry, 3>(TIMEOUT_SECONDS * 1000,
1179 "imgCacheExpirationTracker") {}
1181 void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry* entry) {
1182 // Hold on to a reference to this entry, because the expiration tracker
1183 // mechanism doesn't.
1184 RefPtr<imgCacheEntry> kungFuDeathGrip(entry);
1186 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1187 RefPtr<imgRequest> req = entry->GetRequest();
1188 if (req) {
1189 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheExpirationTracker::NotifyExpired",
1190 "entry", req->CacheKey().URI());
1194 // We can be called multiple times on the same entry. Don't do work multiple
1195 // times.
1196 if (!entry->Evicted()) {
1197 entry->Loader()->RemoveFromCache(entry);
1200 entry->Loader()->VerifyCacheSizes();
1203 ///////////////////////////////////////////////////////////////////////////////
1204 // imgLoader
1205 ///////////////////////////////////////////////////////////////////////////////
1207 double imgLoader::sCacheTimeWeight;
1208 uint32_t imgLoader::sCacheMaxSize;
1209 imgMemoryReporter* imgLoader::sMemReporter;
1211 NS_IMPL_ISUPPORTS(imgLoader, imgILoader, nsIContentSniffer, imgICache,
1212 nsISupportsWeakReference, nsIObserver)
1214 static imgLoader* gNormalLoader = nullptr;
1215 static imgLoader* gPrivateBrowsingLoader = nullptr;
1217 /* static */
1218 already_AddRefed<imgLoader> imgLoader::CreateImageLoader() {
1219 // In some cases, such as xpctests, XPCOM modules are not automatically
1220 // initialized. We need to make sure that our module is initialized before
1221 // we hand out imgLoader instances and code starts using them.
1222 mozilla::image::EnsureModuleInitialized();
1224 RefPtr<imgLoader> loader = new imgLoader();
1225 loader->Init();
1227 return loader.forget();
1230 imgLoader* imgLoader::NormalLoader() {
1231 if (!gNormalLoader) {
1232 gNormalLoader = CreateImageLoader().take();
1234 return gNormalLoader;
1237 imgLoader* imgLoader::PrivateBrowsingLoader() {
1238 if (!gPrivateBrowsingLoader) {
1239 gPrivateBrowsingLoader = CreateImageLoader().take();
1240 gPrivateBrowsingLoader->RespectPrivacyNotifications();
1242 return gPrivateBrowsingLoader;
1245 imgLoader::imgLoader()
1246 : mUncachedImagesMutex("imgLoader::UncachedImages"),
1247 mRespectPrivacy(false) {
1248 sMemReporter->AddRef();
1249 sMemReporter->RegisterLoader(this);
1252 imgLoader::~imgLoader() {
1253 ClearChromeImageCache();
1254 ClearImageCache();
1256 // If there are any of our imgRequest's left they are in the uncached
1257 // images set, so clear their pointer to us.
1258 MutexAutoLock lock(mUncachedImagesMutex);
1259 for (RefPtr<imgRequest> req : mUncachedImages) {
1260 req->ClearLoader();
1263 sMemReporter->UnregisterLoader(this);
1264 sMemReporter->Release();
1267 void imgLoader::VerifyCacheSizes() {
1268 #ifdef DEBUG
1269 if (!mCacheTracker) {
1270 return;
1273 uint32_t cachesize = mCache.Count() + mChromeCache.Count();
1274 uint32_t queuesize =
1275 mCacheQueue.GetNumElements() + mChromeCacheQueue.GetNumElements();
1276 uint32_t trackersize = 0;
1277 for (nsExpirationTracker<imgCacheEntry, 3>::Iterator it(mCacheTracker.get());
1278 it.Next();) {
1279 trackersize++;
1281 MOZ_ASSERT(queuesize == trackersize, "Queue and tracker sizes out of sync!");
1282 MOZ_ASSERT(queuesize <= cachesize, "Queue has more elements than cache!");
1283 #endif
1286 imgLoader::imgCacheTable& imgLoader::GetCache(bool aForChrome) {
1287 return aForChrome ? mChromeCache : mCache;
1290 imgLoader::imgCacheTable& imgLoader::GetCache(const ImageCacheKey& aKey) {
1291 return GetCache(aKey.IsChrome());
1294 imgCacheQueue& imgLoader::GetCacheQueue(bool aForChrome) {
1295 return aForChrome ? mChromeCacheQueue : mCacheQueue;
1298 imgCacheQueue& imgLoader::GetCacheQueue(const ImageCacheKey& aKey) {
1299 return GetCacheQueue(aKey.IsChrome());
1302 void imgLoader::GlobalInit() {
1303 sCacheTimeWeight = StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
1304 int32_t cachesize = StaticPrefs::image_cache_size_AtStartup();
1305 sCacheMaxSize = cachesize > 0 ? cachesize : 0;
1307 sMemReporter = new imgMemoryReporter();
1308 RegisterStrongAsyncMemoryReporter(sMemReporter);
1309 RegisterImagesContentUsedUncompressedDistinguishedAmount(
1310 imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount);
1313 void imgLoader::ShutdownMemoryReporter() {
1314 UnregisterImagesContentUsedUncompressedDistinguishedAmount();
1315 UnregisterStrongMemoryReporter(sMemReporter);
1318 nsresult imgLoader::InitCache() {
1319 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
1320 if (!os) {
1321 return NS_ERROR_FAILURE;
1324 os->AddObserver(this, "memory-pressure", false);
1325 os->AddObserver(this, "chrome-flush-caches", false);
1326 os->AddObserver(this, "last-pb-context-exited", false);
1327 os->AddObserver(this, "profile-before-change", false);
1328 os->AddObserver(this, "xpcom-shutdown", false);
1330 mCacheTracker = MakeUnique<imgCacheExpirationTracker>();
1332 return NS_OK;
1335 nsresult imgLoader::Init() {
1336 InitCache();
1338 return NS_OK;
1341 NS_IMETHODIMP
1342 imgLoader::RespectPrivacyNotifications() {
1343 mRespectPrivacy = true;
1344 return NS_OK;
1347 NS_IMETHODIMP
1348 imgLoader::Observe(nsISupports* aSubject, const char* aTopic,
1349 const char16_t* aData) {
1350 if (strcmp(aTopic, "memory-pressure") == 0) {
1351 MinimizeCaches();
1352 } else if (strcmp(aTopic, "chrome-flush-caches") == 0) {
1353 MinimizeCaches();
1354 ClearChromeImageCache();
1355 } else if (strcmp(aTopic, "last-pb-context-exited") == 0) {
1356 if (mRespectPrivacy) {
1357 ClearImageCache();
1358 ClearChromeImageCache();
1360 } else if (strcmp(aTopic, "profile-before-change") == 0) {
1361 mCacheTracker = nullptr;
1362 } else if (strcmp(aTopic, "xpcom-shutdown") == 0) {
1363 mCacheTracker = nullptr;
1364 ShutdownMemoryReporter();
1366 } else {
1367 // (Nothing else should bring us here)
1368 MOZ_ASSERT(0, "Invalid topic received");
1371 return NS_OK;
1374 NS_IMETHODIMP
1375 imgLoader::ClearCache(bool chrome) {
1376 if (XRE_IsParentProcess()) {
1377 bool privateLoader = this == gPrivateBrowsingLoader;
1378 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1379 Unused << cp->SendClearImageCache(privateLoader, chrome);
1383 if (chrome) {
1384 return ClearChromeImageCache();
1386 return ClearImageCache();
1389 NS_IMETHODIMP
1390 imgLoader::RemoveEntriesFromPrincipalInAllProcesses(nsIPrincipal* aPrincipal) {
1391 if (!XRE_IsParentProcess()) {
1392 return NS_ERROR_NOT_AVAILABLE;
1395 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1396 Unused << cp->SendClearImageCacheFromPrincipal(aPrincipal);
1399 imgLoader* loader;
1400 if (aPrincipal->OriginAttributesRef().mPrivateBrowsingId ==
1401 nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID) {
1402 loader = imgLoader::NormalLoader();
1403 } else {
1404 loader = imgLoader::PrivateBrowsingLoader();
1407 return loader->RemoveEntriesInternal(aPrincipal, nullptr);
1410 NS_IMETHODIMP
1411 imgLoader::RemoveEntriesFromBaseDomainInAllProcesses(
1412 const nsACString& aBaseDomain) {
1413 if (!XRE_IsParentProcess()) {
1414 return NS_ERROR_NOT_AVAILABLE;
1417 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1418 Unused << cp->SendClearImageCacheFromBaseDomain(aBaseDomain);
1421 return RemoveEntriesInternal(nullptr, &aBaseDomain);
1424 nsresult imgLoader::RemoveEntriesInternal(nsIPrincipal* aPrincipal,
1425 const nsACString* aBaseDomain) {
1426 // Can only clear by either principal or base domain.
1427 if ((!aPrincipal && !aBaseDomain) || (aPrincipal && aBaseDomain)) {
1428 return NS_ERROR_INVALID_ARG;
1431 nsAutoString origin;
1432 if (aPrincipal) {
1433 nsresult rv = nsContentUtils::GetUTFOrigin(aPrincipal, origin);
1434 if (NS_WARN_IF(NS_FAILED(rv))) {
1435 return rv;
1439 nsCOMPtr<nsIEffectiveTLDService> tldService;
1440 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1442 // For base domain we only clear the non-chrome cache.
1443 imgCacheTable& cache =
1444 GetCache(aPrincipal && aPrincipal->IsSystemPrincipal());
1445 for (const auto& entry : cache) {
1446 const auto& key = entry.GetKey();
1448 const bool shouldRemove = [&] {
1449 if (aPrincipal) {
1450 if (key.OriginAttributesRef() !=
1451 BasePrincipal::Cast(aPrincipal)->OriginAttributesRef()) {
1452 return false;
1455 nsAutoString imageOrigin;
1456 nsresult rv = nsContentUtils::GetUTFOrigin(key.URI(), imageOrigin);
1457 if (NS_WARN_IF(NS_FAILED(rv))) {
1458 return false;
1461 return imageOrigin == origin;
1464 if (!aBaseDomain) {
1465 return false;
1467 // Clear by baseDomain.
1468 nsAutoCString host;
1469 nsresult rv = key.URI()->GetHost(host);
1470 if (NS_FAILED(rv) || host.IsEmpty()) {
1471 return false;
1474 if (!tldService) {
1475 tldService = do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
1477 if (NS_WARN_IF(!tldService)) {
1478 return false;
1481 bool hasRootDomain = false;
1482 rv = tldService->HasRootDomain(host, *aBaseDomain, &hasRootDomain);
1483 if (NS_SUCCEEDED(rv) && hasRootDomain) {
1484 return true;
1487 // If we don't get a direct base domain match, also check for cache of
1488 // third parties partitioned under aBaseDomain.
1490 // The isolation key is either just the base domain, or an origin suffix
1491 // which contains the partitionKey holding the baseDomain.
1493 if (key.IsolationKeyRef().Equals(*aBaseDomain)) {
1494 return true;
1497 // The isolation key does not match the given base domain. It may be an
1498 // origin suffix. Parse it into origin attributes.
1499 OriginAttributes attrs;
1500 if (!attrs.PopulateFromSuffix(key.IsolationKeyRef())) {
1501 // Key is not an origin suffix.
1502 return false;
1505 return StoragePrincipalHelper::PartitionKeyHasBaseDomain(
1506 attrs.mPartitionKey, *aBaseDomain);
1507 }();
1509 if (shouldRemove) {
1510 entriesToBeRemoved.AppendElement(entry.GetData());
1514 for (auto& entry : entriesToBeRemoved) {
1515 if (!RemoveFromCache(entry)) {
1516 NS_WARNING(
1517 "Couldn't remove an entry from the cache in "
1518 "RemoveEntriesInternal()\n");
1522 return NS_OK;
1525 NS_IMETHODIMP
1526 imgLoader::RemoveEntry(nsIURI* aURI, Document* aDoc) {
1527 if (aURI) {
1528 OriginAttributes attrs;
1529 if (aDoc) {
1530 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1531 if (principal) {
1532 attrs = principal->OriginAttributesRef();
1536 ImageCacheKey key(aURI, attrs, aDoc);
1537 if (RemoveFromCache(key)) {
1538 return NS_OK;
1541 return NS_ERROR_NOT_AVAILABLE;
1544 NS_IMETHODIMP
1545 imgLoader::FindEntryProperties(nsIURI* uri, Document* aDoc,
1546 nsIProperties** _retval) {
1547 *_retval = nullptr;
1549 OriginAttributes attrs;
1550 if (aDoc) {
1551 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1552 if (principal) {
1553 attrs = principal->OriginAttributesRef();
1557 ImageCacheKey key(uri, attrs, aDoc);
1558 imgCacheTable& cache = GetCache(key);
1560 RefPtr<imgCacheEntry> entry;
1561 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
1562 if (mCacheTracker && entry->HasNoProxies()) {
1563 mCacheTracker->MarkUsed(entry);
1566 RefPtr<imgRequest> request = entry->GetRequest();
1567 if (request) {
1568 nsCOMPtr<nsIProperties> properties = request->Properties();
1569 properties.forget(_retval);
1573 return NS_OK;
1576 NS_IMETHODIMP_(void)
1577 imgLoader::ClearCacheForControlledDocument(Document* aDoc) {
1578 MOZ_ASSERT(aDoc);
1579 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1580 imgCacheTable& cache = GetCache(false);
1581 for (const auto& entry : cache) {
1582 const auto& key = entry.GetKey();
1583 if (key.ControlledDocument() == aDoc) {
1584 entriesToBeRemoved.AppendElement(entry.GetData());
1587 for (auto& entry : entriesToBeRemoved) {
1588 if (!RemoveFromCache(entry)) {
1589 NS_WARNING(
1590 "Couldn't remove an entry from the cache in "
1591 "ClearCacheForControlledDocument()\n");
1596 void imgLoader::Shutdown() {
1597 NS_IF_RELEASE(gNormalLoader);
1598 gNormalLoader = nullptr;
1599 NS_IF_RELEASE(gPrivateBrowsingLoader);
1600 gPrivateBrowsingLoader = nullptr;
1603 nsresult imgLoader::ClearChromeImageCache() {
1604 return EvictEntries(mChromeCache);
1607 nsresult imgLoader::ClearImageCache() { return EvictEntries(mCache); }
1609 void imgLoader::MinimizeCaches() {
1610 EvictEntries(mCacheQueue);
1611 EvictEntries(mChromeCacheQueue);
1614 bool imgLoader::PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* entry) {
1615 imgCacheTable& cache = GetCache(aKey);
1617 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::PutIntoCache", "uri",
1618 aKey.URI());
1620 // Check to see if this request already exists in the cache. If so, we'll
1621 // replace the old version.
1622 RefPtr<imgCacheEntry> tmpCacheEntry;
1623 if (cache.Get(aKey, getter_AddRefs(tmpCacheEntry)) && tmpCacheEntry) {
1624 MOZ_LOG(
1625 gImgLog, LogLevel::Debug,
1626 ("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
1627 nullptr));
1628 RefPtr<imgRequest> tmpRequest = tmpCacheEntry->GetRequest();
1630 // If it already exists, and we're putting the same key into the cache, we
1631 // should remove the old version.
1632 MOZ_LOG(gImgLog, LogLevel::Debug,
1633 ("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
1634 nullptr));
1636 RemoveFromCache(aKey);
1637 } else {
1638 MOZ_LOG(gImgLog, LogLevel::Debug,
1639 ("[this=%p] imgLoader::PutIntoCache --"
1640 " Element NOT already in the cache",
1641 nullptr));
1644 cache.InsertOrUpdate(aKey, RefPtr{entry});
1646 // We can be called to resurrect an evicted entry.
1647 if (entry->Evicted()) {
1648 entry->SetEvicted(false);
1651 // If we're resurrecting an entry with no proxies, put it back in the
1652 // tracker and queue.
1653 if (entry->HasNoProxies()) {
1654 nsresult addrv = NS_OK;
1656 if (mCacheTracker) {
1657 addrv = mCacheTracker->AddObject(entry);
1660 if (NS_SUCCEEDED(addrv)) {
1661 imgCacheQueue& queue = GetCacheQueue(aKey);
1662 queue.Push(entry);
1666 RefPtr<imgRequest> request = entry->GetRequest();
1667 request->SetIsInCache(true);
1668 RemoveFromUncachedImages(request);
1670 return true;
1673 bool imgLoader::SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry) {
1674 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasNoProxies", "uri",
1675 aRequest->CacheKey().URI());
1677 aEntry->SetHasNoProxies(true);
1679 if (aEntry->Evicted()) {
1680 return false;
1683 imgCacheQueue& queue = GetCacheQueue(aRequest->IsChrome());
1685 nsresult addrv = NS_OK;
1687 if (mCacheTracker) {
1688 addrv = mCacheTracker->AddObject(aEntry);
1691 if (NS_SUCCEEDED(addrv)) {
1692 queue.Push(aEntry);
1695 imgCacheTable& cache = GetCache(aRequest->IsChrome());
1696 CheckCacheLimits(cache, queue);
1698 return true;
1701 bool imgLoader::SetHasProxies(imgRequest* aRequest) {
1702 VerifyCacheSizes();
1704 const ImageCacheKey& key = aRequest->CacheKey();
1705 imgCacheTable& cache = GetCache(key);
1707 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasProxies", "uri",
1708 key.URI());
1710 RefPtr<imgCacheEntry> entry;
1711 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
1712 // Make sure the cache entry is for the right request
1713 RefPtr<imgRequest> entryRequest = entry->GetRequest();
1714 if (entryRequest == aRequest && entry->HasNoProxies()) {
1715 imgCacheQueue& queue = GetCacheQueue(key);
1716 queue.Remove(entry);
1718 if (mCacheTracker) {
1719 mCacheTracker->RemoveObject(entry);
1722 entry->SetHasNoProxies(false);
1724 return true;
1728 return false;
1731 void imgLoader::CacheEntriesChanged(bool aForChrome,
1732 int32_t aSizeDiff /* = 0 */) {
1733 imgCacheQueue& queue = GetCacheQueue(aForChrome);
1734 // We only need to dirty the queue if there is any sorting
1735 // taking place. Empty or single-entry lists can't become
1736 // dirty.
1737 if (queue.GetNumElements() > 1) {
1738 queue.MarkDirty();
1740 queue.UpdateSize(aSizeDiff);
1743 void imgLoader::CheckCacheLimits(imgCacheTable& cache, imgCacheQueue& queue) {
1744 if (queue.GetNumElements() == 0) {
1745 NS_ASSERTION(queue.GetSize() == 0,
1746 "imgLoader::CheckCacheLimits -- incorrect cache size");
1749 // Remove entries from the cache until we're back at our desired max size.
1750 while (queue.GetSize() > sCacheMaxSize) {
1751 // Remove the first entry in the queue.
1752 RefPtr<imgCacheEntry> entry(queue.Pop());
1754 NS_ASSERTION(entry, "imgLoader::CheckCacheLimits -- NULL entry pointer");
1756 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1757 RefPtr<imgRequest> req = entry->GetRequest();
1758 if (req) {
1759 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::CheckCacheLimits",
1760 "entry", req->CacheKey().URI());
1764 if (entry) {
1765 // We just popped this entry from the queue, so pass AlreadyRemoved
1766 // to avoid searching the queue again in RemoveFromCache.
1767 RemoveFromCache(entry, QueueState::AlreadyRemoved);
1772 bool imgLoader::ValidateRequestWithNewChannel(
1773 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1774 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1775 imgINotificationObserver* aObserver, Document* aLoadingDocument,
1776 uint64_t aInnerWindowId, nsLoadFlags aLoadFlags,
1777 nsContentPolicyType aLoadPolicyType, imgRequestProxy** aProxyRequest,
1778 nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode, bool aLinkPreload,
1779 bool* aNewChannelCreated) {
1780 // now we need to insert a new channel request object in between the real
1781 // request and the proxy that basically delays loading the image until it
1782 // gets a 304 or figures out that this needs to be a new request
1784 nsresult rv;
1786 // If we're currently in the middle of validating this request, just hand
1787 // back a proxy to it; the required work will be done for us.
1788 if (imgCacheValidator* validator = request->GetValidator()) {
1789 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
1790 aObserver, aLoadFlags, aProxyRequest);
1791 if (NS_FAILED(rv)) {
1792 return false;
1795 if (*aProxyRequest) {
1796 imgRequestProxy* proxy = static_cast<imgRequestProxy*>(*aProxyRequest);
1798 // We will send notifications from imgCacheValidator::OnStartRequest().
1799 // In the mean time, we must defer notifications because we are added to
1800 // the imgRequest's proxy list, and we can get extra notifications
1801 // resulting from methods such as StartDecoding(). See bug 579122.
1802 proxy->MarkValidating();
1804 if (aLinkPreload) {
1805 MOZ_ASSERT(aLoadingDocument);
1806 proxy->PrioritizeAsPreload();
1807 auto preloadKey = PreloadHashKey::CreateAsImage(
1808 aURI, aTriggeringPrincipal, aCORSMode);
1809 proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
1812 // Attach the proxy without notifying
1813 validator->AddProxy(proxy);
1816 return true;
1818 // We will rely on Necko to cache this request when it's possible, and to
1819 // tell imgCacheValidator::OnStartRequest whether the request came from its
1820 // cache.
1821 nsCOMPtr<nsIChannel> newChannel;
1822 bool forcePrincipalCheck;
1823 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
1824 aInitialDocumentURI, aCORSMode, aReferrerInfo,
1825 aLoadGroup, aLoadFlags, aLoadPolicyType,
1826 aTriggeringPrincipal, aLoadingDocument, mRespectPrivacy);
1827 if (NS_FAILED(rv)) {
1828 return false;
1831 if (aNewChannelCreated) {
1832 *aNewChannelCreated = true;
1835 RefPtr<imgRequestProxy> req;
1836 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
1837 aObserver, aLoadFlags, getter_AddRefs(req));
1838 if (NS_FAILED(rv)) {
1839 return false;
1842 // Make sure that OnStatus/OnProgress calls have the right request set...
1843 RefPtr<nsProgressNotificationProxy> progressproxy =
1844 new nsProgressNotificationProxy(newChannel, req);
1845 if (!progressproxy) {
1846 return false;
1849 RefPtr<imgCacheValidator> hvc =
1850 new imgCacheValidator(progressproxy, this, request, aLoadingDocument,
1851 aInnerWindowId, forcePrincipalCheck);
1853 // Casting needed here to get past multiple inheritance.
1854 nsCOMPtr<nsIStreamListener> listener =
1855 do_QueryInterface(static_cast<nsIThreadRetargetableStreamListener*>(hvc));
1856 NS_ENSURE_TRUE(listener, false);
1858 // We must set the notification callbacks before setting up the
1859 // CORS listener, because that's also interested inthe
1860 // notification callbacks.
1861 newChannel->SetNotificationCallbacks(hvc);
1863 request->SetValidator(hvc);
1865 // We will send notifications from imgCacheValidator::OnStartRequest().
1866 // In the mean time, we must defer notifications because we are added to
1867 // the imgRequest's proxy list, and we can get extra notifications
1868 // resulting from methods such as StartDecoding(). See bug 579122.
1869 req->MarkValidating();
1871 if (aLinkPreload) {
1872 MOZ_ASSERT(aLoadingDocument);
1873 req->PrioritizeAsPreload();
1874 auto preloadKey =
1875 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, aCORSMode);
1876 req->NotifyOpen(preloadKey, aLoadingDocument, true);
1879 // Add the proxy without notifying
1880 hvc->AddProxy(req);
1882 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
1883 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
1884 aLoadGroup);
1885 rv = newChannel->AsyncOpen(listener);
1886 if (NS_WARN_IF(NS_FAILED(rv))) {
1887 req->CancelAndForgetObserver(rv);
1888 // This will notify any current or future <link preload> tags. Pass the
1889 // non-open channel so that we can read loadinfo and referrer info of that
1890 // channel.
1891 req->NotifyStart(newChannel);
1892 // Use the non-channel overload of this method to force the notification to
1893 // happen. The preload request has not been assigned a channel.
1894 req->NotifyStop(rv);
1895 return false;
1898 req.forget(aProxyRequest);
1899 return true;
1902 void imgLoader::NotifyObserversForCachedImage(
1903 imgCacheEntry* aEntry, imgRequest* request, nsIURI* aURI,
1904 nsIReferrerInfo* aReferrerInfo, Document* aLoadingDocument,
1905 nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode) {
1906 if (aEntry->HasNotified()) {
1907 return;
1910 nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
1912 if (!obsService->HasObservers("http-on-image-cache-response")) {
1913 return;
1916 aEntry->SetHasNotified();
1918 nsCOMPtr<nsIChannel> newChannel;
1919 bool forcePrincipalCheck;
1920 nsresult rv =
1921 NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
1922 nullptr, aCORSMode, aReferrerInfo, nullptr, 0,
1923 nsIContentPolicy::TYPE_INTERNAL_IMAGE,
1924 aTriggeringPrincipal, aLoadingDocument, mRespectPrivacy);
1925 if (NS_FAILED(rv)) {
1926 return;
1929 RefPtr<HttpBaseChannel> httpBaseChannel = do_QueryObject(newChannel);
1930 if (httpBaseChannel) {
1931 httpBaseChannel->SetDummyChannelForImageCache();
1932 newChannel->SetContentType(nsDependentCString(request->GetMimeType()));
1933 RefPtr<mozilla::image::Image> image = request->GetImage();
1934 if (image) {
1935 newChannel->SetContentLength(aEntry->GetDataSize());
1937 obsService->NotifyObservers(newChannel, "http-on-image-cache-response",
1938 nullptr);
1942 bool imgLoader::ValidateEntry(
1943 imgCacheEntry* aEntry, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1944 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1945 imgINotificationObserver* aObserver, Document* aLoadingDocument,
1946 nsLoadFlags aLoadFlags, nsContentPolicyType aLoadPolicyType,
1947 bool aCanMakeNewChannel, bool* aNewChannelCreated,
1948 imgRequestProxy** aProxyRequest, nsIPrincipal* aTriggeringPrincipal,
1949 CORSMode aCORSMode, bool aLinkPreload) {
1950 LOG_SCOPE(gImgLog, "imgLoader::ValidateEntry");
1952 // If the expiration time is zero, then the request has not gotten far enough
1953 // to know when it will expire, or we know it will never expire (see
1954 // nsContentUtils::GetSubresourceCacheValidationInfo).
1955 uint32_t expiryTime = aEntry->GetExpiryTime();
1956 bool hasExpired = expiryTime && expiryTime <= SecondsFromPRTime(PR_Now());
1958 nsresult rv;
1960 // Special treatment for file URLs - aEntry has expired if file has changed
1961 nsCOMPtr<nsIFileURL> fileUrl(do_QueryInterface(aURI));
1962 if (fileUrl) {
1963 uint32_t lastModTime = aEntry->GetLoadTime();
1965 nsCOMPtr<nsIFile> theFile;
1966 rv = fileUrl->GetFile(getter_AddRefs(theFile));
1967 if (NS_SUCCEEDED(rv)) {
1968 PRTime fileLastMod;
1969 rv = theFile->GetLastModifiedTime(&fileLastMod);
1970 if (NS_SUCCEEDED(rv)) {
1971 // nsIFile uses millisec, NSPR usec
1972 fileLastMod *= 1000;
1973 hasExpired = SecondsFromPRTime((PRTime)fileLastMod) > lastModTime;
1978 RefPtr<imgRequest> request(aEntry->GetRequest());
1980 if (!request) {
1981 return false;
1984 if (!ValidateSecurityInfo(request, aEntry->ForcePrincipalCheck(), aCORSMode,
1985 aTriggeringPrincipal, aLoadingDocument,
1986 aLoadPolicyType)) {
1987 return false;
1990 // data URIs are immutable and by their nature can't leak data, so we can
1991 // just return true in that case. Doing so would mean that shift-reload
1992 // doesn't reload data URI documents/images though (which is handy for
1993 // debugging during gecko development) so we make an exception in that case.
1994 if (aURI->SchemeIs("data") && !(aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE)) {
1995 return true;
1998 bool validateRequest = false;
2000 if (!request->CanReuseWithoutValidation(aLoadingDocument)) {
2001 // If we would need to revalidate this entry, but we're being told to
2002 // bypass the cache, we don't allow this entry to be used.
2003 if (aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2004 return false;
2007 if (MOZ_UNLIKELY(ChaosMode::isActive(ChaosFeature::ImageCache))) {
2008 if (ChaosMode::randomUint32LessThan(4) < 1) {
2009 return false;
2013 // Determine whether the cache aEntry must be revalidated...
2014 validateRequest = ShouldRevalidateEntry(aEntry, aLoadFlags, hasExpired);
2016 MOZ_LOG(gImgLog, LogLevel::Debug,
2017 ("imgLoader::ValidateEntry validating cache entry. "
2018 "validateRequest = %d",
2019 validateRequest));
2020 } else if (!aLoadingDocument && MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
2021 MOZ_LOG(gImgLog, LogLevel::Debug,
2022 ("imgLoader::ValidateEntry BYPASSING cache validation for %s "
2023 "because of NULL loading document",
2024 aURI->GetSpecOrDefault().get()));
2027 // If the original request is still transferring don't kick off a validation
2028 // network request because it is a bit silly to issue a validation request if
2029 // the original request hasn't even finished yet. So just return true
2030 // indicating the caller can create a new proxy for the request and use it as
2031 // is.
2032 // This is an optimization but it's also required for correctness. If we don't
2033 // do this then when firing the load complete notification for the original
2034 // request that can unblock load for the document and then spin the event loop
2035 // (see the stack in bug 1641682) which then the OnStartRequest for the
2036 // validation request can fire which can call UpdateProxies and can sync
2037 // notify on the progress tracker about all existing state, which includes
2038 // load complete, so we fire a second load complete notification for the
2039 // image.
2040 // In addition, we want to validate if the original request encountered
2041 // an error for two reasons. The first being if the error was a network error
2042 // then trying to re-fetch the image might succeed. The second is more
2043 // complicated. We decide if we should fire the load or error event for img
2044 // elements depending on if the image has error in its status at the time when
2045 // the load complete notification is received, and we set error status on an
2046 // image if it encounters a network error or a decode error with no real way
2047 // to tell them apart. So if we load an image that will produce a decode error
2048 // the first time we will usually fire the load event, and then decode enough
2049 // to encounter the decode error and set the error status on the image. The
2050 // next time we reference the image in the same document the load complete
2051 // notification is replayed and this time the error status from the decode is
2052 // already present so we fire the error event instead of the load event. This
2053 // is a bug (bug 1645576) that we should fix. In order to avoid that bug in
2054 // some cases (specifically the cases when we hit this code and try to
2055 // validate the request) we make sure to validate. This avoids the bug because
2056 // when the load complete notification arrives the proxy is marked as
2057 // validating so it lies about its status and returns nothing.
2058 bool requestComplete = false;
2059 RefPtr<ProgressTracker> tracker;
2060 RefPtr<mozilla::image::Image> image = request->GetImage();
2061 if (image) {
2062 tracker = image->GetProgressTracker();
2063 } else {
2064 tracker = request->GetProgressTracker();
2066 if (tracker) {
2067 if (tracker->GetProgress() & (FLAG_LOAD_COMPLETE | FLAG_HAS_ERROR)) {
2068 requestComplete = true;
2071 if (!requestComplete) {
2072 return true;
2075 if (validateRequest && aCanMakeNewChannel) {
2076 LOG_SCOPE(gImgLog, "imgLoader::ValidateRequest |cache hit| must validate");
2078 uint64_t innerWindowID =
2079 aLoadingDocument ? aLoadingDocument->InnerWindowID() : 0;
2080 return ValidateRequestWithNewChannel(
2081 request, aURI, aInitialDocumentURI, aReferrerInfo, aLoadGroup,
2082 aObserver, aLoadingDocument, innerWindowID, aLoadFlags, aLoadPolicyType,
2083 aProxyRequest, aTriggeringPrincipal, aCORSMode, aLinkPreload,
2084 aNewChannelCreated);
2087 if (!validateRequest) {
2088 NotifyObserversForCachedImage(aEntry, request, aURI, aReferrerInfo,
2089 aLoadingDocument, aTriggeringPrincipal,
2090 aCORSMode);
2093 return !validateRequest;
2096 bool imgLoader::RemoveFromCache(const ImageCacheKey& aKey) {
2097 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache", "uri",
2098 aKey.URI());
2100 imgCacheTable& cache = GetCache(aKey);
2101 imgCacheQueue& queue = GetCacheQueue(aKey);
2103 RefPtr<imgCacheEntry> entry;
2104 cache.Remove(aKey, getter_AddRefs(entry));
2105 if (entry) {
2106 MOZ_ASSERT(!entry->Evicted(), "Evicting an already-evicted cache entry!");
2108 // Entries with no proxies are in the tracker.
2109 if (entry->HasNoProxies()) {
2110 if (mCacheTracker) {
2111 mCacheTracker->RemoveObject(entry);
2113 queue.Remove(entry);
2116 entry->SetEvicted(true);
2118 RefPtr<imgRequest> request = entry->GetRequest();
2119 request->SetIsInCache(false);
2120 AddToUncachedImages(request);
2122 return true;
2124 return false;
2127 bool imgLoader::RemoveFromCache(imgCacheEntry* entry, QueueState aQueueState) {
2128 LOG_STATIC_FUNC(gImgLog, "imgLoader::RemoveFromCache entry");
2130 RefPtr<imgRequest> request = entry->GetRequest();
2131 if (request) {
2132 const ImageCacheKey& key = request->CacheKey();
2133 imgCacheTable& cache = GetCache(key);
2134 imgCacheQueue& queue = GetCacheQueue(key);
2136 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache",
2137 "entry's uri", key.URI());
2139 cache.Remove(key);
2141 if (entry->HasNoProxies()) {
2142 LOG_STATIC_FUNC(gImgLog,
2143 "imgLoader::RemoveFromCache removing from tracker");
2144 if (mCacheTracker) {
2145 mCacheTracker->RemoveObject(entry);
2147 // Only search the queue to remove the entry if its possible it might
2148 // be in the queue. If we know its not in the queue this would be
2149 // wasted work.
2150 MOZ_ASSERT_IF(aQueueState == QueueState::AlreadyRemoved,
2151 !queue.Contains(entry));
2152 if (aQueueState == QueueState::MaybeExists) {
2153 queue.Remove(entry);
2157 entry->SetEvicted(true);
2158 request->SetIsInCache(false);
2159 AddToUncachedImages(request);
2161 return true;
2164 return false;
2167 nsresult imgLoader::EvictEntries(imgCacheTable& aCacheToClear) {
2168 LOG_STATIC_FUNC(gImgLog, "imgLoader::EvictEntries table");
2170 // We have to make a temporary, since RemoveFromCache removes the element
2171 // from the queue, invalidating iterators.
2172 const auto entries =
2173 ToTArray<nsTArray<RefPtr<imgCacheEntry>>>(aCacheToClear.Values());
2174 for (const auto& entry : entries) {
2175 if (!RemoveFromCache(entry)) {
2176 return NS_ERROR_FAILURE;
2180 MOZ_ASSERT(aCacheToClear.Count() == 0);
2182 return NS_OK;
2185 nsresult imgLoader::EvictEntries(imgCacheQueue& aQueueToClear) {
2186 LOG_STATIC_FUNC(gImgLog, "imgLoader::EvictEntries queue");
2188 // We have to make a temporary, since RemoveFromCache removes the element
2189 // from the queue, invalidating iterators.
2190 nsTArray<RefPtr<imgCacheEntry>> entries(aQueueToClear.GetNumElements());
2191 for (auto i = aQueueToClear.begin(); i != aQueueToClear.end(); ++i) {
2192 entries.AppendElement(*i);
2195 // Iterate in reverse order to minimize array copying.
2196 for (auto& entry : entries) {
2197 if (!RemoveFromCache(entry)) {
2198 return NS_ERROR_FAILURE;
2202 MOZ_ASSERT(aQueueToClear.GetNumElements() == 0);
2204 return NS_OK;
2207 void imgLoader::AddToUncachedImages(imgRequest* aRequest) {
2208 MutexAutoLock lock(mUncachedImagesMutex);
2209 mUncachedImages.Insert(aRequest);
2212 void imgLoader::RemoveFromUncachedImages(imgRequest* aRequest) {
2213 MutexAutoLock lock(mUncachedImagesMutex);
2214 mUncachedImages.Remove(aRequest);
2217 #define LOAD_FLAGS_CACHE_MASK \
2218 (nsIRequest::LOAD_BYPASS_CACHE | nsIRequest::LOAD_FROM_CACHE)
2220 #define LOAD_FLAGS_VALIDATE_MASK \
2221 (nsIRequest::VALIDATE_ALWAYS | nsIRequest::VALIDATE_NEVER | \
2222 nsIRequest::VALIDATE_ONCE_PER_SESSION)
2224 NS_IMETHODIMP
2225 imgLoader::LoadImageXPCOM(
2226 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2227 nsIPrincipal* aTriggeringPrincipal, nsILoadGroup* aLoadGroup,
2228 imgINotificationObserver* aObserver, Document* aLoadingDocument,
2229 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
2230 nsContentPolicyType aContentPolicyType, imgIRequest** _retval) {
2231 // Optional parameter, so defaults to 0 (== TYPE_INVALID)
2232 if (!aContentPolicyType) {
2233 aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE;
2235 imgRequestProxy* proxy;
2236 nsresult rv =
2237 LoadImage(aURI, aInitialDocumentURI, aReferrerInfo, aTriggeringPrincipal,
2238 0, aLoadGroup, aObserver, aLoadingDocument, aLoadingDocument,
2239 aLoadFlags, aCacheKey, aContentPolicyType, u""_ns,
2240 /* aUseUrgentStartForChannel */ false, /* aListPreload */ false,
2241 0, &proxy);
2242 *_retval = proxy;
2243 return rv;
2246 static void MakeRequestStaticIfNeeded(
2247 Document* aLoadingDocument, imgRequestProxy** aProxyAboutToGetReturned) {
2248 if (!aLoadingDocument || !aLoadingDocument->IsStaticDocument()) {
2249 return;
2252 if (!*aProxyAboutToGetReturned) {
2253 return;
2256 RefPtr<imgRequestProxy> proxy = dont_AddRef(*aProxyAboutToGetReturned);
2257 *aProxyAboutToGetReturned = nullptr;
2259 RefPtr<imgRequestProxy> staticProxy =
2260 proxy->GetStaticRequest(aLoadingDocument);
2261 if (staticProxy != proxy) {
2262 proxy->CancelAndForgetObserver(NS_BINDING_ABORTED);
2263 proxy = std::move(staticProxy);
2265 proxy.forget(aProxyAboutToGetReturned);
2268 bool imgLoader::IsImageAvailable(nsIURI* aURI,
2269 nsIPrincipal* aTriggeringPrincipal,
2270 CORSMode aCORSMode, Document* aDocument) {
2271 ImageCacheKey key(aURI, aTriggeringPrincipal->OriginAttributesRef(),
2272 aDocument);
2273 RefPtr<imgCacheEntry> entry;
2274 imgCacheTable& cache = GetCache(key);
2275 if (!cache.Get(key, getter_AddRefs(entry)) || !entry) {
2276 return false;
2278 RefPtr<imgRequest> request = entry->GetRequest();
2279 if (!request) {
2280 return false;
2282 if (nsCOMPtr<nsILoadGroup> docLoadGroup = aDocument->GetDocumentLoadGroup()) {
2283 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2284 docLoadGroup->GetLoadFlags(&requestFlags);
2285 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2286 // If we're bypassing the cache, treat the image as not available.
2287 return false;
2290 return ValidateCORSMode(request, false, aCORSMode, aTriggeringPrincipal);
2293 nsresult imgLoader::LoadImage(
2294 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2295 nsIPrincipal* aTriggeringPrincipal, uint64_t aRequestContextID,
2296 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
2297 nsINode* aContext, Document* aLoadingDocument, nsLoadFlags aLoadFlags,
2298 nsISupports* aCacheKey, nsContentPolicyType aContentPolicyType,
2299 const nsAString& initiatorType, bool aUseUrgentStartForChannel,
2300 bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
2301 imgRequestProxy** _retval) {
2302 VerifyCacheSizes();
2304 NS_ASSERTION(aURI, "imgLoader::LoadImage -- NULL URI pointer");
2306 if (!aURI) {
2307 return NS_ERROR_NULL_POINTER;
2310 auto makeStaticIfNeeded = mozilla::MakeScopeExit(
2311 [&] { MakeRequestStaticIfNeeded(aLoadingDocument, _retval); });
2313 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK,
2314 aURI->GetSpecOrDefault());
2316 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::LoadImage", "aURI", aURI);
2318 *_retval = nullptr;
2320 RefPtr<imgRequest> request;
2322 nsresult rv;
2323 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2325 #ifdef DEBUG
2326 bool isPrivate = false;
2328 if (aLoadingDocument) {
2329 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadingDocument);
2330 } else if (aLoadGroup) {
2331 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadGroup);
2333 MOZ_ASSERT(isPrivate == mRespectPrivacy);
2335 if (aLoadingDocument) {
2336 // The given load group should match that of the document if given. If
2337 // that isn't the case, then we need to add more plumbing to ensure we
2338 // block the document as well.
2339 nsCOMPtr<nsILoadGroup> docLoadGroup =
2340 aLoadingDocument->GetDocumentLoadGroup();
2341 MOZ_ASSERT(docLoadGroup == aLoadGroup);
2343 #endif
2345 // Get the default load flags from the loadgroup (if possible)...
2346 if (aLoadGroup) {
2347 aLoadGroup->GetLoadFlags(&requestFlags);
2350 // Merge the default load flags with those passed in via aLoadFlags.
2351 // Currently, *only* the caching, validation and background load flags
2352 // are merged...
2354 // The flags in aLoadFlags take precedence over the default flags!
2356 if (aLoadFlags & LOAD_FLAGS_CACHE_MASK) {
2357 // Override the default caching flags...
2358 requestFlags = (requestFlags & ~LOAD_FLAGS_CACHE_MASK) |
2359 (aLoadFlags & LOAD_FLAGS_CACHE_MASK);
2361 if (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK) {
2362 // Override the default validation flags...
2363 requestFlags = (requestFlags & ~LOAD_FLAGS_VALIDATE_MASK) |
2364 (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK);
2366 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
2367 // Propagate background loading...
2368 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2370 if (aLoadFlags & nsIRequest::LOAD_RECORD_START_REQUEST_DELAY) {
2371 requestFlags |= nsIRequest::LOAD_RECORD_START_REQUEST_DELAY;
2374 if (aLinkPreload) {
2375 // Set background loading if it is <link rel=preload>
2376 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2379 CORSMode corsmode = CORS_NONE;
2380 if (aLoadFlags & imgILoader::LOAD_CORS_ANONYMOUS) {
2381 corsmode = CORS_ANONYMOUS;
2382 } else if (aLoadFlags & imgILoader::LOAD_CORS_USE_CREDENTIALS) {
2383 corsmode = CORS_USE_CREDENTIALS;
2386 // Look in the preloaded images of loading document first.
2387 if (StaticPrefs::network_preload() && !aLinkPreload && aLoadingDocument) {
2388 auto key =
2389 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, corsmode);
2390 if (RefPtr<PreloaderBase> preload =
2391 aLoadingDocument->Preloads().LookupPreload(key)) {
2392 RefPtr<imgRequestProxy> proxy = do_QueryObject(preload);
2393 MOZ_ASSERT(proxy);
2395 MOZ_LOG(gImgLog, LogLevel::Debug,
2396 ("[this=%p] imgLoader::LoadImage -- preloaded [proxy=%p]"
2397 " [document=%p]\n",
2398 this, proxy.get(), aLoadingDocument));
2400 // Removing the preload for this image to be in parity with Chromium. Any
2401 // following regular image request will be reloaded using the regular
2402 // path: image cache, http cache, network. Any following `<link
2403 // rel=preload as=image>` will start a new image preload that can be
2404 // satisfied from http cache or network.
2406 // There is a spec discussion for "preload cache", see
2407 // https://github.com/w3c/preload/issues/97. And it is also not clear how
2408 // preload image interacts with list of available images, see
2409 // https://github.com/whatwg/html/issues/4474.
2410 proxy->RemoveSelf(aLoadingDocument);
2411 proxy->NotifyUsage();
2413 imgRequest* request = proxy->GetOwner();
2414 nsresult rv =
2415 CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
2416 aObserver, requestFlags, _retval);
2417 NS_ENSURE_SUCCESS(rv, rv);
2419 imgRequestProxy* newProxy = *_retval;
2420 if (imgCacheValidator* validator = request->GetValidator()) {
2421 newProxy->MarkValidating();
2422 // Attach the proxy without notifying and this will add us to the load
2423 // group.
2424 validator->AddProxy(newProxy);
2425 } else {
2426 // It's OK to add here even if the request is done. If it is, it'll send
2427 // a OnStopRequest()and the proxy will be removed from the loadgroup in
2428 // imgRequestProxy::OnLoadComplete.
2429 newProxy->AddToLoadGroup();
2430 newProxy->NotifyListener();
2433 return NS_OK;
2437 RefPtr<imgCacheEntry> entry;
2439 // Look in the cache for our URI, and then validate it.
2440 // XXX For now ignore aCacheKey. We will need it in the future
2441 // for correctly dealing with image load requests that are a result
2442 // of post data.
2443 OriginAttributes attrs;
2444 if (aTriggeringPrincipal) {
2445 attrs = aTriggeringPrincipal->OriginAttributesRef();
2447 ImageCacheKey key(aURI, attrs, aLoadingDocument);
2448 imgCacheTable& cache = GetCache(key);
2450 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
2451 bool newChannelCreated = false;
2452 if (ValidateEntry(entry, aURI, aInitialDocumentURI, aReferrerInfo,
2453 aLoadGroup, aObserver, aLoadingDocument, requestFlags,
2454 aContentPolicyType, true, &newChannelCreated, _retval,
2455 aTriggeringPrincipal, corsmode, aLinkPreload)) {
2456 request = entry->GetRequest();
2458 // If this entry has no proxies, its request has no reference to the
2459 // entry.
2460 if (entry->HasNoProxies()) {
2461 LOG_FUNC_WITH_PARAM(gImgLog,
2462 "imgLoader::LoadImage() adding proxyless entry",
2463 "uri", key.URI());
2464 MOZ_ASSERT(!request->HasCacheEntry(),
2465 "Proxyless entry's request has cache entry!");
2466 request->SetCacheEntry(entry);
2468 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2469 mCacheTracker->MarkUsed(entry);
2473 entry->Touch();
2475 if (!newChannelCreated) {
2476 // This is ugly but it's needed to report CSP violations. We have 3
2477 // scenarios:
2478 // - we don't have cache. We are not in this if() stmt. A new channel is
2479 // created and that triggers the CSP checks.
2480 // - We have a cache entry and this is blocked by CSP directives.
2481 DebugOnly<bool> shouldLoad = ShouldLoadCachedImage(
2482 request, aLoadingDocument, aTriggeringPrincipal, aContentPolicyType,
2483 /* aSendCSPViolationReports */ true);
2484 MOZ_ASSERT(shouldLoad);
2486 } else {
2487 // We can't use this entry. We'll try to load it off the network, and if
2488 // successful, overwrite the old entry in the cache with a new one.
2489 entry = nullptr;
2493 // Keep the channel in this scope, so we can adjust its notificationCallbacks
2494 // later when we create the proxy.
2495 nsCOMPtr<nsIChannel> newChannel;
2496 // If we didn't get a cache hit, we need to load from the network.
2497 if (!request) {
2498 LOG_SCOPE(gImgLog, "imgLoader::LoadImage |cache miss|");
2500 bool forcePrincipalCheck;
2501 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
2502 aInitialDocumentURI, corsmode, aReferrerInfo,
2503 aLoadGroup, requestFlags, aContentPolicyType,
2504 aTriggeringPrincipal, aContext, mRespectPrivacy);
2505 if (NS_FAILED(rv)) {
2506 return NS_ERROR_FAILURE;
2509 MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel) == mRespectPrivacy);
2511 NewRequestAndEntry(forcePrincipalCheck, this, key, getter_AddRefs(request),
2512 getter_AddRefs(entry));
2514 MOZ_LOG(gImgLog, LogLevel::Debug,
2515 ("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
2516 " [request=%p]\n",
2517 this, request.get()));
2519 nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(newChannel));
2520 if (cos) {
2521 if (aUseUrgentStartForChannel && !aLinkPreload) {
2522 cos->AddClassFlags(nsIClassOfService::UrgentStart);
2525 if (StaticPrefs::network_http_tailing_enabled() &&
2526 aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
2527 cos->AddClassFlags(nsIClassOfService::Throttleable |
2528 nsIClassOfService::Tail);
2529 nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(newChannel));
2530 if (httpChannel) {
2531 Unused << httpChannel->SetRequestContextID(aRequestContextID);
2536 nsCOMPtr<nsILoadGroup> channelLoadGroup;
2537 newChannel->GetLoadGroup(getter_AddRefs(channelLoadGroup));
2538 rv = request->Init(aURI, aURI, /* aHadInsecureRedirect = */ false,
2539 channelLoadGroup, newChannel, entry, aLoadingDocument,
2540 aTriggeringPrincipal, corsmode, aReferrerInfo);
2541 if (NS_FAILED(rv)) {
2542 return NS_ERROR_FAILURE;
2545 // Add the initiator type for this image load
2546 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(newChannel);
2547 if (timedChannel) {
2548 timedChannel->SetInitiatorType(initiatorType);
2551 // create the proxy listener
2552 nsCOMPtr<nsIStreamListener> listener = new ProxyListener(request.get());
2554 MOZ_LOG(gImgLog, LogLevel::Debug,
2555 ("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
2556 this));
2558 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
2559 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
2560 aLoadGroup);
2562 nsresult openRes;
2563 if (aEarlyHintPreloaderId) {
2564 nsCOMPtr<nsIHttpChannelInternal> channelInternal =
2565 do_QueryInterface(newChannel);
2566 NS_ENSURE_TRUE(channelInternal != nullptr, NS_ERROR_FAILURE);
2568 rv = channelInternal->SetEarlyHintPreloaderId(aEarlyHintPreloaderId);
2569 NS_ENSURE_SUCCESS(rv, rv);
2571 openRes = newChannel->AsyncOpen(listener);
2573 if (NS_FAILED(openRes)) {
2574 MOZ_LOG(
2575 gImgLog, LogLevel::Debug,
2576 ("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
2577 "\n",
2578 this, static_cast<uint32_t>(openRes)));
2579 request->CancelAndAbort(openRes);
2580 return openRes;
2583 // Try to add the new request into the cache.
2584 PutIntoCache(key, entry);
2585 } else {
2586 LOG_MSG_WITH_PARAM(gImgLog, "imgLoader::LoadImage |cache hit|", "request",
2587 request);
2590 // If we didn't get a proxy when validating the cache entry, we need to
2591 // create one.
2592 if (!*_retval) {
2593 // ValidateEntry() has three return values: "Is valid," "might be valid --
2594 // validating over network", and "not valid." If we don't have a _retval,
2595 // we know ValidateEntry is not validating over the network, so it's safe
2596 // to SetLoadId here because we know this request is valid for this context.
2598 // Note, however, that this doesn't guarantee the behaviour we want (one
2599 // URL maps to the same image on a page) if we load the same image in a
2600 // different tab (see bug 528003), because its load id will get re-set, and
2601 // that'll cause us to validate over the network.
2602 request->SetLoadId(aLoadingDocument);
2604 LOG_MSG(gImgLog, "imgLoader::LoadImage", "creating proxy request.");
2605 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
2606 aObserver, requestFlags, _retval);
2607 if (NS_FAILED(rv)) {
2608 return rv;
2611 imgRequestProxy* proxy = *_retval;
2613 // Make sure that OnStatus/OnProgress calls have the right request set, if
2614 // we did create a channel here.
2615 if (newChannel) {
2616 nsCOMPtr<nsIInterfaceRequestor> requestor(
2617 new nsProgressNotificationProxy(newChannel, proxy));
2618 if (!requestor) {
2619 return NS_ERROR_OUT_OF_MEMORY;
2621 newChannel->SetNotificationCallbacks(requestor);
2624 if (aLinkPreload) {
2625 MOZ_ASSERT(aLoadingDocument);
2626 proxy->PrioritizeAsPreload();
2627 auto preloadKey =
2628 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, corsmode);
2629 proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
2632 // Note that it's OK to add here even if the request is done. If it is,
2633 // it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
2634 // the proxy will be removed from the loadgroup.
2635 proxy->AddToLoadGroup();
2637 // If we're loading off the network, explicitly don't notify our proxy,
2638 // because necko (or things called from necko, such as imgCacheValidator)
2639 // are going to call our notifications asynchronously, and we can't make it
2640 // further asynchronous because observers might rely on imagelib completing
2641 // its work between the channel's OnStartRequest and OnStopRequest.
2642 if (!newChannel) {
2643 proxy->NotifyListener();
2646 return rv;
2649 NS_ASSERTION(*_retval, "imgLoader::LoadImage -- no return value");
2651 return NS_OK;
2654 NS_IMETHODIMP
2655 imgLoader::LoadImageWithChannelXPCOM(nsIChannel* channel,
2656 imgINotificationObserver* aObserver,
2657 Document* aLoadingDocument,
2658 nsIStreamListener** listener,
2659 imgIRequest** _retval) {
2660 nsresult result;
2661 imgRequestProxy* proxy;
2662 result = LoadImageWithChannel(channel, aObserver, aLoadingDocument, listener,
2663 &proxy);
2664 *_retval = proxy;
2665 return result;
2668 nsresult imgLoader::LoadImageWithChannel(nsIChannel* channel,
2669 imgINotificationObserver* aObserver,
2670 Document* aLoadingDocument,
2671 nsIStreamListener** listener,
2672 imgRequestProxy** _retval) {
2673 NS_ASSERTION(channel,
2674 "imgLoader::LoadImageWithChannel -- NULL channel pointer");
2676 MOZ_ASSERT(NS_UsePrivateBrowsing(channel) == mRespectPrivacy);
2678 auto makeStaticIfNeeded = mozilla::MakeScopeExit(
2679 [&] { MakeRequestStaticIfNeeded(aLoadingDocument, _retval); });
2681 LOG_SCOPE(gImgLog, "imgLoader::LoadImageWithChannel");
2682 RefPtr<imgRequest> request;
2684 nsCOMPtr<nsIURI> uri;
2685 channel->GetURI(getter_AddRefs(uri));
2687 NS_ENSURE_TRUE(channel, NS_ERROR_FAILURE);
2688 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2690 OriginAttributes attrs = loadInfo->GetOriginAttributes();
2692 ImageCacheKey key(uri, attrs, aLoadingDocument);
2694 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2695 channel->GetLoadFlags(&requestFlags);
2697 RefPtr<imgCacheEntry> entry;
2699 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2700 RemoveFromCache(key);
2701 } else {
2702 // Look in the cache for our URI, and then validate it.
2703 // XXX For now ignore aCacheKey. We will need it in the future
2704 // for correctly dealing with image load requests that are a result
2705 // of post data.
2706 imgCacheTable& cache = GetCache(key);
2707 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
2708 // We don't want to kick off another network load. So we ask
2709 // ValidateEntry to only do validation without creating a new proxy. If
2710 // it says that the entry isn't valid any more, we'll only use the entry
2711 // we're getting if the channel is loading from the cache anyways.
2713 // XXX -- should this be changed? it's pretty much verbatim from the old
2714 // code, but seems nonsensical.
2716 // Since aCanMakeNewChannel == false, we don't need to pass content policy
2717 // type/principal/etc
2719 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2720 // if there is a loadInfo, use the right contentType, otherwise
2721 // default to the internal image type
2722 nsContentPolicyType policyType = loadInfo->InternalContentPolicyType();
2724 if (ValidateEntry(entry, uri, nullptr, nullptr, nullptr, aObserver,
2725 aLoadingDocument, requestFlags, policyType, false,
2726 nullptr, nullptr, nullptr, CORS_NONE, false)) {
2727 request = entry->GetRequest();
2728 } else {
2729 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(channel));
2730 bool bUseCacheCopy;
2732 if (cacheChan) {
2733 cacheChan->IsFromCache(&bUseCacheCopy);
2734 } else {
2735 bUseCacheCopy = false;
2738 if (!bUseCacheCopy) {
2739 entry = nullptr;
2740 } else {
2741 request = entry->GetRequest();
2745 if (request && entry) {
2746 // If this entry has no proxies, its request has no reference to
2747 // the entry.
2748 if (entry->HasNoProxies()) {
2749 LOG_FUNC_WITH_PARAM(
2750 gImgLog,
2751 "imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
2752 key.URI());
2753 MOZ_ASSERT(!request->HasCacheEntry(),
2754 "Proxyless entry's request has cache entry!");
2755 request->SetCacheEntry(entry);
2757 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2758 mCacheTracker->MarkUsed(entry);
2765 nsCOMPtr<nsILoadGroup> loadGroup;
2766 channel->GetLoadGroup(getter_AddRefs(loadGroup));
2768 #ifdef DEBUG
2769 if (aLoadingDocument) {
2770 // The load group of the channel should always match that of the
2771 // document if given. If that isn't the case, then we need to add more
2772 // plumbing to ensure we block the document as well.
2773 nsCOMPtr<nsILoadGroup> docLoadGroup =
2774 aLoadingDocument->GetDocumentLoadGroup();
2775 MOZ_ASSERT(docLoadGroup == loadGroup);
2777 #endif
2779 // Filter out any load flags not from nsIRequest
2780 requestFlags &= nsIRequest::LOAD_REQUESTMASK;
2782 nsresult rv = NS_OK;
2783 if (request) {
2784 // we have this in our cache already.. cancel the current (document) load
2786 // this should fire an OnStopRequest
2787 channel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
2789 *listener = nullptr; // give them back a null nsIStreamListener
2791 rv = CreateNewProxyForRequest(request, uri, loadGroup, aLoadingDocument,
2792 aObserver, requestFlags, _retval);
2793 static_cast<imgRequestProxy*>(*_retval)->NotifyListener();
2794 } else {
2795 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2796 nsCOMPtr<nsIURI> originalURI;
2797 channel->GetOriginalURI(getter_AddRefs(originalURI));
2799 // XXX(seth): We should be able to just use |key| here, except that |key| is
2800 // constructed above with the *current URI* and not the *original URI*. I'm
2801 // pretty sure this is a bug, and it's preventing us from ever getting a
2802 // cache hit in LoadImageWithChannel when redirects are involved.
2803 ImageCacheKey originalURIKey(originalURI, attrs, aLoadingDocument);
2805 // Default to doing a principal check because we don't know who
2806 // started that load and whether their principal ended up being
2807 // inherited on the channel.
2808 NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
2809 originalURIKey, getter_AddRefs(request),
2810 getter_AddRefs(entry));
2812 // No principal specified here, because we're not passed one.
2813 // In LoadImageWithChannel, the redirects that may have been
2814 // associated with this load would have gone through necko.
2815 // We only have the final URI in ImageLib and hence don't know
2816 // if the request went through insecure redirects. But if it did,
2817 // the necko cache should have handled that (since all necko cache hits
2818 // including the redirects will go through content policy). Hence, we
2819 // can set aHadInsecureRedirect to false here.
2820 rv = request->Init(originalURI, uri, /* aHadInsecureRedirect = */ false,
2821 channel, channel, entry, aLoadingDocument, nullptr,
2822 CORS_NONE, nullptr);
2823 NS_ENSURE_SUCCESS(rv, rv);
2825 RefPtr<ProxyListener> pl =
2826 new ProxyListener(static_cast<nsIStreamListener*>(request.get()));
2827 pl.forget(listener);
2829 // Try to add the new request into the cache.
2830 PutIntoCache(originalURIKey, entry);
2832 rv = CreateNewProxyForRequest(request, originalURI, loadGroup,
2833 aLoadingDocument, aObserver, requestFlags,
2834 _retval);
2836 // Explicitly don't notify our proxy, because we're loading off the
2837 // network, and necko (or things called from necko, such as
2838 // imgCacheValidator) are going to call our notifications asynchronously,
2839 // and we can't make it further asynchronous because observers might rely
2840 // on imagelib completing its work between the channel's OnStartRequest and
2841 // OnStopRequest.
2844 if (NS_FAILED(rv)) {
2845 return rv;
2848 (*_retval)->AddToLoadGroup();
2849 return rv;
2852 bool imgLoader::SupportImageWithMimeType(const nsACString& aMimeType,
2853 AcceptedMimeTypes aAccept
2854 /* = AcceptedMimeTypes::IMAGES */) {
2855 nsAutoCString mimeType(aMimeType);
2856 ToLowerCase(mimeType);
2858 if (aAccept == AcceptedMimeTypes::IMAGES_AND_DOCUMENTS &&
2859 mimeType.EqualsLiteral("image/svg+xml")) {
2860 return true;
2863 DecoderType type = DecoderFactory::GetDecoderType(mimeType.get());
2864 return type != DecoderType::UNKNOWN;
2867 NS_IMETHODIMP
2868 imgLoader::GetMIMETypeFromContent(nsIRequest* aRequest,
2869 const uint8_t* aContents, uint32_t aLength,
2870 nsACString& aContentType) {
2871 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2872 if (channel) {
2873 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2874 if (loadInfo->GetSkipContentSniffing()) {
2875 return NS_ERROR_NOT_AVAILABLE;
2879 nsresult rv =
2880 GetMimeTypeFromContent((const char*)aContents, aLength, aContentType);
2881 if (NS_SUCCEEDED(rv) && channel && XRE_IsParentProcess()) {
2882 if (RefPtr<mozilla::net::nsHttpChannel> httpChannel =
2883 do_QueryObject(channel)) {
2884 // If the image type pattern matching algorithm given bytes does not
2885 // return undefined, then disable the further check and allow the
2886 // response.
2887 httpChannel->DisableIsOpaqueResponseAllowedAfterSniffCheck(
2888 mozilla::net::nsHttpChannel::SnifferType::Image);
2892 return rv;
2895 /* static */
2896 nsresult imgLoader::GetMimeTypeFromContent(const char* aContents,
2897 uint32_t aLength,
2898 nsACString& aContentType) {
2899 nsAutoCString detected;
2901 /* Is it a GIF? */
2902 if (aLength >= 6 &&
2903 (!strncmp(aContents, "GIF87a", 6) || !strncmp(aContents, "GIF89a", 6))) {
2904 aContentType.AssignLiteral(IMAGE_GIF);
2906 /* or a PNG? */
2907 } else if (aLength >= 8 && ((unsigned char)aContents[0] == 0x89 &&
2908 (unsigned char)aContents[1] == 0x50 &&
2909 (unsigned char)aContents[2] == 0x4E &&
2910 (unsigned char)aContents[3] == 0x47 &&
2911 (unsigned char)aContents[4] == 0x0D &&
2912 (unsigned char)aContents[5] == 0x0A &&
2913 (unsigned char)aContents[6] == 0x1A &&
2914 (unsigned char)aContents[7] == 0x0A)) {
2915 aContentType.AssignLiteral(IMAGE_PNG);
2917 /* maybe a JPEG (JFIF)? */
2918 /* JFIF files start with SOI APP0 but older files can start with SOI DQT
2919 * so we test for SOI followed by any marker, i.e. FF D8 FF
2920 * this will also work for SPIFF JPEG files if they appear in the future.
2922 * (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
2924 } else if (aLength >= 3 && ((unsigned char)aContents[0]) == 0xFF &&
2925 ((unsigned char)aContents[1]) == 0xD8 &&
2926 ((unsigned char)aContents[2]) == 0xFF) {
2927 aContentType.AssignLiteral(IMAGE_JPEG);
2929 /* or how about ART? */
2930 /* ART begins with JG (4A 47). Major version offset 2.
2931 * Minor version offset 3. Offset 4 must be nullptr.
2933 } else if (aLength >= 5 && ((unsigned char)aContents[0]) == 0x4a &&
2934 ((unsigned char)aContents[1]) == 0x47 &&
2935 ((unsigned char)aContents[4]) == 0x00) {
2936 aContentType.AssignLiteral(IMAGE_ART);
2938 } else if (aLength >= 2 && !strncmp(aContents, "BM", 2)) {
2939 aContentType.AssignLiteral(IMAGE_BMP);
2941 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
2942 // CURs begin with 2-byte 0 followed by 2-byte 2.
2943 } else if (aLength >= 4 && (!memcmp(aContents, "\000\000\001\000", 4) ||
2944 !memcmp(aContents, "\000\000\002\000", 4))) {
2945 aContentType.AssignLiteral(IMAGE_ICO);
2947 // WebPs always begin with RIFF, a 32-bit length, and WEBP.
2948 } else if (aLength >= 12 && !memcmp(aContents, "RIFF", 4) &&
2949 !memcmp(aContents + 8, "WEBP", 4)) {
2950 aContentType.AssignLiteral(IMAGE_WEBP);
2952 } else if (MatchesMP4(reinterpret_cast<const uint8_t*>(aContents), aLength,
2953 detected) &&
2954 detected.Equals(IMAGE_AVIF)) {
2955 aContentType.AssignLiteral(IMAGE_AVIF);
2956 } else if ((aLength >= 2 && !memcmp(aContents, "\xFF\x0A", 2)) ||
2957 (aLength >= 12 &&
2958 !memcmp(aContents, "\x00\x00\x00\x0CJXL \x0D\x0A\x87\x0A", 12))) {
2959 // Each version is for containerless and containerful files respectively.
2960 aContentType.AssignLiteral(IMAGE_JXL);
2961 } else {
2962 /* none of the above? I give up */
2963 return NS_ERROR_NOT_AVAILABLE;
2966 return NS_OK;
2970 * proxy stream listener class used to handle multipart/x-mixed-replace
2973 #include "nsIRequest.h"
2974 #include "nsIStreamConverterService.h"
2976 NS_IMPL_ISUPPORTS(ProxyListener, nsIStreamListener,
2977 nsIThreadRetargetableStreamListener, nsIRequestObserver)
2979 ProxyListener::ProxyListener(nsIStreamListener* dest) : mDestListener(dest) {
2980 /* member initializers and constructor code */
2983 ProxyListener::~ProxyListener() { /* destructor code */
2986 /** nsIRequestObserver methods **/
2988 NS_IMETHODIMP
2989 ProxyListener::OnStartRequest(nsIRequest* aRequest) {
2990 if (!mDestListener) {
2991 return NS_ERROR_FAILURE;
2994 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2995 if (channel) {
2996 // We need to set the initiator type for the image load
2997 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(channel);
2998 if (timedChannel) {
2999 nsAutoString type;
3000 timedChannel->GetInitiatorType(type);
3001 if (type.IsEmpty()) {
3002 timedChannel->SetInitiatorType(u"img"_ns);
3006 nsAutoCString contentType;
3007 nsresult rv = channel->GetContentType(contentType);
3009 if (!contentType.IsEmpty()) {
3010 /* If multipart/x-mixed-replace content, we'll insert a MIME decoder
3011 in the pipeline to handle the content and pass it along to our
3012 original listener.
3014 if ("multipart/x-mixed-replace"_ns.Equals(contentType)) {
3015 nsCOMPtr<nsIStreamConverterService> convServ(
3016 do_GetService("@mozilla.org/streamConverters;1", &rv));
3017 if (NS_SUCCEEDED(rv)) {
3018 nsCOMPtr<nsIStreamListener> toListener(mDestListener);
3019 nsCOMPtr<nsIStreamListener> fromListener;
3021 rv = convServ->AsyncConvertData("multipart/x-mixed-replace", "*/*",
3022 toListener, nullptr,
3023 getter_AddRefs(fromListener));
3024 if (NS_SUCCEEDED(rv)) {
3025 mDestListener = fromListener;
3032 return mDestListener->OnStartRequest(aRequest);
3035 NS_IMETHODIMP
3036 ProxyListener::OnStopRequest(nsIRequest* aRequest, nsresult status) {
3037 if (!mDestListener) {
3038 return NS_ERROR_FAILURE;
3041 return mDestListener->OnStopRequest(aRequest, status);
3044 /** nsIStreamListener methods **/
3046 NS_IMETHODIMP
3047 ProxyListener::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
3048 uint64_t sourceOffset, uint32_t count) {
3049 if (!mDestListener) {
3050 return NS_ERROR_FAILURE;
3053 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
3056 /** nsThreadRetargetableStreamListener methods **/
3057 NS_IMETHODIMP
3058 ProxyListener::CheckListenerChain() {
3059 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3060 nsresult rv = NS_OK;
3061 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3062 do_QueryInterface(mDestListener, &rv);
3063 if (retargetableListener) {
3064 rv = retargetableListener->CheckListenerChain();
3066 MOZ_LOG(
3067 gImgLog, LogLevel::Debug,
3068 ("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
3069 "]",
3070 (NS_SUCCEEDED(rv) ? "success" : "failure"), this,
3071 (nsIStreamListener*)mDestListener, static_cast<uint32_t>(rv)));
3072 return rv;
3076 * http validate class. check a channel for a 304
3079 NS_IMPL_ISUPPORTS(imgCacheValidator, nsIStreamListener, nsIRequestObserver,
3080 nsIThreadRetargetableStreamListener, nsIChannelEventSink,
3081 nsIInterfaceRequestor, nsIAsyncVerifyRedirectCallback)
3083 imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy* progress,
3084 imgLoader* loader, imgRequest* request,
3085 Document* aDocument,
3086 uint64_t aInnerWindowId,
3087 bool forcePrincipalCheckForCacheEntry)
3088 : mProgressProxy(progress),
3089 mRequest(request),
3090 mDocument(aDocument),
3091 mInnerWindowId(aInnerWindowId),
3092 mImgLoader(loader),
3093 mHadInsecureRedirect(false) {
3094 NewRequestAndEntry(forcePrincipalCheckForCacheEntry, loader,
3095 mRequest->CacheKey(), getter_AddRefs(mNewRequest),
3096 getter_AddRefs(mNewEntry));
3099 imgCacheValidator::~imgCacheValidator() {
3100 if (mRequest) {
3101 // If something went wrong, and we never unblocked the requests waiting on
3102 // validation, now is our last chance. We will cancel the new request and
3103 // switch the waiting proxies to it.
3104 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
3108 void imgCacheValidator::AddProxy(imgRequestProxy* aProxy) {
3109 // aProxy needs to be in the loadgroup since we're validating from
3110 // the network.
3111 aProxy->AddToLoadGroup();
3113 mProxies.AppendElement(aProxy);
3116 void imgCacheValidator::RemoveProxy(imgRequestProxy* aProxy) {
3117 mProxies.RemoveElement(aProxy);
3120 void imgCacheValidator::UpdateProxies(bool aCancelRequest, bool aSyncNotify) {
3121 MOZ_ASSERT(mRequest);
3123 // Clear the validator before updating the proxies. The notifications may
3124 // clone an existing request, and its state could be inconsistent.
3125 mRequest->SetValidator(nullptr);
3126 mRequest = nullptr;
3128 // If an error occurred, we will want to cancel the new request, and make the
3129 // validating proxies point to it. Any proxies still bound to the original
3130 // request which are not validating should remain untouched.
3131 if (aCancelRequest) {
3132 MOZ_ASSERT(mNewRequest);
3133 mNewRequest->CancelAndAbort(NS_BINDING_ABORTED);
3136 // We have finished validating the request, so we can safely take ownership
3137 // of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
3138 // if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
3139 // that any potential notifications should still be suppressed in
3140 // imgRequestProxy::ChangeOwner because we haven't cleared the validating
3141 // flag yet, and thus they will remain deferred.
3142 AutoTArray<RefPtr<imgRequestProxy>, 4> proxies(std::move(mProxies));
3144 for (auto& proxy : proxies) {
3145 // First update the state of all proxies before notifying any of them
3146 // to ensure a consistent state (e.g. in case the notification causes
3147 // other proxies to be touched indirectly.)
3148 MOZ_ASSERT(proxy->IsValidating());
3149 MOZ_ASSERT(proxy->NotificationsDeferred(),
3150 "Proxies waiting on cache validation should be "
3151 "deferring notifications!");
3152 if (mNewRequest) {
3153 proxy->ChangeOwner(mNewRequest);
3155 proxy->ClearValidating();
3158 mNewRequest = nullptr;
3159 mNewEntry = nullptr;
3161 for (auto& proxy : proxies) {
3162 if (aSyncNotify) {
3163 // Notify synchronously, because the caller knows we are already in an
3164 // asynchronously-called function (e.g. OnStartRequest).
3165 proxy->SyncNotifyListener();
3166 } else {
3167 // Notify asynchronously, because the caller does not know our current
3168 // call state (e.g. ~imgCacheValidator).
3169 proxy->NotifyListener();
3174 /** nsIRequestObserver methods **/
3176 NS_IMETHODIMP
3177 imgCacheValidator::OnStartRequest(nsIRequest* aRequest) {
3178 // We may be holding on to a document, so ensure that it's released.
3179 RefPtr<Document> document = mDocument.forget();
3181 // If for some reason we don't still have an existing request (probably
3182 // because OnStartRequest got delivered more than once), just bail.
3183 if (!mRequest) {
3184 MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
3185 aRequest->CancelWithReason(NS_BINDING_ABORTED,
3186 "OnStartRequest delivered more than once?"_ns);
3187 return NS_ERROR_FAILURE;
3190 // If this request is coming from cache and has the same URI as our
3191 // imgRequest, the request all our proxies are pointing at is valid, and all
3192 // we have to do is tell them to notify their listeners.
3193 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(aRequest));
3194 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
3195 if (cacheChan && channel) {
3196 bool isFromCache = false;
3197 cacheChan->IsFromCache(&isFromCache);
3199 nsCOMPtr<nsIURI> channelURI;
3200 channel->GetURI(getter_AddRefs(channelURI));
3202 nsCOMPtr<nsIURI> finalURI;
3203 mRequest->GetFinalURI(getter_AddRefs(finalURI));
3205 bool sameURI = false;
3206 if (channelURI && finalURI) {
3207 channelURI->Equals(finalURI, &sameURI);
3210 if (isFromCache && sameURI) {
3211 // We don't need to load this any more.
3212 aRequest->CancelWithReason(NS_BINDING_ABORTED,
3213 "imgCacheValidator::OnStartRequest"_ns);
3214 mNewRequest = nullptr;
3216 // Clear the validator before updating the proxies. The notifications may
3217 // clone an existing request, and its state could be inconsistent.
3218 mRequest->SetLoadId(document);
3219 mRequest->SetInnerWindowID(mInnerWindowId);
3220 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3221 return NS_OK;
3225 // We can't load out of cache. We have to create a whole new request for the
3226 // data that's coming in off the channel.
3227 nsCOMPtr<nsIURI> uri;
3228 mRequest->GetURI(getter_AddRefs(uri));
3230 LOG_MSG_WITH_PARAM(gImgLog,
3231 "imgCacheValidator::OnStartRequest creating new request",
3232 "uri", uri);
3234 CORSMode corsmode = mRequest->GetCORSMode();
3235 nsCOMPtr<nsIReferrerInfo> referrerInfo = mRequest->GetReferrerInfo();
3236 nsCOMPtr<nsIPrincipal> triggeringPrincipal =
3237 mRequest->GetTriggeringPrincipal();
3239 // Doom the old request's cache entry
3240 mRequest->RemoveFromCache();
3242 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
3243 nsCOMPtr<nsIURI> originalURI;
3244 channel->GetOriginalURI(getter_AddRefs(originalURI));
3245 nsresult rv = mNewRequest->Init(originalURI, uri, mHadInsecureRedirect,
3246 aRequest, channel, mNewEntry, document,
3247 triggeringPrincipal, corsmode, referrerInfo);
3248 if (NS_FAILED(rv)) {
3249 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
3250 return rv;
3253 mDestListener = new ProxyListener(mNewRequest);
3255 // Try to add the new request into the cache. Note that the entry must be in
3256 // the cache before the proxies' ownership changes, because adding a proxy
3257 // changes the caching behaviour for imgRequests.
3258 mImgLoader->PutIntoCache(mNewRequest->CacheKey(), mNewEntry);
3259 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3260 return mDestListener->OnStartRequest(aRequest);
3263 NS_IMETHODIMP
3264 imgCacheValidator::OnStopRequest(nsIRequest* aRequest, nsresult status) {
3265 // Be sure we've released the document that we may have been holding on to.
3266 mDocument = nullptr;
3268 if (!mDestListener) {
3269 return NS_OK;
3272 return mDestListener->OnStopRequest(aRequest, status);
3275 /** nsIStreamListener methods **/
3277 NS_IMETHODIMP
3278 imgCacheValidator::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
3279 uint64_t sourceOffset, uint32_t count) {
3280 if (!mDestListener) {
3281 // XXX see bug 113959
3282 uint32_t _retval;
3283 inStr->ReadSegments(NS_DiscardSegment, nullptr, count, &_retval);
3284 return NS_OK;
3287 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
3290 /** nsIThreadRetargetableStreamListener methods **/
3292 NS_IMETHODIMP
3293 imgCacheValidator::CheckListenerChain() {
3294 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3295 nsresult rv = NS_OK;
3296 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3297 do_QueryInterface(mDestListener, &rv);
3298 if (retargetableListener) {
3299 rv = retargetableListener->CheckListenerChain();
3301 MOZ_LOG(
3302 gImgLog, LogLevel::Debug,
3303 ("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32 "=%s",
3304 this, static_cast<uint32_t>(rv),
3305 NS_SUCCEEDED(rv) ? "succeeded" : "failed"));
3306 return rv;
3309 /** nsIInterfaceRequestor methods **/
3311 NS_IMETHODIMP
3312 imgCacheValidator::GetInterface(const nsIID& aIID, void** aResult) {
3313 if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
3314 return QueryInterface(aIID, aResult);
3317 return mProgressProxy->GetInterface(aIID, aResult);
3320 // These functions are materially the same as the same functions in imgRequest.
3321 // We duplicate them because we're verifying whether cache loads are necessary,
3322 // not unconditionally loading.
3324 /** nsIChannelEventSink methods **/
3325 NS_IMETHODIMP
3326 imgCacheValidator::AsyncOnChannelRedirect(
3327 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
3328 nsIAsyncVerifyRedirectCallback* callback) {
3329 // Note all cache information we get from the old channel.
3330 mNewRequest->SetCacheValidation(mNewEntry, oldChannel);
3332 // If the previous URI is a non-HTTPS URI, record that fact for later use by
3333 // security code, which needs to know whether there is an insecure load at any
3334 // point in the redirect chain.
3335 nsCOMPtr<nsIURI> oldURI;
3336 bool schemeLocal = false;
3337 if (NS_FAILED(oldChannel->GetURI(getter_AddRefs(oldURI))) ||
3338 NS_FAILED(NS_URIChainHasFlags(
3339 oldURI, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE, &schemeLocal)) ||
3340 (!oldURI->SchemeIs("https") && !oldURI->SchemeIs("chrome") &&
3341 !schemeLocal)) {
3342 mHadInsecureRedirect = true;
3345 // Prepare for callback
3346 mRedirectCallback = callback;
3347 mRedirectChannel = newChannel;
3349 return mProgressProxy->AsyncOnChannelRedirect(oldChannel, newChannel, flags,
3350 this);
3353 NS_IMETHODIMP
3354 imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult) {
3355 // If we've already been told to abort, just do so.
3356 if (NS_FAILED(aResult)) {
3357 mRedirectCallback->OnRedirectVerifyCallback(aResult);
3358 mRedirectCallback = nullptr;
3359 mRedirectChannel = nullptr;
3360 return NS_OK;
3363 // make sure we have a protocol that returns data rather than opens
3364 // an external application, e.g. mailto:
3365 nsCOMPtr<nsIURI> uri;
3366 mRedirectChannel->GetURI(getter_AddRefs(uri));
3367 bool doesNotReturnData = false;
3368 NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA,
3369 &doesNotReturnData);
3371 nsresult result = NS_OK;
3373 if (doesNotReturnData) {
3374 result = NS_ERROR_ABORT;
3377 mRedirectCallback->OnRedirectVerifyCallback(result);
3378 mRedirectCallback = nullptr;
3379 mRedirectChannel = nullptr;
3380 return NS_OK;