Hi Segfault, et al,
I think I've at least figured out my build problems.

Basically I'm using the following version of the SDK
gecko-sdk-i586-pc-msvc-1.7.3.zip which I got from
http://ftp.mozilla.org/pub/mozilla.org/mozilla/releases/. I'm also using
the latest nsIContentPolicy.idl that I got from here
http://lxr.mozilla.org/mozilla/source/content/base/public/

1)Using xpidl.exe compile nsIContentPolicy to generate its header.
Include this header in you build.
2)The instructions in the "Creating XPCOM Components" component book
seem to be out of date (although I must say the tutorial itself is very
good). All you need to link against is xpcom.lib (comes in
gecko-sdk-i586-pc-msvc-1.7.3.zip).
3)The nsIContentPolicy header defines two pure virtual functions that
you must implement in your own code,
ShouldLoad() and ShouldProcess(). ShouldLoad is the one I'm interested
in as it's called before a page is rendered and gives you access to URL
info.
4)Implement these functions in your code. 
5) You still need to do the code steps described in "Creating XPCOM
Components" (see
http://www.mozilla.org/projects/xpcom/book/cxc/html/newbookTOC.html) to
register your component and have it loaded when XPCOM starts.

Segfault: You asked for some sample code - I've pasted this in below.
The code compiles and links fine and as far as I can tell it should work
(ya just need to add in your own code where I have "DO STUFF IN HERE"- 

However!!! - I'm still having problems. I register my component using
regxpcom as instructed, but for some reason it doesn't seem to be
working. In fact even though regxpcom says that it's registered
successfully I'm not sure it is. When I use component viewer in Mozilla,
my component doesn't appear in the list of registered components. Anyone
got any ideas why it doesn't appear?
I use regxpcom as follows;
regxpcom -x "C:\Program Files\Common
Files\mozilla.org\GRE\1.7.6_2005031907" "C:\Program Files\Common
Files\mozilla.org\GRE\1.7.6_2005031907\components\TestPageLoad.dll"

Am I putting my dll in the right place?

If I take a look at compreg.dat I can see it's been updated with details
of my component and the interfaces  my component is accessing. But as I
say my code doesn't seem to be getting called and I can't see my
component in component viewer.

Any help greatly appreciated.


Here's the code I have so far Segfault; - please let me know if you spot
anything wrong with it - ta ;-)


#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>

#include "nsIGenericFactory.h"

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

#include "nsIObserver.h"

#include "nsEmbedString.h"

#include "nsIContentPolicy.h"

#define TestPageLoad_CID \
{ 0x82b9ab1d, 0x7169, 0x43b6, \
{ 0xb6, 0xbb, 0x98, 0x70, 0xed, 0x36, 0xf7, 0xc7 }}

#define TestPageLoad_ContractID "@dobrien/testpageload"

class TestPageLoad: public nsIObserver, public nsIContentPolicy {
public:
        TestPageLoad();
        virtual ~TestPageLoad();

        NS_DECL_ISUPPORTS
        NS_DECL_NSIOBSERVER
        NS_DECL_NSICONTENTPOLICY
};

TestPageLoad::TestPageLoad()
{
        NS_INIT_ISUPPORTS();
}

        TestPageLoad::~TestPageLoad()
{
}

NS_IMPL_ISUPPORTS2(TestPageLoad, nsIObserver, nsIContentPolicy);
NS_IMETHODIMP

TestPageLoad::Observe(nsISupports *aSubject, const char *aTopic, const
PRUnichar *aData)
{
        return NS_OK;
}


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

        printf("Hello there, I'm registered!!\n");

    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", 
                                  "TestPageLoad",  
                                  TestPageLoad_ContractID, 
                                  PR_TRUE,  
                                  PR_TRUE,  
                                  &previous); 
//    if (previous) 
//        nsMemory::Free(previous); 

    rv = catman->AddCategoryEntry("content-policy", 
                                  "TestPageLoad",  
                                  TestPageLoad_ContractID, 
                                  PR_TRUE,  
                                  PR_TRUE,  
                                  &previous); 

//    if (previous) 
//        nsMemory::Free(previous); 
    return rv; 
}

