Send connman mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        https://lists.01.org/mailman/listinfo/connman
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of connman digest..."


Today's Topics:

   1. [PATCH 3/6] proxy: Add get/set methods for new jsctx pointer
      (David Woodhouse)
   2. [PATCH 4/6] proxy: Add pacrunner_js_clear_proxy()
      (David Woodhouse)
   3. [PATCH 5/6] mozjs: fix global variable storage (David Woodhouse)


----------------------------------------------------------------------

Message: 1
Date: Mon, 20 Jun 2016 14:25:06 +0100
From: David Woodhouse <[email protected]>
To: [email protected]
Subject: [PATCH 3/6] proxy: Add get/set methods for new jsctx pointer
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

If we want the JS backends to be able to store context per proxy config,
then we need to give them somewhere to put it...
---
 src/pacrunner.h |  3 +++
 src/proxy.c     | 11 +++++++++++
 2 files changed, 14 insertions(+)

diff --git a/src/pacrunner.h b/src/pacrunner.h
index 71913bb..7b7ad15 100644
--- a/src/pacrunner.h
+++ b/src/pacrunner.h
@@ -76,6 +76,9 @@ int pacrunner_proxy_disable(struct pacrunner_proxy *proxy);
 
 char *pacrunner_proxy_lookup(const char *url, const char *host);
 
+void __pacrunner_proxy_set_jsctx(struct pacrunner_proxy *proxy, void *jsctx);
+void *__pacrunner_proxy_get_jsctx(struct pacrunner_proxy *proxy);
+
 int __pacrunner_proxy_init(void);
 void __pacrunner_proxy_cleanup(void);
 
diff --git a/src/proxy.c b/src/proxy.c
index e913a3a..47be08a 100644
--- a/src/proxy.c
+++ b/src/proxy.c
@@ -41,6 +41,7 @@ struct pacrunner_proxy {
        GList **servers;
        GList **excludes;
        GList *domains;
+       void *jsctx;
 };
 
 struct proxy_domain {
@@ -584,6 +585,16 @@ static gboolean proxy_config_timeout(gpointer user_data)
        return FALSE;
 }
 
+void __pacrunner_proxy_set_jsctx(struct pacrunner_proxy *proxy, void *jsctx)
+{
+       proxy->jsctx = jsctx;
+}
+
+void *__pacrunner_proxy_get_jsctx(struct pacrunner_proxy *proxy)
+{
+       return proxy->jsctx;
+}
+
 int __pacrunner_proxy_init(void)
 {
        DBG("");
-- 
2.7.4

-- 
David Woodhouse                            Open Source Technology Centre
[email protected]                              Intel Corporation
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 5760 bytes
Desc: not available
URL: 
<http://lists.01.org/pipermail/connman/attachments/20160620/501a19bf/attachment-0001.bin>

------------------------------

Message: 2
Date: Mon, 20 Jun 2016 14:25:27 +0100
From: David Woodhouse <[email protected]>
To: [email protected]
Subject: [PATCH 4/6] proxy: Add pacrunner_js_clear_proxy()
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

We'll want this for a sane per-proxy-config cleanup of the JS context.
---
 plugins/v8.cc   |  1 +
 src/js.c        | 16 ++++++++++++++++
 src/js.h        |  1 +
 src/pacrunner.h |  1 +
 src/proxy.c     |  2 ++
 5 files changed, 21 insertions(+)

diff --git a/plugins/v8.cc b/plugins/v8.cc
index e947bed..93a1ea5 100644
--- a/plugins/v8.cc
+++ b/plugins/v8.cc
@@ -302,6 +302,7 @@ static struct pacrunner_js_driver v8_driver = {
        "v8",
        PACRUNNER_JS_PRIORITY_HIGH,
        v8_set_proxy,
+       NULL,
        v8_execute,
 };
 
diff --git a/src/js.c b/src/js.c
index ff08cee..3a9274e 100644
--- a/src/js.c
+++ b/src/js.c
@@ -69,6 +69,22 @@ int __pacrunner_js_set_proxy(struct pacrunner_proxy *proxy)
        return -ENXIO;
 }
 
