From cdaabe09befc115d5f841daca632da39c1305475 Mon Sep 17 00:00:00 2001 From: Scott Balneaves Date: Sat, 9 Jan 2016 12:16:26 -0600 Subject: [PATCH] Readability improvements, exception handling improvements --- man/lightdm-webkit2-greeter.1 | 15 +- src/lightdm-webkit2-greeter-ext.c | 426 ++++++++++++++---------------- themes/simple/index.html | 4 +- 3 files changed, 202 insertions(+), 243 deletions(-) diff --git a/man/lightdm-webkit2-greeter.1 b/man/lightdm-webkit2-greeter.1 index 30d9719..9c799be 100644 --- a/man/lightdm-webkit2-greeter.1 +++ b/man/lightdm-webkit2-greeter.1 @@ -50,11 +50,11 @@ expired\&. The greeter should reset the authentication process\&. The following functions are available for the greeter to call to execute actions within LightDM\&. .PP -\fBlightdm\&.authenticate(name)\fR +\fBlightdm\&.authenticate(username)\fR .RS 4 -Specifies the name of the user we'd like to start authenticating as\&. Note that -if you pass an empty string (""), LightDM (via PAM) will issue a show_prompt() -call to ask for the username\&. The older function +Specifies the username of the user we'd like to start authenticating as\&. Note that +if you call lightdm.authenticate with no argument, LightDM (via PAM) will issue +a show_prompt() call to ask for the username\&. The older function lightdm\&.start_authentication() has been deprecated\&. .RE .PP @@ -85,11 +85,12 @@ Returns the value of a named hint provided by LightDM\&. Hibernates the system, if the greeter has the authority to do so\&. .RE .PP -\fBlightdm\&.start_session_sync(user, session)\fR +\fBlightdm\&.start_session_sync(session)\fR .RS 4 Once LightDM has successfully authenticated the user, start the user's session -by calling this function\&. "user" is the authenticated user, and "session" is -the user's session\&. The older function lightdm\&.login() has been +by calling this function\&. "session" is the authenticated user's session\&. +If no session is passed, start the authenticated user with the system default +session. The older function lightdm\&.login(user, session) has been deprecated\&. .RE .PP diff --git a/src/lightdm-webkit2-greeter-ext.c b/src/lightdm-webkit2-greeter-ext.c index e0c524b..1c23f1f 100644 --- a/src/lightdm-webkit2-greeter-ext.c +++ b/src/lightdm-webkit2-greeter-ext.c @@ -44,6 +44,12 @@ G_MODULE_EXPORT void webkit_web_extension_initialize(WebKitWebExtension *extensi guint64 page_id = -1; +#define USER ((LightDMUser *) JSObjectGetPrivate (thisObject)) +#define LANGUAGE ((LightDMLanguage *)JSObjectGetPrivate (thisObject)) +#define LAYOUT ((LightDMLayout *) JSObjectGetPrivate (thisObject)) +#define SESSION ((LightDMSession *) JSObjectGetPrivate (thisObject)) +#define GREETER ((LightDMGreeter *) JSObjectGetPrivate (thisObject)) + static JSClassRef lightdm_greeter_class, gettext_class, @@ -52,6 +58,14 @@ static JSClassRef lightdm_layout_class, lightdm_session_class; +/* + * string_or_null. + * + * When passed a const gchar string, either return a JSValueRef string, or a + * JSValueRef Null reference if the string is NULL. Used to ensure functions + * return proper string args. + */ + static JSValueRef string_or_null(JSContextRef context, const gchar *str) { JSValueRef result; @@ -67,16 +81,67 @@ string_or_null(JSContextRef context, const gchar *str) { return result; } -static JSValueRef -mkexception (JSContextRef context, JSValueRef * exception, const gchar * str) -{ +/* + * mkexception + * + * Convert a const string to an exception which can be passed back to webkit. + */ + +static void +_mkexception (JSContextRef context, JSValueRef * exception, const gchar * str) { + JSValueRef result; JSStringRef string = JSStringCreateWithUTF8CString (str); JSValueRef exceptionString = JSValueMakeString (context, string); JSStringRelease (string); - *exception = JSValueToObject (context, exceptionString, NULL); - return JSValueMakeNull (context); + result = JSValueToObject (context, exceptionString, exception); + if (result != NULL) + *exception = result; } +static JSValueRef + mkexception (JSContextRef context, JSValueRef * exception, const gchar * str) { + _mkexception (context, exception, str); +return JSValueMakeNull (context); +} + +/* + * arg_to_string + * + * Convert an JSValueRef argument to a g_malloc'd gchar string. Calling function + * is responsible for g_freeing the string. + */ + +static gchar * +arg_to_string (JSContextRef context, JSValueRef arg, JSValueRef * exception) { + JSStringRef string; + size_t size; + gchar *result; + + if (JSValueGetType (context, arg) != kJSTypeString) { + _mkexception (context, exception, "Expected a string"); + return NULL; + } + + string = JSValueToStringCopy (context, arg, exception); + if (!string) + return NULL; + size = JSStringGetMaximumUTF8CStringSize (string); + result = g_malloc (size); + if (!result) + return NULL; + JSStringGetUTF8CString (string, result, size); + JSStringRelease (string); + + return result; +} + +/* + * escape + * + * Simple escape function to make sure strings have any single quote characters + * escaped. + */ + static char * escape (const gchar *text) { @@ -114,8 +179,7 @@ get_user_name_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMUser *user = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_user_get_name(user)); + return string_or_null(context, lightdm_user_get_name(USER)); } @@ -124,8 +188,7 @@ get_user_real_name_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMUser *user = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_user_get_real_name(user)); + return string_or_null(context, lightdm_user_get_real_name(USER)); } @@ -134,8 +197,7 @@ get_user_display_name_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMUser *user = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_user_get_display_name(user)); + return string_or_null(context, lightdm_user_get_display_name(USER)); } @@ -144,8 +206,7 @@ get_user_home_directory_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMUser *user = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_user_get_home_directory(user)); + return string_or_null(context, lightdm_user_get_home_directory(USER)); } static JSValueRef @@ -153,8 +214,7 @@ get_user_image_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMUser *user = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_user_get_image(user)); + return string_or_null(context, lightdm_user_get_image(USER)); } @@ -163,8 +223,7 @@ get_user_language_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMUser *user = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_user_get_language(user)); + return string_or_null(context, lightdm_user_get_language(USER)); } @@ -173,8 +232,7 @@ get_user_layout_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMUser *user = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_user_get_layout(user)); + return string_or_null(context, lightdm_user_get_layout(USER)); } @@ -183,8 +241,7 @@ get_user_session_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMUser *user = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_user_get_session(user)); + return string_or_null(context, lightdm_user_get_session(USER)); } @@ -193,8 +250,7 @@ get_user_logged_in_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMUser *user = JSObjectGetPrivate(thisObject); - return JSValueMakeBoolean(context, lightdm_user_get_logged_in(user)); + return JSValueMakeBoolean(context, lightdm_user_get_logged_in(USER)); } @@ -203,8 +259,7 @@ get_language_code_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMLanguage *language = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_language_get_code(language)); + return string_or_null(context, lightdm_language_get_code(LANGUAGE)); } @@ -213,8 +268,7 @@ get_language_name_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMLanguage *language = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_language_get_name(language)); + return string_or_null(context, lightdm_language_get_name(LANGUAGE)); } @@ -223,8 +277,7 @@ get_language_territory_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMLanguage *language = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_language_get_territory(language)); + return string_or_null(context, lightdm_language_get_territory(LANGUAGE)); } @@ -233,8 +286,7 @@ get_layout_name_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMLayout *layout = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_layout_get_name(layout)); + return string_or_null(context, lightdm_layout_get_name(LAYOUT)); } @@ -243,8 +295,7 @@ get_layout_short_description_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMLayout *layout = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_layout_get_short_description(layout)); + return string_or_null(context, lightdm_layout_get_short_description(LAYOUT)); } @@ -253,8 +304,7 @@ get_layout_description_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMLayout *layout = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_layout_get_description(layout)); + return string_or_null(context, lightdm_layout_get_description(LAYOUT)); } @@ -263,8 +313,7 @@ get_session_key_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMSession *session = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_session_get_key(session)); + return string_or_null(context, lightdm_session_get_key(SESSION)); } @@ -273,8 +322,7 @@ get_session_name_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMSession *session = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_session_get_name(session)); + return string_or_null(context, lightdm_session_get_name(SESSION)); } @@ -283,8 +331,7 @@ get_session_comment_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMSession *session = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_session_get_comment(session)); + return string_or_null(context, lightdm_session_get_comment(SESSION)); } @@ -325,9 +372,12 @@ get_users_cb(JSContextRef context, args[i] = JSObjectMake(context, lightdm_user_class, user); } - array = JSObjectMakeArray(context, n_users, args, NULL); + array = JSObjectMakeArray(context, n_users, args, exception); g_free(args); - return array; + if (array == NULL) + return JSValueMakeNull (context); + else + return array; } @@ -350,9 +400,12 @@ get_languages_cb(JSContextRef context, args[i] = JSObjectMake(context, lightdm_language_class, language); } - array = JSObjectMakeArray(context, n_languages, args, NULL); + array = JSObjectMakeArray(context, n_languages, args, exception); g_free(args); - return array; + if (array == NULL) + return JSValueMakeNull (context); + else + return array; } @@ -384,9 +437,12 @@ get_layouts_cb(JSContextRef context, args[i] = JSObjectMake(context, lightdm_layout_class, layout); } - array = JSObjectMakeArray(context, n_layouts, args, NULL); + array = JSObjectMakeArray(context, n_layouts, args, exception); g_free(args); - return array; + if (array == NULL) + return JSValueMakeNull (context); + else + return array; } @@ -405,24 +461,12 @@ set_layout_cb(JSContextRef context, JSStringRef propertyName, JSValueRef value, JSValueRef *exception) { - JSStringRef layout_arg; - size_t layout_size; gchar *layout; const GList *layouts, *link; - if (JSValueGetType(context, value) != kJSTypeString) { - JSStringRef string = JSStringCreateWithUTF8CString("Expected a string"); - JSValueRef exceptionString = JSValueMakeString(context, string); - JSStringRelease(string); - *exception = JSValueToObject(context, exceptionString, NULL); + layout = arg_to_string (context, value, exception); + if (!layout) return false; - } - - layout_arg = JSValueToStringCopy(context, value, NULL); - layout_size = JSStringGetMaximumUTF8CStringSize(layout_arg); - layout = g_malloc (layout_size); - JSStringGetUTF8CString(layout_arg, layout, layout_size); - JSStringRelease(layout_arg); layouts = lightdm_get_layouts (); for (link = layouts; link; link = link->next) @@ -459,9 +503,12 @@ get_sessions_cb(JSContextRef context, args[i] = JSObjectMake(context, lightdm_session_class, session); } - array = JSObjectMakeArray(context, n_sessions, args, NULL); + array = JSObjectMakeArray(context, n_sessions, args, exception); g_free(args); - return array; + if (array == NULL) + return JSValueMakeNull (context); + else + return array; } @@ -470,8 +517,7 @@ get_default_session_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMGreeter *greeter = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_greeter_get_default_session_hint(greeter)); + return string_or_null(context, lightdm_greeter_get_default_session_hint(GREETER)); } @@ -480,8 +526,7 @@ get_lock_hint_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMGreeter *greeter = JSObjectGetPrivate(thisObject); - return JSValueMakeBoolean(context, lightdm_greeter_get_lock_hint(greeter)); + return JSValueMakeBoolean(context, lightdm_greeter_get_lock_hint(GREETER)); } @@ -490,8 +535,7 @@ get_autologin_timeout_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMGreeter *greeter = JSObjectGetPrivate(thisObject); - return JSValueMakeNumber(context, lightdm_greeter_get_autologin_timeout_hint(greeter)); + return JSValueMakeNumber(context, lightdm_greeter_get_autologin_timeout_hint(GREETER)); } @@ -502,12 +546,7 @@ cancel_autologin_cb(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { - LightDMGreeter *greeter = JSObjectGetPrivate(thisObject); - - if (argumentCount != 0) - return mkexception (context, exception, "Argument count not zero"); - - lightdm_greeter_cancel_autologin(greeter); + lightdm_greeter_cancel_autologin(GREETER); return JSValueMakeNull(context); } @@ -519,27 +558,15 @@ authenticate_cb(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { - LightDMGreeter *greeter = JSObjectGetPrivate(thisObject); - JSStringRef name_arg; - size_t name_size; - gchar *name; + gchar *name = NULL; - if (!( argumentCount == 1 && JSValueGetType(context, arguments[0]) == kJSTypeString )) - return mkexception (context, exception, "Username argument not supplied"); + if (argumentCount > 0) + name = arg_to_string (context, arguments[0], exception); - name_arg = JSValueToStringCopy(context, arguments[0], NULL); - name_size = JSStringGetMaximumUTF8CStringSize(name_arg); - name = g_malloc (name_size); - JSStringGetUTF8CString(name_arg, name, name_size); - JSStringRelease(name_arg); - - if (*name == '\0') - lightdm_greeter_authenticate(greeter, NULL); - else - lightdm_greeter_authenticate(greeter, name); + lightdm_greeter_authenticate (GREETER, name); g_free (name); - return JSValueMakeNull(context); + return JSValueMakeNull (context); } static JSValueRef @@ -549,13 +576,7 @@ authenticate_as_guest_cb (JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) { - LightDMGreeter *greeter = JSObjectGetPrivate (thisObject); - - if (argumentCount != 0) - return mkexception (context, exception, "Argument count not zero"); - - lightdm_greeter_authenticate_as_guest (greeter); - + lightdm_greeter_authenticate_as_guest (GREETER); return JSValueMakeNull (context); } @@ -566,28 +587,20 @@ get_hint_cb (JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) { - LightDMGreeter *greeter = JSObjectGetPrivate (thisObject); - JSStringRef hint_arg; - size_t hint_size; gchar *hint_name; - JSStringRef hint; - JSValueRef result; + JSValueRef result; - if (!(argumentCount == 1 && JSValueGetType (context, arguments[0]) == kJSTypeString)) + if (argumentCount != 1) return mkexception (context, exception, "Hint argument not supplied"); - hint_arg = JSValueToStringCopy (context, arguments[0], NULL); - hint_size = JSStringGetMaximumUTF8CStringSize (hint_arg); - hint_name = g_malloc (hint_size); - JSStringGetUTF8CString (hint_arg, hint_name, hint_size); - JSStringRelease (hint_arg); + hint_name = arg_to_string (context, arguments[0], exception); + if (!hint_name) + return JSValueMakeNull (context); - hint = JSStringCreateWithUTF8CString (lightdm_greeter_get_hint (greeter, hint_name)); + result = string_or_null (context, lightdm_greeter_get_hint (GREETER, hint_name)); g_free (hint_name); - result = JSValueMakeString (context, hint); - JSStringRelease (hint); - return result; + return result; } @@ -598,24 +611,19 @@ respond_cb(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { - LightDMGreeter *greeter = JSObjectGetPrivate(thisObject); - JSStringRef response_arg; - size_t response_size; - gchar *response; + gchar *response; - if (!( argumentCount == 1 && JSValueGetType(context, arguments[0]) == kJSTypeString )) + if (argumentCount != 1) return mkexception (context, exception, "Response not supplied"); - response_arg = JSValueToStringCopy(context, arguments[0], NULL); - response_size = JSStringGetMaximumUTF8CStringSize(response_arg); - response = g_malloc (response_size); - JSStringGetUTF8CString(response_arg, response, response_size); - JSStringRelease(response_arg); + response = arg_to_string (context, arguments[0], exception); + if (!response) + return JSValueMakeNull (context); - lightdm_greeter_respond(greeter, response); + lightdm_greeter_respond (GREETER, response); g_free (response); - return JSValueMakeNull(context); + return JSValueMakeNull (context); } @@ -626,12 +634,7 @@ cancel_authentication_cb(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { - LightDMGreeter *greeter = JSObjectGetPrivate(thisObject); - - if (argumentCount != 0) - return mkexception (context, exception, "Argument count not zero"); - - lightdm_greeter_cancel_authentication(greeter); + lightdm_greeter_cancel_authentication(GREETER); return JSValueMakeNull(context); } @@ -641,8 +644,7 @@ get_authentication_user_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMGreeter *greeter = JSObjectGetPrivate(thisObject); - return string_or_null(context, lightdm_greeter_get_authentication_user(greeter)); + return string_or_null(context, lightdm_greeter_get_authentication_user(GREETER)); } static JSValueRef @@ -650,8 +652,7 @@ get_has_guest_account_cb (JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef * exception) { - LightDMGreeter *greeter = JSObjectGetPrivate (thisObject); - return JSValueMakeBoolean (context, lightdm_greeter_get_has_guest_account_hint (greeter)); + return JSValueMakeBoolean (context, lightdm_greeter_get_has_guest_account_hint (GREETER)); } static JSValueRef @@ -659,8 +660,7 @@ get_hide_users_cb (JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef * exception) { - LightDMGreeter *greeter = JSObjectGetPrivate (thisObject); - return JSValueMakeBoolean (context, lightdm_greeter_get_hide_users_hint (greeter)); + return JSValueMakeBoolean (context, lightdm_greeter_get_hide_users_hint (GREETER)); } static JSValueRef @@ -668,8 +668,7 @@ get_select_user_cb (JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef * exception) { - LightDMGreeter *greeter = JSObjectGetPrivate (thisObject); - return string_or_null (context, lightdm_greeter_get_select_user_hint (greeter)); + return string_or_null (context, lightdm_greeter_get_select_user_hint (GREETER)); } static JSValueRef @@ -677,8 +676,7 @@ get_select_guest_cb (JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef * exception) { - LightDMGreeter *greeter = JSObjectGetPrivate (thisObject); - return JSValueMakeBoolean (context, lightdm_greeter_get_select_guest_hint (greeter)); + return JSValueMakeBoolean (context, lightdm_greeter_get_select_guest_hint (GREETER)); } static JSValueRef @@ -686,8 +684,7 @@ get_autologin_user_cb (JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef * exception) { - LightDMGreeter *greeter = JSObjectGetPrivate (thisObject); - return string_or_null (context, lightdm_greeter_get_autologin_user_hint (greeter)); + return string_or_null (context, lightdm_greeter_get_autologin_user_hint (GREETER)); } static JSValueRef @@ -695,8 +692,7 @@ get_autologin_guest_cb (JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef * exception) { - LightDMGreeter *greeter = JSObjectGetPrivate (thisObject); - return JSValueMakeBoolean (context, lightdm_greeter_get_autologin_guest_hint (greeter)); + return JSValueMakeBoolean (context, lightdm_greeter_get_autologin_guest_hint (GREETER)); } static JSValueRef @@ -704,8 +700,7 @@ get_is_authenticated_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMGreeter *greeter = JSObjectGetPrivate(thisObject); - return JSValueMakeBoolean(context, lightdm_greeter_get_is_authenticated(greeter)); + return JSValueMakeBoolean(context, lightdm_greeter_get_is_authenticated(GREETER)); } static JSValueRef @@ -713,8 +708,7 @@ get_in_authentication_cb(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef *exception) { - LightDMGreeter *greeter = JSObjectGetPrivate(thisObject); - return JSValueMakeBoolean(context, lightdm_greeter_get_in_authentication(greeter)); + return JSValueMakeBoolean(context, lightdm_greeter_get_in_authentication(GREETER)); } static JSValueRef @@ -733,9 +727,6 @@ suspend_cb(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { - if (argumentCount != 0) - return mkexception (context, exception, "Argument count not zero"); - lightdm_suspend(NULL); return JSValueMakeNull(context); } @@ -757,9 +748,6 @@ hibernate_cb(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { - if (argumentCount != 0) - return mkexception (context, exception, "Argument count not zero"); - lightdm_hibernate(NULL); return JSValueMakeNull(context); } @@ -781,9 +769,6 @@ restart_cb(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { - if (argumentCount != 0) - return mkexception (context, exception, "Argument count not zero"); - lightdm_restart(NULL); return JSValueMakeNull(context); } @@ -805,9 +790,6 @@ shutdown_cb(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { - if (argumentCount != 0) - return mkexception (context, exception, "Argument count not zero"); - lightdm_shutdown(NULL); return JSValueMakeNull(context); } @@ -820,35 +802,34 @@ start_session_sync_cb(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { - LightDMGreeter *greeter = JSObjectGetPrivate(thisObject); - JSStringRef arg; - size_t username_size, session_size; - gchar *username, *session = NULL; - - if (!((argumentCount == 1 && JSValueGetType (context, arguments[0]) == kJSTypeString) || - (argumentCount == 2 && JSValueGetType (context, arguments[0]) == kJSTypeString && JSValueGetType (context, arguments[1]) == kJSTypeString))) - return mkexception (context, exception, "Username or Session incorrect"); - - arg = JSValueToStringCopy(context, arguments[0], NULL); - username_size = JSStringGetMaximumUTF8CStringSize(arg); - username = g_malloc (username_size); - JSStringGetUTF8CString(arg, username, username_size); - JSStringRelease(arg); - - if (argumentCount > 1) { - arg = JSValueToStringCopy(context, arguments[1], NULL); - session_size = JSStringGetMaximumUTF8CStringSize(arg); - session = g_malloc (session_size); - JSStringGetUTF8CString(arg, session, session_size); - JSStringRelease(arg); + gchar *session = NULL; + gboolean result; + GError *err = NULL; + + /* FIXME: old API required lightdm.login(username, session), but the username + * is never actually used. At some point, deprecate the old usage. For now, + * simply work around it. */ + + if (argumentCount == 1) + session = arg_to_string (context, arguments[0], exception); + else if (argumentCount == 2) + session = arg_to_string (context, arguments[1], exception); + else if (argumentCount == 0) + session = NULL; + else { + _mkexception (context, exception, "Incorrect parameters"); + return JSValueMakeBoolean (context, FALSE); } - lightdm_greeter_start_session_sync(greeter, session, NULL); + result = lightdm_greeter_start_session_sync (GREETER, session, &err); - g_free(username); - g_free(session); + g_free (session); + if (err != NULL) { + _mkexception (context, exception, err->message); + g_error_free (err); + } - return JSValueMakeNull(context); + return JSValueMakeBoolean (context, result); } @@ -859,24 +840,19 @@ set_language_cb(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { - LightDMGreeter *greeter = JSObjectGetPrivate(thisObject); - JSStringRef arg; - size_t language_size; - gchar *language; + gchar *language; - if (!(argumentCount == 1 && JSValueGetType (context, arguments[0]) == kJSTypeString)) + if (argumentCount != 1) return mkexception (context, exception, "Language not supplied"); - arg = JSValueToStringCopy(context, arguments[0], NULL); - language_size = JSStringGetMaximumUTF8CStringSize(arg); - language = g_malloc (language_size); - JSStringGetUTF8CString(arg, language, language_size); - JSStringRelease(arg); + language = arg_to_string (context, arguments[0], exception); + if (!language) + return JSValueMakeNull (context); - lightdm_greeter_set_language(greeter, language); + lightdm_greeter_set_language (GREETER, language); g_free (language); - return JSValueMakeNull(context); + return JSValueMakeNull (context); } @@ -887,24 +863,17 @@ gettext_cb(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { - JSStringRef string_arg, text; - size_t string_size; - gchar *string; - JSValueRef result; + gchar *string; + JSValueRef result; - if (!(argumentCount == 1 && JSValueGetType (context, arguments[0]) == kJSTypeString)) + if (argumentCount != 1) return mkexception (context, exception, "Argument not supplied"); - string_arg = JSValueToStringCopy(context, arguments[0], NULL); - string_size = JSStringGetMaximumUTF8CStringSize(string_arg); - string = g_malloc (string_size); - JSStringGetUTF8CString(string_arg, string, string_size); - JSStringRelease(string_arg); - - text = JSStringCreateWithUTF8CString(gettext(string)); + string = arg_to_string (context, arguments[0], exception); + if (!string) + return JSValueMakeNull (context); + result = string_or_null (context, gettext (string)); g_free (string); - result = JSValueMakeString(context, text); - JSStringRelease (text); return result; } @@ -917,34 +886,24 @@ ngettext_cb(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { - JSStringRef string_arg, plural_string_arg, text; - size_t string_size, plural_string_size; - gchar *string, *plural_string; + gchar *string, *plural_string; unsigned int n; JSValueRef result; if (argumentCount != 3) return mkexception (context, exception, "Needs 3 arguments"); - string_arg = JSValueToStringCopy(context, arguments[0], NULL); - string_size = JSStringGetMaximumUTF8CStringSize(string_arg); - string = g_malloc (string_size); - JSStringGetUTF8CString(string_arg, string, string_size); - JSStringRelease(string_arg); - - plural_string_arg = JSValueToStringCopy(context, arguments[1], NULL); - plural_string_size = JSStringGetMaximumUTF8CStringSize(plural_string_arg); - plural_string = g_malloc (plural_string_size); - JSStringGetUTF8CString(plural_string_arg, string, plural_string_size); - JSStringRelease(plural_string_arg); - - n = JSValueToNumber(context, arguments[2], NULL); + string = arg_to_string (context, arguments[0], exception); + if (!string) + return JSValueMakeNull (context); + plural_string = arg_to_string (context, arguments[1], exception); + if (!plural_string) + return JSValueMakeNull (context); + n = JSValueToNumber (context, arguments[2], exception); - text = JSStringCreateWithUTF8CString(ngettext(string, plural_string, n)); + result = string_or_null (context, ngettext (string, plural_string, n)); g_free (string); g_free (plural_string); - result = JSValueMakeString(context, text); - JSStringRelease (text); return result; } @@ -1018,7 +977,7 @@ static const JSStaticFunction lightdm_greeter_functions[] = { {"authenticate", authenticate_cb, kJSPropertyAttributeReadOnly}, {"authenticate_as_guest", authenticate_as_guest_cb, kJSPropertyAttributeReadOnly}, {"respond", respond_cb, kJSPropertyAttributeReadOnly}, - {"provide_secret", respond_cb, kJSPropertyAttributeReadOnly}, /* Deprecated */ + {"provide_secret", respond_cb, kJSPropertyAttributeReadOnly}, /* Deprecated */ {"cancel_authentication", cancel_authentication_cb, kJSPropertyAttributeReadOnly}, {"suspend", suspend_cb, kJSPropertyAttributeReadOnly}, {"hibernate", hibernate_cb, kJSPropertyAttributeReadOnly}, @@ -1292,4 +1251,3 @@ webkit_web_extension_initialize(WebKitWebExtension *extension) { lightdm_greeter_connect_sync(greeter, NULL); } - diff --git a/themes/simple/index.html b/themes/simple/index.html index b0b7515..9a787ec 100644 --- a/themes/simple/index.html +++ b/themes/simple/index.html @@ -92,7 +92,7 @@ function authentication_complete() { if (lightdm.is_authenticated) { - lightdm.start_session_sync (lightdm.authentication_user, lightdm.default_session); + lightdm.start_session_sync (); // Start default session } else { show_message ("Authentication Failed", "error"); setTimeout (start_authentication, 3000); @@ -130,7 +130,7 @@ function start_authentication() { clear_messages(); - lightdm.authenticate(""); // start with null userid, have pam prompt for userid. + lightdm.authenticate(); // start with null userid, have pam prompt for userid. } /*