NS_IMETHODIMP TestPageLoad::ShouldProcess(PRUint32 aContentType, nsIURI
*aContentLocation, 
                                                                  nsIURI
*aRequestOrigin, nsISupports *aContext, 
                                                                  const
nsACString & aMimeType, nsISupports *aExtra, PRInt16 *_retval)
{
    return NS_OK;
}


NS_IMETHODIMP TestPageLoad::ShouldLoad(PRUint32 aContentType, nsIURI
*contentLocation,
                                                                  nsIURI
*aRequestOrigin, nsISupports *aContext,
                                                                  const
nsACString & aMimeTypeGuess, nsISupports *aExtra, PRInt16 *_retval)
{ 

        // DO STUFF IN HERE

    return NS_OK; 
}


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

        printf("Hello there, starting to unregister!!\n");

        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",
                "TestPageLoad", PR_TRUE);

        if (NS_FAILED(rv))
                return rv;

        rv = catman->DeleteCategoryEntry("content-policy",
        "TestPageLoad", PR_TRUE);

        printf("Hello there, I'm finally unregistered!!\n");

        return rv;
}



NS_GENERIC_FACTORY_CONSTRUCTOR(TestPageLoad)

static const nsModuleComponentInfo components[] =
{
        { "TestPageLoad",
        TestPageLoad_CID,
        TestPageLoad_ContractID,
        TestPageLoadConstructor,
        TestPageLoadRegistration,
        TestPageLoadUnregistration
        }
};

NS_IMPL_NSGETMODULE(TestPageLoadModule, components)

---- end of code ----
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Segfault
Sent: 04 April 2005 16:54
To: mozilla-xpcom@mozilla.org
Subject: Re: How to create a component like IE BHO (Browser Helper
Object) for Mozilla/Firefox


Hi Vonuyx,

I am trying to do something similar.  I have been trying to write
something that will attach to a running instance of Firefox and allow me
to get a DOM handle so I can "drive" Firefox.  At this point though I
would even settle for being able to know when a page was finished
rendering.  Could you post any sample code here that you have come up
with?  I'm really pulling my hair out on this one :)

Thanks,
Segfault

vonuyx wrote:
> Hi,
> I've been reading through a lot about XPCOM and I think I have
figured
> out what to do. Thanks a lot!
> Vonuyx
>
> Gangadhar NPK wrote:
>
> > Using XPCOM, one can attach listeners to various events in a
browser
> > IIRC nsIWebProgressListener and such events. Once you are
subscriber to
> > an event, you will be notified about that event. On obtaining the 
> > notification you can proceed with your application. I think the 
> > xpcom-book
(http://www.mozilla.org/projects/xpcom/book/cxc/)
> > has a simple code for soemthing similar. Please check up if it can
help
> > you.
> > hth
> > Gangadhar
> > vonuyx wrote:
> >
> >> Thanks again for your help.
> >> What I am trying to do is not to embed, but to create an extension
for
> >> Firefox. I suppose there is some difference between two things.
Would
> >> you (or anyone else) tell me where I can find some sample source
codes
> >> fro this?
> >> Thanks a lot
> >> Vonuyx
> >>
> >> Doug Turner wrote:
> >>
> >>> vonuyx wrote:
> >>>
> >>>> Hello,
> >>>>
> >>>>
> >>>> I want to create a component (suppose an XPCOM one) that can be 
> >>>> 'attached' to each Firefox browser window and be informed every
time
> >>>> the user browses to a new page. I also want to get the address
of
> >>>> these pages as well. In IE it can be done by implementing the 
> >>>> IWebBrowser interface to create a so-called BHO. Any one has any

> >>>> idee of how to do this in Firefox?
> >>>> Thankyou very much in advance!
> >>>>
> >>>
> >>> Start here: http://www.mozilla.org/projects/xpcom/
> >>>
> >>> When you understand the gist of XPCOM, take a look at:
> >>>
> >>> http://www.mozilla.org/projects/embedding/
> >>>
http://www.mozilla.org/projects/embedding/embedapiref/embedapiTOC.html
> >>>
> >>> Hope this helps,
> >>> Doug
> >>>
> >>

_______________________________________________
Mozilla-xpcom mailing list
Mozilla-xpcom@mozilla.org http://mail.mozilla.org/listinfo/mozilla-xpcom

_______________________________________________
Mozilla-xpcom mailing list
Mozilla-xpcom@mozilla.org
http://mail.mozilla.org/listinfo/mozilla-xpcom

Reply via email to