summaryrefslogtreecommitdiff
path: root/javascript/jsapi/window.c
blob: 062d925daf5c0811cb3e04c34a947132ccdc8b33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * 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/>.
 */

#include "utils/log.h"

#include "javascript/jsapi.h"
#include "javascript/jsapi/binding.h"


struct jsclass_private {
	struct browser_window *bw;
	struct html_content *htmlc;
	JSObject *document_obj;
	JSObject *navigator_obj;
	JSObject *console_obj;
};

static void jsclass_finalize(JSContext *cx, JSObject *obj);
static JSBool jsclass_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags, JSObject **objp);

JSClass JSClass_Window = {
	"Window",
	JSCLASS_NEW_RESOLVE | JSCLASS_HAS_PRIVATE | JSCLASS_GLOBAL_FLAGS,
	JS_PropertyStub,
	JS_PropertyStub,
	JS_PropertyStub,
	JS_StrictPropertyStub,
	JS_EnumerateStub,
	(JSResolveOp)jsclass_resolve,
	JS_ConvertStub,
	jsclass_finalize,
	JSCLASS_NO_OPTIONAL_MEMBERS
};


static JSBool JSAPI_NATIVE(alert, JSContext *cx, uintN argc, jsval *vp)
{
	JSString* u16_txt;
	char *txt;
	unsigned long length;

	if (!JS_ConvertArguments(cx, argc, JSAPI_ARGV(cx, vp), "S", &u16_txt))
		return JS_FALSE;

	JSString_to_char(u16_txt, txt, length);

	warn_user(txt, NULL);

	JSAPI_SET_RVAL(cx, vp, JSVAL_VOID);

	return JS_TRUE;
}

static JSBool JSAPI_NATIVE(confirm, JSContext *cx, uintN argc, jsval *vp)
{
	JSString* u16_txt;
	char *txt;
	unsigned long length;
	JSBool result = JS_FALSE;

	if (!JS_ConvertArguments(cx, argc, JSAPI_ARGV(cx, vp), "S", &u16_txt))
		return JS_FALSE;

	JSString_to_char(u16_txt, txt, length);

	warn_user(txt, NULL);

	JSAPI_SET_RVAL(cx, vp, BOOLEAN_TO_JSVAL(result));

	return JS_TRUE;
}

static JSBool JSAPI_NATIVE(prompt, JSContext *cx, uintN argc, jsval *vp)
{
	JSString* u16_txt;
	char *txt;
	unsigned long length;

	if (!JS_ConvertArguments(cx, argc, JSAPI_ARGV(cx, vp), "S", &u16_txt))
		return JS_FALSE;

	JSString_to_char(u16_txt, txt, length);

	warn_user(txt, NULL);

	JSAPI_SET_RVAL(cx, vp, JSVAL_VOID);

	return JS_TRUE;
}

static JSBool JSAPI_NATIVE(close, JSContext *cx, uintN argc, jsval *vp)
{
	JSAPI_SET_RVAL(cx, vp, JSVAL_VOID);

	return JS_TRUE;
}

static JSBool JSAPI_NATIVE(stop, JSContext *cx, uintN argc, jsval *vp)
{
	JSAPI_SET_RVAL(cx, vp, JSVAL_VOID);

	return JS_TRUE;
}

static JSBool JSAPI_NATIVE(focus, JSContext *cx, uintN argc, jsval *vp)
{
	JSAPI_SET_RVAL(cx, vp, JSVAL_VOID);

	return JS_TRUE;
}

static JSBool JSAPI_NATIVE(blur, JSContext *cx, uintN argc, jsval *vp)
{
	JSAPI_SET_RVAL(cx, vp, JSVAL_VOID);

	return JS_TRUE;
}

static JSFunctionSpec jsfunctions_window[] =
{
	JSAPI_FS(close, 0, 0),
	JSAPI_FS(stop, 0, 0),
	JSAPI_FS(focus, 0, 0),
	JSAPI_FS(blur, 0, 0),
	JSAPI_FS(alert, 1, 0),
	JSAPI_FS(confirm, 1, 0),
	JSAPI_FS(prompt, 1, 0),
	JSAPI_FS_END
};


