b2g/chrome/content/runapp.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial