Click here to Skip to main content
11,718,289 members (86,366 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ Firefox XPCOM
I am creating application which gets URL from Firefox on every pageload(every url entered by user…).Without using javascript………

Uptil I have created component which is starting up automatically . I registered for AddProgressListener (NOTIFY_STATE_ALL | NOTIFY_ALL)….
But I am unable to get OnStateChange() event. I tried a lot but am still having problems. I searched codes on the i-net but it confuses me.
I am sending code below. I promise it is short code(not big stuff..). it is Build and running program….
Will you please take look by your experienced eyes? You can do changes in code.

And one one other question…..

OnState change is called for every request?

For getting url I have to navigate through all the tabs which are opened?(is there any way to get url directly without doing this stuff…).
I hope you will help me…………………
Waiting for your reply…………..

Sachin Khutwad.

I implemented

class MyComponent : public IMyComponent, public nsIWebProgressListener, public nsIObserver
in my observe() method following is code ::

NS_IMETHODIMP MyComponent::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
    nsresult rv;
    WCHAR wszURL[MAX_PATH] = L"";
    nsCOMPtr<nsIObserverService> os;
    nsCOMPtr<nsIServiceManager> servMan;
    rv = NS_GetServiceManager(getter_AddRefs(servMan));
    if (NS_SUCCEEDED(rv))
        if (_stricmp(aTopic, "xpcom-startup") == 0)
            nsCOMPtr<nsIWebProgress> progress;
            servMan->GetServiceByContractID(";1", NS_GET_IID(nsIWebProgress), getter_AddRefs(progress));
            progress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_ALL | nsIWebProgress::NOTIFY_ALL);

            MessageBox(NULL,L"prgresslistner added",L"Success",MB_OK);
  return NS_OK;

And in OnSteateChange() Method following is code ::

NS_IMETHODIMP MyComponent::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
         MessageBox(NULL,L"inside OnStateChange ()",L"Success",MB_OK);
            if ((aStateFlags & STATE_STOP) && (aStateFlags & STATE_IS_DOCUMENT))
               MessageBox(NULL,L"inside OnStateChange ()",L"Success",MB_OK);
               //nsCString url;
       return NS_OK;

Can anyone please tell me why OnstateChange() method is not getting called?
Please give any code sample if you have.
Posted 22-Nov-10 20:23pm
Edited 22-Nov-10 20:42pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need to inherit your class MyComponent also from nsSupportsWeakReference and have following in implementation NS_IMPL_ISUPPORTSX(CProgressListener, ... , nsISupportsWeakReference) where X is replaced with correct number of classes you inherit from. This will add weak reference implementation to your class. Remember that your object needs to have at least one strong reference (call AddRef on it) in order not to be destroyed by runtime.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

if AddProgressListener fails it might be due to the class not inherited from nsSupportsWeakReference.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 215
1 F-ES Sitecore 210
2 Maciej Los 140
3 Jochen Arndt 120
4 Andy Lanng 115
0 OriginalGriff 535
1 Maciej Los 529
2 Richard MacCutchan 440
3 CHill60 415
4 F-ES Sitecore 415

Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 4 May 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100