I have an XPCOM object built with C++, and I can successfully call into
it from js. However, I'm now trying to do some work asynchronously, and
need to call back into js upon completion of my task.

 

As an intermediate proof-of-technology step, I'm trying to do an
immediate callback to my js object, and this is failing. My apologies
for the length of this, but I figured it's better to give too much info
than too little.... A question or two is at the end of all this...

 

 

I've declared a new interface:

 

[scriptable, uuid(8FC0AAA9-3C3F-4377-8179-6EA50E7A8286)]

interface nsIPhishListener : nsISupports

{

            void onLookupComplete(in nsICancelable aRequest,

 
in long aResult);

};

 

to go with my existing object (plus the asynch request):

 

[scriptable, uuid(EE5AA2A0-B27F-4e73-A62C-CA7A4E8201DC)]

interface IFFHook : nsICancelable

{

            :

            :

 

            nsICancelable AsyncLookupPhish(in AUTF8String    aHostName,

                               in nsIPhishListener aListener);

 

            

};

 

My js code, lifted and modified from a post elsewhere:

 

var listener =

{

    QueryInterface : function(aIID)

    {

        alert("js: myListener: QueryInterface(" + aIID + ")\n");

        if (aIID.equals(Components.interfaces.nsIPhishListener) ||

            aIID.equals(Components.interfaces.nsISupports) ) 

        {

            alert("js: myListener: QueryInterface : ok\n");

            return this;

        }

        throw Components.results.NS_NOINTERFACE;

        Components.returnCode =
Components.results.NS_ERROR_NO_INTERFACE;

        return null;

    },

 

    onLookupComplete: function( aRequest, aResult )

    {

        alert("js: myListener: onLookupComplete(" + aRequest + ", " +
aResult +")\n");

    }

}

 

function MyComponentTestGo() {

            try {

 
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")
;

                        const cid =
"@siteadvisor.com/SiteAdvisor/FFHook;1";

                        obj = Components.classes[cid].createInstance();

                        obj =
obj.QueryInterface(Components.interfaces.IFFHook);

            } catch (err) {

                        alert(err);

                        return;

            }

 

    alert("js: Before AsyncLookupPhish");

    obj.AsyncLookupPhish("www.siteadvisor.com", listener);           

    alert("js:After AsyncLookupPhish");

 

}

 

And the pertinent C++ header and source:

 

class CFFHook : public IFFHook

{

public:

            NS_DECL_ISUPPORTS

            NS_DECL_NSICANCELABLE

            NS_DECL_IFFHOOK

 

 

            CFFHook();

 

private:

  ~CFFHook();

 

protected:

            void ReportComplete();

 

            nsCOMPtr<nsIPhishListener> m_Listener;

 

------------------

 

NS_IMETHODIMP CFFHook::AsyncLookupPhish(const nsACString & aHostName,
nsIPhishListener *aListener, nsICancelable **_retval)

{

            ::MessageBox(NULL, _T("C++: AsyncLookupPhish 1"), _T("Foo"),
MB_OK);

 

            m_Listener = aListener;

 

            ::MessageBox(NULL, _T("C++: AsyncLookupPhish 2"), _T("Foo"),
MB_OK);

 

            

 

            // DEBUG DEBUG

            ReportComplete();

 

 

    return NS_OK;

}

 

 

void CFFHook::ReportComplete()

{

            CString sz;

 

            ::MessageBox(NULL, _T("C++: Before OnLookupComplete"),
_T("Foo"), MB_OK);

 

            nsresult nr = m_Listener->OnLookupComplete(this, 42);

 

            sz.Format(_T("C++: OnLookupComplete returned %08X"), nr);

            ::MessageBox(NULL, sz, _T("Foo"), MB_OK);

 

            m_Listener = NULL;

}

 

 

The QI on my object gets called for nsISupports guid by (I presume) FF's
XPCOM framework at the point of the  "obj.AsyncLookupPhish" call, but
never again. I would have expected a QI call at the "m_Listener =
aListener;" assignment.

 

Then, the "OnLookupComplete" callback returns the not-so-helpful
0x80004005 (NS_ERROR_FAILURE), and no surprise, the js onLookupComplete
is not called.

 

Do I have to do some magic to register my new nsIPhishListener with
XPCOM, beyond putting the generated xpt into FF's component directory
and clearing the xpti and compreg files in my profile?

 

 

_______________________________________________
dev-tech-xpcom mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xpcom

Reply via email to