Because AddWebBrowserListener expects a nsIWeakReference pointer.

And my component derives from nsSupportsWeakReference class, its not
nsISupportsWeakReference. This process is explained in this
http://www.mozilla.org/projects/xpcom/weak_references.html.

And few more questions

1. The chunk of code that I showed you was placed in my Observe
function, I want to know is that the right place to add the listener

2. Do I need to add some category entry for listener functionality to
work in my component registration like I do for "xpcom-startup"

---------If nothing helps I am adding my cpp and header code here
----------------

.H file

//xpidl -m typelib -w -v -I -o nsIHello.idl

#include "stdio.h"
#include "stdlib.h"


#include "nsISupportsPrimitives.h"
#include "nsEmbedString.h"
#include "nsIGenericFactory.h"

#include "nsCOMPtr.h"
#include "nsXPCOM.h"
#include "nsIServiceManager.h"
#include "nsICategoryManager.h"

#include "nsIObserver.h"
#include "nsIComponentManager.h"
#include "nsISimpleEnumerator.h"

#include "nsIContentPolicy.h"
#include "iWebLock.h"
#include "nsmemory.h"
#include "nsIURI.h"
#include "nsIURL.h"
#include "nsIIOService.h"
#include "nsIWebProgressListener.h"
#include "nsIWeakReference.h"
#include "nsIWeakReferenceUtils.h"
#include "nsWeakReference.h"

const int MAX_URL_SIZE = 4096;

#define WebLock_CID \
{ 0x3d1d5816, 0xf6b0, 0x4a38, \
{ 0x83, 0xf5, 0x40, 0xe5, 0x36, 0x17, 0x74, 0xca}}


#define WebLock_ContractID "@BellSouth/WebLock"
static NS_DEFINE_CID(kSupportsCStringCID, NS_SUPPORTS_CSTRING_CID);


// a simple link list.
struct urlNode
{
    char* urlString;
    struct urlNode* next;
};


class WebLock: public iWebLock, public nsIObserver,
        public nsIContentPolicy,
                public nsIWebProgressListener,
                 public nsSupportsWeakReference
{
        public:
                NS_DEFINE_STATIC_IID_ACCESSOR(WebLock_CID)
                WebLock();
                virtual ~WebLock();

                NS_DECL_ISUPPORTS
                NS_DECL_NSIOBSERVER
                NS_DECL_IWEBLOCK
                NS_DECL_NSICONTENTPOLICY
                NS_DECL_NSIWEBPROGRESSLISTENER

        private:
                urlNode* mRootURLNode;
                PRBool   mLocked;
                //nsCOMPtr<nsIIOService> mIOService;
};


void Log(const char* strMessage, int nLineNumber , const char*
strFileName);

.CPP file

#include "weblock.h"
#include "tchar.h"
#include "windows.h"
#include "PCManager.h"
#include "nsIObserverService.h"
#include "nsServiceManagerUtils.h"
#include "nsIWebBrowser.h"
#include "nsIWeakReferenceUtils.h"
#include "nsIWebNavigation.h"
#include "nsIDOMHTMLDocument.h"
#include "nsIDOMWindow.h"
#include <wininet.h>
#include <winsock.h>
#include <time.h>
#include <string>
#include <nsComponentManagerUtils.h>

using namespace std;
#include "URLComponents.h"

TCHAR SERVER [128] = _T ("home.bellsouth.net");
const TCHAR* SEARCH_URL =
_T("http://%s/s/s.dll?spage=search/error.htm&pcclient=1&hbsearch=1&string=%s";);

