Skip to content

Commit

Permalink
core, launcher: Build memory limit support conditionally
Browse files Browse the repository at this point in the history
Check the WPE WebKit version at configuration time to determine whether
support for the memory pressure API is available, define the macro
COG_HAVE_MEM_PRESSURE accordingly, and use it to guard the relevant
code.

This avoids needing to bump the minimum WPE WebKit required version.
  • Loading branch information
aperezdc committed Nov 19, 2021
1 parent 8e6027f commit aee7e84
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ pkg_check_modules(GIO IMPORTED_TARGET REQUIRED ${REQUIRED_GIO_VERSION})
pkg_check_modules(SOUP IMPORTED_TARGET REQUIRED ${REQUIRED_SOUP_VERSION})
pkg_check_modules(WebKit IMPORTED_TARGET REQUIRED ${REQUIRED_WEBKIT_VERSION})

if (WebKit_VERSION VERSION_GREATER_EQUAL 2.34.0)
set(HAVE_WEBKIT_MEM_PRESSURE_API 1)
else()
set(HAVE_WEBKIT_MEM_PRESSURE_API 0)
endif()

include(CheckCCompilerFlag)
check_c_compiler_flag(-Wall HAS_WALL)

Expand Down
1 change: 1 addition & 0 deletions core/cog-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define COG_VERSION_EXTRA "@COG_VERSION_EXTRA@"
#cmakedefine COG_DEFAULT_APPID "@COG_DEFAULT_APPID@"
#cmakedefine COG_DEFAULT_HOME_URI "@COG_DEFAULT_HOME_URI@"
#define COG_HAVE_MEM_PRESSURE @HAVE_WEBKIT_MEM_PRESSURE_API@

/* FIXME: Perhaps make this a cmake define instead. */
#define COG_DEFAULT_APPNAME "Cog"
Expand Down
20 changes: 17 additions & 3 deletions core/cog-shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ typedef struct {
WebKitWebView *web_view;
WebKitWebsiteDataManager *web_data_manager;

#if COG_HAVE_MEM_PRESSURE
WebKitMemoryPressureSettings *web_mem_settings;
WebKitMemoryPressureSettings *net_mem_settings;
#endif /* COG_HAVE_MEM_PRESSURE */
} CogShellPrivate;


Expand All @@ -51,9 +53,11 @@ enum {
PROP_CONFIG_FILE,
PROP_DEVICE_SCALE_FACTOR,
PROP_AUTOMATED,
PROP_WEB_DATA_MANAGER,
#if COG_HAVE_MEM_PRESSURE
PROP_WEB_MEMORY_SETTINGS,
PROP_NETWORK_MEMORY_SETTINGS,
PROP_WEB_DATA_MANAGER,
#endif /* COG_HAVE_MEM_PRESSURE */
N_PROPERTIES,
};

Expand Down Expand Up @@ -202,12 +206,14 @@ cog_shell_get_property (GObject *object,
case PROP_WEB_VIEW:
g_value_set_object (value, cog_shell_get_web_view (shell));
break;
#if COG_HAVE_MEM_PRESSURE
case PROP_WEB_MEMORY_SETTINGS:
g_value_set_boxed(value, PRIV(shell)->web_mem_settings);
break;
case PROP_NETWORK_MEMORY_SETTINGS:
g_value_set_boxed(value, PRIV(shell)->net_mem_settings);
break;
#endif /* COG_HAVE_MEM_PRESSURE */
case PROP_WEB_DATA_MANAGER:
g_value_set_object(value, PRIV(shell)->web_data_manager);
break;
Expand Down Expand Up @@ -241,6 +247,7 @@ cog_shell_set_property (GObject *object,
case PROP_AUTOMATED:
priv->automated = g_value_get_boolean(value);
break;
#if COG_HAVE_MEM_PRESSURE
case PROP_WEB_MEMORY_SETTINGS:
g_clear_pointer(&priv->web_mem_settings, webkit_memory_pressure_settings_free);
priv->web_mem_settings = g_value_dup_boxed(value);
Expand All @@ -249,6 +256,7 @@ cog_shell_set_property (GObject *object,
g_clear_pointer(&priv->net_mem_settings, webkit_memory_pressure_settings_free);
priv->net_mem_settings = g_value_dup_boxed(value);
break;
#endif /* COG_HAVE_MEM_PRESSURE */
case PROP_WEB_DATA_MANAGER:
g_clear_object(&priv->web_data_manager);
priv->web_data_manager = g_value_dup_object(value);
Expand Down Expand Up @@ -283,11 +291,15 @@ cog_shell_constructed(GObject *object)
"base-cache-directory", cache_dir, NULL);
}

priv->web_context = g_object_new(WEBKIT_TYPE_WEB_CONTEXT, "memory-pressure-settings", priv->web_mem_settings,
"website-data-manager", priv->web_data_manager, NULL);
#if COG_HAVE_MEM_PRESSURE
priv->web_context = g_object_new(WEBKIT_TYPE_WEB_CONTEXT, "website-data-manager", priv->web_data_manager,
"memory-pressure-settings", priv->web_mem_settings, NULL);

if (priv->net_mem_settings)
webkit_website_data_manager_set_memory_pressure_settings(priv->net_mem_settings);
#else
priv->web_context = webkit_web_context_new_with_website_data_manager(priv->web_data_manager);
#endif /* COG_HAVE_MEM_PRESSURE */
}

