Reworked Post data to avoid memory leaks... Removed nsIDocument::LoadURL(...). Added nsIDocument::StartDocumentLoad(...) as a callback method for the DocumentLoader...

This commit is contained in:
rpotts 1998-07-01 11:16:09 +00:00
Родитель 00765c5325
Коммит 1afdd4f963
10 изменённых файлов: 226 добавлений и 98 удалений

Просмотреть файл

@ -31,6 +31,7 @@ class nsIPresContext;
class nsIPresShell;
class nsISelection;
class nsIStreamListener;
class nsIStreamObserver;
class nsIStyleSet;
class nsIStyleSheet;
class nsIURL;
@ -47,10 +48,16 @@ class nsIDeviceContext;
{0x93, 0x23, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32} }
// specification for data to be sent via form "post"
class nsIPostData {
// IID for the nsIPostData interface - 152ab6e0-ff13-11d1-beb9-00805f8a66dc
#define NS_IPOSTDATA_IID \
{ 0x152ab6e0, 0xff13, 0x11d1, \
{0xbe, 0xb9, 0x00, 0x80, 0x5f, 0x8a, 0x66, 0xdc} }
class nsIPostData : public nsISupports {
public:
virtual PRBool IsFile() = 0; // is the data a file (or raw data)
virtual const char* GetData() = 0; // get the file name or raw data
virtual PRInt32 GetDataLength() = 0;
};
//----------------------------------------------------------------------
@ -63,10 +70,9 @@ public:
// returns the arena associated with this document.
virtual nsIArena* GetArena() = 0;
NS_IMETHOD LoadURL(nsIURL* aURL,
nsIStreamListener* aListener,
nsIWebWidget* aWebWidget,
nsIPostData* aPostData = 0) = 0;
NS_IMETHOD StartDocumentLoad(nsIURL *aUrl,
nsIWebWidget* aWebWidget,
nsIStreamListener **aDocListener) = 0;
/**
* Return the title of the document. May return null.
@ -197,7 +203,11 @@ public:
// XXX Belongs somewhere else
extern NS_LAYOUT nsresult
NS_NewHTMLDocument(nsIDocument** aInstancePtrResult);
// Note: The buffer passed into NewPostData(...) becomes owned by the IPostData
// instance and is freed when the instance is destroyed...
//
extern NS_LAYOUT nsresult
NS_NewPostData(nsIPostData* aPostData, nsIPostData** aInstancePtrResult);
NS_NewPostData(PRBool aIsFile, char *aData, nsIPostData** aInstancePtrResult);
#endif /* nsIDocument_h___ */

Просмотреть файл

@ -15,6 +15,8 @@
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "plstr.h"
#include "nsDocument.h"
#include "nsIArena.h"
#include "nsIURL.h"
@ -45,28 +47,75 @@ static NS_DEFINE_IID(kIDOMEventCapturerIID, NS_IDOMEVENTCAPTURER_IID);
static NS_DEFINE_IID(kIDOMEventReceiverIID, NS_IDOMEVENTRECEIVER_IID);
static NS_DEFINE_IID(kIEventListenerManagerIID, NS_IEVENTLISTENERMANAGER_IID);
static NS_DEFINE_IID(kIPostDataIID, NS_IPOSTDATA_IID);
NS_LAYOUT nsresult
NS_NewPostData(nsIPostData* aPostData, nsIPostData** aInstancePtrResult)
NS_NewPostData(PRBool aIsFile, char* aData,
nsIPostData** aInstancePtrResult)
{
*aInstancePtrResult = new nsPostData(aPostData);
return NS_OK;
nsresult rv = NS_OK;
if (nsnull == aInstancePtrResult) {
return NS_ERROR_NULL_POINTER;
}
*aInstancePtrResult = new nsPostData(aIsFile, aData);
if (nsnull != *aInstancePtrResult) {
NS_ADDREF(*aInstancePtrResult);
} else {
rv = NS_ERROR_OUT_OF_MEMORY;
}
return rv;
}
nsPostData::nsPostData(nsIPostData* aPostData)
nsPostData::nsPostData(PRBool aIsFile, char* aData)
{
mIsFile = PR_FALSE;
mData = nsnull;
if (aPostData) {
mIsFile = aPostData->IsFile();
const char* data = aPostData->GetData();
if (data) {
PRInt32 len = strlen(data);
mData = new char[len+1];
strcpy(mData, data);
}
NS_INIT_REFCNT();
mData = nsnull;
mDataLen = 0;
mIsFile = aIsFile;
if (aData) {
mDataLen = PL_strlen(aData);
mData = aData;
}
}
nsPostData::~nsPostData()
{
if (nsnull != mData) {
delete [] mData;
mData = nsnull;
}
}
/*
* Implementation of ISupports methods...
*/
NS_IMPL_ISUPPORTS(nsPostData,kIPostDataIID);
PRBool nsPostData::IsFile()
{
return mIsFile;
}
const char* nsPostData::GetData()
{
return mData;
}
PRInt32 nsPostData::GetDataLength()
{
return mDataLen;
}
nsDocument::nsDocument()
{
NS_INIT_REFCNT();
@ -483,6 +532,7 @@ nsresult nsDocument::ResetScriptObject()
return NS_OK;
}
//
// nsIDOMDocument interface
//

Просмотреть файл

@ -30,15 +30,23 @@ class nsIEventListenerManager;
class nsPostData : public nsIPostData {
public:
nsPostData(PRBool aIsFile, char* aData) : mIsFile(aIsFile), mData(aData) {}
nsPostData(nsIPostData* aPostData);
PRBool IsFile() { return mIsFile; }
const char* GetData() { return mData; }
nsPostData(PRBool aIsFile, char* aData);
NS_DECL_ISUPPORTS
virtual PRBool IsFile();
virtual const char* GetData();
virtual PRInt32 GetDataLength();
protected:
PRBool mIsFile;
char* mData;
virtual ~nsPostData();
PRBool mIsFile;
char* mData;
PRInt32 mDataLen;
};
// Base class for our document implementations
class nsDocument : public nsIDocument, public nsIDOMDocument, public nsIScriptObjectOwner, public nsIDOMEventCapturer {
public:

Просмотреть файл

@ -31,6 +31,7 @@
#include "nsIDOMElement.h"
#include "nsIDOMText.h"
#include "nsIPostToServer.h"
#include "nsIStreamListener.h"
#include "nsIURL.h"
#include "nsIWebWidget.h"
@ -74,9 +75,11 @@ NS_IMETHODIMP nsHTMLDocument::QueryInterface(REFNSIID aIID,
return nsDocument::QueryInterface(aIID, aInstancePtr);
}
NS_IMETHODIMP
nsHTMLDocument::LoadURL(nsIURL* aURL, nsIStreamListener* aListener,
nsIWebWidget* aWebWidget, nsIPostData* aPostData)
nsHTMLDocument::StartDocumentLoad(nsIURL *aURL,
nsIWebWidget* aWebWidget,
nsIStreamListener **aDocListener)
{
// Delete references to style sheets - this should be done in superclass...
PRInt32 index = mStyleSheets.Count();
@ -96,21 +99,6 @@ nsHTMLDocument::LoadURL(nsIURL* aURL, nsIStreamListener* aListener,
mDocumentURL = aURL;
NS_ADDREF(aURL);
static NS_DEFINE_IID(kPostToServerIID, NS_IPOSTTOSERVER_IID);
if (aPostData) {
const char* data = aPostData->GetData();
if (data) {
nsIPostToServer* pts;
nsresult result = aURL->QueryInterface(kPostToServerIID, (void **)&pts);
if (aPostData->IsFile()) {
pts->SendDataFromFile(data);
}
else {
pts->SendData(data, strlen(data));
}
}
}
nsIParser* parser;
nsresult rv = NS_NewParser(&parser);
if (NS_OK == rv) {
@ -126,8 +114,14 @@ nsHTMLDocument::LoadURL(nsIURL* aURL, nsIStreamListener* aListener,
AddStyleSheet(mAttrStyleSheet); // tell the world about our new style sheet
}
parser->SetContentSink(sink);
parser->Parse(aURL, aListener);
// Set the parser as the stream listener for the document loader...
static NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID);
rv = parser->QueryInterface(kIStreamListenerIID, (void**)aDocListener);
if (NS_OK == rv) {
parser->SetContentSink(sink);
parser->BeginParse(aURL);
}
NS_RELEASE(sink);
}
NS_RELEASE(parser);
@ -136,6 +130,8 @@ nsHTMLDocument::LoadURL(nsIURL* aURL, nsIStreamListener* aListener,
return rv;
}
static NS_DEFINE_IID(kIDocumentObserverIID, NS_IDOCUMENT_OBSERVER_IID);
NS_IMETHODIMP nsHTMLDocument::SetTitle(const nsString& aTitle)

Просмотреть файл

@ -22,6 +22,7 @@
#include "nsIHTMLDocument.h"
class nsIHTMLStyleSheet;
class nsIViewDocument;
class nsIWebWidget;
class nsHTMLDocument : public nsDocument {
@ -31,10 +32,9 @@ public:
NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr);
NS_IMETHOD LoadURL(nsIURL* aURL,
nsIStreamListener* aListener,
nsIWebWidget* aWebWidget,
nsIPostData* aPostData = 0);
NS_IMETHOD StartDocumentLoad(nsIURL* aUrl,
nsIWebWidget* aWebWidget,
nsIStreamListener** aDocListener);
NS_IMETHOD SetTitle(const nsString& aTitle);

Просмотреть файл

@ -31,6 +31,7 @@ class nsIPresContext;
class nsIPresShell;
class nsISelection;
class nsIStreamListener;
class nsIStreamObserver;
class nsIStyleSet;
class nsIStyleSheet;
class nsIURL;
@ -47,10 +48,16 @@ class nsIDeviceContext;
{0x93, 0x23, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32} }
// specification for data to be sent via form "post"
class nsIPostData {
// IID for the nsIPostData interface - 152ab6e0-ff13-11d1-beb9-00805f8a66dc
#define NS_IPOSTDATA_IID \
{ 0x152ab6e0, 0xff13, 0x11d1, \
{0xbe, 0xb9, 0x00, 0x80, 0x5f, 0x8a, 0x66, 0xdc} }
class nsIPostData : public nsISupports {
public:
virtual PRBool IsFile() = 0; // is the data a file (or raw data)
virtual const char* GetData() = 0; // get the file name or raw data
virtual PRInt32 GetDataLength() = 0;
};
//----------------------------------------------------------------------
@ -63,10 +70,9 @@ public:
// returns the arena associated with this document.
virtual nsIArena* GetArena() = 0;
NS_IMETHOD LoadURL(nsIURL* aURL,
nsIStreamListener* aListener,
nsIWebWidget* aWebWidget,
nsIPostData* aPostData = 0) = 0;
NS_IMETHOD StartDocumentLoad(nsIURL *aUrl,
nsIWebWidget* aWebWidget,
nsIStreamListener **aDocListener) = 0;
/**
* Return the title of the document. May return null.
@ -197,7 +203,11 @@ public:
// XXX Belongs somewhere else
extern NS_LAYOUT nsresult
NS_NewHTMLDocument(nsIDocument** aInstancePtrResult);
// Note: The buffer passed into NewPostData(...) becomes owned by the IPostData
// instance and is freed when the instance is destroyed...
//
extern NS_LAYOUT nsresult
NS_NewPostData(nsIPostData* aPostData, nsIPostData** aInstancePtrResult);
NS_NewPostData(PRBool aIsFile, char *aData, nsIPostData** aInstancePtrResult);
#endif /* nsIDocument_h___ */

Просмотреть файл

@ -15,6 +15,8 @@
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "plstr.h"
#include "nsDocument.h"
#include "nsIArena.h"
#include "nsIURL.h"
@ -45,28 +47,75 @@ static NS_DEFINE_IID(kIDOMEventCapturerIID, NS_IDOMEVENTCAPTURER_IID);
static NS_DEFINE_IID(kIDOMEventReceiverIID, NS_IDOMEVENTRECEIVER_IID);
static NS_DEFINE_IID(kIEventListenerManagerIID, NS_IEVENTLISTENERMANAGER_IID);
static NS_DEFINE_IID(kIPostDataIID, NS_IPOSTDATA_IID);
NS_LAYOUT nsresult
NS_NewPostData(nsIPostData* aPostData, nsIPostData** aInstancePtrResult)
NS_NewPostData(PRBool aIsFile, char* aData,
nsIPostData** aInstancePtrResult)
{
*aInstancePtrResult = new nsPostData(aPostData);
return NS_OK;
nsresult rv = NS_OK;
if (nsnull == aInstancePtrResult) {
return NS_ERROR_NULL_POINTER;
}
*aInstancePtrResult = new nsPostData(aIsFile, aData);
if (nsnull != *aInstancePtrResult) {
NS_ADDREF(*aInstancePtrResult);
} else {
rv = NS_ERROR_OUT_OF_MEMORY;
}
return rv;
}
nsPostData::nsPostData(nsIPostData* aPostData)
nsPostData::nsPostData(PRBool aIsFile, char* aData)
{
mIsFile = PR_FALSE;
mData = nsnull;
if (aPostData) {
mIsFile = aPostData->IsFile();
const char* data = aPostData->GetData();
if (data) {
PRInt32 len = strlen(data);
mData = new char[len+1];
strcpy(mData, data);
}
NS_INIT_REFCNT();
mData = nsnull;
mDataLen = 0;
mIsFile = aIsFile;
if (aData) {
mDataLen = PL_strlen(aData);
mData = aData;
}
}
nsPostData::~nsPostData()
{
if (nsnull != mData) {
delete [] mData;
mData = nsnull;
}
}
/*
* Implementation of ISupports methods...
*/
NS_IMPL_ISUPPORTS(nsPostData,kIPostDataIID);
PRBool nsPostData::IsFile()
{
return mIsFile;
}
const char* nsPostData::GetData()
{
return mData;
}
PRInt32 nsPostData::GetDataLength()
{
return mDataLen;
}
nsDocument::nsDocument()
{
NS_INIT_REFCNT();
@ -483,6 +532,7 @@ nsresult nsDocument::ResetScriptObject()
return NS_OK;
}
//
// nsIDOMDocument interface
//

Просмотреть файл

@ -30,15 +30,23 @@ class nsIEventListenerManager;
class nsPostData : public nsIPostData {
public:
nsPostData(PRBool aIsFile, char* aData) : mIsFile(aIsFile), mData(aData) {}
nsPostData(nsIPostData* aPostData);
PRBool IsFile() { return mIsFile; }
const char* GetData() { return mData; }
nsPostData(PRBool aIsFile, char* aData);
NS_DECL_ISUPPORTS
virtual PRBool IsFile();
virtual const char* GetData();
virtual PRInt32 GetDataLength();
protected:
PRBool mIsFile;
char* mData;
virtual ~nsPostData();
PRBool mIsFile;
char* mData;
PRInt32 mDataLen;
};
// Base class for our document implementations
class nsDocument : public nsIDocument, public nsIDOMDocument, public nsIScriptObjectOwner, public nsIDOMEventCapturer {
public:

Просмотреть файл

@ -31,6 +31,7 @@
#include "nsIDOMElement.h"
#include "nsIDOMText.h"
#include "nsIPostToServer.h"
#include "nsIStreamListener.h"
#include "nsIURL.h"
#include "nsIWebWidget.h"
@ -74,9 +75,11 @@ NS_IMETHODIMP nsHTMLDocument::QueryInterface(REFNSIID aIID,
return nsDocument::QueryInterface(aIID, aInstancePtr);
}
NS_IMETHODIMP
nsHTMLDocument::LoadURL(nsIURL* aURL, nsIStreamListener* aListener,
nsIWebWidget* aWebWidget, nsIPostData* aPostData)
nsHTMLDocument::StartDocumentLoad(nsIURL *aURL,
nsIWebWidget* aWebWidget,
nsIStreamListener **aDocListener)
{
// Delete references to style sheets - this should be done in superclass...
PRInt32 index = mStyleSheets.Count();
@ -96,21 +99,6 @@ nsHTMLDocument::LoadURL(nsIURL* aURL, nsIStreamListener* aListener,
mDocumentURL = aURL;
NS_ADDREF(aURL);
static NS_DEFINE_IID(kPostToServerIID, NS_IPOSTTOSERVER_IID);
if (aPostData) {
const char* data = aPostData->GetData();
if (data) {
nsIPostToServer* pts;
nsresult result = aURL->QueryInterface(kPostToServerIID, (void **)&pts);
if (aPostData->IsFile()) {
pts->SendDataFromFile(data);
}
else {
pts->SendData(data, strlen(data));
}
}
}
nsIParser* parser;
nsresult rv = NS_NewParser(&parser);
if (NS_OK == rv) {
@ -126,8 +114,14 @@ nsHTMLDocument::LoadURL(nsIURL* aURL, nsIStreamListener* aListener,
AddStyleSheet(mAttrStyleSheet); // tell the world about our new style sheet
}
parser->SetContentSink(sink);
parser->Parse(aURL, aListener);
// Set the parser as the stream listener for the document loader...
static NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID);
rv = parser->QueryInterface(kIStreamListenerIID, (void**)aDocListener);
if (NS_OK == rv) {
parser->SetContentSink(sink);
parser->BeginParse(aURL);
}
NS_RELEASE(sink);
}
NS_RELEASE(parser);
@ -136,6 +130,8 @@ nsHTMLDocument::LoadURL(nsIURL* aURL, nsIStreamListener* aListener,
return rv;
}
static NS_DEFINE_IID(kIDocumentObserverIID, NS_IDOCUMENT_OBSERVER_IID);
NS_IMETHODIMP nsHTMLDocument::SetTitle(const nsString& aTitle)

Просмотреть файл

@ -22,6 +22,7 @@
#include "nsIHTMLDocument.h"
class nsIHTMLStyleSheet;
class nsIViewDocument;
class nsIWebWidget;
class nsHTMLDocument : public nsDocument {
@ -31,10 +32,9 @@ public:
NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr);
NS_IMETHOD LoadURL(nsIURL* aURL,
nsIStreamListener* aListener,
nsIWebWidget* aWebWidget,
nsIPostData* aPostData = 0);
NS_IMETHOD StartDocumentLoad(nsIURL* aUrl,
nsIWebWidget* aWebWidget,
nsIStreamListener** aDocListener);
NS_IMETHOD SetTitle(const nsString& aTitle);