/* browser: a small HTML/CSS browser built on ngui, the image library and the * shared uhttp client. * * Inline JavaScript runs through the vendored MuJS engine (console.log, * alert, document.getElementById(...).textContent and location.href); forms * are not supported. External CSS is loaded over uhttp, and images are * decoded with img.c and drawn inline. Page-scoped buffers (page body, * rules and image bitmaps) come from the ring-3 libc heap; each image gets an * exact-size decode arena that is freed as soon as the scaled bitmap exists. * The DOM and the image/CSS I/O scratch use fixed static arenas. */ #include #include #include #include "ucmd_lib.h" #include "ngui.h" #include "browser_html.h" #include "js_browser.h" #include "uhttp.h" #include "img.h" #include "inflate.h" #include "ufont.h" /* Start wide enough for the desktop nav; smaller screens fall back to * 960x640 and 780x540, which still fit a 1024x768 panel. */ #define BR_W 1100 #define BR_H 700 #define BR_TOOLBAR_H 30 #define BR_STATUS_H 18 #define BR_PAGE_MAX (2u * 1024u * 1024u) #define BR_IMG_FILE_MAX (512u * 1024u) #define BR_IMG_PIX_MAX (8u * 1024u * 1024u) #define BR_MAX_IMAGES 16 #define BR_IMG_ARENA_MAX (24u * 1024u * 1024u) #define BR_IMG_LAYOUT_W 1000 #define BR_CSS_MAX (832u * 1024u) #define BR_CSS_COMP_MAX (192u * 1024u) #define BR_SCRIPT_MAX (512u * 1024u) #define FETCH_ABORT (-2) /* Session cache for remote stylesheets (per launch; cleared at startup). */ #define CSS_CACHE_MAX 16 #define CSS_CACHE_FILE_MAX (256u * 1024u) #define CSS_CACHE_PREFIX "bcss-" #define SCRIPT_CACHE_MAX 16 #define SCRIPT_CACHE_FILE_MAX (512u * 1024u) #define SCRIPT_CACHE_PREFIX "bjs-" #define BR_MAX_SHEETS 6 #define BR_HISTORY 16 #define BR_URL_MAX 512 #define BR_STATUS_MAX 200 #define BR_IMG_RGB565(r, g, b) \ ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))) #define BR_GRAY 0x8410 enum { ID_URL = 1, ID_GO, ID_BACK, ID_FORWARD, ID_RELOAD, ID_HOME }; #define ACT_NAV (1u << 0) #define ACT_BACK (1u << 1) #define ACT_FORWARD (1u << 2) #define ACT_RELOAD (1u << 3) #define ACT_HOME (1u << 4) #define ACT_CLOSE (1u << 5) #define ACT_WAIT_NAV (ACT_NAV | ACT_BACK | ACT_FORWARD | ACT_RELOAD | \ ACT_HOME | ACT_CLOSE) struct br_image { int state; /* 0 empty, 1 ready */ struct img_image img; /* scaled bitmap at <= BR_IMG_LAYOUT_W */ int native_w, native_h; char err[32]; }; static struct ngui ui; static struct ngui_input url_in; static struct ngui_scroll page_scroll; static struct br_doc doc; static struct br_image images[BR_MAX_IMAGES]; static int image_count; /* Painted border boxes, in page/screen coordinates (layout starts at * page_rect.y + 4 - scroll). getBoundingClientRect() subtracts page_rect. */ struct br_layout_box { int x, y; int w, h; unsigned char valid; }; static struct br_layout_box layout_boxes[BR_MAX_NODES]; /* Images are loaded one per main-loop iteration so the parsed page stays * interactive while a slow network/TLS fetch or 4K decode is in progress. */ static int preload_active; static int preload_pending; static int preload_wait; static unsigned int preload_node; static int preload_loaded; static int preload_scroll; static char post_status_text[BR_STATUS_MAX]; static int post_status_error; static unsigned int post_status_ttl; /* Content-relative position/height of each , recorded by the measure * pass so image loading can be deferred until it is near the viewport. */ static unsigned short img_top[BR_MAX_NODES]; static unsigned short img_h[BR_MAX_NODES]; /* One reusable I/O buffer: the two halves double as the gzip input scratch * for page/CSS fetches, where the heap has no room for another multi-megabyte * body. Decode arenas are allocated per image instead of living here. */ static unsigned char *image_io_buf; static struct ufont_face *font_faces[BR_MAX_FACES]; #define image_file_buf (image_io_buf) #define image_comp_buf (image_io_buf + BR_IMG_FILE_MAX) #define BR_PAGE_COMP_MAX (2u * BR_IMG_FILE_MAX) static struct inflate image_inflate_ws; #if BR_CSS_MAX + BR_CSS_COMP_MAX > 2u * BR_IMG_FILE_MAX #error "CSS scratch must fit in image_io_buf" #endif static char page_url[BR_URL_MAX]; static char page_title[BR_TITLE_MAX]; static char cwd[64]; static int page_is_local; static int page_loading; static char status_text[BR_STATUS_MAX]; static unsigned int status_until; static int status_error; static char history[BR_HISTORY][BR_URL_MAX]; static int hist_len, hist_pos; static char click_url[BR_URL_MAX]; static char pending_url[BR_URL_MAX]; #define BR_POST_MAX 4096 static char pending_post[BR_POST_MAX]; static unsigned int pending_post_len; static unsigned int pending_action; static int cancel_fetch; static int quit; static struct ngui_rect page_rect; static unsigned short *page_backbuf; static int pagebuf_w, pagebuf_h; static int page_cache_valid; static int page_cache_scroll; static unsigned int page_cache_nnode; static int page_cache_x, page_cache_y, page_cache_w, page_cache_h; static int layout_valid, layout_left, layout_right; static unsigned int layout_nnode; static int layout_content_h; /* layout output */ struct lay_run { unsigned short off, len; unsigned short gap; unsigned short color; unsigned short href; char lit[28]; short img; signed short dy; /* inline image vertical offset */ unsigned short iw, ih; unsigned short w; unsigned char font_px, bold, italic, mono, underline, font_face; unsigned char has_lit; unsigned int form; /* node + 1, 0 = not a control */ unsigned char form_kind; unsigned char form_disabled; }; struct lay { int draw; struct ngui *ui; int left, right; int y; int line_x, line_y, line_h; int line_left, line_right; int run_x; int align; int depth; int space_pending; unsigned char float_active; int float_left, float_right, float_bottom; struct lay_run runs[96]; int nruns; }; struct hit { int x, y, w, h; unsigned short node; }; /* Live form state; the DOM attributes stay the initial/default value. */ #define FORM_MAX 64 #define FORM_VALUE_MAX 63 enum { FORM_TEXT, FORM_PASSWORD, FORM_CHECKBOX, FORM_RADIO, FORM_BUTTON, FORM_SUBMIT, FORM_RESET, FORM_SELECT, FORM_TEXTAREA }; struct form_state { unsigned int node; char value[FORM_VALUE_MAX + 1]; unsigned char len, caret, checked, selected, used; }; static struct form_state form_states[FORM_MAX]; static unsigned int form_focus; static unsigned int form_dirty; static int form_descendant(unsigned int node, unsigned int form); static int form_submit_form(unsigned int node, int fire_event); static void form_reset_event(unsigned int form); static unsigned int s_len(const char *s); static int s_eq(const char *a, const char *b); static void form_key(int k); static void form_click(unsigned int node); static int form_disabled(unsigned int node) { struct br_node *n = &doc.nodes[node]; if (n->attr_flags & BR_ATTR_DISABLED) return 1; return br_find_attr(&doc, node, "disabled") != 0; } static int form_kind_of(unsigned int node) { const char *type = br_type(&doc, node); if (doc.nodes[node].tag == BR_TAG_TEXTAREA) return FORM_TEXTAREA; if (doc.nodes[node].tag == BR_TAG_SELECT) return FORM_SELECT; if (doc.nodes[node].tag == BR_TAG_BUTTON) { if (s_eq(type, "reset")) return FORM_RESET; if (s_eq(type, "button")) return FORM_BUTTON; return FORM_SUBMIT; } if (s_eq(type, "password")) return FORM_PASSWORD; if (s_eq(type, "checkbox")) return FORM_CHECKBOX; if (s_eq(type, "radio")) return FORM_RADIO; if (s_eq(type, "submit")) return FORM_SUBMIT; if (s_eq(type, "reset")) return FORM_RESET; if (s_eq(type, "button")) return FORM_BUTTON; return FORM_TEXT; } static unsigned int form_option_count(unsigned int node) { unsigned int c, n = 0; for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) if (doc.nodes[c].type == BR_NODE_ELEM && doc.nodes[c].tag == BR_TAG_OPTION) n++; return n; } static unsigned int form_option_selected(unsigned int node) { unsigned int c, i = 0; for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) { if (doc.nodes[c].type != BR_NODE_ELEM || doc.nodes[c].tag != BR_TAG_OPTION) continue; if (br_find_attr(&doc, c, "selected")) return i; i++; } return 0; } static unsigned int form_option_text(unsigned int node, unsigned int idx, char *out, unsigned int cap) { unsigned int c, i = 0; for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) { if (doc.nodes[c].type != BR_NODE_ELEM || doc.nodes[c].tag != BR_TAG_OPTION) continue; if (i == idx) return br_text_content(&doc, c, out, cap); i++; } return 0; } static struct form_state *form_get(unsigned int node, int create) { struct form_state *slot = 0; unsigned int i; for (i = 0; i < FORM_MAX; i++) { if (form_states[i].used) { if (form_states[i].node == node) return &form_states[i]; } else if (!slot) { slot = &form_states[i]; } } if (!create || !slot) return 0; slot->node = node; slot->len = 0; slot->caret = 0; slot->checked = br_find_attr(&doc, node, "checked") != 0; slot->selected = form_option_selected(node); slot->used = 1; { unsigned int n = 0; if (doc.nodes[node].tag == BR_TAG_TEXTAREA) { n = br_text_content(&doc, node, slot->value, FORM_VALUE_MAX + 1); } else { const char *v = br_value(&doc, node); if (v) { n = s_len(v); if (n > FORM_VALUE_MAX) n = FORM_VALUE_MAX; for (unsigned int j = 0; j < n; j++) slot->value[j] = v[j]; } if (doc.nodes[node].tag == BR_TAG_BUTTON && n == 0) n = br_text_content(&doc, node, slot->value, FORM_VALUE_MAX + 1); } if (n > FORM_VALUE_MAX) n = FORM_VALUE_MAX; slot->value[n] = '\0'; slot->len = (unsigned char)n; slot->caret = (unsigned char)n; } return slot; } static void form_reset_state(void) { unsigned int i; for (i = 0; i < FORM_MAX; i++) form_states[i].used = 0; form_focus = 0; } static int form_is_control(unsigned int node) { unsigned int tag; if (node >= doc.nnode || doc.nodes[node].type != BR_NODE_ELEM) return 0; tag = doc.nodes[node].tag; return tag == BR_TAG_INPUT || tag == BR_TAG_TEXTAREA || tag == BR_TAG_SELECT || tag == BR_TAG_BUTTON; } #define HIT_MAX 96 static struct hit hits[HIT_MAX]; static int nhits; static unsigned short block_h[BR_MAX_NODES]; static char li_marker[BR_MAX_NODES][6]; /* one connection object; the browser fetches one resource at a time */ /* ---- tiny strings ---- */ static unsigned int s_len(const char *s) { unsigned int n = 0; while (s && s[n]) n++; return n; } static void s_copy(char *d, const char *s, unsigned int cap) { unsigned int i = 0; if (!cap) return; while (s && s[i] && i + 1 < cap) { d[i] = s[i]; i++; } d[i] = '\0'; } static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap) { while (s && *s && *n + 1 < cap) d[(*n)++] = *s++; d[*n] = '\0'; } static void s_append_dec(char *d, unsigned int *n, unsigned int v, unsigned int cap) { char tmp[12]; unsigned int i = 0, j; do { tmp[i++] = (char)('0' + v % 10); v /= 10; } while (v && i < sizeof(tmp)); for (j = 0; j < i && *n + 1 < cap; j++) d[(*n)++] = tmp[i - 1 - j]; d[*n] = '\0'; } static int s_eq(const char *a, const char *b) { while (*a && *b) { if (*a != *b) return 0; a++; b++; } return *a == *b; } static int starts_with(const char *s, const char *p) { unsigned int i = 0; while (p[i]) { if (s[i] != p[i]) return 0; i++; } return 1; } static int contains_ci(const char *s, const char *p) { unsigned int i, j; for (i = 0; s[i]; i++) { for (j = 0; p[j]; j++) { char a = s[i + j]; if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a'); if (a != p[j]) break; } if (!p[j]) return 1; } return 0; } static char *trim(char *s) { unsigned int n; while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n') s++; n = s_len(s); while (n && (s[n - 1] == ' ' || s[n - 1] == '\t' || s[n - 1] == '\r' || s[n - 1] == '\n')) s[--n] = '\0'; return s; } /* ---- per-process heap ---- */ static int heap_start(void) { image_io_buf = (unsigned char *)malloc(2u * BR_IMG_FILE_MAX); if (!image_io_buf) return -1; page_backbuf = (unsigned short *)malloc( (unsigned int)ui.w * (unsigned int)ui.h * 2u); pagebuf_w = page_backbuf ? ui.w : 0; pagebuf_h = page_backbuf ? ui.h : 0; page_cache_valid = 0; return 0; } static void heap_clear(void) { memset(font_faces, 0, sizeof(font_faces)); ngui_font_detach(&ui); heap_reset(); image_io_buf = 0; page_backbuf = 0; pagebuf_w = pagebuf_h = 0; page_cache_valid = 0; } static void browser_rules_alloc(void) { struct br_rule *rules; rules = (struct br_rule *)malloc( (unsigned int)(BR_MAX_RULES * sizeof(struct br_rule))); br_set_rule_store(&doc, rules, rules ? BR_MAX_RULES : 0); } /* ---- status ---- */ static void set_status(const char *s, int err, unsigned int ttl_ms) { s_copy(status_text, s, sizeof(status_text)); status_error = err; status_until = ttl_ms ? u_ticks_ms() + ttl_ms : 0; ngui_invalidate(&ui); } /* ---- JavaScript ---- */ static int resolve_link(const char *base, const char *href, char *out, unsigned int cap); static char js_last_status[BR_STATUS_MAX]; static int js_last_error; static void js_host_status(const char *msg, int error) { s_copy(js_last_status, msg, sizeof(js_last_status)); js_last_error = error; set_status(msg, error, 6000); } static void js_host_report(const char *msg) { u_puts("[js] "); u_puts(msg); u_putc('\n'); s_copy(js_last_status, msg, sizeof(js_last_status)); js_last_error = 1; set_status(msg, 1, 8000); } static void js_host_navigate(const char *url) { char resolved[BR_URL_MAX]; if (!url || !url[0]) return; if (resolve_link(page_url, url, resolved, sizeof(resolved)) != 0) return; s_copy(pending_url, resolved, sizeof(pending_url)); pending_action |= ACT_NAV; cancel_fetch = 1; } static void js_host_changed(void) { /* Class/style/attribute mutations change computed styles, not just the * text: drop the per-node style cache before the next layout. */ br_reset_styles(&doc); layout_valid = 0; ngui_invalidate(&ui); } static int js_host_rect(unsigned int node, int *x, int *y, int *w, int *h) { if (node >= BR_MAX_NODES || !layout_boxes[node].valid) return 0; *x = layout_boxes[node].x - page_rect.x; *y = layout_boxes[node].y - page_rect.y; *w = layout_boxes[node].w; *h = layout_boxes[node].h; return 1; } static void form_copy_out(const char *s, char *buf, int cap) { int i = 0; if (!buf || cap <= 0) return; while (s && s[i] && i + 1 < cap) { buf[i] = s[i]; i++; } buf[i] = '\0'; } static int js_host_form_value(unsigned int node, char *buf, int cap) { struct form_state *fs; int kind; if (!form_is_control(node)) return 0; kind = form_kind_of(node); if (kind == FORM_CHECKBOX || kind == FORM_RADIO) return 0; if (kind == FORM_SELECT) { char opt[64] = ""; unsigned int c, i = 0; fs = form_get(node, 1); if (!fs) return 0; form_option_text(node, fs->selected, opt, sizeof(opt)); for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) { if (doc.nodes[c].type != BR_NODE_ELEM || doc.nodes[c].tag != BR_TAG_OPTION) continue; if (i == fs->selected) { const char *v = br_value(&doc, c); if (v && v[0]) form_copy_out(v, buf, cap); else form_copy_out(opt, buf, cap); return 1; } i++; } form_copy_out(opt, buf, cap); return 1; } fs = form_get(node, 1); if (!fs) return 0; form_copy_out(fs->value, buf, cap); return 1; } static int js_host_form_checked(unsigned int node) { struct form_state *fs; int kind; if (!form_is_control(node)) return -1; kind = form_kind_of(node); if (kind != FORM_CHECKBOX && kind != FORM_RADIO) return -1; fs = form_get(node, 1); return fs ? fs->checked : 0; } static int js_host_form_selected(unsigned int node) { struct form_state *fs; if (!form_is_control(node) || form_kind_of(node) != FORM_SELECT) return -1; fs = form_get(node, 1); return fs ? fs->selected : 0; } static int js_host_form_set_value(unsigned int node, const char *value) { struct form_state *fs; unsigned int n; if (!form_is_control(node)) return 0; fs = form_get(node, 1); if (!fs) return 0; n = s_len(value ? value : ""); if (n > FORM_VALUE_MAX) n = FORM_VALUE_MAX; for (unsigned int i = 0; i < n; i++) fs->value[i] = value[i]; fs->value[n] = '\0'; fs->len = (unsigned char)n; fs->caret = (unsigned char)n; layout_valid = 0; ngui_invalidate(&ui); return 1; } static int js_host_form_set_checked(unsigned int node, int checked) { struct form_state *fs; int kind; if (!form_is_control(node)) return 0; kind = form_kind_of(node); if (kind != FORM_CHECKBOX && kind != FORM_RADIO) return 0; fs = form_get(node, 1); if (!fs) return 0; fs->checked = (unsigned char)(checked != 0); layout_valid = 0; ngui_invalidate(&ui); return 1; } static int js_host_form_set_selected(unsigned int node, int index) { struct form_state *fs; unsigned int n; if (!form_is_control(node) || form_kind_of(node) != FORM_SELECT) return 0; fs = form_get(node, 1); if (!fs) return 0; n = form_option_count(node); if (index < 0) index = 0; if (n && (unsigned int)index >= n) index = (int)n - 1; fs->selected = (unsigned char)index; layout_valid = 0; ngui_invalidate(&ui); return 1; } static int js_host_form_elements(unsigned int form, unsigned int *out, int cap) { unsigned int i; int n = 0; if (!form || form >= doc.nnode || doc.nodes[form].type != BR_NODE_ELEM || doc.nodes[form].tag != BR_TAG_FORM) return 0; for (i = 0; i < doc.nnode && n < cap; i++) { struct br_node *nd = &doc.nodes[i]; if (i == form || nd->type != BR_NODE_ELEM) continue; if (nd->tag != BR_TAG_INPUT && nd->tag != BR_TAG_SELECT && nd->tag != BR_TAG_TEXTAREA && nd->tag != BR_TAG_BUTTON) continue; if (!form_descendant(i, form)) continue; out[n++] = i; } return n; } static int js_host_form_submit(unsigned int form) { form_submit_form(form, 0); return 1; } static int js_host_form_reset(unsigned int form) { form_reset_event(form); return 1; } static int js_host_click(unsigned int node) { char url[BR_URL_MAX]; const char *href; if (node >= doc.nnode) return 0; if (form_is_control(node)) { form_click(node); return 1; } href = br_href(&doc, node); if (resolve_link(page_url, href, url, sizeof(url)) != 0) return 0; s_copy(pending_url, url, sizeof(pending_url)); pending_action |= ACT_NAV; cancel_fetch = 1; ngui_invalidate(&ui); return 1; } static int fetch_script_text(const char *url, unsigned char **out, unsigned int *outlen, char *err, unsigned int errcap); static void script_error(const char *what, const char *url, const char *why) { char msg[192]; unsigned int n = 0; s_append(msg, &n, what, sizeof(msg)); if (url && url[0]) { s_append(msg, &n, " ", sizeof(msg)); s_append(msg, &n, url, sizeof(msg)); } if (why && why[0]) { s_append(msg, &n, ": ", sizeof(msg)); s_append(msg, &n, why, sizeof(msg)); } js_host_report(msg); } /* Run the page's scripts in document order: external