gecko-dev/uriloader/base/nsDocLoader.cpp

1092 lines
29 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
1998-07-01 11:29:20 +00:00
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (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/NPL/
*
* 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 Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1998
* Netscape Communications Corporation. All Rights Reserved.
*/
#ifdef XP_MAC
1998-07-29 21:06:19 +00:00
#include "nsIDocumentLoader.h"
#define NS_IMPL_IDS
#else
#define NS_IMPL_IDS
#include "nsIDocumentLoader.h"
#endif
1998-07-01 11:29:20 +00:00
#include "prmem.h"
#include "plstr.h"
#include "nsString.h"
#include "nsISupportsArray.h"
#include "nsIURL.h"
#include "nsIStreamListener.h"
#include "nsINetSupport.h"
1998-07-01 11:29:20 +00:00
#include "nsIPostToServer.h"
#include "nsIFactory.h"
#include "nsIContentViewerContainer.h"
1998-07-01 11:29:20 +00:00
// XXX: Only needed for dummy factory...
#include "nsIDocument.h"
#include "nsIDocumentViewer.h"
#include "nsICSSParser.h"
1998-07-01 11:29:20 +00:00
/* Forward declarations.... */
class nsDocLoaderImpl;
/* Private IIDs... */
/* eb001fa0-214f-11d2-bec0-00805f8a66dc */
#define NS_DOCUMENTBINDINFO_IID \
{ 0xeb001fa0, 0x214f, 0x11d2, \
{0xbe, 0xc0, 0x00, 0x80, 0x5f, 0x8a, 0x66, 0xdc} }
/* 28475a80-2150-11d2-bec0-00805f8a66dc */
#define NS_DOCLOADERIMPL_IID \
{ 0x28475a80, 0x2150, 0x11d2, \
{0xbe, 0xc0, 0x00, 0x80, 0x5f, 0x8a, 0x66, 0xdc} }
/* Define IIDs... */
NS_DEFINE_IID(kIStreamObserverIID, NS_ISTREAMOBSERVER_IID);
NS_DEFINE_IID(kIDocumentLoaderIID, NS_IDOCUMENTLOADER_IID);
NS_DEFINE_IID(kIDocumentLoaderFactoryIID, NS_IDOCUMENTLOADERFACTORY_IID);
NS_DEFINE_IID(kDocLoaderImplIID, NS_DOCLOADERIMPL_IID);
NS_DEFINE_IID(kDocumentBindInfoIID, NS_DOCUMENTBINDINFO_IID);
1998-07-01 11:29:20 +00:00
/*
* The nsDocumentBindInfo contains the state required when a single document
* is being loaded... Each instance remains alive until its target URL has
* been loaded (or aborted).
*
* The Document Loader maintains a list of nsDocumentBindInfo instances which
* represents the set of documents actively being loaded...
*/
class nsDocumentBindInfo : public nsIStreamListener, public nsINetSupport
1998-07-01 11:29:20 +00:00
{
public:
nsDocumentBindInfo(nsDocLoaderImpl* aDocLoader,
const char *aCommand,
nsIContentViewerContainer* aContainer,
1998-07-01 11:29:20 +00:00
nsISupports* aExtraInfo,
nsIStreamObserver* anObserver);
NS_DECL_ISUPPORTS
nsresult Bind(const nsString& aURLSpec,
nsIPostData* aPostData,
nsIStreamListener* aListener);
nsresult Stop(void);
1998-07-01 11:29:20 +00:00
/* nsIStreamListener interface methods... */
NS_IMETHOD GetBindInfo(nsIURL* aURL);
NS_IMETHOD OnProgress(nsIURL* aURL, PRInt32 aProgress, PRInt32 aProgressMax);
NS_IMETHOD OnStatus(nsIURL* aURL, const nsString& aMsg);
NS_IMETHOD OnStartBinding(nsIURL* aURL, const char *aContentType);
NS_IMETHOD OnDataAvailable(nsIURL* aURL, nsIInputStream *aStream, PRInt32 aLength);
NS_IMETHOD OnStopBinding(nsIURL* aURL, PRInt32 aStatus, const nsString& aMsg);
1998-07-01 11:29:20 +00:00
/* nsINetSupport interface methods */
NS_IMETHOD_(void) Alert(const nsString &aText);
NS_IMETHOD_(PRBool) Confirm(const nsString &aText);
NS_IMETHOD_(PRBool) Prompt(const nsString &aText,
const nsString &aDefault,
nsString &aResult);
NS_IMETHOD_(PRBool) PromptUserAndPassword(const nsString &aText,
nsString &aUser,
nsString &aPassword);
NS_IMETHOD_(PRBool) PromptPassword(const nsString &aText,
nsString &aPassword);
1998-07-24 21:03:33 +00:00
nsresult GetStatus(void) { return mStatus; }
1998-07-01 11:29:20 +00:00
protected:
virtual ~nsDocumentBindInfo();
protected:
char* m_Command;
nsIURL* m_Url;
nsIContentViewerContainer* m_Container;
1998-07-01 11:29:20 +00:00
nsISupports* m_ExtraInfo;
nsIStreamObserver* m_Observer;
nsINetSupport* m_NetSupport;
1998-07-01 11:29:20 +00:00
nsIStreamListener* m_NextStream;
nsDocLoaderImpl* m_DocLoader;
1998-07-24 21:03:33 +00:00
nsresult mStatus;
1998-07-01 11:29:20 +00:00
};
/****************************************************************************
* nsDocFactoryImpl implementation...
****************************************************************************/
1998-07-01 11:29:20 +00:00
class nsDocFactoryImpl : public nsIDocumentLoaderFactory
{
public:
nsDocFactoryImpl();
NS_DECL_ISUPPORTS
NS_IMETHOD CreateInstance(nsIURL* aURL,
const char* aContentType,
const char* aCommand,
nsIContentViewerContainer* aContainer,
1998-07-01 11:29:20 +00:00
nsIStreamListener** aDocListener,
nsIContentViewer** aDocViewer);
nsresult InitUAStyleSheet();
1998-07-27 18:05:34 +00:00
nsresult CreateImageDocument(nsIURL* aURL,
const char* aCommand,
nsIContentViewerContainer* aContainer,
nsIStreamListener** aDocListener,
nsIContentViewer** aDocViewer);
1998-07-01 11:29:20 +00:00
};
static nsIStyleSheet* gUAStyleSheet;
1998-07-01 11:29:20 +00:00
nsDocFactoryImpl::nsDocFactoryImpl()
{
NS_INIT_REFCNT();
}
/*
* Implementation of ISupports methods...
*/
NS_IMPL_ISUPPORTS(nsDocFactoryImpl,kIDocumentLoaderFactoryIID);
1998-07-10 05:35:23 +00:00
static char* gValidTypes[] = {"text/html","text/xml","application/rtf",0};
1998-07-27 18:05:34 +00:00
static char* gImageTypes[] = {"image/gif", "image/jpeg", 0 };
1998-07-01 11:29:20 +00:00
NS_IMETHODIMP
nsDocFactoryImpl::CreateInstance(nsIURL* aURL,
const char* aContentType,
const char *aCommand,
nsIContentViewerContainer* aContainer,
1998-07-01 11:29:20 +00:00
nsIStreamListener** aDocListener,
nsIContentViewer** aDocViewer)
1998-07-01 11:29:20 +00:00
{
1998-07-17 17:54:46 +00:00
nsresult rv = NS_ERROR_FAILURE;
1998-07-01 11:29:20 +00:00
nsIDocument* doc = nsnull;
nsIDocumentViewer* docv = nsnull;
1998-07-01 11:29:20 +00:00
1998-07-10 05:35:23 +00:00
int typeIndex=0;
while(gValidTypes[typeIndex]) {
if (0== PL_strcmp(gValidTypes[typeIndex++], aContentType)) {
goto nextstep;
}
1998-07-01 11:29:20 +00:00
}
1998-07-27 18:05:34 +00:00
// Try secondary types
typeIndex = 0;
while(gImageTypes[typeIndex]) {
if (0== PL_strcmp(gImageTypes[typeIndex++], aContentType)) {
return CreateImageDocument(aURL, aCommand,
aContainer,
aDocListener,
aDocViewer);
}
}
1998-07-10 05:35:23 +00:00
goto done;
1998-07-01 11:29:20 +00:00
1998-07-10 05:35:23 +00:00
nextstep:
// Load the UA style sheet if we haven't already done that
if (nsnull == gUAStyleSheet) {
InitUAStyleSheet();
}
/*
* Create the HTML document...
*/
1998-07-01 11:29:20 +00:00
rv = NS_NewHTMLDocument(&doc);
if (NS_OK != rv) {
goto done;
}
1998-07-01 11:29:20 +00:00
/*
* Create the HTML Content Viewer...
*/
rv = NS_NewDocumentViewer(docv);
if (NS_OK != rv) {
goto done;
}
docv->SetUAStyleSheet(gUAStyleSheet);
1998-07-01 11:29:20 +00:00
/*
* Initialize the document to begin loading the data...
*
* An nsIStreamListener connected to the parser is returned in
* aDocListener.
*/
rv = doc->StartDocumentLoad(aURL, aContainer, aDocListener);
1998-07-27 18:05:34 +00:00
if (NS_OK != rv) {
NS_IF_RELEASE(docv);
goto done;
}
/*
* Bind the document to the Content Viewer...
*/
rv = docv->BindToDocument(doc, aCommand);
*aDocViewer = docv;
done:
NS_IF_RELEASE(doc);
return rv;
}
nsresult
nsDocFactoryImpl::CreateImageDocument(nsIURL* aURL,
const char* aCommand,
nsIContentViewerContainer* aContainer,
nsIStreamListener** aDocListener,
nsIContentViewer** aDocViewer)
{
nsresult rv = NS_ERROR_FAILURE;
nsIDocument* doc = nsnull;
nsIDocumentViewer* docv = nsnull;
// Load the UA style sheet if we haven't already done that
if (nsnull == gUAStyleSheet) {
InitUAStyleSheet();
}
/*
* Create the image document...
*/
rv = NS_NewImageDocument(&doc);
if (NS_OK != rv) {
goto done;
}
/*
* Create the image content viewer...
*/
rv = NS_NewDocumentViewer(docv);
if (NS_OK != rv) {
goto done;
}
docv->SetUAStyleSheet(gUAStyleSheet);
/*
* Initialize the document to begin loading the data...
*
* An nsIStreamListener connected to the parser is returned in
* aDocListener.
*/
rv = doc->StartDocumentLoad(aURL, aContainer, aDocListener);
if (NS_OK != rv) {
NS_IF_RELEASE(docv);
goto done;
}
1998-07-01 11:29:20 +00:00
/*
* Bind the document to the Content Viewer...
*/
rv = docv->BindToDocument(doc, aCommand);
*aDocViewer = docv;
1998-07-01 11:29:20 +00:00
done:
NS_IF_RELEASE(doc);
1998-07-01 11:29:20 +00:00
return rv;
}
#define UA_CSS_URL "resource:/res/ua.css"
nsresult nsDocFactoryImpl::InitUAStyleSheet()
{
nsresult rv = NS_OK;
if (nsnull == gUAStyleSheet) { // snarf one
nsIURL* uaURL;
1998-08-05 04:23:21 +00:00
rv = NS_NewURL(&uaURL, nsnull, nsString(UA_CSS_URL)); // XXX this bites, fix it
if (NS_OK == rv) {
// Get an input stream from the url
PRInt32 ec;
nsIInputStream* in = uaURL->Open(&ec);
if (nsnull != in) {
// Translate the input using the argument character set id into unicode
nsIUnicharInputStream* uin;
rv = NS_NewConverterStream(&uin, nsnull, in);
if (NS_OK == rv) {
// Create parser and set it up to process the input file
nsICSSParser* css;
rv = NS_NewCSSParser(&css);
if (NS_OK == rv) {
// Parse the input and produce a style set
// XXX note: we are ignoring rv until the error code stuff in the
// input routines is converted to use nsresult's
css->Parse(uin, uaURL, gUAStyleSheet);
NS_RELEASE(css);
}
NS_RELEASE(uin);
}
NS_RELEASE(in);
}
else {
// printf("open of %s failed: error=%x\n", UA_CSS_URL, ec);
rv = NS_ERROR_ILLEGAL_VALUE; // XXX need a better error code here
}
NS_RELEASE(uaURL);
}
}
return rv;
}
1998-07-01 11:29:20 +00:00
/****************************************************************************
* nsDocLoaderImpl implementation...
****************************************************************************/
1998-07-01 11:29:20 +00:00
class nsDocLoaderImpl : public nsIDocumentLoader
{
public:
nsDocLoaderImpl(nsDocLoaderImpl* aParent);
1998-07-01 11:29:20 +00:00
NS_DECL_ISUPPORTS
NS_IMETHOD LoadURL(const nsString& aURLSpec,
const char *aCommand,
nsIContentViewerContainer* aContainer,
1998-07-01 11:29:20 +00:00
nsIPostData* aPostData = nsnull,
nsISupports* aExtraInfo = nsnull,
nsIStreamObserver* anObserver = nsnull);
NS_IMETHOD LoadURL(const nsString& aURLSpec,
nsIStreamListener* aListener);
NS_IMETHOD Stop(void);
NS_IMETHOD CreateDocumentLoader(nsIDocumentLoader** anInstance);
NS_IMETHOD SetDocumentFactory(nsIDocumentLoaderFactory* aFactory);
1998-07-01 11:29:20 +00:00
void LoadURLComplete(nsISupports* loader);
protected:
virtual ~nsDocLoaderImpl();
private:
static PRBool StopBindInfoEnumerator (nsISupports* aElement, void* aData);
static PRBool StopDocLoaderEnumerator(nsISupports* aElement, void* aData);
1998-07-01 11:29:20 +00:00
public:
nsIDocumentLoaderFactory* m_DocFactory;
protected:
nsISupportsArray* m_LoadingDocsList;
nsDocLoaderImpl* mParent;
nsISupportsArray* mChildDocLoaderList;
1998-07-01 11:29:20 +00:00
};
nsDocLoaderImpl::nsDocLoaderImpl(nsDocLoaderImpl* aParent)
1998-07-01 11:29:20 +00:00
{
NS_INIT_REFCNT();
NS_NewISupportsArray(&m_LoadingDocsList);
NS_NewISupportsArray(&mChildDocLoaderList);
mParent = aParent;
NS_IF_ADDREF(mParent);
1998-07-01 11:29:20 +00:00
m_DocFactory = new nsDocFactoryImpl();
NS_ADDREF(m_DocFactory);
}
nsDocLoaderImpl::~nsDocLoaderImpl()
{
Stop();
NS_IF_RELEASE(mParent);
NS_IF_RELEASE(mChildDocLoaderList);
1998-07-01 11:29:20 +00:00
NS_IF_RELEASE(m_LoadingDocsList);
NS_IF_RELEASE(m_DocFactory);
}
/*
* Implementation of ISupports methods...
*/
NS_IMPL_ADDREF(nsDocLoaderImpl);
NS_IMPL_RELEASE(nsDocLoaderImpl);
NS_IMETHODIMP
nsDocLoaderImpl::QueryInterface(REFNSIID aIID, void** aInstancePtr)
{
if (NULL == aInstancePtr) {
return NS_ERROR_NULL_POINTER;
}
if (aIID.Equals(kIDocumentLoaderIID)) {
*aInstancePtr = (void*)(nsIDocumentLoader*)this;
AddRef();
return NS_OK;
}
if (aIID.Equals(kDocLoaderImplIID)) {
*aInstancePtr = (void*)this;
AddRef();
return NS_OK;
}
return NS_NOINTERFACE;
}
NS_IMETHODIMP
nsDocLoaderImpl::CreateDocumentLoader(nsIDocumentLoader** anInstance)
{
nsDocLoaderImpl* newLoader = nsnull;
nsresult rv = NS_OK;
/* Check for initial error conditions... */
if (nsnull == anInstance) {
rv = NS_ERROR_NULL_POINTER;
goto done;
}
newLoader = new nsDocLoaderImpl(this);
if (nsnull == newLoader) {
rv = NS_ERROR_OUT_OF_MEMORY;
goto done;
}
rv = newLoader->QueryInterface(kIDocumentLoaderIID, (void**)anInstance);
mChildDocLoaderList->AppendElement(newLoader);
done:
return rv;
}
1998-07-01 11:29:20 +00:00
NS_IMETHODIMP
nsDocLoaderImpl::SetDocumentFactory(nsIDocumentLoaderFactory* aFactory)
{
NS_IF_RELEASE(m_DocFactory);
m_DocFactory = aFactory;
NS_IF_ADDREF(m_DocFactory);
return NS_OK;
}
NS_IMETHODIMP
nsDocLoaderImpl::LoadURL(const nsString& aURLSpec,
const char* aCommand,
nsIContentViewerContainer* aContainer,
nsIPostData* aPostData,
nsISupports* aExtraInfo,
nsIStreamObserver* anObserver)
1998-07-01 11:29:20 +00:00
{
nsresult rv;
nsDocumentBindInfo* loader = nsnull;
/* Check for initial error conditions... */
if (nsnull == aContainer) {
rv = NS_ERROR_NULL_POINTER;
goto done;
}
loader = new nsDocumentBindInfo(this, // DocLoader
aCommand, // Command
aContainer, // Viewer Container
aExtraInfo, // Extra Info
anObserver); // Observer
1998-07-01 11:29:20 +00:00
if (nsnull == loader) {
rv = NS_ERROR_OUT_OF_MEMORY;
goto done;
}
/* The DocumentBindInfo reference is only held by the Array... */
m_LoadingDocsList->AppendElement((nsIStreamListener *)loader);
1998-07-01 11:29:20 +00:00
rv = loader->Bind(aURLSpec, aPostData, nsnull);
1998-07-01 11:29:20 +00:00
done:
return rv;
}
NS_IMETHODIMP
nsDocLoaderImpl::LoadURL(const nsString& aURLSpec,
nsIStreamListener* aListener)
{
nsresult rv;
nsDocumentBindInfo* loader = nsnull;
/* Check for initial error conditions... */
if (nsnull == aListener) {
rv = NS_ERROR_NULL_POINTER;
goto done;
}
loader = new nsDocumentBindInfo(this, // DocLoader
nsnull, // Command
nsnull, // Viewer Container
nsnull, // Extra Info
nsnull); // Observer
if (nsnull == loader) {
rv = NS_ERROR_OUT_OF_MEMORY;
goto done;
}
/* The DocumentBindInfo reference is only held by the Array... */
m_LoadingDocsList->AppendElement(((nsISupports*)(nsIStreamObserver*)loader));
rv = loader->Bind(aURLSpec, nsnull, aListener);
done:
return rv;
}
NS_IMETHODIMP
nsDocLoaderImpl::Stop(void)
{
m_LoadingDocsList->EnumerateForwards(nsDocLoaderImpl::StopBindInfoEnumerator, nsnull);
/*
* Now the only reference to each nsDocumentBindInfo instance is held by
* Netlib via the nsIStreamListener interface...
*
* When each connection is aborted, Netlib will release its reference to
* the StreamListener and the DocumentBindInfo object will be deleted...
*/
m_LoadingDocsList->Clear();
/*
* Now Stop() all documents being loaded by child DocumentLoaders...
*/
mChildDocLoaderList->EnumerateForwards(nsDocLoaderImpl::StopDocLoaderEnumerator, nsnull);
return NS_OK;
}
void nsDocLoaderImpl::LoadURLComplete(nsISupports* aBindInfo)
1998-07-01 11:29:20 +00:00
{
PRBool rv;
rv = m_LoadingDocsList->RemoveElement(aBindInfo);
/*
* If the entry was not found in the list, then it must have been cancelled
* via Stop(...).
*/
#if defined(NS_DEBUG)
if (PR_TRUE != rv) {
nsDocumentBindInfo* docInfo;
1998-07-23 04:25:20 +00:00
rv = aBindInfo->QueryInterface(kDocumentBindInfoIID, (void**)&docInfo);
NS_ASSERTION(((NS_OK == rv) && (docInfo->GetStatus() == NS_BINDING_ABORTED)),
"Entry was not Aborted!");
}
#endif /* NS_DEBUG */
1998-07-01 11:29:20 +00:00
}
PRBool nsDocLoaderImpl::StopBindInfoEnumerator(nsISupports* aElement, void* aData)
{
nsresult rv;
nsDocumentBindInfo* bindInfo;
rv = aElement->QueryInterface(kDocumentBindInfoIID, (void**)&bindInfo);
if (NS_OK == rv) {
bindInfo->Stop();
}
return PR_TRUE;
}
PRBool nsDocLoaderImpl::StopDocLoaderEnumerator(nsISupports* aElement, void* aData)
{
nsresult rv;
nsDocLoaderImpl* docLoader;
rv = aElement->QueryInterface(kDocLoaderImplIID, (void**)&docLoader);
if (NS_OK == rv) {
docLoader->Stop();
}
return PR_TRUE;
}
1998-07-01 11:29:20 +00:00
/****************************************************************************
* nsDocumentBindInfo implementation...
****************************************************************************/
1998-07-01 11:29:20 +00:00
nsDocumentBindInfo::nsDocumentBindInfo(nsDocLoaderImpl* aDocLoader,
const char *aCommand,
nsIContentViewerContainer* aContainer,
1998-07-01 11:29:20 +00:00
nsISupports* aExtraInfo,
nsIStreamObserver* anObserver)
{
NS_INIT_REFCNT();
m_Url = nsnull;
m_NextStream = nsnull;
m_Command = (nsnull != aCommand) ? PL_strdup(aCommand) : nsnull;
m_ExtraInfo = nsnull;
m_DocLoader = aDocLoader;
NS_ADDREF(m_DocLoader);
m_Container = aContainer;
NS_IF_ADDREF(m_Container);
m_Observer = anObserver;
NS_IF_ADDREF(m_Observer);
m_NetSupport = NULL;
if (m_Observer) {
m_Observer->QueryInterface(kINetSupportIID, (void **) &m_NetSupport);
}
1998-07-01 11:29:20 +00:00
m_ExtraInfo = aExtraInfo;
NS_IF_ADDREF(m_ExtraInfo);
1998-07-24 21:03:33 +00:00
mStatus = NS_OK;
1998-07-01 11:29:20 +00:00
}
nsDocumentBindInfo::~nsDocumentBindInfo()
{
if (m_Command) {
PR_Free(m_Command);
}
m_Command = nsnull;
NS_RELEASE (m_DocLoader);
NS_IF_RELEASE(m_Url);
NS_IF_RELEASE(m_NextStream);
NS_IF_RELEASE(m_Container);
NS_IF_RELEASE(m_Observer);
NS_IF_RELEASE(m_NetSupport);
1998-07-01 11:29:20 +00:00
NS_IF_RELEASE(m_ExtraInfo);
}
/*
* Implementation of ISupports methods...
*/
NS_IMPL_ADDREF(nsDocumentBindInfo);
NS_IMPL_RELEASE(nsDocumentBindInfo);
1998-07-01 11:29:20 +00:00
nsresult
nsDocumentBindInfo::QueryInterface(const nsIID& aIID,
void** aInstancePtrResult)
{
NS_PRECONDITION(nsnull != aInstancePtrResult, "null pointer");
if (nsnull == aInstancePtrResult) {
return NS_ERROR_NULL_POINTER;
}
*aInstancePtrResult = NULL;
if (aIID.Equals(kIStreamObserverIID)) {
*aInstancePtrResult = (void*) ((nsIStreamObserver*)this);
AddRef();
return NS_OK;
}
if (aIID.Equals(kIStreamListenerIID)) {
*aInstancePtrResult = (void*) ((nsIStreamListener*)this);
AddRef();
return NS_OK;
}
if (aIID.Equals(kDocumentBindInfoIID)) {
*aInstancePtrResult = (void*) this;
AddRef();
return NS_OK;
}
if (aIID.Equals(kINetSupportIID)) {
*aInstancePtrResult = (void*) ((nsINetSupport*)this);
AddRef();
return NS_OK;
}
return NS_NOINTERFACE;
}
1998-07-01 11:29:20 +00:00
nsresult nsDocumentBindInfo::Bind(const nsString& aURLSpec,
nsIPostData* aPostData,
nsIStreamListener* aListener)
1998-07-01 11:29:20 +00:00
{
nsresult rv;
rv = NS_NewURL(&m_Url, aURLSpec);
if (NS_OK != rv) {
return rv;
}
/* Store any POST data into the URL */
if (nsnull != aPostData) {
static NS_DEFINE_IID(kPostToServerIID, NS_IPOSTTOSERVER_IID);
nsIPostToServer* pts;
rv = m_Url->QueryInterface(kPostToServerIID, (void **)&pts);
if (NS_OK == rv) {
const char* data = aPostData->GetData();
if (aPostData->IsFile()) {
pts->SendDataFromFile(data);
}
else {
pts->SendData(data, aPostData->GetDataLength());
}
}
}
/* Set up the stream listener (if provided)... */
if (nsnull != aListener) {
m_NextStream = aListener;
NS_ADDREF(m_NextStream);
}
1998-07-01 11:29:20 +00:00
/* Start the URL binding process... */
rv = m_Url->Open(this);
return rv;
}
nsresult nsDocumentBindInfo::Stop(void)
{
mStatus = NS_BINDING_ABORTED;
return NS_OK;
}
NS_METHOD nsDocumentBindInfo::GetBindInfo(nsIURL* aURL)
1998-07-01 11:29:20 +00:00
{
nsresult rv = NS_OK;
NS_PRECONDITION(nsnull !=m_NextStream, "DocLoader: No stream for document");
if (nsnull != m_NextStream) {
rv = m_NextStream->GetBindInfo(aURL);
1998-07-01 11:29:20 +00:00
}
return rv;
}
NS_METHOD nsDocumentBindInfo::OnProgress(nsIURL* aURL, PRInt32 aProgress,
PRInt32 aProgressMax)
1998-07-01 11:29:20 +00:00
{
nsresult rv = NS_OK;
/* Pass the notification out to the next stream listener... */
if (nsnull != m_NextStream) {
rv = m_NextStream->OnProgress(aURL, aProgress, aProgressMax);
1998-07-01 11:29:20 +00:00
}
/* Pass the notification out to the Observer... */
if (nsnull != m_Observer) {
/* XXX: Should we ignore the return value? */
(void) m_Observer->OnProgress(aURL, aProgress, aProgressMax);
}
return rv;
}
NS_METHOD nsDocumentBindInfo::OnStatus(nsIURL* aURL, const nsString& aMsg)
{
nsresult rv = NS_OK;
/* Pass the notification out to the next stream listener... */
if (nsnull != m_NextStream) {
rv = m_NextStream->OnStatus(aURL, aMsg);
}
/* Pass the notification out to the Observer... */
if (nsnull != m_Observer) {
/* XXX: Should we ignore the return value? */
(void) m_Observer->OnStatus(aURL, aMsg);
1998-07-01 11:29:20 +00:00
}
return rv;
}
NS_METHOD nsDocumentBindInfo::OnStartBinding(nsIURL* aURL, const char *aContentType)
1998-07-01 11:29:20 +00:00
{
nsresult rv = NS_OK;
nsIContentViewer* viewer = nsnull;
1998-07-01 11:29:20 +00:00
/* If the binding has been canceled via Stop() then abort the load... */
if (NS_BINDING_ABORTED == mStatus) {
rv = NS_BINDING_ABORTED;
1998-07-01 11:29:20 +00:00
goto done;
}
if (nsnull == m_NextStream) {
/*
* Now that the content type is available, create a document (and viewer)
* of the appropriate type...
*/
if (m_DocLoader->m_DocFactory) {
rv = m_DocLoader->m_DocFactory->CreateInstance(m_Url,
aContentType,
m_Command,
m_Container,
&m_NextStream,
&viewer);
} else {
rv = NS_ERROR_NULL_POINTER;
}
if (NS_OK != rv) {
goto done;
}
/*
* Give the document container the new viewer...
*/
viewer->SetContainer(m_Container);
rv = m_Container->Embed(viewer, m_Command, m_ExtraInfo);
if (NS_OK != rv) {
goto done;
}
1998-07-01 11:29:20 +00:00
}
/*
* Pass the OnStartBinding(...) notification out to the document
* IStreamListener.
*/
NS_ASSERTION((nsnull != m_NextStream), "No stream was created!");
1998-07-01 11:29:20 +00:00
if (nsnull != m_NextStream) {
rv = m_NextStream->OnStartBinding(aURL, aContentType);
1998-07-01 11:29:20 +00:00
}
/* Pass the notification out to the Observer... */
if (nsnull != m_Observer) {
/* XXX: Should we ignore the return value? */
(void) m_Observer->OnStartBinding(aURL, aContentType);
1998-07-01 11:29:20 +00:00
}
done:
NS_IF_RELEASE(viewer);
1998-07-01 11:29:20 +00:00
return rv;
}
NS_METHOD nsDocumentBindInfo::OnDataAvailable(nsIURL* aURL,
nsIInputStream *aStream, PRInt32 aLength)
1998-07-01 11:29:20 +00:00
{
nsresult rv = NS_OK;
/* If the binding has been canceled via Stop() then abort the load... */
if (NS_BINDING_ABORTED == mStatus) {
rv = NS_BINDING_ABORTED;
goto done;
}
1998-07-01 11:29:20 +00:00
NS_PRECONDITION(nsnull !=m_NextStream, "DocLoader: No stream for document");
1998-07-01 11:29:20 +00:00
if (nsnull != m_NextStream) {
rv = m_NextStream->OnDataAvailable(aURL, aStream, aLength);
1998-07-01 11:29:20 +00:00
}
done:
1998-07-01 11:29:20 +00:00
return rv;
}
NS_METHOD nsDocumentBindInfo::OnStopBinding(nsIURL* aURL, PRInt32 aStatus,
const nsString& aMsg)
1998-07-01 11:29:20 +00:00
{
nsresult rv = NS_OK;
if (nsnull != m_NextStream) {
rv = m_NextStream->OnStopBinding(aURL, aStatus, aMsg);
1998-07-01 11:29:20 +00:00
}
/* Pass the notification out to the Observer... */
if (nsnull != m_Observer) {
/* XXX: Should we ignore the return value? */
(void) m_Observer->OnStopBinding(aURL, aStatus, aMsg);
1998-07-01 11:29:20 +00:00
}
/*
* The stream is complete... Tell the DocumentLoader to release us...
*/
m_DocLoader->LoadURLComplete((nsIStreamListener *)this);
1998-07-01 11:29:20 +00:00
return rv;
}
NS_IMETHODIMP_(void)
nsDocumentBindInfo::Alert(const nsString &aText)
{
if (nsnull != m_NetSupport) {
m_NetSupport->Alert(aText);
}
}
NS_IMETHODIMP_(PRBool)
nsDocumentBindInfo::Confirm(const nsString &aText)
{
if (nsnull != m_NetSupport) {
return m_NetSupport->Confirm(aText);
}
return PR_FALSE;
}
NS_IMETHODIMP_(PRBool)
nsDocumentBindInfo::Prompt(const nsString &aText,
const nsString &aDefault,
nsString &aResult)
{
if (nsnull != m_NetSupport) {
return m_NetSupport->Prompt(aText, aDefault, aResult);
}
return PR_FALSE;
}
NS_IMETHODIMP_(PRBool)
nsDocumentBindInfo::PromptUserAndPassword(const nsString &aText,
nsString &aUser,
nsString &aPassword)
{
if (nsnull != m_NetSupport) {
return m_NetSupport->PromptUserAndPassword(aText, aUser, aPassword);
}
return PR_FALSE;
}
NS_IMETHODIMP_(PRBool)
nsDocumentBindInfo::PromptPassword(const nsString &aText,
nsString &aPassword)
{
if (nsnull != m_NetSupport) {
return m_NetSupport->PromptPassword(aText, aPassword);
}
return PR_FALSE;
}
1998-07-01 11:29:20 +00:00
/*******************************************
1998-07-03 23:14:21 +00:00
* nsDocLoaderFactory
*******************************************/
1998-07-01 11:29:20 +00:00
static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
static NS_DEFINE_IID(kCDocumentLoader, NS_DOCUMENTLOADER_CID);
class nsDocumentLoaderFactory : public nsIFactory
1998-07-19 01:18:15 +00:00
{
public:
1998-07-01 11:29:20 +00:00
nsDocumentLoaderFactory();
NS_DECL_ISUPPORTS
1998-07-19 01:18:15 +00:00
// nsIFactory methods
NS_IMETHOD CreateInstance(nsISupports *aOuter,
const nsIID &aIID,
void **aResult);
1998-07-01 11:29:20 +00:00
1998-07-19 01:18:15 +00:00
NS_IMETHOD LockFactory(PRBool aLock);
1998-07-01 11:29:20 +00:00
protected:
1998-07-19 01:18:15 +00:00
virtual ~nsDocumentLoaderFactory();
};
1998-07-01 11:29:20 +00:00
nsDocumentLoaderFactory::nsDocumentLoaderFactory()
1998-07-19 01:18:15 +00:00
{
1998-07-01 11:29:20 +00:00
NS_INIT_REFCNT();
1998-07-19 01:18:15 +00:00
}
1998-07-01 11:29:20 +00:00
1998-07-19 01:18:15 +00:00
nsDocumentLoaderFactory::~nsDocumentLoaderFactory()
{
}
1998-07-01 11:29:20 +00:00
/*
* Implementation of ISupports methods...
*/
NS_IMPL_ISUPPORTS(nsDocumentLoaderFactory,kIFactoryIID);
NS_IMETHODIMP
1998-07-19 01:18:15 +00:00
nsDocumentLoaderFactory::CreateInstance(nsISupports* aOuter,
const nsIID& aIID,
void** aResult)
{
1998-07-01 11:29:20 +00:00
nsresult rv;
nsIDocumentLoader* inst;
static nsDocLoaderImpl* gGlobalDocLoader = nsnull;
1998-07-01 11:29:20 +00:00
1998-07-19 01:18:15 +00:00
if (nsnull == aResult) {
1998-07-01 11:29:20 +00:00
rv = NS_ERROR_NULL_POINTER;
goto done;
1998-07-19 01:18:15 +00:00
}
1998-07-01 11:29:20 +00:00
*aResult = nsnull;
if (nsnull != aOuter) {
rv = NS_ERROR_NO_AGGREGATION;
goto done;
}
if (nsnull == gGlobalDocLoader) {
gGlobalDocLoader = new nsDocLoaderImpl(nsnull);
if (nsnull == gGlobalDocLoader) {
rv = NS_ERROR_OUT_OF_MEMORY;
goto done;
}
NS_ADDREF(gGlobalDocLoader); // RefCount = 1
}
rv = gGlobalDocLoader->CreateDocumentLoader(&inst);
if (NS_OK != rv) {
1998-07-01 11:29:20 +00:00
rv = NS_ERROR_OUT_OF_MEMORY;
goto done;
1998-07-19 01:18:15 +00:00
}
*aResult = inst;
1998-07-01 11:29:20 +00:00
done:
return rv;
1998-07-19 01:18:15 +00:00
}
1998-07-01 11:29:20 +00:00
NS_IMETHODIMP
1998-07-19 01:18:15 +00:00
nsDocumentLoaderFactory::LockFactory(PRBool aLock)
{
// Not implemented in simplest case.
return NS_OK;
}
1998-07-01 11:29:20 +00:00
1998-07-19 01:18:15 +00:00
extern "C" NS_WEB nsresult
NS_NewDocumentLoaderFactory(nsIFactory** aFactory)
1998-07-01 11:29:20 +00:00
{
if (nsnull == aFactory) {
return NS_ERROR_NULL_POINTER;
}
*aFactory = new nsDocumentLoaderFactory();
if (nsnull == *aFactory) {
return NS_ERROR_OUT_OF_MEMORY;
}
NS_ADDREF(*aFactory);
return NS_OK;
}