2001-01-23 01:01:03 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
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
|
|
|
|
2001-02-21 02:27:48 +03:00
|
|
|
#include "imgRequest.h"
|
2011-07-08 21:55:03 +04:00
|
|
|
#include "ImageLogging.h"
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2002-05-25 00:54:44 +04:00
|
|
|
#include "imgLoader.h"
|
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
|
|
|
#include "imgRequestProxy.h"
|
2015-01-18 00:18:26 +03:00
|
|
|
#include "DecodePool.h"
|
2014-11-15 07:10:47 +03:00
|
|
|
#include "ProgressTracker.h"
|
2012-12-18 02:05:18 +04:00
|
|
|
#include "ImageFactory.h"
|
2013-02-14 01:53:42 +04:00
|
|
|
#include "Image.h"
|
2015-01-07 12:37:20 +03:00
|
|
|
#include "MultipartImage.h"
|
2013-09-28 22:28:42 +04:00
|
|
|
#include "RasterImage.h"
|
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-01-23 01:01:03 +03:00
|
|
|
#include "nsIChannel.h"
|
2015-11-12 23:09:52 +03:00
|
|
|
#include "nsICacheInfoChannel.h"
|
2020-05-11 17:43:05 +03:00
|
|
|
#include "nsIClassOfService.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2013-09-28 22:28:42 +04:00
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
2001-01-23 01:01:03 +03:00
|
|
|
#include "nsIInputStream.h"
|
2002-03-11 01:29:14 +03:00
|
|
|
#include "nsIMultiPartChannel.h"
|
2002-09-19 02:08:12 +04:00
|
|
|
#include "nsIHttpChannel.h"
|
2013-01-09 01:40:47 +04:00
|
|
|
#include "nsMimeTypes.h"
|
2001-03-25 15:57:25 +04:00
|
|
|
|
2008-12-23 01:20:46 +03:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2005-03-24 00:09:02 +03:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2007-11-09 05:55:41 +03:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2014-02-15 02:56:01 +04:00
|
|
|
#include "nsContentUtils.h"
|
2022-05-06 13:00:52 +03:00
|
|
|
#include "nsEscape.h"
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2002-09-19 02:08:12 +04:00
|
|
|
#include "plstr.h" // PL_strcasestr(...)
|
2017-12-07 16:28:28 +03:00
|
|
|
#include "prtime.h" // for PR_Now
|
2010-02-12 00:59:00 +03:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIProtocolHandler.h"
|
2013-09-07 17:01:08 +04:00
|
|
|
#include "imgIRequest.h"
|
2018-09-15 19:47:04 +03:00
|
|
|
#include "nsProperties.h"
|
2022-05-06 13:00:52 +03:00
|
|
|
#include "nsIURL.h"
|
2010-07-12 05:51:52 +04:00
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2018-09-25 16:13:52 +03:00
|
|
|
#include "mozilla/SizeOfState.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
|
2011-06-12 06:30:15 +04:00
|
|
|
using namespace mozilla;
|
2012-01-06 20:02:27 +04:00
|
|
|
using namespace mozilla::image;
|
2010-08-14 08:09:48 +04:00
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
#define LOG_TEST(level) (MOZ_LOG_TEST(gImgLog, (level)))
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(imgRequest, nsIStreamListener, nsIRequestObserver,
|
|
|
|
nsIThreadRetargetableStreamListener, nsIChannelEventSink,
|
|
|
|
nsIInterfaceRequestor, nsIAsyncVerifyRedirectCallback)
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2015-05-20 20:21:13 +03:00
|
|
|
imgRequest::imgRequest(imgLoader* aLoader, const ImageCacheKey& aCacheKey)
|
2012-09-07 02:05:23 +04:00
|
|
|
: mLoader(aLoader),
|
2015-05-20 20:21:13 +03:00
|
|
|
mCacheKey(aCacheKey),
|
|
|
|
mLoadId(nullptr),
|
2015-08-25 05:49:33 +03:00
|
|
|
mFirstProxy(nullptr),
|
2012-09-07 02:05:23 +04:00
|
|
|
mValidator(nullptr),
|
2021-05-05 18:23:25 +03:00
|
|
|
mCORSMode(CORS_NONE),
|
2014-07-19 05:47:11 +04:00
|
|
|
mImageErrorCode(NS_OK),
|
2019-05-02 02:05:47 +03:00
|
|
|
mImageAvailable(false),
|
2020-05-26 05:06:00 +03:00
|
|
|
mIsDeniedCrossSiteCORSRequest(false),
|
|
|
|
mIsCrossSiteNoCORSRequest(false),
|
2015-03-24 05:37:45 +03:00
|
|
|
mMutex("imgRequest"),
|
|
|
|
mProgressTracker(new ProgressTracker()),
|
2012-09-07 02:05:23 +04:00
|
|
|
mIsMultiPartChannel(false),
|
|
|
|
mIsInCache(false),
|
2015-03-24 05:37:45 +03:00
|
|
|
mDecodeRequested(false),
|
2015-01-07 12:37:20 +03:00
|
|
|
mNewPartPending(false),
|
2022-03-21 23:04:39 +03:00
|
|
|
mHadInsecureRedirect(false),
|
|
|
|
mInnerWindowId(0) {
|
2017-07-25 18:14:00 +03:00
|
|
|
LOG_FUNC(gImgLog, "imgRequest::imgRequest()");
|
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2001-02-21 02:27:48 +03:00
|
|
|
imgRequest::~imgRequest() {
|
2014-09-06 01:36:11 +04:00
|
|
|
if (mLoader) {
|
|
|
|
mLoader->RemoveFromUncachedImages(this);
|
|
|
|
}
|
2011-07-01 21:03:40 +04:00
|
|
|
if (mURI) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog, "imgRequest::~imgRequest()", "keyuri", mURI);
|
2009-01-31 05:17:47 +03:00
|
|
|
} else
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_FUNC(gImgLog, "imgRequest::~imgRequest()");
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
2022-03-21 23:04:39 +03:00
|
|
|
nsresult imgRequest::Init(
|
|
|
|
nsIURI* aURI, nsIURI* aFinalURI, bool aHadInsecureRedirect,
|
|
|
|
nsIRequest* aRequest, nsIChannel* aChannel, imgCacheEntry* aCacheEntry,
|
|
|
|
mozilla::dom::Document* aLoadingDocument,
|
|
|
|
nsIPrincipal* aTriggeringPrincipal, mozilla::CORSMode aCORSMode,
|
2022-08-03 19:39:41 +03:00
|
|
|
nsIReferrerInfo* aReferrerInfo) MOZ_NO_THREAD_SAFETY_ANALYSIS {
|
2013-09-28 22:28:42 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Cannot use nsIURI off main thread!");
|
2021-12-16 17:15:32 +03:00
|
|
|
// Init() can only be called once, and that's before it can be used off
|
|
|
|
// mainthread
|
2013-09-28 22:28:42 +04:00
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_FUNC(gImgLog, "imgRequest::Init");
|
2001-02-22 05:12:21 +03:00
|
|
|
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!mImage, "Multiple calls to init");
|
|
|
|
MOZ_ASSERT(aURI, "No uri");
|
2017-11-13 11:31:24 +03:00
|
|
|
MOZ_ASSERT(aFinalURI, "No final uri");
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aRequest, "No request");
|
|
|
|
MOZ_ASSERT(aChannel, "No channel");
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2018-09-15 19:47:04 +03:00
|
|
|
mProperties = new nsProperties();
|
2018-06-06 03:42:57 +03:00
|
|
|
mURI = aURI;
|
2017-11-13 11:31:24 +03:00
|
|
|
mFinalURI = aFinalURI;
|
2006-03-11 23:32:47 +03:00
|
|
|
mRequest = aRequest;
|
2008-12-23 01:20:46 +03:00
|
|
|
mChannel = aChannel;
|
2011-06-10 01:11:57 +04:00
|
|
|
mTimedChannel = do_QueryInterface(mChannel);
|
2017-10-03 06:38:30 +03:00
|
|
|
mTriggeringPrincipal = aTriggeringPrincipal;
|
2011-07-14 22:47:34 +04:00
|
|
|
mCORSMode = aCORSMode;
|
2019-07-25 11:23:32 +03:00
|
|
|
mReferrerInfo = aReferrerInfo;
|
2011-07-14 22:47:32 +04:00
|
|
|
|
2017-11-13 11:31:24 +03:00
|
|
|
// If the original URI and the final URI are different, check whether the
|
|
|
|
// original URI is secure. We deliberately don't take the final URI into
|
2014-12-16 04:40:43 +03:00
|
|
|
// account, as it needs to be handled using more complicated rules than
|
|
|
|
// earlier elements of the redirect chain.
|
2017-11-13 11:31:24 +03:00
|
|
|
if (aURI != aFinalURI) {
|
2014-12-16 04:40:43 +03:00
|
|
|
bool schemeLocal = false;
|
2019-08-03 12:44:12 +03:00
|
|
|
if (NS_FAILED(NS_URIChainHasFlags(
|
2015-03-31 21:06:00 +03:00
|
|
|
aURI, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE, &schemeLocal)) ||
|
2019-08-03 12:44:12 +03:00
|
|
|
(!aURI->SchemeIs("https") && !aURI->SchemeIs("chrome") &&
|
|
|
|
!schemeLocal)) {
|
2014-12-16 04:40:43 +03:00
|
|
|
mHadInsecureRedirect = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// imgCacheValidator may have handled redirects before we were created, so we
|
|
|
|
// allow the caller to let us know if any redirects were insecure.
|
|
|
|
mHadInsecureRedirect = mHadInsecureRedirect || aHadInsecureRedirect;
|
|
|
|
|
2008-12-23 01:20:46 +03:00
|
|
|
mChannel->GetNotificationCallbacks(getter_AddRefs(mPrevChannelSink));
|
|
|
|
|
|
|
|
NS_ASSERTION(mPrevChannelSink != this,
|
|
|
|
"Initializing with a channel that already calls back to us!");
|
|
|
|
|
|
|
|
mChannel->SetNotificationCallbacks(this);
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2001-03-16 05:40:04 +03:00
|
|
|
mCacheEntry = aCacheEntry;
|
2015-10-20 08:15:25 +03:00
|
|
|
mCacheEntry->UpdateLoadTime();
|
2001-03-16 05:40:04 +03:00
|
|
|
|
2020-04-11 00:14:49 +03:00
|
|
|
SetLoadId(aLoadingDocument);
|
2015-03-24 05:37:45 +03:00
|
|
|
|
|
|
|
// Grab the inner window ID of the loading document, if possible.
|
2020-04-11 00:14:49 +03:00
|
|
|
if (aLoadingDocument) {
|
|
|
|
mInnerWindowId = aLoadingDocument->InnerWindowID();
|
2015-03-24 05:37:45 +03:00
|
|
|
}
|
2002-01-15 08:23:33 +03:00
|
|
|
|
2001-01-23 05:02:29 +03:00
|
|
|
return NS_OK;
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
2020-07-30 11:55:24 +03:00
|
|
|
bool imgRequest::CanReuseWithoutValidation(dom::Document* aDoc) const {
|
|
|
|
// If the request's loadId is the same as the aLoadingDocument, then it is ok
|
|
|
|
// to use this one because it has already been validated for this context.
|
|
|
|
// XXX: nullptr seems to be a 'special' key value that indicates that NO
|
|
|
|
// validation is required.
|
|
|
|
// 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.
|
|
|
|
void* key = (void*)aDoc;
|
|
|
|
uint64_t innerWindowID = aDoc ? aDoc->InnerWindowID() : 0;
|
|
|
|
if (LoadId() == key && InnerWindowID() == innerWindowID) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// As a special-case, if this is a print preview document, also validate on
|
|
|
|
// the original document. This allows to print uncacheable images.
|
|
|
|
if (dom::Document* original = aDoc ? aDoc->GetOriginalDocument() : nullptr) {
|
|
|
|
return CanReuseWithoutValidation(original);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
void imgRequest::ClearLoader() { mLoader = nullptr; }
|
2014-09-06 01:36:11 +04:00
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
already_AddRefed<ProgressTracker> imgRequest::GetProgressTracker() const {
|
2015-03-24 05:37:45 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2015-01-07 12:37:20 +03:00
|
|
|
if (mImage) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!mProgressTracker,
|
|
|
|
"Should have given mProgressTracker to mImage");
|
2014-11-15 07:10:47 +03:00
|
|
|
return mImage->GetProgressTracker();
|
2010-08-24 02:44:07 +04:00
|
|
|
}
|
2017-02-10 15:52:27 +03:00
|
|
|
MOZ_ASSERT(mProgressTracker,
|
|
|
|
"Should have mProgressTracker until we create mImage");
|
|
|
|
RefPtr<ProgressTracker> progressTracker = mProgressTracker;
|
|
|
|
MOZ_ASSERT(progressTracker);
|
|
|
|
return progressTracker.forget();
|
2010-08-24 02:44:07 +04:00
|
|
|
}
|
|
|
|
|
2017-02-10 15:52:27 +03:00
|
|
|
void imgRequest::SetCacheEntry(imgCacheEntry* entry) { mCacheEntry = entry; }
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
bool imgRequest::HasCacheEntry() const { return mCacheEntry != nullptr; }
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
void imgRequest::ResetCacheEntry() {
|
2012-10-12 20:11:21 +04:00
|
|
|
if (HasCacheEntry()) {
|
|
|
|
mCacheEntry->SetDataSize(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
void imgRequest::AddProxy(imgRequestProxy* proxy) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(proxy, "null imgRequestProxy passed in");
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_SCOPE_WITH_PARAM(gImgLog, "imgRequest::AddProxy", "proxy", proxy);
|
2001-02-22 05:06:24 +03:00
|
|
|
|
2015-08-25 05:49:33 +03:00
|
|
|
if (!mFirstProxy) {
|
|
|
|
// Save a raw pointer to the first proxy we see, for use in the network
|
|
|
|
// priority logic.
|
|
|
|
mFirstProxy = proxy;
|
|
|
|
}
|
|
|
|
|
2009-01-31 05:17:47 +03:00
|
|
|
// If we're empty before adding, we have to tell the loader we now have
|
|
|
|
// proxies.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2015-01-07 12:35:20 +03:00
|
|
|
if (progressTracker->ObserverCount() == 0) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(mURI, "Trying to SetHasProxies without key uri.");
|
2014-09-06 01:36:11 +04:00
|
|
|
if (mLoader) {
|
2014-09-11 06:47:00 +04:00
|
|
|
mLoader->SetHasProxies(this);
|
2014-09-06 01:36:11 +04:00
|
|
|
}
|
2009-01-31 05:17:47 +03:00
|
|
|
}
|
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
progressTracker->AddObserver(proxy);
|
2002-01-15 08:23:33 +03:00
|
|
|
}
|
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
nsresult imgRequest::RemoveProxy(imgRequestProxy* proxy, nsresult aStatus) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_SCOPE_WITH_PARAM(gImgLog, "imgRequest::RemoveProxy", "proxy", proxy);
|
2002-01-15 08:23:33 +03:00
|
|
|
|
2010-09-08 04:33:02 +04:00
|
|
|
// This will remove our animation consumers, so after removing
|
|
|
|
// this proxy, we don't end up without proxies with observers, but still
|
|
|
|
// have animation consumers.
|
|
|
|
proxy->ClearAnimationConsumers();
|
|
|
|
|
2010-05-15 00:47:59 +04:00
|
|
|
// Let the status tracker do its thing before we potentially call Cancel()
|
|
|
|
// below, because Cancel() may result in OnStopRequest being called back
|
|
|
|
// before Cancel() returns, leaving the image in a different state then the
|
|
|
|
// one it was in at this point.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2015-03-31 21:06:00 +03:00
|
|
|
if (!progressTracker->RemoveObserver(proxy)) {
|
2012-10-12 20:11:20 +04:00
|
|
|
return NS_OK;
|
2015-03-31 21:06:00 +03:00
|
|
|
}
|
2002-01-15 08:23:33 +03:00
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
if (progressTracker->ObserverCount() == 0) {
|
2009-01-31 05:17:47 +03:00
|
|
|
// If we have no observers, there's nothing holding us alive. If we haven't
|
|
|
|
// been cancelled and thus removed from the cache, tell the image loader so
|
|
|
|
// we can be evicted from the cache.
|
|
|
|
if (mCacheEntry) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(mURI, "Removing last observer without key uri.");
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2014-09-06 01:36:11 +04:00
|
|
|
if (mLoader) {
|
2014-09-11 06:47:00 +04:00
|
|
|
mLoader->SetHasNoProxies(this, mCacheEntry);
|
2014-09-06 01:36:11 +04:00
|
|
|
}
|
2018-06-06 03:42:57 +03:00
|
|
|
} else {
|
2015-03-31 21:06:00 +03:00
|
|
|
LOG_MSG_WITH_PARAM(gImgLog, "imgRequest::RemoveProxy no cache entry",
|
2018-06-06 03:42:57 +03:00
|
|
|
"uri", mURI);
|
2009-01-31 05:17:47 +03:00
|
|
|
}
|
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
/* If |aStatus| is a failure code, then cancel the load if it is still in
|
|
|
|
progress. Otherwise, let the load continue, keeping 'this' in the cache
|
|
|
|
with no observers. This way, if a proxy is destroyed without calling
|
|
|
|
cancel on it, it won't leak and won't leave a bad pointer in the observer
|
|
|
|
list.
|
2002-01-15 08:23:33 +03:00
|
|
|
*/
|
2015-01-25 10:16:46 +03:00
|
|
|
if (!(progressTracker->GetProgress() & FLAG_LAST_PART_COMPLETE) &&
|
|
|
|
NS_FAILED(aStatus)) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_MSG(gImgLog, "imgRequest::RemoveProxy",
|
2015-03-31 21:06:00 +03:00
|
|
|
"load in progress. canceling");
|
2002-01-15 08:23:33 +03:00
|
|
|
|
|
|
|
this->Cancel(NS_BINDING_ABORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* break the cycle from the cache entry. */
|
2012-07-30 18:20:58 +04:00
|
|
|
mCacheEntry = nullptr;
|
2002-01-15 08:23:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2022-02-14 13:00:40 +03:00
|
|
|
uint64_t imgRequest::InnerWindowID() const {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mInnerWindowId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void imgRequest::SetInnerWindowID(uint64_t aInnerWindowId) {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mInnerWindowId = aInnerWindowId;
|
|
|
|
}
|
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
void imgRequest::CancelAndAbort(nsresult aStatus) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_SCOPE(gImgLog, "imgRequest::CancelAndAbort");
|
2009-01-31 05:17:47 +03:00
|
|
|
|
2008-12-23 01:20:46 +03:00
|
|
|
Cancel(aStatus);
|
|
|
|
|
|
|
|
// It's possible for the channel to fail to open after we've set our
|
|
|
|
// notification callbacks. In that case, make sure to break the cycle between
|
|
|
|
// the channel and us, because it won't.
|
|
|
|
if (mChannel) {
|
|
|
|
mChannel->SetNotificationCallbacks(mPrevChannelSink);
|
2012-07-30 18:20:58 +04:00
|
|
|
mPrevChannelSink = nullptr;
|
2008-12-23 01:20:46 +03:00
|
|
|
}
|
2001-03-06 07:46:20 +03:00
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class imgRequestMainThreadCancel : public Runnable {
|
2013-10-16 03:33:11 +04:00
|
|
|
public:
|
2015-03-31 21:06:00 +03:00
|
|
|
imgRequestMainThreadCancel(imgRequest* aImgRequest, nsresult aStatus)
|
2017-05-10 02:19:37 +03:00
|
|
|
: Runnable("imgRequestMainThreadCancel"),
|
|
|
|
mImgRequest(aImgRequest),
|
2013-10-16 03:33:11 +04:00
|
|
|
mStatus(aStatus) {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread(), "Create me off main thread only!");
|
|
|
|
MOZ_ASSERT(aImgRequest);
|
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2013-10-16 03:33:11 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "I should be running on the main thread!");
|
|
|
|
mImgRequest->ContinueCancel(mStatus);
|
|
|
|
return NS_OK;
|
2015-03-31 21:06:00 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-16 03:33:11 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> mImgRequest;
|
2013-10-16 03:33:11 +04:00
|
|
|
nsresult mStatus;
|
|
|
|
};
|
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
void imgRequest::Cancel(nsresult aStatus) {
|
2010-05-15 00:47:59 +04:00
|
|
|
/* The Cancel() method here should only be called by this class. */
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_SCOPE(gImgLog, "imgRequest::Cancel");
|
2010-05-15 00:47:59 +04:00
|
|
|
|
2013-09-28 22:28:42 +04:00
|
|
|
if (NS_IsMainThread()) {
|
2013-10-16 03:33:11 +04:00
|
|
|
ContinueCancel(aStatus);
|
2013-09-28 22:28:42 +04:00
|
|
|
} else {
|
2017-08-14 23:59:58 +03:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
|
|
|
nsCOMPtr<nsIEventTarget> eventTarget = progressTracker->GetEventTarget();
|
|
|
|
nsCOMPtr<nsIRunnable> ev = new imgRequestMainThreadCancel(this, aStatus);
|
|
|
|
eventTarget->Dispatch(ev.forget(), NS_DISPATCH_NORMAL);
|
2013-09-28 22:28:42 +04:00
|
|
|
}
|
2013-10-16 03:33:11 +04:00
|
|
|
}
|
2010-05-15 00:47:59 +04:00
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
void imgRequest::ContinueCancel(nsresult aStatus) {
|
2013-10-16 03:33:11 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2017-11-01 13:59:10 +03:00
|
|
|
progressTracker->SyncNotifyProgress(FLAG_HAS_ERROR);
|
2014-11-15 07:10:47 +03:00
|
|
|
|
2013-10-16 03:33:11 +04:00
|
|
|
RemoveFromCache();
|
|
|
|
|
2015-01-25 10:16:46 +03:00
|
|
|
if (mRequest && !(progressTracker->GetProgress() & FLAG_LAST_PART_COMPLETE)) {
|
2013-10-16 03:33:11 +04:00
|
|
|
mRequest->Cancel(aStatus);
|
|
|
|
}
|
2010-05-15 00:47:59 +04:00
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class imgRequestMainThreadEvict : public Runnable {
|
2014-06-09 02:15:00 +04:00
|
|
|
public:
|
2015-03-31 21:06:00 +03:00
|
|
|
explicit imgRequestMainThreadEvict(imgRequest* aImgRequest)
|
2017-05-10 02:19:37 +03:00
|
|
|
: Runnable("imgRequestMainThreadEvict"), mImgRequest(aImgRequest) {
|
2014-06-09 02:15:00 +04:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread(), "Create me off main thread only!");
|
|
|
|
MOZ_ASSERT(aImgRequest);
|
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2014-06-09 02:15:00 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "I should be running on the main thread!");
|
|
|
|
mImgRequest->ContinueEvict();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-06-09 02:15:00 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> mImgRequest;
|
2014-06-09 02:15:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// EvictFromCache() is written to allowed to get called from any thread
|
2015-03-31 21:06:00 +03:00
|
|
|
void imgRequest::EvictFromCache() {
|
2014-06-09 02:15:00 +04:00
|
|
|
/* The EvictFromCache() method here should only be called by this class. */
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_SCOPE(gImgLog, "imgRequest::EvictFromCache");
|
2014-06-09 02:15:00 +04:00
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
ContinueEvict();
|
|
|
|
} else {
|
|
|
|
NS_DispatchToMainThread(new imgRequestMainThreadEvict(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper-method used by EvictFromCache()
|
2015-03-31 21:06:00 +03:00
|
|
|
void imgRequest::ContinueEvict() {
|
2014-06-09 02:15:00 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
RemoveFromCache();
|
|
|
|
}
|
|
|
|
|
2015-10-30 02:37:42 +03:00
|
|
|
void imgRequest::StartDecoding() {
|
2015-03-24 05:37:45 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mDecodeRequested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool imgRequest::IsDecodeRequested() const {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mDecodeRequested;
|
|
|
|
}
|
|
|
|
|
2018-06-06 03:42:57 +03:00
|
|
|
nsresult imgRequest::GetURI(nsIURI** aURI) {
|
2013-09-28 22:28:42 +04:00
|
|
|
MOZ_ASSERT(aURI);
|
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_FUNC(gImgLog, "imgRequest::GetURI");
|
2001-03-06 07:46:20 +03:00
|
|
|
|
2001-10-12 10:43:52 +04:00
|
|
|
if (mURI) {
|
|
|
|
*aURI = mURI;
|
|
|
|
NS_ADDREF(*aURI);
|
2001-04-14 06:02:19 +04:00
|
|
|
return NS_OK;
|
2001-10-12 10:43:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2001-04-10 10:01:08 +04:00
|
|
|
}
|
|
|
|
|
2017-11-13 11:31:24 +03:00
|
|
|
nsresult imgRequest::GetFinalURI(nsIURI** aURI) {
|
2014-11-11 01:06:24 +03:00
|
|
|
MOZ_ASSERT(aURI);
|
|
|
|
|
2017-11-13 11:31:24 +03:00
|
|
|
LOG_FUNC(gImgLog, "imgRequest::GetFinalURI");
|
2014-11-11 01:06:24 +03:00
|
|
|
|
2017-11-13 11:31:24 +03:00
|
|
|
if (mFinalURI) {
|
|
|
|
*aURI = mFinalURI;
|
2014-11-11 01:06:24 +03:00
|
|
|
NS_ADDREF(*aURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-08-09 18:17:06 +03:00
|
|
|
bool imgRequest::IsChrome() const { return mURI->SchemeIs("chrome"); }
|
2017-09-25 18:44:49 +03:00
|
|
|
|
2019-08-09 18:17:06 +03:00
|
|
|
bool imgRequest::IsData() const { return mURI->SchemeIs("data"); }
|
2015-04-28 21:45:45 +03:00
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
nsresult imgRequest::GetImageErrorCode() { return mImageErrorCode; }
|
2014-07-19 05:47:11 +04:00
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
void imgRequest::RemoveFromCache() {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_SCOPE(gImgLog, "imgRequest::RemoveFromCache");
|
2001-02-22 05:06:24 +03:00
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
bool isInCache = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
isInCache = mIsInCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isInCache && mLoader) {
|
2009-03-18 00:07:16 +03:00
|
|
|
// mCacheEntry is nulled out when we have no more observers.
|
2014-09-06 01:36:11 +04:00
|
|
|
if (mCacheEntry) {
|
2012-06-26 08:20:12 +04:00
|
|
|
mLoader->RemoveFromCache(mCacheEntry);
|
2014-09-06 01:36:11 +04:00
|
|
|
} else {
|
2015-10-27 21:12:46 +03:00
|
|
|
mLoader->RemoveFromCache(mCacheKey);
|
2014-09-06 01:36:11 +04:00
|
|
|
}
|
2009-03-05 06:56:14 +03:00
|
|
|
}
|
2009-03-18 00:07:16 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mCacheEntry = nullptr;
|
2001-01-23 01:01:03 +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
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
bool imgRequest::HasConsumers() const {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2015-01-07 12:35:20 +03:00
|
|
|
return progressTracker && progressTracker->ObserverCount() > 0;
|
2014-09-06 01:36:11 +04:00
|
|
|
}
|
|
|
|
|
2017-03-21 21:09:14 +03:00
|
|
|
already_AddRefed<image::Image> imgRequest::GetImage() const {
|
2015-03-24 05:37:45 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2017-03-21 21:09:14 +03:00
|
|
|
RefPtr<image::Image> image = mImage;
|
2015-03-24 05:37:45 +03:00
|
|
|
return image.forget();
|
|
|
|
}
|
|
|
|
|
2022-05-06 13:00:52 +03:00
|
|
|
void imgRequest::GetFileName(nsACString& aFileName) {
|
|
|
|
nsAutoString fileName;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsCString> supportscstr;
|
|
|
|
if (NS_SUCCEEDED(mProperties->Get("content-disposition",
|
|
|
|
NS_GET_IID(nsISupportsCString),
|
|
|
|
getter_AddRefs(supportscstr))) &&
|
|
|
|
supportscstr) {
|
|
|
|
nsAutoCString cdHeader;
|
|
|
|
supportscstr->GetData(cdHeader);
|
|
|
|
NS_GetFilenameFromDisposition(fileName, cdHeader);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileName.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIURL> imgUrl(do_QueryInterface(mURI));
|
|
|
|
if (imgUrl) {
|
|
|
|
imgUrl->GetFileName(aFileName);
|
|
|
|
NS_UnescapeURL(aFileName);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
aFileName = NS_ConvertUTF16toUTF8(fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t imgRequest::Priority() const {
|
|
|
|
int32_t priority = nsISupportsPriority::PRIORITY_NORMAL;
|
2006-03-11 23:32:47 +03:00
|
|
|
nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mRequest);
|
2015-03-31 21:06:00 +03:00
|
|
|
if (p) {
|
2005-02-21 23:58:01 +03:00
|
|
|
p->GetPriority(&priority);
|
2015-03-31 21:06:00 +03:00
|
|
|
}
|
2005-02-21 23:58:01 +03:00
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
void imgRequest::AdjustPriority(imgRequestProxy* proxy, int32_t delta) {
|
2005-02-21 23:58:01 +03:00
|
|
|
// only the first proxy is allowed to modify the priority of this image load.
|
|
|
|
//
|
|
|
|
// XXX(darin): this is probably not the most optimal algorithm as we may want
|
|
|
|
// to increase the priority of requests that have a lot of proxies. the key
|
|
|
|
// concern though is that image loads remain lower priority than other pieces
|
|
|
|
// of content such as link clicks, CSS, and JS.
|
|
|
|
//
|
2015-08-25 05:49:33 +03:00
|
|
|
if (!mFirstProxy || proxy != mFirstProxy) {
|
2005-02-21 23:58:01 +03:00
|
|
|
return;
|
2015-03-31 21:06:00 +03:00
|
|
|
}
|
2005-02-21 23:58:01 +03:00
|
|
|
|
2017-03-22 14:52:15 +03:00
|
|
|
AdjustPriorityInternal(delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
void imgRequest::AdjustPriorityInternal(int32_t aDelta) {
|
2012-10-13 20:47:36 +04:00
|
|
|
nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mChannel);
|
2015-03-31 21:06:00 +03:00
|
|
|
if (p) {
|
2017-03-22 14:52:15 +03:00
|
|
|
p->AdjustPriority(aDelta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void imgRequest::BoostPriority(uint32_t aCategory) {
|
2019-06-26 06:24:21 +03:00
|
|
|
if (!StaticPrefs::image_layout_network_priority()) {
|
2017-07-14 16:26:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:52:15 +03:00
|
|
|
uint32_t newRequestedCategory =
|
|
|
|
(mBoostCategoriesRequested & aCategory) ^ aCategory;
|
|
|
|
if (!newRequestedCategory) {
|
|
|
|
// priority boost for each category can only apply once.
|
|
|
|
return;
|
2015-03-31 21:06:00 +03:00
|
|
|
}
|
2017-03-22 14:52:15 +03:00
|
|
|
|
|
|
|
MOZ_LOG(gImgLog, LogLevel::Debug,
|
|
|
|
("[this=%p] imgRequest::BoostPriority for category %x", this,
|
|
|
|
newRequestedCategory));
|
|
|
|
|
|
|
|
int32_t delta = 0;
|
|
|
|
|
|
|
|
if (newRequestedCategory & imgIRequest::CATEGORY_FRAME_INIT) {
|
2019-03-28 05:54:16 +03:00
|
|
|
--delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newRequestedCategory & imgIRequest::CATEGORY_FRAME_STYLE) {
|
2017-03-22 14:52:15 +03:00
|
|
|
--delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newRequestedCategory & imgIRequest::CATEGORY_SIZE_QUERY) {
|
|
|
|
--delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newRequestedCategory & imgIRequest::CATEGORY_DISPLAY) {
|
|
|
|
delta += nsISupportsPriority::PRIORITY_HIGH;
|
|
|
|
}
|
|
|
|
|
|
|
|
AdjustPriorityInternal(delta);
|
|
|
|
mBoostCategoriesRequested |= newRequestedCategory;
|
2005-02-21 23:58:01 +03:00
|
|
|
}
|
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
void imgRequest::SetIsInCache(bool aInCache) {
|
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog, "imgRequest::SetIsCacheable", "aInCache",
|
|
|
|
aInCache);
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mIsInCache = aInCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void imgRequest::UpdateCacheEntrySize() {
|
|
|
|
if (!mCacheEntry) {
|
|
|
|
return;
|
2014-11-27 05:00:15 +03:00
|
|
|
}
|
2015-03-24 05:37:45 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> image = GetImage();
|
2017-07-28 13:10:04 +03:00
|
|
|
SizeOfState state(moz_malloc_size_of);
|
|
|
|
size_t size = image->SizeOfSourceWithComputedFallback(state);
|
2015-03-24 05:37:45 +03:00
|
|
|
mCacheEntry->SetDataSize(size);
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
void imgRequest::SetCacheValidation(imgCacheEntry* aCacheEntry,
|
|
|
|
nsIRequest* aRequest) {
|
2011-07-01 21:03:33 +04:00
|
|
|
/* get the expires info */
|
2020-06-12 22:06:04 +03:00
|
|
|
if (!aCacheEntry || aCacheEntry->GetExpiryTime() != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-12 02:40:38 +03:00
|
|
|
|
2021-09-09 12:32:11 +03:00
|
|
|
RefPtr<imgRequest> req = aCacheEntry->GetRequest();
|
|
|
|
MOZ_ASSERT(req);
|
|
|
|
RefPtr<nsIURI> uri;
|
|
|
|
req->GetURI(getter_AddRefs(uri));
|
|
|
|
// TODO(emilio): Seems we should be able to assert `uri` is not null, but we
|
|
|
|
// get here in such cases sometimes (like for some redirects, see
|
|
|
|
// docshell/test/chrome/test_bug89419.xhtml).
|
|
|
|
//
|
|
|
|
// We have the original URI in the cache key though, probably we should be
|
|
|
|
// using that instead of relying on Init() getting called.
|
2022-01-19 22:58:13 +03:00
|
|
|
auto info = nsContentUtils::GetSubresourceCacheValidationInfo(aRequest, uri);
|
2020-06-12 02:40:38 +03:00
|
|
|
|
2020-06-12 22:06:04 +03:00
|
|
|
// Expiration time defaults to 0. We set the expiration time on our entry if
|
|
|
|
// it hasn't been set yet.
|
|
|
|
if (!info.mExpirationTime) {
|
|
|
|
// If the channel doesn't support caching, then ensure this expires the
|
|
|
|
// next time it is used.
|
|
|
|
info.mExpirationTime.emplace(nsContentUtils::SecondsFromPRTime(PR_Now()) -
|
|
|
|
1);
|
|
|
|
}
|
|
|
|
aCacheEntry->SetExpiryTime(*info.mExpirationTime);
|
|
|
|
// Cache entries default to not needing to validate. We ensure that
|
|
|
|
// multiple calls to this function don't override an earlier decision to
|
|
|
|
// validate by making validation a one-way decision.
|
|
|
|
if (info.mMustRevalidate) {
|
|
|
|
aCacheEntry->SetMustValidate(info.mMustRevalidate);
|
2011-07-01 21:03:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
bool imgRequest::GetMultipart() const {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mIsMultiPartChannel;
|
|
|
|
}
|
|
|
|
|
2015-03-30 02:25:48 +03:00
|
|
|
bool imgRequest::HadInsecureRedirect() const {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mHadInsecureRedirect;
|
|
|
|
}
|
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
/** nsIRequestObserver methods **/
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:04 +03:00
|
|
|
imgRequest::OnStartRequest(nsIRequest* aRequest) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_SCOPE(gImgLog, "imgRequest::OnStartRequest");
|
2001-02-22 05:06:24 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> image;
|
2015-01-07 12:37:20 +03:00
|
|
|
|
2020-05-11 17:43:05 +03:00
|
|
|
if (nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aRequest)) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = httpChannel->LoadInfo();
|
|
|
|
mIsDeniedCrossSiteCORSRequest =
|
|
|
|
loadInfo->GetTainting() == LoadTainting::CORS &&
|
|
|
|
(NS_FAILED(httpChannel->GetStatus(&rv)) || NS_FAILED(rv));
|
|
|
|
mIsCrossSiteNoCORSRequest = loadInfo->GetTainting() == LoadTainting::Opaque;
|
|
|
|
}
|
|
|
|
|
2015-01-07 12:37:20 +03:00
|
|
|
// Figure out if we're multipart.
|
2015-03-24 05:37:45 +03:00
|
|
|
nsCOMPtr<nsIMultiPartChannel> multiPartChannel = do_QueryInterface(aRequest);
|
2015-03-31 21:06:00 +03:00
|
|
|
{
|
2015-03-24 05:37:45 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2020-05-26 05:06:07 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(multiPartChannel || !mIsMultiPartChannel,
|
|
|
|
"Stopped being multipart?");
|
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
mNewPartPending = true;
|
|
|
|
image = mImage;
|
|
|
|
mIsMultiPartChannel = bool(multiPartChannel);
|
2012-12-20 01:28:54 +04:00
|
|
|
}
|
2002-02-27 03:32:19 +03:00
|
|
|
|
2015-03-05 09:18:14 +03:00
|
|
|
// If we're not multipart, we shouldn't have an image yet.
|
2015-03-24 05:37:45 +03:00
|
|
|
if (image && !multiPartChannel) {
|
2015-03-05 09:18:14 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Already have an image for a non-multipart request");
|
|
|
|
Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2008-09-30 00:46:53 +04:00
|
|
|
/*
|
|
|
|
* If mRequest is null here, then we need to set it so that we'll be able to
|
|
|
|
* cancel it if our Cancel() method is called. Note that this can only
|
|
|
|
* happen for multipart channels. We could simply not null out mRequest for
|
|
|
|
* non-last parts, if GetIsLastPart() were reliable, but it's not. See
|
|
|
|
* https://bugzilla.mozilla.org/show_bug.cgi?id=339610
|
|
|
|
*/
|
|
|
|
if (!mRequest) {
|
2015-03-24 05:37:45 +03:00
|
|
|
MOZ_ASSERT(multiPartChannel, "Should have mRequest unless we're multipart");
|
|
|
|
nsCOMPtr<nsIChannel> baseChannel;
|
|
|
|
multiPartChannel->GetBaseChannel(getter_AddRefs(baseChannel));
|
|
|
|
mRequest = baseChannel;
|
2008-09-30 00:46:53 +04:00
|
|
|
}
|
|
|
|
|
2008-09-02 00:53:59 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
|
2015-03-24 05:37:45 +03:00
|
|
|
if (channel) {
|
|
|
|
/* Get our principal */
|
2015-03-31 21:06:00 +03:00
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secMan =
|
|
|
|
nsContentUtils::GetSecurityManager();
|
2007-11-09 05:55:41 +03:00
|
|
|
if (secMan) {
|
2015-03-31 21:06:00 +03:00
|
|
|
nsresult rv = secMan->GetChannelResultPrincipal(
|
|
|
|
channel, getter_AddRefs(mPrincipal));
|
2007-11-09 05:55:41 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-02-22 11:41:20 +03:00
|
|
|
|
2011-07-01 21:03:33 +04:00
|
|
|
SetCacheValidation(mCacheEntry, aRequest);
|
2002-03-11 01:29:14 +03:00
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
// Shouldn't we be dead already if this gets hit?
|
|
|
|
// Probably multipart/x-mixed-replace...
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2015-01-07 12:35:20 +03:00
|
|
|
if (progressTracker->ObserverCount() == 0) {
|
2002-03-11 01:29:14 +03:00
|
|
|
this->Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
2017-09-25 18:44:49 +03:00
|
|
|
// Try to retarget OnDataAvailable to a decode thread. We must process data
|
|
|
|
// URIs synchronously as per the spec however.
|
|
|
|
if (!channel || IsData()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-28 22:28:42 +04:00
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> retargetable =
|
|
|
|
do_QueryInterface(aRequest);
|
2017-09-25 18:44:49 +03:00
|
|
|
if (retargetable) {
|
2013-09-28 22:28:42 +04:00
|
|
|
nsAutoCString mimeType;
|
2017-09-25 18:44:49 +03:00
|
|
|
nsresult rv = channel->GetContentType(mimeType);
|
2013-09-28 22:28:42 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !mimeType.EqualsLiteral(IMAGE_SVG_XML)) {
|
2015-01-18 12:27:16 +03:00
|
|
|
// Retarget OnDataAvailable to the DecodePool's IO thread.
|
2015-01-18 00:18:26 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> target =
|
2015-01-18 12:27:16 +03:00
|
|
|
DecodePool::Singleton()->GetIOEventTarget();
|
2013-09-28 22:28:42 +04:00
|
|
|
rv = retargetable->RetargetDeliveryTo(target);
|
|
|
|
}
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(gImgLog, LogLevel::Warning,
|
2013-09-28 22:28:42 +04:00
|
|
|
("[this=%p] imgRequest::OnStartRequest -- "
|
2016-12-16 06:16:31 +03:00
|
|
|
"RetargetDeliveryTo rv %" PRIu32 "=%s\n",
|
|
|
|
this, static_cast<uint32_t>(rv),
|
|
|
|
NS_SUCCEEDED(rv) ? "succeeded" : "failed"));
|
2013-09-28 22:28:42 +04:00
|
|
|
}
|
|
|
|
|
2001-01-23 01:01:03 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:31 +03:00
|
|
|
imgRequest::OnStopRequest(nsIRequest* aRequest, nsresult status) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_FUNC(gImgLog, "imgRequest::OnStopRequest");
|
2014-11-15 07:06:19 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Can't send notifications off-main-thread");
|
2001-02-21 01:43:56 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> image = GetImage();
|
2015-03-24 05:37:45 +03:00
|
|
|
|
2017-01-09 07:04:19 +03:00
|
|
|
RefPtr<imgRequest> strongThis = this;
|
|
|
|
|
2021-12-16 17:15:32 +03:00
|
|
|
bool isMultipart = false;
|
|
|
|
bool newPartPending = false;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
isMultipart = mIsMultiPartChannel;
|
|
|
|
newPartPending = mNewPartPending;
|
|
|
|
}
|
|
|
|
if (isMultipart && newPartPending) {
|
2019-02-28 02:42:27 +03:00
|
|
|
OnDataAvailable(aRequest, nullptr, 0, 0);
|
2017-01-09 07:04:19 +03:00
|
|
|
}
|
|
|
|
|
2006-06-01 22:35:21 +04:00
|
|
|
// XXXldb What if this is a non-last part of a multipart request?
|
2008-12-23 01:20:46 +03:00
|
|
|
// xxx before we release our reference to mRequest, lets
|
2006-11-10 01:47:43 +03:00
|
|
|
// save the last status that we saw so that the
|
|
|
|
// imgRequestProxy will have access to it.
|
2008-12-23 01:20:46 +03:00
|
|
|
if (mRequest) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mRequest = nullptr; // we no longer need the request
|
2006-12-11 07:15:45 +03:00
|
|
|
}
|
2001-02-26 07:22:47 +03:00
|
|
|
|
2008-12-23 01:20:46 +03:00
|
|
|
// stop holding a ref to the channel, since we don't need it anymore
|
|
|
|
if (mChannel) {
|
|
|
|
mChannel->SetNotificationCallbacks(mPrevChannelSink);
|
2012-07-30 18:20:58 +04:00
|
|
|
mPrevChannelSink = nullptr;
|
|
|
|
mChannel = nullptr;
|
2008-12-23 01:20:46 +03:00
|
|
|
}
|
|
|
|
|
2013-02-02 05:06:34 +04:00
|
|
|
bool lastPart = true;
|
|
|
|
nsCOMPtr<nsIMultiPartChannel> mpchan(do_QueryInterface(aRequest));
|
2015-03-31 21:06:00 +03:00
|
|
|
if (mpchan) {
|
2013-02-02 05:06:34 +04:00
|
|
|
mpchan->GetIsLastPart(&lastPart);
|
2015-03-31 21:06:00 +03:00
|
|
|
}
|
2013-02-02 05:06:34 +04:00
|
|
|
|
2014-06-09 02:15:00 +04:00
|
|
|
bool isPartial = false;
|
2015-03-24 05:37:45 +03:00
|
|
|
if (image && (status == NS_ERROR_NET_PARTIAL_TRANSFER)) {
|
2014-06-09 02:15:00 +04:00
|
|
|
isPartial = true;
|
|
|
|
status = NS_OK; // fake happy face
|
|
|
|
}
|
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
// Tell the image that it has all of the source data. Note that this can
|
|
|
|
// trigger a failure, since the image might be waiting for more non-optional
|
|
|
|
// data and this is the point where we break the news that it's not coming.
|
2015-03-24 05:37:45 +03:00
|
|
|
if (image) {
|
2021-11-24 14:25:41 +03:00
|
|
|
nsresult rv = image->OnImageDataComplete(aRequest, status, lastPart);
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2012-12-18 02:05:18 +04:00
|
|
|
// If we got an error in the OnImageDataComplete() call, we don't want to
|
|
|
|
// proceed as if nothing bad happened. However, we also want to give
|
|
|
|
// precedence to failure status codes from necko, since presumably they're
|
|
|
|
// more meaningful.
|
2015-03-31 21:06:00 +03:00
|
|
|
if (NS_FAILED(rv) && NS_SUCCEEDED(status)) {
|
2009-09-13 02:44:18 +04:00
|
|
|
status = rv;
|
2015-03-31 21:06:00 +03:00
|
|
|
}
|
2001-02-25 11:28:36 +03:00
|
|
|
}
|
2002-03-23 16:21:33 +03:00
|
|
|
|
2010-05-15 00:47:59 +04:00
|
|
|
// If the request went through, update the cache entry size. Otherwise,
|
|
|
|
// cancel the request, which removes us from the cache.
|
2015-03-24 05:37:45 +03:00
|
|
|
if (image && NS_SUCCEEDED(status) && !isPartial) {
|
2009-09-13 02:44:18 +04:00
|
|
|
// We update the cache entry size here because this is where we finish
|
|
|
|
// loading compressed source data, which is part of our size calculus.
|
|
|
|
UpdateCacheEntrySize();
|
2015-03-31 21:06:00 +03:00
|
|
|
|
|
|
|
} else if (isPartial) {
|
2014-06-09 02:15:00 +04:00
|
|
|
// Remove the partial image from the cache.
|
|
|
|
this->EvictFromCache();
|
2015-03-31 21:06:00 +03:00
|
|
|
|
|
|
|
} else {
|
2014-07-19 05:47:11 +04:00
|
|
|
mImageErrorCode = status;
|
|
|
|
|
2014-06-09 02:15:00 +04:00
|
|
|
// if the error isn't "just" a partial transfer
|
2010-05-15 00:47:59 +04:00
|
|
|
// stops animations, removes from cache
|
|
|
|
this->Cancel(status);
|
2009-09-13 02:44:18 +04:00
|
|
|
}
|
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
if (!image) {
|
2014-11-15 07:10:47 +03:00
|
|
|
// We have to fire the OnStopRequest notifications ourselves because there's
|
2013-02-02 05:06:34 +04:00
|
|
|
// no image capable of doing so.
|
2014-11-15 07:10:47 +03:00
|
|
|
Progress progress =
|
2014-11-18 01:29:56 +03:00
|
|
|
LoadCompleteProgress(lastPart, /* aError = */ false, status);
|
2014-11-15 07:06:19 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2014-11-15 07:10:47 +03:00
|
|
|
progressTracker->SyncNotifyProgress(progress);
|
2013-02-02 05:06:34 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mTimedChannel = nullptr;
|
2001-02-21 01:43:56 +03:00
|
|
|
return NS_OK;
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
2012-09-07 02:05:23 +04:00
|
|
|
struct mimetype_closure {
|
|
|
|
nsACString* newType;
|
|
|
|
};
|
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
/* prototype for these defined below */
|
2015-03-31 21:06:00 +03:00
|
|
|
static nsresult sniff_mimetype_callback(nsIInputStream* in, void* closure,
|
|
|
|
const char* fromRawSegment,
|
|
|
|
uint32_t toOffset, uint32_t count,
|
|
|
|
uint32_t* writeCount);
|
2001-03-14 03:39:48 +03:00
|
|
|
|
2013-09-28 22:28:42 +04:00
|
|
|
/** nsThreadRetargetableStreamListener methods **/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
imgRequest::CheckListenerChain() {
|
|
|
|
// TODO Might need more checking here.
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-23 01:01:03 +03:00
|
|
|
/** nsIStreamListener methods **/
|
|
|
|
|
2015-03-24 06:00:42 +03:00
|
|
|
struct NewPartResult final {
|
2017-03-21 21:09:14 +03:00
|
|
|
explicit NewPartResult(image::Image* aExistingImage)
|
2015-03-24 05:37:45 +03:00
|
|
|
: mImage(aExistingImage),
|
|
|
|
mIsFirstPart(!aExistingImage),
|
|
|
|
mSucceeded(false),
|
2015-07-07 03:11:14 +03:00
|
|
|
mShouldResetCacheEntry(false) {}
|
2015-03-24 05:37:45 +03:00
|
|
|
|
|
|
|
nsAutoCString mContentType;
|
|
|
|
nsAutoCString mContentDisposition;
|
2017-03-21 21:09:14 +03:00
|
|
|
RefPtr<image::Image> mImage;
|
2015-03-24 05:37:45 +03:00
|
|
|
const bool mIsFirstPart;
|
|
|
|
bool mSucceeded;
|
2015-07-07 03:11:14 +03:00
|
|
|
bool mShouldResetCacheEntry;
|
2015-03-24 05:37:45 +03:00
|
|
|
};
|
2001-02-25 11:28:36 +03:00
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
static NewPartResult PrepareForNewPart(nsIRequest* aRequest,
|
|
|
|
nsIInputStream* aInStr, uint32_t aCount,
|
2018-06-06 03:42:57 +03:00
|
|
|
nsIURI* aURI, bool aIsMultipart,
|
|
|
|
image::Image* aExistingImage,
|
2015-03-24 05:37:45 +03:00
|
|
|
ProgressTracker* aProgressTracker,
|
2022-02-24 12:40:34 +03:00
|
|
|
uint64_t aInnerWindowId) {
|
2015-03-24 05:37:45 +03:00
|
|
|
NewPartResult result(aExistingImage);
|
2001-03-14 03:39:48 +03:00
|
|
|
|
2017-01-09 07:04:19 +03:00
|
|
|
if (aInStr) {
|
|
|
|
mimetype_closure closure;
|
|
|
|
closure.newType = &result.mContentType;
|
2009-02-16 17:11:30 +03:00
|
|
|
|
2017-01-09 07:04:19 +03:00
|
|
|
// Look at the first few bytes and see if we can tell what the data is from
|
|
|
|
// that since servers tend to lie. :(
|
|
|
|
uint32_t out;
|
|
|
|
aInStr->ReadSegments(sniff_mimetype_callback, &closure, aCount, &out);
|
|
|
|
}
|
2010-05-15 00:47:59 +04:00
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
nsCOMPtr<nsIChannel> chan(do_QueryInterface(aRequest));
|
|
|
|
if (result.mContentType.IsEmpty()) {
|
2015-07-09 19:21:20 +03:00
|
|
|
nsresult rv =
|
|
|
|
chan ? chan->GetContentType(result.mContentType) : NS_ERROR_FAILURE;
|
2015-03-24 05:37:45 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(gImgLog, LogLevel::Error,
|
2015-07-09 19:21:17 +03:00
|
|
|
("imgRequest::PrepareForNewPart -- "
|
|
|
|
"Content type unavailable from the channel\n"));
|
2017-01-06 07:46:11 +03:00
|
|
|
if (!aIsMultipart) {
|
|
|
|
return result;
|
|
|
|
}
|
2015-03-24 05:37:45 +03:00
|
|
|
}
|
|
|
|
}
|
2012-09-07 02:05:23 +04:00
|
|
|
|
2015-07-09 19:21:20 +03:00
|
|
|
if (chan) {
|
|
|
|
chan->GetContentDispositionHeader(result.mContentDisposition);
|
|
|
|
}
|
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
MOZ_LOG(gImgLog, LogLevel::Debug,
|
2015-03-24 05:37:45 +03:00
|
|
|
("imgRequest::PrepareForNewPart -- Got content type %s\n",
|
|
|
|
result.mContentType.get()));
|
|
|
|
|
|
|
|
// XXX If server lied about mimetype and it's SVG, we may need to copy
|
|
|
|
// the data and dispatch back to the main thread, AND tell the channel to
|
|
|
|
// dispatch there in the future.
|
|
|
|
|
|
|
|
// Create the new image and give it ownership of our ProgressTracker.
|
|
|
|
if (aIsMultipart) {
|
|
|
|
// Create the ProgressTracker and image for this part.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> progressTracker = new ProgressTracker();
|
2017-03-21 21:09:14 +03:00
|
|
|
RefPtr<image::Image> partImage = image::ImageFactory::CreateImage(
|
|
|
|
aRequest, progressTracker, result.mContentType, aURI,
|
|
|
|
/* aIsMultipart = */ true, aInnerWindowId);
|
2015-03-24 05:37:45 +03:00
|
|
|
|
|
|
|
if (result.mIsFirstPart) {
|
|
|
|
// First part for a multipart channel. Create the MultipartImage wrapper.
|
|
|
|
MOZ_ASSERT(aProgressTracker, "Shouldn't have given away tracker yet");
|
2016-12-04 01:07:10 +03:00
|
|
|
aProgressTracker->SetIsMultipart();
|
2017-03-21 21:09:14 +03:00
|
|
|
result.mImage = image::ImageFactory::CreateMultipartImage(
|
|
|
|
partImage, aProgressTracker);
|
2015-03-24 05:37:45 +03:00
|
|
|
} else {
|
|
|
|
// Transition to the new part.
|
|
|
|
auto multipartImage = static_cast<MultipartImage*>(aExistingImage);
|
|
|
|
multipartImage->BeginTransitionToPart(partImage);
|
2015-07-07 03:11:14 +03:00
|
|
|
|
|
|
|
// Reset our cache entry size so it doesn't keep growing without bound.
|
|
|
|
result.mShouldResetCacheEntry = true;
|
2015-03-24 05:37:45 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(!aExistingImage, "New part for non-multipart channel?");
|
|
|
|
MOZ_ASSERT(aProgressTracker, "Shouldn't have given away tracker yet");
|
|
|
|
|
|
|
|
// Create an image using our progress tracker.
|
2017-03-21 21:09:14 +03:00
|
|
|
result.mImage = image::ImageFactory::CreateImage(
|
|
|
|
aRequest, aProgressTracker, result.mContentType, aURI,
|
|
|
|
/* aIsMultipart = */ false, aInnerWindowId);
|
2015-03-24 05:37:45 +03:00
|
|
|
}
|
2001-03-14 03:39:48 +03:00
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
MOZ_ASSERT(result.mImage);
|
|
|
|
if (!result.mImage->HasError() || aIsMultipart) {
|
|
|
|
// We allow multipart images to fail to initialize (which generally
|
|
|
|
// indicates a bad content type) without cancelling the load, because
|
|
|
|
// subsequent parts might be fine.
|
|
|
|
result.mSucceeded = true;
|
|
|
|
}
|
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-03-24 05:37:45 +03:00
|
|
|
return result;
|
|
|
|
}
|
2001-03-14 03:39:48 +03:00
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class FinishPreparingForNewPartRunnable final : public Runnable {
|
2015-03-24 05:37:45 +03:00
|
|
|
public:
|
|
|
|
FinishPreparingForNewPartRunnable(imgRequest* aImgRequest,
|
|
|
|
NewPartResult&& aResult)
|
2017-05-10 02:19:37 +03:00
|
|
|
: Runnable("FinishPreparingForNewPartRunnable"),
|
|
|
|
mImgRequest(aImgRequest),
|
2015-03-24 05:37:45 +03:00
|
|
|
mResult(aResult) {
|
|
|
|
MOZ_ASSERT(aImgRequest);
|
|
|
|
}
|
2001-03-14 03:39:48 +03:00
|
|
|
|
2015-03-24 06:00:42 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2015-03-24 05:37:45 +03:00
|
|
|
mImgRequest->FinishPreparingForNewPart(mResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-03-14 03:39:48 +03:00
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequest> mImgRequest;
|
2015-03-24 05:37:45 +03:00
|
|
|
NewPartResult mResult;
|
|
|
|
};
|
2001-03-14 03:39:48 +03:00
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
void imgRequest::FinishPreparingForNewPart(const NewPartResult& aResult) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mContentType = aResult.mContentType;
|
|
|
|
|
|
|
|
SetProperties(aResult.mContentType, aResult.mContentDisposition);
|
|
|
|
|
|
|
|
if (aResult.mIsFirstPart) {
|
|
|
|
// Notify listeners that we have an image.
|
2019-05-02 02:05:47 +03:00
|
|
|
mImageAvailable = true;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2015-03-24 05:37:45 +03:00
|
|
|
progressTracker->OnImageAvailable();
|
|
|
|
MOZ_ASSERT(progressTracker->HasImage());
|
|
|
|
}
|
|
|
|
|
2015-07-07 03:11:14 +03:00
|
|
|
if (aResult.mShouldResetCacheEntry) {
|
|
|
|
ResetCacheEntry();
|
|
|
|
}
|
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
if (IsDecodeRequested()) {
|
2016-11-26 10:56:26 +03:00
|
|
|
aResult.mImage->StartDecoding(imgIContainer::FLAG_NONE);
|
2015-03-24 05:37:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 02:05:47 +03:00
|
|
|
bool imgRequest::ImageAvailable() const { return mImageAvailable; }
|
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:42:27 +03:00
|
|
|
imgRequest::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* aInStr,
|
2015-03-24 05:37:45 +03:00
|
|
|
uint64_t aOffset, uint32_t aCount) {
|
2015-11-16 20:21:00 +03:00
|
|
|
LOG_SCOPE_WITH_PARAM(gImgLog, "imgRequest::OnDataAvailable", "count", aCount);
|
2015-03-24 05:37:45 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(aRequest, "imgRequest::OnDataAvailable -- no request!");
|
2001-03-14 03:39:48 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> image;
|
|
|
|
RefPtr<ProgressTracker> progressTracker;
|
2015-03-24 05:37:45 +03:00
|
|
|
bool isMultipart = false;
|
|
|
|
bool newPartPending = false;
|
2022-02-14 13:00:40 +03:00
|
|
|
uint64_t innerWindowId = 0;
|
2015-03-24 05:37:45 +03:00
|
|
|
|
|
|
|
// Retrieve and update our state.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
image = mImage;
|
|
|
|
progressTracker = mProgressTracker;
|
|
|
|
isMultipart = mIsMultiPartChannel;
|
|
|
|
newPartPending = mNewPartPending;
|
|
|
|
mNewPartPending = false;
|
2022-02-14 13:00:40 +03:00
|
|
|
innerWindowId = mInnerWindowId;
|
2015-03-24 05:37:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a new part, we need to sniff its content type and create an
|
|
|
|
// appropriate image.
|
|
|
|
if (newPartPending) {
|
|
|
|
NewPartResult result =
|
|
|
|
PrepareForNewPart(aRequest, aInStr, aCount, mURI, isMultipart, image,
|
2022-02-14 13:00:40 +03:00
|
|
|
progressTracker, innerWindowId);
|
2015-03-24 05:37:45 +03:00
|
|
|
bool succeeded = result.mSucceeded;
|
|
|
|
|
|
|
|
if (result.mImage) {
|
|
|
|
image = result.mImage;
|
2017-08-14 23:59:58 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> eventTarget;
|
2015-03-24 05:37:45 +03:00
|
|
|
|
|
|
|
// Update our state to reflect this new part.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mImage = image;
|
2017-08-14 23:59:58 +03:00
|
|
|
|
|
|
|
// We only get an event target if we are not on the main thread, because
|
|
|
|
// we have to dispatch in that case. If we are on the main thread, but
|
|
|
|
// on a different scheduler group than ProgressTracker would give us,
|
|
|
|
// that is okay because nothing in imagelib requires that, just our
|
|
|
|
// listeners (which have their own checks).
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
eventTarget = mProgressTracker->GetEventTarget();
|
|
|
|
MOZ_ASSERT(eventTarget);
|
|
|
|
}
|
|
|
|
|
2015-03-24 05:37:44 +03:00
|
|
|
mProgressTracker = nullptr;
|
2012-09-07 02:05:23 +04:00
|
|
|
}
|
2015-03-24 05:37:45 +03:00
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
// Some property objects are not threadsafe, and we need to send
|
|
|
|
// OnImageAvailable on the main thread, so finish on the main thread.
|
2017-08-14 23:59:58 +03:00
|
|
|
if (!eventTarget) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-03-24 05:37:45 +03:00
|
|
|
FinishPreparingForNewPart(result);
|
2015-03-24 05:37:45 +03:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2018-05-30 22:15:35 +03:00
|
|
|
new FinishPreparingForNewPartRunnable(this, std::move(result));
|
2021-10-08 22:29:37 +03:00
|
|
|
eventTarget->Dispatch(CreateRenderBlockingRunnable(runnable.forget()),
|
2019-05-18 02:09:38 +03:00
|
|
|
NS_DISPATCH_NORMAL);
|
2015-03-24 05:37:45 +03:00
|
|
|
}
|
2015-01-07 12:37:20 +03:00
|
|
|
}
|
2012-09-07 02:05:23 +04:00
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
if (!succeeded) {
|
|
|
|
// Something went wrong; probably a content type issue.
|
|
|
|
Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
2015-01-07 12:37:20 +03:00
|
|
|
return NS_BINDING_ABORTED;
|
|
|
|
}
|
2001-03-14 03:39:48 +03:00
|
|
|
}
|
|
|
|
|
2012-12-18 02:05:18 +04:00
|
|
|
// Notify the image that it has new data.
|
2017-01-09 07:04:19 +03:00
|
|
|
if (aInStr) {
|
2019-02-28 02:42:27 +03:00
|
|
|
nsresult rv =
|
2021-11-24 14:25:41 +03:00
|
|
|
image->OnImageDataAvailable(aRequest, aInStr, aOffset, aCount);
|
2012-12-18 02:05:18 +04:00
|
|
|
|
2017-01-09 07:04:19 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_LOG(gImgLog, LogLevel::Warning,
|
|
|
|
("[this=%p] imgRequest::OnDataAvailable -- "
|
|
|
|
"copy to RasterImage failed\n",
|
|
|
|
this));
|
|
|
|
Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
|
|
|
return NS_BINDING_ABORTED;
|
|
|
|
}
|
2001-04-16 13:17:59 +04:00
|
|
|
}
|
|
|
|
|
2001-03-06 07:46:20 +03:00
|
|
|
return NS_OK;
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
2001-03-14 03:39:48 +03:00
|
|
|
|
2015-03-24 05:37:45 +03:00
|
|
|
void imgRequest::SetProperties(const nsACString& aContentType,
|
|
|
|
const nsACString& aContentDisposition) {
|
2013-09-28 22:28:42 +04:00
|
|
|
/* set our mimetype as a property */
|
2015-03-24 05:37:45 +03:00
|
|
|
nsCOMPtr<nsISupportsCString> contentType =
|
|
|
|
do_CreateInstance("@mozilla.org/supports-cstring;1");
|
2013-09-28 22:28:42 +04:00
|
|
|
if (contentType) {
|
2015-03-24 05:37:45 +03:00
|
|
|
contentType->SetData(aContentType);
|
2013-09-28 22:28:42 +04:00
|
|
|
mProperties->Set("type", contentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set our content disposition as a property */
|
2015-03-24 05:37:45 +03:00
|
|
|
if (!aContentDisposition.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsISupportsCString> contentDisposition =
|
|
|
|
do_CreateInstance("@mozilla.org/supports-cstring;1");
|
2013-09-28 22:28:42 +04:00
|
|
|
if (contentDisposition) {
|
2015-03-24 05:37:45 +03:00
|
|
|
contentDisposition->SetData(aContentDisposition);
|
2013-09-28 22:28:42 +04:00
|
|
|
mProperties->Set("content-disposition", contentDisposition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
static nsresult sniff_mimetype_callback(nsIInputStream* in, void* data,
|
|
|
|
const char* fromRawSegment,
|
|
|
|
uint32_t toOffset, uint32_t count,
|
|
|
|
uint32_t* writeCount) {
|
2012-09-07 02:05:23 +04:00
|
|
|
mimetype_closure* closure = static_cast<mimetype_closure*>(data);
|
2001-03-14 03:39:48 +03:00
|
|
|
|
2012-09-07 02:05:23 +04:00
|
|
|
NS_ASSERTION(closure, "closure is null!");
|
2001-03-14 03:39:48 +03:00
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
if (count > 0) {
|
2012-11-21 04:08:06 +04:00
|
|
|
imgLoader::GetMimeTypeFromContent(fromRawSegment, count, *closure->newType);
|
2015-03-31 21:06:00 +03:00
|
|
|
}
|
2001-03-14 03:39:48 +03:00
|
|
|
|
|
|
|
*writeCount = 0;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2008-12-23 01:20:46 +03:00
|
|
|
/** nsIInterfaceRequestor methods **/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-03-31 21:06:00 +03:00
|
|
|
imgRequest::GetInterface(const nsIID& aIID, void** aResult) {
|
|
|
|
if (!mPrevChannelSink || aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
|
2008-12-23 01:20:46 +03:00
|
|
|
return QueryInterface(aIID, aResult);
|
2015-03-31 21:06:00 +03:00
|
|
|
}
|
2008-12-23 01:20:46 +03:00
|
|
|
|
2013-03-30 00:14:19 +04:00
|
|
|
NS_ASSERTION(
|
|
|
|
mPrevChannelSink != this,
|
2015-03-31 21:06:00 +03:00
|
|
|
"Infinite recursion - don't keep track of channel sinks that are us!");
|
2008-12-23 01:20:46 +03:00
|
|
|
return mPrevChannelSink->GetInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** nsIChannelEventSink methods **/
|
|
|
|
NS_IMETHODIMP
|
2015-03-31 21:06:00 +03:00
|
|
|
imgRequest::AsyncOnChannelRedirect(nsIChannel* oldChannel,
|
|
|
|
nsIChannel* newChannel, uint32_t flags,
|
|
|
|
nsIAsyncVerifyRedirectCallback* callback) {
|
|
|
|
NS_ASSERTION(mRequest && mChannel,
|
|
|
|
"Got a channel redirect after we nulled out mRequest!");
|
|
|
|
NS_ASSERTION(mChannel == oldChannel,
|
|
|
|
"Got a channel redirect for an unknown channel!");
|
2008-12-23 01:20:46 +03:00
|
|
|
NS_ASSERTION(newChannel, "Got a redirect to a NULL channel!");
|
|
|
|
|
2011-07-01 21:03:33 +04:00
|
|
|
SetCacheValidation(mCacheEntry, oldChannel);
|
|
|
|
|
2010-08-05 06:15:55 +04:00
|
|
|
// Prepare for callback
|
|
|
|
mRedirectCallback = callback;
|
|
|
|
mNewRedirectChannel = newChannel;
|
|
|
|
|
2008-12-23 01:20:46 +03:00
|
|
|
nsCOMPtr<nsIChannelEventSink> sink(do_GetInterface(mPrevChannelSink));
|
|
|
|
if (sink) {
|
2010-08-05 06:15:55 +04:00
|
|
|
nsresult rv =
|
|
|
|
sink->AsyncOnChannelRedirect(oldChannel, newChannel, flags, this);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mRedirectCallback = nullptr;
|
|
|
|
mNewRedirectChannel = nullptr;
|
2010-08-05 06:15:55 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2008-12-23 01:20:46 +03:00
|
|
|
}
|
2011-07-01 21:03:30 +04:00
|
|
|
|
2010-08-05 06:15:55 +04:00
|
|
|
(void)OnRedirectVerifyCallback(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-12-23 01:20:46 +03:00
|
|
|
|
2010-08-05 06:15:55 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgRequest::OnRedirectVerifyCallback(nsresult result) {
|
|
|
|
NS_ASSERTION(mRedirectCallback, "mRedirectCallback not set in callback");
|
|
|
|
NS_ASSERTION(mNewRedirectChannel, "mNewRedirectChannel not set in callback");
|
2011-07-01 21:03:30 +04:00
|
|
|
|
2010-08-05 06:15:55 +04:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
mRedirectCallback->OnRedirectVerifyCallback(result);
|
2012-07-30 18:20:58 +04:00
|
|
|
mRedirectCallback = nullptr;
|
|
|
|
mNewRedirectChannel = nullptr;
|
2010-08-05 06:15:55 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mChannel = mNewRedirectChannel;
|
2011-06-10 01:11:57 +04:00
|
|
|
mTimedChannel = do_QueryInterface(mChannel);
|
2012-07-30 18:20:58 +04:00
|
|
|
mNewRedirectChannel = nullptr;
|
2008-12-23 01:20:46 +03:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
if (LOG_TEST(LogLevel::Debug)) {
|
2016-08-26 09:02:31 +03:00
|
|
|
LOG_MSG_WITH_PARAM(gImgLog, "imgRequest::OnChannelRedirect", "old",
|
2017-11-13 11:31:24 +03:00
|
|
|
mFinalURI ? mFinalURI->GetSpecOrDefault().get() : "");
|
2014-11-11 01:06:24 +03:00
|
|
|
}
|
2009-03-18 00:07:16 +03:00
|
|
|
|
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.
|
|
|
|
bool schemeLocal = false;
|
2019-08-09 18:17:06 +03:00
|
|
|
if (NS_FAILED(NS_URIChainHasFlags(mFinalURI,
|
2015-03-31 21:06:00 +03:00
|
|
|
nsIProtocolHandler::URI_IS_LOCAL_RESOURCE,
|
|
|
|
&schemeLocal)) ||
|
2019-08-09 18:17:06 +03:00
|
|
|
(!mFinalURI->SchemeIs("https") && !mFinalURI->SchemeIs("chrome") &&
|
|
|
|
!schemeLocal)) {
|
2015-03-30 02:25:48 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2015-07-16 00:52:47 +03:00
|
|
|
|
|
|
|
// The csp directive upgrade-insecure-requests performs an internal redirect
|
|
|
|
// to upgrade all requests from http to https before any data is fetched
|
|
|
|
// from the network. Do not pollute mHadInsecureRedirect in case of such an
|
|
|
|
// internal redirect.
|
2019-02-20 15:27:25 +03:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = mChannel->LoadInfo();
|
2018-02-05 18:37:27 +03:00
|
|
|
bool upgradeInsecureRequests =
|
|
|
|
loadInfo ? loadInfo->GetUpgradeInsecureRequests() ||
|
|
|
|
loadInfo->GetBrowserUpgradeInsecureRequests()
|
2015-07-16 00:52:47 +03:00
|
|
|
: false;
|
|
|
|
if (!upgradeInsecureRequests) {
|
|
|
|
mHadInsecureRedirect = true;
|
|
|
|
}
|
2014-12-16 04:40:43 +03:00
|
|
|
}
|
|
|
|
|
2017-11-13 11:31:24 +03:00
|
|
|
// Update the final URI.
|
|
|
|
mChannel->GetURI(getter_AddRefs(mFinalURI));
|
2014-11-11 01:06:24 +03:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
if (LOG_TEST(LogLevel::Debug)) {
|
2016-08-26 09:02:31 +03:00
|
|
|
LOG_MSG_WITH_PARAM(gImgLog, "imgRequest::OnChannelRedirect", "new",
|
2017-11-13 11:31:24 +03:00
|
|
|
mFinalURI ? mFinalURI->GetSpecOrDefault().get() : "");
|
2014-11-11 01:06:24 +03:00
|
|
|
}
|
|
|
|
|
2014-12-16 04:40:43 +03:00
|
|
|
// Make sure we have a protocol that returns data rather than opens an
|
|
|
|
// external application, e.g. 'mailto:'.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool doesNotReturnData = false;
|
2017-11-13 11:31:24 +03:00
|
|
|
nsresult rv = NS_URIChainHasFlags(
|
2015-03-31 21:06:00 +03:00
|
|
|
mFinalURI, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA,
|
2010-08-05 06:15:55 +04:00
|
|
|
&doesNotReturnData);
|
|
|
|
|
2015-03-31 21:06:00 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && doesNotReturnData) {
|
2010-08-05 06:15:55 +04:00
|
|
|
rv = NS_ERROR_ABORT;
|
2015-03-31 21:06:00 +03:00
|
|
|
}
|
2010-08-05 06:15:55 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mRedirectCallback->OnRedirectVerifyCallback(rv);
|
2012-07-30 18:20:58 +04:00
|
|
|
mRedirectCallback = nullptr;
|
2010-08-05 06:15:55 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-02-12 00:59:00 +03:00
|
|
|
|
2010-08-05 06:15:55 +04:00
|
|
|
mRedirectCallback->OnRedirectVerifyCallback(NS_OK);
|
2012-07-30 18:20:58 +04:00
|
|
|
mRedirectCallback = nullptr;
|
2010-08-05 06:15:55 +04:00
|
|
|
return NS_OK;
|
2008-12-23 01:20:46 +03:00
|
|
|
}
|