static JSBool JSAPI_PROPERTYGET(window, JSContext *cx, JSObject *obj, jsval *vp)
{
	JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
	return JS_TRUE;
}

static JSBool JSAPI_PROPERTYGET(self, JSContext *cx, JSObject *obj, jsval *vp)
{
	JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
	return JS_TRUE;
}

static JSBool JSAPI_PROPERTYGET(document, JSContext *cx, JSObject *obj, jsval *vp)
{
	struct jsclass_private *private;

	private = JS_GetInstancePrivate(cx,
			obj,
			&JSClass_Window,
			NULL);
	if (private == NULL)
		return JS_FALSE;

	JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(private->document_obj));
	return JS_TRUE;
}

static JSPropertySpec jsproperties_window[] =
{
	JSAPI_PS_RO(document, 0, JSPROP_ENUMERATE | JSPROP_SHARED),
	JSAPI_PS_RO(window, 0, JSPROP_ENUMERATE | JSPROP_SHARED),
	JSAPI_PS_RO(self, 0, JSPROP_ENUMERATE | JSPROP_SHARED),
	JSAPI_PS_END
};

static JSBool jsclass_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags, JSObject **objp)
{
	*objp = NULL;
	return JS_TRUE;
}

static void jsclass_finalize(JSContext *cx, JSObject *obj)
{	struct jsclass_private *private;

	private = JS_GetInstancePrivate(cx, obj, &JSClass_Window, NULL);
	if (private != NULL) {
		free(private);
	}
}

JSObject *jsapi_InitClass_Window(JSContext *cx, JSObject *parent)
{
	JSObject *window = NULL;
	JSObject *proto;

	window = JS_NewCompartmentAndGlobalObject(cx, &JSClass_Window, NULL);
	if (window == NULL) {
		return NULL;
	}

	/** @todo reconsider global object handling. future
	 * editions of spidermonkey appear to be removing the
	 * idea of a global so we probably need to handle
	 * global object references internally
	 */

	/* set the contexts global */
	JS_SetGlobalObject(cx, window);

	/* Populate the global object with the standard globals, like
	 *  Object and Array.
	 */
	if (!JS_InitStandardClasses(cx, window)) {
		return NULL;
	}

	/* Initialises all the user javascript classes to make their
	 * prototypes available. 
	 */
	/** @todo should we be managing these prototype objects ourselves */
	proto = jsapi_InitClass_Document(cx, window);
	if (proto == NULL) {
		return NULL;
	}

	return window;
}

JSObject *jsapi_new_Window(JSContext *cx, 
			    JSObject *window,
			    JSObject *parent, 
			    struct browser_window *bw, 
			    html_content *htmlc)
{
	struct jsclass_private *private;

	/* @todo sort out windows that are not globals */
	assert(parent == NULL);

	/* create private data */
	private = malloc(sizeof(struct jsclass_private));
	if (private == NULL) {
		return NULL;
	}
	private->bw = bw;
	private->htmlc = htmlc;


	/* instantiate the subclasses off the window global */
	private->document_obj = jsapi_new_Document(cx,
						   NULL,
						   window, 
						   htmlc->document, 
						   htmlc);
	if (private->document_obj == NULL) { 
		free(private);
		return NULL;
	}

	private->navigator_obj = jsapi_new_Navigator(cx, window);
	if (private->navigator_obj == NULL) {
		free(private);
		return NULL;
	}

	/** @todo forms, history, location */

	private->console_obj = jsapi_new_Console(cx, window);
	if (private->console_obj == NULL) {
		free(private);
		return NULL;
	}

	/* private pointer to browsing context */
	if (!JS_SetPrivate(cx, window, private))
		return NULL;

	/* functions */
	if (!JS_DefineFunctions(cx, window, jsfunctions_window)) {
		return NULL;
	}

	/* properties */
	if (!JS_DefineProperties(cx, window, jsproperties_window))
		return NULL;


	LOG(("Created new window object %p", window));

	return window;
}