2012-10-18 02:16:03 +04:00
|
|
|
"use strict";
|
|
|
|
|
2012-08-20 02:35:44 +04:00
|
|
|
// runapp.js:
|
|
|
|
// Provide a --runapp APPNAME command-line option.
|
|
|
|
|
2012-10-18 02:16:03 +04:00
|
|
|
let runAppObj;
|
2012-08-20 02:35:44 +04:00
|
|
|
window.addEventListener('load', function() {
|
|
|
|
// Get the command line arguments that were passed to the b2g client
|
|
|
|
let args = window.arguments[0].QueryInterface(Ci.nsICommandLine);
|
|
|
|
let appname;
|
|
|
|
|
|
|
|
// - Check if the argument is present before doing any work.
|
|
|
|
try {
|
|
|
|
// Returns null if the argument was not specified. Throws
|
|
|
|
// NS_ERROR_INVALID_ARG if there is no parameter specified (because
|
|
|
|
// it was the last argument or the next argument starts with '-').
|
|
|
|
// However, someone could still explicitly pass an empty argument!
|
|
|
|
appname = args.handleFlagWithParam('runapp', false);
|
2012-10-18 02:16:03 +04:00
|
|
|
} catch(e) {
|
2012-08-20 02:35:44 +04:00
|
|
|
// treat a missing parameter like an empty parameter (=> show usage)
|
|
|
|
appname = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// not specified, bail.
|
2012-10-18 02:16:03 +04:00
|
|
|
if (appname === null) {
|
2012-08-20 02:35:44 +04:00
|
|
|
return;
|
2012-10-18 02:16:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
runAppObj = new AppRunner(appname);
|
2013-03-04 22:38:00 +04:00
|
|
|
Services.obs.addObserver(runAppObj, 'browser-ui-startup-complete', false);
|
2012-10-18 02:16:03 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener('unload', function() {
|
2013-05-16 05:35:48 +04:00
|
|
|
if (runAppObj) {
|
|
|
|
Services.obs.removeObserver(runAppObj, 'browser-ui-startup-complete');
|
|
|
|
}
|
2012-10-18 02:16:03 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
function AppRunner(aName) {
|
|
|
|
this._req = null;
|
|
|
|
this._appName = aName;
|
|
|
|
}
|
|
|
|
AppRunner.prototype = {
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
2013-03-04 22:38:00 +04:00
|
|
|
if (aTopic == 'browser-ui-startup-complete') {
|
2012-10-18 02:16:03 +04:00
|
|
|
this.doRunApp();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
doRunApp: function() {
|
|
|
|
// - Get the list of apps since the parameter was specified
|
|
|
|
this._req = navigator.mozApps.mgmt.getAll();
|
|
|
|
this._req.onsuccess = this.getAllSuccess.bind(this);
|
|
|
|
this._req.onerror = this.getAllError.bind(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllSuccess: function() {
|
|
|
|
let apps = this._req.result;
|
2012-08-20 02:35:44 +04:00
|
|
|
|
|
|
|
function findAppWithName(name) {
|
|
|
|
let normalizedSearchName = name.replace(/[- ]+/g, '').toLowerCase();
|
|
|
|
|
|
|
|
for (let i = 0; i < apps.length; i++) {
|
|
|
|
let app = apps[i];
|
|
|
|
let normalizedAppName =
|
2012-10-18 02:16:03 +04:00
|
|
|
app.manifest.name.replace(/[- ]+/g, '').toLowerCase();
|
2012-08-20 02:35:44 +04:00
|
|
|
if (normalizedSearchName === normalizedAppName) {
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function usageAndDie(justApps) {
|
|
|
|
if (!justApps)
|
|
|
|
dump(
|
|
|
|
'The --runapp argument specifies an app to automatically run at\n'+
|
|
|
|
'startup. We match against app names per their manifest and \n' +
|
|
|
|
'ignoring capitalization, dashes, and whitespace.\n' +
|
|
|
|
'\nThe system will load as usual except the lock screen will be ' +
|
|
|
|
'automatically be disabled.\n\n' +
|
|
|
|
'Known apps:\n');
|
|
|
|
|
|
|
|
for (let i = 0; i < apps.length; i++) {
|
|
|
|
dump(' ' + apps[i].manifest.name + '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit the b2g client
|
|
|
|
Services.startup.quit(Ci.nsIAppStartup.eAttemptQuit);
|
|
|
|
}
|
|
|
|
|
2012-10-18 02:16:03 +04:00
|
|
|
if (this._appName === '') {
|
2012-08-20 02:35:44 +04:00
|
|
|
usageAndDie();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-18 02:16:03 +04:00
|
|
|
let app = findAppWithName(this._appName);
|
2012-08-20 02:35:44 +04:00
|
|
|
if (!app) {
|
2012-10-18 02:16:03 +04:00
|
|
|
dump('Could not find app: "' + this._appName + '". Maybe you meant one of:\n');
|
2012-08-20 02:35:44 +04:00
|
|
|
usageAndDie(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let setReq =
|
2012-09-06 04:33:03 +04:00
|
|
|
navigator.mozSettings.createLock().set({'lockscreen.enabled': false});
|
2012-08-20 02:35:44 +04:00
|
|
|
setReq.onsuccess = function() {
|
2012-10-18 02:16:03 +04:00
|
|
|
// give the event loop 100ms to disable the lock screen
|
2012-08-20 02:35:44 +04:00
|
|
|
window.setTimeout(function() {
|
|
|
|
dump('--runapp launching app: ' + app.manifest.name + '\n');
|
|
|
|
app.launch();
|
2012-10-18 02:16:03 +04:00
|
|
|
}, 100);
|
2012-08-20 02:35:44 +04:00
|
|
|
};
|
|
|
|
setReq.onerror = function() {
|
|
|
|
dump('--runapp failed to disable lock-screen. Giving up.\n');
|
|
|
|
};
|
|
|
|
|
|
|
|
dump('--runapp found app: ' + app.manifest.name +
|
|
|
|
', disabling lock screen...\n');
|
2012-10-18 02:16:03 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getAllError: function() {
|
|
|
|
dump('Problem getting the list of all apps!');
|
|
|
|
}
|
2013-03-04 22:38:00 +04:00
|
|
|
};
|