Hi,


Did you get any success?

I am still stuck....

thanks for any help

JM

Looking for a way to generate and send asynchronous events from an XPCOM
component (c++) to JavaScript code in the currently loaded web page, we
are trying two possible solutions... none of which we can get to work:

1. On the XPCOM c++ side, get the nsIDOMDocument instance of the
currently loaded page, then from this instance create a new event
"test", using the nsIDOMDocumentEvent. On the JavaScript side, add an
EventListener for the new event.

2. Call a JavaScript function from C++ XPCOM, using a callback
mechanism. On the JavaScript side, implement a little XPCOM component.


CODE SNIPPETS FROM SOLUTION 1:


****** sol.1 - c++ side ******

nsCOMPtr<nsIWindowWatcher>
          wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
nsIDOMWindow *wind = NULL;
wwatch->GetActiveWindow(&wind);
if (wind) {
   printf("active window instantiated\n");
   nsCOMPtr<nsIDOMDocument> dom_doc;
   wind->GetDocument(getter_AddRefs(dom_doc));
   if (dom_doc) {
     printf("DOM document instantiated\n");
     nsCOMPtr<nsIDOMDocumentEvent>
              docEvent(do_QueryInterface(dom_doc));
     if (docEvent) {
       printf("DocumentEvent instantiated \n");
       nsCOMPtr<nsIDOMEvent> event;
       docEvent->CreateEvent(NS_LITERAL_STRING("Events"),

                             getter_AddRefs(event));
       if (event) {
         printf("Event instantiated \n");
         event->InitEvent(NS_LITERAL_STRING("test"), PR_TRUE, PR_TRUE);
         PRBool noDefault;
         nsCOMPtr<nsIDOMEventTarget> targ(do_QueryInterface(dom_doc));
         if (targ) {
           printf("EventTarget instantiated \n");
           targ->DispatchEvent(event, &noDefault);
         }
       }
     }
   }
}


****** sol.1 - JavaScript side (loaded page) ******


netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var element = document.getElementById("testEl");
element.addEventListener('test', pluto, true);

****** end (sol.1) ******

The code runs, but the event never arrives...

- Is the dom document (dom_doc) the actual instance of our HTML document
which is shown by the browser?

- Is it correct to try and add an event using nsIDOMDocumentEvent?


CODE SNIPPETS FROM SOLUTION 2:


****** sol.2 - IDL for the JS Callback component ******

#include "nsISupports.idl"
[scriptable, uuid(e4881ba4-8829-469f-a010-1c191ba20af5)]
interface nsIMyCallback : nsISupports {
   void OnNotify();
};


****** sol.2 - XUL JavaScript code - implementing Callback comp. ******


<?xml version="1.0"?>
<window
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
onload="init();">
<script type="text/javascript" language="JavaScript">
var cb_obj;
function init() {
   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
   cb_obj = Components.classes["xpc.PROVA_intf;1"].createInstance();
   cb_obj = cb_obj.QueryInterface(Components.interfaces.mozPROVA);
   cb_obj.setCallback(new Callback());
}

function Callback() {}
Callback.prototype = {
   OnNotify: function () { alert("Hello"); },
   QueryInterface: function (iid) {
     if (!iid.equals(Components.interfaces.nsIMyCallback)) {
       throw Components.results.NS_ERROR_NO_INTERFACE;
     }
     return this;
   }
}
</script>
</window>


****** sol.2 - IDL for the c++ XPCOM component ******


[scriptable, uuid(75746dad-0614-4af5-9b53-5813b6938ff0)]
interface nsIMyComponen : nsISupports {
   ...
   void setCallback(in nsIMyCallback cb);
   void notify();
};


****** sol.2 - c++ XPCOM component implementation ******


NS_IMETHODIMP mozPROVAClass::SetCallback(nsIMyCallback *cb) {
   mCallback = cb;
   return NS_OK;
}

NS_IMETHODIMP mozPROVAClass::Notify() {
   if (mCallback) {
     printf("mCallBack presente\n");
     mCallback->OnNotify();
   }
   return NS_OK;
}

****** end (sol.2) ******

- When the OnNotify method of the JavaScript object is called, Mozilla
stops running without any error. Is it necessary to proxy the call?


Any help will be much appreciated.


Regards,
Marily Rossotti




_______________________________________________
Mozilla-xpcom mailing list
[EMAIL PROTECTED]
http://mail.mozilla.org/listinfo/mozilla-xpcom

Reply via email to