+int __pacrunner_js_clear_proxy(struct pacrunner_proxy *proxy)
+{
+       GSList *list;
+
+       for (list = js_driver_list; list; list = list->next) {
+               struct pacrunner_js_driver *driver = list->data;
+
+               if (driver->clear_proxy)
+                       return driver->clear_proxy(proxy);
+               else
+                       return 0;
+       }
+
+       return -ENXIO;
+}
+
 char *__pacrunner_js_execute(struct pacrunner_proxy *proxy, const char *url,
                             const char *host)
 {
diff --git a/src/js.h b/src/js.h
index ea76849..e5d7abc 100644
--- a/src/js.h
+++ b/src/js.h
@@ -27,6 +27,7 @@ struct pacrunner_js_driver {
        const char *name;
        int priority;
        int (*set_proxy) (struct pacrunner_proxy *proxy);
+       int (*clear_proxy) (struct pacrunner_proxy *proxy);
        char *(*execute)(struct pacrunner_proxy *proxy, const char *url,
                         const char *host);
 };
diff --git a/src/pacrunner.h b/src/pacrunner.h
index 7b7ad15..87c51da 100644
--- a/src/pacrunner.h
+++ b/src/pacrunner.h
@@ -113,5 +113,6 @@ char *__pacrunner_mozjs_execute(const char *url, const char 
*host);
 int __pacrunner_js_init(void);
 void __pacrunner_js_cleanup(void);
 int __pacrunner_js_set_proxy(struct pacrunner_proxy *proxy);
+int __pacrunner_js_clear_proxy(struct pacrunner_proxy *proxy);
 char *__pacrunner_js_execute(struct pacrunner_proxy *proxy, const char *url,
                             const char *host);
diff --git a/src/proxy.c b/src/proxy.c
index 47be08a..08ae3dd 100644
--- a/src/proxy.c
+++ b/src/proxy.c
@@ -128,6 +128,8 @@ void pacrunner_proxy_unref(struct pacrunner_proxy *proxy)
        if (!g_atomic_int_dec_and_test(&proxy->refcount))
                return;
 
+       __pacrunner_js_clear_proxy(proxy);
+
        reset_proxy(proxy);
 
        g_list_free_full(proxy->domains, proxy_domain_destroy);
-- 
2.7.4

-- 
David Woodhouse                            Open Source Technology Centre
[email protected]                              Intel Corporation
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 5760 bytes
Desc: not available
URL: 
<http://lists.01.org/pipermail/connman/attachments/20160620/4871ea0b/attachment-0001.bin>

------------------------------

Message: 3
Date: Mon, 20 Jun 2016 14:26:13 +0100
From: David Woodhouse <[email protected]>
To: [email protected]
Subject: [PATCH 5/6] mozjs: fix global variable storage
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

Support a JS context per proxy configuration, instead of only one at a
time.
---
 plugins/mozjs.c | 121 +++++++++++++++++++++++++++++++-------------------------
 1 file changed, 68 insertions(+), 53 deletions(-)

diff --git a/plugins/mozjs.c b/plugins/mozjs.c
index 77a0c13..130c3c9 100644
--- a/plugins/mozjs.c
+++ b/plugins/mozjs.c
@@ -44,7 +44,11 @@
 
 static pthread_mutex_t mozjs_mutex = PTHREAD_MUTEX_INITIALIZER;
 
-static struct pacrunner_proxy *current_proxy = NULL;
+struct pacrunner_mozjs {
+       struct pacrunner_proxy *proxy;
+       JSContext *jsctx;
+       JSObject *jsobj;
+};
 
 static int getaddr(const char *node, char *host, size_t hostlen)
 {
@@ -91,19 +95,20 @@ static int resolve(const char *node, char *host, size_t 
hostlen)
        return 0;
 }
 
-static JSBool myipaddress(JSContext *ctx, uintN argc, jsval *vp)
+static JSBool myipaddress(JSContext *jsctx, uintN argc, jsval *vp)
 {
+       struct pacrunner_mozjs *ctx = JS_GetContextPrivate(jsctx);
        const char *interface;
        char address[NI_MAXHOST];
 
        DBG("");
 
-       JS_SET_RVAL(ctx, vp, JSVAL_NULL);
+       JS_SET_RVAL(jsctx, vp, JSVAL_NULL);
 
-       if (!current_proxy)
+       if (!ctx)
                return JS_TRUE;
 
-       interface = pacrunner_proxy_get_interface(current_proxy);
+       interface = pacrunner_proxy_get_interface(ctx->proxy);
        if (!interface)
                return JS_TRUE;
 
@@ -112,7 +117,8 @@ static JSBool myipaddress(JSContext *ctx, uintN argc, jsval 
*vp)
 
        DBG("address %s", address);
 
-       JS_SET_RVAL(ctx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(ctx, address)));
+       JS_SET_RVAL(jsctx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(jsctx,
+                                                              address)));
 
        return JS_TRUE;
 }
