Skip to content

Commit

Permalink
cog-launcher: Add constructor property to enable automation
Browse files Browse the repository at this point in the history
This property will be used through cog to detect whether it has been
launched in automated mode (e.g. launched from WebDriver).

The most visible changes are the "automated" property and the new
`cog_launcher_init_default(type)`, to initialize the default launcher
with the given session type. The previous `cog_launcher_get_default`
keeps the behavior of creating a non-automated launcher if none has been
created yet (unlikely to happen, though).
  • Loading branch information
lauromoura authored and aperezdc committed Aug 26, 2021
1 parent c501ed9 commit cacd787
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
12 changes: 11 additions & 1 deletion cog.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,17 @@ main (int argc, char *argv[])
g_info("%s:", COG_MODULES_PLATFORM_EXTENSION_POINT);
cog_modules_foreach(COG_MODULES_PLATFORM, print_module_info, NULL);

g_autoptr(GApplication) app = G_APPLICATION (cog_launcher_get_default ());
// We need to check whether we'll use automation mode before creating the launcher
gboolean automated = FALSE;
for (int i = 1; i < argc; i++) {
if (g_str_equal("--automation", argv[i])) {
automated = TRUE;
break;
}
}

CogSessionType sessionType = automated ? COG_SESSION_AUTOMATED : COG_SESSION_REGULAR;
g_autoptr(GApplication) app = G_APPLICATION (cog_launcher_init_default(sessionType));
g_application_add_main_option_entries (app, s_cli_options);
cog_launcher_add_web_settings_option_entries (COG_LAUNCHER (app));
cog_launcher_add_web_cookies_option_entries (COG_LAUNCHER (app));
Expand Down
67 changes: 64 additions & 3 deletions core/cog-launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ g_clear_handle_id (guint *tag_ptr,
#define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f))
#endif

enum {
PROP_0,
PROP_AUTOMATED,
};

/**
* CogLauncher:
*
Expand All @@ -54,6 +59,7 @@ struct _CogLauncher {
GApplication parent;
CogShell *shell;
gboolean allow_all_requests;
gboolean automated;

guint sigint_source;
guint sigterm_source;
Expand Down Expand Up @@ -284,18 +290,42 @@ cog_launcher_constructed (GObject *object)
#endif
}

static void
cog_launcher_set_property (GObject *object, guint propId, const GValue *value, GParamSpec *pspec)
{
CogLauncher *launcher = COG_LAUNCHER(object);

switch (propId)
{
case PROP_AUTOMATED:
launcher->automated = g_value_get_boolean(value);
break;
default:
break;
}
}


static void
cog_launcher_class_init (CogLauncherClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = cog_launcher_dispose;
object_class->constructed = cog_launcher_constructed;
object_class->set_property = cog_launcher_set_property;

GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
application_class->open = cog_launcher_open;
application_class->startup = cog_launcher_startup;
application_class->shutdown = cog_launcher_shutdown;

g_object_class_install_property(object_class,
PROP_AUTOMATED,
g_param_spec_boolean("automated",
"Automated",
"Whether this launcher is automated",
FALSE,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
}


Expand All @@ -308,6 +338,7 @@ cog_launcher_init (G_GNUC_UNUSED CogLauncher *launcher)
static void*
cog_launcher_create_instance (void* user_data)
{
CogSessionType *sessionType = user_data;
/* Global singleton */
const GApplicationFlags app_flags =
#if GLIB_CHECK_VERSION(2, 48, 0)
Expand All @@ -317,21 +348,37 @@ cog_launcher_create_instance (void* user_data)
return g_object_new (COG_TYPE_LAUNCHER,
"application-id", COG_DEFAULT_APPID,
"flags", app_flags,
"automated", *sessionType == COG_SESSION_AUTOMATED,
NULL);
}

/**
* cog_launcher_get_default:
*
* Returns the [[email protected]] single instance, creating it if needed.
* Returns the [[email protected]] single instance, creating a regular
* (non-autoamted) launcher if needed.
*
* Returns: (transfer none): Singleton instance.
*/
CogLauncher*
cog_launcher_get_default ()
{
return cog_launcher_init_default(COG_SESSION_REGULAR);
}

/**
* cog_launcher_init_default:
*
* Initializes once the [[email protected]] single instance. This instance
* can be accessed later through [id@cog_launcher_get_default].
*
* Returns: (transfer none): Singleton instance.
*/
CogLauncher*
cog_launcher_get_default (void)
cog_launcher_init_default (CogSessionType sessionType)
{
static GOnce create_instance_once = G_ONCE_INIT;
g_once (&create_instance_once, cog_launcher_create_instance, NULL);
g_once (&create_instance_once, cog_launcher_create_instance, &sessionType);
g_assert_nonnull (create_instance_once.retval);
return create_instance_once.retval;
}
Expand All @@ -350,6 +397,20 @@ cog_launcher_get_shell (CogLauncher *launcher)
return launcher->shell;
}

/**
* cog_launcher_is_automated:
*
* Whether this launcher was created in automated mode.
*
* Returns: TRUE if this session is automated, FALSE otherwise.
*/
gboolean
cog_launcher_is_automated (CogLauncher *launcher)
{
g_return_val_if_fail (COG_IS_LAUNCHER (launcher), FALSE);
return launcher->automated;
}

/**
* cog_launcher_add_web_settings_option_entries:
*
Expand Down
7 changes: 7 additions & 0 deletions core/cog-launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ struct _CogLauncherClass {
GApplicationClass parent_class;
};

typedef enum {
COG_SESSION_REGULAR,
COG_SESSION_AUTOMATED,
} CogSessionType;

CogLauncher *cog_launcher_get_default (void);
CogLauncher *cog_launcher_init_default (CogSessionType sessionType);
CogShell *cog_launcher_get_shell (CogLauncher *launcher);
gboolean cog_launcher_is_automated (CogLauncher *launcher);

void cog_launcher_add_web_settings_option_entries (CogLauncher *launcher);
void cog_launcher_add_web_cookies_option_entries (CogLauncher *launcher);
Expand Down

0 comments on commit cacd787

Please sign in to comment.