From d1e2eef18b25e97c7c06068aa519003605202853 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sun, 22 Mar 2020 10:14:00 +0000 Subject: [PATCH] JS: Add concept of js_closethread In order to better model content close vs destroy, add the concept of closing a thread to the JS interface. Signed-off-by: Daniel Silverstone --- content/handlers/javascript/duktape/dukky.c | 20 ++++++++++++++++++++ content/handlers/javascript/js.h | 21 +++++++++++++++++---- content/handlers/javascript/none/none.c | 5 +++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/content/handlers/javascript/duktape/dukky.c b/content/handlers/javascript/duktape/dukky.c index bb227d44d..7357863cd 100644 --- a/content/handlers/javascript/duktape/dukky.c +++ b/content/handlers/javascript/duktape/dukky.c @@ -754,6 +754,26 @@ nserror js_newthread(jsheap *heap, void *win_priv, void *doc_priv, jsthread **th #undef CTX #define CTX (thread->ctx) +/* exported interface documented in js.h */ +nserror js_closethread(jsthread *thread) +{ + /* We can always close down a thread, it might just confuse + * the code running, though we don't mind since we're in the + * process of destruction at this point + */ + duk_int_t top = duk_get_top(CTX); + + /* Closing down the extant thread */ + NSLOG(dukky, DEBUG, "Closing down extant thread %p in heap %p", thread, thread->heap); + duk_get_global_string(CTX, MAGIC(closedownThread)); + dukky_pcall(CTX, 0, true); + + /* Restore whatever stack we had */ + duk_set_top(CTX, top); + + return NSERROR_OK; +} + /** * Destroy a Duktape thread */ diff --git a/content/handlers/javascript/js.h b/content/handlers/javascript/js.h index ce9bb9ba4..55f8e22ef 100644 --- a/content/handlers/javascript/js.h +++ b/content/handlers/javascript/js.h @@ -96,15 +96,28 @@ void js_destroyheap(jsheap *heap); */ nserror js_newthread(jsheap *heap, void *win_priv, void *doc_priv, jsthread **thread); +/** + * Close a javascript thread + * + * This should be called when the HTML content which owns the thread is + * being closed. This is a separate process from destroying the thread + * and merely disconnects any callbacks and thus hopefully stops + * additional JS things from triggering. If any code runs and attempts to + * register callbacks after closedown, they will fail. + * + * \param thread The thread to close down + * \return NSERROR_OK on success, appropriate error otherwise + */ +nserror js_closethread(jsthread *thread); + /** * Destroy a javascript thread * * This should be called when the browsing context is done with the thread. * - * Essentially it should be called when the content is about to be destroyed - * but in reality it can be called when the browser window relinquishes its - * handle on the content since nominally the browser window itself owns - * the thread. + * This will be called when the HTML content associated with the browsing + * context is being destroyed. The thread should have already been closed + * down during the HTML content close. * * \param thread The thread to be destroyed */ diff --git a/content/handlers/javascript/none/none.c b/content/handlers/javascript/none/none.c index ff09f52c9..ee01730d2 100644 --- a/content/handlers/javascript/none/none.c +++ b/content/handlers/javascript/none/none.c @@ -51,6 +51,11 @@ nserror js_newthread(jsheap *heap, void *win_priv, void *doc_priv, jsthread **th return NSERROR_NOT_IMPLEMENTED; } +nserror js_closethread(jsthread *thread) +{ + return NSERROR_OK; +} + void js_destroythread(jsthread *thread) { }