Explorar el Código

browser: fix cloudron.io script errors

- <video>/<audio> get play/pause/load/canPlayType (play() resolves
  without playing), paused and readyState, plus the HTMLMediaElement,
  HTMLVideoElement and HTMLAudioElement classes.
- Writes to an element's scrollTop/scrollLeft are accepted instead of
  failing as read-only (jQuery's scrollTop(0) in Bootstrap's modal).
- A synchronous foreground fetch (page, module, fetch()/XHR) that finds
  every helper busy takes over the newest image job's helper; the image
  is re-armed.  cloudron.io's catalogue XHR used to return status 0.
- A failed XHR logs its network error reason.
Johannes Zellner hace 2 días
padre
commit
59bd6db11a
Se han modificado 5 ficheros con 78 adiciones y 4 borrados
  1. 6 2
      AGENTS.md
  2. 0 0
      docs/network.html
  3. 26 2
      user/apps/browser/browser.c
  4. 26 0
      user/apps/browser/js_browser.c
  5. 20 0
      user/apps/browser/js_prelude.js

+ 6 - 2
AGENTS.md

@@ -1769,7 +1769,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   TCP/TLS connection (TLS stays per-process).  Idle helpers stay warm across
   TCP/TLS connection (TLS stays per-process).  Idle helpers stay warm across
   navigations, so a same-origin load reuses the TCP/TLS connection and a
   navigations, so a same-origin load reuses the TCP/TLS connection and a
   cross-origin job just drops the old one in `uhttp_open`; only busy helpers
   cross-origin job just drops the old one in `uhttp_open`; only busy helpers
-  are killed when the page changes.  The helpers' DNS/TLS log lines (and the
+  are killed when the page changes.  A synchronous foreground fetch (the page,
+  a module, a script's `fetch()`/XHR) that finds every helper busy takes
+  the newest image job's helper (`fetch_worker_alloc_fg()`; the image node
+  is re-armed and loads later) instead of failing with "network busy".  The helpers' DNS/TLS log lines (and the
   browser's own `[js]` lines, since `u_tty_create()` rebinds its output) go to
   browser's own `[js]` lines, since `u_tty_create()` rebinds its output) go to
   a private 4 KB tty that nobody renders; the browser drains and discards it
   a private 4 KB tty that nobody renders; the browser drains and discards it
   from every poll loop (`fetch_tty_drain`), because a full tty ring blocks its
   from every poll loop (`fetch_tty_drain`), because a full tty ring blocks its
@@ -3022,7 +3025,8 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     or ten idle seconds stops them, so their sockets do not block raw
     or ten idle seconds stops them, so their sockets do not block raw
     commands.  The helper requests gzip; a page
     commands.  The helper requests gzip; a page
     that is only small when compressed can exceed the 2 MB page/832 KB CSS
     that is only small when compressed can exceed the 2 MB page/832 KB CSS
-    caps.  Video/audio elements are placeholders.  Bare `&` characters are
+    caps.  Video/audio elements are placeholders (`play()` resolves without
+    playing, `pause`/`load` do nothing, `paused` is true).  Bare `&` characters are
     consumed by the entity decoder instead of looping forever.  A page with
     consumed by the entity decoder instead of looping forever.  A page with
     no `html`/`body` background gets the dark default canvas unless its
     no `html`/`body` background gets the dark default canvas unless its
     computed text colour is dark, in which case the canvas is white like a
     computed text colour is dark, in which case the canvas is white like a

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 0 - 0
docs/network.html


+ 26 - 2
user/apps/browser/browser.c

@@ -3504,6 +3504,30 @@ static struct fetch_worker *fetch_worker_alloc(void)
 	return 0;
 	return 0;
 }
 }
 
 
+/* A worker for a synchronous foreground fetch (the page, a module, a
+ * script's fetch()/XHR).  When image prefetches hold every helper, the
+ * newest image job (least progress) is abandoned: fetch_worker_stop()
+ * re-arms its node, so a later scan loads it again.  Failing instead made
+ * cloudron.io's catalogue XHR return status 0 while its icons loaded. */
+static struct fetch_worker *fetch_worker_alloc_fg(void)
+{
+	struct fetch_worker *w = fetch_worker_alloc(), *victim = 0;
+	int i;
+
+	if (w)
+		return w;
+	for (i = 0; i < FETCH_WORKERS; i++) {
+		struct fetch_worker *c = &fetch_workers[i];
+
+		if (c->busy && c->image_job &&
+		    (!victim || (int)(c->seq - victim->seq) > 0))
+			victim = c;
+	}
+	if (victim)
+		fetch_worker_stop(victim);
+	return victim;
+}
+
 static int fetch_worker_submit_req(struct fetch_worker *w, const char *url,
 static int fetch_worker_submit_req(struct fetch_worker *w, const char *url,
 				   unsigned int cap, const char *method,
 				   unsigned int cap, const char *method,
 				   const char *ctype, const char *post,
 				   const char *ctype, const char *post,
@@ -3815,7 +3839,7 @@ static int fetch_remote(const char *url, unsigned int cap,
 			struct fetch_meta *m, int *truncated,
 			struct fetch_meta *m, int *truncated,
 			char *err, unsigned int errcap)
 			char *err, unsigned int errcap)
 {
 {
-	struct fetch_worker *w = fetch_worker_alloc();
+	struct fetch_worker *w = fetch_worker_alloc_fg();
 	int rv;
 	int rv;
 
 
 	fetch_last_error = 0;
 	fetch_last_error = 0;
@@ -3859,7 +3883,7 @@ static int js_worker_http(const char *method, const char *url,
 			  unsigned int body_len, struct js_http_resp *r,
 			  unsigned int body_len, struct js_http_resp *r,
 			  char *err, int errcap)
 			  char *err, int errcap)
 {
 {
-	struct fetch_worker *w = fetch_worker_alloc();
+	struct fetch_worker *w = fetch_worker_alloc_fg();
 	unsigned char *plain;
 	unsigned char *plain;
 	int rv, tr = 0;
 	int rv, tr = 0;
 
 

+ 26 - 0
user/apps/browser/js_browser.c

@@ -2172,6 +2172,27 @@ static int node_has(js_State *state, void *p, const char *name)
 			return 1;
 			return 1;
 		}
 		}
 	}
 	}
