Click here to Skip to main content
Rate this: bad
good
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))
    {
        servMan->GetServiceByContractID("@mozilla.org/observer-service;1",NS_GET_IID(nsIObserverService),getter_AddRefs(os));
        if (_stricmp(aTopic, "xpcom-startup") == 0)
        {
 
            nsCOMPtr<nsIWebProgress> progress;
            servMan->GetServiceByContractID("@mozilla.org/docloaderservice;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;
               //aRequest->GetName(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 21:23pm
Edited 22-Nov-10 21:42pm
JF201553.3K
v2
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
0 OriginalGriff 200
1 George Jonsson 175
2 Kornfeld Eliyahu Peter 169
3 PIEBALDconsult 110
4 Richard MacCutchan 85
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 4 May 2012
Copyright © CodeProject, 1999-2014
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