Bind to JavaScriptCore. Serve files through embedded web server.

This commit is contained in:
Matthew Bowra-Dean
2010-11-25 00:11:29 +13:00
committed by Chris Forbes
parent a561dd376e
commit 69ee1399b9
3 changed files with 87 additions and 104 deletions

View File

@@ -1,27 +0,0 @@
window.external=new Object();
window.external.do_ajax=function(uri)
{
request = new XMLHttpRequest();
request.open("GET", uri, false);
try
{
request.send(null);
}
catch(err)
{
}
return request.responseText;
};
window.external.log=function(msg)
{
window.external.do_ajax("http://localhost:48764/log?msg=" + escape(msg));
};
window.external.launchMod=function(mod)
{
window.external.do_ajax("http://localhost:48764/launch?mod=" + mod);
};
window.external.existsInMod=function(file, mod)
{
return window.external.do_ajax("http://localhost:48764/fileExists?mod=" + mod + "&file=" + escape(file));
};

View File

@@ -10,85 +10,82 @@
#include <string.h>
#include <gtk/gtk.h>
#include <webkit/webkit.h>
#include <JavaScriptCore/JavaScript.h>
#include "server.h"
#define JS_STR(str) JSStringCreateWithUTF8CString(str)
#define JS_FUNC(ctx, callback) JSObjectMakeFunctionWithCallback(ctx, NULL, \
callback)
GtkWindow * window;
WebKitWebView * browser;
gboolean window_delete(GtkWidget * widget, GdkEvent * event, gpointer user_data)
gboolean window_delete(GtkWidget * widget, GdkEvent * event,
gpointer user_data)
{
server_teardown();
gtk_main_quit();
return FALSE;
}
int get_file_uri(char const * filepath, char ** uri)
JSValueRef js_log(JSContextRef ctx, JSObjectRef func, JSObjectRef this,
size_t argc, const JSValueRef argv[],
JSValueRef * exception)
{
FILE * output;
JSValueRef return_value = JSValueMakeNull(ctx);
if (argc < 1)
{
*exception = JSValueMakeString(ctx, JS_STR("Not enough args"));
return return_value;
}
if (JSValueIsString(ctx, argv[0]))
{
char buffer[1024];
size_t buffer_len;
sprintf(buffer, "readlink -f %s", filepath);
output = popen(buffer, "r");
if (!output)
{
g_warning("Could not find absolute path for %s", filepath);
return FALSE;
JSStringRef s = JSValueToStringCopy(ctx, argv[0], NULL);
JSStringGetUTF8CString(s, buffer, 1024);
g_message("JS Log: %s", buffer);
return return_value;
}
fgets(buffer, sizeof(buffer), output);
pclose(output);
buffer_len = strlen(buffer);
buffer[buffer_len - 1] = '\0';
*uri = g_filename_to_uri(buffer, NULL, NULL);
if (!*uri)
else
{
g_warning("Could not convert %s to URI", buffer);
return FALSE;
*exception = JSValueMakeString(ctx, JS_STR("Tried to log something other than a string"));
return return_value;
}
return TRUE;
}
int get_bridge_script(char ** script)
JSValueRef js_exists_in_mod(JSContextRef ctx, JSObjectRef func,
JSObjectRef this, size_t argc,
const JSValueRef argv[], JSValueRef * exception)
{
FILE * f;
long fileSize;
char * buffer;
size_t result;
return JSValueMakeNumber(ctx, 1);
}
f = fopen("bridge.js", "r");
void bind_js_bridge(WebKitWebView * view, WebKitWebFrame * frame,
gpointer context, gpointer window_object,
gpointer user_data)
{
JSGlobalContextRef js_ctx;
JSObjectRef window_obj, external_obj,
log_function, exists_in_mod_function;
if (!f)
{
g_critical("Could not open bridge.js");
return FALSE;
}
js_ctx = (JSGlobalContextRef)context;
fseek(f, 0, SEEK_END);
fileSize = ftell(f);
rewind(f);
buffer = (char *) malloc(sizeof(char) * fileSize);
result = fread(buffer, 1, fileSize, f);
fclose(f);
*script = buffer;
return TRUE;
external_obj = JSObjectMake(js_ctx, NULL, NULL);
log_function = JS_FUNC(js_ctx, js_log);
exists_in_mod_function = JS_FUNC(js_ctx, js_exists_in_mod);
window_obj = (JSObjectRef)window_object;
JSObjectSetProperty(js_ctx, window_obj, JS_STR("external"),
external_obj, 0, NULL);
JSObjectSetProperty(js_ctx, external_obj, JS_STR("log"),
log_function, 0, NULL);
JSObjectSetProperty(js_ctx, external_obj, JS_STR("existsInMod"),
exists_in_mod_function, 0, NULL);
}
int main(int argc, char ** argv)
{
char * uri, * script;
server_init(48764);
gtk_init(&argc, &argv);
@@ -98,24 +95,13 @@ int main(int argc, char ** argv)
gtk_window_set_default_size(window, 800, 600);
browser = WEBKIT_WEB_VIEW(webkit_web_view_new());
g_signal_connect(browser, "window-object-cleared",
G_CALLBACK(bind_js_bridge), 0);
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(browser));
if (!get_bridge_script(&script))
return 1;
webkit_web_view_execute_script(browser, script);
free(script);
if (!get_file_uri("../mods/cnc/mod.html", &uri))
{
return 1;
}
webkit_web_view_load_uri(browser, uri);
free(uri);
webkit_web_view_load_uri(browser,
"http://localhost:48764/mods/cnc/mod.html");
gtk_widget_show_all(GTK_WIDGET(window));
g_signal_connect(window, "delete-event", G_CALLBACK(window_delete), 0);

View File

@@ -6,12 +6,13 @@
* see LICENSE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdint.h>
#include <fcntl.h>
#include <string.h>
#define MHD_PLATFORM_H
#include <microhttpd.h>
@@ -26,6 +27,27 @@ int http_get_args(void * cls, enum MHD_ValueKind kind,
return MHD_YES;
}
int try_file_response(const char * url, struct MHD_Connection * connection)
{
int fd, ret;
struct MHD_Response * response;
struct stat sbuf;
g_message("Opening %s", url + 1);
if ((-1 == (fd = open(url + 1, O_RDONLY))) ||
(0 != fstat(fd, &sbuf)))
{
return MHD_NO;
}
response = MHD_create_response_from_fd(sbuf.st_size, fd);
MHD_add_response_header(response, "Content-Type", "text/html");
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
return ret;
}
int access_handler_callback(void * userdata,
struct MHD_Connection * connection,
const char * url,
@@ -36,17 +58,19 @@ int access_handler_callback(void * userdata,
void ** userpointer)
{
struct MHD_Response * response;
int ret;
int ret = MHD_NO;
char * text = "1";
g_message(url);
MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND,
&http_get_args, NULL);
if ((ret = try_file_response(url, connection)))
return ret;
text = "<html><head><title>Not found</title></head><body>File not found</body></html>";
response = MHD_create_response_from_data(strlen(text), (void *)text,
MHD_NO, MHD_NO);
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
ret = MHD_queue_response(connection, MHD_HTTP_NOT_FOUND, response);
MHD_destroy_response(response);
return ret;