From ed5419ae41747a7a6b0597f0f100f71057898cb3 Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Wed, 28 Nov 2018 21:43:00 -0800 Subject: [PATCH] Toolbar icon! Simplify file defns! Pretty cool! --- extension/background.js | 63 +++++++++++++++++++++++------------------ extension/manifest.json | 2 ++ fs/hello.c | 36 +++++++++++------------ 3 files changed, 54 insertions(+), 47 deletions(-) diff --git a/extension/background.js b/extension/background.js index 25575ca..8588275 100644 --- a/extension/background.js +++ b/extension/background.js @@ -1,4 +1,24 @@ -const ws = new WebSocket("ws://localhost:8888"); +let ws; +function tryConnect() { + ws = new WebSocket("ws://localhost:8888"); + updateToolbarIcon(); + ws.onopen = ws.onclose = updateToolbarIcon; +} + +function updateToolbarIcon() { + if (ws && ws.readyState == 1) { // OPEN + chrome.browserAction.setBadgeBackgroundColor({color: 'blue'}); + chrome.browserAction.setBadgeText({text: 'f'}); + } else { + chrome.browserAction.setBadgeBackgroundColor({color: 'red'}); + chrome.browserAction.setBadgeText({text: '!'}); + } +} + +tryConnect(); +chrome.browserAction.onClicked.addListener(function() { + tryConnect(); +}); const unix = { EPERM: 1, @@ -61,6 +81,7 @@ const fhManager = (function() { // tabs/by-id/ID/mem (?) // tabs/by-id/ID/cpu (?) // tabs/by-id/ID/screenshot.png +// tabs/by-id/ID/text.txt // tabs/by-id/ID/printed.pdf // tabs/by-id/ID/control // tabs/by-id/ID/sources/ @@ -80,38 +101,16 @@ const router = { "*": { "url": { - async getattr() { - return { - st_mode: unix.S_IFREG | 0444, - st_nlink: 1, - st_size: 100 // FIXME - }; - }, - async open(path) { - return 0; - }, async read(path, fh, size, offset) { const tab = await getTab(parseInt(pathComponent(path, -2))); return (tab.url + "\n").substr(offset, size); - }, - async release(path, fh) {} + } }, "title": { - async getattr() { - return { - st_mode: unix.S_IFREG | 0444, - st_nlink: 1, - st_size: 1000 // FIXME - }; - }, - async open(path) { - return 0; - }, async read(path, fh, size, offset) { const tab = await getTab(parseInt(pathComponent(path, -2))); return (tab.title + "\n").substr(offset, size); - }, - async release(path, fh) {} + } }, } } @@ -133,7 +132,15 @@ async function getattr(path) { let route = findRoute(path); if (route.getattr) { return route.getattr(path); + } else if (route.read) { + // default file attrs + return { + st_mode: unix.S_IFREG | 0444, + st_nlink: 1, + st_size: 100 // FIXME + }; } else { + // default dir attrs return { st_mode: unix.S_IFDIR | 0755, st_nlink: 3 @@ -150,6 +157,7 @@ async function readdir(path) { async function open(path) { let route = findRoute(path); if (route.open) return route.open(path); + else return 0; // empty fd } async function read(path, fh, size, offset) { @@ -159,7 +167,7 @@ async function read(path, fh, size, offset) { async function release(path, fh) { let route = findRoute(path); - if (route.read) return route.release(path, fh); + if (route.release) return route.release(path, fh); } ws.onmessage = async function(event) { @@ -192,8 +200,7 @@ ws.onmessage = async function(event) { const buf = await read(req.path, req.fh, req.size, req.offset) response = { op: 'read', - buf, - size: buf.length + buf }; } else if (req.op === 'release') { diff --git a/extension/manifest.json b/extension/manifest.json index b523910..ac424f1 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -7,6 +7,8 @@ "permissions": ["tabs", "debugger"], + "browser_action": {}, + "background": { "scripts": ["background.js"], "persistent": true diff --git a/fs/hello.c b/fs/hello.c index f86ae59..66985e2 100644 --- a/fs/hello.c +++ b/fs/hello.c @@ -14,6 +14,8 @@ #include "cJSON/cJSON.h" #include "cJSON/cJSON.c" +#define DEBUG(...) + struct wby_server server; struct wby_con *con = NULL; @@ -205,10 +207,6 @@ hello_read(const char *path, char *buf, size_t size, off_t offset, cJSON_AddNumberToObject(req, "fh", fi->fh); cJSON_AddNumberToObject(req, "flags", fi->flags); }, { - size_t resp_size; - JSON_GET_PROP_INT(resp_size, "size"); - size = resp_size < size ? resp_size : size; - cJSON *resp_buf_item = cJSON_GetObjectItemCaseSensitive(resp, "buf"); char *resp_buf = cJSON_GetStringValue(resp_buf_item); size_t resp_buf_len = strlen(resp_buf); @@ -266,11 +264,11 @@ websocket_frame(struct wby_con *connection, const struct wby_frame *frame, void unsigned char data[131072] = {0}; int i = 0; - /* printf("WebSocket frame incoming\n"); */ - /* printf(" Frame OpCode: %d\n", frame->opcode); */ - /* printf(" Final frame?: %s\n", (frame->flags & WBY_WSF_FIN) ? "yes" : "no"); */ - /* printf(" Masked? : %s\n", (frame->flags & WBY_WSF_MASKED) ? "yes" : "no"); */ - /* printf(" Data Length : %d\n", (int) frame->payload_length); */ + DEBUG("WebSocket frame incoming\n"); + DEBUG(" Frame OpCode: %d\n", frame->opcode); + DEBUG(" Final frame?: %s\n", (frame->flags & WBY_WSF_FIN) ? "yes" : "no"); + DEBUG(" Masked? : %s\n", (frame->flags & WBY_WSF_MASKED) ? "yes" : "no"); + DEBUG(" Data Length : %d\n", (int) frame->payload_length); if ((unsigned long) frame->payload_length > sizeof(data)) { printf("Data too long!\n"); @@ -283,17 +281,17 @@ websocket_frame(struct wby_con *connection, const struct wby_frame *frame, void size_t read_size = remain > (int) sizeof buffer ? sizeof buffer : (size_t) remain; size_t k; - /* printf("%08x ", (int) i); */ + DEBUG("%08x ", (int) i); if (0 != wby_read(connection, buffer, read_size)) break; - /* for (k = 0; k < read_size; ++k) */ - /* printf("%02x ", buffer[k]); */ - /* for (k = read_size; k < 16; ++k) */ - /* printf(" "); */ - /* printf(" | "); */ - /* for (k = 0; k < read_size; ++k) */ - /* printf("%c", isprint(buffer[k]) ? buffer[k] : '?'); */ - /* printf("\n"); */ + for (k = 0; k < read_size; ++k) + DEBUG("%02x ", buffer[k]); + for (k = read_size; k < 16; ++k) + DEBUG(" "); + DEBUG(" | "); + for (k = 0; k < read_size; ++k) + DEBUG("%c", isprint(buffer[k]) ? buffer[k] : '?'); + DEBUG("\n"); for (k = 0; k < read_size; ++k) data[i + k] = buffer[k]; i += (int)read_size; @@ -338,7 +336,7 @@ websocket_closed(struct wby_con *connection, void *userdata) static void test_log(const char* text) { - /* printf("[debug] %s\n", text); */ + DEBUG("[debug] %s\n", text); } void *websocket_main(void *threadid)