Re: simple nsIWebProgressListener question

[email protected] (yueweng)
Newsgroups gmane.comp.mozilla.devel.xpinstall
Organization http://groups.google.com
Message-ID <bdd3dfe3.0407150309.cdc2171__19337.4496106573$1089889977$gmane$org@posting.google.com>
Christian Biesinger <[email protected]> wrote in message news:<[email protected]>...
> On Wed, Jul 14, 2004 at 03:02:24AM -0700, yueweng wrote:
> > I have implement the suggestion in the following code, but, I am still
> > not able to capture any browser signal yet.  May I know if there is
> > anything I missed out in the following code in order to start
> > receiving Mozilla browser event?
> 
> It seems that you never make sure that your component gets called at any
> time. See for example
> http://www.mozilla.org/projects/xpcom/book/cxc/html/weblock.html#1001977
> for how to make sure that your component gets instantiated on each app
> startup.
> 
> --
Hi Christian ,

Thanks for the help again.  I have made changes by inherit one more
interface from nsIObserver.  Then program is able to perform
nsSampleRegistrationProc() to do AddCategoryEntry("xpcom-startup",....
similar to what weblock has done.

Then I restart the mozilla again.  This time my constructor
nsSampleImpl() is being called, I think it should be, during starting
of mozilla.  However, mozilla crashed inside the constructor,
nsSampleImpl() at the function AddProgressListener().  I don't know
what's wrong with my implementation, I am wondering if you can help me
up again?

/////////////////////////////////////////////////////////////
The implementation of nsSampleImpl() is as follows:

static NS_DEFINE_IID(kDocLoaderServiceCID,
NS_DOCUMENTLOADER_SERVICE_CID);

nsSampleImpl::nsSampleImpl()
{
    nsCOMPtr<nsIWebProgress>
progress(do_GetService(kDocLoaderServiceCID));
    if (progress)
	progress->AddProgressListener((nsIWebProgressListener*)this,						   
  nsIWebProgress::NOTIFY_STATE_DOCUMENT);
}

//////////////////////////////////////////////////////////
The following is the callstack when crashed:

nsQueryInterface::operator()() line 47 + 21 bytes
nsCOMPtr<nsIWeakReference>::assign_from_qi() line 1030 + 17 bytes
nsCOMPtr<nsIWeakReference>::nsCOMPtr<nsIWeakReference>() line 572
nsCOMPtr<nsIWeakReference>::Assert_NoQueryNeeded() line 520
nsCOMPtr<nsIWeakReference>::nsCOMPtr<nsIWeakReference>() line 590
nsDocLoaderImpl::AddProgressListener() line 891 <---------MY PROGRAM
nsSampleImpl::nsSampleImpl() line 79 <---------MY PROGRAM 
nsSampleImplConstructor() line 89 + 27 bytes <---------MY PROGRAM
nsGenericFactory::CreateInstance() line 82 + 21 bytes 
nsComponentManagerImpl::CreateInstanceByContractID() line 2000 + 24
bytes
nsComponentManagerImpl::GetServiceByContractID() line 2420 + 50 bytes
nsGetServiceByContractID::operator()() line 121 + 38 bytes
nsCOMPtr_base::assign_from_helper() line 114 + 18 bytes
nsCOMPtr<nsISupports>::nsCOMPtr<nsISupports>() line 846
NS_CreateServicesFromCategory() line 785 + 37 bytes
NS_InitXPCOM2() line 633 + 17 bytes
NS_InitXPCOM2() line 173 + 18 bytes
GRE_Startup() line 477 + 35 bytes
main() line 1705 + 5 bytes
mainCRTStartup() line 338 + 17 bytes

//////////////////////////////////////////////////////////////////////
The implementation of other parts of the program is as follows:

NS_IMETHODIMP
nsSampleImpl::QueryInterface(REFNSIID aIID, void** aInstancePtr)
{
  if (NULL == aInstancePtr) {
    return NS_ERROR_NULL_POINTER;
  }
  *aInstancePtr = NULL;
  if (aIID.Equals(NS_GET_IID(nsISample))) {
    *aInstancePtr = (void*) NS_STATIC_CAST(nsISample*,this);
    NS_ADDREF_THIS();
    return NS_OK;
  }
  if (aIID.Equals(NS_GET_IID(nsSupportsWeakReference))) {
    *aInstancePtr = (void*) NS_STATIC_CAST(nsISample*,this);
    NS_ADDREF_THIS();
    return NS_OK;
  }
  if (aIID.Equals(NS_GET_IID(nsIObserver))) {
    *aInstancePtr = (void*) NS_STATIC_CAST(nsISample*,this);
    NS_ADDREF_THIS();
    return NS_OK;
  }
  if (aIID.Equals(NS_GET_IID(nsIWebProgressListener))) {
    *aInstancePtr = (void*) NS_STATIC_CAST(nsISample*,this);
    NS_ADDREF_THIS();
    return NS_OK;
  }
  if (aIID.Equals(NS_GET_IID(nsISupports))) {
    *aInstancePtr = (void*) NS_STATIC_CAST(nsISupports*,
                                          
NS_STATIC_CAST(nsISample*,this));
    NS_ADDREF_THIS();
    return NS_OK;
  }
  return NS_NOINTERFACE;
}

NS_IMPL_ADDREF(nsSampleImpl)
NS_IMPL_RELEASE(nsSampleImpl)

#define NS_SAMPLE_CONTRACTID "@mozilla.org/sample;1"

class nsSampleImpl : public nsIWebProgressListener,
		     public nsSupportsWeakReference,
		     public nsIObserver,
		     public nsISample
{
public:
    nsSampleImpl();

    NS_DECL_ISUPPORTS;
    NS_DECL_NSISAMPLE
    NS_DECL_NSIOBSERVER;
    NS_DECL_NSIWEBPROGRESSLISTENER;
	
private:
    ~nsSampleImpl();
    char* mValue;
};

static NS_METHOD nsSampleRegistrationProc(nsIComponentManager
*aCompMgr,
                                          nsIFile *aPath,
                                          const char
*registryLocation,
                                          const char *componentType,
                                          const nsModuleComponentInfo
*info)
{
	nsresult rv;

	nsCOMPtr<nsIServiceManager> servman =
do_QueryInterface((nsISupports*)aCompMgr, &rv);

	if (NS_FAILED(rv))
	     return rv;

	nsCOMPtr<nsICategoryManager> catman;
	servman->GetServiceByContractID(NS_CATEGORYMANAGER_CONTRACTID,
									NS_GET_IID(nsICategoryManager),
									getter_AddRefs(catman));
	if (NS_FAILED(rv))
		return rv;

	char* previous = nsnull;
	rv = catman->AddCategoryEntry("xpcom-startup",
				      "nsSampleImpl",
	                              NS_SAMPLE_CONTRACTID,
				      PR_TRUE,
				      PR_TRUE,
				      &previous);
	if (previous)
  	    nsMemory::Free(previous);
	return rv;
}

static NS_METHOD nsSampleUnregistrationProc(nsIComponentManager
*aCompMgr, nsIFile *aPath, const char *registryLocation, const
nsModuleComponentInfo *info)
{
	nsresult rv;
	nsCOMPtr<nsIServiceManager> servman =
do_QueryInterface((nsISupports*)aCompMgr, &rv);
	
	if (NS_FAILED(rv))
            return rv;
	nsCOMPtr<nsICategoryManager> catman;
	servman->GetServiceByContractID(NS_CATEGORYMANAGER_CONTRACTID,NS_GET_IID(nsICategoryManager),
getter_AddRefs(catman));
	if (NS_FAILED(rv))
	    return rv;
	rv = catman->DeleteCategoryEntry("xpcom-startup",
	                                 "nsSampleImpl",
                                         PR_TRUE);
	return rv;
}

NS_GENERIC_FACTORY_CONSTRUCTOR(nsSampleImpl);

static const nsModuleComponentInfo components[] =
{
  { "Sample Component", 
    NS_SAMPLE_CID, 
    NS_SAMPLE_CONTRACTID, 
    nsSampleImplConstructor,
    nsSampleRegistrationProc,
    nsSampleUnregistrationProc
  }
};

NS_IMPL_NSGETMODULE(nsSampleModule, components)

Thanks very much,
yueweng
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.