Bug 943: Refuse user JS actions in unfocused tabs
[elinks/kon.git] / src / scripting / smjs / action_object.c
blobf6ae68fdc5a8c7f8e39dda748d3bd5abf1d8fdcf
1 /* "elinks.action" */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include "elinks.h"
9 #include "config/kbdbind.h"
10 #include "ecmascript/spidermonkey-shared.h"
11 #include "intl/gettext/libintl.h"
12 #include "scripting/smjs/core.h"
13 #include "scripting/smjs/elinks_object.h"
14 #include "session/session.h"
15 #include "terminal/window.h"
16 #include "util/memory.h"
17 #include "viewer/action.h"
19 /*** action method object ***/
21 struct smjs_action_fn_callback_hop {
22 struct session *ses;
23 action_id_T action_id;
26 static const JSClass action_fn_class; /* defined below */
28 /* @action_fn_class.finalize */
29 static void
30 smjs_action_fn_finalize(JSContext *ctx, JSObject *obj)
32 struct smjs_action_fn_callback_hop *hop;
34 assert(JS_InstanceOf(ctx, obj, (JSClass *) &action_fn_class, NULL));
35 if_assert_failed return;
37 hop = JS_GetInstancePrivate(ctx, obj,
38 (JSClass *) &action_fn_class, NULL);
40 if (hop) mem_free(hop);
43 /* @action_fn_class.call */
44 static JSBool
45 smjs_action_fn_callback(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv,
46 jsval *rval)
48 struct smjs_action_fn_callback_hop *hop;
49 JSObject *fn_obj;
51 assert(smjs_ctx);
52 if_assert_failed return JS_FALSE;
54 *rval = JS_FALSE;
56 if (JS_TRUE != JS_ValueToObject(ctx, argv[-2], &fn_obj))
57 return JS_TRUE;
58 assert(JS_InstanceOf(ctx, fn_obj, (JSClass *) &action_fn_class, NULL));
59 if_assert_failed return JS_FALSE;
61 hop = JS_GetInstancePrivate(ctx, fn_obj,
62 (JSClass *) &action_fn_class, NULL);
63 if (!hop) return JS_TRUE;
65 if (!would_window_receive_keypresses(hop->ses->tab)) {
66 /* The user cannot run actions in this tab by pressing
67 * keys, and some actions could crash if run in this
68 * situation; so we don't let user scripts run actions
69 * either.
71 * In particular, this check should fix bug 943, where
72 * ::ACT_MAIN_TAB_CLOSE called destroy_session(),
73 * which freed struct type_query while BFU dialogs had
74 * pointers to it. That crash could be prevented in
75 * various ways but it seems other similar crashes are
76 * possible, e.g. if the link menu is open and has a
77 * pointer to a session that is then destroyed.
78 * Instead of thoroughly auditing the use of pointers
79 * to sessions and related structures, I'll just
80 * disable the feature, to bring the ELinks 0.12
81 * release closer.
83 * The "%s" prevents interpretation of any percent
84 * signs in translations. */
85 JS_ReportError(ctx, "%s",
86 _("Cannot run actions in a tab that doesn't "
87 "have the focus", hop->ses->tab->term));
88 return JS_FALSE; /* make JS propagate the exception */
91 if (argc >= 1) {
92 int32 val;
94 if (JS_TRUE == JS_ValueToInt32(smjs_ctx, argv[0], &val)) {
95 hop->ses->kbdprefix.repeat_count = val;
99 do_action(hop->ses, hop->action_id, 1);
101 *rval = JS_TRUE;
103 return JS_TRUE;
106 static const JSClass action_fn_class = {
107 "action_fn",
108 JSCLASS_HAS_PRIVATE, /* struct smjs_action_fn_callback_hop * */
109 JS_PropertyStub, JS_PropertyStub,
110 JS_PropertyStub, JS_PropertyStub,
111 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub,
112 smjs_action_fn_finalize,
113 NULL, NULL,
114 smjs_action_fn_callback,
117 static JSObject *
118 smjs_get_action_fn_object(unsigned char *action_str)
120 unsigned char *c;
121 struct smjs_action_fn_callback_hop *hop;
122 JSObject *obj;
124 if (!smjs_ses) return NULL;
126 obj = JS_NewObject(smjs_ctx, (JSClass *) &action_fn_class, NULL, NULL);
127 if (!obj) return NULL;
129 hop = mem_alloc(sizeof(*hop));
130 if (!hop) return NULL;
132 hop->ses = smjs_ses;
134 /* ECMAScript methods cannot have hyphens in the name, so one should
135 * use underscores instead; here, we must convert them back to hyphens
136 * for the action code. */
137 for (c = action_str; *c; ++c) if (*c == '_') *c = '-';
139 hop->action_id = get_action_from_string(KEYMAP_MAIN, action_str);
141 if (-1 != hop->action_id
142 && JS_TRUE == JS_SetPrivate(smjs_ctx, obj, hop)) { /* to @action_fn_class */
143 return obj;
146 mem_free(hop);
147 return NULL;
151 /*** elinks.action object ***/
153 /* @action_class.getProperty */
154 static JSBool
155 action_get_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
157 JSObject *action_fn;
158 unsigned char *action_str;
160 *vp = JSVAL_NULL;
162 action_str = JS_GetStringBytes(JS_ValueToString(ctx, id));
163 if (!action_str) return JS_TRUE;
165 action_fn = smjs_get_action_fn_object(action_str);
166 if (!action_fn) return JS_TRUE;
168 *vp = OBJECT_TO_JSVAL(action_fn);
170 return JS_TRUE;
173 static const JSClass action_class = {
174 "action",
176 JS_PropertyStub, JS_PropertyStub,
177 action_get_property, JS_PropertyStub,
178 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
181 static JSObject *
182 smjs_get_action_object(void)
184 JSObject *obj;
186 assert(smjs_ctx);
188 obj = JS_NewObject(smjs_ctx, (JSClass *) &action_class, NULL, NULL);
190 return obj;
193 void
194 smjs_init_action_interface(void)
196 jsval val;
197 struct JSObject *action_object;
199 if (!smjs_ctx || !smjs_elinks_object)
200 return;
202 action_object = smjs_get_action_object();
203 if (!action_object) return;
205 val = OBJECT_TO_JSVAL(action_object);
207 JS_SetProperty(smjs_ctx, smjs_elinks_object, "action", &val);