From 55f7be5e3e07b978ed669e9cd1aeb568f244a78a Mon Sep 17 00:00:00 2001 From: peterl Date: Thu, 16 Apr 1998 21:48:02 +0000 Subject: [PATCH] changed SetContentSink to do proper ref counting --- htmlparser/src/nsHTMLParser.cpp | 19 ++++++++++++++----- htmlparser/src/nsHTMLParser.h | 2 +- htmlparser/src/nsIParser.h | 2 +- parser/htmlparser/src/nsHTMLParser.cpp | 19 ++++++++++++++----- parser/htmlparser/src/nsHTMLParser.h | 2 +- parser/htmlparser/src/nsIParser.h | 2 +- 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/htmlparser/src/nsHTMLParser.cpp b/htmlparser/src/nsHTMLParser.cpp index 463c8066df1..5ff79edd0bb 100644 --- a/htmlparser/src/nsHTMLParser.cpp +++ b/htmlparser/src/nsHTMLParser.cpp @@ -31,6 +31,7 @@ static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); static NS_DEFINE_IID(kClassIID, NS_IHTML_PARSER_IID); static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID); +static NS_DEFINE_IID(kIHTMLContentSinkIID, NS_IHTMLCONTENTSINK_IID); /**------------------------------------------------------- @@ -107,6 +108,7 @@ nsHTMLParser::~nsHTMLParser() { if(mDTD) delete mDTD; mDTD=0; + NS_IF_RELEASE(mSink); } @@ -270,13 +272,20 @@ CDefaultTokenHandler* nsHTMLParser::AddTokenHandler(CDefaultTokenHandler* aHandl * @param nsIContentSink interface for node receiver * @return *------------------------------------------------------*/ -nsIContentSink* nsHTMLParser::SetContentSink(nsIContentSink* aSink) { +void nsHTMLParser::SetContentSink(nsIContentSink* aSink) { NS_PRECONDITION(0!=aSink,"sink cannot be null!"); - nsIContentSink* old=mSink; if(aSink) { - mSink=(nsHTMLContentSink*)(aSink); + nsIHTMLContentSink* htmlSink; + if (NS_OK == aSink->QueryInterface(kIHTMLContentSinkIID, (void**)&htmlSink)) { + if ((nsHTMLContentSink*)(htmlSink) != mSink) { + NS_IF_RELEASE(mSink); + mSink = (nsHTMLContentSink*)(htmlSink); + } + else { + NS_RELEASE(htmlSink); + } + } } - return old; } /**------------------------------------------------------- @@ -301,7 +310,7 @@ PRBool nsHTMLParser::Parse(nsIURL* aURL){ mTokenizer=new CTokenizer(aURL, delegate); mTokenizer->Tokenize(); -#define VERBOSE_DEBUG +//#define VERBOSE_DEBUG #ifdef VERBOSE_DEBUG mTokenizer->DebugDumpTokens(cout); #endif diff --git a/htmlparser/src/nsHTMLParser.h b/htmlparser/src/nsHTMLParser.h index 4091bcc4f54..074cb85ce8c 100644 --- a/htmlparser/src/nsHTMLParser.h +++ b/htmlparser/src/nsHTMLParser.h @@ -82,7 +82,7 @@ friend class CDefaultTokenHandler; NS_DECL_ISUPPORTS nsHTMLParser(); ~nsHTMLParser(); - virtual nsIContentSink* SetContentSink(nsIContentSink* aSink); + virtual void SetContentSink(nsIContentSink* aSink); virtual PRBool Parse(nsIURL* aURL); virtual PRBool ResumeParse(); diff --git a/htmlparser/src/nsIParser.h b/htmlparser/src/nsIParser.h index 82b82b15008..96e2e8e4411 100644 --- a/htmlparser/src/nsIParser.h +++ b/htmlparser/src/nsIParser.h @@ -51,7 +51,7 @@ class nsIParser : public nsISupports { public: - virtual nsIContentSink* SetContentSink(nsIContentSink* aContentSink)=0; + virtual void SetContentSink(nsIContentSink* aContentSink)=0; virtual PRBool Parse(nsIURL* aURL)=0; virtual PRBool ResumeParse()=0; }; diff --git a/parser/htmlparser/src/nsHTMLParser.cpp b/parser/htmlparser/src/nsHTMLParser.cpp index 463c8066df1..5ff79edd0bb 100644 --- a/parser/htmlparser/src/nsHTMLParser.cpp +++ b/parser/htmlparser/src/nsHTMLParser.cpp @@ -31,6 +31,7 @@ static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); static NS_DEFINE_IID(kClassIID, NS_IHTML_PARSER_IID); static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID); +static NS_DEFINE_IID(kIHTMLContentSinkIID, NS_IHTMLCONTENTSINK_IID); /**------------------------------------------------------- @@ -107,6 +108,7 @@ nsHTMLParser::~nsHTMLParser() { if(mDTD) delete mDTD; mDTD=0; + NS_IF_RELEASE(mSink); } @@ -270,13 +272,20 @@ CDefaultTokenHandler* nsHTMLParser::AddTokenHandler(CDefaultTokenHandler* aHandl * @param nsIContentSink interface for node receiver * @return *------------------------------------------------------*/ -nsIContentSink* nsHTMLParser::SetContentSink(nsIContentSink* aSink) { +void nsHTMLParser::SetContentSink(nsIContentSink* aSink) { NS_PRECONDITION(0!=aSink,"sink cannot be null!"); - nsIContentSink* old=mSink; if(aSink) { - mSink=(nsHTMLContentSink*)(aSink); + nsIHTMLContentSink* htmlSink; + if (NS_OK == aSink->QueryInterface(kIHTMLContentSinkIID, (void**)&htmlSink)) { + if ((nsHTMLContentSink*)(htmlSink) != mSink) { + NS_IF_RELEASE(mSink); + mSink = (nsHTMLContentSink*)(htmlSink); + } + else { + NS_RELEASE(htmlSink); + } + } } - return old; } /**------------------------------------------------------- @@ -301,7 +310,7 @@ PRBool nsHTMLParser::Parse(nsIURL* aURL){ mTokenizer=new CTokenizer(aURL, delegate); mTokenizer->Tokenize(); -#define VERBOSE_DEBUG +//#define VERBOSE_DEBUG #ifdef VERBOSE_DEBUG mTokenizer->DebugDumpTokens(cout); #endif diff --git a/parser/htmlparser/src/nsHTMLParser.h b/parser/htmlparser/src/nsHTMLParser.h index 4091bcc4f54..074cb85ce8c 100644 --- a/parser/htmlparser/src/nsHTMLParser.h +++ b/parser/htmlparser/src/nsHTMLParser.h @@ -82,7 +82,7 @@ friend class CDefaultTokenHandler; NS_DECL_ISUPPORTS nsHTMLParser(); ~nsHTMLParser(); - virtual nsIContentSink* SetContentSink(nsIContentSink* aSink); + virtual void SetContentSink(nsIContentSink* aSink); virtual PRBool Parse(nsIURL* aURL); virtual PRBool ResumeParse(); diff --git a/parser/htmlparser/src/nsIParser.h b/parser/htmlparser/src/nsIParser.h index 82b82b15008..96e2e8e4411 100644 --- a/parser/htmlparser/src/nsIParser.h +++ b/parser/htmlparser/src/nsIParser.h @@ -51,7 +51,7 @@ class nsIParser : public nsISupports { public: - virtual nsIContentSink* SetContentSink(nsIContentSink* aContentSink)=0; + virtual void SetContentSink(nsIContentSink* aContentSink)=0; virtual PRBool Parse(nsIURL* aURL)=0; virtual PRBool ResumeParse()=0; };