Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | "use strict"; |
michael@0 | 2 | |
michael@0 | 3 | // runapp.js: |
michael@0 | 4 | // Provide a --runapp APPNAME command-line option. |
michael@0 | 5 | |
michael@0 | 6 | let runAppObj; |
michael@0 | 7 | window.addEventListener('load', function() { |
michael@0 | 8 | if (!window.arguments) { |
michael@0 | 9 | return; |
michael@0 | 10 | } |
michael@0 | 11 | |
michael@0 | 12 | // Get the command line arguments that were passed to the b2g client |
michael@0 | 13 | let args = window.arguments[0].QueryInterface(Ci.nsICommandLine); |
michael@0 | 14 | let appname; |
michael@0 | 15 | |
michael@0 | 16 | // - Check if the argument is present before doing any work. |
michael@0 | 17 | try { |
michael@0 | 18 | // Returns null if the argument was not specified. Throws |
michael@0 | 19 | // NS_ERROR_INVALID_ARG if there is no parameter specified (because |
michael@0 | 20 | // it was the last argument or the next argument starts with '-'). |
michael@0 | 21 | // However, someone could still explicitly pass an empty argument! |
michael@0 | 22 | appname = args.handleFlagWithParam('runapp', false); |
michael@0 | 23 | } catch(e) { |
michael@0 | 24 | // treat a missing parameter like an empty parameter (=> show usage) |
michael@0 | 25 | appname = ''; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | // not specified, bail. |
michael@0 | 29 | if (appname === null) { |
michael@0 | 30 | return; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | runAppObj = new AppRunner(appname); |
michael@0 | 34 | Services.obs.addObserver(runAppObj, 'browser-ui-startup-complete', false); |
michael@0 | 35 | }); |
michael@0 | 36 | |
michael@0 | 37 | window.addEventListener('unload', function() { |
michael@0 | 38 | if (runAppObj) { |
michael@0 | 39 | Services.obs.removeObserver(runAppObj, 'browser-ui-startup-complete'); |
michael@0 | 40 | } |
michael@0 | 41 | }); |
michael@0 | 42 | |
michael@0 | 43 | function AppRunner(aName) { |
michael@0 | 44 | this._req = null; |
michael@0 | 45 | this._appName = aName; |
michael@0 | 46 | } |
michael@0 | 47 | AppRunner.prototype = { |
michael@0 | 48 | observe: function(aSubject, aTopic, aData) { |
michael@0 | 49 | if (aTopic == 'browser-ui-startup-complete') { |
michael@0 | 50 | this.doRunApp(); |
michael@0 | 51 | } |
michael@0 | 52 | }, |
michael@0 | 53 | |
michael@0 | 54 | doRunApp: function() { |
michael@0 | 55 | // - Get the list of apps since the parameter was specified |
michael@0 | 56 | this._req = navigator.mozApps.mgmt.getAll(); |
michael@0 | 57 | this._req.onsuccess = this.getAllSuccess.bind(this); |
michael@0 | 58 | this._req.onerror = this.getAllError.bind(this); |
michael@0 | 59 | }, |
michael@0 | 60 | |
michael@0 | 61 | getAllSuccess: function() { |
michael@0 | 62 | let apps = this._req.result; |
michael@0 | 63 | |
michael@0 | 64 | function findAppWithName(name) { |
michael@0 | 65 | let normalizedSearchName = name.replace(/[- ]+/g, '').toLowerCase(); |
michael@0 | 66 | |
michael@0 | 67 | for (let i = 0; i < apps.length; i++) { |
michael@0 | 68 | let app = apps[i]; |
michael@0 | 69 | let normalizedAppName = |
michael@0 | 70 | app.manifest.name.replace(/[- ]+/g, '').toLowerCase(); |
michael@0 | 71 | if (normalizedSearchName === normalizedAppName) { |
michael@0 | 72 | return app; |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | return null; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | function usageAndDie(justApps) { |
michael@0 | 79 | if (!justApps) |
michael@0 | 80 | dump( |
michael@0 | 81 | 'The --runapp argument specifies an app to automatically run at\n'+ |
michael@0 | 82 | 'startup. We match against app names per their manifest and \n' + |
michael@0 | 83 | 'ignoring capitalization, dashes, and whitespace.\n' + |
michael@0 | 84 | '\nThe system will load as usual except the lock screen will be ' + |
michael@0 | 85 | 'automatically be disabled.\n\n' + |
michael@0 | 86 | 'Known apps:\n'); |
michael@0 | 87 | |
michael@0 | 88 | for (let i = 0; i < apps.length; i++) { |
michael@0 | 89 | dump(' ' + apps[i].manifest.name + '\n'); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | // Exit the b2g client |
michael@0 | 93 | Services.startup.quit(Ci.nsIAppStartup.eAttemptQuit); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | if (this._appName === '') { |
michael@0 | 97 | usageAndDie(); |
michael@0 | 98 | return; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | let app = findAppWithName(this._appName); |
michael@0 | 102 | if (!app) { |
michael@0 | 103 | dump('Could not find app: "' + this._appName + '". Maybe you meant one of:\n'); |
michael@0 | 104 | usageAndDie(true); |
michael@0 | 105 | return; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | let setReq = |
michael@0 | 109 | navigator.mozSettings.createLock().set({'lockscreen.enabled': false}); |
michael@0 | 110 | setReq.onsuccess = function() { |
michael@0 | 111 | // give the event loop 100ms to disable the lock screen |
michael@0 | 112 | window.setTimeout(function() { |
michael@0 | 113 | dump('--runapp launching app: ' + app.manifest.name + '\n'); |
michael@0 | 114 | app.launch(); |
michael@0 | 115 | }, 100); |
michael@0 | 116 | }; |
michael@0 | 117 | setReq.onerror = function() { |
michael@0 | 118 | dump('--runapp failed to disable lock-screen. Giving up.\n'); |
michael@0 | 119 | }; |
michael@0 | 120 | |
michael@0 | 121 | dump('--runapp found app: ' + app.manifest.name + |
michael@0 | 122 | ', disabling lock screen...\n'); |
michael@0 | 123 | }, |
michael@0 | 124 | |
michael@0 | 125 | getAllError: function() { |
michael@0 | 126 | dump('Problem getting the list of all apps!'); |
michael@0 | 127 | } |
michael@0 | 128 | }; |