2001-01-22 22:01:03 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 11:12:37 +00: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-22 22:01:03 +00:00
|
|
|
|
2001-02-20 23:27:48 +00:00
|
|
|
#include "imgRequest.h"
|
2011-07-08 17:55:03 +00:00
|
|
|
#include "ImageLogging.h"
|
2001-01-22 22:01:03 +00:00
|
|
|
|
2002-05-24 20:54:44 +00: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 02:42:56 +00:00
|
|
|
#include "imgRequestProxy.h"
|
2015-01-17 21:18:26 +00:00
|
|
|
#include "DecodePool.h"
|
2014-11-15 04:10:47 +00:00
|
|
|
#include "ProgressTracker.h"
|
2012-12-17 22:05:18 +00:00
|
|
|
#include "ImageFactory.h"
|
2013-02-13 21:53:42 +00:00
|
|
|
#include "Image.h"
|
2015-01-07 09:37:20 +00:00
|
|
|
#include "MultipartImage.h"
|
2013-09-28 18:28:42 +00: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 02:42:56 +00:00
|
|
|
|
2001-01-22 22:01:03 +00:00
|
|
|
#include "nsIChannel.h"
|
2015-11-12 20:09:52 +00:00
|
|
|
#include "nsICacheInfoChannel.h"
|
2015-03-24 02:37:45 +00:00
|
|
|
#include "nsIDocument.h"
|
2013-09-28 18:28:42 +00:00
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
2001-01-22 22:01:03 +00:00
|
|
|
#include "nsIInputStream.h"
|
2002-03-10 22:29:14 +00:00
|
|
|
#include "nsIMultiPartChannel.h"
|
2002-09-18 22:08:12 +00:00
|
|
|
#include "nsIHttpChannel.h"
|
2012-11-08 18:54:00 +00:00
|
|
|
#include "nsIApplicationCache.h"
|
|
|
|
#include "nsIApplicationCacheChannel.h"
|
2013-01-08 21:40:47 +00:00
|
|
|
#include "nsMimeTypes.h"
|
2001-03-25 11:57:25 +00:00
|
|
|
|
2008-12-22 22:20:46 +00:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2005-03-23 21:09:02 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2007-11-09 02:55:41 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2014-02-14 22:56:01 +00:00
|
|
|
#include "nsContentUtils.h"
|
2001-01-22 22:01:03 +00:00
|
|
|
|
2002-09-18 22:08:12 +00:00
|
|
|
#include "plstr.h" // PL_strcasestr(...)
|
2010-02-11 21:59:00 +00:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIProtocolHandler.h"
|
2013-09-07 13:01:08 +00:00
|
|
|
#include "imgIRequest.h"
|
2010-07-12 01:51:52 +00:00
|
|
|
|
2011-06-12 02:30:15 +00:00
|
|
|
using namespace mozilla;
|
2012-01-06 16:02:27 +00:00
|
|
|
using namespace mozilla::image;
|
2010-08-14 04:09:48 +00:00
|
|
|
|
2015-11-16 17:21:00 +00:00
|
|
|
#define LOG_TEST(level) (MOZ_LOG_TEST(gImgLog, (level)))
|
2001-01-22 22:01:03 +00:00
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(imgRequest,
|
|
|
|
nsIStreamListener, nsIRequestObserver,
|
|
|
|
nsIThreadRetargetableStreamListener,
|
|
|
|
nsIChannelEventSink,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsIAsyncVerifyRedirectCallback)
|
2001-01-22 22:01:03 +00:00
|
|
|
|
2015-05-20 17:21:13 +00:00
|
|
|
imgRequest::imgRequest(imgLoader* aLoader, const ImageCacheKey& aCacheKey)
|
2012-09-06 22:05:23 +00:00
|
|
|
: mLoader(aLoader)
|
2015-05-20 17:21:13 +00:00
|
|
|
, mCacheKey(aCacheKey)
|
|
|
|
, mLoadId(nullptr)
|
2015-08-25 02:49:33 +00:00
|
|
|
, mFirstProxy(nullptr)
|
2012-09-06 22:05:23 +00:00
|
|
|
, mValidator(nullptr)
|
|
|
|
, mInnerWindowId(0)
|
|
|
|
, mCORSMode(imgIRequest::CORS_NONE)
|
2014-11-18 13:46:53 +00:00
|
|
|
, mReferrerPolicy(mozilla::net::RP_Default)
|
2014-07-19 01:47:11 +00:00
|
|
|
, mImageErrorCode(NS_OK)
|
2015-03-24 02:37:45 +00:00
|
|
|
, mMutex("imgRequest")
|
|
|
|
, mProgressTracker(new ProgressTracker())
|
2012-09-06 22:05:23 +00:00
|
|
|
, mIsMultiPartChannel(false)
|
|
|
|
, mGotData(false)
|
|
|
|
, mIsInCache(false)
|
2015-03-24 02:37:45 +00:00
|
|
|
, mDecodeRequested(false)
|
2015-01-07 09:37:20 +00:00
|
|
|
, mNewPartPending(false)
|
2014-12-16 01:40:43 +00:00
|
|
|
, mHadInsecureRedirect(false)
|
2012-12-17 22:05:18 +00:00
|
|
|
{ }
|
2001-01-22 22:01:03 +00:00
|
|
|
|
2001-02-20 23:27:48 +00:00
|
|
|
imgRequest::~imgRequest()
|
2001-01-22 22:01:03 +00:00
|
|
|
{
|
2014-09-05 21:36:11 +00:00
|
|
|
if (mLoader) {
|
|
|
|
mLoader->RemoveFromUncachedImages(this);
|
|
|
|
}
|
2011-07-01 17:03:40 +00:00
|
|
|
if (mURI) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString spec;
|
2011-07-01 17:03:40 +00:00
|
|
|
mURI->GetSpec(spec);
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog, "imgRequest::~imgRequest()",
|
2015-03-31 18:06:00 +00:00
|
|
|
"keyuri", spec.get());
|
2009-01-31 02:17:47 +00:00
|
|
|
} else
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_FUNC(gImgLog, "imgRequest::~imgRequest()");
|
2001-01-22 22:01:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
nsresult
|
|
|
|
imgRequest::Init(nsIURI *aURI,
|
|
|
|
nsIURI *aCurrentURI,
|
|
|
|
bool aHadInsecureRedirect,
|
|
|
|
nsIRequest *aRequest,
|
|
|
|
nsIChannel *aChannel,
|
|
|
|
imgCacheEntry *aCacheEntry,
|
|
|
|
nsISupports* aCX,
|
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
|
|
|
int32_t aCORSMode,
|
|
|
|
ReferrerPolicy aReferrerPolicy)
|
2001-01-22 22:01:03 +00:00
|
|
|
{
|
2013-09-28 18:28:42 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Cannot use nsIURI off main thread!");
|
|
|
|
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_FUNC(gImgLog, "imgRequest::Init");
|
2001-02-22 02:12:21 +00:00
|
|
|
|
2015-02-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(!mImage, "Multiple calls to init");
|
|
|
|
MOZ_ASSERT(aURI, "No uri");
|
|
|
|
MOZ_ASSERT(aCurrentURI, "No current uri");
|
|
|
|
MOZ_ASSERT(aRequest, "No request");
|
|
|
|
MOZ_ASSERT(aChannel, "No channel");
|
2001-01-22 22:01:03 +00:00
|
|
|
|
2005-03-23 21:09:02 +00:00
|
|
|
mProperties = do_CreateInstance("@mozilla.org/properties;1");
|
2010-08-14 04:09:49 +00:00
|
|
|
|
2013-09-28 18:28:42 +00:00
|
|
|
// Use ImageURL to ensure access to URI data off main thread.
|
|
|
|
mURI = new ImageURL(aURI);
|
2011-07-01 17:03:40 +00:00
|
|
|
mCurrentURI = aCurrentURI;
|
2006-03-11 20:32:47 +00:00
|
|
|
mRequest = aRequest;
|
2008-12-22 22:20:46 +00:00
|
|
|
mChannel = aChannel;
|
2011-06-09 21:11:57 +00:00
|
|
|
mTimedChannel = do_QueryInterface(mChannel);
|
|
|
|
|
2011-07-14 18:47:32 +00:00
|
|
|
mLoadingPrincipal = aLoadingPrincipal;
|
2011-07-14 18:47:34 +00:00
|
|
|
mCORSMode = aCORSMode;
|
2014-11-18 13:46:53 +00:00
|
|
|
mReferrerPolicy = aReferrerPolicy;
|
2011-07-14 18:47:32 +00:00
|
|
|
|
2014-12-16 01:40:43 +00:00
|
|
|
// If the original URI and the current URI are different, check whether the
|
|
|
|
// original URI is secure. We deliberately don't take the current URI into
|
|
|
|
// account, as it needs to be handled using more complicated rules than
|
|
|
|
// earlier elements of the redirect chain.
|
|
|
|
if (aURI != aCurrentURI) {
|
|
|
|
bool isHttps = false;
|
|
|
|
bool isChrome = false;
|
|
|
|
bool schemeLocal = false;
|
|
|
|
if (NS_FAILED(aURI->SchemeIs("https", &isHttps)) ||
|
|
|
|
NS_FAILED(aURI->SchemeIs("chrome", &isChrome)) ||
|
2015-03-31 18:06:00 +00:00
|
|
|
NS_FAILED(NS_URIChainHasFlags(
|
|
|
|
aURI,
|
|
|
|
nsIProtocolHandler::URI_IS_LOCAL_RESOURCE , &schemeLocal)) ||
|
2014-12-16 01:40:43 +00:00
|
|
|
(!isHttps && !isChrome && !schemeLocal)) {
|
|
|
|
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-22 22:20:46 +00: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-22 22:01:03 +00:00
|
|
|
|
2001-03-16 02:40:04 +00:00
|
|
|
mCacheEntry = aCacheEntry;
|
2015-10-20 05:15:25 +00:00
|
|
|
mCacheEntry->UpdateLoadTime();
|
2001-03-16 02:40:04 +00:00
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
SetLoadId(aCX);
|
|
|
|
|
|
|
|
// Grab the inner window ID of the loading document, if possible.
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aCX);
|
|
|
|
if (doc) {
|
|
|
|
mInnerWindowId = doc->InnerWindowID();
|
|
|
|
}
|
2002-01-15 05:23:33 +00:00
|
|
|
|
2001-01-23 02:02:29 +00:00
|
|
|
return NS_OK;
|
2001-01-22 22:01:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
void
|
|
|
|
imgRequest::ClearLoader() {
|
2014-09-05 21:36:11 +00:00
|
|
|
mLoader = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-11-15 04:10:47 +00:00
|
|
|
already_AddRefed<ProgressTracker>
|
2015-03-24 02:37:45 +00:00
|
|
|
imgRequest::GetProgressTracker() const
|
2010-08-23 22:44:07 +00:00
|
|
|
{
|
2015-03-24 02:37:45 +00:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2015-01-07 09:37:20 +00:00
|
|
|
if (mImage) {
|
2015-02-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(!mProgressTracker,
|
|
|
|
"Should have given mProgressTracker to mImage");
|
2014-11-15 04:10:47 +00:00
|
|
|
return mImage->GetProgressTracker();
|
2010-08-23 22:44:07 +00:00
|
|
|
} else {
|
2015-02-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(mProgressTracker,
|
|
|
|
"Should have mProgressTracker until we create mImage");
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ProgressTracker> progressTracker = mProgressTracker;
|
2014-11-15 04:10:47 +00:00
|
|
|
MOZ_ASSERT(progressTracker);
|
|
|
|
return progressTracker.forget();
|
2010-08-23 22:44:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
void imgRequest::SetCacheEntry(imgCacheEntry* entry)
|
2009-01-31 02:17:47 +00:00
|
|
|
{
|
|
|
|
mCacheEntry = entry;
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
bool
|
|
|
|
imgRequest::HasCacheEntry() const
|
2009-01-31 02:17:47 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
return mCacheEntry != nullptr;
|
2009-01-31 02:17:47 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
void
|
|
|
|
imgRequest::ResetCacheEntry()
|
2012-10-12 16:11:21 +00:00
|
|
|
{
|
|
|
|
if (HasCacheEntry()) {
|
|
|
|
mCacheEntry->SetDataSize(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
void
|
|
|
|
imgRequest::AddProxy(imgRequestProxy* proxy)
|
2001-01-22 22:01:03 +00:00
|
|
|
{
|
2007-09-22 19:40:57 +00:00
|
|
|
NS_PRECONDITION(proxy, "null imgRequestProxy passed in");
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_SCOPE_WITH_PARAM(gImgLog, "imgRequest::AddProxy", "proxy", proxy);
|
2001-02-22 02:06:24 +00:00
|
|
|
|
2015-08-25 02:49:33 +00: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 02:17:47 +00:00
|
|
|
// If we're empty before adding, we have to tell the loader we now have
|
|
|
|
// proxies.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2015-01-07 09:35:20 +00:00
|
|
|
if (progressTracker->ObserverCount() == 0) {
|
2015-02-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(mURI, "Trying to SetHasProxies without key uri.");
|
2014-09-05 21:36:11 +00:00
|
|
|
if (mLoader) {
|
2014-09-11 02:47:00 +00:00
|
|
|
mLoader->SetHasProxies(this);
|
2014-09-05 21:36:11 +00:00
|
|
|
}
|
2009-01-31 02:17:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 09:35:20 +00:00
|
|
|
progressTracker->AddObserver(proxy);
|
2002-01-15 05:23:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
nsresult
|
|
|
|
imgRequest::RemoveProxy(imgRequestProxy* proxy, nsresult aStatus)
|
2002-01-15 05:23:33 +00:00
|
|
|
{
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_SCOPE_WITH_PARAM(gImgLog, "imgRequest::RemoveProxy", "proxy", proxy);
|
2002-01-15 05:23:33 +00:00
|
|
|
|
2010-09-08 00:33:02 +00: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-14 20:47:59 +00: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 05:24:48 +00:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2015-03-31 18:06:00 +00:00
|
|
|
if (!progressTracker->RemoveObserver(proxy)) {
|
2012-10-12 16:11:20 +00:00
|
|
|
return NS_OK;
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2002-01-15 05:23:33 +00:00
|
|
|
|
2015-01-07 09:35:20 +00:00
|
|
|
if (progressTracker->ObserverCount() == 0) {
|
2009-01-31 02:17:47 +00: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-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(mURI, "Removing last observer without key uri.");
|
2009-01-31 02:17:47 +00:00
|
|
|
|
2014-09-05 21:36:11 +00:00
|
|
|
if (mLoader) {
|
2014-09-11 02:47:00 +00:00
|
|
|
mLoader->SetHasNoProxies(this, mCacheEntry);
|
2014-09-05 21:36:11 +00:00
|
|
|
}
|
2015-11-16 17:21:00 +00:00
|
|
|
} else if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString spec;
|
2011-07-01 17:03:40 +00:00
|
|
|
mURI->GetSpec(spec);
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_MSG_WITH_PARAM(gImgLog,
|
2015-03-31 18:06:00 +00:00
|
|
|
"imgRequest::RemoveProxy no cache entry",
|
|
|
|
"uri", spec.get());
|
2009-01-31 02:17:47 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00: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 05:23:33 +00:00
|
|
|
*/
|
2015-01-25 07:16:46 +00:00
|
|
|
if (!(progressTracker->GetProgress() & FLAG_LAST_PART_COMPLETE) &&
|
|
|
|
NS_FAILED(aStatus)) {
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_MSG(gImgLog, "imgRequest::RemoveProxy",
|
2015-03-31 18:06:00 +00:00
|
|
|
"load in progress. canceling");
|
2002-01-15 05:23:33 +00:00
|
|
|
|
|
|
|
this->Cancel(NS_BINDING_ABORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* break the cycle from the cache entry. */
|
2012-07-30 14:20:58 +00:00
|
|
|
mCacheEntry = nullptr;
|
2002-01-15 05:23:33 +00:00
|
|
|
}
|
|
|
|
|
2003-05-14 05:42:06 +00:00
|
|
|
// If a proxy is removed for a reason other than its owner being
|
|
|
|
// changed, remove the proxy from the loadgroup.
|
2015-03-31 18:06:00 +00:00
|
|
|
if (aStatus != NS_IMAGELIB_CHANGING_OWNER) {
|
2011-10-17 14:59:28 +00:00
|
|
|
proxy->RemoveFromLoadGroup(true);
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2003-05-14 05:42:06 +00:00
|
|
|
|
2002-01-15 05:23:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
void
|
|
|
|
imgRequest::CancelAndAbort(nsresult aStatus)
|
2008-12-22 22:20:46 +00:00
|
|
|
{
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_SCOPE(gImgLog, "imgRequest::CancelAndAbort");
|
2009-01-31 02:17:47 +00:00
|
|
|
|
2008-12-22 22:20:46 +00: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 14:20:58 +00:00
|
|
|
mPrevChannelSink = nullptr;
|
2008-12-22 22:20:46 +00:00
|
|
|
}
|
2001-03-06 04:46:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-26 00:23:21 +00:00
|
|
|
class imgRequestMainThreadCancel : public Runnable
|
2013-10-15 23:33:11 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-03-31 18:06:00 +00:00
|
|
|
imgRequestMainThreadCancel(imgRequest* aImgRequest, nsresult aStatus)
|
2013-10-15 23:33:11 +00:00
|
|
|
: mImgRequest(aImgRequest)
|
|
|
|
, mStatus(aStatus)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread(), "Create me off main thread only!");
|
|
|
|
MOZ_ASSERT(aImgRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "I should be running on the main thread!");
|
|
|
|
mImgRequest->ContinueCancel(mStatus);
|
|
|
|
return NS_OK;
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2013-10-15 23:33:11 +00:00
|
|
|
private:
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<imgRequest> mImgRequest;
|
2013-10-15 23:33:11 +00:00
|
|
|
nsresult mStatus;
|
|
|
|
};
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
void
|
|
|
|
imgRequest::Cancel(nsresult aStatus)
|
2010-05-14 20:47:59 +00:00
|
|
|
{
|
|
|
|
/* The Cancel() method here should only be called by this class. */
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_SCOPE(gImgLog, "imgRequest::Cancel");
|
2010-05-14 20:47:59 +00:00
|
|
|
|
2013-09-28 18:28:42 +00:00
|
|
|
if (NS_IsMainThread()) {
|
2013-10-15 23:33:11 +00:00
|
|
|
ContinueCancel(aStatus);
|
2013-09-28 18:28:42 +00:00
|
|
|
} else {
|
2013-10-15 23:33:11 +00:00
|
|
|
NS_DispatchToMainThread(new imgRequestMainThreadCancel(this, aStatus));
|
2013-09-28 18:28:42 +00:00
|
|
|
}
|
2013-10-15 23:33:11 +00:00
|
|
|
}
|
2010-05-14 20:47:59 +00:00
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
void
|
|
|
|
imgRequest::ContinueCancel(nsresult aStatus)
|
2013-10-15 23:33:11 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2014-11-15 04:10:47 +00:00
|
|
|
progressTracker->SyncNotifyProgress(FLAG_HAS_ERROR | FLAG_ONLOAD_UNBLOCKED);
|
2014-11-15 04:10:47 +00:00
|
|
|
|
2013-10-15 23:33:11 +00:00
|
|
|
RemoveFromCache();
|
|
|
|
|
2015-01-25 07:16:46 +00:00
|
|
|
if (mRequest && !(progressTracker->GetProgress() & FLAG_LAST_PART_COMPLETE)) {
|
2013-10-15 23:33:11 +00:00
|
|
|
mRequest->Cancel(aStatus);
|
|
|
|
}
|
2010-05-14 20:47:59 +00:00
|
|
|
}
|
|
|
|
|
2016-04-26 00:23:21 +00:00
|
|
|
class imgRequestMainThreadEvict : public Runnable
|
2014-06-08 22:15:00 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-03-31 18:06:00 +00:00
|
|
|
explicit imgRequestMainThreadEvict(imgRequest* aImgRequest)
|
2014-06-08 22:15:00 +00:00
|
|
|
: mImgRequest(aImgRequest)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread(), "Create me off main thread only!");
|
|
|
|
MOZ_ASSERT(aImgRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "I should be running on the main thread!");
|
|
|
|
mImgRequest->ContinueEvict();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<imgRequest> mImgRequest;
|
2014-06-08 22:15:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// EvictFromCache() is written to allowed to get called from any thread
|
2015-03-31 18:06:00 +00:00
|
|
|
void
|
|
|
|
imgRequest::EvictFromCache()
|
2014-06-08 22:15:00 +00:00
|
|
|
{
|
|
|
|
/* The EvictFromCache() method here should only be called by this class. */
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_SCOPE(gImgLog, "imgRequest::EvictFromCache");
|
2014-06-08 22:15:00 +00:00
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
ContinueEvict();
|
|
|
|
} else {
|
|
|
|
NS_DispatchToMainThread(new imgRequestMainThreadEvict(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper-method used by EvictFromCache()
|
2015-03-31 18:06:00 +00:00
|
|
|
void
|
|
|
|
imgRequest::ContinueEvict()
|
2014-06-08 22:15:00 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
RemoveFromCache();
|
|
|
|
}
|
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
void
|
2015-10-29 23:37:42 +00:00
|
|
|
imgRequest::StartDecoding()
|
2015-03-24 02:37:45 +00:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mDecodeRequested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
imgRequest::IsDecodeRequested() const
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mDecodeRequested;
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
nsresult imgRequest::GetURI(ImageURL** aURI)
|
2001-03-06 04:46:20 +00:00
|
|
|
{
|
2013-09-28 18:28:42 +00:00
|
|
|
MOZ_ASSERT(aURI);
|
|
|
|
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_FUNC(gImgLog, "imgRequest::GetURI");
|
2001-03-06 04:46:20 +00:00
|
|
|
|
2001-10-12 06:43:52 +00:00
|
|
|
if (mURI) {
|
|
|
|
*aURI = mURI;
|
|
|
|
NS_ADDREF(*aURI);
|
2001-04-14 02:02:19 +00:00
|
|
|
return NS_OK;
|
2001-10-12 06:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2001-04-10 06:01:08 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
nsresult
|
|
|
|
imgRequest::GetCurrentURI(nsIURI** aURI)
|
2014-11-10 22:06:24 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aURI);
|
|
|
|
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_FUNC(gImgLog, "imgRequest::GetCurrentURI");
|
2014-11-10 22:06:24 +00:00
|
|
|
|
|
|
|
if (mCurrentURI) {
|
|
|
|
*aURI = mCurrentURI;
|
|
|
|
NS_ADDREF(*aURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:45:45 +00:00
|
|
|
bool
|
|
|
|
imgRequest::IsChrome() const
|
|
|
|
{
|
|
|
|
bool isChrome = false;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(mURI->SchemeIs("chrome", &isChrome)))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return isChrome;
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
nsresult
|
|
|
|
imgRequest::GetImageErrorCode()
|
2014-07-19 01:47:11 +00:00
|
|
|
{
|
|
|
|
return mImageErrorCode;
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
nsresult
|
|
|
|
imgRequest::GetSecurityInfo(nsISupports** aSecurityInfo)
|
2008-09-01 20:53:59 +00:00
|
|
|
{
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_FUNC(gImgLog, "imgRequest::GetSecurityInfo");
|
2008-09-01 20:53:59 +00:00
|
|
|
|
|
|
|
// Missing security info means this is not a security load
|
|
|
|
// i.e. it is not an error when security info is missing
|
|
|
|
NS_IF_ADDREF(*aSecurityInfo = mSecurityInfo);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
void
|
|
|
|
imgRequest::RemoveFromCache()
|
2001-01-22 22:01:03 +00:00
|
|
|
{
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_SCOPE(gImgLog, "imgRequest::RemoveFromCache");
|
2001-02-22 02:06:24 +00:00
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
bool isInCache = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
isInCache = mIsInCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isInCache && mLoader) {
|
2009-03-17 21:07:16 +00:00
|
|
|
// mCacheEntry is nulled out when we have no more observers.
|
2014-09-05 21:36:11 +00:00
|
|
|
if (mCacheEntry) {
|
2012-06-26 04:20:12 +00:00
|
|
|
mLoader->RemoveFromCache(mCacheEntry);
|
2014-09-05 21:36:11 +00:00
|
|
|
} else {
|
2015-10-27 18:12:46 +00:00
|
|
|
mLoader->RemoveFromCache(mCacheKey);
|
2014-09-05 21:36:11 +00:00
|
|
|
}
|
2009-03-05 03:56:14 +00:00
|
|
|
}
|
2009-03-17 21:07:16 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mCacheEntry = nullptr;
|
2001-01-22 22:01:03 +00:00
|
|
|
}
|
fix for at least bugs 6074,72087,74506,75190,75180,74165,69857,75576,75326,75417,75474 r=waterson, sr=brendan
2001-04-13 02:42:56 +00:00
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
bool
|
|
|
|
imgRequest::HasConsumers() const
|
2014-09-05 21:36:11 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2015-01-07 09:35:20 +00:00
|
|
|
return progressTracker && progressTracker->ObserverCount() > 0;
|
2014-09-05 21:36:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
already_AddRefed<Image>
|
|
|
|
imgRequest::GetImage() const
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Image> image = mImage;
|
2015-03-24 02:37:45 +00:00
|
|
|
return image.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t imgRequest::Priority() const
|
2005-02-21 20:58:01 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t priority = nsISupportsPriority::PRIORITY_NORMAL;
|
2006-03-11 20:32:47 +00:00
|
|
|
nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mRequest);
|
2015-03-31 18:06:00 +00:00
|
|
|
if (p) {
|
2005-02-21 20:58:01 +00:00
|
|
|
p->GetPriority(&priority);
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2005-02-21 20:58:01 +00:00
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
void
|
|
|
|
imgRequest::AdjustPriority(imgRequestProxy* proxy, int32_t delta)
|
2005-02-21 20:58:01 +00: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 02:49:33 +00:00
|
|
|
if (!mFirstProxy || proxy != mFirstProxy) {
|
2005-02-21 20:58:01 +00:00
|
|
|
return;
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2005-02-21 20:58:01 +00:00
|
|
|
|
2012-10-13 16:47:36 +00:00
|
|
|
nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mChannel);
|
2015-03-31 18:06:00 +00:00
|
|
|
if (p) {
|
2005-02-21 22:21:21 +00:00
|
|
|
p->AdjustPriority(delta);
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2005-02-21 20:58:01 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
bool
|
|
|
|
imgRequest::HasTransferredData() const
|
2009-03-05 03:56:14 +00:00
|
|
|
{
|
2015-03-24 02:37:45 +00:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mGotData;
|
2009-03-05 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
void
|
|
|
|
imgRequest::SetIsInCache(bool aInCache)
|
2001-04-10 01:51:36 +00:00
|
|
|
{
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog,
|
2015-03-24 02:37:45 +00:00
|
|
|
"imgRequest::SetIsCacheable", "aInCache", aInCache);
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mIsInCache = aInCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
imgRequest::UpdateCacheEntrySize()
|
|
|
|
{
|
|
|
|
if (!mCacheEntry) {
|
|
|
|
return;
|
2014-11-27 02:00:15 +00:00
|
|
|
}
|
2015-03-24 02:37:45 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Image> image = GetImage();
|
2015-03-24 02:37:45 +00:00
|
|
|
size_t size = image->SizeOfSourceWithComputedFallback(moz_malloc_size_of);
|
|
|
|
mCacheEntry->SetDataSize(size);
|
2001-01-22 22:01:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
void
|
|
|
|
imgRequest::SetCacheValidation(imgCacheEntry* aCacheEntry, nsIRequest* aRequest)
|
2011-07-01 17:03:33 +00:00
|
|
|
{
|
|
|
|
/* get the expires info */
|
|
|
|
if (aCacheEntry) {
|
2015-11-12 20:09:52 +00:00
|
|
|
nsCOMPtr<nsICacheInfoChannel> cacheChannel(do_QueryInterface(aRequest));
|
2011-07-01 17:03:33 +00:00
|
|
|
if (cacheChannel) {
|
2015-11-12 20:09:52 +00:00
|
|
|
uint32_t expiration = 0;
|
|
|
|
/* get the expiration time from the caching channel's token */
|
|
|
|
if (NS_SUCCEEDED(cacheChannel->GetCacheTokenExpirationTime(&expiration))) {
|
|
|
|
// Expiration time defaults to 0. We set the expiration time on our
|
|
|
|
// entry if it hasn't been set yet.
|
|
|
|
if (aCacheEntry->GetExpiryTime() == 0) {
|
|
|
|
aCacheEntry->SetExpiryTime(expiration);
|
2011-07-01 17:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
// Determine whether the cache entry must be revalidated when we try to use
|
|
|
|
// it. Currently, only HTTP specifies this information...
|
2011-07-01 17:03:33 +00:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(aRequest));
|
|
|
|
if (httpChannel) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool bMustRevalidate = false;
|
2011-07-01 17:03:33 +00:00
|
|
|
|
|
|
|
httpChannel->IsNoStoreResponse(&bMustRevalidate);
|
|
|
|
|
|
|
|
if (!bMustRevalidate) {
|
|
|
|
httpChannel->IsNoCacheResponse(&bMustRevalidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bMustRevalidate) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString cacheHeader;
|
2011-07-01 17:03:33 +00:00
|
|
|
|
|
|
|
httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Cache-Control"),
|
|
|
|
cacheHeader);
|
|
|
|
if (PL_strcasestr(cacheHeader.get(), "must-revalidate")) {
|
2011-10-17 14:59:28 +00:00
|
|
|
bMustRevalidate = true;
|
2011-07-01 17:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-03-31 18:06:00 +00:00
|
|
|
if (bMustRevalidate) {
|
2011-07-01 17:03:35 +00:00
|
|
|
aCacheEntry->SetMustValidate(bMustRevalidate);
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2011-07-01 17:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
namespace {
|
2012-11-08 18:54:00 +00:00
|
|
|
|
|
|
|
already_AddRefed<nsIApplicationCache>
|
|
|
|
GetApplicationCache(nsIRequest* aRequest)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
nsCOMPtr<nsIApplicationCacheChannel> appCacheChan =
|
|
|
|
do_QueryInterface(aRequest);
|
2012-11-08 18:54:00 +00:00
|
|
|
if (!appCacheChan) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fromAppCache;
|
|
|
|
rv = appCacheChan->GetLoadedFromApplicationCache(&fromAppCache);
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
if (!fromAppCache) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIApplicationCache> appCache;
|
|
|
|
rv = appCacheChan->GetApplicationCache(getter_AddRefs(appCache));
|
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
|
|
|
|
return appCache.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2012-11-08 18:54:00 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
imgRequest::CacheChanged(nsIRequest* aNewRequest)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIApplicationCache> newAppCache = GetApplicationCache(aNewRequest);
|
|
|
|
|
|
|
|
// Application cache not involved at all or the same app cache involved
|
|
|
|
// in both of the loads (original and new).
|
2015-03-31 18:06:00 +00:00
|
|
|
if (newAppCache == mApplicationCache) {
|
2012-11-08 18:54:00 +00:00
|
|
|
return false;
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2012-11-08 18:54:00 +00:00
|
|
|
|
|
|
|
// In a rare case it may happen that two objects still refer
|
|
|
|
// the same application cache version.
|
|
|
|
if (newAppCache && mApplicationCache) {
|
2012-11-14 12:56:56 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2012-11-08 18:54:00 +00:00
|
|
|
nsAutoCString oldAppCacheClientId, newAppCacheClientId;
|
|
|
|
rv = mApplicationCache->GetClientID(oldAppCacheClientId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
rv = newAppCache->GetClientID(newAppCacheClientId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
if (oldAppCacheClientId == newAppCacheClientId) {
|
2012-11-08 18:54:00 +00:00
|
|
|
return false;
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2012-11-08 18:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When we get here, app caches differ or app cache is involved
|
|
|
|
// just in one of the loads what we also consider as a change
|
|
|
|
// in a loading cache.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
bool
|
|
|
|
imgRequest::GetMultipart() const
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mIsMultiPartChannel;
|
|
|
|
}
|
|
|
|
|
2015-03-29 23:25:48 +00:00
|
|
|
bool
|
|
|
|
imgRequest::HadInsecureRedirect() const
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mHadInsecureRedirect;
|
|
|
|
}
|
|
|
|
|
2001-04-10 06:01:08 +00:00
|
|
|
/** nsIRequestObserver methods **/
|
2001-01-22 22:01:03 +00:00
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgRequest::OnStartRequest(nsIRequest* aRequest, nsISupports* ctxt)
|
2001-01-22 22:01:03 +00:00
|
|
|
{
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_SCOPE(gImgLog, "imgRequest::OnStartRequest");
|
2001-02-22 02:06:24 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Image> image;
|
2015-01-07 09:37:20 +00:00
|
|
|
|
|
|
|
// Figure out if we're multipart.
|
2015-03-24 02:37:45 +00:00
|
|
|
nsCOMPtr<nsIMultiPartChannel> multiPartChannel = do_QueryInterface(aRequest);
|
|
|
|
MOZ_ASSERT(multiPartChannel || !mIsMultiPartChannel,
|
2015-03-31 18:06:00 +00:00
|
|
|
"Stopped being multipart?"); {
|
2015-03-24 02:37:45 +00:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mNewPartPending = true;
|
|
|
|
image = mImage;
|
|
|
|
mIsMultiPartChannel = bool(multiPartChannel);
|
2012-12-19 21:28:54 +00:00
|
|
|
}
|
2002-02-27 00:32:19 +00:00
|
|
|
|
2015-03-05 06:18:14 +00:00
|
|
|
// If we're not multipart, we shouldn't have an image yet.
|
2015-03-24 02:37:45 +00:00
|
|
|
if (image && !multiPartChannel) {
|
2015-03-05 06:18:14 +00:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Already have an image for a non-multipart request");
|
|
|
|
Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-09-12 22:44:18 +00:00
|
|
|
|
2008-09-29 20:46:53 +00: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 02:37:45 +00:00
|
|
|
MOZ_ASSERT(multiPartChannel, "Should have mRequest unless we're multipart");
|
|
|
|
nsCOMPtr<nsIChannel> baseChannel;
|
|
|
|
multiPartChannel->GetBaseChannel(getter_AddRefs(baseChannel));
|
|
|
|
mRequest = baseChannel;
|
2008-09-29 20:46:53 +00:00
|
|
|
}
|
|
|
|
|
2008-09-01 20:53:59 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
|
2015-03-24 02:37:45 +00:00
|
|
|
if (channel) {
|
2008-09-01 20:53:59 +00:00
|
|
|
channel->GetSecurityInfo(getter_AddRefs(mSecurityInfo));
|
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
/* Get our principal */
|
2015-03-31 18:06:00 +00:00
|
|
|
nsCOMPtr<nsIScriptSecurityManager>
|
|
|
|
secMan = nsContentUtils::GetSecurityManager();
|
2007-11-09 02:55:41 +00:00
|
|
|
if (secMan) {
|
2015-03-31 18:06:00 +00:00
|
|
|
nsresult rv =
|
|
|
|
secMan->GetChannelResultPrincipal(channel, getter_AddRefs(mPrincipal));
|
2007-11-09 02:55:41 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-02-22 08:41:20 +00:00
|
|
|
|
2011-07-01 17:03:33 +00:00
|
|
|
SetCacheValidation(mCacheEntry, aRequest);
|
2002-03-10 22:29:14 +00:00
|
|
|
|
2012-11-08 18:54:00 +00:00
|
|
|
mApplicationCache = GetApplicationCache(aRequest);
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
// Shouldn't we be dead already if this gets hit?
|
|
|
|
// Probably multipart/x-mixed-replace...
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2015-01-07 09:35:20 +00:00
|
|
|
if (progressTracker->ObserverCount() == 0) {
|
2002-03-10 22:29:14 +00:00
|
|
|
this->Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
2013-09-28 18:28:42 +00:00
|
|
|
// Try to retarget OnDataAvailable to a decode thread.
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aRequest);
|
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> retargetable =
|
|
|
|
do_QueryInterface(aRequest);
|
2015-01-17 21:18:26 +00:00
|
|
|
if (httpChannel && retargetable) {
|
2013-09-28 18:28:42 +00:00
|
|
|
nsAutoCString mimeType;
|
|
|
|
nsresult rv = httpChannel->GetContentType(mimeType);
|
|
|
|
if (NS_SUCCEEDED(rv) && !mimeType.EqualsLiteral(IMAGE_SVG_XML)) {
|
2015-01-18 09:27:16 +00:00
|
|
|
// Retarget OnDataAvailable to the DecodePool's IO thread.
|
2015-01-17 21:18:26 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> target =
|
2015-01-18 09:27:16 +00:00
|
|
|
DecodePool::Singleton()->GetIOEventTarget();
|
2013-09-28 18:28:42 +00:00
|
|
|
rv = retargetable->RetargetDeliveryTo(target);
|
|
|
|
}
|
2015-11-16 17:21:00 +00:00
|
|
|
MOZ_LOG(gImgLog, LogLevel::Warning,
|
2013-09-28 18:28:42 +00:00
|
|
|
("[this=%p] imgRequest::OnStartRequest -- "
|
|
|
|
"RetargetDeliveryTo rv %d=%s\n",
|
2013-12-12 21:17:35 +00:00
|
|
|
this, rv, NS_SUCCEEDED(rv) ? "succeeded" : "failed"));
|
2013-09-28 18:28:42 +00:00
|
|
|
}
|
|
|
|
|
2001-01-22 22:01:03 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
imgRequest::OnStopRequest(nsIRequest* aRequest,
|
|
|
|
nsISupports* ctxt, nsresult status)
|
2001-01-22 22:01:03 +00:00
|
|
|
{
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_FUNC(gImgLog, "imgRequest::OnStopRequest");
|
2014-11-15 04:06:19 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Can't send notifications off-main-thread");
|
2001-02-20 22:43:56 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Image> image = GetImage();
|
2015-03-24 02:37:45 +00:00
|
|
|
|
2006-06-01 18:35:21 +00:00
|
|
|
// XXXldb What if this is a non-last part of a multipart request?
|
2008-12-22 22:20:46 +00:00
|
|
|
// xxx before we release our reference to mRequest, lets
|
2006-11-09 22:47:43 +00:00
|
|
|
// save the last status that we saw so that the
|
|
|
|
// imgRequestProxy will have access to it.
|
2008-12-22 22:20:46 +00:00
|
|
|
if (mRequest) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mRequest = nullptr; // we no longer need the request
|
2006-12-11 04:15:45 +00:00
|
|
|
}
|
2001-02-26 04:22:47 +00:00
|
|
|
|
2008-12-22 22:20:46 +00:00
|
|
|
// stop holding a ref to the channel, since we don't need it anymore
|
|
|
|
if (mChannel) {
|
|
|
|
mChannel->SetNotificationCallbacks(mPrevChannelSink);
|
2012-07-30 14:20:58 +00:00
|
|
|
mPrevChannelSink = nullptr;
|
|
|
|
mChannel = nullptr;
|
2008-12-22 22:20:46 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 01:06:34 +00:00
|
|
|
bool lastPart = true;
|
|
|
|
nsCOMPtr<nsIMultiPartChannel> mpchan(do_QueryInterface(aRequest));
|
2015-03-31 18:06:00 +00:00
|
|
|
if (mpchan) {
|
2013-02-02 01:06:34 +00:00
|
|
|
mpchan->GetIsLastPart(&lastPart);
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2013-02-02 01:06:34 +00:00
|
|
|
|
2014-06-08 22:15:00 +00:00
|
|
|
bool isPartial = false;
|
2015-03-24 02:37:45 +00:00
|
|
|
if (image && (status == NS_ERROR_NET_PARTIAL_TRANSFER)) {
|
2014-06-08 22:15:00 +00:00
|
|
|
isPartial = true;
|
|
|
|
status = NS_OK; // fake happy face
|
|
|
|
}
|
|
|
|
|
2009-09-12 22:44:18 +00: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 02:37:45 +00:00
|
|
|
if (image) {
|
|
|
|
nsresult rv = image->OnImageDataComplete(aRequest, ctxt, status, lastPart);
|
2009-09-12 22:44:18 +00:00
|
|
|
|
2012-12-17 22:05:18 +00: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 18:06:00 +00:00
|
|
|
if (NS_FAILED(rv) && NS_SUCCEEDED(status)) {
|
2009-09-12 22:44:18 +00:00
|
|
|
status = rv;
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2001-02-25 08:28:36 +00:00
|
|
|
}
|
2002-03-23 13:21:33 +00:00
|
|
|
|
2010-05-14 20:47:59 +00:00
|
|
|
// If the request went through, update the cache entry size. Otherwise,
|
|
|
|
// cancel the request, which removes us from the cache.
|
2015-03-24 02:37:45 +00:00
|
|
|
if (image && NS_SUCCEEDED(status) && !isPartial) {
|
2009-09-12 22:44:18 +00: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 18:06:00 +00:00
|
|
|
|
|
|
|
} else if (isPartial) {
|
2014-06-08 22:15:00 +00:00
|
|
|
// Remove the partial image from the cache.
|
|
|
|
this->EvictFromCache();
|
2015-03-31 18:06:00 +00:00
|
|
|
|
|
|
|
} else {
|
2014-07-19 01:47:11 +00:00
|
|
|
mImageErrorCode = status;
|
|
|
|
|
2014-06-08 22:15:00 +00:00
|
|
|
// if the error isn't "just" a partial transfer
|
2010-05-14 20:47:59 +00:00
|
|
|
// stops animations, removes from cache
|
|
|
|
this->Cancel(status);
|
2009-09-12 22:44:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
if (!image) {
|
2014-11-15 04:10:47 +00:00
|
|
|
// We have to fire the OnStopRequest notifications ourselves because there's
|
2013-02-02 01:06:34 +00:00
|
|
|
// no image capable of doing so.
|
2014-11-15 04:10:47 +00:00
|
|
|
Progress progress =
|
2014-11-17 22:29:56 +00:00
|
|
|
LoadCompleteProgress(lastPart, /* aError = */ false, status);
|
2014-11-15 04:06:19 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2014-11-15 04:10:47 +00:00
|
|
|
progressTracker->SyncNotifyProgress(progress);
|
2013-02-02 01:06:34 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mTimedChannel = nullptr;
|
2001-02-20 22:43:56 +00:00
|
|
|
return NS_OK;
|
2001-01-22 22:01:03 +00:00
|
|
|
}
|
|
|
|
|
2012-09-06 22:05:23 +00:00
|
|
|
struct mimetype_closure
|
|
|
|
{
|
|
|
|
nsACString* newType;
|
|
|
|
};
|
|
|
|
|
2009-09-12 22:44:18 +00:00
|
|
|
/* prototype for these defined below */
|
2015-03-31 18:06:00 +00:00
|
|
|
static NS_METHOD
|
|
|
|
sniff_mimetype_callback(nsIInputStream* in, void* closure,
|
|
|
|
const char* fromRawSegment, uint32_t toOffset,
|
|
|
|
uint32_t count, uint32_t* writeCount);
|
2001-03-14 00:39:48 +00:00
|
|
|
|
2013-09-28 18:28:42 +00: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-22 22:01:03 +00:00
|
|
|
/** nsIStreamListener methods **/
|
|
|
|
|
2015-03-24 03:00:42 +00:00
|
|
|
struct NewPartResult final
|
2001-01-22 22:01:03 +00:00
|
|
|
{
|
2015-03-24 02:37:45 +00:00
|
|
|
explicit NewPartResult(Image* aExistingImage)
|
|
|
|
: mImage(aExistingImage)
|
|
|
|
, mIsFirstPart(!aExistingImage)
|
|
|
|
, mSucceeded(false)
|
2015-07-07 00:11:14 +00:00
|
|
|
, mShouldResetCacheEntry(false)
|
2015-03-24 02:37:45 +00:00
|
|
|
{ }
|
|
|
|
|
|
|
|
nsAutoCString mContentType;
|
|
|
|
nsAutoCString mContentDisposition;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Image> mImage;
|
2015-03-24 02:37:45 +00:00
|
|
|
const bool mIsFirstPart;
|
|
|
|
bool mSucceeded;
|
2015-07-07 00:11:14 +00:00
|
|
|
bool mShouldResetCacheEntry;
|
2015-03-24 02:37:45 +00:00
|
|
|
};
|
2001-02-25 08:28:36 +00:00
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
static NewPartResult
|
|
|
|
PrepareForNewPart(nsIRequest* aRequest, nsIInputStream* aInStr, uint32_t aCount,
|
|
|
|
ImageURL* aURI, bool aIsMultipart, Image* aExistingImage,
|
|
|
|
ProgressTracker* aProgressTracker, uint32_t aInnerWindowId)
|
|
|
|
{
|
|
|
|
NewPartResult result(aExistingImage);
|
2001-03-14 00:39:48 +00:00
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
mimetype_closure closure;
|
|
|
|
closure.newType = &result.mContentType;
|
2009-02-16 14:11:30 +00:00
|
|
|
|
2015-03-24 02:37:45 +00: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-14 20:47:59 +00:00
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
nsCOMPtr<nsIChannel> chan(do_QueryInterface(aRequest));
|
|
|
|
if (result.mContentType.IsEmpty()) {
|
2015-07-09 16:21:20 +00:00
|
|
|
nsresult rv = chan ? chan->GetContentType(result.mContentType)
|
|
|
|
: NS_ERROR_FAILURE;
|
2015-03-24 02:37:45 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-11-16 17:21:00 +00:00
|
|
|
MOZ_LOG(gImgLog,
|
2015-07-09 16:21:17 +00:00
|
|
|
LogLevel::Error, ("imgRequest::PrepareForNewPart -- "
|
|
|
|
"Content type unavailable from the channel\n"));
|
2015-03-24 02:37:45 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2012-09-06 22:05:23 +00:00
|
|
|
|
2015-07-09 16:21:20 +00:00
|
|
|
if (chan) {
|
|
|
|
chan->GetContentDispositionHeader(result.mContentDisposition);
|
|
|
|
}
|
|
|
|
|
2015-11-16 17:21:00 +00:00
|
|
|
MOZ_LOG(gImgLog, LogLevel::Debug,
|
2015-03-24 02:37:45 +00: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 05:24:48 +00:00
|
|
|
RefPtr<ProgressTracker> progressTracker = new ProgressTracker();
|
|
|
|
RefPtr<Image> partImage =
|
2015-03-24 02:37:45 +00:00
|
|
|
ImageFactory::CreateImage(aRequest, progressTracker, result.mContentType,
|
|
|
|
aURI, /* aIsMultipart = */ true,
|
|
|
|
aInnerWindowId);
|
|
|
|
|
|
|
|
if (result.mIsFirstPart) {
|
|
|
|
// First part for a multipart channel. Create the MultipartImage wrapper.
|
|
|
|
MOZ_ASSERT(aProgressTracker, "Shouldn't have given away tracker yet");
|
2015-04-28 23:32:02 +00:00
|
|
|
result.mImage =
|
|
|
|
ImageFactory::CreateMultipartImage(partImage, aProgressTracker);
|
2015-03-24 02:37:45 +00:00
|
|
|
} else {
|
|
|
|
// Transition to the new part.
|
|
|
|
auto multipartImage = static_cast<MultipartImage*>(aExistingImage);
|
|
|
|
multipartImage->BeginTransitionToPart(partImage);
|
2015-07-07 00:11:14 +00:00
|
|
|
|
|
|
|
// Reset our cache entry size so it doesn't keep growing without bound.
|
|
|
|
result.mShouldResetCacheEntry = true;
|
2015-03-24 02:37:45 +00: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.
|
|
|
|
result.mImage =
|
|
|
|
ImageFactory::CreateImage(aRequest, aProgressTracker, result.mContentType,
|
|
|
|
aURI, /* aIsMultipart = */ false,
|
|
|
|
aInnerWindowId);
|
|
|
|
}
|
2001-03-14 00:39:48 +00:00
|
|
|
|
2015-03-24 02:37:45 +00: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 02:42:56 +00:00
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
return result;
|
|
|
|
}
|
2001-03-14 00:39:48 +00:00
|
|
|
|
2016-04-26 00:23:21 +00:00
|
|
|
class FinishPreparingForNewPartRunnable final : public Runnable
|
2015-03-24 02:37:45 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
FinishPreparingForNewPartRunnable(imgRequest* aImgRequest,
|
|
|
|
NewPartResult&& aResult)
|
|
|
|
: mImgRequest(aImgRequest)
|
|
|
|
, mResult(aResult)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImgRequest);
|
|
|
|
}
|
2001-03-14 00:39:48 +00:00
|
|
|
|
2015-03-24 03:00:42 +00:00
|
|
|
NS_IMETHOD Run() override
|
2015-03-24 02:37:45 +00:00
|
|
|
{
|
|
|
|
mImgRequest->FinishPreparingForNewPart(mResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-03-14 00:39:48 +00:00
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
private:
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<imgRequest> mImgRequest;
|
2015-03-24 02:37:45 +00:00
|
|
|
NewPartResult mResult;
|
|
|
|
};
|
2001-03-14 00:39:48 +00:00
|
|
|
|
2015-03-24 02:37:45 +00: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.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
2015-03-24 02:37:45 +00:00
|
|
|
progressTracker->OnImageAvailable();
|
|
|
|
MOZ_ASSERT(progressTracker->HasImage());
|
|
|
|
}
|
|
|
|
|
2015-07-07 00:11:14 +00:00
|
|
|
if (aResult.mShouldResetCacheEntry) {
|
|
|
|
ResetCacheEntry();
|
|
|
|
}
|
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
if (IsDecodeRequested()) {
|
2015-10-29 23:37:42 +00:00
|
|
|
aResult.mImage->StartDecoding();
|
2015-03-24 02:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
imgRequest::OnDataAvailable(nsIRequest* aRequest, nsISupports* aContext,
|
|
|
|
nsIInputStream* aInStr, uint64_t aOffset,
|
|
|
|
uint32_t aCount)
|
|
|
|
{
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_SCOPE_WITH_PARAM(gImgLog, "imgRequest::OnDataAvailable",
|
2015-03-31 18:06:00 +00:00
|
|
|
"count", aCount);
|
2015-03-24 02:37:45 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(aRequest, "imgRequest::OnDataAvailable -- no request!");
|
2001-03-14 00:39:48 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Image> image;
|
|
|
|
RefPtr<ProgressTracker> progressTracker;
|
2015-03-24 02:37:45 +00:00
|
|
|
bool isMultipart = false;
|
|
|
|
bool newPartPending = false;
|
|
|
|
|
|
|
|
// Retrieve and update our state.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mGotData = true;
|
|
|
|
image = mImage;
|
|
|
|
progressTracker = mProgressTracker;
|
|
|
|
isMultipart = mIsMultiPartChannel;
|
|
|
|
newPartPending = mNewPartPending;
|
|
|
|
mNewPartPending = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
progressTracker, mInnerWindowId);
|
|
|
|
bool succeeded = result.mSucceeded;
|
|
|
|
|
|
|
|
if (result.mImage) {
|
|
|
|
image = result.mImage;
|
|
|
|
|
|
|
|
// Update our state to reflect this new part.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mImage = image;
|
2015-03-24 02:37:44 +00:00
|
|
|
mProgressTracker = nullptr;
|
2012-09-06 22:05:23 +00:00
|
|
|
}
|
2015-03-24 02:37:45 +00:00
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
// Some property objects are not threadsafe, and we need to send
|
|
|
|
// OnImageAvailable on the main thread, so finish on the main thread.
|
2015-03-24 02:37:45 +00:00
|
|
|
if (NS_IsMainThread()) {
|
2015-03-24 02:37:45 +00:00
|
|
|
FinishPreparingForNewPart(result);
|
2015-03-24 02:37:45 +00:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2015-03-24 02:37:45 +00:00
|
|
|
new FinishPreparingForNewPartRunnable(this, Move(result));
|
2015-03-24 02:37:45 +00:00
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
2015-01-07 09:37:20 +00:00
|
|
|
}
|
2012-09-06 22:05:23 +00:00
|
|
|
|
2015-03-24 02:37:45 +00:00
|
|
|
if (!succeeded) {
|
|
|
|
// Something went wrong; probably a content type issue.
|
|
|
|
Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
2015-01-07 09:37:20 +00:00
|
|
|
return NS_BINDING_ABORTED;
|
|
|
|
}
|
2001-03-14 00:39:48 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 22:05:18 +00:00
|
|
|
// Notify the image that it has new data.
|
2015-03-24 02:37:45 +00:00
|
|
|
nsresult rv =
|
|
|
|
image->OnImageDataAvailable(aRequest, aContext, aInStr, aOffset, aCount);
|
2012-12-17 22:05:18 +00:00
|
|
|
|
2001-04-16 09:17:59 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-11-16 17:21:00 +00:00
|
|
|
MOZ_LOG(gImgLog, LogLevel::Warning,
|
2009-09-12 22:44:18 +00:00
|
|
|
("[this=%p] imgRequest::OnDataAvailable -- "
|
2010-08-14 04:09:51 +00:00
|
|
|
"copy to RasterImage failed\n", this));
|
2015-03-24 02:37:45 +00:00
|
|
|
Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
2001-04-16 09:17:59 +00:00
|
|
|
return NS_BINDING_ABORTED;
|
|
|
|
}
|
|
|
|
|
2001-03-06 04:46:20 +00:00
|
|
|
return NS_OK;
|
2001-01-22 22:01:03 +00:00
|
|
|
}
|
2001-03-14 00:39:48 +00:00
|
|
|
|
2013-09-28 18:28:42 +00:00
|
|
|
void
|
2015-03-24 02:37:45 +00:00
|
|
|
imgRequest::SetProperties(const nsACString& aContentType,
|
|
|
|
const nsACString& aContentDisposition)
|
2013-09-28 18:28:42 +00:00
|
|
|
{
|
|
|
|
/* set our mimetype as a property */
|
2015-03-24 02:37:45 +00:00
|
|
|
nsCOMPtr<nsISupportsCString> contentType =
|
|
|
|
do_CreateInstance("@mozilla.org/supports-cstring;1");
|
2013-09-28 18:28:42 +00:00
|
|
|
if (contentType) {
|
2015-03-24 02:37:45 +00:00
|
|
|
contentType->SetData(aContentType);
|
2013-09-28 18:28:42 +00:00
|
|
|
mProperties->Set("type", contentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set our content disposition as a property */
|
2015-03-24 02:37:45 +00:00
|
|
|
if (!aContentDisposition.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsISupportsCString> contentDisposition =
|
|
|
|
do_CreateInstance("@mozilla.org/supports-cstring;1");
|
2013-09-28 18:28:42 +00:00
|
|
|
if (contentDisposition) {
|
2015-03-24 02:37:45 +00:00
|
|
|
contentDisposition->SetData(aContentDisposition);
|
2013-09-28 18:28:42 +00:00
|
|
|
mProperties->Set("content-disposition", contentDisposition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
static NS_METHOD
|
|
|
|
sniff_mimetype_callback(nsIInputStream* in,
|
|
|
|
void* data,
|
|
|
|
const char* fromRawSegment,
|
|
|
|
uint32_t toOffset,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t* writeCount)
|
2001-03-14 00:39:48 +00:00
|
|
|
{
|
2012-09-06 22:05:23 +00:00
|
|
|
mimetype_closure* closure = static_cast<mimetype_closure*>(data);
|
2001-03-14 00:39:48 +00:00
|
|
|
|
2012-09-06 22:05:23 +00:00
|
|
|
NS_ASSERTION(closure, "closure is null!");
|
2001-03-14 00:39:48 +00:00
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
if (count > 0) {
|
2012-11-21 00:08:06 +00:00
|
|
|
imgLoader::GetMimeTypeFromContent(fromRawSegment, count, *closure->newType);
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2001-03-14 00:39:48 +00:00
|
|
|
|
|
|
|
*writeCount = 0;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2009-09-12 22:44:18 +00:00
|
|
|
|
2008-12-22 22:20:46 +00:00
|
|
|
/** nsIInterfaceRequestor methods **/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-03-31 18:06:00 +00:00
|
|
|
imgRequest::GetInterface(const nsIID & aIID, void** aResult)
|
2008-12-22 22:20:46 +00:00
|
|
|
{
|
2015-03-31 18:06:00 +00:00
|
|
|
if (!mPrevChannelSink || aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
|
2008-12-22 22:20:46 +00:00
|
|
|
return QueryInterface(aIID, aResult);
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2008-12-22 22:20:46 +00:00
|
|
|
|
2013-03-29 20:14:19 +00:00
|
|
|
NS_ASSERTION(mPrevChannelSink != this,
|
2015-03-31 18:06:00 +00:00
|
|
|
"Infinite recursion - don't keep track of channel sinks that are us!");
|
2008-12-22 22:20:46 +00:00
|
|
|
return mPrevChannelSink->GetInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** nsIChannelEventSink methods **/
|
|
|
|
NS_IMETHODIMP
|
2015-03-31 18:06:00 +00:00
|
|
|
imgRequest::AsyncOnChannelRedirect(nsIChannel* oldChannel,
|
|
|
|
nsIChannel* newChannel, uint32_t flags,
|
|
|
|
nsIAsyncVerifyRedirectCallback* callback)
|
2008-12-22 22:20:46 +00:00
|
|
|
{
|
2015-03-31 18:06:00 +00:00
|
|
|
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-22 22:20:46 +00:00
|
|
|
NS_ASSERTION(newChannel, "Got a redirect to a NULL channel!");
|
|
|
|
|
2011-07-01 17:03:33 +00:00
|
|
|
SetCacheValidation(mCacheEntry, oldChannel);
|
|
|
|
|
2010-08-05 02:15:55 +00:00
|
|
|
// Prepare for callback
|
|
|
|
mRedirectCallback = callback;
|
|
|
|
mNewRedirectChannel = newChannel;
|
|
|
|
|
2008-12-22 22:20:46 +00:00
|
|
|
nsCOMPtr<nsIChannelEventSink> sink(do_GetInterface(mPrevChannelSink));
|
|
|
|
if (sink) {
|
2010-08-05 02:15:55 +00:00
|
|
|
nsresult rv = sink->AsyncOnChannelRedirect(oldChannel, newChannel, flags,
|
|
|
|
this);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mRedirectCallback = nullptr;
|
|
|
|
mNewRedirectChannel = nullptr;
|
2010-08-05 02:15:55 +00:00
|
|
|
}
|
|
|
|
return rv;
|
2008-12-22 22:20:46 +00:00
|
|
|
}
|
2011-07-01 17:03:30 +00:00
|
|
|
|
2010-08-05 02:15:55 +00:00
|
|
|
(void) OnRedirectVerifyCallback(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-12-22 22:20:46 +00:00
|
|
|
|
2010-08-05 02:15:55 +00: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 17:03:30 +00:00
|
|
|
|
2010-08-05 02:15:55 +00:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
mRedirectCallback->OnRedirectVerifyCallback(result);
|
2012-07-30 14:20:58 +00:00
|
|
|
mRedirectCallback = nullptr;
|
|
|
|
mNewRedirectChannel = nullptr;
|
2010-08-05 02:15:55 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mChannel = mNewRedirectChannel;
|
2011-06-09 21:11:57 +00:00
|
|
|
mTimedChannel = do_QueryInterface(mChannel);
|
2012-07-30 14:20:58 +00:00
|
|
|
mNewRedirectChannel = nullptr;
|
2008-12-22 22:20:46 +00:00
|
|
|
|
2015-06-03 22:25:57 +00:00
|
|
|
if (LOG_TEST(LogLevel::Debug)) {
|
2014-11-10 22:06:24 +00:00
|
|
|
nsAutoCString spec;
|
2015-03-31 18:06:00 +00:00
|
|
|
if (mCurrentURI) {
|
2014-11-10 22:06:24 +00:00
|
|
|
mCurrentURI->GetSpec(spec);
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_MSG_WITH_PARAM(gImgLog,
|
2015-03-31 18:06:00 +00:00
|
|
|
"imgRequest::OnChannelRedirect", "old", spec.get());
|
2014-11-10 22:06:24 +00:00
|
|
|
}
|
2009-03-17 21:07:16 +00:00
|
|
|
|
2014-12-16 01:40:43 +00: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 isHttps = false;
|
|
|
|
bool isChrome = false;
|
|
|
|
bool schemeLocal = false;
|
|
|
|
if (NS_FAILED(mCurrentURI->SchemeIs("https", &isHttps)) ||
|
|
|
|
NS_FAILED(mCurrentURI->SchemeIs("chrome", &isChrome)) ||
|
2015-03-31 18:06:00 +00:00
|
|
|
NS_FAILED(NS_URIChainHasFlags(mCurrentURI,
|
|
|
|
nsIProtocolHandler::URI_IS_LOCAL_RESOURCE,
|
|
|
|
&schemeLocal)) ||
|
2014-12-16 01:40:43 +00:00
|
|
|
(!isHttps && !isChrome && !schemeLocal)) {
|
2015-03-29 23:25:48 +00:00
|
|
|
MutexAutoLock lock(mMutex);
|
2015-07-15 21:52:47 +00: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.
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = mChannel->GetLoadInfo();
|
|
|
|
bool upgradeInsecureRequests = loadInfo ? loadInfo->GetUpgradeInsecureRequests()
|
|
|
|
: false;
|
|
|
|
if (!upgradeInsecureRequests) {
|
|
|
|
mHadInsecureRedirect = true;
|
|
|
|
}
|
2014-12-16 01:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the current URI.
|
2011-07-01 17:03:40 +00:00
|
|
|
mChannel->GetURI(getter_AddRefs(mCurrentURI));
|
2014-11-10 22:06:24 +00:00
|
|
|
|
2015-06-03 22:25:57 +00:00
|
|
|
if (LOG_TEST(LogLevel::Debug)) {
|
2014-11-10 22:06:24 +00:00
|
|
|
nsAutoCString spec;
|
2015-03-31 18:06:00 +00:00
|
|
|
if (mCurrentURI) {
|
2014-11-10 22:06:24 +00:00
|
|
|
mCurrentURI->GetSpec(spec);
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_MSG_WITH_PARAM(gImgLog, "imgRequest::OnChannelRedirect",
|
2015-03-31 18:06:00 +00:00
|
|
|
"new", spec.get());
|
2014-11-10 22:06:24 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 01:40:43 +00:00
|
|
|
// Make sure we have a protocol that returns data rather than opens an
|
|
|
|
// external application, e.g. 'mailto:'.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool doesNotReturnData = false;
|
2010-08-05 02:15:55 +00:00
|
|
|
nsresult rv =
|
2015-03-31 18:06:00 +00:00
|
|
|
NS_URIChainHasFlags(mCurrentURI,
|
|
|
|
nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA,
|
2010-08-05 02:15:55 +00:00
|
|
|
&doesNotReturnData);
|
|
|
|
|
2015-03-31 18:06:00 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && doesNotReturnData) {
|
2010-08-05 02:15:55 +00:00
|
|
|
rv = NS_ERROR_ABORT;
|
2015-03-31 18:06:00 +00:00
|
|
|
}
|
2010-08-05 02:15:55 +00:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mRedirectCallback->OnRedirectVerifyCallback(rv);
|
2012-07-30 14:20:58 +00:00
|
|
|
mRedirectCallback = nullptr;
|
2010-08-05 02:15:55 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-02-11 21:59:00 +00:00
|
|
|
|
2010-08-05 02:15:55 +00:00
|
|
|
mRedirectCallback->OnRedirectVerifyCallback(NS_OK);
|
2012-07-30 14:20:58 +00:00
|
|
|
mRedirectCallback = nullptr;
|
2010-08-05 02:15:55 +00:00
|
|
|
return NS_OK;
|
2008-12-22 22:20:46 +00:00
|
|
|
}
|