1 /* The SpiderMonkey location and history objects implementation. */
2 
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #include "elinks.h"
12 
13 #include "ecmascript/spidermonkey/util.h"
14 
15 #include "bfu/dialog.h"
16 #include "cache/cache.h"
17 #include "cookies/cookies.h"
18 #include "dialogs/menu.h"
19 #include "dialogs/status.h"
20 #include "document/html/frames.h"
21 #include "document/document.h"
22 #include "document/forms.h"
23 #include "document/view.h"
24 #include "ecmascript/ecmascript.h"
25 #include "ecmascript/spidermonkey/navigator.h"
26 #include "intl/gettext/libintl.h"
27 #include "main/select.h"
28 #include "osdep/newwin.h"
29 #include "osdep/sysname.h"
30 #include "protocol/http/http.h"
31 #include "protocol/uri.h"
32 #include "session/history.h"
33 #include "session/location.h"
34 #include "session/session.h"
35 #include "session/task.h"
36 #include "terminal/tab.h"
37 #include "terminal/terminal.h"
38 #include "util/conv.h"
39 #include "util/memory.h"
40 #include "util/string.h"
41 #include "viewer/text/draw.h"
42 #include "viewer/text/form.h"
43 #include "viewer/text/link.h"
44 #include "viewer/text/vs.h"
45 
46 
47 static JSBool navigator_get_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp);
48 
49 const JSClass navigator_class = {
50 	"navigator",
51 	JSCLASS_HAS_PRIVATE,
52 	JS_PropertyStub, JS_PropertyStub,
53 	navigator_get_property, JS_PropertyStub,
54 	JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
55 };
56 
57 enum navigator_prop {
58 	JSP_NAVIGATOR_APP_CODENAME,
59 	JSP_NAVIGATOR_APP_NAME,
60 	JSP_NAVIGATOR_APP_VERSION,
61 	JSP_NAVIGATOR_LANGUAGE,
62 	/* JSP_NAVIGATOR_MIME_TYPES, */
63 	JSP_NAVIGATOR_PLATFORM,
64 	/* JSP_NAVIGATOR_PLUGINS, */
65 	JSP_NAVIGATOR_USER_AGENT,
66 };
67 const JSPropertySpec navigator_props[] = {
68 	{ "appCodeName",	JSP_NAVIGATOR_APP_CODENAME,	JSPROP_ENUMERATE | JSPROP_READONLY },
69 	{ "appName",		JSP_NAVIGATOR_APP_NAME,		JSPROP_ENUMERATE | JSPROP_READONLY },
70 	{ "appVersion",		JSP_NAVIGATOR_APP_VERSION,	JSPROP_ENUMERATE | JSPROP_READONLY },
71 	{ "language",		JSP_NAVIGATOR_LANGUAGE,		JSPROP_ENUMERATE | JSPROP_READONLY },
72 	{ "platform",		JSP_NAVIGATOR_PLATFORM,		JSPROP_ENUMERATE | JSPROP_READONLY },
73 	{ "userAgent",		JSP_NAVIGATOR_USER_AGENT,	JSPROP_ENUMERATE | JSPROP_READONLY },
74 	{ NULL }
75 };
76 
77 
78 /* @navigator_class.getProperty */
79 static JSBool
navigator_get_property(JSContext * ctx,JSObject * obj,jsval id,jsval * vp)80 navigator_get_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
81 {
82 	if (!JSVAL_IS_INT(id))
83 		return JS_TRUE;
84 
85 	undef_to_jsval(ctx, vp);
86 
87 	switch (JSVAL_TO_INT(id)) {
88 	case JSP_NAVIGATOR_APP_CODENAME:
89 		string_to_jsval(ctx, vp, "Mozilla"); /* More like a constant nowadays. */
90 		break;
91 	case JSP_NAVIGATOR_APP_NAME:
92 		/* This evil hack makes the compatibility checking .indexOf()
93 		 * code find what it's looking for. */
94 		string_to_jsval(ctx, vp, "ELinks (roughly compatible with Netscape Navigator, Mozilla and Microsoft Internet Explorer)");
95 		break;
96 	case JSP_NAVIGATOR_APP_VERSION:
97 		string_to_jsval(ctx, vp, VERSION);
98 		break;
99 	case JSP_NAVIGATOR_LANGUAGE:
100 #ifdef CONFIG_NLS
101 		if (get_opt_bool("protocol.http.accept_ui_language"))
102 			string_to_jsval(ctx, vp, language_to_iso639(current_language));
103 
104 #endif
105 		break;
106 	case JSP_NAVIGATOR_PLATFORM:
107 		string_to_jsval(ctx, vp, system_name);
108 		break;
109 	case JSP_NAVIGATOR_USER_AGENT:
110 	{
111 		/* FIXME: Code duplication. */
112 		unsigned char *optstr = get_opt_str("protocol.http.user_agent");
113 
114 		if (*optstr && strcmp(optstr, " ")) {
115 			unsigned char *ustr, ts[64] = "";
116 			static unsigned char custr[256];
117 
118 			if (!list_empty(terminals)) {
119 				unsigned int tslen = 0;
120 				struct terminal *term = terminals.prev;
121 
122 				ulongcat(ts, &tslen, term->width, 3, 0);
123 				ts[tslen++] = 'x';
124 				ulongcat(ts, &tslen, term->height, 3, 0);
125 			}
126 			ustr = subst_user_agent(optstr, VERSION_STRING, system_name, ts);
127 
128 			if (ustr) {
129 				safe_strncpy(custr, ustr, 256);
130 				mem_free(ustr);
131 				string_to_jsval(ctx, vp, custr);
132 			}
133 		}
134 	}
135 		break;
136 	default:
137 		/* Unrecognized integer property ID; someone is using
138 		 * the object as an array.  SMJS builtin classes (e.g.
139 		 * js_RegExpClass) just return JS_TRUE in this case
140 		 * and leave *@vp unchanged.  Do the same here.
141 		 * (Actually not quite the same, as we already used
142 		 * @undef_to_jsval.)  */
143 		break;
144 	}
145 
146 	return JS_TRUE;
147 }
148