[XPCOM] Keep the JS context with the Service/Method objects, we
authorKalle Vahlman <kalle.vahlman@movial.com>
Tue, 11 Nov 2008 11:05:15 +0000 (13:05 +0200)
committerKalle Vahlman <kalle.vahlman@movial.com>
Tue, 11 Nov 2008 11:09:31 +0000 (13:09 +0200)
absolutely need it for Signal emission. TODO: Make sure we don't
increase the refcount for it and cause a leak, we can be pretty sure it
will outlive our objects.

xpcom-dbusservice/DBusMarshaling.cpp
xpcom-dbusservice/DBusMarshaling.h
xpcom-dbusservice/DBusMethod.cpp
xpcom-dbusservice/DBusMethod.h
xpcom-dbusservice/DBusService.cpp
xpcom-dbusservice/DBusService.h
xpcom-dbusservice/DBusSignal.cpp
xpcom-dbusservice/DBusSignal.h
xpcom-dbusservice/IDBusService.idl

index b71f23c..71e8f3d 100644 (file)
 
 #include "bdb-debug.h"
 
-void addArrayDataToIter(void *data_ptr, PRUint32 start, PRUint32 count, PRUint16 type, DBusMessageIter *aIter, DBusSignatureIter *aSigIter);
-void addJSValueToIter(JSContext *cx, jsval *aValue, DBusMessageIter *aIter, DBusSignatureIter *aSigIter);
+void getSignatureFromJSValue(JSContext *cx, jsval *aValue, nsCString &aResult);
+void getSignatureFromVariantType(PRUint16 aType, nsCString &aResult);
+void getSignatureFromISupports(JSContext* cx, nsISupports *aISupports, nsCString &aResult);
 
-static void
-listJSObjectProperties(JSContext *cx, JSObject *js_obj)
-{
-    JSIdArray *props = JS_Enumerate(cx, js_obj);
-    if (props)
-    {
-        BDBLOG(("  listJSObjectProperties: got JSIdArray with %i props\n", props->length));
+void addArrayToIter(nsIVariant *aVariant, DBusMessageIter *aIter, int aDBusType);
+void addArrayDataToIter(JSContext* cx, void *data_ptr, PRUint32 start, PRUint32 count, PRUint16 type, DBusMessageIter *aIter, DBusSignatureIter *aSigIter);
 
-        for (int i = 0; i < props->length; i++)
-        {
-            jsval v;
-            const PRUnichar *name;
-            JS_IdToValue( cx, props->vector[i], &v );
-            JSString *prop_string = JS_ValueToString(cx, v);
-            name = reinterpret_cast<const PRUnichar*>(JS_GetStringChars(prop_string));
-            nsDependentString name_s(name);
-            const char *utf8prop = NS_ConvertUTF16toUTF8(name_s).get();
-            BDBLOG(("    property %s\n", utf8prop));
-
-            jsval value;
-            if (JS_LookupUCProperty(cx,
-                                    js_obj,
-                                    JS_GetStringChars(prop_string),
-                                    JS_GetStringLength(prop_string),
-                                    &value) == JS_TRUE)
-            {
-                JSString *val_string = JS_ValueToString(cx, value);
-                const PRUnichar *valstr = reinterpret_cast<const PRUnichar*>(JS_GetStringChars(val_string));
-                nsDependentString val_s(valstr);
-                const char *utf8val = NS_ConvertUTF16toUTF8(val_s).get();
-                BDBLOG(("    value %s\n", utf8val));
-            }
-        }
-        JS_DestroyIdArray(cx, props);
-    }
-}
+void addJSValueToIter(JSContext *cx, jsval *aValue, DBusMessageIter *aIter, DBusSignatureIter *aSigIter);
+already_AddRefed<nsIWritableVariant> getVariantFromIter(DBusMessageIter *aIter, int aDBusType);
+
+void addBasicTypeToIter(nsIVariant *aVariant, DBusMessageIter *aIter, int aDBusType);
 
 void
 getSignatureFromJSValue(JSContext *cx, jsval *aValue, nsCString &aResult)
