summaryrefslogtreecommitdiff
path: root/render/html_script.c
blob: 631dcca1e6d409e46d9b2245629710e66a4240ef (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * 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/>.
 */

/** \file
 * Content for text/html scripts (implementation).
 */

#include <assert.h>
#include <ctype.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>

#include "utils/config.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "javascript/js.h"
#include "content/content_protected.h"
#include "content/fetch.h"
#include "render/html_internal.h"

typedef bool (script_handler_t)(struct jscontext *jscontext, const char *data, size_t size) ;


static script_handler_t *select_script_handler(content_type ctype)
{
	if (ctype == CONTENT_JS) {
		return js_exec;
	}
	return NULL;
}


/* attempt to progress script execution
 *
 * execute scripts using algorithm found in:
 * http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#the-script-element
 *
 */
static bool html_scripts_exec(html_content *c)
{
	unsigned int i;
	struct html_script *s;
	script_handler_t *script_handler;

	if (c->jscontext == NULL)
		return false;

	for (i = 0, s = c->scripts; i != c->scripts_count; i++, s++) {
		if (s->already_started) {
			continue;
		}

		assert((s->type == HTML_SCRIPT_EXTERNAL) ||
		       (s->type == HTML_SCRIPT_INTERNAL));

		if (s->type == HTML_SCRIPT_EXTERNAL) {
			/* ensure script content is present */
			if (s->data.external == NULL)
				continue;

			/* ensure script content fetch status is not an error */
			if (content_get_status(s->data.external) == CONTENT_STATUS_ERROR)
				continue;

			/* ensure script handler for content type */
			script_handler = select_script_handler(content_get_type(s->data.external));
			if (script_handler == NULL)
				continue; /* unsupported type */

			if (content_get_status(s->data.external) == CONTENT_STATUS_DONE) {
				/* external script is now available */
				const char *data;
				unsigned long size;
				data = content_get_source_data(s->data.external, &size );
				script_handler(c->jscontext, data, size);

				s->already_started = true;

			} else {
				/* script not yet available */

				/* check if deferable or asynchronous */
				if (!s->defer && !s->async) {
					break;
				}
			}
		}
	}

	return true;
}

/* create new html script entry */
static struct html_script *
html_process_new_script(html_content *c, enum html_script_type type)
{
	struct html_script *nscript;
	/* add space for new script entry */
	nscript = realloc(c->scripts,
			  sizeof(struct html_script) * (c->scripts_count + 1));
	if (nscript == NULL) {
		return NULL;
	}

	c->scripts = nscript;

	/* increment script entry count */
	nscript = &c->scripts[c->scripts_count];
	c->scripts_count++;

	nscript->already_started = false;
	nscript->parser_inserted = false;
	nscript->force_async = true;
	nscript->ready_exec = false;
	nscript->async = false;
	nscript->defer = false;

	nscript->type = type;

	return nscript;
}

/**
 * Callback for fetchcache() for linked stylesheets.
 */

static nserror
html_convert_script_callback(hlcache_handle *script,
			  const hlcache_event *event,
			  void *pw)
{
	html_content *parent = pw;
	unsigned int i;
	struct html_script *s;

	/* Find script */
	for (i = 0, s = parent->scripts; i != parent->scripts_count; i++, s++) {
		if (s->type == HTML_SCRIPT_EXTERNAL &&
		    s->data.external == script)
			break;
	}

	assert(i != parent->scripts_count);

	switch (event->type) {
	case CONTENT_MSG_LOADING:
		break;

	case CONTENT_MSG_READY:
		break;

	case CONTENT_MSG_DONE:
		LOG(("script %d done '%s'", i,
				nsurl_access(hlcache_handle_get_url(script))));
		parent->base.active--;
		LOG(("%d fetches active", parent->base.active));

		/* script finished loading so try and continue execution */
		html_scripts_exec(parent);
		break;

	case CONTENT_MSG_ERROR:
		LOG(("script %s failed: %s",
				nsurl_access(hlcache_handle_get_url(script)),
				event->data.error));
		hlcache_handle_release(script);
		s->data.external = NULL;
		parent->base.active--;
		LOG(("%d fetches active", parent->base.active));
		content_add_error(&parent->base, "?", 0);

		/* script failed loading so try and continue execution */
		html_scripts_exec(parent);

		break;

	case CONTENT_MSG_STATUS:
		html_set_status(parent, content_get_status_message(script));
		content_broadcast(&parent->base, CONTENT_MSG_STATUS,
				event->data);
		break;

	default:
		assert(0);
	}

	if (parent->base.active == 0)
		html_finish_conversion(parent);

	return NSERROR_OK;
}

/** process script node
 *
 *
 */
dom_hubbub_error
html_process_script(void *ctx, dom_node *node)
{
	html_content *c = (html_content *)ctx;
	dom_exception exc; /* returned by libdom functions */
	dom_string *src, *script, *mimetype;
	struct html_script *nscript;
	union content_msg_data msg_data;

	/* ensure javascript context is available */
	if (c->jscontext == NULL) {
		union content_msg_data msg_data;

		msg_data.jscontext = &c->jscontext;
		content_broadcast(&c->base, CONTENT_MSG_GETCTX, msg_data);
		LOG(("javascript context %p ", c->jscontext));
		if (c->jscontext == NULL) {
			/* no context and it could not be created, abort */
			return DOM_HUBBUB_OK;
		}
	}

	LOG(("content %p parser %p node %p",c,c->parser, node));

	exc = dom_element_get_attribute(node, html_dom_string_type, &mimetype);
	if (exc != DOM_NO_ERR || mimetype == NULL) {
		mimetype = dom_string_ref(html_dom_string_text_javascript);
	}

	exc = dom_element_get_attribute(node, html_dom_string_src, &src);
	if (exc != DOM_NO_ERR || src == NULL) {
		struct lwc_string_s *lwcmimetype;
		script_handler_t *script_handler;

		/* does not appear to be a src so script is inline content */
		exc = dom_node_get_text_content(node, &script);
		if ((exc != DOM_NO_ERR) || (script == NULL)) {
			dom_string_unref(mimetype);
			return DOM_HUBBUB_OK; /* no contents, skip */
		}

		nscript = html_process_new_script(c, HTML_STYLESHEET_INTERNAL);
		if (nscript == NULL) {
			dom_string_unref(mimetype);
			dom_string_unref(script);
			goto html_process_script_no_memory;
		}

		nscript->data.internal = script;
		nscript->mimetype = mimetype;
		nscript->already_started = true;

		/* charset (encoding) */

		/* ensure script handler for content type */
		dom_string_intern(mimetype, &lwcmimetype);
		script_handler = select_script_handler(content_factory_type_from_mime_type(lwcmimetype));
		lwc_string_unref(lwcmimetype);

		if (script_handler != NULL) {
			script_handler(c->jscontext,
				       dom_string_data(script),
				       dom_string_byte_length(script));
		}


	} else {
		/* script with a src tag */
		nserror ns_error;
		nsurl *joined;
		hlcache_child_context child;


		nscript = html_process_new_script(c, HTML_STYLESHEET_EXTERNAL);
		if (nscript == NULL) {
			dom_string_unref(src);
			dom_string_unref(mimetype);
			goto html_process_script_no_memory;
		}

		/* charset (encoding) */

		ns_error = nsurl_join(c->base_url, dom_string_data(src), &joined);
		dom_string_unref(src);
		if (ns_error != NSERROR_OK) {
			dom_string_unref(mimetype);
			goto html_process_script_no_memory;
		}

		nscript->mimetype = mimetype; /* keep reference to mimetype */

		LOG(("script %i '%s'", c->scripts_count, nsurl_access(joined)));

		child.charset = c->encoding;
		child.quirks = c->base.quirks;

		ns_error = hlcache_handle_retrieve(joined,
						   0,
						   content_get_url(&c->base),
						   NULL,
						   html_convert_script_callback,
						   c,
						   &child,
						   CONTENT_SCRIPT,
						   &nscript->data.external);

		nsurl_unref(joined);

		if (ns_error != NSERROR_OK) {
			goto html_process_script_no_memory;
		}

		c->base.active++; /* ensure base content knows the fetch is active */
		LOG(("%d fetches active", c->base.active));

	}
	html_scripts_exec(c);

	return DOM_HUBBUB_OK;

html_process_script_no_memory:
	msg_data.error = messages_get("NoMemory");
	content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data);
	return DOM_HUBBUB_NOMEM;
}