This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dy-launcher.c
621 lines (507 loc) · 18.8 KB
/
dy-launcher.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*
* dy-launcher.c
* Copyright (C) 2017 Adrian Perez <[email protected]>
*
* Distributed under terms of the MIT license.
*/
#include "dy-launcher.h"
#include "dy-request-handler.h"
#include "dy-webkit-utils.h"
#include "dy-utils.h"
#if DY_USE_WEBKITGTK
# include "dy-gtk-utils.h"
#endif
struct _DyLauncher {
DyLauncherBase parent;
WebKitWebContext *web_context;
WebKitWebView *web_view;
char *home_uri;
gboolean automation_mode;
GHashTable *request_handlers; /* (string, DyRequestHandler) */
};
G_DEFINE_TYPE (DyLauncher, dy_launcher, DY_LAUNCHER_BASE_TYPE)
enum {
CREATE_WEB_VIEW,
LAST_SIGNAL,
};
static int s_signals[LAST_SIGNAL] = { 0, };
enum {
PROP_0,
PROP_WEB_CONTEXT,
PROP_WEB_VIEW,
PROP_HOME_URI,
N_PROPERTIES
};
static GParamSpec *s_properties[N_PROPERTIES] = { NULL, };
static void
on_action_quit (G_GNUC_UNUSED GAction *action,
G_GNUC_UNUSED GVariant *param,
DyLauncher *launcher)
{
g_application_quit (G_APPLICATION (launcher));
}
static void
on_action_prev (G_GNUC_UNUSED GAction *action,
G_GNUC_UNUSED GVariant *param,
DyLauncher *launcher)
{
webkit_web_view_go_back (dy_launcher_get_web_view (launcher));
}
static void
on_action_next (G_GNUC_UNUSED GAction *action,
G_GNUC_UNUSED GVariant *param,
DyLauncher *launcher)
{
webkit_web_view_go_forward (dy_launcher_get_web_view (launcher));
}
static void
on_action_reload (G_GNUC_UNUSED GAction *action,
G_GNUC_UNUSED GVariant *param,
DyLauncher *launcher)
{
webkit_web_view_reload (dy_launcher_get_web_view (launcher));
}
static void
on_action_open (G_GNUC_UNUSED GAction *action,
GVariant *param,
DyLauncher *launcher)
{
g_return_if_fail (g_variant_is_of_type (param, G_VARIANT_TYPE_STRING));
dy_launcher_set_home_uri (launcher, g_variant_get_string (param, NULL));
}
static void
dy_launcher_add_action (DyLauncher *launcher,
const char *name,
void (*callback) (GAction*, GVariant*, DyLauncher*),
const GVariantType *param_type)
{
g_assert (DY_IS_LAUNCHER (launcher));
g_assert_nonnull (name);
g_assert_nonnull (callback);
GSimpleAction *action = g_simple_action_new (name, param_type);
g_signal_connect (action, "activate", G_CALLBACK (callback), launcher);
g_action_map_add_action (G_ACTION_MAP (launcher), G_ACTION (action));
}
typedef struct _RequestHandlerMapEntry RequestHandlerMapEntry;
static void request_handler_map_entry_register (const char*, RequestHandlerMapEntry*, WebKitWebContext*);
static WebKitWebView*
on_automation_session_create_web_view (WebKitAutomationSession *session,
DyLauncher *launcher)
{
return WEBKIT_WEB_VIEW (launcher->web_view);
}
static void
on_web_context_automation_started (WebKitWebContext *context,
WebKitAutomationSession *session,
DyLauncher *launcher)
{
g_message ("Automation session started.");
g_signal_connect (session, "create-web-view",
G_CALLBACK (on_automation_session_create_web_view),
launcher);
}
static void
dy_launcher_create_web_context (DyLauncher *launcher)
{
if (launcher->automation_mode) {
launcher->web_context = webkit_web_context_new_ephemeral ();
webkit_web_context_set_automation_allowed (launcher->web_context, TRUE);
g_signal_connect (launcher->web_context, "automation-started",
G_CALLBACK (on_web_context_automation_started),
launcher);
} else {
g_autofree char *data_dir = g_build_filename (g_get_user_data_dir (),
g_get_prgname (),
NULL);
g_autofree char *cache_dir = g_build_filename (g_get_user_cache_dir (),
g_get_prgname (),
NULL);
g_autoptr(WebKitWebsiteDataManager) manager =
webkit_website_data_manager_new ("base-data-directory", data_dir,
"base-cache-directory", cache_dir,
NULL);
launcher->web_context = webkit_web_context_new_with_website_data_manager (manager);
}
g_debug ("Created Web context @ %p (ephemeral: %s, automation: %s)",
launcher->web_context,
webkit_web_context_is_ephemeral (launcher->web_context) ? "yes" : "no",
webkit_web_context_is_automation_allowed (launcher->web_context) ? "yes" : "no");
/*
* Request handlers can be registered with DyLauncher before the web
* context is created: register them now that it has been created.
*/
if (launcher->request_handlers) {
g_hash_table_foreach (launcher->request_handlers,
(GHFunc) request_handler_map_entry_register,
launcher->web_context);
}
}
static void
dy_launcher_open (GApplication *application,
GFile **files,
int n_files,
const char *hint)
{
g_assert (n_files);
if (n_files > 1)
g_warning ("Requested opening %i files, opening only the first one", n_files);
g_autofree char *home_uri = g_file_get_uri (files[0]);
dy_launcher_set_home_uri (DY_LAUNCHER (application), home_uri);
}
static void
on_web_view_close (WebKitWebView *web_view,
DyLauncher *launcher)
{
g_application_quit (G_APPLICATION (launcher));
}
static void
dy_launcher_startup (GApplication *application)
{
G_APPLICATION_CLASS (dy_launcher_parent_class)->startup (application);
DyLauncher *launcher = DY_LAUNCHER (application);
dy_launcher_create_web_context (launcher);
g_signal_emit (launcher,
s_signals[CREATE_WEB_VIEW],
0,
&launcher->web_view);
/*
* Create the web view ourselves if the signal handler did not.
*/
if (!launcher->web_view) {
g_autoptr(WebKitSettings) settings =
webkit_settings_new_with_settings ("enable-developer-extras", TRUE, NULL);
launcher->web_view = WEBKIT_WEB_VIEW (g_object_new (WEBKIT_TYPE_WEB_VIEW,
"web-context", dy_launcher_get_web_context (launcher),
"settings", settings,
"is-controlled-by-automation", launcher->automation_mode,
NULL));
}
if (launcher->automation_mode) {
g_signal_connect (launcher->web_view, "close",
G_CALLBACK (on_web_view_close),
launcher);
}
/*
* The web context being used must be the same created by DyLauncher.
*/
g_assert (webkit_web_view_get_context (launcher->web_view) == dy_launcher_get_web_context (launcher));
/*
* Make sure we keep the floating reference to the web view alive.
*/
g_object_ref_sink (launcher->web_view);
/*
* When building with GTK+ support, the GtkApplicationWindow will
* automatically "hold" the GApplication instance and release it when
* the window is closed. We have to manually call g_application_hold()
* ourselves when building against the WPE port.
*/
#if DY_USE_WEBKITGTK
GtkWidget *window = dy_gtk_create_window (launcher);
g_object_ref_sink (window); // Keep the window object alive.
#else
g_application_hold (application);
#endif
webkit_web_view_load_uri (launcher->web_view, launcher->home_uri);
}
static void
dy_launcher_activate (GApplication *application)
{
/*
* Give user code the chance of creating a customized web view.
*/
DyLauncher *launcher = DY_LAUNCHER (application);
#if DY_USE_WEBKITGTK
dy_gtk_present_window (launcher);
#endif
}
static void
dy_launcher_get_property (GObject *object,
unsigned prop_id,
GValue *value,
GParamSpec *pspec)
{
DyLauncher *launcher = DY_LAUNCHER (object);
switch (prop_id) {
case PROP_WEB_CONTEXT:
g_value_set_object (value, dy_launcher_get_web_context (launcher));
break;
case PROP_WEB_VIEW:
g_value_set_object (value, dy_launcher_get_web_view (launcher));
break;
case PROP_HOME_URI:
g_value_set_string (value, dy_launcher_get_home_uri (launcher));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
dy_launcher_set_property (GObject *object,
unsigned prop_id,
const GValue *value,
GParamSpec *pspec)
{
DyLauncher *launcher = DY_LAUNCHER (object);
switch (prop_id) {
case PROP_HOME_URI:
dy_launcher_set_home_uri (launcher, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
dy_launcher_dispose (GObject *object)
{
DyLauncher *launcher = DY_LAUNCHER (object);
g_clear_object (&launcher->web_view);
g_clear_object (&launcher->web_context);
g_clear_pointer (&launcher->request_handlers, g_hash_table_unref);
G_OBJECT_CLASS (dy_launcher_parent_class)->dispose (object);
}
#if DY_DBUS_SYSTEM_BUS
static void
on_system_bus_acquired (GDBusConnection *connection,
const char *name,
void *userdata)
{
g_autofree char* object_path =
dy_appid_to_dbus_object_path (g_application_get_application_id (G_APPLICATION (userdata)));
g_autoptr(GError) error = NULL;
if (!g_dbus_connection_export_action_group (connection,
object_path,
G_ACTION_GROUP (userdata),
&error))
g_warning ("Cannot expose remote control interface to system bus: %s",
error->message);
}
static void
on_system_bus_name_acquired (G_GNUC_UNUSED GDBusConnection *connection,
const char *name,
G_GNUC_UNUSED void *userdata)
{
g_message ("Acquired D-Bus well-known name %s", name);
}
static void
on_system_bus_name_lost (GDBusConnection *connection,
const char *name,
G_GNUC_UNUSED void *userdata)
{
if (connection) {
g_message ("Lost D-Bus well-known name %s", name);
} else {
g_message ("Lost D-Bus connection to system bus");
}
}
#endif // DY_DBUS_SYSTEM_BUS
#ifndef DY_DEFAULT_APPID
# if DY_USE_WEBKITGTK
# define DY_DEFAULT_APPID "com.igalia.DinghyGtk"
# else
# define DY_DEFAULT_APPID "com.igalia.Dinghy"
# endif
#endif
static void
dy_launcher_constructed (GObject *object)
{
G_OBJECT_CLASS (dy_launcher_parent_class)->constructed (object);
DyLauncher *launcher = DY_LAUNCHER (object);
dy_launcher_add_action (launcher, "quit", on_action_quit, NULL);
dy_launcher_add_action (launcher, "previous", on_action_prev, NULL);
dy_launcher_add_action (launcher, "next", on_action_next, NULL);
dy_launcher_add_action (launcher, "reload", on_action_reload, NULL);
dy_launcher_add_action (launcher, "open", on_action_open, G_VARIANT_TYPE_STRING);
#if DY_DBUS_SYSTEM_BUS
g_bus_own_name (G_BUS_TYPE_SYSTEM,
DY_DEFAULT_APPID,
G_BUS_NAME_OWNER_FLAGS_NONE,
on_system_bus_acquired,
on_system_bus_name_acquired,
on_system_bus_name_lost,
launcher,
NULL);
#endif
}
#ifndef DY_DEFAULT_HOME_URI
# define DY_DEFAULT_HOME_URI "about:blank"
#endif
static void
dy_launcher_class_init (DyLauncherClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = dy_launcher_dispose;
object_class->constructed = dy_launcher_constructed;
object_class->get_property = dy_launcher_get_property;
object_class->set_property = dy_launcher_set_property;
GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
application_class->open = dy_launcher_open;
application_class->startup = dy_launcher_startup;
application_class->activate = dy_launcher_activate;
s_properties[PROP_WEB_CONTEXT] =
g_param_spec_object ("web-context",
"Web Context",
"The WebKitWebContext for the launcher",
WEBKIT_TYPE_WEB_CONTEXT,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
s_properties[PROP_WEB_VIEW] =
g_param_spec_object ("web-view",
"Web View",
"The main WebKitWebView for the launcher",
WEBKIT_TYPE_WEB_VIEW,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
s_properties[PROP_HOME_URI] =
g_param_spec_string ("home-uri",
"Home URI",
"URI loaded by the main WebKitWebView for the launcher at launch",
DY_DEFAULT_HOME_URI,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class,
N_PROPERTIES,
s_properties);
s_signals[CREATE_WEB_VIEW] =
g_signal_new ("create-web-view",
DY_TYPE_LAUNCHER,
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
NULL,
WEBKIT_TYPE_WEB_VIEW,
0);
}
static void
dy_launcher_init (G_GNUC_UNUSED DyLauncher *launcher)
{
}
static void*
dy_launcher_create_instance (void* user_data)
{
/* Global singleton */
const GApplicationFlags app_flags =
#if GLIB_CHECK_VERSION(2, 48, 0)
G_APPLICATION_CAN_OVERRIDE_APP_ID |
#endif // GLIB_CHECK_VERSION
G_APPLICATION_HANDLES_OPEN ;
return g_object_new (DY_TYPE_LAUNCHER,
"application-id", DY_DEFAULT_APPID,
"flags", app_flags,
NULL);
}
DyLauncher*
dy_launcher_get_default (void)
{
static GOnce create_instance_once = G_ONCE_INIT;
g_once (&create_instance_once, dy_launcher_create_instance, NULL);
g_assert_nonnull (create_instance_once.retval);
return create_instance_once.retval;
}
WebKitWebView*
dy_launcher_get_web_view (DyLauncher *launcher)
{
g_return_val_if_fail (DY_IS_LAUNCHER (launcher), NULL);
return launcher->web_view;
}
WebKitWebContext*
dy_launcher_get_web_context (DyLauncher *launcher)
{
g_return_val_if_fail (DY_IS_LAUNCHER (launcher), NULL);
return launcher->web_context;
}
const char*
dy_launcher_get_home_uri (DyLauncher *launcher)
{
g_return_val_if_fail (DY_IS_LAUNCHER (launcher), NULL);
return launcher->home_uri;
}
void
dy_launcher_set_home_uri (DyLauncher *launcher,
const char *home_uri)
{
g_return_if_fail (DY_IS_LAUNCHER (launcher));
g_return_if_fail (home_uri != NULL);
if (g_strcmp0 (launcher->home_uri, home_uri) == 0)
return;
launcher->home_uri = g_strdup (home_uri);
g_object_notify_by_pspec (G_OBJECT (launcher),
s_properties[PROP_HOME_URI]);
if (launcher->web_view)
webkit_web_view_load_uri (launcher->web_view, launcher->home_uri);
}
struct _RequestHandlerMapEntry {
DyRequestHandler *handler;
gboolean registered;
};
static inline RequestHandlerMapEntry*
request_handler_map_entry_new (DyRequestHandler *handler)
{
g_assert_nonnull (handler);
RequestHandlerMapEntry *entry = g_slice_new (RequestHandlerMapEntry);
entry->handler = g_object_ref_sink (handler);
entry->registered = FALSE;
return entry;
}
static inline void
request_handler_map_entry_free (void *pointer)
{
if (pointer) {
RequestHandlerMapEntry *entry = pointer;
g_clear_object (&entry->handler);
g_slice_free (RequestHandlerMapEntry, entry);
}
}
static void
handle_uri_scheme_request (WebKitURISchemeRequest *request,
void *user_data)
{
RequestHandlerMapEntry *entry = user_data;
g_assert_nonnull (entry->handler);
dy_request_handler_run (entry->handler, request);
}
static void
request_handler_map_entry_register (const char *scheme,
RequestHandlerMapEntry *entry,
WebKitWebContext *context)
{
if (context && !entry->registered) {
webkit_web_context_register_uri_scheme (context,
scheme,
handle_uri_scheme_request,
entry,
NULL);
}
}
void
dy_launcher_set_request_handler (DyLauncher *launcher,
const char *scheme,
DyRequestHandler *handler)
{
g_return_if_fail (DY_IS_LAUNCHER (launcher));
g_return_if_fail (scheme != NULL);
g_return_if_fail (DY_IS_REQUEST_HANDLER (handler));
if (!launcher->request_handlers) {
launcher->request_handlers =
g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
request_handler_map_entry_free);
}
RequestHandlerMapEntry *entry =
g_hash_table_lookup (launcher->request_handlers, scheme);
if (!entry) {
entry = request_handler_map_entry_new (handler);
g_hash_table_insert (launcher->request_handlers, g_strdup (scheme), entry);
} else if (entry->handler != handler) {
g_clear_object (&entry->handler);
entry->handler = g_object_ref_sink (handler);
}
request_handler_map_entry_register (scheme, entry, launcher->web_context);
}
void
dy_launcher_enable_automation (DyLauncher *launcher)
{
g_return_if_fail (DY_IS_LAUNCHER (launcher));
launcher->automation_mode = TRUE;
}