2012-06-11 02:17:30 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
|
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Interface to javascript engine functions.
|
|
|
|
*/
|
|
|
|
|
2019-07-12 15:40:26 +03:00
|
|
|
#ifndef NETSURF_JAVASCRIPT_JS_H_
|
|
|
|
#define NETSURF_JAVASCRIPT_JS_H_
|
2012-06-11 02:17:30 +04:00
|
|
|
|
2015-08-13 14:23:59 +03:00
|
|
|
#include "utils/errors.h"
|
|
|
|
|
2015-10-31 16:52:37 +03:00
|
|
|
struct dom_event;
|
2012-12-04 22:01:11 +04:00
|
|
|
struct dom_document;
|
|
|
|
struct dom_node;
|
2015-10-31 16:52:37 +03:00
|
|
|
struct dom_element;
|
2012-12-04 22:01:11 +04:00
|
|
|
struct dom_string;
|
|
|
|
|
2020-03-21 21:30:41 +03:00
|
|
|
/**
|
|
|
|
* JavaScript interpreter heap
|
|
|
|
*
|
|
|
|
* In order to try and be moderately performant, we create a heap
|
|
|
|
* per browser window. This heap is shared by all browsing contexts
|
|
|
|
* we end up creating in that window.
|
|
|
|
*/
|
|
|
|
typedef struct jsheap jsheap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JavaScript interpreter thread
|
|
|
|
*
|
|
|
|
* When we create a browsing context itself (window+content) we have
|
|
|
|
* to create a JS thread to attach to the browsing context.
|
|
|
|
*
|
|
|
|
* JS threads are associated with heaps and will be destroyed when
|
|
|
|
* the heap is destroyed. They can be shut down manually though
|
|
|
|
* and should be for object lifetime safety reasons.
|
|
|
|
*/
|
|
|
|
typedef struct jsthread jsthread;
|
|
|
|
|
2019-07-12 15:40:26 +03:00
|
|
|
/**
|
|
|
|
* Initialise javascript interpreter
|
|
|
|
*/
|
2012-06-11 02:17:30 +04:00
|
|
|
void js_initialise(void);
|
2012-06-26 22:55:57 +04:00
|
|
|
|
2019-07-12 15:40:26 +03:00
|
|
|
/**
|
|
|
|
* finalise javascript interpreter
|
|
|
|
*/
|
2012-06-11 02:17:30 +04:00
|
|
|
void js_finalise(void);
|
|
|
|
|
2019-07-12 15:40:26 +03:00
|
|
|
/**
|
2020-03-21 21:30:41 +03:00
|
|
|
* Create a new javascript heap.
|
2012-06-26 22:55:57 +04:00
|
|
|
*
|
2020-03-21 21:30:41 +03:00
|
|
|
* There is usually one heap per browser window.
|
2013-05-20 02:48:55 +04:00
|
|
|
*
|
2019-07-12 15:40:26 +03:00
|
|
|
* \param timeout elapsed wallclock time (in seconds) before \a callback is called
|
2020-03-21 21:30:41 +03:00
|
|
|
* \param heap Updated to the created JS heap
|
2015-08-13 14:23:59 +03:00
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise.
|
2012-06-26 22:55:57 +04:00
|
|
|
*/
|
2020-03-21 21:30:41 +03:00
|
|
|
nserror js_newheap(int timeout, jsheap **heap);
|
2012-06-26 22:55:57 +04:00
|
|
|
|
2019-07-12 15:40:26 +03:00
|
|
|
/**
|
2020-03-21 21:30:41 +03:00
|
|
|
* Destroy a previously created heap.
|
|
|
|
*
|
|
|
|
* \param heap The heap to destroy
|
2019-07-12 15:40:26 +03:00
|
|
|
*/
|
2020-03-21 21:30:41 +03:00
|
|
|
void js_destroyheap(jsheap *heap);
|
2012-06-11 02:17:30 +04:00
|
|
|
|
2019-07-12 15:40:26 +03:00
|
|
|
/**
|
2020-03-21 21:30:41 +03:00
|
|
|
* Create a new javascript thread
|
2012-06-26 22:55:57 +04:00
|
|
|
*
|
|
|
|
* This is called once for a page with javascript script tags on
|
2020-03-21 21:30:41 +03:00
|
|
|
* it. It constructs a fresh global window object and prepares the JS
|
|
|
|
* browsing context. It's important that threads are shut down cleanly
|
|
|
|
* when the browsing context is going to be cleaned up.
|
|
|
|
*
|
|
|
|
* \param heap The heap to create the thread within
|
|
|
|
* \param win_priv The value to give to the Window constructor as the window
|
|
|
|
* \param doc_priv The value to give to the Document constructor as the document
|
|
|
|
* \param thread Updated to the created thread
|
|
|
|
* \return NSERROR_OK on success, appropriate error otherwise
|
2012-06-26 22:55:57 +04:00
|
|
|
*/
|
2020-03-21 21:30:41 +03:00
|
|
|
nserror js_newthread(jsheap *heap, void *win_priv, void *doc_priv, jsthread **thread);
|
2012-06-11 02:17:30 +04:00
|
|
|
|
2020-03-22 13:14:00 +03:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2020-03-21 21:57:57 +03:00
|
|
|
/**
|
|
|
|
* Destroy a javascript thread
|
|
|
|
*
|
|
|
|
* This should be called when the browsing context is done with the thread.
|
|
|
|
*
|
2020-03-22 13:14:00 +03:00
|
|
|
* 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.
|
2020-03-21 21:57:57 +03:00
|
|
|
*
|
|
|
|
* \param thread The thread to be destroyed
|
|
|
|
*/
|
|
|
|
void js_destroythread(jsthread *thread);
|
|
|
|
|
2019-07-12 15:40:26 +03:00
|
|
|
/**
|
|
|
|
* execute some javascript in a context
|
|
|
|
*/
|
2020-03-21 21:30:41 +03:00
|
|
|
bool js_exec(jsthread *thread, const uint8_t *txt, size_t txtlen, const char *name);
|
2012-06-11 02:17:30 +04:00
|
|
|
|
2019-07-12 15:40:26 +03:00
|
|
|
/**
|
|
|
|
* fire an event at a dom node
|
|
|
|
*/
|
2020-03-21 21:30:41 +03:00
|
|
|
bool js_fire_event(jsthread *thread, const char *type, struct dom_document *doc, struct dom_node *target);
|
2012-11-28 22:07:36 +04:00
|
|
|
|
2012-12-04 22:01:11 +04:00
|
|
|
bool
|
2020-03-21 21:30:41 +03:00
|
|
|
js_dom_event_add_listener(jsthread *thread,
|
2012-12-04 22:01:11 +04:00
|
|
|
struct dom_document *document,
|
|
|
|
struct dom_node *node,
|
|
|
|
struct dom_string *event_type_dom,
|
|
|
|
void *js_funcval);
|
|
|
|
|
2015-10-31 16:52:37 +03:00
|
|
|
/*** New Events ***/
|
|
|
|
|
2019-07-12 15:40:26 +03:00
|
|
|
/**
|
|
|
|
* Handle a new element being created.
|
2015-10-31 16:52:37 +03:00
|
|
|
*
|
|
|
|
* This is called once an element is inserted into the DOM document handled
|
|
|
|
* by the context provided. The JS implementation must then scan the element
|
|
|
|
* for on* attributes and register appropriate listeners for those handlers.
|
|
|
|
*/
|
2020-03-21 21:30:41 +03:00
|
|
|
void js_handle_new_element(jsthread *thread, struct dom_element *node);
|
2015-10-31 16:52:37 +03:00
|
|
|
|
2019-07-12 15:40:26 +03:00
|
|
|
/**
|
|
|
|
* Handle an event propagation finished callback.
|
2015-10-31 16:52:37 +03:00
|
|
|
*
|
|
|
|
* This is called once an event finishes propagating, no matter how it
|
|
|
|
* finishes. The intent here is that the JS context can perform any cleanups
|
|
|
|
* it may need to perform before the DOM finishes and the event may end up
|
|
|
|
* freed.
|
|
|
|
*/
|
2020-03-21 21:30:41 +03:00
|
|
|
void js_event_cleanup(jsthread *thread, struct dom_event *evt);
|
2012-12-04 22:01:11 +04:00
|
|
|
|
2019-07-12 15:40:26 +03:00
|
|
|
#endif /* NETSURF_JAVASCRIPT_JS_H_ */
|