diff --git a/OpenRA.Launcher.Gtk/bridge.js b/OpenRA.Launcher.Gtk/bridge.js deleted file mode 100644 index e9fbc981d6..0000000000 --- a/OpenRA.Launcher.Gtk/bridge.js +++ /dev/null @@ -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)); -}; \ No newline at end of file diff --git a/OpenRA.Launcher.Gtk/main.c b/OpenRA.Launcher.Gtk/main.c index 19760820c1..2231ebc26f 100644 --- a/OpenRA.Launcher.Gtk/main.c +++ b/OpenRA.Launcher.Gtk/main.c @@ -10,85 +10,82 @@ #include #include #include +#include #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; - char buffer[1024]; - size_t buffer_len; - - sprintf(buffer, "readlink -f %s", filepath); - - output = popen(buffer, "r"); - - if (!output) + JSValueRef return_value = JSValueMakeNull(ctx); + if (argc < 1) { - g_warning("Could not find absolute path for %s", filepath); - return FALSE; + *exception = JSValueMakeString(ctx, JS_STR("Not enough args")); + 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) + if (JSValueIsString(ctx, argv[0])) { - g_warning("Could not convert %s to URI", buffer); - return FALSE; + char buffer[1024]; + JSStringRef s = JSValueToStringCopy(ctx, argv[0], NULL); + JSStringGetUTF8CString(s, buffer, 1024); + g_message("JS Log: %s", buffer); + return return_value; + } + else + { + *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; - - f = fopen("bridge.js", "r"); - - if (!f) - { - g_critical("Could not open bridge.js"); - return FALSE; - } - - 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; + return JSValueMakeNumber(ctx, 1); } +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; + + js_ctx = (JSGlobalContextRef)context; + + 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); diff --git a/OpenRA.Launcher.Gtk/server.c b/OpenRA.Launcher.Gtk/server.c index cc2910246f..92c3663ccf 100644 --- a/OpenRA.Launcher.Gtk/server.c +++ b/OpenRA.Launcher.Gtk/server.c @@ -6,12 +6,13 @@ * see LICENSE. */ #include -#include #include #include -#include +#include #include +#include #include + #define MHD_PLATFORM_H #include @@ -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,19 +58,21 @@ int access_handler_callback(void * userdata, void ** userpointer) { struct MHD_Response * response; - int ret; + int ret = MHD_NO; char * text = "1"; g_message(url); + + if ((ret = try_file_response(url, connection))) + return ret; - MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, - &http_get_args, NULL); - + text = "Not foundFile not found"; + 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; }