2003-03-19 01:08:22 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2004-05-13 18:34:18 +00:00
|
|
|
// vim: ft=cpp tw=78 sw=2 et ts=2
|
2003-03-19 01:08:22 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Boris Zbarsky <bzbarsky@mit.edu>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2003
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsImageLoadingContent.h"
|
|
|
|
#include "nsContentErrors.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsINodeInfo.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2004-11-24 22:48:45 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2003-03-19 01:08:22 +00:00
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsILoadGroup.h"
|
|
|
|
#include "imgIContainer.h"
|
|
|
|
#include "gfxIImageFrame.h"
|
|
|
|
#include "imgILoader.h"
|
|
|
|
#include "plevent.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIEventQueue.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
2004-07-31 23:15:21 +00:00
|
|
|
#include "nsPresContext.h"
|
2003-03-19 05:38:33 +00:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsGUIEvent.h"
|
2003-05-09 20:42:49 +00:00
|
|
|
#include "nsDummyLayoutRequest.h"
|
2003-03-19 01:08:22 +00:00
|
|
|
|
2003-04-02 03:27:20 +00:00
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
|
2003-04-16 01:53:48 +00:00
|
|
|
#include "nsLayoutAtoms.h"
|
|
|
|
#include "nsIFrame.h"
|
2004-05-13 18:34:18 +00:00
|
|
|
#include "nsIDOMNode.h"
|
2003-04-16 01:53:48 +00:00
|
|
|
|
2004-03-09 03:57:51 +00:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
2004-06-05 17:57:00 +00:00
|
|
|
#ifdef DEBUG_chb
|
|
|
|
static void PrintReqURL(imgIRequest* req) {
|
|
|
|
if (!req) {
|
|
|
|
printf("(null req)\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
req->GetURI(getter_AddRefs(uri));
|
|
|
|
if (!uri) {
|
|
|
|
printf("(null uri)\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString spec;
|
|
|
|
uri->GetSpec(spec);
|
|
|
|
printf("spec='%s'\n", spec.get());
|
|
|
|
}
|
|
|
|
#endif /* DEBUG_chb */
|
|
|
|
|
2004-05-13 18:34:18 +00:00
|
|
|
|
2003-03-19 01:08:22 +00:00
|
|
|
nsImageLoadingContent::nsImageLoadingContent()
|
|
|
|
: mObserverList(nsnull),
|
|
|
|
mLoadingEnabled(PR_TRUE),
|
2003-11-16 07:34:25 +00:00
|
|
|
mImageIsBlocked(PR_FALSE),
|
|
|
|
mHaveHadObserver(PR_FALSE)
|
2003-03-19 01:08:22 +00:00
|
|
|
{
|
2005-02-03 17:23:35 +00:00
|
|
|
if (!nsContentUtils::GetImgLoader())
|
2003-05-02 19:21:00 +00:00
|
|
|
mLoadingEnabled = PR_FALSE;
|
2003-03-19 01:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsImageLoadingContent::~nsImageLoadingContent()
|
|
|
|
{
|
|
|
|
// Cancel our requests so they won't hold stale refs to us
|
|
|
|
if (mCurrentRequest) {
|
|
|
|
mCurrentRequest->Cancel(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
if (mPendingRequest) {
|
|
|
|
mPendingRequest->Cancel(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
NS_ASSERTION(!mObserverList.mObserver && !mObserverList.mNext,
|
|
|
|
"Observers still registered?");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Macro to call some func on each observer. This handles observers
|
|
|
|
// removing themselves.
|
|
|
|
#define LOOP_OVER_OBSERVERS(func_) \
|
|
|
|
PR_BEGIN_MACRO \
|
2003-07-23 23:09:33 +00:00
|
|
|
for (ImageObserver* observer = &mObserverList, *next; observer; \
|
|
|
|
observer = next) { \
|
|
|
|
next = observer->mNext; \
|
2003-03-19 01:08:22 +00:00
|
|
|
if (observer->mObserver) { \
|
|
|
|
observer->mObserver->func_; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* imgIContainerObserver impl
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::FrameChanged(imgIContainer* aContainer,
|
|
|
|
gfxIImageFrame* aFrame,
|
|
|
|
nsRect* aDirtyRect)
|
|
|
|
{
|
|
|
|
LOOP_OVER_OBSERVERS(FrameChanged(aContainer, aFrame, aDirtyRect));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* imgIDecoderObserver impl
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::OnStartDecode(imgIRequest* aRequest)
|
|
|
|
{
|
|
|
|
LOOP_OVER_OBSERVERS(OnStartDecode(aRequest));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::OnStartContainer(imgIRequest* aRequest,
|
|
|
|
imgIContainer* aContainer)
|
|
|
|
{
|
|
|
|
LOOP_OVER_OBSERVERS(OnStartContainer(aRequest, aContainer));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::OnStartFrame(imgIRequest* aRequest,
|
|
|
|
gfxIImageFrame* aFrame)
|
|
|
|
{
|
|
|
|
LOOP_OVER_OBSERVERS(OnStartFrame(aRequest, aFrame));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::OnDataAvailable(imgIRequest* aRequest,
|
|
|
|
gfxIImageFrame* aFrame,
|
|
|
|
const nsRect* aRect)
|
|
|
|
{
|
|
|
|
LOOP_OVER_OBSERVERS(OnDataAvailable(aRequest, aFrame, aRect));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::OnStopFrame(imgIRequest* aRequest,
|
|
|
|
gfxIImageFrame* aFrame)
|
|
|
|
{
|
|
|
|
LOOP_OVER_OBSERVERS(OnStopFrame(aRequest, aFrame));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::OnStopContainer(imgIRequest* aRequest,
|
|
|
|
imgIContainer* aContainer)
|
|
|
|
{
|
|
|
|
LOOP_OVER_OBSERVERS(OnStopContainer(aRequest, aContainer));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::OnStopDecode(imgIRequest* aRequest,
|
|
|
|
nsresult aStatus,
|
|
|
|
const PRUnichar* aStatusArg)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aRequest == mCurrentRequest || aRequest == mPendingRequest,
|
|
|
|
"Unknown request");
|
|
|
|
LOOP_OVER_OBSERVERS(OnStopDecode(aRequest, aStatus, aStatusArg));
|
|
|
|
|
|
|
|
if (aRequest == mPendingRequest) {
|
|
|
|
mCurrentRequest->Cancel(NS_ERROR_IMAGE_SRC_CHANGED);
|
|
|
|
mCurrentRequest = mPendingRequest;
|
|
|
|
mPendingRequest = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
FireEvent(NS_LITERAL_STRING("load"));
|
|
|
|
} else {
|
|
|
|
FireEvent(NS_LITERAL_STRING("error"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-04-02 03:27:20 +00:00
|
|
|
* nsIImageLoadingContent impl
|
2003-03-19 01:08:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::GetLoadingEnabled(PRBool *aLoadingEnabled)
|
|
|
|
{
|
|
|
|
*aLoadingEnabled = mLoadingEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::SetLoadingEnabled(PRBool aLoadingEnabled)
|
|
|
|
{
|
2005-02-03 17:23:35 +00:00
|
|
|
if (nsContentUtils::GetImgLoader())
|
2003-05-02 19:21:00 +00:00
|
|
|
mLoadingEnabled = aLoadingEnabled;
|
2003-03-19 01:08:22 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::GetImageBlocked(PRBool* aBlocked)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aBlocked, "Null out param");
|
|
|
|
*aBlocked = mImageIsBlocked;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-07-24 17:30:52 +00:00
|
|
|
|
2003-03-19 01:08:22 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::AddObserver(imgIDecoderObserver* aObserver)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aObserver);
|
|
|
|
|
2003-11-16 07:34:25 +00:00
|
|
|
mHaveHadObserver = PR_TRUE;
|
|
|
|
|
2003-03-19 01:08:22 +00:00
|
|
|
if (!mObserverList.mObserver) {
|
|
|
|
mObserverList.mObserver = aObserver;
|
|
|
|
// Don't touch the linking of the list!
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise we have to create a new entry
|
|
|
|
|
|
|
|
ImageObserver* observer = &mObserverList;
|
|
|
|
while (observer->mNext) {
|
|
|
|
observer = observer->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
observer->mNext = new ImageObserver(aObserver);
|
|
|
|
if (! observer->mNext) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::RemoveObserver(imgIDecoderObserver* aObserver)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aObserver);
|
|
|
|
|
|
|
|
if (mObserverList.mObserver == aObserver) {
|
|
|
|
mObserverList.mObserver = nsnull;
|
|
|
|
// Don't touch the linking of the list!
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise have to find it and splice it out
|
|
|
|
ImageObserver* observer = &mObserverList;
|
|
|
|
while (observer->mNext && observer->mNext->mObserver != aObserver) {
|
|
|
|
observer = observer->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we are pointing to the list element whose mNext is
|
|
|
|
// the right observer (assuming of course that mNext is not null)
|
|
|
|
if (observer->mNext) {
|
|
|
|
// splice it out
|
|
|
|
ImageObserver* oldObserver = observer->mNext;
|
|
|
|
observer->mNext = oldObserver->mNext;
|
|
|
|
oldObserver->mNext = nsnull; // so we don't destroy them all
|
|
|
|
delete oldObserver;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
else {
|
|
|
|
NS_WARNING("Asked to remove non-existent observer");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-03-19 04:13:37 +00:00
|
|
|
nsImageLoadingContent::GetRequest(PRInt32 aRequestType,
|
2003-03-19 01:08:22 +00:00
|
|
|
imgIRequest** aRequest)
|
|
|
|
{
|
|
|
|
switch(aRequestType) {
|
|
|
|
case CURRENT_REQUEST:
|
|
|
|
*aRequest = mCurrentRequest;
|
|
|
|
break;
|
|
|
|
case PENDING_REQUEST:
|
|
|
|
*aRequest = mPendingRequest;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unknown request type");
|
|
|
|
*aRequest = nsnull;
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aRequest);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::GetRequestType(imgIRequest* aRequest,
|
|
|
|
PRInt32* aRequestType)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aRequestType, "Null out param");
|
|
|
|
|
|
|
|
if (aRequest == mCurrentRequest) {
|
|
|
|
*aRequestType = CURRENT_REQUEST;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRequest == mPendingRequest) {
|
|
|
|
*aRequestType = PENDING_REQUEST;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aRequestType = UNKNOWN_REQUEST;
|
|
|
|
NS_ERROR("Unknown request");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2004-06-05 17:57:00 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::GetCurrentURI(nsIURI** aURI)
|
|
|
|
{
|
|
|
|
if (mCurrentRequest)
|
|
|
|
return mCurrentRequest->GetURI(aURI);
|
|
|
|
|
2004-09-14 15:06:56 +00:00
|
|
|
NS_IF_ADDREF(*aURI = mCurrentURI);
|
2004-06-05 17:57:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-04-02 03:27:20 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::LoadImageWithChannel(nsIChannel* aChannel,
|
|
|
|
nsIStreamListener** aListener)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aListener, "null out param");
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aChannel);
|
|
|
|
|
2005-02-03 17:23:35 +00:00
|
|
|
if (!nsContentUtils::GetImgLoader())
|
2003-05-02 19:21:00 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2003-04-02 03:27:20 +00:00
|
|
|
// XXX what should we do with content policies here, if anything?
|
|
|
|
// Shouldn't that be done before the start of the load?
|
|
|
|
|
2003-07-28 21:09:56 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetOurDocument();
|
|
|
|
if (!doc) {
|
|
|
|
// Don't bother
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-05-13 18:34:18 +00:00
|
|
|
CancelImageRequests(NS_ERROR_IMAGE_SRC_CHANGED, PR_FALSE);
|
2003-04-02 03:27:20 +00:00
|
|
|
|
|
|
|
nsCOMPtr<imgIRequest> & req = mCurrentRequest ? mPendingRequest : mCurrentRequest;
|
|
|
|
|
2005-02-03 17:23:35 +00:00
|
|
|
return nsContentUtils::GetImgLoader()->LoadImageWithChannel(aChannel, this, doc, aListener, getter_AddRefs(req));
|
2003-04-02 03:27:20 +00:00
|
|
|
}
|
|
|
|
|
2003-03-19 01:08:22 +00:00
|
|
|
// XXX This should be a protected method, not an interface method!!!
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::ImageURIChanged(const nsAString& aNewURI) {
|
|
|
|
return ImageURIChanged(NS_ConvertUCS2toUTF8(aNewURI));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Non-interface methods
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
nsImageLoadingContent::ImageURIChanged(const nsACString& aNewURI)
|
|
|
|
{
|
|
|
|
if (!mLoadingEnabled) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-07-03 02:45:34 +00:00
|
|
|
// First, get a document (needed for security checks and the like)
|
2003-07-28 21:09:56 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetOurDocument();
|
2003-04-15 01:09:09 +00:00
|
|
|
if (!doc) {
|
|
|
|
// No reason to bother, I think...
|
2003-07-28 21:09:56 +00:00
|
|
|
return NS_OK;
|
2003-04-15 01:09:09 +00:00
|
|
|
}
|
2003-03-19 01:08:22 +00:00
|
|
|
|
2003-07-28 21:09:56 +00:00
|
|
|
nsresult rv; // XXXbz Should failures in this method fire onerror?
|
|
|
|
|
2003-03-19 01:08:22 +00:00
|
|
|
nsCOMPtr<nsIURI> imageURI;
|
|
|
|
rv = StringToURI(aNewURI, doc, getter_AddRefs(imageURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2004-06-05 17:57:00 +00:00
|
|
|
// Remember the URL of this request, in case someone asks us for it later
|
|
|
|
// But this only matters if we are affecting the current request
|
|
|
|
if (!mCurrentRequest)
|
|
|
|
mCurrentURI = imageURI;
|
2003-03-19 01:08:22 +00:00
|
|
|
|
|
|
|
// If we'll be loading a new image, we want to cancel our existing
|
|
|
|
// requests; the question is what reason to pass in. If everything
|
|
|
|
// is going smoothly, that reason should be
|
|
|
|
// NS_ERROR_IMAGE_SRC_CHANGED so that our frame (if any) will know
|
|
|
|
// not to show the broken image icon. If the load is blocked by the
|
|
|
|
// content policy or security manager, we will want to cancel with
|
|
|
|
// the error code from those.
|
|
|
|
|
2004-05-13 18:34:18 +00:00
|
|
|
// TODOtw: figure out whether we should show alternate text
|
|
|
|
// (CanLoadImage can tell us this, since Content Policy tells it)
|
|
|
|
PRBool loadImage = nsContentUtils::CanLoadImage(imageURI, this, doc);
|
|
|
|
|
|
|
|
nsresult cancelResult = loadImage ? NS_ERROR_IMAGE_SRC_CHANGED
|
|
|
|
: NS_ERROR_IMAGE_BLOCKED;
|
|
|
|
|
|
|
|
mImageIsBlocked = !loadImage;
|
|
|
|
|
|
|
|
CancelImageRequests(cancelResult, PR_FALSE);
|
2003-03-19 01:08:22 +00:00
|
|
|
|
2004-05-13 18:34:18 +00:00
|
|
|
if (mImageIsBlocked) {
|
2003-03-19 01:08:22 +00:00
|
|
|
// Don't actually load anything! This was blocked by CanLoadImage.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<imgIRequest> & req = mCurrentRequest ? mPendingRequest : mCurrentRequest;
|
|
|
|
|
2003-04-16 01:53:48 +00:00
|
|
|
// It may be that one of our frames has replaced itself with alt text... This
|
|
|
|
// would only have happened if our mCurrentRequest had issues, and we would
|
|
|
|
// have set it to null by now in that case. Have to save that information
|
2003-11-16 07:34:25 +00:00
|
|
|
// here, since LoadImage may clobber the value of mCurrentRequest. On the
|
|
|
|
// other hand, if we've never had an observer, we know there aren't any frames
|
|
|
|
// that have changed to alt text on us yet.
|
|
|
|
PRBool mayNeedReframe = mHaveHadObserver && !mCurrentRequest;
|
2003-04-16 01:53:48 +00:00
|
|
|
|
2004-07-14 21:56:23 +00:00
|
|
|
rv = nsContentUtils::LoadImage(imageURI, doc, doc->GetDocumentURI(),
|
|
|
|
this, nsIRequest::LOAD_NORMAL,
|
2004-03-09 03:57:51 +00:00
|
|
|
getter_AddRefs(req));
|
2004-06-05 17:57:00 +00:00
|
|
|
// If we now have a current request, we don't need to store the URI, since
|
|
|
|
// we can get it off the request. Release it.
|
|
|
|
if (mCurrentRequest) {
|
|
|
|
mCurrentURI = nsnull;
|
|
|
|
}
|
2003-04-16 01:53:48 +00:00
|
|
|
|
|
|
|
if (!mayNeedReframe) {
|
|
|
|
// We're all set
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only continue if we have a parent and a document -- that would mean we're
|
|
|
|
// a useful chunk of the content model and _may_ have a frame. This should
|
|
|
|
// eliminate things like SetAttr calls during the parsing process, as well as
|
|
|
|
// things like setting src on |new Image()|-type things.
|
|
|
|
nsCOMPtr<nsIContent> thisContent = do_QueryInterface(this, &rv);
|
|
|
|
NS_ENSURE_TRUE(thisContent, rv);
|
|
|
|
|
2003-07-28 21:09:56 +00:00
|
|
|
if (!thisContent->GetDocument() || !thisContent->GetParent()) {
|
2003-04-16 01:53:48 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK, now for each PresShell, see whether we have a frame -- this tends to
|
|
|
|
// be expensive, which is why it's the last check.... If we have a frame
|
|
|
|
// and it's not of the right type, reframe it.
|
|
|
|
PRInt32 numShells = doc->GetNumberOfShells();
|
|
|
|
for (PRInt32 i = 0; i < numShells; ++i) {
|
2003-09-27 04:18:26 +00:00
|
|
|
nsIPresShell *shell = doc->GetShellAt(i);
|
2003-04-16 01:53:48 +00:00
|
|
|
if (shell) {
|
|
|
|
nsIFrame* frame = nsnull;
|
|
|
|
shell->GetPrimaryFrameFor(thisContent, &frame);
|
|
|
|
if (frame) {
|
|
|
|
// XXXbz I don't like this one bit... we really need a better way of
|
|
|
|
// doing the CantRenderReplacedElement stuff.. In particular, it needs
|
|
|
|
// to be easily detectable. For example, I suspect that this code will
|
|
|
|
// fail for <object> in the current CantRenderReplacedElement
|
|
|
|
// implementation...
|
2003-10-31 20:19:18 +00:00
|
|
|
nsIAtom* frameType = frame->GetType();
|
2003-04-16 01:53:48 +00:00
|
|
|
if (frameType != nsLayoutAtoms::imageFrame &&
|
|
|
|
frameType != nsLayoutAtoms::imageControlFrame &&
|
|
|
|
frameType != nsLayoutAtoms::objectFrame) {
|
|
|
|
shell->RecreateFramesFor(thisContent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2003-03-19 01:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-05-13 18:34:18 +00:00
|
|
|
nsImageLoadingContent::CancelImageRequests(nsresult aReason,
|
|
|
|
PRBool aEvenIfSizeAvailable)
|
2003-03-19 01:08:22 +00:00
|
|
|
{
|
|
|
|
// Cancel the pending request, if any
|
|
|
|
if (mPendingRequest) {
|
|
|
|
mPendingRequest->Cancel(aReason);
|
|
|
|
mPendingRequest = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel the current request if it has not progressed enough to
|
|
|
|
// have a size yet
|
|
|
|
if (mCurrentRequest) {
|
|
|
|
PRUint32 loadStatus = imgIRequest::STATUS_ERROR;
|
|
|
|
mCurrentRequest->GetImageStatus(&loadStatus);
|
|
|
|
|
2004-05-13 18:34:18 +00:00
|
|
|
if (aEvenIfSizeAvailable ||
|
|
|
|
!(loadStatus & imgIRequest::STATUS_SIZE_AVAILABLE)) {
|
2003-03-19 01:08:22 +00:00
|
|
|
mCurrentRequest->Cancel(aReason);
|
|
|
|
mCurrentRequest = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-28 21:09:56 +00:00
|
|
|
nsIDocument*
|
|
|
|
nsImageLoadingContent::GetOurDocument()
|
2003-03-19 01:08:22 +00:00
|
|
|
{
|
2003-07-28 21:09:56 +00:00
|
|
|
nsCOMPtr<nsIContent> thisContent = do_QueryInterface(this);
|
|
|
|
NS_ENSURE_TRUE(thisContent, nsnull);
|
2003-03-19 01:08:22 +00:00
|
|
|
|
2004-10-11 16:29:42 +00:00
|
|
|
return thisContent->GetOwnerDoc();
|
2003-03-19 01:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsImageLoadingContent::StringToURI(const nsACString& aSpec,
|
|
|
|
nsIDocument* aDocument,
|
|
|
|
nsIURI** aURI)
|
|
|
|
{
|
2003-04-15 01:09:09 +00:00
|
|
|
NS_PRECONDITION(aDocument, "Must have a document");
|
2003-03-19 01:08:22 +00:00
|
|
|
NS_PRECONDITION(aURI, "Null out param");
|
|
|
|
|
|
|
|
// (1) Get the base URI
|
2003-07-03 02:45:34 +00:00
|
|
|
nsCOMPtr<nsIContent> thisContent = do_QueryInterface(this);
|
|
|
|
NS_ASSERTION(thisContent, "An image loading content must be an nsIContent");
|
2004-01-09 23:54:21 +00:00
|
|
|
nsCOMPtr<nsIURI> baseURL = thisContent->GetBaseURI();
|
2003-03-19 01:08:22 +00:00
|
|
|
|
|
|
|
// (2) Get the charset
|
2003-10-22 06:09:48 +00:00
|
|
|
const nsACString &charset = aDocument->GetDocumentCharacterSet();
|
2003-03-19 01:08:22 +00:00
|
|
|
|
|
|
|
// (3) Construct the silly thing
|
|
|
|
return NS_NewURI(aURI,
|
|
|
|
aSpec,
|
2003-10-22 06:09:48 +00:00
|
|
|
charset.IsEmpty() ? nsnull : PromiseFlatCString(charset).get(),
|
2003-05-02 19:21:00 +00:00
|
|
|
baseURL,
|
2005-02-03 17:23:35 +00:00
|
|
|
nsContentUtils::GetIOServiceWeakRef());
|
2003-03-19 01:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Struct used to dispatch events
|
|
|
|
*/
|
2003-04-15 01:09:09 +00:00
|
|
|
MOZ_DECL_CTOR_COUNTER(ImageEvent)
|
|
|
|
|
2003-06-24 05:32:59 +00:00
|
|
|
class ImageEvent : public PLEvent,
|
|
|
|
public nsDummyLayoutRequest
|
2003-05-09 20:42:49 +00:00
|
|
|
{
|
2003-06-24 05:32:59 +00:00
|
|
|
public:
|
2004-07-31 23:15:21 +00:00
|
|
|
ImageEvent(nsPresContext* aPresContext, nsIContent* aContent,
|
2003-06-24 05:32:59 +00:00
|
|
|
const nsAString& aMessage, nsILoadGroup *aLoadGroup)
|
|
|
|
: nsDummyLayoutRequest(nsnull),
|
|
|
|
mPresContext(aPresContext),
|
2003-03-19 05:38:33 +00:00
|
|
|
mContent(aContent),
|
2003-05-09 20:42:49 +00:00
|
|
|
mMessage(aMessage),
|
2003-06-24 05:32:59 +00:00
|
|
|
mLoadGroup(aLoadGroup)
|
2003-04-15 01:09:09 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(ImageEvent);
|
|
|
|
}
|
|
|
|
~ImageEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ImageEvent);
|
|
|
|
}
|
|
|
|
|
2004-07-31 23:15:21 +00:00
|
|
|
nsCOMPtr<nsPresContext> mPresContext;
|
2003-03-19 05:38:33 +00:00
|
|
|
nsCOMPtr<nsIContent> mContent;
|
|
|
|
nsString mMessage;
|
2003-05-09 20:42:49 +00:00
|
|
|
nsCOMPtr<nsILoadGroup> mLoadGroup;
|
2003-03-19 01:08:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PR_STATIC_CALLBACK(void*)
|
|
|
|
HandleImagePLEvent(PLEvent* aEvent)
|
|
|
|
{
|
|
|
|
ImageEvent* evt = NS_STATIC_CAST(ImageEvent*, aEvent);
|
2003-03-19 05:38:33 +00:00
|
|
|
nsEventStatus estatus = nsEventStatus_eIgnore;
|
2004-01-12 08:25:18 +00:00
|
|
|
PRUint32 eventMsg;
|
2003-03-19 05:38:33 +00:00
|
|
|
|
2004-05-22 22:15:22 +00:00
|
|
|
if (evt->mMessage.EqualsLiteral("load")) {
|
2004-01-12 08:25:18 +00:00
|
|
|
eventMsg = NS_IMAGE_LOAD;
|
2003-03-19 05:38:33 +00:00
|
|
|
} else {
|
2004-01-12 08:25:18 +00:00
|
|
|
eventMsg = NS_IMAGE_ERROR;
|
2003-03-19 05:38:33 +00:00
|
|
|
}
|
|
|
|
|
2004-01-12 08:25:18 +00:00
|
|
|
nsEvent event(eventMsg);
|
2003-03-19 05:38:33 +00:00
|
|
|
evt->mContent->HandleDOMEvent(evt->mPresContext, &event, nsnull,
|
|
|
|
NS_EVENT_FLAG_INIT, &estatus);
|
2003-05-09 20:42:49 +00:00
|
|
|
|
2003-06-24 05:32:59 +00:00
|
|
|
evt->mLoadGroup->RemoveRequest(evt, nsnull, NS_OK);
|
|
|
|
|
2003-03-19 01:08:22 +00:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_STATIC_CALLBACK(void)
|
|
|
|
DestroyImagePLEvent(PLEvent* aEvent)
|
|
|
|
{
|
|
|
|
ImageEvent* evt = NS_STATIC_CAST(ImageEvent*, aEvent);
|
2003-06-24 05:32:59 +00:00
|
|
|
|
|
|
|
// We're reference counted, and we hold a strong reference to
|
|
|
|
// ourselves while we're a 'live' PLEvent. Now that the PLEvent is
|
|
|
|
// destroyed, release ourselves.
|
|
|
|
|
|
|
|
NS_RELEASE(evt);
|
2003-03-19 01:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsImageLoadingContent::FireEvent(const nsAString& aEventType)
|
|
|
|
{
|
|
|
|
// We have to fire the event asynchronously so that we won't go into infinite
|
|
|
|
// loops in cases when onLoad handlers reset the src and the new src is in
|
|
|
|
// cache.
|
2003-05-09 20:42:49 +00:00
|
|
|
|
2003-07-28 21:09:56 +00:00
|
|
|
nsCOMPtr<nsIDocument> document = GetOurDocument();
|
2003-05-09 20:42:49 +00:00
|
|
|
if (!document) {
|
|
|
|
// no use to fire events if there is no document....
|
2003-07-28 21:09:56 +00:00
|
|
|
return NS_OK;
|
2003-05-09 20:42:49 +00:00
|
|
|
}
|
2003-07-28 21:09:56 +00:00
|
|
|
nsresult rv;
|
2003-03-19 01:08:22 +00:00
|
|
|
nsCOMPtr<nsIEventQueueService> eventQService =
|
|
|
|
do_GetService("@mozilla.org/event-queue-service;1", &rv);
|
|
|
|
NS_ENSURE_TRUE(eventQService, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventQueue> eventQ;
|
|
|
|
// Use the UI thread event queue (though we should not be getting called from
|
|
|
|
// off the UI thread in any case....)
|
|
|
|
rv = eventQService->GetSpecialEventQueue(nsIEventQueueService::UI_THREAD_EVENT_QUEUE,
|
|
|
|
getter_AddRefs(eventQ));
|
|
|
|
NS_ENSURE_TRUE(eventQ, rv);
|
|
|
|
|
2003-10-22 06:09:48 +00:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = document->GetDocumentLoadGroup();
|
2003-05-09 20:42:49 +00:00
|
|
|
|
2003-09-27 04:18:26 +00:00
|
|
|
nsIPresShell *shell = document->GetShellAt(0);
|
2003-03-19 05:38:33 +00:00
|
|
|
NS_ENSURE_TRUE(shell, NS_ERROR_FAILURE);
|
2003-05-09 20:42:49 +00:00
|
|
|
|
2004-08-20 20:34:37 +00:00
|
|
|
nsPresContext *presContext = shell->GetPresContext();
|
2003-03-19 05:38:33 +00:00
|
|
|
NS_ENSURE_TRUE(presContext, NS_ERROR_FAILURE);
|
2003-05-09 20:42:49 +00:00
|
|
|
|
2003-03-19 05:38:33 +00:00
|
|
|
nsCOMPtr<nsIContent> ourContent = do_QueryInterface(this);
|
2003-03-19 01:08:22 +00:00
|
|
|
|
2003-05-09 20:42:49 +00:00
|
|
|
ImageEvent* evt = new ImageEvent(presContext, ourContent, aEventType,
|
2003-06-24 05:32:59 +00:00
|
|
|
loadGroup);
|
2003-05-09 20:42:49 +00:00
|
|
|
|
2003-03-19 01:08:22 +00:00
|
|
|
NS_ENSURE_TRUE(evt, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
PL_InitEvent(evt, this, ::HandleImagePLEvent, ::DestroyImagePLEvent);
|
|
|
|
|
2003-06-24 05:32:59 +00:00
|
|
|
// The event will own itself while it's in the event queue, once
|
|
|
|
// removed, it will release itself, and if there are no other
|
|
|
|
// references, it will be deleted.
|
|
|
|
NS_ADDREF(evt);
|
|
|
|
|
2003-05-09 20:42:49 +00:00
|
|
|
rv = eventQ->PostEvent(evt);
|
|
|
|
|
2003-10-06 01:46:31 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2003-06-24 05:32:59 +00:00
|
|
|
// Add the dummy request (the ImageEvent) to the load group only
|
|
|
|
// after all the early returns here!
|
|
|
|
loadGroup->AddRequest(evt, nsnull);
|
2003-05-09 20:42:49 +00:00
|
|
|
} else {
|
|
|
|
PL_DestroyEvent(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2003-03-19 01:08:22 +00:00
|
|
|
}
|