2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-17 20:52:41 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-11-02 22:36:43 +03:00
|
|
|
*
|
2004-04-17 20:52:41 +04:00
|
|
|
* 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/
|
1999-11-02 22:36:43 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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.
|
1999-11-02 22:36:43 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-17 20:52:41 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-17 20:52:41 +04:00
|
|
|
* either of 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"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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
|
2004-04-17 20:52:41 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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
|
2004-04-17 20:52:41 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-11-02 22:36:43 +03:00
|
|
|
|
|
|
|
#include "nsURILoader.h"
|
2003-10-09 08:00:01 +04:00
|
|
|
#include "nsAutoPtr.h"
|
1999-11-05 09:01:40 +03:00
|
|
|
#include "nsIURIContentListener.h"
|
1999-11-06 02:06:16 +03:00
|
|
|
#include "nsIContentHandler.h"
|
1999-11-05 09:01:40 +03:00
|
|
|
#include "nsILoadGroup.h"
|
2000-01-29 09:02:36 +03:00
|
|
|
#include "nsIDocumentLoader.h"
|
2000-04-03 06:26:05 +04:00
|
|
|
#include "nsIWebProgress.h"
|
|
|
|
#include "nsIWebProgressListener.h"
|
1999-11-05 09:01:40 +03:00
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIStreamListener.h"
|
1999-11-30 00:00:52 +03:00
|
|
|
#include "nsIURI.h"
|
1999-11-05 09:01:40 +03:00
|
|
|
#include "nsIChannel.h"
|
1999-11-18 10:36:41 +03:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2001-09-06 01:28:38 +04:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
1999-11-10 09:24:40 +03:00
|
|
|
#include "nsIProgressEventSink.h"
|
1999-11-30 00:00:52 +03:00
|
|
|
#include "nsIInputStream.h"
|
1999-11-18 04:02:31 +03:00
|
|
|
#include "nsIStreamConverterService.h"
|
2000-02-15 07:04:34 +03:00
|
|
|
#include "nsWeakReference.h"
|
2001-05-12 01:05:08 +04:00
|
|
|
#include "nsIHttpChannel.h"
|
2002-09-04 02:00:22 +04:00
|
|
|
#include "nsIMultiPartChannel.h"
|
2002-03-21 01:50:33 +03:00
|
|
|
#include "netCore.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2001-05-14 06:16:27 +04:00
|
|
|
#include "nsIDocShell.h"
|
2000-02-15 07:04:34 +03:00
|
|
|
#include "nsIDocShellTreeItem.h"
|
|
|
|
#include "nsIDocShellTreeOwner.h"
|
1999-11-05 09:01:40 +03:00
|
|
|
|
1999-11-02 22:36:43 +03:00
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
2000-09-01 05:54:35 +04:00
|
|
|
#include "nsIDOMWindowInternal.h"
|
2002-09-27 05:30:04 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2000-04-21 09:18:15 +04:00
|
|
|
#include "nsDOMError.h"
|
2000-03-01 00:33:23 +03:00
|
|
|
|
2001-11-30 03:07:13 +03:00
|
|
|
#include "nsICategoryManager.h"
|
2000-09-14 03:57:52 +04:00
|
|
|
#include "nsCExternalHandlerService.h" // contains contractids for the helper app service
|
2000-06-23 08:43:28 +04:00
|
|
|
|
2003-06-13 01:57:49 +04:00
|
|
|
#include "nsIMIMEHeaderParam.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
|
2004-01-08 06:56:02 +03:00
|
|
|
#include "nsMimeTypes.h"
|
|
|
|
|
1999-11-18 04:02:31 +03:00
|
|
|
static NS_DEFINE_CID(kStreamConverterServiceCID, NS_STREAMCONVERTERSERVICE_CID);
|
2003-10-30 05:03:41 +03:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRLogModuleInfo* nsURILoader::mLog = nsnull;
|
|
|
|
#endif
|
1999-11-05 09:01:40 +03:00
|
|
|
|
2003-10-30 05:03:41 +03:00
|
|
|
#define LOG(args) PR_LOG(nsURILoader::mLog, PR_LOG_DEBUG, args)
|
|
|
|
#define LOG_ERROR(args) PR_LOG(nsURILoader::mLog, PR_LOG_ERROR, args)
|
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(nsURILoader::mLog, PR_LOG_DEBUG)
|
2000-01-29 09:02:36 +03:00
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
/**
|
|
|
|
* The nsDocumentOpenInfo contains the state required when a single
|
|
|
|
* document is being opened in order to discover the content type...
|
|
|
|
* Each instance remains alive until its target URL has been loaded
|
|
|
|
* (or aborted).
|
1999-11-05 09:01:40 +03:00
|
|
|
*/
|
1999-11-16 00:35:40 +03:00
|
|
|
class nsDocumentOpenInfo : public nsIStreamListener
|
1999-11-05 09:01:40 +03:00
|
|
|
{
|
|
|
|
public:
|
2003-04-12 07:36:53 +04:00
|
|
|
// Needed for nsCOMPtr to work right... Don't call this!
|
|
|
|
nsDocumentOpenInfo();
|
|
|
|
|
|
|
|
// Real constructor
|
2004-04-17 01:06:07 +04:00
|
|
|
nsDocumentOpenInfo(nsIInterfaceRequestor* aWindowContext,
|
2003-10-09 08:00:01 +04:00
|
|
|
PRBool aIsContentPreferred,
|
|
|
|
nsURILoader* aURILoader);
|
2003-04-12 07:36:53 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
1999-11-05 09:01:40 +03:00
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
nsresult Open(nsIChannel* channel);
|
1999-11-05 09:01:40 +03:00
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
// Call this (from OnStartRequest) to attempt to find an nsIStreamListener to
|
|
|
|
// take the data off our hands.
|
|
|
|
nsresult DispatchContent(nsIRequest *request, nsISupports * aCtxt);
|
1999-11-05 09:01:40 +03:00
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
// Call this if we need to insert a stream converter from aSrcContentType to
|
|
|
|
// aOutContentType into the StreamListener chain. DO NOT call it if the two
|
|
|
|
// types are the same, since no conversion is needed in that case.
|
|
|
|
nsresult ConvertData(nsIRequest *request,
|
2003-10-09 08:00:01 +04:00
|
|
|
nsIURIContentListener *aListener,
|
2003-04-12 07:36:53 +04:00
|
|
|
const nsACString & aSrcContentType,
|
|
|
|
const nsACString & aOutContentType);
|
1999-11-18 04:02:31 +03:00
|
|
|
|
2003-10-09 08:00:01 +04:00
|
|
|
/**
|
|
|
|
* Function to attempt to use aListener to handle the load. If
|
|
|
|
* PR_TRUE is returned, nothing else needs to be done; if PR_FALSE
|
|
|
|
* is returned, then a different way of handling the load should be
|
|
|
|
* tried.
|
|
|
|
*/
|
|
|
|
PRBool TryContentListener(nsIURIContentListener* aListener,
|
|
|
|
nsIChannel* aChannel);
|
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
// nsIRequestObserver methods:
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
1999-11-05 09:01:40 +03:00
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
// nsIStreamListener methods:
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
1999-11-05 09:01:40 +03:00
|
|
|
|
|
|
|
protected:
|
2004-04-17 01:06:07 +04:00
|
|
|
~nsDocumentOpenInfo();
|
1999-12-28 12:20:05 +03:00
|
|
|
|
1999-11-05 09:01:40 +03:00
|
|
|
protected:
|
2003-10-09 08:00:01 +04:00
|
|
|
/**
|
|
|
|
* The first content listener to try dispatching data to. Typically
|
|
|
|
* the listener associated with the entity that originated the load.
|
|
|
|
*/
|
2003-04-12 07:36:53 +04:00
|
|
|
nsCOMPtr<nsIURIContentListener> m_contentListener;
|
2003-10-09 08:00:01 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The stream listener to forward nsIStreamListener notifications
|
|
|
|
* to. This is set once the load is dispatched.
|
|
|
|
*/
|
2003-04-12 07:36:53 +04:00
|
|
|
nsCOMPtr<nsIStreamListener> m_targetStreamListener;
|
2003-10-09 08:00:01 +04:00
|
|
|
|
|
|
|
/**
|
2004-04-17 01:06:07 +04:00
|
|
|
* A pointer to the entity that originated the load. We depend on getting
|
|
|
|
* things like nsIURIContentListeners, nsIDOMWindows, etc off of it.
|
2003-10-09 08:00:01 +04:00
|
|
|
*/
|
2004-04-17 01:06:07 +04:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> m_originalContext;
|
2003-10-09 08:00:01 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Boolean to pass to CanHandleContent (also determines whether we
|
|
|
|
* use CanHandleContent or IsPreferred).
|
|
|
|
*/
|
2003-04-12 07:36:53 +04:00
|
|
|
PRBool mIsContentPreferred;
|
2003-10-09 08:00:01 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of the data we will be trying to dispatch.
|
|
|
|
*/
|
2003-10-09 10:28:49 +04:00
|
|
|
nsCString mContentType;
|
2003-10-09 08:00:01 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the URILoader service so we can access its list of
|
|
|
|
* nsIURIContentListeners.
|
|
|
|
*/
|
|
|
|
nsRefPtr<nsURILoader> mURILoader;
|
1999-11-05 09:01:40 +03:00
|
|
|
};
|
|
|
|
|
2003-09-08 04:18:24 +04:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsDocumentOpenInfo)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsDocumentOpenInfo)
|
1999-11-30 00:00:52 +03:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsDocumentOpenInfo)
|
2003-04-12 07:36:53 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIRequestObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
2000-03-06 00:26:01 +03:00
|
|
|
NS_INTERFACE_MAP_END_THREADSAFE
|
1999-11-05 09:01:40 +03:00
|
|
|
|
|
|
|
nsDocumentOpenInfo::nsDocumentOpenInfo()
|
|
|
|
{
|
2003-04-12 07:36:53 +04:00
|
|
|
NS_NOTREACHED("This should never be called\n");
|
1999-11-05 09:01:40 +03:00
|
|
|
}
|
|
|
|
|
2004-04-17 01:06:07 +04:00
|
|
|
nsDocumentOpenInfo::nsDocumentOpenInfo(nsIInterfaceRequestor* aWindowContext,
|
2003-10-09 08:00:01 +04:00
|
|
|
PRBool aIsContentPreferred,
|
|
|
|
nsURILoader* aURILoader)
|
2003-04-12 07:36:53 +04:00
|
|
|
: m_originalContext(aWindowContext),
|
2003-10-09 08:00:01 +04:00
|
|
|
mIsContentPreferred(aIsContentPreferred),
|
|
|
|
mURILoader(aURILoader)
|
1999-11-05 09:01:40 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
nsDocumentOpenInfo::~nsDocumentOpenInfo()
|
1999-12-28 12:20:05 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
nsresult nsDocumentOpenInfo::Open(nsIChannel *aChannel)
|
1999-11-05 09:01:40 +03:00
|
|
|
{
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG(("[0x%p] nsDocumentOpenInfo::Open", this));
|
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
// this method is not complete!!! Eventually, we should first go
|
1999-11-05 09:01:40 +03:00
|
|
|
// to the content listener and ask them for a protocol handler...
|
|
|
|
// if they don't give us one, we need to go to the registry and get
|
|
|
|
// the preferred protocol handler.
|
|
|
|
|
|
|
|
// But for now, I'm going to let necko do the work for us....
|
|
|
|
|
2001-10-27 06:52:39 +04:00
|
|
|
nsresult rv;
|
2001-05-14 06:16:27 +04:00
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
// ask our window context if it has a uri content listener...
|
|
|
|
m_contentListener = do_GetInterface(m_originalContext, &rv);
|
2001-05-14 06:16:27 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-04-03 06:26:05 +04:00
|
|
|
// now just open the channel!
|
2003-10-30 05:03:41 +03:00
|
|
|
rv = aChannel->AsyncOpen(this, nsnull);
|
2001-02-21 23:38:08 +03:00
|
|
|
|
2001-09-08 08:58:59 +04:00
|
|
|
// no content from this load - that's OK.
|
|
|
|
if (rv == NS_ERROR_DOM_RETVAL_UNDEFINED ||
|
|
|
|
rv == NS_ERROR_NO_CONTENT) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" rv is NS_ERROR_DOM_RETVAL_UNDEFINED or NS_ERROR_NO_CONTENT -- doing nothing"));
|
|
|
|
rv = NS_OK;
|
2000-04-21 09:18:15 +04:00
|
|
|
}
|
2001-09-08 08:58:59 +04:00
|
|
|
|
1999-11-05 09:01:40 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHODIMP nsDocumentOpenInfo::OnStartRequest(nsIRequest *request, nsISupports * aCtxt)
|
1999-11-05 09:01:40 +03:00
|
|
|
{
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG(("[0x%p] nsDocumentOpenInfo::OnStartRequest", this));
|
|
|
|
|
2000-06-17 01:21:04 +04:00
|
|
|
nsresult rv = NS_OK;
|
2000-05-25 09:12:58 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Deal with "special" HTTP responses:
|
|
|
|
//
|
2003-03-20 02:55:12 +03:00
|
|
|
// - In the case of a 204 (No Content) or 205 (Reset Content) response, do not try to find a
|
2000-05-25 09:12:58 +04:00
|
|
|
// content handler. Just return. This causes the request to be
|
|
|
|
// ignored.
|
|
|
|
//
|
2001-05-12 01:05:08 +04:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(request, &rv));
|
2000-05-25 09:12:58 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRUint32 responseCode = 0;
|
|
|
|
|
2001-08-02 02:52:42 +04:00
|
|
|
rv = httpChannel->GetResponseStatus(&responseCode);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG_ERROR((" Failed to get HTTP response status"));
|
|
|
|
|
2001-08-02 02:52:42 +04:00
|
|
|
// behave as in the canceled case
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" HTTP response status: %d", responseCode));
|
|
|
|
|
2003-03-20 02:55:12 +03:00
|
|
|
if (204 == responseCode || 205 == responseCode) {
|
2000-05-25 09:12:58 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-13 10:47:22 +03:00
|
|
|
//
|
|
|
|
// Make sure that the transaction has succeeded, so far...
|
|
|
|
//
|
|
|
|
nsresult status;
|
|
|
|
|
|
|
|
rv = request->GetStatus(&status);
|
|
|
|
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Unable to get request status!");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (NS_FAILED(status)) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG_ERROR((" Request failed, status: 0x%08X", rv));
|
|
|
|
|
2001-11-13 10:47:22 +03:00
|
|
|
//
|
|
|
|
// The transaction has already reported an error - so it will be torn
|
|
|
|
// down. Therefore, it is not necessary to return an error code...
|
|
|
|
//
|
2000-06-17 01:21:04 +04:00
|
|
|
return NS_OK;
|
2001-11-13 10:47:22 +03:00
|
|
|
}
|
2000-06-17 01:21:04 +04:00
|
|
|
|
2004-01-08 06:56:02 +03:00
|
|
|
if (httpChannel && mContentType.IsEmpty()) {
|
|
|
|
// This is our initial dispatch, and this is an HTTP channel. Check for
|
|
|
|
// the text/plain mess.
|
|
|
|
nsCAutoString contentType;
|
|
|
|
httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Content-Type"),
|
|
|
|
contentType);
|
|
|
|
// Make sure to do a case-sensitive exact match comparison here. Apache
|
|
|
|
// 1.x just sends text/plain for "unknown", while Apache 2.x sends
|
|
|
|
// text/plain with a ISO-8859-1 charset. Debian's Apache version, just to
|
|
|
|
// be different, sends text/plain with iso-8859-1 charset. Don't do
|
|
|
|
// general case-insensitive comparison, since we really want to apply this
|
|
|
|
// crap as rarely as we can.
|
2004-05-23 02:15:22 +04:00
|
|
|
if (contentType.EqualsLiteral("text/plain") ||
|
2004-01-08 06:56:02 +03:00
|
|
|
contentType.Equals(
|
|
|
|
NS_LITERAL_CSTRING("text/plain; charset=ISO-8859-1")) ||
|
|
|
|
contentType.Equals(
|
|
|
|
NS_LITERAL_CSTRING("text/plain; charset=iso-8859-1"))) {
|
2004-03-15 05:00:15 +03:00
|
|
|
// Check whether we have content-encoding. If we do, don't try to detect
|
|
|
|
// the type, since that will lead to the content being automatically
|
|
|
|
// decompressed....
|
|
|
|
nsCAutoString contentEncoding;
|
|
|
|
httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Content-Encoding"),
|
|
|
|
contentEncoding);
|
|
|
|
if (contentEncoding.IsEmpty()) {
|
|
|
|
// OK, this is initial dispatch of an HTTP response and its Content-Type
|
|
|
|
// header is exactly "text/plain". We need to check whether this is
|
|
|
|
// really text....
|
|
|
|
LOG((" Possibly bogus text/plain; resetting type to " APPLICATION_MAYBE_TEXT));
|
|
|
|
httpChannel->SetContentType(NS_LITERAL_CSTRING(APPLICATION_MAYBE_TEXT));
|
|
|
|
}
|
2004-01-08 06:56:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = DispatchContent(request, aCtxt);
|
2003-10-30 05:03:41 +03:00
|
|
|
|
|
|
|
LOG((" After dispatch, m_targetStreamListener: 0x%p", m_targetStreamListener.get()));
|
|
|
|
|
1999-11-18 04:02:31 +03:00
|
|
|
if (m_targetStreamListener)
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = m_targetStreamListener->OnStartRequest(request, aCtxt);
|
2003-10-30 05:03:41 +03:00
|
|
|
|
|
|
|
LOG((" OnStartRequest returning: 0x%08X", rv));
|
|
|
|
|
1999-11-16 00:35:40 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHODIMP nsDocumentOpenInfo::OnDataAvailable(nsIRequest *request, nsISupports * aCtxt,
|
1999-11-16 00:35:40 +03:00
|
|
|
nsIInputStream * inStr, PRUint32 sourceOffset, PRUint32 count)
|
|
|
|
{
|
|
|
|
// if we have retarged to the end stream listener, then forward the call....
|
|
|
|
// otherwise, don't do anything
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2000-06-17 01:21:04 +04:00
|
|
|
|
1999-11-16 00:35:40 +03:00
|
|
|
if (m_targetStreamListener)
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = m_targetStreamListener->OnDataAvailable(request, aCtxt, inStr, sourceOffset, count);
|
1999-11-05 09:01:40 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHODIMP nsDocumentOpenInfo::OnStopRequest(nsIRequest *request, nsISupports *aCtxt,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus)
|
1999-11-05 09:01:40 +03:00
|
|
|
{
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG(("[0x%p] nsDocumentOpenInfo::OnStopRequest", this));
|
|
|
|
|
2001-06-27 02:58:18 +04:00
|
|
|
if ( m_targetStreamListener)
|
2000-05-10 06:05:54 +04:00
|
|
|
{
|
2001-06-29 07:09:59 +04:00
|
|
|
nsCOMPtr<nsIStreamListener> listener(m_targetStreamListener);
|
1999-11-05 09:01:40 +03:00
|
|
|
|
2003-10-09 08:00:01 +04:00
|
|
|
// If this is a multipart stream, we could get another
|
|
|
|
// OnStartRequest after this... reset state.
|
2001-06-29 07:09:59 +04:00
|
|
|
m_targetStreamListener = 0;
|
2003-10-09 10:28:49 +04:00
|
|
|
mContentType.Truncate();
|
2001-06-29 07:09:59 +04:00
|
|
|
listener->OnStopRequest(request, aCtxt, aStatus);
|
|
|
|
}
|
1999-12-28 12:20:05 +03:00
|
|
|
|
2001-11-13 10:47:22 +03:00
|
|
|
// Remember...
|
|
|
|
// In the case of multiplexed streams (such as multipart/x-mixed-replace)
|
|
|
|
// these stream listener methods could be called again :-)
|
|
|
|
//
|
2001-06-29 07:09:59 +04:00
|
|
|
return NS_OK;
|
1999-11-05 09:01:40 +03:00
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsresult nsDocumentOpenInfo::DispatchContent(nsIRequest *request, nsISupports * aCtxt)
|
1999-11-18 04:02:31 +03:00
|
|
|
{
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG(("[0x%p] nsDocumentOpenInfo::DispatchContent for type '%s'", this, mContentType.get()));
|
|
|
|
|
2003-10-09 08:00:01 +04:00
|
|
|
NS_PRECONDITION(!m_targetStreamListener,
|
|
|
|
"Why do we already have a target stream listener?");
|
|
|
|
|
2000-01-05 07:44:27 +03:00
|
|
|
nsresult rv;
|
2001-02-21 23:38:08 +03:00
|
|
|
nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request);
|
2001-04-10 10:01:08 +04:00
|
|
|
if (!aChannel) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG_ERROR((" Request is not a channel. Bailing."));
|
|
|
|
return NS_ERROR_FAILURE;
|
2001-04-10 10:01:08 +04:00
|
|
|
}
|
2000-01-05 07:44:27 +03:00
|
|
|
|
2004-01-08 06:56:02 +03:00
|
|
|
NS_NAMED_LITERAL_CSTRING(anyType, "*/*");
|
|
|
|
if (mContentType.IsEmpty() || mContentType == anyType) {
|
2003-10-09 10:28:49 +04:00
|
|
|
rv = aChannel->GetContentType(mContentType);
|
2003-10-09 08:00:01 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Got type from channel: '%s'", mContentType.get()));
|
2003-04-12 07:36:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the data should be forced to be handled externally. This
|
|
|
|
// could happen because the Content-Disposition header is set so, or, in the
|
|
|
|
// future, because the user has specified external handling for the MIME
|
|
|
|
// type.
|
|
|
|
PRBool forceExternalHandling = PR_FALSE;
|
|
|
|
nsCAutoString disposition;
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(request));
|
2004-11-07 05:22:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2003-04-12 07:36:53 +04:00
|
|
|
if (httpChannel)
|
|
|
|
{
|
|
|
|
rv = httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("content-disposition"),
|
|
|
|
disposition);
|
2004-11-07 05:22:55 +03:00
|
|
|
httpChannel->GetURI(getter_AddRefs(uri));
|
2003-04-12 07:36:53 +04:00
|
|
|
}
|
|
|
|
else
|
1999-11-18 04:02:31 +03:00
|
|
|
{
|
2003-04-12 07:36:53 +04:00
|
|
|
nsCOMPtr<nsIMultiPartChannel> multipartChannel(do_QueryInterface(request));
|
|
|
|
if (multipartChannel)
|
2002-09-04 02:00:22 +04:00
|
|
|
{
|
2003-04-12 07:36:53 +04:00
|
|
|
rv = multipartChannel->GetContentDisposition(disposition);
|
2002-09-04 02:00:22 +04:00
|
|
|
}
|
2003-04-12 07:36:53 +04:00
|
|
|
}
|
|
|
|
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Disposition header: '%s'", disposition.get()));
|
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !disposition.IsEmpty())
|
|
|
|
{
|
2003-06-13 01:57:49 +04:00
|
|
|
nsCOMPtr<nsIMIMEHeaderParam> mimehdrpar = do_GetService(NS_MIMEHEADERPARAM_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2003-04-12 07:36:53 +04:00
|
|
|
{
|
2004-11-07 05:22:55 +03:00
|
|
|
nsCAutoString fallbackCharset;
|
|
|
|
if (uri)
|
|
|
|
uri->GetOriginCharset(fallbackCharset);
|
2003-06-13 01:57:49 +04:00
|
|
|
nsAutoString dispToken;
|
|
|
|
// Get the disposition type
|
2004-11-07 05:22:55 +03:00
|
|
|
rv = mimehdrpar->GetParameter(disposition, "", fallbackCharset,
|
|
|
|
PR_TRUE, nsnull, dispToken);
|
2003-04-12 07:36:53 +04:00
|
|
|
// RFC 2183, section 2.8 says that an unknown disposition
|
|
|
|
// value should be treated as "attachment"
|
2004-12-04 20:46:49 +03:00
|
|
|
// XXXbz this code is duplicated in GetFilenameAndExtensionFromChannel in
|
|
|
|
// nsExternalHelperAppService. Factor it out!
|
2003-06-18 13:07:58 +04:00
|
|
|
if (NS_FAILED(rv) ||
|
2004-12-04 20:46:49 +03:00
|
|
|
(// Some broken sites just send
|
|
|
|
// Content-Disposition: ; filename="file"
|
|
|
|
// screen those out here.
|
|
|
|
!dispToken.IsEmpty() &&
|
|
|
|
!dispToken.LowerCaseEqualsLiteral("inline") &&
|
2003-04-12 07:36:53 +04:00
|
|
|
// Broken sites just send
|
|
|
|
// Content-Disposition: filename="file"
|
|
|
|
// without a disposition token... screen those out.
|
2004-01-07 23:23:46 +03:00
|
|
|
!dispToken.EqualsIgnoreCase("filename", 8)) &&
|
|
|
|
// Also in use is Content-Disposition: name="file"
|
|
|
|
!dispToken.EqualsIgnoreCase("name", 4))
|
2003-04-12 07:36:53 +04:00
|
|
|
// We have a content-disposition of "attachment" or unknown
|
|
|
|
forceExternalHandling = PR_TRUE;
|
2002-09-04 02:00:22 +04:00
|
|
|
}
|
2003-04-12 07:36:53 +04:00
|
|
|
}
|
2003-10-30 05:03:41 +03:00
|
|
|
|
|
|
|
LOG((" forceExternalHandling: %s", forceExternalHandling ? "yes" : "no"));
|
2002-09-04 02:00:22 +04:00
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
// We're going to try to find a contentListener that can handle our data
|
|
|
|
nsCOMPtr<nsIURIContentListener> contentListener;
|
|
|
|
// The type or data the contentListener wants.
|
|
|
|
nsXPIDLCString desiredContentType;
|
2002-09-04 02:00:22 +04:00
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
if (!forceExternalHandling)
|
|
|
|
{
|
2000-01-05 07:44:27 +03:00
|
|
|
//
|
2003-10-09 08:00:01 +04:00
|
|
|
// First step: See whether m_contentListener wants to handle this
|
|
|
|
// content type.
|
|
|
|
//
|
|
|
|
if (m_contentListener && TryContentListener(m_contentListener, aChannel)) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Success! Our default listener likes this type"));
|
2003-10-09 08:00:01 +04:00
|
|
|
// All done here
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Second step: See whether some other registered listener wants
|
|
|
|
// to handle this content type.
|
2000-01-05 07:44:27 +03:00
|
|
|
//
|
2003-10-09 08:00:01 +04:00
|
|
|
PRInt32 count = mURILoader->m_listeners.Count();
|
|
|
|
nsCOMPtr<nsIURIContentListener> listener;
|
|
|
|
for (PRInt32 i = 0; i < count; i++) {
|
|
|
|
listener = do_QueryReferent(mURILoader->m_listeners[i]);
|
|
|
|
if (listener) {
|
|
|
|
if (TryContentListener(listener, aChannel)) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Found listener registered on the URILoader"));
|
2003-10-09 08:00:01 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// remove from the listener list, reset i and update count
|
|
|
|
mURILoader->m_listeners.RemoveObjectAt(i--);
|
|
|
|
--count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Third step: Try to find a content listener that has not yet had
|
|
|
|
// the chance to register, as it is contained in a not-yet-loaded
|
|
|
|
// module, but which has registered a contract ID.
|
|
|
|
//
|
|
|
|
nsCOMPtr<nsICategoryManager> catman =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
|
|
|
|
if (catman) {
|
|
|
|
nsXPIDLCString contractidString;
|
|
|
|
rv = catman->GetCategoryEntry(NS_CONTENT_LISTENER_CATEGORYMANAGER_ENTRY,
|
2003-10-09 10:28:49 +04:00
|
|
|
mContentType.get(),
|
2003-10-09 08:00:01 +04:00
|
|
|
getter_Copies(contractidString));
|
|
|
|
if (NS_SUCCEEDED(rv) && !contractidString.IsEmpty()) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Listener contractid for '%s' is '%s'",
|
|
|
|
mContentType.get(), contractidString.get()));
|
|
|
|
|
2003-10-09 08:00:01 +04:00
|
|
|
listener = do_CreateInstance(contractidString);
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Listener from category manager: 0x%p", listener.get()));
|
|
|
|
|
2003-10-09 08:00:01 +04:00
|
|
|
if (listener && TryContentListener(listener, aChannel)) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Listener from category manager likes this type"));
|
2003-10-09 08:00:01 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Fourth step: try to find an nsIContentHandler for our type.
|
|
|
|
//
|
|
|
|
nsCAutoString handlerContractID (NS_CONTENT_HANDLER_CONTRACTID_PREFIX);
|
2003-10-09 10:28:49 +04:00
|
|
|
handlerContractID += mContentType;
|
2003-10-09 08:00:01 +04:00
|
|
|
|
2003-10-09 10:28:49 +04:00
|
|
|
nsCOMPtr<nsIContentHandler> contentHandler =
|
|
|
|
do_CreateInstance(handlerContractID.get());
|
2003-10-09 08:00:01 +04:00
|
|
|
if (contentHandler) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Content handler found"));
|
2004-04-17 01:06:07 +04:00
|
|
|
rv = contentHandler->HandleContent(mContentType.get(),
|
2003-10-09 08:00:01 +04:00
|
|
|
m_originalContext, request);
|
|
|
|
// XXXbz returning an error code to represent handling the
|
|
|
|
// content is just bizarre!
|
|
|
|
if (rv != NS_ERROR_WONT_HANDLE_CONTENT) {
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// The content handler has unexpectedly failed. Cancel the request
|
|
|
|
// just in case the handler didn't...
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Content handler failed. Aborting load"));
|
2003-10-09 08:00:01 +04:00
|
|
|
request->Cancel(rv);
|
|
|
|
}
|
2003-10-30 05:03:41 +03:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
else {
|
|
|
|
LOG((" Content handler taking over load"));
|
|
|
|
}
|
|
|
|
#endif
|
2003-10-09 08:00:01 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2003-04-12 07:36:53 +04:00
|
|
|
}
|
2002-09-04 02:00:22 +04:00
|
|
|
|
2003-10-09 08:00:01 +04:00
|
|
|
NS_ASSERTION(!m_targetStreamListener,
|
|
|
|
"If we found a listener, why are we not using it?");
|
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
//
|
2003-10-09 08:00:01 +04:00
|
|
|
// Fifth step: If no listener prefers this type, see if any stream
|
2003-10-09 10:28:49 +04:00
|
|
|
// converters exist to transform this content type into
|
2003-10-09 08:00:01 +04:00
|
|
|
// some other.
|
2003-04-12 07:36:53 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
// We always want to do this, since even content being forced to
|
|
|
|
// be handled externally may need decoding (eg via the unknown
|
|
|
|
// content decoder).
|
|
|
|
// Don't do this if the server sent us a MIME type of "*/*" because they saw
|
|
|
|
// it in our Accept header and got confused.
|
|
|
|
// XXXbz have to be careful here; may end up in some sort of bizarre infinite
|
|
|
|
// decoding loop.
|
2003-10-09 10:28:49 +04:00
|
|
|
if (mContentType != anyType) {
|
|
|
|
rv = ConvertData(request, m_contentListener, mContentType, anyType);
|
2003-10-09 08:00:01 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
m_targetStreamListener = nsnull;
|
|
|
|
} else if (m_targetStreamListener) {
|
2003-04-12 07:36:53 +04:00
|
|
|
// We found a converter for this MIME type. We'll just pump data into it
|
|
|
|
// and let the downstream nsDocumentOpenInfo handle things.
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Converter taking over now"));
|
2003-04-12 07:36:53 +04:00
|
|
|
return NS_OK;
|
2000-01-05 07:44:27 +03:00
|
|
|
}
|
2003-04-12 07:36:53 +04:00
|
|
|
}
|
2003-10-09 08:00:01 +04:00
|
|
|
|
|
|
|
// Sixth step:
|
2003-04-12 07:36:53 +04:00
|
|
|
//
|
2003-10-09 08:00:01 +04:00
|
|
|
// All attempts to dispatch this content have failed. Just pass it off to
|
|
|
|
// the helper app service.
|
|
|
|
//
|
|
|
|
nsCOMPtr<nsIExternalHelperAppService> helperAppService =
|
|
|
|
do_GetService(NS_EXTERNALHELPERAPPSERVICE_CONTRACTID, &rv);
|
|
|
|
if (helperAppService) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Passing load off to helper app service"));
|
2004-09-18 00:33:25 +04:00
|
|
|
|
|
|
|
// Set these flags to indicate that the channel has been targeted and that
|
|
|
|
// we are not using the original consumer.
|
|
|
|
nsLoadFlags loadFlags = 0;
|
|
|
|
request->GetLoadFlags(&loadFlags);
|
|
|
|
request->SetLoadFlags(loadFlags | nsIChannel::LOAD_RETARGETED_DOCUMENT_URI
|
|
|
|
| nsIChannel::LOAD_TARGETED);
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
rv = helperAppService->DoContent(mContentType,
|
2003-10-09 08:00:01 +04:00
|
|
|
request,
|
|
|
|
m_originalContext,
|
|
|
|
getter_AddRefs(m_targetStreamListener));
|
|
|
|
if (NS_FAILED(rv)) {
|
2004-09-18 00:33:25 +04:00
|
|
|
request->SetLoadFlags(loadFlags);
|
2003-10-09 08:00:01 +04:00
|
|
|
m_targetStreamListener = nsnull;
|
2003-04-12 07:36:53 +04:00
|
|
|
}
|
|
|
|
}
|
2002-09-04 02:00:22 +04:00
|
|
|
|
2003-10-09 08:00:01 +04:00
|
|
|
NS_ASSERTION(m_targetStreamListener || NS_FAILED(rv),
|
|
|
|
"There is no way we should be successful at this point without a m_targetStreamListener");
|
1999-11-18 04:02:31 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
nsresult
|
|
|
|
nsDocumentOpenInfo::ConvertData(nsIRequest *request,
|
2003-10-09 08:00:01 +04:00
|
|
|
nsIURIContentListener* aListener,
|
2003-04-12 07:36:53 +04:00
|
|
|
const nsACString& aSrcContentType,
|
|
|
|
const nsACString& aOutContentType)
|
1999-11-18 04:02:31 +03:00
|
|
|
{
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG(("[0x%p] nsDocumentOpenInfo::ConvertData from '%s' to '%s'", this,
|
|
|
|
PromiseFlatCString(aSrcContentType).get(),
|
|
|
|
PromiseFlatCString(aOutContentType).get()));
|
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
NS_PRECONDITION(aSrcContentType != aOutContentType,
|
2003-10-09 08:00:01 +04:00
|
|
|
"ConvertData called when the two types are the same!");
|
1999-11-18 04:02:31 +03:00
|
|
|
nsresult rv = NS_OK;
|
2000-06-23 08:43:28 +04:00
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
nsCOMPtr<nsIStreamConverterService> StreamConvService =
|
|
|
|
do_GetService(kStreamConverterServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2003-10-30 05:03:41 +03:00
|
|
|
|
|
|
|
LOG((" Got converter service"));
|
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
// When applying stream decoders, it is necessary to "insert" an
|
|
|
|
// intermediate nsDocumentOpenInfo instance to handle the targeting of
|
|
|
|
// the "final" stream or streams.
|
|
|
|
//
|
|
|
|
// For certain content types (ie. multi-part/x-mixed-replace) the input
|
|
|
|
// stream is split up into multiple destination streams. This
|
|
|
|
// intermediate instance is used to target these "decoded" streams...
|
|
|
|
//
|
|
|
|
nsCOMPtr<nsDocumentOpenInfo> nextLink =
|
2003-10-09 08:00:01 +04:00
|
|
|
new nsDocumentOpenInfo(m_originalContext, mIsContentPreferred, mURILoader);
|
2003-04-12 07:36:53 +04:00
|
|
|
if (!nextLink) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Downstream DocumentOpenInfo would be: 0x%p", nextLink.get()));
|
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
// Make sure nextLink starts with the contentListener that said it wanted the
|
|
|
|
// results of this decode.
|
2003-10-09 08:00:01 +04:00
|
|
|
nextLink->m_contentListener = aListener;
|
2003-04-12 07:36:53 +04:00
|
|
|
// Also make sure it has to look for a stream listener to pump data into.
|
|
|
|
nextLink->m_targetStreamListener = nsnull;
|
|
|
|
|
2004-01-08 06:56:02 +03:00
|
|
|
// Make sure that nextLink treats the data as aOutContentType when
|
|
|
|
// dispatching; that way even if the stream converters don't change the type
|
|
|
|
// on the channel we will still do the right thing. If aOutContentType is
|
|
|
|
// */*, that's OK -- that will just indicate to nextLink that it should get
|
|
|
|
// the type off the channel.
|
|
|
|
nextLink->mContentType = aOutContentType;
|
2003-10-09 08:00:01 +04:00
|
|
|
|
2003-04-12 07:36:53 +04:00
|
|
|
// The following call sets m_targetStreamListener to the input end of the
|
|
|
|
// stream converter and sets the output end of the stream converter to
|
|
|
|
// nextLink. As we pump data into m_targetStreamListener the stream
|
|
|
|
// converter will convert it and pass the converted data to nextLink.
|
2004-06-29 20:45:07 +04:00
|
|
|
return StreamConvService->AsyncConvertData(PromiseFlatCString(aSrcContentType).get(),
|
|
|
|
PromiseFlatCString(aOutContentType).get(),
|
2003-04-12 07:36:53 +04:00
|
|
|
nextLink,
|
|
|
|
request,
|
|
|
|
getter_AddRefs(m_targetStreamListener));
|
1999-11-18 04:02:31 +03:00
|
|
|
}
|
|
|
|
|
2003-10-09 08:00:01 +04:00
|
|
|
PRBool
|
|
|
|
nsDocumentOpenInfo::TryContentListener(nsIURIContentListener* aListener,
|
|
|
|
nsIChannel* aChannel)
|
|
|
|
{
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG(("[0x%p] nsDocumentOpenInfo::TryContentListener; mIsContentPreferred = %s",
|
|
|
|
this, mIsContentPreferred ? "true" : "false"));
|
|
|
|
|
2003-10-09 08:00:01 +04:00
|
|
|
NS_PRECONDITION(aListener, "Must have a non-null listener");
|
|
|
|
NS_PRECONDITION(aChannel, "Must have a channel");
|
|
|
|
|
|
|
|
PRBool listenerWantsContent = PR_FALSE;
|
|
|
|
nsXPIDLCString typeToUse;
|
|
|
|
|
|
|
|
if (mIsContentPreferred) {
|
2003-10-09 10:28:49 +04:00
|
|
|
aListener->IsPreferred(mContentType.get(),
|
2003-10-09 08:00:01 +04:00
|
|
|
getter_Copies(typeToUse),
|
|
|
|
&listenerWantsContent);
|
|
|
|
} else {
|
2003-10-09 10:28:49 +04:00
|
|
|
aListener->CanHandleContent(mContentType.get(), PR_FALSE,
|
2003-10-09 08:00:01 +04:00
|
|
|
getter_Copies(typeToUse),
|
|
|
|
&listenerWantsContent);
|
|
|
|
}
|
|
|
|
if (!listenerWantsContent) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Listener is not interested"));
|
2003-10-09 08:00:01 +04:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2003-10-09 10:28:49 +04:00
|
|
|
if (!typeToUse.IsEmpty() && typeToUse != mContentType) {
|
2003-10-09 08:00:01 +04:00
|
|
|
// Need to do a conversion here.
|
|
|
|
|
2003-10-09 10:28:49 +04:00
|
|
|
nsresult rv = ConvertData(aChannel, aListener, mContentType, typeToUse);
|
2003-10-09 08:00:01 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// No conversion path -- we don't want this listener, if we got one
|
|
|
|
m_targetStreamListener = nsnull;
|
|
|
|
}
|
|
|
|
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Found conversion: %s", m_targetStreamListener ? "yes" : "no"));
|
|
|
|
|
2003-10-09 10:28:49 +04:00
|
|
|
// m_targetStreamListener is now the input end of the converter, and we can
|
|
|
|
// just pump the data in there, if it exists. If it does not, we need to
|
|
|
|
// try other nsIURIContentListeners.
|
2003-10-09 08:00:01 +04:00
|
|
|
return m_targetStreamListener != nsnull;
|
|
|
|
}
|
|
|
|
|
2003-10-09 10:28:49 +04:00
|
|
|
// At this point, aListener wants data of type mContentType. Let 'em have
|
|
|
|
// it. But first, if we are retargeting, set an appropriate flag on the
|
|
|
|
// channel
|
|
|
|
nsLoadFlags loadFlags = 0;
|
|
|
|
aChannel->GetLoadFlags(&loadFlags);
|
|
|
|
|
2004-09-18 00:33:25 +04:00
|
|
|
// Set this flag to indicate that the channel has been targeted at a final
|
|
|
|
// consumer. This load flag is tested in nsDocLoader::OnProgress.
|
|
|
|
nsLoadFlags newLoadFlags = nsIChannel::LOAD_TARGETED;
|
|
|
|
|
2003-10-09 08:00:01 +04:00
|
|
|
nsCOMPtr<nsIURIContentListener> originalListener =
|
|
|
|
do_GetInterface(m_originalContext);
|
|
|
|
if (originalListener != aListener) {
|
2004-09-18 00:33:25 +04:00
|
|
|
newLoadFlags |= nsIChannel::LOAD_RETARGETED_DOCUMENT_URI;
|
2003-10-09 08:00:01 +04:00
|
|
|
}
|
2004-09-18 00:33:25 +04:00
|
|
|
aChannel->SetLoadFlags(loadFlags | newLoadFlags);
|
2003-10-09 08:00:01 +04:00
|
|
|
|
|
|
|
PRBool abort = PR_FALSE;
|
2003-10-09 10:28:49 +04:00
|
|
|
nsresult rv = aListener->DoContent(mContentType.get(),
|
|
|
|
mIsContentPreferred,
|
|
|
|
aChannel,
|
|
|
|
getter_AddRefs(m_targetStreamListener),
|
|
|
|
&abort);
|
2003-10-09 08:00:01 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG_ERROR((" DoContent failed"));
|
|
|
|
|
2003-10-09 10:28:49 +04:00
|
|
|
// Unset the RETARGETED_DOCUMENT_URI flag if we set it...
|
|
|
|
aChannel->SetLoadFlags(loadFlags);
|
2003-10-09 08:00:01 +04:00
|
|
|
m_targetStreamListener = nsnull;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (abort) {
|
|
|
|
// Nothing else to do here -- aListener is handling it all. Make
|
|
|
|
// sure m_targetStreamListener is null so we don't do anything
|
|
|
|
// after this point.
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" Listener has aborted the load"));
|
2003-10-09 08:00:01 +04:00
|
|
|
m_targetStreamListener = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(abort || m_targetStreamListener, "DoContent returned no listener?");
|
|
|
|
|
|
|
|
// aListener is handling the load from this point on.
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-06 02:06:16 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Implementation of nsURILoader
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-11-02 22:36:43 +03:00
|
|
|
nsURILoader::nsURILoader()
|
|
|
|
{
|
2003-10-30 05:03:41 +03:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!mLog) {
|
|
|
|
mLog = PR_NewLogModule("URILoader");
|
|
|
|
}
|
|
|
|
#endif
|
1999-11-02 22:36:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsURILoader::~nsURILoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-09-08 04:18:24 +04:00
|
|
|
NS_IMPL_ADDREF(nsURILoader)
|
|
|
|
NS_IMPL_RELEASE(nsURILoader)
|
1999-11-30 00:00:52 +03:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsURILoader)
|
1999-12-02 09:59:39 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIURILoader)
|
1999-11-30 00:00:52 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIURILoader)
|
|
|
|
NS_INTERFACE_MAP_END
|
1999-11-02 22:36:43 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsURILoader::RegisterContentListener(nsIURIContentListener * aContentListener)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2001-03-28 22:09:03 +04:00
|
|
|
|
|
|
|
nsWeakPtr weakListener = do_GetWeakReference(aContentListener);
|
|
|
|
NS_ASSERTION(weakListener, "your URIContentListener must support weak refs!\n");
|
|
|
|
|
|
|
|
if (weakListener)
|
2002-12-11 03:15:19 +03:00
|
|
|
m_listeners.AppendObject(weakListener);
|
1999-11-02 22:36:43 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsURILoader::UnRegisterContentListener(nsIURIContentListener * aContentListener)
|
|
|
|
{
|
2001-03-28 22:09:03 +04:00
|
|
|
nsWeakPtr weakListener = do_GetWeakReference(aContentListener);
|
|
|
|
if (weakListener)
|
2002-12-11 03:15:19 +03:00
|
|
|
m_listeners.RemoveObject(weakListener);
|
2001-03-28 22:09:03 +04:00
|
|
|
|
1999-11-02 22:36:43 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHODIMP nsURILoader::OpenURI(nsIChannel *channel,
|
2001-10-27 06:52:39 +04:00
|
|
|
PRBool aIsContentPreferred,
|
2004-04-17 01:06:07 +04:00
|
|
|
nsIInterfaceRequestor *aWindowContext)
|
1999-11-06 02:06:16 +03:00
|
|
|
{
|
2003-04-07 00:33:38 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(channel);
|
1999-11-05 09:01:40 +03:00
|
|
|
|
2003-10-30 05:03:41 +03:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
channel->GetURI(getter_AddRefs(uri));
|
|
|
|
nsCAutoString spec;
|
|
|
|
uri->GetAsciiSpec(spec);
|
|
|
|
LOG(("nsURILoader::OpenURI for %s", spec.get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-03-24 03:23:40 +03:00
|
|
|
// Let the window context's uriListener know that the open is starting. This
|
|
|
|
// gives that window a chance to abort the load process.
|
2001-05-14 06:16:27 +04:00
|
|
|
nsCOMPtr<nsIURIContentListener> winContextListener(do_GetInterface(aWindowContext));
|
2003-04-07 00:33:38 +04:00
|
|
|
if (winContextListener) {
|
2000-03-24 03:23:40 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2001-02-21 23:38:08 +03:00
|
|
|
channel->GetURI(getter_AddRefs(uri));
|
2003-04-07 00:33:38 +04:00
|
|
|
if (uri) {
|
2000-03-30 07:21:26 +04:00
|
|
|
PRBool doAbort = PR_FALSE;
|
2001-05-14 06:16:27 +04:00
|
|
|
winContextListener->OnStartURIOpen(uri, &doAbort);
|
2000-03-24 03:23:40 +03:00
|
|
|
|
2003-04-07 00:33:38 +04:00
|
|
|
if (doAbort) {
|
2003-10-30 05:03:41 +03:00
|
|
|
LOG((" OnStartURIOpen aborted load"));
|
2003-04-07 00:33:38 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-05-14 06:16:27 +04:00
|
|
|
}
|
|
|
|
}
|
2000-02-15 07:04:34 +03:00
|
|
|
|
2003-04-07 00:33:38 +04:00
|
|
|
// we need to create a DocumentOpenInfo object which will go ahead and open
|
|
|
|
// the url and discover the content type....
|
2003-04-12 07:36:53 +04:00
|
|
|
nsCOMPtr<nsDocumentOpenInfo> loader =
|
2003-10-09 08:00:01 +04:00
|
|
|
new nsDocumentOpenInfo(aWindowContext, aIsContentPreferred, this);
|
2003-04-07 00:33:38 +04:00
|
|
|
|
1999-11-05 09:01:40 +03:00
|
|
|
if (!loader) return NS_ERROR_OUT_OF_MEMORY;
|
2000-01-29 09:02:36 +03:00
|
|
|
|
2000-05-07 06:01:10 +04:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> loadCookie;
|
2001-05-14 06:16:27 +04:00
|
|
|
SetupLoadCookie(aWindowContext, getter_AddRefs(loadCookie));
|
1999-11-05 09:01:40 +03:00
|
|
|
|
2004-09-29 00:48:58 +04:00
|
|
|
// Set the correct loadgroup on the channel
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup(do_GetInterface(loadCookie));
|
|
|
|
channel->SetLoadGroup(loadGroup);
|
|
|
|
|
1999-11-05 09:01:40 +03:00
|
|
|
// now instruct the loader to go ahead and open the url
|
2003-04-12 07:36:53 +04:00
|
|
|
return loader->Open(channel);
|
1999-11-05 09:01:40 +03:00
|
|
|
}
|
|
|
|
|
2000-02-14 13:12:55 +03:00
|
|
|
NS_IMETHODIMP nsURILoader::Stop(nsISupports* aLoadCookie)
|
|
|
|
{
|
2000-05-07 06:01:10 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIDocumentLoader> docLoader;
|
2000-02-14 13:12:55 +03:00
|
|
|
|
2000-05-07 06:01:10 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aLoadCookie);
|
2000-02-14 13:12:55 +03:00
|
|
|
|
2000-05-07 06:01:10 +04:00
|
|
|
docLoader = do_GetInterface(aLoadCookie, &rv);
|
|
|
|
if (docLoader) {
|
|
|
|
rv = docLoader->Stop();
|
|
|
|
}
|
|
|
|
return rv;
|
2000-02-14 13:12:55 +03:00
|
|
|
}
|
|
|
|
|
2000-01-29 09:02:36 +03:00
|
|
|
NS_IMETHODIMP
|
2004-04-17 01:06:07 +04:00
|
|
|
nsURILoader::GetDocumentLoaderForContext(nsIInterfaceRequestor * aWindowContext,
|
2000-05-07 06:01:10 +04:00
|
|
|
nsIDocumentLoader ** aDocLoader)
|
2000-01-29 09:02:36 +03:00
|
|
|
{
|
2000-05-07 06:01:10 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> loadCookieForWindow;
|
|
|
|
|
|
|
|
// Initialize the [out] parameter...
|
|
|
|
*aDocLoader = nsnull;
|
|
|
|
|
2000-01-29 09:02:36 +03:00
|
|
|
NS_ENSURE_ARG(aWindowContext);
|
2000-05-07 06:01:10 +04:00
|
|
|
|
|
|
|
rv = SetupLoadCookie(aWindowContext, getter_AddRefs(loadCookieForWindow));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-01-29 09:02:36 +03:00
|
|
|
|
2004-04-17 01:06:07 +04:00
|
|
|
rv = CallGetInterface(loadCookieForWindow.get(), aDocLoader);
|
2000-05-07 06:01:10 +04:00
|
|
|
return rv;
|
2000-01-29 09:02:36 +03:00
|
|
|
}
|
|
|
|
|
2004-04-17 01:06:07 +04:00
|
|
|
nsresult nsURILoader::SetupLoadCookie(nsIInterfaceRequestor * aWindowContext,
|
2000-05-07 06:01:10 +04:00
|
|
|
nsIInterfaceRequestor ** aLoadCookie)
|
2000-01-29 09:02:36 +03:00
|
|
|
{
|
|
|
|
// first, see if we have already set a load cookie on the cnt listener..
|
|
|
|
// i.e. if this isn't the first time we've tried to run a url through this window
|
|
|
|
// context then we don't need to create another load cookie, we can reuse the first one.
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsISupports> loadCookie;
|
|
|
|
|
2000-05-07 06:01:10 +04:00
|
|
|
// Initialize the [out] parameter...
|
|
|
|
*aLoadCookie = nsnull;
|
|
|
|
|
2000-01-29 09:02:36 +03:00
|
|
|
nsCOMPtr<nsIURIContentListener> cntListener (do_GetInterface(aWindowContext));
|
2000-05-07 06:01:10 +04:00
|
|
|
if (cntListener) {
|
|
|
|
// Get the load cookie for the requested window context...
|
2000-01-29 09:02:36 +03:00
|
|
|
rv = cntListener->GetLoadCookie(getter_AddRefs(loadCookie));
|
2000-05-07 06:01:10 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// If we don't have a load cookie for this window context yet, then
|
|
|
|
// go create one! In order to create a load cookie, we need to get
|
|
|
|
// the parent's load cookie if there is one...
|
|
|
|
//
|
|
|
|
if (!loadCookie) {
|
2000-01-29 09:02:36 +03:00
|
|
|
nsCOMPtr<nsIURIContentListener> parentListener;
|
2000-05-07 06:01:10 +04:00
|
|
|
nsCOMPtr<nsIDocumentLoader> parentDocLoader;
|
|
|
|
nsCOMPtr<nsIDocumentLoader> newDocLoader;
|
|
|
|
|
|
|
|
// Try to get the parent's load cookie...
|
2000-01-29 09:02:36 +03:00
|
|
|
cntListener->GetParentContentListener(getter_AddRefs(parentListener));
|
2000-05-07 06:01:10 +04:00
|
|
|
if (parentListener) {
|
2000-01-29 09:02:36 +03:00
|
|
|
rv = parentListener->GetLoadCookie(getter_AddRefs(loadCookie));
|
2000-05-07 06:01:10 +04:00
|
|
|
|
|
|
|
// if we had a parent cookie use it to help with the creation process
|
|
|
|
if (loadCookie) {
|
|
|
|
parentDocLoader = do_GetInterface(loadCookie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If there is no parent DocLoader, then use the global DocLoader
|
|
|
|
// service as the parent...
|
|
|
|
if (!parentDocLoader) {
|
2000-09-14 03:57:52 +04:00
|
|
|
parentDocLoader = do_GetService(NS_DOCUMENTLOADER_SERVICE_CONTRACTID, &rv);
|
2000-05-07 06:01:10 +04:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create a new document loader. The document loader represents
|
|
|
|
// the load cookie which the uriloader hands out...
|
|
|
|
//
|
|
|
|
rv = parentDocLoader->CreateDocumentLoader(getter_AddRefs(newDocLoader));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
newDocLoader->QueryInterface(NS_GET_IID(nsIInterfaceRequestor),
|
|
|
|
getter_AddRefs(loadCookie));
|
|
|
|
rv = cntListener->SetLoadCookie(loadCookie);
|
2000-01-29 09:02:36 +03:00
|
|
|
} // if we don't have a load cookie already
|
|
|
|
} // if we have a cntListener
|
|
|
|
|
2000-05-11 05:33:25 +04:00
|
|
|
// loadCookie may be null - for example, <a target="popupWin"> if popupWin is
|
|
|
|
// not a defined window. The following prevents a crash (Bug 32898)
|
|
|
|
if (loadCookie) {
|
2004-04-17 01:06:07 +04:00
|
|
|
rv = CallQueryInterface(loadCookie, aLoadCookie);
|
2000-05-11 05:33:25 +04:00
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2000-01-29 09:02:36 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
1999-11-30 00:00:52 +03:00
|
|
|
|