|
|
@@ -20,6 +20,7 @@
|
|
|
#include "browser_html.h"
|
|
|
#include "js_browser.h"
|
|
|
#include "uhttp.h"
|
|
|
+#include "uhttp_cookie.h"
|
|
|
#include "img.h"
|
|
|
#include "inflate.h"
|
|
|
#include "ufont.h"
|
|
|
@@ -1283,19 +1284,41 @@ static char *js_host_load_script(const char *url, unsigned int *len)
|
|
|
/* Web Storage lives in /share, outside the page heap that every navigation
|
|
|
* resets: localStorage per origin for the boot, sessionStorage per origin
|
|
|
* and browser window. */
|
|
|
-static void js_storage_path(int kind, char *out, unsigned int cap)
|
|
|
+#define JS_ORIGIN_MAX 160
|
|
|
+
|
|
|
+/* scheme://host[:port] of the page (a local page's whole path). */
|
|
|
+static void js_page_origin(char *origin, unsigned int cap)
|
|
|
{
|
|
|
- char origin[160];
|
|
|
- unsigned int n = 0, i = 0, slashes = 0;
|
|
|
+ unsigned int i = 0, slashes = 0;
|
|
|
|
|
|
- while (page_url[i] && i + 1 < sizeof(origin)) {
|
|
|
+ while (page_url[i] && i + 1 < cap) {
|
|
|
if (page_url[i] == '/' && ++slashes == 3)
|
|
|
break;
|
|
|
origin[i] = page_url[i];
|
|
|
i++;
|
|
|
}
|
|
|
origin[i] = '\0';
|
|
|
- s_append(out, &n, kind ? "/share/bss" : "/share/bls", cap);
|
|
|
+}
|
|
|
+
|
|
|
+static void js_origin_path(const char *origin, char *out, unsigned int cap)
|
|
|
+{
|
|
|
+ unsigned int n = 0;
|
|
|
+
|
|
|
+ s_append(out, &n, "/share/bls", cap);
|
|
|
+ s_append_dec(out, &n, script_cache_hash(origin), cap);
|
|
|
+}
|
|
|
+
|
|
|
+static void js_storage_path(int kind, char *out, unsigned int cap)
|
|
|
+{
|
|
|
+ char origin[JS_ORIGIN_MAX];
|
|
|
+ unsigned int n = 0;
|
|
|
+
|
|
|
+ js_page_origin(origin, sizeof(origin));
|
|
|
+ if (!kind) {
|
|
|
+ js_origin_path(origin, out, cap);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ s_append(out, &n, "/share/bss", cap);
|
|
|
if (kind) {
|
|
|
s_append_dec(out, &n, (unsigned int)u_getpid(), cap);
|
|
|
s_append(out, &n, "-", cap);
|
|
|
@@ -1303,27 +1326,285 @@ static void js_storage_path(int kind, char *out, unsigned int cap)
|
|
|
s_append_dec(out, &n, script_cache_hash(origin), cap);
|
|
|
}
|
|
|
|
|
|
+static void persist_mark_origin(const char *origin);
|
|
|
+static int persist_store_lookup(const char *origin, char *out, int cap);
|
|
|
+
|
|
|
static int js_host_storage(int kind, const char *set, char *out, int cap)
|
|
|
{
|
|
|
- char path[48];
|
|
|
+ char path[48], origin[JS_ORIGIN_MAX];
|
|
|
int rv;
|
|
|
|
|
|
js_storage_path(kind, path, sizeof(path));
|
|
|
if (set) {
|
|
|
- if (!set[0] || !strcmp(set, "{}")) {
|
|
|
- u_syscall1(SYS_UNLINK, (int)path);
|
|
|
- return 0;
|
|
|
+ if (kind) {
|
|
|
+ if (!set[0] || !strcmp(set, "{}")) {
|
|
|
+ u_syscall1(SYS_UNLINK, (int)path);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ /* localStorage keeps an explicit "{}" rather than no
|
|
|
+ * file: a missing /share file means "not loaded from
|
|
|
+ * /data yet", and a cleared store must not come back
|
|
|
+ * from the disk copy before it is flushed. */
|
|
|
+ if (!set[0])
|
|
|
+ set = "{}";
|
|
|
+ js_page_origin(origin, sizeof(origin));
|
|
|
+ persist_mark_origin(origin);
|
|
|
}
|
|
|
return u_syscall3(SYS_WRITE_FILE, (int)path, (int)set,
|
|
|
(int)strlen(set)) < 0 ? -1 : 0;
|
|
|
}
|
|
|
rv = u_syscall3(SYS_READ_FILE, (int)path, (int)out, cap - 1);
|
|
|
+ if (rv < 0 && !kind) {
|
|
|
+ /* First use this boot: the persistent copy on /data. */
|
|
|
+ js_page_origin(origin, sizeof(origin));
|
|
|
+ rv = persist_store_lookup(origin, out, cap);
|
|
|
+ if (rv >= 0)
|
|
|
+ u_syscall3(SYS_WRITE_FILE, (int)path, (int)out, rv);
|
|
|
+ }
|
|
|
if (rv < 0)
|
|
|
return -1;
|
|
|
out[rv] = '\0';
|
|
|
return rv;
|
|
|
}
|
|
|
|
|
|
+/* ---- persistence on /data ----
|
|
|
+ *
|
|
|
+ * /share is the working copy (RAM, shared by every helper and window); the
|
|
|
+ * installed disk's /data volume keeps the cookie jar and localStorage
|
|
|
+ * across reboots when it is mounted. The browser owns the disk copies: at
|
|
|
+ * start it restores the jar into /share, and its main loop writes back
|
|
|
+ * whatever changed at most every PERSIST_MS, so a burst of cookie or
|
|
|
+ * setItem updates costs one FAT write. The helpers only touch /share (they
|
|
|
+ * are killed on focus loss, so they cannot be trusted to flush). Without a
|
|
|
+ * /data mount every disk access simply fails. Both files are plaintext
|
|
|
+ * bearer credentials, like /data/CHAT.CFG. sessionStorage stays in RAM. */
|
|
|
+#define PERSIST_MS 3000u
|
|
|
+#define SHARE_COOKIES "/share/bcookies"
|
|
|
+#define DATA_COOKIES "/data/BCOOKIES.BIN"
|
|
|
+#define DATA_STORE "/data/BSTORE.DAT"
|
|
|
+#define DATA_STORE_MAGIC "NBLS1\n"
|
|
|
+#define DATA_STORE_MAX (192u * 1024u)
|
|
|
+#define PERSIST_DIRTY_MAX 8
|
|
|
+
|
|
|
+static unsigned int persist_due;
|
|
|
+static unsigned int cookie_sum_saved;
|
|
|
+static int cookie_sum_valid;
|
|
|
+static char persist_dirty[PERSIST_DIRTY_MAX][JS_ORIGIN_MAX];
|
|
|
+static int persist_ndirty;
|
|
|
+
|
|
|
+static unsigned int persist_sum(const unsigned char *p, unsigned int n)
|
|
|
+{
|
|
|
+ unsigned int h = 2166136261u, i;
|
|
|
+
|
|
|
+ for (i = 0; i < n; i++)
|
|
|
+ h = (h ^ p[i]) * 16777619u;
|
|
|
+ return h;
|
|
|
+}
|
|
|
+
|
|
|
+/* Called once at startup, before any helper runs. */
|
|
|
+static void persist_restore(void)
|
|
|
+{
|
|
|
+ unsigned char *jar = (unsigned char *)malloc(sizeof(struct uhttp_jar));
|
|
|
+ int n;
|
|
|
+
|
|
|
+ if (!jar)
|
|
|
+ return;
|
|
|
+ n = u_syscall3(SYS_READ_FILE, (int)SHARE_COOKIES, (int)jar,
|
|
|
+ (int)sizeof(struct uhttp_jar));
|
|
|
+ if (n != (int)sizeof(struct uhttp_jar)) {
|
|
|
+ n = u_syscall3(SYS_READ_FILE, (int)DATA_COOKIES, (int)jar,
|
|
|
+ (int)sizeof(struct uhttp_jar));
|
|
|
+ if (n == (int)sizeof(struct uhttp_jar)) {
|
|
|
+ u_syscall3(SYS_WRITE_FILE, (int)SHARE_COOKIES,
|
|
|
+ (int)jar, n);
|
|
|
+ cookie_sum_saved = persist_sum(jar, (unsigned int)n);
|
|
|
+ cookie_sum_valid = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ free(jar);
|
|
|
+ persist_due = u_ticks_ms() + PERSIST_MS;
|
|
|
+}
|
|
|
+
|
|
|
+static void persist_mark_origin(const char *origin)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+
|
|
|
+ for (i = 0; i < persist_ndirty; i++)
|
|
|
+ if (s_eq(persist_dirty[i], origin))
|
|
|
+ return;
|
|
|
+ if (persist_ndirty < PERSIST_DIRTY_MAX)
|
|
|
+ s_copy(persist_dirty[persist_ndirty++], origin, JS_ORIGIN_MAX);
|
|
|
+}
|
|
|
+
|
|
|
+/* BSTORE.DAT: "NBLS1\n", then per origin "<origin>\n<length>\n<json>\n".
|
|
|
+ * Walk it; returns 1 with the record's fields, 0 at the end. */
|
|
|
+static int store_next(const char *buf, unsigned int len, unsigned int *pos,
|
|
|
+ unsigned int *o_off, unsigned int *o_len,
|
|
|
+ unsigned int *d_off, unsigned int *d_len,
|
|
|
+ unsigned int *rec_end)
|
|
|
+{
|
|
|
+ unsigned int p = *pos, v = 0;
|
|
|
+
|
|
|
+ if (p >= len)
|
|
|
+ return 0;
|
|
|
+ *o_off = p;
|
|
|
+ while (p < len && buf[p] != '\n')
|
|
|
+ p++;
|
|
|
+ if (p >= len)
|
|
|
+ return 0;
|
|
|
+ *o_len = p - *o_off;
|
|
|
+ p++;
|
|
|
+ while (p < len && buf[p] >= '0' && buf[p] <= '9')
|
|
|
+ v = v * 10 + (unsigned int)(buf[p++] - '0');
|
|
|
+ if (p >= len || buf[p] != '\n')
|
|
|
+ return 0;
|
|
|
+ p++;
|
|
|
+ if (v > len - p)
|
|
|
+ return 0;
|
|
|
+ *d_off = p;
|
|
|
+ *d_len = v;
|
|
|
+ p += v;
|
|
|
+ if (p < len && buf[p] == '\n')
|
|
|
+ p++;
|
|
|
+ *rec_end = p;
|
|
|
+ *pos = p;
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+static int store_load(char **out)
|
|
|
+{
|
|
|
+ char *buf = (char *)malloc(DATA_STORE_MAX + 1);
|
|
|
+ int n;
|
|
|
+
|
|
|
+ *out = buf;
|
|
|
+ if (!buf)
|
|
|
+ return -1;
|
|
|
+ n = u_syscall3(SYS_READ_FILE, (int)DATA_STORE, (int)buf,
|
|
|
+ (int)DATA_STORE_MAX);
|
|
|
+ if (n < (int)s_len(DATA_STORE_MAGIC) ||
|
|
|
+ !starts_with(buf, DATA_STORE_MAGIC))
|
|
|
+ return 0;
|
|
|
+ return n;
|
|
|
+}
|
|
|
+
|
|
|
+static int persist_store_lookup(const char *origin, char *out, int cap)
|
|
|
+{
|
|
|
+ char *buf;
|
|
|
+ unsigned int pos = s_len(DATA_STORE_MAGIC), oo, ol, d, dl, e;
|
|
|
+ unsigned int olen = s_len(origin);
|
|
|
+ int n = store_load(&buf), rv = -1;
|
|
|
+
|
|
|
+ while (n > 0 && store_next(buf, (unsigned int)n, &pos, &oo, &ol, &d,
|
|
|
+ &dl, &e)) {
|
|
|
+ if (ol == olen && !memcmp(buf + oo, origin, olen)) {
|
|
|
+ if ((int)dl < cap) {
|
|
|
+ memcpy(out, buf + d, dl);
|
|
|
+ out[dl] = '\0';
|
|
|
+ rv = (int)dl;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ free(buf);
|
|
|
+ return rv;
|
|
|
+}
|
|
|
+
|
|
|
+/* Rewrite BSTORE.DAT with the dirty origins' current /share copies. */
|
|
|
+static void persist_store_flush(void)
|
|
|
+{
|
|
|
+ char *old, *nb, path[48];
|
|
|
+ unsigned int pos = s_len(DATA_STORE_MAGIC), oo, ol, d, dl, e, nn = 0;
|
|
|
+ int n = store_load(&old), i, w;
|
|
|
+
|
|
|
+ nb = (char *)malloc(DATA_STORE_MAX + 1);
|
|
|
+ if (n < 0 || !nb) {
|
|
|
+ free(old);
|
|
|
+ free(nb);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ s_append(nb, &nn, DATA_STORE_MAGIC, DATA_STORE_MAX);
|
|
|
+ /* Untouched origins first, in their old order. */
|
|
|
+ while (n > 0 && store_next(old, (unsigned int)n, &pos, &oo, &ol, &d,
|
|
|
+ &dl, &e)) {
|
|
|
+ int dirty = 0;
|
|
|
+
|
|
|
+ for (i = 0; i < persist_ndirty; i++)
|
|
|
+ if (s_len(persist_dirty[i]) == ol &&
|
|
|
+ !memcmp(persist_dirty[i], old + oo, ol))
|
|
|
+ dirty = 1;
|
|
|
+ if (dirty || nn + (e - oo) > DATA_STORE_MAX)
|
|
|
+ continue;
|
|
|
+ memcpy(nb + nn, old + oo, e - oo);
|
|
|
+ nn += e - oo;
|
|
|
+ }
|
|
|
+ /* Then the changed ones (an emptied store is dropped). */
|
|
|
+ for (i = 0; i < persist_ndirty; i++) {
|
|
|
+ char num[12];
|
|
|
+ unsigned int k = 0;
|
|
|
+
|
|
|
+ js_origin_path(persist_dirty[i], path, sizeof(path));
|
|
|
+ w = u_syscall3(SYS_READ_FILE, (int)path, (int)old,
|
|
|
+ (int)DATA_STORE_MAX);
|
|
|
+ if (w <= 2) /* missing or "{}" */
|
|
|
+ continue;
|
|
|
+ s_append_dec(num, &k, (unsigned int)w, sizeof(num));
|
|
|
+ if (nn + s_len(persist_dirty[i]) + k + (unsigned int)w + 3 >
|
|
|
+ DATA_STORE_MAX) {
|
|
|
+ set_status("Saved site data is full; a site's "
|
|
|
+ "localStorage was not saved", 1, 6000);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ s_append(nb, &nn, persist_dirty[i], DATA_STORE_MAX);
|
|
|
+ nb[nn++] = '\n';
|
|
|
+ s_append(nb, &nn, num, DATA_STORE_MAX);
|
|
|
+ nb[nn++] = '\n';
|
|
|
+ memcpy(nb + nn, old, (unsigned int)w);
|
|
|
+ nn += (unsigned int)w;
|
|
|
+ nb[nn++] = '\n';
|
|
|
+ }
|
|
|
+ u_syscall3(SYS_WRITE_FILE, (int)DATA_STORE, (int)nb, (int)nn);
|
|
|
+ persist_ndirty = 0;
|
|
|
+ free(old);
|
|
|
+ free(nb);
|
|
|
+}
|
|
|
+
|
|
|
+static void persist_cookies_flush(void)
|
|
|
+{
|
|
|
+ unsigned char *jar = (unsigned char *)malloc(sizeof(struct uhttp_jar));
|
|
|
+ unsigned int sum;
|
|
|
+ int n;
|
|
|
+
|
|
|
+ if (!jar)
|
|
|
+ return;
|
|
|
+ n = u_syscall3(SYS_READ_FILE, (int)SHARE_COOKIES, (int)jar,
|
|
|
+ (int)sizeof(struct uhttp_jar));
|
|
|
+ if (n == (int)sizeof(struct uhttp_jar)) {
|
|
|
+ sum = persist_sum(jar, (unsigned int)n);
|
|
|
+ if (!cookie_sum_valid || sum != cookie_sum_saved) {
|
|
|
+ if (u_syscall3(SYS_WRITE_FILE, (int)DATA_COOKIES,
|
|
|
+ (int)jar, n) >= 0) {
|
|
|
+ cookie_sum_saved = sum;
|
|
|
+ cookie_sum_valid = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ free(jar);
|
|
|
+}
|
|
|
+
|
|
|
+/* Main-loop hook: write back to /data at most every PERSIST_MS. */
|
|
|
+static void persist_tick(void)
|
|
|
+{
|
|
|
+ unsigned int now = u_ticks_ms();
|
|
|
+
|
|
|
+ if ((int)(now - persist_due) < 0)
|
|
|
+ return;
|
|
|
+ persist_due = now + PERSIST_MS;
|
|
|
+ persist_cookies_flush();
|
|
|
+ if (persist_ndirty)
|
|
|
+ persist_store_flush();
|
|
|
+}
|
|
|
+
|
|
|
/* <link rel="modulepreload">: download the page's module graph on all
|
|
|
* helpers at once into the boot script cache, before any script runs. The
|
|
|
* engine's module loader is sequential (one blocking fetch per import), so
|
|
|
@@ -10053,6 +10334,7 @@ void _start(const char *arg)
|
|
|
origin_cache_load();
|
|
|
css_cache_init();
|
|
|
script_cache_init();
|
|
|
+ persist_restore();
|
|
|
page_scroll.offset = 0;
|
|
|
page_scroll.content_h = 0;
|
|
|
|
|
|
@@ -10093,6 +10375,7 @@ void _start(const char *arg)
|
|
|
preload_active = 1;
|
|
|
}
|
|
|
js_browser_tick();
|
|
|
+ persist_tick();
|
|
|
if (pending_action)
|
|
|
continue;
|
|
|
now = u_ticks_ms();
|