Skip to content

Commit

Permalink
wl: Split cog_platform_configure in 2 stages (create and configure)
Browse files Browse the repository at this point in the history
Disassociate the configuration respect of the creation of the platform.
  • Loading branch information
psaavedra committed Nov 5, 2023
1 parent d143dcc commit 99bc611
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 29 deletions.
86 changes: 64 additions & 22 deletions core/cog-platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,29 +148,23 @@ cog_platform_create_im_context(CogPlatform *platform)
}

/**
* cog_platform_configure: (constructor)
* @name: (nullable): Name of the platform implementation to use.
* cog_platform_configure:
* @params: (nullable): Parameters passed to the platform implementation.
* @env_prefix: (nullable): Prefix for environment variables to check.
* @shell: The shell that the platform will be configured for.
* @error: Location where to store errors, if any.
*
* Creates and configures a platform implementation.
* Configures a platform implementation.
*
* Search and create a platform implementation with the given @name,
* Search a platform implementation with the given @name,
* configuring it with the passed @params for use with a @shell.
*
* If the @env_prefix is non-%NULL, then the environment variable
* `<env_prefix>_PLATFORM_NAME` can be used to set the platform name
* when @name is %NULL, and the variable `<env_prefix>_PLATFORM_PARAMS`
* `<env_prefix>_PLATFORM_PARAMS`
* can be used to set the configuration parameters when @params is %NULL.
* Environment variables will *not* be used if %NULL is passed as the
* prefix.
*
* If both @name is %NULL and the `<env_prefix>_PLATFORM_NAME` variable
* not defined, then the platform implementation will be chosen automatically
* among the available ones.
*
* Note that [id@cog_modules_add_directory] may be used beforehand to
* configure where to search for available platform plug-ins.
*
Expand All @@ -179,28 +173,26 @@ cog_platform_create_im_context(CogPlatform *platform)
* Since: 0.18
*/
CogPlatform *
cog_platform_configure(const char *name, const char *params, const char *env_prefix, CogShell *shell, GError **error)
cog_platform_configure(const char *params, const char *env_prefix, CogShell *shell, GError **error)
{
g_return_val_if_fail(!default_platform, default_platform);
CogPlatform *platform = cog_platform_get_default();
if (!platform) {
g_set_error_literal(error,
COG_PLATFORM_WPE_ERROR,
COG_PLATFORM_WPE_ERROR_INIT,
"Failed to configure missing platform");
return NULL;
}

g_autofree char *platform_name = g_strdup(name);
g_autofree char *platform_params = g_strdup(params);

if (env_prefix) {
if (!platform_name) {
g_autofree char *name_var = g_strconcat(env_prefix, "_PLATFORM_NAME", NULL);
platform_name = g_strdup(g_getenv(name_var));
}
if (!params) {
g_autofree char *params_var = g_strconcat(env_prefix, "_PLATFORM_PARAMS", NULL);
platform_params = g_strdup(g_getenv(params_var));
}
}
g_debug("%s: name '%s', params '%s'", G_STRFUNC, platform_name, platform_params);

CogPlatform *platform = cog_platform_new(platform_name, error);
if (!platform)
return NULL;
g_debug("%s: params '%s'", G_STRFUNC, platform_params);

if (!cog_platform_setup(platform, shell, platform_params ?: "", error)) {
g_object_unref(platform);
Expand All @@ -212,3 +204,53 @@ cog_platform_configure(const char *name, const char *params, const char *env_pre
g_assert(platform == default_platform);
return platform;
}

/**
* cog_platform_create: (constructor)
* @name: (nullable): Name of the platform implementation to use.
* @env_prefix: (nullable): Prefix for environment variables to check.
* @error: Location where to store errors, if any.
*
* Creates a platform implementation.
*
* Create a platform implementation with the given @name,
*
* If the @env_prefix is non-%NULL, then the environment variable
* `<env_prefix>_PLATFORM_NAME` can be used to set the platform name
* when @name is %NULL.
* Environment variables will *not* be used if %NULL is passed as the
* prefix.
*
* If both @name is %NULL and the `<env_prefix>_PLATFORM_NAME` variable
* not defined, then the platform implementation will be chosen automatically
* among the available ones.
*
* Note that [id@cog_modules_add_directory] may be used beforehand to
* configure where to search for available platform plug-ins.
*
* Returns: (transfer full): The created platform.
*
* Since: 0.20
*/
CogPlatform *
cog_platform_create(const char *name, const char *env_prefix, GError **error)
{
g_autofree char *platform_name = g_strdup(name);

if (env_prefix) {
if (!platform_name) {
g_autofree char *name_var = g_strconcat(env_prefix, "_PLATFORM_NAME", NULL);
platform_name = g_strdup(g_getenv(name_var));
}
}
g_debug("%s: name '%s'", G_STRFUNC, platform_name);

CogPlatform *platform = cog_platform_new(platform_name, error);
if (!platform) {
g_set_error_literal(error, COG_PLATFORM_WPE_ERROR, COG_PLATFORM_WPE_ERROR_INIT, "Failed creating platform");
return NULL;
}

g_assert(platform == default_platform);
return platform;
}
3 changes: 2 additions & 1 deletion core/cog-platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ COG_API
WebKitInputMethodContext *cog_platform_create_im_context (CogPlatform *platform);

COG_API CogPlatform *
cog_platform_configure(const char *name, const char *params, const char *env_prefix, CogShell *shell, GError **error);
cog_platform_configure(const char *params, const char *env_prefix, CogShell *shell, GError **error);
COG_API CogPlatform *cog_platform_create(const char *name, const char *env_prefix, GError **error);

G_END_DECLS

Expand Down
5 changes: 3 additions & 2 deletions examples/viewport.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ main(int argc, char *argv[])
cog_modules_add_directory(g_getenv("COG_MODULEDIR") ?: COG_MODULEDIR);

g_autoptr(GError) error = NULL;
g_autoptr(CogShell) shell = cog_shell_new(g_get_prgname(), FALSE);
g_autoptr(CogPlatform) platform = cog_platform_configure(NULL, NULL, "COG", shell, &error);
g_autoptr(CogPlatform) platform = cog_platform_create(NULL, "COG", &error);
if (!platform)
g_error("Cannot configure platform: %s", error->message);
g_autoptr(CogShell) shell = cog_shell_new(g_get_prgname(), FALSE);
cog_platform_configure(NULL, "COG", shell, &error);

g_autoptr(GMainLoop) loop = g_main_loop_new(NULL, FALSE);

Expand Down
14 changes: 10 additions & 4 deletions launcher/cog-launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,9 @@ cog_launcher_create_view(CogLauncher *self, CogShell *shell)
webkit_web_context_set_cache_model(web_context, WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
}

g_autoptr(GError) error = NULL;
CogPlatform *platform =
cog_platform_configure(s_options.platform_name, s_options.platform_params, "COG", shell, &error);
CogPlatform *platform = cog_platform_get_default();
if (!platform)
g_error("Cannot instantiate a platform: %s", error->message);
g_error("Cannot find a platform available");

#if HAVE_WEBKIT_AUTOPLAY
WebKitWebsitePolicies *website_policies =
Expand Down Expand Up @@ -331,6 +329,10 @@ cog_launcher_startup(GApplication *application)
g_application_hold(application);

CogLauncher *self = COG_LAUNCHER(application);

g_autoptr(GError) error = NULL;
CogPlatform *platform = cog_platform_create(s_options.platform_name, "COG", &error);

self->shell = g_object_new(
COG_TYPE_SHELL, "name", g_get_prgname(), "automated", self->automated, "web-settings", self->web_settings,
#if !COG_USE_WPE2
Expand All @@ -340,6 +342,10 @@ cog_launcher_startup(GApplication *application)
"web-memory-settings", self->web_mem_settings, "network-memory-settings", self->net_mem_settings,
#endif /* COG_HAVE_MEM_PRESSURE */
NULL);

if (platform != cog_platform_configure(s_options.platform_params, "COG", self->shell, &error))
g_error("Cannot configure the platform: %s", error->message);

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

0 comments on commit 99bc611

Please sign in to comment.