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