mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:30:27 +00:00
f9e56e1ed1
- Bug 1140767 - Build more files in security/manager in unified mode; r=dkeeler (11ab39c46) - Bug 1141864. Replace a bunch nsAutoPtr.h includes with nsRefPtr.h (Adapted) (ce31bfbcc) - Bug 1141689 - use services::GetObserverService more; r=ehsan (22e6fcf7e) - Bug 1140162 - IPC Proxy for TextAttributes and DefaultTextAttributes, r=tbsaunde (31bb06b0d) - Bug 1140499 - IPC Proxy for text/char bounds, r=tbsaunde (5441444db) - Bug 1140534 - IPC Proxy for offsetAtPoint, r=tbsaunde (24ca5c668) - Bug 1140895 - IPC Proxy for get/set/add/remove Selection, r=tbsaunde (35d3364b8) - Bug 1140900 - IPC Proxy for ScrollSubstringTo*, r=tbsaunde (1f7de020f) - Bug 1140917 - IPC Proxy for replace/insert/copy/cut/delete/paste, r=tbsaunde (b6fe2db79) - Bug 1140917 followup: add missing MOZ_OVERRIDE annotations on new DocAccessibleChild method-decls. r=ehsan (47cce9086) - Pointer style (b63b44d0a) - Bug 1140636 - Test CPOW function identity. r=billm (6ada9597c) - Bug 1134006 - Avoid IPC for domElement.QueryInterface(nsISupports) and nsIClassInfo. r=billm (753758b63) - Bug 1096488 - Detect and handle switching from remote to non-remote pages and back in marionette.;r=automatedtester (048279bd5) - Bug 1096488 - Test that switching browser remoteness leaves marionette in a usable state.;r=automatedtester (207aabadb) - Bug 1138650 - Update remaining callsites to use newChannel2 in toolkit/devtools (r=jryans) (a4ffc704e) - Bug 1138648 - Update remaining callsites to use newChannel2 in netwerk/ (r=sworkman) (cdf6612a9) - bug 1135160 - implement link rel=preconnect r=smaug (cfac502ce) - bug 1135160 - ioservice have speculative connect use proxy-resolve2() r=hurley (238b58f84) - Bug 1140788 - Set headers to immutable. r=bkelly,ehsan (c48c12acf) - Bug 1137037 - Determine the inner window ID in imgRequest::Init. r=baku (12aa73a7c) - Bug 1137019 (Part 1) - Get rid of unused LockImage forwarding methods on imgRequest. r=baku (6ed5c7d25) - Bug 1137019 (Part 2) - Replace imgRequest's image decoding methods with a single minimal method that updates an atomic. r=baku (adeb8797c)
1081 lines
32 KiB
C++
1081 lines
32 KiB
C++
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
*
|
|
* 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/. */
|
|
|
|
#include "imgRequest.h"
|
|
#include "ImageLogging.h"
|
|
|
|
#include "imgLoader.h"
|
|
#include "imgRequestProxy.h"
|
|
#include "DecodePool.h"
|
|
#include "ProgressTracker.h"
|
|
#include "ImageFactory.h"
|
|
#include "Image.h"
|
|
#include "MultipartImage.h"
|
|
#include "RasterImage.h"
|
|
|
|
#include "nsIChannel.h"
|
|
#include "nsICachingChannel.h"
|
|
#include "nsIDocument.h"
|
|
#include "nsIThreadRetargetableRequest.h"
|
|
#include "nsIInputStream.h"
|
|
#include "nsIMultiPartChannel.h"
|
|
#include "nsIHttpChannel.h"
|
|
#include "nsIApplicationCache.h"
|
|
#include "nsIApplicationCacheChannel.h"
|
|
#include "nsMimeTypes.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
#include "nsISupportsPrimitives.h"
|
|
#include "nsIScriptSecurityManager.h"
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsICacheEntry.h"
|
|
|
|
#include "plstr.h" // PL_strcasestr(...)
|
|
#include "nsNetUtil.h"
|
|
#include "nsIProtocolHandler.h"
|
|
#include "imgIRequest.h"
|
|
|
|
using namespace mozilla;
|
|
using namespace mozilla::image;
|
|
|
|
#if defined(PR_LOGGING)
|
|
PRLogModuleInfo*
|
|
GetImgLog()
|
|
{
|
|
static PRLogModuleInfo* sImgLog;
|
|
if (!sImgLog)
|
|
sImgLog = PR_NewLogModule("imgRequest");
|
|
return sImgLog;
|
|
}
|
|
#define LOG_TEST(level) (GetImgLog() && PR_LOG_TEST(GetImgLog(), (level)))
|
|
#else
|
|
#define LOG_TEST(level) false
|
|
#endif
|
|
|
|
NS_IMPL_ISUPPORTS(imgRequest,
|
|
nsIStreamListener, nsIRequestObserver,
|
|
nsIThreadRetargetableStreamListener,
|
|
nsIChannelEventSink,
|
|
nsIInterfaceRequestor,
|
|
nsIAsyncVerifyRedirectCallback)
|
|
|
|
imgRequest::imgRequest(imgLoader* aLoader)
|
|
: mLoader(aLoader)
|
|
, mProgressTracker(new ProgressTracker())
|
|
, mValidator(nullptr)
|
|
, mInnerWindowId(0)
|
|
, mCORSMode(imgIRequest::CORS_NONE)
|
|
, mReferrerPolicy(mozilla::net::RP_Default)
|
|
, mImageErrorCode(NS_OK)
|
|
, mDecodeRequested(false)
|
|
, mIsMultiPartChannel(false)
|
|
, mGotData(false)
|
|
, mIsInCache(false)
|
|
, mNewPartPending(false)
|
|
{ }
|
|
|
|
imgRequest::~imgRequest()
|
|
{
|
|
if (mLoader) {
|
|
mLoader->RemoveFromUncachedImages(this);
|
|
}
|
|
if (mURI) {
|
|
nsAutoCString spec;
|
|
mURI->GetSpec(spec);
|
|
LOG_FUNC_WITH_PARAM(GetImgLog(), "imgRequest::~imgRequest()", "keyuri", spec.get());
|
|
} else
|
|
LOG_FUNC(GetImgLog(), "imgRequest::~imgRequest()");
|
|
}
|
|
|
|
nsresult imgRequest::Init(nsIURI *aURI,
|
|
nsIURI *aCurrentURI,
|
|
nsIRequest *aRequest,
|
|
nsIChannel *aChannel,
|
|
imgCacheEntry *aCacheEntry,
|
|
nsISupports* aCX,
|
|
nsIPrincipal* aLoadingPrincipal,
|
|
int32_t aCORSMode,
|
|
ReferrerPolicy aReferrerPolicy)
|
|
{
|
|
MOZ_ASSERT(NS_IsMainThread(), "Cannot use nsIURI off main thread!");
|
|
|
|
LOG_FUNC(GetImgLog(), "imgRequest::Init");
|
|
|
|
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");
|
|
|
|
mProperties = do_CreateInstance("@mozilla.org/properties;1");
|
|
|
|
// Use ImageURL to ensure access to URI data off main thread.
|
|
mURI = new ImageURL(aURI);
|
|
mCurrentURI = aCurrentURI;
|
|
mRequest = aRequest;
|
|
mChannel = aChannel;
|
|
mTimedChannel = do_QueryInterface(mChannel);
|
|
|
|
mLoadingPrincipal = aLoadingPrincipal;
|
|
mCORSMode = aCORSMode;
|
|
mReferrerPolicy = aReferrerPolicy;
|
|
|
|
mChannel->GetNotificationCallbacks(getter_AddRefs(mPrevChannelSink));
|
|
|
|
NS_ASSERTION(mPrevChannelSink != this,
|
|
"Initializing with a channel that already calls back to us!");
|
|
|
|
mChannel->SetNotificationCallbacks(this);
|
|
|
|
mCacheEntry = aCacheEntry;
|
|
|
|
SetLoadId(aCX);
|
|
|
|
// Grab the inner window ID of the loading document, if possible.
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aCX);
|
|
if (doc) {
|
|
mInnerWindowId = doc->InnerWindowID();
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
void imgRequest::ClearLoader() {
|
|
mLoader = nullptr;
|
|
}
|
|
|
|
already_AddRefed<ProgressTracker>
|
|
imgRequest::GetProgressTracker()
|
|
{
|
|
if (mImage) {
|
|
MOZ_ASSERT(!mProgressTracker,
|
|
"Should have given mProgressTracker to mImage");
|
|
return mImage->GetProgressTracker();
|
|
} else {
|
|
MOZ_ASSERT(mProgressTracker,
|
|
"Should have mProgressTracker until we create mImage");
|
|
nsRefPtr<ProgressTracker> progressTracker = mProgressTracker;
|
|
MOZ_ASSERT(progressTracker);
|
|
return progressTracker.forget();
|
|
}
|
|
}
|
|
|
|
void imgRequest::SetCacheEntry(imgCacheEntry *entry)
|
|
{
|
|
mCacheEntry = entry;
|
|
}
|
|
|
|
bool imgRequest::HasCacheEntry() const
|
|
{
|
|
return mCacheEntry != nullptr;
|
|
}
|
|
|
|
void imgRequest::ResetCacheEntry()
|
|
{
|
|
if (HasCacheEntry()) {
|
|
mCacheEntry->SetDataSize(0);
|
|
}
|
|
}
|
|
|
|
void imgRequest::AddProxy(imgRequestProxy *proxy)
|
|
{
|
|
NS_PRECONDITION(proxy, "null imgRequestProxy passed in");
|
|
LOG_SCOPE_WITH_PARAM(GetImgLog(), "imgRequest::AddProxy", "proxy", proxy);
|
|
|
|
// If we're empty before adding, we have to tell the loader we now have
|
|
// proxies.
|
|
nsRefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
|
if (progressTracker->ObserverCount() == 0) {
|
|
MOZ_ASSERT(mURI, "Trying to SetHasProxies without key uri.");
|
|
if (mLoader) {
|
|
mLoader->SetHasProxies(this);
|
|
}
|
|
}
|
|
|
|
progressTracker->AddObserver(proxy);
|
|
}
|
|
|
|
nsresult imgRequest::RemoveProxy(imgRequestProxy *proxy, nsresult aStatus)
|
|
{
|
|
LOG_SCOPE_WITH_PARAM(GetImgLog(), "imgRequest::RemoveProxy", "proxy", proxy);
|
|
|
|
// 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();
|
|
|
|
// 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.
|
|
nsRefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
|
if (!progressTracker->RemoveObserver(proxy))
|
|
return NS_OK;
|
|
|
|
if (progressTracker->ObserverCount() == 0) {
|
|
// 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) {
|
|
MOZ_ASSERT(mURI, "Removing last observer without key uri.");
|
|
|
|
if (mLoader) {
|
|
mLoader->SetHasNoProxies(this, mCacheEntry);
|
|
}
|
|
}
|
|
#if defined(PR_LOGGING)
|
|
else {
|
|
nsAutoCString spec;
|
|
mURI->GetSpec(spec);
|
|
LOG_MSG_WITH_PARAM(GetImgLog(), "imgRequest::RemoveProxy no cache entry", "uri", spec.get());
|
|
}
|
|
#endif
|
|
|
|
/* 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.
|
|
*/
|
|
if (!(progressTracker->GetProgress() & FLAG_LAST_PART_COMPLETE) &&
|
|
NS_FAILED(aStatus)) {
|
|
LOG_MSG(GetImgLog(), "imgRequest::RemoveProxy", "load in progress. canceling");
|
|
|
|
this->Cancel(NS_BINDING_ABORTED);
|
|
}
|
|
|
|
/* break the cycle from the cache entry. */
|
|
mCacheEntry = nullptr;
|
|
}
|
|
|
|
// If a proxy is removed for a reason other than its owner being
|
|
// changed, remove the proxy from the loadgroup.
|
|
if (aStatus != NS_IMAGELIB_CHANGING_OWNER)
|
|
proxy->RemoveFromLoadGroup(true);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
void imgRequest::CancelAndAbort(nsresult aStatus)
|
|
{
|
|
LOG_SCOPE(GetImgLog(), "imgRequest::CancelAndAbort");
|
|
|
|
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);
|
|
mPrevChannelSink = nullptr;
|
|
}
|
|
}
|
|
|
|
class imgRequestMainThreadCancel : public nsRunnable
|
|
{
|
|
public:
|
|
imgRequestMainThreadCancel(imgRequest *aImgRequest, nsresult aStatus)
|
|
: 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;
|
|
}
|
|
private:
|
|
nsRefPtr<imgRequest> mImgRequest;
|
|
nsresult mStatus;
|
|
};
|
|
|
|
void imgRequest::Cancel(nsresult aStatus)
|
|
{
|
|
/* The Cancel() method here should only be called by this class. */
|
|
LOG_SCOPE(GetImgLog(), "imgRequest::Cancel");
|
|
|
|
if (NS_IsMainThread()) {
|
|
ContinueCancel(aStatus);
|
|
} else {
|
|
NS_DispatchToMainThread(new imgRequestMainThreadCancel(this, aStatus));
|
|
}
|
|
}
|
|
|
|
void imgRequest::ContinueCancel(nsresult aStatus)
|
|
{
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsRefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
|
progressTracker->SyncNotifyProgress(FLAG_HAS_ERROR | FLAG_ONLOAD_UNBLOCKED);
|
|
|
|
RemoveFromCache();
|
|
|
|
if (mRequest && !(progressTracker->GetProgress() & FLAG_LAST_PART_COMPLETE)) {
|
|
mRequest->Cancel(aStatus);
|
|
}
|
|
}
|
|
|
|
class imgRequestMainThreadEvict : public nsRunnable
|
|
{
|
|
public:
|
|
explicit imgRequestMainThreadEvict(imgRequest *aImgRequest)
|
|
: 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:
|
|
nsRefPtr<imgRequest> mImgRequest;
|
|
};
|
|
|
|
// EvictFromCache() is written to allowed to get called from any thread
|
|
void imgRequest::EvictFromCache()
|
|
{
|
|
/* The EvictFromCache() method here should only be called by this class. */
|
|
LOG_SCOPE(GetImgLog(), "imgRequest::EvictFromCache");
|
|
|
|
if (NS_IsMainThread()) {
|
|
ContinueEvict();
|
|
} else {
|
|
NS_DispatchToMainThread(new imgRequestMainThreadEvict(this));
|
|
}
|
|
}
|
|
|
|
// Helper-method used by EvictFromCache()
|
|
void imgRequest::ContinueEvict()
|
|
{
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
RemoveFromCache();
|
|
}
|
|
|
|
nsresult imgRequest::GetURI(ImageURL **aURI)
|
|
{
|
|
MOZ_ASSERT(aURI);
|
|
|
|
LOG_FUNC(GetImgLog(), "imgRequest::GetURI");
|
|
|
|
if (mURI) {
|
|
*aURI = mURI;
|
|
NS_ADDREF(*aURI);
|
|
return NS_OK;
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
nsresult imgRequest::GetCurrentURI(nsIURI **aURI)
|
|
{
|
|
MOZ_ASSERT(aURI);
|
|
|
|
LOG_FUNC(GetImgLog(), "imgRequest::GetCurrentURI");
|
|
|
|
if (mCurrentURI) {
|
|
*aURI = mCurrentURI;
|
|
NS_ADDREF(*aURI);
|
|
return NS_OK;
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
nsresult imgRequest::GetImageErrorCode()
|
|
{
|
|
return mImageErrorCode;
|
|
}
|
|
|
|
nsresult imgRequest::GetSecurityInfo(nsISupports **aSecurityInfo)
|
|
{
|
|
LOG_FUNC(GetImgLog(), "imgRequest::GetSecurityInfo");
|
|
|
|
// 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;
|
|
}
|
|
|
|
void imgRequest::RemoveFromCache()
|
|
{
|
|
LOG_SCOPE(GetImgLog(), "imgRequest::RemoveFromCache");
|
|
|
|
if (mIsInCache && mLoader) {
|
|
// mCacheEntry is nulled out when we have no more observers.
|
|
if (mCacheEntry) {
|
|
mLoader->RemoveFromCache(mCacheEntry);
|
|
} else {
|
|
mLoader->RemoveFromCache(mURI);
|
|
}
|
|
}
|
|
|
|
mCacheEntry = nullptr;
|
|
}
|
|
|
|
bool imgRequest::HasConsumers()
|
|
{
|
|
nsRefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
|
return progressTracker && progressTracker->ObserverCount() > 0;
|
|
}
|
|
|
|
int32_t imgRequest::Priority() const
|
|
{
|
|
int32_t priority = nsISupportsPriority::PRIORITY_NORMAL;
|
|
nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mRequest);
|
|
if (p)
|
|
p->GetPriority(&priority);
|
|
return priority;
|
|
}
|
|
|
|
void imgRequest::AdjustPriority(imgRequestProxy *proxy, int32_t delta)
|
|
{
|
|
// 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.
|
|
//
|
|
nsRefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
|
if (!progressTracker->FirstObserverIs(proxy))
|
|
return;
|
|
|
|
nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mChannel);
|
|
if (p)
|
|
p->AdjustPriority(delta);
|
|
}
|
|
|
|
void imgRequest::SetIsInCache(bool incache)
|
|
{
|
|
LOG_FUNC_WITH_PARAM(GetImgLog(), "imgRequest::SetIsCacheable", "incache", incache);
|
|
mIsInCache = incache;
|
|
}
|
|
|
|
void imgRequest::UpdateCacheEntrySize()
|
|
{
|
|
if (mCacheEntry) {
|
|
size_t size = mImage->SizeOfSourceWithComputedFallback(moz_malloc_size_of);
|
|
mCacheEntry->SetDataSize(size);
|
|
}
|
|
}
|
|
|
|
void imgRequest::SetCacheValidation(imgCacheEntry* aCacheEntry, nsIRequest* aRequest)
|
|
{
|
|
/* get the expires info */
|
|
if (aCacheEntry) {
|
|
nsCOMPtr<nsICachingChannel> cacheChannel(do_QueryInterface(aRequest));
|
|
if (cacheChannel) {
|
|
nsCOMPtr<nsISupports> cacheToken;
|
|
cacheChannel->GetCacheToken(getter_AddRefs(cacheToken));
|
|
if (cacheToken) {
|
|
nsCOMPtr<nsICacheEntry> entryDesc(do_QueryInterface(cacheToken));
|
|
if (entryDesc) {
|
|
uint32_t expiration;
|
|
/* get the expiration time from the caching channel's token */
|
|
entryDesc->GetExpirationTime(&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);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Determine whether the cache entry must be revalidated when we try to use it.
|
|
// Currently, only HTTP specifies this information...
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(aRequest));
|
|
if (httpChannel) {
|
|
bool bMustRevalidate = false;
|
|
|
|
httpChannel->IsNoStoreResponse(&bMustRevalidate);
|
|
|
|
if (!bMustRevalidate) {
|
|
httpChannel->IsNoCacheResponse(&bMustRevalidate);
|
|
}
|
|
|
|
if (!bMustRevalidate) {
|
|
nsAutoCString cacheHeader;
|
|
|
|
httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Cache-Control"),
|
|
cacheHeader);
|
|
if (PL_strcasestr(cacheHeader.get(), "must-revalidate")) {
|
|
bMustRevalidate = true;
|
|
}
|
|
}
|
|
|
|
// 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 (bMustRevalidate)
|
|
aCacheEntry->SetMustValidate(bMustRevalidate);
|
|
}
|
|
|
|
// We always need to validate file URIs.
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
|
|
if (channel) {
|
|
nsCOMPtr<nsIURI> uri;
|
|
channel->GetURI(getter_AddRefs(uri));
|
|
bool isfile = false;
|
|
uri->SchemeIs("file", &isfile);
|
|
if (isfile)
|
|
aCacheEntry->SetMustValidate(isfile);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace { // anon
|
|
|
|
already_AddRefed<nsIApplicationCache>
|
|
GetApplicationCache(nsIRequest* aRequest)
|
|
{
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIApplicationCacheChannel> appCacheChan = do_QueryInterface(aRequest);
|
|
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();
|
|
}
|
|
|
|
} // anon
|
|
|
|
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).
|
|
if (newAppCache == mApplicationCache)
|
|
return false;
|
|
|
|
// In a rare case it may happen that two objects still refer
|
|
// the same application cache version.
|
|
if (newAppCache && mApplicationCache) {
|
|
nsresult rv;
|
|
|
|
nsAutoCString oldAppCacheClientId, newAppCacheClientId;
|
|
rv = mApplicationCache->GetClientID(oldAppCacheClientId);
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
rv = newAppCache->GetClientID(newAppCacheClientId);
|
|
NS_ENSURE_SUCCESS(rv, true);
|
|
|
|
if (oldAppCacheClientId == newAppCacheClientId)
|
|
return false;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
/** nsIRequestObserver methods **/
|
|
|
|
/* void onStartRequest (in nsIRequest request, in nsISupports ctxt); */
|
|
NS_IMETHODIMP imgRequest::OnStartRequest(nsIRequest *aRequest, nsISupports *ctxt)
|
|
{
|
|
LOG_SCOPE(GetImgLog(), "imgRequest::OnStartRequest");
|
|
|
|
mNewPartPending = true;
|
|
|
|
// Figure out if we're multipart.
|
|
nsCOMPtr<nsIMultiPartChannel> mpchan(do_QueryInterface(aRequest));
|
|
nsRefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
|
if (mpchan) {
|
|
mIsMultiPartChannel = true;
|
|
} else {
|
|
MOZ_ASSERT(!mIsMultiPartChannel, "Something went wrong");
|
|
}
|
|
|
|
// If we're not multipart, we shouldn't have an image yet
|
|
if (mImage && !mIsMultiPartChannel) {
|
|
MOZ_ASSERT_UNREACHABLE("Already have an image for a non-multipart request");
|
|
Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
/*
|
|
* 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) {
|
|
NS_ASSERTION(mpchan,
|
|
"We should have an mRequest here unless we're multipart");
|
|
nsCOMPtr<nsIChannel> chan;
|
|
mpchan->GetBaseChannel(getter_AddRefs(chan));
|
|
mRequest = chan;
|
|
}
|
|
|
|
nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
|
|
if (channel)
|
|
channel->GetSecurityInfo(getter_AddRefs(mSecurityInfo));
|
|
|
|
/* Get our principal */
|
|
nsCOMPtr<nsIChannel> chan(do_QueryInterface(aRequest));
|
|
if (chan) {
|
|
nsCOMPtr<nsIScriptSecurityManager> secMan = nsContentUtils::GetSecurityManager();
|
|
if (secMan) {
|
|
nsresult rv = secMan->GetChannelResultPrincipal(chan,
|
|
getter_AddRefs(mPrincipal));
|
|
if (NS_FAILED(rv)) {
|
|
return rv;
|
|
}
|
|
}
|
|
}
|
|
|
|
SetCacheValidation(mCacheEntry, aRequest);
|
|
|
|
mApplicationCache = GetApplicationCache(aRequest);
|
|
|
|
// Shouldn't we be dead already if this gets hit? Probably multipart/x-mixed-replace...
|
|
if (progressTracker->ObserverCount() == 0) {
|
|
this->Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
|
}
|
|
|
|
// Try to retarget OnDataAvailable to a decode thread.
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aRequest);
|
|
nsCOMPtr<nsIThreadRetargetableRequest> retargetable =
|
|
do_QueryInterface(aRequest);
|
|
if (httpChannel && retargetable) {
|
|
nsAutoCString mimeType;
|
|
nsresult rv = httpChannel->GetContentType(mimeType);
|
|
if (NS_SUCCEEDED(rv) && !mimeType.EqualsLiteral(IMAGE_SVG_XML)) {
|
|
// Retarget OnDataAvailable to the DecodePool's IO thread.
|
|
nsCOMPtr<nsIEventTarget> target =
|
|
DecodePool::Singleton()->GetIOEventTarget();
|
|
rv = retargetable->RetargetDeliveryTo(target);
|
|
}
|
|
PR_LOG(GetImgLog(), PR_LOG_WARNING,
|
|
("[this=%p] imgRequest::OnStartRequest -- "
|
|
"RetargetDeliveryTo rv %d=%s\n",
|
|
this, rv, NS_SUCCEEDED(rv) ? "succeeded" : "failed"));
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
/* void onStopRequest (in nsIRequest request, in nsISupports ctxt, in nsresult status); */
|
|
NS_IMETHODIMP imgRequest::OnStopRequest(nsIRequest *aRequest, nsISupports *ctxt, nsresult status)
|
|
{
|
|
LOG_FUNC(GetImgLog(), "imgRequest::OnStopRequest");
|
|
MOZ_ASSERT(NS_IsMainThread(), "Can't send notifications off-main-thread");
|
|
|
|
// XXXldb What if this is a non-last part of a multipart request?
|
|
// xxx before we release our reference to mRequest, lets
|
|
// save the last status that we saw so that the
|
|
// imgRequestProxy will have access to it.
|
|
if (mRequest) {
|
|
mRequest = nullptr; // we no longer need the request
|
|
}
|
|
|
|
// stop holding a ref to the channel, since we don't need it anymore
|
|
if (mChannel) {
|
|
mChannel->SetNotificationCallbacks(mPrevChannelSink);
|
|
mPrevChannelSink = nullptr;
|
|
mChannel = nullptr;
|
|
}
|
|
|
|
bool lastPart = true;
|
|
nsCOMPtr<nsIMultiPartChannel> mpchan(do_QueryInterface(aRequest));
|
|
if (mpchan)
|
|
mpchan->GetIsLastPart(&lastPart);
|
|
|
|
bool isPartial = false;
|
|
if (mImage && (status == NS_ERROR_NET_PARTIAL_TRANSFER)) {
|
|
isPartial = true;
|
|
status = NS_OK; // fake happy face
|
|
}
|
|
|
|
// 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.
|
|
if (mImage) {
|
|
nsresult rv = mImage->OnImageDataComplete(aRequest, ctxt, status, lastPart);
|
|
|
|
// 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.
|
|
if (NS_FAILED(rv) && NS_SUCCEEDED(status))
|
|
status = rv;
|
|
}
|
|
|
|
// If the request went through, update the cache entry size. Otherwise,
|
|
// cancel the request, which removes us from the cache.
|
|
if (mImage && NS_SUCCEEDED(status) && !isPartial) {
|
|
// 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();
|
|
}
|
|
else if (isPartial) {
|
|
// Remove the partial image from the cache.
|
|
this->EvictFromCache();
|
|
}
|
|
else {
|
|
mImageErrorCode = status;
|
|
|
|
// if the error isn't "just" a partial transfer
|
|
// stops animations, removes from cache
|
|
this->Cancel(status);
|
|
}
|
|
|
|
if (!mImage) {
|
|
// We have to fire the OnStopRequest notifications ourselves because there's
|
|
// no image capable of doing so.
|
|
Progress progress =
|
|
LoadCompleteProgress(lastPart, /* aError = */ false, status);
|
|
|
|
nsRefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
|
progressTracker->SyncNotifyProgress(progress);
|
|
}
|
|
|
|
mTimedChannel = nullptr;
|
|
return NS_OK;
|
|
}
|
|
|
|
struct mimetype_closure
|
|
{
|
|
nsACString* newType;
|
|
};
|
|
|
|
/* prototype for these defined below */
|
|
static NS_METHOD sniff_mimetype_callback(nsIInputStream* in, void* closure, const char* fromRawSegment,
|
|
uint32_t toOffset, uint32_t count, uint32_t *writeCount);
|
|
|
|
/** 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;
|
|
}
|
|
|
|
/** nsIStreamListener methods **/
|
|
|
|
/* void onDataAvailable (in nsIRequest request, in nsISupports ctxt, in nsIInputStream inStr, in unsigned long long sourceOffset, in unsigned long count); */
|
|
NS_IMETHODIMP
|
|
imgRequest::OnDataAvailable(nsIRequest *aRequest, nsISupports *ctxt,
|
|
nsIInputStream *inStr, uint64_t sourceOffset,
|
|
uint32_t count)
|
|
{
|
|
LOG_SCOPE_WITH_PARAM(GetImgLog(), "imgRequest::OnDataAvailable", "count", count);
|
|
|
|
NS_ASSERTION(aRequest, "imgRequest::OnDataAvailable -- no request!");
|
|
|
|
nsresult rv;
|
|
mGotData = true;
|
|
|
|
if (mNewPartPending) {
|
|
LOG_SCOPE(GetImgLog(), "imgRequest::OnDataAvailable |New part; finding MIME type|");
|
|
|
|
mNewPartPending = false;
|
|
|
|
mimetype_closure closure;
|
|
nsAutoCString newType;
|
|
closure.newType = &newType;
|
|
|
|
/* 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;
|
|
inStr->ReadSegments(sniff_mimetype_callback, &closure, count, &out);
|
|
|
|
nsCOMPtr<nsIChannel> chan(do_QueryInterface(aRequest));
|
|
if (newType.IsEmpty()) {
|
|
LOG_SCOPE(GetImgLog(), "imgRequest::OnDataAvailable |sniffing of mimetype failed|");
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
if (chan) {
|
|
rv = chan->GetContentType(newType);
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
PR_LOG(GetImgLog(), PR_LOG_ERROR,
|
|
("[this=%p] imgRequest::OnDataAvailable -- Content type unavailable from the channel\n",
|
|
this));
|
|
|
|
this->Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
|
|
|
return NS_BINDING_ABORTED;
|
|
}
|
|
|
|
LOG_MSG(GetImgLog(), "imgRequest::OnDataAvailable", "Got content type from the channel");
|
|
}
|
|
|
|
mContentType = newType;
|
|
SetProperties(chan);
|
|
bool firstPart = !mImage;
|
|
|
|
LOG_MSG_WITH_PARAM(GetImgLog(), "imgRequest::OnDataAvailable", "content type", 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 (mIsMultiPartChannel) {
|
|
// Create the ProgressTracker and image for this part.
|
|
nsRefPtr<ProgressTracker> progressTracker = new ProgressTracker();
|
|
nsRefPtr<Image> image =
|
|
ImageFactory::CreateImage(aRequest, progressTracker, mContentType,
|
|
mURI, /* aIsMultipart = */ true,
|
|
static_cast<uint32_t>(mInnerWindowId));
|
|
|
|
if (!mImage) {
|
|
// First part for a multipart channel. Create the MultipartImage wrapper.
|
|
MOZ_ASSERT(mProgressTracker, "Shouldn't have given away tracker yet");
|
|
mImage = new MultipartImage(image, mProgressTracker);
|
|
mProgressTracker = nullptr;
|
|
} else {
|
|
// Transition to the new part.
|
|
static_cast<MultipartImage*>(mImage.get())->BeginTransitionToPart(image);
|
|
}
|
|
} else {
|
|
MOZ_ASSERT(!mImage, "New part for non-multipart channel?");
|
|
MOZ_ASSERT(mProgressTracker, "Shouldn't have given away tracker yet");
|
|
|
|
// Create an image using our progress tracker.
|
|
mImage =
|
|
ImageFactory::CreateImage(aRequest, mProgressTracker, mContentType,
|
|
mURI, /* aIsMultipart = */ false,
|
|
static_cast<uint32_t>(mInnerWindowId));
|
|
mProgressTracker = nullptr;
|
|
}
|
|
|
|
if (firstPart) {
|
|
// Notify listeners that we have an image.
|
|
nsRefPtr<ProgressTracker> progressTracker = GetProgressTracker();
|
|
progressTracker->OnImageAvailable();
|
|
MOZ_ASSERT(progressTracker->HasImage());
|
|
}
|
|
|
|
if (mImage->HasError() && !mIsMultiPartChannel) { // Probably bad mimetype
|
|
// We allow multipart images to fail to initialize without cancelling the
|
|
// load because subsequent images might be fine; thus only single part
|
|
// images end up here.
|
|
this->Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
|
return NS_BINDING_ABORTED;
|
|
}
|
|
|
|
MOZ_ASSERT(!mProgressTracker, "Should've given tracker to image");
|
|
MOZ_ASSERT(mImage, "Should have image");
|
|
|
|
if (mDecodeRequested) {
|
|
mImage->StartDecoding();
|
|
}
|
|
}
|
|
|
|
// Notify the image that it has new data.
|
|
rv = mImage->OnImageDataAvailable(aRequest, ctxt, inStr, sourceOffset, count);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
PR_LOG(GetImgLog(), PR_LOG_WARNING,
|
|
("[this=%p] imgRequest::OnDataAvailable -- "
|
|
"copy to RasterImage failed\n", this));
|
|
this->Cancel(NS_IMAGELIB_ERROR_FAILURE);
|
|
return NS_BINDING_ABORTED;
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
class SetPropertiesEvent : public nsRunnable
|
|
{
|
|
public:
|
|
SetPropertiesEvent(imgRequest* aImgRequest, nsIChannel* aChan)
|
|
: mImgRequest(aImgRequest)
|
|
, mChan(aChan)
|
|
{
|
|
MOZ_ASSERT(!NS_IsMainThread(), "Should be created off the main thread");
|
|
MOZ_ASSERT(aImgRequest, "aImgRequest cannot be null");
|
|
}
|
|
NS_IMETHOD Run()
|
|
{
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should run on the main thread only");
|
|
MOZ_ASSERT(mImgRequest, "mImgRequest cannot be null");
|
|
mImgRequest->SetProperties(mChan);
|
|
return NS_OK;
|
|
}
|
|
private:
|
|
nsRefPtr<imgRequest> mImgRequest;
|
|
nsCOMPtr<nsIChannel> mChan;
|
|
};
|
|
|
|
void
|
|
imgRequest::SetProperties(nsIChannel* aChan)
|
|
{
|
|
// Force execution on main thread since some property objects are non
|
|
// threadsafe.
|
|
if (!NS_IsMainThread()) {
|
|
NS_DispatchToMainThread(new SetPropertiesEvent(this, aChan));
|
|
return;
|
|
}
|
|
/* set our mimetype as a property */
|
|
nsCOMPtr<nsISupportsCString> contentType(do_CreateInstance("@mozilla.org/supports-cstring;1"));
|
|
if (contentType) {
|
|
contentType->SetData(mContentType);
|
|
mProperties->Set("type", contentType);
|
|
}
|
|
|
|
/* set our content disposition as a property */
|
|
nsAutoCString disposition;
|
|
if (aChan) {
|
|
aChan->GetContentDispositionHeader(disposition);
|
|
}
|
|
if (!disposition.IsEmpty()) {
|
|
nsCOMPtr<nsISupportsCString> contentDisposition(do_CreateInstance("@mozilla.org/supports-cstring;1"));
|
|
if (contentDisposition) {
|
|
contentDisposition->SetData(disposition);
|
|
mProperties->Set("content-disposition", contentDisposition);
|
|
}
|
|
}
|
|
}
|
|
|
|
static NS_METHOD sniff_mimetype_callback(nsIInputStream* in,
|
|
void* data,
|
|
const char* fromRawSegment,
|
|
uint32_t toOffset,
|
|
uint32_t count,
|
|
uint32_t *writeCount)
|
|
{
|
|
mimetype_closure* closure = static_cast<mimetype_closure*>(data);
|
|
|
|
NS_ASSERTION(closure, "closure is null!");
|
|
|
|
if (count > 0)
|
|
imgLoader::GetMimeTypeFromContent(fromRawSegment, count, *closure->newType);
|
|
|
|
*writeCount = 0;
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
|
|
/** nsIInterfaceRequestor methods **/
|
|
|
|
NS_IMETHODIMP
|
|
imgRequest::GetInterface(const nsIID & aIID, void **aResult)
|
|
{
|
|
if (!mPrevChannelSink || aIID.Equals(NS_GET_IID(nsIChannelEventSink)))
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
NS_ASSERTION(mPrevChannelSink != this,
|
|
"Infinite recursion - don't keep track of channel sinks that are us!");
|
|
return mPrevChannelSink->GetInterface(aIID, aResult);
|
|
}
|
|
|
|
/** nsIChannelEventSink methods **/
|
|
NS_IMETHODIMP
|
|
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!");
|
|
NS_ASSERTION(newChannel, "Got a redirect to a NULL channel!");
|
|
|
|
SetCacheValidation(mCacheEntry, oldChannel);
|
|
|
|
// Prepare for callback
|
|
mRedirectCallback = callback;
|
|
mNewRedirectChannel = newChannel;
|
|
|
|
nsCOMPtr<nsIChannelEventSink> sink(do_GetInterface(mPrevChannelSink));
|
|
if (sink) {
|
|
nsresult rv = sink->AsyncOnChannelRedirect(oldChannel, newChannel, flags,
|
|
this);
|
|
if (NS_FAILED(rv)) {
|
|
mRedirectCallback = nullptr;
|
|
mNewRedirectChannel = nullptr;
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
(void) OnRedirectVerifyCallback(NS_OK);
|
|
return NS_OK;
|
|
}
|
|
|
|
NS_IMETHODIMP
|
|
imgRequest::OnRedirectVerifyCallback(nsresult result)
|
|
{
|
|
NS_ASSERTION(mRedirectCallback, "mRedirectCallback not set in callback");
|
|
NS_ASSERTION(mNewRedirectChannel, "mNewRedirectChannel not set in callback");
|
|
|
|
if (NS_FAILED(result)) {
|
|
mRedirectCallback->OnRedirectVerifyCallback(result);
|
|
mRedirectCallback = nullptr;
|
|
mNewRedirectChannel = nullptr;
|
|
return NS_OK;
|
|
}
|
|
|
|
mChannel = mNewRedirectChannel;
|
|
mTimedChannel = do_QueryInterface(mChannel);
|
|
mNewRedirectChannel = nullptr;
|
|
|
|
if (LOG_TEST(PR_LOG_DEBUG)) {
|
|
nsAutoCString spec;
|
|
if (mCurrentURI)
|
|
mCurrentURI->GetSpec(spec);
|
|
LOG_MSG_WITH_PARAM(GetImgLog(), "imgRequest::OnChannelRedirect", "old", spec.get());
|
|
}
|
|
|
|
// make sure we have a protocol that returns data rather than opens
|
|
// an external application, e.g. mailto:
|
|
mChannel->GetURI(getter_AddRefs(mCurrentURI));
|
|
|
|
if (LOG_TEST(PR_LOG_DEBUG)) {
|
|
nsAutoCString spec;
|
|
if (mCurrentURI)
|
|
mCurrentURI->GetSpec(spec);
|
|
LOG_MSG_WITH_PARAM(GetImgLog(), "imgRequest::OnChannelRedirect", "new", spec.get());
|
|
}
|
|
|
|
bool doesNotReturnData = false;
|
|
nsresult rv =
|
|
NS_URIChainHasFlags(mCurrentURI, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA,
|
|
&doesNotReturnData);
|
|
|
|
if (NS_SUCCEEDED(rv) && doesNotReturnData)
|
|
rv = NS_ERROR_ABORT;
|
|
|
|
if (NS_FAILED(rv)) {
|
|
mRedirectCallback->OnRedirectVerifyCallback(rv);
|
|
mRedirectCallback = nullptr;
|
|
return NS_OK;
|
|
}
|
|
|
|
mRedirectCallback->OnRedirectVerifyCallback(NS_OK);
|
|
mRedirectCallback = nullptr;
|
|
return NS_OK;
|
|
}
|