Skip to content

Commit

Permalink
Merge pull request #29 from Igalia/cog-websettings-cli
Browse files Browse the repository at this point in the history
Provide command line options for WebKitSettings properties
  • Loading branch information
philn committed Jun 11, 2018
2 parents 2a3562f + 72b2818 commit 1c63839
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 24 deletions.
22 changes: 2 additions & 20 deletions cog.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ static struct {
gboolean version;
gboolean print_appid;
gboolean doc_viewer;
gboolean dev_tools;
gboolean webgl;
gboolean log_console;
gdouble scale_factor;
GStrv dir_handlers;
GStrv arguments;
Expand All @@ -58,15 +55,6 @@ static GOptionEntry s_cli_options[] =
{ "print-appid", '\0', 0, G_OPTION_ARG_NONE, &s_options.print_appid,
"Print application ID and exit",
NULL },
{ "dev-tools", 'D', 0, G_OPTION_ARG_NONE, &s_options.dev_tools,
"Enable usage of the inspector and JavaScript console",
NULL },
{ "log-console", 'v', 0, G_OPTION_ARG_NONE, &s_options.log_console,
"Log JavaScript console messages to standard output",
NULL },
{ "webgl", '\0', 0, G_OPTION_ARG_NONE, &s_options.webgl,
"Allow web content to use the WebGL API",
NULL },
{ "scale", '\0', 0, G_OPTION_ARG_DOUBLE, &s_options.scale_factor,
"Zoom/Scaling factor (default: 1.0, no scaling)",
"FACTOR" },
Expand Down Expand Up @@ -284,13 +272,6 @@ on_create_web_view (CogLauncher *launcher,
WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
}

g_autoptr(WebKitSettings) settings =
webkit_settings_new_with_settings ("enable-developer-extras", s_options.dev_tools,
"enable-page-cache", !s_options.doc_viewer,
"enable-webgl", s_options.webgl,
"enable-write-console-messages-to-stdout", s_options.log_console,
NULL);

#if !COG_USE_WEBKITGTK
WebKitWebViewBackend *view_backend = NULL;

Expand Down Expand Up @@ -318,7 +299,7 @@ on_create_web_view (CogLauncher *launcher,
#endif

g_autoptr(WebKitWebView) web_view = g_object_new (WEBKIT_TYPE_WEB_VIEW,
"settings", settings,
"settings", cog_launcher_get_web_settings (launcher),
"web-context", web_context,
"zoom-level", s_options.scale_factor,
#if !COG_USE_WEBKITGTK
Expand Down Expand Up @@ -362,6 +343,7 @@ main (int argc, char *argv[])
{
g_autoptr(GApplication) app = G_APPLICATION (cog_launcher_get_default ());
g_application_add_main_option_entries (app, s_cli_options);
cog_launcher_add_web_settings_option_entries (COG_LAUNCHER (app));

#if !COG_USE_WEBKITGTK
g_signal_connect (app, "shutdown", G_CALLBACK (on_shutdown), NULL);
Expand Down
63 changes: 59 additions & 4 deletions core/cog-launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

struct _CogLauncher {
CogLauncherBase parent;
WebKitSettings *web_settings;
WebKitWebContext *web_context;
WebKitWebView *web_view;
char *home_uri;
Expand All @@ -36,6 +37,7 @@ static int s_signals[LAST_SIGNAL] = { 0, };

enum {
PROP_0,
PROP_WEB_SETTINGS,
PROP_WEB_CONTEXT,
PROP_WEB_VIEW,
PROP_HOME_URI,
Expand Down Expand Up @@ -167,17 +169,17 @@ cog_launcher_startup (GApplication *application)
* 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,
"settings", settings,
"settings", cog_launcher_get_web_settings (launcher),
"web-context", cog_launcher_get_web_context (launcher),
NULL));
}

/*
* The web context being used must be the same created by CogLauncher.
* The web context and settings being used by the web view must be
* the same that were pre-created by CogLauncher.
*/
g_assert (webkit_web_view_get_settings (launcher->web_view) == cog_launcher_get_web_settings (launcher));
g_assert (webkit_web_view_get_context (launcher->web_view) == cog_launcher_get_web_context (launcher));

/*
Expand Down Expand Up @@ -226,6 +228,9 @@ cog_launcher_get_property (GObject *object,
{
CogLauncher *launcher = COG_LAUNCHER (object);
switch (prop_id) {
case PROP_WEB_SETTINGS:
g_value_set_object (value, cog_launcher_get_web_settings (launcher));
break;
case PROP_WEB_CONTEXT:
g_value_set_object (value, cog_launcher_get_web_context (launcher));
break;
Expand Down Expand Up @@ -265,6 +270,7 @@ cog_launcher_dispose (GObject *object)

g_clear_object (&launcher->web_view);
g_clear_object (&launcher->web_context);
g_clear_object (&launcher->web_settings);

g_clear_pointer (&launcher->request_handlers, g_hash_table_unref);

Expand Down Expand Up @@ -327,6 +333,8 @@ cog_launcher_constructed (GObject *object)

CogLauncher *launcher = COG_LAUNCHER (object);

launcher->web_settings = g_object_ref_sink (webkit_settings_new ());

cog_launcher_add_action (launcher, "quit", on_action_quit, NULL);
cog_launcher_add_action (launcher, "previous", on_action_prev, NULL);
cog_launcher_add_action (launcher, "next", on_action_next, NULL);
Expand Down Expand Up @@ -360,6 +368,14 @@ cog_launcher_class_init (CogLauncherClass *klass)
application_class->startup = cog_launcher_startup;
application_class->activate = cog_launcher_activate;

s_properties[PROP_WEB_SETTINGS] =
g_param_spec_object ("web-settings",
"Web Settings",
"The WebKitSettings for the launcher",
WEBKIT_TYPE_SETTINGS,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);

s_properties[PROP_WEB_CONTEXT] =
g_param_spec_object ("web-context",
"Web Context",
Expand Down Expand Up @@ -448,6 +464,14 @@ cog_launcher_get_web_context (CogLauncher *launcher)
}


WebKitSettings*
cog_launcher_get_web_settings (CogLauncher *launcher)
{
g_return_val_if_fail (COG_IS_LAUNCHER (launcher), NULL);
return launcher->web_settings;
}


const char*
cog_launcher_get_home_uri (CogLauncher *launcher)
{
Expand Down Expand Up @@ -565,3 +589,34 @@ cog_launcher_set_request_handler (CogLauncher *launcher,

request_handler_map_entry_register (scheme, entry, launcher->web_context);
}


void
cog_launcher_add_web_settings_option_entries (CogLauncher *launcher)
{
g_return_if_fail (COG_IS_LAUNCHER (launcher));

g_autofree GOptionEntry *option_entries =
cog_option_entries_from_class (G_OBJECT_GET_CLASS (launcher->web_settings));
if (!option_entries) {
g_critical ("Could not deduce option entries for WebKitSettings."
" This should not happen, continuing but YMMV.");
return;
}

g_autoptr(GOptionGroup) option_group =
g_option_group_new ("websettings",
"WebKitSettings options can be used to configure features exposed to the loaded Web content.\n"
"\n"
" BOOL values are either 'true', '1', 'false', or '0'. Ommitting the value implies '1'.\n"
" INTEGER values can be decimal, octal (prefix '0'), or hexadecimal (prefix '0x').\n"
" UNSIGNED values behave like INTEGER, but negative values are not accepted.\n"
" FLOAT values may optionally use decimal separators and scientific notation.\n"
" STRING values may need quoting when passed from the shell.\n",
"Show WebKitSettings options",
launcher->web_settings,
NULL);
g_option_group_add_entries (option_group, option_entries);
g_application_add_option_group (G_APPLICATION (launcher),
g_steal_pointer (&option_group));
}
3 changes: 3 additions & 0 deletions core/cog-launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct _CogLauncherClass
CogLauncher *cog_launcher_get_default (void);
WebKitWebView *cog_launcher_get_web_view (CogLauncher *launcher);
WebKitWebContext *cog_launcher_get_web_context (CogLauncher *launcher);
WebKitSettings *cog_launcher_get_web_settings (CogLauncher *launcher);
const char *cog_launcher_get_home_uri (CogLauncher *launcher);
void cog_launcher_set_home_uri (CogLauncher *launcher,
const char *home_uri);
Expand All @@ -52,6 +53,8 @@ void cog_launcher_set_request_handler (CogLauncher *launcher,
const char *scheme,
CogRequestHandler *handler);

void cog_launcher_add_web_settings_option_entries (CogLauncher *launcher);

G_END_DECLS

#endif /* !COG_LAUNCHER_H */
Loading

0 comments on commit 1c63839

Please sign in to comment.