Patrick McHale wrote:
> 
> John,
> 
> We are still confused - Do you have an example of some actual code that
> implements this process.
> something that actually works...

I'm attaching a very simple example that does an asynchronous
callback into JS code. It uses the (ugly) nsITimer interface for
the delay. I think this is similar to what you first talked about
wanting. I just hacked this on to the existing xpcom sample stuff
to make this easy to build in a mozilla tree (on Windows at
least).

I hope this clears up the concept for you.

John.
Index: nsISample.idl
===================================================================
RCS file: /cvsroot/mozilla/xpcom/sample/nsISample.idl,v
retrieving revision 1.5
diff -u -r1.5 nsISample.idl
--- nsISample.idl       2000/06/27 05:54:46     1.5
+++ nsISample.idl       2001/06/12 16:39:07
@@ -48,4 +48,16 @@
 };
 
 
+[scriptable, uuid(be7cf690-5ee4-11d5-90de-0010a4e73d9a)]
+interface jband_SampleCallBackSink : nsISupports
+{
+    void tick(in PRUint32 whatever);
+};
 
+[scriptable, uuid(799e0530-5ee4-11d5-90de-0010a4e73d9a)]
+interface jband_SampleCallBackService : nsISupports
+{
+    void setTimer(in jband_SampleCallBackSink sink, 
+                  in PRUint32 ticks,
+                  in PRUint32 whatever);
+};
Index: nsSample.cpp
===================================================================
RCS file: /cvsroot/mozilla/xpcom/sample/nsSample.cpp,v
retrieving revision 1.10
diff -u -r1.10 nsSample.cpp
--- nsSample.cpp        2001/03/12 20:42:59     1.10
+++ nsSample.cpp        2001/06/12 16:39:07
@@ -139,3 +139,45 @@
     printf("%s %s\n", aPrefix, mValue);
     return NS_OK;
 }
+
+/***************************************************************************/
+
+NS_IMPL_ISUPPORTS2(jband_SampleCallBackServiceImpl, 
+                   jband_SampleCallBackService,
+                   nsITimerCallback)
+
+jband_SampleCallBackServiceImpl::jband_SampleCallBackServiceImpl()
+    : mWhatever(0)
+{
+    NS_INIT_ISUPPORTS();
+}
+
+jband_SampleCallBackServiceImpl::~jband_SampleCallBackServiceImpl()
+{
+    // do nothing...
+}
+
+/* void setTimer (in jband_SampleCallBackSink sink, in PRUint32 ticks, in PRUint32 
+whatever); */
+NS_IMETHODIMP jband_SampleCallBackServiceImpl::SetTimer(jband_SampleCallBackSink 
+*sink, PRUint32 ticks, PRUint32 whatever)
+{
+    if(!sink || mSink)
+        return NS_ERROR_FAILURE;
+    
+    nsCOMPtr<nsITimer> timer(do_CreateInstance("@mozilla.org/timer;1"));
+    if(!timer)
+        return NS_ERROR_FAILURE;
+    timer->Init(NS_STATIC_CAST(nsITimerCallback*,this), ticks);
+    
+    mSink = sink;       // This will make the nsCOMPtr do an AddRef()
+    mWhatever = whatever;
+    
+    return NS_OK;
+}
+
+NS_IMETHODIMP_(void)
+jband_SampleCallBackServiceImpl::Notify(nsITimer *timer)
+{
+    if(mSink)
+        mSink->Tick(mWhatever);
+    mSink = nsnull; // This will also make the nsCOMPtr do a Release()
+}
Index: nsSample.h
===================================================================
RCS file: /cvsroot/mozilla/xpcom/sample/nsSample.h,v
retrieving revision 1.3
diff -u -r1.3 nsSample.h
--- nsSample.h  2000/09/13 23:56:08     1.3
+++ nsSample.h  2001/06/12 16:39:08
@@ -111,3 +111,28 @@
 private:
     char* mValue;
 };
+
+/***************************************************************************/
+
+#include "nsITimer.h"
+#include "nsITimerCallback.h"
+
+class jband_SampleCallBackServiceImpl : 
+    public jband_SampleCallBackService,
+    public nsITimerCallback
+{
+public:
+    NS_DECL_ISUPPORTS
+    NS_DECL_JBAND_SAMPLECALLBACKSERVICE
+
+    // not very xpcom compilant method from nsITimerCallback
+    NS_IMETHOD_(void) Notify(nsITimer *timer);
+
+    jband_SampleCallBackServiceImpl();
+    virtual ~jband_SampleCallBackServiceImpl();
+
+private:
+    nsCOMPtr<jband_SampleCallBackSink> mSink;
+    PRUint32 mWhatever;
+};
+
Index: nsSampleModule.cpp
===================================================================
RCS file: /cvsroot/mozilla/xpcom/sample/nsSampleModule.cpp,v
retrieving revision 1.17
diff -u -r1.17 nsSampleModule.cpp
--- nsSampleModule.cpp  2001/05/05 05:33:23     1.17
+++ nsSampleModule.cpp  2001/06/12 16:39:08
@@ -92,6 +92,8 @@
 // For each class that wishes to support nsIClassInfo, add a line like this
 NS_DECL_CLASSINFO(nsSampleImpl)
 
+NS_GENERIC_FACTORY_CONSTRUCTOR(jband_SampleCallBackServiceImpl)
+
 static nsModuleComponentInfo components[] =
 {
   { "Sample Component", NS_SAMPLE_CID, NS_SAMPLE_CONTRACTID, nsSampleImplConstructor,
@@ -101,6 +103,12 @@
     NS_CI_INTERFACE_GETTER_NAME(nsSampleImpl),
     NULL /* no language helper */,
     &NS_CLASSINFO_NAME(nsSampleImpl)
+  },
+
+  { "jband_SampleCallBackService",
+    {0x6cc7a6e0, 0x5ee8, 0x11d5, { 0x90, 0xde, 0x0, 0x10, 0xa4, 0xe7, 0x3d, 0x9a }},
+    "@jband/sampleCallbackService;1", 
+    jband_SampleCallBackServiceImplConstructor,
   }
 };
 
Index: xpconnect-sample.html
===================================================================
RCS file: /cvsroot/mozilla/xpcom/sample/xpconnect-sample.html,v
retrieving revision 1.10
diff -u -r1.10 xpconnect-sample.html
--- xpconnect-sample.html       2000/09/14 23:20:41     1.10
+++ xpconnect-sample.html       2001/06/12 16:39:08
@@ -12,6 +12,41 @@
 </script>
 </center>
 
+<hr>
+<p>
+Hacky sample...
+<p>
+
+<script>
+var callback = {
+  tick : function (whatever) 
+  {
+    alert(whatever);
+  }
+}  
+  
+function startTimer() {
+
+  var msec = document.getElementById('msec').value;
+  
+  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
+  var iface = Components.interfaces.jband_SampleCallBackService;
+  var clazz = Components.classes["@jband/sampleCallbackService;1"];
+  var timer = clazz.createInstance(iface);
+  
+  timer.setTimer(callback, msec, msec);
+}
+</script>
+
+
+Enter msec delay...
+<form>
+<input type="text" id="msec">
+<input type="button" value="doit" onclick="startTimer();">
+</form>
+<hr>
+
+
 <p>In the spirit of "worse is better" this somewhat rough guide is being
 released to the world.  It will be expanded upon and improved.
 

Reply via email to