nsLoadGroup now uses weak pointers for observers and LoadGroupListenerFactory.

This commit is contained in:
vidur%netscape.com 1999-09-09 23:55:19 +00:00
parent 56c104a8a3
commit 8fae5bb797
2 changed files with 23 additions and 15 deletions

View File

@ -51,7 +51,7 @@ PRLogModuleInfo* gLoadGroupLog = nsnull;
nsLoadGroup::nsLoadGroup(nsISupports* outer) nsLoadGroup::nsLoadGroup(nsISupports* outer)
: mChannels(nsnull), mSubGroups(nsnull), : mChannels(nsnull), mSubGroups(nsnull),
mDefaultLoadAttributes(nsIChannel::LOAD_NORMAL), mDefaultLoadAttributes(nsIChannel::LOAD_NORMAL),
mObserver(nsnull), mParent(nsnull), mForegroundCount(0), mParent(nsnull), mForegroundCount(0),
mIsActive(PR_FALSE) mIsActive(PR_FALSE)
{ {
NS_INIT_AGGREGATED(outer); NS_INIT_AGGREGATED(outer);
@ -76,7 +76,6 @@ nsLoadGroup::~nsLoadGroup()
NS_ASSERTION(NS_SUCCEEDED(rv), "Cancel failed"); NS_ASSERTION(NS_SUCCEEDED(rv), "Cancel failed");
NS_IF_RELEASE(mChannels); NS_IF_RELEASE(mChannels);
NS_IF_RELEASE(mSubGroups); NS_IF_RELEASE(mSubGroups);
NS_IF_RELEASE(mObserver);
NS_IF_RELEASE(mParent); NS_IF_RELEASE(mParent);
PR_LOG(gLoadGroupLog, PR_LOG_DEBUG, PR_LOG(gLoadGroupLog, PR_LOG_DEBUG,
@ -114,8 +113,11 @@ nsresult nsLoadGroup::SubGroupIsEmpty(nsresult aStatus)
PR_LOG(gLoadGroupLog, PR_LOG_DEBUG, PR_LOG(gLoadGroupLog, PR_LOG_DEBUG,
("LOADGROUP: %x Firing OnStopRequest(...).\n", ("LOADGROUP: %x Firing OnStopRequest(...).\n",
this)); this));
rv = mObserver->OnStopRequest(mDefaultLoadChannel, nsnull, nsCOMPtr<nsIStreamObserver> observer = do_QueryReferent(mObserver);
aStatus, nsnull); if (observer) {
rv = observer->OnStopRequest(mDefaultLoadChannel, nsnull,
aStatus, nsnull);
}
if (mParent) { if (mParent) {
mParent->SubGroupIsEmpty(aStatus); mParent->SubGroupIsEmpty(aStatus);
@ -367,7 +369,6 @@ nsLoadGroup::Init(nsIStreamObserver *observer, nsILoadGroup *parent)
{ {
nsresult rv; nsresult rv;
NS_IF_RELEASE(mObserver);
if (observer) { if (observer) {
/* /*
nsCOMPtr<nsIEventQueue> eventQueue; nsCOMPtr<nsIEventQueue> eventQueue;
@ -384,8 +385,7 @@ nsLoadGroup::Init(nsIStreamObserver *observer, nsILoadGroup *parent)
mObserver = asyncObserver; mObserver = asyncObserver;
*/ */
mObserver = observer; mObserver = getter_AddRefs(NS_GetWeakReference(observer));
NS_ADDREF(mObserver);
} }
if (parent) { if (parent) {
@ -481,7 +481,10 @@ nsLoadGroup::AddChannel(nsIChannel *channel, nsISupports* ctxt)
PR_LOG(gLoadGroupLog, PR_LOG_DEBUG, PR_LOG(gLoadGroupLog, PR_LOG_DEBUG,
("LOADGROUP: %x Firing OnStartRequest(...).\n", ("LOADGROUP: %x Firing OnStartRequest(...).\n",
this)); this));
rv = mObserver->OnStartRequest(channel, ctxt); nsCOMPtr<nsIStreamObserver> observer = do_QueryReferent(mObserver);
if (observer) {
rv = observer->OnStartRequest(channel, ctxt);
}
} }
// return with rv, below // return with rv, below
} }
@ -556,8 +559,11 @@ nsLoadGroup::RemoveChannel(nsIChannel *channel, nsISupports* ctxt,
if (mObserver) { if (mObserver) {
PR_LOG(gLoadGroupLog, PR_LOG_DEBUG, PR_LOG(gLoadGroupLog, PR_LOG_DEBUG,
("LOADGROUP: %x Firing OnStopRequest(...).\n", ("LOADGROUP: %x Firing OnStopRequest(...).\n",
this)); this));
rv = mObserver->OnStopRequest(channel, ctxt, status, errorMsg); nsCOMPtr<nsIStreamObserver> observer = do_QueryReferent(mObserver);
if (observer) {
rv = observer->OnStopRequest(channel, ctxt, status, errorMsg);
}
// return with rv, below // return with rv, below
} }
if (mParent) { if (mParent) {
@ -636,15 +642,16 @@ nsLoadGroup::GetSubGroups(nsISimpleEnumerator * *aSubGroups)
NS_IMETHODIMP NS_IMETHODIMP
nsLoadGroup::GetGroupListenerFactory(nsILoadGroupListenerFactory * *aFactory) nsLoadGroup::GetGroupListenerFactory(nsILoadGroupListenerFactory * *aFactory)
{ {
*aFactory = mGroupListenerFactory; if (mGroupListenerFactory) {
NS_IF_ADDREF(*aFactory); mGroupListenerFactory->QueryReferent(NS_GET_IID(nsILoadGroupListenerFactory), (void**)aFactory);
}
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsLoadGroup::SetGroupListenerFactory(nsILoadGroupListenerFactory *aFactory) nsLoadGroup::SetGroupListenerFactory(nsILoadGroupListenerFactory *aFactory)
{ {
mGroupListenerFactory = aFactory; mGroupListenerFactory = getter_AddRefs(NS_GetWeakReference(aFactory));
return NS_OK; return NS_OK;
} }

View File

@ -24,6 +24,7 @@
#include "nsIStreamListener.h" #include "nsIStreamListener.h"
#include "nsAgg.h" #include "nsAgg.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsWeakPtr.h"
class nsISupportsArray; class nsISupportsArray;
class nsLoadGroupEntry; class nsLoadGroupEntry;
@ -61,14 +62,14 @@ protected:
PRUint32 mDefaultLoadAttributes; PRUint32 mDefaultLoadAttributes;
nsISupportsArray* mChannels; nsISupportsArray* mChannels;
nsISupportsArray* mSubGroups; nsISupportsArray* mSubGroups;
nsIStreamObserver* mObserver; nsWeakPtr mObserver;
nsLoadGroup* mParent; // weak ref nsLoadGroup* mParent; // weak ref
PRUint32 mForegroundCount; PRUint32 mForegroundCount;
PRBool mIsActive; PRBool mIsActive;
nsCOMPtr<nsIChannel> mDefaultLoadChannel; nsCOMPtr<nsIChannel> mDefaultLoadChannel;
nsCOMPtr<nsILoadGroupListenerFactory> mGroupListenerFactory; nsWeakPtr mGroupListenerFactory;
}; };
#endif // nsLoadGroup_h__ #endif // nsLoadGroup_h__