/* browser: a small HTML/CSS browser built on ngui, the image library and the * shared uhttp client. * * Page scripts run on the vendored QuickJS engine (modern ECMAScript) through * the DOM layer in js_browser.c; forms render and submit natively. 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 #include #include "ucmd_lib.h" #include "ngui.h" #include "browser_html.h" #include "js_browser.h" #include "uhttp.h" #include "uhttp_cookie.h" #include "img.h" #include "inflate.h" #include "ufont.h" #include "svg_raster.h" static void svg_cache_clear(void); static unsigned char *decode_arena; /* see decode_arena_get() */ static unsigned int decode_arena_size; /* 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 256 /* Decoded display bitmaps share the page heap: cap their total pixels * (RGB565 plus an optional alpha plane) instead of their count. */ #define BR_IMG_PIXEL_BUDGET (4u * 1024u * 1024u) #define BR_IMG_ARENA_MAX (24u * 1024u * 1024u) #define BR_IMG_LAYOUT_W 1000 /* An image job may hold a fetch helper this long. The page/script * deadline (UHTTP_TOTAL_TIMEOUT_MS) is minutes; one unreachable image host * must not starve the other images of all four helpers that long. */ #define BR_IMG_JOB_TIMEOUT_MS 30000u #define BR_CSS_MAX (832u * 1024u) #define BR_CSS_COMP_MAX (192u * 1024u) /* Scripts and ES modules: Vite bundles reach ~1.7 MB (the Cloudron * dashboard's main chunk); the helper's FETCH_MAX is the same 2 MB. */ #define BR_SCRIPT_MAX (2u * 1024u * 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 24 #define SCRIPT_CACHE_FILE_MAX BR_SCRIPT_MAX #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; unsigned int url_hash; /* source, 0 = not shareable */ 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; static unsigned int image_pixels; /* 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_max_jobs; static int preload_pending; static int preload_wait; static unsigned int preload_node; static int preload_loaded; static int preload_scroll; static int preload_rescan; 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; /* The pending navigation is a POST even when its body is empty (a form * without fields, e.g. the OIDC consent auto-submit). */ static int pending_post_is; /* window "resize" bookkeeping: the page width the last event was for. */ static int resize_sent_w = -1; static int resize_pending; 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; /* Height of the page viewport: what html/body height:100% resolves to. */ static int layout_view_h; static unsigned int layout_nnode; static int layout_content_h; /* layout output */ struct lay_run { unsigned int off; unsigned short 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, strike; unsigned char overline, font_face; unsigned char transform_scale; unsigned char origin_x_kind, origin_y_kind; signed short scale_x, scale_y, rotate_x100; signed short origin_x, origin_y; 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 parent_top; int cb_left, cb_right, cb_top; /* containing block for absolute boxes */ int cb_h; /* definite content height of the containing block for * percentage heights; 0 = indefinite (auto) */ 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 int link_fragment_nav(unsigned int node); static void history_push(const char *url); 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 void style_for_node(unsigned int node, struct br_style *out); 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; } /* A