@@ -194,7 +166,7 @@ getSignatureFromJSValue(JSContext *cx, jsval *aValue, nsCString &aResult)
                 if (variant)
                 {
                     BDBLOG(("    found wrapped variant\n"));
-                    getSignatureFromVariant(variant, aResult);
+                    getSignatureFromVariant(cx, variant, aResult);
                     return;
                 }
             }
@@ -252,7 +224,7 @@ getSignatureFromVariantType(PRUint16 aType, nsCString &aResult)
 }
 
 void
-getSignatureFromVariant(nsIVariant *aVariant, nsCString &aResult)
+getSignatureFromVariant(JSContext* cx, nsIVariant *aVariant, nsCString &aResult)
 {
     aResult.Assign(DBUS_TYPE_INVALID_AS_STRING);
 
@@ -303,7 +275,7 @@ getSignatureFromVariant(nsIVariant *aVariant, nsCString &aResult)
             {
                 // get element signature from first element
                 nsISupports *element = ((nsISupports **)data_ptr)[0];
-                getSignatureFromISupports(element, elementsig);
+                getSignatureFromISupports(cx, element, elementsig);
             }
             else if (type == nsIDataType::VTYPE_ARRAY)
             {
@@ -327,7 +299,7 @@ getSignatureFromVariant(nsIVariant *aVariant, nsCString &aResult)
             nsCOMPtr<nsISupports> is;
             nsIID *iid;
             aVariant->GetAsInterface(&iid, getter_AddRefs(is));
-            getSignatureFromISupports(is, aResult);
+            getSignatureFromISupports(cx, is, aResult);
             break;
         }
         default:
@@ -339,7 +311,7 @@ getSignatureFromVariant(nsIVariant *aVariant, nsCString &aResult)
 }
 
 void
-getSignatureFromISupports(nsISupports *aISupports, nsCString &aResult)
+getSignatureFromISupports(JSContext* cx, nsISupports *aISupports, nsCString &aResult)
 {
     aResult.Assign(DBUS_TYPE_INVALID_AS_STRING);
 
@@ -348,7 +320,7 @@ getSignatureFromISupports(nsISupports *aISupports, nsCString &aResult)
     if (variant)
     {
         BDBLOG(("  getSignatureFromISupports: nsIVariant\n"));
-        getSignatureFromVariant(variant, aResult);
+        getSignatureFromVariant(cx, variant, aResult);
         return;
     }
 
@@ -360,28 +332,6 @@ getSignatureFromISupports(nsISupports *aISupports, nsCString &aResult)
         JSObject *js_obj = nsnull;
         if (NS_SUCCEEDED(wrapped->GetJSObject(&js_obj)))
         {
-            // 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;
-            BDBLOG(("    got nsIXPConnect\n"));
-
-            // get the xpconnect native call context
-            nsAXPCNativeCallContext *callContext = nsnull;
-            xpc->GetCurrentNativeCallContext(&callContext);
-            if(!callContext)
-                return;
-
-            // Get JSContext of current call
-            JSContext* cx;
-            rv = callContext->GetJSContext(&cx);
-            if(NS_FAILED(rv) || !cx)
-                return;
-            BDBLOG(("    got JSContext\n"));
-
             jsval obj_as_jsval = OBJECT_TO_JSVAL(js_obj);
             getSignatureFromJSValue(cx, &obj_as_jsval, aResult);
         }
@@ -423,7 +373,7 @@ PRBool typesMatch(PRUint16 vType, int dType)
 }
 
 void
