1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // Undefine windows version of LoadImage because our code uses that name.
8 #include "mozilla/ScopeExit.h"
9 #include "nsIChildChannel.h"
10 #include "nsIThreadRetargetableStreamListener.h"
13 #include "imgLoader.h"
18 #include "DecoderFactory.h"
20 #include "ImageLogging.h"
21 #include "ReferrerInfo.h"
22 #include "imgRequestProxy.h"
23 #include "mozilla/Attributes.h"
24 #include "mozilla/BasePrincipal.h"
25 #include "mozilla/ChaosMode.h"
26 #include "mozilla/ClearOnShutdown.h"
27 #include "mozilla/LoadInfo.h"
28 #include "mozilla/NullPrincipal.h"
29 #include "mozilla/Preferences.h"
30 #include "mozilla/ProfilerLabels.h"
31 #include "mozilla/StaticPrefs_image.h"
32 #include "mozilla/StaticPrefs_network.h"
33 #include "mozilla/StoragePrincipalHelper.h"
34 #include "mozilla/dom/ContentParent.h"
35 #include "mozilla/dom/nsMixedContentBlocker.h"
36 #include "mozilla/image/ImageMemoryReporter.h"
37 #include "mozilla/layers/CompositorManagerChild.h"
40 #include "nsComponentManagerUtils.h"
41 #include "nsContentPolicyUtils.h"
42 #include "nsContentSecurityManager.h"
43 #include "nsContentUtils.h"
44 #include "nsHttpChannel.h"
45 #include "nsIAsyncVerifyRedirectCallback.h"
46 #include "nsICacheInfoChannel.h"
47 #include "nsIChannelEventSink.h"
48 #include "nsIClassOfService.h"
49 #include "nsIEffectiveTLDService.h"
51 #include "nsIFileURL.h"
52 #include "nsIHttpChannel.h"
53 #include "nsIInterfaceRequestor.h"
54 #include "nsIInterfaceRequestorUtils.h"
55 #include "nsIMemoryReporter.h"
56 #include "nsINetworkPredictor.h"
57 #include "nsIProgressEventSink.h"
58 #include "nsIProtocolHandler.h"
59 #include "nsImageModule.h"
60 #include "nsMediaSniffer.h"
61 #include "nsMimeTypes.h"
63 #include "nsNetUtil.h"
64 #include "nsProxyRelease.h"
65 #include "nsQueryObject.h"
66 #include "nsReadableUtils.h"
67 #include "nsStreamUtils.h"
70 // we want to explore making the document own the load group
71 // so we can associate the document URI with the load group.
72 // until this point, we have an evil hack:
73 #include "nsIHttpChannelInternal.h"
74 #include "nsILoadGroupChild.h"
75 #include "nsIDocShell.h"
77 using namespace mozilla
;
78 using namespace mozilla::dom
;
79 using namespace mozilla::image
;
80 using namespace mozilla::net
;
82 MOZ_DEFINE_MALLOC_SIZE_OF(ImagesMallocSizeOf
)
84 class imgMemoryReporter final
: public nsIMemoryReporter
{
85 ~imgMemoryReporter() = default;
90 NS_IMETHOD
CollectReports(nsIHandleReportCallback
* aHandleReport
,
91 nsISupports
* aData
, bool aAnonymize
) override
{
92 MOZ_ASSERT(NS_IsMainThread());
94 layers::CompositorManagerChild
* manager
=
95 mozilla::layers::CompositorManagerChild::GetInstance();
96 if (!manager
|| !StaticPrefs::image_mem_debug_reporting()) {
97 layers::SharedSurfacesMemoryReport sharedSurfaces
;
98 FinishCollectReports(aHandleReport
, aData
, aAnonymize
, sharedSurfaces
);
102 RefPtr
<imgMemoryReporter
> self(this);
103 nsCOMPtr
<nsIHandleReportCallback
> handleReport(aHandleReport
);
104 nsCOMPtr
<nsISupports
> data(aData
);
105 manager
->SendReportSharedSurfacesMemory(
106 [=](layers::SharedSurfacesMemoryReport aReport
) {
107 self
->FinishCollectReports(handleReport
, data
, aAnonymize
, aReport
);
109 [=](mozilla::ipc::ResponseRejectReason
&& aReason
) {
110 layers::SharedSurfacesMemoryReport sharedSurfaces
;
111 self
->FinishCollectReports(handleReport
, data
, aAnonymize
,
117 void FinishCollectReports(
118 nsIHandleReportCallback
* aHandleReport
, nsISupports
* aData
,
119 bool aAnonymize
, layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
120 nsTArray
<ImageMemoryCounter
> chrome
;
121 nsTArray
<ImageMemoryCounter
> content
;
122 nsTArray
<ImageMemoryCounter
> uncached
;
124 for (uint32_t i
= 0; i
< mKnownLoaders
.Length(); i
++) {
125 for (imgCacheEntry
* entry
: mKnownLoaders
[i
]->mCache
.Values()) {
126 RefPtr
<imgRequest
> req
= entry
->GetRequest();
127 RecordCounterForRequest(req
, &content
, !entry
->HasNoProxies());
129 MutexAutoLock
lock(mKnownLoaders
[i
]->mUncachedImagesMutex
);
130 for (RefPtr
<imgRequest
> req
: mKnownLoaders
[i
]->mUncachedImages
) {
131 RecordCounterForRequest(req
, &uncached
, req
->HasConsumers());
135 // Note that we only need to anonymize content image URIs.
137 ReportCounterArray(aHandleReport
, aData
, chrome
, "images/chrome",
138 /* aAnonymize */ false, aSharedSurfaces
);
140 ReportCounterArray(aHandleReport
, aData
, content
, "images/content",
141 aAnonymize
, aSharedSurfaces
);
143 // Uncached images may be content or chrome, so anonymize them.
144 ReportCounterArray(aHandleReport
, aData
, uncached
, "images/uncached",
145 aAnonymize
, aSharedSurfaces
);
147 // Report any shared surfaces that were not merged with the surface cache.
148 ImageMemoryReporter::ReportSharedSurfaces(aHandleReport
, aData
,
151 nsCOMPtr
<nsIMemoryReporterManager
> imgr
=
152 do_GetService("@mozilla.org/memory-reporter-manager;1");
158 static int64_t ImagesContentUsedUncompressedDistinguishedAmount() {
160 for (uint32_t i
= 0; i
< imgLoader::sMemReporter
->mKnownLoaders
.Length();
162 for (imgCacheEntry
* entry
:
163 imgLoader::sMemReporter
->mKnownLoaders
[i
]->mCache
.Values()) {
164 if (entry
->HasNoProxies()) {
168 RefPtr
<imgRequest
> req
= entry
->GetRequest();
169 RefPtr
<image::Image
> image
= req
->GetImage();
174 // Both this and EntryImageSizes measure
175 // images/content/raster/used/decoded memory. This function's
176 // measurement is secondary -- the result doesn't go in the "explicit"
177 // tree -- so we use moz_malloc_size_of instead of ImagesMallocSizeOf to
178 // prevent DMD from seeing it reported twice.
179 SizeOfState
state(moz_malloc_size_of
);
180 ImageMemoryCounter
counter(req
, image
, state
, /* aIsUsed = */ true);
182 n
+= counter
.Values().DecodedHeap();
183 n
+= counter
.Values().DecodedNonHeap();
184 n
+= counter
.Values().DecodedUnknown();
190 void RegisterLoader(imgLoader
* aLoader
) {
191 mKnownLoaders
.AppendElement(aLoader
);
194 void UnregisterLoader(imgLoader
* aLoader
) {
195 mKnownLoaders
.RemoveElement(aLoader
);
199 nsTArray
<imgLoader
*> mKnownLoaders
;
202 MemoryTotal
& operator+=(const ImageMemoryCounter
& aImageCounter
) {
203 if (aImageCounter
.Type() == imgIContainer::TYPE_RASTER
) {
204 if (aImageCounter
.IsUsed()) {
205 mUsedRasterCounter
+= aImageCounter
.Values();
207 mUnusedRasterCounter
+= aImageCounter
.Values();
209 } else if (aImageCounter
.Type() == imgIContainer::TYPE_VECTOR
) {
210 if (aImageCounter
.IsUsed()) {
211 mUsedVectorCounter
+= aImageCounter
.Values();
213 mUnusedVectorCounter
+= aImageCounter
.Values();
215 } else if (aImageCounter
.Type() == imgIContainer::TYPE_REQUEST
) {
216 // Nothing to do, we did not get to the point of having an image.
218 MOZ_CRASH("Unexpected image type");
224 const MemoryCounter
& UsedRaster() const { return mUsedRasterCounter
; }
225 const MemoryCounter
& UnusedRaster() const { return mUnusedRasterCounter
; }
226 const MemoryCounter
& UsedVector() const { return mUsedVectorCounter
; }
227 const MemoryCounter
& UnusedVector() const { return mUnusedVectorCounter
; }
230 MemoryCounter mUsedRasterCounter
;
231 MemoryCounter mUnusedRasterCounter
;
232 MemoryCounter mUsedVectorCounter
;
233 MemoryCounter mUnusedVectorCounter
;
236 // Reports all images of a single kind, e.g. all used chrome images.
237 void ReportCounterArray(nsIHandleReportCallback
* aHandleReport
,
239 nsTArray
<ImageMemoryCounter
>& aCounterArray
,
240 const char* aPathPrefix
, bool aAnonymize
,
241 layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
242 MemoryTotal summaryTotal
;
243 MemoryTotal nonNotableTotal
;
245 // Report notable images, and compute total and non-notable aggregate sizes.
246 for (uint32_t i
= 0; i
< aCounterArray
.Length(); i
++) {
247 ImageMemoryCounter
& counter
= aCounterArray
[i
];
250 counter
.URI().Truncate();
251 counter
.URI().AppendPrintf("<anonymized-%u>", i
);
253 // The URI could be an extremely long data: URI. Truncate if needed.
254 static const size_t max
= 256;
255 if (counter
.URI().Length() > max
) {
256 counter
.URI().Truncate(max
);
257 counter
.URI().AppendLiteral(" (truncated)");
259 counter
.URI().ReplaceChar('/', '\\');
262 summaryTotal
+= counter
;
264 if (counter
.IsNotable() || StaticPrefs::image_mem_debug_reporting()) {
265 ReportImage(aHandleReport
, aData
, aPathPrefix
, counter
,
268 ImageMemoryReporter::TrimSharedSurfaces(counter
, aSharedSurfaces
);
269 nonNotableTotal
+= counter
;
273 // Report non-notable images in aggregate.
274 ReportTotal(aHandleReport
, aData
, /* aExplicit = */ true, aPathPrefix
,
275 "<non-notable images>/", nonNotableTotal
);
277 // Report a summary in aggregate, outside of the explicit tree.
278 ReportTotal(aHandleReport
, aData
, /* aExplicit = */ false, aPathPrefix
, "",
282 static void ReportImage(nsIHandleReportCallback
* aHandleReport
,
283 nsISupports
* aData
, const char* aPathPrefix
,
284 const ImageMemoryCounter
& aCounter
,
285 layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
286 nsAutoCString
pathPrefix("explicit/"_ns
);
287 pathPrefix
.Append(aPathPrefix
);
289 switch (aCounter
.Type()) {
290 case imgIContainer::TYPE_RASTER
:
291 pathPrefix
.AppendLiteral("/raster/");
293 case imgIContainer::TYPE_VECTOR
:
294 pathPrefix
.AppendLiteral("/vector/");
296 case imgIContainer::TYPE_REQUEST
:
297 pathPrefix
.AppendLiteral("/request/");
300 pathPrefix
.AppendLiteral("/unknown=");
301 pathPrefix
.AppendInt(aCounter
.Type());
302 pathPrefix
.AppendLiteral("/");
306 pathPrefix
.Append(aCounter
.IsUsed() ? "used/" : "unused/");
307 if (aCounter
.IsValidating()) {
308 pathPrefix
.AppendLiteral("validating/");
310 if (aCounter
.HasError()) {
311 pathPrefix
.AppendLiteral("err/");
314 pathPrefix
.AppendLiteral("progress=");
315 pathPrefix
.AppendInt(aCounter
.Progress(), 16);
316 pathPrefix
.AppendLiteral("/");
318 pathPrefix
.AppendLiteral("image(");
319 pathPrefix
.AppendInt(aCounter
.IntrinsicSize().width
);
320 pathPrefix
.AppendLiteral("x");
321 pathPrefix
.AppendInt(aCounter
.IntrinsicSize().height
);
322 pathPrefix
.AppendLiteral(", ");
324 if (aCounter
.URI().IsEmpty()) {
325 pathPrefix
.AppendLiteral("<unknown URI>");
327 pathPrefix
.Append(aCounter
.URI());
330 pathPrefix
.AppendLiteral(")/");
332 ReportSurfaces(aHandleReport
, aData
, pathPrefix
, aCounter
, aSharedSurfaces
);
334 ReportSourceValue(aHandleReport
, aData
, pathPrefix
, aCounter
.Values());
337 static void ReportSurfaces(
338 nsIHandleReportCallback
* aHandleReport
, nsISupports
* aData
,
339 const nsACString
& aPathPrefix
, const ImageMemoryCounter
& aCounter
,
340 layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
341 for (const SurfaceMemoryCounter
& counter
: aCounter
.Surfaces()) {
342 nsAutoCString
surfacePathPrefix(aPathPrefix
);
343 switch (counter
.Type()) {
344 case SurfaceMemoryCounterType::NORMAL
:
345 if (counter
.IsLocked()) {
346 surfacePathPrefix
.AppendLiteral("locked/");
348 surfacePathPrefix
.AppendLiteral("unlocked/");
350 if (counter
.IsFactor2()) {
351 surfacePathPrefix
.AppendLiteral("factor2/");
353 if (counter
.CannotSubstitute()) {
354 surfacePathPrefix
.AppendLiteral("cannot_substitute/");
357 case SurfaceMemoryCounterType::CONTAINER
:
358 surfacePathPrefix
.AppendLiteral("container/");
361 MOZ_ASSERT_UNREACHABLE("Unknown counter type");
365 surfacePathPrefix
.AppendLiteral("types=");
366 surfacePathPrefix
.AppendInt(counter
.Values().SurfaceTypes(), 16);
367 surfacePathPrefix
.AppendLiteral("/surface(");
368 surfacePathPrefix
.AppendInt(counter
.Key().Size().width
);
369 surfacePathPrefix
.AppendLiteral("x");
370 surfacePathPrefix
.AppendInt(counter
.Key().Size().height
);
372 if (!counter
.IsFinished()) {
373 surfacePathPrefix
.AppendLiteral(", incomplete");
376 if (counter
.Values().ExternalHandles() > 0) {
377 surfacePathPrefix
.AppendLiteral(", handles:");
378 surfacePathPrefix
.AppendInt(
379 uint32_t(counter
.Values().ExternalHandles()));
382 ImageMemoryReporter::AppendSharedSurfacePrefix(surfacePathPrefix
, counter
,
385 PlaybackType playback
= counter
.Key().Playback();
386 if (playback
== PlaybackType::eAnimated
) {
387 if (StaticPrefs::image_mem_debug_reporting()) {
388 surfacePathPrefix
.AppendPrintf(
389 " (animation %4u)", uint32_t(counter
.Values().FrameIndex()));
391 surfacePathPrefix
.AppendLiteral(" (animation)");
395 if (counter
.Key().Flags() != DefaultSurfaceFlags()) {
396 surfacePathPrefix
.AppendLiteral(", flags:");
397 surfacePathPrefix
.AppendInt(uint32_t(counter
.Key().Flags()),
401 if (counter
.Key().Region()) {
402 const ImageIntRegion
& region
= counter
.Key().Region().ref();
403 const gfx::IntRect
& rect
= region
.Rect();
404 surfacePathPrefix
.AppendLiteral(", region:[ rect=(");
405 surfacePathPrefix
.AppendInt(rect
.x
);
406 surfacePathPrefix
.AppendLiteral(",");
407 surfacePathPrefix
.AppendInt(rect
.y
);
408 surfacePathPrefix
.AppendLiteral(") ");
409 surfacePathPrefix
.AppendInt(rect
.width
);
410 surfacePathPrefix
.AppendLiteral("x");
411 surfacePathPrefix
.AppendInt(rect
.height
);
412 if (region
.IsRestricted()) {
413 const gfx::IntRect
& restrict
= region
.Restriction();
414 if (restrict
== rect
) {
415 surfacePathPrefix
.AppendLiteral(", restrict=rect");
417 surfacePathPrefix
.AppendLiteral(", restrict=(");
418 surfacePathPrefix
.AppendInt(restrict
.x
);
419 surfacePathPrefix
.AppendLiteral(",");
420 surfacePathPrefix
.AppendInt(restrict
.y
);
421 surfacePathPrefix
.AppendLiteral(") ");
422 surfacePathPrefix
.AppendInt(restrict
.width
);
423 surfacePathPrefix
.AppendLiteral("x");
424 surfacePathPrefix
.AppendInt(restrict
.height
);
427 if (region
.GetExtendMode() != gfx::ExtendMode::CLAMP
) {
428 surfacePathPrefix
.AppendLiteral(", extendMode=");
429 surfacePathPrefix
.AppendInt(int32_t(region
.GetExtendMode()));
431 surfacePathPrefix
.AppendLiteral("]");
434 const SVGImageContext
& context
= counter
.Key().SVGContext();
435 surfacePathPrefix
.AppendLiteral(", svgContext:[ ");
436 if (context
.GetViewportSize()) {
437 const CSSIntSize
& size
= context
.GetViewportSize().ref();
438 surfacePathPrefix
.AppendLiteral("viewport=(");
439 surfacePathPrefix
.AppendInt(size
.width
);
440 surfacePathPrefix
.AppendLiteral("x");
441 surfacePathPrefix
.AppendInt(size
.height
);
442 surfacePathPrefix
.AppendLiteral(") ");
444 if (context
.GetPreserveAspectRatio()) {
446 context
.GetPreserveAspectRatio()->ToString(aspect
);
447 surfacePathPrefix
.AppendLiteral("preserveAspectRatio=(");
448 LossyAppendUTF16toASCII(aspect
, surfacePathPrefix
);
449 surfacePathPrefix
.AppendLiteral(") ");
451 if (auto scheme
= context
.GetColorScheme()) {
452 surfacePathPrefix
.AppendLiteral("colorScheme=");
453 surfacePathPrefix
.AppendInt(int32_t(*scheme
));
454 surfacePathPrefix
.AppendLiteral(" ");
456 if (context
.GetContextPaint()) {
457 const SVGEmbeddingContextPaint
* paint
= context
.GetContextPaint();
458 surfacePathPrefix
.AppendLiteral("contextPaint=(");
459 if (paint
->GetFill()) {
460 surfacePathPrefix
.AppendLiteral(" fill=");
461 surfacePathPrefix
.AppendInt(paint
->GetFill()->ToABGR(), 16);
463 if (paint
->GetFillOpacity() != 1.0) {
464 surfacePathPrefix
.AppendLiteral(" fillOpa=");
465 surfacePathPrefix
.AppendFloat(paint
->GetFillOpacity());
467 if (paint
->GetStroke()) {
468 surfacePathPrefix
.AppendLiteral(" stroke=");
469 surfacePathPrefix
.AppendInt(paint
->GetStroke()->ToABGR(), 16);
471 if (paint
->GetStrokeOpacity() != 1.0) {
472 surfacePathPrefix
.AppendLiteral(" strokeOpa=");
473 surfacePathPrefix
.AppendFloat(paint
->GetStrokeOpacity());
475 surfacePathPrefix
.AppendLiteral(" ) ");
477 surfacePathPrefix
.AppendLiteral("]");
479 surfacePathPrefix
.AppendLiteral(")/");
481 ReportValues(aHandleReport
, aData
, surfacePathPrefix
, counter
.Values());
485 static void ReportTotal(nsIHandleReportCallback
* aHandleReport
,
486 nsISupports
* aData
, bool aExplicit
,
487 const char* aPathPrefix
, const char* aPathInfix
,
488 const MemoryTotal
& aTotal
) {
489 nsAutoCString pathPrefix
;
491 pathPrefix
.AppendLiteral("explicit/");
493 pathPrefix
.Append(aPathPrefix
);
495 nsAutoCString
rasterUsedPrefix(pathPrefix
);
496 rasterUsedPrefix
.AppendLiteral("/raster/used/");
497 rasterUsedPrefix
.Append(aPathInfix
);
498 ReportValues(aHandleReport
, aData
, rasterUsedPrefix
, aTotal
.UsedRaster());
500 nsAutoCString
rasterUnusedPrefix(pathPrefix
);
501 rasterUnusedPrefix
.AppendLiteral("/raster/unused/");
502 rasterUnusedPrefix
.Append(aPathInfix
);
503 ReportValues(aHandleReport
, aData
, rasterUnusedPrefix
,
504 aTotal
.UnusedRaster());
506 nsAutoCString
vectorUsedPrefix(pathPrefix
);
507 vectorUsedPrefix
.AppendLiteral("/vector/used/");
508 vectorUsedPrefix
.Append(aPathInfix
);
509 ReportValues(aHandleReport
, aData
, vectorUsedPrefix
, aTotal
.UsedVector());
511 nsAutoCString
vectorUnusedPrefix(pathPrefix
);
512 vectorUnusedPrefix
.AppendLiteral("/vector/unused/");
513 vectorUnusedPrefix
.Append(aPathInfix
);
514 ReportValues(aHandleReport
, aData
, vectorUnusedPrefix
,
515 aTotal
.UnusedVector());
518 static void ReportValues(nsIHandleReportCallback
* aHandleReport
,
519 nsISupports
* aData
, const nsACString
& aPathPrefix
,
520 const MemoryCounter
& aCounter
) {
521 ReportSourceValue(aHandleReport
, aData
, aPathPrefix
, aCounter
);
523 ReportValue(aHandleReport
, aData
, KIND_HEAP
, aPathPrefix
, "decoded-heap",
524 "Decoded image data which is stored on the heap.",
525 aCounter
.DecodedHeap());
527 ReportValue(aHandleReport
, aData
, KIND_NONHEAP
, aPathPrefix
,
529 "Decoded image data which isn't stored on the heap.",
530 aCounter
.DecodedNonHeap());
532 // We don't know for certain whether or not it is on the heap, so let's
533 // just report it as non-heap for reporting purposes.
534 ReportValue(aHandleReport
, aData
, KIND_NONHEAP
, aPathPrefix
,
536 "Decoded image data which is unknown to be on the heap or not.",
537 aCounter
.DecodedUnknown());
540 static void ReportSourceValue(nsIHandleReportCallback
* aHandleReport
,
542 const nsACString
& aPathPrefix
,
543 const MemoryCounter
& aCounter
) {
544 ReportValue(aHandleReport
, aData
, KIND_HEAP
, aPathPrefix
, "source",
545 "Raster image source data and vector image documents.",
549 static void ReportValue(nsIHandleReportCallback
* aHandleReport
,
550 nsISupports
* aData
, int32_t aKind
,
551 const nsACString
& aPathPrefix
,
552 const char* aPathSuffix
, const char* aDescription
,
558 nsAutoCString
desc(aDescription
);
559 nsAutoCString
path(aPathPrefix
);
560 path
.Append(aPathSuffix
);
562 aHandleReport
->Callback(""_ns
, path
, aKind
, UNITS_BYTES
, aValue
, desc
,
566 static void RecordCounterForRequest(imgRequest
* aRequest
,
567 nsTArray
<ImageMemoryCounter
>* aArray
,
569 SizeOfState
state(ImagesMallocSizeOf
);
570 RefPtr
<image::Image
> image
= aRequest
->GetImage();
572 ImageMemoryCounter
counter(aRequest
, image
, state
, aIsUsed
);
573 aArray
->AppendElement(std::move(counter
));
575 // We can at least record some information about the image from the
576 // request, and mark it as not knowing the image type yet.
577 ImageMemoryCounter
counter(aRequest
, state
, aIsUsed
);
578 aArray
->AppendElement(std::move(counter
));
583 NS_IMPL_ISUPPORTS(imgMemoryReporter
, nsIMemoryReporter
)
585 NS_IMPL_ISUPPORTS(nsProgressNotificationProxy
, nsIProgressEventSink
,
586 nsIChannelEventSink
, nsIInterfaceRequestor
)
589 nsProgressNotificationProxy::OnProgress(nsIRequest
* request
, int64_t progress
,
590 int64_t progressMax
) {
591 nsCOMPtr
<nsILoadGroup
> loadGroup
;
592 request
->GetLoadGroup(getter_AddRefs(loadGroup
));
594 nsCOMPtr
<nsIProgressEventSink
> target
;
595 NS_QueryNotificationCallbacks(mOriginalCallbacks
, loadGroup
,
596 NS_GET_IID(nsIProgressEventSink
),
597 getter_AddRefs(target
));
601 return target
->OnProgress(mImageRequest
, progress
, progressMax
);
605 nsProgressNotificationProxy::OnStatus(nsIRequest
* request
, nsresult status
,
606 const char16_t
* statusArg
) {
607 nsCOMPtr
<nsILoadGroup
> loadGroup
;
608 request
->GetLoadGroup(getter_AddRefs(loadGroup
));
610 nsCOMPtr
<nsIProgressEventSink
> target
;
611 NS_QueryNotificationCallbacks(mOriginalCallbacks
, loadGroup
,
612 NS_GET_IID(nsIProgressEventSink
),
613 getter_AddRefs(target
));
617 return target
->OnStatus(mImageRequest
, status
, statusArg
);
621 nsProgressNotificationProxy::AsyncOnChannelRedirect(
622 nsIChannel
* oldChannel
, nsIChannel
* newChannel
, uint32_t flags
,
623 nsIAsyncVerifyRedirectCallback
* cb
) {
624 // Tell the original original callbacks about it too
625 nsCOMPtr
<nsILoadGroup
> loadGroup
;
626 newChannel
->GetLoadGroup(getter_AddRefs(loadGroup
));
627 nsCOMPtr
<nsIChannelEventSink
> target
;
628 NS_QueryNotificationCallbacks(mOriginalCallbacks
, loadGroup
,
629 NS_GET_IID(nsIChannelEventSink
),
630 getter_AddRefs(target
));
632 cb
->OnRedirectVerifyCallback(NS_OK
);
636 // Delegate to |target| if set, reusing |cb|
637 return target
->AsyncOnChannelRedirect(oldChannel
, newChannel
, flags
, cb
);
641 nsProgressNotificationProxy::GetInterface(const nsIID
& iid
, void** result
) {
642 if (iid
.Equals(NS_GET_IID(nsIProgressEventSink
))) {
643 *result
= static_cast<nsIProgressEventSink
*>(this);
647 if (iid
.Equals(NS_GET_IID(nsIChannelEventSink
))) {
648 *result
= static_cast<nsIChannelEventSink
*>(this);
652 if (mOriginalCallbacks
) {
653 return mOriginalCallbacks
->GetInterface(iid
, result
);
655 return NS_NOINTERFACE
;
658 static void NewRequestAndEntry(bool aForcePrincipalCheckForCacheEntry
,
659 imgLoader
* aLoader
, const ImageCacheKey
& aKey
,
660 imgRequest
** aRequest
, imgCacheEntry
** aEntry
) {
661 RefPtr
<imgRequest
> request
= new imgRequest(aLoader
, aKey
);
662 RefPtr
<imgCacheEntry
> entry
=
663 new imgCacheEntry(aLoader
, request
, aForcePrincipalCheckForCacheEntry
);
664 aLoader
->AddToUncachedImages(request
);
665 request
.forget(aRequest
);
666 entry
.forget(aEntry
);
669 static bool ShouldRevalidateEntry(imgCacheEntry
* aEntry
, nsLoadFlags aFlags
,
671 if (aFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
674 if (aFlags
& nsIRequest::VALIDATE_ALWAYS
) {
677 if (aEntry
->GetMustValidate()) {
681 // The cache entry has expired... Determine whether the stale cache
682 // entry can be used without validation...
683 if (aFlags
& (nsIRequest::LOAD_FROM_CACHE
| nsIRequest::VALIDATE_NEVER
|
684 nsIRequest::VALIDATE_ONCE_PER_SESSION
)) {
685 // LOAD_FROM_CACHE, VALIDATE_NEVER and VALIDATE_ONCE_PER_SESSION allow
686 // stale cache entries to be used unless they have been explicitly marked
687 // to indicate that revalidation is necessary.
690 // Entry is expired, revalidate.
696 /* Call content policies on cached images that went through a redirect */
697 static bool ShouldLoadCachedImage(imgRequest
* aImgRequest
,
698 Document
* aLoadingDocument
,
699 nsIPrincipal
* aTriggeringPrincipal
,
700 nsContentPolicyType aPolicyType
,
701 bool aSendCSPViolationReports
) {
702 /* Call content policies on cached images - Bug 1082837
703 * Cached images are keyed off of the first uri in a redirect chain.
704 * Hence content policies don't get a chance to test the intermediate hops
705 * or the final destination. Here we test the final destination using
706 * mFinalURI off of the imgRequest and passing it into content policies.
707 * For Mixed Content Blocker, we do an additional check to determine if any
708 * of the intermediary hops went through an insecure redirect with the
709 * mHadInsecureRedirect flag
711 bool insecureRedirect
= aImgRequest
->HadInsecureRedirect();
712 nsCOMPtr
<nsIURI
> contentLocation
;
713 aImgRequest
->GetFinalURI(getter_AddRefs(contentLocation
));
716 nsCOMPtr
<nsIPrincipal
> loadingPrincipal
=
717 aLoadingDocument
? aLoadingDocument
->NodePrincipal()
718 : aTriggeringPrincipal
;
719 // If there is no context and also no triggeringPrincipal, then we use a fresh
720 // nullPrincipal as the loadingPrincipal because we can not create a loadinfo
721 // without a valid loadingPrincipal.
722 if (!loadingPrincipal
) {
723 loadingPrincipal
= NullPrincipal::CreateWithoutOriginAttributes();
726 nsCOMPtr
<nsILoadInfo
> secCheckLoadInfo
= new LoadInfo(
727 loadingPrincipal
, aTriggeringPrincipal
, aLoadingDocument
,
728 nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK
, aPolicyType
);
730 secCheckLoadInfo
->SetSendCSPViolationEvents(aSendCSPViolationReports
);
732 int16_t decision
= nsIContentPolicy::REJECT_REQUEST
;
733 rv
= NS_CheckContentLoadPolicy(contentLocation
, secCheckLoadInfo
,
735 &decision
, nsContentUtils::GetContentPolicy());
736 if (NS_FAILED(rv
) || !NS_CP_ACCEPTED(decision
)) {
740 // We call all Content Policies above, but we also have to call mcb
741 // individually to check the intermediary redirect hops are secure.
742 if (insecureRedirect
) {
743 // Bug 1314356: If the image ended up in the cache upgraded by HSTS and the
744 // page uses upgrade-inscure-requests it had an insecure redirect
745 // (http->https). We need to invalidate the image and reload it because
746 // mixed content blocker only bails if upgrade-insecure-requests is set on
747 // the doc and the resource load is http: which would result in an incorrect
748 // mixed content warning.
749 nsCOMPtr
<nsIDocShell
> docShell
=
750 NS_CP_GetDocShellFromContext(ToSupports(aLoadingDocument
));
752 Document
* document
= docShell
->GetDocument();
753 if (document
&& document
->GetUpgradeInsecureRequests(false)) {
758 if (!aTriggeringPrincipal
|| !aTriggeringPrincipal
->IsSystemPrincipal()) {
759 // reset the decision for mixed content blocker check
760 decision
= nsIContentPolicy::REJECT_REQUEST
;
761 rv
= nsMixedContentBlocker::ShouldLoad(insecureRedirect
, contentLocation
,
764 true, // aReportError
766 if (NS_FAILED(rv
) || !NS_CP_ACCEPTED(decision
)) {
775 // Returns true if this request is compatible with the given CORS mode on the
776 // given loading principal, and false if the request may not be reused due
778 static bool ValidateCORSMode(imgRequest
* aRequest
, bool aForcePrincipalCheck
,
780 nsIPrincipal
* aTriggeringPrincipal
) {
781 // If the entry's CORS mode doesn't match, or the CORS mode matches but the
782 // document principal isn't the same, we can't use this request.
783 if (aRequest
->GetCORSMode() != aCORSMode
) {
787 if (aRequest
->GetCORSMode() != CORS_NONE
|| aForcePrincipalCheck
) {
788 nsCOMPtr
<nsIPrincipal
> otherprincipal
= aRequest
->GetTriggeringPrincipal();
790 // If we previously had a principal, but we don't now, we can't use this
792 if (otherprincipal
&& !aTriggeringPrincipal
) {
796 if (otherprincipal
&& aTriggeringPrincipal
&&
797 !otherprincipal
->Equals(aTriggeringPrincipal
)) {
805 static bool ValidateSecurityInfo(imgRequest
* aRequest
,
806 bool aForcePrincipalCheck
, CORSMode aCORSMode
,
807 nsIPrincipal
* aTriggeringPrincipal
,
808 Document
* aLoadingDocument
,
809 nsContentPolicyType aPolicyType
) {
810 if (!ValidateCORSMode(aRequest
, aForcePrincipalCheck
, aCORSMode
,
811 aTriggeringPrincipal
)) {
814 // Content Policy Check on Cached Images
815 return ShouldLoadCachedImage(aRequest
, aLoadingDocument
, aTriggeringPrincipal
,
817 /* aSendCSPViolationReports */ false);
820 static nsresult
NewImageChannel(
821 nsIChannel
** aResult
,
822 // If aForcePrincipalCheckForCacheEntry is true, then we will
823 // force a principal check even when not using CORS before
824 // assuming we have a cache hit on a cache entry that we
825 // create for this channel. This is an out param that should
826 // be set to true if this channel ends up depending on
827 // aTriggeringPrincipal and false otherwise.
828 bool* aForcePrincipalCheckForCacheEntry
, nsIURI
* aURI
,
829 nsIURI
* aInitialDocumentURI
, CORSMode aCORSMode
,
830 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
831 nsLoadFlags aLoadFlags
, nsContentPolicyType aPolicyType
,
832 nsIPrincipal
* aTriggeringPrincipal
, nsINode
* aRequestingNode
,
833 bool aRespectPrivacy
, uint64_t aEarlyHintPreloaderId
) {
837 nsCOMPtr
<nsIHttpChannel
> newHttpChannel
;
839 nsCOMPtr
<nsIInterfaceRequestor
> callbacks
;
842 // Get the notification callbacks from the load group for the new channel.
844 // XXX: This is not exactly correct, because the network request could be
845 // referenced by multiple windows... However, the new channel needs
846 // something. So, using the 'first' notification callbacks is better
849 aLoadGroup
->GetNotificationCallbacks(getter_AddRefs(callbacks
));
852 // Pass in a nullptr loadgroup because this is the underlying network
853 // request. This request may be referenced by several proxy image requests
854 // (possibly in different documents).
855 // If all of the proxy requests are canceled then this request should be
859 nsSecurityFlags securityFlags
=
860 nsContentSecurityManager::ComputeSecurityFlags(
861 aCORSMode
, nsContentSecurityManager::CORSSecurityMapping::
862 CORS_NONE_MAPS_TO_INHERITED_CONTEXT
);
864 securityFlags
|= nsILoadInfo::SEC_ALLOW_CHROME
;
866 // Note we are calling NS_NewChannelWithTriggeringPrincipal() here with a
867 // node and a principal. This is for things like background images that are
868 // specified by user stylesheets, where the document is being styled, but
869 // the principal is that of the user stylesheet.
870 if (aRequestingNode
&& aTriggeringPrincipal
) {
871 rv
= NS_NewChannelWithTriggeringPrincipal(aResult
, aURI
, aRequestingNode
,
872 aTriggeringPrincipal
,
873 securityFlags
, aPolicyType
,
874 nullptr, // PerformanceStorage
875 nullptr, // loadGroup
876 callbacks
, aLoadFlags
);
882 if (aPolicyType
== nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON
) {
883 // If this is a favicon loading, we will use the originAttributes from the
884 // triggeringPrincipal as the channel's originAttributes. This allows the
885 // favicon loading from XUL will use the correct originAttributes.
887 nsCOMPtr
<nsILoadInfo
> loadInfo
= (*aResult
)->LoadInfo();
888 rv
= loadInfo
->SetOriginAttributes(
889 aTriggeringPrincipal
->OriginAttributesRef());
892 // either we are loading something inside a document, in which case
893 // we should always have a requestingNode, or we are loading something
894 // outside a document, in which case the triggeringPrincipal and
895 // triggeringPrincipal should always be the systemPrincipal.
896 // However, there are exceptions: one is Notifications which create a
897 // channel in the parent process in which case we can't get a
899 rv
= NS_NewChannel(aResult
, aURI
, nsContentUtils::GetSystemPrincipal(),
900 securityFlags
, aPolicyType
,
901 nullptr, // nsICookieJarSettings
902 nullptr, // PerformanceStorage
903 nullptr, // loadGroup
904 callbacks
, aLoadFlags
);
910 // Use the OriginAttributes from the loading principal, if one is available,
911 // and adjust the private browsing ID based on what kind of load the caller
912 // has asked us to perform.
913 OriginAttributes attrs
;
914 if (aTriggeringPrincipal
) {
915 attrs
= aTriggeringPrincipal
->OriginAttributesRef();
917 attrs
.mPrivateBrowsingId
= aRespectPrivacy
? 1 : 0;
919 nsCOMPtr
<nsILoadInfo
> loadInfo
= (*aResult
)->LoadInfo();
920 rv
= loadInfo
->SetOriginAttributes(attrs
);
927 // only inherit if we have a principal
928 *aForcePrincipalCheckForCacheEntry
=
929 aTriggeringPrincipal
&& nsContentUtils::ChannelShouldInheritPrincipal(
930 aTriggeringPrincipal
, aURI
,
931 /* aInheritForAboutBlank */ false,
932 /* aForceInherit */ false);
934 // Initialize HTTP-specific attributes
935 newHttpChannel
= do_QueryInterface(*aResult
);
936 if (newHttpChannel
) {
937 nsCOMPtr
<nsIHttpChannelInternal
> httpChannelInternal
=
938 do_QueryInterface(newHttpChannel
);
939 NS_ENSURE_TRUE(httpChannelInternal
, NS_ERROR_UNEXPECTED
);
940 rv
= httpChannelInternal
->SetDocumentURI(aInitialDocumentURI
);
941 MOZ_ASSERT(NS_SUCCEEDED(rv
));
943 DebugOnly
<nsresult
> rv
= newHttpChannel
->SetReferrerInfo(aReferrerInfo
);
944 MOZ_ASSERT(NS_SUCCEEDED(rv
));
947 if (aEarlyHintPreloaderId
) {
948 rv
= httpChannelInternal
->SetEarlyHintPreloaderId(aEarlyHintPreloaderId
);
949 NS_ENSURE_SUCCESS(rv
, rv
);
953 // Image channels are loaded by default with reduced priority.
954 nsCOMPtr
<nsISupportsPriority
> p
= do_QueryInterface(*aResult
);
956 uint32_t priority
= nsISupportsPriority::PRIORITY_LOW
;
958 if (aLoadFlags
& nsIRequest::LOAD_BACKGROUND
) {
959 ++priority
; // further reduce priority for background loads
962 p
->AdjustPriority(priority
);
965 // Create a new loadgroup for this new channel, using the old group as
966 // the parent. The indirection keeps the channel insulated from cancels,
967 // but does allow a way for this revalidation to be associated with at
968 // least one base load group for scheduling/caching purposes.
970 nsCOMPtr
<nsILoadGroup
> loadGroup
= do_CreateInstance(NS_LOADGROUP_CONTRACTID
);
971 nsCOMPtr
<nsILoadGroupChild
> childLoadGroup
= do_QueryInterface(loadGroup
);
972 if (childLoadGroup
) {
973 childLoadGroup
->SetParentLoadGroup(aLoadGroup
);
975 (*aResult
)->SetLoadGroup(loadGroup
);
980 static uint32_t SecondsFromPRTime(PRTime aTime
) {
981 return nsContentUtils::SecondsFromPRTime(aTime
);
985 imgCacheEntry::imgCacheEntry(imgLoader
* loader
, imgRequest
* request
,
986 bool forcePrincipalCheck
)
990 mTouchedTime(SecondsFromPRTime(PR_Now())),
991 mLoadTime(SecondsFromPRTime(PR_Now())),
993 mMustValidate(false),
994 // We start off as evicted so we don't try to update the cache.
995 // PutIntoCache will set this to false.
998 mForcePrincipalCheck(forcePrincipalCheck
),
999 mHasNotified(false) {}
1001 imgCacheEntry::~imgCacheEntry() {
1002 LOG_FUNC(gImgLog
, "imgCacheEntry::~imgCacheEntry()");
1005 void imgCacheEntry::Touch(bool updateTime
/* = true */) {
1006 LOG_SCOPE(gImgLog
, "imgCacheEntry::Touch");
1009 mTouchedTime
= SecondsFromPRTime(PR_Now());
1015 void imgCacheEntry::UpdateCache(int32_t diff
/* = 0 */) {
1016 // Don't update the cache if we've been removed from it or it doesn't care
1017 // about our size or usage.
1018 if (!Evicted() && HasNoProxies()) {
1019 mLoader
->CacheEntriesChanged(diff
);
1023 void imgCacheEntry::UpdateLoadTime() {
1024 mLoadTime
= SecondsFromPRTime(PR_Now());
1027 void imgCacheEntry::SetHasNoProxies(bool hasNoProxies
) {
1028 if (MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1030 LOG_FUNC_WITH_PARAM(gImgLog
, "imgCacheEntry::SetHasNoProxies true", "uri",
1031 mRequest
->CacheKey().URI());
1033 LOG_FUNC_WITH_PARAM(gImgLog
, "imgCacheEntry::SetHasNoProxies false",
1034 "uri", mRequest
->CacheKey().URI());
1038 mHasNoProxies
= hasNoProxies
;
1041 imgCacheQueue::imgCacheQueue() : mDirty(false), mSize(0) {}
1043 void imgCacheQueue::UpdateSize(int32_t diff
) { mSize
+= diff
; }
1045 uint32_t imgCacheQueue::GetSize() const { return mSize
; }
1047 void imgCacheQueue::Remove(imgCacheEntry
* entry
) {
1048 uint64_t index
= mQueue
.IndexOf(entry
);
1049 if (index
== queueContainer::NoIndex
) {
1053 mSize
-= mQueue
[index
]->GetDataSize();
1055 // If the queue is clean and this is the first entry,
1056 // then we can efficiently remove the entry without
1057 // dirtying the sort order.
1058 if (!IsDirty() && index
== 0) {
1059 std::pop_heap(mQueue
.begin(), mQueue
.end(), imgLoader::CompareCacheEntries
);
1060 mQueue
.RemoveLastElement();
1064 // Remove from the middle of the list. This potentially
1065 // breaks the binary heap sort order.
1066 mQueue
.RemoveElementAt(index
);
1068 // If we only have one entry or the queue is empty, though,
1069 // then the sort order is still effectively good. Simply
1070 // refresh the list to clear the dirty flag.
1071 if (mQueue
.Length() <= 1) {
1076 // Otherwise we must mark the queue dirty and potentially
1077 // trigger an expensive sort later.
1081 void imgCacheQueue::Push(imgCacheEntry
* entry
) {
1082 mSize
+= entry
->GetDataSize();
1084 RefPtr
<imgCacheEntry
> refptr(entry
);
1085 mQueue
.AppendElement(std::move(refptr
));
1086 // If we're not dirty already, then we can efficiently add this to the
1087 // binary heap immediately. This is only O(log n).
1089 std::push_heap(mQueue
.begin(), mQueue
.end(),
1090 imgLoader::CompareCacheEntries
);
1094 already_AddRefed
<imgCacheEntry
> imgCacheQueue::Pop() {
1095 if (mQueue
.IsEmpty()) {
1102 std::pop_heap(mQueue
.begin(), mQueue
.end(), imgLoader::CompareCacheEntries
);
1103 RefPtr
<imgCacheEntry
> entry
= mQueue
.PopLastElement();
1105 mSize
-= entry
->GetDataSize();
1106 return entry
.forget();
1109 void imgCacheQueue::Refresh() {
1110 // Resort the list. This is an O(3 * n) operation and best avoided
1112 std::make_heap(mQueue
.begin(), mQueue
.end(), imgLoader::CompareCacheEntries
);
1116 void imgCacheQueue::MarkDirty() { mDirty
= true; }
1118 bool imgCacheQueue::IsDirty() { return mDirty
; }
1120 uint32_t imgCacheQueue::GetNumElements() const { return mQueue
.Length(); }
1122 bool imgCacheQueue::Contains(imgCacheEntry
* aEntry
) const {
1123 return mQueue
.Contains(aEntry
);
1126 imgCacheQueue::iterator
imgCacheQueue::begin() { return mQueue
.begin(); }
1128 imgCacheQueue::const_iterator
imgCacheQueue::begin() const {
1129 return mQueue
.begin();
1132 imgCacheQueue::iterator
imgCacheQueue::end() { return mQueue
.end(); }
1134 imgCacheQueue::const_iterator
imgCacheQueue::end() const {
1135 return mQueue
.end();
1138 nsresult
imgLoader::CreateNewProxyForRequest(
1139 imgRequest
* aRequest
, nsIURI
* aURI
, nsILoadGroup
* aLoadGroup
,
1140 Document
* aLoadingDocument
, imgINotificationObserver
* aObserver
,
1141 nsLoadFlags aLoadFlags
, imgRequestProxy
** _retval
) {
1142 LOG_SCOPE_WITH_PARAM(gImgLog
, "imgLoader::CreateNewProxyForRequest",
1143 "imgRequest", aRequest
);
1145 /* XXX If we move decoding onto separate threads, we should save off the
1146 calling thread here and pass it off to |proxyRequest| so that it call
1147 proxy calls to |aObserver|.
1150 RefPtr
<imgRequestProxy
> proxyRequest
= new imgRequestProxy();
1152 /* It is important to call |SetLoadFlags()| before calling |Init()| because
1153 |Init()| adds the request to the loadgroup.
1155 proxyRequest
->SetLoadFlags(aLoadFlags
);
1157 // init adds itself to imgRequest's list of observers
1158 nsresult rv
= proxyRequest
->Init(aRequest
, aLoadGroup
, aURI
, aObserver
);
1159 if (NS_WARN_IF(NS_FAILED(rv
))) {
1163 proxyRequest
.forget(_retval
);
1167 class imgCacheExpirationTracker final
1168 : public nsExpirationTracker
<imgCacheEntry
, 3> {
1169 enum { TIMEOUT_SECONDS
= 10 };
1172 imgCacheExpirationTracker();
1175 void NotifyExpired(imgCacheEntry
* entry
) override
;
1178 imgCacheExpirationTracker::imgCacheExpirationTracker()
1179 : nsExpirationTracker
<imgCacheEntry
, 3>(TIMEOUT_SECONDS
* 1000,
1180 "imgCacheExpirationTracker") {}
1182 void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry
* entry
) {
1183 // Hold on to a reference to this entry, because the expiration tracker
1184 // mechanism doesn't.
1185 RefPtr
<imgCacheEntry
> kungFuDeathGrip(entry
);
1187 if (MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1188 RefPtr
<imgRequest
> req
= entry
->GetRequest();
1190 LOG_FUNC_WITH_PARAM(gImgLog
, "imgCacheExpirationTracker::NotifyExpired",
1191 "entry", req
->CacheKey().URI());
1195 // We can be called multiple times on the same entry. Don't do work multiple
1197 if (!entry
->Evicted()) {
1198 entry
->Loader()->RemoveFromCache(entry
);
1201 entry
->Loader()->VerifyCacheSizes();
1204 ///////////////////////////////////////////////////////////////////////////////
1206 ///////////////////////////////////////////////////////////////////////////////
1208 double imgLoader::sCacheTimeWeight
;
1209 uint32_t imgLoader::sCacheMaxSize
;
1210 imgMemoryReporter
* imgLoader::sMemReporter
;
1212 NS_IMPL_ISUPPORTS(imgLoader
, imgILoader
, nsIContentSniffer
, imgICache
,
1213 nsISupportsWeakReference
, nsIObserver
)
1215 static imgLoader
* gNormalLoader
= nullptr;
1216 static imgLoader
* gPrivateBrowsingLoader
= nullptr;
1219 already_AddRefed
<imgLoader
> imgLoader::CreateImageLoader() {
1220 // In some cases, such as xpctests, XPCOM modules are not automatically
1221 // initialized. We need to make sure that our module is initialized before
1222 // we hand out imgLoader instances and code starts using them.
1223 mozilla::image::EnsureModuleInitialized();
1225 RefPtr
<imgLoader
> loader
= new imgLoader();
1228 return loader
.forget();
1231 imgLoader
* imgLoader::NormalLoader() {
1232 if (!gNormalLoader
) {
1233 gNormalLoader
= CreateImageLoader().take();
1235 return gNormalLoader
;
1238 imgLoader
* imgLoader::PrivateBrowsingLoader() {
1239 if (!gPrivateBrowsingLoader
) {
1240 gPrivateBrowsingLoader
= CreateImageLoader().take();
1241 gPrivateBrowsingLoader
->RespectPrivacyNotifications();
1243 return gPrivateBrowsingLoader
;
1246 imgLoader::imgLoader()
1247 : mUncachedImagesMutex("imgLoader::UncachedImages"),
1248 mRespectPrivacy(false) {
1249 sMemReporter
->AddRef();
1250 sMemReporter
->RegisterLoader(this);
1253 imgLoader::~imgLoader() {
1256 // If there are any of our imgRequest's left they are in the uncached
1257 // images set, so clear their pointer to us.
1258 MutexAutoLock
lock(mUncachedImagesMutex
);
1259 for (RefPtr
<imgRequest
> req
: mUncachedImages
) {
1263 sMemReporter
->UnregisterLoader(this);
1264 sMemReporter
->Release();
1267 void imgLoader::VerifyCacheSizes() {
1269 if (!mCacheTracker
) {
1273 uint32_t cachesize
= mCache
.Count();
1274 uint32_t queuesize
= mCacheQueue
.GetNumElements();
1275 uint32_t trackersize
= 0;
1276 for (nsExpirationTracker
<imgCacheEntry
, 3>::Iterator
it(mCacheTracker
.get());
1280 MOZ_ASSERT(queuesize
== trackersize
, "Queue and tracker sizes out of sync!");
1281 MOZ_ASSERT(queuesize
<= cachesize
, "Queue has more elements than cache!");
1285 void imgLoader::GlobalInit() {
1286 sCacheTimeWeight
= StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
1287 int32_t cachesize
= StaticPrefs::image_cache_size_AtStartup();
1288 sCacheMaxSize
= cachesize
> 0 ? cachesize
: 0;
1290 sMemReporter
= new imgMemoryReporter();
1291 RegisterStrongAsyncMemoryReporter(sMemReporter
);
1292 RegisterImagesContentUsedUncompressedDistinguishedAmount(
1293 imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount
);
1296 void imgLoader::ShutdownMemoryReporter() {
1297 UnregisterImagesContentUsedUncompressedDistinguishedAmount();
1298 UnregisterStrongMemoryReporter(sMemReporter
);
1301 nsresult
imgLoader::InitCache() {
1302 nsCOMPtr
<nsIObserverService
> os
= mozilla::services::GetObserverService();
1304 return NS_ERROR_FAILURE
;
1307 os
->AddObserver(this, "memory-pressure", false);
1308 os
->AddObserver(this, "chrome-flush-caches", false);
1309 os
->AddObserver(this, "last-pb-context-exited", false);
1310 os
->AddObserver(this, "profile-before-change", false);
1311 os
->AddObserver(this, "xpcom-shutdown", false);
1313 mCacheTracker
= MakeUnique
<imgCacheExpirationTracker
>();
1318 nsresult
imgLoader::Init() {
1325 imgLoader::RespectPrivacyNotifications() {
1326 mRespectPrivacy
= true;
1331 imgLoader::Observe(nsISupports
* aSubject
, const char* aTopic
,
1332 const char16_t
* aData
) {
1333 if (strcmp(aTopic
, "memory-pressure") == 0) {
1335 } else if (strcmp(aTopic
, "chrome-flush-caches") == 0) {
1337 ClearImageCache({ClearOption::ChromeOnly
});
1338 } else if (strcmp(aTopic
, "last-pb-context-exited") == 0) {
1339 if (mRespectPrivacy
) {
1342 } else if (strcmp(aTopic
, "profile-before-change") == 0) {
1343 mCacheTracker
= nullptr;
1344 } else if (strcmp(aTopic
, "xpcom-shutdown") == 0) {
1345 mCacheTracker
= nullptr;
1346 ShutdownMemoryReporter();
1349 // (Nothing else should bring us here)
1350 MOZ_ASSERT(0, "Invalid topic received");
1357 imgLoader::ClearCache(bool chrome
) {
1358 if (XRE_IsParentProcess()) {
1359 bool privateLoader
= this == gPrivateBrowsingLoader
;
1360 for (auto* cp
: ContentParent::AllProcesses(ContentParent::eLive
)) {
1361 Unused
<< cp
->SendClearImageCache(privateLoader
, chrome
);
1364 ClearOptions options
;
1366 options
+= ClearOption::ChromeOnly
;
1368 return ClearImageCache(options
);
1372 imgLoader::RemoveEntriesFromPrincipalInAllProcesses(nsIPrincipal
* aPrincipal
) {
1373 if (!XRE_IsParentProcess()) {
1374 return NS_ERROR_NOT_AVAILABLE
;
1377 for (auto* cp
: ContentParent::AllProcesses(ContentParent::eLive
)) {
1378 Unused
<< cp
->SendClearImageCacheFromPrincipal(aPrincipal
);
1382 if (aPrincipal
->OriginAttributesRef().mPrivateBrowsingId
==
1383 nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID
) {
1384 loader
= imgLoader::NormalLoader();
1386 loader
= imgLoader::PrivateBrowsingLoader();
1389 return loader
->RemoveEntriesInternal(aPrincipal
, nullptr);
1393 imgLoader::RemoveEntriesFromBaseDomainInAllProcesses(
1394 const nsACString
& aBaseDomain
) {
1395 if (!XRE_IsParentProcess()) {
1396 return NS_ERROR_NOT_AVAILABLE
;
1399 for (auto* cp
: ContentParent::AllProcesses(ContentParent::eLive
)) {
1400 Unused
<< cp
->SendClearImageCacheFromBaseDomain(aBaseDomain
);
1403 return RemoveEntriesInternal(nullptr, &aBaseDomain
);
1406 nsresult
imgLoader::RemoveEntriesInternal(nsIPrincipal
* aPrincipal
,
1407 const nsACString
* aBaseDomain
) {
1408 // Can only clear by either principal or base domain.
1409 if ((!aPrincipal
&& !aBaseDomain
) || (aPrincipal
&& aBaseDomain
)) {
1410 return NS_ERROR_INVALID_ARG
;
1413 nsCOMPtr
<nsIEffectiveTLDService
> tldService
;
1414 AutoTArray
<RefPtr
<imgCacheEntry
>, 128> entriesToBeRemoved
;
1416 // For base domain we only clear the non-chrome cache.
1417 for (const auto& entry
: mCache
) {
1418 const auto& key
= entry
.GetKey();
1420 const bool shouldRemove
= [&] {
1422 nsCOMPtr
<nsIPrincipal
> keyPrincipal
=
1423 BasePrincipal::CreateContentPrincipal(key
.URI(),
1424 key
.OriginAttributesRef());
1425 return keyPrincipal
->Equals(aPrincipal
);
1431 // Clear by baseDomain.
1433 nsresult rv
= key
.URI()->GetHost(host
);
1434 if (NS_FAILED(rv
) || host
.IsEmpty()) {
1439 tldService
= do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID
);
1441 if (NS_WARN_IF(!tldService
)) {
1445 bool hasRootDomain
= false;
1446 rv
= tldService
->HasRootDomain(host
, *aBaseDomain
, &hasRootDomain
);
1447 if (NS_SUCCEEDED(rv
) && hasRootDomain
) {
1451 // If we don't get a direct base domain match, also check for cache of
1452 // third parties partitioned under aBaseDomain.
1454 // The isolation key is either just the base domain, or an origin suffix
1455 // which contains the partitionKey holding the baseDomain.
1457 if (key
.IsolationKeyRef().Equals(*aBaseDomain
)) {
1461 // The isolation key does not match the given base domain. It may be an
1462 // origin suffix. Parse it into origin attributes.
1463 OriginAttributes attrs
;
1464 if (!attrs
.PopulateFromSuffix(key
.IsolationKeyRef())) {
1465 // Key is not an origin suffix.
1469 return StoragePrincipalHelper::PartitionKeyHasBaseDomain(
1470 attrs
.mPartitionKey
, *aBaseDomain
);
1474 entriesToBeRemoved
.AppendElement(entry
.GetData());
1478 for (auto& entry
: entriesToBeRemoved
) {
1479 if (!RemoveFromCache(entry
)) {
1481 "Couldn't remove an entry from the cache in "
1482 "RemoveEntriesInternal()\n");
1489 constexpr auto AllCORSModes() {
1490 return MakeInclusiveEnumeratedRange(kFirstCORSMode
, kLastCORSMode
);
1494 imgLoader::RemoveEntry(nsIURI
* aURI
, Document
* aDoc
) {
1498 OriginAttributes attrs
;
1500 attrs
= aDoc
->NodePrincipal()->OriginAttributesRef();
1502 for (auto corsMode
: AllCORSModes()) {
1503 ImageCacheKey
key(aURI
, corsMode
, attrs
, aDoc
);
1504 RemoveFromCache(key
);
1510 imgLoader::FindEntryProperties(nsIURI
* uri
, Document
* aDoc
,
1511 nsIProperties
** _retval
) {
1514 OriginAttributes attrs
;
1516 nsCOMPtr
<nsIPrincipal
> principal
= aDoc
->NodePrincipal();
1518 attrs
= principal
->OriginAttributesRef();
1522 for (auto corsMode
: AllCORSModes()) {
1523 ImageCacheKey
key(uri
, corsMode
, attrs
, aDoc
);
1524 RefPtr
<imgCacheEntry
> entry
;
1525 if (!mCache
.Get(key
, getter_AddRefs(entry
)) || !entry
) {
1528 if (mCacheTracker
&& entry
->HasNoProxies()) {
1529 mCacheTracker
->MarkUsed(entry
);
1531 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1533 nsCOMPtr
<nsIProperties
> properties
= request
->Properties();
1534 properties
.forget(_retval
);
1541 NS_IMETHODIMP_(void)
1542 imgLoader::ClearCacheForControlledDocument(Document
* aDoc
) {
1544 AutoTArray
<RefPtr
<imgCacheEntry
>, 128> entriesToBeRemoved
;
1545 for (const auto& entry
: mCache
) {
1546 const auto& key
= entry
.GetKey();
1547 if (key
.ControlledDocument() == aDoc
) {
1548 entriesToBeRemoved
.AppendElement(entry
.GetData());
1551 for (auto& entry
: entriesToBeRemoved
) {
1552 if (!RemoveFromCache(entry
)) {
1554 "Couldn't remove an entry from the cache in "
1555 "ClearCacheForControlledDocument()\n");
1560 void imgLoader::Shutdown() {
1561 NS_IF_RELEASE(gNormalLoader
);
1562 gNormalLoader
= nullptr;
1563 NS_IF_RELEASE(gPrivateBrowsingLoader
);
1564 gPrivateBrowsingLoader
= nullptr;
1567 bool imgLoader::PutIntoCache(const ImageCacheKey
& aKey
, imgCacheEntry
* entry
) {
1568 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::PutIntoCache", "uri",
1571 // Check to see if this request already exists in the cache. If so, we'll
1572 // replace the old version.
1573 RefPtr
<imgCacheEntry
> tmpCacheEntry
;
1574 if (mCache
.Get(aKey
, getter_AddRefs(tmpCacheEntry
)) && tmpCacheEntry
) {
1576 gImgLog
, LogLevel::Debug
,
1577 ("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
1579 RefPtr
<imgRequest
> tmpRequest
= tmpCacheEntry
->GetRequest();
1581 // If it already exists, and we're putting the same key into the cache, we
1582 // should remove the old version.
1583 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1584 ("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
1587 RemoveFromCache(aKey
);
1589 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1590 ("[this=%p] imgLoader::PutIntoCache --"
1591 " Element NOT already in the cache",
1595 mCache
.InsertOrUpdate(aKey
, RefPtr
{entry
});
1597 // We can be called to resurrect an evicted entry.
1598 if (entry
->Evicted()) {
1599 entry
->SetEvicted(false);
1602 // If we're resurrecting an entry with no proxies, put it back in the
1603 // tracker and queue.
1604 if (entry
->HasNoProxies()) {
1605 nsresult addrv
= NS_OK
;
1607 if (mCacheTracker
) {
1608 addrv
= mCacheTracker
->AddObject(entry
);
1611 if (NS_SUCCEEDED(addrv
)) {
1612 mCacheQueue
.Push(entry
);
1616 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1617 request
->SetIsInCache(true);
1618 RemoveFromUncachedImages(request
);
1623 bool imgLoader::SetHasNoProxies(imgRequest
* aRequest
, imgCacheEntry
* aEntry
) {
1624 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::SetHasNoProxies", "uri",
1625 aRequest
->CacheKey().URI());
1627 aEntry
->SetHasNoProxies(true);
1629 if (aEntry
->Evicted()) {
1633 nsresult addrv
= NS_OK
;
1635 if (mCacheTracker
) {
1636 addrv
= mCacheTracker
->AddObject(aEntry
);
1639 if (NS_SUCCEEDED(addrv
)) {
1640 mCacheQueue
.Push(aEntry
);
1646 bool imgLoader::SetHasProxies(imgRequest
* aRequest
) {
1649 const ImageCacheKey
& key
= aRequest
->CacheKey();
1651 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::SetHasProxies", "uri",
1654 RefPtr
<imgCacheEntry
> entry
;
1655 if (mCache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
1656 // Make sure the cache entry is for the right request
1657 RefPtr
<imgRequest
> entryRequest
= entry
->GetRequest();
1658 if (entryRequest
== aRequest
&& entry
->HasNoProxies()) {
1659 mCacheQueue
.Remove(entry
);
1661 if (mCacheTracker
) {
1662 mCacheTracker
->RemoveObject(entry
);
1665 entry
->SetHasNoProxies(false);
1674 void imgLoader::CacheEntriesChanged(int32_t aSizeDiff
/* = 0 */) {
1675 // We only need to dirty the queue if there is any sorting
1676 // taking place. Empty or single-entry lists can't become
1678 if (mCacheQueue
.GetNumElements() > 1) {
1679 mCacheQueue
.MarkDirty();
1681 mCacheQueue
.UpdateSize(aSizeDiff
);
1684 void imgLoader::CheckCacheLimits() {
1685 if (mCacheQueue
.GetNumElements() == 0) {
1686 NS_ASSERTION(mCacheQueue
.GetSize() == 0,
1687 "imgLoader::CheckCacheLimits -- incorrect cache size");
1690 // Remove entries from the cache until we're back at our desired max size.
1691 while (mCacheQueue
.GetSize() > sCacheMaxSize
) {
1692 // Remove the first entry in the queue.
1693 RefPtr
<imgCacheEntry
> entry(mCacheQueue
.Pop());
1695 NS_ASSERTION(entry
, "imgLoader::CheckCacheLimits -- NULL entry pointer");
1697 if (MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1698 RefPtr
<imgRequest
> req
= entry
->GetRequest();
1700 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::CheckCacheLimits",
1701 "entry", req
->CacheKey().URI());
1706 // We just popped this entry from the queue, so pass AlreadyRemoved
1707 // to avoid searching the queue again in RemoveFromCache.
1708 RemoveFromCache(entry
, QueueState::AlreadyRemoved
);
1713 bool imgLoader::ValidateRequestWithNewChannel(
1714 imgRequest
* request
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
1715 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
1716 imgINotificationObserver
* aObserver
, Document
* aLoadingDocument
,
1717 uint64_t aInnerWindowId
, nsLoadFlags aLoadFlags
,
1718 nsContentPolicyType aLoadPolicyType
, imgRequestProxy
** aProxyRequest
,
1719 nsIPrincipal
* aTriggeringPrincipal
, CORSMode aCORSMode
, bool aLinkPreload
,
1720 uint64_t aEarlyHintPreloaderId
, bool* aNewChannelCreated
) {
1721 // now we need to insert a new channel request object in between the real
1722 // request and the proxy that basically delays loading the image until it
1723 // gets a 304 or figures out that this needs to be a new request
1727 // If we're currently in the middle of validating this request, just hand
1728 // back a proxy to it; the required work will be done for us.
1729 if (imgCacheValidator
* validator
= request
->GetValidator()) {
1730 rv
= CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
1731 aObserver
, aLoadFlags
, aProxyRequest
);
1732 if (NS_FAILED(rv
)) {
1736 if (*aProxyRequest
) {
1737 imgRequestProxy
* proxy
= static_cast<imgRequestProxy
*>(*aProxyRequest
);
1739 // We will send notifications from imgCacheValidator::OnStartRequest().
1740 // In the mean time, we must defer notifications because we are added to
1741 // the imgRequest's proxy list, and we can get extra notifications
1742 // resulting from methods such as StartDecoding(). See bug 579122.
1743 proxy
->MarkValidating();
1746 MOZ_ASSERT(aLoadingDocument
);
1747 auto preloadKey
= PreloadHashKey::CreateAsImage(
1748 aURI
, aTriggeringPrincipal
, aCORSMode
);
1749 proxy
->NotifyOpen(preloadKey
, aLoadingDocument
, true);
1752 // Attach the proxy without notifying
1753 validator
->AddProxy(proxy
);
1758 // We will rely on Necko to cache this request when it's possible, and to
1759 // tell imgCacheValidator::OnStartRequest whether the request came from its
1761 nsCOMPtr
<nsIChannel
> newChannel
;
1762 bool forcePrincipalCheck
;
1764 NewImageChannel(getter_AddRefs(newChannel
), &forcePrincipalCheck
, aURI
,
1765 aInitialDocumentURI
, aCORSMode
, aReferrerInfo
, aLoadGroup
,
1766 aLoadFlags
, aLoadPolicyType
, aTriggeringPrincipal
,
1767 aLoadingDocument
, mRespectPrivacy
, aEarlyHintPreloaderId
);
1768 if (NS_FAILED(rv
)) {
1772 if (aNewChannelCreated
) {
1773 *aNewChannelCreated
= true;
1776 RefPtr
<imgRequestProxy
> req
;
1777 rv
= CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
1778 aObserver
, aLoadFlags
, getter_AddRefs(req
));
1779 if (NS_FAILED(rv
)) {
1783 // Make sure that OnStatus/OnProgress calls have the right request set...
1784 RefPtr
<nsProgressNotificationProxy
> progressproxy
=
1785 new nsProgressNotificationProxy(newChannel
, req
);
1786 if (!progressproxy
) {
1790 RefPtr
<imgCacheValidator
> hvc
=
1791 new imgCacheValidator(progressproxy
, this, request
, aLoadingDocument
,
1792 aInnerWindowId
, forcePrincipalCheck
);
1794 // Casting needed here to get past multiple inheritance.
1795 nsCOMPtr
<nsIStreamListener
> listener
=
1796 static_cast<nsIThreadRetargetableStreamListener
*>(hvc
);
1797 NS_ENSURE_TRUE(listener
, false);
1799 // We must set the notification callbacks before setting up the
1800 // CORS listener, because that's also interested inthe
1801 // notification callbacks.
1802 newChannel
->SetNotificationCallbacks(hvc
);
1804 request
->SetValidator(hvc
);
1806 // We will send notifications from imgCacheValidator::OnStartRequest().
1807 // In the mean time, we must defer notifications because we are added to
1808 // the imgRequest's proxy list, and we can get extra notifications
1809 // resulting from methods such as StartDecoding(). See bug 579122.
1810 req
->MarkValidating();
1813 MOZ_ASSERT(aLoadingDocument
);
1815 PreloadHashKey::CreateAsImage(aURI
, aTriggeringPrincipal
, aCORSMode
);
1816 req
->NotifyOpen(preloadKey
, aLoadingDocument
, true);
1819 // Add the proxy without notifying
1822 mozilla::net::PredictorLearn(aURI
, aInitialDocumentURI
,
1823 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE
,
1825 rv
= newChannel
->AsyncOpen(listener
);
1826 if (NS_WARN_IF(NS_FAILED(rv
))) {
1827 req
->CancelAndForgetObserver(rv
);
1828 // This will notify any current or future <link preload> tags. Pass the
1829 // non-open channel so that we can read loadinfo and referrer info of that
1831 req
->NotifyStart(newChannel
);
1832 // Use the non-channel overload of this method to force the notification to
1833 // happen. The preload request has not been assigned a channel.
1834 req
->NotifyStop(rv
);
1838 req
.forget(aProxyRequest
);
1842 void imgLoader::NotifyObserversForCachedImage(
1843 imgCacheEntry
* aEntry
, imgRequest
* request
, nsIURI
* aURI
,
1844 nsIReferrerInfo
* aReferrerInfo
, Document
* aLoadingDocument
,
1845 nsIPrincipal
* aTriggeringPrincipal
, CORSMode aCORSMode
,
1846 uint64_t aEarlyHintPreloaderId
) {
1847 if (aEntry
->HasNotified()) {
1851 nsCOMPtr
<nsIObserverService
> obsService
= services::GetObserverService();
1853 if (!obsService
->HasObservers("http-on-image-cache-response")) {
1857 aEntry
->SetHasNotified();
1859 nsCOMPtr
<nsIChannel
> newChannel
;
1860 bool forcePrincipalCheck
;
1861 nsresult rv
= NewImageChannel(
1862 getter_AddRefs(newChannel
), &forcePrincipalCheck
, aURI
, nullptr,
1863 aCORSMode
, aReferrerInfo
, nullptr, 0,
1864 nsIContentPolicy::TYPE_INTERNAL_IMAGE
, aTriggeringPrincipal
,
1865 aLoadingDocument
, mRespectPrivacy
, aEarlyHintPreloaderId
);
1866 if (NS_FAILED(rv
)) {
1870 RefPtr
<HttpBaseChannel
> httpBaseChannel
= do_QueryObject(newChannel
);
1871 if (httpBaseChannel
) {
1872 httpBaseChannel
->SetDummyChannelForImageCache();
1873 newChannel
->SetContentType(nsDependentCString(request
->GetMimeType()));
1874 RefPtr
<mozilla::image::Image
> image
= request
->GetImage();
1876 newChannel
->SetContentLength(aEntry
->GetDataSize());
1878 obsService
->NotifyObservers(newChannel
, "http-on-image-cache-response",
1883 bool imgLoader::ValidateEntry(
1884 imgCacheEntry
* aEntry
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
1885 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
1886 imgINotificationObserver
* aObserver
, Document
* aLoadingDocument
,
1887 nsLoadFlags aLoadFlags
, nsContentPolicyType aLoadPolicyType
,
1888 bool aCanMakeNewChannel
, bool* aNewChannelCreated
,
1889 imgRequestProxy
** aProxyRequest
, nsIPrincipal
* aTriggeringPrincipal
,
1890 CORSMode aCORSMode
, bool aLinkPreload
, uint64_t aEarlyHintPreloaderId
) {
1891 LOG_SCOPE(gImgLog
, "imgLoader::ValidateEntry");
1893 // If the expiration time is zero, then the request has not gotten far enough
1894 // to know when it will expire, or we know it will never expire (see
1895 // nsContentUtils::GetSubresourceCacheValidationInfo).
1896 uint32_t expiryTime
= aEntry
->GetExpiryTime();
1897 bool hasExpired
= expiryTime
&& expiryTime
<= SecondsFromPRTime(PR_Now());
1899 // Special treatment for file URLs - aEntry has expired if file has changed
1900 if (nsCOMPtr
<nsIFileURL
> fileUrl
= do_QueryInterface(aURI
)) {
1901 uint32_t lastModTime
= aEntry
->GetLoadTime();
1902 nsCOMPtr
<nsIFile
> theFile
;
1903 if (NS_SUCCEEDED(fileUrl
->GetFile(getter_AddRefs(theFile
)))) {
1905 if (NS_SUCCEEDED(theFile
->GetLastModifiedTime(&fileLastMod
))) {
1906 // nsIFile uses millisec, NSPR usec.
1907 fileLastMod
*= 1000;
1908 hasExpired
= SecondsFromPRTime((PRTime
)fileLastMod
) > lastModTime
;
1913 RefPtr
<imgRequest
> request(aEntry
->GetRequest());
1919 if (!ValidateSecurityInfo(request
, aEntry
->ForcePrincipalCheck(), aCORSMode
,
1920 aTriggeringPrincipal
, aLoadingDocument
,
1925 // data URIs are immutable and by their nature can't leak data, so we can
1926 // just return true in that case. Doing so would mean that shift-reload
1927 // doesn't reload data URI documents/images though (which is handy for
1928 // debugging during gecko development) so we make an exception in that case.
1929 if (aURI
->SchemeIs("data") && !(aLoadFlags
& nsIRequest::LOAD_BYPASS_CACHE
)) {
1933 bool validateRequest
= false;
1935 if (!request
->CanReuseWithoutValidation(aLoadingDocument
)) {
1936 // If we would need to revalidate this entry, but we're being told to
1937 // bypass the cache, we don't allow this entry to be used.
1938 if (aLoadFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
1942 if (MOZ_UNLIKELY(ChaosMode::isActive(ChaosFeature::ImageCache
))) {
1943 if (ChaosMode::randomUint32LessThan(4) < 1) {
1948 // Determine whether the cache aEntry must be revalidated...
1949 validateRequest
= ShouldRevalidateEntry(aEntry
, aLoadFlags
, hasExpired
);
1951 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1952 ("imgLoader::ValidateEntry validating cache entry. "
1953 "validateRequest = %d",
1955 } else if (!aLoadingDocument
&& MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1956 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1957 ("imgLoader::ValidateEntry BYPASSING cache validation for %s "
1958 "because of NULL loading document",
1959 aURI
->GetSpecOrDefault().get()));
1962 // If the original request is still transferring don't kick off a validation
1963 // network request because it is a bit silly to issue a validation request if
1964 // the original request hasn't even finished yet. So just return true
1965 // indicating the caller can create a new proxy for the request and use it as
1967 // This is an optimization but it's also required for correctness. If we don't
1968 // do this then when firing the load complete notification for the original
1969 // request that can unblock load for the document and then spin the event loop
1970 // (see the stack in bug 1641682) which then the OnStartRequest for the
1971 // validation request can fire which can call UpdateProxies and can sync
1972 // notify on the progress tracker about all existing state, which includes
1973 // load complete, so we fire a second load complete notification for the
1975 // In addition, we want to validate if the original request encountered
1976 // an error for two reasons. The first being if the error was a network error
1977 // then trying to re-fetch the image might succeed. The second is more
1978 // complicated. We decide if we should fire the load or error event for img
1979 // elements depending on if the image has error in its status at the time when
1980 // the load complete notification is received, and we set error status on an
1981 // image if it encounters a network error or a decode error with no real way
1982 // to tell them apart. So if we load an image that will produce a decode error
1983 // the first time we will usually fire the load event, and then decode enough
1984 // to encounter the decode error and set the error status on the image. The
1985 // next time we reference the image in the same document the load complete
1986 // notification is replayed and this time the error status from the decode is
1987 // already present so we fire the error event instead of the load event. This
1988 // is a bug (bug 1645576) that we should fix. In order to avoid that bug in
1989 // some cases (specifically the cases when we hit this code and try to
1990 // validate the request) we make sure to validate. This avoids the bug because
1991 // when the load complete notification arrives the proxy is marked as
1992 // validating so it lies about its status and returns nothing.
1993 const bool requestComplete
= [&] {
1994 RefPtr
<ProgressTracker
> tracker
;
1995 RefPtr
<mozilla::image::Image
> image
= request
->GetImage();
1997 tracker
= image
->GetProgressTracker();
1999 tracker
= request
->GetProgressTracker();
2002 tracker
->GetProgress() & (FLAG_LOAD_COMPLETE
| FLAG_HAS_ERROR
);
2005 if (!requestComplete
) {
2009 if (validateRequest
&& aCanMakeNewChannel
) {
2010 LOG_SCOPE(gImgLog
, "imgLoader::ValidateRequest |cache hit| must validate");
2012 uint64_t innerWindowID
=
2013 aLoadingDocument
? aLoadingDocument
->InnerWindowID() : 0;
2014 return ValidateRequestWithNewChannel(
2015 request
, aURI
, aInitialDocumentURI
, aReferrerInfo
, aLoadGroup
,
2016 aObserver
, aLoadingDocument
, innerWindowID
, aLoadFlags
, aLoadPolicyType
,
2017 aProxyRequest
, aTriggeringPrincipal
, aCORSMode
, aLinkPreload
,
2018 aEarlyHintPreloaderId
, aNewChannelCreated
);
2021 if (!validateRequest
) {
2022 NotifyObserversForCachedImage(aEntry
, request
, aURI
, aReferrerInfo
,
2023 aLoadingDocument
, aTriggeringPrincipal
,
2024 aCORSMode
, aEarlyHintPreloaderId
);
2027 return !validateRequest
;
2030 bool imgLoader::RemoveFromCache(const ImageCacheKey
& aKey
) {
2031 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::RemoveFromCache", "uri",
2033 RefPtr
<imgCacheEntry
> entry
;
2034 mCache
.Remove(aKey
, getter_AddRefs(entry
));
2036 MOZ_ASSERT(!entry
->Evicted(), "Evicting an already-evicted cache entry!");
2038 // Entries with no proxies are in the tracker.
2039 if (entry
->HasNoProxies()) {
2040 if (mCacheTracker
) {
2041 mCacheTracker
->RemoveObject(entry
);
2043 mCacheQueue
.Remove(entry
);
2046 entry
->SetEvicted(true);
2048 RefPtr
<imgRequest
> request
= entry
->GetRequest();
2049 request
->SetIsInCache(false);
2050 AddToUncachedImages(request
);
2057 bool imgLoader::RemoveFromCache(imgCacheEntry
* entry
, QueueState aQueueState
) {
2058 LOG_STATIC_FUNC(gImgLog
, "imgLoader::RemoveFromCache entry");
2060 RefPtr
<imgRequest
> request
= entry
->GetRequest();
2062 const ImageCacheKey
& key
= request
->CacheKey();
2063 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::RemoveFromCache",
2064 "entry's uri", key
.URI());
2068 if (entry
->HasNoProxies()) {
2069 LOG_STATIC_FUNC(gImgLog
,
2070 "imgLoader::RemoveFromCache removing from tracker");
2071 if (mCacheTracker
) {
2072 mCacheTracker
->RemoveObject(entry
);
2074 // Only search the queue to remove the entry if its possible it might
2075 // be in the queue. If we know its not in the queue this would be
2077 MOZ_ASSERT_IF(aQueueState
== QueueState::AlreadyRemoved
,
2078 !mCacheQueue
.Contains(entry
));
2079 if (aQueueState
== QueueState::MaybeExists
) {
2080 mCacheQueue
.Remove(entry
);
2084 entry
->SetEvicted(true);
2085 request
->SetIsInCache(false);
2086 AddToUncachedImages(request
);
2094 nsresult
imgLoader::ClearImageCache(ClearOptions aOptions
) {
2095 const bool chromeOnly
= aOptions
.contains(ClearOption::ChromeOnly
);
2096 const auto ShouldRemove
= [&](imgCacheEntry
* aEntry
) {
2098 // TODO: Consider also removing "resource://" etc?
2099 RefPtr
<imgRequest
> request
= aEntry
->GetRequest();
2100 if (!request
|| !request
->CacheKey().URI()->SchemeIs("chrome")) {
2106 if (aOptions
.contains(ClearOption::UnusedOnly
)) {
2107 LOG_STATIC_FUNC(gImgLog
, "imgLoader::ClearImageCache queue");
2108 // We have to make a temporary, since RemoveFromCache removes the element
2109 // from the queue, invalidating iterators.
2110 nsTArray
<RefPtr
<imgCacheEntry
>> entries(mCacheQueue
.GetNumElements());
2111 for (auto& entry
: mCacheQueue
) {
2112 if (ShouldRemove(entry
)) {
2113 entries
.AppendElement(entry
);
2117 // Iterate in reverse order to minimize array copying.
2118 for (auto& entry
: entries
) {
2119 if (!RemoveFromCache(entry
)) {
2120 return NS_ERROR_FAILURE
;
2124 MOZ_ASSERT(chromeOnly
|| mCacheQueue
.GetNumElements() == 0);
2128 LOG_STATIC_FUNC(gImgLog
, "imgLoader::ClearImageCache table");
2129 // We have to make a temporary, since RemoveFromCache removes the element
2130 // from the queue, invalidating iterators.
2131 const auto entries
=
2132 ToTArray
<nsTArray
<RefPtr
<imgCacheEntry
>>>(mCache
.Values());
2133 for (const auto& entry
: entries
) {
2134 if (!ShouldRemove(entry
)) {
2137 if (!RemoveFromCache(entry
)) {
2138 return NS_ERROR_FAILURE
;
2141 MOZ_ASSERT(chromeOnly
|| mCache
.IsEmpty());
2145 void imgLoader::AddToUncachedImages(imgRequest
* aRequest
) {
2146 MutexAutoLock
lock(mUncachedImagesMutex
);
2147 mUncachedImages
.Insert(aRequest
);
2150 void imgLoader::RemoveFromUncachedImages(imgRequest
* aRequest
) {
2151 MutexAutoLock
lock(mUncachedImagesMutex
);
2152 mUncachedImages
.Remove(aRequest
);
2155 #define LOAD_FLAGS_CACHE_MASK \
2156 (nsIRequest::LOAD_BYPASS_CACHE | nsIRequest::LOAD_FROM_CACHE)
2158 #define LOAD_FLAGS_VALIDATE_MASK \
2159 (nsIRequest::VALIDATE_ALWAYS | nsIRequest::VALIDATE_NEVER | \
2160 nsIRequest::VALIDATE_ONCE_PER_SESSION)
2163 imgLoader::LoadImageXPCOM(
2164 nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
, nsIReferrerInfo
* aReferrerInfo
,
2165 nsIPrincipal
* aTriggeringPrincipal
, nsILoadGroup
* aLoadGroup
,
2166 imgINotificationObserver
* aObserver
, Document
* aLoadingDocument
,
2167 nsLoadFlags aLoadFlags
, nsISupports
* aCacheKey
,
2168 nsContentPolicyType aContentPolicyType
, imgIRequest
** _retval
) {
2169 // Optional parameter, so defaults to 0 (== TYPE_INVALID)
2170 if (!aContentPolicyType
) {
2171 aContentPolicyType
= nsIContentPolicy::TYPE_INTERNAL_IMAGE
;
2173 imgRequestProxy
* proxy
;
2175 LoadImage(aURI
, aInitialDocumentURI
, aReferrerInfo
, aTriggeringPrincipal
,
2176 0, aLoadGroup
, aObserver
, aLoadingDocument
, aLoadingDocument
,
2177 aLoadFlags
, aCacheKey
, aContentPolicyType
, u
""_ns
,
2178 /* aUseUrgentStartForChannel */ false, /* aListPreload */ false,
2184 static void MakeRequestStaticIfNeeded(
2185 Document
* aLoadingDocument
, imgRequestProxy
** aProxyAboutToGetReturned
) {
2186 if (!aLoadingDocument
|| !aLoadingDocument
->IsStaticDocument()) {
2190 if (!*aProxyAboutToGetReturned
) {
2194 RefPtr
<imgRequestProxy
> proxy
= dont_AddRef(*aProxyAboutToGetReturned
);
2195 *aProxyAboutToGetReturned
= nullptr;
2197 RefPtr
<imgRequestProxy
> staticProxy
=
2198 proxy
->GetStaticRequest(aLoadingDocument
);
2199 if (staticProxy
!= proxy
) {
2200 proxy
->CancelAndForgetObserver(NS_BINDING_ABORTED
);
2201 proxy
= std::move(staticProxy
);
2203 proxy
.forget(aProxyAboutToGetReturned
);
2206 bool imgLoader::IsImageAvailable(nsIURI
* aURI
,
2207 nsIPrincipal
* aTriggeringPrincipal
,
2208 CORSMode aCORSMode
, Document
* aDocument
) {
2209 ImageCacheKey
key(aURI
, aCORSMode
,
2210 aTriggeringPrincipal
->OriginAttributesRef(), aDocument
);
2211 RefPtr
<imgCacheEntry
> entry
;
2212 if (!mCache
.Get(key
, getter_AddRefs(entry
)) || !entry
) {
2215 RefPtr
<imgRequest
> request
= entry
->GetRequest();
2219 if (nsCOMPtr
<nsILoadGroup
> docLoadGroup
= aDocument
->GetDocumentLoadGroup()) {
2220 nsLoadFlags requestFlags
= nsIRequest::LOAD_NORMAL
;
2221 docLoadGroup
->GetLoadFlags(&requestFlags
);
2222 if (requestFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
2223 // If we're bypassing the cache, treat the image as not available.
2227 return ValidateCORSMode(request
, false, aCORSMode
, aTriggeringPrincipal
);
2230 nsresult
imgLoader::LoadImage(
2231 nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
, nsIReferrerInfo
* aReferrerInfo
,
2232 nsIPrincipal
* aTriggeringPrincipal
, uint64_t aRequestContextID
,
2233 nsILoadGroup
* aLoadGroup
, imgINotificationObserver
* aObserver
,
2234 nsINode
* aContext
, Document
* aLoadingDocument
, nsLoadFlags aLoadFlags
,
2235 nsISupports
* aCacheKey
, nsContentPolicyType aContentPolicyType
,
2236 const nsAString
& initiatorType
, bool aUseUrgentStartForChannel
,
2237 bool aLinkPreload
, uint64_t aEarlyHintPreloaderId
,
2238 imgRequestProxy
** _retval
) {
2241 NS_ASSERTION(aURI
, "imgLoader::LoadImage -- NULL URI pointer");
2244 return NS_ERROR_NULL_POINTER
;
2247 auto makeStaticIfNeeded
= mozilla::MakeScopeExit(
2248 [&] { MakeRequestStaticIfNeeded(aLoadingDocument
, _retval
); });
2250 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK
,
2251 aURI
->GetSpecOrDefault());
2253 LOG_SCOPE_WITH_PARAM(gImgLog
, "imgLoader::LoadImage", "aURI", aURI
);
2257 RefPtr
<imgRequest
> request
;
2260 nsLoadFlags requestFlags
= nsIRequest::LOAD_NORMAL
;
2263 bool isPrivate
= false;
2265 if (aLoadingDocument
) {
2266 isPrivate
= nsContentUtils::IsInPrivateBrowsing(aLoadingDocument
);
2267 } else if (aLoadGroup
) {
2268 isPrivate
= nsContentUtils::IsInPrivateBrowsing(aLoadGroup
);
2270 MOZ_ASSERT(isPrivate
== mRespectPrivacy
);
2272 if (aLoadingDocument
) {
2273 // The given load group should match that of the document if given. If
2274 // that isn't the case, then we need to add more plumbing to ensure we
2275 // block the document as well.
2276 nsCOMPtr
<nsILoadGroup
> docLoadGroup
=
2277 aLoadingDocument
->GetDocumentLoadGroup();
2278 MOZ_ASSERT(docLoadGroup
== aLoadGroup
);
2282 // Get the default load flags from the loadgroup (if possible)...
2284 aLoadGroup
->GetLoadFlags(&requestFlags
);
2287 // Merge the default load flags with those passed in via aLoadFlags.
2288 // Currently, *only* the caching, validation and background load flags
2291 // The flags in aLoadFlags take precedence over the default flags!
2293 if (aLoadFlags
& LOAD_FLAGS_CACHE_MASK
) {
2294 // Override the default caching flags...
2295 requestFlags
= (requestFlags
& ~LOAD_FLAGS_CACHE_MASK
) |
2296 (aLoadFlags
& LOAD_FLAGS_CACHE_MASK
);
2298 if (aLoadFlags
& LOAD_FLAGS_VALIDATE_MASK
) {
2299 // Override the default validation flags...
2300 requestFlags
= (requestFlags
& ~LOAD_FLAGS_VALIDATE_MASK
) |
2301 (aLoadFlags
& LOAD_FLAGS_VALIDATE_MASK
);
2303 if (aLoadFlags
& nsIRequest::LOAD_BACKGROUND
) {
2304 // Propagate background loading...
2305 requestFlags
|= nsIRequest::LOAD_BACKGROUND
;
2309 // Set background loading if it is <link rel=preload>
2310 requestFlags
|= nsIRequest::LOAD_BACKGROUND
;
2313 CORSMode corsmode
= CORS_NONE
;
2314 if (aLoadFlags
& imgILoader::LOAD_CORS_ANONYMOUS
) {
2315 corsmode
= CORS_ANONYMOUS
;
2316 } else if (aLoadFlags
& imgILoader::LOAD_CORS_USE_CREDENTIALS
) {
2317 corsmode
= CORS_USE_CREDENTIALS
;
2320 // Look in the preloaded images of loading document first.
2321 if (!aLinkPreload
&& aLoadingDocument
) {
2322 // All Early Hints preloads are Link preloads, therefore we don't have a
2323 // Early Hints preload here
2324 MOZ_ASSERT(!aEarlyHintPreloaderId
);
2326 PreloadHashKey::CreateAsImage(aURI
, aTriggeringPrincipal
, corsmode
);
2327 if (RefPtr
<PreloaderBase
> preload
=
2328 aLoadingDocument
->Preloads().LookupPreload(key
)) {
2329 RefPtr
<imgRequestProxy
> proxy
= do_QueryObject(preload
);
2332 MOZ_LOG(gImgLog
, LogLevel::Debug
,
2333 ("[this=%p] imgLoader::LoadImage -- preloaded [proxy=%p]"
2335 this, proxy
.get(), aLoadingDocument
));
2337 // Removing the preload for this image to be in parity with Chromium. Any
2338 // following regular image request will be reloaded using the regular
2339 // path: image cache, http cache, network. Any following `<link
2340 // rel=preload as=image>` will start a new image preload that can be
2341 // satisfied from http cache or network.
2343 // There is a spec discussion for "preload cache", see
2344 // https://github.com/w3c/preload/issues/97. And it is also not clear how
2345 // preload image interacts with list of available images, see
2346 // https://github.com/whatwg/html/issues/4474.
2347 proxy
->RemoveSelf(aLoadingDocument
);
2348 proxy
->NotifyUsage(aLoadingDocument
);
2350 imgRequest
* request
= proxy
->GetOwner();
2352 CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
2353 aObserver
, requestFlags
, _retval
);
2354 NS_ENSURE_SUCCESS(rv
, rv
);
2356 imgRequestProxy
* newProxy
= *_retval
;
2357 if (imgCacheValidator
* validator
= request
->GetValidator()) {
2358 newProxy
->MarkValidating();
2359 // Attach the proxy without notifying and this will add us to the load
2361 validator
->AddProxy(newProxy
);
2363 // It's OK to add here even if the request is done. If it is, it'll send
2364 // a OnStopRequest()and the proxy will be removed from the loadgroup in
2365 // imgRequestProxy::OnLoadComplete.
2366 newProxy
->AddToLoadGroup();
2367 newProxy
->NotifyListener();
2374 RefPtr
<imgCacheEntry
> entry
;
2376 // Look in the cache for our URI, and then validate it.
2377 // XXX For now ignore aCacheKey. We will need it in the future
2378 // for correctly dealing with image load requests that are a result
2380 OriginAttributes attrs
;
2381 if (aTriggeringPrincipal
) {
2382 attrs
= aTriggeringPrincipal
->OriginAttributesRef();
2384 ImageCacheKey
key(aURI
, corsmode
, attrs
, aLoadingDocument
);
2385 if (mCache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
2386 bool newChannelCreated
= false;
2387 if (ValidateEntry(entry
, aURI
, aInitialDocumentURI
, aReferrerInfo
,
2388 aLoadGroup
, aObserver
, aLoadingDocument
, requestFlags
,
2389 aContentPolicyType
, true, &newChannelCreated
, _retval
,
2390 aTriggeringPrincipal
, corsmode
, aLinkPreload
,
2391 aEarlyHintPreloaderId
)) {
2392 request
= entry
->GetRequest();
2394 // If this entry has no proxies, its request has no reference to the
2396 if (entry
->HasNoProxies()) {
2397 LOG_FUNC_WITH_PARAM(gImgLog
,
2398 "imgLoader::LoadImage() adding proxyless entry",
2400 MOZ_ASSERT(!request
->HasCacheEntry(),
2401 "Proxyless entry's request has cache entry!");
2402 request
->SetCacheEntry(entry
);
2404 if (mCacheTracker
&& entry
->GetExpirationState()->IsTracked()) {
2405 mCacheTracker
->MarkUsed(entry
);
2411 if (!newChannelCreated
) {
2412 // This is ugly but it's needed to report CSP violations. We have 3
2414 // - we don't have cache. We are not in this if() stmt. A new channel is
2415 // created and that triggers the CSP checks.
2416 // - We have a cache entry and this is blocked by CSP directives.
2417 DebugOnly
<bool> shouldLoad
= ShouldLoadCachedImage(
2418 request
, aLoadingDocument
, aTriggeringPrincipal
, aContentPolicyType
,
2419 /* aSendCSPViolationReports */ true);
2420 MOZ_ASSERT(shouldLoad
);
2423 // We can't use this entry. We'll try to load it off the network, and if
2424 // successful, overwrite the old entry in the cache with a new one.
2429 // Keep the channel in this scope, so we can adjust its notificationCallbacks
2430 // later when we create the proxy.
2431 nsCOMPtr
<nsIChannel
> newChannel
;
2432 // If we didn't get a cache hit, we need to load from the network.
2434 LOG_SCOPE(gImgLog
, "imgLoader::LoadImage |cache miss|");
2436 bool forcePrincipalCheck
;
2437 rv
= NewImageChannel(getter_AddRefs(newChannel
), &forcePrincipalCheck
, aURI
,
2438 aInitialDocumentURI
, corsmode
, aReferrerInfo
,
2439 aLoadGroup
, requestFlags
, aContentPolicyType
,
2440 aTriggeringPrincipal
, aContext
, mRespectPrivacy
,
2441 aEarlyHintPreloaderId
);
2442 if (NS_FAILED(rv
)) {
2443 return NS_ERROR_FAILURE
;
2446 MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel
) == mRespectPrivacy
);
2448 NewRequestAndEntry(forcePrincipalCheck
, this, key
, getter_AddRefs(request
),
2449 getter_AddRefs(entry
));
2451 MOZ_LOG(gImgLog
, LogLevel::Debug
,
2452 ("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
2454 this, request
.get()));
2456 nsCOMPtr
<nsIClassOfService
> cos(do_QueryInterface(newChannel
));
2458 if (aUseUrgentStartForChannel
&& !aLinkPreload
) {
2459 cos
->AddClassFlags(nsIClassOfService::UrgentStart
);
2462 if (StaticPrefs::network_http_tailing_enabled() &&
2463 aContentPolicyType
== nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON
) {
2464 cos
->AddClassFlags(nsIClassOfService::Throttleable
|
2465 nsIClassOfService::Tail
);
2466 nsCOMPtr
<nsIHttpChannel
> httpChannel(do_QueryInterface(newChannel
));
2468 Unused
<< httpChannel
->SetRequestContextID(aRequestContextID
);
2473 nsCOMPtr
<nsILoadGroup
> channelLoadGroup
;
2474 newChannel
->GetLoadGroup(getter_AddRefs(channelLoadGroup
));
2475 rv
= request
->Init(aURI
, aURI
, /* aHadInsecureRedirect = */ false,
2476 channelLoadGroup
, newChannel
, entry
, aLoadingDocument
,
2477 aTriggeringPrincipal
, corsmode
, aReferrerInfo
);
2478 if (NS_FAILED(rv
)) {
2479 return NS_ERROR_FAILURE
;
2482 // Add the initiator type for this image load
2483 nsCOMPtr
<nsITimedChannel
> timedChannel
= do_QueryInterface(newChannel
);
2485 timedChannel
->SetInitiatorType(initiatorType
);
2488 // create the proxy listener
2489 nsCOMPtr
<nsIStreamListener
> listener
= new ProxyListener(request
.get());
2491 MOZ_LOG(gImgLog
, LogLevel::Debug
,
2492 ("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
2495 mozilla::net::PredictorLearn(aURI
, aInitialDocumentURI
,
2496 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE
,
2500 openRes
= newChannel
->AsyncOpen(listener
);
2502 if (NS_FAILED(openRes
)) {
2504 gImgLog
, LogLevel::Debug
,
2505 ("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
2507 this, static_cast<uint32_t>(openRes
)));
2508 request
->CancelAndAbort(openRes
);
2512 // Try to add the new request into the cache.
2513 PutIntoCache(key
, entry
);
2515 LOG_MSG_WITH_PARAM(gImgLog
, "imgLoader::LoadImage |cache hit|", "request",
2519 // If we didn't get a proxy when validating the cache entry, we need to
2522 // ValidateEntry() has three return values: "Is valid," "might be valid --
2523 // validating over network", and "not valid." If we don't have a _retval,
2524 // we know ValidateEntry is not validating over the network, so it's safe
2525 // to SetLoadId here because we know this request is valid for this context.
2527 // Note, however, that this doesn't guarantee the behaviour we want (one
2528 // URL maps to the same image on a page) if we load the same image in a
2529 // different tab (see bug 528003), because its load id will get re-set, and
2530 // that'll cause us to validate over the network.
2531 request
->SetLoadId(aLoadingDocument
);
2533 LOG_MSG(gImgLog
, "imgLoader::LoadImage", "creating proxy request.");
2534 rv
= CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
2535 aObserver
, requestFlags
, _retval
);
2536 if (NS_FAILED(rv
)) {
2540 imgRequestProxy
* proxy
= *_retval
;
2542 // Make sure that OnStatus/OnProgress calls have the right request set, if
2543 // we did create a channel here.
2545 nsCOMPtr
<nsIInterfaceRequestor
> requestor(
2546 new nsProgressNotificationProxy(newChannel
, proxy
));
2548 return NS_ERROR_OUT_OF_MEMORY
;
2550 newChannel
->SetNotificationCallbacks(requestor
);
2554 MOZ_ASSERT(aLoadingDocument
);
2556 PreloadHashKey::CreateAsImage(aURI
, aTriggeringPrincipal
, corsmode
);
2557 proxy
->NotifyOpen(preloadKey
, aLoadingDocument
, true);
2560 // Note that it's OK to add here even if the request is done. If it is,
2561 // it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
2562 // the proxy will be removed from the loadgroup.
2563 proxy
->AddToLoadGroup();
2565 // If we're loading off the network, explicitly don't notify our proxy,
2566 // because necko (or things called from necko, such as imgCacheValidator)
2567 // are going to call our notifications asynchronously, and we can't make it
2568 // further asynchronous because observers might rely on imagelib completing
2569 // its work between the channel's OnStartRequest and OnStopRequest.
2571 proxy
->NotifyListener();
2577 NS_ASSERTION(*_retval
, "imgLoader::LoadImage -- no return value");
2583 imgLoader::LoadImageWithChannelXPCOM(nsIChannel
* channel
,
2584 imgINotificationObserver
* aObserver
,
2585 Document
* aLoadingDocument
,
2586 nsIStreamListener
** listener
,
2587 imgIRequest
** _retval
) {
2589 imgRequestProxy
* proxy
;
2590 result
= LoadImageWithChannel(channel
, aObserver
, aLoadingDocument
, listener
,
2596 nsresult
imgLoader::LoadImageWithChannel(nsIChannel
* channel
,
2597 imgINotificationObserver
* aObserver
,
2598 Document
* aLoadingDocument
,
2599 nsIStreamListener
** listener
,
2600 imgRequestProxy
** _retval
) {
2601 NS_ASSERTION(channel
,
2602 "imgLoader::LoadImageWithChannel -- NULL channel pointer");
2604 MOZ_ASSERT(NS_UsePrivateBrowsing(channel
) == mRespectPrivacy
);
2606 auto makeStaticIfNeeded
= mozilla::MakeScopeExit(
2607 [&] { MakeRequestStaticIfNeeded(aLoadingDocument
, _retval
); });
2609 LOG_SCOPE(gImgLog
, "imgLoader::LoadImageWithChannel");
2610 RefPtr
<imgRequest
> request
;
2612 nsCOMPtr
<nsIURI
> uri
;
2613 channel
->GetURI(getter_AddRefs(uri
));
2615 NS_ENSURE_TRUE(channel
, NS_ERROR_FAILURE
);
2616 nsCOMPtr
<nsILoadInfo
> loadInfo
= channel
->LoadInfo();
2618 OriginAttributes attrs
= loadInfo
->GetOriginAttributes();
2620 // TODO: Get a meaningful cors mode from the caller probably?
2621 const auto corsMode
= CORS_NONE
;
2622 ImageCacheKey
key(uri
, corsMode
, attrs
, aLoadingDocument
);
2624 nsLoadFlags requestFlags
= nsIRequest::LOAD_NORMAL
;
2625 channel
->GetLoadFlags(&requestFlags
);
2627 RefPtr
<imgCacheEntry
> entry
;
2629 if (requestFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
2630 RemoveFromCache(key
);
2632 // Look in the cache for our URI, and then validate it.
2633 // XXX For now ignore aCacheKey. We will need it in the future
2634 // for correctly dealing with image load requests that are a result
2636 if (mCache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
2637 // We don't want to kick off another network load. So we ask
2638 // ValidateEntry to only do validation without creating a new proxy. If
2639 // it says that the entry isn't valid any more, we'll only use the entry
2640 // we're getting if the channel is loading from the cache anyways.
2642 // XXX -- should this be changed? it's pretty much verbatim from the old
2643 // code, but seems nonsensical.
2645 // Since aCanMakeNewChannel == false, we don't need to pass content policy
2646 // type/principal/etc
2648 nsCOMPtr
<nsILoadInfo
> loadInfo
= channel
->LoadInfo();
2649 // if there is a loadInfo, use the right contentType, otherwise
2650 // default to the internal image type
2651 nsContentPolicyType policyType
= loadInfo
->InternalContentPolicyType();
2653 if (ValidateEntry(entry
, uri
, nullptr, nullptr, nullptr, aObserver
,
2654 aLoadingDocument
, requestFlags
, policyType
, false,
2655 nullptr, nullptr, nullptr, corsMode
, false, 0)) {
2656 request
= entry
->GetRequest();
2658 nsCOMPtr
<nsICacheInfoChannel
> cacheChan(do_QueryInterface(channel
));
2662 cacheChan
->IsFromCache(&bUseCacheCopy
);
2664 bUseCacheCopy
= false;
2667 if (!bUseCacheCopy
) {
2670 request
= entry
->GetRequest();
2674 if (request
&& entry
) {
2675 // If this entry has no proxies, its request has no reference to
2677 if (entry
->HasNoProxies()) {
2678 LOG_FUNC_WITH_PARAM(
2680 "imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
2682 MOZ_ASSERT(!request
->HasCacheEntry(),
2683 "Proxyless entry's request has cache entry!");
2684 request
->SetCacheEntry(entry
);
2686 if (mCacheTracker
&& entry
->GetExpirationState()->IsTracked()) {
2687 mCacheTracker
->MarkUsed(entry
);
2694 nsCOMPtr
<nsILoadGroup
> loadGroup
;
2695 channel
->GetLoadGroup(getter_AddRefs(loadGroup
));
2698 if (aLoadingDocument
) {
2699 // The load group of the channel should always match that of the
2700 // document if given. If that isn't the case, then we need to add more
2701 // plumbing to ensure we block the document as well.
2702 nsCOMPtr
<nsILoadGroup
> docLoadGroup
=
2703 aLoadingDocument
->GetDocumentLoadGroup();
2704 MOZ_ASSERT(docLoadGroup
== loadGroup
);
2708 // Filter out any load flags not from nsIRequest
2709 requestFlags
&= nsIRequest::LOAD_REQUESTMASK
;
2711 nsresult rv
= NS_OK
;
2713 // we have this in our cache already.. cancel the current (document) load
2715 // this should fire an OnStopRequest
2716 channel
->Cancel(NS_ERROR_PARSED_DATA_CACHED
);
2718 *listener
= nullptr; // give them back a null nsIStreamListener
2720 rv
= CreateNewProxyForRequest(request
, uri
, loadGroup
, aLoadingDocument
,
2721 aObserver
, requestFlags
, _retval
);
2722 static_cast<imgRequestProxy
*>(*_retval
)->NotifyListener();
2724 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2725 nsCOMPtr
<nsIURI
> originalURI
;
2726 channel
->GetOriginalURI(getter_AddRefs(originalURI
));
2728 // XXX(seth): We should be able to just use |key| here, except that |key| is
2729 // constructed above with the *current URI* and not the *original URI*. I'm
2730 // pretty sure this is a bug, and it's preventing us from ever getting a
2731 // cache hit in LoadImageWithChannel when redirects are involved.
2732 ImageCacheKey
originalURIKey(originalURI
, corsMode
, attrs
,
2735 // Default to doing a principal check because we don't know who
2736 // started that load and whether their principal ended up being
2737 // inherited on the channel.
2738 NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
2739 originalURIKey
, getter_AddRefs(request
),
2740 getter_AddRefs(entry
));
2742 // No principal specified here, because we're not passed one.
2743 // In LoadImageWithChannel, the redirects that may have been
2744 // associated with this load would have gone through necko.
2745 // We only have the final URI in ImageLib and hence don't know
2746 // if the request went through insecure redirects. But if it did,
2747 // the necko cache should have handled that (since all necko cache hits
2748 // including the redirects will go through content policy). Hence, we
2749 // can set aHadInsecureRedirect to false here.
2750 rv
= request
->Init(originalURI
, uri
, /* aHadInsecureRedirect = */ false,
2751 channel
, channel
, entry
, aLoadingDocument
, nullptr,
2753 NS_ENSURE_SUCCESS(rv
, rv
);
2755 RefPtr
<ProxyListener
> pl
=
2756 new ProxyListener(static_cast<nsIStreamListener
*>(request
.get()));
2757 pl
.forget(listener
);
2759 // Try to add the new request into the cache.
2760 PutIntoCache(originalURIKey
, entry
);
2762 rv
= CreateNewProxyForRequest(request
, originalURI
, loadGroup
,
2763 aLoadingDocument
, aObserver
, requestFlags
,
2766 // Explicitly don't notify our proxy, because we're loading off the
2767 // network, and necko (or things called from necko, such as
2768 // imgCacheValidator) are going to call our notifications asynchronously,
2769 // and we can't make it further asynchronous because observers might rely
2770 // on imagelib completing its work between the channel's OnStartRequest and
2774 if (NS_FAILED(rv
)) {
2778 (*_retval
)->AddToLoadGroup();
2782 bool imgLoader::SupportImageWithMimeType(const nsACString
& aMimeType
,
2783 AcceptedMimeTypes aAccept
2784 /* = AcceptedMimeTypes::IMAGES */) {
2785 nsAutoCString
mimeType(aMimeType
);
2786 ToLowerCase(mimeType
);
2788 if (aAccept
== AcceptedMimeTypes::IMAGES_AND_DOCUMENTS
&&
2789 mimeType
.EqualsLiteral("image/svg+xml")) {
2793 DecoderType type
= DecoderFactory::GetDecoderType(mimeType
.get());
2794 return type
!= DecoderType::UNKNOWN
;
2798 imgLoader::GetMIMETypeFromContent(nsIRequest
* aRequest
,
2799 const uint8_t* aContents
, uint32_t aLength
,
2800 nsACString
& aContentType
) {
2801 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
2803 nsCOMPtr
<nsILoadInfo
> loadInfo
= channel
->LoadInfo();
2804 if (loadInfo
->GetSkipContentSniffing()) {
2805 return NS_ERROR_NOT_AVAILABLE
;
2810 GetMimeTypeFromContent((const char*)aContents
, aLength
, aContentType
);
2811 if (NS_SUCCEEDED(rv
) && channel
&& XRE_IsParentProcess()) {
2812 if (RefPtr
<mozilla::net::nsHttpChannel
> httpChannel
=
2813 do_QueryObject(channel
)) {
2814 // If the image type pattern matching algorithm given bytes does not
2815 // return undefined, then disable the further check and allow the
2817 httpChannel
->DisableIsOpaqueResponseAllowedAfterSniffCheck(
2818 mozilla::net::nsHttpChannel::SnifferType::Image
);
2826 nsresult
imgLoader::GetMimeTypeFromContent(const char* aContents
,
2828 nsACString
& aContentType
) {
2829 nsAutoCString detected
;
2833 (!strncmp(aContents
, "GIF87a", 6) || !strncmp(aContents
, "GIF89a", 6))) {
2834 aContentType
.AssignLiteral(IMAGE_GIF
);
2837 } else if (aLength
>= 8 && ((unsigned char)aContents
[0] == 0x89 &&
2838 (unsigned char)aContents
[1] == 0x50 &&
2839 (unsigned char)aContents
[2] == 0x4E &&
2840 (unsigned char)aContents
[3] == 0x47 &&
2841 (unsigned char)aContents
[4] == 0x0D &&
2842 (unsigned char)aContents
[5] == 0x0A &&
2843 (unsigned char)aContents
[6] == 0x1A &&
2844 (unsigned char)aContents
[7] == 0x0A)) {
2845 aContentType
.AssignLiteral(IMAGE_PNG
);
2847 /* maybe a JPEG (JFIF)? */
2848 /* JFIF files start with SOI APP0 but older files can start with SOI DQT
2849 * so we test for SOI followed by any marker, i.e. FF D8 FF
2850 * this will also work for SPIFF JPEG files if they appear in the future.
2852 * (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
2854 } else if (aLength
>= 3 && ((unsigned char)aContents
[0]) == 0xFF &&
2855 ((unsigned char)aContents
[1]) == 0xD8 &&
2856 ((unsigned char)aContents
[2]) == 0xFF) {
2857 aContentType
.AssignLiteral(IMAGE_JPEG
);
2859 /* or how about ART? */
2860 /* ART begins with JG (4A 47). Major version offset 2.
2861 * Minor version offset 3. Offset 4 must be nullptr.
2863 } else if (aLength
>= 5 && ((unsigned char)aContents
[0]) == 0x4a &&
2864 ((unsigned char)aContents
[1]) == 0x47 &&
2865 ((unsigned char)aContents
[4]) == 0x00) {
2866 aContentType
.AssignLiteral(IMAGE_ART
);
2868 } else if (aLength
>= 2 && !strncmp(aContents
, "BM", 2)) {
2869 aContentType
.AssignLiteral(IMAGE_BMP
);
2871 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
2872 // CURs begin with 2-byte 0 followed by 2-byte 2.
2873 } else if (aLength
>= 4 && (!memcmp(aContents
, "\000\000\001\000", 4) ||
2874 !memcmp(aContents
, "\000\000\002\000", 4))) {
2875 aContentType
.AssignLiteral(IMAGE_ICO
);
2877 // WebPs always begin with RIFF, a 32-bit length, and WEBP.
2878 } else if (aLength
>= 12 && !memcmp(aContents
, "RIFF", 4) &&
2879 !memcmp(aContents
+ 8, "WEBP", 4)) {
2880 aContentType
.AssignLiteral(IMAGE_WEBP
);
2882 } else if (MatchesMP4(reinterpret_cast<const uint8_t*>(aContents
), aLength
,
2884 detected
.Equals(IMAGE_AVIF
)) {
2885 aContentType
.AssignLiteral(IMAGE_AVIF
);
2886 } else if ((aLength
>= 2 && !memcmp(aContents
, "\xFF\x0A", 2)) ||
2888 !memcmp(aContents
, "\x00\x00\x00\x0CJXL \x0D\x0A\x87\x0A", 12))) {
2889 // Each version is for containerless and containerful files respectively.
2890 aContentType
.AssignLiteral(IMAGE_JXL
);
2892 /* none of the above? I give up */
2893 return NS_ERROR_NOT_AVAILABLE
;
2900 * proxy stream listener class used to handle multipart/x-mixed-replace
2903 #include "nsIRequest.h"
2904 #include "nsIStreamConverterService.h"
2906 NS_IMPL_ISUPPORTS(ProxyListener
, nsIStreamListener
,
2907 nsIThreadRetargetableStreamListener
, nsIRequestObserver
)
2909 ProxyListener::ProxyListener(nsIStreamListener
* dest
) : mDestListener(dest
) {}
2911 ProxyListener::~ProxyListener() = default;
2913 /** nsIRequestObserver methods **/
2916 ProxyListener::OnStartRequest(nsIRequest
* aRequest
) {
2917 if (!mDestListener
) {
2918 return NS_ERROR_FAILURE
;
2921 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
2923 // We need to set the initiator type for the image load
2924 nsCOMPtr
<nsITimedChannel
> timedChannel
= do_QueryInterface(channel
);
2927 timedChannel
->GetInitiatorType(type
);
2928 if (type
.IsEmpty()) {
2929 timedChannel
->SetInitiatorType(u
"img"_ns
);
2933 nsAutoCString contentType
;
2934 nsresult rv
= channel
->GetContentType(contentType
);
2936 if (!contentType
.IsEmpty()) {
2937 /* If multipart/x-mixed-replace content, we'll insert a MIME decoder
2938 in the pipeline to handle the content and pass it along to our
2941 if ("multipart/x-mixed-replace"_ns
.Equals(contentType
)) {
2942 nsCOMPtr
<nsIStreamConverterService
> convServ(
2943 do_GetService("@mozilla.org/streamConverters;1", &rv
));
2944 if (NS_SUCCEEDED(rv
)) {
2945 nsCOMPtr
<nsIStreamListener
> toListener(mDestListener
);
2946 nsCOMPtr
<nsIStreamListener
> fromListener
;
2948 rv
= convServ
->AsyncConvertData("multipart/x-mixed-replace", "*/*",
2949 toListener
, nullptr,
2950 getter_AddRefs(fromListener
));
2951 if (NS_SUCCEEDED(rv
)) {
2952 mDestListener
= fromListener
;
2959 return mDestListener
->OnStartRequest(aRequest
);
2963 ProxyListener::OnStopRequest(nsIRequest
* aRequest
, nsresult status
) {
2964 if (!mDestListener
) {
2965 return NS_ERROR_FAILURE
;
2968 return mDestListener
->OnStopRequest(aRequest
, status
);
2971 /** nsIStreamListener methods **/
2974 ProxyListener::OnDataAvailable(nsIRequest
* aRequest
, nsIInputStream
* inStr
,
2975 uint64_t sourceOffset
, uint32_t count
) {
2976 if (!mDestListener
) {
2977 return NS_ERROR_FAILURE
;
2980 return mDestListener
->OnDataAvailable(aRequest
, inStr
, sourceOffset
, count
);
2984 ProxyListener::OnDataFinished(nsresult aStatus
) {
2985 if (!mDestListener
) {
2986 return NS_ERROR_FAILURE
;
2988 nsCOMPtr
<nsIThreadRetargetableStreamListener
> retargetableListener
=
2989 do_QueryInterface(mDestListener
);
2990 if (retargetableListener
) {
2991 return retargetableListener
->OnDataFinished(aStatus
);
2997 /** nsThreadRetargetableStreamListener methods **/
2999 ProxyListener::CheckListenerChain() {
3000 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3001 nsresult rv
= NS_OK
;
3002 nsCOMPtr
<nsIThreadRetargetableStreamListener
> retargetableListener
=
3003 do_QueryInterface(mDestListener
, &rv
);
3004 if (retargetableListener
) {
3005 rv
= retargetableListener
->CheckListenerChain();
3008 gImgLog
, LogLevel::Debug
,
3009 ("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
3011 (NS_SUCCEEDED(rv
) ? "success" : "failure"), this,
3012 (nsIStreamListener
*)mDestListener
, static_cast<uint32_t>(rv
)));
3017 * http validate class. check a channel for a 304
3020 NS_IMPL_ISUPPORTS(imgCacheValidator
, nsIStreamListener
, nsIRequestObserver
,
3021 nsIThreadRetargetableStreamListener
, nsIChannelEventSink
,
3022 nsIInterfaceRequestor
, nsIAsyncVerifyRedirectCallback
)
3024 imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy
* progress
,
3025 imgLoader
* loader
, imgRequest
* request
,
3026 Document
* aDocument
,
3027 uint64_t aInnerWindowId
,
3028 bool forcePrincipalCheckForCacheEntry
)
3029 : mProgressProxy(progress
),
3031 mDocument(aDocument
),
3032 mInnerWindowId(aInnerWindowId
),
3034 mHadInsecureRedirect(false) {
3035 NewRequestAndEntry(forcePrincipalCheckForCacheEntry
, loader
,
3036 mRequest
->CacheKey(), getter_AddRefs(mNewRequest
),
3037 getter_AddRefs(mNewEntry
));
3040 imgCacheValidator::~imgCacheValidator() {
3042 // If something went wrong, and we never unblocked the requests waiting on
3043 // validation, now is our last chance. We will cancel the new request and
3044 // switch the waiting proxies to it.
3045 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
3049 void imgCacheValidator::AddProxy(imgRequestProxy
* aProxy
) {
3050 // aProxy needs to be in the loadgroup since we're validating from
3052 aProxy
->AddToLoadGroup();
3054 mProxies
.AppendElement(aProxy
);
3057 void imgCacheValidator::RemoveProxy(imgRequestProxy
* aProxy
) {
3058 mProxies
.RemoveElement(aProxy
);
3061 void imgCacheValidator::UpdateProxies(bool aCancelRequest
, bool aSyncNotify
) {
3062 MOZ_ASSERT(mRequest
);
3064 // Clear the validator before updating the proxies. The notifications may
3065 // clone an existing request, and its state could be inconsistent.
3066 mRequest
->SetValidator(nullptr);
3069 // If an error occurred, we will want to cancel the new request, and make the
3070 // validating proxies point to it. Any proxies still bound to the original
3071 // request which are not validating should remain untouched.
3072 if (aCancelRequest
) {
3073 MOZ_ASSERT(mNewRequest
);
3074 mNewRequest
->CancelAndAbort(NS_BINDING_ABORTED
);
3077 // We have finished validating the request, so we can safely take ownership
3078 // of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
3079 // if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
3080 // that any potential notifications should still be suppressed in
3081 // imgRequestProxy::ChangeOwner because we haven't cleared the validating
3082 // flag yet, and thus they will remain deferred.
3083 AutoTArray
<RefPtr
<imgRequestProxy
>, 4> proxies(std::move(mProxies
));
3085 for (auto& proxy
: proxies
) {
3086 // First update the state of all proxies before notifying any of them
3087 // to ensure a consistent state (e.g. in case the notification causes
3088 // other proxies to be touched indirectly.)
3089 MOZ_ASSERT(proxy
->IsValidating());
3090 MOZ_ASSERT(proxy
->NotificationsDeferred(),
3091 "Proxies waiting on cache validation should be "
3092 "deferring notifications!");
3094 proxy
->ChangeOwner(mNewRequest
);
3096 proxy
->ClearValidating();
3099 mNewRequest
= nullptr;
3100 mNewEntry
= nullptr;
3102 for (auto& proxy
: proxies
) {
3104 // Notify synchronously, because the caller knows we are already in an
3105 // asynchronously-called function (e.g. OnStartRequest).
3106 proxy
->SyncNotifyListener();
3108 // Notify asynchronously, because the caller does not know our current
3109 // call state (e.g. ~imgCacheValidator).
3110 proxy
->NotifyListener();
3115 /** nsIRequestObserver methods **/
3118 imgCacheValidator::OnStartRequest(nsIRequest
* aRequest
) {
3119 // We may be holding on to a document, so ensure that it's released.
3120 RefPtr
<Document
> document
= mDocument
.forget();
3122 // If for some reason we don't still have an existing request (probably
3123 // because OnStartRequest got delivered more than once), just bail.
3125 MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
3126 aRequest
->CancelWithReason(NS_BINDING_ABORTED
,
3127 "OnStartRequest delivered more than once?"_ns
);
3128 return NS_ERROR_FAILURE
;
3131 // If this request is coming from cache and has the same URI as our
3132 // imgRequest, the request all our proxies are pointing at is valid, and all
3133 // we have to do is tell them to notify their listeners.
3134 nsCOMPtr
<nsICacheInfoChannel
> cacheChan(do_QueryInterface(aRequest
));
3135 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
3136 if (cacheChan
&& channel
) {
3137 bool isFromCache
= false;
3138 cacheChan
->IsFromCache(&isFromCache
);
3140 nsCOMPtr
<nsIURI
> channelURI
;
3141 channel
->GetURI(getter_AddRefs(channelURI
));
3143 nsCOMPtr
<nsIURI
> finalURI
;
3144 mRequest
->GetFinalURI(getter_AddRefs(finalURI
));
3146 bool sameURI
= false;
3147 if (channelURI
&& finalURI
) {
3148 channelURI
->Equals(finalURI
, &sameURI
);
3151 if (isFromCache
&& sameURI
) {
3152 // We don't need to load this any more.
3153 aRequest
->CancelWithReason(NS_BINDING_ABORTED
,
3154 "imgCacheValidator::OnStartRequest"_ns
);
3155 mNewRequest
= nullptr;
3157 // Clear the validator before updating the proxies. The notifications may
3158 // clone an existing request, and its state could be inconsistent.
3159 mRequest
->SetLoadId(document
);
3160 mRequest
->SetInnerWindowID(mInnerWindowId
);
3161 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3166 // We can't load out of cache. We have to create a whole new request for the
3167 // data that's coming in off the channel.
3168 nsCOMPtr
<nsIURI
> uri
;
3169 mRequest
->GetURI(getter_AddRefs(uri
));
3171 LOG_MSG_WITH_PARAM(gImgLog
,
3172 "imgCacheValidator::OnStartRequest creating new request",
3175 CORSMode corsmode
= mRequest
->GetCORSMode();
3176 nsCOMPtr
<nsIReferrerInfo
> referrerInfo
= mRequest
->GetReferrerInfo();
3177 nsCOMPtr
<nsIPrincipal
> triggeringPrincipal
=
3178 mRequest
->GetTriggeringPrincipal();
3180 // Doom the old request's cache entry
3181 mRequest
->RemoveFromCache();
3183 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
3184 nsCOMPtr
<nsIURI
> originalURI
;
3185 channel
->GetOriginalURI(getter_AddRefs(originalURI
));
3186 nsresult rv
= mNewRequest
->Init(originalURI
, uri
, mHadInsecureRedirect
,
3187 aRequest
, channel
, mNewEntry
, document
,
3188 triggeringPrincipal
, corsmode
, referrerInfo
);
3189 if (NS_FAILED(rv
)) {
3190 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
3194 mDestListener
= new ProxyListener(mNewRequest
);
3196 // Try to add the new request into the cache. Note that the entry must be in
3197 // the cache before the proxies' ownership changes, because adding a proxy
3198 // changes the caching behaviour for imgRequests.
3199 mImgLoader
->PutIntoCache(mNewRequest
->CacheKey(), mNewEntry
);
3200 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3201 return mDestListener
->OnStartRequest(aRequest
);
3205 imgCacheValidator::OnStopRequest(nsIRequest
* aRequest
, nsresult status
) {
3206 // Be sure we've released the document that we may have been holding on to.
3207 mDocument
= nullptr;
3209 if (!mDestListener
) {
3213 return mDestListener
->OnStopRequest(aRequest
, status
);
3216 /** nsIStreamListener methods **/
3219 imgCacheValidator::OnDataAvailable(nsIRequest
* aRequest
, nsIInputStream
* inStr
,
3220 uint64_t sourceOffset
, uint32_t count
) {
3221 if (!mDestListener
) {
3222 // XXX see bug 113959
3224 inStr
->ReadSegments(NS_DiscardSegment
, nullptr, count
, &_retval
);
3228 return mDestListener
->OnDataAvailable(aRequest
, inStr
, sourceOffset
, count
);
3232 imgCacheValidator::OnDataFinished(nsresult aStatus
) {
3233 if (!mDestListener
) {
3234 return NS_ERROR_FAILURE
;
3236 nsCOMPtr
<nsIThreadRetargetableStreamListener
> retargetableListener
=
3237 do_QueryInterface(mDestListener
);
3238 if (retargetableListener
) {
3239 return retargetableListener
->OnDataFinished(aStatus
);
3245 /** nsIThreadRetargetableStreamListener methods **/
3248 imgCacheValidator::CheckListenerChain() {
3249 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3250 nsresult rv
= NS_OK
;
3251 nsCOMPtr
<nsIThreadRetargetableStreamListener
> retargetableListener
=
3252 do_QueryInterface(mDestListener
, &rv
);
3253 if (retargetableListener
) {
3254 rv
= retargetableListener
->CheckListenerChain();
3257 gImgLog
, LogLevel::Debug
,
3258 ("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32
"=%s",
3259 this, static_cast<uint32_t>(rv
),
3260 NS_SUCCEEDED(rv
) ? "succeeded" : "failed"));
3264 /** nsIInterfaceRequestor methods **/
3267 imgCacheValidator::GetInterface(const nsIID
& aIID
, void** aResult
) {
3268 if (aIID
.Equals(NS_GET_IID(nsIChannelEventSink
))) {
3269 return QueryInterface(aIID
, aResult
);
3272 return mProgressProxy
->GetInterface(aIID
, aResult
);
3275 // These functions are materially the same as the same functions in imgRequest.
3276 // We duplicate them because we're verifying whether cache loads are necessary,
3277 // not unconditionally loading.
3279 /** nsIChannelEventSink methods **/
3281 imgCacheValidator::AsyncOnChannelRedirect(
3282 nsIChannel
* oldChannel
, nsIChannel
* newChannel
, uint32_t flags
,
3283 nsIAsyncVerifyRedirectCallback
* callback
) {
3284 // Note all cache information we get from the old channel.
3285 mNewRequest
->SetCacheValidation(mNewEntry
, oldChannel
);
3287 // If the previous URI is a non-HTTPS URI, record that fact for later use by
3288 // security code, which needs to know whether there is an insecure load at any
3289 // point in the redirect chain.
3290 nsCOMPtr
<nsIURI
> oldURI
;
3291 bool schemeLocal
= false;
3292 if (NS_FAILED(oldChannel
->GetURI(getter_AddRefs(oldURI
))) ||
3293 NS_FAILED(NS_URIChainHasFlags(
3294 oldURI
, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE
, &schemeLocal
)) ||
3295 (!oldURI
->SchemeIs("https") && !oldURI
->SchemeIs("chrome") &&
3297 mHadInsecureRedirect
= true;
3300 // Prepare for callback
3301 mRedirectCallback
= callback
;
3302 mRedirectChannel
= newChannel
;
3304 return mProgressProxy
->AsyncOnChannelRedirect(oldChannel
, newChannel
, flags
,
3309 imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult
) {
3310 // If we've already been told to abort, just do so.
3311 if (NS_FAILED(aResult
)) {
3312 mRedirectCallback
->OnRedirectVerifyCallback(aResult
);
3313 mRedirectCallback
= nullptr;
3314 mRedirectChannel
= nullptr;
3318 // make sure we have a protocol that returns data rather than opens
3319 // an external application, e.g. mailto:
3320 nsCOMPtr
<nsIURI
> uri
;
3321 mRedirectChannel
->GetURI(getter_AddRefs(uri
));
3323 nsresult result
= NS_OK
;
3325 if (nsContentUtils::IsExternalProtocol(uri
)) {
3326 result
= NS_ERROR_ABORT
;
3329 mRedirectCallback
->OnRedirectVerifyCallback(result
);
3330 mRedirectCallback
= nullptr;
3331 mRedirectChannel
= nullptr;