aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2010-10-09 11:44:36 +0200
committerMarcel Holtmann <marcel@holtmann.org>2010-10-09 11:44:36 +0200
commit84ba5998b3c0772eb7fba25bd545c9b0caff034f (patch)
tree24fba16b69396bd8a7269626b31cfb78b5d8c965
parentb33ef3e407b88536d876d8591cf0741e2b369e77 (diff)
downloadpacrunner-84ba5998b3c0772eb7fba25bd545c9b0caff034f.tar.gz
Run Javascript execution in a thread
-rw-r--r--src/client.c62
1 files changed, 53 insertions, 9 deletions
diff --git a/src/client.c b/src/client.c
index 059ddd3..9640ebd 100644
--- a/src/client.c
+++ b/src/client.c
@@ -27,35 +27,79 @@
#include "pacrunner.h"
-static DBusMessage *find_proxy_for_url(DBusConnection *conn,
- DBusMessage *msg, void *user_data)
+struct jsrun_data {
+ DBusConnection *conn;
+ DBusMessage *msg;
+ GThread *thread;
+};
+
+static void jsrun_free(gpointer data)
{
+ struct jsrun_data *jsrun = data;
+
+ dbus_message_unref(jsrun->msg);
+ dbus_connection_unref(jsrun->conn);
+ g_free(jsrun);
+}
+
+static gpointer jsrun_thread(gpointer data)
+{
+ struct jsrun_data *jsrun = data;
const char *sender, *url, *host, *result;
- sender = dbus_message_get_sender(msg);
+ sender = dbus_message_get_sender(jsrun->msg);
DBG("sender %s", sender);
- dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &url,
+ dbus_message_get_args(jsrun->msg, NULL, DBUS_TYPE_STRING, &url,
DBUS_TYPE_STRING, &host,
DBUS_TYPE_INVALID);
DBG("url %s host %s", url, host);
result = __pacrunner_mozjs_execute(url, host);
+
+ DBG("result %s", result);
+
if (result == NULL)
+ result = "DIRECT";
+
+ g_dbus_send_reply(jsrun->conn, jsrun->msg, DBUS_TYPE_STRING, &result,
+ DBUS_TYPE_INVALID);
+
+ jsrun_free(jsrun);
+
+ return NULL;
+}
+
+static DBusMessage *find_proxy_for_url(DBusConnection *conn,
+ DBusMessage *msg, void *user_data)
+{
+ struct jsrun_data *jsrun;
+
+ jsrun = g_try_new0(struct jsrun_data, 1);
+ if (jsrun == NULL)
return g_dbus_create_error(msg,
PACRUNNER_ERROR_INTERFACE ".Failed",
- "PAC execution failed");
+ "Out of memory");
- DBG("result %s", result);
+ jsrun->conn = dbus_connection_ref(conn);
+ jsrun->msg = dbus_message_ref(msg);
- return g_dbus_create_reply(msg, DBUS_TYPE_STRING, &result,
- DBUS_TYPE_INVALID);
+ jsrun->thread = g_thread_create(jsrun_thread, jsrun, FALSE, NULL);
+ if (jsrun->thread == NULL) {
+ jsrun_free(jsrun);
+ return g_dbus_create_error(msg,
+ PACRUNNER_ERROR_INTERFACE ".Failed",
+ "Thread execution failed");
+ }
+
+ return NULL;
}
static GDBusMethodTable client_methods[] = {
- { "FindProxyForURL", "ss", "s", find_proxy_for_url },
+ { "FindProxyForURL", "ss", "s", find_proxy_for_url,
+ G_DBUS_METHOD_FLAG_ASYNC },
{ },
};