////////////////////////////////////////////////////////////////////////////////
// Name: dnsLookup
// Purpose: Does a dnslookup for each domain either by name or IP.
// This is for redirecting the mistyped urls to server
// Called in before navigate event
////////////////////////////////////////////////////////////////////////////////
int dnsLookup (char* query)
{
        clock_t start = clock ();
        if (!query || *query == '\0')
        {
                return -3;
        }
        WSADATA         wsaData;
        hostent         *host;
        in_addr         ipHost;
        int                     i, isIP = 1;

        // Startup Winsock 1.1
        if(WSAStartup(0x0101, &wsaData) != 0)
        {
                WSACleanup();  // just in case ...
                return -2;
        }

        for(i=0; i < strlen(query); i++)
        {
                if( query[i] == '.' )
                {
                        continue;
                }
                else if( isalpha(query[i]) )
                {
                  isIP = 0;
                  break;
                }
        }

        // The gethostbyname() and gethostbyaddr() functions ...
        if(!isIP)
        {
                host = gethostbyname(query);
        }
        else
        {
                ipHost.s_addr = inet_addr(query);
                host = gethostbyaddr( (const char*) &ipHost, sizeof(struct 
in_addr),
AF_INET );
        }

        char szBuf [MAX_URL_SIZE] = "";
        clock_t finish;
        int nErrorCode = WSAGetLastError();
        if (nErrorCode != 0)
        {
                sprintf (szBuf, "DNS Error: %d", nErrorCode);
                Log (szBuf,__LINE__,__FILE__);
                if (nErrorCode == 11001  /* Host not Found */)
                {
                        WSACleanup();
                        finish = clock ();
                        sprintf (szBuf, "DNSLookup %3.2fe %s", (double)(finish 
- start) /
CLOCKS_PER_SEC, query);
                        Log (szBuf,__LINE__,__FILE__);
                        return -1;
                }
        }


        WSACleanup();
        finish = clock ();
        sprintf (szBuf, "DNSLookup %3.2fe %s", (double)(finish - start) /
CLOCKS_PER_SEC, query);
        Log(szBuf,__LINE__,__FILE__);

        return 1;
}

WebLock::WebLock()
{
        Log("Inside WebLock::WebLock()",__LINE__,__FILE__);
        NS_INIT_ISUPPORTS();
        mLocked = PR_TRUE;
        mRootURLNode = nsnull;
}

WebLock::~WebLock()
{
         Log("Inside WebLock::~WebLock()",__LINE__,__FILE__);
}

NS_IMETHODIMP WebLock::Lock()
{
    Log("Inside WebLock::Lock()",__LINE__,__FILE__);
        mLocked = PR_FALSE;
    return NS_OK;
}

NS_IMETHODIMP WebLock::Unlock()
{
    Log("Inside WebLock::Unlock()",__LINE__,__FILE__);
        mLocked = PR_FALSE;
    return NS_OK;
}





NS_IMETHODIMP WebLock::AddSite(const char *url)
{
    Log("Inside WebLock::AddSite",__LINE__,__FILE__);
    return NS_ERROR_NOT_IMPLEMENTED;
}


NS_IMETHODIMP WebLock::RemoveSite(const char *url)
{
    Log("Inside WebLock::RemoveSite",__LINE__,__FILE__);
    return NS_ERROR_FAILURE;
}


NS_IMETHODIMP WebLock::GetSites(nsISimpleEnumerator * *aSites)
{
        Log("Inside WebLock::GetSites",__LINE__,__FILE__);
    return NS_OK;
}

NS_IMETHODIMP WebLock::SetSites(nsISimpleEnumerator * aSites)
{
        Log("Inside WebLock::SetSites",__LINE__,__FILE__);
    return NS_OK;
}

NS_IMETHODIMP WebLock::ShouldProcess(PRUint32 aContentType, nsIURI
*aContentLocation, nsIURI *aRequestOrigin, nsISupports *aContext, const
nsACString & aMimeType, nsISupports *aExtra, PRInt16 *_retval)
{
        Log("Inside WebLock::ShouldProcess",__LINE__,__FILE__);
        return NS_OK;
}

NS_IMETHODIMP WebLock::ShouldLoad(PRUint32 aContentType, nsIURI
*aContentLocation, nsIURI *aRequestOrigin, nsISupports *aContext, const
nsACString & aMimeTypeGuess, nsISupports *aExtra, PRInt16 *_retval)
{
        if (!aContentLocation)
        return NS_ERROR_FAILURE;



        *_retval = PR_TRUE;
    return NS_OK;
}


NS_IMPL_ISUPPORTS5(WebLock,  iWebLock, nsIObserver, nsIContentPolicy,
nsIWebProgressListener, nsISupportsWeakReference);