@@ -160,103 +166,113 @@ static JSClass jscls = {
 };
 
 static JSRuntime *jsrun;
-static JSContext *jsctx = NULL;
-static JSObject *jsobj = NULL;
 
-static void create_object(void)
+static int create_object(struct pacrunner_proxy *proxy)
 {
+       struct pacrunner_mozjs *ctx;
        const char *script;
        jsval rval;
 
-       if (!current_proxy)
-               return;
-
-       script = pacrunner_proxy_get_script(current_proxy);
+       script = pacrunner_proxy_get_script(proxy);
        if (!script)
-               return;
+               return 0;
 
-       jsctx = JS_NewContext(jsrun, 8 * 1024);
+       ctx = g_malloc0(sizeof(struct pacrunner_mozjs));
+
+       ctx->proxy = proxy;
+       ctx->jsctx = JS_NewContext(jsrun, 8 * 1024);
+       if (!ctx->jsctx) {
+               g_free(ctx);
+               return -ENOMEM;
+       }
+       JS_SetContextPrivate(ctx->jsctx, ctx);
+       __pacrunner_proxy_set_jsctx(proxy, ctx);
 
 #if JS_VERSION >= 185
-       jsobj = JS_NewCompartmentAndGlobalObject(jsctx, &jscls, NULL);
+       ctx->jsobj = JS_NewCompartmentAndGlobalObject(ctx->jsctx, &jscls,
+                                                     NULL);
 #else
-       jsobj = JS_NewObject(jsctx, &jscls, NULL, NULL);
+       ctx->jsobj = JS_NewObject(ctx->jsctx, &jscls, NULL, NULL);
 #endif
 
-       if (!JS_InitStandardClasses(jsctx, jsobj))
+       if (!JS_InitStandardClasses(ctx->jsctx, ctx->jsobj))
                pacrunner_error("Failed to init JS standard classes");
 
-       JS_DefineFunction(jsctx, jsobj, "myIpAddress", myipaddress, 0, 0);
-       JS_DefineFunction(jsctx, jsobj, "dnsResolve", dnsresolve, 1, 0);
+       JS_DefineFunction(ctx->jsctx, ctx->jsobj, "myIpAddress",
+                         myipaddress, 0, 0);
+       JS_DefineFunction(ctx->jsctx, ctx->jsobj,
+                         "dnsResolve", dnsresolve, 1, 0);
 
-       JS_EvaluateScript(jsctx, jsobj, JAVASCRIPT_ROUTINES,
-                               strlen(JAVASCRIPT_ROUTINES), NULL, 0, &rval);
+       JS_EvaluateScript(ctx->jsctx, ctx->jsobj, JAVASCRIPT_ROUTINES,
+                         strlen(JAVASCRIPT_ROUTINES), NULL, 0, &rval);
 
-       JS_EvaluateScript(jsctx, jsobj, script, strlen(script),
-                                               "wpad.dat", 0, &rval);
+       JS_EvaluateScript(ctx->jsctx, ctx->jsobj, script, strlen(script),
+                         "wpad.dat", 0, &rval);
+
+       return 0;
 }
 
