3
0
mirror of https://github.com/cytodev/LightDMMock.git synced 2024-11-24 23:19:49 +01:00
LightDMMock/mock/LightDMMock.js

466 lines
75 KiB
JavaScript
Raw Normal View History

/**
* LightDMMock "class"
*
* @author Roel Walraven <mail@cytodev.io>
*
* A LightDM Mock that is tightly based on the source C code of
* Antergos' lightdm-webkit2-greeter. Please note that the deprecation errors
* are intrusive for a reason.
*
* Usage:
* Include the file in your theme that needs mocking
* <script type="text/javascript" src="mock/LightDMMock.js"></script>
* Create a new instance of LightDMMock
2016-07-12 23:05:10 +02:00
* if(!("lightdm" in window)) {
* var LightDMMock = LightDMMock || {};
* window.lightdm = new LightDMMock(autofill, timeout, autoGuest);
* }
*
* @param {boolean} autofill [wether or not the arrays for users, languages,
* layouts, and sessions need to be filled with mock
* data. I advise to test both to make your theme
* less prone to crashing.]
* @param {number} timeout [Value to use for simulated autologin (this value
* is in seconds).]
* @param {boolean} autoGuest [Wether or not to simulate automatic guest login.
* This will also enable a guest account
* in lightdm.has_guest_account]
*/
"use strict";
function LightDMMock(autofill, timeout, autoGuest) {
this.hostname = null;
this.users = null;
2016-07-12 23:51:10 +02:00
this.default_language = null;
this.language = null;
this.languages = null;
2016-07-12 23:51:10 +02:00
this.default_layout = null;
this.layouts = null;
this.layout = null;
this.sessions = null;
this.num_users = 0;
this.default_session = null;
2016-07-12 23:51:10 +02:00
this.timed_login_user = null;
this.timed_login_delay = null;
this.authentication_user = null;
this.in_authentication = false;
this.is_authenticated = false;
this.can_suspend = false;
this.can_hibernate = false;
this.can_restart = false;
this.can_shutdown = false;
this.lock_hint = false;
this.has_guest_account = false;
this.hide_users = false;
this.select_user = null;
this.select_guest = false;
this.autologin_user = null;
this.autologin_guest = false;
this.autologin_timeout = 0;
2016-07-12 23:51:10 +02:00
if(typeof autofill === "boolean" && autofill) {
this.users = JSON.parse(atob("W3siZGlzcGxheV9uYW1lIjoiQWxleGFuZGVyIHRoZSBHcmVhdCIsImxhbmd1YWdlIjoiZ3JfR1IiLCJyZ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mljIChxd2VydHkpIn0seyJuYW1lIjoiYXJhXHRxd2VydHlfZGlnaXRzIiwic2hvcnRfZGVzY3JpcHRpb24iOiIiLCJkZXNjcmlwdGlvbiI6IkFyYWJpYyAocXdlcnR5L2RpZ2l0cykifSx7Im5hbWUiOiJhcmFcdGJ1Y2t3YWx0ZXIiLCJzaG9ydF9kZXNjcmlwdGlvbiI6IiIsImRlc2NyaXB0aW9uIjoiQXJhYmljIChCdWNrd2FsdGVyKSJ9LHsibmFtZSI6ImFsIiwic2hvcnRfZGVzY3JpcHRpb24iOiJzcSIsImRlc2NyaXB0aW9uIjoiQWxiYW5pYW4ifSx7Im5hbWUiOiJhbSIsInNob3J0X2Rlc2NyaXB0aW9uIjoiaHkiLCJkZXNjcmlwdGlvbiI6IkFybWVuaWFuIn0seyJuYW1lIjo
this.sessions = JSON.parse(atob("W3sibmFtZSI6IkxYREUiLCJrZXkiOiJMWERFIiwiY29tbWVudCI6IkxYREUgLSBMaWdodHdlaWdodCBYMTEgZGVza3RvcCBlbnZpcm9ubWVudCJ9LHsibmFtZSI6Ik9wZW5ib3giLCJrZXkiOiJvcGVuYm94IiwiY29tbWVudCI6IkxvZyBpbiB1c2luZyB0aGUgT3BlbmJveCB3aW5kb3cgbWFuYWdlciAod2l0aG91dCBhIHNlc3Npb24gbWFuYWdlcikifSx7Im5hbWUiOiJUV00iLCJrZXkiOiJ0d20iLCJjb21tZW50IjoiVGhlIFRhYiBXaW5kb3cgTWFuYWdlciJ9LHsibmFtZSI6IlRpbnl3bSIsImtleSI6InRpbnl3bSIsImNvbW1lbnQiOiJSaWRpY3Vsb3VzbHkgdGlueSB3aW5kb3cgbWFuYWdlciJ9XQ"));
this.language = this.languages[0].name;
this.layout = this.layouts[0].name;
this.default_session = this.sessions[0].name;
this.layout = this.layouts[0].name;
this.num_users = this.users.length;
if(typeof timeout === "number" && timeout > 0) {
2016-07-12 23:51:10 +02:00
if(typeof autoGuest === "boolean" && autoGuest) {
this.autologin_user = null;
this.autologin_guest = autoGuest;
}
this.autologin_user = this.users[0].name;
this.autologin_timeout = timeout * 1000;
setTimeout(function() {
2016-07-12 23:51:10 +02:00
if((typeof autoGuest === "boolean" && autoGuest) || this.autologin_user !== null)
window.autologin_timer_expired();
}.bind(this), this.timed_login_delay);
}
for(var i = 0; i <= this.users; i++) {
this.users[i].logged_in = Boolean(Math.floor(Math.random() * 2));
this.users[i].session = this.sessions[Math.floor((Math.random() * this.sessions.length))].name;
}
}
}
/**
* Deprecated method.
*/
2016-07-12 23:51:10 +02:00
LightDMMock.prototype.cancel_timed_login = function() {
window.deprecationNotifier("method", "lightdm.cancel_timed_login()", "lightdm.cancel_autologin()");
};
/**
* Cancels the authentication of the autologin user. The older function
* lightdm.cancel_timed_login() has been deprecated.
*/
LightDMMock.prototype.cancel_autologin = function() {
window.checkArguments(arguments, 0, []);
this.autologin_user = null;
this.autologin_guest = false;
this.autologin_timeout = 0;
};
/**
* Deprecated method.
*/
2016-07-12 23:51:10 +02:00
LightDMMock.prototype.start_authentication = function() {
window.deprecationNotifier("method", "lightdm.start_authentication()", "lightdm.authenticate(username)");
};
/**
* 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.
*
* @param {String} username [username to authenticate]
*/
LightDMMock.prototype.authenticate = function(username) {
window.checkArguments(arguments, 1, ["string"]);
if(this.in_authentication) {
window.show_message("Already authenticating " + this.authentication_user, "error");
return;
}
var exists = false;
for(var i = 0; i <= this.users.length; i++) {
if(this.users.name === username)
exists = true;
}
if(!exists) {
window.show_message("Invalid username", "error");
return;
}
this.authentication_user = username;
this.in_authentication = true;
window.show_prompt("Password:", "password");
};
/**
* Authenticates as the guest user.
*/
LightDMMock.prototype.authenticate_as_guest = function() {
window.checkArguments(arguments, 0, []);
if(!this.has_guest_account)
throw new IlligalUsageException("Guest accounts are turned off. Make sure you check the value of 'lightdm.has_guest_account' before calling this function.");
if(this.in_authentication) {
window.show_message("Already authenticating" + this.authentication_user, "error");
return;
}
this.authentication_user = "guest";
this.in_authentication = true;
};
/**
* When LightDM has prompted for input, provide the response to LightDM. The
* deprecated function was "provide_secret". This is still available for
* backwards compatibility, but authors of greeters should move
* to using lightdm.respond().
*
* @param {String} text [the response to the challange, usually a password]
*/
LightDMMock.prototype.respond = function(text) {
window.checkArguments(arguments, 1, ["string"]);
if(!this.in_authentication)
throw new IlligalUsageException("LightDM is currently not in the authentication phase. Make sure to call 'lightdm.authenticate(username)' before calling this function.");
if(text === "password") {
this.is_authenticated = true;
window.authentication_complete();
} else {
window.show_message("Invalid password", "error");
}
};
/**
* Deprecated method.
*/
2016-07-12 23:51:10 +02:00
LightDMMock.prototype.provide_secret = function() {
window.deprecationNotifier("method", "lightdm.provide_secret(text)", "lightdm.respond(text)");
};
/**
* Cancels the authentication of any user currently in the
* process of authenticating.
*/
LightDMMock.prototype.cancel_authentication = function() {
window.checkArguments(arguments, 0, []);
this.authentication_user = null;
this.in_authentication = false;
};
/**
* Suspends the system, if the greeter has the authority to do so.
*/
LightDMMock.prototype.suspend = function() {
window.checkArguments(arguments, 0, []);
if(!this.can_suspend)
throw new IlligalUsageException("LightDM cannot suspend the system. Make sure you check the value of 'lightdm.can_suspend' before calling this function.");
window.alert("System suspended.");
document.location.reload(true);
};
/**
* Hibernates the system, if the greeter has the authority to do so.
*/
LightDMMock.prototype.hibernate = function() {
window.checkArguments(arguments, 0, []);
if(!this.can_hibernate)
throw new IlligalUsageException("LightDM cannot hibernate the system. Make sure you check the value of 'lightdm.can_hibernate' before calling this function.");
window.alert("System hibernated.");
document.location.reload(true);
};
/**
* Shuts down the system, if the greeter has the authority to do so.
*/
LightDMMock.prototype.shutdown = function() {
window.checkArguments(arguments, 0, []);
if(!this.can_shutdown)
throw new IlligalUsageException("LightDM cannot shut down the system. Make sure you check the value of 'lightdm.can_shutdown' before calling this function.");
window.alert("System shut down.");
document.location.reload(true);
};
/**
* Restarts the system, if the greeter has the authority to do so.
*/
LightDMMock.prototype.restart = function() {
window.checkArguments(arguments, 0, []);
if(!this.can_restart)
throw new IlligalUsageException("LightDM cannot restart the system. Make sure you check the value of 'lightdm.can_restart' before calling this function.");
window.alert("System restarted.");
document.location.reload(true);
};
/**
* Will set the language for the current LightDM session.
*
* @param {String} lang [the language to change to]
*/
LightDMMock.prototype.set_language = function(lang) {
window.checkArguments(arguments, 1, ["string"]);
this.language = lang;
};
/**
* Deprecated method.
*/
2016-07-12 23:51:10 +02:00
LightDMMock.prototype.login = function() {
window.deprecationNotifier("method", "lightdm.login()", "lightdm.start_session_sync(session)");
};
/**
* Once LightDM has successfully authenticated the user, start the user's
* session 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.
*
* @param {String} session [the session name to start]
*/
LightDMMock.prototype.start_session_sync = function(session) {
window.checkArguments(arguments, 1, ["string"]);
if(!this.in_authentication)
throw new IlligalUsageException("LightDM is currently not in the authentication phase. Make sure to call 'lightdm.authenticate(username)' before calling this function.");
if(!this.is_authenticated)
throw new IlligalUsageException("LightDM has no authenticated users to log in. Make sure to call 'lightdm.respond()' before calling this function.");
window.alert("LightDM has started a " + session + " session for " + this.authentication_user);
};
/**
* Returns the value of a named hint provided by LightDM.
*
* @param {String} hint_name [name of the hint to show]
*/
LightDMMock.prototype.get_hint = function(hint_name) {
window.checkArguments(arguments, 1, ["string"]);
// @fixme: I have no clue how to simulate this...
};
/******************************************************************************
* Throwables *
*******************************************************************************/
/**
* Throwable IlligalUsageException
*
* @param {string} message [description of illigal usage]
*/
function IlligalUsageException(message) {
this.name = "IlligalUsageException";
this.message = message;
this.toString = function() {
return "[" + this.name + "] " + this.message;
};
}
/**
* Throwable DeprecationException
*
* @param {String} type [method||property]
2016-07-12 23:51:10 +02:00
* @param {String} depricated [deprecated method or property name]
* @param {String} alternative [alternative method or property to use]
*/
2016-07-12 23:51:10 +02:00
function DeprecationException(type, deprecated, alternative) {
this.name = "DeprecationException";
2016-07-12 23:51:10 +02:00
this.message = "The " + type + " '" + deprecated + "' is deprecated. Consider using '" + alternative + "' instead.";
this.toString = function() {
return "[" + this.name + "] " + this.message;
};
}
/**
* Throwable IncompatibleArgumentCountException
*
* @param {Number} expected [expected length of arguments]
* @param {Number} received [found length of arguments]
*/
function IncompatibleArgumentCountException(expected, received) {
this.name = "IncompatibleArgumentCountException";
this.message = "Incorrect number of arguments in function call. Expected " + expected + ", found " + received;
this.toString = function() {
return "[" + this.name + "] " + this.message;
};
}
/**
* Throwable IncompatibleArgumentTypesException
*
* @param {Number} type [argument number (non-zero)]
* @param {String} expected [expected type]
* @param {String} received [found type]
*/
function IncompatibleArgumentTypesException(number, expected, received) {
this.name = "IncompatibleArgumentTypesException";
this.message = "Argument " + number + " is of a wrong type. Expected '" + expected + "', found '" + received + "'";
this.toString = function() {
return "[" + this.name + "] " + this.message;
};
}
/******************************************************************************
* Helpers *
*******************************************************************************/
/**
* global helper deprecationNotifier
* throws ~balls~ errors at users who use deprecated methods and properties.
*
* @param {String} type [method||property]
2016-07-12 23:51:10 +02:00
* @param {String} depricated [deprecated method or property name]
* @param {String} alternative [alternative method or property to use]
*
* @throws {DeprecationException}
*/
2016-07-12 23:51:10 +02:00
window.deprecationNotifier = function(type, deprecated, alternative) {
throw new DeprecationException(type, deprecated, alternative);
};
/**
* global helper checkArguments
* throws ~tables~ errors at users who call methods
* with erroneous arguments.
*
* @param {Array} args [the arguments passed to the original method]
* @param {Number} length [the expected amount of arguments]
* @param {Arrray} types [the expected types of the arguments]
*
* @throws {IncompatibleArgumentTypesException}
*/
window.checkArguments = function(args, length, types) {
if(args.length -1 !== length)
throw new IncompatibleArgumentCountException(length, args.length -1);
for(var i = 1; i <= types.length; i++) {
if(typeof args[i] !== types[i-1])
throw new IncompatibleArgumentTypesException(i, types[i-1], typeof args[i]);
}
};
2016-07-12 23:51:10 +02:00
/******************************************************************************
* Object.watch shim *
*******************************************************************************/
if(!Object.prototype.watch) {
Object.prototype.watch = function (prop, handler) {
var oldval = this[prop];
var newval;
var getter = function() {
return newval;
};
var setter = function(val) {
oldval = newval;
newval = handler.call(this, prop, oldval, val);
return newval;
};
if(delete this[prop]) {
Object.defineProperty(this, prop, {
get: getter,
set: setter
});
}
};
}
if(!Object.prototype.unwatch) {
Object.prototype.unwatch = function (prop) {
var val = this[prop];
delete this[prop];
this[prop] = val;
};
}
LightDMMock.watch('default_language', function() {
window.deprecationNotifier("property", "default_language", "lightdm.language");
});
LightDMMock.watch('default_layout', function() {
window.deprecationNotifier("property", "default_layout", "lightdm.layout");
});
LightDMMock.watch('timed_login_user', function() {
window.deprecationNotifier("property", "timed_login_user", "lightdm.autologin_user");
});
LightDMMock.watch('timed_login_delay', function() {
window.deprecationNotifier("property", "timed_login_delay", "lightdm.autologin_timeout");
});