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"
11 #include "imgLoader.h"
16 #include "DecoderFactory.h"
18 #include "ImageLogging.h"
19 #include "ReferrerInfo.h"
20 #include "imgRequestProxy.h"
21 #include "mozilla/Attributes.h"
22 #include "mozilla/BasePrincipal.h"
23 #include "mozilla/ChaosMode.h"
24 #include "mozilla/ClearOnShutdown.h"
25 #include "mozilla/LoadInfo.h"
26 #include "mozilla/NullPrincipal.h"
27 #include "mozilla/Preferences.h"
28 #include "mozilla/ProfilerLabels.h"
29 #include "mozilla/StaticPrefs_image.h"
30 #include "mozilla/StaticPrefs_network.h"
31 #include "mozilla/dom/ContentParent.h"
32 #include "mozilla/dom/nsMixedContentBlocker.h"
33 #include "mozilla/image/ImageMemoryReporter.h"
34 #include "mozilla/layers/CompositorManagerChild.h"
37 #include "nsComponentManagerUtils.h"
38 #include "nsContentPolicyUtils.h"
39 #include "nsContentUtils.h"
40 #include "nsIApplicationCache.h"
41 #include "nsIApplicationCacheContainer.h"
42 #include "nsIAsyncVerifyRedirectCallback.h"
43 #include "nsICacheInfoChannel.h"
44 #include "nsIChannelEventSink.h"
45 #include "nsIClassOfService.h"
47 #include "nsIFileURL.h"
48 #include "nsIHttpChannel.h"
49 #include "nsIInterfaceRequestor.h"
50 #include "nsIInterfaceRequestorUtils.h"
51 #include "nsIMemoryReporter.h"
52 #include "nsINetworkPredictor.h"
53 #include "nsIProgressEventSink.h"
54 #include "nsIProtocolHandler.h"
55 #include "nsImageModule.h"
56 #include "nsMediaSniffer.h"
57 #include "nsMimeTypes.h"
59 #include "nsNetUtil.h"
60 #include "nsProxyRelease.h"
61 #include "nsQueryObject.h"
62 #include "nsReadableUtils.h"
63 #include "nsStreamUtils.h"
66 // we want to explore making the document own the load group
67 // so we can associate the document URI with the load group.
68 // until this point, we have an evil hack:
69 #include "nsIHttpChannelInternal.h"
70 #include "nsILoadGroupChild.h"
71 #include "nsIDocShell.h"
73 using namespace mozilla
;
74 using namespace mozilla::dom
;
75 using namespace mozilla::image
;
76 using namespace mozilla::net
;
78 MOZ_DEFINE_MALLOC_SIZE_OF(ImagesMallocSizeOf
)
80 class imgMemoryReporter final
: public nsIMemoryReporter
{
81 ~imgMemoryReporter() = default;
86 NS_IMETHOD
CollectReports(nsIHandleReportCallback
* aHandleReport
,
87 nsISupports
* aData
, bool aAnonymize
) override
{
88 MOZ_ASSERT(NS_IsMainThread());
90 layers::CompositorManagerChild
* manager
=
91 mozilla::layers::CompositorManagerChild::GetInstance();
92 if (!manager
|| !StaticPrefs::image_mem_debug_reporting()) {
93 layers::SharedSurfacesMemoryReport sharedSurfaces
;
94 FinishCollectReports(aHandleReport
, aData
, aAnonymize
, sharedSurfaces
);
98 RefPtr
<imgMemoryReporter
> self(this);
99 nsCOMPtr
<nsIHandleReportCallback
> handleReport(aHandleReport
);
100 nsCOMPtr
<nsISupports
> data(aData
);
101 manager
->SendReportSharedSurfacesMemory(
102 [=](layers::SharedSurfacesMemoryReport aReport
) {
103 self
->FinishCollectReports(handleReport
, data
, aAnonymize
, aReport
);
105 [=](mozilla::ipc::ResponseRejectReason
&& aReason
) {
106 layers::SharedSurfacesMemoryReport sharedSurfaces
;
107 self
->FinishCollectReports(handleReport
, data
, aAnonymize
,
113 void FinishCollectReports(
114 nsIHandleReportCallback
* aHandleReport
, nsISupports
* aData
,
115 bool aAnonymize
, layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
116 nsTArray
<ImageMemoryCounter
> chrome
;
117 nsTArray
<ImageMemoryCounter
> content
;
118 nsTArray
<ImageMemoryCounter
> uncached
;
120 for (uint32_t i
= 0; i
< mKnownLoaders
.Length(); i
++) {
121 for (auto iter
= mKnownLoaders
[i
]->mChromeCache
.Iter(); !iter
.Done();
123 imgCacheEntry
* entry
= iter
.UserData();
124 RefPtr
<imgRequest
> req
= entry
->GetRequest();
125 RecordCounterForRequest(req
, &chrome
, !entry
->HasNoProxies());
127 for (auto iter
= mKnownLoaders
[i
]->mCache
.Iter(); !iter
.Done();
129 imgCacheEntry
* entry
= iter
.UserData();
130 RefPtr
<imgRequest
> req
= entry
->GetRequest();
131 RecordCounterForRequest(req
, &content
, !entry
->HasNoProxies());
133 MutexAutoLock
lock(mKnownLoaders
[i
]->mUncachedImagesMutex
);
134 for (auto iter
= mKnownLoaders
[i
]->mUncachedImages
.Iter(); !iter
.Done();
136 nsPtrHashKey
<imgRequest
>* entry
= iter
.Get();
137 RefPtr
<imgRequest
> req
= entry
->GetKey();
138 RecordCounterForRequest(req
, &uncached
, req
->HasConsumers());
142 // Note that we only need to anonymize content image URIs.
144 ReportCounterArray(aHandleReport
, aData
, chrome
, "images/chrome",
145 /* aAnonymize */ false, aSharedSurfaces
);
147 ReportCounterArray(aHandleReport
, aData
, content
, "images/content",
148 aAnonymize
, aSharedSurfaces
);
150 // Uncached images may be content or chrome, so anonymize them.
151 ReportCounterArray(aHandleReport
, aData
, uncached
, "images/uncached",
152 aAnonymize
, aSharedSurfaces
);
154 // Report any shared surfaces that were not merged with the surface cache.
155 ImageMemoryReporter::ReportSharedSurfaces(aHandleReport
, aData
,
158 nsCOMPtr
<nsIMemoryReporterManager
> imgr
=
159 do_GetService("@mozilla.org/memory-reporter-manager;1");
165 static int64_t ImagesContentUsedUncompressedDistinguishedAmount() {
167 for (uint32_t i
= 0; i
< imgLoader::sMemReporter
->mKnownLoaders
.Length();
169 for (auto iter
= imgLoader::sMemReporter
->mKnownLoaders
[i
]->mCache
.Iter();
170 !iter
.Done(); iter
.Next()) {
171 imgCacheEntry
* entry
= iter
.UserData();
172 if (entry
->HasNoProxies()) {
176 RefPtr
<imgRequest
> req
= entry
->GetRequest();
177 RefPtr
<image::Image
> image
= req
->GetImage();
182 // Both this and EntryImageSizes measure
183 // images/content/raster/used/decoded memory. This function's
184 // measurement is secondary -- the result doesn't go in the "explicit"
185 // tree -- so we use moz_malloc_size_of instead of ImagesMallocSizeOf to
186 // prevent DMD from seeing it reported twice.
187 SizeOfState
state(moz_malloc_size_of
);
188 ImageMemoryCounter
counter(req
, image
, state
, /* aIsUsed = */ true);
190 n
+= counter
.Values().DecodedHeap();
191 n
+= counter
.Values().DecodedNonHeap();
192 n
+= counter
.Values().DecodedUnknown();
198 void RegisterLoader(imgLoader
* aLoader
) {
199 mKnownLoaders
.AppendElement(aLoader
);
202 void UnregisterLoader(imgLoader
* aLoader
) {
203 mKnownLoaders
.RemoveElement(aLoader
);
207 nsTArray
<imgLoader
*> mKnownLoaders
;
210 MemoryTotal
& operator+=(const ImageMemoryCounter
& aImageCounter
) {
211 if (aImageCounter
.Type() == imgIContainer::TYPE_RASTER
) {
212 if (aImageCounter
.IsUsed()) {
213 mUsedRasterCounter
+= aImageCounter
.Values();
215 mUnusedRasterCounter
+= aImageCounter
.Values();
217 } else if (aImageCounter
.Type() == imgIContainer::TYPE_VECTOR
) {
218 if (aImageCounter
.IsUsed()) {
219 mUsedVectorCounter
+= aImageCounter
.Values();
221 mUnusedVectorCounter
+= aImageCounter
.Values();
223 } else if (aImageCounter
.Type() == imgIContainer::TYPE_REQUEST
) {
224 // Nothing to do, we did not get to the point of having an image.
226 MOZ_CRASH("Unexpected image type");
232 const MemoryCounter
& UsedRaster() const { return mUsedRasterCounter
; }
233 const MemoryCounter
& UnusedRaster() const { return mUnusedRasterCounter
; }
234 const MemoryCounter
& UsedVector() const { return mUsedVectorCounter
; }
235 const MemoryCounter
& UnusedVector() const { return mUnusedVectorCounter
; }
238 MemoryCounter mUsedRasterCounter
;
239 MemoryCounter mUnusedRasterCounter
;
240 MemoryCounter mUsedVectorCounter
;
241 MemoryCounter mUnusedVectorCounter
;
244 // Reports all images of a single kind, e.g. all used chrome images.
245 void ReportCounterArray(nsIHandleReportCallback
* aHandleReport
,
247 nsTArray
<ImageMemoryCounter
>& aCounterArray
,
248 const char* aPathPrefix
, bool aAnonymize
,
249 layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
250 MemoryTotal summaryTotal
;
251 MemoryTotal nonNotableTotal
;
253 // Report notable images, and compute total and non-notable aggregate sizes.
254 for (uint32_t i
= 0; i
< aCounterArray
.Length(); i
++) {
255 ImageMemoryCounter
& counter
= aCounterArray
[i
];
258 counter
.URI().Truncate();
259 counter
.URI().AppendPrintf("<anonymized-%u>", i
);
261 // The URI could be an extremely long data: URI. Truncate if needed.
262 static const size_t max
= 256;
263 if (counter
.URI().Length() > max
) {
264 counter
.URI().Truncate(max
);
265 counter
.URI().AppendLiteral(" (truncated)");
267 counter
.URI().ReplaceChar('/', '\\');
270 summaryTotal
+= counter
;
272 if (counter
.IsNotable() || StaticPrefs::image_mem_debug_reporting()) {
273 ReportImage(aHandleReport
, aData
, aPathPrefix
, counter
,
276 ImageMemoryReporter::TrimSharedSurfaces(counter
, aSharedSurfaces
);
277 nonNotableTotal
+= counter
;
281 // Report non-notable images in aggregate.
282 ReportTotal(aHandleReport
, aData
, /* aExplicit = */ true, aPathPrefix
,
283 "<non-notable images>/", nonNotableTotal
);
285 // Report a summary in aggregate, outside of the explicit tree.
286 ReportTotal(aHandleReport
, aData
, /* aExplicit = */ false, aPathPrefix
, "",
290 static void ReportImage(nsIHandleReportCallback
* aHandleReport
,
291 nsISupports
* aData
, const char* aPathPrefix
,
292 const ImageMemoryCounter
& aCounter
,
293 layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
294 nsAutoCString
pathPrefix("explicit/"_ns
);
295 pathPrefix
.Append(aPathPrefix
);
297 switch (aCounter
.Type()) {
298 case imgIContainer::TYPE_RASTER
:
299 pathPrefix
.AppendLiteral("/raster/");
301 case imgIContainer::TYPE_VECTOR
:
302 pathPrefix
.AppendLiteral("/vector/");
304 case imgIContainer::TYPE_REQUEST
:
305 pathPrefix
.AppendLiteral("/request/");
308 pathPrefix
.AppendLiteral("/unknown=");
309 pathPrefix
.AppendInt(aCounter
.Type());
310 pathPrefix
.AppendLiteral("/");
314 pathPrefix
.Append(aCounter
.IsUsed() ? "used/" : "unused/");
315 if (aCounter
.IsValidating()) {
316 pathPrefix
.AppendLiteral("validating/");
318 if (aCounter
.HasError()) {
319 pathPrefix
.AppendLiteral("err/");
322 pathPrefix
.AppendLiteral("progress=");
323 pathPrefix
.AppendInt(aCounter
.Progress(), 16);
324 pathPrefix
.AppendLiteral("/");
326 pathPrefix
.AppendLiteral("image(");
327 pathPrefix
.AppendInt(aCounter
.IntrinsicSize().width
);
328 pathPrefix
.AppendLiteral("x");
329 pathPrefix
.AppendInt(aCounter
.IntrinsicSize().height
);
330 pathPrefix
.AppendLiteral(", ");
332 if (aCounter
.URI().IsEmpty()) {
333 pathPrefix
.AppendLiteral("<unknown URI>");
335 pathPrefix
.Append(aCounter
.URI());
338 pathPrefix
.AppendLiteral(")/");
340 ReportSurfaces(aHandleReport
, aData
, pathPrefix
, aCounter
, aSharedSurfaces
);
342 ReportSourceValue(aHandleReport
, aData
, pathPrefix
, aCounter
.Values());
345 static void ReportSurfaces(
346 nsIHandleReportCallback
* aHandleReport
, nsISupports
* aData
,
347 const nsACString
& aPathPrefix
, const ImageMemoryCounter
& aCounter
,
348 layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
349 for (const SurfaceMemoryCounter
& counter
: aCounter
.Surfaces()) {
350 nsAutoCString
surfacePathPrefix(aPathPrefix
);
351 if (counter
.IsLocked()) {
352 surfacePathPrefix
.AppendLiteral("locked/");
354 surfacePathPrefix
.AppendLiteral("unlocked/");
356 if (counter
.IsFactor2()) {
357 surfacePathPrefix
.AppendLiteral("factor2/");
359 if (counter
.CannotSubstitute()) {
360 surfacePathPrefix
.AppendLiteral("cannot_substitute/");
362 surfacePathPrefix
.AppendLiteral("types=");
363 surfacePathPrefix
.AppendInt(counter
.Values().SurfaceTypes(), 16);
364 surfacePathPrefix
.AppendLiteral("/surface(");
365 surfacePathPrefix
.AppendInt(counter
.Key().Size().width
);
366 surfacePathPrefix
.AppendLiteral("x");
367 surfacePathPrefix
.AppendInt(counter
.Key().Size().height
);
369 if (!counter
.IsFinished()) {
370 surfacePathPrefix
.AppendLiteral(", incomplete");
373 if (counter
.Values().ExternalHandles() > 0) {
374 surfacePathPrefix
.AppendLiteral(", handles:");
375 surfacePathPrefix
.AppendInt(
376 uint32_t(counter
.Values().ExternalHandles()));
379 ImageMemoryReporter::AppendSharedSurfacePrefix(surfacePathPrefix
, counter
,
382 if (counter
.Type() == SurfaceMemoryCounterType::NORMAL
) {
383 PlaybackType playback
= counter
.Key().Playback();
384 if (playback
== PlaybackType::eAnimated
) {
385 if (StaticPrefs::image_mem_debug_reporting()) {
386 surfacePathPrefix
.AppendPrintf(
387 " (animation %4u)", uint32_t(counter
.Values().FrameIndex()));
389 surfacePathPrefix
.AppendLiteral(" (animation)");
393 if (counter
.Key().Flags() != DefaultSurfaceFlags()) {
394 surfacePathPrefix
.AppendLiteral(", flags:");
395 surfacePathPrefix
.AppendInt(uint32_t(counter
.Key().Flags()),
399 if (counter
.Key().SVGContext()) {
400 const SVGImageContext
& context
= counter
.Key().SVGContext().ref();
401 surfacePathPrefix
.AppendLiteral(", svgContext:[ ");
402 if (context
.GetViewportSize()) {
403 const CSSIntSize
& size
= context
.GetViewportSize().ref();
404 surfacePathPrefix
.AppendLiteral("viewport=(");
405 surfacePathPrefix
.AppendInt(size
.width
);
406 surfacePathPrefix
.AppendLiteral("x");
407 surfacePathPrefix
.AppendInt(size
.height
);
408 surfacePathPrefix
.AppendLiteral(") ");
410 if (context
.GetPreserveAspectRatio()) {
412 context
.GetPreserveAspectRatio()->ToString(aspect
);
413 surfacePathPrefix
.AppendLiteral("preserveAspectRatio=(");
414 LossyAppendUTF16toASCII(aspect
, surfacePathPrefix
);
415 surfacePathPrefix
.AppendLiteral(") ");
417 if (context
.GetContextPaint()) {
418 const SVGEmbeddingContextPaint
* paint
= context
.GetContextPaint();
419 surfacePathPrefix
.AppendLiteral("contextPaint=(");
420 if (paint
->GetFill()) {
421 surfacePathPrefix
.AppendLiteral(" fill=");
422 surfacePathPrefix
.AppendInt(paint
->GetFill()->ToABGR(), 16);
424 if (paint
->GetFillOpacity()) {
425 surfacePathPrefix
.AppendLiteral(" fillOpa=");
426 surfacePathPrefix
.AppendFloat(paint
->GetFillOpacity());
428 if (paint
->GetStroke()) {
429 surfacePathPrefix
.AppendLiteral(" stroke=");
430 surfacePathPrefix
.AppendInt(paint
->GetStroke()->ToABGR(), 16);
432 if (paint
->GetStrokeOpacity()) {
433 surfacePathPrefix
.AppendLiteral(" strokeOpa=");
434 surfacePathPrefix
.AppendFloat(paint
->GetStrokeOpacity());
436 surfacePathPrefix
.AppendLiteral(" ) ");
438 surfacePathPrefix
.AppendLiteral("]");
440 } else if (counter
.Type() == SurfaceMemoryCounterType::COMPOSITING
) {
441 surfacePathPrefix
.AppendLiteral(", compositing frame");
442 } else if (counter
.Type() == SurfaceMemoryCounterType::COMPOSITING_PREV
) {
443 surfacePathPrefix
.AppendLiteral(", compositing prev frame");
445 MOZ_ASSERT_UNREACHABLE("Unknown counter type");
448 surfacePathPrefix
.AppendLiteral(")/");
450 ReportValues(aHandleReport
, aData
, surfacePathPrefix
, counter
.Values());
454 static void ReportTotal(nsIHandleReportCallback
* aHandleReport
,
455 nsISupports
* aData
, bool aExplicit
,
456 const char* aPathPrefix
, const char* aPathInfix
,
457 const MemoryTotal
& aTotal
) {
458 nsAutoCString pathPrefix
;
460 pathPrefix
.AppendLiteral("explicit/");
462 pathPrefix
.Append(aPathPrefix
);
464 nsAutoCString
rasterUsedPrefix(pathPrefix
);
465 rasterUsedPrefix
.AppendLiteral("/raster/used/");
466 rasterUsedPrefix
.Append(aPathInfix
);
467 ReportValues(aHandleReport
, aData
, rasterUsedPrefix
, aTotal
.UsedRaster());
469 nsAutoCString
rasterUnusedPrefix(pathPrefix
);
470 rasterUnusedPrefix
.AppendLiteral("/raster/unused/");
471 rasterUnusedPrefix
.Append(aPathInfix
);
472 ReportValues(aHandleReport
, aData
, rasterUnusedPrefix
,
473 aTotal
.UnusedRaster());
475 nsAutoCString
vectorUsedPrefix(pathPrefix
);
476 vectorUsedPrefix
.AppendLiteral("/vector/used/");
477 vectorUsedPrefix
.Append(aPathInfix
);
478 ReportValues(aHandleReport
, aData
, vectorUsedPrefix
, aTotal
.UsedVector());
480 nsAutoCString
vectorUnusedPrefix(pathPrefix
);
481 vectorUnusedPrefix
.AppendLiteral("/vector/unused/");
482 vectorUnusedPrefix
.Append(aPathInfix
);
483 ReportValues(aHandleReport
, aData
, vectorUnusedPrefix
,
484 aTotal
.UnusedVector());
487 static void ReportValues(nsIHandleReportCallback
* aHandleReport
,
488 nsISupports
* aData
, const nsACString
& aPathPrefix
,
489 const MemoryCounter
& aCounter
) {
490 ReportSourceValue(aHandleReport
, aData
, aPathPrefix
, aCounter
);
492 ReportValue(aHandleReport
, aData
, KIND_HEAP
, aPathPrefix
, "decoded-heap",
493 "Decoded image data which is stored on the heap.",
494 aCounter
.DecodedHeap());
496 ReportValue(aHandleReport
, aData
, KIND_NONHEAP
, aPathPrefix
,
498 "Decoded image data which isn't stored on the heap.",
499 aCounter
.DecodedNonHeap());
501 // We don't know for certain whether or not it is on the heap, so let's
502 // just report it as non-heap for reporting purposes.
503 ReportValue(aHandleReport
, aData
, KIND_NONHEAP
, aPathPrefix
,
505 "Decoded image data which is unknown to be on the heap or not.",
506 aCounter
.DecodedUnknown());
509 static void ReportSourceValue(nsIHandleReportCallback
* aHandleReport
,
511 const nsACString
& aPathPrefix
,
512 const MemoryCounter
& aCounter
) {
513 ReportValue(aHandleReport
, aData
, KIND_HEAP
, aPathPrefix
, "source",
514 "Raster image source data and vector image documents.",
518 static void ReportValue(nsIHandleReportCallback
* aHandleReport
,
519 nsISupports
* aData
, int32_t aKind
,
520 const nsACString
& aPathPrefix
,
521 const char* aPathSuffix
, const char* aDescription
,
527 nsAutoCString
desc(aDescription
);
528 nsAutoCString
path(aPathPrefix
);
529 path
.Append(aPathSuffix
);
531 aHandleReport
->Callback(""_ns
, path
, aKind
, UNITS_BYTES
, aValue
, desc
,
535 static void RecordCounterForRequest(imgRequest
* aRequest
,
536 nsTArray
<ImageMemoryCounter
>* aArray
,
538 SizeOfState
state(ImagesMallocSizeOf
);
539 RefPtr
<image::Image
> image
= aRequest
->GetImage();
541 ImageMemoryCounter
counter(aRequest
, image
, state
, aIsUsed
);
542 aArray
->AppendElement(std::move(counter
));
544 // We can at least record some information about the image from the
545 // request, and mark it as not knowing the image type yet.
546 ImageMemoryCounter
counter(aRequest
, state
, aIsUsed
);
547 aArray
->AppendElement(std::move(counter
));
552 NS_IMPL_ISUPPORTS(imgMemoryReporter
, nsIMemoryReporter
)
554 NS_IMPL_ISUPPORTS(nsProgressNotificationProxy
, nsIProgressEventSink
,
555 nsIChannelEventSink
, nsIInterfaceRequestor
)
558 nsProgressNotificationProxy::OnProgress(nsIRequest
* request
, int64_t progress
,
559 int64_t progressMax
) {
560 nsCOMPtr
<nsILoadGroup
> loadGroup
;
561 request
->GetLoadGroup(getter_AddRefs(loadGroup
));
563 nsCOMPtr
<nsIProgressEventSink
> target
;
564 NS_QueryNotificationCallbacks(mOriginalCallbacks
, loadGroup
,
565 NS_GET_IID(nsIProgressEventSink
),
566 getter_AddRefs(target
));
570 return target
->OnProgress(mImageRequest
, progress
, progressMax
);
574 nsProgressNotificationProxy::OnStatus(nsIRequest
* request
, nsresult status
,
575 const char16_t
* statusArg
) {
576 nsCOMPtr
<nsILoadGroup
> loadGroup
;
577 request
->GetLoadGroup(getter_AddRefs(loadGroup
));
579 nsCOMPtr
<nsIProgressEventSink
> target
;
580 NS_QueryNotificationCallbacks(mOriginalCallbacks
, loadGroup
,
581 NS_GET_IID(nsIProgressEventSink
),
582 getter_AddRefs(target
));
586 return target
->OnStatus(mImageRequest
, status
, statusArg
);
590 nsProgressNotificationProxy::AsyncOnChannelRedirect(
591 nsIChannel
* oldChannel
, nsIChannel
* newChannel
, uint32_t flags
,
592 nsIAsyncVerifyRedirectCallback
* cb
) {
593 // Tell the original original callbacks about it too
594 nsCOMPtr
<nsILoadGroup
> loadGroup
;
595 newChannel
->GetLoadGroup(getter_AddRefs(loadGroup
));
596 nsCOMPtr
<nsIChannelEventSink
> target
;
597 NS_QueryNotificationCallbacks(mOriginalCallbacks
, loadGroup
,
598 NS_GET_IID(nsIChannelEventSink
),
599 getter_AddRefs(target
));
601 cb
->OnRedirectVerifyCallback(NS_OK
);
605 // Delegate to |target| if set, reusing |cb|
606 return target
->AsyncOnChannelRedirect(oldChannel
, newChannel
, flags
, cb
);
610 nsProgressNotificationProxy::GetInterface(const nsIID
& iid
, void** result
) {
611 if (iid
.Equals(NS_GET_IID(nsIProgressEventSink
))) {
612 *result
= static_cast<nsIProgressEventSink
*>(this);
616 if (iid
.Equals(NS_GET_IID(nsIChannelEventSink
))) {
617 *result
= static_cast<nsIChannelEventSink
*>(this);
621 if (mOriginalCallbacks
) {
622 return mOriginalCallbacks
->GetInterface(iid
, result
);
624 return NS_NOINTERFACE
;
627 static void NewRequestAndEntry(bool aForcePrincipalCheckForCacheEntry
,
628 imgLoader
* aLoader
, const ImageCacheKey
& aKey
,
629 imgRequest
** aRequest
, imgCacheEntry
** aEntry
) {
630 RefPtr
<imgRequest
> request
= new imgRequest(aLoader
, aKey
);
631 RefPtr
<imgCacheEntry
> entry
=
632 new imgCacheEntry(aLoader
, request
, aForcePrincipalCheckForCacheEntry
);
633 aLoader
->AddToUncachedImages(request
);
634 request
.forget(aRequest
);
635 entry
.forget(aEntry
);
638 static bool ShouldRevalidateEntry(imgCacheEntry
* aEntry
, nsLoadFlags aFlags
,
640 bool bValidateEntry
= false;
642 if (aFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
646 if (aFlags
& nsIRequest::VALIDATE_ALWAYS
) {
647 bValidateEntry
= true;
648 } else if (aEntry
->GetMustValidate()) {
649 bValidateEntry
= true;
650 } else if (aHasExpired
) {
651 // The cache entry has expired... Determine whether the stale cache
652 // entry can be used without validation...
654 (nsIRequest::VALIDATE_NEVER
| nsIRequest::VALIDATE_ONCE_PER_SESSION
)) {
655 // VALIDATE_NEVER and VALIDATE_ONCE_PER_SESSION allow stale cache
656 // entries to be used unless they have been explicitly marked to
657 // indicate that revalidation is necessary.
658 bValidateEntry
= false;
660 } else if (!(aFlags
& nsIRequest::LOAD_FROM_CACHE
)) {
661 // LOAD_FROM_CACHE allows a stale cache entry to be used... Otherwise,
662 // the entry must be revalidated.
663 bValidateEntry
= true;
667 return bValidateEntry
;
670 /* Call content policies on cached images that went through a redirect */
671 static bool ShouldLoadCachedImage(imgRequest
* aImgRequest
,
672 Document
* aLoadingDocument
,
673 nsIPrincipal
* aTriggeringPrincipal
,
674 nsContentPolicyType aPolicyType
,
675 bool aSendCSPViolationReports
) {
676 /* Call content policies on cached images - Bug 1082837
677 * Cached images are keyed off of the first uri in a redirect chain.
678 * Hence content policies don't get a chance to test the intermediate hops
679 * or the final destination. Here we test the final destination using
680 * mFinalURI off of the imgRequest and passing it into content policies.
681 * For Mixed Content Blocker, we do an additional check to determine if any
682 * of the intermediary hops went through an insecure redirect with the
683 * mHadInsecureRedirect flag
685 bool insecureRedirect
= aImgRequest
->HadInsecureRedirect();
686 nsCOMPtr
<nsIURI
> contentLocation
;
687 aImgRequest
->GetFinalURI(getter_AddRefs(contentLocation
));
690 nsCOMPtr
<nsIPrincipal
> loadingPrincipal
=
691 aLoadingDocument
? aLoadingDocument
->NodePrincipal()
692 : aTriggeringPrincipal
;
693 // If there is no context and also no triggeringPrincipal, then we use a fresh
694 // nullPrincipal as the loadingPrincipal because we can not create a loadinfo
695 // without a valid loadingPrincipal.
696 if (!loadingPrincipal
) {
697 loadingPrincipal
= NullPrincipal::CreateWithoutOriginAttributes();
700 nsCOMPtr
<nsILoadInfo
> secCheckLoadInfo
= new LoadInfo(
701 loadingPrincipal
, aTriggeringPrincipal
, aLoadingDocument
,
702 nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK
, aPolicyType
);
704 secCheckLoadInfo
->SetSendCSPViolationEvents(aSendCSPViolationReports
);
706 int16_t decision
= nsIContentPolicy::REJECT_REQUEST
;
707 rv
= NS_CheckContentLoadPolicy(contentLocation
, secCheckLoadInfo
,
709 &decision
, nsContentUtils::GetContentPolicy());
710 if (NS_FAILED(rv
) || !NS_CP_ACCEPTED(decision
)) {
714 // We call all Content Policies above, but we also have to call mcb
715 // individually to check the intermediary redirect hops are secure.
716 if (insecureRedirect
) {
717 // Bug 1314356: If the image ended up in the cache upgraded by HSTS and the
718 // page uses upgrade-inscure-requests it had an insecure redirect
719 // (http->https). We need to invalidate the image and reload it because
720 // mixed content blocker only bails if upgrade-insecure-requests is set on
721 // the doc and the resource load is http: which would result in an incorrect
722 // mixed content warning.
723 nsCOMPtr
<nsIDocShell
> docShell
=
724 NS_CP_GetDocShellFromContext(ToSupports(aLoadingDocument
));
726 Document
* document
= docShell
->GetDocument();
727 if (document
&& document
->GetUpgradeInsecureRequests(false)) {
732 if (!aTriggeringPrincipal
|| !aTriggeringPrincipal
->IsSystemPrincipal()) {
733 // reset the decision for mixed content blocker check
734 decision
= nsIContentPolicy::REJECT_REQUEST
;
735 rv
= nsMixedContentBlocker::ShouldLoad(insecureRedirect
, contentLocation
,
738 true, // aReportError
740 if (NS_FAILED(rv
) || !NS_CP_ACCEPTED(decision
)) {
749 // Returns true if this request is compatible with the given CORS mode on the
750 // given loading principal, and false if the request may not be reused due
751 // to CORS. Also checks the Referrer Policy, since requests with different
752 // referrers/policies may generate different responses.
753 static bool ValidateSecurityInfo(imgRequest
* request
, bool forcePrincipalCheck
,
755 nsIPrincipal
* triggeringPrincipal
,
756 Document
* aLoadingDocument
,
757 nsContentPolicyType aPolicyType
) {
758 // If the entry's CORS mode doesn't match, or the CORS mode matches but the
759 // document principal isn't the same, we can't use this request.
760 if (request
->GetCORSMode() != corsmode
) {
763 if (request
->GetCORSMode() != imgIRequest::CORS_NONE
|| forcePrincipalCheck
) {
764 nsCOMPtr
<nsIPrincipal
> otherprincipal
= request
->GetTriggeringPrincipal();
766 // If we previously had a principal, but we don't now, we can't use this
768 if (otherprincipal
&& !triggeringPrincipal
) {
772 if (otherprincipal
&& triggeringPrincipal
) {
774 otherprincipal
->Equals(triggeringPrincipal
, &equals
);
781 // Content Policy Check on Cached Images
782 return ShouldLoadCachedImage(request
, aLoadingDocument
, triggeringPrincipal
,
784 /* aSendCSPViolationReports */ false);
787 static nsresult
NewImageChannel(
788 nsIChannel
** aResult
,
789 // If aForcePrincipalCheckForCacheEntry is true, then we will
790 // force a principal check even when not using CORS before
791 // assuming we have a cache hit on a cache entry that we
792 // create for this channel. This is an out param that should
793 // be set to true if this channel ends up depending on
794 // aTriggeringPrincipal and false otherwise.
795 bool* aForcePrincipalCheckForCacheEntry
, nsIURI
* aURI
,
796 nsIURI
* aInitialDocumentURI
, int32_t aCORSMode
,
797 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
798 nsLoadFlags aLoadFlags
, nsContentPolicyType aPolicyType
,
799 nsIPrincipal
* aTriggeringPrincipal
, nsINode
* aRequestingNode
,
800 bool aRespectPrivacy
) {
804 nsCOMPtr
<nsIHttpChannel
> newHttpChannel
;
806 nsCOMPtr
<nsIInterfaceRequestor
> callbacks
;
809 // Get the notification callbacks from the load group for the new channel.
811 // XXX: This is not exactly correct, because the network request could be
812 // referenced by multiple windows... However, the new channel needs
813 // something. So, using the 'first' notification callbacks is better
816 aLoadGroup
->GetNotificationCallbacks(getter_AddRefs(callbacks
));
819 // Pass in a nullptr loadgroup because this is the underlying network
820 // request. This request may be referenced by several proxy image requests
821 // (possibly in different documents).
822 // If all of the proxy requests are canceled then this request should be
826 nsSecurityFlags securityFlags
=
827 aCORSMode
== imgIRequest::CORS_NONE
828 ? nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT
829 : nsILoadInfo::SEC_REQUIRE_CORS_INHERITS_SEC_CONTEXT
;
830 if (aCORSMode
== imgIRequest::CORS_ANONYMOUS
) {
831 securityFlags
|= nsILoadInfo::SEC_COOKIES_SAME_ORIGIN
;
832 } else if (aCORSMode
== imgIRequest::CORS_USE_CREDENTIALS
) {
833 securityFlags
|= nsILoadInfo::SEC_COOKIES_INCLUDE
;
835 securityFlags
|= nsILoadInfo::SEC_ALLOW_CHROME
;
837 // Note we are calling NS_NewChannelWithTriggeringPrincipal() here with a
838 // node and a principal. This is for things like background images that are
839 // specified by user stylesheets, where the document is being styled, but
840 // the principal is that of the user stylesheet.
841 if (aRequestingNode
&& aTriggeringPrincipal
) {
842 rv
= NS_NewChannelWithTriggeringPrincipal(aResult
, aURI
, aRequestingNode
,
843 aTriggeringPrincipal
,
844 securityFlags
, aPolicyType
,
845 nullptr, // PerformanceStorage
846 nullptr, // loadGroup
847 callbacks
, aLoadFlags
);
853 if (aPolicyType
== nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON
) {
854 // If this is a favicon loading, we will use the originAttributes from the
855 // triggeringPrincipal as the channel's originAttributes. This allows the
856 // favicon loading from XUL will use the correct originAttributes.
858 nsCOMPtr
<nsILoadInfo
> loadInfo
= (*aResult
)->LoadInfo();
859 rv
= loadInfo
->SetOriginAttributes(
860 aTriggeringPrincipal
->OriginAttributesRef());
863 // either we are loading something inside a document, in which case
864 // we should always have a requestingNode, or we are loading something
865 // outside a document, in which case the triggeringPrincipal and
866 // triggeringPrincipal should always be the systemPrincipal.
867 // However, there are exceptions: one is Notifications which create a
868 // channel in the parent process in which case we can't get a
870 rv
= NS_NewChannel(aResult
, aURI
, nsContentUtils::GetSystemPrincipal(),
871 securityFlags
, aPolicyType
,
872 nullptr, // nsICookieJarSettings
873 nullptr, // PerformanceStorage
874 nullptr, // loadGroup
875 callbacks
, aLoadFlags
);
881 // Use the OriginAttributes from the loading principal, if one is available,
882 // and adjust the private browsing ID based on what kind of load the caller
883 // has asked us to perform.
884 OriginAttributes attrs
;
885 if (aTriggeringPrincipal
) {
886 attrs
= aTriggeringPrincipal
->OriginAttributesRef();
888 attrs
.mPrivateBrowsingId
= aRespectPrivacy
? 1 : 0;
890 nsCOMPtr
<nsILoadInfo
> loadInfo
= (*aResult
)->LoadInfo();
891 rv
= loadInfo
->SetOriginAttributes(attrs
);
898 // only inherit if we have a principal
899 *aForcePrincipalCheckForCacheEntry
=
900 aTriggeringPrincipal
&& nsContentUtils::ChannelShouldInheritPrincipal(
901 aTriggeringPrincipal
, aURI
,
902 /* aInheritForAboutBlank */ false,
903 /* aForceInherit */ false);
905 // Initialize HTTP-specific attributes
906 newHttpChannel
= do_QueryInterface(*aResult
);
907 if (newHttpChannel
) {
908 nsCOMPtr
<nsIHttpChannelInternal
> httpChannelInternal
=
909 do_QueryInterface(newHttpChannel
);
910 NS_ENSURE_TRUE(httpChannelInternal
, NS_ERROR_UNEXPECTED
);
911 rv
= httpChannelInternal
->SetDocumentURI(aInitialDocumentURI
);
912 MOZ_ASSERT(NS_SUCCEEDED(rv
));
914 DebugOnly
<nsresult
> rv
= newHttpChannel
->SetReferrerInfo(aReferrerInfo
);
915 MOZ_ASSERT(NS_SUCCEEDED(rv
));
919 // Image channels are loaded by default with reduced priority.
920 nsCOMPtr
<nsISupportsPriority
> p
= do_QueryInterface(*aResult
);
922 uint32_t priority
= nsISupportsPriority::PRIORITY_LOW
;
924 if (aLoadFlags
& nsIRequest::LOAD_BACKGROUND
) {
925 ++priority
; // further reduce priority for background loads
928 p
->AdjustPriority(priority
);
931 // Create a new loadgroup for this new channel, using the old group as
932 // the parent. The indirection keeps the channel insulated from cancels,
933 // but does allow a way for this revalidation to be associated with at
934 // least one base load group for scheduling/caching purposes.
936 nsCOMPtr
<nsILoadGroup
> loadGroup
= do_CreateInstance(NS_LOADGROUP_CONTRACTID
);
937 nsCOMPtr
<nsILoadGroupChild
> childLoadGroup
= do_QueryInterface(loadGroup
);
938 if (childLoadGroup
) {
939 childLoadGroup
->SetParentLoadGroup(aLoadGroup
);
941 (*aResult
)->SetLoadGroup(loadGroup
);
946 static uint32_t SecondsFromPRTime(PRTime aTime
) {
947 return nsContentUtils::SecondsFromPRTime(aTime
);
951 imgCacheEntry::imgCacheEntry(imgLoader
* loader
, imgRequest
* request
,
952 bool forcePrincipalCheck
)
956 mTouchedTime(SecondsFromPRTime(PR_Now())),
957 mLoadTime(SecondsFromPRTime(PR_Now())),
959 mMustValidate(false),
960 // We start off as evicted so we don't try to update the cache.
961 // PutIntoCache will set this to false.
964 mForcePrincipalCheck(forcePrincipalCheck
) {}
966 imgCacheEntry::~imgCacheEntry() {
967 LOG_FUNC(gImgLog
, "imgCacheEntry::~imgCacheEntry()");
970 void imgCacheEntry::Touch(bool updateTime
/* = true */) {
971 LOG_SCOPE(gImgLog
, "imgCacheEntry::Touch");
974 mTouchedTime
= SecondsFromPRTime(PR_Now());
980 void imgCacheEntry::UpdateCache(int32_t diff
/* = 0 */) {
981 // Don't update the cache if we've been removed from it or it doesn't care
982 // about our size or usage.
983 if (!Evicted() && HasNoProxies()) {
984 mLoader
->CacheEntriesChanged(mRequest
->IsChrome(), diff
);
988 void imgCacheEntry::UpdateLoadTime() {
989 mLoadTime
= SecondsFromPRTime(PR_Now());
992 void imgCacheEntry::SetHasNoProxies(bool hasNoProxies
) {
993 if (MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
995 LOG_FUNC_WITH_PARAM(gImgLog
, "imgCacheEntry::SetHasNoProxies true", "uri",
996 mRequest
->CacheKey().URI());
998 LOG_FUNC_WITH_PARAM(gImgLog
, "imgCacheEntry::SetHasNoProxies false",
999 "uri", mRequest
->CacheKey().URI());
1003 mHasNoProxies
= hasNoProxies
;
1006 imgCacheQueue::imgCacheQueue() : mDirty(false), mSize(0) {}
1008 void imgCacheQueue::UpdateSize(int32_t diff
) { mSize
+= diff
; }
1010 uint32_t imgCacheQueue::GetSize() const { return mSize
; }
1012 void imgCacheQueue::Remove(imgCacheEntry
* entry
) {
1013 uint64_t index
= mQueue
.IndexOf(entry
);
1014 if (index
== queueContainer::NoIndex
) {
1018 mSize
-= mQueue
[index
]->GetDataSize();
1020 // If the queue is clean and this is the first entry,
1021 // then we can efficiently remove the entry without
1022 // dirtying the sort order.
1023 if (!IsDirty() && index
== 0) {
1024 std::pop_heap(mQueue
.begin(), mQueue
.end(), imgLoader::CompareCacheEntries
);
1025 mQueue
.RemoveLastElement();
1029 // Remove from the middle of the list. This potentially
1030 // breaks the binary heap sort order.
1031 mQueue
.RemoveElementAt(index
);
1033 // If we only have one entry or the queue is empty, though,
1034 // then the sort order is still effectively good. Simply
1035 // refresh the list to clear the dirty flag.
1036 if (mQueue
.Length() <= 1) {
1041 // Otherwise we must mark the queue dirty and potentially
1042 // trigger an expensive sort later.
1046 void imgCacheQueue::Push(imgCacheEntry
* entry
) {
1047 mSize
+= entry
->GetDataSize();
1049 RefPtr
<imgCacheEntry
> refptr(entry
);
1050 mQueue
.AppendElement(std::move(refptr
));
1051 // If we're not dirty already, then we can efficiently add this to the
1052 // binary heap immediately. This is only O(log n).
1054 std::push_heap(mQueue
.begin(), mQueue
.end(),
1055 imgLoader::CompareCacheEntries
);
1059 already_AddRefed
<imgCacheEntry
> imgCacheQueue::Pop() {
1060 if (mQueue
.IsEmpty()) {
1067 std::pop_heap(mQueue
.begin(), mQueue
.end(), imgLoader::CompareCacheEntries
);
1068 RefPtr
<imgCacheEntry
> entry
= mQueue
.PopLastElement();
1070 mSize
-= entry
->GetDataSize();
1071 return entry
.forget();
1074 void imgCacheQueue::Refresh() {
1075 // Resort the list. This is an O(3 * n) operation and best avoided
1077 std::make_heap(mQueue
.begin(), mQueue
.end(), imgLoader::CompareCacheEntries
);
1081 void imgCacheQueue::MarkDirty() { mDirty
= true; }
1083 bool imgCacheQueue::IsDirty() { return mDirty
; }
1085 uint32_t imgCacheQueue::GetNumElements() const { return mQueue
.Length(); }
1087 bool imgCacheQueue::Contains(imgCacheEntry
* aEntry
) const {
1088 return mQueue
.Contains(aEntry
);
1091 imgCacheQueue::iterator
imgCacheQueue::begin() { return mQueue
.begin(); }
1093 imgCacheQueue::const_iterator
imgCacheQueue::begin() const {
1094 return mQueue
.begin();
1097 imgCacheQueue::iterator
imgCacheQueue::end() { return mQueue
.end(); }
1099 imgCacheQueue::const_iterator
imgCacheQueue::end() const {
1100 return mQueue
.end();
1103 nsresult
imgLoader::CreateNewProxyForRequest(
1104 imgRequest
* aRequest
, nsIURI
* aURI
, nsILoadGroup
* aLoadGroup
,
1105 Document
* aLoadingDocument
, imgINotificationObserver
* aObserver
,
1106 nsLoadFlags aLoadFlags
, imgRequestProxy
** _retval
) {
1107 LOG_SCOPE_WITH_PARAM(gImgLog
, "imgLoader::CreateNewProxyForRequest",
1108 "imgRequest", aRequest
);
1110 /* XXX If we move decoding onto separate threads, we should save off the
1111 calling thread here and pass it off to |proxyRequest| so that it call
1112 proxy calls to |aObserver|.
1115 RefPtr
<imgRequestProxy
> proxyRequest
= new imgRequestProxy();
1117 /* It is important to call |SetLoadFlags()| before calling |Init()| because
1118 |Init()| adds the request to the loadgroup.
1120 proxyRequest
->SetLoadFlags(aLoadFlags
);
1122 // init adds itself to imgRequest's list of observers
1123 nsresult rv
= proxyRequest
->Init(aRequest
, aLoadGroup
, aLoadingDocument
, aURI
,
1125 if (NS_WARN_IF(NS_FAILED(rv
))) {
1129 proxyRequest
.forget(_retval
);
1133 class imgCacheExpirationTracker final
1134 : public nsExpirationTracker
<imgCacheEntry
, 3> {
1135 enum { TIMEOUT_SECONDS
= 10 };
1138 imgCacheExpirationTracker();
1141 void NotifyExpired(imgCacheEntry
* entry
) override
;
1144 imgCacheExpirationTracker::imgCacheExpirationTracker()
1145 : nsExpirationTracker
<imgCacheEntry
, 3>(TIMEOUT_SECONDS
* 1000,
1146 "imgCacheExpirationTracker") {}
1148 void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry
* entry
) {
1149 // Hold on to a reference to this entry, because the expiration tracker
1150 // mechanism doesn't.
1151 RefPtr
<imgCacheEntry
> kungFuDeathGrip(entry
);
1153 if (MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1154 RefPtr
<imgRequest
> req
= entry
->GetRequest();
1156 LOG_FUNC_WITH_PARAM(gImgLog
, "imgCacheExpirationTracker::NotifyExpired",
1157 "entry", req
->CacheKey().URI());
1161 // We can be called multiple times on the same entry. Don't do work multiple
1163 if (!entry
->Evicted()) {
1164 entry
->Loader()->RemoveFromCache(entry
);
1167 entry
->Loader()->VerifyCacheSizes();
1170 ///////////////////////////////////////////////////////////////////////////////
1172 ///////////////////////////////////////////////////////////////////////////////
1174 double imgLoader::sCacheTimeWeight
;
1175 uint32_t imgLoader::sCacheMaxSize
;
1176 imgMemoryReporter
* imgLoader::sMemReporter
;
1178 NS_IMPL_ISUPPORTS(imgLoader
, imgILoader
, nsIContentSniffer
, imgICache
,
1179 nsISupportsWeakReference
, nsIObserver
)
1181 static imgLoader
* gNormalLoader
= nullptr;
1182 static imgLoader
* gPrivateBrowsingLoader
= nullptr;
1185 mozilla::CORSMode
imgLoader::ConvertToCORSMode(uint32_t aImgCORS
) {
1187 case imgIRequest::CORS_NONE
:
1188 return CORSMode::CORS_NONE
;
1189 case imgIRequest::CORS_ANONYMOUS
:
1190 return CORSMode::CORS_ANONYMOUS
;
1191 case imgIRequest::CORS_USE_CREDENTIALS
:
1192 return CORSMode::CORS_USE_CREDENTIALS
;
1195 MOZ_ASSERT(false, "Unexpected imgIRequest CORS value");
1196 return CORSMode::CORS_NONE
;
1200 already_AddRefed
<imgLoader
> imgLoader::CreateImageLoader() {
1201 // In some cases, such as xpctests, XPCOM modules are not automatically
1202 // initialized. We need to make sure that our module is initialized before
1203 // we hand out imgLoader instances and code starts using them.
1204 mozilla::image::EnsureModuleInitialized();
1206 RefPtr
<imgLoader
> loader
= new imgLoader();
1209 return loader
.forget();
1212 imgLoader
* imgLoader::NormalLoader() {
1213 if (!gNormalLoader
) {
1214 gNormalLoader
= CreateImageLoader().take();
1216 return gNormalLoader
;
1219 imgLoader
* imgLoader::PrivateBrowsingLoader() {
1220 if (!gPrivateBrowsingLoader
) {
1221 gPrivateBrowsingLoader
= CreateImageLoader().take();
1222 gPrivateBrowsingLoader
->RespectPrivacyNotifications();
1224 return gPrivateBrowsingLoader
;
1227 imgLoader::imgLoader()
1228 : mUncachedImagesMutex("imgLoader::UncachedImages"),
1229 mRespectPrivacy(false) {
1230 sMemReporter
->AddRef();
1231 sMemReporter
->RegisterLoader(this);
1234 imgLoader::~imgLoader() {
1235 ClearChromeImageCache();
1238 // If there are any of our imgRequest's left they are in the uncached
1239 // images set, so clear their pointer to us.
1240 MutexAutoLock
lock(mUncachedImagesMutex
);
1241 for (auto iter
= mUncachedImages
.Iter(); !iter
.Done(); iter
.Next()) {
1242 nsPtrHashKey
<imgRequest
>* entry
= iter
.Get();
1243 RefPtr
<imgRequest
> req
= entry
->GetKey();
1247 sMemReporter
->UnregisterLoader(this);
1248 sMemReporter
->Release();
1251 void imgLoader::VerifyCacheSizes() {
1253 if (!mCacheTracker
) {
1257 uint32_t cachesize
= mCache
.Count() + mChromeCache
.Count();
1258 uint32_t queuesize
=
1259 mCacheQueue
.GetNumElements() + mChromeCacheQueue
.GetNumElements();
1260 uint32_t trackersize
= 0;
1261 for (nsExpirationTracker
<imgCacheEntry
, 3>::Iterator
it(mCacheTracker
.get());
1265 MOZ_ASSERT(queuesize
== trackersize
, "Queue and tracker sizes out of sync!");
1266 MOZ_ASSERT(queuesize
<= cachesize
, "Queue has more elements than cache!");
1270 imgLoader::imgCacheTable
& imgLoader::GetCache(bool aForChrome
) {
1271 return aForChrome
? mChromeCache
: mCache
;
1274 imgLoader::imgCacheTable
& imgLoader::GetCache(const ImageCacheKey
& aKey
) {
1275 return GetCache(aKey
.IsChrome());
1278 imgCacheQueue
& imgLoader::GetCacheQueue(bool aForChrome
) {
1279 return aForChrome
? mChromeCacheQueue
: mCacheQueue
;
1282 imgCacheQueue
& imgLoader::GetCacheQueue(const ImageCacheKey
& aKey
) {
1283 return GetCacheQueue(aKey
.IsChrome());
1286 void imgLoader::GlobalInit() {
1287 sCacheTimeWeight
= StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
1288 int32_t cachesize
= StaticPrefs::image_cache_size_AtStartup();
1289 sCacheMaxSize
= cachesize
> 0 ? cachesize
: 0;
1291 sMemReporter
= new imgMemoryReporter();
1292 RegisterStrongAsyncMemoryReporter(sMemReporter
);
1293 RegisterImagesContentUsedUncompressedDistinguishedAmount(
1294 imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount
);
1297 void imgLoader::ShutdownMemoryReporter() {
1298 UnregisterImagesContentUsedUncompressedDistinguishedAmount();
1299 UnregisterStrongMemoryReporter(sMemReporter
);
1302 nsresult
imgLoader::InitCache() {
1303 nsCOMPtr
<nsIObserverService
> os
= mozilla::services::GetObserverService();
1305 return NS_ERROR_FAILURE
;
1308 os
->AddObserver(this, "memory-pressure", false);
1309 os
->AddObserver(this, "chrome-flush-caches", false);
1310 os
->AddObserver(this, "last-pb-context-exited", false);
1311 os
->AddObserver(this, "profile-before-change", false);
1312 os
->AddObserver(this, "xpcom-shutdown", false);
1314 mCacheTracker
= MakeUnique
<imgCacheExpirationTracker
>();
1319 nsresult
imgLoader::Init() {
1326 imgLoader::RespectPrivacyNotifications() {
1327 mRespectPrivacy
= true;
1332 imgLoader::Observe(nsISupports
* aSubject
, const char* aTopic
,
1333 const char16_t
* aData
) {
1334 if (strcmp(aTopic
, "memory-pressure") == 0) {
1336 } else if (strcmp(aTopic
, "chrome-flush-caches") == 0) {
1338 ClearChromeImageCache();
1339 } else if (strcmp(aTopic
, "last-pb-context-exited") == 0) {
1340 if (mRespectPrivacy
) {
1342 ClearChromeImageCache();
1344 } else if (strcmp(aTopic
, "profile-before-change") == 0) {
1345 mCacheTracker
= nullptr;
1346 } else if (strcmp(aTopic
, "xpcom-shutdown") == 0) {
1347 mCacheTracker
= nullptr;
1348 ShutdownMemoryReporter();
1351 // (Nothing else should bring us here)
1352 MOZ_ASSERT(0, "Invalid topic received");
1359 imgLoader::ClearCache(bool chrome
) {
1360 if (XRE_IsParentProcess()) {
1361 bool privateLoader
= this == gPrivateBrowsingLoader
;
1362 for (auto* cp
: ContentParent::AllProcesses(ContentParent::eLive
)) {
1363 Unused
<< cp
->SendClearImageCache(privateLoader
, chrome
);
1368 return ClearChromeImageCache();
1370 return ClearImageCache();
1374 imgLoader::RemoveEntriesFromPrincipal(nsIPrincipal
* aPrincipal
) {
1375 nsAutoString origin
;
1376 nsresult rv
= nsContentUtils::GetUTFOrigin(aPrincipal
, origin
);
1377 if (NS_WARN_IF(NS_FAILED(rv
))) {
1381 AutoTArray
<RefPtr
<imgCacheEntry
>, 128> entriesToBeRemoved
;
1383 imgCacheTable
& cache
= GetCache(aPrincipal
->IsSystemPrincipal());
1384 for (auto iter
= cache
.Iter(); !iter
.Done(); iter
.Next()) {
1385 auto& key
= iter
.Key();
1387 if (key
.OriginAttributesRef() !=
1388 BasePrincipal::Cast(aPrincipal
)->OriginAttributesRef()) {
1392 nsAutoString imageOrigin
;
1393 nsresult rv
= nsContentUtils::GetUTFOrigin(key
.URI(), imageOrigin
);
1394 if (NS_WARN_IF(NS_FAILED(rv
))) {
1398 if (imageOrigin
== origin
) {
1399 entriesToBeRemoved
.AppendElement(iter
.Data());
1403 for (auto& entry
: entriesToBeRemoved
) {
1404 if (!RemoveFromCache(entry
)) {
1406 "Couldn't remove an entry from the cache in "
1407 "RemoveEntriesFromPrincipal()\n");
1415 imgLoader::RemoveEntry(nsIURI
* aURI
, Document
* aDoc
) {
1417 OriginAttributes attrs
;
1419 nsCOMPtr
<nsIPrincipal
> principal
= aDoc
->NodePrincipal();
1421 attrs
= principal
->OriginAttributesRef();
1425 ImageCacheKey
key(aURI
, attrs
, aDoc
);
1426 if (RemoveFromCache(key
)) {
1430 return NS_ERROR_NOT_AVAILABLE
;
1434 imgLoader::FindEntryProperties(nsIURI
* uri
, Document
* aDoc
,
1435 nsIProperties
** _retval
) {
1438 OriginAttributes attrs
;
1440 nsCOMPtr
<nsIPrincipal
> principal
= aDoc
->NodePrincipal();
1442 attrs
= principal
->OriginAttributesRef();
1446 ImageCacheKey
key(uri
, attrs
, aDoc
);
1447 imgCacheTable
& cache
= GetCache(key
);
1449 RefPtr
<imgCacheEntry
> entry
;
1450 if (cache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
1451 if (mCacheTracker
&& entry
->HasNoProxies()) {
1452 mCacheTracker
->MarkUsed(entry
);
1455 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1457 nsCOMPtr
<nsIProperties
> properties
= request
->Properties();
1458 properties
.forget(_retval
);
1465 NS_IMETHODIMP_(void)
1466 imgLoader::ClearCacheForControlledDocument(Document
* aDoc
) {
1468 AutoTArray
<RefPtr
<imgCacheEntry
>, 128> entriesToBeRemoved
;
1469 imgCacheTable
& cache
= GetCache(false);
1470 for (auto iter
= cache
.Iter(); !iter
.Done(); iter
.Next()) {
1471 auto& key
= iter
.Key();
1472 if (key
.ControlledDocument() == aDoc
) {
1473 entriesToBeRemoved
.AppendElement(iter
.Data());
1476 for (auto& entry
: entriesToBeRemoved
) {
1477 if (!RemoveFromCache(entry
)) {
1479 "Couldn't remove an entry from the cache in "
1480 "ClearCacheForControlledDocument()\n");
1485 void imgLoader::Shutdown() {
1486 NS_IF_RELEASE(gNormalLoader
);
1487 gNormalLoader
= nullptr;
1488 NS_IF_RELEASE(gPrivateBrowsingLoader
);
1489 gPrivateBrowsingLoader
= nullptr;
1492 nsresult
imgLoader::ClearChromeImageCache() {
1493 return EvictEntries(mChromeCache
);
1496 nsresult
imgLoader::ClearImageCache() { return EvictEntries(mCache
); }
1498 void imgLoader::MinimizeCaches() {
1499 EvictEntries(mCacheQueue
);
1500 EvictEntries(mChromeCacheQueue
);
1503 bool imgLoader::PutIntoCache(const ImageCacheKey
& aKey
, imgCacheEntry
* entry
) {
1504 imgCacheTable
& cache
= GetCache(aKey
);
1506 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::PutIntoCache", "uri",
1509 // Check to see if this request already exists in the cache. If so, we'll
1510 // replace the old version.
1511 RefPtr
<imgCacheEntry
> tmpCacheEntry
;
1512 if (cache
.Get(aKey
, getter_AddRefs(tmpCacheEntry
)) && tmpCacheEntry
) {
1514 gImgLog
, LogLevel::Debug
,
1515 ("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
1517 RefPtr
<imgRequest
> tmpRequest
= tmpCacheEntry
->GetRequest();
1519 // If it already exists, and we're putting the same key into the cache, we
1520 // should remove the old version.
1521 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1522 ("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
1525 RemoveFromCache(aKey
);
1527 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1528 ("[this=%p] imgLoader::PutIntoCache --"
1529 " Element NOT already in the cache",
1533 cache
.InsertOrUpdate(aKey
, RefPtr
{entry
});
1535 // We can be called to resurrect an evicted entry.
1536 if (entry
->Evicted()) {
1537 entry
->SetEvicted(false);
1540 // If we're resurrecting an entry with no proxies, put it back in the
1541 // tracker and queue.
1542 if (entry
->HasNoProxies()) {
1543 nsresult addrv
= NS_OK
;
1545 if (mCacheTracker
) {
1546 addrv
= mCacheTracker
->AddObject(entry
);
1549 if (NS_SUCCEEDED(addrv
)) {
1550 imgCacheQueue
& queue
= GetCacheQueue(aKey
);
1555 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1556 request
->SetIsInCache(true);
1557 RemoveFromUncachedImages(request
);
1562 bool imgLoader::SetHasNoProxies(imgRequest
* aRequest
, imgCacheEntry
* aEntry
) {
1563 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::SetHasNoProxies", "uri",
1564 aRequest
->CacheKey().URI());
1566 aEntry
->SetHasNoProxies(true);
1568 if (aEntry
->Evicted()) {
1572 imgCacheQueue
& queue
= GetCacheQueue(aRequest
->IsChrome());
1574 nsresult addrv
= NS_OK
;
1576 if (mCacheTracker
) {
1577 addrv
= mCacheTracker
->AddObject(aEntry
);
1580 if (NS_SUCCEEDED(addrv
)) {
1584 imgCacheTable
& cache
= GetCache(aRequest
->IsChrome());
1585 CheckCacheLimits(cache
, queue
);
1590 bool imgLoader::SetHasProxies(imgRequest
* aRequest
) {
1593 const ImageCacheKey
& key
= aRequest
->CacheKey();
1594 imgCacheTable
& cache
= GetCache(key
);
1596 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::SetHasProxies", "uri",
1599 RefPtr
<imgCacheEntry
> entry
;
1600 if (cache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
1601 // Make sure the cache entry is for the right request
1602 RefPtr
<imgRequest
> entryRequest
= entry
->GetRequest();
1603 if (entryRequest
== aRequest
&& entry
->HasNoProxies()) {
1604 imgCacheQueue
& queue
= GetCacheQueue(key
);
1605 queue
.Remove(entry
);
1607 if (mCacheTracker
) {
1608 mCacheTracker
->RemoveObject(entry
);
1611 entry
->SetHasNoProxies(false);
1620 void imgLoader::CacheEntriesChanged(bool aForChrome
,
1621 int32_t aSizeDiff
/* = 0 */) {
1622 imgCacheQueue
& queue
= GetCacheQueue(aForChrome
);
1623 // We only need to dirty the queue if there is any sorting
1624 // taking place. Empty or single-entry lists can't become
1626 if (queue
.GetNumElements() > 1) {
1629 queue
.UpdateSize(aSizeDiff
);
1632 void imgLoader::CheckCacheLimits(imgCacheTable
& cache
, imgCacheQueue
& queue
) {
1633 if (queue
.GetNumElements() == 0) {
1634 NS_ASSERTION(queue
.GetSize() == 0,
1635 "imgLoader::CheckCacheLimits -- incorrect cache size");
1638 // Remove entries from the cache until we're back at our desired max size.
1639 while (queue
.GetSize() > sCacheMaxSize
) {
1640 // Remove the first entry in the queue.
1641 RefPtr
<imgCacheEntry
> entry(queue
.Pop());
1643 NS_ASSERTION(entry
, "imgLoader::CheckCacheLimits -- NULL entry pointer");
1645 if (MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1646 RefPtr
<imgRequest
> req
= entry
->GetRequest();
1648 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::CheckCacheLimits",
1649 "entry", req
->CacheKey().URI());
1654 // We just popped this entry from the queue, so pass AlreadyRemoved
1655 // to avoid searching the queue again in RemoveFromCache.
1656 RemoveFromCache(entry
, QueueState::AlreadyRemoved
);
1661 bool imgLoader::ValidateRequestWithNewChannel(
1662 imgRequest
* request
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
1663 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
1664 imgINotificationObserver
* aObserver
, Document
* aLoadingDocument
,
1665 uint64_t aInnerWindowId
, nsLoadFlags aLoadFlags
,
1666 nsContentPolicyType aLoadPolicyType
, imgRequestProxy
** aProxyRequest
,
1667 nsIPrincipal
* aTriggeringPrincipal
, int32_t aCORSMode
, bool aLinkPreload
,
1668 bool* aNewChannelCreated
) {
1669 // now we need to insert a new channel request object in between the real
1670 // request and the proxy that basically delays loading the image until it
1671 // gets a 304 or figures out that this needs to be a new request
1675 // If we're currently in the middle of validating this request, just hand
1676 // back a proxy to it; the required work will be done for us.
1677 if (imgCacheValidator
* validator
= request
->GetValidator()) {
1678 rv
= CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
1679 aObserver
, aLoadFlags
, aProxyRequest
);
1680 if (NS_FAILED(rv
)) {
1684 if (*aProxyRequest
) {
1685 imgRequestProxy
* proxy
= static_cast<imgRequestProxy
*>(*aProxyRequest
);
1687 // We will send notifications from imgCacheValidator::OnStartRequest().
1688 // In the mean time, we must defer notifications because we are added to
1689 // the imgRequest's proxy list, and we can get extra notifications
1690 // resulting from methods such as StartDecoding(). See bug 579122.
1691 proxy
->MarkValidating();
1694 MOZ_ASSERT(aLoadingDocument
);
1695 proxy
->PrioritizeAsPreload();
1696 auto preloadKey
= PreloadHashKey::CreateAsImage(
1697 aURI
, aTriggeringPrincipal
, ConvertToCORSMode(aCORSMode
));
1698 proxy
->NotifyOpen(preloadKey
, aLoadingDocument
, true);
1701 // Attach the proxy without notifying
1702 validator
->AddProxy(proxy
);
1707 // We will rely on Necko to cache this request when it's possible, and to
1708 // tell imgCacheValidator::OnStartRequest whether the request came from its
1710 nsCOMPtr
<nsIChannel
> newChannel
;
1711 bool forcePrincipalCheck
;
1712 rv
= NewImageChannel(getter_AddRefs(newChannel
), &forcePrincipalCheck
, aURI
,
1713 aInitialDocumentURI
, aCORSMode
, aReferrerInfo
,
1714 aLoadGroup
, aLoadFlags
, aLoadPolicyType
,
1715 aTriggeringPrincipal
, aLoadingDocument
, mRespectPrivacy
);
1716 if (NS_FAILED(rv
)) {
1720 if (aNewChannelCreated
) {
1721 *aNewChannelCreated
= true;
1724 RefPtr
<imgRequestProxy
> req
;
1725 rv
= CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
1726 aObserver
, aLoadFlags
, getter_AddRefs(req
));
1727 if (NS_FAILED(rv
)) {
1731 // Make sure that OnStatus/OnProgress calls have the right request set...
1732 RefPtr
<nsProgressNotificationProxy
> progressproxy
=
1733 new nsProgressNotificationProxy(newChannel
, req
);
1734 if (!progressproxy
) {
1738 RefPtr
<imgCacheValidator
> hvc
=
1739 new imgCacheValidator(progressproxy
, this, request
, aLoadingDocument
,
1740 aInnerWindowId
, forcePrincipalCheck
);
1742 // Casting needed here to get past multiple inheritance.
1743 nsCOMPtr
<nsIStreamListener
> listener
=
1744 do_QueryInterface(static_cast<nsIThreadRetargetableStreamListener
*>(hvc
));
1745 NS_ENSURE_TRUE(listener
, false);
1747 // We must set the notification callbacks before setting up the
1748 // CORS listener, because that's also interested inthe
1749 // notification callbacks.
1750 newChannel
->SetNotificationCallbacks(hvc
);
1752 request
->SetValidator(hvc
);
1754 // We will send notifications from imgCacheValidator::OnStartRequest().
1755 // In the mean time, we must defer notifications because we are added to
1756 // the imgRequest's proxy list, and we can get extra notifications
1757 // resulting from methods such as StartDecoding(). See bug 579122.
1758 req
->MarkValidating();
1761 MOZ_ASSERT(aLoadingDocument
);
1762 req
->PrioritizeAsPreload();
1763 auto preloadKey
= PreloadHashKey::CreateAsImage(
1764 aURI
, aTriggeringPrincipal
, ConvertToCORSMode(aCORSMode
));
1765 req
->NotifyOpen(preloadKey
, aLoadingDocument
, true);
1768 // Add the proxy without notifying
1771 mozilla::net::PredictorLearn(aURI
, aInitialDocumentURI
,
1772 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE
,
1774 rv
= newChannel
->AsyncOpen(listener
);
1775 if (NS_WARN_IF(NS_FAILED(rv
))) {
1776 req
->CancelAndForgetObserver(rv
);
1777 // This will notify any current or future <link preload> tags. Pass the
1778 // non-open channel so that we can read loadinfo and referrer info of that
1780 req
->NotifyStart(newChannel
);
1781 // Use the non-channel overload of this method to force the notification to
1782 // happen. The preload request has not been assigned a channel.
1783 req
->NotifyStop(rv
);
1787 req
.forget(aProxyRequest
);
1791 bool imgLoader::ValidateEntry(
1792 imgCacheEntry
* aEntry
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
1793 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
1794 imgINotificationObserver
* aObserver
, Document
* aLoadingDocument
,
1795 nsLoadFlags aLoadFlags
, nsContentPolicyType aLoadPolicyType
,
1796 bool aCanMakeNewChannel
, bool* aNewChannelCreated
,
1797 imgRequestProxy
** aProxyRequest
, nsIPrincipal
* aTriggeringPrincipal
,
1798 int32_t aCORSMode
, bool aLinkPreload
) {
1799 LOG_SCOPE(gImgLog
, "imgLoader::ValidateEntry");
1801 // If the expiration time is zero, then the request has not gotten far enough
1802 // to know when it will expire.
1803 uint32_t expiryTime
= aEntry
->GetExpiryTime();
1805 expiryTime
!= 0 && expiryTime
<= SecondsFromPRTime(PR_Now());
1809 // Special treatment for file URLs - aEntry has expired if file has changed
1810 nsCOMPtr
<nsIFileURL
> fileUrl(do_QueryInterface(aURI
));
1812 uint32_t lastModTime
= aEntry
->GetLoadTime();
1814 nsCOMPtr
<nsIFile
> theFile
;
1815 rv
= fileUrl
->GetFile(getter_AddRefs(theFile
));
1816 if (NS_SUCCEEDED(rv
)) {
1818 rv
= theFile
->GetLastModifiedTime(&fileLastMod
);
1819 if (NS_SUCCEEDED(rv
)) {
1820 // nsIFile uses millisec, NSPR usec
1821 fileLastMod
*= 1000;
1822 hasExpired
= SecondsFromPRTime((PRTime
)fileLastMod
) > lastModTime
;
1827 RefPtr
<imgRequest
> request(aEntry
->GetRequest());
1833 if (!ValidateSecurityInfo(request
, aEntry
->ForcePrincipalCheck(), aCORSMode
,
1834 aTriggeringPrincipal
, aLoadingDocument
,
1839 // data URIs are immutable and by their nature can't leak data, so we can
1840 // just return true in that case. Doing so would mean that shift-reload
1841 // doesn't reload data URI documents/images though (which is handy for
1842 // debugging during gecko development) so we make an exception in that case.
1843 nsAutoCString scheme
;
1844 aURI
->GetScheme(scheme
);
1845 if (scheme
.EqualsLiteral("data") &&
1846 !(aLoadFlags
& nsIRequest::LOAD_BYPASS_CACHE
)) {
1850 bool validateRequest
= false;
1852 if (!request
->CanReuseWithoutValidation(aLoadingDocument
)) {
1853 // If we would need to revalidate this entry, but we're being told to
1854 // bypass the cache, we don't allow this entry to be used.
1855 if (aLoadFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
1859 if (MOZ_UNLIKELY(ChaosMode::isActive(ChaosFeature::ImageCache
))) {
1860 if (ChaosMode::randomUint32LessThan(4) < 1) {
1865 // Determine whether the cache aEntry must be revalidated...
1866 validateRequest
= ShouldRevalidateEntry(aEntry
, aLoadFlags
, hasExpired
);
1868 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1869 ("imgLoader::ValidateEntry validating cache entry. "
1870 "validateRequest = %d",
1872 } else if (!aLoadingDocument
&& MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1873 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1874 ("imgLoader::ValidateEntry BYPASSING cache validation for %s "
1875 "because of NULL loading document",
1876 aURI
->GetSpecOrDefault().get()));
1879 // We can't use a cached request if it comes from a different
1880 // application cache than this load is expecting.
1881 nsCOMPtr
<nsIApplicationCacheContainer
> appCacheContainer
;
1882 nsCOMPtr
<nsIApplicationCache
> requestAppCache
;
1883 nsCOMPtr
<nsIApplicationCache
> groupAppCache
;
1884 if ((appCacheContainer
= do_GetInterface(request
->GetRequest()))) {
1885 appCacheContainer
->GetApplicationCache(getter_AddRefs(requestAppCache
));
1887 if ((appCacheContainer
= do_QueryInterface(aLoadGroup
))) {
1888 appCacheContainer
->GetApplicationCache(getter_AddRefs(groupAppCache
));
1891 if (requestAppCache
!= groupAppCache
) {
1892 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1893 ("imgLoader::ValidateEntry - Unable to use cached imgRequest "
1894 "[request=%p] because of mismatched application caches\n",
1895 address_of(request
)));
1899 // If the original request is still transferring don't kick off a validation
1900 // network request because it is a bit silly to issue a validation request if
1901 // the original request hasn't even finished yet. So just return true
1902 // indicating the caller can create a new proxy for the request and use it as
1904 // This is an optimization but it's also required for correctness. If we don't
1905 // do this then when firing the load complete notification for the original
1906 // request that can unblock load for the document and then spin the event loop
1907 // (see the stack in bug 1641682) which then the OnStartRequest for the
1908 // validation request can fire which can call UpdateProxies and can sync
1909 // notify on the progress tracker about all existing state, which includes
1910 // load complete, so we fire a second load complete notification for the
1912 // In addition, we want to validate if the original request encountered
1913 // an error for two reasons. The first being if the error was a network error
1914 // then trying to re-fetch the image might succeed. The second is more
1915 // complicated. We decide if we should fire the load or error event for img
1916 // elements depending on if the image has error in its status at the time when
1917 // the load complete notification is received, and we set error status on an
1918 // image if it encounters a network error or a decode error with no real way
1919 // to tell them apart. So if we load an image that will produce a decode error
1920 // the first time we will usually fire the load event, and then decode enough
1921 // to encounter the decode error and set the error status on the image. The
1922 // next time we reference the image in the same document the load complete
1923 // notification is replayed and this time the error status from the decode is
1924 // already present so we fire the error event instead of the load event. This
1925 // is a bug (bug 1645576) that we should fix. In order to avoid that bug in
1926 // some cases (specifically the cases when we hit this code and try to
1927 // validate the request) we make sure to validate. This avoids the bug because
1928 // when the load complete notification arrives the proxy is marked as
1929 // validating so it lies about its status and returns nothing.
1930 bool requestComplete
= false;
1931 RefPtr
<ProgressTracker
> tracker
;
1932 RefPtr
<mozilla::image::Image
> image
= request
->GetImage();
1934 tracker
= image
->GetProgressTracker();
1936 tracker
= request
->GetProgressTracker();
1939 if (tracker
->GetProgress() & (FLAG_LOAD_COMPLETE
| FLAG_HAS_ERROR
)) {
1940 requestComplete
= true;
1943 if (!requestComplete
) {
1947 if (validateRequest
&& aCanMakeNewChannel
) {
1948 LOG_SCOPE(gImgLog
, "imgLoader::ValidateRequest |cache hit| must validate");
1950 uint64_t innerWindowID
=
1951 aLoadingDocument
? aLoadingDocument
->InnerWindowID() : 0;
1952 return ValidateRequestWithNewChannel(
1953 request
, aURI
, aInitialDocumentURI
, aReferrerInfo
, aLoadGroup
,
1954 aObserver
, aLoadingDocument
, innerWindowID
, aLoadFlags
, aLoadPolicyType
,
1955 aProxyRequest
, aTriggeringPrincipal
, aCORSMode
, aLinkPreload
,
1956 aNewChannelCreated
);
1959 return !validateRequest
;
1962 bool imgLoader::RemoveFromCache(const ImageCacheKey
& aKey
) {
1963 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::RemoveFromCache", "uri",
1966 imgCacheTable
& cache
= GetCache(aKey
);
1967 imgCacheQueue
& queue
= GetCacheQueue(aKey
);
1969 RefPtr
<imgCacheEntry
> entry
;
1970 cache
.Remove(aKey
, getter_AddRefs(entry
));
1972 MOZ_ASSERT(!entry
->Evicted(), "Evicting an already-evicted cache entry!");
1974 // Entries with no proxies are in the tracker.
1975 if (entry
->HasNoProxies()) {
1976 if (mCacheTracker
) {
1977 mCacheTracker
->RemoveObject(entry
);
1979 queue
.Remove(entry
);
1982 entry
->SetEvicted(true);
1984 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1985 request
->SetIsInCache(false);
1986 AddToUncachedImages(request
);
1993 bool imgLoader::RemoveFromCache(imgCacheEntry
* entry
, QueueState aQueueState
) {
1994 LOG_STATIC_FUNC(gImgLog
, "imgLoader::RemoveFromCache entry");
1996 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1998 const ImageCacheKey
& key
= request
->CacheKey();
1999 imgCacheTable
& cache
= GetCache(key
);
2000 imgCacheQueue
& queue
= GetCacheQueue(key
);
2002 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::RemoveFromCache",
2003 "entry's uri", key
.URI());
2007 if (entry
->HasNoProxies()) {
2008 LOG_STATIC_FUNC(gImgLog
,
2009 "imgLoader::RemoveFromCache removing from tracker");
2010 if (mCacheTracker
) {
2011 mCacheTracker
->RemoveObject(entry
);
2013 // Only search the queue to remove the entry if its possible it might
2014 // be in the queue. If we know its not in the queue this would be
2016 MOZ_ASSERT_IF(aQueueState
== QueueState::AlreadyRemoved
,
2017 !queue
.Contains(entry
));
2018 if (aQueueState
== QueueState::MaybeExists
) {
2019 queue
.Remove(entry
);
2023 entry
->SetEvicted(true);
2024 request
->SetIsInCache(false);
2025 AddToUncachedImages(request
);
2033 nsresult
imgLoader::EvictEntries(imgCacheTable
& aCacheToClear
) {
2034 LOG_STATIC_FUNC(gImgLog
, "imgLoader::EvictEntries table");
2036 // We have to make a temporary, since RemoveFromCache removes the element
2037 // from the queue, invalidating iterators.
2038 nsTArray
<RefPtr
<imgCacheEntry
> > entries
;
2039 for (auto iter
= aCacheToClear
.Iter(); !iter
.Done(); iter
.Next()) {
2040 RefPtr
<imgCacheEntry
>& data
= iter
.Data();
2041 entries
.AppendElement(data
);
2044 for (uint32_t i
= 0; i
< entries
.Length(); ++i
) {
2045 if (!RemoveFromCache(entries
[i
])) {
2046 return NS_ERROR_FAILURE
;
2050 MOZ_ASSERT(aCacheToClear
.Count() == 0);
2055 nsresult
imgLoader::EvictEntries(imgCacheQueue
& aQueueToClear
) {
2056 LOG_STATIC_FUNC(gImgLog
, "imgLoader::EvictEntries queue");
2058 // We have to make a temporary, since RemoveFromCache removes the element
2059 // from the queue, invalidating iterators.
2060 nsTArray
<RefPtr
<imgCacheEntry
> > entries(aQueueToClear
.GetNumElements());
2061 for (auto i
= aQueueToClear
.begin(); i
!= aQueueToClear
.end(); ++i
) {
2062 entries
.AppendElement(*i
);
2065 // Iterate in reverse order to minimize array copying.
2066 for (auto& entry
: entries
) {
2067 if (!RemoveFromCache(entry
)) {
2068 return NS_ERROR_FAILURE
;
2072 MOZ_ASSERT(aQueueToClear
.GetNumElements() == 0);
2077 void imgLoader::AddToUncachedImages(imgRequest
* aRequest
) {
2078 MutexAutoLock
lock(mUncachedImagesMutex
);
2079 mUncachedImages
.PutEntry(aRequest
);
2082 void imgLoader::RemoveFromUncachedImages(imgRequest
* aRequest
) {
2083 MutexAutoLock
lock(mUncachedImagesMutex
);
2084 mUncachedImages
.RemoveEntry(aRequest
);
2087 bool imgLoader::PreferLoadFromCache(nsIURI
* aURI
) const {
2088 // If we are trying to load an image from a protocol that doesn't support
2089 // caching (e.g. thumbnails via the moz-page-thumb:// protocol, or icons via
2090 // the moz-extension:// protocol), load it directly from the cache to prevent
2091 // re-decoding the image. See Bug 1373258.
2092 // TODO: Bug 1406134
2093 return aURI
->SchemeIs("moz-page-thumb") || aURI
->SchemeIs("moz-extension");
2096 #define LOAD_FLAGS_CACHE_MASK \
2097 (nsIRequest::LOAD_BYPASS_CACHE | nsIRequest::LOAD_FROM_CACHE)
2099 #define LOAD_FLAGS_VALIDATE_MASK \
2100 (nsIRequest::VALIDATE_ALWAYS | nsIRequest::VALIDATE_NEVER | \
2101 nsIRequest::VALIDATE_ONCE_PER_SESSION)
2104 imgLoader::LoadImageXPCOM(
2105 nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
, nsIReferrerInfo
* aReferrerInfo
,
2106 nsIPrincipal
* aTriggeringPrincipal
, nsILoadGroup
* aLoadGroup
,
2107 imgINotificationObserver
* aObserver
, Document
* aLoadingDocument
,
2108 nsLoadFlags aLoadFlags
, nsISupports
* aCacheKey
,
2109 nsContentPolicyType aContentPolicyType
, imgIRequest
** _retval
) {
2110 // Optional parameter, so defaults to 0 (== TYPE_INVALID)
2111 if (!aContentPolicyType
) {
2112 aContentPolicyType
= nsIContentPolicy::TYPE_INTERNAL_IMAGE
;
2114 imgRequestProxy
* proxy
;
2115 nsresult rv
= LoadImage(
2116 aURI
, aInitialDocumentURI
, aReferrerInfo
, aTriggeringPrincipal
, 0,
2117 aLoadGroup
, aObserver
, aLoadingDocument
, aLoadingDocument
, aLoadFlags
,
2118 aCacheKey
, aContentPolicyType
, u
""_ns
,
2119 /* aUseUrgentStartForChannel */ false, /* aListPreload */ false, &proxy
);
2124 static void MakeRequestStaticIfNeeded(
2125 Document
* aLoadingDocument
, imgRequestProxy
** aProxyAboutToGetReturned
) {
2126 if (!aLoadingDocument
|| !aLoadingDocument
->IsStaticDocument()) {
2130 if (!*aProxyAboutToGetReturned
) {
2134 RefPtr
<imgRequestProxy
> proxy
= dont_AddRef(*aProxyAboutToGetReturned
);
2135 *aProxyAboutToGetReturned
= nullptr;
2137 RefPtr
<imgRequestProxy
> staticProxy
=
2138 proxy
->GetStaticRequest(aLoadingDocument
);
2139 if (staticProxy
!= proxy
) {
2140 proxy
->CancelAndForgetObserver(NS_BINDING_ABORTED
);
2141 proxy
= std::move(staticProxy
);
2143 proxy
.forget(aProxyAboutToGetReturned
);
2146 nsresult
imgLoader::LoadImage(
2147 nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
, nsIReferrerInfo
* aReferrerInfo
,
2148 nsIPrincipal
* aTriggeringPrincipal
, uint64_t aRequestContextID
,
2149 nsILoadGroup
* aLoadGroup
, imgINotificationObserver
* aObserver
,
2150 nsINode
* aContext
, Document
* aLoadingDocument
, nsLoadFlags aLoadFlags
,
2151 nsISupports
* aCacheKey
, nsContentPolicyType aContentPolicyType
,
2152 const nsAString
& initiatorType
, bool aUseUrgentStartForChannel
,
2153 bool aLinkPreload
, imgRequestProxy
** _retval
) {
2156 NS_ASSERTION(aURI
, "imgLoader::LoadImage -- NULL URI pointer");
2159 return NS_ERROR_NULL_POINTER
;
2162 auto makeStaticIfNeeded
= mozilla::MakeScopeExit(
2163 [&] { MakeRequestStaticIfNeeded(aLoadingDocument
, _retval
); });
2165 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK
,
2166 aURI
->GetSpecOrDefault());
2168 LOG_SCOPE_WITH_PARAM(gImgLog
, "imgLoader::LoadImage", "aURI", aURI
);
2172 RefPtr
<imgRequest
> request
;
2175 nsLoadFlags requestFlags
= nsIRequest::LOAD_NORMAL
;
2178 bool isPrivate
= false;
2180 if (aLoadingDocument
) {
2181 isPrivate
= nsContentUtils::IsInPrivateBrowsing(aLoadingDocument
);
2182 } else if (aLoadGroup
) {
2183 isPrivate
= nsContentUtils::IsInPrivateBrowsing(aLoadGroup
);
2185 MOZ_ASSERT(isPrivate
== mRespectPrivacy
);
2187 if (aLoadingDocument
) {
2188 // The given load group should match that of the document if given. If
2189 // that isn't the case, then we need to add more plumbing to ensure we
2190 // block the document as well.
2191 nsCOMPtr
<nsILoadGroup
> docLoadGroup
=
2192 aLoadingDocument
->GetDocumentLoadGroup();
2193 MOZ_ASSERT(docLoadGroup
== aLoadGroup
);
2197 // Get the default load flags from the loadgroup (if possible)...
2199 aLoadGroup
->GetLoadFlags(&requestFlags
);
2200 if (PreferLoadFromCache(aURI
)) {
2201 requestFlags
|= nsIRequest::LOAD_FROM_CACHE
;
2205 // Merge the default load flags with those passed in via aLoadFlags.
2206 // Currently, *only* the caching, validation and background load flags
2209 // The flags in aLoadFlags take precedence over the default flags!
2211 if (aLoadFlags
& LOAD_FLAGS_CACHE_MASK
) {
2212 // Override the default caching flags...
2213 requestFlags
= (requestFlags
& ~LOAD_FLAGS_CACHE_MASK
) |
2214 (aLoadFlags
& LOAD_FLAGS_CACHE_MASK
);
2216 if (aLoadFlags
& LOAD_FLAGS_VALIDATE_MASK
) {
2217 // Override the default validation flags...
2218 requestFlags
= (requestFlags
& ~LOAD_FLAGS_VALIDATE_MASK
) |
2219 (aLoadFlags
& LOAD_FLAGS_VALIDATE_MASK
);
2221 if (aLoadFlags
& nsIRequest::LOAD_BACKGROUND
) {
2222 // Propagate background loading...
2223 requestFlags
|= nsIRequest::LOAD_BACKGROUND
;
2227 // Set background loading if it is <link rel=preload>
2228 requestFlags
|= nsIRequest::LOAD_BACKGROUND
;
2231 int32_t corsmode
= imgIRequest::CORS_NONE
;
2232 if (aLoadFlags
& imgILoader::LOAD_CORS_ANONYMOUS
) {
2233 corsmode
= imgIRequest::CORS_ANONYMOUS
;
2234 } else if (aLoadFlags
& imgILoader::LOAD_CORS_USE_CREDENTIALS
) {
2235 corsmode
= imgIRequest::CORS_USE_CREDENTIALS
;
2238 // Look in the preloaded images of loading document first.
2239 if (StaticPrefs::network_preload() && !aLinkPreload
&& aLoadingDocument
) {
2240 auto key
= PreloadHashKey::CreateAsImage(aURI
, aTriggeringPrincipal
,
2241 ConvertToCORSMode(corsmode
));
2242 if (RefPtr
<PreloaderBase
> preload
=
2243 aLoadingDocument
->Preloads().LookupPreload(key
)) {
2244 RefPtr
<imgRequestProxy
> proxy
= do_QueryObject(preload
);
2247 MOZ_LOG(gImgLog
, LogLevel::Debug
,
2248 ("[this=%p] imgLoader::LoadImage -- preloaded [proxy=%p]"
2250 this, proxy
.get(), aLoadingDocument
));
2252 // Removing the preload for this image to be in parity with Chromium. Any
2253 // following regular image request will be reloaded using the regular
2254 // path: image cache, http cache, network. Any following `<link
2255 // rel=preload as=image>` will start a new image preload that can be
2256 // satisfied from http cache or network.
2258 // There is a spec discussion for "preload cache", see
2259 // https://github.com/w3c/preload/issues/97. And it is also not clear how
2260 // preload image interacts with list of available images, see
2261 // https://github.com/whatwg/html/issues/4474.
2262 proxy
->RemoveSelf(aLoadingDocument
);
2263 proxy
->NotifyUsage();
2265 imgRequest
* request
= proxy
->GetOwner();
2267 CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
2268 aObserver
, requestFlags
, _retval
);
2269 NS_ENSURE_SUCCESS(rv
, rv
);
2271 imgRequestProxy
* newProxy
= *_retval
;
2272 if (imgCacheValidator
* validator
= request
->GetValidator()) {
2273 newProxy
->MarkValidating();
2274 // Attach the proxy without notifying and this will add us to the load
2276 validator
->AddProxy(newProxy
);
2278 // It's OK to add here even if the request is done. If it is, it'll send
2279 // a OnStopRequest()and the proxy will be removed from the loadgroup in
2280 // imgRequestProxy::OnLoadComplete.
2281 newProxy
->AddToLoadGroup();
2282 newProxy
->NotifyListener();
2289 RefPtr
<imgCacheEntry
> entry
;
2291 // Look in the cache for our URI, and then validate it.
2292 // XXX For now ignore aCacheKey. We will need it in the future
2293 // for correctly dealing with image load requests that are a result
2295 OriginAttributes attrs
;
2296 if (aTriggeringPrincipal
) {
2297 attrs
= aTriggeringPrincipal
->OriginAttributesRef();
2299 ImageCacheKey
key(aURI
, attrs
, aLoadingDocument
);
2300 imgCacheTable
& cache
= GetCache(key
);
2302 if (cache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
2303 bool newChannelCreated
= false;
2304 if (ValidateEntry(entry
, aURI
, aInitialDocumentURI
, aReferrerInfo
,
2305 aLoadGroup
, aObserver
, aLoadingDocument
, requestFlags
,
2306 aContentPolicyType
, true, &newChannelCreated
, _retval
,
2307 aTriggeringPrincipal
, corsmode
, aLinkPreload
)) {
2308 request
= entry
->GetRequest();
2310 // If this entry has no proxies, its request has no reference to the
2312 if (entry
->HasNoProxies()) {
2313 LOG_FUNC_WITH_PARAM(gImgLog
,
2314 "imgLoader::LoadImage() adding proxyless entry",
2316 MOZ_ASSERT(!request
->HasCacheEntry(),
2317 "Proxyless entry's request has cache entry!");
2318 request
->SetCacheEntry(entry
);
2320 if (mCacheTracker
&& entry
->GetExpirationState()->IsTracked()) {
2321 mCacheTracker
->MarkUsed(entry
);
2327 if (!newChannelCreated
) {
2328 // This is ugly but it's needed to report CSP violations. We have 3
2330 // - we don't have cache. We are not in this if() stmt. A new channel is
2331 // created and that triggers the CSP checks.
2332 // - We have a cache entry and this is blocked by CSP directives.
2333 DebugOnly
<bool> shouldLoad
= ShouldLoadCachedImage(
2334 request
, aLoadingDocument
, aTriggeringPrincipal
, aContentPolicyType
,
2335 /* aSendCSPViolationReports */ true);
2336 MOZ_ASSERT(shouldLoad
);
2339 // We can't use this entry. We'll try to load it off the network, and if
2340 // successful, overwrite the old entry in the cache with a new one.
2345 // Keep the channel in this scope, so we can adjust its notificationCallbacks
2346 // later when we create the proxy.
2347 nsCOMPtr
<nsIChannel
> newChannel
;
2348 // If we didn't get a cache hit, we need to load from the network.
2350 LOG_SCOPE(gImgLog
, "imgLoader::LoadImage |cache miss|");
2352 bool forcePrincipalCheck
;
2353 rv
= NewImageChannel(getter_AddRefs(newChannel
), &forcePrincipalCheck
, aURI
,
2354 aInitialDocumentURI
, corsmode
, aReferrerInfo
,
2355 aLoadGroup
, requestFlags
, aContentPolicyType
,
2356 aTriggeringPrincipal
, aContext
, mRespectPrivacy
);
2357 if (NS_FAILED(rv
)) {
2358 return NS_ERROR_FAILURE
;
2361 MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel
) == mRespectPrivacy
);
2363 NewRequestAndEntry(forcePrincipalCheck
, this, key
, getter_AddRefs(request
),
2364 getter_AddRefs(entry
));
2366 MOZ_LOG(gImgLog
, LogLevel::Debug
,
2367 ("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
2369 this, request
.get()));
2371 nsCOMPtr
<nsIClassOfService
> cos(do_QueryInterface(newChannel
));
2373 if (aUseUrgentStartForChannel
&& !aLinkPreload
) {
2374 cos
->AddClassFlags(nsIClassOfService::UrgentStart
);
2377 if (StaticPrefs::network_http_tailing_enabled() &&
2378 aContentPolicyType
== nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON
) {
2379 cos
->AddClassFlags(nsIClassOfService::Throttleable
|
2380 nsIClassOfService::Tail
);
2381 nsCOMPtr
<nsIHttpChannel
> httpChannel(do_QueryInterface(newChannel
));
2383 Unused
<< httpChannel
->SetRequestContextID(aRequestContextID
);
2388 nsCOMPtr
<nsILoadGroup
> channelLoadGroup
;
2389 newChannel
->GetLoadGroup(getter_AddRefs(channelLoadGroup
));
2390 rv
= request
->Init(aURI
, aURI
, /* aHadInsecureRedirect = */ false,
2391 channelLoadGroup
, newChannel
, entry
, aLoadingDocument
,
2392 aTriggeringPrincipal
, corsmode
, aReferrerInfo
);
2393 if (NS_FAILED(rv
)) {
2394 return NS_ERROR_FAILURE
;
2397 // Add the initiator type for this image load
2398 nsCOMPtr
<nsITimedChannel
> timedChannel
= do_QueryInterface(newChannel
);
2400 timedChannel
->SetInitiatorType(initiatorType
);
2403 // create the proxy listener
2404 nsCOMPtr
<nsIStreamListener
> listener
= new ProxyListener(request
.get());
2406 MOZ_LOG(gImgLog
, LogLevel::Debug
,
2407 ("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
2410 mozilla::net::PredictorLearn(aURI
, aInitialDocumentURI
,
2411 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE
,
2414 nsresult openRes
= newChannel
->AsyncOpen(listener
);
2416 if (NS_FAILED(openRes
)) {
2418 gImgLog
, LogLevel::Debug
,
2419 ("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
2421 this, static_cast<uint32_t>(openRes
)));
2422 request
->CancelAndAbort(openRes
);
2426 // Try to add the new request into the cache.
2427 PutIntoCache(key
, entry
);
2429 LOG_MSG_WITH_PARAM(gImgLog
, "imgLoader::LoadImage |cache hit|", "request",
2433 // If we didn't get a proxy when validating the cache entry, we need to
2436 // ValidateEntry() has three return values: "Is valid," "might be valid --
2437 // validating over network", and "not valid." If we don't have a _retval,
2438 // we know ValidateEntry is not validating over the network, so it's safe
2439 // to SetLoadId here because we know this request is valid for this context.
2441 // Note, however, that this doesn't guarantee the behaviour we want (one
2442 // URL maps to the same image on a page) if we load the same image in a
2443 // different tab (see bug 528003), because its load id will get re-set, and
2444 // that'll cause us to validate over the network.
2445 request
->SetLoadId(aLoadingDocument
);
2447 LOG_MSG(gImgLog
, "imgLoader::LoadImage", "creating proxy request.");
2448 rv
= CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
2449 aObserver
, requestFlags
, _retval
);
2450 if (NS_FAILED(rv
)) {
2454 imgRequestProxy
* proxy
= *_retval
;
2456 // Make sure that OnStatus/OnProgress calls have the right request set, if
2457 // we did create a channel here.
2459 nsCOMPtr
<nsIInterfaceRequestor
> requestor(
2460 new nsProgressNotificationProxy(newChannel
, proxy
));
2462 return NS_ERROR_OUT_OF_MEMORY
;
2464 newChannel
->SetNotificationCallbacks(requestor
);
2468 MOZ_ASSERT(aLoadingDocument
);
2469 proxy
->PrioritizeAsPreload();
2470 auto preloadKey
= PreloadHashKey::CreateAsImage(
2471 aURI
, aTriggeringPrincipal
, ConvertToCORSMode(corsmode
));
2472 proxy
->NotifyOpen(preloadKey
, aLoadingDocument
, true);
2475 // Note that it's OK to add here even if the request is done. If it is,
2476 // it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
2477 // the proxy will be removed from the loadgroup.
2478 proxy
->AddToLoadGroup();
2480 // If we're loading off the network, explicitly don't notify our proxy,
2481 // because necko (or things called from necko, such as imgCacheValidator)
2482 // are going to call our notifications asynchronously, and we can't make it
2483 // further asynchronous because observers might rely on imagelib completing
2484 // its work between the channel's OnStartRequest and OnStopRequest.
2486 proxy
->NotifyListener();
2492 NS_ASSERTION(*_retval
, "imgLoader::LoadImage -- no return value");
2498 imgLoader::LoadImageWithChannelXPCOM(nsIChannel
* channel
,
2499 imgINotificationObserver
* aObserver
,
2500 Document
* aLoadingDocument
,
2501 nsIStreamListener
** listener
,
2502 imgIRequest
** _retval
) {
2504 imgRequestProxy
* proxy
;
2505 result
= LoadImageWithChannel(channel
, aObserver
, aLoadingDocument
, listener
,
2511 nsresult
imgLoader::LoadImageWithChannel(nsIChannel
* channel
,
2512 imgINotificationObserver
* aObserver
,
2513 Document
* aLoadingDocument
,
2514 nsIStreamListener
** listener
,
2515 imgRequestProxy
** _retval
) {
2516 NS_ASSERTION(channel
,
2517 "imgLoader::LoadImageWithChannel -- NULL channel pointer");
2519 MOZ_ASSERT(NS_UsePrivateBrowsing(channel
) == mRespectPrivacy
);
2521 auto makeStaticIfNeeded
= mozilla::MakeScopeExit(
2522 [&] { MakeRequestStaticIfNeeded(aLoadingDocument
, _retval
); });
2524 LOG_SCOPE(gImgLog
, "imgLoader::LoadImageWithChannel");
2525 RefPtr
<imgRequest
> request
;
2527 nsCOMPtr
<nsIURI
> uri
;
2528 channel
->GetURI(getter_AddRefs(uri
));
2530 NS_ENSURE_TRUE(channel
, NS_ERROR_FAILURE
);
2531 nsCOMPtr
<nsILoadInfo
> loadInfo
= channel
->LoadInfo();
2533 OriginAttributes attrs
= loadInfo
->GetOriginAttributes();
2535 ImageCacheKey
key(uri
, attrs
, aLoadingDocument
);
2537 nsLoadFlags requestFlags
= nsIRequest::LOAD_NORMAL
;
2538 channel
->GetLoadFlags(&requestFlags
);
2540 if (PreferLoadFromCache(uri
)) {
2541 requestFlags
|= nsIRequest::LOAD_FROM_CACHE
;
2544 RefPtr
<imgCacheEntry
> entry
;
2546 if (requestFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
2547 RemoveFromCache(key
);
2549 // Look in the cache for our URI, and then validate it.
2550 // XXX For now ignore aCacheKey. We will need it in the future
2551 // for correctly dealing with image load requests that are a result
2553 imgCacheTable
& cache
= GetCache(key
);
2554 if (cache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
2555 // We don't want to kick off another network load. So we ask
2556 // ValidateEntry to only do validation without creating a new proxy. If
2557 // it says that the entry isn't valid any more, we'll only use the entry
2558 // we're getting if the channel is loading from the cache anyways.
2560 // XXX -- should this be changed? it's pretty much verbatim from the old
2561 // code, but seems nonsensical.
2563 // Since aCanMakeNewChannel == false, we don't need to pass content policy
2564 // type/principal/etc
2566 nsCOMPtr
<nsILoadInfo
> loadInfo
= channel
->LoadInfo();
2567 // if there is a loadInfo, use the right contentType, otherwise
2568 // default to the internal image type
2569 nsContentPolicyType policyType
= loadInfo
->InternalContentPolicyType();
2571 if (ValidateEntry(entry
, uri
, nullptr, nullptr, nullptr, aObserver
,
2572 aLoadingDocument
, requestFlags
, policyType
, false,
2573 nullptr, nullptr, nullptr, imgIRequest::CORS_NONE
,
2575 request
= entry
->GetRequest();
2577 nsCOMPtr
<nsICacheInfoChannel
> cacheChan(do_QueryInterface(channel
));
2581 cacheChan
->IsFromCache(&bUseCacheCopy
);
2583 bUseCacheCopy
= false;
2586 if (!bUseCacheCopy
) {
2589 request
= entry
->GetRequest();
2593 if (request
&& entry
) {
2594 // If this entry has no proxies, its request has no reference to
2596 if (entry
->HasNoProxies()) {
2597 LOG_FUNC_WITH_PARAM(
2599 "imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
2601 MOZ_ASSERT(!request
->HasCacheEntry(),
2602 "Proxyless entry's request has cache entry!");
2603 request
->SetCacheEntry(entry
);
2605 if (mCacheTracker
&& entry
->GetExpirationState()->IsTracked()) {
2606 mCacheTracker
->MarkUsed(entry
);
2613 nsCOMPtr
<nsILoadGroup
> loadGroup
;
2614 channel
->GetLoadGroup(getter_AddRefs(loadGroup
));
2617 if (aLoadingDocument
) {
2618 // The load group of the channel should always match that of the
2619 // document if given. If that isn't the case, then we need to add more
2620 // plumbing to ensure we block the document as well.
2621 nsCOMPtr
<nsILoadGroup
> docLoadGroup
=
2622 aLoadingDocument
->GetDocumentLoadGroup();
2623 MOZ_ASSERT(docLoadGroup
== loadGroup
);
2627 // Filter out any load flags not from nsIRequest
2628 requestFlags
&= nsIRequest::LOAD_REQUESTMASK
;
2630 nsresult rv
= NS_OK
;
2632 // we have this in our cache already.. cancel the current (document) load
2634 // this should fire an OnStopRequest
2635 channel
->Cancel(NS_ERROR_PARSED_DATA_CACHED
);
2637 *listener
= nullptr; // give them back a null nsIStreamListener
2639 rv
= CreateNewProxyForRequest(request
, uri
, loadGroup
, aLoadingDocument
,
2640 aObserver
, requestFlags
, _retval
);
2641 static_cast<imgRequestProxy
*>(*_retval
)->NotifyListener();
2643 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2644 nsCOMPtr
<nsIURI
> originalURI
;
2645 channel
->GetOriginalURI(getter_AddRefs(originalURI
));
2647 // XXX(seth): We should be able to just use |key| here, except that |key| is
2648 // constructed above with the *current URI* and not the *original URI*. I'm
2649 // pretty sure this is a bug, and it's preventing us from ever getting a
2650 // cache hit in LoadImageWithChannel when redirects are involved.
2651 ImageCacheKey
originalURIKey(originalURI
, attrs
, aLoadingDocument
);
2653 // Default to doing a principal check because we don't know who
2654 // started that load and whether their principal ended up being
2655 // inherited on the channel.
2656 NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
2657 originalURIKey
, getter_AddRefs(request
),
2658 getter_AddRefs(entry
));
2660 // No principal specified here, because we're not passed one.
2661 // In LoadImageWithChannel, the redirects that may have been
2662 // associated with this load would have gone through necko.
2663 // We only have the final URI in ImageLib and hence don't know
2664 // if the request went through insecure redirects. But if it did,
2665 // the necko cache should have handled that (since all necko cache hits
2666 // including the redirects will go through content policy). Hence, we
2667 // can set aHadInsecureRedirect to false here.
2668 rv
= request
->Init(originalURI
, uri
, /* aHadInsecureRedirect = */ false,
2669 channel
, channel
, entry
, aLoadingDocument
, nullptr,
2670 imgIRequest::CORS_NONE
, nullptr);
2671 NS_ENSURE_SUCCESS(rv
, rv
);
2673 RefPtr
<ProxyListener
> pl
=
2674 new ProxyListener(static_cast<nsIStreamListener
*>(request
.get()));
2675 pl
.forget(listener
);
2677 // Try to add the new request into the cache.
2678 PutIntoCache(originalURIKey
, entry
);
2680 rv
= CreateNewProxyForRequest(request
, originalURI
, loadGroup
,
2681 aLoadingDocument
, aObserver
, requestFlags
,
2684 // Explicitly don't notify our proxy, because we're loading off the
2685 // network, and necko (or things called from necko, such as
2686 // imgCacheValidator) are going to call our notifications asynchronously,
2687 // and we can't make it further asynchronous because observers might rely
2688 // on imagelib completing its work between the channel's OnStartRequest and
2692 if (NS_FAILED(rv
)) {
2696 (*_retval
)->AddToLoadGroup();
2700 bool imgLoader::SupportImageWithMimeType(const char* aMimeType
,
2701 AcceptedMimeTypes aAccept
2702 /* = AcceptedMimeTypes::IMAGES */) {
2703 nsAutoCString
mimeType(aMimeType
);
2704 ToLowerCase(mimeType
);
2706 if (aAccept
== AcceptedMimeTypes::IMAGES_AND_DOCUMENTS
&&
2707 mimeType
.EqualsLiteral("image/svg+xml")) {
2711 DecoderType type
= DecoderFactory::GetDecoderType(mimeType
.get());
2712 return type
!= DecoderType::UNKNOWN
;
2716 imgLoader::GetMIMETypeFromContent(nsIRequest
* aRequest
,
2717 const uint8_t* aContents
, uint32_t aLength
,
2718 nsACString
& aContentType
) {
2719 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
2721 nsCOMPtr
<nsILoadInfo
> loadInfo
= channel
->LoadInfo();
2722 if (loadInfo
->GetSkipContentSniffing()) {
2723 return NS_ERROR_NOT_AVAILABLE
;
2726 return GetMimeTypeFromContent((const char*)aContents
, aLength
, aContentType
);
2730 nsresult
imgLoader::GetMimeTypeFromContent(const char* aContents
,
2732 nsACString
& aContentType
) {
2733 nsAutoCString detected
;
2737 (!strncmp(aContents
, "GIF87a", 6) || !strncmp(aContents
, "GIF89a", 6))) {
2738 aContentType
.AssignLiteral(IMAGE_GIF
);
2741 } else if (aLength
>= 8 && ((unsigned char)aContents
[0] == 0x89 &&
2742 (unsigned char)aContents
[1] == 0x50 &&
2743 (unsigned char)aContents
[2] == 0x4E &&
2744 (unsigned char)aContents
[3] == 0x47 &&
2745 (unsigned char)aContents
[4] == 0x0D &&
2746 (unsigned char)aContents
[5] == 0x0A &&
2747 (unsigned char)aContents
[6] == 0x1A &&
2748 (unsigned char)aContents
[7] == 0x0A)) {
2749 aContentType
.AssignLiteral(IMAGE_PNG
);
2751 /* maybe a JPEG (JFIF)? */
2752 /* JFIF files start with SOI APP0 but older files can start with SOI DQT
2753 * so we test for SOI followed by any marker, i.e. FF D8 FF
2754 * this will also work for SPIFF JPEG files if they appear in the future.
2756 * (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
2758 } else if (aLength
>= 3 && ((unsigned char)aContents
[0]) == 0xFF &&
2759 ((unsigned char)aContents
[1]) == 0xD8 &&
2760 ((unsigned char)aContents
[2]) == 0xFF) {
2761 aContentType
.AssignLiteral(IMAGE_JPEG
);
2763 /* or how about ART? */
2764 /* ART begins with JG (4A 47). Major version offset 2.
2765 * Minor version offset 3. Offset 4 must be nullptr.
2767 } else if (aLength
>= 5 && ((unsigned char)aContents
[0]) == 0x4a &&
2768 ((unsigned char)aContents
[1]) == 0x47 &&
2769 ((unsigned char)aContents
[4]) == 0x00) {
2770 aContentType
.AssignLiteral(IMAGE_ART
);
2772 } else if (aLength
>= 2 && !strncmp(aContents
, "BM", 2)) {
2773 aContentType
.AssignLiteral(IMAGE_BMP
);
2775 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
2776 // CURs begin with 2-byte 0 followed by 2-byte 2.
2777 } else if (aLength
>= 4 && (!memcmp(aContents
, "\000\000\001\000", 4) ||
2778 !memcmp(aContents
, "\000\000\002\000", 4))) {
2779 aContentType
.AssignLiteral(IMAGE_ICO
);
2781 // WebPs always begin with RIFF, a 32-bit length, and WEBP.
2782 } else if (aLength
>= 12 && !memcmp(aContents
, "RIFF", 4) &&
2783 !memcmp(aContents
+ 8, "WEBP", 4)) {
2784 aContentType
.AssignLiteral(IMAGE_WEBP
);
2786 } else if (MatchesMP4(reinterpret_cast<const uint8_t*>(aContents
), aLength
,
2788 detected
.Equals(IMAGE_AVIF
)) {
2789 aContentType
.AssignLiteral(IMAGE_AVIF
);
2791 /* none of the above? I give up */
2792 return NS_ERROR_NOT_AVAILABLE
;
2799 * proxy stream listener class used to handle multipart/x-mixed-replace
2802 #include "nsIRequest.h"
2803 #include "nsIStreamConverterService.h"
2805 NS_IMPL_ISUPPORTS(ProxyListener
, nsIStreamListener
,
2806 nsIThreadRetargetableStreamListener
, nsIRequestObserver
)
2808 ProxyListener::ProxyListener(nsIStreamListener
* dest
) : mDestListener(dest
) {
2809 /* member initializers and constructor code */
2812 ProxyListener::~ProxyListener() { /* destructor code */
2815 /** nsIRequestObserver methods **/
2818 ProxyListener::OnStartRequest(nsIRequest
* aRequest
) {
2819 if (!mDestListener
) {
2820 return NS_ERROR_FAILURE
;
2823 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
2825 // We need to set the initiator type for the image load
2826 nsCOMPtr
<nsITimedChannel
> timedChannel
= do_QueryInterface(channel
);
2829 timedChannel
->GetInitiatorType(type
);
2830 if (type
.IsEmpty()) {
2831 timedChannel
->SetInitiatorType(u
"img"_ns
);
2835 nsAutoCString contentType
;
2836 nsresult rv
= channel
->GetContentType(contentType
);
2838 if (!contentType
.IsEmpty()) {
2839 /* If multipart/x-mixed-replace content, we'll insert a MIME decoder
2840 in the pipeline to handle the content and pass it along to our
2843 if ("multipart/x-mixed-replace"_ns
.Equals(contentType
)) {
2844 nsCOMPtr
<nsIStreamConverterService
> convServ(
2845 do_GetService("@mozilla.org/streamConverters;1", &rv
));
2846 if (NS_SUCCEEDED(rv
)) {
2847 nsCOMPtr
<nsIStreamListener
> toListener(mDestListener
);
2848 nsCOMPtr
<nsIStreamListener
> fromListener
;
2850 rv
= convServ
->AsyncConvertData("multipart/x-mixed-replace", "*/*",
2851 toListener
, nullptr,
2852 getter_AddRefs(fromListener
));
2853 if (NS_SUCCEEDED(rv
)) {
2854 mDestListener
= fromListener
;
2861 return mDestListener
->OnStartRequest(aRequest
);
2865 ProxyListener::OnStopRequest(nsIRequest
* aRequest
, nsresult status
) {
2866 if (!mDestListener
) {
2867 return NS_ERROR_FAILURE
;
2870 return mDestListener
->OnStopRequest(aRequest
, status
);
2873 /** nsIStreamListener methods **/
2876 ProxyListener::OnDataAvailable(nsIRequest
* aRequest
, nsIInputStream
* inStr
,
2877 uint64_t sourceOffset
, uint32_t count
) {
2878 if (!mDestListener
) {
2879 return NS_ERROR_FAILURE
;
2882 return mDestListener
->OnDataAvailable(aRequest
, inStr
, sourceOffset
, count
);
2885 /** nsThreadRetargetableStreamListener methods **/
2887 ProxyListener::CheckListenerChain() {
2888 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
2889 nsresult rv
= NS_OK
;
2890 nsCOMPtr
<nsIThreadRetargetableStreamListener
> retargetableListener
=
2891 do_QueryInterface(mDestListener
, &rv
);
2892 if (retargetableListener
) {
2893 rv
= retargetableListener
->CheckListenerChain();
2896 gImgLog
, LogLevel::Debug
,
2897 ("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
2899 (NS_SUCCEEDED(rv
) ? "success" : "failure"), this,
2900 (nsIStreamListener
*)mDestListener
, static_cast<uint32_t>(rv
)));
2905 * http validate class. check a channel for a 304
2908 NS_IMPL_ISUPPORTS(imgCacheValidator
, nsIStreamListener
, nsIRequestObserver
,
2909 nsIThreadRetargetableStreamListener
, nsIChannelEventSink
,
2910 nsIInterfaceRequestor
, nsIAsyncVerifyRedirectCallback
)
2912 imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy
* progress
,
2913 imgLoader
* loader
, imgRequest
* request
,
2914 Document
* aDocument
,
2915 uint64_t aInnerWindowId
,
2916 bool forcePrincipalCheckForCacheEntry
)
2917 : mProgressProxy(progress
),
2919 mDocument(aDocument
),
2920 mInnerWindowId(aInnerWindowId
),
2922 mHadInsecureRedirect(false) {
2923 NewRequestAndEntry(forcePrincipalCheckForCacheEntry
, loader
,
2924 mRequest
->CacheKey(), getter_AddRefs(mNewRequest
),
2925 getter_AddRefs(mNewEntry
));
2928 imgCacheValidator::~imgCacheValidator() {
2930 // If something went wrong, and we never unblocked the requests waiting on
2931 // validation, now is our last chance. We will cancel the new request and
2932 // switch the waiting proxies to it.
2933 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
2937 void imgCacheValidator::AddProxy(imgRequestProxy
* aProxy
) {
2938 // aProxy needs to be in the loadgroup since we're validating from
2940 aProxy
->AddToLoadGroup();
2942 mProxies
.AppendElement(aProxy
);
2945 void imgCacheValidator::RemoveProxy(imgRequestProxy
* aProxy
) {
2946 mProxies
.RemoveElement(aProxy
);
2949 void imgCacheValidator::UpdateProxies(bool aCancelRequest
, bool aSyncNotify
) {
2950 MOZ_ASSERT(mRequest
);
2952 // Clear the validator before updating the proxies. The notifications may
2953 // clone an existing request, and its state could be inconsistent.
2954 mRequest
->SetValidator(nullptr);
2957 // If an error occurred, we will want to cancel the new request, and make the
2958 // validating proxies point to it. Any proxies still bound to the original
2959 // request which are not validating should remain untouched.
2960 if (aCancelRequest
) {
2961 MOZ_ASSERT(mNewRequest
);
2962 mNewRequest
->CancelAndAbort(NS_BINDING_ABORTED
);
2965 // We have finished validating the request, so we can safely take ownership
2966 // of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
2967 // if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
2968 // that any potential notifications should still be suppressed in
2969 // imgRequestProxy::ChangeOwner because we haven't cleared the validating
2970 // flag yet, and thus they will remain deferred.
2971 AutoTArray
<RefPtr
<imgRequestProxy
>, 4> proxies(std::move(mProxies
));
2973 for (auto& proxy
: proxies
) {
2974 // First update the state of all proxies before notifying any of them
2975 // to ensure a consistent state (e.g. in case the notification causes
2976 // other proxies to be touched indirectly.)
2977 MOZ_ASSERT(proxy
->IsValidating());
2978 MOZ_ASSERT(proxy
->NotificationsDeferred(),
2979 "Proxies waiting on cache validation should be "
2980 "deferring notifications!");
2982 proxy
->ChangeOwner(mNewRequest
);
2984 proxy
->ClearValidating();
2987 mNewRequest
= nullptr;
2988 mNewEntry
= nullptr;
2990 for (auto& proxy
: proxies
) {
2992 // Notify synchronously, because the caller knows we are already in an
2993 // asynchronously-called function (e.g. OnStartRequest).
2994 proxy
->SyncNotifyListener();
2996 // Notify asynchronously, because the caller does not know our current
2997 // call state (e.g. ~imgCacheValidator).
2998 proxy
->NotifyListener();
3003 /** nsIRequestObserver methods **/
3006 imgCacheValidator::OnStartRequest(nsIRequest
* aRequest
) {
3007 // We may be holding on to a document, so ensure that it's released.
3008 RefPtr
<Document
> document
= mDocument
.forget();
3010 // If for some reason we don't still have an existing request (probably
3011 // because OnStartRequest got delivered more than once), just bail.
3013 MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
3014 aRequest
->Cancel(NS_BINDING_ABORTED
);
3015 return NS_ERROR_FAILURE
;
3018 // If this request is coming from cache and has the same URI as our
3019 // imgRequest, the request all our proxies are pointing at is valid, and all
3020 // we have to do is tell them to notify their listeners.
3021 nsCOMPtr
<nsICacheInfoChannel
> cacheChan(do_QueryInterface(aRequest
));
3022 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
3023 if (cacheChan
&& channel
&& !mRequest
->CacheChanged(aRequest
)) {
3024 bool isFromCache
= false;
3025 cacheChan
->IsFromCache(&isFromCache
);
3027 nsCOMPtr
<nsIURI
> channelURI
;
3028 channel
->GetURI(getter_AddRefs(channelURI
));
3030 nsCOMPtr
<nsIURI
> finalURI
;
3031 mRequest
->GetFinalURI(getter_AddRefs(finalURI
));
3033 bool sameURI
= false;
3034 if (channelURI
&& finalURI
) {
3035 channelURI
->Equals(finalURI
, &sameURI
);
3038 if (isFromCache
&& sameURI
) {
3039 // We don't need to load this any more.
3040 aRequest
->Cancel(NS_BINDING_ABORTED
);
3041 mNewRequest
= nullptr;
3043 // Clear the validator before updating the proxies. The notifications may
3044 // clone an existing request, and its state could be inconsistent.
3045 mRequest
->SetLoadId(document
);
3046 mRequest
->SetInnerWindowID(mInnerWindowId
);
3047 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3052 // We can't load out of cache. We have to create a whole new request for the
3053 // data that's coming in off the channel.
3054 nsCOMPtr
<nsIURI
> uri
;
3055 mRequest
->GetURI(getter_AddRefs(uri
));
3057 LOG_MSG_WITH_PARAM(gImgLog
,
3058 "imgCacheValidator::OnStartRequest creating new request",
3061 int32_t corsmode
= mRequest
->GetCORSMode();
3062 nsCOMPtr
<nsIReferrerInfo
> referrerInfo
= mRequest
->GetReferrerInfo();
3063 nsCOMPtr
<nsIPrincipal
> triggeringPrincipal
=
3064 mRequest
->GetTriggeringPrincipal();
3066 // Doom the old request's cache entry
3067 mRequest
->RemoveFromCache();
3069 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
3070 nsCOMPtr
<nsIURI
> originalURI
;
3071 channel
->GetOriginalURI(getter_AddRefs(originalURI
));
3072 nsresult rv
= mNewRequest
->Init(originalURI
, uri
, mHadInsecureRedirect
,
3073 aRequest
, channel
, mNewEntry
, document
,
3074 triggeringPrincipal
, corsmode
, referrerInfo
);
3075 if (NS_FAILED(rv
)) {
3076 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
3080 mDestListener
= new ProxyListener(mNewRequest
);
3082 // Try to add the new request into the cache. Note that the entry must be in
3083 // the cache before the proxies' ownership changes, because adding a proxy
3084 // changes the caching behaviour for imgRequests.
3085 mImgLoader
->PutIntoCache(mNewRequest
->CacheKey(), mNewEntry
);
3086 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3087 return mDestListener
->OnStartRequest(aRequest
);
3091 imgCacheValidator::OnStopRequest(nsIRequest
* aRequest
, nsresult status
) {
3092 // Be sure we've released the document that we may have been holding on to.
3093 mDocument
= nullptr;
3095 if (!mDestListener
) {
3099 return mDestListener
->OnStopRequest(aRequest
, status
);
3102 /** nsIStreamListener methods **/
3105 imgCacheValidator::OnDataAvailable(nsIRequest
* aRequest
, nsIInputStream
* inStr
,
3106 uint64_t sourceOffset
, uint32_t count
) {
3107 if (!mDestListener
) {
3108 // XXX see bug 113959
3110 inStr
->ReadSegments(NS_DiscardSegment
, nullptr, count
, &_retval
);
3114 return mDestListener
->OnDataAvailable(aRequest
, inStr
, sourceOffset
, count
);
3117 /** nsIThreadRetargetableStreamListener methods **/
3120 imgCacheValidator::CheckListenerChain() {
3121 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3122 nsresult rv
= NS_OK
;
3123 nsCOMPtr
<nsIThreadRetargetableStreamListener
> retargetableListener
=
3124 do_QueryInterface(mDestListener
, &rv
);
3125 if (retargetableListener
) {
3126 rv
= retargetableListener
->CheckListenerChain();
3129 gImgLog
, LogLevel::Debug
,
3130 ("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32
"=%s",
3131 this, static_cast<uint32_t>(rv
),
3132 NS_SUCCEEDED(rv
) ? "succeeded" : "failed"));
3136 /** nsIInterfaceRequestor methods **/
3139 imgCacheValidator::GetInterface(const nsIID
& aIID
, void** aResult
) {
3140 if (aIID
.Equals(NS_GET_IID(nsIChannelEventSink
))) {
3141 return QueryInterface(aIID
, aResult
);
3144 return mProgressProxy
->GetInterface(aIID
, aResult
);
3147 // These functions are materially the same as the same functions in imgRequest.
3148 // We duplicate them because we're verifying whether cache loads are necessary,
3149 // not unconditionally loading.
3151 /** nsIChannelEventSink methods **/
3153 imgCacheValidator::AsyncOnChannelRedirect(
3154 nsIChannel
* oldChannel
, nsIChannel
* newChannel
, uint32_t flags
,
3155 nsIAsyncVerifyRedirectCallback
* callback
) {
3156 // Note all cache information we get from the old channel.
3157 mNewRequest
->SetCacheValidation(mNewEntry
, oldChannel
);
3159 // If the previous URI is a non-HTTPS URI, record that fact for later use by
3160 // security code, which needs to know whether there is an insecure load at any
3161 // point in the redirect chain.
3162 nsCOMPtr
<nsIURI
> oldURI
;
3163 bool schemeLocal
= false;
3164 if (NS_FAILED(oldChannel
->GetURI(getter_AddRefs(oldURI
))) ||
3165 NS_FAILED(NS_URIChainHasFlags(
3166 oldURI
, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE
, &schemeLocal
)) ||
3167 (!oldURI
->SchemeIs("https") && !oldURI
->SchemeIs("chrome") &&
3169 mHadInsecureRedirect
= true;
3172 // Prepare for callback
3173 mRedirectCallback
= callback
;
3174 mRedirectChannel
= newChannel
;
3176 return mProgressProxy
->AsyncOnChannelRedirect(oldChannel
, newChannel
, flags
,
3181 imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult
) {
3182 // If we've already been told to abort, just do so.
3183 if (NS_FAILED(aResult
)) {
3184 mRedirectCallback
->OnRedirectVerifyCallback(aResult
);
3185 mRedirectCallback
= nullptr;
3186 mRedirectChannel
= nullptr;
3190 // make sure we have a protocol that returns data rather than opens
3191 // an external application, e.g. mailto:
3192 nsCOMPtr
<nsIURI
> uri
;
3193 mRedirectChannel
->GetURI(getter_AddRefs(uri
));
3194 bool doesNotReturnData
= false;
3195 NS_URIChainHasFlags(uri
, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA
,
3196 &doesNotReturnData
);
3198 nsresult result
= NS_OK
;
3200 if (doesNotReturnData
) {
3201 result
= NS_ERROR_ABORT
;
3204 mRedirectCallback
->OnRedirectVerifyCallback(result
);
3205 mRedirectCallback
= nullptr;
3206 mRedirectChannel
= nullptr;