Bug 1622408 [wpt PR 22244] - Restore the event delegate for a CSSTransition after...
[gecko.git] / image / imgLoader.cpp
blobe1916f406b5b6f204b5d21a14120d340693ac518
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 #undef LoadImage
10 #include "imgLoader.h"
12 #include <algorithm>
13 #include <utility>
15 #include "DecoderFactory.h"
16 #include "Image.h"
17 #include "ImageLogging.h"
18 #include "ReferrerInfo.h"
19 #include "imgRequestProxy.h"
20 #include "mozilla/Attributes.h"
21 #include "mozilla/BasePrincipal.h"
22 #include "mozilla/ChaosMode.h"
23 #include "mozilla/ClearOnShutdown.h"
24 #include "mozilla/LoadInfo.h"
25 #include "mozilla/NullPrincipal.h"
26 #include "mozilla/Preferences.h"
27 #include "mozilla/StaticPrefs_image.h"
28 #include "mozilla/StaticPrefs_network.h"
29 #include "mozilla/dom/ContentParent.h"
30 #include "mozilla/dom/nsMixedContentBlocker.h"
31 #include "mozilla/image/ImageMemoryReporter.h"
32 #include "mozilla/layers/CompositorManagerChild.h"
33 #include "nsCOMPtr.h"
34 #include "nsCRT.h"
35 #include "nsContentPolicyUtils.h"
36 #include "nsContentUtils.h"
37 #include "nsIApplicationCache.h"
38 #include "nsIApplicationCacheContainer.h"
39 #include "nsIAsyncVerifyRedirectCallback.h"
40 #include "nsICacheInfoChannel.h"
41 #include "nsIChannelEventSink.h"
42 #include "nsIClassOfService.h"
43 #include "nsIFile.h"
44 #include "nsIFileURL.h"
45 #include "nsIHttpChannel.h"
46 #include "nsIInterfaceRequestor.h"
47 #include "nsIInterfaceRequestorUtils.h"
48 #include "nsIMemoryReporter.h"
49 #include "nsINetworkPredictor.h"
50 #include "nsIProgressEventSink.h"
51 #include "nsIProtocolHandler.h"
52 #include "nsImageModule.h"
53 #include "nsMimeTypes.h"
54 #include "nsNetCID.h"
55 #include "nsNetUtil.h"
56 #include "nsReadableUtils.h"
57 #include "nsStreamUtils.h"
58 #include "prtime.h"
60 // we want to explore making the document own the load group
61 // so we can associate the document URI with the load group.
62 // until this point, we have an evil hack:
63 #include "nsIHttpChannelInternal.h"
64 #include "nsILoadGroupChild.h"
65 #include "nsIDocShell.h"
67 using namespace mozilla;
68 using namespace mozilla::dom;
69 using namespace mozilla::image;
70 using namespace mozilla::net;
72 MOZ_DEFINE_MALLOC_SIZE_OF(ImagesMallocSizeOf)
74 class imgMemoryReporter final : public nsIMemoryReporter {
75 ~imgMemoryReporter() = default;
77 public:
78 NS_DECL_ISUPPORTS
80 NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
81 nsISupports* aData, bool aAnonymize) override {
82 MOZ_ASSERT(NS_IsMainThread());
84 layers::CompositorManagerChild* manager =
85 mozilla::layers::CompositorManagerChild::GetInstance();
86 if (!manager || !StaticPrefs::image_mem_debug_reporting()) {
87 layers::SharedSurfacesMemoryReport sharedSurfaces;
88 FinishCollectReports(aHandleReport, aData, aAnonymize, sharedSurfaces);
89 return NS_OK;
92 RefPtr<imgMemoryReporter> self(this);
93 nsCOMPtr<nsIHandleReportCallback> handleReport(aHandleReport);
94 nsCOMPtr<nsISupports> data(aData);
95 manager->SendReportSharedSurfacesMemory(
96 [=](layers::SharedSurfacesMemoryReport aReport) {
97 self->FinishCollectReports(handleReport, data, aAnonymize, aReport);
99 [=](mozilla::ipc::ResponseRejectReason&& aReason) {
100 layers::SharedSurfacesMemoryReport sharedSurfaces;
101 self->FinishCollectReports(handleReport, data, aAnonymize,
102 sharedSurfaces);
104 return NS_OK;
107 void FinishCollectReports(
108 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
109 bool aAnonymize, layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
110 nsTArray<ImageMemoryCounter> chrome;
111 nsTArray<ImageMemoryCounter> content;
112 nsTArray<ImageMemoryCounter> uncached;
114 for (uint32_t i = 0; i < mKnownLoaders.Length(); i++) {
115 for (auto iter = mKnownLoaders[i]->mChromeCache.Iter(); !iter.Done();
116 iter.Next()) {
117 imgCacheEntry* entry = iter.UserData();
118 RefPtr<imgRequest> req = entry->GetRequest();
119 RecordCounterForRequest(req, &chrome, !entry->HasNoProxies());
121 for (auto iter = mKnownLoaders[i]->mCache.Iter(); !iter.Done();
122 iter.Next()) {
123 imgCacheEntry* entry = iter.UserData();
124 RefPtr<imgRequest> req = entry->GetRequest();
125 RecordCounterForRequest(req, &content, !entry->HasNoProxies());
127 MutexAutoLock lock(mKnownLoaders[i]->mUncachedImagesMutex);
128 for (auto iter = mKnownLoaders[i]->mUncachedImages.Iter(); !iter.Done();
129 iter.Next()) {
130 nsPtrHashKey<imgRequest>* entry = iter.Get();
131 RefPtr<imgRequest> req = entry->GetKey();
132 RecordCounterForRequest(req, &uncached, req->HasConsumers());
136 // Note that we only need to anonymize content image URIs.
138 ReportCounterArray(aHandleReport, aData, chrome, "images/chrome",
139 /* aAnonymize */ false, aSharedSurfaces);
141 ReportCounterArray(aHandleReport, aData, content, "images/content",
142 aAnonymize, aSharedSurfaces);
144 // Uncached images may be content or chrome, so anonymize them.
145 ReportCounterArray(aHandleReport, aData, uncached, "images/uncached",
146 aAnonymize, aSharedSurfaces);
148 // Report any shared surfaces that were not merged with the surface cache.
149 ImageMemoryReporter::ReportSharedSurfaces(aHandleReport, aData,
150 aSharedSurfaces);
152 nsCOMPtr<nsIMemoryReporterManager> imgr =
153 do_GetService("@mozilla.org/memory-reporter-manager;1");
154 if (imgr) {
155 imgr->EndReport();
159 static int64_t ImagesContentUsedUncompressedDistinguishedAmount() {
160 size_t n = 0;
161 for (uint32_t i = 0; i < imgLoader::sMemReporter->mKnownLoaders.Length();
162 i++) {
163 for (auto iter = imgLoader::sMemReporter->mKnownLoaders[i]->mCache.Iter();
164 !iter.Done(); iter.Next()) {
165 imgCacheEntry* entry = iter.UserData();
166 if (entry->HasNoProxies()) {
167 continue;
170 RefPtr<imgRequest> req = entry->GetRequest();
171 RefPtr<image::Image> image = req->GetImage();
172 if (!image) {
173 continue;
176 // Both this and EntryImageSizes measure
177 // images/content/raster/used/decoded memory. This function's
178 // measurement is secondary -- the result doesn't go in the "explicit"
179 // tree -- so we use moz_malloc_size_of instead of ImagesMallocSizeOf to
180 // prevent DMD from seeing it reported twice.
181 SizeOfState state(moz_malloc_size_of);
182 ImageMemoryCounter counter(req, image, state, /* aIsUsed = */ true);
184 n += counter.Values().DecodedHeap();
185 n += counter.Values().DecodedNonHeap();
186 n += counter.Values().DecodedUnknown();
189 return n;
192 void RegisterLoader(imgLoader* aLoader) {
193 mKnownLoaders.AppendElement(aLoader);
196 void UnregisterLoader(imgLoader* aLoader) {
197 mKnownLoaders.RemoveElement(aLoader);
200 private:
201 nsTArray<imgLoader*> mKnownLoaders;
203 struct MemoryTotal {
204 MemoryTotal& operator+=(const ImageMemoryCounter& aImageCounter) {
205 if (aImageCounter.Type() == imgIContainer::TYPE_RASTER) {
206 if (aImageCounter.IsUsed()) {
207 mUsedRasterCounter += aImageCounter.Values();
208 } else {
209 mUnusedRasterCounter += aImageCounter.Values();
211 } else if (aImageCounter.Type() == imgIContainer::TYPE_VECTOR) {
212 if (aImageCounter.IsUsed()) {
213 mUsedVectorCounter += aImageCounter.Values();
214 } else {
215 mUnusedVectorCounter += aImageCounter.Values();
217 } else if (aImageCounter.Type() == imgIContainer::TYPE_REQUEST) {
218 // Nothing to do, we did not get to the point of having an image.
219 } else {
220 MOZ_CRASH("Unexpected image type");
223 return *this;
226 const MemoryCounter& UsedRaster() const { return mUsedRasterCounter; }
227 const MemoryCounter& UnusedRaster() const { return mUnusedRasterCounter; }
228 const MemoryCounter& UsedVector() const { return mUsedVectorCounter; }
229 const MemoryCounter& UnusedVector() const { return mUnusedVectorCounter; }
231 private:
232 MemoryCounter mUsedRasterCounter;
233 MemoryCounter mUnusedRasterCounter;
234 MemoryCounter mUsedVectorCounter;
235 MemoryCounter mUnusedVectorCounter;
238 // Reports all images of a single kind, e.g. all used chrome images.
239 void ReportCounterArray(nsIHandleReportCallback* aHandleReport,
240 nsISupports* aData,
241 nsTArray<ImageMemoryCounter>& aCounterArray,
242 const char* aPathPrefix, bool aAnonymize,
243 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
244 MemoryTotal summaryTotal;
245 MemoryTotal nonNotableTotal;
247 // Report notable images, and compute total and non-notable aggregate sizes.
248 for (uint32_t i = 0; i < aCounterArray.Length(); i++) {
249 ImageMemoryCounter& counter = aCounterArray[i];
251 if (aAnonymize) {
252 counter.URI().Truncate();
253 counter.URI().AppendPrintf("<anonymized-%u>", i);
254 } else {
255 // The URI could be an extremely long data: URI. Truncate if needed.
256 static const size_t max = 256;
257 if (counter.URI().Length() > max) {
258 counter.URI().Truncate(max);
259 counter.URI().AppendLiteral(" (truncated)");
261 counter.URI().ReplaceChar('/', '\\');
264 summaryTotal += counter;
266 if (counter.IsNotable() || StaticPrefs::image_mem_debug_reporting()) {
267 ReportImage(aHandleReport, aData, aPathPrefix, counter,
268 aSharedSurfaces);
269 } else {
270 ImageMemoryReporter::TrimSharedSurfaces(counter, aSharedSurfaces);
271 nonNotableTotal += counter;
275 // Report non-notable images in aggregate.
276 ReportTotal(aHandleReport, aData, /* aExplicit = */ true, aPathPrefix,
277 "<non-notable images>/", nonNotableTotal);
279 // Report a summary in aggregate, outside of the explicit tree.
280 ReportTotal(aHandleReport, aData, /* aExplicit = */ false, aPathPrefix, "",
281 summaryTotal);
284 static void ReportImage(nsIHandleReportCallback* aHandleReport,
285 nsISupports* aData, const char* aPathPrefix,
286 const ImageMemoryCounter& aCounter,
287 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
288 nsAutoCString pathPrefix(NS_LITERAL_CSTRING("explicit/"));
289 pathPrefix.Append(aPathPrefix);
291 switch (aCounter.Type()) {
292 case imgIContainer::TYPE_RASTER:
293 pathPrefix.AppendLiteral("/raster/");
294 break;
295 case imgIContainer::TYPE_VECTOR:
296 pathPrefix.AppendLiteral("/vector/");
297 break;
298 case imgIContainer::TYPE_REQUEST:
299 pathPrefix.AppendLiteral("/request/");
300 break;
301 default:
302 pathPrefix.AppendLiteral("/unknown=");
303 pathPrefix.AppendInt(aCounter.Type());
304 pathPrefix.AppendLiteral("/");
305 break;
308 pathPrefix.Append(aCounter.IsUsed() ? "used/" : "unused/");
309 if (aCounter.IsValidating()) {
310 pathPrefix.AppendLiteral("validating/");
312 if (aCounter.HasError()) {
313 pathPrefix.AppendLiteral("err/");
316 pathPrefix.AppendLiteral("progress=");
317 pathPrefix.AppendInt(aCounter.Progress(), 16);
318 pathPrefix.AppendLiteral("/");
320 pathPrefix.AppendLiteral("image(");
321 pathPrefix.AppendInt(aCounter.IntrinsicSize().width);
322 pathPrefix.AppendLiteral("x");
323 pathPrefix.AppendInt(aCounter.IntrinsicSize().height);
324 pathPrefix.AppendLiteral(", ");
326 if (aCounter.URI().IsEmpty()) {
327 pathPrefix.AppendLiteral("<unknown URI>");
328 } else {
329 pathPrefix.Append(aCounter.URI());
332 pathPrefix.AppendLiteral(")/");
334 ReportSurfaces(aHandleReport, aData, pathPrefix, aCounter, aSharedSurfaces);
336 ReportSourceValue(aHandleReport, aData, pathPrefix, aCounter.Values());
339 static void ReportSurfaces(
340 nsIHandleReportCallback* aHandleReport, nsISupports* aData,
341 const nsACString& aPathPrefix, const ImageMemoryCounter& aCounter,
342 layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
343 for (const SurfaceMemoryCounter& counter : aCounter.Surfaces()) {
344 nsAutoCString surfacePathPrefix(aPathPrefix);
345 if (counter.IsLocked()) {
346 surfacePathPrefix.AppendLiteral("locked/");
347 } else {
348 surfacePathPrefix.AppendLiteral("unlocked/");
350 if (counter.IsFactor2()) {
351 surfacePathPrefix.AppendLiteral("factor2/");
353 if (counter.CannotSubstitute()) {
354 surfacePathPrefix.AppendLiteral("cannot_substitute/");
356 surfacePathPrefix.AppendLiteral("types=");
357 surfacePathPrefix.AppendInt(counter.Values().SurfaceTypes(), 16);
358 surfacePathPrefix.AppendLiteral("/surface(");
359 surfacePathPrefix.AppendInt(counter.Key().Size().width);
360 surfacePathPrefix.AppendLiteral("x");
361 surfacePathPrefix.AppendInt(counter.Key().Size().height);
363 if (!counter.IsFinished()) {
364 surfacePathPrefix.AppendLiteral(", incomplete");
367 if (counter.Values().ExternalHandles() > 0) {
368 surfacePathPrefix.AppendLiteral(", handles:");
369 surfacePathPrefix.AppendInt(
370 uint32_t(counter.Values().ExternalHandles()));
373 ImageMemoryReporter::AppendSharedSurfacePrefix(surfacePathPrefix, counter,
374 aSharedSurfaces);
376 if (counter.Type() == SurfaceMemoryCounterType::NORMAL) {
377 PlaybackType playback = counter.Key().Playback();
378 if (playback == PlaybackType::eAnimated) {
379 if (StaticPrefs::image_mem_debug_reporting()) {
380 surfacePathPrefix.AppendPrintf(
381 " (animation %4u)", uint32_t(counter.Values().FrameIndex()));
382 } else {
383 surfacePathPrefix.AppendLiteral(" (animation)");
387 if (counter.Key().Flags() != DefaultSurfaceFlags()) {
388 surfacePathPrefix.AppendLiteral(", flags:");
389 surfacePathPrefix.AppendInt(uint32_t(counter.Key().Flags()),
390 /* aRadix = */ 16);
393 if (counter.Key().SVGContext()) {
394 const SVGImageContext& context = counter.Key().SVGContext().ref();
395 surfacePathPrefix.AppendLiteral(", svgContext:[ ");
396 if (context.GetViewportSize()) {
397 const CSSIntSize& size = context.GetViewportSize().ref();
398 surfacePathPrefix.AppendLiteral("viewport=(");
399 surfacePathPrefix.AppendInt(size.width);
400 surfacePathPrefix.AppendLiteral("x");
401 surfacePathPrefix.AppendInt(size.height);
402 surfacePathPrefix.AppendLiteral(") ");
404 if (context.GetPreserveAspectRatio()) {
405 nsAutoString aspect;
406 context.GetPreserveAspectRatio()->ToString(aspect);
407 surfacePathPrefix.AppendLiteral("preserveAspectRatio=(");
408 LossyAppendUTF16toASCII(aspect, surfacePathPrefix);
409 surfacePathPrefix.AppendLiteral(") ");
411 if (context.GetContextPaint()) {
412 const SVGEmbeddingContextPaint* paint = context.GetContextPaint();
413 surfacePathPrefix.AppendLiteral("contextPaint=(");
414 if (paint->GetFill()) {
415 surfacePathPrefix.AppendLiteral(" fill=");
416 surfacePathPrefix.AppendInt(paint->GetFill()->ToABGR(), 16);
418 if (paint->GetFillOpacity()) {
419 surfacePathPrefix.AppendLiteral(" fillOpa=");
420 surfacePathPrefix.AppendFloat(paint->GetFillOpacity());
422 if (paint->GetStroke()) {
423 surfacePathPrefix.AppendLiteral(" stroke=");
424 surfacePathPrefix.AppendInt(paint->GetStroke()->ToABGR(), 16);
426 if (paint->GetStrokeOpacity()) {
427 surfacePathPrefix.AppendLiteral(" strokeOpa=");
428 surfacePathPrefix.AppendFloat(paint->GetStrokeOpacity());
430 surfacePathPrefix.AppendLiteral(" ) ");
432 surfacePathPrefix.AppendLiteral("]");
434 } else if (counter.Type() == SurfaceMemoryCounterType::COMPOSITING) {
435 surfacePathPrefix.AppendLiteral(", compositing frame");
436 } else if (counter.Type() == SurfaceMemoryCounterType::COMPOSITING_PREV) {
437 surfacePathPrefix.AppendLiteral(", compositing prev frame");
438 } else {
439 MOZ_ASSERT_UNREACHABLE("Unknown counter type");
442 surfacePathPrefix.AppendLiteral(")/");
444 ReportValues(aHandleReport, aData, surfacePathPrefix, counter.Values());
448 static void ReportTotal(nsIHandleReportCallback* aHandleReport,
449 nsISupports* aData, bool aExplicit,
450 const char* aPathPrefix, const char* aPathInfix,
451 const MemoryTotal& aTotal) {
452 nsAutoCString pathPrefix;
453 if (aExplicit) {
454 pathPrefix.AppendLiteral("explicit/");
456 pathPrefix.Append(aPathPrefix);
458 nsAutoCString rasterUsedPrefix(pathPrefix);
459 rasterUsedPrefix.AppendLiteral("/raster/used/");
460 rasterUsedPrefix.Append(aPathInfix);
461 ReportValues(aHandleReport, aData, rasterUsedPrefix, aTotal.UsedRaster());
463 nsAutoCString rasterUnusedPrefix(pathPrefix);
464 rasterUnusedPrefix.AppendLiteral("/raster/unused/");
465 rasterUnusedPrefix.Append(aPathInfix);
466 ReportValues(aHandleReport, aData, rasterUnusedPrefix,
467 aTotal.UnusedRaster());
469 nsAutoCString vectorUsedPrefix(pathPrefix);
470 vectorUsedPrefix.AppendLiteral("/vector/used/");
471 vectorUsedPrefix.Append(aPathInfix);
472 ReportValues(aHandleReport, aData, vectorUsedPrefix, aTotal.UsedVector());
474 nsAutoCString vectorUnusedPrefix(pathPrefix);
475 vectorUnusedPrefix.AppendLiteral("/vector/unused/");
476 vectorUnusedPrefix.Append(aPathInfix);
477 ReportValues(aHandleReport, aData, vectorUnusedPrefix,
478 aTotal.UnusedVector());
481 static void ReportValues(nsIHandleReportCallback* aHandleReport,
482 nsISupports* aData, const nsACString& aPathPrefix,
483 const MemoryCounter& aCounter) {
484 ReportSourceValue(aHandleReport, aData, aPathPrefix, aCounter);
486 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "decoded-heap",
487 "Decoded image data which is stored on the heap.",
488 aCounter.DecodedHeap());
490 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
491 "decoded-nonheap",
492 "Decoded image data which isn't stored on the heap.",
493 aCounter.DecodedNonHeap());
495 // We don't know for certain whether or not it is on the heap, so let's
496 // just report it as non-heap for reporting purposes.
497 ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
498 "decoded-unknown",
499 "Decoded image data which is unknown to be on the heap or not.",
500 aCounter.DecodedUnknown());
503 static void ReportSourceValue(nsIHandleReportCallback* aHandleReport,
504 nsISupports* aData,
505 const nsACString& aPathPrefix,
506 const MemoryCounter& aCounter) {
507 ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "source",
508 "Raster image source data and vector image documents.",
509 aCounter.Source());
512 static void ReportValue(nsIHandleReportCallback* aHandleReport,
513 nsISupports* aData, int32_t aKind,
514 const nsACString& aPathPrefix,
515 const char* aPathSuffix, const char* aDescription,
516 size_t aValue) {
517 if (aValue == 0) {
518 return;
521 nsAutoCString desc(aDescription);
522 nsAutoCString path(aPathPrefix);
523 path.Append(aPathSuffix);
525 aHandleReport->Callback(EmptyCString(), path, aKind, UNITS_BYTES, aValue,
526 desc, aData);
529 static void RecordCounterForRequest(imgRequest* aRequest,
530 nsTArray<ImageMemoryCounter>* aArray,
531 bool aIsUsed) {
532 SizeOfState state(ImagesMallocSizeOf);
533 RefPtr<image::Image> image = aRequest->GetImage();
534 if (image) {
535 ImageMemoryCounter counter(aRequest, image, state, aIsUsed);
536 aArray->AppendElement(std::move(counter));
537 } else {
538 // We can at least record some information about the image from the
539 // request, and mark it as not knowing the image type yet.
540 ImageMemoryCounter counter(aRequest, state, aIsUsed);
541 aArray->AppendElement(std::move(counter));
546 NS_IMPL_ISUPPORTS(imgMemoryReporter, nsIMemoryReporter)
548 NS_IMPL_ISUPPORTS(nsProgressNotificationProxy, nsIProgressEventSink,
549 nsIChannelEventSink, nsIInterfaceRequestor)
551 NS_IMETHODIMP
552 nsProgressNotificationProxy::OnProgress(nsIRequest* request, nsISupports* ctxt,
553 int64_t progress, int64_t progressMax) {
554 nsCOMPtr<nsILoadGroup> loadGroup;
555 request->GetLoadGroup(getter_AddRefs(loadGroup));
557 nsCOMPtr<nsIProgressEventSink> target;
558 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
559 NS_GET_IID(nsIProgressEventSink),
560 getter_AddRefs(target));
561 if (!target) {
562 return NS_OK;
564 return target->OnProgress(mImageRequest, ctxt, progress, progressMax);
567 NS_IMETHODIMP
568 nsProgressNotificationProxy::OnStatus(nsIRequest* request, nsISupports* ctxt,
569 nsresult status,
570 const char16_t* statusArg) {
571 nsCOMPtr<nsILoadGroup> loadGroup;
572 request->GetLoadGroup(getter_AddRefs(loadGroup));
574 nsCOMPtr<nsIProgressEventSink> target;
575 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
576 NS_GET_IID(nsIProgressEventSink),
577 getter_AddRefs(target));
578 if (!target) {
579 return NS_OK;
581 return target->OnStatus(mImageRequest, ctxt, status, statusArg);
584 NS_IMETHODIMP
585 nsProgressNotificationProxy::AsyncOnChannelRedirect(
586 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
587 nsIAsyncVerifyRedirectCallback* cb) {
588 // Tell the original original callbacks about it too
589 nsCOMPtr<nsILoadGroup> loadGroup;
590 newChannel->GetLoadGroup(getter_AddRefs(loadGroup));
591 nsCOMPtr<nsIChannelEventSink> target;
592 NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
593 NS_GET_IID(nsIChannelEventSink),
594 getter_AddRefs(target));
595 if (!target) {
596 cb->OnRedirectVerifyCallback(NS_OK);
597 return NS_OK;
600 // Delegate to |target| if set, reusing |cb|
601 return target->AsyncOnChannelRedirect(oldChannel, newChannel, flags, cb);
604 NS_IMETHODIMP
605 nsProgressNotificationProxy::GetInterface(const nsIID& iid, void** result) {
606 if (iid.Equals(NS_GET_IID(nsIProgressEventSink))) {
607 *result = static_cast<nsIProgressEventSink*>(this);
608 NS_ADDREF_THIS();
609 return NS_OK;
611 if (iid.Equals(NS_GET_IID(nsIChannelEventSink))) {
612 *result = static_cast<nsIChannelEventSink*>(this);
613 NS_ADDREF_THIS();
614 return NS_OK;
616 if (mOriginalCallbacks) {
617 return mOriginalCallbacks->GetInterface(iid, result);
619 return NS_NOINTERFACE;
622 static void NewRequestAndEntry(bool aForcePrincipalCheckForCacheEntry,
623 imgLoader* aLoader, const ImageCacheKey& aKey,
624 imgRequest** aRequest, imgCacheEntry** aEntry) {
625 RefPtr<imgRequest> request = new imgRequest(aLoader, aKey);
626 RefPtr<imgCacheEntry> entry =
627 new imgCacheEntry(aLoader, request, aForcePrincipalCheckForCacheEntry);
628 aLoader->AddToUncachedImages(request);
629 request.forget(aRequest);
630 entry.forget(aEntry);
633 static bool ShouldRevalidateEntry(imgCacheEntry* aEntry, nsLoadFlags aFlags,
634 bool aHasExpired) {
635 bool bValidateEntry = false;
637 if (aFlags & nsIRequest::LOAD_BYPASS_CACHE) {
638 return false;
641 if (aFlags & nsIRequest::VALIDATE_ALWAYS) {
642 bValidateEntry = true;
643 } else if (aEntry->GetMustValidate()) {
644 bValidateEntry = true;
645 } else if (aHasExpired) {
646 // The cache entry has expired... Determine whether the stale cache
647 // entry can be used without validation...
648 if (aFlags &
649 (nsIRequest::VALIDATE_NEVER | nsIRequest::VALIDATE_ONCE_PER_SESSION)) {
650 // VALIDATE_NEVER and VALIDATE_ONCE_PER_SESSION allow stale cache
651 // entries to be used unless they have been explicitly marked to
652 // indicate that revalidation is necessary.
653 bValidateEntry = false;
655 } else if (!(aFlags & nsIRequest::LOAD_FROM_CACHE)) {
656 // LOAD_FROM_CACHE allows a stale cache entry to be used... Otherwise,
657 // the entry must be revalidated.
658 bValidateEntry = true;
662 return bValidateEntry;
665 /* Call content policies on cached images that went through a redirect */
666 static bool ShouldLoadCachedImage(imgRequest* aImgRequest,
667 nsISupports* aLoadingContext,
668 nsIPrincipal* aTriggeringPrincipal,
669 nsContentPolicyType aPolicyType,
670 bool aSendCSPViolationReports) {
671 /* Call content policies on cached images - Bug 1082837
672 * Cached images are keyed off of the first uri in a redirect chain.
673 * Hence content policies don't get a chance to test the intermediate hops
674 * or the final desitnation. Here we test the final destination using
675 * mFinalURI off of the imgRequest and passing it into content policies.
676 * For Mixed Content Blocker, we do an additional check to determine if any
677 * of the intermediary hops went through an insecure redirect with the
678 * mHadInsecureRedirect flag
680 bool insecureRedirect = aImgRequest->HadInsecureRedirect();
681 nsCOMPtr<nsIURI> contentLocation;
682 aImgRequest->GetFinalURI(getter_AddRefs(contentLocation));
683 nsresult rv;
685 nsCOMPtr<nsINode> requestingNode = do_QueryInterface(aLoadingContext);
686 nsCOMPtr<nsIPrincipal> loadingPrincipal =
687 requestingNode ? requestingNode->NodePrincipal() : aTriggeringPrincipal;
688 // If there is no context and also no triggeringPrincipal, then we use a fresh
689 // nullPrincipal as the loadingPrincipal because we can not create a loadinfo
690 // without a valid loadingPrincipal.
691 if (!loadingPrincipal) {
692 loadingPrincipal = NullPrincipal::CreateWithoutOriginAttributes();
695 nsCOMPtr<nsILoadInfo> secCheckLoadInfo = new LoadInfo(
696 loadingPrincipal, aTriggeringPrincipal, requestingNode,
697 nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK, aPolicyType);
699 secCheckLoadInfo->SetSendCSPViolationEvents(aSendCSPViolationReports);
701 int16_t decision = nsIContentPolicy::REJECT_REQUEST;
702 rv = NS_CheckContentLoadPolicy(contentLocation, secCheckLoadInfo,
703 EmptyCString(), // mime guess
704 &decision, nsContentUtils::GetContentPolicy());
705 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
706 return false;
709 // We call all Content Policies above, but we also have to call mcb
710 // individually to check the intermediary redirect hops are secure.
711 if (insecureRedirect) {
712 // Bug 1314356: If the image ended up in the cache upgraded by HSTS and the
713 // page uses upgrade-inscure-requests it had an insecure redirect
714 // (http->https). We need to invalidate the image and reload it because
715 // mixed content blocker only bails if upgrade-insecure-requests is set on
716 // the doc and the resource load is http: which would result in an incorrect
717 // mixed content warning.
718 nsCOMPtr<nsIDocShell> docShell =
719 NS_CP_GetDocShellFromContext(aLoadingContext);
720 if (docShell) {
721 Document* document = docShell->GetDocument();
722 if (document && document->GetUpgradeInsecureRequests(false)) {
723 return false;
727 if (!aTriggeringPrincipal || !aTriggeringPrincipal->IsSystemPrincipal()) {
728 // reset the decision for mixed content blocker check
729 decision = nsIContentPolicy::REJECT_REQUEST;
730 rv = nsMixedContentBlocker::ShouldLoad(insecureRedirect, aPolicyType,
731 contentLocation, nullptr,
732 aLoadingContext,
733 EmptyCString(), // mime guess
734 aTriggeringPrincipal, &decision);
735 if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
736 return false;
741 return true;
744 // Returns true if this request is compatible with the given CORS mode on the
745 // given loading principal, and false if the request may not be reused due
746 // to CORS. Also checks the Referrer Policy, since requests with different
747 // referrers/policies may generate different responses.
748 static bool ValidateSecurityInfo(imgRequest* request, bool forcePrincipalCheck,
749 int32_t corsmode,
750 nsIPrincipal* triggeringPrincipal,
751 nsISupports* aCX,
752 nsContentPolicyType aPolicyType,
753 nsIReferrerInfo* aReferrerInfo) {
754 // If the referrer policy doesn't match, we can't use this request.
755 // XXX: Note that we only validate referrer policy, not referrerInfo object.
756 // We should do with referrerInfo object, but it will cause us to use more
757 // resources in the common case (the same policies but different original
758 // referrers).
759 // XXX: this will return false if an image has different referrer attributes,
760 // i.e. we currently don't use the cached image but reload the image with
761 // the new referrer policy bug 1174921
762 ReferrerPolicy referrerPolicy = ReferrerPolicy::_empty;
763 if (aReferrerInfo) {
764 referrerPolicy = aReferrerInfo->ReferrerPolicy();
767 ReferrerPolicy requestReferrerPolicy = ReferrerPolicy::_empty;
768 if (request->GetReferrerInfo()) {
769 requestReferrerPolicy = request->GetReferrerInfo()->ReferrerPolicy();
772 if (referrerPolicy != requestReferrerPolicy) {
773 return false;
776 // If the entry's CORS mode doesn't match, or the CORS mode matches but the
777 // document principal isn't the same, we can't use this request.
778 if (request->GetCORSMode() != corsmode) {
779 return false;
781 if (request->GetCORSMode() != imgIRequest::CORS_NONE || forcePrincipalCheck) {
782 nsCOMPtr<nsIPrincipal> otherprincipal = request->GetTriggeringPrincipal();
784 // If we previously had a principal, but we don't now, we can't use this
785 // request.
786 if (otherprincipal && !triggeringPrincipal) {
787 return false;
790 if (otherprincipal && triggeringPrincipal) {
791 bool equals = false;
792 otherprincipal->Equals(triggeringPrincipal, &equals);
793 if (!equals) {
794 return false;
799 // Content Policy Check on Cached Images
800 return ShouldLoadCachedImage(request, aCX, triggeringPrincipal, aPolicyType,
801 /* aSendCSPViolationReports */ false);
804 static nsresult NewImageChannel(
805 nsIChannel** aResult,
806 // If aForcePrincipalCheckForCacheEntry is true, then we will
807 // force a principal check even when not using CORS before
808 // assuming we have a cache hit on a cache entry that we
809 // create for this channel. This is an out param that should
810 // be set to true if this channel ends up depending on
811 // aTriggeringPrincipal and false otherwise.
812 bool* aForcePrincipalCheckForCacheEntry, nsIURI* aURI,
813 nsIURI* aInitialDocumentURI, int32_t aCORSMode,
814 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
815 nsLoadFlags aLoadFlags, nsContentPolicyType aPolicyType,
816 nsIPrincipal* aTriggeringPrincipal, nsISupports* aRequestingContext,
817 bool aRespectPrivacy) {
818 MOZ_ASSERT(aResult);
820 nsresult rv;
821 nsCOMPtr<nsIHttpChannel> newHttpChannel;
823 nsCOMPtr<nsIInterfaceRequestor> callbacks;
825 if (aLoadGroup) {
826 // Get the notification callbacks from the load group for the new channel.
828 // XXX: This is not exactly correct, because the network request could be
829 // referenced by multiple windows... However, the new channel needs
830 // something. So, using the 'first' notification callbacks is better
831 // than nothing...
833 aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
836 // Pass in a nullptr loadgroup because this is the underlying network
837 // request. This request may be referenced by several proxy image requests
838 // (possibly in different documents).
839 // If all of the proxy requests are canceled then this request should be
840 // canceled too.
843 nsCOMPtr<nsINode> requestingNode = do_QueryInterface(aRequestingContext);
845 nsSecurityFlags securityFlags =
846 aCORSMode == imgIRequest::CORS_NONE
847 ? nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS
848 : nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS;
849 if (aCORSMode == imgIRequest::CORS_ANONYMOUS) {
850 securityFlags |= nsILoadInfo::SEC_COOKIES_SAME_ORIGIN;
851 } else if (aCORSMode == imgIRequest::CORS_USE_CREDENTIALS) {
852 securityFlags |= nsILoadInfo::SEC_COOKIES_INCLUDE;
854 securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
856 // Note we are calling NS_NewChannelWithTriggeringPrincipal() here with a
857 // node and a principal. This is for things like background images that are
858 // specified by user stylesheets, where the document is being styled, but
859 // the principal is that of the user stylesheet.
860 if (requestingNode && aTriggeringPrincipal) {
861 rv = NS_NewChannelWithTriggeringPrincipal(aResult, aURI, requestingNode,
862 aTriggeringPrincipal,
863 securityFlags, aPolicyType,
864 nullptr, // PerformanceStorage
865 nullptr, // loadGroup
866 callbacks, aLoadFlags);
868 if (NS_FAILED(rv)) {
869 return rv;
872 if (aPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
873 // If this is a favicon loading, we will use the originAttributes from the
874 // triggeringPrincipal as the channel's originAttributes. This allows the
875 // favicon loading from XUL will use the correct originAttributes.
877 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
878 rv = loadInfo->SetOriginAttributes(
879 aTriggeringPrincipal->OriginAttributesRef());
881 } else {
882 // either we are loading something inside a document, in which case
883 // we should always have a requestingNode, or we are loading something
884 // outside a document, in which case the triggeringPrincipal and
885 // triggeringPrincipal should always be the systemPrincipal.
886 // However, there are exceptions: one is Notifications which create a
887 // channel in the parent process in which case we can't get a
888 // requestingNode.
889 rv = NS_NewChannel(aResult, aURI, nsContentUtils::GetSystemPrincipal(),
890 securityFlags, aPolicyType,
891 nullptr, // nsICookieJarSettings
892 nullptr, // PerformanceStorage
893 nullptr, // loadGroup
894 callbacks, aLoadFlags);
896 if (NS_FAILED(rv)) {
897 return rv;
900 // Use the OriginAttributes from the loading principal, if one is available,
901 // and adjust the private browsing ID based on what kind of load the caller
902 // has asked us to perform.
903 OriginAttributes attrs;
904 if (aTriggeringPrincipal) {
905 attrs = aTriggeringPrincipal->OriginAttributesRef();
907 attrs.mPrivateBrowsingId = aRespectPrivacy ? 1 : 0;
909 nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
910 rv = loadInfo->SetOriginAttributes(attrs);
913 if (NS_FAILED(rv)) {
914 return rv;
917 // only inherit if we have a principal
918 *aForcePrincipalCheckForCacheEntry =
919 aTriggeringPrincipal && nsContentUtils::ChannelShouldInheritPrincipal(
920 aTriggeringPrincipal, aURI,
921 /* aInheritForAboutBlank */ false,
922 /* aForceInherit */ false);
924 // Initialize HTTP-specific attributes
925 newHttpChannel = do_QueryInterface(*aResult);
926 if (newHttpChannel) {
927 nsCOMPtr<nsIHttpChannelInternal> httpChannelInternal =
928 do_QueryInterface(newHttpChannel);
929 NS_ENSURE_TRUE(httpChannelInternal, NS_ERROR_UNEXPECTED);
930 rv = httpChannelInternal->SetDocumentURI(aInitialDocumentURI);
931 MOZ_ASSERT(NS_SUCCEEDED(rv));
932 if (aReferrerInfo) {
933 DebugOnly<nsresult> rv = newHttpChannel->SetReferrerInfo(aReferrerInfo);
934 MOZ_ASSERT(NS_SUCCEEDED(rv));
938 // Image channels are loaded by default with reduced priority.
939 nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(*aResult);
940 if (p) {
941 uint32_t priority = nsISupportsPriority::PRIORITY_LOW;
943 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
944 ++priority; // further reduce priority for background loads
947 p->AdjustPriority(priority);
950 // Create a new loadgroup for this new channel, using the old group as
951 // the parent. The indirection keeps the channel insulated from cancels,
952 // but does allow a way for this revalidation to be associated with at
953 // least one base load group for scheduling/caching purposes.
955 nsCOMPtr<nsILoadGroup> loadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID);
956 nsCOMPtr<nsILoadGroupChild> childLoadGroup = do_QueryInterface(loadGroup);
957 if (childLoadGroup) {
958 childLoadGroup->SetParentLoadGroup(aLoadGroup);
960 (*aResult)->SetLoadGroup(loadGroup);
962 return NS_OK;
965 /* static */
966 uint32_t imgCacheEntry::SecondsFromPRTime(PRTime prTime) {
967 return uint32_t(int64_t(prTime) / int64_t(PR_USEC_PER_SEC));
970 imgCacheEntry::imgCacheEntry(imgLoader* loader, imgRequest* request,
971 bool forcePrincipalCheck)
972 : mLoader(loader),
973 mRequest(request),
974 mDataSize(0),
975 mTouchedTime(SecondsFromPRTime(PR_Now())),
976 mLoadTime(SecondsFromPRTime(PR_Now())),
977 mExpiryTime(0),
978 mMustValidate(false),
979 // We start off as evicted so we don't try to update the cache.
980 // PutIntoCache will set this to false.
981 mEvicted(true),
982 mHasNoProxies(true),
983 mForcePrincipalCheck(forcePrincipalCheck) {}
985 imgCacheEntry::~imgCacheEntry() {
986 LOG_FUNC(gImgLog, "imgCacheEntry::~imgCacheEntry()");
989 void imgCacheEntry::Touch(bool updateTime /* = true */) {
990 LOG_SCOPE(gImgLog, "imgCacheEntry::Touch");
992 if (updateTime) {
993 mTouchedTime = SecondsFromPRTime(PR_Now());
996 UpdateCache();
999 void imgCacheEntry::UpdateCache(int32_t diff /* = 0 */) {
1000 // Don't update the cache if we've been removed from it or it doesn't care
1001 // about our size or usage.
1002 if (!Evicted() && HasNoProxies()) {
1003 mLoader->CacheEntriesChanged(mRequest->IsChrome(), diff);
1007 void imgCacheEntry::UpdateLoadTime() {
1008 mLoadTime = SecondsFromPRTime(PR_Now());
1011 void imgCacheEntry::SetHasNoProxies(bool hasNoProxies) {
1012 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1013 if (hasNoProxies) {
1014 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies true", "uri",
1015 mRequest->CacheKey().URI());
1016 } else {
1017 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies false",
1018 "uri", mRequest->CacheKey().URI());
1022 mHasNoProxies = hasNoProxies;
1025 imgCacheQueue::imgCacheQueue() : mDirty(false), mSize(0) {}
1027 void imgCacheQueue::UpdateSize(int32_t diff) { mSize += diff; }
1029 uint32_t imgCacheQueue::GetSize() const { return mSize; }
1031 void imgCacheQueue::Remove(imgCacheEntry* entry) {
1032 uint64_t index = mQueue.IndexOf(entry);
1033 if (index == queueContainer::NoIndex) {
1034 return;
1037 mSize -= mQueue[index]->GetDataSize();
1039 // If the queue is clean and this is the first entry,
1040 // then we can efficiently remove the entry without
1041 // dirtying the sort order.
1042 if (!IsDirty() && index == 0) {
1043 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1044 mQueue.RemoveLastElement();
1045 return;
1048 // Remove from the middle of the list. This potentially
1049 // breaks the binary heap sort order.
1050 mQueue.RemoveElementAt(index);
1052 // If we only have one entry or the queue is empty, though,
1053 // then the sort order is still effectively good. Simply
1054 // refresh the list to clear the dirty flag.
1055 if (mQueue.Length() <= 1) {
1056 Refresh();
1057 return;
1060 // Otherwise we must mark the queue dirty and potentially
1061 // trigger an expensive sort later.
1062 MarkDirty();
1065 void imgCacheQueue::Push(imgCacheEntry* entry) {
1066 mSize += entry->GetDataSize();
1068 RefPtr<imgCacheEntry> refptr(entry);
1069 mQueue.AppendElement(std::move(refptr));
1070 // If we're not dirty already, then we can efficiently add this to the
1071 // binary heap immediately. This is only O(log n).
1072 if (!IsDirty()) {
1073 std::push_heap(mQueue.begin(), mQueue.end(),
1074 imgLoader::CompareCacheEntries);
1078 already_AddRefed<imgCacheEntry> imgCacheQueue::Pop() {
1079 if (mQueue.IsEmpty()) {
1080 return nullptr;
1082 if (IsDirty()) {
1083 Refresh();
1086 std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1087 RefPtr<imgCacheEntry> entry = mQueue.PopLastElement();
1089 mSize -= entry->GetDataSize();
1090 return entry.forget();
1093 void imgCacheQueue::Refresh() {
1094 // Resort the list. This is an O(3 * n) operation and best avoided
1095 // if possible.
1096 std::make_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
1097 mDirty = false;
1100 void imgCacheQueue::MarkDirty() { mDirty = true; }
1102 bool imgCacheQueue::IsDirty() { return mDirty; }
1104 uint32_t imgCacheQueue::GetNumElements() const { return mQueue.Length(); }
1106 bool imgCacheQueue::Contains(imgCacheEntry* aEntry) const {
1107 return mQueue.Contains(aEntry);
1110 imgCacheQueue::iterator imgCacheQueue::begin() { return mQueue.begin(); }
1112 imgCacheQueue::const_iterator imgCacheQueue::begin() const {
1113 return mQueue.begin();
1116 imgCacheQueue::iterator imgCacheQueue::end() { return mQueue.end(); }
1118 imgCacheQueue::const_iterator imgCacheQueue::end() const {
1119 return mQueue.end();
1122 nsresult imgLoader::CreateNewProxyForRequest(
1123 imgRequest* aRequest, nsILoadGroup* aLoadGroup, Document* aLoadingDocument,
1124 imgINotificationObserver* aObserver, nsLoadFlags aLoadFlags,
1125 imgRequestProxy** _retval) {
1126 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::CreateNewProxyForRequest",
1127 "imgRequest", aRequest);
1129 /* XXX If we move decoding onto separate threads, we should save off the
1130 calling thread here and pass it off to |proxyRequest| so that it call
1131 proxy calls to |aObserver|.
1134 RefPtr<imgRequestProxy> proxyRequest = new imgRequestProxy();
1136 /* It is important to call |SetLoadFlags()| before calling |Init()| because
1137 |Init()| adds the request to the loadgroup.
1139 proxyRequest->SetLoadFlags(aLoadFlags);
1141 nsCOMPtr<nsIURI> uri;
1142 aRequest->GetURI(getter_AddRefs(uri));
1144 // init adds itself to imgRequest's list of observers
1145 nsresult rv = proxyRequest->Init(aRequest, aLoadGroup, aLoadingDocument, uri,
1146 aObserver);
1147 if (NS_WARN_IF(NS_FAILED(rv))) {
1148 return rv;
1151 proxyRequest.forget(_retval);
1152 return NS_OK;
1155 class imgCacheExpirationTracker final
1156 : public nsExpirationTracker<imgCacheEntry, 3> {
1157 enum { TIMEOUT_SECONDS = 10 };
1159 public:
1160 imgCacheExpirationTracker();
1162 protected:
1163 void NotifyExpired(imgCacheEntry* entry) override;
1166 imgCacheExpirationTracker::imgCacheExpirationTracker()
1167 : nsExpirationTracker<imgCacheEntry, 3>(
1168 TIMEOUT_SECONDS * 1000, "imgCacheExpirationTracker",
1169 SystemGroup::EventTargetFor(TaskCategory::Other)) {}
1171 void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry* entry) {
1172 // Hold on to a reference to this entry, because the expiration tracker
1173 // mechanism doesn't.
1174 RefPtr<imgCacheEntry> kungFuDeathGrip(entry);
1176 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1177 RefPtr<imgRequest> req = entry->GetRequest();
1178 if (req) {
1179 LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheExpirationTracker::NotifyExpired",
1180 "entry", req->CacheKey().URI());
1184 // We can be called multiple times on the same entry. Don't do work multiple
1185 // times.
1186 if (!entry->Evicted()) {
1187 entry->Loader()->RemoveFromCache(entry);
1190 entry->Loader()->VerifyCacheSizes();
1193 ///////////////////////////////////////////////////////////////////////////////
1194 // imgLoader
1195 ///////////////////////////////////////////////////////////////////////////////
1197 double imgLoader::sCacheTimeWeight;
1198 uint32_t imgLoader::sCacheMaxSize;
1199 imgMemoryReporter* imgLoader::sMemReporter;
1201 NS_IMPL_ISUPPORTS(imgLoader, imgILoader, nsIContentSniffer, imgICache,
1202 nsISupportsWeakReference, nsIObserver)
1204 static imgLoader* gNormalLoader = nullptr;
1205 static imgLoader* gPrivateBrowsingLoader = nullptr;
1207 /* static */
1208 already_AddRefed<imgLoader> imgLoader::CreateImageLoader() {
1209 // In some cases, such as xpctests, XPCOM modules are not automatically
1210 // initialized. We need to make sure that our module is initialized before
1211 // we hand out imgLoader instances and code starts using them.
1212 mozilla::image::EnsureModuleInitialized();
1214 RefPtr<imgLoader> loader = new imgLoader();
1215 loader->Init();
1217 return loader.forget();
1220 imgLoader* imgLoader::NormalLoader() {
1221 if (!gNormalLoader) {
1222 gNormalLoader = CreateImageLoader().take();
1224 return gNormalLoader;
1227 imgLoader* imgLoader::PrivateBrowsingLoader() {
1228 if (!gPrivateBrowsingLoader) {
1229 gPrivateBrowsingLoader = CreateImageLoader().take();
1230 gPrivateBrowsingLoader->RespectPrivacyNotifications();
1232 return gPrivateBrowsingLoader;
1235 imgLoader::imgLoader()
1236 : mUncachedImagesMutex("imgLoader::UncachedImages"),
1237 mRespectPrivacy(false) {
1238 sMemReporter->AddRef();
1239 sMemReporter->RegisterLoader(this);
1242 imgLoader::~imgLoader() {
1243 ClearChromeImageCache();
1244 ClearImageCache();
1246 // If there are any of our imgRequest's left they are in the uncached
1247 // images set, so clear their pointer to us.
1248 MutexAutoLock lock(mUncachedImagesMutex);
1249 for (auto iter = mUncachedImages.Iter(); !iter.Done(); iter.Next()) {
1250 nsPtrHashKey<imgRequest>* entry = iter.Get();
1251 RefPtr<imgRequest> req = entry->GetKey();
1252 req->ClearLoader();
1255 sMemReporter->UnregisterLoader(this);
1256 sMemReporter->Release();
1259 void imgLoader::VerifyCacheSizes() {
1260 #ifdef DEBUG
1261 if (!mCacheTracker) {
1262 return;
1265 uint32_t cachesize = mCache.Count() + mChromeCache.Count();
1266 uint32_t queuesize =
1267 mCacheQueue.GetNumElements() + mChromeCacheQueue.GetNumElements();
1268 uint32_t trackersize = 0;
1269 for (nsExpirationTracker<imgCacheEntry, 3>::Iterator it(mCacheTracker.get());
1270 it.Next();) {
1271 trackersize++;
1273 MOZ_ASSERT(queuesize == trackersize, "Queue and tracker sizes out of sync!");
1274 MOZ_ASSERT(queuesize <= cachesize, "Queue has more elements than cache!");
1275 #endif
1278 imgLoader::imgCacheTable& imgLoader::GetCache(bool aForChrome) {
1279 return aForChrome ? mChromeCache : mCache;
1282 imgLoader::imgCacheTable& imgLoader::GetCache(const ImageCacheKey& aKey) {
1283 return GetCache(aKey.IsChrome());
1286 imgCacheQueue& imgLoader::GetCacheQueue(bool aForChrome) {
1287 return aForChrome ? mChromeCacheQueue : mCacheQueue;
1290 imgCacheQueue& imgLoader::GetCacheQueue(const ImageCacheKey& aKey) {
1291 return GetCacheQueue(aKey.IsChrome());
1294 void imgLoader::GlobalInit() {
1295 sCacheTimeWeight = StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
1296 int32_t cachesize = StaticPrefs::image_cache_size_AtStartup();
1297 sCacheMaxSize = cachesize > 0 ? cachesize : 0;
1299 sMemReporter = new imgMemoryReporter();
1300 RegisterStrongAsyncMemoryReporter(sMemReporter);
1301 RegisterImagesContentUsedUncompressedDistinguishedAmount(
1302 imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount);
1305 void imgLoader::ShutdownMemoryReporter() {
1306 UnregisterImagesContentUsedUncompressedDistinguishedAmount();
1307 UnregisterStrongMemoryReporter(sMemReporter);
1310 nsresult imgLoader::InitCache() {
1311 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
1312 if (!os) {
1313 return NS_ERROR_FAILURE;
1316 os->AddObserver(this, "memory-pressure", false);
1317 os->AddObserver(this, "chrome-flush-caches", false);
1318 os->AddObserver(this, "last-pb-context-exited", false);
1319 os->AddObserver(this, "profile-before-change", false);
1320 os->AddObserver(this, "xpcom-shutdown", false);
1322 mCacheTracker = MakeUnique<imgCacheExpirationTracker>();
1324 return NS_OK;
1327 nsresult imgLoader::Init() {
1328 InitCache();
1330 return NS_OK;
1333 NS_IMETHODIMP
1334 imgLoader::RespectPrivacyNotifications() {
1335 mRespectPrivacy = true;
1336 return NS_OK;
1339 NS_IMETHODIMP
1340 imgLoader::Observe(nsISupports* aSubject, const char* aTopic,
1341 const char16_t* aData) {
1342 if (strcmp(aTopic, "memory-pressure") == 0) {
1343 MinimizeCaches();
1344 } else if (strcmp(aTopic, "chrome-flush-caches") == 0) {
1345 MinimizeCaches();
1346 ClearChromeImageCache();
1347 } else if (strcmp(aTopic, "last-pb-context-exited") == 0) {
1348 if (mRespectPrivacy) {
1349 ClearImageCache();
1350 ClearChromeImageCache();
1352 } else if (strcmp(aTopic, "profile-before-change") == 0) {
1353 mCacheTracker = nullptr;
1354 } else if (strcmp(aTopic, "xpcom-shutdown") == 0) {
1355 mCacheTracker = nullptr;
1356 ShutdownMemoryReporter();
1358 } else {
1359 // (Nothing else should bring us here)
1360 MOZ_ASSERT(0, "Invalid topic received");
1363 return NS_OK;
1366 NS_IMETHODIMP
1367 imgLoader::ClearCache(bool chrome) {
1368 if (XRE_IsParentProcess()) {
1369 bool privateLoader = this == gPrivateBrowsingLoader;
1370 for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
1371 Unused << cp->SendClearImageCache(privateLoader, chrome);
1375 if (chrome) {
1376 return ClearChromeImageCache();
1378 return ClearImageCache();
1381 NS_IMETHODIMP
1382 imgLoader::RemoveEntriesFromPrincipal(nsIPrincipal* aPrincipal) {
1383 nsAutoString origin;
1384 nsresult rv = nsContentUtils::GetUTFOrigin(aPrincipal, origin);
1385 if (NS_WARN_IF(NS_FAILED(rv))) {
1386 return rv;
1389 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1391 imgCacheTable& cache = GetCache(aPrincipal->IsSystemPrincipal());
1392 for (auto iter = cache.Iter(); !iter.Done(); iter.Next()) {
1393 auto& key = iter.Key();
1395 if (key.OriginAttributesRef() !=
1396 BasePrincipal::Cast(aPrincipal)->OriginAttributesRef()) {
1397 continue;
1400 nsAutoString imageOrigin;
1401 nsresult rv = nsContentUtils::GetUTFOrigin(key.URI(), imageOrigin);
1402 if (NS_WARN_IF(NS_FAILED(rv))) {
1403 continue;
1406 if (imageOrigin == origin) {
1407 entriesToBeRemoved.AppendElement(iter.Data());
1411 for (auto& entry : entriesToBeRemoved) {
1412 if (!RemoveFromCache(entry)) {
1413 NS_WARNING(
1414 "Couldn't remove an entry from the cache in "
1415 "RemoveEntriesFromPrincipal()\n");
1419 return NS_OK;
1422 NS_IMETHODIMP
1423 imgLoader::RemoveEntry(nsIURI* aURI, Document* aDoc) {
1424 if (aURI) {
1425 OriginAttributes attrs;
1426 if (aDoc) {
1427 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1428 if (principal) {
1429 attrs = principal->OriginAttributesRef();
1433 ImageCacheKey key(aURI, attrs, aDoc);
1434 if (RemoveFromCache(key)) {
1435 return NS_OK;
1438 return NS_ERROR_NOT_AVAILABLE;
1441 NS_IMETHODIMP
1442 imgLoader::FindEntryProperties(nsIURI* uri, Document* aDoc,
1443 nsIProperties** _retval) {
1444 *_retval = nullptr;
1446 OriginAttributes attrs;
1447 if (aDoc) {
1448 nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
1449 if (principal) {
1450 attrs = principal->OriginAttributesRef();
1454 ImageCacheKey key(uri, attrs, aDoc);
1455 imgCacheTable& cache = GetCache(key);
1457 RefPtr<imgCacheEntry> entry;
1458 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
1459 if (mCacheTracker && entry->HasNoProxies()) {
1460 mCacheTracker->MarkUsed(entry);
1463 RefPtr<imgRequest> request = entry->GetRequest();
1464 if (request) {
1465 nsCOMPtr<nsIProperties> properties = request->Properties();
1466 properties.forget(_retval);
1470 return NS_OK;
1473 NS_IMETHODIMP_(void)
1474 imgLoader::ClearCacheForControlledDocument(Document* aDoc) {
1475 MOZ_ASSERT(aDoc);
1476 AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
1477 imgCacheTable& cache = GetCache(false);
1478 for (auto iter = cache.Iter(); !iter.Done(); iter.Next()) {
1479 auto& key = iter.Key();
1480 if (key.ControlledDocument() == aDoc) {
1481 entriesToBeRemoved.AppendElement(iter.Data());
1484 for (auto& entry : entriesToBeRemoved) {
1485 if (!RemoveFromCache(entry)) {
1486 NS_WARNING(
1487 "Couldn't remove an entry from the cache in "
1488 "ClearCacheForControlledDocument()\n");
1493 void imgLoader::Shutdown() {
1494 NS_IF_RELEASE(gNormalLoader);
1495 gNormalLoader = nullptr;
1496 NS_IF_RELEASE(gPrivateBrowsingLoader);
1497 gPrivateBrowsingLoader = nullptr;
1500 nsresult imgLoader::ClearChromeImageCache() {
1501 return EvictEntries(mChromeCache);
1504 nsresult imgLoader::ClearImageCache() { return EvictEntries(mCache); }
1506 void imgLoader::MinimizeCaches() {
1507 EvictEntries(mCacheQueue);
1508 EvictEntries(mChromeCacheQueue);
1511 bool imgLoader::PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* entry) {
1512 imgCacheTable& cache = GetCache(aKey);
1514 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::PutIntoCache", "uri",
1515 aKey.URI());
1517 // Check to see if this request already exists in the cache. If so, we'll
1518 // replace the old version.
1519 RefPtr<imgCacheEntry> tmpCacheEntry;
1520 if (cache.Get(aKey, getter_AddRefs(tmpCacheEntry)) && tmpCacheEntry) {
1521 MOZ_LOG(
1522 gImgLog, LogLevel::Debug,
1523 ("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
1524 nullptr));
1525 RefPtr<imgRequest> tmpRequest = tmpCacheEntry->GetRequest();
1527 // If it already exists, and we're putting the same key into the cache, we
1528 // should remove the old version.
1529 MOZ_LOG(gImgLog, LogLevel::Debug,
1530 ("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
1531 nullptr));
1533 RemoveFromCache(aKey);
1534 } else {
1535 MOZ_LOG(gImgLog, LogLevel::Debug,
1536 ("[this=%p] imgLoader::PutIntoCache --"
1537 " Element NOT already in the cache",
1538 nullptr));
1541 cache.Put(aKey, RefPtr{entry});
1543 // We can be called to resurrect an evicted entry.
1544 if (entry->Evicted()) {
1545 entry->SetEvicted(false);
1548 // If we're resurrecting an entry with no proxies, put it back in the
1549 // tracker and queue.
1550 if (entry->HasNoProxies()) {
1551 nsresult addrv = NS_OK;
1553 if (mCacheTracker) {
1554 addrv = mCacheTracker->AddObject(entry);
1557 if (NS_SUCCEEDED(addrv)) {
1558 imgCacheQueue& queue = GetCacheQueue(aKey);
1559 queue.Push(entry);
1563 RefPtr<imgRequest> request = entry->GetRequest();
1564 request->SetIsInCache(true);
1565 RemoveFromUncachedImages(request);
1567 return true;
1570 bool imgLoader::SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry) {
1571 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasNoProxies", "uri",
1572 aRequest->CacheKey().URI());
1574 aEntry->SetHasNoProxies(true);
1576 if (aEntry->Evicted()) {
1577 return false;
1580 imgCacheQueue& queue = GetCacheQueue(aRequest->IsChrome());
1582 nsresult addrv = NS_OK;
1584 if (mCacheTracker) {
1585 addrv = mCacheTracker->AddObject(aEntry);
1588 if (NS_SUCCEEDED(addrv)) {
1589 queue.Push(aEntry);
1592 imgCacheTable& cache = GetCache(aRequest->IsChrome());
1593 CheckCacheLimits(cache, queue);
1595 return true;
1598 bool imgLoader::SetHasProxies(imgRequest* aRequest) {
1599 VerifyCacheSizes();
1601 const ImageCacheKey& key = aRequest->CacheKey();
1602 imgCacheTable& cache = GetCache(key);
1604 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasProxies", "uri",
1605 key.URI());
1607 RefPtr<imgCacheEntry> entry;
1608 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
1609 // Make sure the cache entry is for the right request
1610 RefPtr<imgRequest> entryRequest = entry->GetRequest();
1611 if (entryRequest == aRequest && entry->HasNoProxies()) {
1612 imgCacheQueue& queue = GetCacheQueue(key);
1613 queue.Remove(entry);
1615 if (mCacheTracker) {
1616 mCacheTracker->RemoveObject(entry);
1619 entry->SetHasNoProxies(false);
1621 return true;
1625 return false;
1628 void imgLoader::CacheEntriesChanged(bool aForChrome,
1629 int32_t aSizeDiff /* = 0 */) {
1630 imgCacheQueue& queue = GetCacheQueue(aForChrome);
1631 // We only need to dirty the queue if there is any sorting
1632 // taking place. Empty or single-entry lists can't become
1633 // dirty.
1634 if (queue.GetNumElements() > 1) {
1635 queue.MarkDirty();
1637 queue.UpdateSize(aSizeDiff);
1640 void imgLoader::CheckCacheLimits(imgCacheTable& cache, imgCacheQueue& queue) {
1641 if (queue.GetNumElements() == 0) {
1642 NS_ASSERTION(queue.GetSize() == 0,
1643 "imgLoader::CheckCacheLimits -- incorrect cache size");
1646 // Remove entries from the cache until we're back at our desired max size.
1647 while (queue.GetSize() > sCacheMaxSize) {
1648 // Remove the first entry in the queue.
1649 RefPtr<imgCacheEntry> entry(queue.Pop());
1651 NS_ASSERTION(entry, "imgLoader::CheckCacheLimits -- NULL entry pointer");
1653 if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1654 RefPtr<imgRequest> req = entry->GetRequest();
1655 if (req) {
1656 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::CheckCacheLimits",
1657 "entry", req->CacheKey().URI());
1661 if (entry) {
1662 // We just popped this entry from the queue, so pass AlreadyRemoved
1663 // to avoid searching the queue again in RemoveFromCache.
1664 RemoveFromCache(entry, QueueState::AlreadyRemoved);
1669 bool imgLoader::ValidateRequestWithNewChannel(
1670 imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1671 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1672 imgINotificationObserver* aObserver, nsISupports* aCX,
1673 Document* aLoadingDocument, uint64_t aInnerWindowId, nsLoadFlags aLoadFlags,
1674 nsContentPolicyType aLoadPolicyType, imgRequestProxy** aProxyRequest,
1675 nsIPrincipal* aTriggeringPrincipal, int32_t aCORSMode,
1676 bool* aNewChannelCreated) {
1677 // now we need to insert a new channel request object in between the real
1678 // request and the proxy that basically delays loading the image until it
1679 // gets a 304 or figures out that this needs to be a new request
1681 nsresult rv;
1683 // If we're currently in the middle of validating this request, just hand
1684 // back a proxy to it; the required work will be done for us.
1685 if (request->GetValidator()) {
1686 rv = CreateNewProxyForRequest(request, aLoadGroup, aLoadingDocument,
1687 aObserver, aLoadFlags, aProxyRequest);
1688 if (NS_FAILED(rv)) {
1689 return false;
1692 if (*aProxyRequest) {
1693 imgRequestProxy* proxy = static_cast<imgRequestProxy*>(*aProxyRequest);
1695 // We will send notifications from imgCacheValidator::OnStartRequest().
1696 // In the mean time, we must defer notifications because we are added to
1697 // the imgRequest's proxy list, and we can get extra notifications
1698 // resulting from methods such as StartDecoding(). See bug 579122.
1699 proxy->MarkValidating();
1701 // Attach the proxy without notifying
1702 request->GetValidator()->AddProxy(proxy);
1705 return NS_SUCCEEDED(rv);
1707 // We will rely on Necko to cache this request when it's possible, and to
1708 // tell imgCacheValidator::OnStartRequest whether the request came from its
1709 // cache.
1710 nsCOMPtr<nsIChannel> newChannel;
1711 bool forcePrincipalCheck;
1712 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
1713 aInitialDocumentURI, aCORSMode, aReferrerInfo,
1714 aLoadGroup, aLoadFlags, aLoadPolicyType,
1715 aTriggeringPrincipal, aCX, mRespectPrivacy);
1716 if (NS_FAILED(rv)) {
1717 return false;
1720 if (aNewChannelCreated) {
1721 *aNewChannelCreated = true;
1724 RefPtr<imgRequestProxy> req;
1725 rv = CreateNewProxyForRequest(request, aLoadGroup, aLoadingDocument,
1726 aObserver, aLoadFlags, getter_AddRefs(req));
1727 if (NS_FAILED(rv)) {
1728 return false;
1731 // Make sure that OnStatus/OnProgress calls have the right request set...
1732 RefPtr<nsProgressNotificationProxy> progressproxy =
1733 new nsProgressNotificationProxy(newChannel, req);
1734 if (!progressproxy) {
1735 return false;
1738 RefPtr<imgCacheValidator> hvc = new imgCacheValidator(
1739 progressproxy, this, request, aCX, aInnerWindowId, forcePrincipalCheck);
1741 // Casting needed here to get past multiple inheritance.
1742 nsCOMPtr<nsIStreamListener> listener =
1743 do_QueryInterface(static_cast<nsIThreadRetargetableStreamListener*>(hvc));
1744 NS_ENSURE_TRUE(listener, false);
1746 // We must set the notification callbacks before setting up the
1747 // CORS listener, because that's also interested inthe
1748 // notification callbacks.
1749 newChannel->SetNotificationCallbacks(hvc);
1751 request->SetValidator(hvc);
1753 // We will send notifications from imgCacheValidator::OnStartRequest().
1754 // In the mean time, we must defer notifications because we are added to
1755 // the imgRequest's proxy list, and we can get extra notifications
1756 // resulting from methods such as StartDecoding(). See bug 579122.
1757 req->MarkValidating();
1759 // Add the proxy without notifying
1760 hvc->AddProxy(req);
1762 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
1763 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
1764 aLoadGroup);
1765 rv = newChannel->AsyncOpen(listener);
1766 if (NS_WARN_IF(NS_FAILED(rv))) {
1767 req->CancelAndForgetObserver(rv);
1768 return false;
1771 req.forget(aProxyRequest);
1772 return true;
1775 bool imgLoader::ValidateEntry(
1776 imgCacheEntry* aEntry, nsIURI* aURI, nsIURI* aInitialDocumentURI,
1777 nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
1778 imgINotificationObserver* aObserver, nsISupports* aCX,
1779 Document* aLoadingDocument, nsLoadFlags aLoadFlags,
1780 nsContentPolicyType aLoadPolicyType, bool aCanMakeNewChannel,
1781 bool* aNewChannelCreated, imgRequestProxy** aProxyRequest,
1782 nsIPrincipal* aTriggeringPrincipal, int32_t aCORSMode) {
1783 LOG_SCOPE(gImgLog, "imgLoader::ValidateEntry");
1785 // If the expiration time is zero, then the request has not gotten far enough
1786 // to know when it will expire.
1787 uint32_t expiryTime = aEntry->GetExpiryTime();
1788 bool hasExpired = expiryTime != 0 &&
1789 expiryTime <= imgCacheEntry::SecondsFromPRTime(PR_Now());
1791 nsresult rv;
1793 // Special treatment for file URLs - aEntry has expired if file has changed
1794 nsCOMPtr<nsIFileURL> fileUrl(do_QueryInterface(aURI));
1795 if (fileUrl) {
1796 uint32_t lastModTime = aEntry->GetLoadTime();
1798 nsCOMPtr<nsIFile> theFile;
1799 rv = fileUrl->GetFile(getter_AddRefs(theFile));
1800 if (NS_SUCCEEDED(rv)) {
1801 PRTime fileLastMod;
1802 rv = theFile->GetLastModifiedTime(&fileLastMod);
1803 if (NS_SUCCEEDED(rv)) {
1804 // nsIFile uses millisec, NSPR usec
1805 fileLastMod *= 1000;
1806 hasExpired =
1807 imgCacheEntry::SecondsFromPRTime((PRTime)fileLastMod) > lastModTime;
1812 RefPtr<imgRequest> request(aEntry->GetRequest());
1814 if (!request) {
1815 return false;
1818 if (!ValidateSecurityInfo(request, aEntry->ForcePrincipalCheck(), aCORSMode,
1819 aTriggeringPrincipal, aCX, aLoadPolicyType,
1820 aReferrerInfo))
1821 return false;
1823 // data URIs are immutable and by their nature can't leak data, so we can
1824 // just return true in that case. Doing so would mean that shift-reload
1825 // doesn't reload data URI documents/images though (which is handy for
1826 // debugging during gecko development) so we make an exception in that case.
1827 nsAutoCString scheme;
1828 aURI->GetScheme(scheme);
1829 if (scheme.EqualsLiteral("data") &&
1830 !(aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE)) {
1831 return true;
1834 bool validateRequest = false;
1836 // If the request's loadId is the same as the aCX, then it is ok to use
1837 // this one because it has already been validated for this context.
1839 // XXX: nullptr seems to be a 'special' key value that indicates that NO
1840 // validation is required.
1841 // XXX: we also check the window ID because the loadID() can return a reused
1842 // pointer of a document. This can still happen for non-document image
1843 // cache entries.
1844 void* key = (void*)aCX;
1845 nsCOMPtr<Document> doc = do_QueryInterface(aCX);
1846 uint64_t innerWindowID = doc ? doc->InnerWindowID() : 0;
1847 if (request->LoadId() != key || request->InnerWindowID() != innerWindowID) {
1848 // If we would need to revalidate this entry, but we're being told to
1849 // bypass the cache, we don't allow this entry to be used.
1850 if (aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE) {
1851 return false;
1854 if (MOZ_UNLIKELY(ChaosMode::isActive(ChaosFeature::ImageCache))) {
1855 if (ChaosMode::randomUint32LessThan(4) < 1) {
1856 return false;
1860 // Determine whether the cache aEntry must be revalidated...
1861 validateRequest = ShouldRevalidateEntry(aEntry, aLoadFlags, hasExpired);
1863 MOZ_LOG(gImgLog, LogLevel::Debug,
1864 ("imgLoader::ValidateEntry validating cache entry. "
1865 "validateRequest = %d",
1866 validateRequest));
1867 } else if (!key && MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
1868 MOZ_LOG(gImgLog, LogLevel::Debug,
1869 ("imgLoader::ValidateEntry BYPASSING cache validation for %s "
1870 "because of NULL LoadID",
1871 aURI->GetSpecOrDefault().get()));
1874 // We can't use a cached request if it comes from a different
1875 // application cache than this load is expecting.
1876 nsCOMPtr<nsIApplicationCacheContainer> appCacheContainer;
1877 nsCOMPtr<nsIApplicationCache> requestAppCache;
1878 nsCOMPtr<nsIApplicationCache> groupAppCache;
1879 if ((appCacheContainer = do_GetInterface(request->GetRequest()))) {
1880 appCacheContainer->GetApplicationCache(getter_AddRefs(requestAppCache));
1882 if ((appCacheContainer = do_QueryInterface(aLoadGroup))) {
1883 appCacheContainer->GetApplicationCache(getter_AddRefs(groupAppCache));
1886 if (requestAppCache != groupAppCache) {
1887 MOZ_LOG(gImgLog, LogLevel::Debug,
1888 ("imgLoader::ValidateEntry - Unable to use cached imgRequest "
1889 "[request=%p] because of mismatched application caches\n",
1890 address_of(request)));
1891 return false;
1894 if (validateRequest && aCanMakeNewChannel) {
1895 LOG_SCOPE(gImgLog, "imgLoader::ValidateRequest |cache hit| must validate");
1897 return ValidateRequestWithNewChannel(
1898 request, aURI, aInitialDocumentURI, aReferrerInfo, aLoadGroup,
1899 aObserver, aCX, aLoadingDocument, innerWindowID, aLoadFlags,
1900 aLoadPolicyType, aProxyRequest, aTriggeringPrincipal, aCORSMode,
1901 aNewChannelCreated);
1904 return !validateRequest;
1907 bool imgLoader::RemoveFromCache(const ImageCacheKey& aKey) {
1908 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache", "uri",
1909 aKey.URI());
1911 imgCacheTable& cache = GetCache(aKey);
1912 imgCacheQueue& queue = GetCacheQueue(aKey);
1914 RefPtr<imgCacheEntry> entry;
1915 cache.Remove(aKey, getter_AddRefs(entry));
1916 if (entry) {
1917 MOZ_ASSERT(!entry->Evicted(), "Evicting an already-evicted cache entry!");
1919 // Entries with no proxies are in the tracker.
1920 if (entry->HasNoProxies()) {
1921 if (mCacheTracker) {
1922 mCacheTracker->RemoveObject(entry);
1924 queue.Remove(entry);
1927 entry->SetEvicted(true);
1929 RefPtr<imgRequest> request = entry->GetRequest();
1930 request->SetIsInCache(false);
1931 AddToUncachedImages(request);
1933 return true;
1935 return false;
1938 bool imgLoader::RemoveFromCache(imgCacheEntry* entry, QueueState aQueueState) {
1939 LOG_STATIC_FUNC(gImgLog, "imgLoader::RemoveFromCache entry");
1941 RefPtr<imgRequest> request = entry->GetRequest();
1942 if (request) {
1943 const ImageCacheKey& key = request->CacheKey();
1944 imgCacheTable& cache = GetCache(key);
1945 imgCacheQueue& queue = GetCacheQueue(key);
1947 LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache",
1948 "entry's uri", key.URI());
1950 cache.Remove(key);
1952 if (entry->HasNoProxies()) {
1953 LOG_STATIC_FUNC(gImgLog,
1954 "imgLoader::RemoveFromCache removing from tracker");
1955 if (mCacheTracker) {
1956 mCacheTracker->RemoveObject(entry);
1958 // Only search the queue to remove the entry if its possible it might
1959 // be in the queue. If we know its not in the queue this would be
1960 // wasted work.
1961 MOZ_ASSERT_IF(aQueueState == QueueState::AlreadyRemoved,
1962 !queue.Contains(entry));
1963 if (aQueueState == QueueState::MaybeExists) {
1964 queue.Remove(entry);
1968 entry->SetEvicted(true);
1969 request->SetIsInCache(false);
1970 AddToUncachedImages(request);
1972 return true;
1975 return false;
1978 nsresult imgLoader::EvictEntries(imgCacheTable& aCacheToClear) {
1979 LOG_STATIC_FUNC(gImgLog, "imgLoader::EvictEntries table");
1981 // We have to make a temporary, since RemoveFromCache removes the element
1982 // from the queue, invalidating iterators.
1983 nsTArray<RefPtr<imgCacheEntry> > entries;
1984 for (auto iter = aCacheToClear.Iter(); !iter.Done(); iter.Next()) {
1985 RefPtr<imgCacheEntry>& data = iter.Data();
1986 entries.AppendElement(data);
1989 for (uint32_t i = 0; i < entries.Length(); ++i) {
1990 if (!RemoveFromCache(entries[i])) {
1991 return NS_ERROR_FAILURE;
1995 MOZ_ASSERT(aCacheToClear.Count() == 0);
1997 return NS_OK;
2000 nsresult imgLoader::EvictEntries(imgCacheQueue& aQueueToClear) {
2001 LOG_STATIC_FUNC(gImgLog, "imgLoader::EvictEntries queue");
2003 // We have to make a temporary, since RemoveFromCache removes the element
2004 // from the queue, invalidating iterators.
2005 nsTArray<RefPtr<imgCacheEntry> > entries(aQueueToClear.GetNumElements());
2006 for (auto i = aQueueToClear.begin(); i != aQueueToClear.end(); ++i) {
2007 entries.AppendElement(*i);
2010 // Iterate in reverse order to minimize array copying.
2011 for (auto& entry : entries) {
2012 if (!RemoveFromCache(entry)) {
2013 return NS_ERROR_FAILURE;
2017 MOZ_ASSERT(aQueueToClear.GetNumElements() == 0);
2019 return NS_OK;
2022 void imgLoader::AddToUncachedImages(imgRequest* aRequest) {
2023 MutexAutoLock lock(mUncachedImagesMutex);
2024 mUncachedImages.PutEntry(aRequest);
2027 void imgLoader::RemoveFromUncachedImages(imgRequest* aRequest) {
2028 MutexAutoLock lock(mUncachedImagesMutex);
2029 mUncachedImages.RemoveEntry(aRequest);
2032 bool imgLoader::PreferLoadFromCache(nsIURI* aURI) const {
2033 // If we are trying to load an image from a protocol that doesn't support
2034 // caching (e.g. thumbnails via the moz-page-thumb:// protocol, or icons via
2035 // the moz-extension:// protocol), load it directly from the cache to prevent
2036 // re-decoding the image. See Bug 1373258.
2037 // TODO: Bug 1406134
2038 return aURI->SchemeIs("moz-page-thumb") || aURI->SchemeIs("moz-extension");
2041 #define LOAD_FLAGS_CACHE_MASK \
2042 (nsIRequest::LOAD_BYPASS_CACHE | nsIRequest::LOAD_FROM_CACHE)
2044 #define LOAD_FLAGS_VALIDATE_MASK \
2045 (nsIRequest::VALIDATE_ALWAYS | nsIRequest::VALIDATE_NEVER | \
2046 nsIRequest::VALIDATE_ONCE_PER_SESSION)
2048 NS_IMETHODIMP
2049 imgLoader::LoadImageXPCOM(nsIURI* aURI, nsIURI* aInitialDocumentURI,
2050 nsIReferrerInfo* aReferrerInfo,
2051 nsIPrincipal* aTriggeringPrincipal,
2052 nsILoadGroup* aLoadGroup,
2053 imgINotificationObserver* aObserver, nsISupports* aCX,
2054 nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
2055 nsContentPolicyType aContentPolicyType,
2056 imgIRequest** _retval) {
2057 // Optional parameter, so defaults to 0 (== TYPE_INVALID)
2058 if (!aContentPolicyType) {
2059 aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE;
2061 imgRequestProxy* proxy;
2062 nsCOMPtr<nsINode> node = do_QueryInterface(aCX);
2063 nsCOMPtr<Document> doc = do_QueryInterface(aCX);
2064 nsresult rv =
2065 LoadImage(aURI, aInitialDocumentURI, aReferrerInfo, aTriggeringPrincipal,
2066 0, aLoadGroup, aObserver, node, doc, aLoadFlags, aCacheKey,
2067 aContentPolicyType, EmptyString(),
2068 /* aUseUrgentStartForChannel */ false, &proxy);
2069 *_retval = proxy;
2070 return rv;
2073 nsresult imgLoader::LoadImage(
2074 nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
2075 nsIPrincipal* aTriggeringPrincipal, uint64_t aRequestContextID,
2076 nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
2077 nsINode* aContext, Document* aLoadingDocument, nsLoadFlags aLoadFlags,
2078 nsISupports* aCacheKey, nsContentPolicyType aContentPolicyType,
2079 const nsAString& initiatorType, bool aUseUrgentStartForChannel,
2080 imgRequestProxy** _retval) {
2081 VerifyCacheSizes();
2083 NS_ASSERTION(aURI, "imgLoader::LoadImage -- NULL URI pointer");
2085 if (!aURI) {
2086 return NS_ERROR_NULL_POINTER;
2089 #ifdef MOZ_GECKO_PROFILER
2090 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK,
2091 aURI->GetSpecOrDefault());
2092 #endif
2094 LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::LoadImage", "aURI", aURI);
2096 *_retval = nullptr;
2098 RefPtr<imgRequest> request;
2100 nsresult rv;
2101 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2103 #ifdef DEBUG
2104 bool isPrivate = false;
2106 if (aLoadingDocument) {
2107 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadingDocument);
2108 } else if (aLoadGroup) {
2109 isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadGroup);
2111 MOZ_ASSERT(isPrivate == mRespectPrivacy);
2113 if (aLoadingDocument) {
2114 // The given load group should match that of the document if given. If
2115 // that isn't the case, then we need to add more plumbing to ensure we
2116 // block the document as well.
2117 nsCOMPtr<nsILoadGroup> docLoadGroup =
2118 aLoadingDocument->GetDocumentLoadGroup();
2119 MOZ_ASSERT(docLoadGroup == aLoadGroup);
2121 #endif
2123 // Get the default load flags from the loadgroup (if possible)...
2124 if (aLoadGroup) {
2125 aLoadGroup->GetLoadFlags(&requestFlags);
2126 if (PreferLoadFromCache(aURI)) {
2127 requestFlags |= nsIRequest::LOAD_FROM_CACHE;
2131 // Merge the default load flags with those passed in via aLoadFlags.
2132 // Currently, *only* the caching, validation and background load flags
2133 // are merged...
2135 // The flags in aLoadFlags take precedence over the default flags!
2137 if (aLoadFlags & LOAD_FLAGS_CACHE_MASK) {
2138 // Override the default caching flags...
2139 requestFlags = (requestFlags & ~LOAD_FLAGS_CACHE_MASK) |
2140 (aLoadFlags & LOAD_FLAGS_CACHE_MASK);
2142 if (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK) {
2143 // Override the default validation flags...
2144 requestFlags = (requestFlags & ~LOAD_FLAGS_VALIDATE_MASK) |
2145 (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK);
2147 if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
2148 // Propagate background loading...
2149 requestFlags |= nsIRequest::LOAD_BACKGROUND;
2152 int32_t corsmode = imgIRequest::CORS_NONE;
2153 if (aLoadFlags & imgILoader::LOAD_CORS_ANONYMOUS) {
2154 corsmode = imgIRequest::CORS_ANONYMOUS;
2155 } else if (aLoadFlags & imgILoader::LOAD_CORS_USE_CREDENTIALS) {
2156 corsmode = imgIRequest::CORS_USE_CREDENTIALS;
2159 RefPtr<imgCacheEntry> entry;
2161 // Look in the cache for our URI, and then validate it.
2162 // XXX For now ignore aCacheKey. We will need it in the future
2163 // for correctly dealing with image load requests that are a result
2164 // of post data.
2165 OriginAttributes attrs;
2166 if (aTriggeringPrincipal) {
2167 attrs = aTriggeringPrincipal->OriginAttributesRef();
2169 ImageCacheKey key(aURI, attrs, aLoadingDocument);
2170 imgCacheTable& cache = GetCache(key);
2172 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
2173 bool newChannelCreated = false;
2174 if (ValidateEntry(entry, aURI, aInitialDocumentURI, aReferrerInfo,
2175 aLoadGroup, aObserver, ToSupports(aLoadingDocument),
2176 aLoadingDocument, requestFlags, aContentPolicyType, true,
2177 &newChannelCreated, _retval, aTriggeringPrincipal,
2178 corsmode)) {
2179 request = entry->GetRequest();
2181 // If this entry has no proxies, its request has no reference to the
2182 // entry.
2183 if (entry->HasNoProxies()) {
2184 LOG_FUNC_WITH_PARAM(gImgLog,
2185 "imgLoader::LoadImage() adding proxyless entry",
2186 "uri", key.URI());
2187 MOZ_ASSERT(!request->HasCacheEntry(),
2188 "Proxyless entry's request has cache entry!");
2189 request->SetCacheEntry(entry);
2191 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2192 mCacheTracker->MarkUsed(entry);
2196 entry->Touch();
2198 if (!newChannelCreated) {
2199 // This is ugly but it's needed to report CSP violations. We have 3
2200 // scenarios:
2201 // - we don't have cache. We are not in this if() stmt. A new channel is
2202 // created and that triggers the CSP checks.
2203 // - We have a cache entry and this is blocked by CSP directives.
2204 DebugOnly<bool> shouldLoad =
2205 ShouldLoadCachedImage(request, ToSupports(aLoadingDocument),
2206 aTriggeringPrincipal, aContentPolicyType,
2207 /* aSendCSPViolationReports */ true);
2208 MOZ_ASSERT(shouldLoad);
2210 } else {
2211 // We can't use this entry. We'll try to load it off the network, and if
2212 // successful, overwrite the old entry in the cache with a new one.
2213 entry = nullptr;
2217 // Keep the channel in this scope, so we can adjust its notificationCallbacks
2218 // later when we create the proxy.
2219 nsCOMPtr<nsIChannel> newChannel;
2220 // If we didn't get a cache hit, we need to load from the network.
2221 if (!request) {
2222 LOG_SCOPE(gImgLog, "imgLoader::LoadImage |cache miss|");
2224 bool forcePrincipalCheck;
2225 rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
2226 aInitialDocumentURI, corsmode, aReferrerInfo,
2227 aLoadGroup, requestFlags, aContentPolicyType,
2228 aTriggeringPrincipal, aContext, mRespectPrivacy);
2229 if (NS_FAILED(rv)) {
2230 return NS_ERROR_FAILURE;
2233 MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel) == mRespectPrivacy);
2235 NewRequestAndEntry(forcePrincipalCheck, this, key, getter_AddRefs(request),
2236 getter_AddRefs(entry));
2238 MOZ_LOG(gImgLog, LogLevel::Debug,
2239 ("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
2240 " [request=%p]\n",
2241 this, request.get()));
2243 nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(newChannel));
2244 if (cos) {
2245 if (aUseUrgentStartForChannel) {
2246 cos->AddClassFlags(nsIClassOfService::UrgentStart);
2249 if (StaticPrefs::network_http_tailing_enabled() &&
2250 aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
2251 cos->AddClassFlags(nsIClassOfService::Throttleable |
2252 nsIClassOfService::Tail);
2253 nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(newChannel));
2254 if (httpChannel) {
2255 Unused << httpChannel->SetRequestContextID(aRequestContextID);
2260 nsCOMPtr<nsILoadGroup> channelLoadGroup;
2261 newChannel->GetLoadGroup(getter_AddRefs(channelLoadGroup));
2262 rv = request->Init(aURI, aURI, /* aHadInsecureRedirect = */ false,
2263 channelLoadGroup, newChannel, entry,
2264 ToSupports(aLoadingDocument), aTriggeringPrincipal,
2265 corsmode, aReferrerInfo);
2266 if (NS_FAILED(rv)) {
2267 return NS_ERROR_FAILURE;
2270 // Add the initiator type for this image load
2271 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(newChannel);
2272 if (timedChannel) {
2273 timedChannel->SetInitiatorType(initiatorType);
2276 // create the proxy listener
2277 nsCOMPtr<nsIStreamListener> listener = new ProxyListener(request.get());
2279 MOZ_LOG(gImgLog, LogLevel::Debug,
2280 ("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
2281 this));
2283 mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
2284 nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
2285 aLoadGroup);
2287 nsresult openRes = newChannel->AsyncOpen(listener);
2289 if (NS_FAILED(openRes)) {
2290 MOZ_LOG(
2291 gImgLog, LogLevel::Debug,
2292 ("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
2293 "\n",
2294 this, static_cast<uint32_t>(openRes)));
2295 request->CancelAndAbort(openRes);
2296 return openRes;
2299 // Try to add the new request into the cache.
2300 PutIntoCache(key, entry);
2301 } else {
2302 LOG_MSG_WITH_PARAM(gImgLog, "imgLoader::LoadImage |cache hit|", "request",
2303 request);
2306 // If we didn't get a proxy when validating the cache entry, we need to
2307 // create one.
2308 if (!*_retval) {
2309 // ValidateEntry() has three return values: "Is valid," "might be valid --
2310 // validating over network", and "not valid." If we don't have a _retval,
2311 // we know ValidateEntry is not validating over the network, so it's safe
2312 // to SetLoadId here because we know this request is valid for this context.
2314 // Note, however, that this doesn't guarantee the behaviour we want (one
2315 // URL maps to the same image on a page) if we load the same image in a
2316 // different tab (see bug 528003), because its load id will get re-set, and
2317 // that'll cause us to validate over the network.
2318 request->SetLoadId(aLoadingDocument);
2320 LOG_MSG(gImgLog, "imgLoader::LoadImage", "creating proxy request.");
2321 rv = CreateNewProxyForRequest(request, aLoadGroup, aLoadingDocument,
2322 aObserver, requestFlags, _retval);
2323 if (NS_FAILED(rv)) {
2324 return rv;
2327 imgRequestProxy* proxy = *_retval;
2329 // Make sure that OnStatus/OnProgress calls have the right request set, if
2330 // we did create a channel here.
2331 if (newChannel) {
2332 nsCOMPtr<nsIInterfaceRequestor> requestor(
2333 new nsProgressNotificationProxy(newChannel, proxy));
2334 if (!requestor) {
2335 return NS_ERROR_OUT_OF_MEMORY;
2337 newChannel->SetNotificationCallbacks(requestor);
2340 // Note that it's OK to add here even if the request is done. If it is,
2341 // it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
2342 // the proxy will be removed from the loadgroup.
2343 proxy->AddToLoadGroup();
2345 // If we're loading off the network, explicitly don't notify our proxy,
2346 // because necko (or things called from necko, such as imgCacheValidator)
2347 // are going to call our notifications asynchronously, and we can't make it
2348 // further asynchronous because observers might rely on imagelib completing
2349 // its work between the channel's OnStartRequest and OnStopRequest.
2350 if (!newChannel) {
2351 proxy->NotifyListener();
2354 return rv;
2357 NS_ASSERTION(*_retval, "imgLoader::LoadImage -- no return value");
2359 return NS_OK;
2362 NS_IMETHODIMP
2363 imgLoader::LoadImageWithChannelXPCOM(nsIChannel* channel,
2364 imgINotificationObserver* aObserver,
2365 nsISupports* aCX,
2366 nsIStreamListener** listener,
2367 imgIRequest** _retval) {
2368 nsresult result;
2369 imgRequestProxy* proxy;
2370 result = LoadImageWithChannel(channel, aObserver, aCX, listener, &proxy);
2371 *_retval = proxy;
2372 return result;
2375 nsresult imgLoader::LoadImageWithChannel(nsIChannel* channel,
2376 imgINotificationObserver* aObserver,
2377 nsISupports* aCX,
2378 nsIStreamListener** listener,
2379 imgRequestProxy** _retval) {
2380 NS_ASSERTION(channel,
2381 "imgLoader::LoadImageWithChannel -- NULL channel pointer");
2383 MOZ_ASSERT(NS_UsePrivateBrowsing(channel) == mRespectPrivacy);
2385 LOG_SCOPE(gImgLog, "imgLoader::LoadImageWithChannel");
2386 RefPtr<imgRequest> request;
2388 nsCOMPtr<nsIURI> uri;
2389 channel->GetURI(getter_AddRefs(uri));
2390 nsCOMPtr<Document> doc = do_QueryInterface(aCX);
2392 NS_ENSURE_TRUE(channel, NS_ERROR_FAILURE);
2393 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2395 OriginAttributes attrs = loadInfo->GetOriginAttributes();
2397 ImageCacheKey key(uri, attrs, doc);
2399 nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
2400 channel->GetLoadFlags(&requestFlags);
2402 if (PreferLoadFromCache(uri)) {
2403 requestFlags |= nsIRequest::LOAD_FROM_CACHE;
2406 RefPtr<imgCacheEntry> entry;
2408 if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
2409 RemoveFromCache(key);
2410 } else {
2411 // Look in the cache for our URI, and then validate it.
2412 // XXX For now ignore aCacheKey. We will need it in the future
2413 // for correctly dealing with image load requests that are a result
2414 // of post data.
2415 imgCacheTable& cache = GetCache(key);
2416 if (cache.Get(key, getter_AddRefs(entry)) && entry) {
2417 // We don't want to kick off another network load. So we ask
2418 // ValidateEntry to only do validation without creating a new proxy. If
2419 // it says that the entry isn't valid any more, we'll only use the entry
2420 // we're getting if the channel is loading from the cache anyways.
2422 // XXX -- should this be changed? it's pretty much verbatim from the old
2423 // code, but seems nonsensical.
2425 // Since aCanMakeNewChannel == false, we don't need to pass content policy
2426 // type/principal/etc
2428 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2429 // if there is a loadInfo, use the right contentType, otherwise
2430 // default to the internal image type
2431 nsContentPolicyType policyType = loadInfo->InternalContentPolicyType();
2433 if (ValidateEntry(entry, uri, nullptr, nullptr, nullptr, aObserver, aCX,
2434 doc, requestFlags, policyType, false, nullptr, nullptr,
2435 nullptr, imgIRequest::CORS_NONE)) {
2436 request = entry->GetRequest();
2437 } else {
2438 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(channel));
2439 bool bUseCacheCopy;
2441 if (cacheChan) {
2442 cacheChan->IsFromCache(&bUseCacheCopy);
2443 } else {
2444 bUseCacheCopy = false;
2447 if (!bUseCacheCopy) {
2448 entry = nullptr;
2449 } else {
2450 request = entry->GetRequest();
2454 if (request && entry) {
2455 // If this entry has no proxies, its request has no reference to
2456 // the entry.
2457 if (entry->HasNoProxies()) {
2458 LOG_FUNC_WITH_PARAM(
2459 gImgLog,
2460 "imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
2461 key.URI());
2462 MOZ_ASSERT(!request->HasCacheEntry(),
2463 "Proxyless entry's request has cache entry!");
2464 request->SetCacheEntry(entry);
2466 if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
2467 mCacheTracker->MarkUsed(entry);
2474 nsCOMPtr<nsILoadGroup> loadGroup;
2475 channel->GetLoadGroup(getter_AddRefs(loadGroup));
2477 #ifdef DEBUG
2478 if (doc) {
2479 // The load group of the channel should always match that of the
2480 // document if given. If that isn't the case, then we need to add more
2481 // plumbing to ensure we block the document as well.
2482 nsCOMPtr<nsILoadGroup> docLoadGroup = doc->GetDocumentLoadGroup();
2483 MOZ_ASSERT(docLoadGroup == loadGroup);
2485 #endif
2487 // Filter out any load flags not from nsIRequest
2488 requestFlags &= nsIRequest::LOAD_REQUESTMASK;
2490 nsresult rv = NS_OK;
2491 if (request) {
2492 // we have this in our cache already.. cancel the current (document) load
2494 // this should fire an OnStopRequest
2495 channel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
2497 *listener = nullptr; // give them back a null nsIStreamListener
2499 rv = CreateNewProxyForRequest(request, loadGroup, doc, aObserver,
2500 requestFlags, _retval);
2501 static_cast<imgRequestProxy*>(*_retval)->NotifyListener();
2502 } else {
2503 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2504 nsCOMPtr<nsIURI> originalURI;
2505 channel->GetOriginalURI(getter_AddRefs(originalURI));
2507 // XXX(seth): We should be able to just use |key| here, except that |key| is
2508 // constructed above with the *current URI* and not the *original URI*. I'm
2509 // pretty sure this is a bug, and it's preventing us from ever getting a
2510 // cache hit in LoadImageWithChannel when redirects are involved.
2511 ImageCacheKey originalURIKey(originalURI, attrs, doc);
2513 // Default to doing a principal check because we don't know who
2514 // started that load and whether their principal ended up being
2515 // inherited on the channel.
2516 NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
2517 originalURIKey, getter_AddRefs(request),
2518 getter_AddRefs(entry));
2520 // No principal specified here, because we're not passed one.
2521 // In LoadImageWithChannel, the redirects that may have been
2522 // associated with this load would have gone through necko.
2523 // We only have the final URI in ImageLib and hence don't know
2524 // if the request went through insecure redirects. But if it did,
2525 // the necko cache should have handled that (since all necko cache hits
2526 // including the redirects will go through content policy). Hence, we
2527 // can set aHadInsecureRedirect to false here.
2528 rv = request->Init(originalURI, uri, /* aHadInsecureRedirect = */ false,
2529 channel, channel, entry, aCX, nullptr,
2530 imgIRequest::CORS_NONE, nullptr);
2531 NS_ENSURE_SUCCESS(rv, rv);
2533 RefPtr<ProxyListener> pl =
2534 new ProxyListener(static_cast<nsIStreamListener*>(request.get()));
2535 pl.forget(listener);
2537 // Try to add the new request into the cache.
2538 PutIntoCache(originalURIKey, entry);
2540 rv = CreateNewProxyForRequest(request, loadGroup, doc, aObserver,
2541 requestFlags, _retval);
2543 // Explicitly don't notify our proxy, because we're loading off the
2544 // network, and necko (or things called from necko, such as
2545 // imgCacheValidator) are going to call our notifications asynchronously,
2546 // and we can't make it further asynchronous because observers might rely
2547 // on imagelib completing its work between the channel's OnStartRequest and
2548 // OnStopRequest.
2551 if (NS_FAILED(rv)) {
2552 return rv;
2555 (*_retval)->AddToLoadGroup();
2556 return rv;
2559 bool imgLoader::SupportImageWithMimeType(const char* aMimeType,
2560 AcceptedMimeTypes aAccept
2561 /* = AcceptedMimeTypes::IMAGES */) {
2562 nsAutoCString mimeType(aMimeType);
2563 ToLowerCase(mimeType);
2565 if (aAccept == AcceptedMimeTypes::IMAGES_AND_DOCUMENTS &&
2566 mimeType.EqualsLiteral("image/svg+xml")) {
2567 return true;
2570 DecoderType type = DecoderFactory::GetDecoderType(mimeType.get());
2571 return type != DecoderType::UNKNOWN;
2574 NS_IMETHODIMP
2575 imgLoader::GetMIMETypeFromContent(nsIRequest* aRequest,
2576 const uint8_t* aContents, uint32_t aLength,
2577 nsACString& aContentType) {
2578 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2579 if (channel) {
2580 nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
2581 if (loadInfo->GetSkipContentSniffing()) {
2582 return NS_ERROR_NOT_AVAILABLE;
2585 return GetMimeTypeFromContent((const char*)aContents, aLength, aContentType);
2588 /* static */
2589 nsresult imgLoader::GetMimeTypeFromContent(const char* aContents,
2590 uint32_t aLength,
2591 nsACString& aContentType) {
2592 /* Is it a GIF? */
2593 if (aLength >= 6 &&
2594 (!strncmp(aContents, "GIF87a", 6) || !strncmp(aContents, "GIF89a", 6))) {
2595 aContentType.AssignLiteral(IMAGE_GIF);
2597 /* or a PNG? */
2598 } else if (aLength >= 8 && ((unsigned char)aContents[0] == 0x89 &&
2599 (unsigned char)aContents[1] == 0x50 &&
2600 (unsigned char)aContents[2] == 0x4E &&
2601 (unsigned char)aContents[3] == 0x47 &&
2602 (unsigned char)aContents[4] == 0x0D &&
2603 (unsigned char)aContents[5] == 0x0A &&
2604 (unsigned char)aContents[6] == 0x1A &&
2605 (unsigned char)aContents[7] == 0x0A)) {
2606 aContentType.AssignLiteral(IMAGE_PNG);
2608 /* maybe a JPEG (JFIF)? */
2609 /* JFIF files start with SOI APP0 but older files can start with SOI DQT
2610 * so we test for SOI followed by any marker, i.e. FF D8 FF
2611 * this will also work for SPIFF JPEG files if they appear in the future.
2613 * (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
2615 } else if (aLength >= 3 && ((unsigned char)aContents[0]) == 0xFF &&
2616 ((unsigned char)aContents[1]) == 0xD8 &&
2617 ((unsigned char)aContents[2]) == 0xFF) {
2618 aContentType.AssignLiteral(IMAGE_JPEG);
2620 /* or how about ART? */
2621 /* ART begins with JG (4A 47). Major version offset 2.
2622 * Minor version offset 3. Offset 4 must be nullptr.
2624 } else if (aLength >= 5 && ((unsigned char)aContents[0]) == 0x4a &&
2625 ((unsigned char)aContents[1]) == 0x47 &&
2626 ((unsigned char)aContents[4]) == 0x00) {
2627 aContentType.AssignLiteral(IMAGE_ART);
2629 } else if (aLength >= 2 && !strncmp(aContents, "BM", 2)) {
2630 aContentType.AssignLiteral(IMAGE_BMP);
2632 // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
2633 // CURs begin with 2-byte 0 followed by 2-byte 2.
2634 } else if (aLength >= 4 && (!memcmp(aContents, "\000\000\001\000", 4) ||
2635 !memcmp(aContents, "\000\000\002\000", 4))) {
2636 aContentType.AssignLiteral(IMAGE_ICO);
2638 // WebPs always begin with RIFF, a 32-bit length, and WEBP.
2639 } else if (aLength >= 12 && !memcmp(aContents, "RIFF", 4) &&
2640 !memcmp(aContents + 8, "WEBP", 4)) {
2641 aContentType.AssignLiteral(IMAGE_WEBP);
2643 } else {
2644 /* none of the above? I give up */
2645 return NS_ERROR_NOT_AVAILABLE;
2648 return NS_OK;
2652 * proxy stream listener class used to handle multipart/x-mixed-replace
2655 #include "nsIRequest.h"
2656 #include "nsIStreamConverterService.h"
2658 NS_IMPL_ISUPPORTS(ProxyListener, nsIStreamListener,
2659 nsIThreadRetargetableStreamListener, nsIRequestObserver)
2661 ProxyListener::ProxyListener(nsIStreamListener* dest) : mDestListener(dest) {
2662 /* member initializers and constructor code */
2665 ProxyListener::~ProxyListener() { /* destructor code */
2668 /** nsIRequestObserver methods **/
2670 NS_IMETHODIMP
2671 ProxyListener::OnStartRequest(nsIRequest* aRequest) {
2672 if (!mDestListener) {
2673 return NS_ERROR_FAILURE;
2676 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2677 if (channel) {
2678 // We need to set the initiator type for the image load
2679 nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(channel);
2680 if (timedChannel) {
2681 nsAutoString type;
2682 timedChannel->GetInitiatorType(type);
2683 if (type.IsEmpty()) {
2684 timedChannel->SetInitiatorType(NS_LITERAL_STRING("img"));
2688 nsAutoCString contentType;
2689 nsresult rv = channel->GetContentType(contentType);
2691 if (!contentType.IsEmpty()) {
2692 /* If multipart/x-mixed-replace content, we'll insert a MIME decoder
2693 in the pipeline to handle the content and pass it along to our
2694 original listener.
2696 if (NS_LITERAL_CSTRING("multipart/x-mixed-replace").Equals(contentType)) {
2697 nsCOMPtr<nsIStreamConverterService> convServ(
2698 do_GetService("@mozilla.org/streamConverters;1", &rv));
2699 if (NS_SUCCEEDED(rv)) {
2700 nsCOMPtr<nsIStreamListener> toListener(mDestListener);
2701 nsCOMPtr<nsIStreamListener> fromListener;
2703 rv = convServ->AsyncConvertData("multipart/x-mixed-replace", "*/*",
2704 toListener, nullptr,
2705 getter_AddRefs(fromListener));
2706 if (NS_SUCCEEDED(rv)) {
2707 mDestListener = fromListener;
2714 return mDestListener->OnStartRequest(aRequest);
2717 NS_IMETHODIMP
2718 ProxyListener::OnStopRequest(nsIRequest* aRequest, nsresult status) {
2719 if (!mDestListener) {
2720 return NS_ERROR_FAILURE;
2723 return mDestListener->OnStopRequest(aRequest, status);
2726 /** nsIStreamListener methods **/
2728 NS_IMETHODIMP
2729 ProxyListener::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
2730 uint64_t sourceOffset, uint32_t count) {
2731 if (!mDestListener) {
2732 return NS_ERROR_FAILURE;
2735 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
2738 /** nsThreadRetargetableStreamListener methods **/
2739 NS_IMETHODIMP
2740 ProxyListener::CheckListenerChain() {
2741 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
2742 nsresult rv = NS_OK;
2743 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
2744 do_QueryInterface(mDestListener, &rv);
2745 if (retargetableListener) {
2746 rv = retargetableListener->CheckListenerChain();
2748 MOZ_LOG(
2749 gImgLog, LogLevel::Debug,
2750 ("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
2751 "]",
2752 (NS_SUCCEEDED(rv) ? "success" : "failure"), this,
2753 (nsIStreamListener*)mDestListener, static_cast<uint32_t>(rv)));
2754 return rv;
2758 * http validate class. check a channel for a 304
2761 NS_IMPL_ISUPPORTS(imgCacheValidator, nsIStreamListener, nsIRequestObserver,
2762 nsIThreadRetargetableStreamListener, nsIChannelEventSink,
2763 nsIInterfaceRequestor, nsIAsyncVerifyRedirectCallback)
2765 imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy* progress,
2766 imgLoader* loader, imgRequest* request,
2767 nsISupports* aContext,
2768 uint64_t aInnerWindowId,
2769 bool forcePrincipalCheckForCacheEntry)
2770 : mProgressProxy(progress),
2771 mRequest(request),
2772 mContext(aContext),
2773 mInnerWindowId(aInnerWindowId),
2774 mImgLoader(loader),
2775 mHadInsecureRedirect(false) {
2776 NewRequestAndEntry(forcePrincipalCheckForCacheEntry, loader,
2777 mRequest->CacheKey(), getter_AddRefs(mNewRequest),
2778 getter_AddRefs(mNewEntry));
2781 imgCacheValidator::~imgCacheValidator() {
2782 if (mRequest) {
2783 // If something went wrong, and we never unblocked the requests waiting on
2784 // validation, now is our last chance. We will cancel the new request and
2785 // switch the waiting proxies to it.
2786 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
2790 void imgCacheValidator::AddProxy(imgRequestProxy* aProxy) {
2791 // aProxy needs to be in the loadgroup since we're validating from
2792 // the network.
2793 aProxy->AddToLoadGroup();
2795 mProxies.AppendElement(aProxy);
2798 void imgCacheValidator::RemoveProxy(imgRequestProxy* aProxy) {
2799 mProxies.RemoveElement(aProxy);
2802 void imgCacheValidator::UpdateProxies(bool aCancelRequest, bool aSyncNotify) {
2803 MOZ_ASSERT(mRequest);
2805 // Clear the validator before updating the proxies. The notifications may
2806 // clone an existing request, and its state could be inconsistent.
2807 mRequest->SetValidator(nullptr);
2808 mRequest = nullptr;
2810 // If an error occurred, we will want to cancel the new request, and make the
2811 // validating proxies point to it. Any proxies still bound to the original
2812 // request which are not validating should remain untouched.
2813 if (aCancelRequest) {
2814 MOZ_ASSERT(mNewRequest);
2815 mNewRequest->CancelAndAbort(NS_BINDING_ABORTED);
2818 // We have finished validating the request, so we can safely take ownership
2819 // of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
2820 // if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
2821 // that any potential notifications should still be suppressed in
2822 // imgRequestProxy::ChangeOwner because we haven't cleared the validating
2823 // flag yet, and thus they will remain deferred.
2824 AutoTArray<RefPtr<imgRequestProxy>, 4> proxies(std::move(mProxies));
2826 for (auto& proxy : proxies) {
2827 // First update the state of all proxies before notifying any of them
2828 // to ensure a consistent state (e.g. in case the notification causes
2829 // other proxies to be touched indirectly.)
2830 MOZ_ASSERT(proxy->IsValidating());
2831 MOZ_ASSERT(proxy->NotificationsDeferred(),
2832 "Proxies waiting on cache validation should be "
2833 "deferring notifications!");
2834 if (mNewRequest) {
2835 proxy->ChangeOwner(mNewRequest);
2837 proxy->ClearValidating();
2840 mNewRequest = nullptr;
2841 mNewEntry = nullptr;
2843 for (auto& proxy : proxies) {
2844 if (aSyncNotify) {
2845 // Notify synchronously, because the caller knows we are already in an
2846 // asynchronously-called function (e.g. OnStartRequest).
2847 proxy->SyncNotifyListener();
2848 } else {
2849 // Notify asynchronously, because the caller does not know our current
2850 // call state (e.g. ~imgCacheValidator).
2851 proxy->NotifyListener();
2856 /** nsIRequestObserver methods **/
2858 NS_IMETHODIMP
2859 imgCacheValidator::OnStartRequest(nsIRequest* aRequest) {
2860 // We may be holding on to a document, so ensure that it's released.
2861 nsCOMPtr<nsISupports> context = mContext.forget();
2863 // If for some reason we don't still have an existing request (probably
2864 // because OnStartRequest got delivered more than once), just bail.
2865 if (!mRequest) {
2866 MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
2867 aRequest->Cancel(NS_BINDING_ABORTED);
2868 return NS_ERROR_FAILURE;
2871 // If this request is coming from cache and has the same URI as our
2872 // imgRequest, the request all our proxies are pointing at is valid, and all
2873 // we have to do is tell them to notify their listeners.
2874 nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(aRequest));
2875 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
2876 if (cacheChan && channel && !mRequest->CacheChanged(aRequest)) {
2877 bool isFromCache = false;
2878 cacheChan->IsFromCache(&isFromCache);
2880 nsCOMPtr<nsIURI> channelURI;
2881 channel->GetURI(getter_AddRefs(channelURI));
2883 nsCOMPtr<nsIURI> finalURI;
2884 mRequest->GetFinalURI(getter_AddRefs(finalURI));
2886 bool sameURI = false;
2887 if (channelURI && finalURI) {
2888 channelURI->Equals(finalURI, &sameURI);
2891 if (isFromCache && sameURI) {
2892 // We don't need to load this any more.
2893 aRequest->Cancel(NS_BINDING_ABORTED);
2894 mNewRequest = nullptr;
2896 // Clear the validator before updating the proxies. The notifications may
2897 // clone an existing request, and its state could be inconsistent.
2898 mRequest->SetLoadId(context);
2899 mRequest->SetInnerWindowID(mInnerWindowId);
2900 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
2901 return NS_OK;
2905 // We can't load out of cache. We have to create a whole new request for the
2906 // data that's coming in off the channel.
2907 nsCOMPtr<nsIURI> uri;
2908 mRequest->GetURI(getter_AddRefs(uri));
2910 LOG_MSG_WITH_PARAM(gImgLog,
2911 "imgCacheValidator::OnStartRequest creating new request",
2912 "uri", uri);
2914 int32_t corsmode = mRequest->GetCORSMode();
2915 nsCOMPtr<nsIReferrerInfo> referrerInfo = mRequest->GetReferrerInfo();
2916 nsCOMPtr<nsIPrincipal> triggeringPrincipal =
2917 mRequest->GetTriggeringPrincipal();
2919 // Doom the old request's cache entry
2920 mRequest->RemoveFromCache();
2922 // We use originalURI here to fulfil the imgIRequest contract on GetURI.
2923 nsCOMPtr<nsIURI> originalURI;
2924 channel->GetOriginalURI(getter_AddRefs(originalURI));
2925 nsresult rv = mNewRequest->Init(originalURI, uri, mHadInsecureRedirect,
2926 aRequest, channel, mNewEntry, context,
2927 triggeringPrincipal, corsmode, referrerInfo);
2928 if (NS_FAILED(rv)) {
2929 UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
2930 return rv;
2933 mDestListener = new ProxyListener(mNewRequest);
2935 // Try to add the new request into the cache. Note that the entry must be in
2936 // the cache before the proxies' ownership changes, because adding a proxy
2937 // changes the caching behaviour for imgRequests.
2938 mImgLoader->PutIntoCache(mNewRequest->CacheKey(), mNewEntry);
2939 UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
2940 return mDestListener->OnStartRequest(aRequest);
2943 NS_IMETHODIMP
2944 imgCacheValidator::OnStopRequest(nsIRequest* aRequest, nsresult status) {
2945 // Be sure we've released the document that we may have been holding on to.
2946 mContext = nullptr;
2948 if (!mDestListener) {
2949 return NS_OK;
2952 return mDestListener->OnStopRequest(aRequest, status);
2955 /** nsIStreamListener methods **/
2957 NS_IMETHODIMP
2958 imgCacheValidator::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
2959 uint64_t sourceOffset, uint32_t count) {
2960 if (!mDestListener) {
2961 // XXX see bug 113959
2962 uint32_t _retval;
2963 inStr->ReadSegments(NS_DiscardSegment, nullptr, count, &_retval);
2964 return NS_OK;
2967 return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
2970 /** nsIThreadRetargetableStreamListener methods **/
2972 NS_IMETHODIMP
2973 imgCacheValidator::CheckListenerChain() {
2974 NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
2975 nsresult rv = NS_OK;
2976 nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
2977 do_QueryInterface(mDestListener, &rv);
2978 if (retargetableListener) {
2979 rv = retargetableListener->CheckListenerChain();
2981 MOZ_LOG(
2982 gImgLog, LogLevel::Debug,
2983 ("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32 "=%s",
2984 this, static_cast<uint32_t>(rv),
2985 NS_SUCCEEDED(rv) ? "succeeded" : "failed"));
2986 return rv;
2989 /** nsIInterfaceRequestor methods **/
2991 NS_IMETHODIMP
2992 imgCacheValidator::GetInterface(const nsIID& aIID, void** aResult) {
2993 if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
2994 return QueryInterface(aIID, aResult);
2997 return mProgressProxy->GetInterface(aIID, aResult);
3000 // These functions are materially the same as the same functions in imgRequest.
3001 // We duplicate them because we're verifying whether cache loads are necessary,
3002 // not unconditionally loading.
3004 /** nsIChannelEventSink methods **/
3005 NS_IMETHODIMP
3006 imgCacheValidator::AsyncOnChannelRedirect(
3007 nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
3008 nsIAsyncVerifyRedirectCallback* callback) {
3009 // Note all cache information we get from the old channel.
3010 mNewRequest->SetCacheValidation(mNewEntry, oldChannel);
3012 // If the previous URI is a non-HTTPS URI, record that fact for later use by
3013 // security code, which needs to know whether there is an insecure load at any
3014 // point in the redirect chain.
3015 nsCOMPtr<nsIURI> oldURI;
3016 bool schemeLocal = false;
3017 if (NS_FAILED(oldChannel->GetURI(getter_AddRefs(oldURI))) ||
3018 NS_FAILED(NS_URIChainHasFlags(
3019 oldURI, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE, &schemeLocal)) ||
3020 (!oldURI->SchemeIs("https") && !oldURI->SchemeIs("chrome") &&
3021 !schemeLocal)) {
3022 mHadInsecureRedirect = true;
3025 // Prepare for callback
3026 mRedirectCallback = callback;
3027 mRedirectChannel = newChannel;
3029 return mProgressProxy->AsyncOnChannelRedirect(oldChannel, newChannel, flags,
3030 this);
3033 NS_IMETHODIMP
3034 imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult) {
3035 // If we've already been told to abort, just do so.
3036 if (NS_FAILED(aResult)) {
3037 mRedirectCallback->OnRedirectVerifyCallback(aResult);
3038 mRedirectCallback = nullptr;
3039 mRedirectChannel = nullptr;
3040 return NS_OK;
3043 // make sure we have a protocol that returns data rather than opens
3044 // an external application, e.g. mailto:
3045 nsCOMPtr<nsIURI> uri;
3046 mRedirectChannel->GetURI(getter_AddRefs(uri));
3047 bool doesNotReturnData = false;
3048 NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA,
3049 &doesNotReturnData);
3051 nsresult result = NS_OK;
3053 if (doesNotReturnData) {
3054 result = NS_ERROR_ABORT;
3057 mRedirectCallback->OnRedirectVerifyCallback(result);
3058 mRedirectCallback = nullptr;
3059 mRedirectChannel = nullptr;
3060 return NS_OK;