2929
3030#include < ev.h>
3131#include < stdlib.h>
32+ #include < webkit/webkit.h>
3233
33- namespace nwebkit {
3434
35- using namespace v8 ;
35+ #include " context_wrap.h"
36+
37+ #define NWEBKIT_DEF_METHOD (obj,name,func ) \
38+ obj->Set (v8::String::NewSymbol(name), \
39+ v8::FunctionTemplate::New(func)->GetFunction())
3640
41+ namespace nwebkit {
42+
43+ using namespace v8 ;
44+ static WebKitWebView* _webview;
3745struct econtext {
3846 GPollFD *pfd;
3947 ev_io *iow;
@@ -104,6 +112,9 @@ static void prepare_cb (EV_P_ ev_prepare *w, int revents) {
104112
105113 if (timeout >= 0 )
106114 {
115+ // if (timeout == 0)
116+ // g_main_context_dispatch (ctx->gc);
117+
107118 ev_timer_set (&ctx->tw , timeout * 1e-3 , 0 .);
108119 ev_timer_start (EV_A_ &ctx->tw );
109120 }
@@ -142,9 +153,6 @@ static void check_cb (EV_P_ ev_check *w, int revents) {
142153}
143154
144155static struct econtext default_context;
145- // extern "C" {
146- void nwebkit_view_init (const char *, int , int );
147- // }
148156
149157// Extracts a C string from a V8 Utf8Value.
150158const char * ToCString (const v8::String::Utf8Value& value) {
@@ -159,29 +167,110 @@ static Handle<Value> GtkInit (const Arguments &args) {
159167
160168 HandleScope scope;
161169 v8::Local<v8::Context> entered, current;
162- Local<Object> thisObj = args.This ();
163170 Local<Object> options = Local<Object>::Cast (args[1 ]);
164171
165- v8::String::Utf8Value str (options->Get (v8::String::New (" url" )));
172+ Local<Value> url = options->Get (v8::String::New (" url" ));
173+ v8::String::Utf8Value str (url);
166174 const char * cstr = ToCString (str);
175+ if (url->IsUndefined ()) {
176+ cstr = " " ;
177+ }
167178 int width = options->Get (v8::String::New (" width" ))->Int32Value ();
168179 int height = options->Get (v8::String::New (" height" ))->Int32Value ();
169- nwebkit_view_init (cstr, width, height);
170- return Undefined ();
180+ _webview = nwebkit_view_init (cstr, width, height);
181+
182+ WebKitWebSettings *settings = webkit_web_settings_new ();
183+ g_object_set (G_OBJECT (settings), " enable-file-access-from-file-uris" ,
184+ TRUE , NULL );
185+
186+ webkit_web_view_set_settings (WEBKIT_WEB_VIEW (_webview), settings);
187+
188+ return args.This ();
189+ }
190+
191+ static Handle<Value> Open (const Arguments &args) {
192+
193+ HandleScope scope;
194+
195+ Local<String> arg0 = args[0 ]->ToString ();
196+
197+ v8::String::Utf8Value str (arg0);
198+ const char * uri = ToCString (str);
199+ if (arg0->IsUndefined ()) {
200+ uri = " " ;
201+ }
202+ gchar *url = filename_to_url (uri);
203+ webkit_web_view_load_uri (_webview, url ? url : uri);
204+
205+ g_free (url);
206+
207+ return args.This ();
171208}
172209
210+ static Handle<Value> LoadString (const Arguments &args) {
211+
212+ HandleScope scope;
213+
214+ Local<String> arg0 = args[0 ]->ToString ();
215+
216+ String::Utf8Value str (arg0);
217+ const char * content = ToCString (str);
218+ const char * mime_type = NULL ;
219+ const char * base_uri = " " ;
220+ String::Utf8Value arg1 (args[1 ]->ToString ());
221+ String::Utf8Value arg2 (args[2 ]->ToString ());
222+
223+ if (args[1 ]->IsString ())
224+ mime_type = *arg1;
225+ if (args[2 ]->IsString ())
226+ base_uri = *arg2;
227+
228+ webkit_web_view_load_string (_webview, content, mime_type, NULL , base_uri);
229+
230+ return args.This ();
231+ }
232+
233+ static Handle<Value> Reload (const Arguments &args) {
234+ webkit_web_view_reload (_webview);
235+ return args.This ();
236+ }
237+
238+ static Handle<Value> SetViewMode (const Arguments &args) {
239+
240+ int mode;
241+ if (args[0 ]->IsNumber ()) {
242+ mode = args[1 ]->Int32Value ();
243+ } else {
244+ mode = WEBKIT_WEB_VIEW_VIEW_MODE_WINDOWED ;
245+ }
246+
247+ webkit_web_view_set_view_mode (_webview, (WebKitWebViewViewMode)mode);
248+
249+ return args.This ();
250+ }
251+
252+
173253void init (Handle<Object> target) {
174254 HandleScope scope;
175255
176256 if (!g_thread_supported ())
177257 g_thread_init (NULL );
178258
179- target->Set (v8::String::NewSymbol (" _init" ),
180- v8::FunctionTemplate::New (GtkInit)->GetFunction ());
259+ NWEBKIT_DEF_METHOD (target, " _init" , GtkInit);
260+ NWEBKIT_DEF_METHOD (target, " open" , Open);
261+ NWEBKIT_DEF_METHOD (target, " loadString" , LoadString);
262+ NWEBKIT_DEF_METHOD (target, " reload" , Reload);
263+ NWEBKIT_DEF_METHOD (target, " setViewMode" , SetViewMode);
181264
265+ ContextWrap::Initialize (target);
182266 GMainContext *gc = g_main_context_default ();
183267 struct econtext *ctx = &default_context;
184268
269+ // ev thread need to be the owner of the context so it can be wake
270+ // up properly by g_source_attach(idle source) from the i/o thread
271+ // launched by webkit/libsoup
272+
273+ g_main_context_acquire (gc);
185274 ctx->gc = g_main_context_ref (gc);
186275 ctx->nfd = 0 ;
187276 ctx->afd = 0 ;
0 commit comments