+	/* <video>/<audio> are placeholder boxes: give them the
+	 * HTMLMediaElement methods from the prelude (__nb_media) and report a
+	 * paused, never-loaded element.  The global object stays below the
+	 * method on the stack; the hook result is the top value. */
+	if (n->type == BR_NODE_ELEM &&
+	    (n->tag == BR_TAG_VIDEO || n->tag == BR_TAG_AUDIO)) {
+		if (!strcmp(name, "play") || !strcmp(name, "pause") ||
+		    !strcmp(name, "load") || !strcmp(name, "canPlayType")) {
+			js_getglobal(state, "__nb_media");
+			js_getproperty(state, -1, name);
+			return 1;
+		}
+		if (!strcmp(name, "paused")) {
+			js_pushboolean(state, 1);
+			return 1;
+		}
+		if (!strcmp(name, "readyState")) {
+			js_pushnumber(state, 0);
+			return 1;
+		}
+	}
 	/* An <option> without a value attribute has its text as value. */
 	/* An <option> without a value attribute has its text as value. */
 	if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_OPTION &&
 	if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_OPTION &&
 	    !strcmp(name, "value") && !node_attr(node, "value")) {
 	    !strcmp(name, "value") && !node_attr(node, "value")) {
@@ -2463,6 +2484,11 @@ static int node_put(js_State *state, void *p, const char *name)
 		js_setregistry(state, key);
 		js_setregistry(state, key);
 		return 1;
 		return 1;
 	}
 	}
+	/* There are no scrollable inner boxes, so an element's scroll offset
+	 * stays 0; accept the write (jQuery's scrollTop(0) in Bootstrap's
+	 * modal show) instead of letting the getter make it read-only. */
+	if (!strcmp(name, "scrollTop") || !strcmp(name, "scrollLeft"))
+		return 1;
 	if (!strcmp(name, "style")) {
 	if (!strcmp(name, "style")) {
 		if (js_isstring(state, -1))
 		if (js_isstring(state, -1))
 			node_set_string(node, "style",
 			node_set_string(node, "style",

+ 20 - 0
user/apps/browser/js_prelude.js

@@ -683,6 +683,10 @@ class XMLHttpRequest {
 		try {
 		try {
 			out = send(this._m, this._u, this._h, body);
 			out = send(this._m, this._u, this._h, body);
 		} catch (e) {
 		} catch (e) {
+			/* Like a browser's console network error: the page
+			 * only sees status 0, so say why. */
+			con.error("XMLHttpRequest " + this._m + " " + this._u +
+				  ": " + (e && e.message));
 			err = true;
 			err = true;
 		}
 		}
 		setTimeout(function () {
 		setTimeout(function () {
@@ -1047,6 +1051,22 @@ domClass("HTMLLinkElement", tag("LINK"));
 domClass("HTMLScriptElement", tag("SCRIPT"));
 domClass("HTMLScriptElement", tag("SCRIPT"));
 domClass("HTMLStyleElement", tag("STYLE"));
 domClass("HTMLStyleElement", tag("STYLE"));
 domClass("HTMLCanvasElement", tag("CANVAS"));
 domClass("HTMLCanvasElement", tag("CANVAS"));
+domClass("HTMLMediaElement", function (o) {
+	return o.nodeType === 1 && (o.tagName === "VIDEO" || o.tagName === "AUDIO");
+});
+domClass("HTMLVideoElement", tag("VIDEO"));
+domClass("HTMLAudioElement", tag("AUDIO"));
+
+/* <video>/<audio> are placeholders: the C hook hands these methods to
+ * media elements only (so `"play" in div` stays false).  play() resolves
+ * without playing: pages call it without a catch on scroll, and a
+ * rejection would only add an "Uncaught" line per element. */
+def("__nb_media", {
+	play: function () { return Promise.resolve(); },
+	pause: function () {},
+	load: function () {},
+	canPlayType: function () { return ""; }
+});
 def("Window", function Window() {});
 def("Window", function Window() {});
 
 
 /* ---- Custom Elements ----
 /* ---- Custom Elements ----

Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio