Bug 1767595 [wpt PR 33926] - Added a WPT-test for connecting a TMST to a video elemen...
[gecko.git] / image / imgLoader.cpp
blob6f8090713442bb69ed8080e711e1c6c0dc84734b
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 #undef LoadImage
11 #include "imgLoader.h"
13 #include <algorithm>
14 #include <utility>
16 #include "DecoderFactory.h"
17 #include "Image.h"
18 #include "ImageLogging.h"
19 #include "ReferrerInfo.h"
20 #include "imgRequestProxy.h"
21 #include "mozilla/Attributes.h"
22 #include "mozilla/BasePrincipal.h"
23 #include "mozilla/ChaosMode.h"
24 #include "mozilla/ClearOnShutdown.h"
25 #include "mozilla/LoadInfo.h"
26 #include "mozilla/NullPrincipal.h"
27 #include "mozilla/Preferences.h"
28 #include "mozilla/ProfilerLabels.h"
29 #include "mozilla/StaticPrefs_image.h"
30 #include "mozilla/StaticPrefs_network.h"
31 #include "mozilla/StoragePrincipalHelper.h"
32 #include "mozilla/dom/ContentParent.h"
33 #include "mozilla/dom/nsMixedContentBlocker.h"
34 #include "mozilla/image/ImageMemoryReporter.h"
35 #include "mozilla/layers/CompositorManagerChild.h"
36 #include "nsCOMPtr.h"
37 #include "nsCRT.h"
38 #include "nsComponentManagerUtils.h"
39 #include "nsContentPolicyUtils.h"
40 #include "nsContentUtils.h"
41 #include "nsHttpChannel.h"
42 #include "nsIAsyncVerifyRedirectCallback.h"
43 #include "nsICacheInfoChannel.h"
44 #include "nsIChannelEventSink.h"
45 #include "nsIClassOfService.h"
46 #include "nsIEffectiveTLDService.h"
47 #include "nsIFile.h"
48 #include "nsIFileURL.h"
49 #include "nsIHttpChannel.h"
50 #include "nsIInterfaceRequestor.h"
51 #include "nsIInterfaceRequestorUtils.h"
52 #include "nsIMemoryReporter.h"
53 #include "nsINetworkPredictor.h"
54 #include "nsIProgressEventSink.h"
55 #include "nsIProtocolHandler.h"
56 #include "nsImageModule.h"
57 #include "nsMediaSniffer.h"
58 #include "nsMimeTypes.h"
59 #include "nsNetCID.h"
60 #include "nsNetUtil.h"
61 #include "nsProxyRelease.h"
62 #include "nsQueryObject.h"
63 #include "nsReadableUtils.h"
64 #include "nsStreamUtils.h"
65 #include "prtime.h"
67 // we want to explore making the document own the load group
68 // so we can associate the document URI with the load group.
69 // until this point, we have an evil hack:
70 #include "nsIHttpChannelInternal.h"
71 #include "nsILoadGroupChild.h"
72 #include "nsIDocShell.h"
74 using namespace mozilla;
75 using namespace mozilla::dom;
76 using namespace mozilla::image;
77 using namespace mozilla::net;
79 MOZ_DEFINE_MALLOC_SIZE_OF(ImagesMallocSizeOf)
81 class imgMemoryReporter final : public nsIMemoryReporter {
82 ~imgMemoryReporter() = default;
84 public:
85 NS_DECL_ISUPPORTS
87 NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
88 nsISupports* aData, bool aAnonymize) override {
89 MOZ_ASSERT(NS_IsMainThread());
91 layers::CompositorManagerChild* manager =
92 mozilla::layers::CompositorManagerChild::GetInstance();
93 if (!manager || !StaticPrefs::image_mem_debug_reporting()) {
94 layers::SharedSurfacesMemoryReport sharedSurfaces;
95 FinishCollectReports(aHandleReport, aData, aAnonymize, sharedSurfaces);
96 return NS_OK;
99 RefPtr<imgMemoryReporter> self(this);
100 nsCOMPtr<nsIHandleReportCallback> handleReport(aHandleReport);
101 nsCOMPtr<nsISupports> data(aData);
102 manager->SendReportSharedSurfacesMemory(
103 [=](layers::SharedSurfacesMemoryReport aReport) {
104 self->FinishCollectReports(handleReport, data, aAnonymize, aReport);
106 [=](mozilla::ipc::ResponseRejectReason&& aReason) {
107 layers::SharedSurfacesMemoryReport sharedSurfaces;
108 self->FinishCollectReports(handleReport, data, aAnonymize,
109 sharedSurfaces);
111 return NS_OK;
114 void FinishCollectReports(
115 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
116 bool aAnonymize, layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
117 nsTArray<ImageMemoryCounter> chrome;
118 nsTArray<ImageMemoryCounter> content;
119 nsTArray<ImageMemoryCounter> uncached;
121 for (uint32_t i = 0; i < mKnownLoaders.Length(); i++) {
122 for (imgCacheEntry* entry : mKnownLoaders[i]->mChromeCache.Values()) {
123 RefPtr<imgRequest> req = entry->GetRequest();
124 RecordCounterForRequest(req, &chrome, !entry->HasNoProxies());
126 for (imgCacheEntry* entry : mKnownLoaders[i]->mCache.Values()) {
127 RefPtr<imgRequest> req = entry->GetRequest();
128 RecordCounterForRequest(req, &content, !entry->HasNoProxies());
130 MutexAutoLock lock(mKnownLoaders[i]->mUncachedImagesMutex);
131 for (RefPtr<imgRequest> req : mKnownLoaders[i]->mUncachedImages) {
132 RecordCounterForRequest(req, &uncached, req->HasConsumers());
136 // Note that we only need to anonymize content image URIs.
138 ReportCounterArray(aHandleReport, aData, chrome, "images/chrome",
139 /* aAnonymize */ false, aSharedSurfaces);
141 ReportCounterArray(aHandleReport, aData, content, "images/content",
142 aAnonymize, aSharedSurfaces);
144 // Uncached images may be content or chrome, so anonymize them.
145 ReportCounterArray(aHandleReport, aData, uncached, "images/uncached",
146 aAnonymize, aSharedSurfaces);
148 // Report any shared surfaces that were not merged with the surface cache.
149 ImageMemoryReporter::ReportSharedSurfaces(aHandleReport, aData,
150 aSharedSurfaces);
152 nsCOMPtr<nsIMemoryReporterManager> imgr =
153 do_GetService("@mozilla.org/memory-reporter-manager;1");
154 if (imgr) {
155 imgr->EndReport();
159 static int64_t ImagesContentUsedUncompressedDistinguishedAmount() {
160 size_t n = 0;
161 for (uint32_t i = 0; i < imgLoader::sMemReporter->mKnownLoaders.Length();
162 i++) {
163 for (imgCacheEntry* entry :
164 imgLoader::sMemReporter->mKnownLoaders[i]->mCache.Values()) {
165 if (entry->HasNoProxies()) {
166 continue;
169 RefPtr<imgRequest> req = entry->GetRequest();
170 RefPtr<image::Image> image = req->GetImage();
171 if (!image) {
172 continue;
175 // Both this and EntryImageSizes measure
176 // images/content/raster/used/decoded memory. This function's
177 // measurement is secondary -- the result doesn't go in the "explicit"
178 // tree -- so we use moz_malloc_size_of instead of ImagesMallocSizeOf to
179 // prevent DMD from seeing it reported twice.
180 SizeOfState state(moz_malloc_size_of);
181 ImageMemoryCounter counter(req, image, state, /* aIsUsed = */ true);
183 n += counter.Values().DecodedHeap();
184 n += counter.Values().DecodedNonHeap();
185 n += counter.Values().DecodedUnknown();
188 return n;
191 void RegisterLoader(imgLoader* aLoader) {
192 mKnownLoaders.AppendElement(aLoader);
195 void UnregisterLoader(imgLoader* aLoader) {
196 mKnownLoaders.RemoveElement(aLoader);
199 private:
200 nsTArray<imgLoader*> mKnownLoaders;
202 struct MemoryTotal {
203 MemoryTotal& operator+=(const ImageMemoryCounter& aImageCounter) {
204 if (aImageCounter.Type() == imgIContainer::TYPE_RASTER) {
205 if (aImageCounter.IsUsed()) {
206 mUsedRasterCounter += aImageCounter.Values();
207 } else {
208 mUnusedRasterCounter += aImageCounter.Values();
210 } else if (aImageCounter.Type() == imgIContainer::TYPE_VECTOR) {
211 if (aImageCounter.IsUsed()) {
212 mUsedVectorCounter += aImageCounter.Values();
213 } else {
214 mUnusedVectorCounter += aImageCounter.Values();
216 } else if (aImageCounter.Type() == imgIContainer::TYPE_REQUEST) {
217 // Nothing to do, we did not get to the point of having an image.
218 } else {
219 MOZ_CRASH("Unexpected image type");
222 return *this;
225 const MemoryCounter& UsedRaster() const { return mUsedRasterCounter; }
226 const MemoryCounter& UnusedRaster() const { return mUnusedRasterCounter; }
227 const MemoryCounter& UsedVector() const { return mUsedVectorCounter; }
228 const MemoryCounter& UnusedVector() const { return mUnusedVectorCounter; }
230 private:
231 MemoryCounter mUsedRasterCounter;
232 MemoryCounter mUnusedRasterCounter;
233 MemoryCounter mUsedVectorCounter;
234 MemoryCounter mUnusedVectorCounter;
237 // Reports all images of a single kind, e.g. all used chrome images.
238 void ReportCounterArray(nsIHandleReportCallback* aHandleReport,
239 nsISupports* aData,
240 nsTArray<ImageMemoryCounter>& aCounterArray,
241 const char* aPathPrefix, bool aAnonymize,
242 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
243 MemoryTotal summaryTotal;
244 MemoryTotal nonNotableTotal;
246 // Report notable images, and compute total and non-notable aggregate sizes.
247 for (uint32_t i = 0; i < aCounterArray.Length(); i++) {
248 ImageMemoryCounter& counter = aCounterArray[i];
250 if (aAnonymize) {
251 counter.URI().Truncate();
252 counter.URI().AppendPrintf("<anonymized-%u>", i);
253 } else {
254 // The URI could be an extremely long data: URI. Truncate if needed.
255 static const size_t max = 256;
256 if (counter.URI().Length() > max) {
257 counter.URI().Truncate(max);
258 counter.URI().AppendLiteral(" (truncated)");
260 counter.URI().ReplaceChar('/', '\\');
263 summaryTotal += counter;
265 if (counter.IsNotable() || StaticPrefs::image_mem_debug_reporting()) {
266 ReportImage(aHandleReport, aData, aPathPrefix, counter,
267 aSharedSurfaces);
268 } else {
269 ImageMemoryReporter::TrimSharedSurfaces(counter, aSharedSurfaces);
270 nonNotableTotal += counter;
274 // Report non-notable images in aggregate.
275 ReportTotal(aHandleReport, aData, /* aExplicit = */ true, aPathPrefix,
276 "<non-notable images>/", nonNotableTotal);
278 // Report a summary in aggregate, outside of the explicit tree.
279 ReportTotal(aHandleReport, aData, /* aExplicit = */ false, aPathPrefix, "",
280 summaryTotal);
283 static void ReportImage(nsIHandleReportCallback* aHandleReport,
284 nsISupports* aData, const char* aPathPrefix,
285 const ImageMemoryCounter& aCounter,
286 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
287 nsAutoCString pathPrefix("explicit/"_ns);
288 pathPrefix.Append(aPathPrefix);
290 switch (aCounter.Type()) {
291 case imgIContainer::TYPE_RASTER:
292 pathPrefix.AppendLiteral("/raster/");
293 break;
294 case imgIContainer::TYPE_VECTOR:
295 pathPrefix.AppendLiteral("/vector/");
296 break;
297 case imgIContainer::TYPE_REQUEST:
298 pathPrefix.AppendLiteral("/request/");
299 break;
300 default:
301 pathPrefix.AppendLiteral("/unknown=");
302 pathPrefix.AppendInt(aCounter.Type());
303 pathPrefix.AppendLiteral("/");
304 break;
307 pathPrefix.Append(aCounter.IsUsed() ? "used/" : "unused/");
308 if (aCounter.IsValidating()) {
309 pathPrefix.AppendLiteral("validating/");
311 if (aCounter.HasError()) {
312 pathPrefix.AppendLiteral("err/");
315 pathPrefix.AppendLiteral("progress=");
316 pathPrefix.AppendInt(aCounter.Progress(), 16);
317 pathPrefix.AppendLiteral("/");
319 pathPrefix.AppendLiteral("image(");
320 pathPrefix.AppendInt(aCounter.IntrinsicSize().width);
321 pathPrefix.AppendLiteral("x");
322 pathPrefix.AppendInt(aCounter.IntrinsicSize().height);
323 pathPrefix.AppendLiteral(", ");
325 if (aCounter.URI().IsEmpty()) {
326 pathPrefix.AppendLiteral("<unknown URI>");
327 } else {
328 pathPrefix.Append(aCounter.URI());
331 pathPrefix.AppendLiteral(")/");
333 ReportSurfaces(aHandleReport, aData, pathPrefix, aCounter, aSharedSurfaces);
335 ReportSourceValue(aHandleReport, aData, pathPrefix, aCounter.Values());
338 static void ReportSurfaces(
339 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
340 const nsACString& aPathPrefix, const ImageMemoryCounter& aCounter,
341 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
342 for (const SurfaceMemoryCounter& counter : aCounter.Surfaces()) {
343 nsAutoCString surfacePathPrefix(aPathPrefix);
344 switch (counter.Type()) {
345 case SurfaceMemoryCounterType::NORMAL:
346 if (counter.IsLocked()) {
347 surfacePathPrefix.AppendLiteral("locked/");
348 } else {
349 surfacePathPrefix.AppendLiteral("unlocked/");
351 if (counter.IsFactor2()) {
352 surfacePathPrefix.AppendLiteral("factor2/");
354 if (counter.CannotSubstitute()) {
355 surfacePathPrefix.AppendLiteral("cannot_substitute/");
357 break;
358 case SurfaceMemoryCounterType::CONTAINER:
359 surfacePathPrefix.AppendLiteral("container/");
360 break;
361 default:
362 MOZ_ASSERT_UNREACHABLE("Unknown counter type");
363 break;
366 surfacePathPrefix.AppendLiteral("types=");
367 surfacePathPrefix.AppendInt(counter.Values().SurfaceTypes(), 16);
368 surfacePathPrefix.AppendLiteral("/surface(");
369 surfacePathPrefix.AppendInt(counter.Key().Size().width);
370 surfacePathPrefix.AppendLiteral("x");
371 surfacePathPrefix.AppendInt(counter.Key().Size().height);
373 if (!counter.IsFinished()) {
374 surfacePathPrefix.AppendLiteral(", incomplete");
377 if (counter.Values().ExternalHandles() > 0) {
378 surfacePathPrefix.AppendLiteral(", handles:");
379 surfacePathPrefix.AppendInt(
380 uint32_t(counter.Values().ExternalHandles()));
383 ImageMemoryReporter::AppendSharedSurfacePrefix(surfacePathPrefix, counter,
384 aSharedSurfaces);
386 PlaybackType playback = counter.Key().Playback();
387 if (playback == PlaybackType::eAnimated) {
388 if (StaticPrefs::image_mem_debug_reporting()) {
389 surfacePathPrefix.AppendPrintf(
390 " (animation %4u)", uint32_t(counter.Values().FrameIndex()));
391 } else {
392 surfacePathPrefix.AppendLiteral(" (animation)");
396 if (counter.Key().Flags() != DefaultSurfaceFlags()) {
397 surfacePathPrefix.AppendLiteral(", flags:");
398 surfacePathPrefix.AppendInt(uint32_t(counter.Key().Flags()),
399 /* aRadix = */ 16);
402 if (counter.Key().Region()) {
403 const ImageIntRegion& region = counter.Key().Region().ref();
404 const gfx::IntRect& rect = region.Rect();
405 surfacePathPrefix.AppendLiteral(", region:[ rect=(");
406 surfacePathPrefix.AppendInt(rect.x);
407 surfacePathPrefix.AppendLiteral(",");
408 surfacePathPrefix.AppendInt(rect.y);
409 surfacePathPrefix.AppendLiteral(") ");
410 surfacePathPrefix.AppendInt(rect.width);
411 surfacePathPrefix.AppendLiteral("x");
412 surfacePathPrefix.AppendInt(rect.height);
413 if (region.IsRestricted()) {
414 const gfx::IntRect& restrict = region.Restriction();
415 if (restrict == rect) {
416 surfacePathPrefix.AppendLiteral(", restrict=rect");
417 } else {
418 surfacePathPrefix.AppendLiteral(", restrict=(");
419 surfacePathPrefix.AppendInt(restrict.x);
420 surfacePathPrefix.AppendLiteral(",");
421 surfacePathPrefix.AppendInt(restrict.y);
422 surfacePathPrefix.AppendLiteral(") ");
423 surfacePathPrefix.AppendInt(restrict.width);
424 surfacePathPrefix.AppendLiteral("x");
425 surfacePathPrefix.AppendInt(restrict.height);
428 if (region.GetExtendMode() != gfx::ExtendMode::CLAMP) {
429 surfacePathPrefix.AppendLiteral(", extendMode=");
430 surfacePathPrefix.AppendInt(int32_t(region.GetExtendMode()));
432 surfacePathPrefix.AppendLiteral("]");
435 if (counter.Key().SVGContext()) {
436 const SVGImageContext& context = counter.Key().SVGContext().ref();
437 surfacePathPrefix.AppendLiteral(", svgContext:[ ");
438 if (context.GetViewportSize()) {
439 const CSSIntSize& size = context.GetViewportSize().ref();
440 surfacePathPrefix.AppendLiteral("viewport=(");
441 surfacePathPrefix.AppendInt(size.width);
442 surfacePathPrefix.AppendLiteral("x");
443 surfacePathPrefix.AppendInt(size.height);
444 surfacePathPrefix.AppendLiteral(") ");
446 if (context.GetPreserveAspectRatio()) {
447 nsAutoString aspect;
448 context.GetPreserveAspectRatio()->ToString(aspect);
449 surfacePathPrefix.AppendLiteral("preserveAspectRatio=(");
450 LossyAppendUTF16toASCII(aspect, surfacePathPrefix);
451 surfacePathPrefix.AppendLiteral(") ");
453 if (auto scheme = context.GetColorScheme()) {
454 surfacePathPrefix.AppendLiteral("colorScheme=");
455 surfacePathPrefix.AppendInt(int32_t(*scheme));
456 surfacePathPrefix.AppendLiteral(" ");
458 if (context.GetContextPaint()) {
459 const SVGEmbeddingContextPaint* paint = context.GetContextPaint();
460 surfacePathPrefix.AppendLiteral("contextPaint=(");
461 if (paint->GetFill()) {
462 surfacePathPrefix.AppendLiteral(" fill=");
463 surfacePathPrefix.AppendInt(paint->GetFill()->ToABGR(), 16);
465 if (paint->GetFillOpacity() != 1.0) {
466 surfacePathPrefix.AppendLiteral(" fillOpa=");
467 surfacePathPrefix.AppendFloat(paint->GetFillOpacity());
469 if (paint->GetStroke()) {
470 surfacePathPrefix.AppendLiteral(" stroke=");
471 surfacePathPrefix.AppendInt(paint->GetStroke()->ToABGR(), 16);
473 if (paint->GetStrokeOpacity() != 1.0) {
474 surfacePathPrefix.AppendLiteral(" strokeOpa=");
475 surfacePathPrefix.AppendFloat(paint->GetStrokeOpacity());
477 surfacePathPrefix.AppendLiteral(" ) ");
479 surfacePathPrefix.AppendLiteral("]");
482 surfacePathPrefix.AppendLiteral(")/");
484 ReportValues(aHandleReport, aData, surfacePathPrefix, counter.Values());
488 static void ReportTotal(nsIHandleReportCallback* aHandleReport,
489 nsISupports* aData, bool aExplicit,
490 const char* aPathPrefix, const char* aPathInfix,
491 const MemoryTotal& aTotal) {
492 nsAutoCString pathPrefix;
493 if (aExplicit) {
494 pathPrefix.AppendLiteral("explicit/");
496 pathPrefix.Append(aPathPrefix);
498 nsAutoCString rasterUsedPrefix(pathPrefix);
499 rasterUsedPrefix.AppendLiteral("/raster/used/");
500 rasterUsedPrefix.Append(aPathInfix);
501 ReportValues(aHandleReport, aData, rasterUsedPrefix, aTotal.UsedRaster());
503 nsAutoCString rasterUnusedPrefix(pathPrefix);
504 rasterUnusedPrefix.AppendLiteral("/raster/unused/");
505 rasterUnusedPrefix.Append(aPathInfix);
506 ReportValues(aHandleReport, aData, rasterUnusedPrefix,
507 aTotal.UnusedRaster());
509 nsAutoCString vectorUsedPrefix(pathPrefix);
510 vectorUsedPrefix.AppendLiteral("/vector/used/");
511 vectorUsedPrefix.Append(aPathInfix);
512 ReportValues(aHandleReport, aData, vectorUsedPrefix, aTotal.UsedVector());
514 nsAutoCString vectorUnusedPrefix(pathPrefix);
515 vectorUnusedPrefix.AppendLiteral("/vector/unused/");
516 vectorUnusedPrefix.Append(aPathInfix);
517 ReportValues(aHandleReport, aData, vectorUnusedPrefix,
518 aTotal.UnusedVector());
521 static void ReportValues(nsIHandleReportCallback* aHandleReport,
522 nsISupports* aData, const nsACString& aPathPrefix,
523 const MemoryCounter& aCounter) {
524 ReportSourceValue(aHandleReport, aData, aPathPrefix, aCounter);
526 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "decoded-heap",
527 "Decoded image data which is stored on the heap.",
528 aCounter.DecodedHeap());
530 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
531 "decoded-nonheap",
532 "Decoded image data which isn't stored on the heap.",
533 aCounter.DecodedNonHeap());
535 // We don't know for certain whether or not it is on the heap, so let's
536 // just report it as non-heap for reporting purposes.
537 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
538 "decoded-unknown",
539 "Decoded image data which is unknown to be on the heap or not.",
540 aCounter.DecodedUnknown());
543 static void ReportSourceValue(nsIHandleReportCallback* aHandleReport,
544 nsISupports* aData,
545 const nsACString& aPathPrefix,
546 const MemoryCounter& aCounter) {
547 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "source",
548 "Raster image source data and vector image documents.",
549 aCounter.Source());
552 static void ReportValue(nsIHandleReportCallback* aHandleReport,
553 nsISupports* aData, int32_t aKind,
554 const nsACString& aPathPrefix,
555 const char* aPathSuffix, const char* aDescription,
556 size_t aValue) {
557 if (aValue == 0) {
558 return;
561 nsAutoCString desc(aDescription);
562 nsAutoCString path(aPathPrefix);
563 path.Append(aPathSuffix);
565 aHandleReport->Callback(""_ns, path, aKind, UNITS_BYTES, aValue, desc,
566 aData);
569 static void RecordCounterForRequest(imgRequest* aRequest,
570 nsTArray<ImageMemoryCounter>* aArray,
571 bool aIsUsed) {
572 SizeOfState state(ImagesMallocSizeOf);
573 RefPtr<image::Image> image = aRequest->GetImage();
574 if (image) {
575 ImageMemoryCounter counter(aRequest, image, state, aIsUsed);
576 aArray->AppendElement(std::move(counter));
577 } else {
578 // We can at least record some information about the image from the
579 // request, and mark it as not knowing the image type yet.
580 ImageMemoryCounter counter(aRequest, state, aIsUsed);
581 aArray->AppendElement(std::move(counter));
586 NS_IMPL_ISUPPORTS(imgMemoryReporter, nsIMemoryReporter)
588 NS_IMPL_ISUPPORTS(nsProgressNotificationProxy, nsIProgressEventSink,
589 nsIChannelEventSink, nsIInterfaceRequestor)
591 NS_IMETHODIMP
592 nsProgressNotificationProxy::OnProgress(nsIRequest* request, int64_t progress,
593 int64_t progressMax) {
594 nsCOMPtr<nsILoadGroup> loadGroup;
595 request->GetLoadGroup(getter_AddRefs(loadGroup));
597 nsCOMPtr<nsIProgressEventSink> target;
598 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
599 NS_GET_IID(nsIProgressEventSink),
600 getter_AddRefs(target));
601 if (!target) {
602 return NS_OK;
604 return target->OnProgress(mImageRequest, progress, progressMax);
607 NS_IMETHODIMP
608 nsProgressNotificationProxy::OnStatus(nsIRequest* request, nsresult status,
609 const char16_t* statusArg) {
610 nsCOMPtr<nsILoadGroup> loadGroup;
611 request->GetLoadGroup(getter_AddRefs(loadGroup));
613 nsCOMPtr<nsIProgressEventSink> target;
614 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
615 NS_GET_IID(nsIProgressEventSink),
616 getter_AddRefs(target));
617 if (!target) {
618 return NS_OK;
620 return target->OnStatus(mImageRequest, status, statusArg);
623 NS_IMETHODIMP
624 nsProgressNotificationProxy::AsyncOnChannelRedirect(
625 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
626 nsIAsyncVerifyRedirectCallback* cb) {
627 // Tell the original original callbacks about it too
628 nsCOMPtr<nsILoadGroup> loadGroup;
629 newChannel->GetLoadGroup(getter_AddRefs(loadGroup));
630 nsCOMPtr<nsIChannelEventSink> target;
631 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
632 NS_GET_IID(nsIChannelEventSink),
633 getter_AddRefs(target));
634 if (!target) {
635 cb->OnRedirectVerifyCallback(NS_OK);
636 return NS_OK;
639 // Delegate to |target| if set, reusing |cb|
640 return target->AsyncOnChannelRedirect(oldChannel, newChannel, flags, cb);
643 NS_IMETHODIMP
644 nsProgressNotificationProxy::GetInterface(const nsIID& iid, void** result) {
645 if (iid.Equals(NS_GET_IID(nsIProgressEventSink))) {
646 *result = static_cast<nsIProgressEventSink*>(this);
647 NS_ADDREF_THIS();
648 return NS_OK;
650 if (iid.Equals(NS_GET_IID(nsIChannelEventSink))) {
651 *result = static_cast<nsIChannelEventSink*>(this);
652 NS_ADDREF_THIS();
653 return NS_OK;
655 if (mOriginalCallbacks) {
656 return mOriginalCallbacks->GetInterface(iid, result);
658 return NS_NOINTERFACE;
661 static void NewRequestAndEntry(bool aForcePrincipalCheckForCacheEntry,
662 imgLoader* aLoader, const ImageCacheKey& aKey,
663 imgRequest** aRequest, imgCacheEntry** aEntry) {
664 RefPtr<imgRequest> request = new imgRequest(aLoader, aKey);
665 RefPtr<imgCacheEntry> entry =
666 new imgCacheEntry(aLoader, request, aForcePrincipalCheckForCacheEntry);
667 aLoader->AddToUncachedImages(request);
668 request.forget(aRequest);
669 entry.forget(aEntry);
672 static bool ShouldRevalidateEntry(imgCacheEntry* aEntry, nsLoadFlags aFlags,
673 bool aHasExpired) {
674 if (aFlags & nsIRequest::LOAD_BYPASS_CACHE) {
675 return false;
677 if (aFlags & nsIRequest::VALIDATE_ALWAYS) {
678 return true;
680 if (aEntry->GetMustValidate()) {
681 return true;
683 if (aHasExpired) {
684 // The cache entry has expired... Determine whether the stale cache
685 // entry can be used without validation...
686 if (aFlags & (nsIRequest::LOAD_FROM_CACHE | nsIRequest::VALIDATE_NEVER |
687 nsIRequest::VALIDATE_ONCE_PER_SESSION)) {
688 // LOAD_FROM_CACHE, VALIDATE_NEVER and VALIDATE_ONCE_PER_SESSION allow
689 // stale cache entries to be used unless they have been explicitly marked
690 // to indicate that revalidation is necessary.
691 return false;
693 // Entry is expired, revalidate.
694 return true;
696 return false;
699 /* Call content policies on cached images that went through a redirect */
700 static bool ShouldLoadCachedImage(imgRequest* aImgRequest,
701 Document* aLoadingDocument,
702 nsIPrincipal* aTriggeringPrincipal,
703 nsContentPolicyType aPolicyType,
704 bool aSendCSPViolationReports) {
705 /* Call content policies on cached images - Bug 1082837
706 * Cached images are keyed off of the first uri in a redirect chain.
707 * Hence content policies don't get a chance to test the intermediate hops
708 * or the final destination. Here we test the final destination using
709 * mFinalURI off of the imgRequest and passing it into content policies.
710 * For Mixed Content Blocker, we do an additional check to determine if any
711 * of the intermediary hops went through an insecure redirect with the
712 * mHadInsecureRedirect flag
714 bool insecureRedirect = aImgRequest->HadInsecureRedirect();
715 nsCOMPtr<nsIURI> contentLocation;
716 aImgRequest->GetFinalURI(getter_AddRefs(contentLocation));
717 nsresult rv;
719 nsCOMPtr<nsIPrincipal> loadingPrincipal =
720 aLoadingDocument ? aLoadingDocument->NodePrincipal()
721 : aTriggeringPrincipal;
722 // If there is no context and also no triggeringPrincipal, then we use a fresh
723 // nullPrincipal as the loadingPrincipal because we can not create a loadinfo
724 // without a valid loadingPrincipal.
725 if (!loadingPrincipal) {
726 loadingPrincipal = NullPrincipal::CreateWithoutOriginAttributes();
729 nsCOMPtr<nsILoadInfo> secCheckLoadInfo = new LoadInfo(
730 loadingPrincipal, aTriggeringPrincipal, aLoadingDocument,
731 nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK, aPolicyType);
733 secCheckLoadInfo->SetSendCSPViolationEvents(aSendCSPViolationReports);
735 int16_t decision = nsIContentPolicy::REJECT_REQUEST;
736 rv = NS_CheckContentLoadPolicy(contentLocation, secCheckLoadInfo,
737 ""_ns, // mime guess
738 &decision, nsContentUtils::GetContentPolicy());
739 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
740 return false;
743 // We call all Content Policies above, but we also have to call mcb
744 // individually to check the intermediary redirect hops are secure.
745 if (insecureRedirect) {
746 // Bug 1314356: If the image ended up in the cache upgraded by HSTS and the
747 // page uses upgrade-inscure-requests it had an insecure redirect
748 // (http->https). We need to invalidate the image and reload it because
749 // mixed content blocker only bails if upgrade-insecure-requests is set on
750 // the doc and the resource load is http: which would result in an incorrect
751 // mixed content warning.
752 nsCOMPtr<nsIDocShell> docShell =
753 NS_CP_GetDocShellFromContext(ToSupports(aLoadingDocument));
754 if (docShell) {
755 Document* document = docShell->GetDocument();
756 if (document && document->GetUpgradeInsecureRequests(false)) {
757 return false;
761 if (!aTriggeringPrincipal || !aTriggeringPrincipal->IsSystemPrincipal()) {
762 // reset the decision for mixed content blocker check
763 decision = nsIContentPolicy::REJECT_REQUEST;
764 rv = nsMixedContentBlocker::ShouldLoad(insecureRedirect, contentLocation,
765 secCheckLoadInfo,
766 ""_ns, // mime guess
767 true, // aReportError
768 &decision);
769 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
770 return false;
775 return true;
778 // Returns true if this request is compatible with the given CORS mode on the
779 // given loading principal, and false if the request may not be reused due
780 // to CORS.
781 static bool ValidateCORSMode(imgRequest* aRequest, bool aForcePrincipalCheck,
782 CORSMode aCORSMode,
783 nsIPrincipal* aTriggeringPrincipal) {
784 // If the entry's CORS mode doesn't match, or the CORS mode matches but the
785 // document principal isn't the same, we can't use this request.
786 if (aRequest->GetCORSMode() != aCORSMode) {
787 return false;
790 if (aRequest->GetCORSMode() != CORS_NONE || aForcePrincipalCheck) {
791 nsCOMPtr<nsIPrincipal> otherprincipal = aRequest->GetTriggeringPrincipal();
793 // If we previously had a principal, but we don't now, we can't use this
794 // request.
795 if (otherprincipal && !aTriggeringPrincipal) {
796 return false;
799 if (otherprincipal && aTriggeringPrincipal &&
800 !otherprincipal->Equals(aTriggeringPrincipal)) {
801 return false;
805 return true;
808 static bool ValidateSecurityInfo(imgRequest* aRequest,
809 bool aForcePrincipalCheck, CORSMode aCORSMode,
810 nsIPrincipal* aTriggeringPrincipal,
811 Document* aLoadingDocument,
812 nsContentPolicyType aPolicyType) {
813 if (!ValidateCORSMode(aRequest, aForcePrincipalCheck, aCORSMode,
814 aTriggeringPrincipal)) {
815 return false;
817 // Content Policy Check on Cached Images
818 return ShouldLoadCachedImage(aRequest, aLoadingDocument, aTriggeringPrincipal,
819 aPolicyType,
820 /* aSendCSPViolationReports */ false);
823 static nsresult NewImageChannel(
824 nsIChannel** aResult,
825 // If aForcePrincipalCheckForCacheEntry is true, then we will
826 // force a principal check even when not using CORS before
827 // assuming we have a cache hit on a cache entry that we
828 // create for this channel. This is an out param that should
829 // be set to true if this channel ends up depending on
830 // aTriggeringPrincipal and false otherwise.
831 bool* aForcePrincipalCheckForCacheEntry, nsIURI* aURI,
832 nsIURI* aInitialDocumentURI, CORSMode aCORSMode,
833 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
834 nsLoadFlags aLoadFlags, nsContentPolicyType aPolicyType,
835 nsIPrincipal* aTriggeringPrincipal, nsINode* aRequestingNode,
836 bool aRespectPrivacy) {
837 MOZ_ASSERT(aResult);
839 nsresult rv;
840 nsCOMPtr<nsIHttpChannel> newHttpChannel;
842 nsCOMPtr<nsIInterfaceRequestor> callbacks;
844 if (aLoadGroup) {
845 // Get the notification callbacks from the load group for the new channel.
847 // XXX: This is not exactly correct, because the network request could be
848 // referenced by multiple windows... However, the new channel needs
849 // something. So, using the 'first' notification callbacks is better
850 // than nothing...
852 aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
855 // Pass in a nullptr loadgroup because this is the underlying network
856 // request. This request may be referenced by several proxy image requests
857 // (possibly in different documents).
858 // If all of the proxy requests are canceled then this request should be
859 // canceled too.
862 nsSecurityFlags securityFlags =
863 aCORSMode == CORS_NONE
864 ? nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT
865 : nsILoadInfo::SEC_REQUIRE_CORS_INHERITS_SEC_CONTEXT;
866 if (aCORSMode == CORS_ANONYMOUS) {
867 securityFlags |= nsILoadInfo::SEC_COOKIES_SAME_ORIGIN;
868 } else if (aCORSMode == CORS_USE_CREDENTIALS) {
869 securityFlags |= nsILoadInfo::SEC_COOKIES_INCLUDE;
871 securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
873 // Note we are calling NS_NewChannelWithTriggeringPrincipal() here with a
874 // node and a principal. This is for things like background images that are
875 // specified by user stylesheets, where the document is being styled, but
876 // the principal is that of the user stylesheet.
877 if (aRequestingNode && aTriggeringPrincipal) {
878 rv = NS_NewChannelWithTriggeringPrincipal(aResult, aURI, aRequestingNode,
879 aTriggeringPrincipal,
880 securityFlags, aPolicyType,
881 nullptr, // PerformanceStorage
882 nullptr, // loadGroup
883 callbacks, aLoadFlags);
885 if (NS_FAILED(rv)) {
886 return rv;
889 if (aPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
890 // If this is a favicon loading, we will use the originAttributes from the
891 // triggeringPrincipal as the channel's originAttributes. This allows the
892 // favicon loading from XUL will use the correct originAttributes.
894 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
895 rv = loadInfo->SetOriginAttributes(
896 aTriggeringPrincipal->OriginAttributesRef());
898 } else {
899 // either we are loading something inside a document, in which case
900 // we should always have a requestingNode, or we are loading something
901 // outside a document, in which case the triggeringPrincipal and
902 // triggeringPrincipal should always be the systemPrincipal.
903 // However, there are exceptions: one is Notifications which create a
904 // channel in the parent process in which case we can't get a
905 // requestingNode.
906 rv = NS_NewChannel(aResult, aURI, nsContentUtils::GetSystemPrincipal(),
907 securityFlags, aPolicyType,
908 nullptr, // nsICookieJarSettings
909 nullptr, // PerformanceStorage
910 nullptr, // loadGroup
911 callbacks, aLoadFlags);
913 if (NS_FAILED(rv)) {
914 return rv;
917 // Use the OriginAttributes from the loading principal, if one is available,
918 // and adjust the private browsing ID based on what kind of load the caller
919 // has asked us to perform.
920 OriginAttributes attrs;
921 if (aTriggeringPrincipal) {
922 attrs = aTriggeringPrincipal->OriginAttributesRef();
924 attrs.mPrivateBrowsingId = aRespectPrivacy ? 1 : 0;
926 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
927 rv = loadInfo->SetOriginAttributes(attrs);
930 if (NS_FAILED(rv)) {
931 return rv;
934 // only inherit if we have a principal
935 *aForcePrincipalCheckForCacheEntry =
936 aTriggeringPrincipal && nsContentUtils::ChannelShouldInheritPrincipal(
937 aTriggeringPrincipal, aURI,
938 /* aInheritForAboutBlank */ false,
939 /* aForceInherit */ false);
941 // Initialize HTTP-specific attributes
942 newHttpChannel = do_QueryInterface(*aResult);
943 if (newHttpChannel) {
944 nsCOMPtr<nsIHttpChannelInternal> httpChannelInternal =
945 do_QueryInterface(newHttpChannel);
946 NS_ENSURE_TRUE(httpChannelInternal, NS_ERROR_UNEXPECTED);
947 rv = httpChannelInternal->SetDocumentURI(aInitialDocumentURI);
948 MOZ_ASSERT(NS_SUCCEEDED(rv));
949 if (aReferrerInfo) {
950 DebugOnly<nsresult> rv = newHttpChannel->SetReferrerInfo(aReferrerInfo);
951 MOZ_ASSERT(NS_SUCCEEDED(rv));
955 // Image channels are loaded by default with reduced priority.
956 nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(*aResult);
957 if (p) {
958 uint32_t priority = nsISupportsPriority::PRIORITY_LOW;
960 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
961 ++priority; // further reduce priority for background loads
964 p->AdjustPriority(priority);
967 // Create a new loadgroup for this new channel, using the old group as
968 // the parent. The indirection keeps the channel insulated from cancels,
969 // but does allow a way for this revalidation to be associated with at
970 // least one base load group for scheduling/caching purposes.
972 nsCOMPtr<nsILoadGroup> loadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID);
973 nsCOMPtr<nsILoadGroupChild> childLoadGroup = do_QueryInterface(loadGroup);
974 if (childLoadGroup) {
975 childLoadGroup->SetParentLoadGroup(aLoadGroup);
977 (*aResult)->SetLoadGroup(loadGroup);
979 return NS_OK;
982 static uint32_t SecondsFromPRTime(PRTime aTime) {
983 return nsContentUtils::SecondsFromPRTime(aTime);
986 /* static */
987 imgCacheEntry::imgCacheEntry(imgLoader* loader, imgRequest* request,
988 bool forcePrincipalCheck)
989 : mLoader(loader),
990 mRequest(request),
991 mDataSize(0),
992 mTouchedTime(SecondsFromPRTime(PR_Now())),
993 mLoadTime(SecondsFromPRTime(PR_Now())),
994 mExpiryTime(0),
995 mMustValidate(false),
996 // We start off as evicted so we don't try to update the cache.
997 // PutIntoCache will set this to false.
998 mEvicted(true),
999 mHasNoProxies(true),
1000 mForcePrincipalCheck(forcePrincipalCheck),
1001 mHasNotified(false) {}
1003 imgCacheEntry::~imgCacheEntry() {
1004 LOG_FUNC(gImgLog, "imgCacheEntry::~imgCacheEntry()");
1007 void imgCacheEntry::Touch(bool updateTime /* = true */) {
1008 LOG_SCOPE(gImgLog, "imgCacheEntry::Touch");
1010 if (updateTime) {
1011 mTouchedTime = SecondsFromPRTime(PR_Now());
1014 UpdateCache();
1017 void imgCacheEntry::UpdateCache(int32_t diff /* = 0 */) {
1018 // Don't update the cache if we've been removed from it or it doesn't care
1019 // about our size or usage.
1020 if (!Evicted() && HasNoProxies()) {
1021 mLoader->CacheEntriesChanged(mRequest->IsChrome(), diff);
1025 void imgCacheEntry::UpdateLoadTime() {
1026 mLoadTime = SecondsFromPRTime(PR_Now());
1029 void imgCacheEntry::SetHasNoProxies(bool hasNoProxies) {
1030 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1031 if (hasNoProxies) {
1032 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies true", "uri",
1033 mRequest->CacheKey().URI());
1034 } else {
1035 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies false",
1036 "uri", mRequest->CacheKey().URI());
1040 mHasNoProxies = hasNoProxies;
1043 imgCacheQueue::imgCacheQueue() : mDirty(false), mSize(0) {}
1045 void imgCacheQueue::UpdateSize(int32_t diff) { mSize += diff; }
1047 uint32_t imgCacheQueue::GetSize() const { return mSize; }
1049 void imgCacheQueue::Remove(imgCacheEntry* entry) {
1050 uint64_t index = mQueue.IndexOf(entry);
1051 if (index == queueContainer::NoIndex) {
1052 return;
1055 mSize -= mQueue[index]->GetDataSize();
1057 // If the queue is clean and this is the first entry,
1058 // then we can efficiently remove the entry without
1059 // dirtying the sort order.
1060 if (!IsDirty() && index == 0) {
1061 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1062 mQueue.RemoveLastElement();
1063 return;
1066 // Remove from the middle of the list. This potentially
1067 // breaks the binary heap sort order.
1068 mQueue.RemoveElementAt(index);
1070 // If we only have one entry or the queue is empty, though,
1071 // then the sort order is still effectively good. Simply
1072 // refresh the list to clear the dirty flag.
1073 if (mQueue.Length() <= 1) {
1074 Refresh();
1075 return;
1078 // Otherwise we must mark the queue dirty and potentially
1079 // trigger an expensive sort later.
1080 MarkDirty();
1083 void imgCacheQueue::Push(imgCacheEntry* entry) {
1084 mSize += entry->GetDataSize();
1086 RefPtr<imgCacheEntry> refptr(entry);
1087 mQueue.AppendElement(std::move(refptr));
1088 // If we're not dirty already, then we can efficiently add this to the
1089 // binary heap immediately. This is only O(log n).
1090 if (!IsDirty()) {
1091 std::push_heap(mQueue.begin(), mQueue.end(),
1092 imgLoader::CompareCacheEntries);
1096 already_AddRefed<imgCacheEntry> imgCacheQueue::Pop() {
1097 if (mQueue.IsEmpty()) {
1098 return nullptr;
1100 if (IsDirty()) {
1101 Refresh();
1104 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1105 RefPtr<imgCacheEntry> entry = mQueue.PopLastElement();
1107 mSize -= entry->GetDataSize();
1108 return entry.forget();
1111 void imgCacheQueue::Refresh() {
1112 // Resort the list. This is an O(3 * n) operation and best avoided
1113 // if possible.
1114 std::make_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1115 mDirty = false;
1118 void imgCacheQueue::MarkDirty() { mDirty = true; }
1120 bool imgCacheQueue::IsDirty() { return mDirty; }
1122 uint32_t imgCacheQueue::GetNumElements() const { return mQueue.Length(); }
1124 bool imgCacheQueue::Contains(imgCacheEntry* aEntry) const {
1125 return mQueue.Contains(aEntry);
1128 imgCacheQueue::iterator imgCacheQueue::begin() { return mQueue.begin(); }
1130 imgCacheQueue::const_iterator imgCacheQueue::begin() const {
1131 return mQueue.begin();
1134 imgCacheQueue::iterator imgCacheQueue::end() { return mQueue.end(); }
1136 imgCacheQueue::const_iterator imgCacheQueue::end() const {
1137 return mQueue.end();
1140 nsresult imgLoader::CreateNewProxyForRequest(
1141 imgRequest* aRequest, nsIURI* aURI, nsILoadGroup* aLoadGroup,
1142 Document* aLoadingDocument, imgINotificationObserver* aObserver,
1143 nsLoadFlags aLoadFlags, imgRequestProxy** _retval) {
1144 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::CreateNewProxyForRequest",
1145 "imgRequest", aRequest);
1147 /* XXX If we move decoding onto separate threads, we should save off the
1148 calling thread here and pass it off to |proxyRequest| so that it call
1149 proxy calls to |aObserver|.
1152 RefPtr<imgRequestProxy> proxyRequest = new imgRequestProxy();
1154 /* It is important to call |SetLoadFlags()| before calling |Init()| because
1155 |Init()| adds the request to the loadgroup.
1157 proxyRequest->SetLoadFlags(aLoadFlags);
1159 // init adds itself to imgRequest's list of observers
1160 nsresult rv = proxyRequest->Init(aRequest, aLoadGroup, aLoadingDocument, aURI,
1161 aObserver);
1162 if (NS_WARN_IF(NS_FAILED(rv))) {
1163 return rv;
1166 proxyRequest.forget(_retval);
1167 return NS_OK;
1170 class imgCacheExpirationTracker final
1171 : public nsExpirationTracker<imgCacheEntry, 3> {
1172 enum { TIMEOUT_SECONDS = 10 };
1174 public:
1175 imgCacheExpirationTracker();
1177 protected:
1178 void NotifyExpired(imgCacheEntry* entry) override;
1181 imgCacheExpirationTracker::imgCacheExpirationTracker()
1182 : nsExpirationTracker<imgCacheEntry, 3>(TIMEOUT_SECONDS * 1000,
1183 "imgCacheExpirationTracker") {}
1185 void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry* entry) {
1186 // Hold on to a reference to this entry, because the expiration tracker
1187 // mechanism doesn't.
1188 RefPtr<imgCacheEntry> kungFuDeathGrip(entry);
1190 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1191 RefPtr<imgRequest> req = entry->GetRequest();
1192 if (req) {
1193 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheExpirationTracker::NotifyExpired",
1194 "entry", req->CacheKey().URI());
1198 // We can be called multiple times on the same entry. Don't do work multiple
1199 // times.
1200 if (!entry->Evicted()) {
1201 entry->Loader()->RemoveFromCache(entry);
1204 entry->Loader()->VerifyCacheSizes();
1207 ///////////////////////////////////////////////////////////////////////////////
1208 // imgLoader
1209 ///////////////////////////////////////////////////////////////////////////////
1211 double imgLoader::sCacheTimeWeight;
1212 uint32_t imgLoader::sCacheMaxSize;
1213 imgMemoryReporter* imgLoader::sMemReporter;
1215 NS_IMPL_ISUPPORTS(imgLoader, imgILoader, nsIContentSniffer, imgICache,
1216 nsISupportsWeakReference, nsIObserver)
1218 static imgLoader* gNormalLoader = nullptr;
1219 static imgLoader* gPrivateBrowsingLoader = nullptr;
1221 /* static */
1222 already_AddRefed<imgLoader> imgLoader::CreateImageLoader() {
1223 // In some cases, such as xpctests, XPCOM modules are not automatically
1224 // initialized. We need to make sure that our module is initialized before
1225 // we hand out imgLoader instances and code starts using them.
1226 mozilla::image::EnsureModuleInitialized();
1228 RefPtr<imgLoader> loader = new imgLoader();
1229 loader->Init();
1231 return loader.forget();
1234 imgLoader* imgLoader::NormalLoader() {
1235 if (!gNormalLoader) {
1236 gNormalLoader = CreateImageLoader().take();
1238 return gNormalLoader;
1241 imgLoader* imgLoader::PrivateBrowsingLoader() {
1242 if (!gPrivateBrowsingLoader) {
1243 gPrivateBrowsingLoader = CreateImageLoader().take();
1244 gPrivateBrowsingLoader->RespectPrivacyNotifications();
1246 return gPrivateBrowsingLoader;
1249 imgLoader::imgLoader()
1250 : mUncachedImagesMutex("imgLoader::UncachedImages"),
1251 mRespectPrivacy(false) {
1252 sMemReporter->AddRef();
1253 sMemReporter->RegisterLoader(this);
1256 imgLoader::~imgLoader() {
1257 ClearChromeImageCache();
1258 ClearImageCache();
1260 // If there are any of our imgRequest's left they are in the uncached
1261 // images set, so clear their pointer to us.
1262 MutexAutoLock lock(mUncachedImagesMutex);
1263 for (RefPtr<imgRequest> req : mUncachedImages) {
1264 req->ClearLoader();
1267 sMemReporter->UnregisterLoader(this);
1268 sMemReporter->Release();
1271 void imgLoader::VerifyCacheSizes() {
1272 #ifdef DEBUG
1273 if (!mCacheTracker) {
1274 return;
1277 uint32_t cachesize = mCache.Count() + mChromeCache.Count();
1278 uint32_t queuesize =
1279 mCacheQueue.GetNumElements() + mChromeCacheQueue.GetNumElements();
1280 uint32_t trackersize = 0;
1281 for (nsExpirationTracker<imgCacheEntry, 3>::Iterator it(mCacheTracker.get());
1282 it.Next();) {
1283 trackersize++;
1285 MOZ_ASSERT(queuesize == trackersize, "Queue and tracker sizes out of sync!");
1286 MOZ_ASSERT(queuesize <= cachesize, "Queue has more elements than cache!");
1287 #endif
1290 imgLoader::imgCacheTable& imgLoader::GetCache(bool aForChrome) {
1291 return aForChrome ? mChromeCache : mCache;
1294 imgLoader::imgCacheTable& imgLoader::GetCache(const ImageCacheKey& aKey) {
1295 return GetCache(aKey.IsChrome());
1298 imgCacheQueue& imgLoader::GetCacheQueue(bool aForChrome) {
1299 return aForChrome ? mChromeCacheQueue : mCacheQueue;
1302 imgCacheQueue& imgLoader::GetCacheQueue(const ImageCacheKey& aKey) {
1303 return GetCacheQueue(aKey.IsChrome());
1306 void imgLoader::GlobalInit() {
1307 sCacheTimeWeight = StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
1308 int32_t cachesize = StaticPrefs::image_cache_size_AtStartup();
1309 sCacheMaxSize = cachesize > 0 ? cachesize : 0;
1311 sMemReporter = new imgMemoryReporter();
1312 RegisterStrongAsyncMemoryReporter(sMemReporter);
1313 RegisterImagesContentUsedUncompressedDistinguishedAmount(
1314 imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount);
1317 void imgLoader::ShutdownMemoryReporter() {
1318 UnregisterImagesContentUsedUncompressedDistinguishedAmount();
1319 UnregisterStrongMemoryReporter(sMemReporter);
1322 nsresult imgLoader::InitCache() {
1323 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
1324 if (!os) {
1325 return NS_ERROR_FAILURE;
1328 os->AddObserver(this, "memory-pressure", false);
1329 os->AddObserver(this, "chrome-flush-caches", false);
1330 os->AddObserver(this, "last-pb-context-exited", false);
1331 os->AddObserver(this, "profile-before-change", false);
1332 os->AddObserver(this, "xpcom-shutdown", false);
1334 mCacheTracker = MakeUnique<imgCacheExpirationTracker>();
1336 return NS_OK;
1339 nsresult imgLoader::Init() {
1340 InitCache();
1342 return NS_OK;
1345 NS_IMETHODIMP
1346 imgLoader::RespectPrivacyNotifications() {
1347 mRespectPrivacy = true;
1348 return NS_OK;
1351 NS_IMETHODIMP
1352 imgLoader::Observe(nsISupports* aSubject, const char* aTopic,
1353 const char16_t* aData) {
1354 if (strcmp(aTopic, "memory-pressure") == 0) {
1355 MinimizeCaches();
1356 } else if (strcmp(aTopic, "chrome-flush-caches") == 0) {
1357 MinimizeCaches();
1358 ClearChromeImageCache();
1359 } else if (strcmp(aTopic, "last-pb-context-exited") == 0) {
1360 if (mRespectPrivacy) {
1361 ClearImageCache();
1362 ClearChromeImageCache();
1364 } else if (strcmp(aTopic, "profile-before-change") == 0) {
1365 mCacheTracker = nullptr;
1366 } else if (strcmp(aTopic, "xpcom-shutdown") == 0) {
1367 mCacheTracker = nullptr;
1368 ShutdownMemoryReporter();
1370 } else {
1371 // (Nothing else should bring us here)
1372 MOZ_ASSERT(0, "Invalid topic received");
1375 return NS_OK;
1378 NS_IMETHODIMP
1379 imgLoader::ClearCache(bool chrome) {
1380 if (XRE_IsParentProcess()) {
1381 bool privateLoader = this == gPrivateBrowsingLoader;
1382 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1383 Unused << cp->SendClearImageCache(privateLoader, chrome);
1387 if (chrome) {
1388 return ClearChromeImageCache();
1390 return ClearImageCache();
1393 NS_IMETHODIMP
1394 imgLoader::RemoveEntriesFromPrincipalInAllProcesses(nsIPrincipal* aPrincipal) {
1395 if (!XRE_IsParentProcess()) {
1396 return NS_ERROR_NOT_AVAILABLE;
1399 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1400 Unused << cp->SendClearImageCacheFromPrincipal(aPrincipal);
1403 imgLoader* loader;
1404 if (aPrincipal->OriginAttributesRef().mPrivateBrowsingId ==
1405 nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID) {
1406 loader = imgLoader::NormalLoader();
1407 } else {
1408 loader = imgLoader::PrivateBrowsingLoader();
1411 return loader->RemoveEntriesInternal(aPrincipal, nullptr);
1414 NS_IMETHODIMP
1415 imgLoader::RemoveEntriesFromBaseDomainInAllProcesses(
1416 const nsACString& aBaseDomain) {
1417 if (!XRE_IsParentProcess()) {
1418 return NS_ERROR_NOT_AVAILABLE;
1421 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1422 Unused << cp->SendClearImageCacheFromBaseDomain(nsCString(aBaseDomain));
1425 return RemoveEntriesInternal(nullptr, &aBaseDomain);
1428 nsresult imgLoader::RemoveEntriesInternal(nsIPrincipal* aPrincipal,
1429 const nsACString* aBaseDomain) {
1430 // Can only clear by either principal or base domain.
1431 if ((!aPrincipal && !aBaseDomain) || (aPrincipal && aBaseDomain)) {
1432 return NS_ERROR_INVALID_ARG;
1435 nsAutoString origin;
1436 if (aPrincipal) {
1437 nsresult rv = nsContentUtils::GetUTFOrigin(aPrincipal, origin);
1438 if (NS_WARN_IF(NS_FAILED(rv))) {
1439 return rv;
1443 nsCOMPtr<nsIEffectiveTLDService> tldService;
1444 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1446 // For base domain we only clear the non-chrome cache.
1447 imgCacheTable& cache =
1448 GetCache(aPrincipal && aPrincipal->IsSystemPrincipal());
1449 for (const auto& entry : cache) {
1450 const auto& key = entry.GetKey();
1452 const bool shouldRemove = [&] {
1453 if (aPrincipal) {
1454 if (key.OriginAttributesRef() !=
1455 BasePrincipal::Cast(aPrincipal)->OriginAttributesRef()) {
1456 return false;
1459 nsAutoString imageOrigin;
1460 nsresult rv = nsContentUtils::GetUTFOrigin(key.URI(), imageOrigin);
1461 if (NS_WARN_IF(NS_FAILED(rv))) {
1462 return false;
1465 return imageOrigin == origin;
1468 if (!aBaseDomain) {
1469 return false;
1471 // Clear by baseDomain.
1472 nsAutoCString host;
1473 nsresult rv = key.URI()->GetHost(host);
1474 if (NS_FAILED(rv) || host.IsEmpty()) {
1475 return false;
1478 if (!tldService) {
1479 tldService = do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
1481 if (NS_WARN_IF(!tldService)) {
1482 return false;
1485 bool hasRootDomain = false;
1486 rv = tldService->HasRootDomain(host, *aBaseDomain, &hasRootDomain);
1487 if (NS_SUCCEEDED(rv) && hasRootDomain) {
1488 return true;
1491 // If we don't get a direct base domain match, also check for cache of
1492 // third parties partitioned under aBaseDomain.
1494 // The isolation key is either just the base domain, or an origin suffix
1495 // which contains the partitionKey holding the baseDomain.
1497 if (key.IsolationKeyRef().Equals(*aBaseDomain)) {
1498 return true;
1501 // The isolation key does not match the given base domain. It may be an
1502 // origin suffix. Parse it into origin attributes.
1503 OriginAttributes attrs;
1504 if (!attrs.PopulateFromSuffix(key.IsolationKeyRef())) {
1505 // Key is not an origin suffix.
1506 return false;
1509 return StoragePrincipalHelper::PartitionKeyHasBaseDomain(
1510 attrs.mPartitionKey, *aBaseDomain);
1511 }();
1513 if (shouldRemove) {
1514 entriesToBeRemoved.AppendElement(entry.GetData());
1518 for (auto& entry : entriesToBeRemoved) {
1519 if (!RemoveFromCache(entry)) {
1520 NS_WARNING(
1521 "Couldn't remove an entry from the cache in "
1522 "RemoveEntriesInternal()\n");
1526 return NS_OK;
1529 NS_IMETHODIMP
1530 imgLoader::RemoveEntry(nsIURI* aURI, Document* aDoc) {
1531 if (aURI) {
1532 OriginAttributes attrs;
1533 if (aDoc) {
1534 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1535 if (principal) {
1536 attrs = principal->OriginAttributesRef();
1540 ImageCacheKey key(aURI, attrs, aDoc);
1541 if (RemoveFromCache(key)) {
1542 return NS_OK;
1545 return NS_ERROR_NOT_AVAILABLE;
1548 NS_IMETHODIMP
1549 imgLoader::FindEntryProperties(nsIURI* uri, Document* aDoc,
1550 nsIProperties** _retval) {
1551 *_retval = nullptr;
1553 OriginAttributes attrs;
1554 if (aDoc) {
1555 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1556 if (principal) {
1557 attrs = principal->OriginAttributesRef();
1561 ImageCacheKey key(uri, attrs, aDoc);
1562 imgCacheTable& cache = GetCache(key);
1564 RefPtr<imgCacheEntry> entry;
1565 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
1566 if (mCacheTracker && entry->HasNoProxies()) {
1567 mCacheTracker->MarkUsed(entry);
1570 RefPtr<imgRequest> request = entry->GetRequest();
1571 if (request) {
1572 nsCOMPtr<nsIProperties> properties = request->Properties();
1573 properties.forget(_retval);
1577 return NS_OK;
1580 NS_IMETHODIMP_(void)
1581 imgLoader::ClearCacheForControlledDocument(Document* aDoc) {
1582 MOZ_ASSERT(aDoc);
1583 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1584 imgCacheTable& cache = GetCache(false);
1585 for (const auto& entry : cache) {
1586 const auto& key = entry.GetKey();
1587 if (key.ControlledDocument() == aDoc) {
1588 entriesToBeRemoved.AppendElement(entry.GetData());
1591 for (auto& entry : entriesToBeRemoved) {
1592 if (!RemoveFromCache(entry)) {
1593 NS_WARNING(
1594 "Couldn't remove an entry from the cache in "
1595 "ClearCacheForControlledDocument()\n");
1600 void imgLoader::Shutdown() {
1601 NS_IF_RELEASE(gNormalLoader);
1602 gNormalLoader = nullptr;
1603 NS_IF_RELEASE(gPrivateBrowsingLoader);
1604 gPrivateBrowsingLoader = nullptr;
1607 nsresult imgLoader::ClearChromeImageCache() {
1608 return EvictEntries(mChromeCache);
1611 nsresult imgLoader::ClearImageCache() { return EvictEntries(mCache); }
1613 void imgLoader::MinimizeCaches() {
1614 EvictEntries(mCacheQueue);
1615 EvictEntries(mChromeCacheQueue);
1618 bool imgLoader::PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* entry) {
1619 imgCacheTable& cache = GetCache(aKey);
1621 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::PutIntoCache", "uri",
1622 aKey.URI());
1624 // Check to see if this request already exists in the cache. If so, we'll
1625 // replace the old version.
1626 RefPtr<imgCacheEntry> tmpCacheEntry;
1627 if (cache.Get(aKey, getter_AddRefs(tmpCacheEntry)) && tmpCacheEntry) {
1628 MOZ_LOG(
1629 gImgLog, LogLevel::Debug,
1630 ("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
1631 nullptr));
1632 RefPtr<imgRequest> tmpRequest = tmpCacheEntry->GetRequest();
1634 // If it already exists, and we're putting the same key into the cache, we
1635 // should remove the old version.
1636 MOZ_LOG(gImgLog, LogLevel::Debug,
1637 ("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
1638 nullptr));
1640 RemoveFromCache(aKey);
1641 } else {
1642 MOZ_LOG(gImgLog, LogLevel::Debug,
1643 ("[this=%p] imgLoader::PutIntoCache --"
1644 " Element NOT already in the cache",
1645 nullptr));
1648 cache.InsertOrUpdate(aKey, RefPtr{entry});
1650 // We can be called to resurrect an evicted entry.
1651 if (entry->Evicted()) {
1652 entry->SetEvicted(false);
1655 // If we're resurrecting an entry with no proxies, put it back in the
1656 // tracker and queue.
1657 if (entry->HasNoProxies()) {
1658 nsresult addrv = NS_OK;
1660 if (mCacheTracker) {
1661 addrv = mCacheTracker->AddObject(entry);
1664 if (NS_SUCCEEDED(addrv)) {
1665 imgCacheQueue& queue = GetCacheQueue(aKey);
1666 queue.Push(entry);
1670 RefPtr<imgRequest> request = entry->GetRequest();
1671 request->SetIsInCache(true);
1672 RemoveFromUncachedImages(request);
1674 return true;
1677 bool imgLoader::SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry) {
1678 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasNoProxies", "uri",
1679 aRequest->CacheKey().URI());
1681 aEntry->SetHasNoProxies(true);
1683 if (aEntry->Evicted()) {
1684 return false;
1687 imgCacheQueue& queue = GetCacheQueue(aRequest->IsChrome());
1689 nsresult addrv = NS_OK;
1691 if (mCacheTracker) {
1692 addrv = mCacheTracker->AddObject(aEntry);
1695 if (NS_SUCCEEDED(addrv)) {
1696 queue.Push(aEntry);
1699 imgCacheTable& cache = GetCache(aRequest->IsChrome());
1700 CheckCacheLimits(cache, queue);
1702 return true;
1705 bool imgLoader::SetHasProxies(imgRequest* aRequest) {
1706 VerifyCacheSizes();
1708 const ImageCacheKey& key = aRequest->CacheKey();
1709 imgCacheTable& cache = GetCache(key);
1711 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasProxies", "uri",
1712 key.URI());
1714 RefPtr<imgCacheEntry> entry;
1715 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
1716 // Make sure the cache entry is for the right request
1717 RefPtr<imgRequest> entryRequest = entry->GetRequest();
1718 if (entryRequest == aRequest && entry->HasNoProxies()) {
1719 imgCacheQueue& queue = GetCacheQueue(key);
1720 queue.Remove(entry);
1722 if (mCacheTracker) {
1723 mCacheTracker->RemoveObject(entry);
1726 entry->SetHasNoProxies(false);
1728 return true;
1732 return false;
1735 void imgLoader::CacheEntriesChanged(bool aForChrome,
1736 int32_t aSizeDiff /* = 0 */) {
1737 imgCacheQueue& queue = GetCacheQueue(aForChrome);
1738 // We only need to dirty the queue if there is any sorting
1739 // taking place. Empty or single-entry lists can't become
1740 // dirty.
1741 if (queue.GetNumElements() > 1) {
1742 queue.MarkDirty();
1744 queue.UpdateSize(aSizeDiff);
1747 void imgLoader::CheckCacheLimits(imgCacheTable& cache, imgCacheQueue& queue) {
1748 if (queue.GetNumElements() == 0) {
1749 NS_ASSERTION(queue.GetSize() == 0,
1750 "imgLoader::CheckCacheLimits -- incorrect cache size");
1753 // Remove entries from the cache until we're back at our desired max size.
1754 while (queue.GetSize() > sCacheMaxSize) {
1755 // Remove the first entry in the queue.
1756 RefPtr<imgCacheEntry> entry(queue.Pop());
1758 NS_ASSERTION(entry, "imgLoader::CheckCacheLimits -- NULL entry pointer");
1760 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1761 RefPtr<imgRequest> req = entry->GetRequest();
1762 if (req) {
1763 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::CheckCacheLimits",
1764 "entry", req->CacheKey().URI());
1768 if (entry) {
1769 // We just popped this entry from the queue, so pass AlreadyRemoved
1770 // to avoid searching the queue again in RemoveFromCache.
1771 RemoveFromCache(entry, QueueState::AlreadyRemoved);
1776 bool imgLoader::ValidateRequestWithNewChannel(
1777 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1778 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1779 imgINotificationObserver* aObserver, Document* aLoadingDocument,
1780 uint64_t aInnerWindowId, nsLoadFlags aLoadFlags,
1781 nsContentPolicyType aLoadPolicyType, imgRequestProxy** aProxyRequest,
1782 nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode, bool aLinkPreload,
1783 bool* aNewChannelCreated) {
1784 // now we need to insert a new channel request object in between the real
1785 // request and the proxy that basically delays loading the image until it
1786 // gets a 304 or figures out that this needs to be a new request
1788 nsresult rv;
1790 // If we're currently in the middle of validating this request, just hand
1791 // back a proxy to it; the required work will be done for us.
1792 if (imgCacheValidator* validator = request->GetValidator()) {
1793 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
1794 aObserver, aLoadFlags, aProxyRequest);
1795 if (NS_FAILED(rv)) {
1796 return false;
1799 if (*aProxyRequest) {
1800 imgRequestProxy* proxy = static_cast<imgRequestProxy*>(*aProxyRequest);
1802 // We will send notifications from imgCacheValidator::OnStartRequest().
1803 // In the mean time, we must defer notifications because we are added to
1804 // the imgRequest's proxy list, and we can get extra notifications
1805 // resulting from methods such as StartDecoding(). See bug 579122.
1806 proxy->MarkValidating();
1808 if (aLinkPreload) {
1809 MOZ_ASSERT(aLoadingDocument);
1810 proxy->PrioritizeAsPreload();
1811 auto preloadKey = PreloadHashKey::CreateAsImage(
1812 aURI, aTriggeringPrincipal, aCORSMode);
1813 proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
1816 // Attach the proxy without notifying
1817 validator->AddProxy(proxy);
1820 return true;
1822 // We will rely on Necko to cache this request when it's possible, and to
1823 // tell imgCacheValidator::OnStartRequest whether the request came from its
1824 // cache.
1825 nsCOMPtr<nsIChannel> newChannel;
1826 bool forcePrincipalCheck;
1827 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
1828 aInitialDocumentURI, aCORSMode, aReferrerInfo,
1829 aLoadGroup, aLoadFlags, aLoadPolicyType,
1830 aTriggeringPrincipal, aLoadingDocument, mRespectPrivacy);
1831 if (NS_FAILED(rv)) {
1832 return false;
1835 if (aNewChannelCreated) {
1836 *aNewChannelCreated = true;
1839 RefPtr<imgRequestProxy> req;
1840 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
1841 aObserver, aLoadFlags, getter_AddRefs(req));
1842 if (NS_FAILED(rv)) {
1843 return false;
1846 // Make sure that OnStatus/OnProgress calls have the right request set...
1847 RefPtr<nsProgressNotificationProxy> progressproxy =
1848 new nsProgressNotificationProxy(newChannel, req);
1849 if (!progressproxy) {
1850 return false;
1853 RefPtr<imgCacheValidator> hvc =
1854 new imgCacheValidator(progressproxy, this, request, aLoadingDocument,
1855 aInnerWindowId, forcePrincipalCheck);
1857 // Casting needed here to get past multiple inheritance.
1858 nsCOMPtr<nsIStreamListener> listener =
1859 do_QueryInterface(static_cast<nsIThreadRetargetableStreamListener*>(hvc));
1860 NS_ENSURE_TRUE(listener, false);
1862 // We must set the notification callbacks before setting up the
1863 // CORS listener, because that's also interested inthe
1864 // notification callbacks.
1865 newChannel->SetNotificationCallbacks(hvc);
1867 request->SetValidator(hvc);
1869 // We will send notifications from imgCacheValidator::OnStartRequest().
1870 // In the mean time, we must defer notifications because we are added to
1871 // the imgRequest's proxy list, and we can get extra notifications
1872 // resulting from methods such as StartDecoding(). See bug 579122.
1873 req->MarkValidating();
1875 if (aLinkPreload) {
1876 MOZ_ASSERT(aLoadingDocument);
1877 req->PrioritizeAsPreload();
1878 auto preloadKey =
1879 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, aCORSMode);
1880 req->NotifyOpen(preloadKey, aLoadingDocument, true);
1883 // Add the proxy without notifying
1884 hvc->AddProxy(req);
1886 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
1887 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
1888 aLoadGroup);
1889 rv = newChannel->AsyncOpen(listener);
1890 if (NS_WARN_IF(NS_FAILED(rv))) {
1891 req->CancelAndForgetObserver(rv);
1892 // This will notify any current or future <link preload> tags. Pass the
1893 // non-open channel so that we can read loadinfo and referrer info of that
1894 // channel.
1895 req->NotifyStart(newChannel);
1896 // Use the non-channel overload of this method to force the notification to
1897 // happen. The preload request has not been assigned a channel.
1898 req->NotifyStop(rv);
1899 return false;
1902 req.forget(aProxyRequest);
1903 return true;
1906 void imgLoader::NotifyObserversForCachedImage(
1907 imgCacheEntry* aEntry, imgRequest* request, nsIURI* aURI,
1908 nsIReferrerInfo* aReferrerInfo, Document* aLoadingDocument,
1909 nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode) {
1910 if (aEntry->HasNotified()) {
1911 return;
1914 nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
1916 if (!obsService->HasObservers("http-on-image-cache-response")) {
1917 return;
1920 aEntry->SetHasNotified();
1922 nsCOMPtr<nsIChannel> newChannel;
1923 bool forcePrincipalCheck;
1924 nsresult rv =
1925 NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
1926 nullptr, aCORSMode, aReferrerInfo, nullptr, 0,
1927 nsIContentPolicy::TYPE_INTERNAL_IMAGE,
1928 aTriggeringPrincipal, aLoadingDocument, mRespectPrivacy);
1929 if (NS_FAILED(rv)) {
1930 return;
1933 RefPtr<HttpBaseChannel> httpBaseChannel = do_QueryObject(newChannel);
1934 if (httpBaseChannel) {
1935 httpBaseChannel->SetDummyChannelForImageCache();
1936 newChannel->SetContentType(nsDependentCString(request->GetMimeType()));
1937 RefPtr<mozilla::image::Image> image = request->GetImage();
1938 if (image) {
1939 newChannel->SetContentLength(aEntry->GetDataSize());
1941 obsService->NotifyObservers(newChannel, "http-on-image-cache-response",
1942 nullptr);
1946 bool imgLoader::ValidateEntry(
1947 imgCacheEntry* aEntry, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1948 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1949 imgINotificationObserver* aObserver, Document* aLoadingDocument,
1950 nsLoadFlags aLoadFlags, nsContentPolicyType aLoadPolicyType,
1951 bool aCanMakeNewChannel, bool* aNewChannelCreated,
1952 imgRequestProxy** aProxyRequest, nsIPrincipal* aTriggeringPrincipal,
1953 CORSMode aCORSMode, bool aLinkPreload) {
1954 LOG_SCOPE(gImgLog, "imgLoader::ValidateEntry");
1956 // If the expiration time is zero, then the request has not gotten far enough
1957 // to know when it will expire, or we know it will never expire (see
1958 // nsContentUtils::GetSubresourceCacheValidationInfo).
1959 uint32_t expiryTime = aEntry->GetExpiryTime();
1960 bool hasExpired = expiryTime && expiryTime <= SecondsFromPRTime(PR_Now());
1962 nsresult rv;
1964 // Special treatment for file URLs - aEntry has expired if file has changed
1965 nsCOMPtr<nsIFileURL> fileUrl(do_QueryInterface(aURI));
1966 if (fileUrl) {
1967 uint32_t lastModTime = aEntry->GetLoadTime();
1969 nsCOMPtr<nsIFile> theFile;
1970 rv = fileUrl->GetFile(getter_AddRefs(theFile));
1971 if (NS_SUCCEEDED(rv)) {
1972 PRTime fileLastMod;
1973 rv = theFile->GetLastModifiedTime(&fileLastMod);
1974 if (NS_SUCCEEDED(rv)) {
1975 // nsIFile uses millisec, NSPR usec
1976 fileLastMod *= 1000;
1977 hasExpired = SecondsFromPRTime((PRTime)fileLastMod) > lastModTime;
1982 RefPtr<imgRequest> request(aEntry->GetRequest());
1984 if (!request) {
1985 return false;
1988 if (!ValidateSecurityInfo(request, aEntry->ForcePrincipalCheck(), aCORSMode,
1989 aTriggeringPrincipal, aLoadingDocument,
1990 aLoadPolicyType)) {
1991 return false;
1994 // data URIs are immutable and by their nature can't leak data, so we can
1995 // just return true in that case. Doing so would mean that shift-reload
1996 // doesn't reload data URI documents/images though (which is handy for
1997 // debugging during gecko development) so we make an exception in that case.
1998 nsAutoCString scheme;
1999 aURI->GetScheme(scheme);
2000 if (scheme.EqualsLiteral("data") &&
2001 !(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 aNewChannelCreated);
2094 if (!validateRequest) {
2095 NotifyObserversForCachedImage(aEntry, request, aURI, aReferrerInfo,
2096 aLoadingDocument, aTriggeringPrincipal,
2097 aCORSMode);
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 = LoadImage(
2244 aURI, aInitialDocumentURI, aReferrerInfo, aTriggeringPrincipal, 0,
2245 aLoadGroup, aObserver, aLoadingDocument, aLoadingDocument, aLoadFlags,
2246 aCacheKey, aContentPolicyType, u""_ns,
2247 /* aUseUrgentStartForChannel */ false, /* aListPreload */ false, &proxy);
2248 *_retval = proxy;
2249 return rv;
2252 static void MakeRequestStaticIfNeeded(
2253 Document* aLoadingDocument, imgRequestProxy** aProxyAboutToGetReturned) {
2254 if (!aLoadingDocument || !aLoadingDocument->IsStaticDocument()) {
2255 return;
2258 if (!*aProxyAboutToGetReturned) {
2259 return;
2262 RefPtr<imgRequestProxy> proxy = dont_AddRef(*aProxyAboutToGetReturned);
2263 *aProxyAboutToGetReturned = nullptr;
2265 RefPtr<imgRequestProxy> staticProxy =
2266 proxy->GetStaticRequest(aLoadingDocument);
2267 if (staticProxy != proxy) {
2268 proxy->CancelAndForgetObserver(NS_BINDING_ABORTED);
2269 proxy = std::move(staticProxy);
2271 proxy.forget(aProxyAboutToGetReturned);
2274 bool imgLoader::IsImageAvailable(nsIURI* aURI,
2275 nsIPrincipal* aTriggeringPrincipal,
2276 CORSMode aCORSMode, Document* aDocument) {
2277 ImageCacheKey key(aURI, aTriggeringPrincipal->OriginAttributesRef(),
2278 aDocument);
2279 RefPtr<imgCacheEntry> entry;
2280 imgCacheTable& cache = GetCache(key);
2281 if (!cache.Get(key, getter_AddRefs(entry)) || !entry) {
2282 return false;
2284 RefPtr<imgRequest> request = entry->GetRequest();
2285 if (!request) {
2286 return false;
2288 return ValidateCORSMode(request, false, aCORSMode, aTriggeringPrincipal);
2291 nsresult imgLoader::LoadImage(
2292 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2293 nsIPrincipal* aTriggeringPrincipal, uint64_t aRequestContextID,
2294 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
2295 nsINode* aContext, Document* aLoadingDocument, nsLoadFlags aLoadFlags,
2296 nsISupports* aCacheKey, nsContentPolicyType aContentPolicyType,
2297 const nsAString& initiatorType, bool aUseUrgentStartForChannel,
2298 bool aLinkPreload, imgRequestProxy** _retval) {
2299 VerifyCacheSizes();
2301 NS_ASSERTION(aURI, "imgLoader::LoadImage -- NULL URI pointer");
2303 if (!aURI) {
2304 return NS_ERROR_NULL_POINTER;
2307 auto makeStaticIfNeeded = mozilla::MakeScopeExit(
2308 [&] { MakeRequestStaticIfNeeded(aLoadingDocument, _retval); });
2310 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK,
2311 aURI->GetSpecOrDefault());
2313 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::LoadImage", "aURI", aURI);
2315 *_retval = nullptr;
2317 RefPtr<imgRequest> request;
2319 nsresult rv;
2320 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2322 #ifdef DEBUG
2323 bool isPrivate = false;
2325 if (aLoadingDocument) {
2326 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadingDocument);
2327 } else if (aLoadGroup) {
2328 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadGroup);
2330 MOZ_ASSERT(isPrivate == mRespectPrivacy);
2332 if (aLoadingDocument) {
2333 // The given load group should match that of the document if given. If
2334 // that isn't the case, then we need to add more plumbing to ensure we
2335 // block the document as well.
2336 nsCOMPtr<nsILoadGroup> docLoadGroup =
2337 aLoadingDocument->GetDocumentLoadGroup();
2338 MOZ_ASSERT(docLoadGroup == aLoadGroup);
2340 #endif
2342 // Get the default load flags from the loadgroup (if possible)...
2343 if (aLoadGroup) {
2344 aLoadGroup->GetLoadFlags(&requestFlags);
2347 // Merge the default load flags with those passed in via aLoadFlags.
2348 // Currently, *only* the caching, validation and background load flags
2349 // are merged...
2351 // The flags in aLoadFlags take precedence over the default flags!
2353 if (aLoadFlags & LOAD_FLAGS_CACHE_MASK) {
2354 // Override the default caching flags...
2355 requestFlags = (requestFlags & ~LOAD_FLAGS_CACHE_MASK) |
2356 (aLoadFlags & LOAD_FLAGS_CACHE_MASK);
2358 if (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK) {
2359 // Override the default validation flags...
2360 requestFlags = (requestFlags & ~LOAD_FLAGS_VALIDATE_MASK) |
2361 (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK);
2363 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
2364 // Propagate background loading...
2365 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2367 if (aLoadFlags & nsIRequest::LOAD_RECORD_START_REQUEST_DELAY) {
2368 requestFlags |= nsIRequest::LOAD_RECORD_START_REQUEST_DELAY;
2371 if (aLinkPreload) {
2372 // Set background loading if it is <link rel=preload>
2373 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2376 CORSMode corsmode = CORS_NONE;
2377 if (aLoadFlags & imgILoader::LOAD_CORS_ANONYMOUS) {
2378 corsmode = CORS_ANONYMOUS;
2379 } else if (aLoadFlags & imgILoader::LOAD_CORS_USE_CREDENTIALS) {
2380 corsmode = CORS_USE_CREDENTIALS;
2383 // Look in the preloaded images of loading document first.
2384 if (StaticPrefs::network_preload() && !aLinkPreload && aLoadingDocument) {
2385 auto key =
2386 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, corsmode);
2387 if (RefPtr<PreloaderBase> preload =
2388 aLoadingDocument->Preloads().LookupPreload(key)) {
2389 RefPtr<imgRequestProxy> proxy = do_QueryObject(preload);
2390 MOZ_ASSERT(proxy);
2392 MOZ_LOG(gImgLog, LogLevel::Debug,
2393 ("[this=%p] imgLoader::LoadImage -- preloaded [proxy=%p]"
2394 " [document=%p]\n",
2395 this, proxy.get(), aLoadingDocument));
2397 // Removing the preload for this image to be in parity with Chromium. Any
2398 // following regular image request will be reloaded using the regular
2399 // path: image cache, http cache, network. Any following `<link
2400 // rel=preload as=image>` will start a new image preload that can be
2401 // satisfied from http cache or network.
2403 // There is a spec discussion for "preload cache", see
2404 // https://github.com/w3c/preload/issues/97. And it is also not clear how
2405 // preload image interacts with list of available images, see
2406 // https://github.com/whatwg/html/issues/4474.
2407 proxy->RemoveSelf(aLoadingDocument);
2408 proxy->NotifyUsage();
2410 imgRequest* request = proxy->GetOwner();
2411 nsresult rv =
2412 CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
2413 aObserver, requestFlags, _retval);
2414 NS_ENSURE_SUCCESS(rv, rv);
2416 imgRequestProxy* newProxy = *_retval;
2417 if (imgCacheValidator* validator = request->GetValidator()) {
2418 newProxy->MarkValidating();
2419 // Attach the proxy without notifying and this will add us to the load
2420 // group.
2421 validator->AddProxy(newProxy);
2422 } else {
2423 // It's OK to add here even if the request is done. If it is, it'll send
2424 // a OnStopRequest()and the proxy will be removed from the loadgroup in
2425 // imgRequestProxy::OnLoadComplete.
2426 newProxy->AddToLoadGroup();
2427 newProxy->NotifyListener();
2430 return NS_OK;
2434 RefPtr<imgCacheEntry> entry;
2436 // Look in the cache for our URI, and then validate it.
2437 // XXX For now ignore aCacheKey. We will need it in the future
2438 // for correctly dealing with image load requests that are a result
2439 // of post data.
2440 OriginAttributes attrs;
2441 if (aTriggeringPrincipal) {
2442 attrs = aTriggeringPrincipal->OriginAttributesRef();
2444 ImageCacheKey key(aURI, attrs, aLoadingDocument);
2445 imgCacheTable& cache = GetCache(key);
2447 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
2448 bool newChannelCreated = false;
2449 if (ValidateEntry(entry, aURI, aInitialDocumentURI, aReferrerInfo,
2450 aLoadGroup, aObserver, aLoadingDocument, requestFlags,
2451 aContentPolicyType, true, &newChannelCreated, _retval,
2452 aTriggeringPrincipal, corsmode, aLinkPreload)) {
2453 request = entry->GetRequest();
2455 // If this entry has no proxies, its request has no reference to the
2456 // entry.
2457 if (entry->HasNoProxies()) {
2458 LOG_FUNC_WITH_PARAM(gImgLog,
2459 "imgLoader::LoadImage() adding proxyless entry",
2460 "uri", key.URI());
2461 MOZ_ASSERT(!request->HasCacheEntry(),
2462 "Proxyless entry's request has cache entry!");
2463 request->SetCacheEntry(entry);
2465 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2466 mCacheTracker->MarkUsed(entry);
2470 entry->Touch();
2472 if (!newChannelCreated) {
2473 // This is ugly but it's needed to report CSP violations. We have 3
2474 // scenarios:
2475 // - we don't have cache. We are not in this if() stmt. A new channel is
2476 // created and that triggers the CSP checks.
2477 // - We have a cache entry and this is blocked by CSP directives.
2478 DebugOnly<bool> shouldLoad = ShouldLoadCachedImage(
2479 request, aLoadingDocument, aTriggeringPrincipal, aContentPolicyType,
2480 /* aSendCSPViolationReports */ true);
2481 MOZ_ASSERT(shouldLoad);
2483 } else {
2484 // We can't use this entry. We'll try to load it off the network, and if
2485 // successful, overwrite the old entry in the cache with a new one.
2486 entry = nullptr;
2490 // Keep the channel in this scope, so we can adjust its notificationCallbacks
2491 // later when we create the proxy.
2492 nsCOMPtr<nsIChannel> newChannel;
2493 // If we didn't get a cache hit, we need to load from the network.
2494 if (!request) {
2495 LOG_SCOPE(gImgLog, "imgLoader::LoadImage |cache miss|");
2497 bool forcePrincipalCheck;
2498 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
2499 aInitialDocumentURI, corsmode, aReferrerInfo,
2500 aLoadGroup, requestFlags, aContentPolicyType,
2501 aTriggeringPrincipal, aContext, mRespectPrivacy);
2502 if (NS_FAILED(rv)) {
2503 return NS_ERROR_FAILURE;
2506 MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel) == mRespectPrivacy);
2508 NewRequestAndEntry(forcePrincipalCheck, this, key, getter_AddRefs(request),
2509 getter_AddRefs(entry));
2511 MOZ_LOG(gImgLog, LogLevel::Debug,
2512 ("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
2513 " [request=%p]\n",
2514 this, request.get()));
2516 nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(newChannel));
2517 if (cos) {
2518 if (aUseUrgentStartForChannel && !aLinkPreload) {
2519 cos->AddClassFlags(nsIClassOfService::UrgentStart);
2522 if (StaticPrefs::network_http_tailing_enabled() &&
2523 aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
2524 cos->AddClassFlags(nsIClassOfService::Throttleable |
2525 nsIClassOfService::Tail);
2526 nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(newChannel));
2527 if (httpChannel) {
2528 Unused << httpChannel->SetRequestContextID(aRequestContextID);
2533 nsCOMPtr<nsILoadGroup> channelLoadGroup;
2534 newChannel->GetLoadGroup(getter_AddRefs(channelLoadGroup));
2535 rv = request->Init(aURI, aURI, /* aHadInsecureRedirect = */ false,
2536 channelLoadGroup, newChannel, entry, aLoadingDocument,
2537 aTriggeringPrincipal, corsmode, aReferrerInfo);
2538 if (NS_FAILED(rv)) {
2539 return NS_ERROR_FAILURE;
2542 // Add the initiator type for this image load
2543 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(newChannel);
2544 if (timedChannel) {
2545 timedChannel->SetInitiatorType(initiatorType);
2548 // create the proxy listener
2549 nsCOMPtr<nsIStreamListener> listener = new ProxyListener(request.get());
2551 MOZ_LOG(gImgLog, LogLevel::Debug,
2552 ("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
2553 this));
2555 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
2556 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
2557 aLoadGroup);
2559 nsresult openRes = newChannel->AsyncOpen(listener);
2561 if (NS_FAILED(openRes)) {
2562 MOZ_LOG(
2563 gImgLog, LogLevel::Debug,
2564 ("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
2565 "\n",
2566 this, static_cast<uint32_t>(openRes)));
2567 request->CancelAndAbort(openRes);
2568 return openRes;
2571 // Try to add the new request into the cache.
2572 PutIntoCache(key, entry);
2573 } else {
2574 LOG_MSG_WITH_PARAM(gImgLog, "imgLoader::LoadImage |cache hit|", "request",
2575 request);
2578 // If we didn't get a proxy when validating the cache entry, we need to
2579 // create one.
2580 if (!*_retval) {
2581 // ValidateEntry() has three return values: "Is valid," "might be valid --
2582 // validating over network", and "not valid." If we don't have a _retval,
2583 // we know ValidateEntry is not validating over the network, so it's safe
2584 // to SetLoadId here because we know this request is valid for this context.
2586 // Note, however, that this doesn't guarantee the behaviour we want (one
2587 // URL maps to the same image on a page) if we load the same image in a
2588 // different tab (see bug 528003), because its load id will get re-set, and
2589 // that'll cause us to validate over the network.
2590 request->SetLoadId(aLoadingDocument);
2592 LOG_MSG(gImgLog, "imgLoader::LoadImage", "creating proxy request.");
2593 rv = CreateNewProxyForRequest(request, aURI, aLoadGroup, aLoadingDocument,
2594 aObserver, requestFlags, _retval);
2595 if (NS_FAILED(rv)) {
2596 return rv;
2599 imgRequestProxy* proxy = *_retval;
2601 // Make sure that OnStatus/OnProgress calls have the right request set, if
2602 // we did create a channel here.
2603 if (newChannel) {
2604 nsCOMPtr<nsIInterfaceRequestor> requestor(
2605 new nsProgressNotificationProxy(newChannel, proxy));
2606 if (!requestor) {
2607 return NS_ERROR_OUT_OF_MEMORY;
2609 newChannel->SetNotificationCallbacks(requestor);
2612 if (aLinkPreload) {
2613 MOZ_ASSERT(aLoadingDocument);
2614 proxy->PrioritizeAsPreload();
2615 auto preloadKey =
2616 PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal, corsmode);
2617 proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
2620 // Note that it's OK to add here even if the request is done. If it is,
2621 // it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
2622 // the proxy will be removed from the loadgroup.
2623 proxy->AddToLoadGroup();
2625 // If we're loading off the network, explicitly don't notify our proxy,
2626 // because necko (or things called from necko, such as imgCacheValidator)
2627 // are going to call our notifications asynchronously, and we can't make it
2628 // further asynchronous because observers might rely on imagelib completing
2629 // its work between the channel's OnStartRequest and OnStopRequest.
2630 if (!newChannel) {
2631 proxy->NotifyListener();
2634 return rv;
2637 NS_ASSERTION(*_retval, "imgLoader::LoadImage -- no return value");
2639 return NS_OK;
2642 NS_IMETHODIMP
2643 imgLoader::LoadImageWithChannelXPCOM(nsIChannel* channel,
2644 imgINotificationObserver* aObserver,
2645 Document* aLoadingDocument,
2646 nsIStreamListener** listener,
2647 imgIRequest** _retval) {
2648 nsresult result;
2649 imgRequestProxy* proxy;
2650 result = LoadImageWithChannel(channel, aObserver, aLoadingDocument, listener,
2651 &proxy);
2652 *_retval = proxy;
2653 return result;
2656 nsresult imgLoader::LoadImageWithChannel(nsIChannel* channel,
2657 imgINotificationObserver* aObserver,
2658 Document* aLoadingDocument,
2659 nsIStreamListener** listener,
2660 imgRequestProxy** _retval) {
2661 NS_ASSERTION(channel,
2662 "imgLoader::LoadImageWithChannel -- NULL channel pointer");
2664 MOZ_ASSERT(NS_UsePrivateBrowsing(channel) == mRespectPrivacy);
2666 auto makeStaticIfNeeded = mozilla::MakeScopeExit(
2667 [&] { MakeRequestStaticIfNeeded(aLoadingDocument, _retval); });
2669 LOG_SCOPE(gImgLog, "imgLoader::LoadImageWithChannel");
2670 RefPtr<imgRequest> request;
2672 nsCOMPtr<nsIURI> uri;
2673 channel->GetURI(getter_AddRefs(uri));
2675 NS_ENSURE_TRUE(channel, NS_ERROR_FAILURE);
2676 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2678 OriginAttributes attrs = loadInfo->GetOriginAttributes();
2680 ImageCacheKey key(uri, attrs, aLoadingDocument);
2682 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2683 channel->GetLoadFlags(&requestFlags);
2685 RefPtr<imgCacheEntry> entry;
2687 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2688 RemoveFromCache(key);
2689 } else {
2690 // Look in the cache for our URI, and then validate it.
2691 // XXX For now ignore aCacheKey. We will need it in the future
2692 // for correctly dealing with image load requests that are a result
2693 // of post data.
2694 imgCacheTable& cache = GetCache(key);
2695 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
2696 // We don't want to kick off another network load. So we ask
2697 // ValidateEntry to only do validation without creating a new proxy. If
2698 // it says that the entry isn't valid any more, we'll only use the entry
2699 // we're getting if the channel is loading from the cache anyways.
2701 // XXX -- should this be changed? it's pretty much verbatim from the old
2702 // code, but seems nonsensical.
2704 // Since aCanMakeNewChannel == false, we don't need to pass content policy
2705 // type/principal/etc
2707 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2708 // if there is a loadInfo, use the right contentType, otherwise
2709 // default to the internal image type
2710 nsContentPolicyType policyType = loadInfo->InternalContentPolicyType();
2712 if (ValidateEntry(entry, uri, nullptr, nullptr, nullptr, aObserver,
2713 aLoadingDocument, requestFlags, policyType, false,
2714 nullptr, nullptr, nullptr, CORS_NONE, false)) {
2715 request = entry->GetRequest();
2716 } else {
2717 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(channel));
2718 bool bUseCacheCopy;
2720 if (cacheChan) {
2721 cacheChan->IsFromCache(&bUseCacheCopy);
2722 } else {
2723 bUseCacheCopy = false;
2726 if (!bUseCacheCopy) {
2727 entry = nullptr;
2728 } else {
2729 request = entry->GetRequest();
2733 if (request && entry) {
2734 // If this entry has no proxies, its request has no reference to
2735 // the entry.
2736 if (entry->HasNoProxies()) {
2737 LOG_FUNC_WITH_PARAM(
2738 gImgLog,
2739 "imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
2740 key.URI());
2741 MOZ_ASSERT(!request->HasCacheEntry(),
2742 "Proxyless entry's request has cache entry!");
2743 request->SetCacheEntry(entry);
2745 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2746 mCacheTracker->MarkUsed(entry);
2753 nsCOMPtr<nsILoadGroup> loadGroup;
2754 channel->GetLoadGroup(getter_AddRefs(loadGroup));
2756 #ifdef DEBUG
2757 if (aLoadingDocument) {
2758 // The load group of the channel should always match that of the
2759 // document if given. If that isn't the case, then we need to add more
2760 // plumbing to ensure we block the document as well.
2761 nsCOMPtr<nsILoadGroup> docLoadGroup =
2762 aLoadingDocument->GetDocumentLoadGroup();
2763 MOZ_ASSERT(docLoadGroup == loadGroup);
2765 #endif
2767 // Filter out any load flags not from nsIRequest
2768 requestFlags &= nsIRequest::LOAD_REQUESTMASK;
2770 nsresult rv = NS_OK;
2771 if (request) {
2772 // we have this in our cache already.. cancel the current (document) load
2774 // this should fire an OnStopRequest
2775 channel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
2777 *listener = nullptr; // give them back a null nsIStreamListener
2779 rv = CreateNewProxyForRequest(request, uri, loadGroup, aLoadingDocument,
2780 aObserver, requestFlags, _retval);
2781 static_cast<imgRequestProxy*>(*_retval)->NotifyListener();
2782 } else {
2783 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2784 nsCOMPtr<nsIURI> originalURI;
2785 channel->GetOriginalURI(getter_AddRefs(originalURI));
2787 // XXX(seth): We should be able to just use |key| here, except that |key| is
2788 // constructed above with the *current URI* and not the *original URI*. I'm
2789 // pretty sure this is a bug, and it's preventing us from ever getting a
2790 // cache hit in LoadImageWithChannel when redirects are involved.
2791 ImageCacheKey originalURIKey(originalURI, attrs, aLoadingDocument);
2793 // Default to doing a principal check because we don't know who
2794 // started that load and whether their principal ended up being
2795 // inherited on the channel.
2796 NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
2797 originalURIKey, getter_AddRefs(request),
2798 getter_AddRefs(entry));
2800 // No principal specified here, because we're not passed one.
2801 // In LoadImageWithChannel, the redirects that may have been
2802 // associated with this load would have gone through necko.
2803 // We only have the final URI in ImageLib and hence don't know
2804 // if the request went through insecure redirects. But if it did,
2805 // the necko cache should have handled that (since all necko cache hits
2806 // including the redirects will go through content policy). Hence, we
2807 // can set aHadInsecureRedirect to false here.
2808 rv = request->Init(originalURI, uri, /* aHadInsecureRedirect = */ false,
2809 channel, channel, entry, aLoadingDocument, nullptr,
2810 CORS_NONE, nullptr);
2811 NS_ENSURE_SUCCESS(rv, rv);
2813 RefPtr<ProxyListener> pl =
2814 new ProxyListener(static_cast<nsIStreamListener*>(request.get()));
2815 pl.forget(listener);
2817 // Try to add the new request into the cache.
2818 PutIntoCache(originalURIKey, entry);
2820 rv = CreateNewProxyForRequest(request, originalURI, loadGroup,
2821 aLoadingDocument, aObserver, requestFlags,
2822 _retval);
2824 // Explicitly don't notify our proxy, because we're loading off the
2825 // network, and necko (or things called from necko, such as
2826 // imgCacheValidator) are going to call our notifications asynchronously,
2827 // and we can't make it further asynchronous because observers might rely
2828 // on imagelib completing its work between the channel's OnStartRequest and
2829 // OnStopRequest.
2832 if (NS_FAILED(rv)) {
2833 return rv;
2836 (*_retval)->AddToLoadGroup();
2837 return rv;
2840 bool imgLoader::SupportImageWithMimeType(const nsACString& aMimeType,
2841 AcceptedMimeTypes aAccept
2842 /* = AcceptedMimeTypes::IMAGES */) {
2843 nsAutoCString mimeType(aMimeType);
2844 ToLowerCase(mimeType);
2846 if (aAccept == AcceptedMimeTypes::IMAGES_AND_DOCUMENTS &&
2847 mimeType.EqualsLiteral("image/svg+xml")) {
2848 return true;
2851 DecoderType type = DecoderFactory::GetDecoderType(mimeType.get());
2852 return type != DecoderType::UNKNOWN;
2855 NS_IMETHODIMP
2856 imgLoader::GetMIMETypeFromContent(nsIRequest* aRequest,
2857 const uint8_t* aContents, uint32_t aLength,
2858 nsACString& aContentType) {
2859 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2860 if (channel) {
2861 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2862 if (loadInfo->GetSkipContentSniffing()) {
2863 return NS_ERROR_NOT_AVAILABLE;
2867 nsresult rv =
2868 GetMimeTypeFromContent((const char*)aContents, aLength, aContentType);
2869 if (NS_SUCCEEDED(rv) && channel && XRE_IsParentProcess()) {
2870 if (RefPtr<mozilla::net::nsHttpChannel> httpChannel =
2871 do_QueryObject(channel)) {
2872 // If the image type pattern matching algorithm given bytes does not
2873 // return undefined, then disable the further check and allow the
2874 // response.
2875 httpChannel->DisableIsOpaqueResponseAllowedAfterSniffCheck(
2876 mozilla::net::nsHttpChannel::SnifferType::Image);
2880 return rv;
2883 /* static */
2884 nsresult imgLoader::GetMimeTypeFromContent(const char* aContents,
2885 uint32_t aLength,
2886 nsACString& aContentType) {
2887 nsAutoCString detected;
2889 /* Is it a GIF? */
2890 if (aLength >= 6 &&
2891 (!strncmp(aContents, "GIF87a", 6) || !strncmp(aContents, "GIF89a", 6))) {
2892 aContentType.AssignLiteral(IMAGE_GIF);
2894 /* or a PNG? */
2895 } else if (aLength >= 8 && ((unsigned char)aContents[0] == 0x89 &&
2896 (unsigned char)aContents[1] == 0x50 &&
2897 (unsigned char)aContents[2] == 0x4E &&
2898 (unsigned char)aContents[3] == 0x47 &&
2899 (unsigned char)aContents[4] == 0x0D &&
2900 (unsigned char)aContents[5] == 0x0A &&
2901 (unsigned char)aContents[6] == 0x1A &&
2902 (unsigned char)aContents[7] == 0x0A)) {
2903 aContentType.AssignLiteral(IMAGE_PNG);
2905 /* maybe a JPEG (JFIF)? */
2906 /* JFIF files start with SOI APP0 but older files can start with SOI DQT
2907 * so we test for SOI followed by any marker, i.e. FF D8 FF
2908 * this will also work for SPIFF JPEG files if they appear in the future.
2910 * (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
2912 } else if (aLength >= 3 && ((unsigned char)aContents[0]) == 0xFF &&
2913 ((unsigned char)aContents[1]) == 0xD8 &&
2914 ((unsigned char)aContents[2]) == 0xFF) {
2915 aContentType.AssignLiteral(IMAGE_JPEG);
2917 /* or how about ART? */
2918 /* ART begins with JG (4A 47). Major version offset 2.
2919 * Minor version offset 3. Offset 4 must be nullptr.
2921 } else if (aLength >= 5 && ((unsigned char)aContents[0]) == 0x4a &&
2922 ((unsigned char)aContents[1]) == 0x47 &&
2923 ((unsigned char)aContents[4]) == 0x00) {
2924 aContentType.AssignLiteral(IMAGE_ART);
2926 } else if (aLength >= 2 && !strncmp(aContents, "BM", 2)) {
2927 aContentType.AssignLiteral(IMAGE_BMP);
2929 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
2930 // CURs begin with 2-byte 0 followed by 2-byte 2.
2931 } else if (aLength >= 4 && (!memcmp(aContents, "\000\000\001\000", 4) ||
2932 !memcmp(aContents, "\000\000\002\000", 4))) {
2933 aContentType.AssignLiteral(IMAGE_ICO);
2935 // WebPs always begin with RIFF, a 32-bit length, and WEBP.
2936 } else if (aLength >= 12 && !memcmp(aContents, "RIFF", 4) &&
2937 !memcmp(aContents + 8, "WEBP", 4)) {
2938 aContentType.AssignLiteral(IMAGE_WEBP);
2940 } else if (MatchesMP4(reinterpret_cast<const uint8_t*>(aContents), aLength,
2941 detected) &&
2942 detected.Equals(IMAGE_AVIF)) {
2943 aContentType.AssignLiteral(IMAGE_AVIF);
2944 } else if ((aLength >= 2 && !memcmp(aContents, "\xFF\x0A", 2)) ||
2945 (aLength >= 12 &&
2946 !memcmp(aContents, "\x00\x00\x00\x0CJXL \x0D\x0A\x87\x0A", 12))) {
2947 // Each version is for containerless and containerful files respectively.
2948 aContentType.AssignLiteral(IMAGE_JXL);
2949 } else {
2950 /* none of the above? I give up */
2951 return NS_ERROR_NOT_AVAILABLE;
2954 return NS_OK;
2958 * proxy stream listener class used to handle multipart/x-mixed-replace
2961 #include "nsIRequest.h"
2962 #include "nsIStreamConverterService.h"
2964 NS_IMPL_ISUPPORTS(ProxyListener, nsIStreamListener,
2965 nsIThreadRetargetableStreamListener, nsIRequestObserver)
2967 ProxyListener::ProxyListener(nsIStreamListener* dest) : mDestListener(dest) {
2968 /* member initializers and constructor code */
2971 ProxyListener::~ProxyListener() { /* destructor code */
2974 /** nsIRequestObserver methods **/
2976 NS_IMETHODIMP
2977 ProxyListener::OnStartRequest(nsIRequest* aRequest) {
2978 if (!mDestListener) {
2979 return NS_ERROR_FAILURE;
2982 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2983 if (channel) {
2984 // We need to set the initiator type for the image load
2985 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(channel);
2986 if (timedChannel) {
2987 nsAutoString type;
2988 timedChannel->GetInitiatorType(type);
2989 if (type.IsEmpty()) {
2990 timedChannel->SetInitiatorType(u"img"_ns);
2994 nsAutoCString contentType;
2995 nsresult rv = channel->GetContentType(contentType);
2997 if (!contentType.IsEmpty()) {
2998 /* If multipart/x-mixed-replace content, we'll insert a MIME decoder
2999 in the pipeline to handle the content and pass it along to our
3000 original listener.
3002 if ("multipart/x-mixed-replace"_ns.Equals(contentType)) {
3003 nsCOMPtr<nsIStreamConverterService> convServ(
3004 do_GetService("@mozilla.org/streamConverters;1", &rv));
3005 if (NS_SUCCEEDED(rv)) {
3006 nsCOMPtr<nsIStreamListener> toListener(mDestListener);
3007 nsCOMPtr<nsIStreamListener> fromListener;
3009 rv = convServ->AsyncConvertData("multipart/x-mixed-replace", "*/*",
3010 toListener, nullptr,
3011 getter_AddRefs(fromListener));
3012 if (NS_SUCCEEDED(rv)) {
3013 mDestListener = fromListener;
3020 return mDestListener->OnStartRequest(aRequest);
3023 NS_IMETHODIMP
3024 ProxyListener::OnStopRequest(nsIRequest* aRequest, nsresult status) {
3025 if (!mDestListener) {
3026 return NS_ERROR_FAILURE;
3029 return mDestListener->OnStopRequest(aRequest, status);
3032 /** nsIStreamListener methods **/
3034 NS_IMETHODIMP
3035 ProxyListener::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
3036 uint64_t sourceOffset, uint32_t count) {
3037 if (!mDestListener) {
3038 return NS_ERROR_FAILURE;
3041 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
3044 /** nsThreadRetargetableStreamListener methods **/
3045 NS_IMETHODIMP
3046 ProxyListener::CheckListenerChain() {
3047 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3048 nsresult rv = NS_OK;
3049 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3050 do_QueryInterface(mDestListener, &rv);
3051 if (retargetableListener) {
3052 rv = retargetableListener->CheckListenerChain();
3054 MOZ_LOG(
3055 gImgLog, LogLevel::Debug,
3056 ("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
3057 "]",
3058 (NS_SUCCEEDED(rv) ? "success" : "failure"), this,
3059 (nsIStreamListener*)mDestListener, static_cast<uint32_t>(rv)));
3060 return rv;
3064 * http validate class. check a channel for a 304
3067 NS_IMPL_ISUPPORTS(imgCacheValidator, nsIStreamListener, nsIRequestObserver,
3068 nsIThreadRetargetableStreamListener, nsIChannelEventSink,
3069 nsIInterfaceRequestor, nsIAsyncVerifyRedirectCallback)
3071 imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy* progress,
3072 imgLoader* loader, imgRequest* request,
3073 Document* aDocument,
3074 uint64_t aInnerWindowId,
3075 bool forcePrincipalCheckForCacheEntry)
3076 : mProgressProxy(progress),
3077 mRequest(request),
3078 mDocument(aDocument),
3079 mInnerWindowId(aInnerWindowId),
3080 mImgLoader(loader),
3081 mHadInsecureRedirect(false) {
3082 NewRequestAndEntry(forcePrincipalCheckForCacheEntry, loader,
3083 mRequest->CacheKey(), getter_AddRefs(mNewRequest),
3084 getter_AddRefs(mNewEntry));
3087 imgCacheValidator::~imgCacheValidator() {
3088 if (mRequest) {
3089 // If something went wrong, and we never unblocked the requests waiting on
3090 // validation, now is our last chance. We will cancel the new request and
3091 // switch the waiting proxies to it.
3092 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
3096 void imgCacheValidator::AddProxy(imgRequestProxy* aProxy) {
3097 // aProxy needs to be in the loadgroup since we're validating from
3098 // the network.
3099 aProxy->AddToLoadGroup();
3101 mProxies.AppendElement(aProxy);
3104 void imgCacheValidator::RemoveProxy(imgRequestProxy* aProxy) {
3105 mProxies.RemoveElement(aProxy);
3108 void imgCacheValidator::UpdateProxies(bool aCancelRequest, bool aSyncNotify) {
3109 MOZ_ASSERT(mRequest);
3111 // Clear the validator before updating the proxies. The notifications may
3112 // clone an existing request, and its state could be inconsistent.
3113 mRequest->SetValidator(nullptr);
3114 mRequest = nullptr;
3116 // If an error occurred, we will want to cancel the new request, and make the
3117 // validating proxies point to it. Any proxies still bound to the original
3118 // request which are not validating should remain untouched.
3119 if (aCancelRequest) {
3120 MOZ_ASSERT(mNewRequest);
3121 mNewRequest->CancelAndAbort(NS_BINDING_ABORTED);
3124 // We have finished validating the request, so we can safely take ownership
3125 // of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
3126 // if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
3127 // that any potential notifications should still be suppressed in
3128 // imgRequestProxy::ChangeOwner because we haven't cleared the validating
3129 // flag yet, and thus they will remain deferred.
3130 AutoTArray<RefPtr<imgRequestProxy>, 4> proxies(std::move(mProxies));
3132 for (auto& proxy : proxies) {
3133 // First update the state of all proxies before notifying any of them
3134 // to ensure a consistent state (e.g. in case the notification causes
3135 // other proxies to be touched indirectly.)
3136 MOZ_ASSERT(proxy->IsValidating());
3137 MOZ_ASSERT(proxy->NotificationsDeferred(),
3138 "Proxies waiting on cache validation should be "
3139 "deferring notifications!");
3140 if (mNewRequest) {
3141 proxy->ChangeOwner(mNewRequest);
3143 proxy->ClearValidating();
3146 mNewRequest = nullptr;
3147 mNewEntry = nullptr;
3149 for (auto& proxy : proxies) {
3150 if (aSyncNotify) {
3151 // Notify synchronously, because the caller knows we are already in an
3152 // asynchronously-called function (e.g. OnStartRequest).
3153 proxy->SyncNotifyListener();
3154 } else {
3155 // Notify asynchronously, because the caller does not know our current
3156 // call state (e.g. ~imgCacheValidator).
3157 proxy->NotifyListener();
3162 /** nsIRequestObserver methods **/
3164 NS_IMETHODIMP
3165 imgCacheValidator::OnStartRequest(nsIRequest* aRequest) {
3166 // We may be holding on to a document, so ensure that it's released.
3167 RefPtr<Document> document = mDocument.forget();
3169 // If for some reason we don't still have an existing request (probably
3170 // because OnStartRequest got delivered more than once), just bail.
3171 if (!mRequest) {
3172 MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
3173 aRequest->Cancel(NS_BINDING_ABORTED);
3174 return NS_ERROR_FAILURE;
3177 // If this request is coming from cache and has the same URI as our
3178 // imgRequest, the request all our proxies are pointing at is valid, and all
3179 // we have to do is tell them to notify their listeners.
3180 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(aRequest));
3181 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
3182 if (cacheChan && channel) {
3183 bool isFromCache = false;
3184 cacheChan->IsFromCache(&isFromCache);
3186 nsCOMPtr<nsIURI> channelURI;
3187 channel->GetURI(getter_AddRefs(channelURI));
3189 nsCOMPtr<nsIURI> finalURI;
3190 mRequest->GetFinalURI(getter_AddRefs(finalURI));
3192 bool sameURI = false;
3193 if (channelURI && finalURI) {
3194 channelURI->Equals(finalURI, &sameURI);
3197 if (isFromCache && sameURI) {
3198 // We don't need to load this any more.
3199 aRequest->Cancel(NS_BINDING_ABORTED);
3200 mNewRequest = nullptr;
3202 // Clear the validator before updating the proxies. The notifications may
3203 // clone an existing request, and its state could be inconsistent.
3204 mRequest->SetLoadId(document);
3205 mRequest->SetInnerWindowID(mInnerWindowId);
3206 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3207 return NS_OK;
3211 // We can't load out of cache. We have to create a whole new request for the
3212 // data that's coming in off the channel.
3213 nsCOMPtr<nsIURI> uri;
3214 mRequest->GetURI(getter_AddRefs(uri));
3216 LOG_MSG_WITH_PARAM(gImgLog,
3217 "imgCacheValidator::OnStartRequest creating new request",
3218 "uri", uri);
3220 CORSMode corsmode = mRequest->GetCORSMode();
3221 nsCOMPtr<nsIReferrerInfo> referrerInfo = mRequest->GetReferrerInfo();
3222 nsCOMPtr<nsIPrincipal> triggeringPrincipal =
3223 mRequest->GetTriggeringPrincipal();
3225 // Doom the old request's cache entry
3226 mRequest->RemoveFromCache();
3228 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
3229 nsCOMPtr<nsIURI> originalURI;
3230 channel->GetOriginalURI(getter_AddRefs(originalURI));
3231 nsresult rv = mNewRequest->Init(originalURI, uri, mHadInsecureRedirect,
3232 aRequest, channel, mNewEntry, document,
3233 triggeringPrincipal, corsmode, referrerInfo);
3234 if (NS_FAILED(rv)) {
3235 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
3236 return rv;
3239 mDestListener = new ProxyListener(mNewRequest);
3241 // Try to add the new request into the cache. Note that the entry must be in
3242 // the cache before the proxies' ownership changes, because adding a proxy
3243 // changes the caching behaviour for imgRequests.
3244 mImgLoader->PutIntoCache(mNewRequest->CacheKey(), mNewEntry);
3245 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3246 return mDestListener->OnStartRequest(aRequest);
3249 NS_IMETHODIMP
3250 imgCacheValidator::OnStopRequest(nsIRequest* aRequest, nsresult status) {
3251 // Be sure we've released the document that we may have been holding on to.
3252 mDocument = nullptr;
3254 if (!mDestListener) {
3255 return NS_OK;
3258 return mDestListener->OnStopRequest(aRequest, status);
3261 /** nsIStreamListener methods **/
3263 NS_IMETHODIMP
3264 imgCacheValidator::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
3265 uint64_t sourceOffset, uint32_t count) {
3266 if (!mDestListener) {
3267 // XXX see bug 113959
3268 uint32_t _retval;
3269 inStr->ReadSegments(NS_DiscardSegment, nullptr, count, &_retval);
3270 return NS_OK;
3273 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
3276 /** nsIThreadRetargetableStreamListener methods **/
3278 NS_IMETHODIMP
3279 imgCacheValidator::CheckListenerChain() {
3280 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3281 nsresult rv = NS_OK;
3282 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
3283 do_QueryInterface(mDestListener, &rv);
3284 if (retargetableListener) {
3285 rv = retargetableListener->CheckListenerChain();
3287 MOZ_LOG(
3288 gImgLog, LogLevel::Debug,
3289 ("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32 "=%s",
3290 this, static_cast<uint32_t>(rv),
3291 NS_SUCCEEDED(rv) ? "succeeded" : "failed"));
3292 return rv;
3295 /** nsIInterfaceRequestor methods **/
3297 NS_IMETHODIMP
3298 imgCacheValidator::GetInterface(const nsIID& aIID, void** aResult) {
3299 if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
3300 return QueryInterface(aIID, aResult);
3303 return mProgressProxy->GetInterface(aIID, aResult);
3306 // These functions are materially the same as the same functions in imgRequest.
3307 // We duplicate them because we're verifying whether cache loads are necessary,
3308 // not unconditionally loading.
3310 /** nsIChannelEventSink methods **/
3311 NS_IMETHODIMP
3312 imgCacheValidator::AsyncOnChannelRedirect(
3313 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
3314 nsIAsyncVerifyRedirectCallback* callback) {
3315 // Note all cache information we get from the old channel.
3316 mNewRequest->SetCacheValidation(mNewEntry, oldChannel);
3318 // If the previous URI is a non-HTTPS URI, record that fact for later use by
3319 // security code, which needs to know whether there is an insecure load at any
3320 // point in the redirect chain.
3321 nsCOMPtr<nsIURI> oldURI;
3322 bool schemeLocal = false;
3323 if (NS_FAILED(oldChannel->GetURI(getter_AddRefs(oldURI))) ||
3324 NS_FAILED(NS_URIChainHasFlags(
3325 oldURI, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE, &schemeLocal)) ||
3326 (!oldURI->SchemeIs("https") && !oldURI->SchemeIs("chrome") &&
3327 !schemeLocal)) {
3328 mHadInsecureRedirect = true;
3331 // Prepare for callback
3332 mRedirectCallback = callback;
3333 mRedirectChannel = newChannel;
3335 return mProgressProxy->AsyncOnChannelRedirect(oldChannel, newChannel, flags,
3336 this);
3339 NS_IMETHODIMP
3340 imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult) {
3341 // If we've already been told to abort, just do so.
3342 if (NS_FAILED(aResult)) {
3343 mRedirectCallback->OnRedirectVerifyCallback(aResult);
3344 mRedirectCallback = nullptr;
3345 mRedirectChannel = nullptr;
3346 return NS_OK;
3349 // make sure we have a protocol that returns data rather than opens
3350 // an external application, e.g. mailto:
3351 nsCOMPtr<nsIURI> uri;
3352 mRedirectChannel->GetURI(getter_AddRefs(uri));
3353 bool doesNotReturnData = false;
3354 NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA,
3355 &doesNotReturnData);
3357 nsresult result = NS_OK;
3359 if (doesNotReturnData) {
3360 result = NS_ERROR_ABORT;
3363 mRedirectCallback->OnRedirectVerifyCallback(result);
3364 mRedirectCallback = nullptr;
3365 mRedirectChannel = nullptr;
3366 return NS_OK;