-static void destroy_object(void)
+static int mozjs_clear_proxy(struct pacrunner_proxy *proxy)
 {
-       if (!jsctx)
-               return;
+       struct pacrunner_mozjs *ctx = __pacrunner_proxy_get_jsctx(proxy);
 
-       JS_DestroyContext(jsctx);
-       jsctx = NULL;
+       DBG("proxy %p ctx %p", proxy, ctx);
 
-       jsobj = NULL;
+       if (!ctx)
+               return -EINVAL;
+
+       JS_DestroyContext(ctx->jsctx);
+       __pacrunner_proxy_set_jsctx(proxy, NULL);
+
+       return 0;
 }
 
 static int mozjs_set_proxy(struct pacrunner_proxy *proxy)
 {
        DBG("proxy %p", proxy);
 
-       if (current_proxy)
-               destroy_object();
-
-       current_proxy = proxy;
+       if (!proxy)
+               return 0;
 
-       if (current_proxy)
-               create_object();
+       mozjs_clear_proxy(proxy);
 
-       return 0;
+       return create_object(proxy);
 }
 
 static char * mozjs_execute(struct pacrunner_proxy *proxy, const char *url,
                            const char *host)
 {
+       struct pacrunner_mozjs *ctx = __pacrunner_proxy_get_jsctx(proxy);
        JSBool result;
        jsval rval, args[2];
        char *answer, *g_answer;
 
-       DBG("proxy %p url %s host %s", proxy, url, host);
-
-       if (!jsctx)
-               return NULL;
+       DBG("proxy %p ctx %p url %s host %s", proxy, ctx, url, host);
 
-       if (proxy != current_proxy && mozjs_set_proxy(proxy))
+       if (!ctx)
                return NULL;
 
        pthread_mutex_lock(&mozjs_mutex);
 
-       JS_BeginRequest(jsctx);
+       JS_BeginRequest(ctx->jsctx);
 
-       args[0] = STRING_TO_JSVAL(JS_NewStringCopyZ(jsctx, url));
-       args[1] = STRING_TO_JSVAL(JS_NewStringCopyZ(jsctx, host));
+       args[0] = STRING_TO_JSVAL(JS_NewStringCopyZ(ctx->jsctx, url));
+       args[1] = STRING_TO_JSVAL(JS_NewStringCopyZ(ctx->jsctx, host));
 
-       result = JS_CallFunctionName(jsctx, jsobj, "FindProxyForURL",
-                                                       2, args, &rval);
+       result = JS_CallFunctionName(ctx->jsctx, ctx->jsobj,
+                                    "FindProxyForURL", 2, args, &rval);
 
-       JS_EndRequest(jsctx);
+       JS_EndRequest(ctx->jsctx);
 
-       JS_MaybeGC(jsctx);
+       JS_MaybeGC(ctx->jsctx);
 
        pthread_mutex_unlock(&mozjs_mutex);
 
        if (result) {
-               answer = JS_EncodeString(jsctx, JS_ValueToString(jsctx, rval));
+               answer = JS_EncodeString(ctx->jsctx,
+                                        JS_ValueToString(ctx->jsctx, rval));
                g_answer = g_strdup(answer);
-               JS_free(jsctx, answer);
+               JS_free(ctx->jsctx, answer);
                return g_answer;
        }
 
@@ -267,6 +283,7 @@ static struct pacrunner_js_driver mozjs_driver = {
        .name           = "mozjs",
        .priority       = PACRUNNER_JS_PRIORITY_DEFAULT,
        .set_proxy      = mozjs_set_proxy,
+       .clear_proxy    = mozjs_clear_proxy,
        .execute        = mozjs_execute,
 };
 
@@ -285,8 +302,6 @@ static void mozjs_exit(void)
 
        pacrunner_js_driver_unregister(&mozjs_driver);
 
-       mozjs_set_proxy(NULL);
-
        JS_DestroyRuntime(jsrun);
 }
 
-- 
2.7.4

-- 
David Woodhouse                            Open Source Technology Centre
[email protected]                              Intel Corporation
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 5760 bytes
Desc: not available
URL: 
<http://lists.01.org/pipermail/connman/attachments/20160620/afdfdc5f/attachment.bin>

------------------------------

Subject: Digest Footer

_______________________________________________
connman mailing list
[email protected]
https://lists.01.org/mailman/listinfo/connman


------------------------------

End of connman Digest, Vol 8, Issue 28
**************************************

Reply via email to