2013-01-18 04:45:11 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2017-03-21 21:09:13 +03:00
|
|
|
// Undefine windows version of LoadImage because our code uses that name.
|
|
|
|
#undef LoadImage
|
|
|
|
|
2019-08-30 22:58:25 +03:00
|
|
|
#include <algorithm>
|
|
|
|
|
2016-06-30 20:27:03 +03:00
|
|
|
#include "ImageLogging.h"
|
|
|
|
#include "imgLoader.h"
|
|
|
|
|
2012-01-03 00:23:41 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-10-12 16:43:01 +04:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2015-04-28 21:45:45 +03:00
|
|
|
#include "mozilla/Move.h"
|
2018-07-17 22:37:48 +03:00
|
|
|
#include "mozilla/NullPrincipal.h"
|
2015-04-28 21:45:45 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_image.h"
|
|
|
|
#include "mozilla/StaticPrefs_network.h"
|
2016-06-07 01:23:43 +03:00
|
|
|
#include "mozilla/ChaosMode.h"
|
2018-03-29 13:16:23 +03:00
|
|
|
#include "mozilla/LoadInfo.h"
|
2012-01-03 00:23:41 +04:00
|
|
|
|
2016-05-19 15:31:15 +03:00
|
|
|
#include "nsImageModule.h"
|
2010-08-14 08:09:48 +04:00
|
|
|
#include "imgRequestProxy.h"
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2001-05-08 08:12:40 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2015-03-24 19:18:48 +03:00
|
|
|
#include "nsContentPolicyUtils.h"
|
2011-07-14 22:47:34 +04:00
|
|
|
#include "nsContentUtils.h"
|
2002-09-19 02:08:12 +04:00
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsIProtocolHandler.h"
|
2013-01-09 01:40:47 +04:00
|
|
|
#include "nsMimeTypes.h"
|
2011-06-24 18:22:33 +04:00
|
|
|
#include "nsStreamUtils.h"
|
2001-05-12 01:05:08 +04:00
|
|
|
#include "nsIHttpChannel.h"
|
2015-11-12 23:09:52 +03:00
|
|
|
#include "nsICacheInfoChannel.h"
|
2017-04-25 04:17:38 +03:00
|
|
|
#include "nsIClassOfService.h"
|
2009-07-28 20:13:48 +04:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2009-07-28 20:13:48 +04:00
|
|
|
#include "nsIProgressEventSink.h"
|
2009-08-13 15:20:41 +04:00
|
|
|
#include "nsIChannelEventSink.h"
|
2010-08-05 06:15:55 +04:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
2008-09-05 03:00:42 +04:00
|
|
|
#include "nsIFileURL.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIFile.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2014-06-04 00:37:46 +04:00
|
|
|
#include "nsINetworkPredictor.h"
|
2017-11-17 22:08:52 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2016-06-07 01:23:43 +03:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2015-03-24 19:18:48 +03:00
|
|
|
#include "mozilla/dom/nsMixedContentBlocker.h"
|
2018-09-25 16:13:52 +03:00
|
|
|
#include "mozilla/image/ImageMemoryReporter.h"
|
|
|
|
#include "mozilla/layers/CompositorManagerChild.h"
|
2002-03-23 16:21:33 +03:00
|
|
|
|
2009-01-05 08:52:22 +03:00
|
|
|
#include "nsIApplicationCache.h"
|
|
|
|
#include "nsIApplicationCacheContainer.h"
|
|
|
|
|
2010-05-22 08:10:14 +04:00
|
|
|
#include "nsIMemoryReporter.h"
|
2015-07-23 08:39:48 +03:00
|
|
|
#include "DecoderFactory.h"
|
2013-09-07 17:01:08 +04:00
|
|
|
#include "Image.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "prtime.h"
|
2019-05-02 15:33:55 +03:00
|
|
|
#include "ReferrerInfo.h"
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2002-10-01 03:18:43 +04:00
|
|
|
// we want to explore making the document own the load group
|
|
|
|
// so we can associate the document URI with the load group.
|
|
|
|
// until this point, we have an evil hack:
|
2013-03-30 00:14:19 +04:00
|
|
|
#include "nsIHttpChannelInternal.h"
|
2012-06-26 08:20:12 +04:00
|
|
|
#include "nsILoadContext.h"
|
2013-04-09 21:38:48 +04:00
|
|
|
#include "nsILoadGroupChild.h"
|
2017-03-02 16:08:19 +03:00
|
|
|
#include "nsIDocShell.h"
|
2002-10-01 03:18:43 +04:00
|
|
|
|
2011-06-12 06:30:15 +04:00
|
|
|
using namespace mozilla;
|
2016-06-07 01:23:43 +03:00
|
|
|
using namespace mozilla::dom;
|
2012-01-06 20:02:27 +04:00
|
|
|
using namespace mozilla::image;
|
2014-11-18 16:46:53 +03:00
|
|
|
using namespace mozilla::net;
|
2010-08-14 08:09:49 +04:00
|
|
|
|
2013-12-08 09:38:32 +04:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(ImagesMallocSizeOf)
|
2012-03-02 04:05:32 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class imgMemoryReporter final : public nsIMemoryReporter {
|
2016-11-16 18:39:43 +03:00
|
|
|
~imgMemoryReporter() = default;
|
2014-06-23 22:49:08 +04:00
|
|
|
|
2010-05-22 08:10:14 +04:00
|
|
|
public:
|
2013-12-08 09:39:47 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
2011-06-06 05:22:45 +04:00
|
|
|
|
2014-11-27 05:00:15 +03:00
|
|
|
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsISupports* aData, bool aAnonymize) override {
|
2018-09-25 16:13:52 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
layers::CompositorManagerChild* manager =
|
|
|
|
CompositorManagerChild::GetInstance();
|
2019-06-26 06:24:21 +03:00
|
|
|
if (!manager || !StaticPrefs::image_mem_debug_reporting()) {
|
2018-09-25 16:13:52 +03:00
|
|
|
layers::SharedSurfacesMemoryReport sharedSurfaces;
|
|
|
|
FinishCollectReports(aHandleReport, aData, aAnonymize, sharedSurfaces);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<imgMemoryReporter> self(this);
|
|
|
|
nsCOMPtr<nsIHandleReportCallback> handleReport(aHandleReport);
|
|
|
|
nsCOMPtr<nsISupports> data(aData);
|
|
|
|
manager->SendReportSharedSurfacesMemory(
|
|
|
|
[=](layers::SharedSurfacesMemoryReport aReport) {
|
|
|
|
self->FinishCollectReports(handleReport, data, aAnonymize, aReport);
|
|
|
|
},
|
2018-12-11 22:22:26 +03:00
|
|
|
[=](mozilla::ipc::ResponseRejectReason&& aReason) {
|
2018-09-25 16:13:52 +03:00
|
|
|
layers::SharedSurfacesMemoryReport sharedSurfaces;
|
|
|
|
self->FinishCollectReports(handleReport, data, aAnonymize,
|
|
|
|
sharedSurfaces);
|
|
|
|
});
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FinishCollectReports(
|
|
|
|
nsIHandleReportCallback* aHandleReport, nsISupports* aData,
|
|
|
|
bool aAnonymize, layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
|
2014-11-27 05:00:15 +03:00
|
|
|
nsTArray<ImageMemoryCounter> chrome;
|
|
|
|
nsTArray<ImageMemoryCounter> content;
|
|
|
|
nsTArray<ImageMemoryCounter> uncached;
|
2012-06-26 08:20:12 +04:00
|
|
|
|
2012-09-06 18:11:28 +04:00
|
|
|
for (uint32_t i = 0; i < mKnownLoaders.Length(); i++) {
|
2015-11-11 10:07:09 +03:00
|
|
|
for (auto iter = mKnownLoaders[i]->mChromeCache.Iter(); !iter.Done();
|
|
|
|
iter.Next()) {
|
|
|
|
imgCacheEntry* entry = iter.UserData();
|
|
|
|
RefPtr<imgRequest> req = entry->GetRequest();
|
|
|
|
RecordCounterForRequest(req, &chrome, !entry->HasNoProxies());
|
|
|
|
}
|
|
|
|
for (auto iter = mKnownLoaders[i]->mCache.Iter(); !iter.Done();
|
|
|
|
iter.Next()) {
|
|
|
|
imgCacheEntry* entry = iter.UserData();
|
|
|
|
RefPtr<imgRequest> req = entry->GetRequest();
|
|
|
|
RecordCounterForRequest(req, &content, !entry->HasNoProxies());
|
|
|
|
}
|
2014-09-06 01:36:11 +04:00
|
|
|
MutexAutoLock lock(mKnownLoaders[i]->mUncachedImagesMutex);
|
2015-07-14 16:37:57 +03:00
|
|
|
for (auto iter = mKnownLoaders[i]->mUncachedImages.Iter(); !iter.Done();
|
|
|
|
iter.Next()) {
|
|
|
|
nsPtrHashKey<imgRequest>* entry = iter.Get();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> req = entry->GetKey();
|
2015-07-14 16:37:57 +03:00
|
|
|
RecordCounterForRequest(req, &uncached, req->HasConsumers());
|
|
|
|
}
|
2012-06-26 08:20:12 +04:00
|
|
|
}
|
2012-03-02 04:05:32 +04:00
|
|
|
|
2014-05-21 10:06:54 +04:00
|
|
|
// Note that we only need to anonymize content image URIs.
|
|
|
|
|
2018-09-25 16:13:52 +03:00
|
|
|
ReportCounterArray(aHandleReport, aData, chrome, "images/chrome",
|
|
|
|
/* aAnonymize */ false, aSharedSurfaces);
|
2012-03-02 04:05:32 +04:00
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
ReportCounterArray(aHandleReport, aData, content, "images/content",
|
2018-09-25 16:13:52 +03:00
|
|
|
aAnonymize, aSharedSurfaces);
|
2012-03-02 04:05:32 +04:00
|
|
|
|
2014-11-27 05:00:15 +03:00
|
|
|
// Uncached images may be content or chrome, so anonymize them.
|
2016-08-24 08:23:45 +03:00
|
|
|
ReportCounterArray(aHandleReport, aData, uncached, "images/uncached",
|
2018-09-25 16:13:52 +03:00
|
|
|
aAnonymize, aSharedSurfaces);
|
2018-09-25 13:18:07 +03:00
|
|
|
|
2018-09-25 16:13:52 +03:00
|
|
|
// Report any shared surfaces that were not merged with the surface cache.
|
|
|
|
ImageMemoryReporter::ReportSharedSurfaces(aHandleReport, aData,
|
|
|
|
aSharedSurfaces);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMemoryReporterManager> imgr =
|
|
|
|
do_GetService("@mozilla.org/memory-reporter-manager;1");
|
|
|
|
if (imgr) {
|
|
|
|
imgr->EndReport();
|
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
}
|
|
|
|
|
2013-09-20 02:52:30 +04:00
|
|
|
static int64_t ImagesContentUsedUncompressedDistinguishedAmount() {
|
2012-03-02 04:05:32 +04:00
|
|
|
size_t n = 0;
|
2015-04-08 00:49:00 +03:00
|
|
|
for (uint32_t i = 0; i < imgLoader::sMemReporter->mKnownLoaders.Length();
|
|
|
|
i++) {
|
2015-11-11 10:07:09 +03:00
|
|
|
for (auto iter = imgLoader::sMemReporter->mKnownLoaders[i]->mCache.Iter();
|
|
|
|
!iter.Done(); iter.Next()) {
|
|
|
|
imgCacheEntry* entry = iter.UserData();
|
|
|
|
if (entry->HasNoProxies()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<imgRequest> req = entry->GetRequest();
|
2017-03-21 21:09:13 +03:00
|
|
|
RefPtr<image::Image> image = req->GetImage();
|
2015-11-11 10:07:09 +03:00
|
|
|
if (!image) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both this and EntryImageSizes measure
|
|
|
|
// images/content/raster/used/decoded memory. This function's
|
|
|
|
// measurement is secondary -- the result doesn't go in the "explicit"
|
|
|
|
// tree -- so we use moz_malloc_size_of instead of ImagesMallocSizeOf to
|
|
|
|
// prevent DMD from seeing it reported twice.
|
2017-07-28 13:10:04 +03:00
|
|
|
SizeOfState state(moz_malloc_size_of);
|
|
|
|
ImageMemoryCounter counter(image, state, /* aIsUsed = */ true);
|
2015-11-11 10:07:09 +03:00
|
|
|
|
|
|
|
n += counter.Values().DecodedHeap();
|
|
|
|
n += counter.Values().DecodedNonHeap();
|
|
|
|
}
|
2012-06-26 08:20:12 +04:00
|
|
|
}
|
2012-03-02 04:05:32 +04:00
|
|
|
return n;
|
2010-05-22 08:10:14 +04:00
|
|
|
}
|
|
|
|
|
2012-06-26 08:20:12 +04:00
|
|
|
void RegisterLoader(imgLoader* aLoader) {
|
|
|
|
mKnownLoaders.AppendElement(aLoader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnregisterLoader(imgLoader* aLoader) {
|
|
|
|
mKnownLoaders.RemoveElement(aLoader);
|
|
|
|
}
|
|
|
|
|
2012-03-02 04:05:32 +04:00
|
|
|
private:
|
2012-06-26 08:20:12 +04:00
|
|
|
nsTArray<imgLoader*> mKnownLoaders;
|
|
|
|
|
2014-11-27 05:00:15 +03:00
|
|
|
struct MemoryTotal {
|
|
|
|
MemoryTotal& operator+=(const ImageMemoryCounter& aImageCounter) {
|
|
|
|
if (aImageCounter.Type() == imgIContainer::TYPE_RASTER) {
|
|
|
|
if (aImageCounter.IsUsed()) {
|
|
|
|
mUsedRasterCounter += aImageCounter.Values();
|
|
|
|
} else {
|
|
|
|
mUnusedRasterCounter += aImageCounter.Values();
|
|
|
|
}
|
|
|
|
} else if (aImageCounter.Type() == imgIContainer::TYPE_VECTOR) {
|
|
|
|
if (aImageCounter.IsUsed()) {
|
|
|
|
mUsedVectorCounter += aImageCounter.Values();
|
|
|
|
} else {
|
|
|
|
mUnusedVectorCounter += aImageCounter.Values();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unexpected image type");
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MemoryCounter& UsedRaster() const { return mUsedRasterCounter; }
|
|
|
|
const MemoryCounter& UnusedRaster() const { return mUnusedRasterCounter; }
|
|
|
|
const MemoryCounter& UsedVector() const { return mUsedVectorCounter; }
|
|
|
|
const MemoryCounter& UnusedVector() const { return mUnusedVectorCounter; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
MemoryCounter mUsedRasterCounter;
|
|
|
|
MemoryCounter mUnusedRasterCounter;
|
|
|
|
MemoryCounter mUsedVectorCounter;
|
|
|
|
MemoryCounter mUnusedVectorCounter;
|
2010-05-22 08:10:14 +04:00
|
|
|
};
|
|
|
|
|
2014-11-27 05:00:15 +03:00
|
|
|
// Reports all images of a single kind, e.g. all used chrome images.
|
2016-08-24 08:23:45 +03:00
|
|
|
void ReportCounterArray(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData,
|
|
|
|
nsTArray<ImageMemoryCounter>& aCounterArray,
|
|
|
|
const char* aPathPrefix, bool aAnonymize,
|
2018-09-25 16:13:52 +03:00
|
|
|
layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
|
2014-11-27 05:00:15 +03:00
|
|
|
MemoryTotal summaryTotal;
|
|
|
|
MemoryTotal nonNotableTotal;
|
2014-05-16 03:11:57 +04:00
|
|
|
|
|
|
|
// Report notable images, and compute total and non-notable aggregate sizes.
|
2014-11-27 05:00:15 +03:00
|
|
|
for (uint32_t i = 0; i < aCounterArray.Length(); i++) {
|
2015-04-28 21:46:17 +03:00
|
|
|
ImageMemoryCounter& counter = aCounterArray[i];
|
2014-05-21 10:06:54 +04:00
|
|
|
|
|
|
|
if (aAnonymize) {
|
2014-11-27 05:00:15 +03:00
|
|
|
counter.URI().Truncate();
|
|
|
|
counter.URI().AppendPrintf("<anonymized-%u>", i);
|
2014-05-21 10:06:54 +04:00
|
|
|
} else {
|
2014-11-27 05:00:15 +03:00
|
|
|
// The URI could be an extremely long data: URI. Truncate if needed.
|
2014-05-21 10:06:54 +04:00
|
|
|
static const size_t max = 256;
|
2014-11-27 05:00:15 +03:00
|
|
|
if (counter.URI().Length() > max) {
|
|
|
|
counter.URI().Truncate(max);
|
|
|
|
counter.URI().AppendLiteral(" (truncated)");
|
2014-05-21 10:06:54 +04:00
|
|
|
}
|
2014-11-27 05:00:15 +03:00
|
|
|
counter.URI().ReplaceChar('/', '\\');
|
2014-05-16 03:11:57 +04:00
|
|
|
}
|
|
|
|
|
2014-11-27 05:00:15 +03:00
|
|
|
summaryTotal += counter;
|
2014-05-16 03:11:57 +04:00
|
|
|
|
2019-06-26 06:24:21 +03:00
|
|
|
if (counter.IsNotable() || StaticPrefs::image_mem_debug_reporting()) {
|
2018-09-25 16:13:52 +03:00
|
|
|
ReportImage(aHandleReport, aData, aPathPrefix, counter,
|
|
|
|
aSharedSurfaces);
|
2014-11-27 05:00:15 +03:00
|
|
|
} else {
|
2018-09-25 16:13:52 +03:00
|
|
|
ImageMemoryReporter::TrimSharedSurfaces(counter, aSharedSurfaces);
|
2014-11-27 05:00:15 +03:00
|
|
|
nonNotableTotal += counter;
|
2014-05-16 03:11:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report non-notable images in aggregate.
|
2016-08-24 08:23:45 +03:00
|
|
|
ReportTotal(aHandleReport, aData, /* aExplicit = */ true, aPathPrefix,
|
|
|
|
"<non-notable images>/", nonNotableTotal);
|
2014-11-26 12:37:57 +03:00
|
|
|
|
2014-11-27 05:00:15 +03:00
|
|
|
// Report a summary in aggregate, outside of the explicit tree.
|
2016-08-24 08:23:45 +03:00
|
|
|
ReportTotal(aHandleReport, aData, /* aExplicit = */ false, aPathPrefix, "",
|
|
|
|
summaryTotal);
|
2014-11-26 12:37:57 +03:00
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
static void ReportImage(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, const char* aPathPrefix,
|
2018-09-25 16:13:52 +03:00
|
|
|
const ImageMemoryCounter& aCounter,
|
|
|
|
layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
|
2014-11-27 05:00:15 +03:00
|
|
|
nsAutoCString pathPrefix(NS_LITERAL_CSTRING("explicit/"));
|
|
|
|
pathPrefix.Append(aPathPrefix);
|
|
|
|
pathPrefix.Append(aCounter.Type() == imgIContainer::TYPE_RASTER
|
|
|
|
? "/raster/"
|
|
|
|
: "/vector/");
|
|
|
|
pathPrefix.Append(aCounter.IsUsed() ? "used/" : "unused/");
|
2017-09-04 08:14:11 +03:00
|
|
|
pathPrefix.AppendLiteral("image(");
|
2015-04-28 21:46:17 +03:00
|
|
|
pathPrefix.AppendInt(aCounter.IntrinsicSize().width);
|
2017-09-04 08:14:11 +03:00
|
|
|
pathPrefix.AppendLiteral("x");
|
2015-04-28 21:46:17 +03:00
|
|
|
pathPrefix.AppendInt(aCounter.IntrinsicSize().height);
|
2017-09-04 08:14:11 +03:00
|
|
|
pathPrefix.AppendLiteral(", ");
|
2015-04-28 21:46:17 +03:00
|
|
|
|
2014-11-27 05:00:15 +03:00
|
|
|
if (aCounter.URI().IsEmpty()) {
|
2017-09-04 08:14:11 +03:00
|
|
|
pathPrefix.AppendLiteral("<unknown URI>");
|
2014-11-27 05:00:15 +03:00
|
|
|
} else {
|
|
|
|
pathPrefix.Append(aCounter.URI());
|
|
|
|
}
|
2015-04-28 21:46:17 +03:00
|
|
|
|
2017-09-04 08:14:11 +03:00
|
|
|
pathPrefix.AppendLiteral(")/");
|
2014-11-26 12:37:57 +03:00
|
|
|
|
2018-09-25 16:13:52 +03:00
|
|
|
ReportSurfaces(aHandleReport, aData, pathPrefix, aCounter, aSharedSurfaces);
|
2015-05-05 02:08:51 +03:00
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
ReportSourceValue(aHandleReport, aData, pathPrefix, aCounter.Values());
|
2015-04-28 21:46:17 +03:00
|
|
|
}
|
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
static void ReportSurfaces(
|
|
|
|
nsIHandleReportCallback* aHandleReport, nsISupports* aData,
|
2018-09-25 16:13:52 +03:00
|
|
|
const nsACString& aPathPrefix, const ImageMemoryCounter& aCounter,
|
|
|
|
layers::SharedSurfacesMemoryReport& aSharedSurfaces) {
|
2015-04-28 21:46:17 +03:00
|
|
|
for (const SurfaceMemoryCounter& counter : aCounter.Surfaces()) {
|
|
|
|
nsAutoCString surfacePathPrefix(aPathPrefix);
|
2017-09-04 08:14:11 +03:00
|
|
|
if (counter.IsLocked()) {
|
|
|
|
surfacePathPrefix.AppendLiteral("locked/");
|
|
|
|
} else {
|
|
|
|
surfacePathPrefix.AppendLiteral("unlocked/");
|
|
|
|
}
|
2017-09-05 14:58:46 +03:00
|
|
|
if (counter.IsFactor2()) {
|
2017-09-04 08:14:11 +03:00
|
|
|
surfacePathPrefix.AppendLiteral("factor2/");
|
2017-09-05 14:58:46 +03:00
|
|
|
}
|
|
|
|
if (counter.CannotSubstitute()) {
|
2017-09-04 08:14:11 +03:00
|
|
|
surfacePathPrefix.AppendLiteral("cannot_substitute/");
|
2017-09-05 14:58:46 +03:00
|
|
|
}
|
2017-09-04 08:14:11 +03:00
|
|
|
surfacePathPrefix.AppendLiteral("surface(");
|
2015-04-28 21:46:17 +03:00
|
|
|
surfacePathPrefix.AppendInt(counter.Key().Size().width);
|
2017-09-04 08:14:11 +03:00
|
|
|
surfacePathPrefix.AppendLiteral("x");
|
2015-04-28 21:46:17 +03:00
|
|
|
surfacePathPrefix.AppendInt(counter.Key().Size().height);
|
|
|
|
|
2018-02-22 22:26:29 +03:00
|
|
|
if (counter.Values().ExternalHandles() > 0) {
|
2018-09-25 16:13:52 +03:00
|
|
|
surfacePathPrefix.AppendLiteral(", handles:");
|
2018-02-22 22:26:29 +03:00
|
|
|
surfacePathPrefix.AppendInt(
|
|
|
|
uint32_t(counter.Values().ExternalHandles()));
|
2017-02-22 17:30:22 +03:00
|
|
|
}
|
|
|
|
|
2018-09-25 16:13:52 +03:00
|
|
|
ImageMemoryReporter::AppendSharedSurfacePrefix(surfacePathPrefix, counter,
|
|
|
|
aSharedSurfaces);
|
|
|
|
|
2015-04-28 21:46:17 +03:00
|
|
|
if (counter.Type() == SurfaceMemoryCounterType::NORMAL) {
|
2016-08-18 10:06:41 +03:00
|
|
|
PlaybackType playback = counter.Key().Playback();
|
2018-09-25 16:13:52 +03:00
|
|
|
if (playback == PlaybackType::eAnimated) {
|
2019-06-26 06:24:21 +03:00
|
|
|
if (StaticPrefs::image_mem_debug_reporting()) {
|
2018-09-25 16:13:52 +03:00
|
|
|
surfacePathPrefix.AppendPrintf(
|
|
|
|
" (animation %4u)", uint32_t(counter.Values().FrameIndex()));
|
|
|
|
} else {
|
|
|
|
surfacePathPrefix.AppendLiteral(" (animation)");
|
|
|
|
}
|
|
|
|
}
|
2015-04-28 21:46:17 +03:00
|
|
|
|
2015-08-15 03:56:44 +03:00
|
|
|
if (counter.Key().Flags() != DefaultSurfaceFlags()) {
|
2017-09-04 08:14:11 +03:00
|
|
|
surfacePathPrefix.AppendLiteral(", flags:");
|
2015-08-15 03:56:44 +03:00
|
|
|
surfacePathPrefix.AppendInt(uint32_t(counter.Key().Flags()),
|
|
|
|
/* aRadix = */ 16);
|
2015-04-28 21:46:17 +03:00
|
|
|
}
|
2017-11-17 22:08:52 +03:00
|
|
|
|
|
|
|
if (counter.Key().SVGContext()) {
|
|
|
|
const SVGImageContext& context = counter.Key().SVGContext().ref();
|
|
|
|
surfacePathPrefix.AppendLiteral(", svgContext:[ ");
|
|
|
|
if (context.GetViewportSize()) {
|
|
|
|
const CSSIntSize& size = context.GetViewportSize().ref();
|
|
|
|
surfacePathPrefix.AppendLiteral("viewport=(");
|
|
|
|
surfacePathPrefix.AppendInt(size.width);
|
|
|
|
surfacePathPrefix.AppendLiteral("x");
|
|
|
|
surfacePathPrefix.AppendInt(size.height);
|
|
|
|
surfacePathPrefix.AppendLiteral(") ");
|
|
|
|
}
|
|
|
|
if (context.GetPreserveAspectRatio()) {
|
|
|
|
nsAutoString aspect;
|
|
|
|
context.GetPreserveAspectRatio()->ToString(aspect);
|
|
|
|
surfacePathPrefix.AppendLiteral("preserveAspectRatio=(");
|
|
|
|
LossyAppendUTF16toASCII(aspect, surfacePathPrefix);
|
|
|
|
surfacePathPrefix.AppendLiteral(") ");
|
|
|
|
}
|
|
|
|
if (context.GetContextPaint()) {
|
|
|
|
const SVGEmbeddingContextPaint* paint = context.GetContextPaint();
|
|
|
|
surfacePathPrefix.AppendLiteral("contextPaint=(");
|
|
|
|
if (paint->GetFill()) {
|
|
|
|
surfacePathPrefix.AppendLiteral(" fill=");
|
|
|
|
surfacePathPrefix.AppendInt(paint->GetFill()->ToABGR(), 16);
|
|
|
|
}
|
|
|
|
if (paint->GetFillOpacity()) {
|
|
|
|
surfacePathPrefix.AppendLiteral(" fillOpa=");
|
|
|
|
surfacePathPrefix.AppendFloat(paint->GetFillOpacity());
|
|
|
|
}
|
|
|
|
if (paint->GetStroke()) {
|
|
|
|
surfacePathPrefix.AppendLiteral(" stroke=");
|
|
|
|
surfacePathPrefix.AppendInt(paint->GetStroke()->ToABGR(), 16);
|
|
|
|
}
|
|
|
|
if (paint->GetStrokeOpacity()) {
|
|
|
|
surfacePathPrefix.AppendLiteral(" strokeOpa=");
|
|
|
|
surfacePathPrefix.AppendFloat(paint->GetStrokeOpacity());
|
|
|
|
}
|
|
|
|
surfacePathPrefix.AppendLiteral(" ) ");
|
|
|
|
}
|
|
|
|
surfacePathPrefix.AppendLiteral("]");
|
|
|
|
}
|
2015-04-28 21:46:17 +03:00
|
|
|
} else if (counter.Type() == SurfaceMemoryCounterType::COMPOSITING) {
|
2017-09-04 08:14:11 +03:00
|
|
|
surfacePathPrefix.AppendLiteral(", compositing frame");
|
2015-04-28 21:46:17 +03:00
|
|
|
} else if (counter.Type() == SurfaceMemoryCounterType::COMPOSITING_PREV) {
|
2017-09-04 08:14:11 +03:00
|
|
|
surfacePathPrefix.AppendLiteral(", compositing prev frame");
|
2015-04-28 21:46:17 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown counter type");
|
|
|
|
}
|
|
|
|
|
2017-09-04 08:14:11 +03:00
|
|
|
surfacePathPrefix.AppendLiteral(")/");
|
2015-04-28 21:46:17 +03:00
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
ReportValues(aHandleReport, aData, surfacePathPrefix, counter.Values());
|
2015-04-28 21:46:17 +03:00
|
|
|
}
|
2014-11-27 05:00:15 +03:00
|
|
|
}
|
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
static void ReportTotal(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, bool aExplicit,
|
|
|
|
const char* aPathPrefix, const char* aPathInfix,
|
|
|
|
const MemoryTotal& aTotal) {
|
2014-11-27 05:00:15 +03:00
|
|
|
nsAutoCString pathPrefix;
|
|
|
|
if (aExplicit) {
|
2017-09-04 08:14:11 +03:00
|
|
|
pathPrefix.AppendLiteral("explicit/");
|
2014-11-26 12:37:57 +03:00
|
|
|
}
|
2014-11-27 05:00:15 +03:00
|
|
|
pathPrefix.Append(aPathPrefix);
|
|
|
|
|
|
|
|
nsAutoCString rasterUsedPrefix(pathPrefix);
|
2017-09-04 08:14:11 +03:00
|
|
|
rasterUsedPrefix.AppendLiteral("/raster/used/");
|
2014-11-27 05:00:15 +03:00
|
|
|
rasterUsedPrefix.Append(aPathInfix);
|
2016-08-24 08:23:45 +03:00
|
|
|
ReportValues(aHandleReport, aData, rasterUsedPrefix, aTotal.UsedRaster());
|
2014-11-27 05:00:15 +03:00
|
|
|
|
|
|
|
nsAutoCString rasterUnusedPrefix(pathPrefix);
|
2017-09-04 08:14:11 +03:00
|
|
|
rasterUnusedPrefix.AppendLiteral("/raster/unused/");
|
2014-11-27 05:00:15 +03:00
|
|
|
rasterUnusedPrefix.Append(aPathInfix);
|
2016-08-24 08:23:45 +03:00
|
|
|
ReportValues(aHandleReport, aData, rasterUnusedPrefix,
|
|
|
|
aTotal.UnusedRaster());
|
2014-11-27 05:00:15 +03:00
|
|
|
|
|
|
|
nsAutoCString vectorUsedPrefix(pathPrefix);
|
2017-09-04 08:14:11 +03:00
|
|
|
vectorUsedPrefix.AppendLiteral("/vector/used/");
|
2014-11-27 05:00:15 +03:00
|
|
|
vectorUsedPrefix.Append(aPathInfix);
|
2016-08-24 08:23:45 +03:00
|
|
|
ReportValues(aHandleReport, aData, vectorUsedPrefix, aTotal.UsedVector());
|
2014-11-27 05:00:15 +03:00
|
|
|
|
|
|
|
nsAutoCString vectorUnusedPrefix(pathPrefix);
|
2017-09-04 08:14:11 +03:00
|
|
|
vectorUnusedPrefix.AppendLiteral("/vector/unused/");
|
2014-11-27 05:00:15 +03:00
|
|
|
vectorUnusedPrefix.Append(aPathInfix);
|
2016-08-24 08:23:45 +03:00
|
|
|
ReportValues(aHandleReport, aData, vectorUnusedPrefix,
|
|
|
|
aTotal.UnusedVector());
|
2014-11-27 05:00:15 +03:00
|
|
|
}
|
2014-11-26 12:37:57 +03:00
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
static void ReportValues(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, const nsACString& aPathPrefix,
|
|
|
|
const MemoryCounter& aCounter) {
|
|
|
|
ReportSourceValue(aHandleReport, aData, aPathPrefix, aCounter);
|
2014-11-27 05:00:15 +03:00
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "decoded-heap",
|
|
|
|
"Decoded image data which is stored on the heap.",
|
|
|
|
aCounter.DecodedHeap());
|
2014-11-27 05:00:15 +03:00
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
ReportValue(aHandleReport, aData, KIND_NONHEAP, aPathPrefix,
|
|
|
|
"decoded-nonheap",
|
|
|
|
"Decoded image data which isn't stored on the heap.",
|
|
|
|
aCounter.DecodedNonHeap());
|
2014-11-27 05:00:15 +03:00
|
|
|
}
|
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
static void ReportSourceValue(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData,
|
|
|
|
const nsACString& aPathPrefix,
|
|
|
|
const MemoryCounter& aCounter) {
|
|
|
|
ReportValue(aHandleReport, aData, KIND_HEAP, aPathPrefix, "source",
|
|
|
|
"Raster image source data and vector image documents.",
|
|
|
|
aCounter.Source());
|
2015-05-05 02:08:51 +03:00
|
|
|
}
|
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
static void ReportValue(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, int32_t aKind,
|
|
|
|
const nsACString& aPathPrefix,
|
|
|
|
const char* aPathSuffix, const char* aDescription,
|
|
|
|
size_t aValue) {
|
2014-11-27 05:00:15 +03:00
|
|
|
if (aValue == 0) {
|
2016-08-24 08:23:45 +03:00
|
|
|
return;
|
2014-11-27 05:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString desc(aDescription);
|
|
|
|
nsAutoCString path(aPathPrefix);
|
|
|
|
path.Append(aPathSuffix);
|
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
aHandleReport->Callback(EmptyCString(), path, aKind, UNITS_BYTES, aValue,
|
|
|
|
desc, aData);
|
2014-11-27 05:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RecordCounterForRequest(imgRequest* aRequest,
|
|
|
|
nsTArray<ImageMemoryCounter>* aArray,
|
|
|
|
bool aIsUsed) {
|
2017-03-21 21:09:13 +03:00
|
|
|
RefPtr<image::Image> image = aRequest->GetImage();
|
2014-11-27 05:00:15 +03:00
|
|
|
if (!image) {
|
|
|
|
return;
|
2014-09-06 01:36:11 +04:00
|
|
|
}
|
|
|
|
|
2017-07-28 13:10:04 +03:00
|
|
|
SizeOfState state(ImagesMallocSizeOf);
|
|
|
|
ImageMemoryCounter counter(image, state, aIsUsed);
|
2014-11-27 05:00:15 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
aArray->AppendElement(std::move(counter));
|
2014-09-06 01:36:11 +04:00
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(imgMemoryReporter, nsIMemoryReporter)
|
2013-12-08 09:39:47 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsProgressNotificationProxy, nsIProgressEventSink,
|
|
|
|
nsIChannelEventSink, nsIInterfaceRequestor)
|
2009-07-28 20:13:48 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProgressNotificationProxy::OnProgress(nsIRequest* request, nsISupports* ctxt,
|
2015-01-08 22:48:52 +03:00
|
|
|
int64_t progress, int64_t progressMax) {
|
2009-07-28 20:13:48 +04:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
2010-11-21 22:21:57 +03:00
|
|
|
request->GetLoadGroup(getter_AddRefs(loadGroup));
|
2009-07-28 20:13:48 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIProgressEventSink> target;
|
|
|
|
NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
|
|
|
|
NS_GET_IID(nsIProgressEventSink),
|
|
|
|
getter_AddRefs(target));
|
2015-04-08 00:49:00 +03:00
|
|
|
if (!target) {
|
2009-07-28 20:13:48 +04:00
|
|
|
return NS_OK;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-07-28 20:13:48 +04:00
|
|
|
return target->OnProgress(mImageRequest, ctxt, progress, progressMax);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProgressNotificationProxy::OnStatus(nsIRequest* request, nsISupports* ctxt,
|
|
|
|
nsresult status,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* statusArg) {
|
2009-07-28 20:13:48 +04:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
2010-11-21 22:21:57 +03:00
|
|
|
request->GetLoadGroup(getter_AddRefs(loadGroup));
|
2009-07-28 20:13:48 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIProgressEventSink> target;
|
|
|
|
NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
|
|
|
|
NS_GET_IID(nsIProgressEventSink),
|
|
|
|
getter_AddRefs(target));
|
2015-04-08 00:49:00 +03:00
|
|
|
if (!target) {
|
2009-07-28 20:13:48 +04:00
|
|
|
return NS_OK;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-07-28 20:13:48 +04:00
|
|
|
return target->OnStatus(mImageRequest, ctxt, status, statusArg);
|
|
|
|
}
|
|
|
|
|
2009-08-13 15:20:41 +04:00
|
|
|
NS_IMETHODIMP
|
2015-04-08 00:49:00 +03:00
|
|
|
nsProgressNotificationProxy::AsyncOnChannelRedirect(
|
|
|
|
nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
|
|
|
|
nsIAsyncVerifyRedirectCallback* cb) {
|
2009-08-13 15:20:41 +04:00
|
|
|
// Tell the original original callbacks about it too
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
2010-11-21 22:21:57 +03:00
|
|
|
newChannel->GetLoadGroup(getter_AddRefs(loadGroup));
|
2009-08-13 15:20:41 +04:00
|
|
|
nsCOMPtr<nsIChannelEventSink> target;
|
|
|
|
NS_QueryNotificationCallbacks(mOriginalCallbacks, loadGroup,
|
|
|
|
NS_GET_IID(nsIChannelEventSink),
|
|
|
|
getter_AddRefs(target));
|
2010-08-05 06:15:55 +04:00
|
|
|
if (!target) {
|
|
|
|
cb->OnRedirectVerifyCallback(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delegate to |target| if set, reusing |cb|
|
|
|
|
return target->AsyncOnChannelRedirect(oldChannel, newChannel, flags, cb);
|
2009-08-13 15:20:41 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:13:48 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProgressNotificationProxy::GetInterface(const nsIID& iid, void** result) {
|
|
|
|
if (iid.Equals(NS_GET_IID(nsIProgressEventSink))) {
|
|
|
|
*result = static_cast<nsIProgressEventSink*>(this);
|
2009-07-28 21:46:04 +04:00
|
|
|
NS_ADDREF_THIS();
|
2009-07-28 20:13:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-08-13 15:20:41 +04:00
|
|
|
if (iid.Equals(NS_GET_IID(nsIChannelEventSink))) {
|
|
|
|
*result = static_cast<nsIChannelEventSink*>(this);
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-04-08 00:49:00 +03:00
|
|
|
if (mOriginalCallbacks) {
|
2009-07-28 20:13:48 +04:00
|
|
|
return mOriginalCallbacks->GetInterface(iid, result);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-07-28 20:13:48 +04:00
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
static void NewRequestAndEntry(bool aForcePrincipalCheckForCacheEntry,
|
2015-05-20 20:21:13 +03:00
|
|
|
imgLoader* aLoader, const ImageCacheKey& aKey,
|
2015-04-08 00:49:00 +03:00
|
|
|
imgRequest** aRequest, imgCacheEntry** aEntry) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> request = new imgRequest(aLoader, aKey);
|
|
|
|
RefPtr<imgCacheEntry> entry =
|
2015-04-08 00:49:00 +03:00
|
|
|
new imgCacheEntry(aLoader, request, aForcePrincipalCheckForCacheEntry);
|
2014-09-06 01:36:11 +04:00
|
|
|
aLoader->AddToUncachedImages(request);
|
2011-10-01 20:14:40 +04:00
|
|
|
request.forget(aRequest);
|
|
|
|
entry.forget(aEntry);
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
2008-09-04 20:36:27 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
static bool ShouldRevalidateEntry(imgCacheEntry* aEntry, nsLoadFlags aFlags,
|
|
|
|
bool aHasExpired) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool bValidateEntry = false;
|
2002-09-19 02:08:12 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (aFlags & nsIRequest::LOAD_BYPASS_CACHE) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2002-09-19 02:08:12 +04:00
|
|
|
|
|
|
|
if (aFlags & nsIRequest::VALIDATE_ALWAYS) {
|
2011-10-17 18:59:28 +04:00
|
|
|
bValidateEntry = true;
|
2015-04-08 00:49:00 +03:00
|
|
|
} else if (aEntry->GetMustValidate()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
bValidateEntry = true;
|
2015-04-08 00:49:00 +03:00
|
|
|
} else if (aHasExpired) {
|
|
|
|
// The cache entry has expired... Determine whether the stale cache
|
|
|
|
// entry can be used without validation...
|
2013-03-30 00:14:19 +04:00
|
|
|
if (aFlags &
|
2015-04-08 00:49:00 +03:00
|
|
|
(nsIRequest::VALIDATE_NEVER | nsIRequest::VALIDATE_ONCE_PER_SESSION)) {
|
|
|
|
// VALIDATE_NEVER and VALIDATE_ONCE_PER_SESSION allow stale cache
|
|
|
|
// entries to be used unless they have been explicitly marked to
|
|
|
|
// indicate that revalidation is necessary.
|
2011-10-17 18:59:28 +04:00
|
|
|
bValidateEntry = false;
|
2015-04-08 00:49:00 +03:00
|
|
|
|
|
|
|
} else if (!(aFlags & nsIRequest::LOAD_FROM_CACHE)) {
|
|
|
|
// LOAD_FROM_CACHE allows a stale cache entry to be used... Otherwise,
|
|
|
|
// the entry must be revalidated.
|
2011-10-17 18:59:28 +04:00
|
|
|
bValidateEntry = true;
|
2002-09-19 02:08:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bValidateEntry;
|
|
|
|
}
|
|
|
|
|
2015-03-24 19:18:48 +03:00
|
|
|
/* Call content policies on cached images that went through a redirect */
|
|
|
|
static bool ShouldLoadCachedImage(imgRequest* aImgRequest,
|
|
|
|
nsISupports* aLoadingContext,
|
2017-10-03 06:38:30 +03:00
|
|
|
nsIPrincipal* aTriggeringPrincipal,
|
2018-08-01 07:35:24 +03:00
|
|
|
nsContentPolicyType aPolicyType,
|
|
|
|
bool aSendCSPViolationReports) {
|
2015-03-24 19:18:48 +03:00
|
|
|
/* Call content policies on cached images - Bug 1082837
|
|
|
|
* Cached images are keyed off of the first uri in a redirect chain.
|
|
|
|
* Hence content policies don't get a chance to test the intermediate hops
|
|
|
|
* or the final desitnation. Here we test the final destination using
|
2017-11-13 11:31:24 +03:00
|
|
|
* mFinalURI off of the imgRequest and passing it into content policies.
|
2015-03-24 19:18:48 +03:00
|
|
|
* For Mixed Content Blocker, we do an additional check to determine if any
|
|
|
|
* of the intermediary hops went through an insecure redirect with the
|
|
|
|
* mHadInsecureRedirect flag
|
|
|
|
*/
|
|
|
|
bool insecureRedirect = aImgRequest->HadInsecureRedirect();
|
|
|
|
nsCOMPtr<nsIURI> contentLocation;
|
2017-11-13 11:31:24 +03:00
|
|
|
aImgRequest->GetFinalURI(getter_AddRefs(contentLocation));
|
2015-03-24 19:18:48 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2018-03-29 13:16:23 +03:00
|
|
|
nsCOMPtr<nsINode> requestingNode = do_QueryInterface(aLoadingContext);
|
|
|
|
nsCOMPtr<nsIPrincipal> loadingPrincipal =
|
|
|
|
requestingNode ? requestingNode->NodePrincipal() : aTriggeringPrincipal;
|
|
|
|
// If there is no context and also no triggeringPrincipal, then we use a fresh
|
|
|
|
// nullPrincipal as the loadingPrincipal because we can not create a loadinfo
|
|
|
|
// without a valid loadingPrincipal.
|
|
|
|
if (!loadingPrincipal) {
|
|
|
|
loadingPrincipal = NullPrincipal::CreateWithoutOriginAttributes();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadInfo> secCheckLoadInfo = new LoadInfo(
|
|
|
|
loadingPrincipal, aTriggeringPrincipal, requestingNode,
|
|
|
|
nsILoadInfo::SEC_ONLY_FOR_EXPLICIT_CONTENTSEC_CHECK, aPolicyType);
|
|
|
|
|
2018-08-01 07:35:24 +03:00
|
|
|
secCheckLoadInfo->SetSendCSPViolationEvents(aSendCSPViolationReports);
|
|
|
|
|
2015-03-24 19:18:48 +03:00
|
|
|
int16_t decision = nsIContentPolicy::REJECT_REQUEST;
|
2018-03-29 13:16:23 +03:00
|
|
|
rv = NS_CheckContentLoadPolicy(contentLocation, secCheckLoadInfo,
|
2015-03-24 19:18:48 +03:00
|
|
|
EmptyCString(), // mime guess
|
2017-07-11 01:00:03 +03:00
|
|
|
&decision, nsContentUtils::GetContentPolicy());
|
2015-03-24 19:18:48 +03:00
|
|
|
if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We call all Content Policies above, but we also have to call mcb
|
|
|
|
// individually to check the intermediary redirect hops are secure.
|
|
|
|
if (insecureRedirect) {
|
2017-03-02 16:08:19 +03:00
|
|
|
// Bug 1314356: If the image ended up in the cache upgraded by HSTS and the
|
|
|
|
// page uses upgrade-inscure-requests it had an insecure redirect
|
|
|
|
// (http->https). We need to invalidate the image and reload it because
|
|
|
|
// mixed content blocker only bails if upgrade-insecure-requests is set on
|
|
|
|
// the doc and the resource load is http: which would result in an incorrect
|
|
|
|
// mixed content warning.
|
|
|
|
nsCOMPtr<nsIDocShell> docShell =
|
|
|
|
NS_CP_GetDocShellFromContext(aLoadingContext);
|
|
|
|
if (docShell) {
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* document = docShell->GetDocument();
|
2017-03-02 16:08:19 +03:00
|
|
|
if (document && document->GetUpgradeInsecureRequests(false)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 06:38:30 +03:00
|
|
|
if (!nsContentUtils::IsSystemPrincipal(aTriggeringPrincipal)) {
|
|
|
|
// Set the requestingLocation from the aTriggeringPrincipal.
|
2015-03-24 19:18:48 +03:00
|
|
|
nsCOMPtr<nsIURI> requestingLocation;
|
2019-01-21 15:21:30 +03:00
|
|
|
if (aTriggeringPrincipal) {
|
2017-10-03 06:38:30 +03:00
|
|
|
rv = aTriggeringPrincipal->GetURI(getter_AddRefs(requestingLocation));
|
2015-03-24 19:18:48 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset the decision for mixed content blocker check
|
|
|
|
decision = nsIContentPolicy::REJECT_REQUEST;
|
|
|
|
rv = nsMixedContentBlocker::ShouldLoad(
|
|
|
|
insecureRedirect, aPolicyType, contentLocation, requestingLocation,
|
|
|
|
aLoadingContext,
|
|
|
|
EmptyCString(), // mime guess
|
2017-10-03 06:38:30 +03:00
|
|
|
aTriggeringPrincipal, &decision);
|
2015-03-24 19:18:48 +03:00
|
|
|
if (NS_FAILED(rv) || !NS_CP_ACCEPTED(decision)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-14 22:47:34 +04:00
|
|
|
// Returns true if this request is compatible with the given CORS mode on the
|
|
|
|
// given loading principal, and false if the request may not be reused due
|
2014-11-18 16:46:53 +03:00
|
|
|
// to CORS. Also checks the Referrer Policy, since requests with different
|
|
|
|
// referrers/policies may generate different responses.
|
|
|
|
static bool ValidateSecurityInfo(imgRequest* request, bool forcePrincipalCheck,
|
2017-10-03 06:38:30 +03:00
|
|
|
int32_t corsmode,
|
|
|
|
nsIPrincipal* triggeringPrincipal,
|
2016-04-27 20:40:56 +03:00
|
|
|
nsISupports* aCX,
|
|
|
|
nsContentPolicyType aPolicyType,
|
2019-07-25 11:23:32 +03:00
|
|
|
nsIReferrerInfo* aReferrerInfo) {
|
|
|
|
// If the referrer policy doesn't match, we can't use this request.
|
|
|
|
// XXX: Note that we only validate referrer policy, not referrerInfo object.
|
|
|
|
// We should do with referrerInfo object, but it will cause us to use more
|
2019-09-12 12:00:52 +03:00
|
|
|
// resources in the common case (the same policies but different original
|
2019-07-25 11:23:32 +03:00
|
|
|
// referrers).
|
2015-06-06 01:25:24 +03:00
|
|
|
// XXX: this will return false if an image has different referrer attributes,
|
|
|
|
// i.e. we currently don't use the cached image but reload the image with
|
|
|
|
// the new referrer policy bug 1174921
|
2019-08-21 16:24:45 +03:00
|
|
|
ReferrerPolicy referrerPolicy = ReferrerPolicy::_empty;
|
2019-07-25 11:23:32 +03:00
|
|
|
if (aReferrerInfo) {
|
2019-08-21 16:24:45 +03:00
|
|
|
referrerPolicy = aReferrerInfo->ReferrerPolicy();
|
2019-07-25 11:23:32 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 16:24:45 +03:00
|
|
|
ReferrerPolicy requestReferrerPolicy = ReferrerPolicy::_empty;
|
2019-07-25 11:23:32 +03:00
|
|
|
if (request->GetReferrerInfo()) {
|
2019-08-21 16:24:45 +03:00
|
|
|
requestReferrerPolicy = request->GetReferrerInfo()->ReferrerPolicy();
|
2019-07-25 11:23:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (referrerPolicy != requestReferrerPolicy) {
|
2014-11-18 16:46:53 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-14 22:47:34 +04:00
|
|
|
// If the entry's CORS mode doesn't match, or the CORS mode matches but the
|
|
|
|
// document principal isn't the same, we can't use this request.
|
|
|
|
if (request->GetCORSMode() != corsmode) {
|
|
|
|
return false;
|
2017-02-10 15:52:27 +03:00
|
|
|
}
|
|
|
|
if (request->GetCORSMode() != imgIRequest::CORS_NONE || forcePrincipalCheck) {
|
2017-10-03 06:38:30 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> otherprincipal = request->GetTriggeringPrincipal();
|
2011-07-14 22:47:34 +04:00
|
|
|
|
|
|
|
// If we previously had a principal, but we don't now, we can't use this
|
|
|
|
// request.
|
2017-10-03 06:38:30 +03:00
|
|
|
if (otherprincipal && !triggeringPrincipal) {
|
2011-07-14 22:47:34 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-03 06:38:30 +03:00
|
|
|
if (otherprincipal && triggeringPrincipal) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool equals = false;
|
2017-10-03 06:38:30 +03:00
|
|
|
otherprincipal->Equals(triggeringPrincipal, &equals);
|
2015-03-24 19:18:48 +03:00
|
|
|
if (!equals) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-14 22:47:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 19:18:48 +03:00
|
|
|
// Content Policy Check on Cached Images
|
2018-08-01 07:35:24 +03:00
|
|
|
return ShouldLoadCachedImage(request, aCX, triggeringPrincipal, aPolicyType,
|
|
|
|
/* aSendCSPViolationReports */ false);
|
2011-07-14 22:47:34 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
static nsresult NewImageChannel(
|
|
|
|
nsIChannel** aResult,
|
|
|
|
// If aForcePrincipalCheckForCacheEntry is true, then we will
|
|
|
|
// force a principal check even when not using CORS before
|
|
|
|
// assuming we have a cache hit on a cache entry that we
|
|
|
|
// create for this channel. This is an out param that should
|
|
|
|
// be set to true if this channel ends up depending on
|
2017-10-03 06:38:30 +03:00
|
|
|
// aTriggeringPrincipal and false otherwise.
|
2015-04-08 00:49:00 +03:00
|
|
|
bool* aForcePrincipalCheckForCacheEntry, nsIURI* aURI,
|
2019-07-25 11:23:32 +03:00
|
|
|
nsIURI* aInitialDocumentURI, int32_t aCORSMode,
|
|
|
|
nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
|
2015-04-08 00:49:00 +03:00
|
|
|
const nsCString& aAcceptHeader, nsLoadFlags aLoadFlags,
|
2017-10-03 06:38:30 +03:00
|
|
|
nsContentPolicyType aPolicyType, nsIPrincipal* aTriggeringPrincipal,
|
2016-04-13 16:34:36 +03:00
|
|
|
nsISupports* aRequestingContext, bool aRespectPrivacy) {
|
2015-04-19 03:18:49 +03:00
|
|
|
MOZ_ASSERT(aResult);
|
|
|
|
|
2002-09-19 02:08:12 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIHttpChannel> newHttpChannel;
|
2013-03-30 00:14:19 +04:00
|
|
|
|
2002-09-19 02:08:12 +04:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> callbacks;
|
|
|
|
|
|
|
|
if (aLoadGroup) {
|
|
|
|
// Get the notification callbacks from the load group for the new channel.
|
|
|
|
//
|
|
|
|
// XXX: This is not exactly correct, because the network request could be
|
|
|
|
// referenced by multiple windows... However, the new channel needs
|
|
|
|
// something. So, using the 'first' notification callbacks is better
|
|
|
|
// than nothing...
|
|
|
|
//
|
|
|
|
aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
|
|
|
|
}
|
|
|
|
|
2013-08-23 23:51:00 +04:00
|
|
|
// Pass in a nullptr loadgroup because this is the underlying network
|
|
|
|
// request. This request may be referenced by several proxy image requests
|
|
|
|
// (possibly in different documents).
|
2002-09-19 02:08:12 +04:00
|
|
|
// If all of the proxy requests are canceled then this request should be
|
|
|
|
// canceled too.
|
|
|
|
//
|
2014-09-21 20:45:34 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> requestingNode = do_QueryInterface(aRequestingContext);
|
2016-04-27 20:40:56 +03:00
|
|
|
|
|
|
|
nsSecurityFlags securityFlags =
|
|
|
|
aCORSMode == imgIRequest::CORS_NONE
|
|
|
|
? nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS
|
|
|
|
: nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS;
|
|
|
|
if (aCORSMode == imgIRequest::CORS_ANONYMOUS) {
|
|
|
|
securityFlags |= nsILoadInfo::SEC_COOKIES_SAME_ORIGIN;
|
|
|
|
} else if (aCORSMode == imgIRequest::CORS_USE_CREDENTIALS) {
|
|
|
|
securityFlags |= nsILoadInfo::SEC_COOKIES_INCLUDE;
|
2014-07-18 03:07:58 +04:00
|
|
|
}
|
2016-04-27 20:40:56 +03:00
|
|
|
securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
|
2014-08-20 01:49:38 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
// Note we are calling NS_NewChannelWithTriggeringPrincipal() here with a
|
|
|
|
// node and a principal. This is for things like background images that are
|
|
|
|
// specified by user stylesheets, where the document is being styled, but
|
|
|
|
// the principal is that of the user stylesheet.
|
2017-10-03 06:38:30 +03:00
|
|
|
if (requestingNode && aTriggeringPrincipal) {
|
2014-11-14 19:56:55 +03:00
|
|
|
rv = NS_NewChannelWithTriggeringPrincipal(aResult, aURI, requestingNode,
|
2017-10-03 06:38:30 +03:00
|
|
|
aTriggeringPrincipal,
|
2014-11-14 19:56:55 +03:00
|
|
|
securityFlags, aPolicyType,
|
2018-01-24 19:17:31 +03:00
|
|
|
nullptr, // PerformanceStorage
|
2014-11-14 19:56:55 +03:00
|
|
|
nullptr, // loadGroup
|
|
|
|
callbacks, aLoadFlags);
|
2016-10-13 10:43:57 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
|
|
|
|
// If this is a favicon loading, we will use the originAttributes from the
|
2017-10-03 06:38:30 +03:00
|
|
|
// triggeringPrincipal as the channel's originAttributes. This allows the
|
2016-10-13 10:43:57 +03:00
|
|
|
// favicon loading from XUL will use the correct originAttributes.
|
|
|
|
|
2019-02-20 15:27:25 +03:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
|
|
|
|
rv = loadInfo->SetOriginAttributes(
|
|
|
|
aTriggeringPrincipal->OriginAttributesRef());
|
2016-10-13 10:43:57 +03:00
|
|
|
}
|
2015-04-08 00:49:00 +03:00
|
|
|
} else {
|
2014-11-14 19:56:55 +03:00
|
|
|
// either we are loading something inside a document, in which case
|
|
|
|
// we should always have a requestingNode, or we are loading something
|
2017-10-03 06:38:30 +03:00
|
|
|
// outside a document, in which case the triggeringPrincipal and
|
2016-04-27 20:40:56 +03:00
|
|
|
// triggeringPrincipal should always be the systemPrincipal.
|
2016-10-13 10:43:57 +03:00
|
|
|
// However, there are exceptions: one is Notifications which create a
|
2019-03-08 12:04:11 +03:00
|
|
|
// channel in the parent process in which case we can't get a
|
2016-10-13 10:43:57 +03:00
|
|
|
// requestingNode.
|
2016-04-27 20:40:56 +03:00
|
|
|
rv = NS_NewChannel(aResult, aURI, nsContentUtils::GetSystemPrincipal(),
|
2014-11-14 19:56:55 +03:00
|
|
|
securityFlags, aPolicyType,
|
2019-03-08 12:04:11 +03:00
|
|
|
nullptr, // nsICookieSettings
|
2018-01-24 19:17:31 +03:00
|
|
|
nullptr, // PerformanceStorage
|
2014-11-14 19:56:55 +03:00
|
|
|
nullptr, // loadGroup
|
|
|
|
callbacks, aLoadFlags);
|
2016-08-31 00:54:58 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the OriginAttributes from the loading principal, if one is available,
|
|
|
|
// and adjust the private browsing ID based on what kind of load the caller
|
|
|
|
// has asked us to perform.
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes attrs;
|
2017-10-03 06:38:30 +03:00
|
|
|
if (aTriggeringPrincipal) {
|
|
|
|
attrs = aTriggeringPrincipal->OriginAttributesRef();
|
2016-08-31 00:54:58 +03:00
|
|
|
}
|
2017-01-12 19:38:48 +03:00
|
|
|
attrs.mPrivateBrowsingId = aRespectPrivacy ? 1 : 0;
|
2016-08-31 00:54:58 +03:00
|
|
|
|
2019-02-20 15:27:25 +03:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = (*aResult)->LoadInfo();
|
|
|
|
rv = loadInfo->SetOriginAttributes(attrs);
|
2014-11-14 19:56:55 +03:00
|
|
|
}
|
2014-09-21 20:45:34 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2002-09-19 02:08:12 +04:00
|
|
|
return rv;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2002-09-19 02:08:12 +04:00
|
|
|
|
2016-04-27 20:40:56 +03:00
|
|
|
// only inherit if we have a principal
|
|
|
|
*aForcePrincipalCheckForCacheEntry =
|
|
|
|
aTriggeringPrincipal && nsContentUtils::ChannelShouldInheritPrincipal(
|
2017-10-03 06:38:30 +03:00
|
|
|
aTriggeringPrincipal, aURI,
|
2016-04-27 20:40:56 +03:00
|
|
|
/* aInheritForAboutBlank */ false,
|
|
|
|
/* aForceInherit */ false);
|
2011-09-21 01:00:42 +04:00
|
|
|
|
2002-09-19 02:08:12 +04:00
|
|
|
// Initialize HTTP-specific attributes
|
|
|
|
newHttpChannel = do_QueryInterface(*aResult);
|
|
|
|
if (newHttpChannel) {
|
2016-12-20 06:51:33 +03:00
|
|
|
rv = newHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
|
|
|
|
aAcceptHeader, false);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2002-10-01 03:18:43 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
nsCOMPtr<nsIHttpChannelInternal> httpChannelInternal =
|
|
|
|
do_QueryInterface(newHttpChannel);
|
2002-10-01 03:18:43 +04:00
|
|
|
NS_ENSURE_TRUE(httpChannelInternal, NS_ERROR_UNEXPECTED);
|
2016-12-20 06:51:33 +03:00
|
|
|
rv = httpChannelInternal->SetDocumentURI(aInitialDocumentURI);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2019-07-25 11:23:32 +03:00
|
|
|
if (aReferrerInfo) {
|
|
|
|
DebugOnly<nsresult> rv = newHttpChannel->SetReferrerInfo(aReferrerInfo);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
2002-09-19 02:08:12 +04:00
|
|
|
}
|
|
|
|
|
2005-02-21 23:58:01 +03:00
|
|
|
// Image channels are loaded by default with reduced priority.
|
|
|
|
nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(*aResult);
|
|
|
|
if (p) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t priority = nsISupportsPriority::PRIORITY_LOW;
|
2005-02-21 23:58:01 +03:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
|
2005-02-21 23:58:01 +03:00
|
|
|
++priority; // further reduce priority for background loads
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2005-02-21 23:58:01 +03:00
|
|
|
|
2005-02-22 01:21:21 +03:00
|
|
|
p->AdjustPriority(priority);
|
2005-02-21 23:58:01 +03:00
|
|
|
}
|
|
|
|
|
2014-01-30 07:35:40 +04:00
|
|
|
// Create a new loadgroup for this new channel, using the old group as
|
|
|
|
// the parent. The indirection keeps the channel insulated from cancels,
|
|
|
|
// but does allow a way for this revalidation to be associated with at
|
|
|
|
// least one base load group for scheduling/caching purposes.
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID);
|
|
|
|
nsCOMPtr<nsILoadGroupChild> childLoadGroup = do_QueryInterface(loadGroup);
|
|
|
|
if (childLoadGroup) {
|
|
|
|
childLoadGroup->SetParentLoadGroup(aLoadGroup);
|
|
|
|
}
|
|
|
|
(*aResult)->SetLoadGroup(loadGroup);
|
|
|
|
|
2002-09-19 02:08:12 +04:00
|
|
|
return NS_OK;
|
2002-01-15 08:23:33 +03:00
|
|
|
}
|
2001-07-17 05:14:40 +04:00
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
uint32_t imgCacheEntry::SecondsFromPRTime(PRTime prTime) {
|
2012-08-22 19:56:38 +04:00
|
|
|
return uint32_t(int64_t(prTime) / int64_t(PR_USEC_PER_SEC));
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
imgCacheEntry::imgCacheEntry(imgLoader* loader, imgRequest* request,
|
|
|
|
bool forcePrincipalCheck)
|
2012-06-26 08:20:12 +04:00
|
|
|
: mLoader(loader),
|
|
|
|
mRequest(request),
|
2008-09-05 03:00:42 +04:00
|
|
|
mDataSize(0),
|
|
|
|
mTouchedTime(SecondsFromPRTime(PR_Now())),
|
2015-10-20 08:15:25 +03:00
|
|
|
mLoadTime(SecondsFromPRTime(PR_Now())),
|
2008-09-05 03:00:42 +04:00
|
|
|
mExpiryTime(0),
|
2011-10-17 18:59:28 +04:00
|
|
|
mMustValidate(false),
|
2011-07-01 21:03:38 +04:00
|
|
|
// We start off as evicted so we don't try to update the cache.
|
|
|
|
// PutIntoCache will set this to false.
|
2011-10-17 18:59:28 +04:00
|
|
|
mEvicted(true),
|
|
|
|
mHasNoProxies(true),
|
2017-05-18 00:17:23 +03:00
|
|
|
mForcePrincipalCheck(forcePrincipalCheck) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2009-01-31 05:17:47 +03:00
|
|
|
imgCacheEntry::~imgCacheEntry() {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_FUNC(gImgLog, "imgCacheEntry::~imgCacheEntry()");
|
2009-01-31 05:17:47 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgCacheEntry::Touch(bool updateTime /* = true */) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_SCOPE(gImgLog, "imgCacheEntry::Touch");
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (updateTime) {
|
2008-09-05 03:00:42 +04:00
|
|
|
mTouchedTime = SecondsFromPRTime(PR_Now());
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
UpdateCache();
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgCacheEntry::UpdateCache(int32_t diff /* = 0 */) {
|
2009-01-31 05:17:47 +03:00
|
|
|
// Don't update the cache if we've been removed from it or it doesn't care
|
|
|
|
// about our size or usage.
|
|
|
|
if (!Evicted() && HasNoProxies()) {
|
2015-04-28 21:45:45 +03:00
|
|
|
mLoader->CacheEntriesChanged(mRequest->IsChrome(), diff);
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 08:15:25 +03:00
|
|
|
void imgCacheEntry::UpdateLoadTime() {
|
|
|
|
mLoadTime = SecondsFromPRTime(PR_Now());
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgCacheEntry::SetHasNoProxies(bool hasNoProxies) {
|
2015-11-16 20:21:00 +03:00
|
|
|
if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
|
2015-05-11 23:42:30 +03:00
|
|
|
if (hasNoProxies) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies true", "uri",
|
2018-06-06 03:42:57 +03:00
|
|
|
mRequest->CacheKey().URI());
|
2015-05-11 23:42:30 +03:00
|
|
|
} else {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheEntry::SetHasNoProxies false",
|
2018-06-06 03:42:57 +03:00
|
|
|
"uri", mRequest->CacheKey().URI());
|
2015-05-11 23:42:30 +03:00
|
|
|
}
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
|
|
|
|
mHasNoProxies = hasNoProxies;
|
|
|
|
}
|
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
imgCacheQueue::imgCacheQueue() : mDirty(false), mSize(0) {}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgCacheQueue::UpdateSize(int32_t diff) { mSize += diff; }
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
uint32_t imgCacheQueue::GetSize() const { return mSize; }
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgCacheQueue::Remove(imgCacheEntry* entry) {
|
2017-09-06 02:20:18 +03:00
|
|
|
uint64_t index = mQueue.IndexOf(entry);
|
|
|
|
if (index == queueContainer::NoIndex) {
|
2017-09-06 02:20:18 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-06 02:20:18 +03:00
|
|
|
mSize -= mQueue[index]->GetDataSize();
|
2017-09-06 02:20:18 +03:00
|
|
|
|
|
|
|
// If the queue is clean and this is the first entry,
|
|
|
|
// then we can efficiently remove the entry without
|
|
|
|
// dirtying the sort order.
|
2017-09-06 02:20:18 +03:00
|
|
|
if (!IsDirty() && index == 0) {
|
2017-09-06 02:20:18 +03:00
|
|
|
std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
|
2018-03-13 16:51:33 +03:00
|
|
|
mQueue.RemoveLastElement();
|
2017-09-06 02:20:18 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from the middle of the list. This potentially
|
|
|
|
// breaks the binary heap sort order.
|
2017-09-06 02:20:18 +03:00
|
|
|
mQueue.RemoveElementAt(index);
|
2017-09-06 02:20:18 +03:00
|
|
|
|
|
|
|
// If we only have one entry or the queue is empty, though,
|
|
|
|
// then the sort order is still effectively good. Simply
|
|
|
|
// refresh the list to clear the dirty flag.
|
2017-09-06 02:20:18 +03:00
|
|
|
if (mQueue.Length() <= 1) {
|
2017-09-06 02:20:18 +03:00
|
|
|
Refresh();
|
|
|
|
return;
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
2017-09-06 02:20:18 +03:00
|
|
|
|
|
|
|
// Otherwise we must mark the queue dirty and potentially
|
|
|
|
// trigger an expensive sort later.
|
|
|
|
MarkDirty();
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgCacheQueue::Push(imgCacheEntry* entry) {
|
2008-09-05 03:00:42 +04:00
|
|
|
mSize += entry->GetDataSize();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgCacheEntry> refptr(entry);
|
2018-05-30 22:15:35 +03:00
|
|
|
mQueue.AppendElement(std::move(refptr));
|
2017-09-06 02:20:17 +03:00
|
|
|
// If we're not dirty already, then we can efficiently add this to the
|
|
|
|
// binary heap immediately. This is only O(log n).
|
|
|
|
if (!IsDirty()) {
|
|
|
|
std::push_heap(mQueue.begin(), mQueue.end(),
|
|
|
|
imgLoader::CompareCacheEntries);
|
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
already_AddRefed<imgCacheEntry> imgCacheQueue::Pop() {
|
2017-09-06 02:20:18 +03:00
|
|
|
if (mQueue.IsEmpty()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
|
|
|
if (IsDirty()) {
|
2008-09-05 03:00:42 +04:00
|
|
|
Refresh();
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
|
|
|
std::pop_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
|
2018-03-13 16:51:33 +03:00
|
|
|
RefPtr<imgCacheEntry> entry = mQueue.PopLastElement();
|
2008-09-05 03:00:42 +04:00
|
|
|
|
|
|
|
mSize -= entry->GetDataSize();
|
2013-04-22 15:15:59 +04:00
|
|
|
return entry.forget();
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgCacheQueue::Refresh() {
|
2017-09-06 02:20:17 +03:00
|
|
|
// Resort the list. This is an O(3 * n) operation and best avoided
|
|
|
|
// if possible.
|
2008-09-05 03:00:42 +04:00
|
|
|
std::make_heap(mQueue.begin(), mQueue.end(), imgLoader::CompareCacheEntries);
|
2011-10-17 18:59:28 +04:00
|
|
|
mDirty = false;
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgCacheQueue::MarkDirty() { mDirty = true; }
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
bool imgCacheQueue::IsDirty() { return mDirty; }
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
uint32_t imgCacheQueue::GetNumElements() const { return mQueue.Length(); }
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2017-09-06 23:12:05 +03:00
|
|
|
bool imgCacheQueue::Contains(imgCacheEntry* aEntry) const {
|
|
|
|
return mQueue.Contains(aEntry);
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
imgCacheQueue::iterator imgCacheQueue::begin() { return mQueue.begin(); }
|
|
|
|
|
|
|
|
imgCacheQueue::const_iterator imgCacheQueue::begin() const {
|
2008-09-05 03:00:42 +04:00
|
|
|
return mQueue.begin();
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
imgCacheQueue::iterator imgCacheQueue::end() { return mQueue.end(); }
|
|
|
|
|
|
|
|
imgCacheQueue::const_iterator imgCacheQueue::end() const {
|
2008-09-05 03:00:42 +04:00
|
|
|
return mQueue.end();
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
nsresult imgLoader::CreateNewProxyForRequest(
|
2019-01-02 16:05:23 +03:00
|
|
|
imgRequest* aRequest, nsILoadGroup* aLoadGroup, Document* aLoadingDocument,
|
|
|
|
imgINotificationObserver* aObserver, nsLoadFlags aLoadFlags,
|
|
|
|
imgRequestProxy** _retval) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::CreateNewProxyForRequest",
|
2015-04-08 00:49:00 +03:00
|
|
|
"imgRequest", aRequest);
|
2008-09-05 03:00:42 +04:00
|
|
|
|
|
|
|
/* XXX If we move decoding onto separate threads, we should save off the
|
|
|
|
calling thread here and pass it off to |proxyRequest| so that it call
|
|
|
|
proxy calls to |aObserver|.
|
|
|
|
*/
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequestProxy> proxyRequest = new imgRequestProxy();
|
2008-09-05 03:00:42 +04:00
|
|
|
|
|
|
|
/* It is important to call |SetLoadFlags()| before calling |Init()| because
|
|
|
|
|Init()| adds the request to the loadgroup.
|
|
|
|
*/
|
|
|
|
proxyRequest->SetLoadFlags(aLoadFlags);
|
|
|
|
|
2018-06-06 03:42:57 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2010-05-11 07:27:41 +04:00
|
|
|
aRequest->GetURI(getter_AddRefs(uri));
|
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
// init adds itself to imgRequest's list of observers
|
2017-07-19 21:15:11 +03:00
|
|
|
nsresult rv = proxyRequest->Init(aRequest, aLoadGroup, aLoadingDocument, uri,
|
|
|
|
aObserver);
|
2015-03-05 09:18:09 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2008-09-05 03:00:42 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-03-05 09:18:09 +03:00
|
|
|
proxyRequest.forget(_retval);
|
2008-09-05 03:00:42 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class imgCacheExpirationTracker final
|
2012-01-03 00:23:41 +04:00
|
|
|
: public nsExpirationTracker<imgCacheEntry, 3> {
|
2008-09-05 03:00:42 +04:00
|
|
|
enum { TIMEOUT_SECONDS = 10 };
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
public:
|
|
|
|
imgCacheExpirationTracker();
|
|
|
|
|
|
|
|
protected:
|
2016-11-16 18:41:47 +03:00
|
|
|
void NotifyExpired(imgCacheEntry* entry) override;
|
2008-09-05 03:00:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
imgCacheExpirationTracker::imgCacheExpirationTracker()
|
2015-09-10 07:07:07 +03:00
|
|
|
: nsExpirationTracker<imgCacheEntry, 3>(
|
|
|
|
TIMEOUT_SECONDS * 1000, "imgCacheExpirationTracker",
|
2017-08-15 03:23:16 +03:00
|
|
|
SystemGroup::EventTargetFor(TaskCategory::Other)) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgCacheExpirationTracker::NotifyExpired(imgCacheEntry* entry) {
|
2009-02-25 22:09:44 +03:00
|
|
|
// Hold on to a reference to this entry, because the expiration tracker
|
|
|
|
// mechanism doesn't.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgCacheEntry> kungFuDeathGrip(entry);
|
2009-02-25 22:09:44 +03:00
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> req = entry->GetRequest();
|
2015-05-11 23:42:30 +03:00
|
|
|
if (req) {
|
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog, "imgCacheExpirationTracker::NotifyExpired",
|
2018-06-06 03:42:57 +03:00
|
|
|
"entry", req->CacheKey().URI());
|
2015-05-11 23:42:30 +03:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
}
|
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
// We can be called multiple times on the same entry. Don't do work multiple
|
|
|
|
// times.
|
2015-04-08 00:49:00 +03:00
|
|
|
if (!entry->Evicted()) {
|
2012-06-26 08:20:12 +04:00
|
|
|
entry->Loader()->RemoveFromCache(entry);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2012-06-26 08:20:12 +04:00
|
|
|
entry->Loader()->VerifyCacheSizes();
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// imgLoader
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-08-09 11:09:42 +04:00
|
|
|
double imgLoader::sCacheTimeWeight;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t imgLoader::sCacheMaxSize;
|
2012-06-26 08:20:12 +04:00
|
|
|
imgMemoryReporter* imgLoader::sMemReporter;
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS(imgLoader, imgILoader, nsIContentSniffer, imgICache,
|
|
|
|
nsISupportsWeakReference, nsIObserver)
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2016-05-19 15:31:15 +03:00
|
|
|
static imgLoader* gNormalLoader = nullptr;
|
|
|
|
static imgLoader* gPrivateBrowsingLoader = nullptr;
|
2014-04-09 02:51:33 +04:00
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<imgLoader> imgLoader::CreateImageLoader() {
|
2016-05-19 15:31:15 +03:00
|
|
|
// In some cases, such as xpctests, XPCOM modules are not automatically
|
|
|
|
// initialized. We need to make sure that our module is initialized before
|
|
|
|
// we hand out imgLoader instances and code starts using them.
|
|
|
|
mozilla::image::EnsureModuleInitialized();
|
|
|
|
|
|
|
|
RefPtr<imgLoader> loader = new imgLoader();
|
|
|
|
loader->Init();
|
|
|
|
|
|
|
|
return loader.forget();
|
|
|
|
}
|
|
|
|
|
2016-05-19 15:31:15 +03:00
|
|
|
imgLoader* imgLoader::NormalLoader() {
|
|
|
|
if (!gNormalLoader) {
|
|
|
|
gNormalLoader = CreateImageLoader().take();
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2016-05-19 15:31:15 +03:00
|
|
|
return gNormalLoader;
|
2014-04-09 02:51:33 +04:00
|
|
|
}
|
|
|
|
|
2016-05-19 15:31:15 +03:00
|
|
|
imgLoader* imgLoader::PrivateBrowsingLoader() {
|
|
|
|
if (!gPrivateBrowsingLoader) {
|
|
|
|
gPrivateBrowsingLoader = CreateImageLoader().take();
|
|
|
|
gPrivateBrowsingLoader->RespectPrivacyNotifications();
|
2014-04-09 02:51:33 +04:00
|
|
|
}
|
2016-05-19 15:31:15 +03:00
|
|
|
return gPrivateBrowsingLoader;
|
2014-04-09 02:51:33 +04:00
|
|
|
}
|
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
imgLoader::imgLoader()
|
2014-09-06 01:36:11 +04:00
|
|
|
: mUncachedImagesMutex("imgLoader::UncachedImages"),
|
|
|
|
mRespectPrivacy(false) {
|
2012-06-26 08:20:12 +04:00
|
|
|
sMemReporter->AddRef();
|
|
|
|
sMemReporter->RegisterLoader(this);
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
imgLoader::~imgLoader() {
|
2012-06-26 08:20:12 +04:00
|
|
|
ClearChromeImageCache();
|
|
|
|
ClearImageCache();
|
2014-09-06 01:36:11 +04:00
|
|
|
{
|
|
|
|
// If there are any of our imgRequest's left they are in the uncached
|
|
|
|
// images set, so clear their pointer to us.
|
|
|
|
MutexAutoLock lock(mUncachedImagesMutex);
|
2015-07-14 16:37:57 +03:00
|
|
|
for (auto iter = mUncachedImages.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsPtrHashKey<imgRequest>* entry = iter.Get();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> req = entry->GetKey();
|
2015-07-14 16:37:57 +03:00
|
|
|
req->ClearLoader();
|
|
|
|
}
|
2014-09-06 01:36:11 +04:00
|
|
|
}
|
2012-06-26 08:20:12 +04:00
|
|
|
sMemReporter->UnregisterLoader(this);
|
|
|
|
sMemReporter->Release();
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgLoader::VerifyCacheSizes() {
|
2010-04-29 10:27:00 +04:00
|
|
|
#ifdef DEBUG
|
2015-04-08 00:49:00 +03:00
|
|
|
if (!mCacheTracker) {
|
2008-09-05 03:00:42 +04:00
|
|
|
return;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2012-06-26 08:20:12 +04:00
|
|
|
uint32_t cachesize = mCache.Count() + mChromeCache.Count();
|
2015-04-08 00:49:00 +03:00
|
|
|
uint32_t queuesize =
|
|
|
|
mCacheQueue.GetNumElements() + mChromeCacheQueue.GetNumElements();
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t trackersize = 0;
|
2015-10-17 13:53:28 +03:00
|
|
|
for (nsExpirationTracker<imgCacheEntry, 3>::Iterator it(mCacheTracker.get());
|
2015-04-08 00:49:00 +03:00
|
|
|
it.Next();) {
|
2008-09-05 03:00:42 +04:00
|
|
|
trackersize++;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(queuesize == trackersize, "Queue and tracker sizes out of sync!");
|
|
|
|
MOZ_ASSERT(queuesize <= cachesize, "Queue has more elements than cache!");
|
2010-04-29 10:27:00 +04:00
|
|
|
#endif
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
imgLoader::imgCacheTable& imgLoader::GetCache(bool aForChrome) {
|
|
|
|
return aForChrome ? mChromeCache : mCache;
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
imgLoader::imgCacheTable& imgLoader::GetCache(const ImageCacheKey& aKey) {
|
|
|
|
return GetCache(aKey.IsChrome());
|
2013-09-28 22:28:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
imgCacheQueue& imgLoader::GetCacheQueue(bool aForChrome) {
|
|
|
|
return aForChrome ? mChromeCacheQueue : mCacheQueue;
|
2013-09-28 22:28:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
imgCacheQueue& imgLoader::GetCacheQueue(const ImageCacheKey& aKey) {
|
|
|
|
return GetCacheQueue(aKey.IsChrome());
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2012-06-26 08:20:12 +04:00
|
|
|
void imgLoader::GlobalInit() {
|
2019-07-22 05:10:14 +03:00
|
|
|
sCacheTimeWeight = StaticPrefs::image_cache_timeweight_AtStartup() / 1000.0;
|
|
|
|
int32_t cachesize = StaticPrefs::image_cache_size_AtStartup();
|
2014-09-23 01:30:20 +04:00
|
|
|
sCacheMaxSize = cachesize > 0 ? cachesize : 0;
|
2009-07-15 11:22:40 +04:00
|
|
|
|
2012-06-26 08:20:12 +04:00
|
|
|
sMemReporter = new imgMemoryReporter();
|
2018-09-25 16:13:52 +03:00
|
|
|
RegisterStrongAsyncMemoryReporter(sMemReporter);
|
2015-04-08 00:49:00 +03:00
|
|
|
RegisterImagesContentUsedUncompressedDistinguishedAmount(
|
|
|
|
imgMemoryReporter::ImagesContentUsedUncompressedDistinguishedAmount);
|
2012-06-26 08:20:12 +04:00
|
|
|
}
|
|
|
|
|
2016-09-19 20:29:59 +03:00
|
|
|
void imgLoader::ShutdownMemoryReporter() {
|
|
|
|
UnregisterImagesContentUsedUncompressedDistinguishedAmount();
|
|
|
|
UnregisterStrongMemoryReporter(sMemReporter);
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
nsresult imgLoader::InitCache() {
|
2012-06-26 08:20:12 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
2015-04-08 00:49:00 +03:00
|
|
|
if (!os) {
|
2012-06-26 08:20:12 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2012-06-26 08:20:12 +04:00
|
|
|
|
|
|
|
os->AddObserver(this, "memory-pressure", false);
|
|
|
|
os->AddObserver(this, "chrome-flush-caches", false);
|
|
|
|
os->AddObserver(this, "last-pb-context-exited", false);
|
|
|
|
os->AddObserver(this, "profile-before-change", false);
|
|
|
|
os->AddObserver(this, "xpcom-shutdown", false);
|
|
|
|
|
2015-10-17 13:53:28 +03:00
|
|
|
mCacheTracker = MakeUnique<imgCacheExpirationTracker>();
|
2012-06-26 08:20:12 +04:00
|
|
|
|
2013-09-02 12:41:57 +04:00
|
|
|
return NS_OK;
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
nsresult imgLoader::Init() {
|
2012-06-26 08:20:12 +04:00
|
|
|
InitCache();
|
|
|
|
|
2011-06-12 06:30:15 +04:00
|
|
|
ReadAcceptHeaderPref();
|
2009-07-15 11:22:40 +04:00
|
|
|
|
2011-06-12 06:30:15 +04:00
|
|
|
Preferences::AddWeakObserver(this, "image.http.accept");
|
2009-07-15 11:22:40 +04:00
|
|
|
|
2012-06-26 08:20:12 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-07-12 05:01:53 +04:00
|
|
|
|
2012-06-26 08:20:12 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgLoader::RespectPrivacyNotifications() {
|
|
|
|
mRespectPrivacy = true;
|
2009-07-15 11:22:40 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-04-08 00:49:00 +03:00
|
|
|
imgLoader::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
2010-07-12 05:01:53 +04:00
|
|
|
// We listen for pref change notifications...
|
|
|
|
if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
|
2016-07-21 08:03:25 +03:00
|
|
|
if (!NS_strcmp(aData, u"image.http.accept")) {
|
2011-06-12 06:30:15 +04:00
|
|
|
ReadAcceptHeaderPref();
|
2010-07-12 05:01:53 +04:00
|
|
|
}
|
2009-07-15 11:22:40 +04:00
|
|
|
|
2012-06-26 08:20:12 +04:00
|
|
|
} else if (strcmp(aTopic, "memory-pressure") == 0) {
|
|
|
|
MinimizeCaches();
|
2019-04-24 14:18:50 +03:00
|
|
|
} else if (strcmp(aTopic, "chrome-flush-caches") == 0) {
|
2012-06-26 08:20:12 +04:00
|
|
|
MinimizeCaches();
|
|
|
|
ClearChromeImageCache();
|
|
|
|
} else if (strcmp(aTopic, "last-pb-context-exited") == 0) {
|
|
|
|
if (mRespectPrivacy) {
|
2010-07-12 05:01:53 +04:00
|
|
|
ClearImageCache();
|
2012-06-26 08:20:12 +04:00
|
|
|
ClearChromeImageCache();
|
|
|
|
}
|
2016-09-19 20:29:59 +03:00
|
|
|
} else if (strcmp(aTopic, "profile-before-change") == 0) {
|
|
|
|
mCacheTracker = nullptr;
|
|
|
|
} else if (strcmp(aTopic, "xpcom-shutdown") == 0) {
|
2012-06-26 08:20:12 +04:00
|
|
|
mCacheTracker = nullptr;
|
2016-09-19 20:29:59 +03:00
|
|
|
ShutdownMemoryReporter();
|
2010-07-12 05:01:53 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
} else {
|
2010-07-12 05:01:53 +04:00
|
|
|
// (Nothing else should bring us here)
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(0, "Invalid topic received");
|
2010-07-12 05:01:53 +04:00
|
|
|
}
|
|
|
|
|
2009-07-15 11:22:40 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-12 06:30:15 +04:00
|
|
|
void imgLoader::ReadAcceptHeaderPref() {
|
2017-07-31 07:28:48 +03:00
|
|
|
nsAutoCString accept;
|
|
|
|
nsresult rv = Preferences::GetCString("image.http.accept", accept);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-07-15 11:22:40 +04:00
|
|
|
mAcceptHeader = accept;
|
2015-04-08 00:49:00 +03:00
|
|
|
} else {
|
|
|
|
mAcceptHeader =
|
2017-07-31 07:28:48 +03:00
|
|
|
IMAGE_PNG "," IMAGE_WILDCARD ";q=0.8," ANY_WILDCARD ";q=0.5";
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-07-15 11:22:40 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgLoader::ClearCache(bool chrome) {
|
2016-06-07 01:23:43 +03:00
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
bool privateLoader = this == gPrivateBrowsingLoader;
|
|
|
|
for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
|
|
|
|
Unused << cp->SendClearImageCache(privateLoader, chrome);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (chrome) {
|
2008-09-05 03:00:42 +04:00
|
|
|
return ClearChromeImageCache();
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2017-02-10 15:52:27 +03:00
|
|
|
return ClearImageCache();
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2018-06-20 18:57:50 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgLoader::RemoveEntriesFromPrincipal(nsIPrincipal* aPrincipal) {
|
|
|
|
nsAutoString origin;
|
|
|
|
nsresult rv = nsContentUtils::GetUTFOrigin(aPrincipal, origin);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
|
|
|
|
|
|
|
|
imgCacheTable& cache =
|
|
|
|
GetCache(nsContentUtils::IsSystemPrincipal(aPrincipal));
|
|
|
|
for (auto iter = cache.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto& key = iter.Key();
|
|
|
|
|
|
|
|
if (key.OriginAttributesRef() !=
|
|
|
|
BasePrincipal::Cast(aPrincipal)->OriginAttributesRef()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString imageOrigin;
|
|
|
|
nsresult rv = nsContentUtils::GetUTFOrigin(key.URI(), imageOrigin);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imageOrigin == origin) {
|
|
|
|
entriesToBeRemoved.AppendElement(iter.Data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& entry : entriesToBeRemoved) {
|
|
|
|
if (!RemoveFromCache(entry)) {
|
|
|
|
NS_WARNING(
|
|
|
|
"Couldn't remove an entry from the cache in "
|
|
|
|
"RemoveEntriesFromPrincipal()\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-11 11:14:09 +03:00
|
|
|
NS_IMETHODIMP
|
2019-01-02 16:05:23 +03:00
|
|
|
imgLoader::RemoveEntry(nsIURI* aURI, Document* aDoc) {
|
2017-04-11 11:14:09 +03:00
|
|
|
if (aURI) {
|
|
|
|
OriginAttributes attrs;
|
2018-05-11 20:46:15 +03:00
|
|
|
if (aDoc) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
|
2017-04-11 11:14:09 +03:00
|
|
|
if (principal) {
|
|
|
|
attrs = principal->OriginAttributesRef();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:04 +03:00
|
|
|
ImageCacheKey key(aURI, attrs, aDoc);
|
|
|
|
if (RemoveFromCache(key)) {
|
2017-04-11 11:14:09 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMETHODIMP
|
2019-01-02 16:05:23 +03:00
|
|
|
imgLoader::FindEntryProperties(nsIURI* uri, Document* aDoc,
|
2015-10-26 23:03:26 +03:00
|
|
|
nsIProperties** _retval) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*_retval = nullptr;
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes attrs;
|
2018-05-11 20:46:15 +03:00
|
|
|
if (aDoc) {
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = aDoc->NodePrincipal();
|
2016-08-04 21:22:00 +03:00
|
|
|
if (principal) {
|
2016-12-19 13:30:29 +03:00
|
|
|
attrs = principal->OriginAttributesRef();
|
2016-08-04 21:22:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:04 +03:00
|
|
|
ImageCacheKey key(uri, attrs, aDoc);
|
2015-04-28 21:45:45 +03:00
|
|
|
imgCacheTable& cache = GetCache(key);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgCacheEntry> entry;
|
2015-04-28 21:45:45 +03:00
|
|
|
if (cache.Get(key, getter_AddRefs(entry)) && entry) {
|
2015-04-08 00:49:00 +03:00
|
|
|
if (mCacheTracker && entry->HasNoProxies()) {
|
2012-06-26 08:20:12 +04:00
|
|
|
mCacheTracker->MarkUsed(entry);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> request = entry->GetRequest();
|
2008-09-05 03:00:42 +04:00
|
|
|
if (request) {
|
2015-03-05 09:18:09 +03:00
|
|
|
nsCOMPtr<nsIProperties> properties = request->Properties();
|
|
|
|
properties.forget(_retval);
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-28 23:01:07 +03:00
|
|
|
NS_IMETHODIMP_(void)
|
2019-01-02 16:05:23 +03:00
|
|
|
imgLoader::ClearCacheForControlledDocument(Document* aDoc) {
|
2015-10-28 23:01:07 +03:00
|
|
|
MOZ_ASSERT(aDoc);
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
|
2015-10-28 23:01:07 +03:00
|
|
|
imgCacheTable& cache = GetCache(false);
|
|
|
|
for (auto iter = cache.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto& key = iter.Key();
|
|
|
|
if (key.ControlledDocument() == aDoc) {
|
|
|
|
entriesToBeRemoved.AppendElement(iter.Data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& entry : entriesToBeRemoved) {
|
|
|
|
if (!RemoveFromCache(entry)) {
|
|
|
|
NS_WARNING(
|
|
|
|
"Couldn't remove an entry from the cache in "
|
|
|
|
"ClearCacheForControlledDocument()\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgLoader::Shutdown() {
|
2016-05-19 15:31:15 +03:00
|
|
|
NS_IF_RELEASE(gNormalLoader);
|
|
|
|
gNormalLoader = nullptr;
|
|
|
|
NS_IF_RELEASE(gPrivateBrowsingLoader);
|
|
|
|
gPrivateBrowsingLoader = nullptr;
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
nsresult imgLoader::ClearChromeImageCache() {
|
2012-06-26 08:20:12 +04:00
|
|
|
return EvictEntries(mChromeCache);
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
nsresult imgLoader::ClearImageCache() { return EvictEntries(mCache); }
|
2009-05-08 00:55:06 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgLoader::MinimizeCaches() {
|
2012-06-26 08:20:12 +04:00
|
|
|
EvictEntries(mCacheQueue);
|
|
|
|
EvictEntries(mChromeCacheQueue);
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
bool imgLoader::PutIntoCache(const ImageCacheKey& aKey, imgCacheEntry* entry) {
|
|
|
|
imgCacheTable& cache = GetCache(aKey);
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::PutIntoCache", "uri",
|
2018-06-06 03:42:57 +03:00
|
|
|
aKey.URI());
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-05-01 23:01:51 +03:00
|
|
|
// Check to see if this request already exists in the cache. If so, we'll
|
|
|
|
// replace the old version.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgCacheEntry> tmpCacheEntry;
|
2015-04-28 21:45:45 +03:00
|
|
|
if (cache.Get(aKey, getter_AddRefs(tmpCacheEntry)) && tmpCacheEntry) {
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
gImgLog, LogLevel::Debug,
|
2015-04-08 00:49:00 +03:00
|
|
|
("[this=%p] imgLoader::PutIntoCache -- Element already in the cache",
|
|
|
|
nullptr));
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> tmpRequest = tmpCacheEntry->GetRequest();
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2008-10-01 01:47:47 +04:00
|
|
|
// If it already exists, and we're putting the same key into the cache, we
|
|
|
|
// should remove the old version.
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(gImgLog, LogLevel::Debug,
|
2015-04-08 00:49:00 +03:00
|
|
|
("[this=%p] imgLoader::PutIntoCache -- Replacing cached element",
|
|
|
|
nullptr));
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
RemoveFromCache(aKey);
|
2008-10-01 01:47:47 +04:00
|
|
|
} else {
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(gImgLog, LogLevel::Debug,
|
2015-04-08 00:49:00 +03:00
|
|
|
("[this=%p] imgLoader::PutIntoCache --"
|
|
|
|
" Element NOT already in the cache",
|
|
|
|
nullptr));
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
cache.Put(aKey, entry);
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2009-03-18 00:07:16 +03:00
|
|
|
// We can be called to resurrect an evicted entry.
|
2015-04-08 00:49:00 +03:00
|
|
|
if (entry->Evicted()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
entry->SetEvicted(false);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-03-18 00:07:16 +03:00
|
|
|
|
|
|
|
// If we're resurrecting an entry with no proxies, put it back in the
|
|
|
|
// tracker and queue.
|
|
|
|
if (entry->HasNoProxies()) {
|
|
|
|
nsresult addrv = NS_OK;
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (mCacheTracker) {
|
2012-06-26 08:20:12 +04:00
|
|
|
addrv = mCacheTracker->AddObject(entry);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-03-18 00:07:16 +03:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(addrv)) {
|
2015-04-28 21:45:45 +03:00
|
|
|
imgCacheQueue& queue = GetCacheQueue(aKey);
|
2009-03-18 00:07:16 +03:00
|
|
|
queue.Push(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> request = entry->GetRequest();
|
2011-10-17 18:59:28 +04:00
|
|
|
request->SetIsInCache(true);
|
2014-09-06 01:36:11 +04:00
|
|
|
RemoveFromUncachedImages(request);
|
2009-03-18 00:07:16 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-01-31 05:17:47 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
bool imgLoader::SetHasNoProxies(imgRequest* aRequest, imgCacheEntry* aEntry) {
|
2015-05-20 20:21:13 +03:00
|
|
|
LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasNoProxies", "uri",
|
2018-06-06 03:42:57 +03:00
|
|
|
aRequest->CacheKey().URI());
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2014-09-11 06:47:00 +04:00
|
|
|
aEntry->SetHasNoProxies(true);
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (aEntry->Evicted()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
imgCacheQueue& queue = GetCacheQueue(aRequest->IsChrome());
|
2009-01-31 05:17:47 +03:00
|
|
|
|
|
|
|
nsresult addrv = NS_OK;
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (mCacheTracker) {
|
2014-09-11 06:47:00 +04:00
|
|
|
addrv = mCacheTracker->AddObject(aEntry);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(addrv)) {
|
2014-09-11 06:47:00 +04:00
|
|
|
queue.Push(aEntry);
|
2009-01-31 05:17:47 +03:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
imgCacheTable& cache = GetCache(aRequest->IsChrome());
|
2008-09-05 03:00:42 +04:00
|
|
|
CheckCacheLimits(cache, queue);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
bool imgLoader::SetHasProxies(imgRequest* aRequest) {
|
2009-01-31 05:17:47 +03:00
|
|
|
VerifyCacheSizes();
|
|
|
|
|
2015-05-20 20:21:13 +03:00
|
|
|
const ImageCacheKey& key = aRequest->CacheKey();
|
2015-04-28 21:45:45 +03:00
|
|
|
imgCacheTable& cache = GetCache(key);
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::SetHasProxies", "uri",
|
2018-06-06 03:42:57 +03:00
|
|
|
key.URI());
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgCacheEntry> entry;
|
2015-04-28 21:45:45 +03:00
|
|
|
if (cache.Get(key, getter_AddRefs(entry)) && entry) {
|
2014-09-11 06:47:00 +04:00
|
|
|
// Make sure the cache entry is for the right request
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> entryRequest = entry->GetRequest();
|
2014-09-11 06:47:00 +04:00
|
|
|
if (entryRequest == aRequest && entry->HasNoProxies()) {
|
2015-04-28 21:45:45 +03:00
|
|
|
imgCacheQueue& queue = GetCacheQueue(key);
|
2014-09-11 06:47:00 +04:00
|
|
|
queue.Remove(entry);
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (mCacheTracker) {
|
2014-09-11 06:47:00 +04:00
|
|
|
mCacheTracker->RemoveObject(entry);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2014-09-11 06:47:00 +04:00
|
|
|
entry->SetHasNoProxies(false);
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2014-09-11 06:47:00 +04:00
|
|
|
return true;
|
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-01-31 05:17:47 +03:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
void imgLoader::CacheEntriesChanged(bool aForChrome,
|
|
|
|
int32_t aSizeDiff /* = 0 */) {
|
|
|
|
imgCacheQueue& queue = GetCacheQueue(aForChrome);
|
2017-09-06 02:20:18 +03:00
|
|
|
// We only need to dirty the queue if there is any sorting
|
|
|
|
// taking place. Empty or single-entry lists can't become
|
|
|
|
// dirty.
|
|
|
|
if (queue.GetNumElements() > 1) {
|
|
|
|
queue.MarkDirty();
|
|
|
|
}
|
2015-04-28 21:45:45 +03:00
|
|
|
queue.UpdateSize(aSizeDiff);
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgLoader::CheckCacheLimits(imgCacheTable& cache, imgCacheQueue& queue) {
|
|
|
|
if (queue.GetNumElements() == 0) {
|
2013-03-30 00:14:19 +04:00
|
|
|
NS_ASSERTION(queue.GetSize() == 0,
|
2008-09-05 03:00:42 +04:00
|
|
|
"imgLoader::CheckCacheLimits -- incorrect cache size");
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2014-07-19 05:47:39 +04:00
|
|
|
// Remove entries from the cache until we're back at our desired max size.
|
|
|
|
while (queue.GetSize() > sCacheMaxSize) {
|
2008-09-05 03:00:42 +04:00
|
|
|
// Remove the first entry in the queue.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgCacheEntry> entry(queue.Pop());
|
2008-09-05 03:00:42 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(entry, "imgLoader::CheckCacheLimits -- NULL entry pointer");
|
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> req = entry->GetRequest();
|
2015-05-11 23:42:30 +03:00
|
|
|
if (req) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::CheckCacheLimits",
|
2018-06-06 03:42:57 +03:00
|
|
|
"entry", req->CacheKey().URI());
|
2015-05-11 23:42:30 +03:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (entry) {
|
2017-09-06 23:12:05 +03:00
|
|
|
// We just popped this entry from the queue, so pass AlreadyRemoved
|
|
|
|
// to avoid searching the queue again in RemoveFromCache.
|
|
|
|
RemoveFromCache(entry, QueueState::AlreadyRemoved);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
bool imgLoader::ValidateRequestWithNewChannel(
|
|
|
|
imgRequest* request, nsIURI* aURI, nsIURI* aInitialDocumentURI,
|
2019-07-25 11:23:32 +03:00
|
|
|
nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
|
|
|
|
imgINotificationObserver* aObserver, nsISupports* aCX,
|
2019-09-17 16:16:19 +03:00
|
|
|
Document* aLoadingDocument, uint64_t aInnerWindowId, nsLoadFlags aLoadFlags,
|
2015-04-08 00:49:00 +03:00
|
|
|
nsContentPolicyType aLoadPolicyType, imgRequestProxy** aProxyRequest,
|
2017-10-03 06:38:30 +03:00
|
|
|
nsIPrincipal* aTriggeringPrincipal, int32_t aCORSMode,
|
2018-08-01 07:35:24 +03:00
|
|
|
bool* aNewChannelCreated) {
|
2019-09-12 12:00:52 +03:00
|
|
|
// now we need to insert a new channel request object in between the real
|
2008-09-05 03:00:42 +04:00
|
|
|
// request and the proxy that basically delays loading the image until it
|
|
|
|
// gets a 304 or figures out that this needs to be a new request
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2009-01-31 05:17:47 +03:00
|
|
|
// If we're currently in the middle of validating this request, just hand
|
|
|
|
// back a proxy to it; the required work will be done for us.
|
2015-03-24 05:37:45 +03:00
|
|
|
if (request->GetValidator()) {
|
2017-07-19 21:15:11 +03:00
|
|
|
rv = CreateNewProxyForRequest(request, aLoadGroup, aLoadingDocument,
|
|
|
|
aObserver, aLoadFlags, aProxyRequest);
|
2010-05-25 18:34:04 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-25 18:34:04 +04:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2010-08-02 23:44:49 +04:00
|
|
|
if (*aProxyRequest) {
|
|
|
|
imgRequestProxy* proxy = static_cast<imgRequestProxy*>(*aProxyRequest);
|
|
|
|
|
|
|
|
// We will send notifications from imgCacheValidator::OnStartRequest().
|
|
|
|
// In the mean time, we must defer notifications because we are added to
|
|
|
|
// the imgRequest's proxy list, and we can get extra notifications
|
2015-10-30 02:37:42 +03:00
|
|
|
// resulting from methods such as StartDecoding(). See bug 579122.
|
2018-02-07 15:27:27 +03:00
|
|
|
proxy->MarkValidating();
|
2010-08-02 23:44:49 +04:00
|
|
|
|
|
|
|
// Attach the proxy without notifying
|
2015-03-24 05:37:45 +03:00
|
|
|
request->GetValidator()->AddProxy(proxy);
|
2010-08-02 23:44:49 +04:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
|
|
|
return NS_SUCCEEDED(rv);
|
2017-02-10 15:52:27 +03:00
|
|
|
}
|
|
|
|
// We will rely on Necko to cache this request when it's possible, and to
|
|
|
|
// tell imgCacheValidator::OnStartRequest whether the request came from its
|
|
|
|
// cache.
|
|
|
|
nsCOMPtr<nsIChannel> newChannel;
|
|
|
|
bool forcePrincipalCheck;
|
|
|
|
rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
|
2019-07-25 11:23:32 +03:00
|
|
|
aInitialDocumentURI, aCORSMode, aReferrerInfo,
|
|
|
|
aLoadGroup, mAcceptHeader, aLoadFlags, aLoadPolicyType,
|
|
|
|
aTriggeringPrincipal, aCX, mRespectPrivacy);
|
2017-02-10 15:52:27 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2018-08-01 07:35:24 +03:00
|
|
|
if (aNewChannelCreated) {
|
|
|
|
*aNewChannelCreated = true;
|
|
|
|
}
|
|
|
|
|
2017-02-10 15:52:27 +03:00
|
|
|
RefPtr<imgRequestProxy> req;
|
2017-07-19 21:15:11 +03:00
|
|
|
rv = CreateNewProxyForRequest(request, aLoadGroup, aLoadingDocument,
|
|
|
|
aObserver, aLoadFlags, getter_AddRefs(req));
|
2017-02-10 15:52:27 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-28 20:13:48 +04:00
|
|
|
|
2017-02-10 15:52:27 +03:00
|
|
|
// Make sure that OnStatus/OnProgress calls have the right request set...
|
|
|
|
RefPtr<nsProgressNotificationProxy> progressproxy =
|
|
|
|
new nsProgressNotificationProxy(newChannel, req);
|
|
|
|
if (!progressproxy) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-14 22:47:34 +04:00
|
|
|
|
2017-02-10 15:52:27 +03:00
|
|
|
RefPtr<imgCacheValidator> hvc = new imgCacheValidator(
|
2019-09-17 16:16:19 +03:00
|
|
|
progressproxy, this, request, aCX, aInnerWindowId, forcePrincipalCheck);
|
2011-07-14 22:47:34 +04:00
|
|
|
|
2017-02-10 15:52:27 +03:00
|
|
|
// Casting needed here to get past multiple inheritance.
|
|
|
|
nsCOMPtr<nsIStreamListener> listener =
|
|
|
|
do_QueryInterface(static_cast<nsIThreadRetargetableStreamListener*>(hvc));
|
|
|
|
NS_ENSURE_TRUE(listener, false);
|
2012-03-02 09:13:54 +04:00
|
|
|
|
2017-02-10 15:52:27 +03:00
|
|
|
// We must set the notification callbacks before setting up the
|
|
|
|
// CORS listener, because that's also interested inthe
|
|
|
|
// notification callbacks.
|
|
|
|
newChannel->SetNotificationCallbacks(hvc);
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2017-02-10 15:52:27 +03:00
|
|
|
request->SetValidator(hvc);
|
2010-08-02 23:44:49 +04:00
|
|
|
|
2017-02-10 15:52:27 +03:00
|
|
|
// We will send notifications from imgCacheValidator::OnStartRequest().
|
|
|
|
// In the mean time, we must defer notifications because we are added to
|
|
|
|
// the imgRequest's proxy list, and we can get extra notifications
|
|
|
|
// resulting from methods such as StartDecoding(). See bug 579122.
|
2018-02-07 15:27:27 +03:00
|
|
|
req->MarkValidating();
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2017-02-10 15:52:27 +03:00
|
|
|
// Add the proxy without notifying
|
|
|
|
hvc->AddProxy(req);
|
2013-10-26 01:56:58 +04:00
|
|
|
|
2017-02-10 15:52:27 +03:00
|
|
|
mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
|
|
|
|
nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
|
|
|
|
aLoadGroup);
|
2019-02-12 19:08:25 +03:00
|
|
|
rv = newChannel->AsyncOpen(listener);
|
2017-02-10 15:52:27 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
Bug 1267075 - Part 3: cancel imgRequestProxy if asyncOpen2 failed. r=tnikkel
The problem is if we found a cache hit, then we could go through
ValidateRequestWithNewChannel to validate the cache.
Then if the CSP check fail(asyncOpen2() will fail), then the
imgRequestProxy will remain there, and cause the timeout.
I run into problem when running mochitest
browser/base/content/test/general/browser_aboutHome.js in non-e10s mode.
In the beginning, browser.xul will load defaultFavicon.png, will create
an image cache there.
Next time when the test starts to run, when it loads about:home, then it
will try to load defaultFavicon.png, it will found an image cache hit
(loaded previously by browser.xul), and call
ValidateRequestWithNewChannel there, however the asyncOpen2 call failed,
and the imgRequestProxy is added to the loadGroup of about:home, and
never be notified until timeout.
2017-01-20 06:32:08 +03:00
|
|
|
req->CancelAndForgetObserver(rv);
|
2017-02-10 15:52:27 +03:00
|
|
|
return false;
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
2017-02-10 15:52:27 +03:00
|
|
|
|
|
|
|
req.forget(aProxyRequest);
|
|
|
|
return true;
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
bool imgLoader::ValidateEntry(
|
|
|
|
imgCacheEntry* aEntry, nsIURI* aURI, nsIURI* aInitialDocumentURI,
|
2019-07-25 11:23:32 +03:00
|
|
|
nsIReferrerInfo* aReferrerInfo, nsILoadGroup* aLoadGroup,
|
|
|
|
imgINotificationObserver* aObserver, nsISupports* aCX,
|
|
|
|
Document* aLoadingDocument, nsLoadFlags aLoadFlags,
|
2015-04-08 00:49:00 +03:00
|
|
|
nsContentPolicyType aLoadPolicyType, bool aCanMakeNewChannel,
|
|
|
|
bool* aNewChannelCreated, imgRequestProxy** aProxyRequest,
|
2017-10-03 06:38:30 +03:00
|
|
|
nsIPrincipal* aTriggeringPrincipal, int32_t aCORSMode) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_SCOPE(gImgLog, "imgLoader::ValidateEntry");
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2017-12-07 16:28:28 +03:00
|
|
|
// If the expiration time is zero, then the request has not gotten far enough
|
|
|
|
// to know when it will expire.
|
|
|
|
uint32_t expiryTime = aEntry->GetExpiryTime();
|
|
|
|
bool hasExpired = expiryTime != 0 &&
|
|
|
|
expiryTime <= imgCacheEntry::SecondsFromPRTime(PR_Now());
|
2008-09-05 03:00:42 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Special treatment for file URLs - aEntry has expired if file has changed
|
|
|
|
nsCOMPtr<nsIFileURL> fileUrl(do_QueryInterface(aURI));
|
|
|
|
if (fileUrl) {
|
2015-10-20 08:15:25 +03:00
|
|
|
uint32_t lastModTime = aEntry->GetLoadTime();
|
2008-09-05 03:00:42 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> theFile;
|
|
|
|
rv = fileUrl->GetFile(getter_AddRefs(theFile));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-08-30 11:10:35 +04:00
|
|
|
PRTime fileLastMod;
|
2008-09-05 03:00:42 +04:00
|
|
|
rv = theFile->GetLastModifiedTime(&fileLastMod);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// nsIFile uses millisec, NSPR usec
|
|
|
|
fileLastMod *= 1000;
|
2017-12-07 16:28:28 +03:00
|
|
|
hasExpired =
|
|
|
|
imgCacheEntry::SecondsFromPRTime((PRTime)fileLastMod) > lastModTime;
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> request(aEntry->GetRequest());
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (!request) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2014-11-18 16:46:53 +03:00
|
|
|
if (!ValidateSecurityInfo(request, aEntry->ForcePrincipalCheck(), aCORSMode,
|
2017-10-03 06:38:30 +03:00
|
|
|
aTriggeringPrincipal, aCX, aLoadPolicyType,
|
2019-07-25 11:23:32 +03:00
|
|
|
aReferrerInfo))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-07-14 22:47:34 +04:00
|
|
|
|
2014-10-22 13:48:36 +04:00
|
|
|
// data URIs are immutable and by their nature can't leak data, so we can
|
|
|
|
// just return true in that case. Doing so would mean that shift-reload
|
|
|
|
// doesn't reload data URI documents/images though (which is handy for
|
|
|
|
// debugging during gecko development) so we make an exception in that case.
|
2012-11-21 04:58:32 +04:00
|
|
|
nsAutoCString scheme;
|
|
|
|
aURI->GetScheme(scheme);
|
2014-10-22 13:48:36 +04:00
|
|
|
if (scheme.EqualsLiteral("data") &&
|
|
|
|
!(aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE)) {
|
2012-11-21 04:58:32 +04:00
|
|
|
return true;
|
2014-10-22 13:48:36 +04:00
|
|
|
}
|
2012-11-21 04:58:32 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool validateRequest = false;
|
2008-09-05 03:00:42 +04:00
|
|
|
|
|
|
|
// If the request's loadId is the same as the aCX, then it is ok to use
|
|
|
|
// this one because it has already been validated for this context.
|
|
|
|
//
|
2012-07-30 18:20:58 +04:00
|
|
|
// XXX: nullptr seems to be a 'special' key value that indicates that NO
|
2008-09-05 03:00:42 +04:00
|
|
|
// validation is required.
|
2018-10-02 18:05:21 +03:00
|
|
|
// XXX: we also check the window ID because the loadID() can return a reused
|
|
|
|
// pointer of a document. This can still happen for non-document image
|
|
|
|
// cache entries.
|
2015-04-08 00:49:00 +03:00
|
|
|
void* key = (void*)aCX;
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = do_QueryInterface(aCX);
|
2018-10-02 18:05:21 +03:00
|
|
|
uint64_t innerWindowID = doc ? doc->InnerWindowID() : 0;
|
|
|
|
if (request->LoadId() != key || request->InnerWindowID() != innerWindowID) {
|
2009-11-13 02:00:30 +03:00
|
|
|
// If we would need to revalidate this entry, but we're being told to
|
|
|
|
// bypass the cache, we don't allow this entry to be used.
|
2015-04-08 00:49:00 +03:00
|
|
|
if (aLoadFlags & nsIRequest::LOAD_BYPASS_CACHE) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-11-13 02:00:30 +03:00
|
|
|
|
2016-04-02 21:53:12 +03:00
|
|
|
if (MOZ_UNLIKELY(ChaosMode::isActive(ChaosFeature::ImageCache))) {
|
|
|
|
if (ChaosMode::randomUint32LessThan(4) < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
// Determine whether the cache aEntry must be revalidated...
|
|
|
|
validateRequest = ShouldRevalidateEntry(aEntry, aLoadFlags, hasExpired);
|
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(gImgLog, LogLevel::Debug,
|
2013-03-30 00:14:19 +04:00
|
|
|
("imgLoader::ValidateEntry validating cache entry. "
|
2008-09-05 03:00:42 +04:00
|
|
|
"validateRequest = %d",
|
|
|
|
validateRequest));
|
2015-11-16 20:21:00 +03:00
|
|
|
} else if (!key && MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
|
|
|
|
MOZ_LOG(gImgLog, LogLevel::Debug,
|
2013-03-30 00:14:19 +04:00
|
|
|
("imgLoader::ValidateEntry BYPASSING cache validation for %s "
|
2016-08-26 09:02:31 +03:00
|
|
|
"because of NULL LoadID",
|
|
|
|
aURI->GetSpecOrDefault().get()));
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2009-01-05 08:52:22 +03:00
|
|
|
// We can't use a cached request if it comes from a different
|
|
|
|
// application cache than this load is expecting.
|
|
|
|
nsCOMPtr<nsIApplicationCacheContainer> appCacheContainer;
|
|
|
|
nsCOMPtr<nsIApplicationCache> requestAppCache;
|
|
|
|
nsCOMPtr<nsIApplicationCache> groupAppCache;
|
2015-03-24 05:37:45 +03:00
|
|
|
if ((appCacheContainer = do_GetInterface(request->GetRequest()))) {
|
2009-01-05 08:52:22 +03:00
|
|
|
appCacheContainer->GetApplicationCache(getter_AddRefs(requestAppCache));
|
2015-03-24 05:37:45 +03:00
|
|
|
}
|
|
|
|
if ((appCacheContainer = do_QueryInterface(aLoadGroup))) {
|
2009-01-05 08:52:22 +03:00
|
|
|
appCacheContainer->GetApplicationCache(getter_AddRefs(groupAppCache));
|
2015-03-24 05:37:45 +03:00
|
|
|
}
|
2009-01-05 08:52:22 +03:00
|
|
|
|
|
|
|
if (requestAppCache != groupAppCache) {
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(gImgLog, LogLevel::Debug,
|
2009-01-05 08:52:22 +03:00
|
|
|
("imgLoader::ValidateEntry - Unable to use cached imgRequest "
|
|
|
|
"[request=%p] because of mismatched application caches\n",
|
|
|
|
address_of(request)));
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-01-05 08:52:22 +03:00
|
|
|
}
|
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
if (validateRequest && aCanMakeNewChannel) {
|
2015-04-08 00:49:00 +03:00
|
|
|
LOG_SCOPE(gImgLog, "imgLoader::ValidateRequest |cache hit| must validate");
|
2008-09-05 03:00:42 +04:00
|
|
|
|
|
|
|
return ValidateRequestWithNewChannel(
|
2019-07-25 11:23:32 +03:00
|
|
|
request, aURI, aInitialDocumentURI, aReferrerInfo, aLoadGroup,
|
2019-09-17 16:16:19 +03:00
|
|
|
aObserver, aCX, aLoadingDocument, innerWindowID, aLoadFlags,
|
|
|
|
aLoadPolicyType, aProxyRequest, aTriggeringPrincipal, aCORSMode,
|
|
|
|
aNewChannelCreated);
|
2011-07-14 22:47:34 +04:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
|
|
|
return !validateRequest;
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
bool imgLoader::RemoveFromCache(const ImageCacheKey& aKey) {
|
2018-06-06 03:42:57 +03:00
|
|
|
LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache", "uri",
|
|
|
|
aKey.URI());
|
2013-09-28 22:28:42 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
imgCacheTable& cache = GetCache(aKey);
|
|
|
|
imgCacheQueue& queue = GetCacheQueue(aKey);
|
2013-09-28 22:28:42 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgCacheEntry> entry;
|
2017-06-14 02:54:26 +03:00
|
|
|
cache.Remove(aKey, getter_AddRefs(entry));
|
|
|
|
if (entry) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!entry->Evicted(), "Evicting an already-evicted cache entry!");
|
2009-01-31 05:17:47 +03:00
|
|
|
|
|
|
|
// Entries with no proxies are in the tracker.
|
|
|
|
if (entry->HasNoProxies()) {
|
2015-04-08 00:49:00 +03:00
|
|
|
if (mCacheTracker) {
|
2012-06-26 08:20:12 +04:00
|
|
|
mCacheTracker->RemoveObject(entry);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
queue.Remove(entry);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
entry->SetEvicted(true);
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> request = entry->GetRequest();
|
2011-10-17 18:59:28 +04:00
|
|
|
request->SetIsInCache(false);
|
2014-09-06 01:36:11 +04:00
|
|
|
AddToUncachedImages(request);
|
2009-03-18 00:07:16 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2017-02-10 15:52:27 +03:00
|
|
|
return false;
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2017-09-06 23:12:05 +03:00
|
|
|
bool imgLoader::RemoveFromCache(imgCacheEntry* entry, QueueState aQueueState) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_STATIC_FUNC(gImgLog, "imgLoader::RemoveFromCache entry");
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> request = entry->GetRequest();
|
2008-09-05 03:00:42 +04:00
|
|
|
if (request) {
|
2015-05-20 20:21:13 +03:00
|
|
|
const ImageCacheKey& key = request->CacheKey();
|
|
|
|
imgCacheTable& cache = GetCache(key);
|
|
|
|
imgCacheQueue& queue = GetCacheQueue(key);
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_STATIC_FUNC_WITH_PARAM(gImgLog, "imgLoader::RemoveFromCache",
|
2015-05-20 20:21:13 +03:00
|
|
|
"entry's uri", key.URI());
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-05-20 20:21:13 +03:00
|
|
|
cache.Remove(key);
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-05-20 20:21:13 +03:00
|
|
|
if (entry->HasNoProxies()) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_STATIC_FUNC(gImgLog,
|
2015-05-20 20:21:13 +03:00
|
|
|
"imgLoader::RemoveFromCache removing from tracker");
|
|
|
|
if (mCacheTracker) {
|
|
|
|
mCacheTracker->RemoveObject(entry);
|
2009-01-31 05:17:47 +03:00
|
|
|
}
|
2017-09-06 23:12:05 +03:00
|
|
|
// Only search the queue to remove the entry if its possible it might
|
|
|
|
// be in the queue. If we know its not in the queue this would be
|
|
|
|
// wasted work.
|
|
|
|
MOZ_ASSERT_IF(aQueueState == QueueState::AlreadyRemoved,
|
|
|
|
!queue.Contains(entry));
|
|
|
|
if (aQueueState == QueueState::MaybeExists) {
|
|
|
|
queue.Remove(entry);
|
|
|
|
}
|
2015-05-20 20:21:13 +03:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-05-20 20:21:13 +03:00
|
|
|
entry->SetEvicted(true);
|
|
|
|
request->SetIsInCache(false);
|
|
|
|
AddToUncachedImages(request);
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-05-20 20:21:13 +03:00
|
|
|
return true;
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-01-31 05:17:47 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
nsresult imgLoader::EvictEntries(imgCacheTable& aCacheToClear) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_STATIC_FUNC(gImgLog, "imgLoader::EvictEntries table");
|
2008-09-05 03:00:42 +04:00
|
|
|
|
|
|
|
// We have to make a temporary, since RemoveFromCache removes the element
|
|
|
|
// from the queue, invalidating iterators.
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<imgCacheEntry> > entries;
|
2015-11-11 08:30:02 +03:00
|
|
|
for (auto iter = aCacheToClear.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
RefPtr<imgCacheEntry>& data = iter.Data();
|
|
|
|
entries.AppendElement(data);
|
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
for (uint32_t i = 0; i < entries.Length(); ++i) {
|
|
|
|
if (!RemoveFromCache(entries[i])) {
|
2008-09-05 03:00:42 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
MOZ_ASSERT(aCacheToClear.Count() == 0);
|
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
nsresult imgLoader::EvictEntries(imgCacheQueue& aQueueToClear) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_STATIC_FUNC(gImgLog, "imgLoader::EvictEntries queue");
|
2009-05-08 00:55:06 +04:00
|
|
|
|
|
|
|
// We have to make a temporary, since RemoveFromCache removes the element
|
|
|
|
// from the queue, invalidating iterators.
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<imgCacheEntry> > entries(aQueueToClear.GetNumElements());
|
2017-09-06 02:20:18 +03:00
|
|
|
for (auto i = aQueueToClear.begin(); i != aQueueToClear.end(); ++i) {
|
2009-05-08 00:55:06 +04:00
|
|
|
entries.AppendElement(*i);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-05-08 00:55:06 +04:00
|
|
|
|
2017-09-06 02:20:18 +03:00
|
|
|
// Iterate in reverse order to minimize array copying.
|
|
|
|
for (auto& entry : entries) {
|
|
|
|
if (!RemoveFromCache(entry)) {
|
2009-05-08 00:55:06 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
|
|
|
}
|
2009-05-08 00:55:06 +04:00
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
MOZ_ASSERT(aQueueToClear.GetNumElements() == 0);
|
|
|
|
|
2009-05-08 00:55:06 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgLoader::AddToUncachedImages(imgRequest* aRequest) {
|
2014-09-06 01:36:11 +04:00
|
|
|
MutexAutoLock lock(mUncachedImagesMutex);
|
|
|
|
mUncachedImages.PutEntry(aRequest);
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgLoader::RemoveFromUncachedImages(imgRequest* aRequest) {
|
2014-09-06 01:36:11 +04:00
|
|
|
MutexAutoLock lock(mUncachedImagesMutex);
|
|
|
|
mUncachedImages.RemoveEntry(aRequest);
|
|
|
|
}
|
|
|
|
|
2019-05-16 21:55:19 +03:00
|
|
|
bool imgLoader::PreferLoadFromCache(nsIURI* aURI) const {
|
|
|
|
// If we are trying to load an image from a protocol that doesn't support
|
|
|
|
// caching (e.g. thumbnails via the moz-page-thumb:// protocol, or icons via
|
|
|
|
// the moz-extension:// protocol), load it directly from the cache to prevent
|
|
|
|
// re-decoding the image. See Bug 1373258.
|
|
|
|
// TODO: Bug 1406134
|
2019-08-03 12:44:12 +03:00
|
|
|
return aURI->SchemeIs("moz-page-thumb") || aURI->SchemeIs("moz-extension");
|
2019-05-16 21:55:19 +03:00
|
|
|
}
|
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
#define LOAD_FLAGS_CACHE_MASK \
|
|
|
|
(nsIRequest::LOAD_BYPASS_CACHE | nsIRequest::LOAD_FROM_CACHE)
|
2014-09-06 01:36:11 +04:00
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
#define LOAD_FLAGS_VALIDATE_MASK \
|
|
|
|
(nsIRequest::VALIDATE_ALWAYS | nsIRequest::VALIDATE_NEVER | \
|
|
|
|
nsIRequest::VALIDATE_ONCE_PER_SESSION)
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMETHODIMP
|
2019-07-25 11:23:32 +03:00
|
|
|
imgLoader::LoadImageXPCOM(nsIURI* aURI, nsIURI* aInitialDocumentURI,
|
|
|
|
nsIReferrerInfo* aReferrerInfo,
|
|
|
|
nsIPrincipal* aTriggeringPrincipal,
|
|
|
|
nsILoadGroup* aLoadGroup,
|
|
|
|
imgINotificationObserver* aObserver, nsISupports* aCX,
|
|
|
|
nsLoadFlags aLoadFlags, nsISupports* aCacheKey,
|
|
|
|
nsContentPolicyType aContentPolicyType,
|
|
|
|
imgIRequest** _retval) {
|
2014-08-20 01:49:38 +04:00
|
|
|
// Optional parameter, so defaults to 0 (== TYPE_INVALID)
|
|
|
|
if (!aContentPolicyType) {
|
2015-09-21 00:55:59 +03:00
|
|
|
aContentPolicyType = nsIContentPolicy::TYPE_INTERNAL_IMAGE;
|
2014-08-20 01:49:38 +04:00
|
|
|
}
|
2015-04-08 00:49:00 +03:00
|
|
|
imgRequestProxy* proxy;
|
2016-04-11 11:58:03 +03:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aCX);
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = do_QueryInterface(aCX);
|
2014-11-18 16:46:53 +03:00
|
|
|
nsresult rv =
|
2019-07-25 11:23:32 +03:00
|
|
|
LoadImage(aURI, aInitialDocumentURI, aReferrerInfo, aTriggeringPrincipal,
|
|
|
|
0, aLoadGroup, aObserver, node, doc, aLoadFlags, aCacheKey,
|
|
|
|
aContentPolicyType, EmptyString(),
|
2017-04-25 04:17:38 +03:00
|
|
|
/* aUseUrgentStartForChannel */ false, &proxy);
|
2012-10-12 16:43:01 +04:00
|
|
|
*_retval = proxy;
|
2014-11-18 16:46:53 +03:00
|
|
|
return rv;
|
2012-10-12 16:43:01 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
nsresult imgLoader::LoadImage(
|
2019-07-25 11:23:32 +03:00
|
|
|
nsIURI* aURI, nsIURI* aInitialDocumentURI, nsIReferrerInfo* aReferrerInfo,
|
|
|
|
nsIPrincipal* aTriggeringPrincipal, uint64_t aRequestContextID,
|
|
|
|
nsILoadGroup* aLoadGroup, imgINotificationObserver* aObserver,
|
|
|
|
nsINode* aContext, Document* aLoadingDocument, nsLoadFlags aLoadFlags,
|
|
|
|
nsISupports* aCacheKey, nsContentPolicyType aContentPolicyType,
|
|
|
|
const nsAString& initiatorType, bool aUseUrgentStartForChannel,
|
|
|
|
imgRequestProxy** _retval) {
|
2014-08-20 01:49:38 +04:00
|
|
|
VerifyCacheSizes();
|
2004-05-06 01:17:31 +04:00
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
NS_ASSERTION(aURI, "imgLoader::LoadImage -- NULL URI pointer");
|
2008-09-04 20:36:27 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (!aURI) {
|
2001-03-30 05:59:03 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2001-03-30 05:59:03 +04:00
|
|
|
|
2018-11-05 21:13:39 +03:00
|
|
|
#ifdef MOZ_GECKO_PROFILER
|
|
|
|
AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("imgLoader::LoadImage", NETWORK,
|
|
|
|
aURI->GetSpecOrDefault());
|
|
|
|
#endif
|
|
|
|
|
2018-06-06 03:42:57 +03:00
|
|
|
LOG_SCOPE_WITH_PARAM(gImgLog, "imgLoader::LoadImage", "aURI", aURI);
|
2001-02-23 11:36:51 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*_retval = nullptr;
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> request;
|
2001-02-21 01:43:56 +03:00
|
|
|
|
2001-10-17 09:57:29 +04:00
|
|
|
nsresult rv;
|
2002-09-19 02:08:12 +04:00
|
|
|
nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
|
|
|
|
|
2012-06-26 08:20:12 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool isPrivate = false;
|
|
|
|
|
2017-07-17 16:38:10 +03:00
|
|
|
if (aLoadingDocument) {
|
|
|
|
isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadingDocument);
|
|
|
|
} else if (aLoadGroup) {
|
|
|
|
isPrivate = nsContentUtils::IsInPrivateBrowsing(aLoadGroup);
|
2012-06-26 08:20:12 +04:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(isPrivate == mRespectPrivacy);
|
2017-11-01 13:59:09 +03:00
|
|
|
|
|
|
|
if (aLoadingDocument) {
|
|
|
|
// The given load group should match that of the document if given. If
|
|
|
|
// that isn't the case, then we need to add more plumbing to ensure we
|
|
|
|
// block the document as well.
|
|
|
|
nsCOMPtr<nsILoadGroup> docLoadGroup =
|
|
|
|
aLoadingDocument->GetDocumentLoadGroup();
|
|
|
|
MOZ_ASSERT(docLoadGroup == aLoadGroup);
|
|
|
|
}
|
2012-06-26 08:20:12 +04:00
|
|
|
#endif
|
|
|
|
|
2002-09-19 02:08:12 +04:00
|
|
|
// Get the default load flags from the loadgroup (if possible)...
|
|
|
|
if (aLoadGroup) {
|
|
|
|
aLoadGroup->GetLoadFlags(&requestFlags);
|
2019-05-16 21:55:19 +03:00
|
|
|
if (PreferLoadFromCache(aURI)) {
|
2017-06-05 17:04:06 +03:00
|
|
|
requestFlags |= nsIRequest::LOAD_FROM_CACHE;
|
|
|
|
}
|
2002-09-19 02:08:12 +04:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// Merge the default load flags with those passed in via aLoadFlags.
|
|
|
|
// Currently, *only* the caching, validation and background load flags
|
|
|
|
// are merged...
|
|
|
|
//
|
2008-09-05 03:00:42 +04:00
|
|
|
// The flags in aLoadFlags take precedence over the default flags!
|
2002-09-19 02:08:12 +04:00
|
|
|
//
|
|
|
|
if (aLoadFlags & LOAD_FLAGS_CACHE_MASK) {
|
|
|
|
// Override the default caching flags...
|
|
|
|
requestFlags = (requestFlags & ~LOAD_FLAGS_CACHE_MASK) |
|
|
|
|
(aLoadFlags & LOAD_FLAGS_CACHE_MASK);
|
|
|
|
}
|
|
|
|
if (aLoadFlags & LOAD_FLAGS_VALIDATE_MASK) {
|
|
|
|
// Override the default validation flags...
|
|
|
|
requestFlags = (requestFlags & ~LOAD_FLAGS_VALIDATE_MASK) |
|
|
|
|
(aLoadFlags & LOAD_FLAGS_VALIDATE_MASK);
|
|
|
|
}
|
|
|
|
if (aLoadFlags & nsIRequest::LOAD_BACKGROUND) {
|
|
|
|
// Propagate background loading...
|
|
|
|
requestFlags |= nsIRequest::LOAD_BACKGROUND;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t corsmode = imgIRequest::CORS_NONE;
|
2011-07-14 22:47:34 +04:00
|
|
|
if (aLoadFlags & imgILoader::LOAD_CORS_ANONYMOUS) {
|
|
|
|
corsmode = imgIRequest::CORS_ANONYMOUS;
|
|
|
|
} else if (aLoadFlags & imgILoader::LOAD_CORS_USE_CREDENTIALS) {
|
|
|
|
corsmode = imgIRequest::CORS_USE_CREDENTIALS;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgCacheEntry> entry;
|
2001-07-17 05:14:40 +04:00
|
|
|
|
2009-11-13 02:00:30 +03:00
|
|
|
// Look in the cache for our URI, and then validate it.
|
|
|
|
// XXX For now ignore aCacheKey. We will need it in the future
|
|
|
|
// for correctly dealing with image load requests that are a result
|
|
|
|
// of post data.
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes attrs;
|
2017-10-03 06:38:30 +03:00
|
|
|
if (aTriggeringPrincipal) {
|
|
|
|
attrs = aTriggeringPrincipal->OriginAttributesRef();
|
2016-08-04 21:22:00 +03:00
|
|
|
}
|
2019-02-22 17:25:04 +03:00
|
|
|
ImageCacheKey key(aURI, attrs, aLoadingDocument);
|
2015-04-28 21:45:45 +03:00
|
|
|
imgCacheTable& cache = GetCache(key);
|
2008-12-12 03:47:47 +03:00
|
|
|
|
2015-04-28 21:45:45 +03:00
|
|
|
if (cache.Get(key, getter_AddRefs(entry)) && entry) {
|
2018-08-01 07:35:24 +03:00
|
|
|
bool newChannelCreated = false;
|
2019-07-25 11:23:32 +03:00
|
|
|
if (ValidateEntry(entry, aURI, aInitialDocumentURI, aReferrerInfo,
|
|
|
|
aLoadGroup, aObserver, ToSupports(aLoadingDocument),
|
|
|
|
aLoadingDocument, requestFlags, aContentPolicyType, true,
|
|
|
|
&newChannelCreated, _retval, aTriggeringPrincipal,
|
|
|
|
corsmode)) {
|
2013-07-10 13:56:21 +04:00
|
|
|
request = entry->GetRequest();
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
// If this entry has no proxies, its request has no reference to the
|
|
|
|
// entry.
|
2009-11-13 02:00:30 +03:00
|
|
|
if (entry->HasNoProxies()) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog,
|
2018-06-06 03:42:57 +03:00
|
|
|
"imgLoader::LoadImage() adding proxyless entry",
|
|
|
|
"uri", key.URI());
|
2015-04-08 00:49:00 +03:00
|
|
|
MOZ_ASSERT(!request->HasCacheEntry(),
|
|
|
|
"Proxyless entry's request has cache entry!");
|
2009-11-13 02:00:30 +03:00
|
|
|
request->SetCacheEntry(entry);
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2017-05-30 22:49:26 +03:00
|
|
|
if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
|
2012-06-26 08:20:12 +04:00
|
|
|
mCacheTracker->MarkUsed(entry);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2013-03-30 00:14:19 +04:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2009-11-13 02:00:30 +03:00
|
|
|
entry->Touch();
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2018-08-01 07:35:24 +03:00
|
|
|
if (!newChannelCreated) {
|
|
|
|
// This is ugly but it's needed to report CSP violations. We have 3
|
|
|
|
// scenarios:
|
|
|
|
// - we don't have cache. We are not in this if() stmt. A new channel is
|
|
|
|
// created and that triggers the CSP checks.
|
|
|
|
// - We have a cache entry and this is blocked by CSP directives.
|
2018-12-26 05:32:55 +03:00
|
|
|
DebugOnly<bool> shouldLoad =
|
|
|
|
ShouldLoadCachedImage(request, ToSupports(aLoadingDocument),
|
|
|
|
aTriggeringPrincipal, aContentPolicyType,
|
|
|
|
/* aSendCSPViolationReports */ true);
|
2018-08-01 07:35:24 +03:00
|
|
|
MOZ_ASSERT(shouldLoad);
|
|
|
|
}
|
2015-04-08 00:49:00 +03:00
|
|
|
} else {
|
2009-11-13 02:00:30 +03:00
|
|
|
// We can't use this entry. We'll try to load it off the network, and if
|
|
|
|
// successful, overwrite the old entry in the cache with a new one.
|
2012-07-30 18:20:58 +04:00
|
|
|
entry = nullptr;
|
2001-03-06 07:46:20 +03:00
|
|
|
}
|
2001-10-17 09:57:29 +04:00
|
|
|
}
|
|
|
|
|
2009-07-28 20:13:48 +04:00
|
|
|
// Keep the channel in this scope, so we can adjust its notificationCallbacks
|
|
|
|
// later when we create the proxy.
|
|
|
|
nsCOMPtr<nsIChannel> newChannel;
|
2008-09-05 03:00:42 +04:00
|
|
|
// If we didn't get a cache hit, we need to load from the network.
|
|
|
|
if (!request) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_SCOPE(gImgLog, "imgLoader::LoadImage |cache miss|");
|
2001-02-23 11:36:51 +03:00
|
|
|
|
2011-09-21 01:00:42 +04:00
|
|
|
bool forcePrincipalCheck;
|
2002-09-19 02:08:12 +04:00
|
|
|
rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
|
2019-07-25 11:23:32 +03:00
|
|
|
aInitialDocumentURI, corsmode, aReferrerInfo,
|
|
|
|
aLoadGroup, mAcceptHeader, requestFlags,
|
|
|
|
aContentPolicyType, aTriggeringPrincipal, aContext,
|
|
|
|
mRespectPrivacy);
|
2015-04-08 00:49:00 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2002-09-19 02:08:12 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2012-10-04 02:43:26 +04:00
|
|
|
MOZ_ASSERT(NS_UsePrivateBrowsing(newChannel) == mRespectPrivacy);
|
2012-06-26 08:20:12 +04:00
|
|
|
|
2015-05-20 20:21:13 +03:00
|
|
|
NewRequestAndEntry(forcePrincipalCheck, this, key, getter_AddRefs(request),
|
2015-04-08 00:49:00 +03:00
|
|
|
getter_AddRefs(entry));
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(gImgLog, LogLevel::Debug,
|
2015-04-08 00:49:00 +03:00
|
|
|
("[this=%p] imgLoader::LoadImage -- Created new imgRequest"
|
|
|
|
" [request=%p]\n",
|
|
|
|
this, request.get()));
|
2001-03-20 00:57:39 +03:00
|
|
|
|
2017-04-25 04:17:38 +03:00
|
|
|
nsCOMPtr<nsIClassOfService> cos(do_QueryInterface(newChannel));
|
2017-09-21 03:09:00 +03:00
|
|
|
if (cos) {
|
|
|
|
if (aUseUrgentStartForChannel) {
|
|
|
|
cos->AddClassFlags(nsIClassOfService::UrgentStart);
|
|
|
|
}
|
|
|
|
|
2019-05-02 00:10:31 +03:00
|
|
|
if (StaticPrefs::network_http_tailing_enabled() &&
|
2017-09-21 03:09:00 +03:00
|
|
|
aContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON) {
|
|
|
|
cos->AddClassFlags(nsIClassOfService::Throttleable |
|
|
|
|
nsIClassOfService::Tail);
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(newChannel));
|
|
|
|
if (httpChannel) {
|
|
|
|
Unused << httpChannel->SetRequestContextID(aRequestContextID);
|
|
|
|
}
|
|
|
|
}
|
2017-04-25 04:17:38 +03:00
|
|
|
}
|
|
|
|
|
2014-01-30 07:35:40 +04:00
|
|
|
nsCOMPtr<nsILoadGroup> channelLoadGroup;
|
|
|
|
newChannel->GetLoadGroup(getter_AddRefs(channelLoadGroup));
|
2016-08-29 08:34:32 +03:00
|
|
|
rv = request->Init(aURI, aURI, /* aHadInsecureRedirect = */ false,
|
2018-12-26 05:32:55 +03:00
|
|
|
channelLoadGroup, newChannel, entry,
|
|
|
|
ToSupports(aLoadingDocument), aTriggeringPrincipal,
|
2019-07-25 11:23:32 +03:00
|
|
|
corsmode, aReferrerInfo);
|
2016-08-29 08:34:32 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2013-10-16 05:35:44 +04:00
|
|
|
// Add the initiator type for this image load
|
|
|
|
nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(newChannel);
|
|
|
|
if (timedChannel) {
|
|
|
|
timedChannel->SetInitiatorType(initiatorType);
|
|
|
|
}
|
|
|
|
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
// create the proxy listener
|
2016-04-27 20:40:56 +03:00
|
|
|
nsCOMPtr<nsIStreamListener> listener = new ProxyListener(request.get());
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(gImgLog, LogLevel::Debug,
|
2019-02-12 19:08:25 +03:00
|
|
|
("[this=%p] imgLoader::LoadImage -- Calling channel->AsyncOpen()\n",
|
2015-04-08 00:49:00 +03:00
|
|
|
this));
|
2002-03-07 04:50:14 +03:00
|
|
|
|
2014-06-04 00:37:46 +04:00
|
|
|
mozilla::net::PredictorLearn(aURI, aInitialDocumentURI,
|
|
|
|
nsINetworkPredictor::LEARN_LOAD_SUBRESOURCE,
|
|
|
|
aLoadGroup);
|
2013-10-26 01:56:58 +04:00
|
|
|
|
2019-02-12 19:08:25 +03:00
|
|
|
nsresult openRes = newChannel->AsyncOpen(listener);
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
|
2002-09-19 02:08:12 +04:00
|
|
|
if (NS_FAILED(openRes)) {
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
gImgLog, LogLevel::Debug,
|
2019-02-12 19:08:25 +03:00
|
|
|
("[this=%p] imgLoader::LoadImage -- AsyncOpen() failed: 0x%" PRIx32
|
2016-12-16 06:16:31 +03:00
|
|
|
"\n",
|
|
|
|
this, static_cast<uint32_t>(openRes)));
|
2008-12-23 01:20:46 +03:00
|
|
|
request->CancelAndAbort(openRes);
|
2002-09-19 02:08:12 +04:00
|
|
|
return openRes;
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
}
|
2001-02-23 11:36:51 +03:00
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
// Try to add the new request into the cache.
|
2015-04-28 21:45:45 +03:00
|
|
|
PutIntoCache(key, entry);
|
2001-02-23 11:36:51 +03:00
|
|
|
} else {
|
2002-09-19 02:08:12 +04:00
|
|
|
LOG_MSG_WITH_PARAM(gImgLog, "imgLoader::LoadImage |cache hit|", "request",
|
|
|
|
request);
|
2001-02-21 01:43:56 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
// If we didn't get a proxy when validating the cache entry, we need to
|
|
|
|
// create one.
|
2008-09-05 03:00:42 +04:00
|
|
|
if (!*_retval) {
|
2009-11-13 02:00:30 +03:00
|
|
|
// ValidateEntry() has three return values: "Is valid," "might be valid --
|
|
|
|
// validating over network", and "not valid." If we don't have a _retval,
|
|
|
|
// we know ValidateEntry is not validating over the network, so it's safe
|
|
|
|
// to SetLoadId here because we know this request is valid for this context.
|
|
|
|
//
|
|
|
|
// Note, however, that this doesn't guarantee the behaviour we want (one
|
|
|
|
// URL maps to the same image on a page) if we load the same image in a
|
|
|
|
// different tab (see bug 528003), because its load id will get re-set, and
|
|
|
|
// that'll cause us to validate over the network.
|
2016-04-11 11:58:03 +03:00
|
|
|
request->SetLoadId(aLoadingDocument);
|
2003-05-14 09:42:06 +04:00
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_MSG(gImgLog, "imgLoader::LoadImage", "creating proxy request.");
|
2017-07-19 21:15:11 +03:00
|
|
|
rv = CreateNewProxyForRequest(request, aLoadGroup, aLoadingDocument,
|
|
|
|
aObserver, requestFlags, _retval);
|
2010-05-25 18:34:04 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
imgRequestProxy* proxy = *_retval;
|
2003-05-14 09:42:06 +04:00
|
|
|
|
2009-07-28 20:13:48 +04:00
|
|
|
// Make sure that OnStatus/OnProgress calls have the right request set, if
|
|
|
|
// we did create a channel here.
|
|
|
|
if (newChannel) {
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> requestor(
|
|
|
|
new nsProgressNotificationProxy(newChannel, proxy));
|
2015-04-08 00:49:00 +03:00
|
|
|
if (!requestor) {
|
2009-07-28 20:13:48 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2009-07-28 20:13:48 +04:00
|
|
|
newChannel->SetNotificationCallbacks(requestor);
|
|
|
|
}
|
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
// Note that it's OK to add here even if the request is done. If it is,
|
2010-05-11 07:27:41 +04:00
|
|
|
// it'll send a OnStopRequest() to the proxy in imgRequestProxy::Notify and
|
|
|
|
// the proxy will be removed from the loadgroup.
|
2008-09-05 03:00:42 +04:00
|
|
|
proxy->AddToLoadGroup();
|
2008-09-04 19:23:16 +04:00
|
|
|
|
2010-07-29 01:52:14 +04:00
|
|
|
// If we're loading off the network, explicitly don't notify our proxy,
|
|
|
|
// because necko (or things called from necko, such as imgCacheValidator)
|
|
|
|
// are going to call our notifications asynchronously, and we can't make it
|
|
|
|
// further asynchronous because observers might rely on imagelib completing
|
|
|
|
// its work between the channel's OnStartRequest and OnStopRequest.
|
2015-04-08 00:49:00 +03:00
|
|
|
if (!newChannel) {
|
2010-07-29 01:52:14 +04:00
|
|
|
proxy->NotifyListener();
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2017-04-16 00:19:13 +03:00
|
|
|
return rv;
|
2003-05-14 09:42:06 +04:00
|
|
|
}
|
2002-01-15 08:23:33 +03:00
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
NS_ASSERTION(*_retval, "imgLoader::LoadImage -- no return value");
|
2001-02-21 01:43:56 +03:00
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
return NS_OK;
|
2001-02-21 01:43:56 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgLoader::LoadImageWithChannelXPCOM(nsIChannel* channel,
|
|
|
|
imgINotificationObserver* aObserver,
|
|
|
|
nsISupports* aCX,
|
|
|
|
nsIStreamListener** listener,
|
|
|
|
imgIRequest** _retval) {
|
2012-10-12 16:43:01 +04:00
|
|
|
nsresult result;
|
|
|
|
imgRequestProxy* proxy;
|
|
|
|
result = LoadImageWithChannel(channel, aObserver, aCX, listener, &proxy);
|
|
|
|
*_retval = proxy;
|
|
|
|
return result;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
nsresult imgLoader::LoadImageWithChannel(nsIChannel* channel,
|
|
|
|
imgINotificationObserver* aObserver,
|
|
|
|
nsISupports* aCX,
|
|
|
|
nsIStreamListener** listener,
|
|
|
|
imgRequestProxy** _retval) {
|
|
|
|
NS_ASSERTION(channel,
|
|
|
|
"imgLoader::LoadImageWithChannel -- NULL channel pointer");
|
2001-02-21 01:43:56 +03:00
|
|
|
|
2012-10-04 02:43:26 +04:00
|
|
|
MOZ_ASSERT(NS_UsePrivateBrowsing(channel) == mRespectPrivacy);
|
2012-06-26 08:20:12 +04:00
|
|
|
|
2017-11-01 13:59:09 +03:00
|
|
|
LOG_SCOPE(gImgLog, "imgLoader::LoadImageWithChannel");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> request;
|
2001-02-21 01:43:56 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2003-03-26 08:29:11 +03:00
|
|
|
channel->GetURI(getter_AddRefs(uri));
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = do_QueryInterface(aCX);
|
2016-08-04 21:22:00 +03:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(channel, NS_ERROR_FAILURE);
|
2019-02-20 15:27:25 +03:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
|
2016-08-04 21:22:00 +03:00
|
|
|
|
2019-02-20 15:27:25 +03:00
|
|
|
OriginAttributes attrs = loadInfo->GetOriginAttributes();
|
2016-08-04 21:22:00 +03:00
|
|
|
|
2019-02-22 17:25:04 +03:00
|
|
|
ImageCacheKey key(uri, attrs, doc);
|
2001-02-21 01:43:56 +03:00
|
|
|
|
2002-09-19 02:08:12 +04:00
|
|
|
nsLoadFlags requestFlags = nsIRequest::LOAD_NORMAL;
|
|
|
|
channel->GetLoadFlags(&requestFlags);
|
|
|
|
|
2019-05-16 21:55:19 +03:00
|
|
|
if (PreferLoadFromCache(uri)) {
|
2017-06-05 17:04:06 +03:00
|
|
|
requestFlags |= nsIRequest::LOAD_FROM_CACHE;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgCacheEntry> entry;
|
2002-09-19 02:08:12 +04:00
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
if (requestFlags & nsIRequest::LOAD_BYPASS_CACHE) {
|
2015-04-28 21:45:45 +03:00
|
|
|
RemoveFromCache(key);
|
2008-09-05 03:00:42 +04:00
|
|
|
} else {
|
|
|
|
// Look in the cache for our URI, and then validate it.
|
|
|
|
// XXX For now ignore aCacheKey. We will need it in the future
|
|
|
|
// for correctly dealing with image load requests that are a result
|
|
|
|
// of post data.
|
2015-04-28 21:45:45 +03:00
|
|
|
imgCacheTable& cache = GetCache(key);
|
|
|
|
if (cache.Get(key, getter_AddRefs(entry)) && entry) {
|
2008-09-05 03:00:42 +04:00
|
|
|
// We don't want to kick off another network load. So we ask
|
|
|
|
// ValidateEntry to only do validation without creating a new proxy. If
|
|
|
|
// it says that the entry isn't valid any more, we'll only use the entry
|
|
|
|
// we're getting if the channel is loading from the cache anyways.
|
|
|
|
//
|
|
|
|
// XXX -- should this be changed? it's pretty much verbatim from the old
|
|
|
|
// code, but seems nonsensical.
|
2014-08-20 01:49:38 +04:00
|
|
|
//
|
|
|
|
// Since aCanMakeNewChannel == false, we don't need to pass content policy
|
|
|
|
// type/principal/etc
|
2016-04-27 20:40:56 +03:00
|
|
|
|
2019-02-20 15:27:25 +03:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
|
2016-04-27 20:40:56 +03:00
|
|
|
// if there is a loadInfo, use the right contentType, otherwise
|
|
|
|
// default to the internal image type
|
2019-02-20 15:27:25 +03:00
|
|
|
nsContentPolicyType policyType = loadInfo->InternalContentPolicyType();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-07-25 11:23:32 +03:00
|
|
|
if (ValidateEntry(entry, uri, nullptr, nullptr, nullptr, aObserver, aCX,
|
|
|
|
doc, requestFlags, policyType, false, nullptr, nullptr,
|
|
|
|
nullptr, imgIRequest::CORS_NONE)) {
|
2013-07-10 13:56:21 +04:00
|
|
|
request = entry->GetRequest();
|
2003-04-10 05:01:31 +04:00
|
|
|
} else {
|
2015-11-12 23:09:52 +03:00
|
|
|
nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(channel));
|
2011-09-29 10:19:26 +04:00
|
|
|
bool bUseCacheCopy;
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (cacheChan) {
|
2008-09-05 03:00:42 +04:00
|
|
|
cacheChan->IsFromCache(&bUseCacheCopy);
|
2015-04-08 00:49:00 +03:00
|
|
|
} else {
|
2011-10-17 18:59:28 +04:00
|
|
|
bUseCacheCopy = false;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2013-07-10 13:56:21 +04:00
|
|
|
if (!bUseCacheCopy) {
|
2012-07-30 18:20:58 +04:00
|
|
|
entry = nullptr;
|
2013-07-10 13:56:21 +04:00
|
|
|
} else {
|
|
|
|
request = entry->GetRequest();
|
2008-09-05 03:00:42 +04:00
|
|
|
}
|
2002-09-19 02:08:12 +04:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
|
|
|
|
if (request && entry) {
|
2015-04-08 00:49:00 +03:00
|
|
|
// If this entry has no proxies, its request has no reference to
|
|
|
|
// the entry.
|
2009-01-31 05:17:47 +03:00
|
|
|
if (entry->HasNoProxies()) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_FUNC_WITH_PARAM(
|
|
|
|
gImgLog,
|
2015-04-08 00:49:00 +03:00
|
|
|
"imgLoader::LoadImageWithChannel() adding proxyless entry", "uri",
|
2018-06-06 03:42:57 +03:00
|
|
|
key.URI());
|
2015-04-08 00:49:00 +03:00
|
|
|
MOZ_ASSERT(!request->HasCacheEntry(),
|
|
|
|
"Proxyless entry's request has cache entry!");
|
2009-01-31 05:17:47 +03:00
|
|
|
request->SetCacheEntry(entry);
|
|
|
|
|
2017-05-30 22:49:26 +03:00
|
|
|
if (mCacheTracker && entry->GetExpirationState()->IsTracked()) {
|
2012-06-26 08:20:12 +04:00
|
|
|
mCacheTracker->MarkUsed(entry);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2013-03-30 00:14:19 +04:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
}
|
2002-09-19 02:08:12 +04:00
|
|
|
}
|
|
|
|
}
|
2001-05-08 08:21:49 +04:00
|
|
|
|
2002-01-15 08:23:33 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
channel->GetLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
|
2017-11-01 13:59:09 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (doc) {
|
|
|
|
// The load group of the channel should always match that of the
|
|
|
|
// document if given. If that isn't the case, then we need to add more
|
|
|
|
// plumbing to ensure we block the document as well.
|
|
|
|
nsCOMPtr<nsILoadGroup> docLoadGroup = doc->GetDocumentLoadGroup();
|
|
|
|
MOZ_ASSERT(docLoadGroup == loadGroup);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-09 23:03:41 +04:00
|
|
|
// Filter out any load flags not from nsIRequest
|
|
|
|
requestFlags &= nsIRequest::LOAD_REQUESTMASK;
|
2010-07-29 01:52:14 +04:00
|
|
|
|
2019-02-22 17:25:04 +03:00
|
|
|
nsresult rv = NS_OK;
|
2001-02-21 02:35:22 +03:00
|
|
|
if (request) {
|
2001-02-21 01:43:56 +03:00
|
|
|
// we have this in our cache already.. cancel the current (document) load
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
// this should fire an OnStopRequest
|
|
|
|
channel->Cancel(NS_ERROR_PARSED_DATA_CACHED);
|
2001-02-21 01:43:56 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*listener = nullptr; // give them back a null nsIStreamListener
|
2010-07-29 01:52:14 +04:00
|
|
|
|
2017-07-19 21:15:11 +03:00
|
|
|
rv = CreateNewProxyForRequest(request, loadGroup, doc, aObserver,
|
2012-11-04 07:04:07 +04:00
|
|
|
requestFlags, _retval);
|
2010-07-29 01:52:14 +04:00
|
|
|
static_cast<imgRequestProxy*>(*_retval)->NotifyListener();
|
2001-02-21 01:43:56 +03:00
|
|
|
} else {
|
2015-05-20 20:21:13 +03:00
|
|
|
// We use originalURI here to fulfil the imgIRequest contract on GetURI.
|
|
|
|
nsCOMPtr<nsIURI> originalURI;
|
|
|
|
channel->GetOriginalURI(getter_AddRefs(originalURI));
|
|
|
|
|
|
|
|
// XXX(seth): We should be able to just use |key| here, except that |key| is
|
|
|
|
// constructed above with the *current URI* and not the *original URI*. I'm
|
|
|
|
// pretty sure this is a bug, and it's preventing us from ever getting a
|
|
|
|
// cache hit in LoadImageWithChannel when redirects are involved.
|
2019-02-22 17:25:04 +03:00
|
|
|
ImageCacheKey originalURIKey(originalURI, attrs, doc);
|
2015-05-20 20:21:13 +03:00
|
|
|
|
2011-09-21 01:00:42 +04:00
|
|
|
// Default to doing a principal check because we don't know who
|
|
|
|
// started that load and whether their principal ended up being
|
|
|
|
// inherited on the channel.
|
2015-05-20 20:21:13 +03:00
|
|
|
NewRequestAndEntry(/* aForcePrincipalCheckForCacheEntry = */ true, this,
|
|
|
|
originalURIKey, getter_AddRefs(request),
|
2015-04-08 00:49:00 +03:00
|
|
|
getter_AddRefs(entry));
|
2001-03-16 05:40:04 +03:00
|
|
|
|
2011-07-14 22:47:32 +04:00
|
|
|
// No principal specified here, because we're not passed one.
|
2014-12-16 04:40:43 +03:00
|
|
|
// In LoadImageWithChannel, the redirects that may have been
|
2019-09-12 12:00:52 +03:00
|
|
|
// associated with this load would have gone through necko.
|
2014-12-16 04:40:43 +03:00
|
|
|
// We only have the final URI in ImageLib and hence don't know
|
|
|
|
// if the request went through insecure redirects. But if it did,
|
|
|
|
// the necko cache should have handled that (since all necko cache hits
|
|
|
|
// including the redirects will go through content policy). Hence, we
|
|
|
|
// can set aHadInsecureRedirect to false here.
|
2016-08-29 08:34:32 +03:00
|
|
|
rv = request->Init(originalURI, uri, /* aHadInsecureRedirect = */ false,
|
|
|
|
channel, channel, entry, aCX, nullptr,
|
2019-07-25 11:23:32 +03:00
|
|
|
imgIRequest::CORS_NONE, nullptr);
|
2016-08-29 08:34:32 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-02-21 01:43:56 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProxyListener> pl =
|
2015-03-05 09:18:09 +03:00
|
|
|
new ProxyListener(static_cast<nsIStreamListener*>(request.get()));
|
|
|
|
pl.forget(listener);
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
|
2008-09-05 03:00:42 +04:00
|
|
|
// Try to add the new request into the cache.
|
2015-05-20 20:21:13 +03:00
|
|
|
PutIntoCache(originalURIKey, entry);
|
2001-02-21 01:43:56 +03:00
|
|
|
|
2017-07-19 21:15:11 +03:00
|
|
|
rv = CreateNewProxyForRequest(request, loadGroup, doc, aObserver,
|
2012-11-04 07:04:07 +04:00
|
|
|
requestFlags, _retval);
|
2002-09-19 02:08:12 +04:00
|
|
|
|
2010-07-29 01:52:14 +04:00
|
|
|
// Explicitly don't notify our proxy, because we're loading off the
|
|
|
|
// network, and necko (or things called from necko, such as
|
|
|
|
// imgCacheValidator) are going to call our notifications asynchronously,
|
|
|
|
// and we can't make it further asynchronous because observers might rely
|
|
|
|
// on imagelib completing its work between the channel's OnStartRequest and
|
|
|
|
// OnStopRequest.
|
|
|
|
}
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
|
2017-11-01 13:59:09 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*_retval)->AddToLoadGroup();
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-12-05 22:33:08 +03:00
|
|
|
bool imgLoader::SupportImageWithMimeType(const char* aMimeType,
|
|
|
|
AcceptedMimeTypes aAccept
|
|
|
|
/* = AcceptedMimeTypes::IMAGES */) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString mimeType(aMimeType);
|
2002-04-02 01:14:07 +04:00
|
|
|
ToLowerCase(mimeType);
|
2014-12-05 22:33:08 +03:00
|
|
|
|
|
|
|
if (aAccept == AcceptedMimeTypes::IMAGES_AND_DOCUMENTS &&
|
|
|
|
mimeType.EqualsLiteral("image/svg+xml")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-23 08:39:48 +03:00
|
|
|
DecoderType type = DecoderFactory::GetDecoderType(mimeType.get());
|
|
|
|
return type != DecoderType::UNKNOWN;
|
2002-03-23 16:21:33 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgLoader::GetMIMETypeFromContent(nsIRequest* aRequest,
|
|
|
|
const uint8_t* aContents, uint32_t aLength,
|
|
|
|
nsACString& aContentType) {
|
2019-08-14 16:59:06 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
|
|
|
|
if (channel) {
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
|
|
|
|
if (loadInfo->GetSkipContentSniffing()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
2004-07-19 22:53:02 +04:00
|
|
|
return GetMimeTypeFromContent((const char*)aContents, aLength, aContentType);
|
2002-05-25 00:54:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2015-04-08 00:49:00 +03:00
|
|
|
nsresult imgLoader::GetMimeTypeFromContent(const char* aContents,
|
|
|
|
uint32_t aLength,
|
|
|
|
nsACString& aContentType) {
|
2002-03-23 16:21:33 +03:00
|
|
|
/* Is it a GIF? */
|
2018-01-09 17:49:37 +03:00
|
|
|
if (aLength >= 6 &&
|
|
|
|
(!strncmp(aContents, "GIF87a", 6) || !strncmp(aContents, "GIF89a", 6))) {
|
2013-01-09 01:40:47 +04:00
|
|
|
aContentType.AssignLiteral(IMAGE_GIF);
|
2002-03-23 16:21:33 +03:00
|
|
|
|
|
|
|
/* or a PNG? */
|
2015-04-08 00:49:00 +03:00
|
|
|
} else if (aLength >= 8 && ((unsigned char)aContents[0] == 0x89 &&
|
|
|
|
(unsigned char)aContents[1] == 0x50 &&
|
|
|
|
(unsigned char)aContents[2] == 0x4E &&
|
|
|
|
(unsigned char)aContents[3] == 0x47 &&
|
|
|
|
(unsigned char)aContents[4] == 0x0D &&
|
|
|
|
(unsigned char)aContents[5] == 0x0A &&
|
|
|
|
(unsigned char)aContents[6] == 0x1A &&
|
|
|
|
(unsigned char)aContents[7] == 0x0A)) {
|
2013-01-09 01:40:47 +04:00
|
|
|
aContentType.AssignLiteral(IMAGE_PNG);
|
2002-03-23 16:21:33 +03:00
|
|
|
|
|
|
|
/* maybe a JPEG (JFIF)? */
|
|
|
|
/* JFIF files start with SOI APP0 but older files can start with SOI DQT
|
|
|
|
* so we test for SOI followed by any marker, i.e. FF D8 FF
|
|
|
|
* this will also work for SPIFF JPEG files if they appear in the future.
|
|
|
|
*
|
|
|
|
* (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
|
|
|
|
*/
|
2015-04-08 00:49:00 +03:00
|
|
|
} else if (aLength >= 3 && ((unsigned char)aContents[0]) == 0xFF &&
|
|
|
|
((unsigned char)aContents[1]) == 0xD8 &&
|
|
|
|
((unsigned char)aContents[2]) == 0xFF) {
|
2013-01-09 01:40:47 +04:00
|
|
|
aContentType.AssignLiteral(IMAGE_JPEG);
|
2002-03-23 16:21:33 +03:00
|
|
|
|
|
|
|
/* or how about ART? */
|
|
|
|
/* ART begins with JG (4A 47). Major version offset 2.
|
2013-08-23 23:51:00 +04:00
|
|
|
* Minor version offset 3. Offset 4 must be nullptr.
|
2002-03-23 16:21:33 +03:00
|
|
|
*/
|
2015-04-08 00:49:00 +03:00
|
|
|
} else if (aLength >= 5 && ((unsigned char)aContents[0]) == 0x4a &&
|
|
|
|
((unsigned char)aContents[1]) == 0x47 &&
|
|
|
|
((unsigned char)aContents[4]) == 0x00) {
|
2013-01-09 01:40:47 +04:00
|
|
|
aContentType.AssignLiteral(IMAGE_ART);
|
2002-03-23 16:21:33 +03:00
|
|
|
|
2018-01-09 17:49:37 +03:00
|
|
|
} else if (aLength >= 2 && !strncmp(aContents, "BM", 2)) {
|
2013-01-09 01:40:47 +04:00
|
|
|
aContentType.AssignLiteral(IMAGE_BMP);
|
2002-03-23 16:21:33 +03:00
|
|
|
|
|
|
|
// ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
|
2005-01-12 23:16:07 +03:00
|
|
|
// CURs begin with 2-byte 0 followed by 2-byte 2.
|
2015-04-08 00:49:00 +03:00
|
|
|
} else if (aLength >= 4 && (!memcmp(aContents, "\000\000\001\000", 4) ||
|
|
|
|
!memcmp(aContents, "\000\000\002\000", 4))) {
|
2013-01-09 01:40:47 +04:00
|
|
|
aContentType.AssignLiteral(IMAGE_ICO);
|
2002-03-23 16:21:33 +03:00
|
|
|
|
2018-06-30 03:30:05 +03:00
|
|
|
// WebPs always begin with RIFF, a 32-bit length, and WEBP.
|
|
|
|
} else if (aLength >= 12 && !memcmp(aContents, "RIFF", 4) &&
|
|
|
|
!memcmp(aContents + 8, "WEBP", 4)) {
|
|
|
|
aContentType.AssignLiteral(IMAGE_WEBP);
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
} else {
|
2002-06-12 02:40:29 +04:00
|
|
|
/* none of the above? I give up */
|
2004-07-19 22:53:02 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2002-06-04 12:48:12 +04:00
|
|
|
}
|
|
|
|
|
2004-07-19 22:53:02 +04:00
|
|
|
return NS_OK;
|
2002-03-23 16:21:33 +03:00
|
|
|
}
|
|
|
|
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
/**
|
|
|
|
* proxy stream listener class used to handle multipart/x-mixed-replace
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsIRequest.h"
|
|
|
|
#include "nsIStreamConverterService.h"
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ProxyListener, nsIStreamListener,
|
|
|
|
nsIThreadRetargetableStreamListener, nsIRequestObserver)
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
ProxyListener::ProxyListener(nsIStreamListener* dest) : mDestListener(dest) {
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
/* member initializers and constructor code */
|
|
|
|
}
|
|
|
|
|
|
|
|
ProxyListener::~ProxyListener() { /* destructor code */
|
|
|
|
}
|
|
|
|
|
|
|
|
/** nsIRequestObserver methods **/
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:04 +03:00
|
|
|
ProxyListener::OnStartRequest(nsIRequest* aRequest) {
|
2015-04-08 00:49:00 +03:00
|
|
|
if (!mDestListener) {
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
|
|
|
|
if (channel) {
|
2014-10-29 18:35:05 +03:00
|
|
|
// We need to set the initiator type for the image load
|
|
|
|
nsCOMPtr<nsITimedChannel> timedChannel = do_QueryInterface(channel);
|
|
|
|
if (timedChannel) {
|
|
|
|
nsAutoString type;
|
|
|
|
timedChannel->GetInitiatorType(type);
|
|
|
|
if (type.IsEmpty()) {
|
|
|
|
timedChannel->SetInitiatorType(NS_LITERAL_STRING("img"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString contentType;
|
2002-03-21 01:50:33 +03:00
|
|
|
nsresult rv = channel->GetContentType(contentType);
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
|
2002-03-21 01:50:33 +03:00
|
|
|
if (!contentType.IsEmpty()) {
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
/* If multipart/x-mixed-replace content, we'll insert a MIME decoder
|
|
|
|
in the pipeline to handle the content and pass it along to our
|
|
|
|
original listener.
|
|
|
|
*/
|
2002-01-15 08:23:33 +03:00
|
|
|
if (NS_LITERAL_CSTRING("multipart/x-mixed-replace").Equals(contentType)) {
|
2015-04-08 00:49:00 +03:00
|
|
|
nsCOMPtr<nsIStreamConverterService> convServ(
|
|
|
|
do_GetService("@mozilla.org/streamConverters;1", &rv));
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIStreamListener> toListener(mDestListener);
|
|
|
|
nsCOMPtr<nsIStreamListener> fromListener;
|
|
|
|
|
2004-06-29 20:45:07 +04:00
|
|
|
rv = convServ->AsyncConvertData("multipart/x-mixed-replace", "*/*",
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
toListener, nullptr,
|
|
|
|
getter_AddRefs(fromListener));
|
2015-04-08 00:49:00 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
mDestListener = fromListener;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 02:41:04 +03:00
|
|
|
return mDestListener->OnStartRequest(aRequest);
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:31 +03:00
|
|
|
ProxyListener::OnStopRequest(nsIRequest* aRequest, nsresult status) {
|
2015-04-08 00:49:00 +03:00
|
|
|
if (!mDestListener) {
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
|
2019-02-28 02:41:31 +03:00
|
|
|
return mDestListener->OnStopRequest(aRequest, status);
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** nsIStreamListener methods **/
|
|
|
|
|
2012-09-06 06:41:02 +04:00
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:42:27 +03:00
|
|
|
ProxyListener::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
|
2015-04-08 00:49:00 +03:00
|
|
|
uint64_t sourceOffset, uint32_t count) {
|
|
|
|
if (!mDestListener) {
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
|
2019-02-28 02:42:27 +03:00
|
|
|
return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 06:42:56 +04:00
|
|
|
}
|
2002-01-15 08:23:33 +03:00
|
|
|
|
2013-09-28 22:28:42 +04:00
|
|
|
/** nsThreadRetargetableStreamListener methods **/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ProxyListener::CheckListenerChain() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
|
|
|
|
do_QueryInterface(mDestListener, &rv);
|
|
|
|
if (retargetableListener) {
|
|
|
|
rv = retargetableListener->CheckListenerChain();
|
|
|
|
}
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
gImgLog, LogLevel::Debug,
|
2016-12-16 06:16:31 +03:00
|
|
|
("ProxyListener::CheckListenerChain %s [this=%p listener=%p rv=%" PRIx32
|
|
|
|
"]",
|
2013-09-28 22:28:42 +04:00
|
|
|
(NS_SUCCEEDED(rv) ? "success" : "failure"), this,
|
2016-12-16 06:16:31 +03:00
|
|
|
(nsIStreamListener*)mDestListener, static_cast<uint32_t>(rv)));
|
2013-09-28 22:28:42 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-01-15 08:23:33 +03:00
|
|
|
/**
|
|
|
|
* http validate class. check a channel for a 304
|
|
|
|
*/
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(imgCacheValidator, nsIStreamListener, nsIRequestObserver,
|
|
|
|
nsIThreadRetargetableStreamListener, nsIChannelEventSink,
|
|
|
|
nsIInterfaceRequestor, nsIAsyncVerifyRedirectCallback)
|
2002-01-15 08:23:33 +03:00
|
|
|
|
2011-07-01 21:03:38 +04:00
|
|
|
imgCacheValidator::imgCacheValidator(nsProgressNotificationProxy* progress,
|
2015-04-08 00:49:00 +03:00
|
|
|
imgLoader* loader, imgRequest* request,
|
2015-03-24 05:37:45 +03:00
|
|
|
nsISupports* aContext,
|
2019-09-17 16:16:19 +03:00
|
|
|
uint64_t aInnerWindowId,
|
2015-03-24 05:37:45 +03:00
|
|
|
bool forcePrincipalCheckForCacheEntry)
|
2011-07-01 21:03:38 +04:00
|
|
|
: mProgressProxy(progress),
|
|
|
|
mRequest(request),
|
2012-06-26 08:20:12 +04:00
|
|
|
mContext(aContext),
|
2019-09-17 16:16:19 +03:00
|
|
|
mInnerWindowId(aInnerWindowId),
|
2014-12-16 04:40:43 +03:00
|
|
|
mImgLoader(loader),
|
|
|
|
mHadInsecureRedirect(false) {
|
2015-04-08 00:49:00 +03:00
|
|
|
NewRequestAndEntry(forcePrincipalCheckForCacheEntry, loader,
|
|
|
|
mRequest->CacheKey(), getter_AddRefs(mNewRequest),
|
2012-06-26 08:20:12 +04:00
|
|
|
getter_AddRefs(mNewEntry));
|
2011-07-01 21:03:38 +04:00
|
|
|
}
|
2002-01-15 08:23:33 +03:00
|
|
|
|
2002-12-24 17:30:12 +03:00
|
|
|
imgCacheValidator::~imgCacheValidator() {
|
2003-04-02 07:19:50 +04:00
|
|
|
if (mRequest) {
|
2018-04-17 21:42:35 +03:00
|
|
|
// If something went wrong, and we never unblocked the requests waiting on
|
|
|
|
// validation, now is our last chance. We will cancel the new request and
|
|
|
|
// switch the waiting proxies to it.
|
|
|
|
UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ false);
|
2003-04-02 07:19:50 +04:00
|
|
|
}
|
2002-01-15 08:23:33 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
void imgCacheValidator::AddProxy(imgRequestProxy* aProxy) {
|
2003-05-14 09:42:06 +04:00
|
|
|
// aProxy needs to be in the loadgroup since we're validating from
|
|
|
|
// the network.
|
|
|
|
aProxy->AddToLoadGroup();
|
|
|
|
|
2018-02-07 15:27:27 +03:00
|
|
|
mProxies.AppendElement(aProxy);
|
2002-01-15 08:23:33 +03:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:02:59 +03:00
|
|
|
void imgCacheValidator::RemoveProxy(imgRequestProxy* aProxy) {
|
2018-02-07 15:27:27 +03:00
|
|
|
mProxies.RemoveElement(aProxy);
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:42:35 +03:00
|
|
|
void imgCacheValidator::UpdateProxies(bool aCancelRequest, bool aSyncNotify) {
|
|
|
|
MOZ_ASSERT(mRequest);
|
|
|
|
|
|
|
|
// Clear the validator before updating the proxies. The notifications may
|
|
|
|
// clone an existing request, and its state could be inconsistent.
|
|
|
|
mRequest->SetValidator(nullptr);
|
|
|
|
mRequest = nullptr;
|
|
|
|
|
|
|
|
// If an error occurred, we will want to cancel the new request, and make the
|
|
|
|
// validating proxies point to it. Any proxies still bound to the original
|
|
|
|
// request which are not validating should remain untouched.
|
|
|
|
if (aCancelRequest) {
|
|
|
|
MOZ_ASSERT(mNewRequest);
|
|
|
|
mNewRequest->CancelAndAbort(NS_BINDING_ABORTED);
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:27:27 +03:00
|
|
|
// We have finished validating the request, so we can safely take ownership
|
|
|
|
// of the proxy list. imgRequestProxy::SyncNotifyListener can mutate the list
|
|
|
|
// if imgRequestProxy::CancelAndForgetObserver is called by its owner. Note
|
|
|
|
// that any potential notifications should still be suppressed in
|
|
|
|
// imgRequestProxy::ChangeOwner because we haven't cleared the validating
|
|
|
|
// flag yet, and thus they will remain deferred.
|
2018-05-30 22:15:35 +03:00
|
|
|
AutoTArray<RefPtr<imgRequestProxy>, 4> proxies(std::move(mProxies));
|
2018-02-07 15:27:27 +03:00
|
|
|
|
|
|
|
for (auto& proxy : proxies) {
|
|
|
|
// First update the state of all proxies before notifying any of them
|
|
|
|
// to ensure a consistent state (e.g. in case the notification causes
|
|
|
|
// other proxies to be touched indirectly.)
|
|
|
|
MOZ_ASSERT(proxy->IsValidating());
|
|
|
|
MOZ_ASSERT(proxy->NotificationsDeferred(),
|
|
|
|
"Proxies waiting on cache validation should be "
|
|
|
|
"deferring notifications!");
|
|
|
|
if (mNewRequest) {
|
|
|
|
proxy->ChangeOwner(mNewRequest);
|
|
|
|
}
|
|
|
|
proxy->ClearValidating();
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:42:35 +03:00
|
|
|
mNewRequest = nullptr;
|
|
|
|
mNewEntry = nullptr;
|
|
|
|
|
2018-02-07 15:27:27 +03:00
|
|
|
for (auto& proxy : proxies) {
|
2018-04-17 21:42:35 +03:00
|
|
|
if (aSyncNotify) {
|
|
|
|
// Notify synchronously, because the caller knows we are already in an
|
|
|
|
// asynchronously-called function (e.g. OnStartRequest).
|
|
|
|
proxy->SyncNotifyListener();
|
|
|
|
} else {
|
|
|
|
// Notify asynchronously, because the caller does not know our current
|
|
|
|
// call state (e.g. ~imgCacheValidator).
|
|
|
|
proxy->NotifyListener();
|
|
|
|
}
|
2018-02-07 15:27:27 +03:00
|
|
|
}
|
2017-11-14 20:02:59 +03:00
|
|
|
}
|
|
|
|
|
2002-01-15 08:23:33 +03:00
|
|
|
/** nsIRequestObserver methods **/
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:04 +03:00
|
|
|
imgCacheValidator::OnStartRequest(nsIRequest* aRequest) {
|
2015-04-07 20:02:02 +03:00
|
|
|
// We may be holding on to a document, so ensure that it's released.
|
|
|
|
nsCOMPtr<nsISupports> context = mContext.forget();
|
|
|
|
|
2015-03-28 06:07:20 +03:00
|
|
|
// If for some reason we don't still have an existing request (probably
|
|
|
|
// because OnStartRequest got delivered more than once), just bail.
|
|
|
|
if (!mRequest) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("OnStartRequest delivered more than once?");
|
|
|
|
aRequest->Cancel(NS_BINDING_ABORTED);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-07-01 21:03:42 +04:00
|
|
|
// If this request is coming from cache and has the same URI as our
|
|
|
|
// imgRequest, the request all our proxies are pointing at is valid, and all
|
|
|
|
// we have to do is tell them to notify their listeners.
|
2015-11-12 23:09:52 +03:00
|
|
|
nsCOMPtr<nsICacheInfoChannel> cacheChan(do_QueryInterface(aRequest));
|
2011-07-01 21:03:42 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
|
2012-11-08 22:54:00 +04:00
|
|
|
if (cacheChan && channel && !mRequest->CacheChanged(aRequest)) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isFromCache = false;
|
2011-07-01 21:03:42 +04:00
|
|
|
cacheChan->IsFromCache(&isFromCache);
|
2011-07-01 05:58:34 +04:00
|
|
|
|
2011-07-01 21:03:42 +04:00
|
|
|
nsCOMPtr<nsIURI> channelURI;
|
|
|
|
channel->GetURI(getter_AddRefs(channelURI));
|
2015-03-24 05:37:45 +03:00
|
|
|
|
2017-11-13 11:31:24 +03:00
|
|
|
nsCOMPtr<nsIURI> finalURI;
|
|
|
|
mRequest->GetFinalURI(getter_AddRefs(finalURI));
|
2015-03-24 05:37:45 +03:00
|
|
|
|
|
|
|
bool sameURI = false;
|
2017-11-13 11:31:24 +03:00
|
|
|
if (channelURI && finalURI) {
|
|
|
|
channelURI->Equals(finalURI, &sameURI);
|
2015-03-24 05:37:45 +03:00
|
|
|
}
|
2011-07-01 21:03:42 +04:00
|
|
|
|
|
|
|
if (isFromCache && sameURI) {
|
|
|
|
// We don't need to load this any more.
|
|
|
|
aRequest->Cancel(NS_BINDING_ABORTED);
|
2018-04-17 21:42:35 +03:00
|
|
|
mNewRequest = nullptr;
|
2011-07-01 21:03:42 +04:00
|
|
|
|
2018-02-07 15:27:27 +03:00
|
|
|
// Clear the validator before updating the proxies. The notifications may
|
|
|
|
// clone an existing request, and its state could be inconsistent.
|
2015-04-07 20:02:02 +03:00
|
|
|
mRequest->SetLoadId(context);
|
2019-09-17 16:16:19 +03:00
|
|
|
mRequest->SetInnerWindowID(mInnerWindowId);
|
2018-04-17 21:42:35 +03:00
|
|
|
UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
|
2002-01-15 08:23:33 +03:00
|
|
|
return NS_OK;
|
2002-12-24 17:30:12 +03:00
|
|
|
}
|
|
|
|
}
|
2008-09-05 03:00:42 +04:00
|
|
|
|
2009-01-31 05:17:47 +03:00
|
|
|
// We can't load out of cache. We have to create a whole new request for the
|
|
|
|
// data that's coming in off the channel.
|
2002-12-24 17:30:12 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2018-06-06 03:42:57 +03:00
|
|
|
mRequest->GetURI(getter_AddRefs(uri));
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2018-06-06 03:42:57 +03:00
|
|
|
LOG_MSG_WITH_PARAM(gImgLog,
|
|
|
|
"imgCacheValidator::OnStartRequest creating new request",
|
|
|
|
"uri", uri);
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t corsmode = mRequest->GetCORSMode();
|
2019-07-25 11:23:32 +03:00
|
|
|
nsCOMPtr<nsIReferrerInfo> referrerInfo = mRequest->GetReferrerInfo();
|
2017-10-03 06:38:30 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> triggeringPrincipal =
|
|
|
|
mRequest->GetTriggeringPrincipal();
|
2011-07-14 22:47:32 +04:00
|
|
|
|
2002-12-24 17:30:12 +03:00
|
|
|
// Doom the old request's cache entry
|
2003-06-24 02:55:17 +04:00
|
|
|
mRequest->RemoveFromCache();
|
2002-01-15 08:23:33 +03:00
|
|
|
|
2008-12-23 01:20:46 +03:00
|
|
|
// We use originalURI here to fulfil the imgIRequest contract on GetURI.
|
2006-03-11 23:32:47 +03:00
|
|
|
nsCOMPtr<nsIURI> originalURI;
|
|
|
|
channel->GetOriginalURI(getter_AddRefs(originalURI));
|
2016-08-29 08:34:32 +03:00
|
|
|
nsresult rv = mNewRequest->Init(originalURI, uri, mHadInsecureRedirect,
|
2017-10-03 06:38:30 +03:00
|
|
|
aRequest, channel, mNewEntry, context,
|
2019-07-25 11:23:32 +03:00
|
|
|
triggeringPrincipal, corsmode, referrerInfo);
|
2016-08-29 08:34:32 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2018-04-17 21:42:35 +03:00
|
|
|
UpdateProxies(/* aCancelRequest */ true, /* aSyncNotify */ true);
|
2016-08-29 08:34:32 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2002-01-15 08:23:33 +03:00
|
|
|
|
2011-07-14 22:47:34 +04:00
|
|
|
mDestListener = new ProxyListener(mNewRequest);
|
2002-01-15 08:23:33 +03:00
|
|
|
|
2009-01-31 05:17:47 +03:00
|
|
|
// Try to add the new request into the cache. Note that the entry must be in
|
|
|
|
// the cache before the proxies' ownership changes, because adding a proxy
|
|
|
|
// changes the caching behaviour for imgRequests.
|
2015-05-20 20:21:13 +03:00
|
|
|
mImgLoader->PutIntoCache(mNewRequest->CacheKey(), mNewEntry);
|
2018-04-17 21:42:35 +03:00
|
|
|
UpdateProxies(/* aCancelRequest */ false, /* aSyncNotify */ true);
|
2019-02-28 02:41:04 +03:00
|
|
|
return mDestListener->OnStartRequest(aRequest);
|
2002-01-15 08:23:33 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:31 +03:00
|
|
|
imgCacheValidator::OnStopRequest(nsIRequest* aRequest, nsresult status) {
|
2015-04-07 20:02:02 +03:00
|
|
|
// Be sure we've released the document that we may have been holding on to.
|
|
|
|
mContext = nullptr;
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
if (!mDestListener) {
|
2002-01-15 08:23:33 +03:00
|
|
|
return NS_OK;
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2002-01-15 08:23:33 +03:00
|
|
|
|
2019-02-28 02:41:31 +03:00
|
|
|
return mDestListener->OnStopRequest(aRequest, status);
|
2002-01-15 08:23:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** nsIStreamListener methods **/
|
|
|
|
|
2012-09-06 06:41:02 +04:00
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:42:27 +03:00
|
|
|
imgCacheValidator::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* inStr,
|
2015-04-08 00:49:00 +03:00
|
|
|
uint64_t sourceOffset, uint32_t count) {
|
2002-01-15 08:23:33 +03:00
|
|
|
if (!mDestListener) {
|
|
|
|
// XXX see bug 113959
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t _retval;
|
2012-07-30 18:20:58 +04:00
|
|
|
inStr->ReadSegments(NS_DiscardSegment, nullptr, count, &_retval);
|
2002-01-15 08:23:33 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-02-28 02:42:27 +03:00
|
|
|
return mDestListener->OnDataAvailable(aRequest, inStr, sourceOffset, count);
|
2002-01-15 08:23:33 +03:00
|
|
|
}
|
2011-07-01 21:03:38 +04:00
|
|
|
|
2013-09-28 22:28:42 +04:00
|
|
|
/** nsIThreadRetargetableStreamListener methods **/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
imgCacheValidator::CheckListenerChain() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener =
|
|
|
|
do_QueryInterface(mDestListener, &rv);
|
|
|
|
if (retargetableListener) {
|
|
|
|
rv = retargetableListener->CheckListenerChain();
|
|
|
|
}
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
gImgLog, LogLevel::Debug,
|
2016-12-16 06:16:31 +03:00
|
|
|
("[this=%p] imgCacheValidator::CheckListenerChain -- rv %" PRId32 "=%s",
|
|
|
|
this, static_cast<uint32_t>(rv),
|
|
|
|
NS_SUCCEEDED(rv) ? "succeeded" : "failed"));
|
2013-09-28 22:28:42 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-07-01 21:03:38 +04:00
|
|
|
/** nsIInterfaceRequestor methods **/
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgCacheValidator::GetInterface(const nsIID& aIID, void** aResult) {
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
|
2011-07-01 21:03:38 +04:00
|
|
|
return QueryInterface(aIID, aResult);
|
2015-04-08 00:49:00 +03:00
|
|
|
}
|
2011-07-01 21:03:38 +04:00
|
|
|
|
|
|
|
return mProgressProxy->GetInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
// These functions are materially the same as the same functions in imgRequest.
|
|
|
|
// We duplicate them because we're verifying whether cache loads are necessary,
|
|
|
|
// not unconditionally loading.
|
|
|
|
|
|
|
|
/** nsIChannelEventSink methods **/
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgCacheValidator::AsyncOnChannelRedirect(
|
|
|
|
nsIChannel* oldChannel, nsIChannel* newChannel, uint32_t flags,
|
|
|
|
nsIAsyncVerifyRedirectCallback* callback) {
|
2011-07-01 21:03:38 +04:00
|
|
|
// Note all cache information we get from the old channel.
|
|
|
|
mNewRequest->SetCacheValidation(mNewEntry, oldChannel);
|
|
|
|
|
2014-12-16 04:40:43 +03:00
|
|
|
// If the previous URI is a non-HTTPS URI, record that fact for later use by
|
|
|
|
// security code, which needs to know whether there is an insecure load at any
|
|
|
|
// point in the redirect chain.
|
|
|
|
nsCOMPtr<nsIURI> oldURI;
|
|
|
|
bool schemeLocal = false;
|
|
|
|
if (NS_FAILED(oldChannel->GetURI(getter_AddRefs(oldURI))) ||
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_FAILED(NS_URIChainHasFlags(
|
|
|
|
oldURI, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE, &schemeLocal)) ||
|
2019-08-03 12:44:12 +03:00
|
|
|
(!oldURI->SchemeIs("https") && !oldURI->SchemeIs("chrome") &&
|
|
|
|
!schemeLocal)) {
|
2014-12-16 04:40:43 +03:00
|
|
|
mHadInsecureRedirect = true;
|
|
|
|
}
|
|
|
|
|
2011-07-01 21:03:38 +04:00
|
|
|
// Prepare for callback
|
|
|
|
mRedirectCallback = callback;
|
|
|
|
mRedirectChannel = newChannel;
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
return mProgressProxy->AsyncOnChannelRedirect(oldChannel, newChannel, flags,
|
|
|
|
this);
|
2011-07-01 21:03:38 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:49:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgCacheValidator::OnRedirectVerifyCallback(nsresult aResult) {
|
2011-07-01 21:03:38 +04:00
|
|
|
// If we've already been told to abort, just do so.
|
|
|
|
if (NS_FAILED(aResult)) {
|
|
|
|
mRedirectCallback->OnRedirectVerifyCallback(aResult);
|
2012-07-30 18:20:58 +04:00
|
|
|
mRedirectCallback = nullptr;
|
|
|
|
mRedirectChannel = nullptr;
|
2011-07-01 21:03:38 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we have a protocol that returns data rather than opens
|
|
|
|
// an external application, e.g. mailto:
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
mRedirectChannel->GetURI(getter_AddRefs(uri));
|
2011-09-29 10:19:26 +04:00
|
|
|
bool doesNotReturnData = false;
|
2011-07-01 21:03:38 +04:00
|
|
|
NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA,
|
|
|
|
&doesNotReturnData);
|
|
|
|
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
if (doesNotReturnData) {
|
|
|
|
result = NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRedirectCallback->OnRedirectVerifyCallback(result);
|
2012-07-30 18:20:58 +04:00
|
|
|
mRedirectCallback = nullptr;
|
|
|
|
mRedirectChannel = nullptr;
|
2011-07-01 21:03:38 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|