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
2010-07-28 21:51:37 +00:00
/* We end up pulling in windows.h because we eventually hit gfxWindowsSurface;
* windows . h defines LoadImage , so we have to # undef it or imgLoader : : LoadImage
* gets changed .
* This # undef needs to be in multiple places because we don ' t always pull
* headers in in the same order .
*/
# undef LoadImage
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"
2010-08-14 04:09:49 +00:00
# include "RasterImage.h"
2010-09-08 20:40:39 +00:00
# include "VectorImage.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-05-08 04:21:49 +00:00
# include "imgILoader.h"
2002-05-03 07:49:43 +00:00
# include "netCore.h"
2001-01-22 22:01:03 +00:00
# include "nsIChannel.h"
2001-05-08 04:21:49 +00:00
# include "nsICachingChannel.h"
2001-02-25 08:37:26 +00:00
# include "nsILoadGroup.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"
2001-03-25 11:57:25 +00:00
2001-01-22 22:01:03 +00:00
# include "nsIComponentManager.h"
2008-12-22 22:20:46 +00:00
# include "nsIInterfaceRequestorUtils.h"
2001-01-23 02:02:29 +00:00
# include "nsIServiceManager.h"
2005-03-23 21:09:02 +00:00
# include "nsISupportsPrimitives.h"
2007-11-09 02:55:41 +00:00
# include "nsIScriptSecurityManager.h"
2001-01-22 22:01:03 +00:00
2008-09-04 23:00:42 +00:00
# include "nsICacheVisitor.h"
2001-01-22 22:01:03 +00:00
# include "nsString.h"
2001-03-14 00:39:48 +00:00
# include "nsXPIDLString.h"
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"
2001-01-22 22:01:03 +00:00
2011-06-12 02:30:15 +00:00
# include "mozilla/Preferences.h"
2012-10-26 13:32:10 +00:00
# include "mozilla/Likely.h"
2010-06-24 18:33:34 +00:00
2010-08-14 04:09:49 +00:00
# include "DiscardTracker.h"
2010-08-05 02:15:55 +00:00
# include "nsAsyncRedirectVerifyHelper.h"
2010-07-12 01:51:52 +00:00
2010-08-14 04:09:49 +00:00
# define SVG_MIMETYPE "image / svg+xml"
2010-06-24 18:33:34 +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
2011-12-22 14:21:00 +00:00
static bool gInitializedPrefCaches = false ;
2011-09-29 06:19:26 +00:00
static bool gDecodeOnDraw = false ;
static bool gDiscardable = false ;
2009-09-12 22:44:18 +00:00
2010-06-24 18:33:34 +00:00
static void
2011-12-22 14:21:00 +00:00
InitPrefCaches ( )
2010-06-24 18:33:34 +00:00
{
2011-12-22 14:21:00 +00:00
Preferences : : AddBoolVarCache ( & gDiscardable , " image.mem.discardable " ) ;
Preferences : : AddBoolVarCache ( & gDecodeOnDraw , " image.mem.decodeondraw " ) ;
gInitializedPrefCaches = true ;
2010-06-24 18:33:34 +00:00
}
2001-02-22 02:06:24 +00:00
# if defined(PR_LOGGING)
2012-10-29 23:32:10 +00:00
PRLogModuleInfo *
GetImgLog ( )
{
static PRLogModuleInfo * sImgLog ;
if ( ! sImgLog )
sImgLog = PR_NewLogModule ( " imgRequest " ) ;
return sImgLog ;
}
2001-02-22 02:06:24 +00:00
# endif
2001-01-22 22:01:03 +00:00
2012-10-12 16:11:21 +00:00
NS_IMPL_ISUPPORTS5 ( imgRequest ,
2006-03-14 06:24:02 +00:00
nsIStreamListener , nsIRequestObserver ,
2008-12-22 22:20:46 +00:00
nsIChannelEventSink ,
2010-08-05 02:15:55 +00:00
nsIInterfaceRequestor ,
nsIAsyncVerifyRedirectCallback )
2001-01-22 22:01:03 +00:00
2012-09-06 22:05:23 +00:00
imgRequest : : imgRequest ( imgLoader * aLoader )
: mLoader ( aLoader )
2012-10-12 16:11:21 +00:00
, mStatusTracker ( new imgStatusTracker ( nullptr , this ) )
2012-09-06 22:05:23 +00:00
, mValidator ( nullptr )
, mImageSniffers ( " image-sniffing-services " )
, mInnerWindowId ( 0 )
, mCORSMode ( imgIRequest : : CORS_NONE )
, mDecodeRequested ( false )
, mIsMultiPartChannel ( false )
, mGotData ( false )
, mIsInCache ( false )
, mResniffMimeType ( false )
2011-12-22 14:21:00 +00:00
{
// Register our pref observers if we haven't yet.
2012-10-26 13:32:10 +00:00
if ( MOZ_UNLIKELY ( ! gInitializedPrefCaches ) ) {
2011-12-22 14:21:00 +00:00
InitPrefCaches ( ) ;
}
}
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
{
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 ) ;
2012-10-29 23:32:10 +00:00
LOG_FUNC_WITH_PARAM ( GetImgLog ( ) , " imgRequest::~imgRequest() " , " keyuri " , spec . get ( ) ) ;
2009-01-31 02:17:47 +00:00
} else
2012-10-29 23:32:10 +00:00
LOG_FUNC ( GetImgLog ( ) , " imgRequest::~imgRequest() " ) ;
2001-01-22 22:01:03 +00:00
}
2006-03-11 20:32:47 +00:00
nsresult imgRequest : : Init ( nsIURI * aURI ,
2011-07-01 17:03:40 +00:00
nsIURI * aCurrentURI ,
2006-03-11 20:32:47 +00:00
nsIRequest * aRequest ,
2008-12-22 22:20:46 +00:00
nsIChannel * aChannel ,
2008-09-04 23:00:42 +00:00
imgCacheEntry * aCacheEntry ,
2011-07-14 18:47:32 +00:00
void * aLoadId ,
2011-07-14 18:47:34 +00:00
nsIPrincipal * aLoadingPrincipal ,
2012-08-22 15:56:38 +00:00
int32_t aCORSMode )
2001-01-22 22:01:03 +00:00
{
2012-10-29 23:32:10 +00:00
LOG_FUNC ( GetImgLog ( ) , " imgRequest::Init " ) ;
2001-02-22 02:12:21 +00:00
2009-01-31 02:17:47 +00:00
NS_ABORT_IF_FALSE ( ! mImage , " Multiple calls to init " ) ;
NS_ABORT_IF_FALSE ( aURI , " No uri " ) ;
2011-07-01 17:03:40 +00:00
NS_ABORT_IF_FALSE ( aCurrentURI , " No current uri " ) ;
2009-01-31 02:17:47 +00:00
NS_ABORT_IF_FALSE ( aRequest , " No request " ) ;
NS_ABORT_IF_FALSE ( 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
2006-03-11 20:32:47 +00:00
mURI = 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 ;
2011-07-14 18:47:32 +00:00
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 ;
2002-01-15 05:23:33 +00:00
SetLoadId ( aLoadId ) ;
2001-01-23 02:02:29 +00:00
return NS_OK ;
2001-01-22 22:01:03 +00:00
}
2010-08-23 22:44:07 +00:00
imgStatusTracker &
imgRequest : : GetStatusTracker ( )
{
2012-05-19 19:32:37 +00:00
if ( mImage & & mGotData ) {
2010-08-23 22:44:07 +00:00
NS_ABORT_IF_FALSE ( ! mStatusTracker ,
" Should have given mStatusTracker to mImage " ) ;
return mImage - > GetStatusTracker ( ) ;
} else {
NS_ABORT_IF_FALSE ( mStatusTracker ,
" Should have mStatusTracker until we create mImage " ) ;
return * mStatusTracker ;
}
}
2009-01-31 02:17:47 +00:00
void imgRequest : : SetCacheEntry ( imgCacheEntry * entry )
{
mCacheEntry = entry ;
}
2011-09-29 06:19:26 +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
}
2012-10-12 16:11:21 +00:00
void imgRequest : : ResetCacheEntry ( )
{
if ( HasCacheEntry ( ) ) {
mCacheEntry - > SetDataSize ( 0 ) ;
}
}
2012-10-03 22:39:59 +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 " ) ;
2012-10-29 23:32:10 +00:00
LOG_SCOPE_WITH_PARAM ( GetImgLog ( ) , " imgRequest::AddProxy " , " proxy " , proxy ) ;
2001-02-22 02:06:24 +00:00
2009-01-31 02:17:47 +00:00
// If we're empty before adding, we have to tell the loader we now have
// proxies.
2012-10-12 16:11:20 +00:00
if ( GetStatusTracker ( ) . ConsumerCount ( ) = = 0 ) {
2011-07-01 17:03:40 +00:00
NS_ABORT_IF_FALSE ( mURI , " Trying to SetHasProxies without key uri. " ) ;
2012-06-26 04:20:12 +00:00
mLoader - > SetHasProxies ( mURI ) ;
2009-01-31 02:17:47 +00:00
}
2012-10-12 16:11:20 +00:00
GetStatusTracker ( ) . AddConsumer ( proxy ) ;
2002-01-15 05:23:33 +00:00
}
2012-10-11 16:35:43 +00:00
nsresult imgRequest : : RemoveProxy ( imgRequestProxy * proxy , nsresult aStatus )
2002-01-15 05:23:33 +00:00
{
2012-10-29 23:32:10 +00:00
LOG_SCOPE_WITH_PARAM ( GetImgLog ( ) , " 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.
2010-08-23 22:44:07 +00:00
imgStatusTracker & statusTracker = GetStatusTracker ( ) ;
2012-10-12 16:11:20 +00:00
if ( ! statusTracker . RemoveConsumer ( proxy , aStatus ) )
return NS_OK ;
2002-01-15 05:23:33 +00:00
2012-10-12 16:11:20 +00:00
if ( statusTracker . ConsumerCount ( ) = = 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 ) {
2011-07-01 17:03:40 +00:00
NS_ABORT_IF_FALSE ( mURI , " Removing last observer without key uri. " ) ;
2009-01-31 02:17:47 +00:00
2012-06-26 04:20:12 +00:00
mLoader - > SetHasNoProxies ( mURI , mCacheEntry ) ;
2009-01-31 02:17:47 +00:00
}
# if defined(PR_LOGGING)
else {
2012-09-02 02:35:17 +00:00
nsAutoCString spec ;
2011-07-01 17:03:40 +00:00
mURI - > GetSpec ( spec ) ;
2012-10-29 23:32:10 +00:00
LOG_MSG_WITH_PARAM ( GetImgLog ( ) , " imgRequest::RemoveProxy no cache entry " , " uri " , spec . get ( ) ) ;
2009-01-31 02:17:47 +00:00
}
# endif
2002-01-15 05:23:33 +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
2012-10-12 16:11:20 +00:00
and won ' t leave a bad pointer in the observer list .
2002-01-15 05:23:33 +00:00
*/
2010-08-23 22:44:07 +00:00
if ( statusTracker . IsLoading ( ) & & NS_FAILED ( aStatus ) ) {
2012-10-29 23:32:10 +00:00
LOG_MSG ( GetImgLog ( ) , " imgRequest::RemoveProxy " , " 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.
if ( aStatus ! = NS_IMAGELIB_CHANGING_OWNER )
2011-10-17 14:59:28 +00:00
proxy - > RemoveFromLoadGroup ( true ) ;
2003-05-14 05:42:06 +00:00
2002-01-15 05:23:33 +00:00
return NS_OK ;
}
2008-12-22 22:20:46 +00:00
void imgRequest : : CancelAndAbort ( nsresult aStatus )
{
2012-10-29 23:32:10 +00:00
LOG_SCOPE ( GetImgLog ( ) , " 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
}
2010-05-14 20:47:59 +00:00
void imgRequest : : Cancel ( nsresult aStatus )
{
/* The Cancel() method here should only be called by this class. */
2012-10-29 23:32:10 +00:00
LOG_SCOPE ( GetImgLog ( ) , " imgRequest::Cancel " ) ;
2010-05-14 20:47:59 +00:00
2010-08-23 22:44:07 +00:00
imgStatusTracker & statusTracker = GetStatusTracker ( ) ;
2012-08-13 22:58:53 +00:00
2012-10-12 16:11:21 +00:00
statusTracker . MaybeUnblockOnload ( ) ;
2012-08-13 22:58:53 +00:00
2010-08-23 22:44:07 +00:00
statusTracker . RecordCancel ( ) ;
2010-05-14 20:47:59 +00:00
RemoveFromCache ( ) ;
2010-08-23 22:44:07 +00:00
if ( mRequest & & statusTracker . IsLoading ( ) )
2010-05-14 20:47:59 +00:00
mRequest - > Cancel ( aStatus ) ;
}
2001-10-12 06:43:52 +00:00
nsresult imgRequest : : GetURI ( nsIURI * * aURI )
2001-03-06 04:46:20 +00:00
{
2012-10-29 23:32:10 +00:00
LOG_FUNC ( GetImgLog ( ) , " 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
}
2008-09-01 20:53:59 +00:00
nsresult imgRequest : : GetSecurityInfo ( nsISupports * * aSecurityInfo )
{
2012-10-29 23:32:10 +00:00
LOG_FUNC ( GetImgLog ( ) , " 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 ;
}
2001-10-12 06:43:52 +00:00
void imgRequest : : RemoveFromCache ( )
2001-01-22 22:01:03 +00:00
{
2012-10-29 23:32:10 +00:00
LOG_SCOPE ( GetImgLog ( ) , " imgRequest::RemoveFromCache " ) ;
2001-02-22 02:06:24 +00:00
2009-03-17 21:07:16 +00:00
if ( mIsInCache ) {
// mCacheEntry is nulled out when we have no more observers.
2009-03-05 03:56:14 +00:00
if ( mCacheEntry )
2012-06-26 04:20:12 +00:00
mLoader - > RemoveFromCache ( mCacheEntry ) ;
2009-03-05 03:56:14 +00:00
else
2012-06-26 04:20:12 +00:00
mLoader - > RemoveFromCache ( mURI ) ;
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
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 ) ;
2005-02-21 20:58:01 +00:00
if ( p )
p - > GetPriority ( & priority ) ;
return priority ;
}
2012-08-22 15:56:38 +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.
//
2012-10-12 16:11:20 +00:00
if ( ! GetStatusTracker ( ) . FirstConsumerIs ( proxy ) )
2005-02-21 20:58:01 +00:00
return ;
2012-10-13 16:47:36 +00:00
nsCOMPtr < nsISupportsPriority > p = do_QueryInterface ( mChannel ) ;
2005-02-21 20:58:01 +00:00
if ( p )
2005-02-21 22:21:21 +00:00
p - > AdjustPriority ( delta ) ;
2005-02-21 20:58:01 +00:00
}
2011-09-29 06:19:26 +00:00
void imgRequest : : SetIsInCache ( bool incache )
2009-03-05 03:56:14 +00:00
{
2012-10-29 23:32:10 +00:00
LOG_FUNC_WITH_PARAM ( GetImgLog ( ) , " imgRequest::SetIsCacheable " , " incache " , incache ) ;
2009-03-17 21:07:16 +00:00
mIsInCache = incache ;
2009-03-05 03:56:14 +00:00
}
2009-09-12 22:44:18 +00:00
void imgRequest : : UpdateCacheEntrySize ( )
2001-04-10 01:51:36 +00:00
{
2009-09-12 22:44:18 +00:00
if ( mCacheEntry ) {
2012-02-20 03:51:48 +00:00
mCacheEntry - > SetDataSize ( mImage - > SizeOfData ( ) ) ;
2001-04-10 01:51:36 +00:00
2009-09-12 22:44:18 +00:00
# ifdef DEBUG_joe
2012-09-02 02:35:17 +00:00
nsAutoCString url ;
2009-09-12 22:44:18 +00:00
mURI - > GetSpec ( url ) ;
printf ( " CACHEPUT: %d %s %d \n " , time ( NULL ) , url . get ( ) , imageSize ) ;
# endif
}
2001-01-22 22:01:03 +00:00
}
2011-07-01 17:03:33 +00:00
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 < nsICacheEntryInfo > entryDesc ( do_QueryInterface ( cacheToken ) ) ;
if ( entryDesc ) {
2012-08-22 15:56:38 +00:00
uint32_t expiration ;
2011-07-01 17:03:33 +00:00
/* 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 ) ;
}
}
}
2011-07-01 17:03:35 +00:00
// Determine whether the cache entry must be revalidated when we try to use it.
2011-07-01 17:03:33 +00:00
// Currently, only HTTP specifies this information...
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.
if ( bMustRevalidate )
2011-07-01 17:03:35 +00:00
aCacheEntry - > SetMustValidate ( bMustRevalidate ) ;
2011-07-01 17:03:33 +00:00
}
2011-07-01 17:03:37 +00:00
// We always need to validate file URIs.
nsCOMPtr < nsIChannel > channel = do_QueryInterface ( aRequest ) ;
if ( channel ) {
nsCOMPtr < nsIURI > uri ;
channel - > GetURI ( getter_AddRefs ( uri ) ) ;
2011-09-29 06:19:26 +00:00
bool isfile = false ;
2011-07-01 17:03:37 +00:00
uri - > SchemeIs ( " file " , & isfile ) ;
if ( isfile )
aCacheEntry - > SetMustValidate ( isfile ) ;
}
2011-07-01 17:03:33 +00:00
}
}
2009-09-16 00:33:14 +00:00
nsresult
imgRequest : : LockImage ( )
{
2010-05-14 20:47:59 +00:00
return mImage - > LockImage ( ) ;
2009-09-16 00:33:14 +00:00
}
nsresult
imgRequest : : UnlockImage ( )
{
2010-05-14 20:47:59 +00:00
return mImage - > UnlockImage ( ) ;
2009-09-16 00:33:14 +00:00
}
nsresult
imgRequest : : RequestDecode ( )
{
2010-05-14 20:47:59 +00:00
// If we've initialized our image, we can request a decode.
2010-08-23 22:44:07 +00:00
if ( mImage ) {
2009-09-16 00:33:14 +00:00
return mImage - > RequestDecode ( ) ;
}
// Otherwise, flag to do it when we get the image
2011-10-17 14:59:28 +00:00
mDecodeRequested = true ;
2009-09-16 00:33:14 +00:00
return NS_OK ;
}
2012-10-04 20:02:15 +00:00
nsresult
imgRequest : : StartDecoding ( )
{
// If we've initialized our image, we can request a decode.
if ( mImage ) {
return mImage - > StartDecoding ( ) ;
}
// Otherwise, flag to do it when we get the image
mDecodeRequested = true ;
return NS_OK ;
}
2001-04-10 06:01:08 +00:00
/** nsIRequestObserver methods **/
2001-01-22 22:01:03 +00:00
2001-02-22 08:41:20 +00:00
/* void onStartRequest (in nsIRequest request, in nsISupports ctxt); */
NS_IMETHODIMP imgRequest : : OnStartRequest ( nsIRequest * aRequest , nsISupports * ctxt )
2001-01-22 22:01:03 +00:00
{
2012-10-29 23:32:10 +00:00
LOG_SCOPE ( GetImgLog ( ) , " imgRequest::OnStartRequest " ) ;
2001-02-22 02:06:24 +00:00
2009-09-12 22:44:18 +00:00
// Figure out if we're multipart
2006-02-28 23:43:35 +00:00
nsCOMPtr < nsIMultiPartChannel > mpchan ( do_QueryInterface ( aRequest ) ) ;
if ( mpchan )
2011-10-17 14:59:28 +00:00
mIsMultiPartChannel = true ;
2002-02-27 00:32:19 +00:00
2009-09-12 22:44:18 +00:00
// If we're not multipart, we shouldn't have an image yet
2010-08-23 22:44:07 +00:00
NS_ABORT_IF_FALSE ( mIsMultiPartChannel | | ! mImage ,
2009-09-12 22:44:18 +00:00
" Already have an image for non-multipart request " ) ;
2012-09-06 22:05:23 +00:00
// If we're multipart and about to load another image, signal so we can
// detect the mime type in OnDataAvailable.
2010-09-08 20:40:38 +00:00
if ( mIsMultiPartChannel & & mImage ) {
2012-09-06 22:05:23 +00:00
mResniffMimeType = true ;
if ( mImage - > GetType ( ) = = imgIContainer : : TYPE_RASTER ) {
// Tell the RasterImage to reinitialize itself. We have to do this in
// OnStartRequest so that its state machine is always in a consistent
// state.
// Note that if our MIME type changes, mImage will be replaced with a
// new object.
static_cast < RasterImage * > ( mImage . get ( ) ) - > NewSourceData ( ) ;
}
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 ) {
NS_ASSERTION ( mpchan ,
" We should have an mRequest here unless we're multipart " ) ;
nsCOMPtr < nsIChannel > chan ;
mpchan - > GetBaseChannel ( getter_AddRefs ( chan ) ) ;
mRequest = chan ;
}
2012-10-12 16:11:21 +00:00
GetStatusTracker ( ) . OnStartRequest ( ) ;
2001-03-14 00:39:48 +00:00
2008-09-01 20:53:59 +00:00
nsCOMPtr < nsIChannel > channel ( do_QueryInterface ( aRequest ) ) ;
if ( channel )
channel - > GetSecurityInfo ( getter_AddRefs ( mSecurityInfo ) ) ;
2007-11-09 02:55:41 +00:00
/* Get our principal */
2001-02-22 08:41:20 +00:00
nsCOMPtr < nsIChannel > chan ( do_QueryInterface ( aRequest ) ) ;
2007-11-09 02:55:41 +00:00
if ( chan ) {
nsCOMPtr < nsIScriptSecurityManager > secMan =
do_GetService ( " @mozilla.org/scriptsecuritymanager;1 " ) ;
if ( secMan ) {
nsresult rv = secMan - > GetChannelPrincipal ( chan ,
getter_AddRefs ( mPrincipal ) ) ;
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
// Shouldn't we be dead already if this gets hit? Probably multipart/x-mixed-replace...
2012-10-12 16:11:20 +00:00
if ( GetStatusTracker ( ) . ConsumerCount ( ) = = 0 ) {
2002-03-10 22:29:14 +00:00
this - > Cancel ( NS_IMAGELIB_ERROR_FAILURE ) ;
}
2001-01-22 22:01:03 +00:00
return NS_OK ;
}
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
/* void onStopRequest (in nsIRequest request, in nsISupports ctxt, in nsresult status); */
2001-04-10 06:01:08 +00:00
NS_IMETHODIMP imgRequest : : OnStopRequest ( nsIRequest * aRequest , nsISupports * ctxt , nsresult status )
2001-01-22 22:01:03 +00:00
{
2012-10-29 23:32:10 +00:00
LOG_FUNC ( GetImgLog ( ) , " imgRequest::OnStopRequest " ) ;
2001-02-20 22:43:56 +00:00
2011-09-29 06:19:26 +00:00
bool lastPart = true ;
2006-06-01 18:35:21 +00:00
nsCOMPtr < nsIMultiPartChannel > mpchan ( do_QueryInterface ( aRequest ) ) ;
2010-05-11 03:27:41 +00:00
if ( mpchan )
mpchan - > GetIsLastPart ( & lastPart ) ;
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
}
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.
2010-09-08 20:40:38 +00:00
if ( mImage ) {
nsresult rv ;
if ( mImage - > GetType ( ) = = imgIContainer : : TYPE_RASTER ) {
// Notify the image
rv = static_cast < RasterImage * > ( mImage . get ( ) ) - > SourceDataComplete ( ) ;
} else { // imageType == imgIContainer::TYPE_VECTOR
nsCOMPtr < nsIStreamListener > imageAsStream = do_QueryInterface ( mImage ) ;
NS_ABORT_IF_FALSE ( imageAsStream ,
" SVG-typed Image failed QI to nsIStreamListener " ) ;
rv = imageAsStream - > OnStopRequest ( aRequest , ctxt , status ) ;
}
2009-09-12 22:44:18 +00:00
2010-09-08 20:40:38 +00:00
// If we got an error in the SourceDataComplete() / OnStopRequest() 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.
2009-09-12 22:44:18 +00:00
if ( NS_FAILED ( rv ) & & NS_SUCCEEDED ( status ) )
status = rv ;
2001-02-25 08:28:36 +00:00
}
2002-03-23 13:21:33 +00:00
2010-08-23 22:44:07 +00:00
imgStatusTracker & statusTracker = GetStatusTracker ( ) ;
statusTracker . RecordStopRequest ( lastPart , status ) ;
2009-09-12 22:44:18 +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.
2010-08-23 22:44:07 +00:00
if ( mImage & & NS_SUCCEEDED ( status ) ) {
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 ( ) ;
2001-04-09 00:45:06 +00:00
}
2010-05-14 20:47:59 +00:00
else {
// stops animations, removes from cache
this - > Cancel ( status ) ;
2009-09-12 22:44:18 +00:00
}
2012-10-12 16:11:21 +00:00
GetStatusTracker ( ) . OnStopRequest ( lastPart , status ) ;
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
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
{
imgRequest * request ;
nsACString * newType ;
} ;
2009-09-12 22:44:18 +00:00
/* prototype for these defined below */
2001-03-15 23:33:52 +00:00
static NS_METHOD sniff_mimetype_callback ( nsIInputStream * in , void * closure , const char * fromRawSegment ,
2012-08-22 15:56:38 +00:00
uint32_t toOffset , uint32_t count , uint32_t * writeCount ) ;
2001-03-14 00:39:48 +00:00
2001-01-22 22:01:03 +00:00
/** nsIStreamListener methods **/
2012-09-06 02:41:02 +00:00
/* 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 )
2001-01-22 22:01:03 +00:00
{
2012-10-29 23:32:10 +00:00
LOG_SCOPE_WITH_PARAM ( GetImgLog ( ) , " imgRequest::OnDataAvailable " , " count " , count ) ;
2001-02-25 08:28:36 +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
NS_ASSERTION ( aRequest , " imgRequest::OnDataAvailable -- no request! " ) ;
2001-03-14 00:39:48 +00:00
2009-09-12 22:44:18 +00:00
nsresult rv ;
2009-02-16 14:11:30 +00:00
2012-09-06 22:05:23 +00:00
if ( ! mGotData | | mResniffMimeType ) {
2012-10-29 23:32:10 +00:00
LOG_SCOPE ( GetImgLog ( ) , " imgRequest::OnDataAvailable |First time through... finding mimetype| " ) ;
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
2011-10-17 14:59:28 +00:00
mGotData = true ;
2010-05-14 20:47:59 +00:00
2012-09-06 22:05:23 +00:00
mimetype_closure closure ;
nsAutoCString newType ;
closure . request = this ;
closure . newType = & newType ;
2001-03-14 00:39:48 +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 . : (
*/
2012-08-22 15:56:38 +00:00
uint32_t out ;
2012-09-06 22:05:23 +00:00
inStr - > ReadSegments ( sniff_mimetype_callback , & closure , count , & out ) ;
2001-03-14 00:39:48 +00:00
2012-06-25 19:59:42 +00:00
# ifdef DEBUG
2001-03-14 00:39:48 +00:00
/* NS_WARNING if the content type from the channel isn't the same if the sniffing */
# endif
2011-09-09 22:41:04 +00:00
nsCOMPtr < nsIChannel > chan ( do_QueryInterface ( aRequest ) ) ;
2012-09-06 22:05:23 +00:00
if ( newType . IsEmpty ( ) ) {
2012-10-29 23:32:10 +00:00
LOG_SCOPE ( GetImgLog ( ) , " imgRequest::OnDataAvailable |sniffing of mimetype failed| " ) ;
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
2009-09-12 22:44:18 +00:00
rv = NS_ERROR_FAILURE ;
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
if ( chan ) {
2012-09-06 22:05:23 +00:00
rv = chan - > GetContentType ( newType ) ;
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-03-14 00:39:48 +00:00
if ( NS_FAILED ( rv ) ) {
2012-10-29 23:32:10 +00:00
PR_LOG ( GetImgLog ( ) , PR_LOG_ERROR ,
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
( " [this=%p] imgRequest::OnDataAvailable -- Content type unavailable from the channel \n " ,
2001-03-14 00:39:48 +00:00
this ) ) ;
2001-08-21 20:17:22 +00:00
this - > Cancel ( NS_IMAGELIB_ERROR_FAILURE ) ;
2001-03-14 00:39:48 +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
return NS_BINDING_ABORTED ;
2001-03-14 00:39:48 +00:00
}
2012-10-29 23:32:10 +00:00
LOG_MSG ( GetImgLog ( ) , " imgRequest::OnDataAvailable " , " Got content type from the channel " ) ;
2001-03-14 00:39:48 +00:00
}
2012-09-06 22:05:23 +00:00
// If we're a regular image and this is the first call to OnDataAvailable,
// this will always be true. If we've resniffed our MIME type (i.e. we're a
// multipart/x-mixed-replace image), we have to be able to switch our image
// type and decoder.
// We always reinitialize for SVGs, because they have no way of
// reinitializing themselves.
if ( mContentType ! = newType | | newType . EqualsLiteral ( SVG_MIMETYPE ) ) {
mContentType = newType ;
// If we've resniffed our MIME type and it changed, we need to create a
// new status tracker to give to the image, because we don't have one of
// our own any more.
if ( mResniffMimeType ) {
NS_ABORT_IF_FALSE ( mIsMultiPartChannel , " Resniffing a non-multipart image " ) ;
2012-10-12 16:11:20 +00:00
imgStatusTracker * freshTracker = new imgStatusTracker ( nullptr , this ) ;
2012-10-12 16:11:21 +00:00
freshTracker - > AdoptConsumers ( & GetStatusTracker ( ) ) ;
2012-10-12 16:11:20 +00:00
mStatusTracker = freshTracker ;
2012-09-06 22:05:23 +00:00
}
2010-08-23 22:44:07 +00:00
2012-09-06 22:05:23 +00:00
mResniffMimeType = false ;
2010-08-14 04:09:49 +00:00
2012-09-06 22:05:23 +00:00
/* now we have mimetype, so we can infer the image type that we want */
if ( mContentType . EqualsLiteral ( SVG_MIMETYPE ) ) {
mImage = new VectorImage ( mStatusTracker . forget ( ) ) ;
} else {
mImage = new RasterImage ( mStatusTracker . forget ( ) ) ;
}
mImage - > SetInnerWindowID ( mInnerWindowId ) ;
2005-03-23 21:09:02 +00:00
2012-10-12 16:11:21 +00:00
GetStatusTracker ( ) . OnDataAvailable ( ) ;
2005-03-23 21:09:02 +00:00
2012-09-06 22:05:23 +00:00
/* 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 ) ;
}
2009-09-16 00:33:14 +00:00
2012-09-06 22:05:23 +00:00
/* set our content disposition as a property */
nsAutoCString disposition ;
if ( chan ) {
chan - > 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 ) ;
}
}
2012-10-29 23:32:10 +00:00
LOG_MSG_WITH_PARAM ( GetImgLog ( ) , " imgRequest::OnDataAvailable " , " content type " , mContentType . get ( ) ) ;
2012-09-06 22:05:23 +00:00
//
// Figure out our Image initialization flags
//
// We default to the static globals
bool isDiscardable = gDiscardable ;
bool doDecodeOnDraw = gDecodeOnDraw ;
// We want UI to be as snappy as possible and not to flicker. Disable discarding
// and decode-on-draw for chrome URLS
bool isChrome = false ;
rv = mURI - > SchemeIs ( " chrome " , & isChrome ) ;
if ( NS_SUCCEEDED ( rv ) & & isChrome )
isDiscardable = doDecodeOnDraw = false ;
// We don't want resources like the "loading" icon to be discardable or
// decode-on-draw either.
bool isResource = false ;
rv = mURI - > SchemeIs ( " resource " , & isResource ) ;
if ( NS_SUCCEEDED ( rv ) & & isResource )
isDiscardable = doDecodeOnDraw = false ;
// For multipart/x-mixed-replace, we basically want a direct channel to the
// decoder. Disable both for this case as well.
if ( mIsMultiPartChannel )
isDiscardable = doDecodeOnDraw = false ;
// We have all the information we need
uint32_t imageFlags = Image : : INIT_FLAG_NONE ;
if ( isDiscardable )
imageFlags | = Image : : INIT_FLAG_DISCARDABLE ;
if ( doDecodeOnDraw )
imageFlags | = Image : : INIT_FLAG_DECODE_ON_DRAW ;
if ( mIsMultiPartChannel )
imageFlags | = Image : : INIT_FLAG_MULTIPART ;
// Get our URI string
nsAutoCString uriString ;
rv = mURI - > GetSpec ( uriString ) ;
if ( NS_FAILED ( rv ) )
uriString . Assign ( " <unknown image URI> " ) ;
// Initialize the image that we created above. For RasterImages, this
// instantiates a decoder behind the scenes, so if we don't have a decoder
// for this mimetype we'll find out about it here.
2012-10-12 16:11:21 +00:00
rv = mImage - > Init ( GetStatusTracker ( ) . GetDecoderObserver ( ) ,
mContentType . get ( ) , uriString . get ( ) , imageFlags ) ;
2012-09-11 18:03:52 +00:00
// We allow multipart images to fail to initialize without cancelling the
// load because subsequent images might be fine.
if ( NS_FAILED ( rv ) & & ! mIsMultiPartChannel ) { // Probably bad mimetype
2012-09-06 22:05:23 +00:00
this - > Cancel ( rv ) ;
return NS_BINDING_ABORTED ;
}
if ( mImage - > GetType ( ) = = imgIContainer : : TYPE_RASTER ) {
/* Use content-length as a size hint for http channels. */
nsCOMPtr < nsIHttpChannel > httpChannel ( do_QueryInterface ( aRequest ) ) ;
if ( httpChannel ) {
nsAutoCString contentLength ;
rv = httpChannel - > GetResponseHeader ( NS_LITERAL_CSTRING ( " content-length " ) ,
contentLength ) ;
if ( NS_SUCCEEDED ( rv ) ) {
int32_t len = contentLength . ToInteger ( & rv ) ;
// Pass anything usable on so that the RasterImage can preallocate
// its source buffer
if ( len > 0 ) {
uint32_t sizeHint = ( uint32_t ) len ;
sizeHint = NS_MIN < uint32_t > ( sizeHint , 20000000 ) ; /* Bound by something reasonable */
RasterImage * rasterImage = static_cast < RasterImage * > ( mImage . get ( ) ) ;
rv = rasterImage - > SetSourceSizeHint ( sizeHint ) ;
if ( NS_FAILED ( rv ) ) {
// Flush memory, try to get some back, and try again
rv = nsMemory : : HeapMinimize ( true ) ;
nsresult rv2 = rasterImage - > SetSourceSizeHint ( sizeHint ) ;
// If we've still failed at this point, things are going downhill
if ( NS_FAILED ( rv ) | | NS_FAILED ( rv2 ) ) {
NS_WARNING ( " About to hit OOM in imagelib! " ) ;
}
2011-06-09 02:21:53 +00:00
}
}
2010-08-14 04:09:49 +00:00
}
2010-06-25 18:21:40 +00:00
}
}
2012-09-06 22:05:23 +00:00
if ( mImage - > GetType ( ) = = imgIContainer : : TYPE_RASTER ) {
// If we were waiting on the image to do something, now's our chance.
if ( mDecodeRequested ) {
2012-10-04 20:02:15 +00:00
mImage - > StartDecoding ( ) ;
2012-09-06 22:05:23 +00:00
}
} else { // mImage->GetType() == imgIContainer::TYPE_VECTOR
nsCOMPtr < nsIStreamListener > imageAsStream = do_QueryInterface ( mImage ) ;
NS_ABORT_IF_FALSE ( imageAsStream ,
" SVG-typed Image failed QI to nsIStreamListener " ) ;
imageAsStream - > OnStartRequest ( aRequest , nullptr ) ;
2010-09-08 20:40:38 +00:00
}
2009-09-16 00:33:14 +00:00
}
2001-03-14 00:39:48 +00:00
}
2012-09-06 22:05:23 +00:00
if ( mImage - > GetType ( ) = = imgIContainer : : TYPE_RASTER ) {
2010-09-08 20:40:38 +00:00
// WriteToRasterImage always consumes everything it gets
2011-06-09 02:21:53 +00:00
// if it doesn't run out of memory
2012-08-22 15:56:38 +00:00
uint32_t bytesRead ;
2010-09-08 20:40:38 +00:00
rv = inStr - > ReadSegments ( RasterImage : : WriteToRasterImage ,
static_cast < void * > ( mImage ) ,
count , & bytesRead ) ;
2011-06-09 02:21:53 +00:00
NS_ABORT_IF_FALSE ( bytesRead = = count | | mImage - > HasError ( ) ,
" WriteToRasterImage should consume everything or the image must be in error! " ) ;
2012-09-06 22:05:23 +00:00
} else { // mImage->GetType() == imgIContainer::TYPE_VECTOR
2010-09-08 20:40:38 +00:00
nsCOMPtr < nsIStreamListener > imageAsStream = do_QueryInterface ( mImage ) ;
rv = imageAsStream - > OnDataAvailable ( aRequest , ctxt , inStr ,
sourceOffset , count ) ;
}
2001-04-16 09:17:59 +00:00
if ( NS_FAILED ( rv ) ) {
2012-10-29 23:32:10 +00:00
PR_LOG ( GetImgLog ( ) , PR_LOG_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 ) ) ;
2001-08-21 20:17:22 +00:00
this - > 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
static NS_METHOD sniff_mimetype_callback ( nsIInputStream * in ,
2012-09-06 22:05:23 +00:00
void * data ,
2001-03-14 00:39:48 +00:00
const char * fromRawSegment ,
2012-08-22 15:56:38 +00:00
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
if ( count > 0 )
2012-09-06 22:05:23 +00:00
closure - > request - > SniffMimeType ( fromRawSegment , count , * closure - > newType ) ;
2001-03-14 00:39:48 +00:00
* writeCount = 0 ;
return NS_ERROR_FAILURE ;
}
void
2012-09-06 22:05:23 +00:00
imgRequest : : SniffMimeType ( const char * buf , uint32_t len , nsACString & newType )
2001-03-14 00:39:48 +00:00
{
2012-09-06 22:05:23 +00:00
imgLoader : : GetMimeTypeFromContent ( buf , len , newType ) ;
2007-09-02 16:10:57 +00:00
// The vast majority of the time, imgLoader will find a gif/jpeg/png image
2012-09-06 22:05:23 +00:00
// and fill newType with the sniffed MIME type.
if ( ! newType . IsEmpty ( ) )
2007-09-02 16:10:57 +00:00
return ;
// When our sniffing fails, we want to query registered image decoders
// to see if they can identify the image. If we always trusted the server
// to send the right MIME, images sent as text/plain would not be rendered.
const nsCOMArray < nsIContentSniffer > & sniffers = mImageSniffers . GetEntries ( ) ;
2012-08-22 15:56:38 +00:00
uint32_t length = sniffers . Count ( ) ;
for ( uint32_t i = 0 ; i < length ; + + i ) {
2007-09-02 16:10:57 +00:00
nsresult rv =
2012-09-06 22:05:23 +00:00
sniffers [ i ] - > GetMIMETypeFromContent ( nullptr , ( const uint8_t * ) buf , len , newType ) ;
if ( NS_SUCCEEDED ( rv ) & & ! newType . IsEmpty ( ) ) {
2007-09-02 16:10:57 +00:00
return ;
}
}
2001-03-15 23:33:52 +00:00
}
2006-11-09 22:47:43 +00:00
2009-09-12 22:44:18 +00:00
2008-12-22 22:20:46 +00:00
/** 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
2010-08-05 02:15:55 +00:00
imgRequest : : AsyncOnChannelRedirect ( nsIChannel * oldChannel ,
2012-08-22 15:56:38 +00:00
nsIChannel * newChannel , uint32_t flags ,
2010-08-05 02:15:55 +00:00
nsIAsyncVerifyRedirectCallback * callback )
2008-12-22 22:20:46 +00:00
{
2010-08-05 02:15:55 +00:00
NS_ASSERTION ( mRequest & & mChannel , " Got a channel redirect after we nulled out mRequest! " ) ;
2008-12-22 22:20:46 +00:00
NS_ASSERTION ( mChannel = = oldChannel , " Got a channel redirect for an unknown channel! " ) ;
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
2011-07-01 17:03:32 +00:00
# if defined(PR_LOGGING)
2012-09-02 02:35:17 +00:00
nsAutoCString oldspec ;
2011-07-01 17:03:40 +00:00
if ( mCurrentURI )
mCurrentURI - > GetSpec ( oldspec ) ;
2012-10-29 23:32:10 +00:00
LOG_MSG_WITH_PARAM ( GetImgLog ( ) , " imgRequest::OnChannelRedirect " , " old " , oldspec . get ( ) ) ;
2011-07-01 17:03:32 +00:00
# endif
2009-03-17 21:07:16 +00:00
2010-02-11 21:59:00 +00:00
// make sure we have a protocol that returns data rather than opens
// an external application, e.g. mailto:
2011-07-01 17:03:40 +00:00
mChannel - > GetURI ( getter_AddRefs ( mCurrentURI ) ) ;
2011-09-29 06:19:26 +00:00
bool doesNotReturnData = false ;
2010-08-05 02:15:55 +00:00
nsresult rv =
2011-07-01 17:03:40 +00:00
NS_URIChainHasFlags ( mCurrentURI , nsIProtocolHandler : : URI_DOES_NOT_RETURN_DATA ,
2010-08-05 02:15:55 +00:00
& doesNotReturnData ) ;
if ( NS_SUCCEEDED ( rv ) & & doesNotReturnData )
rv = NS_ERROR_ABORT ;
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
}