Bug 1784772 - Queue SwipeEventQueue only if we don't know whether the pan event wasn...
[gecko.git] / image / imgLoader.cpp
blob7a6562908a0f5337f2fa3f1d7d5a88ae976f9030
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, uint64_t aEarlyHintPreloaderId) {
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));
950 if (aEarlyHintPreloaderId) {
951 rv = httpChannelInternal->SetEarlyHintPreloaderId(aEarlyHintPreloaderId);
952 NS_ENSURE_SUCCESS(rv, rv);
956 // Image channels are loaded by default with reduced priority.
957 nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(*aResult);
958 if (p) {
959 uint32_t priority = nsISupportsPriority::PRIORITY_LOW;
961 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
962 ++priority; // further reduce priority for background loads
965 p->AdjustPriority(priority);
968 // Create a new loadgroup for this new channel, using the old group as
969 // the parent. The indirection keeps the channel insulated from cancels,
970 // but does allow a way for this revalidation to be associated with at
971 // least one base load group for scheduling/caching purposes.
973 nsCOMPtr<nsILoadGroup> loadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID);
974 nsCOMPtr<nsILoadGroupChild> childLoadGroup = do_QueryInterface(loadGroup);
975 if (childLoadGroup) {
976 childLoadGroup->SetParentLoadGroup(aLoadGroup);
978 (*aResult)->SetLoadGroup(loadGroup);
980 return NS_OK;
983 static uint32_t SecondsFromPRTime(PRTime aTime) {
984 return nsContentUtils::SecondsFromPRTime(aTime);
987 /* static */
988 imgCacheEntry::imgCacheEntry(imgLoader* loader, imgRequest* request,
989 bool forcePrincipalCheck)
990 : mLoader(loader),
991 mRequest(request),
992 mDataSize(0),
993 mTouchedTime(SecondsFromPRTime(PR_Now())),
994 mLoadTime(SecondsFromPRTime(PR_Now())),
995 mExpiryTime(0),
996 mMustValidate(false),
997 // We start off as evicted so we don't try to update the cache.
998 // PutIntoCache will set this to false.
999 mEvicted(true),
1000 mHasNoProxies(true),
1001 mForcePrincipalCheck(forcePrincipalCheck),
1002 mHasNotified(false) {}
1004 imgCacheEntry::~imgCacheEntry() {
1005 LOG_FUNC(gImgLog, "imgCacheEntry::~imgCacheEntry()");
1008 void imgCacheEntry::Touch(bool updateTime /* = true */) {
1009 LOG_SCOPE(gImgLog, "imgCacheEntry::Touch");
1011 if (updateTime) {
1012 mTouchedTime = SecondsFromPRTime(PR_Now());
1015 UpdateCache();
1018 void imgCacheEntry::UpdateCache(int32_t diff /* = 0 */) {
1019 // Don't update the cache if we've been removed from it or it doesn't care
1020 // about our size or usage.
1021 if (!Evicted() && HasNoProxies()) {
1022 mLoader->CacheEntriesChanged(mRequest->IsChrome(), diff);
1026 void imgCacheEntry::UpdateLoadTime() {
1027 mLoadTime = SecondsFromPRTime(PR_Now());
1030 void imgCacheEntry::SetHasNoProxies(bool hasNoProxies) {
1031 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1032 if (hasNoProxies) {
1033 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies true", "uri",
1034 mRequest->CacheKey().URI());
1035 } else {
1036 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies false",
1037 "uri", mRequest->CacheKey().URI());
1041 mHasNoProxies = hasNoProxies;
1044 imgCacheQueue::imgCacheQueue() : mDirty(false), mSize(0) {}
1046 void imgCacheQueue::UpdateSize(int32_t diff) { mSize += diff; }
1048 uint32_t imgCacheQueue::GetSize() const { return mSize; }
1050 void imgCacheQueue::Remove(imgCacheEntry* entry) {
1051 uint64_t index = mQueue.IndexOf(entry);
1052 if (index == queueContainer::NoIndex) {
1053 return;
1056 mSize -= mQueue[index]->GetDataSize();
1058 // If the queue is clean and this is the first entry,
1059 // then we can efficiently remove the entry without
1060 // dirtying the sort order.
1061 if (!IsDirty() && index == 0) {
1062 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1063 mQueue.RemoveLastElement();
1064 return;
1067 // Remove from the middle of the list. This potentially
1068 // breaks the binary heap sort order.
1069 mQueue.RemoveElementAt(index);
1071 // If we only have one entry or the queue is empty, though,
1072 // then the sort order is still effectively good. Simply
1073 // refresh the list to clear the dirty flag.
1074 if (mQueue.Length() <= 1) {
1075 Refresh();
1076 return;
1079 // Otherwise we must mark the queue dirty and potentially
1080 // trigger an expensive sort later.
1081 MarkDirty();
1084 void imgCacheQueue::Push(imgCacheEntry* entry) {
1085 mSize += entry->GetDataSize();
1087 RefPtr<imgCacheEntry> refptr(entry);
1088 mQueue.AppendElement(std::move(refptr));
1089 // If we're not dirty already, then we can efficiently add this to the
1090 // binary heap immediately. This is only O(log n).
1091 if (!IsDirty()) {
1092 std::push_heap(mQueue.begin(), mQueue.end(),
1093 imgLoader::CompareCacheEntries);
1097 already_AddRefed<imgCacheEntry> imgCacheQueue::Pop() {
1098 if (mQueue.IsEmpty()) {
1099 return nullptr;
1101 if (IsDirty()) {
1102 Refresh();
1105 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1106 RefPtr<imgCacheEntry> entry = mQueue.PopLastElement();
1108 mSize -= entry->GetDataSize();
1109 return entry.forget();
1112 void imgCacheQueue::Refresh() {
1113 // Resort the list. This is an O(3 * n) operation and best avoided
1114 // if possible.
1115 std::make_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1116 mDirty = false;
1119 void imgCacheQueue::MarkDirty() { mDirty = true; }
1121 bool imgCacheQueue::IsDirty() { return mDirty; }
1123 uint32_t imgCacheQueue::GetNumElements() const { return mQueue.Length(); }
1125 bool imgCacheQueue::Contains(imgCacheEntry* aEntry) const {
1126 return mQueue.Contains(aEntry);
1129 imgCacheQueue::iterator imgCacheQueue::begin() { return mQueue.begin(); }
1131 imgCacheQueue::const_iterator imgCacheQueue::begin() const {
1132 return mQueue.begin();
1135 imgCacheQueue::iterator imgCacheQueue::end() { return mQueue.end(); }
1137 imgCacheQueue::const_iterator imgCacheQueue::end() const {
1138 return mQueue.end();
1141 nsresult imgLoader::CreateNewProxyForRequest(
1142 imgRequest* aRequest, nsIURI* aURI, nsILoadGroup* aLoadGroup,
1143 Document* aLoadingDocument, imgINotificationObserver* aObserver,
1144 nsLoadFlags aLoadFlags, imgRequestProxy** _retval) {
1145 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::CreateNewProxyForRequest",
1146 "imgRequest", aRequest);
1148 /* XXX If we move decoding onto separate threads, we should save off the
1149 calling thread here and pass it off to |proxyRequest| so that it call
1150 proxy calls to |aObserver|.
1153 RefPtr<imgRequestProxy> proxyRequest = new imgRequestProxy();
1155 /* It is important to call |SetLoadFlags()| before calling |Init()| because
1156 |Init()| adds the request to the loadgroup.
1158 proxyRequest->SetLoadFlags(aLoadFlags);
1160 // init adds itself to imgRequest's list of observers
1161 nsresult rv = proxyRequest->Init(aRequest, aLoadGroup, aLoadingDocument, aURI,
1162 aObserver);
1163 if (NS_WARN_IF(NS_FAILED(rv))) {
1164 return rv;
1167 proxyRequest.forget(_retval);
1168 return NS_OK;
1171 class imgCacheExpirationTracker final
1172 : public nsExpirationTracker<imgCacheEntry, 3> {
1173 enum { TIMEOUT_SECONDS = 10 };
1175 public:
1176 imgCacheExpirationTracker();
1178 protected:
1179 void NotifyExpired(imgCacheEntry* entry) override;
1182 imgCacheExpirationTracker::imgCacheExpirationTracker()
1183 : nsExpirationTracker<imgCacheEntry, 3>(TIMEOUT_SECONDS * 1000,
1184 "imgCacheExpirationTracker") {}
1186 void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry* entry) {
1187 // Hold on to a reference to this entry, because the expiration tracker
1188 // mechanism doesn't.
1189 RefPtr<imgCacheEntry> kungFuDeathGrip(entry);
1191 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1192 RefPtr<imgRequest> req = entry->GetRequest();
1193 if (req) {
1194 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheExpirationTracker::NotifyExpired",
1195 "entry", req->CacheKey().URI());
1199 // We can be called multiple times on the same entry. Don't do work multiple
1200 // times.
1201 if (!entry->Evicted()) {
1202 entry->Loader()->RemoveFromCache(entry);
1205 entry->Loader()->VerifyCacheSizes();
1208 ///////////////////////////////////////////////////////////////////////////////
1209 // imgLoader
1210 ///////////////////////////////////////////////////////////////////////////////
1212 double imgLoader::sCacheTimeWeight;
1213 uint32_t imgLoader::sCacheMaxSize;
1214 imgMemoryReporter* imgLoader::sMemReporter;
1216 NS_IMPL_ISUPPORTS(imgLoader, imgILoader, nsIContentSniffer, imgICache,
1217 nsISupportsWeakReference, nsIObserver)
1219 static imgLoader* gNormalLoader = nullptr;
1220 static imgLoader* gPrivateBrowsingLoader = nullptr;
1222 /* static */
1223 already_AddRefed<imgLoader> imgLoader::CreateImageLoader() {
1224 // In some cases, such as xpctests, XPCOM modules are not automatically
1225 // initialized. We need to make sure that our module is initialized before
1226 // we hand out imgLoader instances and code starts using them.
1227 mozilla::image::EnsureModuleInitialized();
1229 RefPtr<imgLoader> loader = new imgLoader();
1230 loader->Init();
1232 return loader.forget();
1235 imgLoader* imgLoader::NormalLoader() {
1236 if (!gNormalLoader) {
1237 gNormalLoader = CreateImageLoader().take();
1239 return gNormalLoader;
1242 imgLoader* imgLoader::PrivateBrowsingLoader() {
1243 if (!gPrivateBrowsingLoader) {
1244 gPrivateBrowsingLoader = CreateImageLoader().take();
1245 gPrivateBrowsingLoader->RespectPrivacyNotifications();
1247 return gPrivateBrowsingLoader;
1250 imgLoader::imgLoader()
1251 : mUncachedImagesMutex("imgLoader::UncachedImages"),
1252 mRespectPrivacy(false) {
1253 sMemReporter->AddRef();
1254 sMemReporter->RegisterLoader(this);
1257 imgLoader::~imgLoader() {
1258 ClearChromeImageCache();
1259 ClearImageCache();
1261 // If there are any of our imgRequest's left they are in the uncached
1262 // images set, so clear their pointer to us.
1263 MutexAutoLock lock(mUncachedImagesMutex);
1264 for (RefPtr<imgRequest> req : mUncachedImages) {
1265 req->ClearLoader();
1268 sMemReporter->UnregisterLoader(this);
1269 sMemReporter->Release();
1272 void imgLoader::VerifyCacheSizes() {
1273 #ifdef DEBUG
1274 if (!mCacheTracker) {
1275 return;
1278 uint32_t cachesize = mCache.Count() + mChromeCache.Count();
1279 uint32_t queuesize =
1280 mCacheQueue.GetNumElements() + mChromeCacheQueue.GetNumElements();
1281 uint32_t trackersize = 0;
1282 for (nsExpirationTracker<imgCacheEntry, 3>::Iterator it(mCacheTracker.get());
1283 it.Next();) {
1284 trackersize++;
1286 MOZ_ASSERT(queuesize == trackersize, "Queue and tracker sizes out of sync!");
1287 MOZ_ASSERT(queuesize <= cachesize, "Queue has more elements than cache!");
1288 #endif
1291 imgLoader::imgCacheTable& imgLoader::GetCache(bool aForChrome) {
1292 return aForChrome ? mChromeCache : mCache;
1295 imgLoader::imgCacheTable& imgLoader::GetCache(const ImageCacheKey& aKey) {
1296 return GetCache(aKey.IsChrome());
1299 imgCacheQueue& imgLoader::GetCacheQueue(bool aForChrome) {
1300 return aForChrome ? mChromeCacheQueue : mCacheQueue;
1303 imgCacheQueue& imgLoader::GetCacheQueue(const ImageCacheKey& aKey) {
1304 return GetCacheQueue(aKey.IsChrome());
1307 void imgLoader::GlobalInit() {
1308 sCacheTimeWeight = StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
1309 int32_t cachesize = StaticPrefs::image_cache_size_AtStartup();
1310 sCacheMaxSize = cachesize > 0 ? cachesize : 0;
1312 sMemReporter = new imgMemoryReporter();
1313 RegisterStrongAsyncMemoryReporter(sMemReporter);
1314 RegisterImagesContentUsedUncompressedDistinguishedAmount(
1315 imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount);
1318 void imgLoader::ShutdownMemoryReporter() {
1319 UnregisterImagesContentUsedUncompressedDistinguishedAmount();
1320 UnregisterStrongMemoryReporter(sMemReporter);
1323 nsresult imgLoader::InitCache() {
1324 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
1325 if (!os) {
1326 return NS_ERROR_FAILURE;
1329 os->AddObserver(this, "memory-pressure", false);
1330 os->AddObserver(this, "chrome-flush-caches", false);
1331 os->AddObserver(this, "last-pb-context-exited", false);
1332 os->AddObserver(this, "profile-before-change", false);
1333 os->AddObserver(this, "xpcom-shutdown", false);
1335 mCacheTracker = MakeUnique<imgCacheExpirationTracker>();
1337 return NS_OK;
1340 nsresult imgLoader::Init() {
1341 InitCache();
1343 return NS_OK;
1346 NS_IMETHODIMP
1347 imgLoader::RespectPrivacyNotifications() {
1348 mRespectPrivacy = true;
1349 return NS_OK;
1352 NS_IMETHODIMP
1353 imgLoader::Observe(nsISupports* aSubject, const char* aTopic,
1354 const char16_t* aData) {
1355 if (strcmp(aTopic, "memory-pressure") == 0) {
1356 MinimizeCaches();
1357 } else if (strcmp(aTopic, "chrome-flush-caches") == 0) {
1358 MinimizeCaches();
1359 ClearChromeImageCache();
1360 } else if (strcmp(aTopic, "last-pb-context-exited") == 0) {
1361 if (mRespectPrivacy) {
1362 ClearImageCache();
1363 ClearChromeImageCache();
1365 } else if (strcmp(aTopic, "profile-before-change") == 0) {
1366 mCacheTracker = nullptr;
1367 } else if (strcmp(aTopic, "xpcom-shutdown") == 0) {
1368 mCacheTracker = nullptr;
1369 ShutdownMemoryReporter();
1371 } else {
1372 // (Nothing else should bring us here)
1373 MOZ_ASSERT(0, "Invalid topic received");
1376 return NS_OK;
1379 NS_IMETHODIMP
1380 imgLoader::ClearCache(bool chrome) {
1381 if (XRE_IsParentProcess()) {
1382 bool privateLoader = this == gPrivateBrowsingLoader;
1383 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1384 Unused << cp->SendClearImageCache(privateLoader, chrome);
1388 if (chrome) {
1389 return ClearChromeImageCache();
1391 return ClearImageCache();
1394 NS_IMETHODIMP
1395 imgLoader::RemoveEntriesFromPrincipalInAllProcesses(nsIPrincipal* aPrincipal) {
1396 if (!XRE_IsParentProcess()) {
1397 return NS_ERROR_NOT_AVAILABLE;
1400 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1401 Unused << cp->SendClearImageCacheFromPrincipal(aPrincipal);
1404 imgLoader* loader;
1405 if (aPrincipal->OriginAttributesRef().mPrivateBrowsingId ==
1406 nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID) {
1407 loader = imgLoader::NormalLoader();
1408 } else {
1409 loader = imgLoader::PrivateBrowsingLoader();
1412 return loader->RemoveEntriesInternal(aPrincipal, nullptr);
1415 NS_IMETHODIMP
1416 imgLoader::RemoveEntriesFromBaseDomainInAllProcesses(
1417 const nsACString& aBaseDomain) {
1418 if (!XRE_IsParentProcess()) {
1419 return NS_ERROR_NOT_AVAILABLE;
1422 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1423 Unused << cp->SendClearImageCacheFromBaseDomain(aBaseDomain);
1426 return RemoveEntriesInternal(nullptr, &aBaseDomain);
1429 nsresult imgLoader::RemoveEntriesInternal(nsIPrincipal* aPrincipal,
1430 const nsACString* aBaseDomain) {
1431 // Can only clear by either principal or base domain.
1432 if ((!aPrincipal && !aBaseDomain) || (aPrincipal && aBaseDomain)) {
1433 return NS_ERROR_INVALID_ARG;
1436 nsAutoString origin;
1437 if (aPrincipal) {
1438 nsresult rv = nsContentUtils::GetUTFOrigin(aPrincipal, origin);
1439 if (NS_WARN_IF(NS_FAILED(rv))) {
1440 return rv;
1444 nsCOMPtr<nsIEffectiveTLDService> tldService;
1445 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1447 // For base domain we only clear the non-chrome cache.
1448 imgCacheTable& cache =
1449 GetCache(aPrincipal && aPrincipal->IsSystemPrincipal());
1450 for (const auto& entry : cache) {
1451 const auto& key = entry.GetKey();
1453 const bool shouldRemove = [&] {
1454 if (aPrincipal) {
1455 if (key.OriginAttributesRef() !=
1456 BasePrincipal::Cast(aPrincipal)->OriginAttributesRef()) {
1457 return false;
1460 nsAutoString imageOrigin;
1461 nsresult rv = nsContentUtils::GetUTFOrigin(key.URI(), imageOrigin);
1462 if (NS_WARN_IF(NS_FAILED(rv))) {
1463 return false;
1466 return imageOrigin == origin;
1469 if (!aBaseDomain) {
1470 return false;
1472 // Clear by baseDomain.
1473 nsAutoCString host;
1474 nsresult rv = key.URI()->GetHost(host);
1475 if (NS_FAILED(rv) || host.IsEmpty()) {
1476 return false;
1479 if (!tldService) {
1480 tldService = do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
1482 if (NS_WARN_IF(!tldService)) {
1483 return false;
1486 bool hasRootDomain = false;
1487 rv = tldService->HasRootDomain(host, *aBaseDomain, &hasRootDomain);
1488 if (NS_SUCCEEDED(rv) && hasRootDomain) {
1489 return true;
1492 // If we don't get a direct base domain match, also check for cache of
1493 // third parties partitioned under aBaseDomain.
1495 // The isolation key is either just the base domain, or an origin suffix
1496 // which contains the partitionKey holding the baseDomain.
1498 if (key.IsolationKeyRef().Equals(*aBaseDomain)) {
1499 return true;
1502 // The isolation key does not match the given base domain. It may be an
1503 // origin suffix. Parse it into origin attributes.
1504 OriginAttributes attrs;
1505 if (!attrs.PopulateFromSuffix(key.IsolationKeyRef())) {
1506 // Key is not an origin suffix.
1507 return false;
1510 return StoragePrincipalHelper::PartitionKeyHasBaseDomain(
1511 attrs.mPartitionKey, *aBaseDomain);
1512 }();
1514 if (shouldRemove) {
1515 entriesToBeRemoved.AppendElement(entry.GetData());
1519 for (auto& entry : entriesToBeRemoved) {
1520 if (!RemoveFromCache(entry)) {
1521 NS_WARNING(
1522 "Couldn't remove an entry from the cache in "
1523 "RemoveEntriesInternal()\n");
1527 return NS_OK;
1530 NS_IMETHODIMP
1531 imgLoader::RemoveEntry(nsIURI* aURI, Document* aDoc) {
1532 if (aURI) {
1533 OriginAttributes attrs;
1534 if (aDoc) {
1535 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1536 if (principal) {
1537 attrs = principal->OriginAttributesRef();
1541 ImageCacheKey key(aURI, attrs, aDoc);
1542 if (RemoveFromCache(key)) {
1543 return NS_OK;
1546 return NS_ERROR_NOT_AVAILABLE;
1549 NS_IMETHODIMP
1550 imgLoader::FindEntryProperties(nsIURI* uri, Document* aDoc,
1551 nsIProperties** _retval) {
1552 *_retval = nullptr;
1554 OriginAttributes attrs;
1555 if (aDoc) {
1556 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1557 if (principal) {
1558 attrs = principal->OriginAttributesRef();
1562 ImageCacheKey key(uri, attrs, aDoc);
1563 imgCacheTable& cache = GetCache(key);
1565 RefPtr<imgCacheEntry> entry;
1566 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
1567 if (mCacheTracker && entry->HasNoProxies()) {
1568 mCacheTracker->MarkUsed(entry);
1571 RefPtr<imgRequest> request = entry->GetRequest();
1572 if (request) {
1573 nsCOMPtr<nsIProperties> properties = request->Properties();
1574 properties.forget(_retval);
1578 return NS_OK;
1581 NS_IMETHODIMP_(void)
1582 imgLoader::ClearCacheForControlledDocument(Document* aDoc) {
1583 MOZ_ASSERT(aDoc);
1584 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1585 imgCacheTable& cache = GetCache(false);
1586 for (const auto& entry : cache) {
1587 const auto& key = entry.GetKey();
1588 if (key.ControlledDocument() == aDoc) {
1589 entriesToBeRemoved.AppendElement(entry.GetData());
1592 for (auto& entry : entriesToBeRemoved) {
1593 if (!RemoveFromCache(entry)) {
1594 NS_WARNING(
1595 "Couldn't remove an entry from the cache in "
1596 "ClearCacheForControlledDocument()\n");
1601 void imgLoader::Shutdown() {
1602 NS_IF_RELEASE(gNormalLoader);
1603 gNormalLoader = nullptr;
1604 NS_IF_RELEASE(gPrivateBrowsingLoader);
1605 gPrivateBrowsingLoader = nullptr;
1608 nsresult imgLoader::ClearChromeImageCache() {
1609 return EvictEntries(mChromeCache);
1612 nsresult imgLoader::ClearImageCache() { return EvictEntries(mCache); }
1614 void imgLoader::MinimizeCaches() {
1615 EvictEntries(mCacheQueue);
1616 EvictEntries(mChromeCacheQueue);
1619 bool imgLoader::PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* entry) {
1620 imgCacheTable& cache = GetCache(aKey);
1622 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::PutIntoCache", "uri",
1623 aKey.URI());
1625 // Check to see if this request already exists in the cache. If so, we'll
1626 // replace the old version.
1627 RefPtr<imgCacheEntry> tmpCacheEntry;
1628 if (cache.Get(aKey, getter_AddRefs(tmpCacheEntry)) && tmpCacheEntry) {
1629 MOZ_LOG(
1630 gImgLog, LogLevel::Debug,
1631 ("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
1632 nullptr));
1633 RefPtr<imgRequest> tmpRequest = tmpCacheEntry->GetRequest();
1635 // If it already exists, and we're putting the same key into the cache, we
1636 // should remove the old version.
1637 MOZ_LOG(gImgLog, LogLevel::Debug,
1638 ("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
1639 nullptr));
1641 RemoveFromCache(aKey);
1642 } else {
1643 MOZ_LOG(gImgLog, LogLevel::Debug,
1644 ("[this=%p] imgLoader::PutIntoCache --"
1645 " Element NOT already in the cache",
1646 nullptr));
1649 cache.InsertOrUpdate(aKey, RefPtr{entry});
1651 // We can be called to resurrect an evicted entry.
1652 if (entry->Evicted()) {
1653 entry->SetEvicted(false);
1656 // If we're resurrecting an entry with no proxies, put it back in the
1657 // tracker and queue.
1658 if (entry->HasNoProxies()) {
1659 nsresult addrv = NS_OK;
1661 if (mCacheTracker) {
1662 addrv = mCacheTracker->AddObject(entry);
1665 if (NS_SUCCEEDED(addrv)) {
1666 imgCacheQueue& queue = GetCacheQueue(aKey);
1667 queue.Push(entry);
1671 RefPtr<imgRequest> request = entry->GetRequest();
1672 request->SetIsInCache(true);
1673 RemoveFromUncachedImages(request);
1675 return true;
1678 bool imgLoader::SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry) {
1679 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasNoProxies", "uri",
1680 aRequest->CacheKey().URI());
1682 aEntry->SetHasNoProxies(true);
1684 if (aEntry->Evicted()) {
1685 return false;
1688 imgCacheQueue& queue = GetCacheQueue(aRequest->IsChrome());
1690 nsresult addrv = NS_OK;
1692 if (mCacheTracker) {
1693 addrv = mCacheTracker->AddObject(aEntry);
1696 if (NS_SUCCEEDED(addrv)) {
1697 queue.Push(aEntry);
1700 imgCacheTable& cache = GetCache(aRequest->IsChrome());
1701 CheckCacheLimits(cache, queue);
1703 return true;
1706 bool imgLoader::SetHasProxies(imgRequest* aRequest) {
1707 VerifyCacheSizes();
1709 const ImageCacheKey& key = aRequest->CacheKey();
1710 imgCacheTable& cache = GetCache(key);
1712 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasProxies", "uri",
1713 key.URI());
1715 RefPtr<imgCacheEntry> entry;
1716 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
1717 // Make sure the cache entry is for the right request
1718 RefPtr<imgRequest> entryRequest = entry->GetRequest();
1719 if (entryRequest == aRequest && entry->HasNoProxies()) {
1720 imgCacheQueue& queue = GetCacheQueue(key);
1721 queue.Remove(entry);
1723 if (mCacheTracker) {
1724 mCacheTracker->RemoveObject(entry);
1727 entry->SetHasNoProxies(false);
1729 return true;
1733 return false;
1736 void imgLoader::CacheEntriesChanged(bool aForChrome,
1737 int32_t aSizeDiff /* = 0 */) {
1738 imgCacheQueue& queue = GetCacheQueue(aForChrome);
1739 // We only need to dirty the queue if there is any sorting
1740 // taking place. Empty or single-entry lists can't become
1741 // dirty.
1742 if (queue.GetNumElements() > 1) {
1743 queue.MarkDirty();
1745 queue.UpdateSize(aSizeDiff);
1748 void imgLoader::CheckCacheLimits(imgCacheTable& cache, imgCacheQueue& queue) {
1749 if (queue.GetNumElements() == 0) {
1750 NS_ASSERTION(queue.GetSize() == 0,
1751 "imgLoader::CheckCacheLimits -- incorrect cache size");
1754 // Remove entries from the cache until we're back at our desired max size.
1755 while (queue.GetSize() > sCacheMaxSize) {
1756 // Remove the first entry in the queue.
1757 RefPtr<imgCacheEntry> entry(queue.Pop());
1759 NS_ASSERTION(entry, "imgLoader::CheckCacheLimits -- NULL entry pointer");
1761 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1762 RefPtr<imgRequest> req = entry->GetRequest();
1763 if (req) {
1764 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::CheckCacheLimits",
1765 "entry", req->CacheKey().URI());
1769 if (entry) {
1770 // We just popped this entry from the queue, so pass AlreadyRemoved
1771 // to avoid searching the queue again in RemoveFromCache.
1772 RemoveFromCache(entry, QueueState::AlreadyRemoved);
1777 bool imgLoader::ValidateRequestWithNewChannel(
1778 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1779 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1780 imgINotificationObserver* aObserver, Document* aLoadingDocument,
1781 uint64_t aInnerWindowId, nsLoadFlags aLoadFlags,
1782 nsContentPolicyType aLoadPolicyType, imgRequestProxy** aProxyRequest,
1783 nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode, bool aLinkPreload,
1784 uint64_t aEarlyHintPreloaderId, bool* aNewChannelCreated) {
1785 // now we need to insert a new channel request object in between the real
1786 // request and the proxy that basically delays loading the image until it
1787 // gets a 304 or figures out that this needs to be a new request
1789 nsresult rv;
1791 // If we're currently in the middle of validating this request, just hand
1792 // back a proxy to it; the required work will be done for us.
1793 if (imgCacheValidator* validator = request->GetValidator()) {
1794 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
1795 aObserver, aLoadFlags, aProxyRequest);
1796 if (NS_FAILED(rv)) {
1797 return false;
1800 if (*aProxyRequest) {
1801 imgRequestProxy* proxy = static_cast<imgRequestProxy*>(*aProxyRequest);
1803 // We will send notifications from imgCacheValidator::OnStartRequest().
1804 // In the mean time, we must defer notifications because we are added to
1805 // the imgRequest's proxy list, and we can get extra notifications
1806 // resulting from methods such as StartDecoding(). See bug 579122.
1807 proxy->MarkValidating();
1809 if (aLinkPreload) {
1810 MOZ_ASSERT(aLoadingDocument);
1811 proxy->PrioritizeAsPreload();
1812 auto preloadKey = PreloadHashKey::CreateAsImage(
1813 aURI, aTriggeringPrincipal, aCORSMode);
1814 proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
1817 // Attach the proxy without notifying
1818 validator->AddProxy(proxy);
1821 return true;
1823 // We will rely on Necko to cache this request when it's possible, and to
1824 // tell imgCacheValidator::OnStartRequest whether the request came from its
1825 // cache.
1826 nsCOMPtr<nsIChannel> newChannel;
1827 bool forcePrincipalCheck;
1828 rv =
1829 NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
1830 aInitialDocumentURI, aCORSMode, aReferrerInfo, aLoadGroup,
1831 aLoadFlags, aLoadPolicyType, aTriggeringPrincipal,
1832 aLoadingDocument, mRespectPrivacy, aEarlyHintPreloaderId);
1833 if (NS_FAILED(rv)) {
1834 return false;
1837 if (aNewChannelCreated) {
1838 *aNewChannelCreated = true;
1841 RefPtr<imgRequestProxy> req;
1842 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
1843 aObserver, aLoadFlags, getter_AddRefs(req));
1844 if (NS_FAILED(rv)) {
1845 return false;
1848 // Make sure that OnStatus/OnProgress calls have the right request set...
1849 RefPtr<nsProgressNotificationProxy> progressproxy =
1850 new nsProgressNotificationProxy(newChannel, req);
1851 if (!progressproxy) {
1852 return false;
1855 RefPtr<imgCacheValidator> hvc =
1856 new imgCacheValidator(progressproxy, this, request, aLoadingDocument,
1857 aInnerWindowId, forcePrincipalCheck);
1859 // Casting needed here to get past multiple inheritance.
1860 nsCOMPtr<nsIStreamListener> listener =
1861 do_QueryInterface(static_cast<nsIThreadRetargetableStreamListener*>(hvc));
1862 NS_ENSURE_TRUE(listener, false);
1864 // We must set the notification callbacks before setting up the
1865 // CORS listener, because that's also interested inthe
1866 // notification callbacks.
1867 newChannel->SetNotificationCallbacks(hvc);
1869 request->SetValidator(hvc);
1871 // We will send notifications from imgCacheValidator::OnStartRequest().
1872 // In the mean time, we must defer notifications because we are added to
1873 // the imgRequest's proxy list, and we can get extra notifications
1874 // resulting from methods such as StartDecoding(). See bug 579122.
1875 req->MarkValidating();
1877 if (aLinkPreload) {
1878 MOZ_ASSERT(aLoadingDocument);
1879 req->PrioritizeAsPreload();
1880 auto preloadKey =
1881 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, aCORSMode);
1882 req->NotifyOpen(preloadKey, aLoadingDocument, true);
1885 // Add the proxy without notifying
1886 hvc->AddProxy(req);
1888 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
1889 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
1890 aLoadGroup);
1891 rv = newChannel->AsyncOpen(listener);
1892 if (NS_WARN_IF(NS_FAILED(rv))) {
1893 req->CancelAndForgetObserver(rv);
1894 // This will notify any current or future <link preload> tags. Pass the
1895 // non-open channel so that we can read loadinfo and referrer info of that
1896 // channel.
1897 req->NotifyStart(newChannel);
1898 // Use the non-channel overload of this method to force the notification to
1899 // happen. The preload request has not been assigned a channel.
1900 req->NotifyStop(rv);
1901 return false;
1904 req.forget(aProxyRequest);
1905 return true;
1908 void imgLoader::NotifyObserversForCachedImage(
1909 imgCacheEntry* aEntry, imgRequest* request, nsIURI* aURI,
1910 nsIReferrerInfo* aReferrerInfo, Document* aLoadingDocument,
1911 nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode,
1912 uint64_t aEarlyHintPreloaderId) {
1913 if (aEntry->HasNotified()) {
1914 return;
1917 nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
1919 if (!obsService->HasObservers("http-on-image-cache-response")) {
1920 return;
1923 aEntry->SetHasNotified();
1925 nsCOMPtr<nsIChannel> newChannel;
1926 bool forcePrincipalCheck;
1927 nsresult rv = NewImageChannel(
1928 getter_AddRefs(newChannel), &forcePrincipalCheck, aURI, nullptr,
1929 aCORSMode, aReferrerInfo, nullptr, 0,
1930 nsIContentPolicy::TYPE_INTERNAL_IMAGE, aTriggeringPrincipal,
1931 aLoadingDocument, mRespectPrivacy, aEarlyHintPreloaderId);
1932 if (NS_FAILED(rv)) {
1933 return;
1936 RefPtr<HttpBaseChannel> httpBaseChannel = do_QueryObject(newChannel);
1937 if (httpBaseChannel) {
1938 httpBaseChannel->SetDummyChannelForImageCache();
1939 newChannel->SetContentType(nsDependentCString(request->GetMimeType()));
1940 RefPtr<mozilla::image::Image> image = request->GetImage();
1941 if (image) {
1942 newChannel->SetContentLength(aEntry->GetDataSize());
1944 obsService->NotifyObservers(newChannel, "http-on-image-cache-response",
1945 nullptr);
1949 bool imgLoader::ValidateEntry(
1950 imgCacheEntry* aEntry, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1951 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1952 imgINotificationObserver* aObserver, Document* aLoadingDocument,
1953 nsLoadFlags aLoadFlags, nsContentPolicyType aLoadPolicyType,
1954 bool aCanMakeNewChannel, bool* aNewChannelCreated,
1955 imgRequestProxy** aProxyRequest, nsIPrincipal* aTriggeringPrincipal,
1956 CORSMode aCORSMode, bool aLinkPreload, uint64_t aEarlyHintPreloaderId) {
1957 LOG_SCOPE(gImgLog, "imgLoader::ValidateEntry");
1959 // If the expiration time is zero, then the request has not gotten far enough
1960 // to know when it will expire, or we know it will never expire (see
1961 // nsContentUtils::GetSubresourceCacheValidationInfo).
1962 uint32_t expiryTime = aEntry->GetExpiryTime();
1963 bool hasExpired = expiryTime && expiryTime <= SecondsFromPRTime(PR_Now());
1965 nsresult rv;
1967 // Special treatment for file URLs - aEntry has expired if file has changed
1968 nsCOMPtr<nsIFileURL> fileUrl(do_QueryInterface(aURI));
1969 if (fileUrl) {
1970 uint32_t lastModTime = aEntry->GetLoadTime();
1972 nsCOMPtr<nsIFile> theFile;
1973 rv = fileUrl->GetFile(getter_AddRefs(theFile));
1974 if (NS_SUCCEEDED(rv)) {
1975 PRTime fileLastMod;
1976 rv = theFile->GetLastModifiedTime(&fileLastMod);
1977 if (NS_SUCCEEDED(rv)) {
1978 // nsIFile uses millisec, NSPR usec
1979 fileLastMod *= 1000;
1980 hasExpired = SecondsFromPRTime((PRTime)fileLastMod) > lastModTime;
1985 RefPtr<imgRequest> request(aEntry->GetRequest());
1987 if (!request) {
1988 return false;
1991 if (!ValidateSecurityInfo(request, aEntry->ForcePrincipalCheck(), aCORSMode,
1992 aTriggeringPrincipal, aLoadingDocument,
1993 aLoadPolicyType)) {
1994 return false;
1997 // data URIs are immutable and by their nature can't leak data, so we can
1998 // just return true in that case. Doing so would mean that shift-reload
1999 // doesn't reload data URI documents/images though (which is handy for
2000 // debugging during gecko development) so we make an exception in that case.
2001 if (aURI->SchemeIs("data") && !(aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE)) {
2002 return true;
2005 bool validateRequest = false;
2007 if (!request->CanReuseWithoutValidation(aLoadingDocument)) {
2008 // If we would need to revalidate this entry, but we're being told to
2009 // bypass the cache, we don't allow this entry to be used.
2010 if (aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2011 return false;
2014 if (MOZ_UNLIKELY(ChaosMode::isActive(ChaosFeature::ImageCache))) {
2015 if (ChaosMode::randomUint32LessThan(4) < 1) {
2016 return false;
2020 // Determine whether the cache aEntry must be revalidated...
2021 validateRequest = ShouldRevalidateEntry(aEntry, aLoadFlags, hasExpired);
2023 MOZ_LOG(gImgLog, LogLevel::Debug,
2024 ("imgLoader::ValidateEntry validating cache entry. "
2025 "validateRequest = %d",
2026 validateRequest));
2027 } else if (!aLoadingDocument && MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
2028 MOZ_LOG(gImgLog, LogLevel::Debug,
2029 ("imgLoader::ValidateEntry BYPASSING cache validation for %s "
2030 "because of NULL loading document",
2031 aURI->GetSpecOrDefault().get()));
2034 // If the original request is still transferring don't kick off a validation
2035 // network request because it is a bit silly to issue a validation request if
2036 // the original request hasn't even finished yet. So just return true
2037 // indicating the caller can create a new proxy for the request and use it as
2038 // is.
2039 // This is an optimization but it's also required for correctness. If we don't
2040 // do this then when firing the load complete notification for the original
2041 // request that can unblock load for the document and then spin the event loop
2042 // (see the stack in bug 1641682) which then the OnStartRequest for the
2043 // validation request can fire which can call UpdateProxies and can sync
2044 // notify on the progress tracker about all existing state, which includes
2045 // load complete, so we fire a second load complete notification for the
2046 // image.
2047 // In addition, we want to validate if the original request encountered
2048 // an error for two reasons. The first being if the error was a network error
2049 // then trying to re-fetch the image might succeed. The second is more
2050 // complicated. We decide if we should fire the load or error event for img
2051 // elements depending on if the image has error in its status at the time when
2052 // the load complete notification is received, and we set error status on an
2053 // image if it encounters a network error or a decode error with no real way
2054 // to tell them apart. So if we load an image that will produce a decode error
2055 // the first time we will usually fire the load event, and then decode enough
2056 // to encounter the decode error and set the error status on the image. The
2057 // next time we reference the image in the same document the load complete
2058 // notification is replayed and this time the error status from the decode is
2059 // already present so we fire the error event instead of the load event. This
2060 // is a bug (bug 1645576) that we should fix. In order to avoid that bug in
2061 // some cases (specifically the cases when we hit this code and try to
2062 // validate the request) we make sure to validate. This avoids the bug because
2063 // when the load complete notification arrives the proxy is marked as
2064 // validating so it lies about its status and returns nothing.
2065 bool requestComplete = false;
2066 RefPtr<ProgressTracker> tracker;
2067 RefPtr<mozilla::image::Image> image = request->GetImage();
2068 if (image) {
2069 tracker = image->GetProgressTracker();
2070 } else {
2071 tracker = request->GetProgressTracker();
2073 if (tracker) {
2074 if (tracker->GetProgress() & (FLAG_LOAD_COMPLETE | FLAG_HAS_ERROR)) {
2075 requestComplete = true;
2078 if (!requestComplete) {
2079 return true;
2082 if (validateRequest && aCanMakeNewChannel) {
2083 LOG_SCOPE(gImgLog, "imgLoader::ValidateRequest |cache hit| must validate");
2085 uint64_t innerWindowID =
2086 aLoadingDocument ? aLoadingDocument->InnerWindowID() : 0;
2087 return ValidateRequestWithNewChannel(
2088 request, aURI, aInitialDocumentURI, aReferrerInfo, aLoadGroup,
2089 aObserver, aLoadingDocument, innerWindowID, aLoadFlags, aLoadPolicyType,
2090 aProxyRequest, aTriggeringPrincipal, aCORSMode, aLinkPreload,
2091 aEarlyHintPreloaderId, aNewChannelCreated);
2094 if (!validateRequest) {
2095 NotifyObserversForCachedImage(aEntry, request, aURI, aReferrerInfo,
2096 aLoadingDocument, aTriggeringPrincipal,
2097 aCORSMode, aEarlyHintPreloaderId);
2100 return !validateRequest;
2103 bool imgLoader::RemoveFromCache(const ImageCacheKey& aKey) {
2104 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache", "uri",
2105 aKey.URI());
2107 imgCacheTable& cache = GetCache(aKey);
2108 imgCacheQueue& queue = GetCacheQueue(aKey);
2110 RefPtr<imgCacheEntry> entry;
2111 cache.Remove(aKey, getter_AddRefs(entry));
2112 if (entry) {
2113 MOZ_ASSERT(!entry->Evicted(), "Evicting an already-evicted cache entry!");
2115 // Entries with no proxies are in the tracker.
2116 if (entry->HasNoProxies()) {
2117 if (mCacheTracker) {
2118 mCacheTracker->RemoveObject(entry);
2120 queue.Remove(entry);
2123 entry->SetEvicted(true);
2125 RefPtr<imgRequest> request = entry->GetRequest();
2126 request->SetIsInCache(false);
2127 AddToUncachedImages(request);
2129 return true;
2131 return false;
2134 bool imgLoader::RemoveFromCache(imgCacheEntry* entry, QueueState aQueueState) {
2135 LOG_STATIC_FUNC(gImgLog, "imgLoader::RemoveFromCache entry");
2137 RefPtr<imgRequest> request = entry->GetRequest();
2138 if (request) {
2139 const ImageCacheKey& key = request->CacheKey();
2140 imgCacheTable& cache = GetCache(key);
2141 imgCacheQueue& queue = GetCacheQueue(key);
2143 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache",
2144 "entry's uri", key.URI());
2146 cache.Remove(key);
2148 if (entry->HasNoProxies()) {
2149 LOG_STATIC_FUNC(gImgLog,
2150 "imgLoader::RemoveFromCache removing from tracker");
2151 if (mCacheTracker) {
2152 mCacheTracker->RemoveObject(entry);
2154 // Only search the queue to remove the entry if its possible it might
2155 // be in the queue. If we know its not in the queue this would be
2156 // wasted work.
2157 MOZ_ASSERT_IF(aQueueState == QueueState::AlreadyRemoved,
2158 !queue.Contains(entry));
2159 if (aQueueState == QueueState::MaybeExists) {
2160 queue.Remove(entry);
2164 entry->SetEvicted(true);
2165 request->SetIsInCache(false);
2166 AddToUncachedImages(request);
2168 return true;
2171 return false;
2174 nsresult imgLoader::EvictEntries(imgCacheTable& aCacheToClear) {
2175 LOG_STATIC_FUNC(gImgLog, "imgLoader::EvictEntries table");
2177 // We have to make a temporary, since RemoveFromCache removes the element
2178 // from the queue, invalidating iterators.
2179 const auto entries =
2180 ToTArray<nsTArray<RefPtr<imgCacheEntry>>>(aCacheToClear.Values());
2181 for (const auto& entry : entries) {
2182 if (!RemoveFromCache(entry)) {
2183 return NS_ERROR_FAILURE;
2187 MOZ_ASSERT(aCacheToClear.Count() == 0);
2189 return NS_OK;
2192 nsresult imgLoader::EvictEntries(imgCacheQueue& aQueueToClear) {
2193 LOG_STATIC_FUNC(gImgLog, "imgLoader::EvictEntries queue");
2195 // We have to make a temporary, since RemoveFromCache removes the element
2196 // from the queue, invalidating iterators.
2197 nsTArray<RefPtr<imgCacheEntry>> entries(aQueueToClear.GetNumElements());
2198 for (auto i = aQueueToClear.begin(); i != aQueueToClear.end(); ++i) {
2199 entries.AppendElement(*i);
2202 // Iterate in reverse order to minimize array copying.
2203 for (auto& entry : entries) {
2204 if (!RemoveFromCache(entry)) {
2205 return NS_ERROR_FAILURE;
2209 MOZ_ASSERT(aQueueToClear.GetNumElements() == 0);
2211 return NS_OK;
2214 void imgLoader::AddToUncachedImages(imgRequest* aRequest) {
2215 MutexAutoLock lock(mUncachedImagesMutex);
2216 mUncachedImages.Insert(aRequest);
2219 void imgLoader::RemoveFromUncachedImages(imgRequest* aRequest) {
2220 MutexAutoLock lock(mUncachedImagesMutex);
2221 mUncachedImages.Remove(aRequest);
2224 #define LOAD_FLAGS_CACHE_MASK \
2225 (nsIRequest::LOAD_BYPASS_CACHE | nsIRequest::LOAD_FROM_CACHE)
2227 #define LOAD_FLAGS_VALIDATE_MASK \
2228 (nsIRequest::VALIDATE_ALWAYS | nsIRequest::VALIDATE_NEVER | \
2229 nsIRequest::VALIDATE_ONCE_PER_SESSION)
2231 NS_IMETHODIMP
2232 imgLoader::LoadImageXPCOM(
2233 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2234 nsIPrincipal* aTriggeringPrincipal, nsILoadGroup* aLoadGroup,
2235 imgINotificationObserver* aObserver, Document* aLoadingDocument,
2236 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
2237 nsContentPolicyType aContentPolicyType, imgIRequest** _retval) {
2238 // Optional parameter, so defaults to 0 (== TYPE_INVALID)
2239 if (!aContentPolicyType) {
2240 aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE;
2242 imgRequestProxy* proxy;
2243 nsresult rv =
2244 LoadImage(aURI, aInitialDocumentURI, aReferrerInfo, aTriggeringPrincipal,
2245 0, aLoadGroup, aObserver, aLoadingDocument, aLoadingDocument,
2246 aLoadFlags, aCacheKey, aContentPolicyType, u""_ns,
2247 /* aUseUrgentStartForChannel */ false, /* aListPreload */ false,
2248 0, &proxy);
2249 *_retval = proxy;
2250 return rv;
2253 static void MakeRequestStaticIfNeeded(
2254 Document* aLoadingDocument, imgRequestProxy** aProxyAboutToGetReturned) {
2255 if (!aLoadingDocument || !aLoadingDocument->IsStaticDocument()) {
2256 return;
2259 if (!*aProxyAboutToGetReturned) {
2260 return;
2263 RefPtr<imgRequestProxy> proxy = dont_AddRef(*aProxyAboutToGetReturned);
2264 *aProxyAboutToGetReturned = nullptr;
2266 RefPtr<imgRequestProxy> staticProxy =
2267 proxy->GetStaticRequest(aLoadingDocument);
2268 if (staticProxy != proxy) {
2269 proxy->CancelAndForgetObserver(NS_BINDING_ABORTED);
2270 proxy = std::move(staticProxy);
2272 proxy.forget(aProxyAboutToGetReturned);
2275 bool imgLoader::IsImageAvailable(nsIURI* aURI,
2276 nsIPrincipal* aTriggeringPrincipal,
2277 CORSMode aCORSMode, Document* aDocument) {
2278 ImageCacheKey key(aURI, aTriggeringPrincipal->OriginAttributesRef(),
2279 aDocument);
2280 RefPtr<imgCacheEntry> entry;
2281 imgCacheTable& cache = GetCache(key);
2282 if (!cache.Get(key, getter_AddRefs(entry)) || !entry) {
2283 return false;
2285 RefPtr<imgRequest> request = entry->GetRequest();
2286 if (!request) {
2287 return false;
2289 if (nsCOMPtr<nsILoadGroup> docLoadGroup = aDocument->GetDocumentLoadGroup()) {
2290 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2291 docLoadGroup->GetLoadFlags(&requestFlags);
2292 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2293 // If we're bypassing the cache, treat the image as not available.
2294 return false;
2297 return ValidateCORSMode(request, false, aCORSMode, aTriggeringPrincipal);
2300 nsresult imgLoader::LoadImage(
2301 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2302 nsIPrincipal* aTriggeringPrincipal, uint64_t aRequestContextID,
2303 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
2304 nsINode* aContext, Document* aLoadingDocument, nsLoadFlags aLoadFlags,
2305 nsISupports* aCacheKey, nsContentPolicyType aContentPolicyType,
2306 const nsAString& initiatorType, bool aUseUrgentStartForChannel,
2307 bool aLinkPreload, uint64_t aEarlyHintPreloaderId,
2308 imgRequestProxy** _retval) {
2309 VerifyCacheSizes();
2311 NS_ASSERTION(aURI, "imgLoader::LoadImage -- NULL URI pointer");
2313 if (!aURI) {
2314 return NS_ERROR_NULL_POINTER;
2317 auto makeStaticIfNeeded = mozilla::MakeScopeExit(
2318 [&] { MakeRequestStaticIfNeeded(aLoadingDocument, _retval); });
2320 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK,
2321 aURI->GetSpecOrDefault());
2323 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::LoadImage", "aURI", aURI);
2325 *_retval = nullptr;
2327 RefPtr<imgRequest> request;
2329 nsresult rv;
2330 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2332 #ifdef DEBUG
2333 bool isPrivate = false;
2335 if (aLoadingDocument) {
2336 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadingDocument);
2337 } else if (aLoadGroup) {
2338 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadGroup);
2340 MOZ_ASSERT(isPrivate == mRespectPrivacy);
2342 if (aLoadingDocument) {
2343 // The given load group should match that of the document if given. If
2344 // that isn't the case, then we need to add more plumbing to ensure we
2345 // block the document as well.
2346 nsCOMPtr<nsILoadGroup> docLoadGroup =
2347 aLoadingDocument->GetDocumentLoadGroup();
2348 MOZ_ASSERT(docLoadGroup == aLoadGroup);
2350 #endif
2352 // Get the default load flags from the loadgroup (if possible)...
2353 if (aLoadGroup) {
2354 aLoadGroup->GetLoadFlags(&requestFlags);
2357 // Merge the default load flags with those passed in via aLoadFlags.
2358 // Currently, *only* the caching, validation and background load flags
2359 // are merged...
2361 // The flags in aLoadFlags take precedence over the default flags!
2363 if (aLoadFlags & LOAD_FLAGS_CACHE_MASK) {
2364 // Override the default caching flags...
2365 requestFlags = (requestFlags & ~LOAD_FLAGS_CACHE_MASK) |
2366 (aLoadFlags & LOAD_FLAGS_CACHE_MASK);
2368 if (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK) {
2369 // Override the default validation flags...
2370 requestFlags = (requestFlags & ~LOAD_FLAGS_VALIDATE_MASK) |
2371 (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK);
2373 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
2374 // Propagate background loading...
2375 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2377 if (aLoadFlags & nsIRequest::LOAD_RECORD_START_REQUEST_DELAY) {
2378 requestFlags |= nsIRequest::LOAD_RECORD_START_REQUEST_DELAY;
2381 if (aLinkPreload) {
2382 // Set background loading if it is <link rel=preload>
2383 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2386 CORSMode corsmode = CORS_NONE;
2387 if (aLoadFlags & imgILoader::LOAD_CORS_ANONYMOUS) {
2388 corsmode = CORS_ANONYMOUS;
2389 } else if (aLoadFlags & imgILoader::LOAD_CORS_USE_CREDENTIALS) {
2390 corsmode = CORS_USE_CREDENTIALS;
2393 // Look in the preloaded images of loading document first.
2394 if (StaticPrefs::network_preload() && !aLinkPreload && aLoadingDocument) {
2395 // All Early Hints preloads are Link preloads, therefore we don't have a
2396 // Early Hints preload here
2397 MOZ_ASSERT(!aEarlyHintPreloaderId);
2398 auto key =
2399 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, corsmode);
2400 if (RefPtr<PreloaderBase> preload =
2401 aLoadingDocument->Preloads().LookupPreload(key)) {
2402 RefPtr<imgRequestProxy> proxy = do_QueryObject(preload);
2403 MOZ_ASSERT(proxy);
2405 MOZ_LOG(gImgLog, LogLevel::Debug,
2406 ("[this=%p] imgLoader::LoadImage -- preloaded [proxy=%p]"
2407 " [document=%p]\n",
2408 this, proxy.get(), aLoadingDocument));
2410 // Removing the preload for this image to be in parity with Chromium. Any
2411 // following regular image request will be reloaded using the regular
2412 // path: image cache, http cache, network. Any following `<link
2413 // rel=preload as=image>` will start a new image preload that can be
2414 // satisfied from http cache or network.
2416 // There is a spec discussion for "preload cache", see
2417 // https://github.com/w3c/preload/issues/97. And it is also not clear how
2418 // preload image interacts with list of available images, see
2419 // https://github.com/whatwg/html/issues/4474.
2420 proxy->RemoveSelf(aLoadingDocument);
2421 proxy->NotifyUsage();
2423 imgRequest* request = proxy->GetOwner();
2424 nsresult rv =
2425 CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
2426 aObserver, requestFlags, _retval);
2427 NS_ENSURE_SUCCESS(rv, rv);
2429 imgRequestProxy* newProxy = *_retval;
2430 if (imgCacheValidator* validator = request->GetValidator()) {
2431 newProxy->MarkValidating();
2432 // Attach the proxy without notifying and this will add us to the load
2433 // group.
2434 validator->AddProxy(newProxy);
2435 } else {
2436 // It's OK to add here even if the request is done. If it is, it'll send
2437 // a OnStopRequest()and the proxy will be removed from the loadgroup in
2438 // imgRequestProxy::OnLoadComplete.
2439 newProxy->AddToLoadGroup();
2440 newProxy->NotifyListener();
2443 return NS_OK;
2447 RefPtr<imgCacheEntry> entry;
2449 // Look in the cache for our URI, and then validate it.
2450 // XXX For now ignore aCacheKey. We will need it in the future
2451 // for correctly dealing with image load requests that are a result
2452 // of post data.
2453 OriginAttributes attrs;
2454 if (aTriggeringPrincipal) {
2455 attrs = aTriggeringPrincipal->OriginAttributesRef();
2457 ImageCacheKey key(aURI, attrs, aLoadingDocument);
2458 imgCacheTable& cache = GetCache(key);
2460 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
2461 bool newChannelCreated = false;
2462 if (ValidateEntry(entry, aURI, aInitialDocumentURI, aReferrerInfo,
2463 aLoadGroup, aObserver, aLoadingDocument, requestFlags,
2464 aContentPolicyType, true, &newChannelCreated, _retval,
2465 aTriggeringPrincipal, corsmode, aLinkPreload,
2466 aEarlyHintPreloaderId)) {
2467 request = entry->GetRequest();
2469 // If this entry has no proxies, its request has no reference to the
2470 // entry.
2471 if (entry->HasNoProxies()) {
2472 LOG_FUNC_WITH_PARAM(gImgLog,
2473 "imgLoader::LoadImage() adding proxyless entry",
2474 "uri", key.URI());
2475 MOZ_ASSERT(!request->HasCacheEntry(),
2476 "Proxyless entry's request has cache entry!");
2477 request->SetCacheEntry(entry);
2479 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2480 mCacheTracker->MarkUsed(entry);
2484 entry->Touch();
2486 if (!newChannelCreated) {
2487 // This is ugly but it's needed to report CSP violations. We have 3
2488 // scenarios:
2489 // - we don't have cache. We are not in this if() stmt. A new channel is
2490 // created and that triggers the CSP checks.
2491 // - We have a cache entry and this is blocked by CSP directives.
2492 DebugOnly<bool> shouldLoad = ShouldLoadCachedImage(
2493 request, aLoadingDocument, aTriggeringPrincipal, aContentPolicyType,
2494 /* aSendCSPViolationReports */ true);
2495 MOZ_ASSERT(shouldLoad);
2497 } else {
2498 // We can't use this entry. We'll try to load it off the network, and if
2499 // successful, overwrite the old entry in the cache with a new one.
2500 entry = nullptr;
2504 // Keep the channel in this scope, so we can adjust its notificationCallbacks
2505 // later when we create the proxy.
2506 nsCOMPtr<nsIChannel> newChannel;
2507 // If we didn't get a cache hit, we need to load from the network.
2508 if (!request) {
2509 LOG_SCOPE(gImgLog, "imgLoader::LoadImage |cache miss|");
2511 bool forcePrincipalCheck;
2512 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
2513 aInitialDocumentURI, corsmode, aReferrerInfo,
2514 aLoadGroup, requestFlags, aContentPolicyType,
2515 aTriggeringPrincipal, aContext, mRespectPrivacy,
2516 aEarlyHintPreloaderId);
2517 if (NS_FAILED(rv)) {
2518 return NS_ERROR_FAILURE;
2521 MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel) == mRespectPrivacy);
2523 NewRequestAndEntry(forcePrincipalCheck, this, key, getter_AddRefs(request),
2524 getter_AddRefs(entry));
2526 MOZ_LOG(gImgLog, LogLevel::Debug,
2527 ("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
2528 " [request=%p]\n",
2529 this, request.get()));
2531 nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(newChannel));
2532 if (cos) {
2533 if (aUseUrgentStartForChannel && !aLinkPreload) {
2534 cos->AddClassFlags(nsIClassOfService::UrgentStart);
2537 if (StaticPrefs::network_http_tailing_enabled() &&
2538 aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
2539 cos->AddClassFlags(nsIClassOfService::Throttleable |
2540 nsIClassOfService::Tail);
2541 nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(newChannel));
2542 if (httpChannel) {
2543 Unused << httpChannel->SetRequestContextID(aRequestContextID);
2548 nsCOMPtr<nsILoadGroup> channelLoadGroup;
2549 newChannel->GetLoadGroup(getter_AddRefs(channelLoadGroup));
2550 rv = request->Init(aURI, aURI, /* aHadInsecureRedirect = */ false,
2551 channelLoadGroup, newChannel, entry, aLoadingDocument,
2552 aTriggeringPrincipal, corsmode, aReferrerInfo);
2553 if (NS_FAILED(rv)) {
2554 return NS_ERROR_FAILURE;
2557 // Add the initiator type for this image load
2558 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(newChannel);
2559 if (timedChannel) {
2560 timedChannel->SetInitiatorType(initiatorType);
2563 // create the proxy listener
2564 nsCOMPtr<nsIStreamListener> listener = new ProxyListener(request.get());
2566 MOZ_LOG(gImgLog, LogLevel::Debug,
2567 ("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
2568 this));
2570 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
2571 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
2572 aLoadGroup);
2574 nsresult openRes;
2575 openRes = newChannel->AsyncOpen(listener);
2577 if (NS_FAILED(openRes)) {
2578 MOZ_LOG(
2579 gImgLog, LogLevel::Debug,
2580 ("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
2581 "\n",
2582 this, static_cast<uint32_t>(openRes)));
2583 request->CancelAndAbort(openRes);
2584 return openRes;
2587 // Try to add the new request into the cache.
2588 PutIntoCache(key, entry);
2589 } else {
2590 LOG_MSG_WITH_PARAM(gImgLog, "imgLoader::LoadImage |cache hit|", "request",
2591 request);
2594 // If we didn't get a proxy when validating the cache entry, we need to
2595 // create one.
2596 if (!*_retval) {
2597 // ValidateEntry() has three return values: "Is valid," "might be valid --
2598 // validating over network", and "not valid." If we don't have a _retval,
2599 // we know ValidateEntry is not validating over the network, so it's safe
2600 // to SetLoadId here because we know this request is valid for this context.
2602 // Note, however, that this doesn't guarantee the behaviour we want (one
2603 // URL maps to the same image on a page) if we load the same image in a
2604 // different tab (see bug 528003), because its load id will get re-set, and
2605 // that'll cause us to validate over the network.
2606 request->SetLoadId(aLoadingDocument);
2608 LOG_MSG(gImgLog, "imgLoader::LoadImage", "creating proxy request.");
2609 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
2610 aObserver, requestFlags, _retval);
2611 if (NS_FAILED(rv)) {
2612 return rv;
2615 imgRequestProxy* proxy = *_retval;
2617 // Make sure that OnStatus/OnProgress calls have the right request set, if
2618 // we did create a channel here.
2619 if (newChannel) {
2620 nsCOMPtr<nsIInterfaceRequestor> requestor(
2621 new nsProgressNotificationProxy(newChannel, proxy));
2622 if (!requestor) {
2623 return NS_ERROR_OUT_OF_MEMORY;
2625 newChannel->SetNotificationCallbacks(requestor);
2628 if (aLinkPreload) {
2629 MOZ_ASSERT(aLoadingDocument);
2630 proxy->PrioritizeAsPreload();
2631 auto preloadKey =
2632 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, corsmode);
2633 proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
2636 // Note that it's OK to add here even if the request is done. If it is,
2637 // it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
2638 // the proxy will be removed from the loadgroup.
2639 proxy->AddToLoadGroup();
2641 // If we're loading off the network, explicitly don't notify our proxy,
2642 // because necko (or things called from necko, such as imgCacheValidator)
2643 // are going to call our notifications asynchronously, and we can't make it
2644 // further asynchronous because observers might rely on imagelib completing
2645 // its work between the channel's OnStartRequest and OnStopRequest.
2646 if (!newChannel) {
2647 proxy->NotifyListener();
2650 return rv;
2653 NS_ASSERTION(*_retval, "imgLoader::LoadImage -- no return value");
2655 return NS_OK;
2658 NS_IMETHODIMP
2659 imgLoader::LoadImageWithChannelXPCOM(nsIChannel* channel,
2660 imgINotificationObserver* aObserver,
2661 Document* aLoadingDocument,
2662 nsIStreamListener** listener,
2663 imgIRequest** _retval) {
2664 nsresult result;
2665 imgRequestProxy* proxy;
2666 result = LoadImageWithChannel(channel, aObserver, aLoadingDocument, listener,
2667 &proxy);
2668 *_retval = proxy;
2669 return result;
2672 nsresult imgLoader::LoadImageWithChannel(nsIChannel* channel,
2673 imgINotificationObserver* aObserver,
2674 Document* aLoadingDocument,
2675 nsIStreamListener** listener,
2676 imgRequestProxy** _retval) {
2677 NS_ASSERTION(channel,
2678 "imgLoader::LoadImageWithChannel -- NULL channel pointer");
2680 MOZ_ASSERT(NS_UsePrivateBrowsing(channel) == mRespectPrivacy);
2682 auto makeStaticIfNeeded = mozilla::MakeScopeExit(
2683 [&] { MakeRequestStaticIfNeeded(aLoadingDocument, _retval); });
2685 LOG_SCOPE(gImgLog, "imgLoader::LoadImageWithChannel");
2686 RefPtr<imgRequest> request;
2688 nsCOMPtr<nsIURI> uri;
2689 channel->GetURI(getter_AddRefs(uri));
2691 NS_ENSURE_TRUE(channel, NS_ERROR_FAILURE);
2692 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2694 OriginAttributes attrs = loadInfo->GetOriginAttributes();
2696 ImageCacheKey key(uri, attrs, aLoadingDocument);
2698 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2699 channel->GetLoadFlags(&requestFlags);
2701 RefPtr<imgCacheEntry> entry;
2703 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2704 RemoveFromCache(key);
2705 } else {
2706 // Look in the cache for our URI, and then validate it.
2707 // XXX For now ignore aCacheKey. We will need it in the future
2708 // for correctly dealing with image load requests that are a result
2709 // of post data.
2710 imgCacheTable& cache = GetCache(key);
2711 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
2712 // We don't want to kick off another network load. So we ask
2713 // ValidateEntry to only do validation without creating a new proxy. If
2714 // it says that the entry isn't valid any more, we'll only use the entry
2715 // we're getting if the channel is loading from the cache anyways.
2717 // XXX -- should this be changed? it's pretty much verbatim from the old
2718 // code, but seems nonsensical.
2720 // Since aCanMakeNewChannel == false, we don't need to pass content policy
2721 // type/principal/etc
2723 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2724 // if there is a loadInfo, use the right contentType, otherwise
2725 // default to the internal image type
2726 nsContentPolicyType policyType = loadInfo->InternalContentPolicyType();
2728 if (ValidateEntry(entry, uri, nullptr, nullptr, nullptr, aObserver,
2729 aLoadingDocument, requestFlags, policyType, false,
2730 nullptr, nullptr, nullptr, CORS_NONE, false, 0)) {
2731 request = entry->GetRequest();
2732 } else {
2733 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(channel));
2734 bool bUseCacheCopy;
2736 if (cacheChan) {
2737 cacheChan->IsFromCache(&bUseCacheCopy);
2738 } else {
2739 bUseCacheCopy = false;
2742 if (!bUseCacheCopy) {
2743 entry = nullptr;
2744 } else {
2745 request = entry->GetRequest();
2749 if (request && entry) {
2750 // If this entry has no proxies, its request has no reference to
2751 // the entry.
2752 if (entry->HasNoProxies()) {
2753 LOG_FUNC_WITH_PARAM(
2754 gImgLog,
2755 "imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
2756 key.URI());
2757 MOZ_ASSERT(!request->HasCacheEntry(),
2758 "Proxyless entry's request has cache entry!");
2759 request->SetCacheEntry(entry);
2761 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2762 mCacheTracker->MarkUsed(entry);
2769 nsCOMPtr<nsILoadGroup> loadGroup;
2770 channel->GetLoadGroup(getter_AddRefs(loadGroup));
2772 #ifdef DEBUG
2773 if (aLoadingDocument) {
2774 // The load group of the channel should always match that of the
2775 // document if given. If that isn't the case, then we need to add more
2776 // plumbing to ensure we block the document as well.
2777 nsCOMPtr<nsILoadGroup> docLoadGroup =
2778 aLoadingDocument->GetDocumentLoadGroup();
2779 MOZ_ASSERT(docLoadGroup == loadGroup);
2781 #endif
2783 // Filter out any load flags not from nsIRequest
2784 requestFlags &= nsIRequest::LOAD_REQUESTMASK;
2786 nsresult rv = NS_OK;
2787 if (request) {
2788 // we have this in our cache already.. cancel the current (document) load
2790 // this should fire an OnStopRequest
2791 channel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
2793 *listener = nullptr; // give them back a null nsIStreamListener
2795 rv = CreateNewProxyForRequest(request, uri, loadGroup, aLoadingDocument,
2796 aObserver, requestFlags, _retval);
2797 static_cast<imgRequestProxy*>(*_retval)->NotifyListener();
2798 } else {
2799 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2800 nsCOMPtr<nsIURI> originalURI;
2801 channel->GetOriginalURI(getter_AddRefs(originalURI));
2803 // XXX(seth): We should be able to just use |key| here, except that |key| is
2804 // constructed above with the *current URI* and not the *original URI*. I'm
2805 // pretty sure this is a bug, and it's preventing us from ever getting a
2806 // cache hit in LoadImageWithChannel when redirects are involved.
2807 ImageCacheKey originalURIKey(originalURI, attrs, aLoadingDocument);
2809 // Default to doing a principal check because we don't know who
2810 // started that load and whether their principal ended up being
2811 // inherited on the channel.
2812 NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
2813 originalURIKey, getter_AddRefs(request),
2814 getter_AddRefs(entry));
2816 // No principal specified here, because we're not passed one.
2817 // In LoadImageWithChannel, the redirects that may have been
2818 // associated with this load would have gone through necko.
2819 // We only have the final URI in ImageLib and hence don't know
2820 // if the request went through insecure redirects. But if it did,
2821 // the necko cache should have handled that (since all necko cache hits
2822 // including the redirects will go through content policy). Hence, we
2823 // can set aHadInsecureRedirect to false here.
2824 rv = request->Init(originalURI, uri, /* aHadInsecureRedirect = */ false,
2825 channel, channel, entry, aLoadingDocument, nullptr,
2826 CORS_NONE, nullptr);
2827 NS_ENSURE_SUCCESS(rv, rv);
2829 RefPtr<ProxyListener> pl =
2830 new ProxyListener(static_cast<nsIStreamListener*>(request.get()));
2831 pl.forget(listener);
2833 // Try to add the new request into the cache.
2834 PutIntoCache(originalURIKey, entry);
2836 rv = CreateNewProxyForRequest(request, originalURI, loadGroup,
2837 aLoadingDocument, aObserver, requestFlags,
2838 _retval);
2840 // Explicitly don't notify our proxy, because we're loading off the
2841 // network, and necko (or things called from necko, such as
2842 // imgCacheValidator) are going to call our notifications asynchronously,
2843 // and we can't make it further asynchronous because observers might rely
2844 // on imagelib completing its work between the channel's OnStartRequest and
2845 // OnStopRequest.
2848 if (NS_FAILED(rv)) {
2849 return rv;
2852 (*_retval)->AddToLoadGroup();
2853 return rv;
2856 bool imgLoader::SupportImageWithMimeType(const nsACString& aMimeType,
2857 AcceptedMimeTypes aAccept
2858 /* = AcceptedMimeTypes::IMAGES */) {
2859 nsAutoCString mimeType(aMimeType);
2860 ToLowerCase(mimeType);
2862 if (aAccept == AcceptedMimeTypes::IMAGES_AND_DOCUMENTS &&
2863 mimeType.EqualsLiteral("image/svg+xml")) {
2864 return true;
2867 DecoderType type = DecoderFactory::GetDecoderType(mimeType.get());
2868 return type != DecoderType::UNKNOWN;
2871 NS_IMETHODIMP
2872 imgLoader::GetMIMETypeFromContent(nsIRequest* aRequest,
2873 const uint8_t* aContents, uint32_t aLength,
2874 nsACString& aContentType) {
2875 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2876 if (channel) {
2877 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2878 if (loadInfo->GetSkipContentSniffing()) {
2879 return NS_ERROR_NOT_AVAILABLE;
2883 nsresult rv =
2884 GetMimeTypeFromContent((const char*)aContents, aLength, aContentType);
2885 if (NS_SUCCEEDED(rv) && channel && XRE_IsParentProcess()) {
2886 if (RefPtr<mozilla::net::nsHttpChannel> httpChannel =
2887 do_QueryObject(channel)) {
2888 // If the image type pattern matching algorithm given bytes does not
2889 // return undefined, then disable the further check and allow the
2890 // response.
2891 httpChannel->DisableIsOpaqueResponseAllowedAfterSniffCheck(
2892 mozilla::net::nsHttpChannel::SnifferType::Image);
2896 return rv;
2899 /* static */
2900 nsresult imgLoader::GetMimeTypeFromContent(const char* aContents,
2901 uint32_t aLength,
2902 nsACString& aContentType) {
2903 nsAutoCString detected;
2905 /* Is it a GIF? */
2906 if (aLength >= 6 &&
2907 (!strncmp(aContents, "GIF87a", 6) || !strncmp(aContents, "GIF89a", 6))) {
2908 aContentType.AssignLiteral(IMAGE_GIF);
2910 /* or a PNG? */
2911 } else if (aLength >= 8 && ((unsigned char)aContents[0] == 0x89 &&
2912 (unsigned char)aContents[1] == 0x50 &&
2913 (unsigned char)aContents[2] == 0x4E &&
2914 (unsigned char)aContents[3] == 0x47 &&
2915 (unsigned char)aContents[4] == 0x0D &&
2916 (unsigned char)aContents[5] == 0x0A &&
2917 (unsigned char)aContents[6] == 0x1A &&
2918 (unsigned char)aContents[7] == 0x0A)) {
2919 aContentType.AssignLiteral(IMAGE_PNG);
2921 /* maybe a JPEG (JFIF)? */
2922 /* JFIF files start with SOI APP0 but older files can start with SOI DQT
2923 * so we test for SOI followed by any marker, i.e. FF D8 FF
2924 * this will also work for SPIFF JPEG files if they appear in the future.
2926 * (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
2928 } else if (aLength >= 3 && ((unsigned char)aContents[0]) == 0xFF &&
2929 ((unsigned char)aContents[1]) == 0xD8 &&
2930 ((unsigned char)aContents[2]) == 0xFF) {
2931 aContentType.AssignLiteral(IMAGE_JPEG);
2933 /* or how about ART? */
2934 /* ART begins with JG (4A 47). Major version offset 2.
2935 * Minor version offset 3. Offset 4 must be nullptr.
2937 } else if (aLength >= 5 && ((unsigned char)aContents[0]) == 0x4a &&
2938 ((unsigned char)aContents[1]) == 0x47 &&
2939 ((unsigned char)aContents[4]) == 0x00) {
2940 aContentType.AssignLiteral(IMAGE_ART);
2942 } else if (aLength >= 2 && !strncmp(aContents, "BM", 2)) {
2943 aContentType.AssignLiteral(IMAGE_BMP);
2945 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
2946 // CURs begin with 2-byte 0 followed by 2-byte 2.
2947 } else if (aLength >= 4 && (!memcmp(aContents, "\000\000\001\000", 4) ||
2948 !memcmp(aContents, "\000\000\002\000", 4))) {
2949 aContentType.AssignLiteral(IMAGE_ICO);
2951 // WebPs always begin with RIFF, a 32-bit length, and WEBP.
2952 } else if (aLength >= 12 && !memcmp(aContents, "RIFF", 4) &&
2953 !memcmp(aContents + 8, "WEBP", 4)) {
2954 aContentType.AssignLiteral(IMAGE_WEBP);
2956 } else if (MatchesMP4(reinterpret_cast<const uint8_t*>(aContents), aLength,
2957 detected) &&
2958 detected.Equals(IMAGE_AVIF)) {
2959 aContentType.AssignLiteral(IMAGE_AVIF);
2960 } else if ((aLength >= 2 && !memcmp(aContents, "\xFF\x0A", 2)) ||
2961 (aLength >= 12 &&
2962 !memcmp(aContents, "\x00\x00\x00\x0CJXL \x0D\x0A\x87\x0A", 12))) {
2963 // Each version is for containerless and containerful files respectively.
2964 aContentType.AssignLiteral(IMAGE_JXL);
2965 } else {
2966 /* none of the above? I give up */
2967 return NS_ERROR_NOT_AVAILABLE;
2970 return NS_OK;
2974 * proxy stream listener class used to handle multipart/x-mixed-replace
2977 #include "nsIRequest.h"
2978 #include "nsIStreamConverterService.h"
2980 NS_IMPL_ISUPPORTS(ProxyListener, nsIStreamListener,
2981 nsIThreadRetargetableStreamListener, nsIRequestObserver)
2983 ProxyListener::ProxyListener(nsIStreamListener* dest) : mDestListener(dest) {
2984 /* member initializers and constructor code */
2987 ProxyListener::~ProxyListener() { /* destructor code */
2990 /** nsIRequestObserver methods **/
2992 NS_IMETHODIMP
2993 ProxyListener::OnStartRequest(nsIRequest* aRequest) {
2994 if (!mDestListener) {
2995 return NS_ERROR_FAILURE;
2998 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2999 if (channel) {
3000 // We need to set the initiator type for the image load
3001 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(channel);
3002 if (timedChannel) {
3003 nsAutoString type;
3004 timedChannel->GetInitiatorType(type);
3005 if (type.IsEmpty()) {
3006 timedChannel->SetInitiatorType(u"img"_ns);
3010 nsAutoCString contentType;
3011 nsresult rv = channel->GetContentType(contentType);
3013 if (!contentType.IsEmpty()) {
3014 /* If multipart/x-mixed-replace content, we'll insert a MIME decoder
3015 in the pipeline to handle the content and pass it along to our
3016 original listener.
3018 if ("multipart/x-mixed-replace"_ns.Equals(contentType)) {
3019 nsCOMPtr<nsIStreamConverterService> convServ(
3020 do_GetService("@mozilla.org/streamConverters;1", &rv));
3021 if (NS_SUCCEEDED(rv)) {
3022 nsCOMPtr<nsIStreamListener> toListener(mDestListener);
3023 nsCOMPtr<nsIStreamListener> fromListener;
3025 rv = convServ->AsyncConvertData("multipart/x-mixed-replace", "*/*",
3026 toListener, nullptr,
3027 getter_AddRefs(fromListener));
3028 if (NS_SUCCEEDED(rv)) {
3029 mDestListener = fromListener;
3036 return mDestListener->OnStartRequest(aRequest);
3039 NS_IMETHODIMP
3040 ProxyListener::OnStopRequest(nsIRequest* aRequest, nsresult status) {
3041 if (!mDestListener) {
3042 return NS_ERROR_FAILURE;
3045 return mDestListener->OnStopRequest(aRequest, status);
3048 /** nsIStreamListener methods **/
3050 NS_IMETHODIMP
3051 ProxyListener::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
3052 uint64_t sourceOffset, uint32_t count) {
3053 if (!mDestListener) {
3054 return NS_ERROR_FAILURE;
3057 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
3060 /** nsThreadRetargetableStreamListener methods **/
3061 NS_IMETHODIMP
3062 ProxyListener::CheckListenerChain() {
3063 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3064 nsresult rv = NS_OK;
3065 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3066 do_QueryInterface(mDestListener, &rv);
3067 if (retargetableListener) {
3068 rv = retargetableListener->CheckListenerChain();
3070 MOZ_LOG(
3071 gImgLog, LogLevel::Debug,
3072 ("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
3073 "]",
3074 (NS_SUCCEEDED(rv) ? "success" : "failure"), this,
3075 (nsIStreamListener*)mDestListener, static_cast<uint32_t>(rv)));
3076 return rv;
3080 * http validate class. check a channel for a 304
3083 NS_IMPL_ISUPPORTS(imgCacheValidator, nsIStreamListener, nsIRequestObserver,
3084 nsIThreadRetargetableStreamListener, nsIChannelEventSink,
3085 nsIInterfaceRequestor, nsIAsyncVerifyRedirectCallback)
3087 imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy* progress,
3088 imgLoader* loader, imgRequest* request,
3089 Document* aDocument,
3090 uint64_t aInnerWindowId,
3091 bool forcePrincipalCheckForCacheEntry)
3092 : mProgressProxy(progress),
3093 mRequest(request),
3094 mDocument(aDocument),
3095 mInnerWindowId(aInnerWindowId),
3096 mImgLoader(loader),
3097 mHadInsecureRedirect(false) {
3098 NewRequestAndEntry(forcePrincipalCheckForCacheEntry, loader,
3099 mRequest->CacheKey(), getter_AddRefs(mNewRequest),
3100 getter_AddRefs(mNewEntry));
3103 imgCacheValidator::~imgCacheValidator() {
3104 if (mRequest) {
3105 // If something went wrong, and we never unblocked the requests waiting on
3106 // validation, now is our last chance. We will cancel the new request and
3107 // switch the waiting proxies to it.
3108 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
3112 void imgCacheValidator::AddProxy(imgRequestProxy* aProxy) {
3113 // aProxy needs to be in the loadgroup since we're validating from
3114 // the network.
3115 aProxy->AddToLoadGroup();
3117 mProxies.AppendElement(aProxy);
3120 void imgCacheValidator::RemoveProxy(imgRequestProxy* aProxy) {
3121 mProxies.RemoveElement(aProxy);
3124 void imgCacheValidator::UpdateProxies(bool aCancelRequest, bool aSyncNotify) {
3125 MOZ_ASSERT(mRequest);
3127 // Clear the validator before updating the proxies. The notifications may
3128 // clone an existing request, and its state could be inconsistent.
3129 mRequest->SetValidator(nullptr);
3130 mRequest = nullptr;
3132 // If an error occurred, we will want to cancel the new request, and make the
3133 // validating proxies point to it. Any proxies still bound to the original
3134 // request which are not validating should remain untouched.
3135 if (aCancelRequest) {
3136 MOZ_ASSERT(mNewRequest);
3137 mNewRequest->CancelAndAbort(NS_BINDING_ABORTED);
3140 // We have finished validating the request, so we can safely take ownership
3141 // of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
3142 // if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
3143 // that any potential notifications should still be suppressed in
3144 // imgRequestProxy::ChangeOwner because we haven't cleared the validating
3145 // flag yet, and thus they will remain deferred.
3146 AutoTArray<RefPtr<imgRequestProxy>, 4> proxies(std::move(mProxies));
3148 for (auto& proxy : proxies) {
3149 // First update the state of all proxies before notifying any of them
3150 // to ensure a consistent state (e.g. in case the notification causes
3151 // other proxies to be touched indirectly.)
3152 MOZ_ASSERT(proxy->IsValidating());
3153 MOZ_ASSERT(proxy->NotificationsDeferred(),
3154 "Proxies waiting on cache validation should be "
3155 "deferring notifications!");
3156 if (mNewRequest) {
3157 proxy->ChangeOwner(mNewRequest);
3159 proxy->ClearValidating();
3162 mNewRequest = nullptr;
3163 mNewEntry = nullptr;
3165 for (auto& proxy : proxies) {
3166 if (aSyncNotify) {
3167 // Notify synchronously, because the caller knows we are already in an
3168 // asynchronously-called function (e.g. OnStartRequest).
3169 proxy->SyncNotifyListener();
3170 } else {
3171 // Notify asynchronously, because the caller does not know our current
3172 // call state (e.g. ~imgCacheValidator).
3173 proxy->NotifyListener();
3178 /** nsIRequestObserver methods **/
3180 NS_IMETHODIMP
3181 imgCacheValidator::OnStartRequest(nsIRequest* aRequest) {
3182 // We may be holding on to a document, so ensure that it's released.
3183 RefPtr<Document> document = mDocument.forget();
3185 // If for some reason we don't still have an existing request (probably
3186 // because OnStartRequest got delivered more than once), just bail.
3187 if (!mRequest) {
3188 MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
3189 aRequest->CancelWithReason(NS_BINDING_ABORTED,
3190 "OnStartRequest delivered more than once?"_ns);
3191 return NS_ERROR_FAILURE;
3194 // If this request is coming from cache and has the same URI as our
3195 // imgRequest, the request all our proxies are pointing at is valid, and all
3196 // we have to do is tell them to notify their listeners.
3197 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(aRequest));
3198 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
3199 if (cacheChan && channel) {
3200 bool isFromCache = false;
3201 cacheChan->IsFromCache(&isFromCache);
3203 nsCOMPtr<nsIURI> channelURI;
3204 channel->GetURI(getter_AddRefs(channelURI));
3206 nsCOMPtr<nsIURI> finalURI;
3207 mRequest->GetFinalURI(getter_AddRefs(finalURI));
3209 bool sameURI = false;
3210 if (channelURI && finalURI) {
3211 channelURI->Equals(finalURI, &sameURI);
3214 if (isFromCache && sameURI) {
3215 // We don't need to load this any more.
3216 aRequest->CancelWithReason(NS_BINDING_ABORTED,
3217 "imgCacheValidator::OnStartRequest"_ns);
3218 mNewRequest = nullptr;
3220 // Clear the validator before updating the proxies. The notifications may
3221 // clone an existing request, and its state could be inconsistent.
3222 mRequest->SetLoadId(document);
3223 mRequest->SetInnerWindowID(mInnerWindowId);
3224 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3225 return NS_OK;
3229 // We can't load out of cache. We have to create a whole new request for the
3230 // data that's coming in off the channel.
3231 nsCOMPtr<nsIURI> uri;
3232 mRequest->GetURI(getter_AddRefs(uri));
3234 LOG_MSG_WITH_PARAM(gImgLog,
3235 "imgCacheValidator::OnStartRequest creating new request",
3236 "uri", uri);
3238 CORSMode corsmode = mRequest->GetCORSMode();
3239 nsCOMPtr<nsIReferrerInfo> referrerInfo = mRequest->GetReferrerInfo();
3240 nsCOMPtr<nsIPrincipal> triggeringPrincipal =
3241 mRequest->GetTriggeringPrincipal();
3243 // Doom the old request's cache entry
3244 mRequest->RemoveFromCache();
3246 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
3247 nsCOMPtr<nsIURI> originalURI;
3248 channel->GetOriginalURI(getter_AddRefs(originalURI));
3249 nsresult rv = mNewRequest->Init(originalURI, uri, mHadInsecureRedirect,
3250 aRequest, channel, mNewEntry, document,
3251 triggeringPrincipal, corsmode, referrerInfo);
3252 if (NS_FAILED(rv)) {
3253 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
3254 return rv;
3257 mDestListener = new ProxyListener(mNewRequest);
3259 // Try to add the new request into the cache. Note that the entry must be in
3260 // the cache before the proxies' ownership changes, because adding a proxy
3261 // changes the caching behaviour for imgRequests.
3262 mImgLoader->PutIntoCache(mNewRequest->CacheKey(), mNewEntry);
3263 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3264 return mDestListener->OnStartRequest(aRequest);
3267 NS_IMETHODIMP
3268 imgCacheValidator::OnStopRequest(nsIRequest* aRequest, nsresult status) {
3269 // Be sure we've released the document that we may have been holding on to.
3270 mDocument = nullptr;
3272 if (!mDestListener) {
3273 return NS_OK;
3276 return mDestListener->OnStopRequest(aRequest, status);
3279 /** nsIStreamListener methods **/
3281 NS_IMETHODIMP
3282 imgCacheValidator::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
3283 uint64_t sourceOffset, uint32_t count) {
3284 if (!mDestListener) {
3285 // XXX see bug 113959
3286 uint32_t _retval;
3287 inStr->ReadSegments(NS_DiscardSegment, nullptr, count, &_retval);
3288 return NS_OK;
3291 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
3294 /** nsIThreadRetargetableStreamListener methods **/
3296 NS_IMETHODIMP
3297 imgCacheValidator::CheckListenerChain() {
3298 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3299 nsresult rv = NS_OK;
3300 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3301 do_QueryInterface(mDestListener, &rv);
3302 if (retargetableListener) {
3303 rv = retargetableListener->CheckListenerChain();
3305 MOZ_LOG(
3306 gImgLog, LogLevel::Debug,
3307 ("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32 "=%s",
3308 this, static_cast<uint32_t>(rv),
3309 NS_SUCCEEDED(rv) ? "succeeded" : "failed"));
3310 return rv;
3313 /** nsIInterfaceRequestor methods **/
3315 NS_IMETHODIMP
3316 imgCacheValidator::GetInterface(const nsIID& aIID, void** aResult) {
3317 if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
3318 return QueryInterface(aIID, aResult);
3321 return mProgressProxy->GetInterface(aIID, aResult);
3324 // These functions are materially the same as the same functions in imgRequest.
3325 // We duplicate them because we're verifying whether cache loads are necessary,
3326 // not unconditionally loading.
3328 /** nsIChannelEventSink methods **/
3329 NS_IMETHODIMP
3330 imgCacheValidator::AsyncOnChannelRedirect(
3331 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
3332 nsIAsyncVerifyRedirectCallback* callback) {
3333 // Note all cache information we get from the old channel.
3334 mNewRequest->SetCacheValidation(mNewEntry, oldChannel);
3336 // If the previous URI is a non-HTTPS URI, record that fact for later use by
3337 // security code, which needs to know whether there is an insecure load at any
3338 // point in the redirect chain.
3339 nsCOMPtr<nsIURI> oldURI;
3340 bool schemeLocal = false;
3341 if (NS_FAILED(oldChannel->GetURI(getter_AddRefs(oldURI))) ||
3342 NS_FAILED(NS_URIChainHasFlags(
3343 oldURI, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE, &schemeLocal)) ||
3344 (!oldURI->SchemeIs("https") && !oldURI->SchemeIs("chrome") &&
3345 !schemeLocal)) {
3346 mHadInsecureRedirect = true;
3349 // Prepare for callback
3350 mRedirectCallback = callback;
3351 mRedirectChannel = newChannel;
3353 return mProgressProxy->AsyncOnChannelRedirect(oldChannel, newChannel, flags,
3354 this);
3357 NS_IMETHODIMP
3358 imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult) {
3359 // If we've already been told to abort, just do so.
3360 if (NS_FAILED(aResult)) {
3361 mRedirectCallback->OnRedirectVerifyCallback(aResult);
3362 mRedirectCallback = nullptr;
3363 mRedirectChannel = nullptr;
3364 return NS_OK;
3367 // make sure we have a protocol that returns data rather than opens
3368 // an external application, e.g. mailto:
3369 nsCOMPtr<nsIURI> uri;
3370 mRedirectChannel->GetURI(getter_AddRefs(uri));
3371 bool doesNotReturnData = false;
3372 NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA,
3373 &doesNotReturnData);
3375 nsresult result = NS_OK;
3377 if (doesNotReturnData) {
3378 result = NS_ERROR_ABORT;
3381 mRedirectCallback->OnRedirectVerifyCallback(result);
3382 mRedirectCallback = nullptr;
3383 mRedirectChannel = nullptr;
3384 return NS_OK;