surf

Mahdi's build of surf
git clone git://mahdi.pw/surf.git
Log | Files | Refs | README | LICENSE

webext-surf.c (2501B)


      1 #include <sys/socket.h>
      2 #include <sys/stat.h>
      3 #include <fcntl.h>
      4 #include <inttypes.h>
      5 #include <limits.h>
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 
      9 #include <gio/gio.h>
     10 #include <gio/gunixfdlist.h>
     11 #include <webkit2/webkit-web-extension.h>
     12 #include <webkitdom/webkitdom.h>
     13 #include <webkitdom/WebKitDOMDOMWindowUnstable.h>
     14 
     15 #include "common.h"
     16 
     17 #define LENGTH(x)   (sizeof(x) / sizeof(x[0]))
     18 
     19 static WebKitWebExtension *webext;
     20 static int sock;
     21 
     22 static void
     23 msgsurf(guint64 pageid, const char *s)
     24 {
     25 	static char msg[MSGBUFSZ];
     26 	size_t sln = strlen(s);
     27 	int ret;
     28 
     29 	if ((ret = snprintf(msg, sizeof(msg), "%c%s", pageid, s))
     30 	    >= sizeof(msg)) {
     31 		fprintf(stderr, "webext: msg: message too long: %d\n", ret);
     32 		return;
     33 	}
     34 
     35 	if (send(sock, msg, ret, 0) < 0)
     36 		fprintf(stderr, "webext: error sending: %s\n", msg+1);
     37 }
     38 
     39 static gboolean
     40 readsock(GIOChannel *s, GIOCondition c, gpointer unused)
     41 {
     42 	static char js[48], msg[MSGBUFSZ];
     43 	WebKitWebPage *page;
     44 	JSCContext *jsc;
     45 	GError *gerr = NULL;
     46 	gsize msgsz;
     47 
     48 	if (g_io_channel_read_chars(s, msg, sizeof(msg), &msgsz, &gerr) !=
     49 	    G_IO_STATUS_NORMAL) {
     50 		if (gerr) {
     51 			fprintf(stderr, "webext: error reading socket: %s\n",
     52 			        gerr->message);
     53 			g_error_free(gerr);
     54 		}
     55 		return TRUE;
     56 	}
     57 
     58 	if (msgsz < 2) {
     59 		fprintf(stderr, "webext: readsock: message too short: %d\n",
     60 		        msgsz);
     61 		return TRUE;
     62 	}
     63 
     64 	if (!(page = webkit_web_extension_get_page(webext, msg[0])))
     65 		return TRUE;
     66 
     67 	jsc = webkit_frame_get_js_context(webkit_web_page_get_main_frame(page));
     68 
     69 	switch (msg[1]) {
     70 	case 'h':
     71 		if (msgsz != 3)
     72 			return TRUE;
     73 		snprintf(js, sizeof(js),
     74 		         "window.scrollBy(window.innerWidth/100*%d,0);",
     75 		         msg[2]);
     76 		jsc_context_evaluate(jsc, js, -1);
     77 		break;
     78 	case 'v':
     79 		if (msgsz != 3)
     80 			return TRUE;
     81 		snprintf(js, sizeof(js),
     82 		         "window.scrollBy(0,window.innerHeight/100*%d);",
     83 		         msg[2]);
     84 		jsc_context_evaluate(jsc, js, -1);
     85 		break;
     86 	}
     87 
     88 	return TRUE;
     89 }
     90 
     91 G_MODULE_EXPORT void
     92 webkit_web_extension_initialize_with_user_data(WebKitWebExtension *e,
     93                                                const GVariant *gv)
     94 {
     95 	GIOChannel *gchansock;
     96 
     97 	webext = e;
     98 
     99 	g_variant_get(gv, "i", &sock);
    100 
    101 	gchansock = g_io_channel_unix_new(sock);
    102 	g_io_channel_set_encoding(gchansock, NULL, NULL);
    103 	g_io_channel_set_flags(gchansock, g_io_channel_get_flags(gchansock)
    104 	                       | G_IO_FLAG_NONBLOCK, NULL);
    105 	g_io_channel_set_close_on_unref(gchansock, TRUE);
    106 	g_io_add_watch(gchansock, G_IO_IN, readsock, NULL);
    107 }