NS_IMETHODIMP WebLock::Observe(nsISupports *aSubject, const char
*aTopic, const PRUnichar *aData)
{
        nsresult rv;

        nsCOMPtr<nsIWebBrowser> pBrowser2;
        pBrowser2 =
do_GetService("@mozilla.org/embedding/browser/nsWebBrowser;1");

    nsCOMPtr<nsIWeakReference> listener(
        do_GetWeakReference(NS_STATIC_CAST(nsIWebProgressListener*,
this)));
    rv = pBrowser2->AddWebBrowserListener(this,
NS_GET_IID(nsIWebProgressListener));
        if (rv == NS_ERROR_INVALID_ARG)
                MessageBox(NULL,"Failed to add listener","-",0);


//      nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(pBrowser2));

//      nsCOMPtr<nsIDOMDocument> doc;
//      nsCOMPtr<nsIDOMWindow> window;
//      pBrowser2->GetContentDOMWindow(getter_AddRefs(window));
//      if (window) {
//        window->GetDocument(getter_AddRefs(doc));
//      }


//nsCOMPtr<nsIDOMDocument> domDocument;
//webNav->GetDocument(getter_AddRefs(domDocument));
//nsCOMPtr<nsIDOMHTMLDocument>
//htmlDomDocument(do_QueryInterface(domDocument));
//if (domDocument)
//{
//nsString title;
//htmlDomDocument->GetTitle (title);
//}
//      Log (title.Get (), __LINE__,__FILE__);



//      Log((const char *)id ,__LINE__,__FILE__);
//      rv = pBrowser2->AddWebBrowserListener(
                                                                
//NS_GetWeakReference(static_cast<nsIWebProgressListener*>(this)),
                                                                
//this->NS_DEFINE_STATIC_IID_ACCESSOR(NS_IWEBPROGRESSLISTENER_IID));
                                //NS_GET_IID(WebLock));
/*    nsWeakPtr weakling(

dont_AddRef(NS_GetWeakReference(NS_STATIC_CAST(nsIWebProgressListener*,
this))));
    rv = pBrowser2->AddWebBrowserListener(weakling,
NS_GET_IID(nsIWebProgressListener));

        //      nsWeakPtr weakPtr = getter_AddRefs(
NS_GetWeakReference(static_cast<nsIWebProgressListener*>(this)) );
    //rv =
pBrowser2->AddWebBrowserListener(weakPtr,NS_GET_IID(nsIWebProgressListener));
        if (rv == NS_OK)
                MessageBox(NULL,"Success to add listener","-",0);
        else
        if (rv == NS_ERROR_INVALID_ARG)
                MessageBox(NULL,"Failed to add listener","-",0);

        else
                MessageBox(NULL,"Unknown to add listener","-",0); */

        Log("Inside WebLock::Observe",__LINE__,__FILE__);
        Log(aTopic,__LINE__,__FILE__);
        return NS_OK;
}

