29
29
30
30
#include < ev.h>
31
31
#include < stdlib.h>
32
+ #include < webkit/webkit.h>
32
33
33
- namespace nwebkit {
34
34
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())
36
40
41
+ namespace nwebkit {
42
+
43
+ using namespace v8 ;
44
+ static WebKitWebView* _webview;
37
45
struct econtext {
38
46
GPollFD *pfd;
39
47
ev_io *iow;
@@ -104,6 +112,9 @@ static void prepare_cb (EV_P_ ev_prepare *w, int revents) {
104
112
105
113
if (timeout >= 0 )
106
114
{
115
+ // if (timeout == 0)
116
+ // g_main_context_dispatch (ctx->gc);
117
+
107
118
ev_timer_set (&ctx->tw , timeout * 1e-3 , 0 .);
108
119
ev_timer_start (EV_A_ &ctx->tw );
109
120
}
@@ -142,9 +153,6 @@ static void check_cb (EV_P_ ev_check *w, int revents) {
142
153
}
143
154
144
155
static struct econtext default_context;
145
- // extern "C" {
146
- void nwebkit_view_init (const char *, int , int );
147
- // }
148
156
149
157
// Extracts a C string from a V8 Utf8Value.
150
158
const char * ToCString (const v8::String::Utf8Value& value) {
@@ -159,29 +167,110 @@ static Handle<Value> GtkInit (const Arguments &args) {
159
167
160
168
HandleScope scope;
161
169
v8::Local<v8::Context> entered, current;
162
- Local<Object> thisObj = args.This ();
163
170
Local<Object> options = Local<Object>::Cast (args[1 ]);
164
171
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);
166
174
const char * cstr = ToCString (str);
175
+ if (url->IsUndefined ()) {
176
+ cstr = " " ;
177
+ }
167
178
int width = options->Get (v8::String::New (" width" ))->Int32Value ();
168
179
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 ();
171
208
}
172
209
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
+
173
253
void init (Handle<Object> target) {
174
254
HandleScope scope;
175
255
176
256
if (!g_thread_supported ())
177
257
g_thread_init (NULL );
178
258
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);
181
264
265
+ ContextWrap::Initialize (target);
182
266
GMainContext *gc = g_main_context_default ();
183
267
struct econtext *ctx = &default_context;
184
268
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);
185
274
ctx->gc = g_main_context_ref (gc);
186
275
ctx->nfd = 0 ;
187
276
ctx->afd = 0 ;
0 commit comments