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 "nsContentPolicyUtils.h"
37 #include "nsContentUtils.h"
38 #include "nsIApplicationCache.h"
39 #include "nsIApplicationCacheContainer.h"
40 #include "nsIAsyncVerifyRedirectCallback.h"
41 #include "nsICacheInfoChannel.h"
42 #include "nsIChannelEventSink.h"
43 #include "nsIClassOfService.h"
45 #include "nsIFileURL.h"
46 #include "nsIHttpChannel.h"
47 #include "nsIInterfaceRequestor.h"
48 #include "nsIInterfaceRequestorUtils.h"
49 #include "nsIMemoryReporter.h"
50 #include "nsINetworkPredictor.h"
51 #include "nsIProgressEventSink.h"
52 #include "nsIProtocolHandler.h"
53 #include "nsImageModule.h"
54 #include "nsMediaSniffer.h"
55 #include "nsMimeTypes.h"
57 #include "nsNetUtil.h"
58 #include "nsQueryObject.h"
59 #include "nsReadableUtils.h"
60 #include "nsStreamUtils.h"
63 // we want to explore making the document own the load group
64 // so we can associate the document URI with the load group.
65 // until this point, we have an evil hack:
66 #include "nsIHttpChannelInternal.h"
67 #include "nsILoadGroupChild.h"
68 #include "nsIDocShell.h"
70 using namespace mozilla
;
71 using namespace mozilla::dom
;
72 using namespace mozilla::image
;
73 using namespace mozilla::net
;
75 MOZ_DEFINE_MALLOC_SIZE_OF(ImagesMallocSizeOf
)
77 class imgMemoryReporter final
: public nsIMemoryReporter
{
78 ~imgMemoryReporter() = default;
83 NS_IMETHOD
CollectReports(nsIHandleReportCallback
* aHandleReport
,
84 nsISupports
* aData
, bool aAnonymize
) override
{
85 MOZ_ASSERT(NS_IsMainThread());
87 layers::CompositorManagerChild
* manager
=
88 mozilla::layers::CompositorManagerChild::GetInstance();
89 if (!manager
|| !StaticPrefs::image_mem_debug_reporting()) {
90 layers::SharedSurfacesMemoryReport sharedSurfaces
;
91 FinishCollectReports(aHandleReport
, aData
, aAnonymize
, sharedSurfaces
);
95 RefPtr
<imgMemoryReporter
> self(this);
96 nsCOMPtr
<nsIHandleReportCallback
> handleReport(aHandleReport
);
97 nsCOMPtr
<nsISupports
> data(aData
);
98 manager
->SendReportSharedSurfacesMemory(
99 [=](layers::SharedSurfacesMemoryReport aReport
) {
100 self
->FinishCollectReports(handleReport
, data
, aAnonymize
, aReport
);
102 [=](mozilla::ipc::ResponseRejectReason
&& aReason
) {
103 layers::SharedSurfacesMemoryReport sharedSurfaces
;
104 self
->FinishCollectReports(handleReport
, data
, aAnonymize
,
110 void FinishCollectReports(
111 nsIHandleReportCallback
* aHandleReport
, nsISupports
* aData
,
112 bool aAnonymize
, layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
113 nsTArray
<ImageMemoryCounter
> chrome
;
114 nsTArray
<ImageMemoryCounter
> content
;
115 nsTArray
<ImageMemoryCounter
> uncached
;
117 for (uint32_t i
= 0; i
< mKnownLoaders
.Length(); i
++) {
118 for (auto iter
= mKnownLoaders
[i
]->mChromeCache
.Iter(); !iter
.Done();
120 imgCacheEntry
* entry
= iter
.UserData();
121 RefPtr
<imgRequest
> req
= entry
->GetRequest();
122 RecordCounterForRequest(req
, &chrome
, !entry
->HasNoProxies());
124 for (auto iter
= mKnownLoaders
[i
]->mCache
.Iter(); !iter
.Done();
126 imgCacheEntry
* entry
= iter
.UserData();
127 RefPtr
<imgRequest
> req
= entry
->GetRequest();
128 RecordCounterForRequest(req
, &content
, !entry
->HasNoProxies());
130 MutexAutoLock
lock(mKnownLoaders
[i
]->mUncachedImagesMutex
);
131 for (auto iter
= mKnownLoaders
[i
]->mUncachedImages
.Iter(); !iter
.Done();
133 nsPtrHashKey
<imgRequest
>* entry
= iter
.Get();
134 RefPtr
<imgRequest
> req
= entry
->GetKey();
135 RecordCounterForRequest(req
, &uncached
, req
->HasConsumers());
139 // Note that we only need to anonymize content image URIs.
141 ReportCounterArray(aHandleReport
, aData
, chrome
, "images/chrome",
142 /* aAnonymize */ false, aSharedSurfaces
);
144 ReportCounterArray(aHandleReport
, aData
, content
, "images/content",
145 aAnonymize
, aSharedSurfaces
);
147 // Uncached images may be content or chrome, so anonymize them.
148 ReportCounterArray(aHandleReport
, aData
, uncached
, "images/uncached",
149 aAnonymize
, aSharedSurfaces
);
151 // Report any shared surfaces that were not merged with the surface cache.
152 ImageMemoryReporter::ReportSharedSurfaces(aHandleReport
, aData
,
155 nsCOMPtr
<nsIMemoryReporterManager
> imgr
=
156 do_GetService("@mozilla.org/memory-reporter-manager;1");
162 static int64_t ImagesContentUsedUncompressedDistinguishedAmount() {
164 for (uint32_t i
= 0; i
< imgLoader::sMemReporter
->mKnownLoaders
.Length();
166 for (auto iter
= imgLoader::sMemReporter
->mKnownLoaders
[i
]->mCache
.Iter();
167 !iter
.Done(); iter
.Next()) {
168 imgCacheEntry
* entry
= iter
.UserData();
169 if (entry
->HasNoProxies()) {
173 RefPtr
<imgRequest
> req
= entry
->GetRequest();
174 RefPtr
<image::Image
> image
= req
->GetImage();
179 // Both this and EntryImageSizes measure
180 // images/content/raster/used/decoded memory. This function's
181 // measurement is secondary -- the result doesn't go in the "explicit"
182 // tree -- so we use moz_malloc_size_of instead of ImagesMallocSizeOf to
183 // prevent DMD from seeing it reported twice.
184 SizeOfState
state(moz_malloc_size_of
);
185 ImageMemoryCounter
counter(req
, image
, state
, /* aIsUsed = */ true);
187 n
+= counter
.Values().DecodedHeap();
188 n
+= counter
.Values().DecodedNonHeap();
189 n
+= counter
.Values().DecodedUnknown();
195 void RegisterLoader(imgLoader
* aLoader
) {
196 mKnownLoaders
.AppendElement(aLoader
);
199 void UnregisterLoader(imgLoader
* aLoader
) {
200 mKnownLoaders
.RemoveElement(aLoader
);
204 nsTArray
<imgLoader
*> mKnownLoaders
;
207 MemoryTotal
& operator+=(const ImageMemoryCounter
& aImageCounter
) {
208 if (aImageCounter
.Type() == imgIContainer::TYPE_RASTER
) {
209 if (aImageCounter
.IsUsed()) {
210 mUsedRasterCounter
+= aImageCounter
.Values();
212 mUnusedRasterCounter
+= aImageCounter
.Values();
214 } else if (aImageCounter
.Type() == imgIContainer::TYPE_VECTOR
) {
215 if (aImageCounter
.IsUsed()) {
216 mUsedVectorCounter
+= aImageCounter
.Values();
218 mUnusedVectorCounter
+= aImageCounter
.Values();
220 } else if (aImageCounter
.Type() == imgIContainer::TYPE_REQUEST
) {
221 // Nothing to do, we did not get to the point of having an image.
223 MOZ_CRASH("Unexpected image type");
229 const MemoryCounter
& UsedRaster() const { return mUsedRasterCounter
; }
230 const MemoryCounter
& UnusedRaster() const { return mUnusedRasterCounter
; }
231 const MemoryCounter
& UsedVector() const { return mUsedVectorCounter
; }
232 const MemoryCounter
& UnusedVector() const { return mUnusedVectorCounter
; }
235 MemoryCounter mUsedRasterCounter
;
236 MemoryCounter mUnusedRasterCounter
;
237 MemoryCounter mUsedVectorCounter
;
238 MemoryCounter mUnusedVectorCounter
;
241 // Reports all images of a single kind, e.g. all used chrome images.
242 void ReportCounterArray(nsIHandleReportCallback
* aHandleReport
,
244 nsTArray
<ImageMemoryCounter
>& aCounterArray
,
245 const char* aPathPrefix
, bool aAnonymize
,
246 layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
247 MemoryTotal summaryTotal
;
248 MemoryTotal nonNotableTotal
;
250 // Report notable images, and compute total and non-notable aggregate sizes.
251 for (uint32_t i
= 0; i
< aCounterArray
.Length(); i
++) {
252 ImageMemoryCounter
& counter
= aCounterArray
[i
];
255 counter
.URI().Truncate();
256 counter
.URI().AppendPrintf("<anonymized-%u>", i
);
258 // The URI could be an extremely long data: URI. Truncate if needed.
259 static const size_t max
= 256;
260 if (counter
.URI().Length() > max
) {
261 counter
.URI().Truncate(max
);
262 counter
.URI().AppendLiteral(" (truncated)");
264 counter
.URI().ReplaceChar('/', '\\');
267 summaryTotal
+= counter
;
269 if (counter
.IsNotable() || StaticPrefs::image_mem_debug_reporting()) {
270 ReportImage(aHandleReport
, aData
, aPathPrefix
, counter
,
273 ImageMemoryReporter::TrimSharedSurfaces(counter
, aSharedSurfaces
);
274 nonNotableTotal
+= counter
;
278 // Report non-notable images in aggregate.
279 ReportTotal(aHandleReport
, aData
, /* aExplicit = */ true, aPathPrefix
,
280 "<non-notable images>/", nonNotableTotal
);
282 // Report a summary in aggregate, outside of the explicit tree.
283 ReportTotal(aHandleReport
, aData
, /* aExplicit = */ false, aPathPrefix
, "",
287 static void ReportImage(nsIHandleReportCallback
* aHandleReport
,
288 nsISupports
* aData
, const char* aPathPrefix
,
289 const ImageMemoryCounter
& aCounter
,
290 layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
291 nsAutoCString
pathPrefix("explicit/"_ns
);
292 pathPrefix
.Append(aPathPrefix
);
294 switch (aCounter
.Type()) {
295 case imgIContainer::TYPE_RASTER
:
296 pathPrefix
.AppendLiteral("/raster/");
298 case imgIContainer::TYPE_VECTOR
:
299 pathPrefix
.AppendLiteral("/vector/");
301 case imgIContainer::TYPE_REQUEST
:
302 pathPrefix
.AppendLiteral("/request/");
305 pathPrefix
.AppendLiteral("/unknown=");
306 pathPrefix
.AppendInt(aCounter
.Type());
307 pathPrefix
.AppendLiteral("/");
311 pathPrefix
.Append(aCounter
.IsUsed() ? "used/" : "unused/");
312 if (aCounter
.IsValidating()) {
313 pathPrefix
.AppendLiteral("validating/");
315 if (aCounter
.HasError()) {
316 pathPrefix
.AppendLiteral("err/");
319 pathPrefix
.AppendLiteral("progress=");
320 pathPrefix
.AppendInt(aCounter
.Progress(), 16);
321 pathPrefix
.AppendLiteral("/");
323 pathPrefix
.AppendLiteral("image(");
324 pathPrefix
.AppendInt(aCounter
.IntrinsicSize().width
);
325 pathPrefix
.AppendLiteral("x");
326 pathPrefix
.AppendInt(aCounter
.IntrinsicSize().height
);
327 pathPrefix
.AppendLiteral(", ");
329 if (aCounter
.URI().IsEmpty()) {
330 pathPrefix
.AppendLiteral("<unknown URI>");
332 pathPrefix
.Append(aCounter
.URI());
335 pathPrefix
.AppendLiteral(")/");
337 ReportSurfaces(aHandleReport
, aData
, pathPrefix
, aCounter
, aSharedSurfaces
);
339 ReportSourceValue(aHandleReport
, aData
, pathPrefix
, aCounter
.Values());
342 static void ReportSurfaces(
343 nsIHandleReportCallback
* aHandleReport
, nsISupports
* aData
,
344 const nsACString
& aPathPrefix
, const ImageMemoryCounter
& aCounter
,
345 layers::SharedSurfacesMemoryReport
& aSharedSurfaces
) {
346 for (const SurfaceMemoryCounter
& counter
: aCounter
.Surfaces()) {
347 nsAutoCString
surfacePathPrefix(aPathPrefix
);
348 if (counter
.IsLocked()) {
349 surfacePathPrefix
.AppendLiteral("locked/");
351 surfacePathPrefix
.AppendLiteral("unlocked/");
353 if (counter
.IsFactor2()) {
354 surfacePathPrefix
.AppendLiteral("factor2/");
356 if (counter
.CannotSubstitute()) {
357 surfacePathPrefix
.AppendLiteral("cannot_substitute/");
359 surfacePathPrefix
.AppendLiteral("types=");
360 surfacePathPrefix
.AppendInt(counter
.Values().SurfaceTypes(), 16);
361 surfacePathPrefix
.AppendLiteral("/surface(");
362 surfacePathPrefix
.AppendInt(counter
.Key().Size().width
);
363 surfacePathPrefix
.AppendLiteral("x");
364 surfacePathPrefix
.AppendInt(counter
.Key().Size().height
);
366 if (!counter
.IsFinished()) {
367 surfacePathPrefix
.AppendLiteral(", incomplete");
370 if (counter
.Values().ExternalHandles() > 0) {
371 surfacePathPrefix
.AppendLiteral(", handles:");
372 surfacePathPrefix
.AppendInt(
373 uint32_t(counter
.Values().ExternalHandles()));
376 ImageMemoryReporter::AppendSharedSurfacePrefix(surfacePathPrefix
, counter
,
379 if (counter
.Type() == SurfaceMemoryCounterType::NORMAL
) {
380 PlaybackType playback
= counter
.Key().Playback();
381 if (playback
== PlaybackType::eAnimated
) {
382 if (StaticPrefs::image_mem_debug_reporting()) {
383 surfacePathPrefix
.AppendPrintf(
384 " (animation %4u)", uint32_t(counter
.Values().FrameIndex()));
386 surfacePathPrefix
.AppendLiteral(" (animation)");
390 if (counter
.Key().Flags() != DefaultSurfaceFlags()) {
391 surfacePathPrefix
.AppendLiteral(", flags:");
392 surfacePathPrefix
.AppendInt(uint32_t(counter
.Key().Flags()),
396 if (counter
.Key().SVGContext()) {
397 const SVGImageContext
& context
= counter
.Key().SVGContext().ref();
398 surfacePathPrefix
.AppendLiteral(", svgContext:[ ");
399 if (context
.GetViewportSize()) {
400 const CSSIntSize
& size
= context
.GetViewportSize().ref();
401 surfacePathPrefix
.AppendLiteral("viewport=(");
402 surfacePathPrefix
.AppendInt(size
.width
);
403 surfacePathPrefix
.AppendLiteral("x");
404 surfacePathPrefix
.AppendInt(size
.height
);
405 surfacePathPrefix
.AppendLiteral(") ");
407 if (context
.GetPreserveAspectRatio()) {
409 context
.GetPreserveAspectRatio()->ToString(aspect
);
410 surfacePathPrefix
.AppendLiteral("preserveAspectRatio=(");
411 LossyAppendUTF16toASCII(aspect
, surfacePathPrefix
);
412 surfacePathPrefix
.AppendLiteral(") ");
414 if (context
.GetContextPaint()) {
415 const SVGEmbeddingContextPaint
* paint
= context
.GetContextPaint();
416 surfacePathPrefix
.AppendLiteral("contextPaint=(");
417 if (paint
->GetFill()) {
418 surfacePathPrefix
.AppendLiteral(" fill=");
419 surfacePathPrefix
.AppendInt(paint
->GetFill()->ToABGR(), 16);
421 if (paint
->GetFillOpacity()) {
422 surfacePathPrefix
.AppendLiteral(" fillOpa=");
423 surfacePathPrefix
.AppendFloat(paint
->GetFillOpacity());
425 if (paint
->GetStroke()) {
426 surfacePathPrefix
.AppendLiteral(" stroke=");
427 surfacePathPrefix
.AppendInt(paint
->GetStroke()->ToABGR(), 16);
429 if (paint
->GetStrokeOpacity()) {
430 surfacePathPrefix
.AppendLiteral(" strokeOpa=");
431 surfacePathPrefix
.AppendFloat(paint
->GetStrokeOpacity());
433 surfacePathPrefix
.AppendLiteral(" ) ");
435 surfacePathPrefix
.AppendLiteral("]");
437 } else if (counter
.Type() == SurfaceMemoryCounterType::COMPOSITING
) {
438 surfacePathPrefix
.AppendLiteral(", compositing frame");
439 } else if (counter
.Type() == SurfaceMemoryCounterType::COMPOSITING_PREV
) {
440 surfacePathPrefix
.AppendLiteral(", compositing prev frame");
442 MOZ_ASSERT_UNREACHABLE("Unknown counter type");
445 surfacePathPrefix
.AppendLiteral(")/");
447 ReportValues(aHandleReport
, aData
, surfacePathPrefix
, counter
.Values());
451 static void ReportTotal(nsIHandleReportCallback
* aHandleReport
,
452 nsISupports
* aData
, bool aExplicit
,
453 const char* aPathPrefix
, const char* aPathInfix
,
454 const MemoryTotal
& aTotal
) {
455 nsAutoCString pathPrefix
;
457 pathPrefix
.AppendLiteral("explicit/");
459 pathPrefix
.Append(aPathPrefix
);
461 nsAutoCString
rasterUsedPrefix(pathPrefix
);
462 rasterUsedPrefix
.AppendLiteral("/raster/used/");
463 rasterUsedPrefix
.Append(aPathInfix
);
464 ReportValues(aHandleReport
, aData
, rasterUsedPrefix
, aTotal
.UsedRaster());
466 nsAutoCString
rasterUnusedPrefix(pathPrefix
);
467 rasterUnusedPrefix
.AppendLiteral("/raster/unused/");
468 rasterUnusedPrefix
.Append(aPathInfix
);
469 ReportValues(aHandleReport
, aData
, rasterUnusedPrefix
,
470 aTotal
.UnusedRaster());
472 nsAutoCString
vectorUsedPrefix(pathPrefix
);
473 vectorUsedPrefix
.AppendLiteral("/vector/used/");
474 vectorUsedPrefix
.Append(aPathInfix
);
475 ReportValues(aHandleReport
, aData
, vectorUsedPrefix
, aTotal
.UsedVector());
477 nsAutoCString
vectorUnusedPrefix(pathPrefix
);
478 vectorUnusedPrefix
.AppendLiteral("/vector/unused/");
479 vectorUnusedPrefix
.Append(aPathInfix
);
480 ReportValues(aHandleReport
, aData
, vectorUnusedPrefix
,
481 aTotal
.UnusedVector());
484 static void ReportValues(nsIHandleReportCallback
* aHandleReport
,
485 nsISupports
* aData
, const nsACString
& aPathPrefix
,
486 const MemoryCounter
& aCounter
) {
487 ReportSourceValue(aHandleReport
, aData
, aPathPrefix
, aCounter
);
489 ReportValue(aHandleReport
, aData
, KIND_HEAP
, aPathPrefix
, "decoded-heap",
490 "Decoded image data which is stored on the heap.",
491 aCounter
.DecodedHeap());
493 ReportValue(aHandleReport
, aData
, KIND_NONHEAP
, aPathPrefix
,
495 "Decoded image data which isn't stored on the heap.",
496 aCounter
.DecodedNonHeap());
498 // We don't know for certain whether or not it is on the heap, so let's
499 // just report it as non-heap for reporting purposes.
500 ReportValue(aHandleReport
, aData
, KIND_NONHEAP
, aPathPrefix
,
502 "Decoded image data which is unknown to be on the heap or not.",
503 aCounter
.DecodedUnknown());
506 static void ReportSourceValue(nsIHandleReportCallback
* aHandleReport
,
508 const nsACString
& aPathPrefix
,
509 const MemoryCounter
& aCounter
) {
510 ReportValue(aHandleReport
, aData
, KIND_HEAP
, aPathPrefix
, "source",
511 "Raster image source data and vector image documents.",
515 static void ReportValue(nsIHandleReportCallback
* aHandleReport
,
516 nsISupports
* aData
, int32_t aKind
,
517 const nsACString
& aPathPrefix
,
518 const char* aPathSuffix
, const char* aDescription
,
524 nsAutoCString
desc(aDescription
);
525 nsAutoCString
path(aPathPrefix
);
526 path
.Append(aPathSuffix
);
528 aHandleReport
->Callback(""_ns
, path
, aKind
, UNITS_BYTES
, aValue
, desc
,
532 static void RecordCounterForRequest(imgRequest
* aRequest
,
533 nsTArray
<ImageMemoryCounter
>* aArray
,
535 SizeOfState
state(ImagesMallocSizeOf
);
536 RefPtr
<image::Image
> image
= aRequest
->GetImage();
538 ImageMemoryCounter
counter(aRequest
, image
, state
, aIsUsed
);
539 aArray
->AppendElement(std::move(counter
));
541 // We can at least record some information about the image from the
542 // request, and mark it as not knowing the image type yet.
543 ImageMemoryCounter
counter(aRequest
, state
, aIsUsed
);
544 aArray
->AppendElement(std::move(counter
));
549 NS_IMPL_ISUPPORTS(imgMemoryReporter
, nsIMemoryReporter
)
551 NS_IMPL_ISUPPORTS(nsProgressNotificationProxy
, nsIProgressEventSink
,
552 nsIChannelEventSink
, nsIInterfaceRequestor
)
555 nsProgressNotificationProxy::OnProgress(nsIRequest
* request
, int64_t progress
,
556 int64_t progressMax
) {
557 nsCOMPtr
<nsILoadGroup
> loadGroup
;
558 request
->GetLoadGroup(getter_AddRefs(loadGroup
));
560 nsCOMPtr
<nsIProgressEventSink
> target
;
561 NS_QueryNotificationCallbacks(mOriginalCallbacks
, loadGroup
,
562 NS_GET_IID(nsIProgressEventSink
),
563 getter_AddRefs(target
));
567 return target
->OnProgress(mImageRequest
, progress
, progressMax
);
571 nsProgressNotificationProxy::OnStatus(nsIRequest
* request
, nsresult status
,
572 const char16_t
* statusArg
) {
573 nsCOMPtr
<nsILoadGroup
> loadGroup
;
574 request
->GetLoadGroup(getter_AddRefs(loadGroup
));
576 nsCOMPtr
<nsIProgressEventSink
> target
;
577 NS_QueryNotificationCallbacks(mOriginalCallbacks
, loadGroup
,
578 NS_GET_IID(nsIProgressEventSink
),
579 getter_AddRefs(target
));
583 return target
->OnStatus(mImageRequest
, status
, statusArg
);
587 nsProgressNotificationProxy::AsyncOnChannelRedirect(
588 nsIChannel
* oldChannel
, nsIChannel
* newChannel
, uint32_t flags
,
589 nsIAsyncVerifyRedirectCallback
* cb
) {
590 // Tell the original original callbacks about it too
591 nsCOMPtr
<nsILoadGroup
> loadGroup
;
592 newChannel
->GetLoadGroup(getter_AddRefs(loadGroup
));
593 nsCOMPtr
<nsIChannelEventSink
> target
;
594 NS_QueryNotificationCallbacks(mOriginalCallbacks
, loadGroup
,
595 NS_GET_IID(nsIChannelEventSink
),
596 getter_AddRefs(target
));
598 cb
->OnRedirectVerifyCallback(NS_OK
);
602 // Delegate to |target| if set, reusing |cb|
603 return target
->AsyncOnChannelRedirect(oldChannel
, newChannel
, flags
, cb
);
607 nsProgressNotificationProxy::GetInterface(const nsIID
& iid
, void** result
) {
608 if (iid
.Equals(NS_GET_IID(nsIProgressEventSink
))) {
609 *result
= static_cast<nsIProgressEventSink
*>(this);
613 if (iid
.Equals(NS_GET_IID(nsIChannelEventSink
))) {
614 *result
= static_cast<nsIChannelEventSink
*>(this);
618 if (mOriginalCallbacks
) {
619 return mOriginalCallbacks
->GetInterface(iid
, result
);
621 return NS_NOINTERFACE
;
624 static void NewRequestAndEntry(bool aForcePrincipalCheckForCacheEntry
,
625 imgLoader
* aLoader
, const ImageCacheKey
& aKey
,
626 imgRequest
** aRequest
, imgCacheEntry
** aEntry
) {
627 RefPtr
<imgRequest
> request
= new imgRequest(aLoader
, aKey
);
628 RefPtr
<imgCacheEntry
> entry
=
629 new imgCacheEntry(aLoader
, request
, aForcePrincipalCheckForCacheEntry
);
630 aLoader
->AddToUncachedImages(request
);
631 request
.forget(aRequest
);
632 entry
.forget(aEntry
);
635 static bool ShouldRevalidateEntry(imgCacheEntry
* aEntry
, nsLoadFlags aFlags
,
637 bool bValidateEntry
= false;
639 if (aFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
643 if (aFlags
& nsIRequest::VALIDATE_ALWAYS
) {
644 bValidateEntry
= true;
645 } else if (aEntry
->GetMustValidate()) {
646 bValidateEntry
= true;
647 } else if (aHasExpired
) {
648 // The cache entry has expired... Determine whether the stale cache
649 // entry can be used without validation...
651 (nsIRequest::VALIDATE_NEVER
| nsIRequest::VALIDATE_ONCE_PER_SESSION
)) {
652 // VALIDATE_NEVER and VALIDATE_ONCE_PER_SESSION allow stale cache
653 // entries to be used unless they have been explicitly marked to
654 // indicate that revalidation is necessary.
655 bValidateEntry
= false;
657 } else if (!(aFlags
& nsIRequest::LOAD_FROM_CACHE
)) {
658 // LOAD_FROM_CACHE allows a stale cache entry to be used... Otherwise,
659 // the entry must be revalidated.
660 bValidateEntry
= true;
664 return bValidateEntry
;
667 /* Call content policies on cached images that went through a redirect */
668 static bool ShouldLoadCachedImage(imgRequest
* aImgRequest
,
669 Document
* aLoadingDocument
,
670 nsIPrincipal
* aTriggeringPrincipal
,
671 nsContentPolicyType aPolicyType
,
672 bool aSendCSPViolationReports
) {
673 /* Call content policies on cached images - Bug 1082837
674 * Cached images are keyed off of the first uri in a redirect chain.
675 * Hence content policies don't get a chance to test the intermediate hops
676 * or the final destination. Here we test the final destination using
677 * mFinalURI off of the imgRequest and passing it into content policies.
678 * For Mixed Content Blocker, we do an additional check to determine if any
679 * of the intermediary hops went through an insecure redirect with the
680 * mHadInsecureRedirect flag
682 bool insecureRedirect
= aImgRequest
->HadInsecureRedirect();
683 nsCOMPtr
<nsIURI
> contentLocation
;
684 aImgRequest
->GetFinalURI(getter_AddRefs(contentLocation
));
687 nsCOMPtr
<nsIPrincipal
> loadingPrincipal
=
688 aLoadingDocument
? aLoadingDocument
->NodePrincipal()
689 : aTriggeringPrincipal
;
690 // If there is no context and also no triggeringPrincipal, then we use a fresh
691 // nullPrincipal as the loadingPrincipal because we can not create a loadinfo
692 // without a valid loadingPrincipal.
693 if (!loadingPrincipal
) {
694 loadingPrincipal
= NullPrincipal::CreateWithoutOriginAttributes();
697 nsCOMPtr
<nsILoadInfo
> secCheckLoadInfo
= new LoadInfo(
698 loadingPrincipal
, aTriggeringPrincipal
, aLoadingDocument
,
699 nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK
, aPolicyType
);
701 secCheckLoadInfo
->SetSendCSPViolationEvents(aSendCSPViolationReports
);
703 int16_t decision
= nsIContentPolicy::REJECT_REQUEST
;
704 rv
= NS_CheckContentLoadPolicy(contentLocation
, secCheckLoadInfo
,
706 &decision
, nsContentUtils::GetContentPolicy());
707 if (NS_FAILED(rv
) || !NS_CP_ACCEPTED(decision
)) {
711 // We call all Content Policies above, but we also have to call mcb
712 // individually to check the intermediary redirect hops are secure.
713 if (insecureRedirect
) {
714 // Bug 1314356: If the image ended up in the cache upgraded by HSTS and the
715 // page uses upgrade-inscure-requests it had an insecure redirect
716 // (http->https). We need to invalidate the image and reload it because
717 // mixed content blocker only bails if upgrade-insecure-requests is set on
718 // the doc and the resource load is http: which would result in an incorrect
719 // mixed content warning.
720 nsCOMPtr
<nsIDocShell
> docShell
=
721 NS_CP_GetDocShellFromContext(ToSupports(aLoadingDocument
));
723 Document
* document
= docShell
->GetDocument();
724 if (document
&& document
->GetUpgradeInsecureRequests(false)) {
729 if (!aTriggeringPrincipal
|| !aTriggeringPrincipal
->IsSystemPrincipal()) {
730 // reset the decision for mixed content blocker check
731 decision
= nsIContentPolicy::REJECT_REQUEST
;
732 rv
= nsMixedContentBlocker::ShouldLoad(insecureRedirect
, contentLocation
,
735 true, // aReportError
737 if (NS_FAILED(rv
) || !NS_CP_ACCEPTED(decision
)) {
746 // Returns true if this request is compatible with the given CORS mode on the
747 // given loading principal, and false if the request may not be reused due
748 // to CORS. Also checks the Referrer Policy, since requests with different
749 // referrers/policies may generate different responses.
750 static bool ValidateSecurityInfo(imgRequest
* request
, bool forcePrincipalCheck
,
752 nsIPrincipal
* triggeringPrincipal
,
753 Document
* aLoadingDocument
,
754 nsContentPolicyType aPolicyType
) {
755 // If the entry's CORS mode doesn't match, or the CORS mode matches but the
756 // document principal isn't the same, we can't use this request.
757 if (request
->GetCORSMode() != corsmode
) {
760 if (request
->GetCORSMode() != imgIRequest::CORS_NONE
|| forcePrincipalCheck
) {
761 nsCOMPtr
<nsIPrincipal
> otherprincipal
= request
->GetTriggeringPrincipal();
763 // If we previously had a principal, but we don't now, we can't use this
765 if (otherprincipal
&& !triggeringPrincipal
) {
769 if (otherprincipal
&& triggeringPrincipal
) {
771 otherprincipal
->Equals(triggeringPrincipal
, &equals
);
778 // Content Policy Check on Cached Images
779 return ShouldLoadCachedImage(request
, aLoadingDocument
, triggeringPrincipal
,
781 /* aSendCSPViolationReports */ false);
784 static nsresult
NewImageChannel(
785 nsIChannel
** aResult
,
786 // If aForcePrincipalCheckForCacheEntry is true, then we will
787 // force a principal check even when not using CORS before
788 // assuming we have a cache hit on a cache entry that we
789 // create for this channel. This is an out param that should
790 // be set to true if this channel ends up depending on
791 // aTriggeringPrincipal and false otherwise.
792 bool* aForcePrincipalCheckForCacheEntry
, nsIURI
* aURI
,
793 nsIURI
* aInitialDocumentURI
, int32_t aCORSMode
,
794 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
795 nsLoadFlags aLoadFlags
, nsContentPolicyType aPolicyType
,
796 nsIPrincipal
* aTriggeringPrincipal
, nsINode
* aRequestingNode
,
797 bool aRespectPrivacy
) {
801 nsCOMPtr
<nsIHttpChannel
> newHttpChannel
;
803 nsCOMPtr
<nsIInterfaceRequestor
> callbacks
;
806 // Get the notification callbacks from the load group for the new channel.
808 // XXX: This is not exactly correct, because the network request could be
809 // referenced by multiple windows... However, the new channel needs
810 // something. So, using the 'first' notification callbacks is better
813 aLoadGroup
->GetNotificationCallbacks(getter_AddRefs(callbacks
));
816 // Pass in a nullptr loadgroup because this is the underlying network
817 // request. This request may be referenced by several proxy image requests
818 // (possibly in different documents).
819 // If all of the proxy requests are canceled then this request should be
823 nsSecurityFlags securityFlags
=
824 aCORSMode
== imgIRequest::CORS_NONE
825 ? nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT
826 : nsILoadInfo::SEC_REQUIRE_CORS_INHERITS_SEC_CONTEXT
;
827 if (aCORSMode
== imgIRequest::CORS_ANONYMOUS
) {
828 securityFlags
|= nsILoadInfo::SEC_COOKIES_SAME_ORIGIN
;
829 } else if (aCORSMode
== imgIRequest::CORS_USE_CREDENTIALS
) {
830 securityFlags
|= nsILoadInfo::SEC_COOKIES_INCLUDE
;
832 securityFlags
|= nsILoadInfo::SEC_ALLOW_CHROME
;
834 // Note we are calling NS_NewChannelWithTriggeringPrincipal() here with a
835 // node and a principal. This is for things like background images that are
836 // specified by user stylesheets, where the document is being styled, but
837 // the principal is that of the user stylesheet.
838 if (aRequestingNode
&& aTriggeringPrincipal
) {
839 rv
= NS_NewChannelWithTriggeringPrincipal(aResult
, aURI
, aRequestingNode
,
840 aTriggeringPrincipal
,
841 securityFlags
, aPolicyType
,
842 nullptr, // PerformanceStorage
843 nullptr, // loadGroup
844 callbacks
, aLoadFlags
);
850 if (aPolicyType
== nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON
) {
851 // If this is a favicon loading, we will use the originAttributes from the
852 // triggeringPrincipal as the channel's originAttributes. This allows the
853 // favicon loading from XUL will use the correct originAttributes.
855 nsCOMPtr
<nsILoadInfo
> loadInfo
= (*aResult
)->LoadInfo();
856 rv
= loadInfo
->SetOriginAttributes(
857 aTriggeringPrincipal
->OriginAttributesRef());
860 // either we are loading something inside a document, in which case
861 // we should always have a requestingNode, or we are loading something
862 // outside a document, in which case the triggeringPrincipal and
863 // triggeringPrincipal should always be the systemPrincipal.
864 // However, there are exceptions: one is Notifications which create a
865 // channel in the parent process in which case we can't get a
867 rv
= NS_NewChannel(aResult
, aURI
, nsContentUtils::GetSystemPrincipal(),
868 securityFlags
, aPolicyType
,
869 nullptr, // nsICookieJarSettings
870 nullptr, // PerformanceStorage
871 nullptr, // loadGroup
872 callbacks
, aLoadFlags
);
878 // Use the OriginAttributes from the loading principal, if one is available,
879 // and adjust the private browsing ID based on what kind of load the caller
880 // has asked us to perform.
881 OriginAttributes attrs
;
882 if (aTriggeringPrincipal
) {
883 attrs
= aTriggeringPrincipal
->OriginAttributesRef();
885 attrs
.mPrivateBrowsingId
= aRespectPrivacy
? 1 : 0;
887 nsCOMPtr
<nsILoadInfo
> loadInfo
= (*aResult
)->LoadInfo();
888 rv
= loadInfo
->SetOriginAttributes(attrs
);
895 // only inherit if we have a principal
896 *aForcePrincipalCheckForCacheEntry
=
897 aTriggeringPrincipal
&& nsContentUtils::ChannelShouldInheritPrincipal(
898 aTriggeringPrincipal
, aURI
,
899 /* aInheritForAboutBlank */ false,
900 /* aForceInherit */ false);
902 // Initialize HTTP-specific attributes
903 newHttpChannel
= do_QueryInterface(*aResult
);
904 if (newHttpChannel
) {
905 nsCOMPtr
<nsIHttpChannelInternal
> httpChannelInternal
=
906 do_QueryInterface(newHttpChannel
);
907 NS_ENSURE_TRUE(httpChannelInternal
, NS_ERROR_UNEXPECTED
);
908 rv
= httpChannelInternal
->SetDocumentURI(aInitialDocumentURI
);
909 MOZ_ASSERT(NS_SUCCEEDED(rv
));
911 DebugOnly
<nsresult
> rv
= newHttpChannel
->SetReferrerInfo(aReferrerInfo
);
912 MOZ_ASSERT(NS_SUCCEEDED(rv
));
916 // Image channels are loaded by default with reduced priority.
917 nsCOMPtr
<nsISupportsPriority
> p
= do_QueryInterface(*aResult
);
919 uint32_t priority
= nsISupportsPriority::PRIORITY_LOW
;
921 if (aLoadFlags
& nsIRequest::LOAD_BACKGROUND
) {
922 ++priority
; // further reduce priority for background loads
925 p
->AdjustPriority(priority
);
928 // Create a new loadgroup for this new channel, using the old group as
929 // the parent. The indirection keeps the channel insulated from cancels,
930 // but does allow a way for this revalidation to be associated with at
931 // least one base load group for scheduling/caching purposes.
933 nsCOMPtr
<nsILoadGroup
> loadGroup
= do_CreateInstance(NS_LOADGROUP_CONTRACTID
);
934 nsCOMPtr
<nsILoadGroupChild
> childLoadGroup
= do_QueryInterface(loadGroup
);
935 if (childLoadGroup
) {
936 childLoadGroup
->SetParentLoadGroup(aLoadGroup
);
938 (*aResult
)->SetLoadGroup(loadGroup
);
943 static uint32_t SecondsFromPRTime(PRTime aTime
) {
944 return nsContentUtils::SecondsFromPRTime(aTime
);
948 imgCacheEntry::imgCacheEntry(imgLoader
* loader
, imgRequest
* request
,
949 bool forcePrincipalCheck
)
953 mTouchedTime(SecondsFromPRTime(PR_Now())),
954 mLoadTime(SecondsFromPRTime(PR_Now())),
956 mMustValidate(false),
957 // We start off as evicted so we don't try to update the cache.
958 // PutIntoCache will set this to false.
961 mForcePrincipalCheck(forcePrincipalCheck
) {}
963 imgCacheEntry::~imgCacheEntry() {
964 LOG_FUNC(gImgLog
, "imgCacheEntry::~imgCacheEntry()");
967 void imgCacheEntry::Touch(bool updateTime
/* = true */) {
968 LOG_SCOPE(gImgLog
, "imgCacheEntry::Touch");
971 mTouchedTime
= SecondsFromPRTime(PR_Now());
977 void imgCacheEntry::UpdateCache(int32_t diff
/* = 0 */) {
978 // Don't update the cache if we've been removed from it or it doesn't care
979 // about our size or usage.
980 if (!Evicted() && HasNoProxies()) {
981 mLoader
->CacheEntriesChanged(mRequest
->IsChrome(), diff
);
985 void imgCacheEntry::UpdateLoadTime() {
986 mLoadTime
= SecondsFromPRTime(PR_Now());
989 void imgCacheEntry::SetHasNoProxies(bool hasNoProxies
) {
990 if (MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
992 LOG_FUNC_WITH_PARAM(gImgLog
, "imgCacheEntry::SetHasNoProxies true", "uri",
993 mRequest
->CacheKey().URI());
995 LOG_FUNC_WITH_PARAM(gImgLog
, "imgCacheEntry::SetHasNoProxies false",
996 "uri", mRequest
->CacheKey().URI());
1000 mHasNoProxies
= hasNoProxies
;
1003 imgCacheQueue::imgCacheQueue() : mDirty(false), mSize(0) {}
1005 void imgCacheQueue::UpdateSize(int32_t diff
) { mSize
+= diff
; }
1007 uint32_t imgCacheQueue::GetSize() const { return mSize
; }
1009 void imgCacheQueue::Remove(imgCacheEntry
* entry
) {
1010 uint64_t index
= mQueue
.IndexOf(entry
);
1011 if (index
== queueContainer::NoIndex
) {
1015 mSize
-= mQueue
[index
]->GetDataSize();
1017 // If the queue is clean and this is the first entry,
1018 // then we can efficiently remove the entry without
1019 // dirtying the sort order.
1020 if (!IsDirty() && index
== 0) {
1021 std::pop_heap(mQueue
.begin(), mQueue
.end(), imgLoader::CompareCacheEntries
);
1022 mQueue
.RemoveLastElement();
1026 // Remove from the middle of the list. This potentially
1027 // breaks the binary heap sort order.
1028 mQueue
.RemoveElementAt(index
);
1030 // If we only have one entry or the queue is empty, though,
1031 // then the sort order is still effectively good. Simply
1032 // refresh the list to clear the dirty flag.
1033 if (mQueue
.Length() <= 1) {
1038 // Otherwise we must mark the queue dirty and potentially
1039 // trigger an expensive sort later.
1043 void imgCacheQueue::Push(imgCacheEntry
* entry
) {
1044 mSize
+= entry
->GetDataSize();
1046 RefPtr
<imgCacheEntry
> refptr(entry
);
1047 mQueue
.AppendElement(std::move(refptr
));
1048 // If we're not dirty already, then we can efficiently add this to the
1049 // binary heap immediately. This is only O(log n).
1051 std::push_heap(mQueue
.begin(), mQueue
.end(),
1052 imgLoader::CompareCacheEntries
);
1056 already_AddRefed
<imgCacheEntry
> imgCacheQueue::Pop() {
1057 if (mQueue
.IsEmpty()) {
1064 std::pop_heap(mQueue
.begin(), mQueue
.end(), imgLoader::CompareCacheEntries
);
1065 RefPtr
<imgCacheEntry
> entry
= mQueue
.PopLastElement();
1067 mSize
-= entry
->GetDataSize();
1068 return entry
.forget();
1071 void imgCacheQueue::Refresh() {
1072 // Resort the list. This is an O(3 * n) operation and best avoided
1074 std::make_heap(mQueue
.begin(), mQueue
.end(), imgLoader::CompareCacheEntries
);
1078 void imgCacheQueue::MarkDirty() { mDirty
= true; }
1080 bool imgCacheQueue::IsDirty() { return mDirty
; }
1082 uint32_t imgCacheQueue::GetNumElements() const { return mQueue
.Length(); }
1084 bool imgCacheQueue::Contains(imgCacheEntry
* aEntry
) const {
1085 return mQueue
.Contains(aEntry
);
1088 imgCacheQueue::iterator
imgCacheQueue::begin() { return mQueue
.begin(); }
1090 imgCacheQueue::const_iterator
imgCacheQueue::begin() const {
1091 return mQueue
.begin();
1094 imgCacheQueue::iterator
imgCacheQueue::end() { return mQueue
.end(); }
1096 imgCacheQueue::const_iterator
imgCacheQueue::end() const {
1097 return mQueue
.end();
1100 nsresult
imgLoader::CreateNewProxyForRequest(
1101 imgRequest
* aRequest
, nsIURI
* aURI
, nsILoadGroup
* aLoadGroup
,
1102 Document
* aLoadingDocument
, imgINotificationObserver
* aObserver
,
1103 nsLoadFlags aLoadFlags
, imgRequestProxy
** _retval
) {
1104 LOG_SCOPE_WITH_PARAM(gImgLog
, "imgLoader::CreateNewProxyForRequest",
1105 "imgRequest", aRequest
);
1107 /* XXX If we move decoding onto separate threads, we should save off the
1108 calling thread here and pass it off to |proxyRequest| so that it call
1109 proxy calls to |aObserver|.
1112 RefPtr
<imgRequestProxy
> proxyRequest
= new imgRequestProxy();
1114 /* It is important to call |SetLoadFlags()| before calling |Init()| because
1115 |Init()| adds the request to the loadgroup.
1117 proxyRequest
->SetLoadFlags(aLoadFlags
);
1119 // init adds itself to imgRequest's list of observers
1120 nsresult rv
= proxyRequest
->Init(aRequest
, aLoadGroup
, aLoadingDocument
, aURI
,
1122 if (NS_WARN_IF(NS_FAILED(rv
))) {
1126 proxyRequest
.forget(_retval
);
1130 class imgCacheExpirationTracker final
1131 : public nsExpirationTracker
<imgCacheEntry
, 3> {
1132 enum { TIMEOUT_SECONDS
= 10 };
1135 imgCacheExpirationTracker();
1138 void NotifyExpired(imgCacheEntry
* entry
) override
;
1141 imgCacheExpirationTracker::imgCacheExpirationTracker()
1142 : nsExpirationTracker
<imgCacheEntry
, 3>(TIMEOUT_SECONDS
* 1000,
1143 "imgCacheExpirationTracker") {}
1145 void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry
* entry
) {
1146 // Hold on to a reference to this entry, because the expiration tracker
1147 // mechanism doesn't.
1148 RefPtr
<imgCacheEntry
> kungFuDeathGrip(entry
);
1150 if (MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1151 RefPtr
<imgRequest
> req
= entry
->GetRequest();
1153 LOG_FUNC_WITH_PARAM(gImgLog
, "imgCacheExpirationTracker::NotifyExpired",
1154 "entry", req
->CacheKey().URI());
1158 // We can be called multiple times on the same entry. Don't do work multiple
1160 if (!entry
->Evicted()) {
1161 entry
->Loader()->RemoveFromCache(entry
);
1164 entry
->Loader()->VerifyCacheSizes();
1167 ///////////////////////////////////////////////////////////////////////////////
1169 ///////////////////////////////////////////////////////////////////////////////
1171 double imgLoader::sCacheTimeWeight
;
1172 uint32_t imgLoader::sCacheMaxSize
;
1173 imgMemoryReporter
* imgLoader::sMemReporter
;
1175 NS_IMPL_ISUPPORTS(imgLoader
, imgILoader
, nsIContentSniffer
, imgICache
,
1176 nsISupportsWeakReference
, nsIObserver
)
1178 static imgLoader
* gNormalLoader
= nullptr;
1179 static imgLoader
* gPrivateBrowsingLoader
= nullptr;
1182 mozilla::CORSMode
imgLoader::ConvertToCORSMode(uint32_t aImgCORS
) {
1184 case imgIRequest::CORS_NONE
:
1185 return CORSMode::CORS_NONE
;
1186 case imgIRequest::CORS_ANONYMOUS
:
1187 return CORSMode::CORS_ANONYMOUS
;
1188 case imgIRequest::CORS_USE_CREDENTIALS
:
1189 return CORSMode::CORS_USE_CREDENTIALS
;
1192 MOZ_ASSERT(false, "Unexpected imgIRequest CORS value");
1193 return CORSMode::CORS_NONE
;
1197 already_AddRefed
<imgLoader
> imgLoader::CreateImageLoader() {
1198 // In some cases, such as xpctests, XPCOM modules are not automatically
1199 // initialized. We need to make sure that our module is initialized before
1200 // we hand out imgLoader instances and code starts using them.
1201 mozilla::image::EnsureModuleInitialized();
1203 RefPtr
<imgLoader
> loader
= new imgLoader();
1206 return loader
.forget();
1209 imgLoader
* imgLoader::NormalLoader() {
1210 if (!gNormalLoader
) {
1211 gNormalLoader
= CreateImageLoader().take();
1213 return gNormalLoader
;
1216 imgLoader
* imgLoader::PrivateBrowsingLoader() {
1217 if (!gPrivateBrowsingLoader
) {
1218 gPrivateBrowsingLoader
= CreateImageLoader().take();
1219 gPrivateBrowsingLoader
->RespectPrivacyNotifications();
1221 return gPrivateBrowsingLoader
;
1224 imgLoader::imgLoader()
1225 : mUncachedImagesMutex("imgLoader::UncachedImages"),
1226 mRespectPrivacy(false) {
1227 sMemReporter
->AddRef();
1228 sMemReporter
->RegisterLoader(this);
1231 imgLoader::~imgLoader() {
1232 ClearChromeImageCache();
1235 // If there are any of our imgRequest's left they are in the uncached
1236 // images set, so clear their pointer to us.
1237 MutexAutoLock
lock(mUncachedImagesMutex
);
1238 for (auto iter
= mUncachedImages
.Iter(); !iter
.Done(); iter
.Next()) {
1239 nsPtrHashKey
<imgRequest
>* entry
= iter
.Get();
1240 RefPtr
<imgRequest
> req
= entry
->GetKey();
1244 sMemReporter
->UnregisterLoader(this);
1245 sMemReporter
->Release();
1248 void imgLoader::VerifyCacheSizes() {
1250 if (!mCacheTracker
) {
1254 uint32_t cachesize
= mCache
.Count() + mChromeCache
.Count();
1255 uint32_t queuesize
=
1256 mCacheQueue
.GetNumElements() + mChromeCacheQueue
.GetNumElements();
1257 uint32_t trackersize
= 0;
1258 for (nsExpirationTracker
<imgCacheEntry
, 3>::Iterator
it(mCacheTracker
.get());
1262 MOZ_ASSERT(queuesize
== trackersize
, "Queue and tracker sizes out of sync!");
1263 MOZ_ASSERT(queuesize
<= cachesize
, "Queue has more elements than cache!");
1267 imgLoader::imgCacheTable
& imgLoader::GetCache(bool aForChrome
) {
1268 return aForChrome
? mChromeCache
: mCache
;
1271 imgLoader::imgCacheTable
& imgLoader::GetCache(const ImageCacheKey
& aKey
) {
1272 return GetCache(aKey
.IsChrome());
1275 imgCacheQueue
& imgLoader::GetCacheQueue(bool aForChrome
) {
1276 return aForChrome
? mChromeCacheQueue
: mCacheQueue
;
1279 imgCacheQueue
& imgLoader::GetCacheQueue(const ImageCacheKey
& aKey
) {
1280 return GetCacheQueue(aKey
.IsChrome());
1283 void imgLoader::GlobalInit() {
1284 sCacheTimeWeight
= StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
1285 int32_t cachesize
= StaticPrefs::image_cache_size_AtStartup();
1286 sCacheMaxSize
= cachesize
> 0 ? cachesize
: 0;
1288 sMemReporter
= new imgMemoryReporter();
1289 RegisterStrongAsyncMemoryReporter(sMemReporter
);
1290 RegisterImagesContentUsedUncompressedDistinguishedAmount(
1291 imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount
);
1294 void imgLoader::ShutdownMemoryReporter() {
1295 UnregisterImagesContentUsedUncompressedDistinguishedAmount();
1296 UnregisterStrongMemoryReporter(sMemReporter
);
1299 nsresult
imgLoader::InitCache() {
1300 nsCOMPtr
<nsIObserverService
> os
= mozilla::services::GetObserverService();
1302 return NS_ERROR_FAILURE
;
1305 os
->AddObserver(this, "memory-pressure", false);
1306 os
->AddObserver(this, "chrome-flush-caches", false);
1307 os
->AddObserver(this, "last-pb-context-exited", false);
1308 os
->AddObserver(this, "profile-before-change", false);
1309 os
->AddObserver(this, "xpcom-shutdown", false);
1311 mCacheTracker
= MakeUnique
<imgCacheExpirationTracker
>();
1316 nsresult
imgLoader::Init() {
1323 imgLoader::RespectPrivacyNotifications() {
1324 mRespectPrivacy
= true;
1329 imgLoader::Observe(nsISupports
* aSubject
, const char* aTopic
,
1330 const char16_t
* aData
) {
1331 if (strcmp(aTopic
, "memory-pressure") == 0) {
1333 } else if (strcmp(aTopic
, "chrome-flush-caches") == 0) {
1335 ClearChromeImageCache();
1336 } else if (strcmp(aTopic
, "last-pb-context-exited") == 0) {
1337 if (mRespectPrivacy
) {
1339 ClearChromeImageCache();
1341 } else if (strcmp(aTopic
, "profile-before-change") == 0) {
1342 mCacheTracker
= nullptr;
1343 } else if (strcmp(aTopic
, "xpcom-shutdown") == 0) {
1344 mCacheTracker
= nullptr;
1345 ShutdownMemoryReporter();
1348 // (Nothing else should bring us here)
1349 MOZ_ASSERT(0, "Invalid topic received");
1356 imgLoader::ClearCache(bool chrome
) {
1357 if (XRE_IsParentProcess()) {
1358 bool privateLoader
= this == gPrivateBrowsingLoader
;
1359 for (auto* cp
: ContentParent::AllProcesses(ContentParent::eLive
)) {
1360 Unused
<< cp
->SendClearImageCache(privateLoader
, chrome
);
1365 return ClearChromeImageCache();
1367 return ClearImageCache();
1371 imgLoader::RemoveEntriesFromPrincipal(nsIPrincipal
* aPrincipal
) {
1372 nsAutoString origin
;
1373 nsresult rv
= nsContentUtils::GetUTFOrigin(aPrincipal
, origin
);
1374 if (NS_WARN_IF(NS_FAILED(rv
))) {
1378 AutoTArray
<RefPtr
<imgCacheEntry
>, 128> entriesToBeRemoved
;
1380 imgCacheTable
& cache
= GetCache(aPrincipal
->IsSystemPrincipal());
1381 for (auto iter
= cache
.Iter(); !iter
.Done(); iter
.Next()) {
1382 auto& key
= iter
.Key();
1384 if (key
.OriginAttributesRef() !=
1385 BasePrincipal::Cast(aPrincipal
)->OriginAttributesRef()) {
1389 nsAutoString imageOrigin
;
1390 nsresult rv
= nsContentUtils::GetUTFOrigin(key
.URI(), imageOrigin
);
1391 if (NS_WARN_IF(NS_FAILED(rv
))) {
1395 if (imageOrigin
== origin
) {
1396 entriesToBeRemoved
.AppendElement(iter
.Data());
1400 for (auto& entry
: entriesToBeRemoved
) {
1401 if (!RemoveFromCache(entry
)) {
1403 "Couldn't remove an entry from the cache in "
1404 "RemoveEntriesFromPrincipal()\n");
1412 imgLoader::RemoveEntry(nsIURI
* aURI
, Document
* aDoc
) {
1414 OriginAttributes attrs
;
1416 nsCOMPtr
<nsIPrincipal
> principal
= aDoc
->NodePrincipal();
1418 attrs
= principal
->OriginAttributesRef();
1422 ImageCacheKey
key(aURI
, attrs
, aDoc
);
1423 if (RemoveFromCache(key
)) {
1427 return NS_ERROR_NOT_AVAILABLE
;
1431 imgLoader::FindEntryProperties(nsIURI
* uri
, Document
* aDoc
,
1432 nsIProperties
** _retval
) {
1435 OriginAttributes attrs
;
1437 nsCOMPtr
<nsIPrincipal
> principal
= aDoc
->NodePrincipal();
1439 attrs
= principal
->OriginAttributesRef();
1443 ImageCacheKey
key(uri
, attrs
, aDoc
);
1444 imgCacheTable
& cache
= GetCache(key
);
1446 RefPtr
<imgCacheEntry
> entry
;
1447 if (cache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
1448 if (mCacheTracker
&& entry
->HasNoProxies()) {
1449 mCacheTracker
->MarkUsed(entry
);
1452 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1454 nsCOMPtr
<nsIProperties
> properties
= request
->Properties();
1455 properties
.forget(_retval
);
1462 NS_IMETHODIMP_(void)
1463 imgLoader::ClearCacheForControlledDocument(Document
* aDoc
) {
1465 AutoTArray
<RefPtr
<imgCacheEntry
>, 128> entriesToBeRemoved
;
1466 imgCacheTable
& cache
= GetCache(false);
1467 for (auto iter
= cache
.Iter(); !iter
.Done(); iter
.Next()) {
1468 auto& key
= iter
.Key();
1469 if (key
.ControlledDocument() == aDoc
) {
1470 entriesToBeRemoved
.AppendElement(iter
.Data());
1473 for (auto& entry
: entriesToBeRemoved
) {
1474 if (!RemoveFromCache(entry
)) {
1476 "Couldn't remove an entry from the cache in "
1477 "ClearCacheForControlledDocument()\n");
1482 void imgLoader::Shutdown() {
1483 NS_IF_RELEASE(gNormalLoader
);
1484 gNormalLoader
= nullptr;
1485 NS_IF_RELEASE(gPrivateBrowsingLoader
);
1486 gPrivateBrowsingLoader
= nullptr;
1489 nsresult
imgLoader::ClearChromeImageCache() {
1490 return EvictEntries(mChromeCache
);
1493 nsresult
imgLoader::ClearImageCache() { return EvictEntries(mCache
); }
1495 void imgLoader::MinimizeCaches() {
1496 EvictEntries(mCacheQueue
);
1497 EvictEntries(mChromeCacheQueue
);
1500 bool imgLoader::PutIntoCache(const ImageCacheKey
& aKey
, imgCacheEntry
* entry
) {
1501 imgCacheTable
& cache
= GetCache(aKey
);
1503 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::PutIntoCache", "uri",
1506 // Check to see if this request already exists in the cache. If so, we'll
1507 // replace the old version.
1508 RefPtr
<imgCacheEntry
> tmpCacheEntry
;
1509 if (cache
.Get(aKey
, getter_AddRefs(tmpCacheEntry
)) && tmpCacheEntry
) {
1511 gImgLog
, LogLevel::Debug
,
1512 ("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
1514 RefPtr
<imgRequest
> tmpRequest
= tmpCacheEntry
->GetRequest();
1516 // If it already exists, and we're putting the same key into the cache, we
1517 // should remove the old version.
1518 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1519 ("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
1522 RemoveFromCache(aKey
);
1524 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1525 ("[this=%p] imgLoader::PutIntoCache --"
1526 " Element NOT already in the cache",
1530 cache
.Put(aKey
, RefPtr
{entry
});
1532 // We can be called to resurrect an evicted entry.
1533 if (entry
->Evicted()) {
1534 entry
->SetEvicted(false);
1537 // If we're resurrecting an entry with no proxies, put it back in the
1538 // tracker and queue.
1539 if (entry
->HasNoProxies()) {
1540 nsresult addrv
= NS_OK
;
1542 if (mCacheTracker
) {
1543 addrv
= mCacheTracker
->AddObject(entry
);
1546 if (NS_SUCCEEDED(addrv
)) {
1547 imgCacheQueue
& queue
= GetCacheQueue(aKey
);
1552 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1553 request
->SetIsInCache(true);
1554 RemoveFromUncachedImages(request
);
1559 bool imgLoader::SetHasNoProxies(imgRequest
* aRequest
, imgCacheEntry
* aEntry
) {
1560 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::SetHasNoProxies", "uri",
1561 aRequest
->CacheKey().URI());
1563 aEntry
->SetHasNoProxies(true);
1565 if (aEntry
->Evicted()) {
1569 imgCacheQueue
& queue
= GetCacheQueue(aRequest
->IsChrome());
1571 nsresult addrv
= NS_OK
;
1573 if (mCacheTracker
) {
1574 addrv
= mCacheTracker
->AddObject(aEntry
);
1577 if (NS_SUCCEEDED(addrv
)) {
1581 imgCacheTable
& cache
= GetCache(aRequest
->IsChrome());
1582 CheckCacheLimits(cache
, queue
);
1587 bool imgLoader::SetHasProxies(imgRequest
* aRequest
) {
1590 const ImageCacheKey
& key
= aRequest
->CacheKey();
1591 imgCacheTable
& cache
= GetCache(key
);
1593 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::SetHasProxies", "uri",
1596 RefPtr
<imgCacheEntry
> entry
;
1597 if (cache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
1598 // Make sure the cache entry is for the right request
1599 RefPtr
<imgRequest
> entryRequest
= entry
->GetRequest();
1600 if (entryRequest
== aRequest
&& entry
->HasNoProxies()) {
1601 imgCacheQueue
& queue
= GetCacheQueue(key
);
1602 queue
.Remove(entry
);
1604 if (mCacheTracker
) {
1605 mCacheTracker
->RemoveObject(entry
);
1608 entry
->SetHasNoProxies(false);
1617 void imgLoader::CacheEntriesChanged(bool aForChrome
,
1618 int32_t aSizeDiff
/* = 0 */) {
1619 imgCacheQueue
& queue
= GetCacheQueue(aForChrome
);
1620 // We only need to dirty the queue if there is any sorting
1621 // taking place. Empty or single-entry lists can't become
1623 if (queue
.GetNumElements() > 1) {
1626 queue
.UpdateSize(aSizeDiff
);
1629 void imgLoader::CheckCacheLimits(imgCacheTable
& cache
, imgCacheQueue
& queue
) {
1630 if (queue
.GetNumElements() == 0) {
1631 NS_ASSERTION(queue
.GetSize() == 0,
1632 "imgLoader::CheckCacheLimits -- incorrect cache size");
1635 // Remove entries from the cache until we're back at our desired max size.
1636 while (queue
.GetSize() > sCacheMaxSize
) {
1637 // Remove the first entry in the queue.
1638 RefPtr
<imgCacheEntry
> entry(queue
.Pop());
1640 NS_ASSERTION(entry
, "imgLoader::CheckCacheLimits -- NULL entry pointer");
1642 if (MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1643 RefPtr
<imgRequest
> req
= entry
->GetRequest();
1645 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::CheckCacheLimits",
1646 "entry", req
->CacheKey().URI());
1651 // We just popped this entry from the queue, so pass AlreadyRemoved
1652 // to avoid searching the queue again in RemoveFromCache.
1653 RemoveFromCache(entry
, QueueState::AlreadyRemoved
);
1658 bool imgLoader::ValidateRequestWithNewChannel(
1659 imgRequest
* request
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
1660 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
1661 imgINotificationObserver
* aObserver
, Document
* aLoadingDocument
,
1662 uint64_t aInnerWindowId
, nsLoadFlags aLoadFlags
,
1663 nsContentPolicyType aLoadPolicyType
, imgRequestProxy
** aProxyRequest
,
1664 nsIPrincipal
* aTriggeringPrincipal
, int32_t aCORSMode
, bool aLinkPreload
,
1665 bool* aNewChannelCreated
) {
1666 // now we need to insert a new channel request object in between the real
1667 // request and the proxy that basically delays loading the image until it
1668 // gets a 304 or figures out that this needs to be a new request
1672 // If we're currently in the middle of validating this request, just hand
1673 // back a proxy to it; the required work will be done for us.
1674 if (imgCacheValidator
* validator
= request
->GetValidator()) {
1675 rv
= CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
1676 aObserver
, aLoadFlags
, aProxyRequest
);
1677 if (NS_FAILED(rv
)) {
1681 if (*aProxyRequest
) {
1682 imgRequestProxy
* proxy
= static_cast<imgRequestProxy
*>(*aProxyRequest
);
1684 // We will send notifications from imgCacheValidator::OnStartRequest().
1685 // In the mean time, we must defer notifications because we are added to
1686 // the imgRequest's proxy list, and we can get extra notifications
1687 // resulting from methods such as StartDecoding(). See bug 579122.
1688 proxy
->MarkValidating();
1691 MOZ_ASSERT(aLoadingDocument
);
1692 proxy
->PrioritizeAsPreload();
1693 auto preloadKey
= PreloadHashKey::CreateAsImage(
1694 aURI
, aTriggeringPrincipal
, ConvertToCORSMode(aCORSMode
));
1695 proxy
->NotifyOpen(preloadKey
, aLoadingDocument
, true);
1698 // Attach the proxy without notifying
1699 validator
->AddProxy(proxy
);
1704 // We will rely on Necko to cache this request when it's possible, and to
1705 // tell imgCacheValidator::OnStartRequest whether the request came from its
1707 nsCOMPtr
<nsIChannel
> newChannel
;
1708 bool forcePrincipalCheck
;
1709 rv
= NewImageChannel(getter_AddRefs(newChannel
), &forcePrincipalCheck
, aURI
,
1710 aInitialDocumentURI
, aCORSMode
, aReferrerInfo
,
1711 aLoadGroup
, aLoadFlags
, aLoadPolicyType
,
1712 aTriggeringPrincipal
, aLoadingDocument
, mRespectPrivacy
);
1713 if (NS_FAILED(rv
)) {
1717 if (aNewChannelCreated
) {
1718 *aNewChannelCreated
= true;
1721 RefPtr
<imgRequestProxy
> req
;
1722 rv
= CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
1723 aObserver
, aLoadFlags
, getter_AddRefs(req
));
1724 if (NS_FAILED(rv
)) {
1728 // Make sure that OnStatus/OnProgress calls have the right request set...
1729 RefPtr
<nsProgressNotificationProxy
> progressproxy
=
1730 new nsProgressNotificationProxy(newChannel
, req
);
1731 if (!progressproxy
) {
1735 RefPtr
<imgCacheValidator
> hvc
=
1736 new imgCacheValidator(progressproxy
, this, request
, aLoadingDocument
,
1737 aInnerWindowId
, forcePrincipalCheck
);
1739 // Casting needed here to get past multiple inheritance.
1740 nsCOMPtr
<nsIStreamListener
> listener
=
1741 do_QueryInterface(static_cast<nsIThreadRetargetableStreamListener
*>(hvc
));
1742 NS_ENSURE_TRUE(listener
, false);
1744 // We must set the notification callbacks before setting up the
1745 // CORS listener, because that's also interested inthe
1746 // notification callbacks.
1747 newChannel
->SetNotificationCallbacks(hvc
);
1749 request
->SetValidator(hvc
);
1751 // We will send notifications from imgCacheValidator::OnStartRequest().
1752 // In the mean time, we must defer notifications because we are added to
1753 // the imgRequest's proxy list, and we can get extra notifications
1754 // resulting from methods such as StartDecoding(). See bug 579122.
1755 req
->MarkValidating();
1758 MOZ_ASSERT(aLoadingDocument
);
1759 req
->PrioritizeAsPreload();
1760 auto preloadKey
= PreloadHashKey::CreateAsImage(
1761 aURI
, aTriggeringPrincipal
, ConvertToCORSMode(aCORSMode
));
1762 req
->NotifyOpen(preloadKey
, aLoadingDocument
, true);
1765 // Add the proxy without notifying
1768 mozilla::net::PredictorLearn(aURI
, aInitialDocumentURI
,
1769 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE
,
1771 rv
= newChannel
->AsyncOpen(listener
);
1772 if (NS_WARN_IF(NS_FAILED(rv
))) {
1773 req
->CancelAndForgetObserver(rv
);
1774 // This will notify any current or future <link preload> tags. Pass the
1775 // non-open channel so that we can read loadinfo and referrer info of that
1777 req
->NotifyStart(newChannel
);
1778 // Use the non-channel overload of this method to force the notification to
1779 // happen. The preload request has not been assigned a channel.
1780 req
->NotifyStop(rv
);
1784 req
.forget(aProxyRequest
);
1788 bool imgLoader::ValidateEntry(
1789 imgCacheEntry
* aEntry
, nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
,
1790 nsIReferrerInfo
* aReferrerInfo
, nsILoadGroup
* aLoadGroup
,
1791 imgINotificationObserver
* aObserver
, Document
* aLoadingDocument
,
1792 nsLoadFlags aLoadFlags
, nsContentPolicyType aLoadPolicyType
,
1793 bool aCanMakeNewChannel
, bool* aNewChannelCreated
,
1794 imgRequestProxy
** aProxyRequest
, nsIPrincipal
* aTriggeringPrincipal
,
1795 int32_t aCORSMode
, bool aLinkPreload
) {
1796 LOG_SCOPE(gImgLog
, "imgLoader::ValidateEntry");
1798 // If the expiration time is zero, then the request has not gotten far enough
1799 // to know when it will expire.
1800 uint32_t expiryTime
= aEntry
->GetExpiryTime();
1802 expiryTime
!= 0 && expiryTime
<= SecondsFromPRTime(PR_Now());
1806 // Special treatment for file URLs - aEntry has expired if file has changed
1807 nsCOMPtr
<nsIFileURL
> fileUrl(do_QueryInterface(aURI
));
1809 uint32_t lastModTime
= aEntry
->GetLoadTime();
1811 nsCOMPtr
<nsIFile
> theFile
;
1812 rv
= fileUrl
->GetFile(getter_AddRefs(theFile
));
1813 if (NS_SUCCEEDED(rv
)) {
1815 rv
= theFile
->GetLastModifiedTime(&fileLastMod
);
1816 if (NS_SUCCEEDED(rv
)) {
1817 // nsIFile uses millisec, NSPR usec
1818 fileLastMod
*= 1000;
1819 hasExpired
= SecondsFromPRTime((PRTime
)fileLastMod
) > lastModTime
;
1824 RefPtr
<imgRequest
> request(aEntry
->GetRequest());
1830 if (!ValidateSecurityInfo(request
, aEntry
->ForcePrincipalCheck(), aCORSMode
,
1831 aTriggeringPrincipal
, aLoadingDocument
,
1836 // data URIs are immutable and by their nature can't leak data, so we can
1837 // just return true in that case. Doing so would mean that shift-reload
1838 // doesn't reload data URI documents/images though (which is handy for
1839 // debugging during gecko development) so we make an exception in that case.
1840 nsAutoCString scheme
;
1841 aURI
->GetScheme(scheme
);
1842 if (scheme
.EqualsLiteral("data") &&
1843 !(aLoadFlags
& nsIRequest::LOAD_BYPASS_CACHE
)) {
1847 bool validateRequest
= false;
1849 if (!request
->CanReuseWithoutValidation(aLoadingDocument
)) {
1850 // If we would need to revalidate this entry, but we're being told to
1851 // bypass the cache, we don't allow this entry to be used.
1852 if (aLoadFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
1856 if (MOZ_UNLIKELY(ChaosMode::isActive(ChaosFeature::ImageCache
))) {
1857 if (ChaosMode::randomUint32LessThan(4) < 1) {
1862 // Determine whether the cache aEntry must be revalidated...
1863 validateRequest
= ShouldRevalidateEntry(aEntry
, aLoadFlags
, hasExpired
);
1865 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1866 ("imgLoader::ValidateEntry validating cache entry. "
1867 "validateRequest = %d",
1869 } else if (!aLoadingDocument
&& MOZ_LOG_TEST(gImgLog
, LogLevel::Debug
)) {
1870 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1871 ("imgLoader::ValidateEntry BYPASSING cache validation for %s "
1872 "because of NULL loading document",
1873 aURI
->GetSpecOrDefault().get()));
1876 // We can't use a cached request if it comes from a different
1877 // application cache than this load is expecting.
1878 nsCOMPtr
<nsIApplicationCacheContainer
> appCacheContainer
;
1879 nsCOMPtr
<nsIApplicationCache
> requestAppCache
;
1880 nsCOMPtr
<nsIApplicationCache
> groupAppCache
;
1881 if ((appCacheContainer
= do_GetInterface(request
->GetRequest()))) {
1882 appCacheContainer
->GetApplicationCache(getter_AddRefs(requestAppCache
));
1884 if ((appCacheContainer
= do_QueryInterface(aLoadGroup
))) {
1885 appCacheContainer
->GetApplicationCache(getter_AddRefs(groupAppCache
));
1888 if (requestAppCache
!= groupAppCache
) {
1889 MOZ_LOG(gImgLog
, LogLevel::Debug
,
1890 ("imgLoader::ValidateEntry - Unable to use cached imgRequest "
1891 "[request=%p] because of mismatched application caches\n",
1892 address_of(request
)));
1896 // If the original request is still transferring don't kick off a validation
1897 // network request because it is a bit silly to issue a validation request if
1898 // the original request hasn't even finished yet. So just return true
1899 // indicating the caller can create a new proxy for the request and use it as
1901 // This is an optimization but it's also required for correctness. If we don't
1902 // do this then when firing the load complete notification for the original
1903 // request that can unblock load for the document and then spin the event loop
1904 // (see the stack in bug 1641682) which then the OnStartRequest for the
1905 // validation request can fire which can call UpdateProxies and can sync
1906 // notify on the progress tracker about all existing state, which includes
1907 // load complete, so we fire a second load complete notification for the
1909 // In addition, we want to validate if the original request encountered
1910 // an error for two reasons. The first being if the error was a network error
1911 // then trying to re-fetch the image might succeed. The second is more
1912 // complicated. We decide if we should fire the load or error event for img
1913 // elements depending on if the image has error in its status at the time when
1914 // the load complete notification is received, and we set error status on an
1915 // image if it encounters a network error or a decode error with no real way
1916 // to tell them apart. So if we load an image that will produce a decode error
1917 // the first time we will usually fire the load event, and then decode enough
1918 // to encounter the decode error and set the error status on the image. The
1919 // next time we reference the image in the same document the load complete
1920 // notification is replayed and this time the error status from the decode is
1921 // already present so we fire the error event instead of the load event. This
1922 // is a bug (bug 1645576) that we should fix. In order to avoid that bug in
1923 // some cases (specifically the cases when we hit this code and try to
1924 // validate the request) we make sure to validate. This avoids the bug because
1925 // when the load complete notification arrives the proxy is marked as
1926 // validating so it lies about its status and returns nothing.
1927 bool requestComplete
= false;
1928 RefPtr
<ProgressTracker
> tracker
;
1929 RefPtr
<mozilla::image::Image
> image
= request
->GetImage();
1931 tracker
= image
->GetProgressTracker();
1933 tracker
= request
->GetProgressTracker();
1936 if (tracker
->GetProgress() & (FLAG_LOAD_COMPLETE
| FLAG_HAS_ERROR
)) {
1937 requestComplete
= true;
1940 if (!requestComplete
) {
1944 if (validateRequest
&& aCanMakeNewChannel
) {
1945 LOG_SCOPE(gImgLog
, "imgLoader::ValidateRequest |cache hit| must validate");
1947 uint64_t innerWindowID
=
1948 aLoadingDocument
? aLoadingDocument
->InnerWindowID() : 0;
1949 return ValidateRequestWithNewChannel(
1950 request
, aURI
, aInitialDocumentURI
, aReferrerInfo
, aLoadGroup
,
1951 aObserver
, aLoadingDocument
, innerWindowID
, aLoadFlags
, aLoadPolicyType
,
1952 aProxyRequest
, aTriggeringPrincipal
, aCORSMode
, aLinkPreload
,
1953 aNewChannelCreated
);
1956 return !validateRequest
;
1959 bool imgLoader::RemoveFromCache(const ImageCacheKey
& aKey
) {
1960 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::RemoveFromCache", "uri",
1963 imgCacheTable
& cache
= GetCache(aKey
);
1964 imgCacheQueue
& queue
= GetCacheQueue(aKey
);
1966 RefPtr
<imgCacheEntry
> entry
;
1967 cache
.Remove(aKey
, getter_AddRefs(entry
));
1969 MOZ_ASSERT(!entry
->Evicted(), "Evicting an already-evicted cache entry!");
1971 // Entries with no proxies are in the tracker.
1972 if (entry
->HasNoProxies()) {
1973 if (mCacheTracker
) {
1974 mCacheTracker
->RemoveObject(entry
);
1976 queue
.Remove(entry
);
1979 entry
->SetEvicted(true);
1981 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1982 request
->SetIsInCache(false);
1983 AddToUncachedImages(request
);
1990 bool imgLoader::RemoveFromCache(imgCacheEntry
* entry
, QueueState aQueueState
) {
1991 LOG_STATIC_FUNC(gImgLog
, "imgLoader::RemoveFromCache entry");
1993 RefPtr
<imgRequest
> request
= entry
->GetRequest();
1995 const ImageCacheKey
& key
= request
->CacheKey();
1996 imgCacheTable
& cache
= GetCache(key
);
1997 imgCacheQueue
& queue
= GetCacheQueue(key
);
1999 LOG_STATIC_FUNC_WITH_PARAM(gImgLog
, "imgLoader::RemoveFromCache",
2000 "entry's uri", key
.URI());
2004 if (entry
->HasNoProxies()) {
2005 LOG_STATIC_FUNC(gImgLog
,
2006 "imgLoader::RemoveFromCache removing from tracker");
2007 if (mCacheTracker
) {
2008 mCacheTracker
->RemoveObject(entry
);
2010 // Only search the queue to remove the entry if its possible it might
2011 // be in the queue. If we know its not in the queue this would be
2013 MOZ_ASSERT_IF(aQueueState
== QueueState::AlreadyRemoved
,
2014 !queue
.Contains(entry
));
2015 if (aQueueState
== QueueState::MaybeExists
) {
2016 queue
.Remove(entry
);
2020 entry
->SetEvicted(true);
2021 request
->SetIsInCache(false);
2022 AddToUncachedImages(request
);
2030 nsresult
imgLoader::EvictEntries(imgCacheTable
& aCacheToClear
) {
2031 LOG_STATIC_FUNC(gImgLog
, "imgLoader::EvictEntries table");
2033 // We have to make a temporary, since RemoveFromCache removes the element
2034 // from the queue, invalidating iterators.
2035 nsTArray
<RefPtr
<imgCacheEntry
> > entries
;
2036 for (auto iter
= aCacheToClear
.Iter(); !iter
.Done(); iter
.Next()) {
2037 RefPtr
<imgCacheEntry
>& data
= iter
.Data();
2038 entries
.AppendElement(data
);
2041 for (uint32_t i
= 0; i
< entries
.Length(); ++i
) {
2042 if (!RemoveFromCache(entries
[i
])) {
2043 return NS_ERROR_FAILURE
;
2047 MOZ_ASSERT(aCacheToClear
.Count() == 0);
2052 nsresult
imgLoader::EvictEntries(imgCacheQueue
& aQueueToClear
) {
2053 LOG_STATIC_FUNC(gImgLog
, "imgLoader::EvictEntries queue");
2055 // We have to make a temporary, since RemoveFromCache removes the element
2056 // from the queue, invalidating iterators.
2057 nsTArray
<RefPtr
<imgCacheEntry
> > entries(aQueueToClear
.GetNumElements());
2058 for (auto i
= aQueueToClear
.begin(); i
!= aQueueToClear
.end(); ++i
) {
2059 entries
.AppendElement(*i
);
2062 // Iterate in reverse order to minimize array copying.
2063 for (auto& entry
: entries
) {
2064 if (!RemoveFromCache(entry
)) {
2065 return NS_ERROR_FAILURE
;
2069 MOZ_ASSERT(aQueueToClear
.GetNumElements() == 0);
2074 void imgLoader::AddToUncachedImages(imgRequest
* aRequest
) {
2075 MutexAutoLock
lock(mUncachedImagesMutex
);
2076 mUncachedImages
.PutEntry(aRequest
);
2079 void imgLoader::RemoveFromUncachedImages(imgRequest
* aRequest
) {
2080 MutexAutoLock
lock(mUncachedImagesMutex
);
2081 mUncachedImages
.RemoveEntry(aRequest
);
2084 bool imgLoader::PreferLoadFromCache(nsIURI
* aURI
) const {
2085 // If we are trying to load an image from a protocol that doesn't support
2086 // caching (e.g. thumbnails via the moz-page-thumb:// protocol, or icons via
2087 // the moz-extension:// protocol), load it directly from the cache to prevent
2088 // re-decoding the image. See Bug 1373258.
2089 // TODO: Bug 1406134
2090 return aURI
->SchemeIs("moz-page-thumb") || aURI
->SchemeIs("moz-extension");
2093 #define LOAD_FLAGS_CACHE_MASK \
2094 (nsIRequest::LOAD_BYPASS_CACHE | nsIRequest::LOAD_FROM_CACHE)
2096 #define LOAD_FLAGS_VALIDATE_MASK \
2097 (nsIRequest::VALIDATE_ALWAYS | nsIRequest::VALIDATE_NEVER | \
2098 nsIRequest::VALIDATE_ONCE_PER_SESSION)
2101 imgLoader::LoadImageXPCOM(
2102 nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
, nsIReferrerInfo
* aReferrerInfo
,
2103 nsIPrincipal
* aTriggeringPrincipal
, nsILoadGroup
* aLoadGroup
,
2104 imgINotificationObserver
* aObserver
, Document
* aLoadingDocument
,
2105 nsLoadFlags aLoadFlags
, nsISupports
* aCacheKey
,
2106 nsContentPolicyType aContentPolicyType
, imgIRequest
** _retval
) {
2107 // Optional parameter, so defaults to 0 (== TYPE_INVALID)
2108 if (!aContentPolicyType
) {
2109 aContentPolicyType
= nsIContentPolicy::TYPE_INTERNAL_IMAGE
;
2111 imgRequestProxy
* proxy
;
2112 nsresult rv
= LoadImage(
2113 aURI
, aInitialDocumentURI
, aReferrerInfo
, aTriggeringPrincipal
, 0,
2114 aLoadGroup
, aObserver
, aLoadingDocument
, aLoadingDocument
, aLoadFlags
,
2115 aCacheKey
, aContentPolicyType
, u
""_ns
,
2116 /* aUseUrgentStartForChannel */ false, /* aListPreload */ false, &proxy
);
2121 static void MakeRequestStaticIfNeeded(
2122 Document
* aLoadingDocument
, imgRequestProxy
** aProxyAboutToGetReturned
) {
2123 if (!aLoadingDocument
|| !aLoadingDocument
->IsStaticDocument()) {
2127 if (!*aProxyAboutToGetReturned
) {
2131 RefPtr
<imgRequestProxy
> proxy
= dont_AddRef(*aProxyAboutToGetReturned
);
2132 *aProxyAboutToGetReturned
= nullptr;
2134 RefPtr
<imgRequestProxy
> staticProxy
=
2135 proxy
->GetStaticRequest(aLoadingDocument
);
2136 if (staticProxy
!= proxy
) {
2137 proxy
->CancelAndForgetObserver(NS_BINDING_ABORTED
);
2138 proxy
= std::move(staticProxy
);
2140 proxy
.forget(aProxyAboutToGetReturned
);
2143 nsresult
imgLoader::LoadImage(
2144 nsIURI
* aURI
, nsIURI
* aInitialDocumentURI
, nsIReferrerInfo
* aReferrerInfo
,
2145 nsIPrincipal
* aTriggeringPrincipal
, uint64_t aRequestContextID
,
2146 nsILoadGroup
* aLoadGroup
, imgINotificationObserver
* aObserver
,
2147 nsINode
* aContext
, Document
* aLoadingDocument
, nsLoadFlags aLoadFlags
,
2148 nsISupports
* aCacheKey
, nsContentPolicyType aContentPolicyType
,
2149 const nsAString
& initiatorType
, bool aUseUrgentStartForChannel
,
2150 bool aLinkPreload
, imgRequestProxy
** _retval
) {
2153 NS_ASSERTION(aURI
, "imgLoader::LoadImage -- NULL URI pointer");
2156 return NS_ERROR_NULL_POINTER
;
2159 auto makeStaticIfNeeded
= mozilla::MakeScopeExit(
2160 [&] { MakeRequestStaticIfNeeded(aLoadingDocument
, _retval
); });
2162 #ifdef MOZ_GECKO_PROFILER
2163 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK
,
2164 aURI
->GetSpecOrDefault());
2167 LOG_SCOPE_WITH_PARAM(gImgLog
, "imgLoader::LoadImage", "aURI", aURI
);
2171 RefPtr
<imgRequest
> request
;
2174 nsLoadFlags requestFlags
= nsIRequest::LOAD_NORMAL
;
2177 bool isPrivate
= false;
2179 if (aLoadingDocument
) {
2180 isPrivate
= nsContentUtils::IsInPrivateBrowsing(aLoadingDocument
);
2181 } else if (aLoadGroup
) {
2182 isPrivate
= nsContentUtils::IsInPrivateBrowsing(aLoadGroup
);
2184 MOZ_ASSERT(isPrivate
== mRespectPrivacy
);
2186 if (aLoadingDocument
) {
2187 // The given load group should match that of the document if given. If
2188 // that isn't the case, then we need to add more plumbing to ensure we
2189 // block the document as well.
2190 nsCOMPtr
<nsILoadGroup
> docLoadGroup
=
2191 aLoadingDocument
->GetDocumentLoadGroup();
2192 MOZ_ASSERT(docLoadGroup
== aLoadGroup
);
2196 // Get the default load flags from the loadgroup (if possible)...
2198 aLoadGroup
->GetLoadFlags(&requestFlags
);
2199 if (PreferLoadFromCache(aURI
)) {
2200 requestFlags
|= nsIRequest::LOAD_FROM_CACHE
;
2204 // Merge the default load flags with those passed in via aLoadFlags.
2205 // Currently, *only* the caching, validation and background load flags
2208 // The flags in aLoadFlags take precedence over the default flags!
2210 if (aLoadFlags
& LOAD_FLAGS_CACHE_MASK
) {
2211 // Override the default caching flags...
2212 requestFlags
= (requestFlags
& ~LOAD_FLAGS_CACHE_MASK
) |
2213 (aLoadFlags
& LOAD_FLAGS_CACHE_MASK
);
2215 if (aLoadFlags
& LOAD_FLAGS_VALIDATE_MASK
) {
2216 // Override the default validation flags...
2217 requestFlags
= (requestFlags
& ~LOAD_FLAGS_VALIDATE_MASK
) |
2218 (aLoadFlags
& LOAD_FLAGS_VALIDATE_MASK
);
2220 if (aLoadFlags
& nsIRequest::LOAD_BACKGROUND
) {
2221 // Propagate background loading...
2222 requestFlags
|= nsIRequest::LOAD_BACKGROUND
;
2226 // Set background loading if it is <link rel=preload>
2227 requestFlags
|= nsIRequest::LOAD_BACKGROUND
;
2230 int32_t corsmode
= imgIRequest::CORS_NONE
;
2231 if (aLoadFlags
& imgILoader::LOAD_CORS_ANONYMOUS
) {
2232 corsmode
= imgIRequest::CORS_ANONYMOUS
;
2233 } else if (aLoadFlags
& imgILoader::LOAD_CORS_USE_CREDENTIALS
) {
2234 corsmode
= imgIRequest::CORS_USE_CREDENTIALS
;
2237 // Look in the preloaded images of loading document first.
2238 if (StaticPrefs::network_preload() && !aLinkPreload
&& aLoadingDocument
) {
2239 auto key
= PreloadHashKey::CreateAsImage(aURI
, aTriggeringPrincipal
,
2240 ConvertToCORSMode(corsmode
));
2241 if (RefPtr
<PreloaderBase
> preload
=
2242 aLoadingDocument
->Preloads().LookupPreload(key
)) {
2243 RefPtr
<imgRequestProxy
> proxy
= do_QueryObject(preload
);
2246 MOZ_LOG(gImgLog
, LogLevel::Debug
,
2247 ("[this=%p] imgLoader::LoadImage -- preloaded [proxy=%p]"
2249 this, proxy
.get(), aLoadingDocument
));
2251 // Removing the preload for this image to be in parity with Chromium. Any
2252 // following regular image request will be reloaded using the regular
2253 // path: image cache, http cache, network. Any following `<link
2254 // rel=preload as=image>` will start a new image preload that can be
2255 // satisfied from http cache or network.
2257 // There is a spec discussion for "preload cache", see
2258 // https://github.com/w3c/preload/issues/97. And it is also not clear how
2259 // preload image interacts with list of available images, see
2260 // https://github.com/whatwg/html/issues/4474.
2261 proxy
->RemoveSelf(aLoadingDocument
);
2262 proxy
->NotifyUsage();
2264 imgRequest
* request
= proxy
->GetOwner();
2266 CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
2267 aObserver
, requestFlags
, _retval
);
2268 NS_ENSURE_SUCCESS(rv
, rv
);
2270 imgRequestProxy
* newProxy
= *_retval
;
2271 if (imgCacheValidator
* validator
= request
->GetValidator()) {
2272 newProxy
->MarkValidating();
2273 // Attach the proxy without notifying and this will add us to the load
2275 validator
->AddProxy(newProxy
);
2277 // It's OK to add here even if the request is done. If it is, it'll send
2278 // a OnStopRequest()and the proxy will be removed from the loadgroup in
2279 // imgRequestProxy::OnLoadComplete.
2280 newProxy
->AddToLoadGroup();
2281 newProxy
->NotifyListener();
2288 RefPtr
<imgCacheEntry
> entry
;
2290 // Look in the cache for our URI, and then validate it.
2291 // XXX For now ignore aCacheKey. We will need it in the future
2292 // for correctly dealing with image load requests that are a result
2294 OriginAttributes attrs
;
2295 if (aTriggeringPrincipal
) {
2296 attrs
= aTriggeringPrincipal
->OriginAttributesRef();
2298 ImageCacheKey
key(aURI
, attrs
, aLoadingDocument
);
2299 imgCacheTable
& cache
= GetCache(key
);
2301 if (cache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
2302 bool newChannelCreated
= false;
2303 if (ValidateEntry(entry
, aURI
, aInitialDocumentURI
, aReferrerInfo
,
2304 aLoadGroup
, aObserver
, aLoadingDocument
, requestFlags
,
2305 aContentPolicyType
, true, &newChannelCreated
, _retval
,
2306 aTriggeringPrincipal
, corsmode
, aLinkPreload
)) {
2307 request
= entry
->GetRequest();
2309 // If this entry has no proxies, its request has no reference to the
2311 if (entry
->HasNoProxies()) {
2312 LOG_FUNC_WITH_PARAM(gImgLog
,
2313 "imgLoader::LoadImage() adding proxyless entry",
2315 MOZ_ASSERT(!request
->HasCacheEntry(),
2316 "Proxyless entry's request has cache entry!");
2317 request
->SetCacheEntry(entry
);
2319 if (mCacheTracker
&& entry
->GetExpirationState()->IsTracked()) {
2320 mCacheTracker
->MarkUsed(entry
);
2326 if (!newChannelCreated
) {
2327 // This is ugly but it's needed to report CSP violations. We have 3
2329 // - we don't have cache. We are not in this if() stmt. A new channel is
2330 // created and that triggers the CSP checks.
2331 // - We have a cache entry and this is blocked by CSP directives.
2332 DebugOnly
<bool> shouldLoad
= ShouldLoadCachedImage(
2333 request
, aLoadingDocument
, aTriggeringPrincipal
, aContentPolicyType
,
2334 /* aSendCSPViolationReports */ true);
2335 MOZ_ASSERT(shouldLoad
);
2338 // We can't use this entry. We'll try to load it off the network, and if
2339 // successful, overwrite the old entry in the cache with a new one.
2344 // Keep the channel in this scope, so we can adjust its notificationCallbacks
2345 // later when we create the proxy.
2346 nsCOMPtr
<nsIChannel
> newChannel
;
2347 // If we didn't get a cache hit, we need to load from the network.
2349 LOG_SCOPE(gImgLog
, "imgLoader::LoadImage |cache miss|");
2351 bool forcePrincipalCheck
;
2352 rv
= NewImageChannel(getter_AddRefs(newChannel
), &forcePrincipalCheck
, aURI
,
2353 aInitialDocumentURI
, corsmode
, aReferrerInfo
,
2354 aLoadGroup
, requestFlags
, aContentPolicyType
,
2355 aTriggeringPrincipal
, aContext
, mRespectPrivacy
);
2356 if (NS_FAILED(rv
)) {
2357 return NS_ERROR_FAILURE
;
2360 MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel
) == mRespectPrivacy
);
2362 NewRequestAndEntry(forcePrincipalCheck
, this, key
, getter_AddRefs(request
),
2363 getter_AddRefs(entry
));
2365 MOZ_LOG(gImgLog
, LogLevel::Debug
,
2366 ("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
2368 this, request
.get()));
2370 nsCOMPtr
<nsIClassOfService
> cos(do_QueryInterface(newChannel
));
2372 if (aUseUrgentStartForChannel
&& !aLinkPreload
) {
2373 cos
->AddClassFlags(nsIClassOfService::UrgentStart
);
2376 if (StaticPrefs::network_http_tailing_enabled() &&
2377 aContentPolicyType
== nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON
) {
2378 cos
->AddClassFlags(nsIClassOfService::Throttleable
|
2379 nsIClassOfService::Tail
);
2380 nsCOMPtr
<nsIHttpChannel
> httpChannel(do_QueryInterface(newChannel
));
2382 Unused
<< httpChannel
->SetRequestContextID(aRequestContextID
);
2387 nsCOMPtr
<nsILoadGroup
> channelLoadGroup
;
2388 newChannel
->GetLoadGroup(getter_AddRefs(channelLoadGroup
));
2389 rv
= request
->Init(aURI
, aURI
, /* aHadInsecureRedirect = */ false,
2390 channelLoadGroup
, newChannel
, entry
, aLoadingDocument
,
2391 aTriggeringPrincipal
, corsmode
, aReferrerInfo
);
2392 if (NS_FAILED(rv
)) {
2393 return NS_ERROR_FAILURE
;
2396 // Add the initiator type for this image load
2397 nsCOMPtr
<nsITimedChannel
> timedChannel
= do_QueryInterface(newChannel
);
2399 timedChannel
->SetInitiatorType(initiatorType
);
2402 // create the proxy listener
2403 nsCOMPtr
<nsIStreamListener
> listener
= new ProxyListener(request
.get());
2405 MOZ_LOG(gImgLog
, LogLevel::Debug
,
2406 ("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
2409 mozilla::net::PredictorLearn(aURI
, aInitialDocumentURI
,
2410 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE
,
2413 nsresult openRes
= newChannel
->AsyncOpen(listener
);
2415 if (NS_FAILED(openRes
)) {
2417 gImgLog
, LogLevel::Debug
,
2418 ("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
2420 this, static_cast<uint32_t>(openRes
)));
2421 request
->CancelAndAbort(openRes
);
2425 // Try to add the new request into the cache.
2426 PutIntoCache(key
, entry
);
2428 LOG_MSG_WITH_PARAM(gImgLog
, "imgLoader::LoadImage |cache hit|", "request",
2432 // If we didn't get a proxy when validating the cache entry, we need to
2435 // ValidateEntry() has three return values: "Is valid," "might be valid --
2436 // validating over network", and "not valid." If we don't have a _retval,
2437 // we know ValidateEntry is not validating over the network, so it's safe
2438 // to SetLoadId here because we know this request is valid for this context.
2440 // Note, however, that this doesn't guarantee the behaviour we want (one
2441 // URL maps to the same image on a page) if we load the same image in a
2442 // different tab (see bug 528003), because its load id will get re-set, and
2443 // that'll cause us to validate over the network.
2444 request
->SetLoadId(aLoadingDocument
);
2446 LOG_MSG(gImgLog
, "imgLoader::LoadImage", "creating proxy request.");
2447 rv
= CreateNewProxyForRequest(request
, aURI
, aLoadGroup
, aLoadingDocument
,
2448 aObserver
, requestFlags
, _retval
);
2449 if (NS_FAILED(rv
)) {
2453 imgRequestProxy
* proxy
= *_retval
;
2455 // Make sure that OnStatus/OnProgress calls have the right request set, if
2456 // we did create a channel here.
2458 nsCOMPtr
<nsIInterfaceRequestor
> requestor(
2459 new nsProgressNotificationProxy(newChannel
, proxy
));
2461 return NS_ERROR_OUT_OF_MEMORY
;
2463 newChannel
->SetNotificationCallbacks(requestor
);
2467 MOZ_ASSERT(aLoadingDocument
);
2468 proxy
->PrioritizeAsPreload();
2469 auto preloadKey
= PreloadHashKey::CreateAsImage(
2470 aURI
, aTriggeringPrincipal
, ConvertToCORSMode(corsmode
));
2471 proxy
->NotifyOpen(preloadKey
, aLoadingDocument
, true);
2474 // Note that it's OK to add here even if the request is done. If it is,
2475 // it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
2476 // the proxy will be removed from the loadgroup.
2477 proxy
->AddToLoadGroup();
2479 // If we're loading off the network, explicitly don't notify our proxy,
2480 // because necko (or things called from necko, such as imgCacheValidator)
2481 // are going to call our notifications asynchronously, and we can't make it
2482 // further asynchronous because observers might rely on imagelib completing
2483 // its work between the channel's OnStartRequest and OnStopRequest.
2485 proxy
->NotifyListener();
2491 NS_ASSERTION(*_retval
, "imgLoader::LoadImage -- no return value");
2497 imgLoader::LoadImageWithChannelXPCOM(nsIChannel
* channel
,
2498 imgINotificationObserver
* aObserver
,
2499 Document
* aLoadingDocument
,
2500 nsIStreamListener
** listener
,
2501 imgIRequest
** _retval
) {
2503 imgRequestProxy
* proxy
;
2504 result
= LoadImageWithChannel(channel
, aObserver
, aLoadingDocument
, listener
,
2510 nsresult
imgLoader::LoadImageWithChannel(nsIChannel
* channel
,
2511 imgINotificationObserver
* aObserver
,
2512 Document
* aLoadingDocument
,
2513 nsIStreamListener
** listener
,
2514 imgRequestProxy
** _retval
) {
2515 NS_ASSERTION(channel
,
2516 "imgLoader::LoadImageWithChannel -- NULL channel pointer");
2518 MOZ_ASSERT(NS_UsePrivateBrowsing(channel
) == mRespectPrivacy
);
2520 auto makeStaticIfNeeded
= mozilla::MakeScopeExit(
2521 [&] { MakeRequestStaticIfNeeded(aLoadingDocument
, _retval
); });
2523 LOG_SCOPE(gImgLog
, "imgLoader::LoadImageWithChannel");
2524 RefPtr
<imgRequest
> request
;
2526 nsCOMPtr
<nsIURI
> uri
;
2527 channel
->GetURI(getter_AddRefs(uri
));
2529 NS_ENSURE_TRUE(channel
, NS_ERROR_FAILURE
);
2530 nsCOMPtr
<nsILoadInfo
> loadInfo
= channel
->LoadInfo();
2532 OriginAttributes attrs
= loadInfo
->GetOriginAttributes();
2534 ImageCacheKey
key(uri
, attrs
, aLoadingDocument
);
2536 nsLoadFlags requestFlags
= nsIRequest::LOAD_NORMAL
;
2537 channel
->GetLoadFlags(&requestFlags
);
2539 if (PreferLoadFromCache(uri
)) {
2540 requestFlags
|= nsIRequest::LOAD_FROM_CACHE
;
2543 RefPtr
<imgCacheEntry
> entry
;
2545 if (requestFlags
& nsIRequest::LOAD_BYPASS_CACHE
) {
2546 RemoveFromCache(key
);
2548 // Look in the cache for our URI, and then validate it.
2549 // XXX For now ignore aCacheKey. We will need it in the future
2550 // for correctly dealing with image load requests that are a result
2552 imgCacheTable
& cache
= GetCache(key
);
2553 if (cache
.Get(key
, getter_AddRefs(entry
)) && entry
) {
2554 // We don't want to kick off another network load. So we ask
2555 // ValidateEntry to only do validation without creating a new proxy. If
2556 // it says that the entry isn't valid any more, we'll only use the entry
2557 // we're getting if the channel is loading from the cache anyways.
2559 // XXX -- should this be changed? it's pretty much verbatim from the old
2560 // code, but seems nonsensical.
2562 // Since aCanMakeNewChannel == false, we don't need to pass content policy
2563 // type/principal/etc
2565 nsCOMPtr
<nsILoadInfo
> loadInfo
= channel
->LoadInfo();
2566 // if there is a loadInfo, use the right contentType, otherwise
2567 // default to the internal image type
2568 nsContentPolicyType policyType
= loadInfo
->InternalContentPolicyType();
2570 if (ValidateEntry(entry
, uri
, nullptr, nullptr, nullptr, aObserver
,
2571 aLoadingDocument
, requestFlags
, policyType
, false,
2572 nullptr, nullptr, nullptr, imgIRequest::CORS_NONE
,
2574 request
= entry
->GetRequest();
2576 nsCOMPtr
<nsICacheInfoChannel
> cacheChan(do_QueryInterface(channel
));
2580 cacheChan
->IsFromCache(&bUseCacheCopy
);
2582 bUseCacheCopy
= false;
2585 if (!bUseCacheCopy
) {
2588 request
= entry
->GetRequest();
2592 if (request
&& entry
) {
2593 // If this entry has no proxies, its request has no reference to
2595 if (entry
->HasNoProxies()) {
2596 LOG_FUNC_WITH_PARAM(
2598 "imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
2600 MOZ_ASSERT(!request
->HasCacheEntry(),
2601 "Proxyless entry's request has cache entry!");
2602 request
->SetCacheEntry(entry
);
2604 if (mCacheTracker
&& entry
->GetExpirationState()->IsTracked()) {
2605 mCacheTracker
->MarkUsed(entry
);
2612 nsCOMPtr
<nsILoadGroup
> loadGroup
;
2613 channel
->GetLoadGroup(getter_AddRefs(loadGroup
));
2616 if (aLoadingDocument
) {
2617 // The load group of the channel should always match that of the
2618 // document if given. If that isn't the case, then we need to add more
2619 // plumbing to ensure we block the document as well.
2620 nsCOMPtr
<nsILoadGroup
> docLoadGroup
=
2621 aLoadingDocument
->GetDocumentLoadGroup();
2622 MOZ_ASSERT(docLoadGroup
== loadGroup
);
2626 // Filter out any load flags not from nsIRequest
2627 requestFlags
&= nsIRequest::LOAD_REQUESTMASK
;
2629 nsresult rv
= NS_OK
;
2631 // we have this in our cache already.. cancel the current (document) load
2633 // this should fire an OnStopRequest
2634 channel
->Cancel(NS_ERROR_PARSED_DATA_CACHED
);
2636 *listener
= nullptr; // give them back a null nsIStreamListener
2638 rv
= CreateNewProxyForRequest(request
, uri
, loadGroup
, aLoadingDocument
,
2639 aObserver
, requestFlags
, _retval
);
2640 static_cast<imgRequestProxy
*>(*_retval
)->NotifyListener();
2642 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2643 nsCOMPtr
<nsIURI
> originalURI
;
2644 channel
->GetOriginalURI(getter_AddRefs(originalURI
));
2646 // XXX(seth): We should be able to just use |key| here, except that |key| is
2647 // constructed above with the *current URI* and not the *original URI*. I'm
2648 // pretty sure this is a bug, and it's preventing us from ever getting a
2649 // cache hit in LoadImageWithChannel when redirects are involved.
2650 ImageCacheKey
originalURIKey(originalURI
, attrs
, aLoadingDocument
);
2652 // Default to doing a principal check because we don't know who
2653 // started that load and whether their principal ended up being
2654 // inherited on the channel.
2655 NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
2656 originalURIKey
, getter_AddRefs(request
),
2657 getter_AddRefs(entry
));
2659 // No principal specified here, because we're not passed one.
2660 // In LoadImageWithChannel, the redirects that may have been
2661 // associated with this load would have gone through necko.
2662 // We only have the final URI in ImageLib and hence don't know
2663 // if the request went through insecure redirects. But if it did,
2664 // the necko cache should have handled that (since all necko cache hits
2665 // including the redirects will go through content policy). Hence, we
2666 // can set aHadInsecureRedirect to false here.
2667 rv
= request
->Init(originalURI
, uri
, /* aHadInsecureRedirect = */ false,
2668 channel
, channel
, entry
, aLoadingDocument
, nullptr,
2669 imgIRequest::CORS_NONE
, nullptr);
2670 NS_ENSURE_SUCCESS(rv
, rv
);
2672 RefPtr
<ProxyListener
> pl
=
2673 new ProxyListener(static_cast<nsIStreamListener
*>(request
.get()));
2674 pl
.forget(listener
);
2676 // Try to add the new request into the cache.
2677 PutIntoCache(originalURIKey
, entry
);
2679 rv
= CreateNewProxyForRequest(request
, originalURI
, loadGroup
,
2680 aLoadingDocument
, aObserver
, requestFlags
,
2683 // Explicitly don't notify our proxy, because we're loading off the
2684 // network, and necko (or things called from necko, such as
2685 // imgCacheValidator) are going to call our notifications asynchronously,
2686 // and we can't make it further asynchronous because observers might rely
2687 // on imagelib completing its work between the channel's OnStartRequest and
2691 if (NS_FAILED(rv
)) {
2695 (*_retval
)->AddToLoadGroup();
2699 bool imgLoader::SupportImageWithMimeType(const char* aMimeType
,
2700 AcceptedMimeTypes aAccept
2701 /* = AcceptedMimeTypes::IMAGES */) {
2702 nsAutoCString
mimeType(aMimeType
);
2703 ToLowerCase(mimeType
);
2705 if (aAccept
== AcceptedMimeTypes::IMAGES_AND_DOCUMENTS
&&
2706 mimeType
.EqualsLiteral("image/svg+xml")) {
2710 DecoderType type
= DecoderFactory::GetDecoderType(mimeType
.get());
2711 return type
!= DecoderType::UNKNOWN
;
2715 imgLoader::GetMIMETypeFromContent(nsIRequest
* aRequest
,
2716 const uint8_t* aContents
, uint32_t aLength
,
2717 nsACString
& aContentType
) {
2718 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
2720 nsCOMPtr
<nsILoadInfo
> loadInfo
= channel
->LoadInfo();
2721 if (loadInfo
->GetSkipContentSniffing()) {
2722 return NS_ERROR_NOT_AVAILABLE
;
2725 return GetMimeTypeFromContent((const char*)aContents
, aLength
, aContentType
);
2729 nsresult
imgLoader::GetMimeTypeFromContent(const char* aContents
,
2731 nsACString
& aContentType
) {
2732 nsAutoCString detected
;
2736 (!strncmp(aContents
, "GIF87a", 6) || !strncmp(aContents
, "GIF89a", 6))) {
2737 aContentType
.AssignLiteral(IMAGE_GIF
);
2740 } else if (aLength
>= 8 && ((unsigned char)aContents
[0] == 0x89 &&
2741 (unsigned char)aContents
[1] == 0x50 &&
2742 (unsigned char)aContents
[2] == 0x4E &&
2743 (unsigned char)aContents
[3] == 0x47 &&
2744 (unsigned char)aContents
[4] == 0x0D &&
2745 (unsigned char)aContents
[5] == 0x0A &&
2746 (unsigned char)aContents
[6] == 0x1A &&
2747 (unsigned char)aContents
[7] == 0x0A)) {
2748 aContentType
.AssignLiteral(IMAGE_PNG
);
2750 /* maybe a JPEG (JFIF)? */
2751 /* JFIF files start with SOI APP0 but older files can start with SOI DQT
2752 * so we test for SOI followed by any marker, i.e. FF D8 FF
2753 * this will also work for SPIFF JPEG files if they appear in the future.
2755 * (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
2757 } else if (aLength
>= 3 && ((unsigned char)aContents
[0]) == 0xFF &&
2758 ((unsigned char)aContents
[1]) == 0xD8 &&
2759 ((unsigned char)aContents
[2]) == 0xFF) {
2760 aContentType
.AssignLiteral(IMAGE_JPEG
);
2762 /* or how about ART? */
2763 /* ART begins with JG (4A 47). Major version offset 2.
2764 * Minor version offset 3. Offset 4 must be nullptr.
2766 } else if (aLength
>= 5 && ((unsigned char)aContents
[0]) == 0x4a &&
2767 ((unsigned char)aContents
[1]) == 0x47 &&
2768 ((unsigned char)aContents
[4]) == 0x00) {
2769 aContentType
.AssignLiteral(IMAGE_ART
);
2771 } else if (aLength
>= 2 && !strncmp(aContents
, "BM", 2)) {
2772 aContentType
.AssignLiteral(IMAGE_BMP
);
2774 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
2775 // CURs begin with 2-byte 0 followed by 2-byte 2.
2776 } else if (aLength
>= 4 && (!memcmp(aContents
, "\000\000\001\000", 4) ||
2777 !memcmp(aContents
, "\000\000\002\000", 4))) {
2778 aContentType
.AssignLiteral(IMAGE_ICO
);
2780 // WebPs always begin with RIFF, a 32-bit length, and WEBP.
2781 } else if (aLength
>= 12 && !memcmp(aContents
, "RIFF", 4) &&
2782 !memcmp(aContents
+ 8, "WEBP", 4)) {
2783 aContentType
.AssignLiteral(IMAGE_WEBP
);
2785 } else if (MatchesMP4(reinterpret_cast<const uint8_t*>(aContents
), aLength
,
2787 detected
.Equals(IMAGE_AVIF
)) {
2788 aContentType
.AssignLiteral(IMAGE_AVIF
);
2790 /* none of the above? I give up */
2791 return NS_ERROR_NOT_AVAILABLE
;
2798 * proxy stream listener class used to handle multipart/x-mixed-replace
2801 #include "nsIRequest.h"
2802 #include "nsIStreamConverterService.h"
2804 NS_IMPL_ISUPPORTS(ProxyListener
, nsIStreamListener
,
2805 nsIThreadRetargetableStreamListener
, nsIRequestObserver
)
2807 ProxyListener::ProxyListener(nsIStreamListener
* dest
) : mDestListener(dest
) {
2808 /* member initializers and constructor code */
2811 ProxyListener::~ProxyListener() { /* destructor code */
2814 /** nsIRequestObserver methods **/
2817 ProxyListener::OnStartRequest(nsIRequest
* aRequest
) {
2818 if (!mDestListener
) {
2819 return NS_ERROR_FAILURE
;
2822 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
2824 // We need to set the initiator type for the image load
2825 nsCOMPtr
<nsITimedChannel
> timedChannel
= do_QueryInterface(channel
);
2828 timedChannel
->GetInitiatorType(type
);
2829 if (type
.IsEmpty()) {
2830 timedChannel
->SetInitiatorType(u
"img"_ns
);
2834 nsAutoCString contentType
;
2835 nsresult rv
= channel
->GetContentType(contentType
);
2837 if (!contentType
.IsEmpty()) {
2838 /* If multipart/x-mixed-replace content, we'll insert a MIME decoder
2839 in the pipeline to handle the content and pass it along to our
2842 if ("multipart/x-mixed-replace"_ns
.Equals(contentType
)) {
2843 nsCOMPtr
<nsIStreamConverterService
> convServ(
2844 do_GetService("@mozilla.org/streamConverters;1", &rv
));
2845 if (NS_SUCCEEDED(rv
)) {
2846 nsCOMPtr
<nsIStreamListener
> toListener(mDestListener
);
2847 nsCOMPtr
<nsIStreamListener
> fromListener
;
2849 rv
= convServ
->AsyncConvertData("multipart/x-mixed-replace", "*/*",
2850 toListener
, nullptr,
2851 getter_AddRefs(fromListener
));
2852 if (NS_SUCCEEDED(rv
)) {
2853 mDestListener
= fromListener
;
2860 return mDestListener
->OnStartRequest(aRequest
);
2864 ProxyListener::OnStopRequest(nsIRequest
* aRequest
, nsresult status
) {
2865 if (!mDestListener
) {
2866 return NS_ERROR_FAILURE
;
2869 return mDestListener
->OnStopRequest(aRequest
, status
);
2872 /** nsIStreamListener methods **/
2875 ProxyListener::OnDataAvailable(nsIRequest
* aRequest
, nsIInputStream
* inStr
,
2876 uint64_t sourceOffset
, uint32_t count
) {
2877 if (!mDestListener
) {
2878 return NS_ERROR_FAILURE
;
2881 return mDestListener
->OnDataAvailable(aRequest
, inStr
, sourceOffset
, count
);
2884 /** nsThreadRetargetableStreamListener methods **/
2886 ProxyListener::CheckListenerChain() {
2887 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
2888 nsresult rv
= NS_OK
;
2889 nsCOMPtr
<nsIThreadRetargetableStreamListener
> retargetableListener
=
2890 do_QueryInterface(mDestListener
, &rv
);
2891 if (retargetableListener
) {
2892 rv
= retargetableListener
->CheckListenerChain();
2895 gImgLog
, LogLevel::Debug
,
2896 ("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
2898 (NS_SUCCEEDED(rv
) ? "success" : "failure"), this,
2899 (nsIStreamListener
*)mDestListener
, static_cast<uint32_t>(rv
)));
2904 * http validate class. check a channel for a 304
2907 NS_IMPL_ISUPPORTS(imgCacheValidator
, nsIStreamListener
, nsIRequestObserver
,
2908 nsIThreadRetargetableStreamListener
, nsIChannelEventSink
,
2909 nsIInterfaceRequestor
, nsIAsyncVerifyRedirectCallback
)
2911 imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy
* progress
,
2912 imgLoader
* loader
, imgRequest
* request
,
2913 Document
* aDocument
,
2914 uint64_t aInnerWindowId
,
2915 bool forcePrincipalCheckForCacheEntry
)
2916 : mProgressProxy(progress
),
2918 mDocument(aDocument
),
2919 mInnerWindowId(aInnerWindowId
),
2921 mHadInsecureRedirect(false) {
2922 NewRequestAndEntry(forcePrincipalCheckForCacheEntry
, loader
,
2923 mRequest
->CacheKey(), getter_AddRefs(mNewRequest
),
2924 getter_AddRefs(mNewEntry
));
2927 imgCacheValidator::~imgCacheValidator() {
2929 // If something went wrong, and we never unblocked the requests waiting on
2930 // validation, now is our last chance. We will cancel the new request and
2931 // switch the waiting proxies to it.
2932 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
2936 void imgCacheValidator::AddProxy(imgRequestProxy
* aProxy
) {
2937 // aProxy needs to be in the loadgroup since we're validating from
2939 aProxy
->AddToLoadGroup();
2941 mProxies
.AppendElement(aProxy
);
2944 void imgCacheValidator::RemoveProxy(imgRequestProxy
* aProxy
) {
2945 mProxies
.RemoveElement(aProxy
);
2948 void imgCacheValidator::UpdateProxies(bool aCancelRequest
, bool aSyncNotify
) {
2949 MOZ_ASSERT(mRequest
);
2951 // Clear the validator before updating the proxies. The notifications may
2952 // clone an existing request, and its state could be inconsistent.
2953 mRequest
->SetValidator(nullptr);
2956 // If an error occurred, we will want to cancel the new request, and make the
2957 // validating proxies point to it. Any proxies still bound to the original
2958 // request which are not validating should remain untouched.
2959 if (aCancelRequest
) {
2960 MOZ_ASSERT(mNewRequest
);
2961 mNewRequest
->CancelAndAbort(NS_BINDING_ABORTED
);
2964 // We have finished validating the request, so we can safely take ownership
2965 // of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
2966 // if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
2967 // that any potential notifications should still be suppressed in
2968 // imgRequestProxy::ChangeOwner because we haven't cleared the validating
2969 // flag yet, and thus they will remain deferred.
2970 AutoTArray
<RefPtr
<imgRequestProxy
>, 4> proxies(std::move(mProxies
));
2972 for (auto& proxy
: proxies
) {
2973 // First update the state of all proxies before notifying any of them
2974 // to ensure a consistent state (e.g. in case the notification causes
2975 // other proxies to be touched indirectly.)
2976 MOZ_ASSERT(proxy
->IsValidating());
2977 MOZ_ASSERT(proxy
->NotificationsDeferred(),
2978 "Proxies waiting on cache validation should be "
2979 "deferring notifications!");
2981 proxy
->ChangeOwner(mNewRequest
);
2983 proxy
->ClearValidating();
2986 mNewRequest
= nullptr;
2987 mNewEntry
= nullptr;
2989 for (auto& proxy
: proxies
) {
2991 // Notify synchronously, because the caller knows we are already in an
2992 // asynchronously-called function (e.g. OnStartRequest).
2993 proxy
->SyncNotifyListener();
2995 // Notify asynchronously, because the caller does not know our current
2996 // call state (e.g. ~imgCacheValidator).
2997 proxy
->NotifyListener();
3002 /** nsIRequestObserver methods **/
3005 imgCacheValidator::OnStartRequest(nsIRequest
* aRequest
) {
3006 // We may be holding on to a document, so ensure that it's released.
3007 RefPtr
<Document
> document
= mDocument
.forget();
3009 // If for some reason we don't still have an existing request (probably
3010 // because OnStartRequest got delivered more than once), just bail.
3012 MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
3013 aRequest
->Cancel(NS_BINDING_ABORTED
);
3014 return NS_ERROR_FAILURE
;
3017 // If this request is coming from cache and has the same URI as our
3018 // imgRequest, the request all our proxies are pointing at is valid, and all
3019 // we have to do is tell them to notify their listeners.
3020 nsCOMPtr
<nsICacheInfoChannel
> cacheChan(do_QueryInterface(aRequest
));
3021 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
3022 if (cacheChan
&& channel
&& !mRequest
->CacheChanged(aRequest
)) {
3023 bool isFromCache
= false;
3024 cacheChan
->IsFromCache(&isFromCache
);
3026 nsCOMPtr
<nsIURI
> channelURI
;
3027 channel
->GetURI(getter_AddRefs(channelURI
));
3029 nsCOMPtr
<nsIURI
> finalURI
;
3030 mRequest
->GetFinalURI(getter_AddRefs(finalURI
));
3032 bool sameURI
= false;
3033 if (channelURI
&& finalURI
) {
3034 channelURI
->Equals(finalURI
, &sameURI
);
3037 if (isFromCache
&& sameURI
) {
3038 // We don't need to load this any more.
3039 aRequest
->Cancel(NS_BINDING_ABORTED
);
3040 mNewRequest
= nullptr;
3042 // Clear the validator before updating the proxies. The notifications may
3043 // clone an existing request, and its state could be inconsistent.
3044 mRequest
->SetLoadId(document
);
3045 mRequest
->SetInnerWindowID(mInnerWindowId
);
3046 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3051 // We can't load out of cache. We have to create a whole new request for the
3052 // data that's coming in off the channel.
3053 nsCOMPtr
<nsIURI
> uri
;
3054 mRequest
->GetURI(getter_AddRefs(uri
));
3056 LOG_MSG_WITH_PARAM(gImgLog
,
3057 "imgCacheValidator::OnStartRequest creating new request",
3060 int32_t corsmode
= mRequest
->GetCORSMode();
3061 nsCOMPtr
<nsIReferrerInfo
> referrerInfo
= mRequest
->GetReferrerInfo();
3062 nsCOMPtr
<nsIPrincipal
> triggeringPrincipal
=
3063 mRequest
->GetTriggeringPrincipal();
3065 // Doom the old request's cache entry
3066 mRequest
->RemoveFromCache();
3068 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
3069 nsCOMPtr
<nsIURI
> originalURI
;
3070 channel
->GetOriginalURI(getter_AddRefs(originalURI
));
3071 nsresult rv
= mNewRequest
->Init(originalURI
, uri
, mHadInsecureRedirect
,
3072 aRequest
, channel
, mNewEntry
, document
,
3073 triggeringPrincipal
, corsmode
, referrerInfo
);
3074 if (NS_FAILED(rv
)) {
3075 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
3079 mDestListener
= new ProxyListener(mNewRequest
);
3081 // Try to add the new request into the cache. Note that the entry must be in
3082 // the cache before the proxies' ownership changes, because adding a proxy
3083 // changes the caching behaviour for imgRequests.
3084 mImgLoader
->PutIntoCache(mNewRequest
->CacheKey(), mNewEntry
);
3085 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
3086 return mDestListener
->OnStartRequest(aRequest
);
3090 imgCacheValidator::OnStopRequest(nsIRequest
* aRequest
, nsresult status
) {
3091 // Be sure we've released the document that we may have been holding on to.
3092 mDocument
= nullptr;
3094 if (!mDestListener
) {
3098 return mDestListener
->OnStopRequest(aRequest
, status
);
3101 /** nsIStreamListener methods **/
3104 imgCacheValidator::OnDataAvailable(nsIRequest
* aRequest
, nsIInputStream
* inStr
,
3105 uint64_t sourceOffset
, uint32_t count
) {
3106 if (!mDestListener
) {
3107 // XXX see bug 113959
3109 inStr
->ReadSegments(NS_DiscardSegment
, nullptr, count
, &_retval
);
3113 return mDestListener
->OnDataAvailable(aRequest
, inStr
, sourceOffset
, count
);
3116 /** nsIThreadRetargetableStreamListener methods **/
3119 imgCacheValidator::CheckListenerChain() {
3120 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
3121 nsresult rv
= NS_OK
;
3122 nsCOMPtr
<nsIThreadRetargetableStreamListener
> retargetableListener
=
3123 do_QueryInterface(mDestListener
, &rv
);
3124 if (retargetableListener
) {
3125 rv
= retargetableListener
->CheckListenerChain();
3128 gImgLog
, LogLevel::Debug
,
3129 ("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32
"=%s",
3130 this, static_cast<uint32_t>(rv
),
3131 NS_SUCCEEDED(rv
) ? "succeeded" : "failed"));
3135 /** nsIInterfaceRequestor methods **/
3138 imgCacheValidator::GetInterface(const nsIID
& aIID
, void** aResult
) {
3139 if (aIID
.Equals(NS_GET_IID(nsIChannelEventSink
))) {
3140 return QueryInterface(aIID
, aResult
);
3143 return mProgressProxy
->GetInterface(aIID
, aResult
);
3146 // These functions are materially the same as the same functions in imgRequest.
3147 // We duplicate them because we're verifying whether cache loads are necessary,
3148 // not unconditionally loading.
3150 /** nsIChannelEventSink methods **/
3152 imgCacheValidator::AsyncOnChannelRedirect(
3153 nsIChannel
* oldChannel
, nsIChannel
* newChannel
, uint32_t flags
,
3154 nsIAsyncVerifyRedirectCallback
* callback
) {
3155 // Note all cache information we get from the old channel.
3156 mNewRequest
->SetCacheValidation(mNewEntry
, oldChannel
);
3158 // If the previous URI is a non-HTTPS URI, record that fact for later use by
3159 // security code, which needs to know whether there is an insecure load at any
3160 // point in the redirect chain.
3161 nsCOMPtr
<nsIURI
> oldURI
;
3162 bool schemeLocal
= false;
3163 if (NS_FAILED(oldChannel
->GetURI(getter_AddRefs(oldURI
))) ||
3164 NS_FAILED(NS_URIChainHasFlags(
3165 oldURI
, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE
, &schemeLocal
)) ||
3166 (!oldURI
->SchemeIs("https") && !oldURI
->SchemeIs("chrome") &&
3168 mHadInsecureRedirect
= true;
3171 // Prepare for callback
3172 mRedirectCallback
= callback
;
3173 mRedirectChannel
= newChannel
;
3175 return mProgressProxy
->AsyncOnChannelRedirect(oldChannel
, newChannel
, flags
,
3180 imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult
) {
3181 // If we've already been told to abort, just do so.
3182 if (NS_FAILED(aResult
)) {
3183 mRedirectCallback
->OnRedirectVerifyCallback(aResult
);
3184 mRedirectCallback
= nullptr;
3185 mRedirectChannel
= nullptr;
3189 // make sure we have a protocol that returns data rather than opens
3190 // an external application, e.g. mailto:
3191 nsCOMPtr
<nsIURI
> uri
;
3192 mRedirectChannel
->GetURI(getter_AddRefs(uri
));
3193 bool doesNotReturnData
= false;
3194 NS_URIChainHasFlags(uri
, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA
,
3195 &doesNotReturnData
);
3197 nsresult result
= NS_OK
;
3199 if (doesNotReturnData
) {
3200 result
= NS_ERROR_ABORT
;
3203 mRedirectCallback
->OnRedirectVerifyCallback(result
);
3204 mRedirectCallback
= nullptr;
3205 mRedirectChannel
= nullptr;