1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00
elinks/src/ecmascript/ecmascript.c

345 lines
9.3 KiB
C
Raw Normal View History

/* Base ECMAScript file. Mostly a proxy for specific library backends. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include "elinks.h"
#include "config/options.h"
#include "document/document.h"
#include "document/view.h"
#include "ecmascript/ecmascript.h"
2006-01-27 04:08:19 -05:00
#include "ecmascript/spidermonkey.h"
#include "intl/gettext/libintl.h"
#include "main/module.h"
#include "main/timer.h"
2006-07-09 07:02:44 -04:00
#include "osdep/osdep.h"
#include "protocol/protocol.h"
#include "protocol/uri.h"
#include "session/session.h"
#include "session/task.h"
#include "terminal/terminal.h"
#include "terminal/window.h"
#include "util/conv.h"
#include "util/string.h"
#include "viewer/text/view.h" /* current_frame() */
#include "viewer/text/form.h" /* <-ecmascript_reset_state() */
#include "viewer/text/vs.h"
/* TODO: We should have some kind of ACL for the scripts - i.e. ability to
* disallow the scripts to open new windows (or so that the windows are always
* directed to tabs, this particular option would be a tristate), disallow
* messing with your menubar/statusbar visibility, disallow changing the
* statusbar content etc. --pasky */
bug 764: Initialize the right member of union option_value INIT_OPTION used to initialize union option_value at compile time by casting the default value to LIST_OF(struct option) *, which is the type of the first member. On sparc64 and other big-endian systems where sizeof(int) < sizeof(struct list_head *), this tended to leave option->value.number as zero, thus messing up OPT_INT and OPT_BOOL at least. OPT_LONG however tended to work right. This would be easy to fix with C99 designated initializers, but doc/hacking.txt says ELinks must be kept C89 compatible. Another solution would be to make register_options() read the value from option->value.tree (the first member), cast it back to the right type, and write it to the appropriate member; but that would still require somewhat dubious conversions between integers, data pointers, and function pointers. So here's a rather more invasive solution. Add struct option_init, which is somewhat similar to struct option but has non-overlapping members for different types of values, to ensure nothing is lost in compile-time conversions. Move unsigned char *path from struct option_info to struct option_init, and replace struct option_info with a union that contains struct option_init and struct option. Now, this union can be initialized with no portability problems, and register_options() then moves the values from struct option_init to their final places in struct option. In my x86 ELinks build with plenty of options configured in, this change bloated the text section by 340 bytes but compressed the data section by 2784 bytes, presumably because union option_info is a pointer smaller than struct option_info was. (cherry picked from elinks-0.12 commit e5f6592ee20780a61f70feeb1f9e17631b9c5835) Conflicts: src/protocol/fsp/fsp.c: All options had been removed in 0.13.GIT. src/protocol/smb/smb2.c: Ditto.
2009-08-15 15:39:07 -04:00
static union option_info ecmascript_options[] = {
INIT_OPT_TREE("", N_("ECMAScript"),
"ecmascript", 0,
N_("ECMAScript options.")),
INIT_OPT_BOOL("ecmascript", N_("Enable"),
"enable", 0, 0,
N_("Whether to run those scripts inside of documents.")),
INIT_OPT_BOOL("ecmascript", N_("Script error reporting"),
"error_reporting", 0, 0,
N_("Open a message box when a script reports an error.")),
INIT_OPT_BOOL("ecmascript", N_("Ignore <noscript> content"),
"ignore_noscript", 0, 0,
N_("Whether to ignore content enclosed by the <noscript> tag "
"when ECMAScript is enabled.")),
INIT_OPT_INT("ecmascript", N_("Maximum execution time"),
"max_exec_time", 0, 1, 3600, 5,
N_("Maximum execution time in seconds for a script.")),
INIT_OPT_BOOL("ecmascript", N_("Pop-up window blocking"),
"block_window_opening", 0, 0,
N_("Whether to disallow scripts to open new windows or tabs.")),
NULL_OPTION_INFO,
};
static int interpreter_count;
2006-01-27 04:08:19 -05:00
struct ecmascript_interpreter *
ecmascript_get_interpreter(struct view_state *vs)
{
struct ecmascript_interpreter *interpreter;
assert(vs);
interpreter = mem_calloc(1, sizeof(*interpreter));
if (!interpreter)
return NULL;
++interpreter_count;
2006-01-27 04:08:19 -05:00
interpreter->vs = vs;
2007-06-21 04:31:12 -04:00
interpreter->vs->ecmascript_fragile = 0;
2006-01-27 04:08:19 -05:00
init_list(interpreter->onload_snippets);
/* The following backend call reads interpreter->vs. */
if (
!spidermonkey_get_interpreter(interpreter)
) {
/* Undo what was done above. */
interpreter->vs->ecmascript_fragile = 1;
mem_free(interpreter);
--interpreter_count;
return NULL;
}
2006-10-23 16:45:55 -04:00
init_string(&interpreter->code);
2006-01-27 04:08:19 -05:00
return interpreter;
}
void
ecmascript_put_interpreter(struct ecmascript_interpreter *interpreter)
{
assert(interpreter);
assert(interpreter->backend_nesting == 0);
/* If the assertion fails, it is better to leak the
* interpreter than to corrupt memory. */
if_assert_failed return;
2006-01-27 04:08:19 -05:00
spidermonkey_put_interpreter(interpreter);
free_string_list(&interpreter->onload_snippets);
2006-10-23 16:45:55 -04:00
done_string(&interpreter->code);
/* Is it superfluous? */
if (interpreter->vs->doc_view)
kill_timer(&interpreter->vs->doc_view->document->timeout);
2006-01-27 04:08:19 -05:00
interpreter->vs->ecmascript = NULL;
2007-05-21 06:50:27 -04:00
interpreter->vs->ecmascript_fragile = 1;
2006-01-27 04:08:19 -05:00
mem_free(interpreter);
--interpreter_count;
}
int
ecmascript_get_interpreter_count(void)
{
return interpreter_count;
2006-01-27 04:08:19 -05:00
}
void
ecmascript_eval(struct ecmascript_interpreter *interpreter,
2006-01-27 06:07:45 -05:00
struct string *code, struct string *ret)
2006-01-27 04:08:19 -05:00
{
if (!get_ecmascript_enable())
return;
assert(interpreter);
interpreter->backend_nesting++;
2006-01-27 06:07:45 -05:00
spidermonkey_eval(interpreter, code, ret);
interpreter->backend_nesting--;
2006-01-27 04:08:19 -05:00
}
unsigned char *
ecmascript_eval_stringback(struct ecmascript_interpreter *interpreter,
struct string *code)
{
unsigned char *result;
2006-01-27 04:08:19 -05:00
if (!get_ecmascript_enable())
return NULL;
assert(interpreter);
interpreter->backend_nesting++;
result = spidermonkey_eval_stringback(interpreter, code);
interpreter->backend_nesting--;
return result;
2006-01-27 04:08:19 -05:00
}
int
ecmascript_eval_boolback(struct ecmascript_interpreter *interpreter,
struct string *code)
{
int result;
2006-01-27 04:08:19 -05:00
if (!get_ecmascript_enable())
return -1;
assert(interpreter);
interpreter->backend_nesting++;
result = spidermonkey_eval_boolback(interpreter, code);
interpreter->backend_nesting--;
return result;
2006-01-27 04:08:19 -05:00
}
void
ecmascript_detach_form_view(struct form_view *fv)
{
spidermonkey_detach_form_view(fv);
}
void ecmascript_detach_form_state(struct form_state *fs)
{
spidermonkey_detach_form_state(fs);
}
void ecmascript_moved_form_state(struct form_state *fs)
{
spidermonkey_moved_form_state(fs);
}
2006-01-27 04:08:19 -05:00
void
ecmascript_reset_state(struct view_state *vs)
{
struct form_view *fv;
int i;
/* Normally, if vs->ecmascript == NULL, the associated
* ecmascript_obj pointers are also NULL. However, they might
* be non-NULL if the ECMAScript objects have been lazily
* created because of scripts running in sibling HTML frames. */
foreach (fv, vs->forms)
ecmascript_detach_form_view(fv);
for (i = 0; i < vs->form_info_len; i++)
ecmascript_detach_form_state(&vs->form_info[i]);
vs->ecmascript_fragile = 0;
if (vs->ecmascript)
ecmascript_put_interpreter(vs->ecmascript);
vs->ecmascript = ecmascript_get_interpreter(vs);
if (!vs->ecmascript)
vs->ecmascript_fragile = 1;
}
void
ecmascript_protocol_handler(struct session *ses, struct uri *uri)
{
struct document_view *doc_view = current_frame(ses);
struct string current_url = INIT_STRING(struri(uri), strlen(struri(uri)));
unsigned char *redirect_url, *redirect_abs_url;
struct uri *redirect_uri;
if (!doc_view) /* Blank initial document. TODO: Start at about:blank? */
return;
assert(doc_view->vs);
if (doc_view->vs->ecmascript_fragile)
ecmascript_reset_state(doc_view->vs);
if (!doc_view->vs->ecmascript)
return;
redirect_url = ecmascript_eval_stringback(doc_view->vs->ecmascript,
&current_url);
if (!redirect_url)
return;
/* XXX: This code snippet is duplicated over here,
* location_set_property(), html_a() and who knows where else. */
redirect_abs_url = join_urls(doc_view->document->uri,
trim_chars(redirect_url, ' ', 0));
mem_free(redirect_url);
if (!redirect_abs_url)
return;
redirect_uri = get_uri(redirect_abs_url, 0);
mem_free(redirect_abs_url);
if (!redirect_uri)
return;
/* XXX: Is that safe to do at this point? --pasky */
goto_uri_frame(ses, redirect_uri, doc_view->name,
CACHE_MODE_NORMAL);
done_uri(redirect_uri);
}
void
ecmascript_timeout_dialog(struct terminal *term, int max_exec_time)
{
info_box(term, MSGBOX_FREE_TEXT,
N_("JavaScript Emergency"), ALIGN_LEFT,
msg_text(term,
N_("A script embedded in the current document was running\n"
"for more than %d seconds. This probably means there is\n"
"a bug in the script and it could have halted the whole\n"
"ELinks, so the script execution was interrupted."),
max_exec_time));
}
void
ecmascript_set_action(unsigned char **action, unsigned char *string)
{
struct uri *protocol;
trim_chars(string, ' ', NULL);
protocol = get_uri(string, URI_PROTOCOL);
if (protocol) { /* full uri with protocol */
done_uri(protocol);
mem_free_set(action, string);
} else {
2006-07-09 07:02:44 -04:00
if (dir_sep(string[0])) { /* absolute uri, TODO: disk drive under WIN32 */
struct uri *uri = get_uri(*action, URI_HTTP_REFERRER_HOST);
if (uri->protocol == PROTOCOL_FILE) {
mem_free_set(action, straconcat(struri(uri), string, (unsigned char *) NULL));
}
else
mem_free_set(action, straconcat(struri(uri), string + 1, (unsigned char *) NULL));
done_uri(uri);
mem_free(string);
} else { /* relative uri */
unsigned char *last_slash = strrchr((const char *)*action, '/');
unsigned char *new_action;
if (last_slash) *(last_slash + 1) = '\0';
new_action = straconcat(*action, string,
(unsigned char *) NULL);
mem_free_set(action, new_action);
mem_free(string);
}
}
}
/* Timer callback for @interpreter->vs->doc_view->document->timeout.
* As explained in @install_timer, this function must erase the
* expired timer ID from all variables. */
2006-10-23 16:45:55 -04:00
static void
ecmascript_timeout_handler(void *i)
{
struct ecmascript_interpreter *interpreter = i;
assertm(interpreter->vs->doc_view != NULL,
"setTimeout: vs with no document (e_f %d)",
interpreter->vs->ecmascript_fragile);
interpreter->vs->doc_view->document->timeout = TIMER_ID_UNDEF;
/* The expired timer ID has now been erased. */
2006-10-23 16:45:55 -04:00
ecmascript_eval(interpreter, &interpreter->code, NULL);
}
void
ecmascript_set_timeout(struct ecmascript_interpreter *interpreter, unsigned char *code, int timeout)
{
assert(interpreter && interpreter->vs->doc_view->document);
2006-10-23 16:45:55 -04:00
if (!code) return;
done_string(&interpreter->code);
init_string(&interpreter->code);
add_to_string(&interpreter->code, code);
mem_free(code);
kill_timer(&interpreter->vs->doc_view->document->timeout);
install_timer(&interpreter->vs->doc_view->document->timeout, timeout, ecmascript_timeout_handler, interpreter);
2006-10-23 16:45:55 -04:00
}
2006-01-16 08:14:14 -05:00
static struct module *ecmascript_modules[] = {
#ifdef CONFIG_ECMASCRIPT_SMJS
2006-01-16 08:14:14 -05:00
&spidermonkey_module,
#endif
NULL,
};
struct module ecmascript_module = struct_module(
/* name: */ N_("ECMAScript"),
/* options: */ ecmascript_options,
/* events: */ NULL,
2006-01-16 08:14:14 -05:00
/* submodules: */ ecmascript_modules,
/* data: */ NULL,
/* init: */ NULL,
/* done: */ NULL
);