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/StaticPrefs_image.h"
29 #include "mozilla/StaticPrefs_network.h"
30 #include "mozilla/dom/ContentParent.h"
31 #include "mozilla/dom/nsMixedContentBlocker.h"
32 #include "mozilla/image/ImageMemoryReporter.h"
33 #include "mozilla/layers/CompositorManagerChild.h"
36 #include "nsComponentManagerUtils.h"
37 #include "nsContentPolicyUtils.h"
38 #include "nsContentUtils.h"
39 #include "nsIApplicationCache.h"
40 #include "nsIApplicationCacheContainer.h"
41 #include "nsIAsyncVerifyRedirectCallback.h"
42 #include "nsICacheInfoChannel.h"
43 #include "nsIChannelEventSink.h"
44 #include "nsIClassOfService.h"
46 #include "nsIFileURL.h"
47 #include "nsIHttpChannel.h"
48 #include "nsIInterfaceRequestor.h"
49 #include "nsIInterfaceRequestorUtils.h"
50 #include "nsIMemoryReporter.h"
51 #include "nsINetworkPredictor.h"
52 #include "nsIProgressEventSink.h"
53 #include "nsIProtocolHandler.h"
54 #include "nsImageModule.h"
55 #include "nsMediaSniffer.h"
56 #include "nsMimeTypes.h"
58 #include "nsNetUtil.h"
59 #include "nsProxyRelease.h"
60 #include "nsQueryObject.h"
61 #include "nsReadableUtils.h"
62 #include "nsStreamUtils.h"
65 // we want to explore making the document own the load group
66 // so we can associate the document URI with the load group.
67 // until this point, we have an evil hack:
68 #include "nsIHttpChannelInternal.h"
69 #include "nsILoadGroupChild.h"
70 #include "nsIDocShell.h"
72 using namespace mozilla
;
73 using namespace mozilla::dom
;
74 using namespace mozilla::image
;
75 using namespace mozilla::net
;
77 MOZ_DEFINE_MALLOC_SIZE_OF(ImagesMallocSizeOf
)
79 class imgMemoryReporter final
: public nsIMemoryReporter
{
80 ~imgMemoryReporter() = default;
85 NS_IMETHOD
CollectReports(nsIHandleReportCallback
* aHandleReport
,
86 nsISupports
* aData
, bool aAnonymize
) override
{
87 MOZ_ASSERT(NS_IsMainThread());
89 layers::CompositorManagerChild
* manager
=
90 mozilla::layers::CompositorManagerChild::GetInstance();
91 if (!manager
|| !StaticPrefs::image_mem_debug_reporting()) {
92 layers::SharedSurfacesMemoryReport sharedSurfaces
;
93 FinishCollectReports(aHandleReport
, aData
, aAnonymize
, sharedSurfaces
);
97 RefPtr
<imgMemoryReporter
> self(this);
98 nsCOMPtr
<nsIHandleReportCallback
> handleReport(aHandleReport
);
99 nsCOMPtr
<nsISupports
> data(aData
);
100 manager
->SendReportSharedSurfacesMemory(
101 [=](layers::SharedSurfacesMemoryReport aReport
) {
102 self
->FinishCollectReports(handleReport
, data
, aAnonymize
, aReport
);
104 [=](mozilla::ipc::ResponseRejectReason
&& aReason
) {
105 layers::SharedSurfacesMemoryReport sharedSurfaces
;
106 self
->FinishCollectReports(handleReport
, data
, aAnonymize
,
112 void FinishCollectReports(
113 nsIHandleReportCallback
* aHandleReport
, nsISupports
* aData
,
114 bool aAnonymize
, layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
115 nsTArray
<ImageMemoryCounter
> chrome
;
116 nsTArray
<ImageMemoryCounter
> content
;
117 nsTArray
<ImageMemoryCounter
> uncached
;
119 for (uint32_t i
= 0; i
< mKnownLoaders
.Length(); i
++) {
120 for (auto iter
= mKnownLoaders
[i
]->mChromeCache
.Iter(); !iter
.Done();
122 imgCacheEntry
* entry
= iter
.UserData();
123 RefPtr
<imgRequest
> req
= entry
->GetRequest();
124 RecordCounterForRequest(req
, &chrome
, !entry
->HasNoProxies());
126 for (auto iter
= mKnownLoaders
[i
]->mCache
.Iter(); !iter
.Done();
128 imgCacheEntry
* entry
= iter
.UserData();
129 RefPtr
<imgRequest
> req
= entry
->GetRequest();
130 RecordCounterForRequest(req
, &content
, !entry
->HasNoProxies());
132 MutexAutoLock
lock(mKnownLoaders
[i
]->mUncachedImagesMutex
);
133 for (auto iter
= mKnownLoaders
[i
]->mUncachedImages
.Iter(); !iter
.Done();
135 nsPtrHashKey
<imgRequest
>* entry
= iter
.Get();
136 RefPtr
<imgRequest
> req
= entry
->GetKey();
137 RecordCounterForRequest(req
, &uncached
, req
->HasConsumers());
141 // Note that we only need to anonymize content image URIs.
143 ReportCounterArray(aHandleReport
, aData
, chrome
, "images/chrome",
144 /* aAnonymize */ false, aSharedSurfaces
);
146 ReportCounterArray(aHandleReport
, aData
, content
, "images/content",
147 aAnonymize
, aSharedSurfaces
);
149 // Uncached images may be content or chrome, so anonymize them.
150 ReportCounterArray(aHandleReport
, aData
, uncached
, "images/uncached",
151 aAnonymize
, aSharedSurfaces
);
153 // Report any shared surfaces that were not merged with the surface cache.
154 ImageMemoryReporter::ReportSharedSurfaces(aHandleReport
, aData
,
157 nsCOMPtr
<nsIMemoryReporterManager
> imgr
=
158 do_GetService("@mozilla.org/memory-reporter-manager;1");
164 static int64_t ImagesContentUsedUncompressedDistinguishedAmount() {
166 for (uint32_t i
= 0; i
< imgLoader::sMemReporter
->mKnownLoaders
.Length();
168 for (auto iter
= imgLoader::sMemReporter
->mKnownLoaders
[i
]->mCache
.Iter();
169 !iter
.Done(); iter
.Next()) {
170 imgCacheEntry
* entry
= iter
.UserData();
171 if (entry
->HasNoProxies()) {
175 RefPtr
<imgRequest
> req
= entry
->GetRequest();
176 RefPtr
<image::Image
> image
= req
->GetImage();
181 // Both this and EntryImageSizes measure
182 // images/content/raster/used/decoded memory. This function's
183 // measurement is secondary -- the result doesn't go in the "explicit"
184 // tree -- so we use moz_malloc_size_of instead of ImagesMallocSizeOf to
185 // prevent DMD from seeing it reported twice.
186 SizeOfState
state(moz_malloc_size_of
);
187 ImageMemoryCounter
counter(req
, image
, state
, /* aIsUsed = */ true);
189 n
+= counter
.Values().DecodedHeap();
190 n
+= counter
.Values().DecodedNonHeap();
191 n
+= counter
.Values().DecodedUnknown();
197 void RegisterLoader(imgLoader
* aLoader
) {
198 mKnownLoaders
.AppendElement(aLoader
);
201 void UnregisterLoader(imgLoader
* aLoader
) {
202 mKnownLoaders
.RemoveElement(aLoader
);
206 nsTArray
<imgLoader
*> mKnownLoaders
;
209 MemoryTotal
& operator+=(const ImageMemoryCounter
& aImageCounter
) {
210 if (aImageCounter
.Type() == imgIContainer::TYPE_RASTER
) {
211 if (aImageCounter
.IsUsed()) {
212 mUsedRasterCounter
+= aImageCounter
.Values();
214 mUnusedRasterCounter
+= aImageCounter
.Values();
216 } else if (aImageCounter
.Type() == imgIContainer::TYPE_VECTOR
) {
217 if (aImageCounter
.IsUsed()) {
218 mUsedVectorCounter
+= aImageCounter
.Values();
220 mUnusedVectorCounter
+= aImageCounter
.Values();
222 } else if (aImageCounter
.Type() == imgIContainer::TYPE_REQUEST
) {
223 // Nothing to do, we did not get to the point of having an image.
225 MOZ_CRASH("Unexpected image type");
231 const MemoryCounter
& UsedRaster() const { return mUsedRasterCounter
; }
232 const MemoryCounter
& UnusedRaster() const { return mUnusedRasterCounter
; }
233 const MemoryCounter
& UsedVector() const { return mUsedVectorCounter
; }
234 const MemoryCounter
& UnusedVector() const { return mUnusedVectorCounter
; }
237 MemoryCounter mUsedRasterCounter
;
238 MemoryCounter mUnusedRasterCounter
;
239 MemoryCounter mUsedVectorCounter
;
240 MemoryCounter mUnusedVectorCounter
;
243 // Reports all images of a single kind, e.g. all used chrome images.
244 void ReportCounterArray(nsIHandleReportCallback
* aHandleReport
,
246 nsTArray
<ImageMemoryCounter
>& aCounterArray
,
247 const char* aPathPrefix
, bool aAnonymize
,
248 layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
249 MemoryTotal summaryTotal
;
250 MemoryTotal nonNotableTotal
;
252 // Report notable images, and compute total and non-notable aggregate sizes.
253 for (uint32_t i
= 0; i
< aCounterArray
.Length(); i
++) {
254 ImageMemoryCounter
& counter
= aCounterArray
[i
];
257 counter
.URI().Truncate();
258 counter
.URI().AppendPrintf("<anonymized-%u>", i
);
260 // The URI could be an extremely long data: URI. Truncate if needed.
261 static const size_t max
= 256;
262 if (counter
.URI().Length() > max
) {
263 counter
.URI().Truncate(max
);
264 counter
.URI().AppendLiteral(" (truncated)");
266 counter
.URI().ReplaceChar('/', '\\');
269 summaryTotal
+= counter
;
271 if (counter
.IsNotable() || StaticPrefs::image_mem_debug_reporting()) {
272 ReportImage(aHandleReport
, aData
, aPathPrefix
, counter
,
275 ImageMemoryReporter::TrimSharedSurfaces(counter
, aSharedSurfaces
);
276 nonNotableTotal
+= counter
;
280 // Report non-notable images in aggregate.
281 ReportTotal(aHandleReport
, aData
, /* aExplicit = */ true, aPathPrefix
,
282 "<non-notable images>/", nonNotableTotal
);
284 // Report a summary in aggregate, outside of the explicit tree.
285 ReportTotal(aHandleReport
, aData
, /* aExplicit = */ false, aPathPrefix
, "",
289 static void ReportImage(nsIHandleReportCallback
* aHandleReport
,
290 nsISupports
* aData
, const char* aPathPrefix
,
291 const ImageMemoryCounter
& aCounter
,
292 layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
293 nsAutoCString
pathPrefix("explicit/"_ns
);
294 pathPrefix
.Append(aPathPrefix
);
296 switch (aCounter
.Type()) {
297 case imgIContainer::TYPE_RASTER
:
298 pathPrefix
.AppendLiteral("/raster/");
300 case imgIContainer::TYPE_VECTOR
:
301 pathPrefix
.AppendLiteral("/vector/");
303 case imgIContainer::TYPE_REQUEST
:
304 pathPrefix
.AppendLiteral("/request/");
307 pathPrefix
.AppendLiteral("/unknown=");
308 pathPrefix
.AppendInt(aCounter
.Type());
309 pathPrefix
.AppendLiteral("/");
313 pathPrefix
.Append(aCounter
.IsUsed() ? "used/" : "unused/");
314 if (aCounter
.IsValidating()) {
315 pathPrefix
.AppendLiteral("validating/");
317 if (aCounter
.HasError()) {
318 pathPrefix
.AppendLiteral("err/");
321 pathPrefix
.AppendLiteral("progress=");
322 pathPrefix
.AppendInt(aCounter
.Progress(), 16);
323 pathPrefix
.AppendLiteral("/");
325 pathPrefix
.AppendLiteral("image(");
326 pathPrefix
.AppendInt(aCounter
.IntrinsicSize().width
);
327 pathPrefix
.AppendLiteral("x");
328 pathPrefix
.AppendInt(aCounter
.IntrinsicSize().height
);
329 pathPrefix
.AppendLiteral(", ");
331 if (aCounter
.URI().IsEmpty()) {
332 pathPrefix
.AppendLiteral("<unknown URI>");
334 pathPrefix
.Append(aCounter
.URI());
337 pathPrefix
.AppendLiteral(")/");
339 ReportSurfaces(aHandleReport
, aData
, pathPrefix
, aCounter
, aSharedSurfaces
);
341 ReportSourceValue(aHandleReport
, aData
, pathPrefix
, aCounter
.Values());
344 static void ReportSurfaces(
345 nsIHandleReportCallback
* aHandleReport
, nsISupports
* aData
,
346 const nsACString
& aPathPrefix
, const ImageMemoryCounter
& aCounter
,
347 layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
348 for (const SurfaceMemoryCounter
& counter
: aCounter
.Surfaces()) {
349 nsAutoCString
surfacePathPrefix(aPathPrefix
);
350 if (counter
.IsLocked()) {
351 surfacePathPrefix
.AppendLiteral("locked/");
353 surfacePathPrefix
.AppendLiteral("unlocked/");
355 if (counter
.IsFactor2()) {
356 surfacePathPrefix
.AppendLiteral("factor2/");
358 if (counter
.CannotSubstitute()) {
359 surfacePathPrefix
.AppendLiteral("cannot_substitute/");
361 surfacePathPrefix
.AppendLiteral("types=");
362 surfacePathPrefix
.AppendInt(counter
.Values().SurfaceTypes(), 16);
363 surfacePathPrefix
.AppendLiteral("/surface(");
364 surfacePathPrefix
.AppendInt(counter
.Key().Size().width
);
365 surfacePathPrefix
.AppendLiteral("x");
366 surfacePathPrefix
.AppendInt(counter
.Key().Size().height
);
368 if (!counter
.IsFinished()) {
369 surfacePathPrefix
.AppendLiteral(", incomplete");
372 if (counter
.Values().ExternalHandles() > 0) {
373 surfacePathPrefix
.AppendLiteral(", handles:");
374 surfacePathPrefix
.AppendInt(
375 uint32_t(counter
.Values().ExternalHandles()));
378 ImageMemoryReporter::AppendSharedSurfacePrefix(surfacePathPrefix
, counter
,
381 if (counter
.Type() == SurfaceMemoryCounterType::NORMAL
) {
382 PlaybackType playback
= counter
.Key().Playback();
383 if (playback
== PlaybackType::eAnimated
) {
384 if (StaticPrefs::image_mem_debug_reporting()) {
385 surfacePathPrefix
.AppendPrintf(
386 " (animation %4u)", uint32_t(counter
.Values().FrameIndex()));
388 surfacePathPrefix
.AppendLiteral(" (animation)");
392 if (counter
.Key().Flags() != DefaultSurfaceFlags()) {
393 surfacePathPrefix
.AppendLiteral(", flags:");
394 surfacePathPrefix
.AppendInt(uint32_t(counter
.Key().Flags()),
398 if (counter
.Key().SVGContext()) {
399 const SVGImageContext
& context
= counter
.Key().SVGContext().ref();
400 surfacePathPrefix
.AppendLiteral(", svgContext:[ ");
401 if (context
.GetViewportSize()) {
402 const CSSIntSize
& size
= context
.GetViewportSize().ref();
403 surfacePathPrefix
.AppendLiteral("viewport=(");
404 surfacePathPrefix
.AppendInt(size
.width
);
405 surfacePathPrefix
.AppendLiteral("x");
406 surfacePathPrefix
.AppendInt(size
.height
);
407 surfacePathPrefix
.AppendLiteral(") ");
409 if (context
.GetPreserveAspectRatio()) {
411 context
.GetPreserveAspectRatio()->ToString(aspect
);
412 surfacePathPrefix
.AppendLiteral("preserveAspectRatio=(");
413 LossyAppendUTF16toASCII(aspect
, surfacePathPrefix
);
414 surfacePathPrefix
.AppendLiteral(") ");
416 if (context
.GetContextPaint()) {
417 const SVGEmbeddingContextPaint
* paint
= context
.GetContextPaint();
418 surfacePathPrefix
.AppendLiteral("contextPaint=(");
419 if (paint
->GetFill()) {
420 surfacePathPrefix
.AppendLiteral(" fill=");
421 surfacePathPrefix
.AppendInt(paint
->GetFill()->ToABGR(), 16);
423 if (paint
->GetFillOpacity()) {
424 surfacePathPrefix
.AppendLiteral(" fillOpa=");
425 surfacePathPrefix
.AppendFloat(paint
->GetFillOpacity());
427 if (paint
->GetStroke()) {
428 surfacePathPrefix
.AppendLiteral(" stroke=");
429 surfacePathPrefix
.AppendInt(paint
->GetStroke()->ToABGR(), 16);
431 if (paint
->GetStrokeOpacity()) {
432 surfacePathPrefix
.AppendLiteral(" strokeOpa=");
433 surfacePathPrefix
.AppendFloat(paint
->GetStrokeOpacity());
435 surfacePathPrefix
.AppendLiteral(" ) ");
437 surfacePathPrefix
.AppendLiteral("]");
439 } else if (counter
.Type() == SurfaceMemoryCounterType::COMPOSITING
) {
440 surfacePathPrefix
.AppendLiteral(", compositing frame");
441 } else if (counter
.Type() == SurfaceMemoryCounterType::COMPOSITING_PREV
) {
442 surfacePathPrefix
.AppendLiteral(", compositing prev frame");
444 MOZ_ASSERT_UNREACHABLE("Unknown counter type");
447 surfacePathPrefix
.AppendLiteral(")/");
449 ReportValues(aHandleReport
, aData
, surfacePathPrefix
, counter
.Values());
453 static void ReportTotal(nsIHandleReportCallback
* aHandleReport
,
454 nsISupports
* aData
, bool aExplicit
,
455 const char* aPathPrefix
, const char* aPathInfix
,
456 const MemoryTotal
& aTotal
) {
457 nsAutoCString pathPrefix
;
459 pathPrefix
.AppendLiteral("explicit/");
461 pathPrefix
.Append(aPathPrefix
);
463 nsAutoCString
rasterUsedPrefix(pathPrefix
);
464 rasterUsedPrefix
.AppendLiteral("/raster/used/");
465 rasterUsedPrefix
.Append(aPathInfix
);
466 ReportValues(aHandleReport
, aData
, rasterUsedPrefix
, aTotal
.UsedRaster());
468 nsAutoCString
rasterUnusedPrefix(pathPrefix
);
469 rasterUnusedPrefix
.AppendLiteral("/raster/unused/");
470 rasterUnusedPrefix
.Append(aPathInfix
);
471 ReportValues(aHandleReport
, aData
, rasterUnusedPrefix
,
472 aTotal
.UnusedRaster());
474 nsAutoCString
vectorUsedPrefix(pathPrefix
);
475 vectorUsedPrefix
.AppendLiteral("/vector/used/");
476 vectorUsedPrefix
.Append(aPathInfix
);
477 ReportValues(aHandleReport
, aData
, vectorUsedPrefix
, aTotal
.UsedVector());
479 nsAutoCString
vectorUnusedPrefix(pathPrefix
);
480 vectorUnusedPrefix
.AppendLiteral("/vector/unused/");
481 vectorUnusedPrefix
.Append(aPathInfix
);
482 ReportValues(aHandleReport
, aData
, vectorUnusedPrefix
,
483 aTotal
.UnusedVector());
486 static void ReportValues(nsIHandleReportCallback
* aHandleReport
,
487 nsISupports
* aData
, const nsACString
& aPathPrefix
,
488 const MemoryCounter
& aCounter
) {
489 ReportSourceValue(aHandleReport
, aData
, aPathPrefix
, aCounter
);
491 ReportValue(aHandleReport
, aData
, KIND_HEAP
, aPathPrefix
, "decoded-heap",
492 "Decoded image data which is stored on the heap.",
493 aCounter
.DecodedHeap());
495 ReportValue(aHandleReport
, aData
, KIND_NONHEAP
, aPathPrefix
,
497 "Decoded image data which isn't stored on the heap.",
498 aCounter
.DecodedNonHeap());
500 // We don't know for certain whether or not it is on the heap, so let's
501 // just report it as non-heap for reporting purposes.
502 ReportValue(aHandleReport
, aData
, KIND_NONHEAP
, aPathPrefix
,
504 "Decoded image data which is unknown to be on the heap or not.",
505 aCounter
.DecodedUnknown());
508 static void ReportSourceValue(nsIHandleReportCallback
* aHandleReport
,
510 const nsACString
& aPathPrefix
,
511 const MemoryCounter
& aCounter
) {
512 ReportValue(aHandleReport
, aData
, KIND_HEAP
, aPathPrefix
, "source",
513 "Raster image source data and vector image documents.",
517 static void ReportValue(nsIHandleReportCallback
* aHandleReport
,
518 nsISupports
* aData
, int32_t aKind
,
519 const nsACString
& aPathPrefix
,
520 const char* aPathSuffix
, const char* aDescription
,
526 nsAutoCString
desc(aDescription
);
527 nsAutoCString
path(aPathPrefix
);
528 path
.Append(aPathSuffix
);
530 aHandleReport
->Callback(""_ns
, path
, aKind
, UNITS_BYTES
, aValue
, desc
,
534 static void RecordCounterForRequest(imgRequest
* aRequest
,
535 nsTArray
<ImageMemoryCounter
>* aArray
,
537 SizeOfState
state(ImagesMallocSizeOf
);
538 RefPtr
<image::Image
> image
= aRequest
->GetImage();
540 ImageMemoryCounter
counter(aRequest
, image
, state
, aIsUsed
);
541 aArray
->AppendElement(std::move(counter
));
543 // We can at least record some information about the image from the
544 // request, and mark it as not knowing the image type yet.
545 ImageMemoryCounter
counter(aRequest
, state
, aIsUsed
);
546 aArray
->AppendElement(std::move(counter
));
551 NS_IMPL_ISUPPORTS(imgMemoryReporter
, nsIMemoryReporter
)
553 NS_IMPL_ISUPPORTS(nsProgressNotificationProxy
, nsIProgressEventSink
,
554 nsIChannelEventSink
, nsIInterfaceRequestor
)
557 nsProgressNotificationProxy::OnProgress(nsIRequest
* request
, int64_t progress
,
558 int64_t progressMax
) {
559 nsCOMPtr
<nsILoadGroup
> loadGroup
;
560 request
->GetLoadGroup(getter_AddRefs(loadGroup
));
562 nsCOMPtr
<nsIProgressEventSink
> target
;
563 NS_QueryNotificationCallbacks(mOriginalCallbacks
, loadGroup
,
564 NS_GET_IID(nsIProgressEventSink
),
565 getter_AddRefs(target
));
569 return target
->OnProgress(mImageRequest
, progress
, progressMax
);
573 nsProgressNotificationProxy::OnStatus(nsIRequest
* request
, nsresult status
,
574 const char16_t
* statusArg
) {
575 nsCOMPtr
<nsILoadGroup
> loadGroup
;
576 request
->GetLoadGroup(getter_AddRefs(loadGroup
));
578 nsCOMPtr
<nsIProgressEventSink
> target
;
579 NS_QueryNotificationCallbacks(mOriginalCallbacks
, loadGroup
,
580 NS_GET_IID(nsIProgressEventSink
),
581 getter_AddRefs(target
));
585 return target
->OnStatus(mImageRequest
, status
, statusArg
);
589 nsProgressNotificationProxy::AsyncOnChannelRedirect(
590 nsIChannel
* oldChannel
, nsIChannel
* newChannel
, uint32_t flags
,
591 nsIAsyncVerifyRedirectCallback
* cb
) {
592 // Tell the original original callbacks about it too
593 nsCOMPtr
<nsILoadGroup
> loadGroup
;
594 newChannel
->GetLoadGroup(getter_AddRefs(loadGroup
));
595 nsCOMPtr
<nsIChannelEventSink
> target
;
596 NS_QueryNotificationCallbacks(mOriginalCallbacks
, loadGroup
,
597 NS_GET_IID(nsIChannelEventSink
),
598 getter_AddRefs(target
));
600 cb
->OnRedirectVerifyCallback(NS_OK
);
604 // Delegate to |target| if set, reusing |cb|
605 return target
->AsyncOnChannelRedirect(oldChannel
, newChannel
, flags
, cb
);
609 nsProgressNotificationProxy::GetInterface(const nsIID
& iid
, void** result
) {
610 if (iid
.Equals(NS_GET_IID(nsIProgressEventSink
))) {
611 *result
= static_cast<nsIProgressEventSink
*>(this);
615 if (iid
.Equals(NS_GET_IID(nsIChannelEventSink
))) {
616 *result
= static_cast<nsIChannelEventSink
*>(this);
620 if (mOriginalCallbacks
) {
621 return mOriginalCallbacks
->GetInterface(iid
, result
);
623 return NS_NOINTERFACE
;
626 static void NewRequestAndEntry(bool aForcePrincipalCheckForCacheEntry
,
627 imgLoader
* aLoader
, const ImageCacheKey
& aKey
,
628 imgRequest
** aRequest
, imgCacheEntry
** aEntry
) {
629 RefPtr
<imgRequest
> request
= new imgRequest(aLoader
, aKey
);
630 RefPtr
<imgCacheEntry
> entry
=
631 new imgCacheEntry(aLoader
, request
, aForcePrincipalCheckForCacheEntry
);
632 aLoader
->AddToUncachedImages(request
);
633 request
.forget(aRequest
);
634 entry
.forget(aEntry
);
637 static bool ShouldRevalidateEntry(imgCacheEntry
* aEntry
, nsLoadFlags aFlags
,
639 bool bValidateEntry
= false;
641 if (aFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
645 if (aFlags
& nsIRequest::VALIDATE_ALWAYS
) {
646 bValidateEntry
= true;
647 } else if (aEntry
->GetMustValidate()) {
648 bValidateEntry
= true;
649 } else if (aHasExpired
) {
650 // The cache entry has expired... Determine whether the stale cache
651 // entry can be used without validation...
653 (nsIRequest::VALIDATE_NEVER
| nsIRequest::VALIDATE_ONCE_PER_SESSION
)) {
654 // VALIDATE_NEVER and VALIDATE_ONCE_PER_SESSION allow stale cache
655 // entries to be used unless they have been explicitly marked to
656 // indicate that revalidation is necessary.
657 bValidateEntry
= false;
659 } else if (!(aFlags
& nsIRequest::LOAD_FROM_CACHE
)) {
660 // LOAD_FROM_CACHE allows a stale cache entry to be used... Otherwise,
661 // the entry must be revalidated.
662 bValidateEntry
= true;
666 return bValidateEntry
;
669 /* Call content policies on cached images that went through a redirect */
670 static bool ShouldLoadCachedImage(imgRequest
* aImgRequest
,
671 Document
* aLoadingDocument
,
672 nsIPrincipal
* aTriggeringPrincipal
,
673 nsContentPolicyType aPolicyType
,
674 bool aSendCSPViolationReports
) {
675 /* Call content policies on cached images - Bug 1082837
676 * Cached images are keyed off of the first uri in a redirect chain.
677 * Hence content policies don't get a chance to test the intermediate hops
678 * or the final destination. Here we test the final destination using
679 * mFinalURI off of the imgRequest and passing it into content policies.
680 * For Mixed Content Blocker, we do an additional check to determine if any
681 * of the intermediary hops went through an insecure redirect with the
682 * mHadInsecureRedirect flag
684 bool insecureRedirect
= aImgRequest
->HadInsecureRedirect();
685 nsCOMPtr
<nsIURI
> contentLocation
;
686 aImgRequest
->GetFinalURI(getter_AddRefs(contentLocation
));
689 nsCOMPtr
<nsIPrincipal
> loadingPrincipal
=
690 aLoadingDocument
? aLoadingDocument
->NodePrincipal()
691 : aTriggeringPrincipal
;
692 // If there is no context and also no triggeringPrincipal, then we use a fresh
693 // nullPrincipal as the loadingPrincipal because we can not create a loadinfo
694 // without a valid loadingPrincipal.
695 if (!loadingPrincipal
) {
696 loadingPrincipal
= NullPrincipal::CreateWithoutOriginAttributes();
699 nsCOMPtr
<nsILoadInfo
> secCheckLoadInfo
= new LoadInfo(
700 loadingPrincipal
, aTriggeringPrincipal
, aLoadingDocument
,
701 nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK
, aPolicyType
);
703 secCheckLoadInfo
->SetSendCSPViolationEvents(aSendCSPViolationReports
);
705 int16_t decision
= nsIContentPolicy::REJECT_REQUEST
;
706 rv
= NS_CheckContentLoadPolicy(contentLocation
, secCheckLoadInfo
,
708 &decision
, nsContentUtils::GetContentPolicy());
709 if (NS_FAILED(rv
) || !NS_CP_ACCEPTED(decision
)) {
713 // We call all Content Policies above, but we also have to call mcb
714 // individually to check the intermediary redirect hops are secure.
715 if (insecureRedirect
) {
716 // Bug 1314356: If the image ended up in the cache upgraded by HSTS and the
717 // page uses upgrade-inscure-requests it had an insecure redirect
718 // (http->https). We need to invalidate the image and reload it because
719 // mixed content blocker only bails if upgrade-insecure-requests is set on
720 // the doc and the resource load is http: which would result in an incorrect
721 // mixed content warning.
722 nsCOMPtr
<nsIDocShell
> docShell
=
723 NS_CP_GetDocShellFromContext(ToSupports(aLoadingDocument
));
725 Document
* document
= docShell
->GetDocument();
726 if (document
&& document
->GetUpgradeInsecureRequests(false)) {
731 if (!aTriggeringPrincipal
|| !aTriggeringPrincipal
->IsSystemPrincipal()) {
732 // reset the decision for mixed content blocker check
733 decision
= nsIContentPolicy::REJECT_REQUEST
;
734 rv
= nsMixedContentBlocker::ShouldLoad(insecureRedirect
, contentLocation
,
737 true, // aReportError
739 if (NS_FAILED(rv
) || !NS_CP_ACCEPTED(decision
)) {
748 // Returns true if this request is compatible with the given CORS mode on the
749 // given loading principal, and false if the request may not be reused due
750 // to CORS. Also checks the Referrer Policy, since requests with different
751 // referrers/policies may generate different responses.
752 static bool ValidateSecurityInfo(imgRequest
* request
, bool forcePrincipalCheck
,
754 nsIPrincipal
* triggeringPrincipal
,
755 Document
* aLoadingDocument
,
756 nsContentPolicyType aPolicyType
) {
757 // If the entry's CORS mode doesn't match, or the CORS mode matches but the
758 // document principal isn't the same, we can't use this request.
759 if (request
->GetCORSMode() != corsmode
) {
762 if (request
->GetCORSMode() != imgIRequest::CORS_NONE
|| forcePrincipalCheck
) {
763 nsCOMPtr
<nsIPrincipal
> otherprincipal
= request
->GetTriggeringPrincipal();
765 // If we previously had a principal, but we don't now, we can't use this
767 if (otherprincipal
&& !triggeringPrincipal
) {
771 if (otherprincipal
&& triggeringPrincipal
) {
773 otherprincipal
->Equals(triggeringPrincipal
, &equals
);
780 // Content Policy Check on Cached Images
781 return ShouldLoadCachedImage(request
, aLoadingDocument
, triggeringPrincipal
,
783 /* aSendCSPViolationReports */ false);
786 static nsresult
NewImageChannel(
787 nsIChannel
** aResult
,
788 // If aForcePrincipalCheckForCacheEntry is true, then we will
789 // force a principal check even when not using CORS before
790 // assuming we have a cache hit on a cache entry that we
791 // create for this channel. This is an out param that should
792 // be set to true if this channel ends up depending on
793 // aTriggeringPrincipal and false otherwise.
794 bool* aForcePrincipalCheckForCacheEntry
, nsIURI
* aURI
,
795 nsIURI
* aInitialDocumentURI
, int32_t aCORSMode
,
796 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
797 nsLoadFlags aLoadFlags
, nsContentPolicyType aPolicyType
,
798 nsIPrincipal
* aTriggeringPrincipal
, nsINode
* aRequestingNode
,
799 bool aRespectPrivacy
) {
803 nsCOMPtr
<nsIHttpChannel
> newHttpChannel
;
805 nsCOMPtr
<nsIInterfaceRequestor
> callbacks
;
808 // Get the notification callbacks from the load group for the new channel.
810 // XXX: This is not exactly correct, because the network request could be
811 // referenced by multiple windows... However, the new channel needs
812 // something. So, using the 'first' notification callbacks is better
815 aLoadGroup
->GetNotificationCallbacks(getter_AddRefs(callbacks
));
818 // Pass in a nullptr loadgroup because this is the underlying network
819 // request. This request may be referenced by several proxy image requests
820 // (possibly in different documents).
821 // If all of the proxy requests are canceled then this request should be
825 nsSecurityFlags securityFlags
=
826 aCORSMode
== imgIRequest::CORS_NONE
827 ? nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT
828 : nsILoadInfo::SEC_REQUIRE_CORS_INHERITS_SEC_CONTEXT
;
829 if (aCORSMode
== imgIRequest::CORS_ANONYMOUS
) {
830 securityFlags
|= nsILoadInfo::SEC_COOKIES_SAME_ORIGIN
;
831 } else if (aCORSMode
== imgIRequest::CORS_USE_CREDENTIALS
) {
832 securityFlags
|= nsILoadInfo::SEC_COOKIES_INCLUDE
;
834 securityFlags
|= nsILoadInfo::SEC_ALLOW_CHROME
;
836 // Note we are calling NS_NewChannelWithTriggeringPrincipal() here with a
837 // node and a principal. This is for things like background images that are
838 // specified by user stylesheets, where the document is being styled, but
839 // the principal is that of the user stylesheet.
840 if (aRequestingNode
&& aTriggeringPrincipal
) {
841 rv
= NS_NewChannelWithTriggeringPrincipal(aResult
, aURI
, aRequestingNode
,
842 aTriggeringPrincipal
,
843 securityFlags
, aPolicyType
,
844 nullptr, // PerformanceStorage
845 nullptr, // loadGroup
846 callbacks
, aLoadFlags
);
852 if (aPolicyType
== nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON
) {
853 // If this is a favicon loading, we will use the originAttributes from the
854 // triggeringPrincipal as the channel's originAttributes. This allows the
855 // favicon loading from XUL will use the correct originAttributes.
857 nsCOMPtr
<nsILoadInfo
> loadInfo
= (*aResult
)->LoadInfo();
858 rv
= loadInfo
->SetOriginAttributes(
859 aTriggeringPrincipal
->OriginAttributesRef());
862 // either we are loading something inside a document, in which case
863 // we should always have a requestingNode, or we are loading something
864 // outside a document, in which case the triggeringPrincipal and
865 // triggeringPrincipal should always be the systemPrincipal.
866 // However, there are exceptions: one is Notifications which create a
867 // channel in the parent process in which case we can't get a
869 rv
= NS_NewChannel(aResult
, aURI
, nsContentUtils::GetSystemPrincipal(),
870 securityFlags
, aPolicyType
,
871 nullptr, // nsICookieJarSettings
872 nullptr, // PerformanceStorage
873 nullptr, // loadGroup
874 callbacks
, aLoadFlags
);
880 // Use the OriginAttributes from the loading principal, if one is available,
881 // and adjust the private browsing ID based on what kind of load the caller
882 // has asked us to perform.
883 OriginAttributes attrs
;
884 if (aTriggeringPrincipal
) {
885 attrs
= aTriggeringPrincipal
->OriginAttributesRef();
887 attrs
.mPrivateBrowsingId
= aRespectPrivacy
? 1 : 0;
889 nsCOMPtr
<nsILoadInfo
> loadInfo
= (*aResult
)->LoadInfo();
890 rv
= loadInfo
->SetOriginAttributes(attrs
);
897 // only inherit if we have a principal
898 *aForcePrincipalCheckForCacheEntry
=
899 aTriggeringPrincipal
&& nsContentUtils::ChannelShouldInheritPrincipal(
900 aTriggeringPrincipal
, aURI
,
901 /* aInheritForAboutBlank */ false,
902 /* aForceInherit */ false);
904 // Initialize HTTP-specific attributes
905 newHttpChannel
= do_QueryInterface(*aResult
);
906 if (newHttpChannel
) {
907 nsCOMPtr
<nsIHttpChannelInternal
> httpChannelInternal
=
908 do_QueryInterface(newHttpChannel
);
909 NS_ENSURE_TRUE(httpChannelInternal
, NS_ERROR_UNEXPECTED
);
910 rv
= httpChannelInternal
->SetDocumentURI(aInitialDocumentURI
);
911 MOZ_ASSERT(NS_SUCCEEDED(rv
));
913 DebugOnly
<nsresult
> rv
= newHttpChannel
->SetReferrerInfo(aReferrerInfo
);
914 MOZ_ASSERT(NS_SUCCEEDED(rv
));
918 // Image channels are loaded by default with reduced priority.
919 nsCOMPtr
<nsISupportsPriority
> p
= do_QueryInterface(*aResult
);
921 uint32_t priority
= nsISupportsPriority::PRIORITY_LOW
;
923 if (aLoadFlags
& nsIRequest::LOAD_BACKGROUND
) {
924 ++priority
; // further reduce priority for background loads
927 p
->AdjustPriority(priority
);
930 // Create a new loadgroup for this new channel, using the old group as
931 // the parent. The indirection keeps the channel insulated from cancels,
932 // but does allow a way for this revalidation to be associated with at
933 // least one base load group for scheduling/caching purposes.
935 nsCOMPtr
<nsILoadGroup
> loadGroup
= do_CreateInstance(NS_LOADGROUP_CONTRACTID
);
936 nsCOMPtr
<nsILoadGroupChild
> childLoadGroup
= do_QueryInterface(loadGroup
);
937 if (childLoadGroup
) {
938 childLoadGroup
->SetParentLoadGroup(aLoadGroup
);
940 (*aResult
)->SetLoadGroup(loadGroup
);
945 static uint32_t SecondsFromPRTime(PRTime aTime
) {
946 return nsContentUtils::SecondsFromPRTime(aTime
);
950 imgCacheEntry::imgCacheEntry(imgLoader
* loader
, imgRequest
* request
,
951 bool forcePrincipalCheck
)
955 mTouchedTime(SecondsFromPRTime(PR_Now())),
956 mLoadTime(SecondsFromPRTime(PR_Now())),
958 mMustValidate(false),
959 // We start off as evicted so we don't try to update the cache.
960 // PutIntoCache will set this to false.
963 mForcePrincipalCheck(forcePrincipalCheck
) {}
965 imgCacheEntry::~imgCacheEntry() {
966 LOG_FUNC(gImgLog
, "imgCacheEntry::~imgCacheEntry()");
969 void imgCacheEntry::Touch(bool updateTime
/* = true */) {
970 LOG_SCOPE(gImgLog
, "imgCacheEntry::Touch");
973 mTouchedTime
= SecondsFromPRTime(PR_Now());
979 void imgCacheEntry::UpdateCache(int32_t diff
/* = 0 */) {
980 // Don't update the cache if we've been removed from it or it doesn't care
981 // about our size or usage.
982 if (!Evicted() && HasNoProxies()) {
983 mLoader
->CacheEntriesChanged(mRequest
->IsChrome(), diff
);
987 void imgCacheEntry::UpdateLoadTime() {
988 mLoadTime
= SecondsFromPRTime(PR_Now());
991 void imgCacheEntry::SetHasNoProxies(bool hasNoProxies
) {
992 if (MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
994 LOG_FUNC_WITH_PARAM(gImgLog
, "imgCacheEntry::SetHasNoProxies true", "uri",
995 mRequest
->CacheKey().URI());
997 LOG_FUNC_WITH_PARAM(gImgLog
, "imgCacheEntry::SetHasNoProxies false",
998 "uri", mRequest
->CacheKey().URI());
1002 mHasNoProxies
= hasNoProxies
;
1005 imgCacheQueue::imgCacheQueue() : mDirty(false), mSize(0) {}
1007 void imgCacheQueue::UpdateSize(int32_t diff
) { mSize
+= diff
; }
1009 uint32_t imgCacheQueue::GetSize() const { return mSize
; }
1011 void imgCacheQueue::Remove(imgCacheEntry
* entry
) {
1012 uint64_t index
= mQueue
.IndexOf(entry
);
1013 if (index
== queueContainer::NoIndex
) {
1017 mSize
-= mQueue
[index
]->GetDataSize();
1019 // If the queue is clean and this is the first entry,
1020 // then we can efficiently remove the entry without
1021 // dirtying the sort order.
1022 if (!IsDirty() && index
== 0) {
1023 std::pop_heap(mQueue
.begin(), mQueue
.end(), imgLoader::CompareCacheEntries
);
1024 mQueue
.RemoveLastElement();
1028 // Remove from the middle of the list. This potentially
1029 // breaks the binary heap sort order.
1030 mQueue
.RemoveElementAt(index
);
1032 // If we only have one entry or the queue is empty, though,
1033 // then the sort order is still effectively good. Simply
1034 // refresh the list to clear the dirty flag.
1035 if (mQueue
.Length() <= 1) {
1040 // Otherwise we must mark the queue dirty and potentially
1041 // trigger an expensive sort later.
1045 void imgCacheQueue::Push(imgCacheEntry
* entry
) {
1046 mSize
+= entry
->GetDataSize();
1048 RefPtr
<imgCacheEntry
> refptr(entry
);
1049 mQueue
.AppendElement(std::move(refptr
));
1050 // If we're not dirty already, then we can efficiently add this to the
1051 // binary heap immediately. This is only O(log n).
1053 std::push_heap(mQueue
.begin(), mQueue
.end(),
1054 imgLoader::CompareCacheEntries
);
1058 already_AddRefed
<imgCacheEntry
> imgCacheQueue::Pop() {
1059 if (mQueue
.IsEmpty()) {
1066 std::pop_heap(mQueue
.begin(), mQueue
.end(), imgLoader::CompareCacheEntries
);
1067 RefPtr
<imgCacheEntry
> entry
= mQueue
.PopLastElement();
1069 mSize
-= entry
->GetDataSize();
1070 return entry
.forget();
1073 void imgCacheQueue::Refresh() {
1074 // Resort the list. This is an O(3 * n) operation and best avoided
1076 std::make_heap(mQueue
.begin(), mQueue
.end(), imgLoader::CompareCacheEntries
);
1080 void imgCacheQueue::MarkDirty() { mDirty
= true; }
1082 bool imgCacheQueue::IsDirty() { return mDirty
; }
1084 uint32_t imgCacheQueue::GetNumElements() const { return mQueue
.Length(); }
1086 bool imgCacheQueue::Contains(imgCacheEntry
* aEntry
) const {
1087 return mQueue
.Contains(aEntry
);
1090 imgCacheQueue::iterator
imgCacheQueue::begin() { return mQueue
.begin(); }
1092 imgCacheQueue::const_iterator
imgCacheQueue::begin() const {
1093 return mQueue
.begin();
1096 imgCacheQueue::iterator
imgCacheQueue::end() { return mQueue
.end(); }
1098 imgCacheQueue::const_iterator
imgCacheQueue::end() const {
1099 return mQueue
.end();
1102 nsresult
imgLoader::CreateNewProxyForRequest(
1103 imgRequest
* aRequest
, nsIURI
* aURI
, nsILoadGroup
* aLoadGroup
,
1104 Document
* aLoadingDocument
, imgINotificationObserver
* aObserver
,
1105 nsLoadFlags aLoadFlags
, imgRequestProxy
** _retval
) {
1106 LOG_SCOPE_WITH_PARAM(gImgLog
, "imgLoader::CreateNewProxyForRequest",
1107 "imgRequest", aRequest
);
1109 /* XXX If we move decoding onto separate threads, we should save off the
1110 calling thread here and pass it off to |proxyRequest| so that it call
1111 proxy calls to |aObserver|.
1114 RefPtr
<imgRequestProxy
> proxyRequest
= new imgRequestProxy();
1116 /* It is important to call |SetLoadFlags()| before calling |Init()| because
1117 |Init()| adds the request to the loadgroup.
1119 proxyRequest
->SetLoadFlags(aLoadFlags
);
1121 // init adds itself to imgRequest's list of observers
1122 nsresult rv
= proxyRequest
->Init(aRequest
, aLoadGroup
, aLoadingDocument
, aURI
,
1124 if (NS_WARN_IF(NS_FAILED(rv
))) {
1128 proxyRequest
.forget(_retval
);
1132 class imgCacheExpirationTracker final
1133 : public nsExpirationTracker
<imgCacheEntry
, 3> {
1134 enum { TIMEOUT_SECONDS
= 10 };
1137 imgCacheExpirationTracker();
1140 void NotifyExpired(imgCacheEntry
* entry
) override
;
1143 imgCacheExpirationTracker::imgCacheExpirationTracker()
1144 : nsExpirationTracker
<imgCacheEntry
, 3>(TIMEOUT_SECONDS
* 1000,
1145 "imgCacheExpirationTracker") {}
1147 void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry
* entry
) {
1148 // Hold on to a reference to this entry, because the expiration tracker
1149 // mechanism doesn't.
1150 RefPtr
<imgCacheEntry
> kungFuDeathGrip(entry
);
1152 if (MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1153 RefPtr
<imgRequest
> req
= entry
->GetRequest();
1155 LOG_FUNC_WITH_PARAM(gImgLog
, "imgCacheExpirationTracker::NotifyExpired",
1156 "entry", req
->CacheKey().URI());
1160 // We can be called multiple times on the same entry. Don't do work multiple
1162 if (!entry
->Evicted()) {
1163 entry
->Loader()->RemoveFromCache(entry
);
1166 entry
->Loader()->VerifyCacheSizes();
1169 ///////////////////////////////////////////////////////////////////////////////
1171 ///////////////////////////////////////////////////////////////////////////////
1173 double imgLoader::sCacheTimeWeight
;
1174 uint32_t imgLoader::sCacheMaxSize
;
1175 imgMemoryReporter
* imgLoader::sMemReporter
;
1177 NS_IMPL_ISUPPORTS(imgLoader
, imgILoader
, nsIContentSniffer
, imgICache
,
1178 nsISupportsWeakReference
, nsIObserver
)
1180 static imgLoader
* gNormalLoader
= nullptr;
1181 static imgLoader
* gPrivateBrowsingLoader
= nullptr;
1184 mozilla::CORSMode
imgLoader::ConvertToCORSMode(uint32_t aImgCORS
) {
1186 case imgIRequest::CORS_NONE
:
1187 return CORSMode::CORS_NONE
;
1188 case imgIRequest::CORS_ANONYMOUS
:
1189 return CORSMode::CORS_ANONYMOUS
;
1190 case imgIRequest::CORS_USE_CREDENTIALS
:
1191 return CORSMode::CORS_USE_CREDENTIALS
;
1194 MOZ_ASSERT(false, "Unexpected imgIRequest CORS value");
1195 return CORSMode::CORS_NONE
;
1199 already_AddRefed
<imgLoader
> imgLoader::CreateImageLoader() {
1200 // In some cases, such as xpctests, XPCOM modules are not automatically
1201 // initialized. We need to make sure that our module is initialized before
1202 // we hand out imgLoader instances and code starts using them.
1203 mozilla::image::EnsureModuleInitialized();
1205 RefPtr
<imgLoader
> loader
= new imgLoader();
1208 return loader
.forget();
1211 imgLoader
* imgLoader::NormalLoader() {
1212 if (!gNormalLoader
) {
1213 gNormalLoader
= CreateImageLoader().take();
1215 return gNormalLoader
;
1218 imgLoader
* imgLoader::PrivateBrowsingLoader() {
1219 if (!gPrivateBrowsingLoader
) {
1220 gPrivateBrowsingLoader
= CreateImageLoader().take();
1221 gPrivateBrowsingLoader
->RespectPrivacyNotifications();
1223 return gPrivateBrowsingLoader
;
1226 imgLoader::imgLoader()
1227 : mUncachedImagesMutex("imgLoader::UncachedImages"),
1228 mRespectPrivacy(false) {
1229 sMemReporter
->AddRef();
1230 sMemReporter
->RegisterLoader(this);
1233 imgLoader::~imgLoader() {
1234 ClearChromeImageCache();
1237 // If there are any of our imgRequest's left they are in the uncached
1238 // images set, so clear their pointer to us.
1239 MutexAutoLock
lock(mUncachedImagesMutex
);
1240 for (auto iter
= mUncachedImages
.Iter(); !iter
.Done(); iter
.Next()) {
1241 nsPtrHashKey
<imgRequest
>* entry
= iter
.Get();
1242 RefPtr
<imgRequest
> req
= entry
->GetKey();
1246 sMemReporter
->UnregisterLoader(this);
1247 sMemReporter
->Release();
1250 void imgLoader::VerifyCacheSizes() {
1252 if (!mCacheTracker
) {
1256 uint32_t cachesize
= mCache
.Count() + mChromeCache
.Count();
1257 uint32_t queuesize
=
1258 mCacheQueue
.GetNumElements() + mChromeCacheQueue
.GetNumElements();
1259 uint32_t trackersize
= 0;
1260 for (nsExpirationTracker
<imgCacheEntry
, 3>::Iterator
it(mCacheTracker
.get());
1264 MOZ_ASSERT(queuesize
== trackersize
, "Queue and tracker sizes out of sync!");
1265 MOZ_ASSERT(queuesize
<= cachesize
, "Queue has more elements than cache!");
1269 imgLoader::imgCacheTable
& imgLoader::GetCache(bool aForChrome
) {
1270 return aForChrome
? mChromeCache
: mCache
;
1273 imgLoader::imgCacheTable
& imgLoader::GetCache(const ImageCacheKey
& aKey
) {
1274 return GetCache(aKey
.IsChrome());
1277 imgCacheQueue
& imgLoader::GetCacheQueue(bool aForChrome
) {
1278 return aForChrome
? mChromeCacheQueue
: mCacheQueue
;
1281 imgCacheQueue
& imgLoader::GetCacheQueue(const ImageCacheKey
& aKey
) {
1282 return GetCacheQueue(aKey
.IsChrome());
1285 void imgLoader::GlobalInit() {
1286 sCacheTimeWeight
= StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
1287 int32_t cachesize
= StaticPrefs::image_cache_size_AtStartup();
1288 sCacheMaxSize
= cachesize
> 0 ? cachesize
: 0;
1290 sMemReporter
= new imgMemoryReporter();
1291 RegisterStrongAsyncMemoryReporter(sMemReporter
);
1292 RegisterImagesContentUsedUncompressedDistinguishedAmount(
1293 imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount
);
1296 void imgLoader::ShutdownMemoryReporter() {
1297 UnregisterImagesContentUsedUncompressedDistinguishedAmount();
1298 UnregisterStrongMemoryReporter(sMemReporter
);
1301 nsresult
imgLoader::InitCache() {
1302 nsCOMPtr
<nsIObserverService
> os
= mozilla::services::GetObserverService();
1304 return NS_ERROR_FAILURE
;
1307 os
->AddObserver(this, "memory-pressure", false);
1308 os
->AddObserver(this, "chrome-flush-caches", false);
1309 os
->AddObserver(this, "last-pb-context-exited", false);
1310 os
->AddObserver(this, "profile-before-change", false);
1311 os
->AddObserver(this, "xpcom-shutdown", false);
1313 mCacheTracker
= MakeUnique
<imgCacheExpirationTracker
>();
1318 nsresult
imgLoader::Init() {
1325 imgLoader::RespectPrivacyNotifications() {
1326 mRespectPrivacy
= true;
1331 imgLoader::Observe(nsISupports
* aSubject
, const char* aTopic
,
1332 const char16_t
* aData
) {
1333 if (strcmp(aTopic
, "memory-pressure") == 0) {
1335 } else if (strcmp(aTopic
, "chrome-flush-caches") == 0) {
1337 ClearChromeImageCache();
1338 } else if (strcmp(aTopic
, "last-pb-context-exited") == 0) {
1339 if (mRespectPrivacy
) {
1341 ClearChromeImageCache();
1343 } else if (strcmp(aTopic
, "profile-before-change") == 0) {
1344 mCacheTracker
= nullptr;
1345 } else if (strcmp(aTopic
, "xpcom-shutdown") == 0) {
1346 mCacheTracker
= nullptr;
1347 ShutdownMemoryReporter();
1350 // (Nothing else should bring us here)
1351 MOZ_ASSERT(0, "Invalid topic received");
1358 imgLoader::ClearCache(bool chrome
) {
1359 if (XRE_IsParentProcess()) {
1360 bool privateLoader
= this == gPrivateBrowsingLoader
;
1361 for (auto* cp
: ContentParent::AllProcesses(ContentParent::eLive
)) {
1362 Unused
<< cp
->SendClearImageCache(privateLoader
, chrome
);
1367 return ClearChromeImageCache();
1369 return ClearImageCache();
1373 imgLoader::RemoveEntriesFromPrincipal(nsIPrincipal
* aPrincipal
) {
1374 nsAutoString origin
;
1375 nsresult rv
= nsContentUtils::GetUTFOrigin(aPrincipal
, origin
);
1376 if (NS_WARN_IF(NS_FAILED(rv
))) {
1380 AutoTArray
<RefPtr
<imgCacheEntry
>, 128> entriesToBeRemoved
;
1382 imgCacheTable
& cache
= GetCache(aPrincipal
->IsSystemPrincipal());
1383 for (auto iter
= cache
.Iter(); !iter
.Done(); iter
.Next()) {
1384 auto& key
= iter
.Key();
1386 if (key
.OriginAttributesRef() !=
1387 BasePrincipal::Cast(aPrincipal
)->OriginAttributesRef()) {
1391 nsAutoString imageOrigin
;
1392 nsresult rv
= nsContentUtils::GetUTFOrigin(key
.URI(), imageOrigin
);
1393 if (NS_WARN_IF(NS_FAILED(rv
))) {
1397 if (imageOrigin
== origin
) {
1398 entriesToBeRemoved
.AppendElement(iter
.Data());
1402 for (auto& entry
: entriesToBeRemoved
) {
1403 if (!RemoveFromCache(entry
)) {
1405 "Couldn't remove an entry from the cache in "
1406 "RemoveEntriesFromPrincipal()\n");
1414 imgLoader::RemoveEntry(nsIURI
* aURI
, Document
* aDoc
) {
1416 OriginAttributes attrs
;
1418 nsCOMPtr
<nsIPrincipal
> principal
= aDoc
->NodePrincipal();
1420 attrs
= principal
->OriginAttributesRef();
1424 ImageCacheKey
key(aURI
, attrs
, aDoc
);
1425 if (RemoveFromCache(key
)) {
1429 return NS_ERROR_NOT_AVAILABLE
;
1433 imgLoader::FindEntryProperties(nsIURI
* uri
, Document
* aDoc
,
1434 nsIProperties
** _retval
) {
1437 OriginAttributes attrs
;
1439 nsCOMPtr
<nsIPrincipal
> principal
= aDoc
->NodePrincipal();
1441 attrs
= principal
->OriginAttributesRef();
1445 ImageCacheKey
key(uri
, attrs
, aDoc
);
1446 imgCacheTable
& cache
= GetCache(key
);
1448 RefPtr
<imgCacheEntry
> entry
;
1449 if (cache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
1450 if (mCacheTracker
&& entry
->HasNoProxies()) {
1451 mCacheTracker
->MarkUsed(entry
);
1454 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1456 nsCOMPtr
<nsIProperties
> properties
= request
->Properties();
1457 properties
.forget(_retval
);
1464 NS_IMETHODIMP_(void)
1465 imgLoader::ClearCacheForControlledDocument(Document
* aDoc
) {
1467 AutoTArray
<RefPtr
<imgCacheEntry
>, 128> entriesToBeRemoved
;
1468 imgCacheTable
& cache
= GetCache(false);
1469 for (auto iter
= cache
.Iter(); !iter
.Done(); iter
.Next()) {
1470 auto& key
= iter
.Key();
1471 if (key
.ControlledDocument() == aDoc
) {
1472 entriesToBeRemoved
.AppendElement(iter
.Data());
1475 for (auto& entry
: entriesToBeRemoved
) {
1476 if (!RemoveFromCache(entry
)) {
1478 "Couldn't remove an entry from the cache in "
1479 "ClearCacheForControlledDocument()\n");
1484 void imgLoader::Shutdown() {
1485 NS_IF_RELEASE(gNormalLoader
);
1486 gNormalLoader
= nullptr;
1487 NS_IF_RELEASE(gPrivateBrowsingLoader
);
1488 gPrivateBrowsingLoader
= nullptr;
1491 nsresult
imgLoader::ClearChromeImageCache() {
1492 return EvictEntries(mChromeCache
);
1495 nsresult
imgLoader::ClearImageCache() { return EvictEntries(mCache
); }
1497 void imgLoader::MinimizeCaches() {
1498 EvictEntries(mCacheQueue
);
1499 EvictEntries(mChromeCacheQueue
);
1502 bool imgLoader::PutIntoCache(const ImageCacheKey
& aKey
, imgCacheEntry
* entry
) {
1503 imgCacheTable
& cache
= GetCache(aKey
);
1505 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::PutIntoCache", "uri",
1508 // Check to see if this request already exists in the cache. If so, we'll
1509 // replace the old version.
1510 RefPtr
<imgCacheEntry
> tmpCacheEntry
;
1511 if (cache
.Get(aKey
, getter_AddRefs(tmpCacheEntry
)) && tmpCacheEntry
) {
1513 gImgLog
, LogLevel::Debug
,
1514 ("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
1516 RefPtr
<imgRequest
> tmpRequest
= tmpCacheEntry
->GetRequest();
1518 // If it already exists, and we're putting the same key into the cache, we
1519 // should remove the old version.
1520 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1521 ("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
1524 RemoveFromCache(aKey
);
1526 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1527 ("[this=%p] imgLoader::PutIntoCache --"
1528 " Element NOT already in the cache",
1532 cache
.Put(aKey
, RefPtr
{entry
});
1534 // We can be called to resurrect an evicted entry.
1535 if (entry
->Evicted()) {
1536 entry
->SetEvicted(false);
1539 // If we're resurrecting an entry with no proxies, put it back in the
1540 // tracker and queue.
1541 if (entry
->HasNoProxies()) {
1542 nsresult addrv
= NS_OK
;
1544 if (mCacheTracker
) {
1545 addrv
= mCacheTracker
->AddObject(entry
);
1548 if (NS_SUCCEEDED(addrv
)) {
1549 imgCacheQueue
& queue
= GetCacheQueue(aKey
);
1554 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1555 request
->SetIsInCache(true);
1556 RemoveFromUncachedImages(request
);
1561 bool imgLoader::SetHasNoProxies(imgRequest
* aRequest
, imgCacheEntry
* aEntry
) {
1562 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::SetHasNoProxies", "uri",
1563 aRequest
->CacheKey().URI());
1565 aEntry
->SetHasNoProxies(true);
1567 if (aEntry
->Evicted()) {
1571 imgCacheQueue
& queue
= GetCacheQueue(aRequest
->IsChrome());
1573 nsresult addrv
= NS_OK
;
1575 if (mCacheTracker
) {
1576 addrv
= mCacheTracker
->AddObject(aEntry
);
1579 if (NS_SUCCEEDED(addrv
)) {
1583 imgCacheTable
& cache
= GetCache(aRequest
->IsChrome());
1584 CheckCacheLimits(cache
, queue
);
1589 bool imgLoader::SetHasProxies(imgRequest
* aRequest
) {
1592 const ImageCacheKey
& key
= aRequest
->CacheKey();
1593 imgCacheTable
& cache
= GetCache(key
);
1595 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::SetHasProxies", "uri",
1598 RefPtr
<imgCacheEntry
> entry
;
1599 if (cache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
1600 // Make sure the cache entry is for the right request
1601 RefPtr
<imgRequest
> entryRequest
= entry
->GetRequest();
1602 if (entryRequest
== aRequest
&& entry
->HasNoProxies()) {
1603 imgCacheQueue
& queue
= GetCacheQueue(key
);
1604 queue
.Remove(entry
);
1606 if (mCacheTracker
) {
1607 mCacheTracker
->RemoveObject(entry
);
1610 entry
->SetHasNoProxies(false);
1619 void imgLoader::CacheEntriesChanged(bool aForChrome
,
1620 int32_t aSizeDiff
/* = 0 */) {
1621 imgCacheQueue
& queue
= GetCacheQueue(aForChrome
);
1622 // We only need to dirty the queue if there is any sorting
1623 // taking place. Empty or single-entry lists can't become
1625 if (queue
.GetNumElements() > 1) {
1628 queue
.UpdateSize(aSizeDiff
);
1631 void imgLoader::CheckCacheLimits(imgCacheTable
& cache
, imgCacheQueue
& queue
) {
1632 if (queue
.GetNumElements() == 0) {
1633 NS_ASSERTION(queue
.GetSize() == 0,
1634 "imgLoader::CheckCacheLimits -- incorrect cache size");
1637 // Remove entries from the cache until we're back at our desired max size.
1638 while (queue
.GetSize() > sCacheMaxSize
) {
1639 // Remove the first entry in the queue.
1640 RefPtr
<imgCacheEntry
> entry(queue
.Pop());
1642 NS_ASSERTION(entry
, "imgLoader::CheckCacheLimits -- NULL entry pointer");
1644 if (MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1645 RefPtr
<imgRequest
> req
= entry
->GetRequest();
1647 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::CheckCacheLimits",
1648 "entry", req
->CacheKey().URI());
1653 // We just popped this entry from the queue, so pass AlreadyRemoved
1654 // to avoid searching the queue again in RemoveFromCache.
1655 RemoveFromCache(entry
, QueueState::AlreadyRemoved
);
1660 bool imgLoader::ValidateRequestWithNewChannel(
1661 imgRequest
* request
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
1662 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
1663 imgINotificationObserver
* aObserver
, Document
* aLoadingDocument
,
1664 uint64_t aInnerWindowId
, nsLoadFlags aLoadFlags
,
1665 nsContentPolicyType aLoadPolicyType
, imgRequestProxy
** aProxyRequest
,
1666 nsIPrincipal
* aTriggeringPrincipal
, int32_t aCORSMode
, bool aLinkPreload
,
1667 bool* aNewChannelCreated
) {
1668 // now we need to insert a new channel request object in between the real
1669 // request and the proxy that basically delays loading the image until it
1670 // gets a 304 or figures out that this needs to be a new request
1674 // If we're currently in the middle of validating this request, just hand
1675 // back a proxy to it; the required work will be done for us.
1676 if (imgCacheValidator
* validator
= request
->GetValidator()) {
1677 rv
= CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
1678 aObserver
, aLoadFlags
, aProxyRequest
);
1679 if (NS_FAILED(rv
)) {
1683 if (*aProxyRequest
) {
1684 imgRequestProxy
* proxy
= static_cast<imgRequestProxy
*>(*aProxyRequest
);
1686 // We will send notifications from imgCacheValidator::OnStartRequest().
1687 // In the mean time, we must defer notifications because we are added to
1688 // the imgRequest's proxy list, and we can get extra notifications
1689 // resulting from methods such as StartDecoding(). See bug 579122.
1690 proxy
->MarkValidating();
1693 MOZ_ASSERT(aLoadingDocument
);
1694 proxy
->PrioritizeAsPreload();
1695 auto preloadKey
= PreloadHashKey::CreateAsImage(
1696 aURI
, aTriggeringPrincipal
, ConvertToCORSMode(aCORSMode
));
1697 proxy
->NotifyOpen(preloadKey
, aLoadingDocument
, true);
1700 // Attach the proxy without notifying
1701 validator
->AddProxy(proxy
);
1706 // We will rely on Necko to cache this request when it's possible, and to
1707 // tell imgCacheValidator::OnStartRequest whether the request came from its
1709 nsCOMPtr
<nsIChannel
> newChannel
;
1710 bool forcePrincipalCheck
;
1711 rv
= NewImageChannel(getter_AddRefs(newChannel
), &forcePrincipalCheck
, aURI
,
1712 aInitialDocumentURI
, aCORSMode
, aReferrerInfo
,
1713 aLoadGroup
, aLoadFlags
, aLoadPolicyType
,
1714 aTriggeringPrincipal
, aLoadingDocument
, mRespectPrivacy
);
1715 if (NS_FAILED(rv
)) {
1719 if (aNewChannelCreated
) {
1720 *aNewChannelCreated
= true;
1723 RefPtr
<imgRequestProxy
> req
;
1724 rv
= CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
1725 aObserver
, aLoadFlags
, getter_AddRefs(req
));
1726 if (NS_FAILED(rv
)) {
1730 // Make sure that OnStatus/OnProgress calls have the right request set...
1731 RefPtr
<nsProgressNotificationProxy
> progressproxy
=
1732 new nsProgressNotificationProxy(newChannel
, req
);
1733 if (!progressproxy
) {
1737 RefPtr
<imgCacheValidator
> hvc
=
1738 new imgCacheValidator(progressproxy
, this, request
, aLoadingDocument
,
1739 aInnerWindowId
, forcePrincipalCheck
);
1741 // Casting needed here to get past multiple inheritance.
1742 nsCOMPtr
<nsIStreamListener
> listener
=
1743 do_QueryInterface(static_cast<nsIThreadRetargetableStreamListener
*>(hvc
));
1744 NS_ENSURE_TRUE(listener
, false);
1746 // We must set the notification callbacks before setting up the
1747 // CORS listener, because that's also interested inthe
1748 // notification callbacks.
1749 newChannel
->SetNotificationCallbacks(hvc
);
1751 request
->SetValidator(hvc
);
1753 // We will send notifications from imgCacheValidator::OnStartRequest().
1754 // In the mean time, we must defer notifications because we are added to
1755 // the imgRequest's proxy list, and we can get extra notifications
1756 // resulting from methods such as StartDecoding(). See bug 579122.
1757 req
->MarkValidating();
1760 MOZ_ASSERT(aLoadingDocument
);
1761 req
->PrioritizeAsPreload();
1762 auto preloadKey
= PreloadHashKey::CreateAsImage(
1763 aURI
, aTriggeringPrincipal
, ConvertToCORSMode(aCORSMode
));
1764 req
->NotifyOpen(preloadKey
, aLoadingDocument
, true);
1767 // Add the proxy without notifying
1770 mozilla::net::PredictorLearn(aURI
, aInitialDocumentURI
,
1771 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE
,
1773 rv
= newChannel
->AsyncOpen(listener
);
1774 if (NS_WARN_IF(NS_FAILED(rv
))) {
1775 req
->CancelAndForgetObserver(rv
);
1776 // This will notify any current or future <link preload> tags. Pass the
1777 // non-open channel so that we can read loadinfo and referrer info of that
1779 req
->NotifyStart(newChannel
);
1780 // Use the non-channel overload of this method to force the notification to
1781 // happen. The preload request has not been assigned a channel.
1782 req
->NotifyStop(rv
);
1786 req
.forget(aProxyRequest
);
1790 bool imgLoader::ValidateEntry(
1791 imgCacheEntry
* aEntry
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
1792 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
1793 imgINotificationObserver
* aObserver
, Document
* aLoadingDocument
,
1794 nsLoadFlags aLoadFlags
, nsContentPolicyType aLoadPolicyType
,
1795 bool aCanMakeNewChannel
, bool* aNewChannelCreated
,
1796 imgRequestProxy
** aProxyRequest
, nsIPrincipal
* aTriggeringPrincipal
,
1797 int32_t aCORSMode
, bool aLinkPreload
) {
1798 LOG_SCOPE(gImgLog
, "imgLoader::ValidateEntry");
1800 // If the expiration time is zero, then the request has not gotten far enough
1801 // to know when it will expire.
1802 uint32_t expiryTime
= aEntry
->GetExpiryTime();
1804 expiryTime
!= 0 && expiryTime
<= SecondsFromPRTime(PR_Now());
1808 // Special treatment for file URLs - aEntry has expired if file has changed
1809 nsCOMPtr
<nsIFileURL
> fileUrl(do_QueryInterface(aURI
));
1811 uint32_t lastModTime
= aEntry
->GetLoadTime();
1813 nsCOMPtr
<nsIFile
> theFile
;
1814 rv
= fileUrl
->GetFile(getter_AddRefs(theFile
));
1815 if (NS_SUCCEEDED(rv
)) {
1817 rv
= theFile
->GetLastModifiedTime(&fileLastMod
);
1818 if (NS_SUCCEEDED(rv
)) {
1819 // nsIFile uses millisec, NSPR usec
1820 fileLastMod
*= 1000;
1821 hasExpired
= SecondsFromPRTime((PRTime
)fileLastMod
) > lastModTime
;
1826 RefPtr
<imgRequest
> request(aEntry
->GetRequest());
1832 if (!ValidateSecurityInfo(request
, aEntry
->ForcePrincipalCheck(), aCORSMode
,
1833 aTriggeringPrincipal
, aLoadingDocument
,
1838 // data URIs are immutable and by their nature can't leak data, so we can
1839 // just return true in that case. Doing so would mean that shift-reload
1840 // doesn't reload data URI documents/images though (which is handy for
1841 // debugging during gecko development) so we make an exception in that case.
1842 nsAutoCString scheme
;
1843 aURI
->GetScheme(scheme
);
1844 if (scheme
.EqualsLiteral("data") &&
1845 !(aLoadFlags
& nsIRequest::LOAD_BYPASS_CACHE
)) {
1849 bool validateRequest
= false;
1851 if (!request
->CanReuseWithoutValidation(aLoadingDocument
)) {
1852 // If we would need to revalidate this entry, but we're being told to
1853 // bypass the cache, we don't allow this entry to be used.
1854 if (aLoadFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
1858 if (MOZ_UNLIKELY(ChaosMode::isActive(ChaosFeature::ImageCache
))) {
1859 if (ChaosMode::randomUint32LessThan(4) < 1) {
1864 // Determine whether the cache aEntry must be revalidated...
1865 validateRequest
= ShouldRevalidateEntry(aEntry
, aLoadFlags
, hasExpired
);
1867 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1868 ("imgLoader::ValidateEntry validating cache entry. "
1869 "validateRequest = %d",
1871 } else if (!aLoadingDocument
&& MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1872 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1873 ("imgLoader::ValidateEntry BYPASSING cache validation for %s "
1874 "because of NULL loading document",
1875 aURI
->GetSpecOrDefault().get()));
1878 // We can't use a cached request if it comes from a different
1879 // application cache than this load is expecting.
1880 nsCOMPtr
<nsIApplicationCacheContainer
> appCacheContainer
;
1881 nsCOMPtr
<nsIApplicationCache
> requestAppCache
;
1882 nsCOMPtr
<nsIApplicationCache
> groupAppCache
;
1883 if ((appCacheContainer
= do_GetInterface(request
->GetRequest()))) {
1884 appCacheContainer
->GetApplicationCache(getter_AddRefs(requestAppCache
));
1886 if ((appCacheContainer
= do_QueryInterface(aLoadGroup
))) {
1887 appCacheContainer
->GetApplicationCache(getter_AddRefs(groupAppCache
));
1890 if (requestAppCache
!= groupAppCache
) {
1891 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1892 ("imgLoader::ValidateEntry - Unable to use cached imgRequest "
1893 "[request=%p] because of mismatched application caches\n",
1894 address_of(request
)));
1898 // If the original request is still transferring don't kick off a validation
1899 // network request because it is a bit silly to issue a validation request if
1900 // the original request hasn't even finished yet. So just return true
1901 // indicating the caller can create a new proxy for the request and use it as
1903 // This is an optimization but it's also required for correctness. If we don't
1904 // do this then when firing the load complete notification for the original
1905 // request that can unblock load for the document and then spin the event loop
1906 // (see the stack in bug 1641682) which then the OnStartRequest for the
1907 // validation request can fire which can call UpdateProxies and can sync
1908 // notify on the progress tracker about all existing state, which includes
1909 // load complete, so we fire a second load complete notification for the
1911 // In addition, we want to validate if the original request encountered
1912 // an error for two reasons. The first being if the error was a network error
1913 // then trying to re-fetch the image might succeed. The second is more
1914 // complicated. We decide if we should fire the load or error event for img
1915 // elements depending on if the image has error in its status at the time when
1916 // the load complete notification is received, and we set error status on an
1917 // image if it encounters a network error or a decode error with no real way
1918 // to tell them apart. So if we load an image that will produce a decode error
1919 // the first time we will usually fire the load event, and then decode enough
1920 // to encounter the decode error and set the error status on the image. The
1921 // next time we reference the image in the same document the load complete
1922 // notification is replayed and this time the error status from the decode is
1923 // already present so we fire the error event instead of the load event. This
1924 // is a bug (bug 1645576) that we should fix. In order to avoid that bug in
1925 // some cases (specifically the cases when we hit this code and try to
1926 // validate the request) we make sure to validate. This avoids the bug because
1927 // when the load complete notification arrives the proxy is marked as
1928 // validating so it lies about its status and returns nothing.
1929 bool requestComplete
= false;
1930 RefPtr
<ProgressTracker
> tracker
;
1931 RefPtr
<mozilla::image::Image
> image
= request
->GetImage();
1933 tracker
= image
->GetProgressTracker();
1935 tracker
= request
->GetProgressTracker();
1938 if (tracker
->GetProgress() & (FLAG_LOAD_COMPLETE
| FLAG_HAS_ERROR
)) {
1939 requestComplete
= true;
1942 if (!requestComplete
) {
1946 if (validateRequest
&& aCanMakeNewChannel
) {
1947 LOG_SCOPE(gImgLog
, "imgLoader::ValidateRequest |cache hit| must validate");
1949 uint64_t innerWindowID
=
1950 aLoadingDocument
? aLoadingDocument
->InnerWindowID() : 0;
1951 return ValidateRequestWithNewChannel(
1952 request
, aURI
, aInitialDocumentURI
, aReferrerInfo
, aLoadGroup
,
1953 aObserver
, aLoadingDocument
, innerWindowID
, aLoadFlags
, aLoadPolicyType
,
1954 aProxyRequest
, aTriggeringPrincipal
, aCORSMode
, aLinkPreload
,
1955 aNewChannelCreated
);
1958 return !validateRequest
;
1961 bool imgLoader::RemoveFromCache(const ImageCacheKey
& aKey
) {
1962 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::RemoveFromCache", "uri",
1965 imgCacheTable
& cache
= GetCache(aKey
);
1966 imgCacheQueue
& queue
= GetCacheQueue(aKey
);
1968 RefPtr
<imgCacheEntry
> entry
;
1969 cache
.Remove(aKey
, getter_AddRefs(entry
));
1971 MOZ_ASSERT(!entry
->Evicted(), "Evicting an already-evicted cache entry!");
1973 // Entries with no proxies are in the tracker.
1974 if (entry
->HasNoProxies()) {
1975 if (mCacheTracker
) {
1976 mCacheTracker
->RemoveObject(entry
);
1978 queue
.Remove(entry
);
1981 entry
->SetEvicted(true);
1983 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1984 request
->SetIsInCache(false);
1985 AddToUncachedImages(request
);
1992 bool imgLoader::RemoveFromCache(imgCacheEntry
* entry
, QueueState aQueueState
) {
1993 LOG_STATIC_FUNC(gImgLog
, "imgLoader::RemoveFromCache entry");
1995 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1997 const ImageCacheKey
& key
= request
->CacheKey();
1998 imgCacheTable
& cache
= GetCache(key
);
1999 imgCacheQueue
& queue
= GetCacheQueue(key
);
2001 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::RemoveFromCache",
2002 "entry's uri", key
.URI());
2006 if (entry
->HasNoProxies()) {
2007 LOG_STATIC_FUNC(gImgLog
,
2008 "imgLoader::RemoveFromCache removing from tracker");
2009 if (mCacheTracker
) {
2010 mCacheTracker
->RemoveObject(entry
);
2012 // Only search the queue to remove the entry if its possible it might
2013 // be in the queue. If we know its not in the queue this would be
2015 MOZ_ASSERT_IF(aQueueState
== QueueState::AlreadyRemoved
,
2016 !queue
.Contains(entry
));
2017 if (aQueueState
== QueueState::MaybeExists
) {
2018 queue
.Remove(entry
);
2022 entry
->SetEvicted(true);
2023 request
->SetIsInCache(false);
2024 AddToUncachedImages(request
);
2032 nsresult
imgLoader::EvictEntries(imgCacheTable
& aCacheToClear
) {
2033 LOG_STATIC_FUNC(gImgLog
, "imgLoader::EvictEntries table");
2035 // We have to make a temporary, since RemoveFromCache removes the element
2036 // from the queue, invalidating iterators.
2037 nsTArray
<RefPtr
<imgCacheEntry
> > entries
;
2038 for (auto iter
= aCacheToClear
.Iter(); !iter
.Done(); iter
.Next()) {
2039 RefPtr
<imgCacheEntry
>& data
= iter
.Data();
2040 entries
.AppendElement(data
);
2043 for (uint32_t i
= 0; i
< entries
.Length(); ++i
) {
2044 if (!RemoveFromCache(entries
[i
])) {
2045 return NS_ERROR_FAILURE
;
2049 MOZ_ASSERT(aCacheToClear
.Count() == 0);
2054 nsresult
imgLoader::EvictEntries(imgCacheQueue
& aQueueToClear
) {
2055 LOG_STATIC_FUNC(gImgLog
, "imgLoader::EvictEntries queue");
2057 // We have to make a temporary, since RemoveFromCache removes the element
2058 // from the queue, invalidating iterators.
2059 nsTArray
<RefPtr
<imgCacheEntry
> > entries(aQueueToClear
.GetNumElements());
2060 for (auto i
= aQueueToClear
.begin(); i
!= aQueueToClear
.end(); ++i
) {
2061 entries
.AppendElement(*i
);
2064 // Iterate in reverse order to minimize array copying.
2065 for (auto& entry
: entries
) {
2066 if (!RemoveFromCache(entry
)) {
2067 return NS_ERROR_FAILURE
;
2071 MOZ_ASSERT(aQueueToClear
.GetNumElements() == 0);
2076 void imgLoader::AddToUncachedImages(imgRequest
* aRequest
) {
2077 MutexAutoLock
lock(mUncachedImagesMutex
);
2078 mUncachedImages
.PutEntry(aRequest
);
2081 void imgLoader::RemoveFromUncachedImages(imgRequest
* aRequest
) {
2082 MutexAutoLock
lock(mUncachedImagesMutex
);
2083 mUncachedImages
.RemoveEntry(aRequest
);
2086 bool imgLoader::PreferLoadFromCache(nsIURI
* aURI
) const {
2087 // If we are trying to load an image from a protocol that doesn't support
2088 // caching (e.g. thumbnails via the moz-page-thumb:// protocol, or icons via
2089 // the moz-extension:// protocol), load it directly from the cache to prevent
2090 // re-decoding the image. See Bug 1373258.
2091 // TODO: Bug 1406134
2092 return aURI
->SchemeIs("moz-page-thumb") || aURI
->SchemeIs("moz-extension");
2095 #define LOAD_FLAGS_CACHE_MASK \
2096 (nsIRequest::LOAD_BYPASS_CACHE | nsIRequest::LOAD_FROM_CACHE)
2098 #define LOAD_FLAGS_VALIDATE_MASK \
2099 (nsIRequest::VALIDATE_ALWAYS | nsIRequest::VALIDATE_NEVER | \
2100 nsIRequest::VALIDATE_ONCE_PER_SESSION)
2103 imgLoader::LoadImageXPCOM(
2104 nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
, nsIReferrerInfo
* aReferrerInfo
,
2105 nsIPrincipal
* aTriggeringPrincipal
, nsILoadGroup
* aLoadGroup
,
2106 imgINotificationObserver
* aObserver
, Document
* aLoadingDocument
,
2107 nsLoadFlags aLoadFlags
, nsISupports
* aCacheKey
,
2108 nsContentPolicyType aContentPolicyType
, imgIRequest
** _retval
) {
2109 // Optional parameter, so defaults to 0 (== TYPE_INVALID)
2110 if (!aContentPolicyType
) {
2111 aContentPolicyType
= nsIContentPolicy::TYPE_INTERNAL_IMAGE
;
2113 imgRequestProxy
* proxy
;
2114 nsresult rv
= LoadImage(
2115 aURI
, aInitialDocumentURI
, aReferrerInfo
, aTriggeringPrincipal
, 0,
2116 aLoadGroup
, aObserver
, aLoadingDocument
, aLoadingDocument
, aLoadFlags
,
2117 aCacheKey
, aContentPolicyType
, u
""_ns
,
2118 /* aUseUrgentStartForChannel */ false, /* aListPreload */ false, &proxy
);
2123 static void MakeRequestStaticIfNeeded(
2124 Document
* aLoadingDocument
, imgRequestProxy
** aProxyAboutToGetReturned
) {
2125 if (!aLoadingDocument
|| !aLoadingDocument
->IsStaticDocument()) {
2129 if (!*aProxyAboutToGetReturned
) {
2133 RefPtr
<imgRequestProxy
> proxy
= dont_AddRef(*aProxyAboutToGetReturned
);
2134 *aProxyAboutToGetReturned
= nullptr;
2136 RefPtr
<imgRequestProxy
> staticProxy
=
2137 proxy
->GetStaticRequest(aLoadingDocument
);
2138 if (staticProxy
!= proxy
) {
2139 proxy
->CancelAndForgetObserver(NS_BINDING_ABORTED
);
2140 proxy
= std::move(staticProxy
);
2142 proxy
.forget(aProxyAboutToGetReturned
);
2145 nsresult
imgLoader::LoadImage(
2146 nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
, nsIReferrerInfo
* aReferrerInfo
,
2147 nsIPrincipal
* aTriggeringPrincipal
, uint64_t aRequestContextID
,
2148 nsILoadGroup
* aLoadGroup
, imgINotificationObserver
* aObserver
,
2149 nsINode
* aContext
, Document
* aLoadingDocument
, nsLoadFlags aLoadFlags
,
2150 nsISupports
* aCacheKey
, nsContentPolicyType aContentPolicyType
,
2151 const nsAString
& initiatorType
, bool aUseUrgentStartForChannel
,
2152 bool aLinkPreload
, imgRequestProxy
** _retval
) {
2155 NS_ASSERTION(aURI
, "imgLoader::LoadImage -- NULL URI pointer");
2158 return NS_ERROR_NULL_POINTER
;
2161 auto makeStaticIfNeeded
= mozilla::MakeScopeExit(
2162 [&] { MakeRequestStaticIfNeeded(aLoadingDocument
, _retval
); });
2164 #ifdef MOZ_GECKO_PROFILER
2165 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK
,
2166 aURI
->GetSpecOrDefault());
2169 LOG_SCOPE_WITH_PARAM(gImgLog
, "imgLoader::LoadImage", "aURI", aURI
);
2173 RefPtr
<imgRequest
> request
;
2176 nsLoadFlags requestFlags
= nsIRequest::LOAD_NORMAL
;
2179 bool isPrivate
= false;
2181 if (aLoadingDocument
) {
2182 isPrivate
= nsContentUtils::IsInPrivateBrowsing(aLoadingDocument
);
2183 } else if (aLoadGroup
) {
2184 isPrivate
= nsContentUtils::IsInPrivateBrowsing(aLoadGroup
);
2186 MOZ_ASSERT(isPrivate
== mRespectPrivacy
);
2188 if (aLoadingDocument
) {
2189 // The given load group should match that of the document if given. If
2190 // that isn't the case, then we need to add more plumbing to ensure we
2191 // block the document as well.
2192 nsCOMPtr
<nsILoadGroup
> docLoadGroup
=
2193 aLoadingDocument
->GetDocumentLoadGroup();
2194 MOZ_ASSERT(docLoadGroup
== aLoadGroup
);
2198 // Get the default load flags from the loadgroup (if possible)...
2200 aLoadGroup
->GetLoadFlags(&requestFlags
);
2201 if (PreferLoadFromCache(aURI
)) {
2202 requestFlags
|= nsIRequest::LOAD_FROM_CACHE
;
2206 // Merge the default load flags with those passed in via aLoadFlags.
2207 // Currently, *only* the caching, validation and background load flags
2210 // The flags in aLoadFlags take precedence over the default flags!
2212 if (aLoadFlags
& LOAD_FLAGS_CACHE_MASK
) {
2213 // Override the default caching flags...
2214 requestFlags
= (requestFlags
& ~LOAD_FLAGS_CACHE_MASK
) |
2215 (aLoadFlags
& LOAD_FLAGS_CACHE_MASK
);
2217 if (aLoadFlags
& LOAD_FLAGS_VALIDATE_MASK
) {
2218 // Override the default validation flags...
2219 requestFlags
= (requestFlags
& ~LOAD_FLAGS_VALIDATE_MASK
) |
2220 (aLoadFlags
& LOAD_FLAGS_VALIDATE_MASK
);
2222 if (aLoadFlags
& nsIRequest::LOAD_BACKGROUND
) {
2223 // Propagate background loading...
2224 requestFlags
|= nsIRequest::LOAD_BACKGROUND
;
2228 // Set background loading if it is <link rel=preload>
2229 requestFlags
|= nsIRequest::LOAD_BACKGROUND
;
2232 int32_t corsmode
= imgIRequest::CORS_NONE
;
2233 if (aLoadFlags
& imgILoader::LOAD_CORS_ANONYMOUS
) {
2234 corsmode
= imgIRequest::CORS_ANONYMOUS
;
2235 } else if (aLoadFlags
& imgILoader::LOAD_CORS_USE_CREDENTIALS
) {
2236 corsmode
= imgIRequest::CORS_USE_CREDENTIALS
;
2239 // Look in the preloaded images of loading document first.
2240 if (StaticPrefs::network_preload() && !aLinkPreload
&& aLoadingDocument
) {
2241 auto key
= PreloadHashKey::CreateAsImage(aURI
, aTriggeringPrincipal
,
2242 ConvertToCORSMode(corsmode
));
2243 if (RefPtr
<PreloaderBase
> preload
=
2244 aLoadingDocument
->Preloads().LookupPreload(key
)) {
2245 RefPtr
<imgRequestProxy
> proxy
= do_QueryObject(preload
);
2248 MOZ_LOG(gImgLog
, LogLevel::Debug
,
2249 ("[this=%p] imgLoader::LoadImage -- preloaded [proxy=%p]"
2251 this, proxy
.get(), aLoadingDocument
));
2253 // Removing the preload for this image to be in parity with Chromium. Any
2254 // following regular image request will be reloaded using the regular
2255 // path: image cache, http cache, network. Any following `<link
2256 // rel=preload as=image>` will start a new image preload that can be
2257 // satisfied from http cache or network.
2259 // There is a spec discussion for "preload cache", see
2260 // https://github.com/w3c/preload/issues/97. And it is also not clear how
2261 // preload image interacts with list of available images, see
2262 // https://github.com/whatwg/html/issues/4474.
2263 proxy
->RemoveSelf(aLoadingDocument
);
2264 proxy
->NotifyUsage();
2266 imgRequest
* request
= proxy
->GetOwner();
2268 CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
2269 aObserver
, requestFlags
, _retval
);
2270 NS_ENSURE_SUCCESS(rv
, rv
);
2272 imgRequestProxy
* newProxy
= *_retval
;
2273 if (imgCacheValidator
* validator
= request
->GetValidator()) {
2274 newProxy
->MarkValidating();
2275 // Attach the proxy without notifying and this will add us to the load
2277 validator
->AddProxy(newProxy
);
2279 // It's OK to add here even if the request is done. If it is, it'll send
2280 // a OnStopRequest()and the proxy will be removed from the loadgroup in
2281 // imgRequestProxy::OnLoadComplete.
2282 newProxy
->AddToLoadGroup();
2283 newProxy
->NotifyListener();
2290 RefPtr
<imgCacheEntry
> entry
;
2292 // Look in the cache for our URI, and then validate it.
2293 // XXX For now ignore aCacheKey. We will need it in the future
2294 // for correctly dealing with image load requests that are a result
2296 OriginAttributes attrs
;
2297 if (aTriggeringPrincipal
) {
2298 attrs
= aTriggeringPrincipal
->OriginAttributesRef();
2300 ImageCacheKey
key(aURI
, attrs
, aLoadingDocument
);
2301 imgCacheTable
& cache
= GetCache(key
);
2303 if (cache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
2304 bool newChannelCreated
= false;
2305 if (ValidateEntry(entry
, aURI
, aInitialDocumentURI
, aReferrerInfo
,
2306 aLoadGroup
, aObserver
, aLoadingDocument
, requestFlags
,
2307 aContentPolicyType
, true, &newChannelCreated
, _retval
,
2308 aTriggeringPrincipal
, corsmode
, aLinkPreload
)) {
2309 request
= entry
->GetRequest();
2311 // If this entry has no proxies, its request has no reference to the
2313 if (entry
->HasNoProxies()) {
2314 LOG_FUNC_WITH_PARAM(gImgLog
,
2315 "imgLoader::LoadImage() adding proxyless entry",
2317 MOZ_ASSERT(!request
->HasCacheEntry(),
2318 "Proxyless entry's request has cache entry!");
2319 request
->SetCacheEntry(entry
);
2321 if (mCacheTracker
&& entry
->GetExpirationState()->IsTracked()) {
2322 mCacheTracker
->MarkUsed(entry
);
2328 if (!newChannelCreated
) {
2329 // This is ugly but it's needed to report CSP violations. We have 3
2331 // - we don't have cache. We are not in this if() stmt. A new channel is
2332 // created and that triggers the CSP checks.
2333 // - We have a cache entry and this is blocked by CSP directives.
2334 DebugOnly
<bool> shouldLoad
= ShouldLoadCachedImage(
2335 request
, aLoadingDocument
, aTriggeringPrincipal
, aContentPolicyType
,
2336 /* aSendCSPViolationReports */ true);
2337 MOZ_ASSERT(shouldLoad
);
2340 // We can't use this entry. We'll try to load it off the network, and if
2341 // successful, overwrite the old entry in the cache with a new one.
2346 // Keep the channel in this scope, so we can adjust its notificationCallbacks
2347 // later when we create the proxy.
2348 nsCOMPtr
<nsIChannel
> newChannel
;
2349 // If we didn't get a cache hit, we need to load from the network.
2351 LOG_SCOPE(gImgLog
, "imgLoader::LoadImage |cache miss|");
2353 bool forcePrincipalCheck
;
2354 rv
= NewImageChannel(getter_AddRefs(newChannel
), &forcePrincipalCheck
, aURI
,
2355 aInitialDocumentURI
, corsmode
, aReferrerInfo
,
2356 aLoadGroup
, requestFlags
, aContentPolicyType
,
2357 aTriggeringPrincipal
, aContext
, mRespectPrivacy
);
2358 if (NS_FAILED(rv
)) {
2359 return NS_ERROR_FAILURE
;
2362 MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel
) == mRespectPrivacy
);
2364 NewRequestAndEntry(forcePrincipalCheck
, this, key
, getter_AddRefs(request
),
2365 getter_AddRefs(entry
));
2367 MOZ_LOG(gImgLog
, LogLevel::Debug
,
2368 ("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
2370 this, request
.get()));
2372 nsCOMPtr
<nsIClassOfService
> cos(do_QueryInterface(newChannel
));
2374 if (aUseUrgentStartForChannel
&& !aLinkPreload
) {
2375 cos
->AddClassFlags(nsIClassOfService::UrgentStart
);
2378 if (StaticPrefs::network_http_tailing_enabled() &&
2379 aContentPolicyType
== nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON
) {
2380 cos
->AddClassFlags(nsIClassOfService::Throttleable
|
2381 nsIClassOfService::Tail
);
2382 nsCOMPtr
<nsIHttpChannel
> httpChannel(do_QueryInterface(newChannel
));
2384 Unused
<< httpChannel
->SetRequestContextID(aRequestContextID
);
2389 nsCOMPtr
<nsILoadGroup
> channelLoadGroup
;
2390 newChannel
->GetLoadGroup(getter_AddRefs(channelLoadGroup
));
2391 rv
= request
->Init(aURI
, aURI
, /* aHadInsecureRedirect = */ false,
2392 channelLoadGroup
, newChannel
, entry
, aLoadingDocument
,
2393 aTriggeringPrincipal
, corsmode
, aReferrerInfo
);
2394 if (NS_FAILED(rv
)) {
2395 return NS_ERROR_FAILURE
;
2398 // Add the initiator type for this image load
2399 nsCOMPtr
<nsITimedChannel
> timedChannel
= do_QueryInterface(newChannel
);
2401 timedChannel
->SetInitiatorType(initiatorType
);
2404 // create the proxy listener
2405 nsCOMPtr
<nsIStreamListener
> listener
= new ProxyListener(request
.get());
2407 MOZ_LOG(gImgLog
, LogLevel::Debug
,
2408 ("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
2411 mozilla::net::PredictorLearn(aURI
, aInitialDocumentURI
,
2412 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE
,
2415 nsresult openRes
= newChannel
->AsyncOpen(listener
);
2417 if (NS_FAILED(openRes
)) {
2419 gImgLog
, LogLevel::Debug
,
2420 ("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
2422 this, static_cast<uint32_t>(openRes
)));
2423 request
->CancelAndAbort(openRes
);
2427 // Try to add the new request into the cache.
2428 PutIntoCache(key
, entry
);
2430 LOG_MSG_WITH_PARAM(gImgLog
, "imgLoader::LoadImage |cache hit|", "request",
2434 // If we didn't get a proxy when validating the cache entry, we need to
2437 // ValidateEntry() has three return values: "Is valid," "might be valid --
2438 // validating over network", and "not valid." If we don't have a _retval,
2439 // we know ValidateEntry is not validating over the network, so it's safe
2440 // to SetLoadId here because we know this request is valid for this context.
2442 // Note, however, that this doesn't guarantee the behaviour we want (one
2443 // URL maps to the same image on a page) if we load the same image in a
2444 // different tab (see bug 528003), because its load id will get re-set, and
2445 // that'll cause us to validate over the network.
2446 request
->SetLoadId(aLoadingDocument
);
2448 LOG_MSG(gImgLog
, "imgLoader::LoadImage", "creating proxy request.");
2449 rv
= CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
2450 aObserver
, requestFlags
, _retval
);
2451 if (NS_FAILED(rv
)) {
2455 imgRequestProxy
* proxy
= *_retval
;
2457 // Make sure that OnStatus/OnProgress calls have the right request set, if
2458 // we did create a channel here.
2460 nsCOMPtr
<nsIInterfaceRequestor
> requestor(
2461 new nsProgressNotificationProxy(newChannel
, proxy
));
2463 return NS_ERROR_OUT_OF_MEMORY
;
2465 newChannel
->SetNotificationCallbacks(requestor
);
2469 MOZ_ASSERT(aLoadingDocument
);
2470 proxy
->PrioritizeAsPreload();
2471 auto preloadKey
= PreloadHashKey::CreateAsImage(
2472 aURI
, aTriggeringPrincipal
, ConvertToCORSMode(corsmode
));
2473 proxy
->NotifyOpen(preloadKey
, aLoadingDocument
, true);
2476 // Note that it's OK to add here even if the request is done. If it is,
2477 // it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
2478 // the proxy will be removed from the loadgroup.
2479 proxy
->AddToLoadGroup();
2481 // If we're loading off the network, explicitly don't notify our proxy,
2482 // because necko (or things called from necko, such as imgCacheValidator)
2483 // are going to call our notifications asynchronously, and we can't make it
2484 // further asynchronous because observers might rely on imagelib completing
2485 // its work between the channel's OnStartRequest and OnStopRequest.
2487 proxy
->NotifyListener();
2493 NS_ASSERTION(*_retval
, "imgLoader::LoadImage -- no return value");
2499 imgLoader::LoadImageWithChannelXPCOM(nsIChannel
* channel
,
2500 imgINotificationObserver
* aObserver
,
2501 Document
* aLoadingDocument
,
2502 nsIStreamListener
** listener
,
2503 imgIRequest
** _retval
) {
2505 imgRequestProxy
* proxy
;
2506 result
= LoadImageWithChannel(channel
, aObserver
, aLoadingDocument
, listener
,
2512 nsresult
imgLoader::LoadImageWithChannel(nsIChannel
* channel
,
2513 imgINotificationObserver
* aObserver
,
2514 Document
* aLoadingDocument
,
2515 nsIStreamListener
** listener
,
2516 imgRequestProxy
** _retval
) {
2517 NS_ASSERTION(channel
,
2518 "imgLoader::LoadImageWithChannel -- NULL channel pointer");
2520 MOZ_ASSERT(NS_UsePrivateBrowsing(channel
) == mRespectPrivacy
);
2522 auto makeStaticIfNeeded
= mozilla::MakeScopeExit(
2523 [&] { MakeRequestStaticIfNeeded(aLoadingDocument
, _retval
); });
2525 LOG_SCOPE(gImgLog
, "imgLoader::LoadImageWithChannel");
2526 RefPtr
<imgRequest
> request
;
2528 nsCOMPtr
<nsIURI
> uri
;
2529 channel
->GetURI(getter_AddRefs(uri
));
2531 NS_ENSURE_TRUE(channel
, NS_ERROR_FAILURE
);
2532 nsCOMPtr
<nsILoadInfo
> loadInfo
= channel
->LoadInfo();
2534 OriginAttributes attrs
= loadInfo
->GetOriginAttributes();
2536 ImageCacheKey
key(uri
, attrs
, aLoadingDocument
);
2538 nsLoadFlags requestFlags
= nsIRequest::LOAD_NORMAL
;
2539 channel
->GetLoadFlags(&requestFlags
);
2541 if (PreferLoadFromCache(uri
)) {
2542 requestFlags
|= nsIRequest::LOAD_FROM_CACHE
;
2545 RefPtr
<imgCacheEntry
> entry
;
2547 if (requestFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
2548 RemoveFromCache(key
);
2550 // Look in the cache for our URI, and then validate it.
2551 // XXX For now ignore aCacheKey. We will need it in the future
2552 // for correctly dealing with image load requests that are a result
2554 imgCacheTable
& cache
= GetCache(key
);
2555 if (cache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
2556 // We don't want to kick off another network load. So we ask
2557 // ValidateEntry to only do validation without creating a new proxy. If
2558 // it says that the entry isn't valid any more, we'll only use the entry
2559 // we're getting if the channel is loading from the cache anyways.
2561 // XXX -- should this be changed? it's pretty much verbatim from the old
2562 // code, but seems nonsensical.
2564 // Since aCanMakeNewChannel == false, we don't need to pass content policy
2565 // type/principal/etc
2567 nsCOMPtr
<nsILoadInfo
> loadInfo
= channel
->LoadInfo();
2568 // if there is a loadInfo, use the right contentType, otherwise
2569 // default to the internal image type
2570 nsContentPolicyType policyType
= loadInfo
->InternalContentPolicyType();
2572 if (ValidateEntry(entry
, uri
, nullptr, nullptr, nullptr, aObserver
,
2573 aLoadingDocument
, requestFlags
, policyType
, false,
2574 nullptr, nullptr, nullptr, imgIRequest::CORS_NONE
,
2576 request
= entry
->GetRequest();
2578 nsCOMPtr
<nsICacheInfoChannel
> cacheChan(do_QueryInterface(channel
));
2582 cacheChan
->IsFromCache(&bUseCacheCopy
);
2584 bUseCacheCopy
= false;
2587 if (!bUseCacheCopy
) {
2590 request
= entry
->GetRequest();
2594 if (request
&& entry
) {
2595 // If this entry has no proxies, its request has no reference to
2597 if (entry
->HasNoProxies()) {
2598 LOG_FUNC_WITH_PARAM(
2600 "imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
2602 MOZ_ASSERT(!request
->HasCacheEntry(),
2603 "Proxyless entry's request has cache entry!");
2604 request
->SetCacheEntry(entry
);
2606 if (mCacheTracker
&& entry
->GetExpirationState()->IsTracked()) {
2607 mCacheTracker
->MarkUsed(entry
);
2614 nsCOMPtr
<nsILoadGroup
> loadGroup
;
2615 channel
->GetLoadGroup(getter_AddRefs(loadGroup
));
2618 if (aLoadingDocument
) {
2619 // The load group of the channel should always match that of the
2620 // document if given. If that isn't the case, then we need to add more
2621 // plumbing to ensure we block the document as well.
2622 nsCOMPtr
<nsILoadGroup
> docLoadGroup
=
2623 aLoadingDocument
->GetDocumentLoadGroup();
2624 MOZ_ASSERT(docLoadGroup
== loadGroup
);
2628 // Filter out any load flags not from nsIRequest
2629 requestFlags
&= nsIRequest::LOAD_REQUESTMASK
;
2631 nsresult rv
= NS_OK
;
2633 // we have this in our cache already.. cancel the current (document) load
2635 // this should fire an OnStopRequest
2636 channel
->Cancel(NS_ERROR_PARSED_DATA_CACHED
);
2638 *listener
= nullptr; // give them back a null nsIStreamListener
2640 rv
= CreateNewProxyForRequest(request
, uri
, loadGroup
, aLoadingDocument
,
2641 aObserver
, requestFlags
, _retval
);
2642 static_cast<imgRequestProxy
*>(*_retval
)->NotifyListener();
2644 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2645 nsCOMPtr
<nsIURI
> originalURI
;
2646 channel
->GetOriginalURI(getter_AddRefs(originalURI
));
2648 // XXX(seth): We should be able to just use |key| here, except that |key| is
2649 // constructed above with the *current URI* and not the *original URI*. I'm
2650 // pretty sure this is a bug, and it's preventing us from ever getting a
2651 // cache hit in LoadImageWithChannel when redirects are involved.
2652 ImageCacheKey
originalURIKey(originalURI
, attrs
, aLoadingDocument
);
2654 // Default to doing a principal check because we don't know who
2655 // started that load and whether their principal ended up being
2656 // inherited on the channel.
2657 NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
2658 originalURIKey
, getter_AddRefs(request
),
2659 getter_AddRefs(entry
));
2661 // No principal specified here, because we're not passed one.
2662 // In LoadImageWithChannel, the redirects that may have been
2663 // associated with this load would have gone through necko.
2664 // We only have the final URI in ImageLib and hence don't know
2665 // if the request went through insecure redirects. But if it did,
2666 // the necko cache should have handled that (since all necko cache hits
2667 // including the redirects will go through content policy). Hence, we
2668 // can set aHadInsecureRedirect to false here.
2669 rv
= request
->Init(originalURI
, uri
, /* aHadInsecureRedirect = */ false,
2670 channel
, channel
, entry
, aLoadingDocument
, nullptr,
2671 imgIRequest::CORS_NONE
, nullptr);
2672 NS_ENSURE_SUCCESS(rv
, rv
);
2674 RefPtr
<ProxyListener
> pl
=
2675 new ProxyListener(static_cast<nsIStreamListener
*>(request
.get()));
2676 pl
.forget(listener
);
2678 // Try to add the new request into the cache.
2679 PutIntoCache(originalURIKey
, entry
);
2681 rv
= CreateNewProxyForRequest(request
, originalURI
, loadGroup
,
2682 aLoadingDocument
, aObserver
, requestFlags
,
2685 // Explicitly don't notify our proxy, because we're loading off the
2686 // network, and necko (or things called from necko, such as
2687 // imgCacheValidator) are going to call our notifications asynchronously,
2688 // and we can't make it further asynchronous because observers might rely
2689 // on imagelib completing its work between the channel's OnStartRequest and
2693 if (NS_FAILED(rv
)) {
2697 (*_retval
)->AddToLoadGroup();
2701 bool imgLoader::SupportImageWithMimeType(const char* aMimeType
,
2702 AcceptedMimeTypes aAccept
2703 /* = AcceptedMimeTypes::IMAGES */) {
2704 nsAutoCString
mimeType(aMimeType
);
2705 ToLowerCase(mimeType
);
2707 if (aAccept
== AcceptedMimeTypes::IMAGES_AND_DOCUMENTS
&&
2708 mimeType
.EqualsLiteral("image/svg+xml")) {
2712 DecoderType type
= DecoderFactory::GetDecoderType(mimeType
.get());
2713 return type
!= DecoderType::UNKNOWN
;
2717 imgLoader::GetMIMETypeFromContent(nsIRequest
* aRequest
,
2718 const uint8_t* aContents
, uint32_t aLength
,
2719 nsACString
& aContentType
) {
2720 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
2722 nsCOMPtr
<nsILoadInfo
> loadInfo
= channel
->LoadInfo();
2723 if (loadInfo
->GetSkipContentSniffing()) {
2724 return NS_ERROR_NOT_AVAILABLE
;
2727 return GetMimeTypeFromContent((const char*)aContents
, aLength
, aContentType
);
2731 nsresult
imgLoader::GetMimeTypeFromContent(const char* aContents
,
2733 nsACString
& aContentType
) {
2734 nsAutoCString detected
;
2738 (!strncmp(aContents
, "GIF87a", 6) || !strncmp(aContents
, "GIF89a", 6))) {
2739 aContentType
.AssignLiteral(IMAGE_GIF
);
2742 } else if (aLength
>= 8 && ((unsigned char)aContents
[0] == 0x89 &&
2743 (unsigned char)aContents
[1] == 0x50 &&
2744 (unsigned char)aContents
[2] == 0x4E &&
2745 (unsigned char)aContents
[3] == 0x47 &&
2746 (unsigned char)aContents
[4] == 0x0D &&
2747 (unsigned char)aContents
[5] == 0x0A &&
2748 (unsigned char)aContents
[6] == 0x1A &&
2749 (unsigned char)aContents
[7] == 0x0A)) {
2750 aContentType
.AssignLiteral(IMAGE_PNG
);
2752 /* maybe a JPEG (JFIF)? */
2753 /* JFIF files start with SOI APP0 but older files can start with SOI DQT
2754 * so we test for SOI followed by any marker, i.e. FF D8 FF
2755 * this will also work for SPIFF JPEG files if they appear in the future.
2757 * (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
2759 } else if (aLength
>= 3 && ((unsigned char)aContents
[0]) == 0xFF &&
2760 ((unsigned char)aContents
[1]) == 0xD8 &&
2761 ((unsigned char)aContents
[2]) == 0xFF) {
2762 aContentType
.AssignLiteral(IMAGE_JPEG
);
2764 /* or how about ART? */
2765 /* ART begins with JG (4A 47). Major version offset 2.
2766 * Minor version offset 3. Offset 4 must be nullptr.
2768 } else if (aLength
>= 5 && ((unsigned char)aContents
[0]) == 0x4a &&
2769 ((unsigned char)aContents
[1]) == 0x47 &&
2770 ((unsigned char)aContents
[4]) == 0x00) {
2771 aContentType
.AssignLiteral(IMAGE_ART
);
2773 } else if (aLength
>= 2 && !strncmp(aContents
, "BM", 2)) {
2774 aContentType
.AssignLiteral(IMAGE_BMP
);
2776 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
2777 // CURs begin with 2-byte 0 followed by 2-byte 2.
2778 } else if (aLength
>= 4 && (!memcmp(aContents
, "\000\000\001\000", 4) ||
2779 !memcmp(aContents
, "\000\000\002\000", 4))) {
2780 aContentType
.AssignLiteral(IMAGE_ICO
);
2782 // WebPs always begin with RIFF, a 32-bit length, and WEBP.
2783 } else if (aLength
>= 12 && !memcmp(aContents
, "RIFF", 4) &&
2784 !memcmp(aContents
+ 8, "WEBP", 4)) {
2785 aContentType
.AssignLiteral(IMAGE_WEBP
);
2787 } else if (MatchesMP4(reinterpret_cast<const uint8_t*>(aContents
), aLength
,
2789 detected
.Equals(IMAGE_AVIF
)) {
2790 aContentType
.AssignLiteral(IMAGE_AVIF
);
2792 /* none of the above? I give up */
2793 return NS_ERROR_NOT_AVAILABLE
;
2800 * proxy stream listener class used to handle multipart/x-mixed-replace
2803 #include "nsIRequest.h"
2804 #include "nsIStreamConverterService.h"
2806 NS_IMPL_ISUPPORTS(ProxyListener
, nsIStreamListener
,
2807 nsIThreadRetargetableStreamListener
, nsIRequestObserver
)
2809 ProxyListener::ProxyListener(nsIStreamListener
* dest
) : mDestListener(dest
) {
2810 /* member initializers and constructor code */
2813 ProxyListener::~ProxyListener() { /* destructor code */
2816 /** nsIRequestObserver methods **/
2819 ProxyListener::OnStartRequest(nsIRequest
* aRequest
) {
2820 if (!mDestListener
) {
2821 return NS_ERROR_FAILURE
;
2824 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
2826 // We need to set the initiator type for the image load
2827 nsCOMPtr
<nsITimedChannel
> timedChannel
= do_QueryInterface(channel
);
2830 timedChannel
->GetInitiatorType(type
);
2831 if (type
.IsEmpty()) {
2832 timedChannel
->SetInitiatorType(u
"img"_ns
);
2836 nsAutoCString contentType
;
2837 nsresult rv
= channel
->GetContentType(contentType
);
2839 if (!contentType
.IsEmpty()) {
2840 /* If multipart/x-mixed-replace content, we'll insert a MIME decoder
2841 in the pipeline to handle the content and pass it along to our
2844 if ("multipart/x-mixed-replace"_ns
.Equals(contentType
)) {
2845 nsCOMPtr
<nsIStreamConverterService
> convServ(
2846 do_GetService("@mozilla.org/streamConverters;1", &rv
));
2847 if (NS_SUCCEEDED(rv
)) {
2848 nsCOMPtr
<nsIStreamListener
> toListener(mDestListener
);
2849 nsCOMPtr
<nsIStreamListener
> fromListener
;
2851 rv
= convServ
->AsyncConvertData("multipart/x-mixed-replace", "*/*",
2852 toListener
, nullptr,
2853 getter_AddRefs(fromListener
));
2854 if (NS_SUCCEEDED(rv
)) {
2855 mDestListener
= fromListener
;
2862 return mDestListener
->OnStartRequest(aRequest
);
2866 ProxyListener::OnStopRequest(nsIRequest
* aRequest
, nsresult status
) {
2867 if (!mDestListener
) {
2868 return NS_ERROR_FAILURE
;
2871 return mDestListener
->OnStopRequest(aRequest
, status
);
2874 /** nsIStreamListener methods **/
2877 ProxyListener::OnDataAvailable(nsIRequest
* aRequest
, nsIInputStream
* inStr
,
2878 uint64_t sourceOffset
, uint32_t count
) {
2879 if (!mDestListener
) {
2880 return NS_ERROR_FAILURE
;
2883 return mDestListener
->OnDataAvailable(aRequest
, inStr
, sourceOffset
, count
);
2886 /** nsThreadRetargetableStreamListener methods **/
2888 ProxyListener::CheckListenerChain() {
2889 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
2890 nsresult rv
= NS_OK
;
2891 nsCOMPtr
<nsIThreadRetargetableStreamListener
> retargetableListener
=
2892 do_QueryInterface(mDestListener
, &rv
);
2893 if (retargetableListener
) {
2894 rv
= retargetableListener
->CheckListenerChain();
2897 gImgLog
, LogLevel::Debug
,
2898 ("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
2900 (NS_SUCCEEDED(rv
) ? "success" : "failure"), this,
2901 (nsIStreamListener
*)mDestListener
, static_cast<uint32_t>(rv
)));
2906 * http validate class. check a channel for a 304
2909 NS_IMPL_ISUPPORTS(imgCacheValidator
, nsIStreamListener
, nsIRequestObserver
,
2910 nsIThreadRetargetableStreamListener
, nsIChannelEventSink
,
2911 nsIInterfaceRequestor
, nsIAsyncVerifyRedirectCallback
)
2913 imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy
* progress
,
2914 imgLoader
* loader
, imgRequest
* request
,
2915 Document
* aDocument
,
2916 uint64_t aInnerWindowId
,
2917 bool forcePrincipalCheckForCacheEntry
)
2918 : mProgressProxy(progress
),
2920 mDocument(aDocument
),
2921 mInnerWindowId(aInnerWindowId
),
2923 mHadInsecureRedirect(false) {
2924 NewRequestAndEntry(forcePrincipalCheckForCacheEntry
, loader
,
2925 mRequest
->CacheKey(), getter_AddRefs(mNewRequest
),
2926 getter_AddRefs(mNewEntry
));
2929 imgCacheValidator::~imgCacheValidator() {
2931 // If something went wrong, and we never unblocked the requests waiting on
2932 // validation, now is our last chance. We will cancel the new request and
2933 // switch the waiting proxies to it.
2934 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
2938 void imgCacheValidator::AddProxy(imgRequestProxy
* aProxy
) {
2939 // aProxy needs to be in the loadgroup since we're validating from
2941 aProxy
->AddToLoadGroup();
2943 mProxies
.AppendElement(aProxy
);
2946 void imgCacheValidator::RemoveProxy(imgRequestProxy
* aProxy
) {
2947 mProxies
.RemoveElement(aProxy
);
2950 void imgCacheValidator::UpdateProxies(bool aCancelRequest
, bool aSyncNotify
) {
2951 MOZ_ASSERT(mRequest
);
2953 // Clear the validator before updating the proxies. The notifications may
2954 // clone an existing request, and its state could be inconsistent.
2955 mRequest
->SetValidator(nullptr);
2958 // If an error occurred, we will want to cancel the new request, and make the
2959 // validating proxies point to it. Any proxies still bound to the original
2960 // request which are not validating should remain untouched.
2961 if (aCancelRequest
) {
2962 MOZ_ASSERT(mNewRequest
);
2963 mNewRequest
->CancelAndAbort(NS_BINDING_ABORTED
);
2966 // We have finished validating the request, so we can safely take ownership
2967 // of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
2968 // if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
2969 // that any potential notifications should still be suppressed in
2970 // imgRequestProxy::ChangeOwner because we haven't cleared the validating
2971 // flag yet, and thus they will remain deferred.
2972 AutoTArray
<RefPtr
<imgRequestProxy
>, 4> proxies(std::move(mProxies
));
2974 for (auto& proxy
: proxies
) {
2975 // First update the state of all proxies before notifying any of them
2976 // to ensure a consistent state (e.g. in case the notification causes
2977 // other proxies to be touched indirectly.)
2978 MOZ_ASSERT(proxy
->IsValidating());
2979 MOZ_ASSERT(proxy
->NotificationsDeferred(),
2980 "Proxies waiting on cache validation should be "
2981 "deferring notifications!");
2983 proxy
->ChangeOwner(mNewRequest
);
2985 proxy
->ClearValidating();
2988 mNewRequest
= nullptr;
2989 mNewEntry
= nullptr;
2991 for (auto& proxy
: proxies
) {
2993 // Notify synchronously, because the caller knows we are already in an
2994 // asynchronously-called function (e.g. OnStartRequest).
2995 proxy
->SyncNotifyListener();
2997 // Notify asynchronously, because the caller does not know our current
2998 // call state (e.g. ~imgCacheValidator).
2999 proxy
->NotifyListener();
3004 /** nsIRequestObserver methods **/
3007 imgCacheValidator::OnStartRequest(nsIRequest
* aRequest
) {
3008 // We may be holding on to a document, so ensure that it's released.
3009 RefPtr
<Document
> document
= mDocument
.forget();
3011 // If for some reason we don't still have an existing request (probably
3012 // because OnStartRequest got delivered more than once), just bail.
3014 MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
3015 aRequest
->Cancel(NS_BINDING_ABORTED
);
3016 return NS_ERROR_FAILURE
;
3019 // If this request is coming from cache and has the same URI as our
3020 // imgRequest, the request all our proxies are pointing at is valid, and all
3021 // we have to do is tell them to notify their listeners.
3022 nsCOMPtr
<nsICacheInfoChannel
> cacheChan(do_QueryInterface(aRequest
));
3023 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
3024 if (cacheChan
&& channel
&& !mRequest
->CacheChanged(aRequest
)) {
3025 bool isFromCache
= false;
3026 cacheChan
->IsFromCache(&isFromCache
);
3028 nsCOMPtr
<nsIURI
> channelURI
;
3029 channel
->GetURI(getter_AddRefs(channelURI
));
3031 nsCOMPtr
<nsIURI
> finalURI
;
3032 mRequest
->GetFinalURI(getter_AddRefs(finalURI
));
3034 bool sameURI
= false;
3035 if (channelURI
&& finalURI
) {
3036 channelURI
->Equals(finalURI
, &sameURI
);
3039 if (isFromCache
&& sameURI
) {
3040 // We don't need to load this any more.
3041 aRequest
->Cancel(NS_BINDING_ABORTED
);
3042 mNewRequest
= nullptr;
3044 // Clear the validator before updating the proxies. The notifications may
3045 // clone an existing request, and its state could be inconsistent.
3046 mRequest
->SetLoadId(document
);
3047 mRequest
->SetInnerWindowID(mInnerWindowId
);
3048 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3053 // We can't load out of cache. We have to create a whole new request for the
3054 // data that's coming in off the channel.
3055 nsCOMPtr
<nsIURI
> uri
;
3056 mRequest
->GetURI(getter_AddRefs(uri
));
3058 LOG_MSG_WITH_PARAM(gImgLog
,
3059 "imgCacheValidator::OnStartRequest creating new request",
3062 int32_t corsmode
= mRequest
->GetCORSMode();
3063 nsCOMPtr
<nsIReferrerInfo
> referrerInfo
= mRequest
->GetReferrerInfo();
3064 nsCOMPtr
<nsIPrincipal
> triggeringPrincipal
=
3065 mRequest
->GetTriggeringPrincipal();
3067 // Doom the old request's cache entry
3068 mRequest
->RemoveFromCache();
3070 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
3071 nsCOMPtr
<nsIURI
> originalURI
;
3072 channel
->GetOriginalURI(getter_AddRefs(originalURI
));
3073 nsresult rv
= mNewRequest
->Init(originalURI
, uri
, mHadInsecureRedirect
,
3074 aRequest
, channel
, mNewEntry
, document
,
3075 triggeringPrincipal
, corsmode
, referrerInfo
);
3076 if (NS_FAILED(rv
)) {
3077 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
3081 mDestListener
= new ProxyListener(mNewRequest
);
3083 // Try to add the new request into the cache. Note that the entry must be in
3084 // the cache before the proxies' ownership changes, because adding a proxy
3085 // changes the caching behaviour for imgRequests.
3086 mImgLoader
->PutIntoCache(mNewRequest
->CacheKey(), mNewEntry
);
3087 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3088 return mDestListener
->OnStartRequest(aRequest
);
3092 imgCacheValidator::OnStopRequest(nsIRequest
* aRequest
, nsresult status
) {
3093 // Be sure we've released the document that we may have been holding on to.
3094 mDocument
= nullptr;
3096 if (!mDestListener
) {
3100 return mDestListener
->OnStopRequest(aRequest
, status
);
3103 /** nsIStreamListener methods **/
3106 imgCacheValidator::OnDataAvailable(nsIRequest
* aRequest
, nsIInputStream
* inStr
,
3107 uint64_t sourceOffset
, uint32_t count
) {
3108 if (!mDestListener
) {
3109 // XXX see bug 113959
3111 inStr
->ReadSegments(NS_DiscardSegment
, nullptr, count
, &_retval
);
3115 return mDestListener
->OnDataAvailable(aRequest
, inStr
, sourceOffset
, count
);
3118 /** nsIThreadRetargetableStreamListener methods **/
3121 imgCacheValidator::CheckListenerChain() {
3122 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3123 nsresult rv
= NS_OK
;
3124 nsCOMPtr
<nsIThreadRetargetableStreamListener
> retargetableListener
=
3125 do_QueryInterface(mDestListener
, &rv
);
3126 if (retargetableListener
) {
3127 rv
= retargetableListener
->CheckListenerChain();
3130 gImgLog
, LogLevel::Debug
,
3131 ("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32
"=%s",
3132 this, static_cast<uint32_t>(rv
),
3133 NS_SUCCEEDED(rv
) ? "succeeded" : "failed"));
3137 /** nsIInterfaceRequestor methods **/
3140 imgCacheValidator::GetInterface(const nsIID
& aIID
, void** aResult
) {
3141 if (aIID
.Equals(NS_GET_IID(nsIChannelEventSink
))) {
3142 return QueryInterface(aIID
, aResult
);
3145 return mProgressProxy
->GetInterface(aIID
, aResult
);
3148 // These functions are materially the same as the same functions in imgRequest.
3149 // We duplicate them because we're verifying whether cache loads are necessary,
3150 // not unconditionally loading.
3152 /** nsIChannelEventSink methods **/
3154 imgCacheValidator::AsyncOnChannelRedirect(
3155 nsIChannel
* oldChannel
, nsIChannel
* newChannel
, uint32_t flags
,
3156 nsIAsyncVerifyRedirectCallback
* callback
) {
3157 // Note all cache information we get from the old channel.
3158 mNewRequest
->SetCacheValidation(mNewEntry
, oldChannel
);
3160 // If the previous URI is a non-HTTPS URI, record that fact for later use by
3161 // security code, which needs to know whether there is an insecure load at any
3162 // point in the redirect chain.
3163 nsCOMPtr
<nsIURI
> oldURI
;
3164 bool schemeLocal
= false;
3165 if (NS_FAILED(oldChannel
->GetURI(getter_AddRefs(oldURI
))) ||
3166 NS_FAILED(NS_URIChainHasFlags(
3167 oldURI
, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE
, &schemeLocal
)) ||
3168 (!oldURI
->SchemeIs("https") && !oldURI
->SchemeIs("chrome") &&
3170 mHadInsecureRedirect
= true;
3173 // Prepare for callback
3174 mRedirectCallback
= callback
;
3175 mRedirectChannel
= newChannel
;
3177 return mProgressProxy
->AsyncOnChannelRedirect(oldChannel
, newChannel
, flags
,
3182 imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult
) {
3183 // If we've already been told to abort, just do so.
3184 if (NS_FAILED(aResult
)) {
3185 mRedirectCallback
->OnRedirectVerifyCallback(aResult
);
3186 mRedirectCallback
= nullptr;
3187 mRedirectChannel
= nullptr;
3191 // make sure we have a protocol that returns data rather than opens
3192 // an external application, e.g. mailto:
3193 nsCOMPtr
<nsIURI
> uri
;
3194 mRedirectChannel
->GetURI(getter_AddRefs(uri
));
3195 bool doesNotReturnData
= false;
3196 NS_URIChainHasFlags(uri
, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA
,
3197 &doesNotReturnData
);
3199 nsresult result
= NS_OK
;
3201 if (doesNotReturnData
) {
3202 result
= NS_ERROR_ABORT
;
3205 mRedirectCallback
->OnRedirectVerifyCallback(result
);
3206 mRedirectCallback
= nullptr;
3207 mRedirectChannel
= nullptr;