1 /*
2  * Copyright © 2016 Aidan Holm <aidanholm@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18 
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <glib.h>
22 
23 #include "common/util.h"
24 #include "common/luah.h"
25 #include "common/luautil.h"
26 #include "common/luauniq.h"
27 #include "extension/ipc.h"
28 #include "common/luaobject.h"
29 #include "extension/extension.h"
30 
31 #include "extension/clib/luakit.h"
32 #include "extension/clib/dom_document.h"
33 #include "extension/clib/dom_element.h"
34 #include "extension/clib/page.h"
35 #include "extension/clib/soup.h"
36 #include "extension/clib/msg.h"
37 #include "common/clib/ipc.h"
38 #include "common/clib/timer.h"
39 #include "common/clib/regex.h"
40 #include "common/clib/utf8.h"
41 
42 #include "extension/scroll.h"
43 #include "extension/luajs.h"
44 #include "extension/script_world.h"
45 
46 /* This is the global definition of common; it's also visible
47 everwhere common/common.h is included, which probably is about
48 everywhere in extensions; note that this common is separate
49 from the common visible on the UI side. */
50 common_t common;
51 
52 /* Similarly, this is the global definition of extension */
53 extension_t extension;
54 
55 
56 static void
web_lua_init(const char * package_path,const char * package_cpath)57 web_lua_init(const char *package_path, const char *package_cpath)
58 {
59     debug("Lua initializing...");
60 
61     lua_State *L = common.L;
62 
63     /* Set panic fuction */
64     lua_atpanic(L, luaH_panic);
65 
66     luaL_openlibs(L);
67     luaH_fixups(L);
68     luaH_object_setup(L);
69     luaH_uniq_setup(L, NULL, "v");
70 
71     lua_getglobal(L, "package");
72     lua_pushstring(L, package_path);
73     lua_setfield(L, -2, "path");
74     lua_pushstring(L, package_cpath);
75     lua_setfield(L, -2, "cpath");
76     lua_pop(L, 1);
77 
78     luakit_lib_setup(L);
79     soup_lib_setup(L);
80     ipc_channel_class_setup(L);
81     timer_class_setup(L);
82     regex_class_setup(L);
83     utf8_lib_setup(L);
84     dom_document_class_setup(L);
85     dom_element_class_setup(L);
86     page_class_setup(L);
87     msg_lib_setup(L);
88 
89     debug("Lua initialized");
90 }
91 
92 G_MODULE_EXPORT void
webkit_web_extension_initialize_with_user_data(WebKitWebExtension * ext,GVariant * payload)93 webkit_web_extension_initialize_with_user_data(WebKitWebExtension *ext, GVariant *payload)
94 {
95     gchar *socket_path, *package_path, *package_cpath;
96     g_variant_get(payload, "(sss)", &socket_path, &package_path, &package_cpath);
97 
98     common.L = luaL_newstate();
99     common.L = common.L;
100     extension.ext = ext;
101     extension.ipc = ipc_endpoint_new(g_strdup_printf("Web[%d]", getpid()));
102 
103     if (web_extension_connect(socket_path)) {
104         debug("connecting to UI thread failed");
105         exit(EXIT_FAILURE);
106     }
107 
108     web_lua_init(package_path, package_cpath);
109     web_scroll_init();
110     web_luajs_init();
111     web_script_world_init();
112 
113     debug("PID %d", getpid());
114     debug("ready for messages");
115 
116     ipc_header_t header = { .type = IPC_TYPE_extension_init, .length = 0 };
117     ipc_send(extension.ipc, &header, NULL);
118 }
119 
120 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80
121