static NS_METHOD WebLockRegistration(nsIComponentManager *aCompMgr,
nsIFile *aPath,const char *registryLocation,
                        const char *componentType,const nsModuleComponentInfo 
*info)
{

        Log("Starting  WebLockRegistration",__LINE__,__FILE__);
        nsresult rv;

        nsCOMPtr<nsIServiceManager> servman =
do_QueryInterface((nsISupports*)aCompMgr, &rv);
        if (NS_FAILED(rv))
        {
                Log("Terminating WebLockRegistration Global
Function:do_QueryInterface Failed",__LINE__,__FILE__);
                return rv;
        }


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

        if (NS_FAILED(rv))
        {
                Log("Terminating WebLockRegistration Global
Function:GetServiceByContractID Failed",__LINE__,__FILE__);
                return rv;
        }


        char* previous = nsnull;
        rv =
catman->AddCategoryEntry("xpcom-startup",WebLock_ContractID,WebLock_ContractID,PR_TRUE,PR_TRUE,&previous);

        if (NS_FAILED(rv))
        {
                Log("Terminating WebLockRegistration Global
Function:AddCategoryEntry(xpcom-startup) Failed",__LINE__,__FILE__);
                return rv;
        }

        if(previous==nsnull)
                Log("Previous was null",__LINE__,__FILE__);

        if (previous)
        nsMemory::Free(previous);

    rv = catman->AddCategoryEntry("content-policy",WebLock_ContractID,
WebLock_ContractID,PR_TRUE,PR_FALSE,&previous);
        if(previous==nsnull)
                Log("Previous was null",__LINE__,__FILE__);

    if (previous)
        nsMemory::Free(previous);


        if (NS_FAILED(rv))
        {
                Log("Terminating WebLockRegistration Global
Function:AddCategoryEntry(content-policy) Failed",__LINE__,__FILE__);
                return rv;
        }


        Log("Leaving WebLockRegistration Global Function:AddCategoryEntry
Succeed",__LINE__,__FILE__);
        return rv;
}

static NS_METHOD WebLockUnregistration(nsIComponentManager
*aCompMgr,nsIFile *aPath,const char *registryLocation,
                                                const nsModuleComponentInfo 
*info)
{
        Log("Starting  WebLockUnRegistration",__LINE__,__FILE__);
        nsresult rv;

        nsCOMPtr<nsIServiceManager> servman =
do_QueryInterface((nsISupports*)aCompMgr, &rv);
        if (NS_FAILED(rv))
        {
                Log("Terminating WebLockUnRegistration Global
Function:do_QueryInterface Failed",__LINE__,__FILE__);
                return rv;
        }


        nsCOMPtr<nsICategoryManager> catman;
        servman->GetServiceByContractID(NS_CATEGORYMANAGER_CONTRACTID,
        NS_GET_IID(nsICategoryManager),
        getter_AddRefs(catman));

        if (NS_FAILED(rv))
        {
                Log("Terminating WebLockUnRegistration Global Function:Unknown
Error",__LINE__,__FILE__);
                return rv;
        }

        rv =
catman->DeleteCategoryEntry("xpcom-startup",WebLock_ContractID,PR_TRUE);
        if (NS_FAILED(rv))
        {
                Log("Terminating WebLockUnRegistration Global
Function:DeleteCategoryEntry(xpcom-startup) Failed",__LINE__,__FILE__);
                return rv;
        }
        rv =
catman->DeleteCategoryEntry("content-policy",WebLock_ContractID,PR_TRUE);
        if (NS_FAILED(rv))
        {
                Log("Terminating WebLockUnRegistration Global
Function:DeleteCategoryEntry(content-policy)
Failed",__LINE__,__FILE__);
                return rv;
        }


        Log("Leaving WebLockUnRegistration Global Function:DeleteCategoryEntry
Succeed",__LINE__,__FILE__);
        return rv;
}



NS_GENERIC_FACTORY_CONSTRUCTOR(WebLock)

static const nsModuleComponentInfo components[] =
{
        {
                "WebLock",
                WebLock_CID,
                WebLock_ContractID,
                WebLockConstructor,
                WebLockRegistration,
                WebLockUnregistration
        }
};

NS_DECL_CLASSINFO(WebLockModule)

NS_IMPL_NSGETMODULE(WebLockModule, components)

#define WEBLOCK_LOG
void Log(const char* strMessage, int nLineNumber , const char*
strFileName)
{

#ifdef WEBLOCK_LOG
        FILE *fp = NULL;
        char * str = NULL;
        char strLineNumber[10];
        int nLen = NULL;
        nLen = (int) (strlen(strMessage) + sizeof(strLineNumber) +
strlen(strFileName) + 5);
        str = new char[nLen];
        memset(strLineNumber,0,sizeof(strLineNumber));
        sprintf(strLineNumber,"%d",nLineNumber);
        strcpy(str,strFileName);
        strcat(str," (");
        strcat(str,strLineNumber);
        strcat(str,") : ");
        strcat(str,strMessage);
        strcat(str,"\n");
        fp = fopen("c:\\temp\\weblock.txt","a+");
        nLen = (int)strlen(str);
        fwrite(str,1,nLen,fp);
        fclose(fp);
        delete [] str;
#endif
}


NS_IMETHODIMP WebLock::OnStateChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
{
        nsresult nr;
        return nr;
}
NS_IMETHODIMP WebLock::OnProgressChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32
aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
{
        nsresult nr;
        return nr;
}
NS_IMETHODIMP WebLock::OnLocationChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest, nsIURI *aLocation)
{
        nsresult nr;
        return nr;
}
NS_IMETHODIMP WebLock::OnStatusChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
{
        nsresult nr;
        return nr;
}
NS_IMETHODIMP WebLock::OnSecurityChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest, PRUint32 aState)
{
        nsresult nr;
        return nr;
}

_______________________________________________
Mozilla-xpcom mailing list
[email protected]
http://mail.mozilla.org/listinfo/mozilla-xpcom

Reply via email to