static void
Expand Down Expand Up @@ -431,6 +443,7 @@ cog_shell_class_init (CogShellClass *klass)
FALSE,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);

#if COG_HAVE_MEM_PRESSURE
/**
* CogShell:web-memory-settings:
*
Expand All @@ -456,6 +469,7 @@ cog_shell_class_init (CogShellClass *klass)
"Memory pressure handling settings for network processes",
WEBKIT_TYPE_MEMORY_PRESSURE_SETTINGS,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
#endif /* COG_HAVE_MEM_PRESSURE */

/**
* CogShell:web-data-manager:
Expand Down
22 changes: 19 additions & 3 deletions launcher/cog-launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ struct _CogLauncher {
WebKitSettings *web_settings;
WebKitWebsiteDataManager *web_data_manager;

#if COG_HAVE_MEM_PRESSURE
WebKitMemoryPressureSettings *web_mem_settings;
WebKitMemoryPressureSettings *net_mem_settings;
#endif /* COG_HAVE_MEM_PRESSURE */

guint sigint_source;
guint sigterm_source;
Expand Down Expand Up @@ -340,9 +342,13 @@ cog_launcher_startup(GApplication *application)
g_application_hold(application);

CogLauncher *self = COG_LAUNCHER(application);
self->shell = g_object_new(COG_TYPE_SHELL, "name", g_get_prgname(), "automated", self->automated, "web-settings",
self->web_settings, "web-data-manager", self->web_data_manager, "web-memory-settings",
self->web_mem_settings, "network-memory-settings", self->net_mem_settings, NULL);
self->shell =
g_object_new(COG_TYPE_SHELL, "name", g_get_prgname(), "automated", self->automated, "web-settings",
self->web_settings, "web-data-manager", self->web_data_manager,
#if COG_HAVE_MEM_PRESSURE
"web-memory-settings", self->web_mem_settings, "network-memory-settings", self->net_mem_settings,
#endif /* COG_HAVE_MEM_PRESSURE */
NULL);
g_signal_connect_swapped(self->shell, "create-view", G_CALLBACK(cog_launcher_create_view), self);
g_signal_connect(self->shell, "notify::web-view", G_CALLBACK(on_notify_web_view), self);

Expand Down Expand Up @@ -408,8 +414,10 @@ cog_launcher_dispose(GObject *object)

g_clear_object(&launcher->web_settings);

#if COG_HAVE_MEM_PRESSURE
g_clear_pointer(&launcher->web_mem_settings, webkit_memory_pressure_settings_free);
g_clear_pointer(&launcher->net_mem_settings, webkit_memory_pressure_settings_free);
#endif /* COG_HAVE_MEM_PRESSURE */

G_OBJECT_CLASS(cog_launcher_parent_class)->dispose(object);
}
Expand Down Expand Up @@ -718,6 +726,7 @@ cog_launcher_add_web_cookies_option_entries(CogLauncher *launcher)
g_application_add_option_group(G_APPLICATION(launcher), g_steal_pointer(&option_group));
}

#if COG_HAVE_MEM_PRESSURE
static WebKitMemoryPressureSettings *
mem_settings_pick(CogLauncher *launcher, const char *option)
{
Expand Down Expand Up @@ -896,6 +905,7 @@ cog_launcher_add_mem_pressure_option_entries(CogLauncher *self)
g_option_group_add_entries(group, entries);
g_application_add_option_group(G_APPLICATION(self), group);
}
#endif /* COG_HAVE_MEM_PRESSURE */

static gboolean
option_entry_parse_permissions(const char *option G_GNUC_UNUSED,
Expand Down Expand Up @@ -1026,7 +1036,10 @@ cog_launcher_constructed(GObject *object)
cog_launcher_add_web_settings_option_entries(launcher);
cog_launcher_add_web_cookies_option_entries(launcher);
cog_launcher_add_web_permissions_option_entries(launcher);

#if COG_HAVE_MEM_PRESSURE
cog_launcher_add_mem_pressure_option_entries(launcher);
#endif /* COG_HAVE_MEM_PRESSURE */

launcher->sigint_source = g_unix_signal_add(SIGINT, G_SOURCE_FUNC(on_signal_quit), launcher);
launcher->sigterm_source = g_unix_signal_add(SIGTERM, G_SOURCE_FUNC(on_signal_quit), launcher);
Expand Down Expand Up @@ -1270,8 +1283,11 @@ static void
cog_launcher_init(CogLauncher *self)
{
self->web_settings = g_object_ref_sink(webkit_settings_new());

#if COG_HAVE_MEM_PRESSURE
self->web_mem_settings = webkit_memory_pressure_settings_new();
self->net_mem_settings = webkit_memory_pressure_settings_new();
#endif /* COG_HAVE_MEM_PRESSURE */
}

CogLauncher *
Expand Down

0 comments on commit aee7e84

Please sign in to comment.