Retry only for https protocol
[elinks.git] / src / scripting / smjs / action_object.c
blob113d132ad9a31d5d13b53fafcd1998ac48340e8b
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 mem_free_if(hop);
43 /* @action_fn_class.call */
44 static JSBool
45 smjs_action_fn_callback(JSContext *ctx, uintN argc, jsval *rval)
47 jsval value;
48 jsval *argv = JS_ARGV(ctx, rval);
49 struct smjs_action_fn_callback_hop *hop;
50 JSObject *fn_obj;
52 assert(smjs_ctx);
53 if_assert_failed return JS_FALSE;
55 value = JSVAL_FALSE;
57 if (JS_TRUE != JS_ValueToObject(ctx, JS_CALLEE(ctx, rval), &fn_obj)) {
58 JS_SET_RVAL(ctx, rval, value);
59 return JS_TRUE;
61 assert(JS_InstanceOf(ctx, fn_obj, (JSClass *) &action_fn_class, NULL));
62 if_assert_failed return JS_FALSE;
64 hop = JS_GetInstancePrivate(ctx, fn_obj,
65 (JSClass *) &action_fn_class, NULL);
66 if (!hop) {
67 JS_SET_RVAL(ctx, rval, value);
68 return JS_TRUE;
71 if (!would_window_receive_keypresses(hop->ses->tab)) {
72 /* The user cannot run actions in this tab by pressing
73 * keys, and some actions could crash if run in this
74 * situation; so we don't let user scripts run actions
75 * either.
77 * In particular, this check should fix bug 943, where
78 * ::ACT_MAIN_TAB_CLOSE called destroy_session(),
79 * which freed struct type_query while BFU dialogs had
80 * pointers to it. That crash could be prevented in
81 * various ways but it seems other similar crashes are
82 * possible, e.g. if the link menu is open and has a
83 * pointer to a session that is then destroyed.
84 * Instead of thoroughly auditing the use of pointers
85 * to sessions and related structures, I'll just
86 * disable the feature, to bring the ELinks 0.12
87 * release closer.
89 * The "%s" prevents interpretation of any percent
90 * signs in translations. */
91 JS_ReportError(ctx, "%s",
92 _("Cannot run actions in a tab that doesn't "
93 "have the focus", hop->ses->tab->term));
94 return JS_FALSE; /* make JS propagate the exception */
97 if (argc >= 1) {
98 int32 val;
100 if (JS_TRUE == JS_ValueToInt32(smjs_ctx, argv[0], &val)) {
101 set_kbd_repeat_count(hop->ses, val);
105 do_action(hop->ses, hop->action_id, 1);
107 value = JSVAL_TRUE;
108 JS_SET_RVAL(ctx, rval, value);
110 return JS_TRUE;
113 static const JSClass action_fn_class = {
114 "action_fn",
115 JSCLASS_HAS_PRIVATE, /* struct smjs_action_fn_callback_hop * */
116 JS_PropertyStub, JS_PropertyStub,
117 JS_PropertyStub, JS_StrictPropertyStub,
118 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub,
119 smjs_action_fn_finalize,
120 NULL, NULL,
121 smjs_action_fn_callback,
124 static JSObject *
125 smjs_get_action_fn_object(unsigned char *action_str)
127 unsigned char *c;
128 struct smjs_action_fn_callback_hop *hop;
129 JSObject *obj;
131 if (!smjs_ses) return NULL;
133 obj = JS_NewObject(smjs_ctx, (JSClass *) &action_fn_class, NULL, NULL);
134 if (!obj) return NULL;
136 hop = mem_alloc(sizeof(*hop));
137 if (!hop) return NULL;
139 hop->ses = smjs_ses;
141 /* ECMAScript methods cannot have hyphens in the name, so one should
142 * use underscores instead; here, we must convert them back to hyphens
143 * for the action code. */
144 for (c = action_str; *c; ++c) if (*c == '_') *c = '-';
146 hop->action_id = get_action_from_string(KEYMAP_MAIN, action_str);
148 if (-1 != hop->action_id
149 && JS_TRUE == JS_SetPrivate(smjs_ctx, obj, hop)) { /* to @action_fn_class */
150 return obj;
153 mem_free(hop);
154 return NULL;
158 /*** elinks.action object ***/
160 /* @action_class.getProperty */
161 static JSBool
162 action_get_property(JSContext *ctx, JSObject *obj, jsid id, jsval *vp)
164 jsval val;
165 JSObject *action_fn;
166 unsigned char *action_str;
168 *vp = JSVAL_NULL;
170 JS_IdToValue(ctx, id, &val);
171 action_str = JS_EncodeString(ctx, JS_ValueToString(ctx, val));
172 if (!action_str) return JS_TRUE;
174 action_fn = smjs_get_action_fn_object(action_str);
175 if (!action_fn) return JS_TRUE;
177 *vp = OBJECT_TO_JSVAL(action_fn);
179 return JS_TRUE;
182 static const JSClass action_class = {
183 "action",
185 JS_PropertyStub, JS_PropertyStub,
186 action_get_property, JS_StrictPropertyStub,
187 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
190 static JSObject *
191 smjs_get_action_object(void)
193 JSObject *obj;
195 assert(smjs_ctx);
197 obj = JS_NewObject(smjs_ctx, (JSClass *) &action_class, NULL, NULL);
199 return obj;
202 void
203 smjs_init_action_interface(void)
205 jsval val;
206 struct JSObject *action_object;
208 if (!smjs_ctx || !smjs_elinks_object)
209 return;
211 action_object = smjs_get_action_object();
212 if (!action_object) return;
214 val = OBJECT_TO_JSVAL(action_object);
216 JS_SetProperty(smjs_ctx, smjs_elinks_object, "action", &val);