[XPCOM] Keep the JS context with the Service/Method objects, we
[browser-dbus-bridge.git] / xpcom-dbusservice / DBusService.cpp
index 96ae0a4c2f9bcba9dd7f9d0c76117ff38192275c..4e53aa2c5cbe7b2f844ebba368882d1594959e0d 100644 (file)
 #include "nsIGenericFactory.h"
 #include "nsIInterfaceRequestorUtils.h"
 #include "nsComponentManagerUtils.h"
 #include "nsIGenericFactory.h"
 #include "nsIInterfaceRequestorUtils.h"
 #include "nsComponentManagerUtils.h"
+#include "nsServiceManagerUtils.h"
 
 #include "nsEmbedString.h"
 #include "nsIMutableArray.h"
 #include "nsArrayUtils.h"
 
 #include "nsEmbedString.h"
 #include "nsIMutableArray.h"
 #include "nsArrayUtils.h"
+#include "nsIXPConnect.h"
 
 #include "IDBusService.h"
 
 
 #include "IDBusService.h"
 
@@ -124,7 +126,8 @@ DBusService::GetSignal(PRUint32 aBusType,
                                          aInterfaceName,
                                          aSignalName,
                                          aSender,
                                          aInterfaceName,
                                          aSignalName,
                                          aSender,
-                                         aObjectPath);
+                                         aObjectPath,
+                                         GetCurrentJSContext());
 
     NS_ENSURE_TRUE(signal, NS_ERROR_OUT_OF_MEMORY);
 
 
     NS_ENSURE_TRUE(signal, NS_ERROR_OUT_OF_MEMORY);
 
@@ -164,7 +167,8 @@ DBusService::GetMethod(PRUint32 aBusType,
                                          aObjectPath,
                                          aMethodName,
                                          aInterfaceName,
                                          aObjectPath,
                                          aMethodName,
                                          aInterfaceName,
-                                         aSignature);
+                                         aSignature,
+                                         GetCurrentJSContext());
 
     NS_ENSURE_TRUE(method, NS_ERROR_OUT_OF_MEMORY);
 
 
     NS_ENSURE_TRUE(method, NS_ERROR_OUT_OF_MEMORY);
 
@@ -270,7 +274,9 @@ DBusHandlerResult _signal_filter(DBusConnection *connection,
             nsCOMPtr<nsIMutableArray> args_array;
 
             dbus_message_iter_init(message, &iter);
             nsCOMPtr<nsIMutableArray> args_array;
 
             dbus_message_iter_init(message, &iter);
-            args_array = getArrayFromIter(&iter);
+            JSContext *cx;
+            signal->GetJSContext(&cx);
+            args_array = getArrayFromIter(cx, &iter);
 
             PRUint32 arg_items;
             args_array->GetLength(&arg_items);
 
             PRUint32 arg_items;
             args_array->GetLength(&arg_items);
@@ -497,6 +503,35 @@ void DBusService::RemoveSignalObserver(IDBusSignal *aSignal)
     }
 }
 
     }
 }
 
+JSContext *DBusService::GetCurrentJSContext()
+{
+    // try to get a JS context (code borrowed from xpcsample1.cpp)
+
+    // get the xpconnect service
+    nsresult rv;
+    nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));
+    if(NS_FAILED(rv))
+        return nsnull;
+    BDBLOG(("    got nsIXPConnect\n"));
+
+    // get the xpconnect native call context
+    nsAXPCNativeCallContext *callContext = nsnull;
+    xpc->GetCurrentNativeCallContext(&callContext);
+    if(!callContext)
+    {
+    BDBLOG(("    callContext :(\n"));
+        return nsnull;
+    }
+    // Get JSContext of current call
+    JSContext* cx;
+    rv = callContext->GetJSContext(&cx);
+    if(NS_FAILED(rv) || !cx)
+        return nsnull;
+    BDBLOG(("    got JSContext\n"));
+
+    return cx;
+}
+
 DBusConnection *DBusService::GetConnection(PRUint32 aConnType)
 {
     BDBLOG(("DBusService::GetConnection(%d)\n", aConnType));
 DBusConnection *DBusService::GetConnection(PRUint32 aConnType)
 {
     BDBLOG(("DBusService::GetConnection(%d)\n", aConnType));