-addVariantToIter(nsIVariant *aVariant, DBusMessageIter *aIter, DBusSignatureIter *aSigIter)
+addVariantToIter(JSContext* cx, nsIVariant *aVariant, DBusMessageIter *aIter, DBusSignatureIter *aSigIter)
 {
     char *element_signature = dbus_signature_iter_get_signature(aSigIter);
     int element_type = dbus_signature_iter_get_current_type(aSigIter);
@@ -463,30 +413,6 @@ addVariantToIter(nsIVariant *aVariant, DBusMessageIter *aIter, DBusSignatureIter
                 JSObject *js_obj = nsnull;
                 if (NS_SUCCEEDED(wrapped->GetJSObject(&js_obj)))
                 {
-                    // 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;
-                    BDBLOG(("    got nsIXPConnect\n"));
-
-                    // get the xpconnect native call context
-                    nsAXPCNativeCallContext *callContext = nsnull;
-                    xpc->GetCurrentNativeCallContext(&callContext);
-                    if(!callContext)
-                        return;
-
-                    // Get JSContext of current call
-                    JSContext* cx;
-                    rv = callContext->GetJSContext(&cx);
-                    if(NS_FAILED(rv) || !cx)
-                        return;
-                    BDBLOG(("    got JSContext\n"));
-
-                    jsval obj_as_jsval = OBJECT_TO_JSVAL(js_obj);
-
                     // try to enumerate object properties
                     JSIdArray *props = JS_Enumerate(cx, js_obj);
                     if (props)
@@ -506,6 +432,13 @@ addVariantToIter(nsIVariant *aVariant, DBusMessageIter *aIter, DBusSignatureIter
                         dbus_signature_iter_recurse(&childSigIter, &dictSigIter);
                         dbus_signature_iter_next(&dictSigIter); // key type
 
+                        nsresult rv;
+                        nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));
+                        if(NS_FAILED(rv))
+                            return;
+                        BDBLOG(("    got nsIXPConnect\n"));
+
+
                         for (int p = 0; p < props->length; p++)
                         {
                             jsval propname;
@@ -538,7 +471,7 @@ addVariantToIter(nsIVariant *aVariant, DBusMessageIter *aIter, DBusSignatureIter
                                 nsresult rs = xpc->JSToVariant(cx, propvalue, &var);
                                 NS_ENSURE_SUCCESS(rs, );
 
-                                addVariantToIter(var, &dictIter, &dictSigIter);
+                                addVariantToIter(cx, var, &dictIter, &dictSigIter);
                             }
                             
                             // Close the dict entry container
@@ -584,7 +517,7 @@ addVariantToIter(nsIVariant *aVariant, DBusMessageIter *aIter, DBusSignatureIter
                 if (dbus_message_iter_open_container(aIter, DBUS_TYPE_ARRAY,
                                                      array_signature, &arrayIter))
                 {
-                    addArrayDataToIter(data_ptr, 0, count, type,
+                    addArrayDataToIter(cx, data_ptr, 0, count, type,
                                        &arrayIter, &aChildSigIter);
                     dbus_message_iter_close_container(aIter, &arrayIter);
                 }
@@ -621,7 +554,7 @@ addVariantToIter(nsIVariant *aVariant, DBusMessageIter *aIter, DBusSignatureIter
                             break;
                     }
 
-                    addVariantToIter(variant, &arrayIter, &aChildSigIter);
+                    addVariantToIter(cx, variant, &arrayIter, &aChildSigIter);
                 }
 
                 dbus_message_iter_close_container(aIter, &arrayIter);
@@ -786,7 +719,7 @@ void addBasicTypeToIter(nsIVariant *aVariant, DBusMessageIter *aIter, int aDBusT
     }
 }
 
-void addArrayDataToIter(void *data_ptr, PRUint32 start, PRUint32 count, PRUint16 type, DBusMessageIter *aIter, DBusSignatureIter *aSigIter)
+void addArrayDataToIter(JSContext* cx, void *data_ptr, PRUint32 start, PRUint32 count, PRUint16 type, DBusMessageIter *aIter, DBusSignatureIter *aSigIter)
 {
     int aDBusType = dbus_signature_iter_get_current_type(aSigIter);
     BDBLOG(("addArrayDataToIter: appending %d elements of type %d '%c'\n", count, type, aDBusType));
@@ -855,7 +788,7 @@ void addArrayDataToIter(void *data_ptr, PRUint32 start, PRUint32 count, PRUint16
             {
                 nsCOMPtr<nsIVariant> data_variant = do_QueryInterface(data[i]);
                 if (data_variant)
-                    addVariantToIter(data_variant, aIter, aSigIter);
+                    addVariantToIter(cx, data_variant, aIter, aSigIter);
                 else
                     BDBLOG(("  interface not nsIVariant\n"));
             }
@@ -947,7 +880,7 @@ addJSValueToIter(JSContext *cx, jsval *aValue, DBusMessageIter *aIter, DBusSigna
                 nsresult rs = xpc->JSToVariant(cx, *aValue, &var);
                 NS_ENSURE_SUCCESS(rs, );
 
-                addVariantToIter(var, aIter, aSigIter);
+                addVariantToIter(cx, var, aIter, aSigIter);
             }
             
             break;
@@ -1046,7 +979,7 @@ void getJSValueFromIter(JSContext* cx, DBusMessageIter *aIter, int aDBusType, js
     return;
 }
 
-already_AddRefed<nsIWritableVariant> getVariantFromIter(DBusMessageIter *aIter, int aDBusType)
+already_AddRefed<nsIWritableVariant> getVariantFromIter(JSContext* cx, DBusMessageIter *aIter, int aDBusType)
 {
     nsCOMPtr<nsIWritableVariant> variant = do_CreateInstance("@mozilla.org/variant;1");
     nsIWritableVariant *retval;
@@ -1121,32 +1054,6 @@ already_AddRefed<nsIWritableVariant> getVariantFromIter(DBusMessageIter *aIter,
             {
                 BDBLOG(("    arg type ARRAY with DICT_ENTRY\n"));
 
-                // 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
-                // FIXME: this doesn't work for signals since there's no
-                // active context there...
-                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"));
-
                 // Construct a JS Object
                 JSObject *obj = JS_NewObject(cx, nsnull, nsnull, nsnull);
                 
@@ -1170,6 +1077,13 @@ already_AddRefed<nsIWritableVariant> getVariantFromIter(DBusMessageIter *aIter,
                     
                 } while (dbus_message_iter_next(&array_iter));
                 
+                // get the xpconnect service
+                nsresult rv;
+                nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));
+                if(NS_FAILED(rv))
+                    return nsnull;
+                BDBLOG(("    got nsIXPConnect\n"));
+
                 // Convert to variant and return
                 nsIVariant *var = nsnull;
                 nsresult rs = xpc->JSToVariant(cx, OBJECT_TO_JSVAL(obj), &var);
@@ -1188,7 +1102,7 @@ already_AddRefed<nsIWritableVariant> getVariantFromIter(DBusMessageIter *aIter,
 
                 BDBLOG(("    arg type ARRAY\n"));
                 dbus_message_iter_recurse(aIter, &array_iter);
-                items = getArrayFromIter(&array_iter);
+                items = getArrayFromIter(cx, &array_iter);
                 items->GetLength(&item_count);
                 BDBLOG(("    array: %d items\n", item_count));
 
@@ -1220,7 +1134,7 @@ already_AddRefed<nsIWritableVariant> getVariantFromIter(DBusMessageIter *aIter,
     return retval;
 }
 
-already_AddRefed<nsIMutableArray> getArrayFromIter(DBusMessageIter *aIter)
+already_AddRefed<nsIMutableArray> getArrayFromIter(JSContext* cx, DBusMessageIter *aIter)
 {
     int current_type;
     nsCOMPtr<nsIMutableArray> array = do_CreateInstance("@mozilla.org/array;1");
@@ -1230,7 +1144,7 @@ already_AddRefed<nsIMutableArray> getArrayFromIter(DBusMessageIter *aIter)
 
     while ((current_type = dbus_message_iter_get_arg_type(aIter)) != DBUS_TYPE_INVALID)
     {
-        nsCOMPtr<nsIWritableVariant> variant = getVariantFromIter(aIter, current_type);
+        nsCOMPtr<nsIWritableVariant> variant = getVariantFromIter(cx, aIter, current_type);
         if (variant)
             array->AppendElement(variant, PR_FALSE);
         else
index 8b96379..a631642 100644 (file)
 #include "nsIMutableArray.h"
 
 #include "jsapi.h"
-#include "jsarray.h"
-#include "jsobj.h"
 
-void getSignatureFromJSValue(JSContext *cx, jsval *aValue, nsCString &aResult);
+/* Acquire signature from a variant */
+void getSignatureFromVariant(JSContext* cx, nsIVariant *aVariant, nsCString &aResult);
 
-void getSignatureFromVariantType(PRUint16 aType, nsCString &aResult);
+/* Add a variant to a D-Bus message iter */
+void addVariantToIter(JSContext* cx, nsIVariant *aVariant, DBusMessageIter *aIter, DBusSignatureIter *aSigIter);
 
-void getSignatureFromVariant(nsIVariant *aVariant, nsCString &aResult);
-
-void getSignatureFromISupports(nsISupports *aISupports, nsCString &aResult);
-
-// --------------------------- OLD STUFF ---------------------------
-//
-int getDBusType(nsIVariant *aVariant);
-
-already_AddRefed<nsIMutableArray> getArrayFromIter(DBusMessageIter *aIter);
-
-already_AddRefed<nsIWritableVariant> getVariantFromIter(DBusMessageIter *aIter, int aDBusType);
-
-void addArrayToIter(nsIVariant *aVariant, DBusMessageIter *aIter, int aDBusType);
-
-void addVariantToIter(nsIVariant *aVariant, DBusMessageIter *aIter, DBusSignatureIter *aSigIter);
-void addBasicTypeToIter(nsIVariant *aVariant, DBusMessageIter *aIter, int aDBusType);
+/* Get array of variants from a D-Bus message iter */
+already_AddRefed<nsIMutableArray> getArrayFromIter(JSContext* cx, DBusMessageIter *aIter);
 
 #endif
index 7dd03b5..01d9079 100644 (file)
@@ -32,6 +32,7 @@
 #include "nsArrayUtils.h"
 #include "nsISupportsPrimitives.h"
 #include "nsIProperties.h"
+#include "nsIXPConnect.h"
 
 #include "IDBusService.h"
 #include "DBusMethod.h"
@@ -43,7 +44,7 @@
 // helper declarations
 //
 
-static void DoCallBack(IDBusMethod *aCallback, DBusMessage *aReply);
+static void DoCallBack(DBusMethod *aCallback, DBusMessage *aReply);
 static void ReplyHandler(DBusPendingCall *pending, void *user_data);
 
 //
@@ -58,7 +59,8 @@ DBusMethod::DBusMethod(DBusService *aDBusService,
                        const nsACString& aObjectPath,
                        const nsACString& aMethodName,
                        const nsACString& aInterfaceName,
-                       const nsACString& aSignature) :
+                       const nsACString& aSignature,
+                       JSContext *cx) :
     mDBusService(aDBusService),
     mBusType(aBusType),
     mDestination(aDestination),
@@ -68,7 +70,8 @@ DBusMethod::DBusMethod(DBusService *aDBusService,
     mSignature(aSignature),
     mAsync(PR_TRUE),
     mCallback(nsnull),
-    mErrorCallback(nsnull)
+    mErrorCallback(nsnull),
+    mJScx(cx)
 {
     BDBLOG(("DBusMethod::DBusMethod()\n"));
     BDBLOG(("  aBusType          : %d\n", aBusType));
@@ -142,11 +145,18 @@ DBusMethod::SetOnError(IDBusMethodCallback *aOnError)
     return NS_OK;
 }
 
+NS_IMETHODIMP
+DBusMethod::GetJSContext(JSContext **aJSContext)
+{
+    *aJSContext = mJScx;
+    return NS_OK;
+}
+
 static void
 ReplyHandler(DBusPendingCall *pending, void *user_data)
 {
     DBusMessage *reply;
-    nsCOMPtr<IDBusMethod> method = (IDBusMethod *) user_data;
+    nsCOMPtr<DBusMethod> method = (DBusMethod *) user_data;
 
     reply = dbus_pending_call_steal_reply(pending);
     DoCallBack(method, reply);
@@ -155,7 +165,7 @@ ReplyHandler(DBusPendingCall *pending, void *user_data)
 
 
 static void
-DoCallBack(IDBusMethod *aMethod, DBusMessage *aReply)
+DoCallBack(DBusMethod *aMethod, DBusMessage *aReply)
 {
     DBusMessageIter iter;
     int current_type;
@@ -166,7 +176,9 @@ DoCallBack(IDBusMethod *aMethod, DBusMessage *aReply)
 
     dbus_message_iter_init(aReply, &iter);
 
-    reply_args = getArrayFromIter(&iter);
+    JSContext *cx;
+    aMethod->GetJSContext(&cx);
+    reply_args = getArrayFromIter(cx, &iter);
 
     switch (msg_type)
     {
@@ -204,7 +216,7 @@ DoCallBack(IDBusMethod *aMethod, DBusMessage *aReply)
         /* arguments are packed as an array into an nsIVariant */
         nsIVariant **callback_args = new nsIVariant*[reply_items];
         nsCOMPtr<nsIWritableVariant> args = do_CreateInstance("@mozilla.org/variant;1");
-        for (int i = 0; i < reply_items; i++)
+        for (PRUint32 i = 0; i < reply_items; i++)
         {
             nsCOMPtr<nsIVariant> arg = do_QueryElementAt(reply_args, i);
             callback_args[i] = arg;
@@ -214,7 +226,7 @@ DoCallBack(IDBusMethod *aMethod, DBusMessage *aReply)
                          &NS_GET_IID(nsIVariant),
                          reply_items,
                          callback_args);
-        for (int i = 0; i < reply_items; i++)
+        for (PRUint32 i = 0; i < reply_items; i++)
             NS_RELEASE(callback_args[i]);
         delete[] callback_args;
         callback->OnReply(args);
@@ -247,7 +259,7 @@ DBusMethod::DoCall(nsIVariant **aArgs, PRUint32 aCount)
             nsCOMPtr<nsIVariant> data = aArgs[i];
             nsCAutoString tmpsig;
 
-            getSignatureFromVariant(data, tmpsig);
+            getSignatureFromVariant(mJScx, data, tmpsig);
             BDBLOG(("  aArgs[%02d]       : signature \"%s\"\n",
                    i,
                    PromiseFlatCString(tmpsig).get()));
@@ -275,7 +287,7 @@ DBusMethod::DoCall(nsIVariant **aArgs, PRUint32 aCount)
             BDBLOG(("  element \"%s\" from signature\n", element_signature));
             BDBLOG(("  type %c from signature\n", current_type));
 
-            addVariantToIter(aArgs[i], &msg_iter, &sig_iter);
+            addVariantToIter(mJScx, aArgs[i], &msg_iter, &sig_iter);
 
             i++;
             dbus_free(element_signature);
index feb63a4..c93ceed 100644 (file)
@@ -57,7 +57,8 @@ public:
                const nsACString& aObjectPath,
                const nsACString& aMethodName,
                const nsACString& aInterfaceName,
-               const nsACString& aSignature);
+               const nsACString& aSignature,
+               JSContext *cx);
 
 private:
     ~DBusMethod();
@@ -73,6 +74,7 @@ protected:
     PRBool      mAsync;
     IDBusMethodCallback *mCallback;
     IDBusMethodCallback *mErrorCallback;
+    JSContext *mJScx;
 };
 
 
index 96ae0a4..4e53aa2 100644 (file)
 #include "nsIGenericFactory.h"
 #include "nsIInterfaceRequestorUtils.h"
 #include "nsComponentManagerUtils.h"
+#include "nsServiceManagerUtils.h"
 
 #include "nsEmbedString.h"
 #include "nsIMutableArray.h"
 #include "nsArrayUtils.h"
+#include "nsIXPConnect.h"
 
 #include "IDBusService.h"
 
@@ -124,7 +126,8 @@ DBusService::GetSignal(PRUint32 aBusType,
                                          aInterfaceName,
                                          aSignalName,
                                          aSender,
-                                         aObjectPath);
+                                         aObjectPath,
+                                         GetCurrentJSContext());
 
     NS_ENSURE_TRUE(signal, NS_ERROR_OUT_OF_MEMORY);
 
@@ -164,7 +167,8 @@ DBusService::GetMethod(PRUint32 aBusType,
                                          aObjectPath,
                                          aMethodName,
                                          aInterfaceName,
-                                         aSignature);
+                                         aSignature,
+                                         GetCurrentJSContext());
 
     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);
-            args_array = getArrayFromIter(&iter);
+            JSContext *cx;
+            signal->GetJSContext(&cx);
+            args_array = getArrayFromIter(cx, &iter);
 
             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));
index a0619ce..83647bb 100644 (file)
@@ -77,6 +77,8 @@ private:
 
     DBusConnection *GetConnection(PRUint32 aConnType);
 
+    JSContext *GetCurrentJSContext();
+
     DBusConnection *mSystemBus;
     DBusConnection *mSessionBus;
 
index 6ec5b97..fa57679 100644 (file)
@@ -44,7 +44,8 @@ DBusSignal::DBusSignal(DBusService *aDBusService,
                        const nsACString& aInterface,
                        const nsACString& aSignal,
                        const nsACString& aSender,
-                       const nsACString& aObject) :
+                       const nsACString& aObject,
+                       JSContext *cx) :
     mDBusService(aDBusService),
     mBusType(aBusType),
     mInterface(aInterface),
@@ -53,7 +54,8 @@ DBusSignal::DBusSignal(DBusService *aDBusService,
     mObject(aObject),
     mCallback(0),
     mEnabled(PR_FALSE),
-    mFilterActive(PR_FALSE)
+    mFilterActive(PR_FALSE),
+    mJScx(cx)
 {
     BDBLOG(("DBusSignal::DBusSignal()\n"));
     BDBLOG(("  mBusType   : %d\n", mBusType));
@@ -162,6 +164,13 @@ DBusSignal::GetObjectPath(nsACString& aObject)
     return NS_OK;
 }
 
+NS_IMETHODIMP
+DBusSignal::GetJSContext(JSContext **aJSContext)
+{
+    *aJSContext = mJScx;
+    return NS_OK;
+}
+
 void
 DBusSignal::filterEnable()
 {
index e60e7d2..999b977 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "nsEmbedString.h"
 #include "nsWeakReference.h"
+#include "nsIXPConnect.h"
 
 #include "DBusService.h"
 
@@ -54,7 +55,8 @@ public:
                const nsACString& aInterface,
                const nsACString& aSignal,
                const nsACString& aSender,
-               const nsACString& aObject);
+               const nsACString& aObject,
+               JSContext *cx);
 
 private:
     ~DBusSignal();
@@ -72,6 +74,7 @@ protected:
     IDBusSignalObserver *mCallback;
     PRBool mEnabled;
     PRBool mFilterActive;
+    JSContext *mJScx;
 };
 
 
index 4289f30..b5d587f 100644 (file)
@@ -27,6 +27,7 @@
 #include "nsIWeakReference.idl"
 #include "nsIVariant.idl"
 #include "nsIArray.idl"
+#include "nsIXPConnect.idl"
 
 [function, scriptable, uuid(8eeddd8d-6e82-438a-9451-753f92bef2db)]
 interface IDBusMethodCallback : nsISupports
@@ -43,6 +44,8 @@ interface IDBusMethod : nsISupports
 
         void doCall([array, size_is(count)] in nsIVariant args,
                     in PRUint32 count);
+
+        [noscript] readonly attribute JSContextPtr JSContext;        
 };
 
 [function, scriptable, uuid(0f87e70b-af7b-44c0-b0a3-1df8e3fa66da)]
@@ -63,6 +66,8 @@ interface IDBusSignal : nsISupports
         readonly attribute ACString signalName;
         readonly attribute ACString sender;
         readonly attribute ACString objectPath;
+
+        [noscript] readonly attribute JSContextPtr JSContext;        
 };
 
 [scriptable, uuid(084b9397-0d6b-4c2c-93f9-f929f6b6e9e7)]