how to implement nsIWepProgressListener in C++

30 views
Skip to first unread message

Christian Sell

unread,
Feb 20, 2014, 12:33:49 PM2/20/14
to
Hello,

I have the following class declaration:

class MyLocationListener: public nsIWebProgressListener
{
public:
MyLocationListener();
virtual ~MyLocationListener();
NS_DECL_ISUPPORTS
NS_DECL_NSIWEBPROGRESSLISTENER
};

and the corresponding implementation. I am using this class in the following way:

MyLocationListener * listener = new MyLocationListener();

nsCOMPtr<nsIWeakReference> weakref = do_GetWeakReference(listener, &retval);
CHECKERR();
retval = browser->AddWebBrowserListener(weakref, NS_GET_IID(nsIWebProgressListener));
CHECKERR();

however, the do_GetWeakReference call fails with an NS_ERROR_NO_INTERFACE error code. Can someone point out to me how to fix that? I tried to add a nsIWeakReference superclass to the class decl, including the NS_DECL_xx macros, but that yielded compile errors that I was notable to decipher. Maybe someone has a quick hint, or link..

TIA,
Christian

Christian Sell

unread,
Feb 24, 2014, 6:15:14 AM2/24/14
to
ok, I have finally managed. Heres the code:

//class decl
class LocationListener: public nsIWebProgressListener, public nsSupportsWeakReference
{
public:
LocationListener();
virtual ~LocationListenerW();
NS_DECL_ISUPPORTS
NS_DECL_NSIWEBPROGRESSLISTENER
};

//class impl
NS_IMPL_ISUPPORTS2(LocationListener, nsIWebProgressListener, nsISupportsWeakReference)

NS_IMETHODIMP LocationListener::OnXXChange(...)
{
return NS_OK;
}

//and class usage. plistener must be created non-locally
LocationListener * listener = new LocationListener();
nsCOMPtr<nsISupports> plistener = static_cast<nsIWebProgressListener *>(listener);

nsCOMPtr<nsIWeakReference> weakref = do_GetWeakReference(plistener, &retval);
Reply all
Reply to author
Forward
0 new messages