b2g/chrome/content/runapp.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/chrome/content/runapp.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     1.4 +"use strict";
     1.5 +
     1.6 +// runapp.js:
     1.7 +// Provide a --runapp APPNAME command-line option.
     1.8 +
     1.9 +let runAppObj;
    1.10 +window.addEventListener('load', function() {
    1.11 +  if (!window.arguments) {
    1.12 +    return;
    1.13 +  }
    1.14 +
    1.15 +  // Get the command line arguments that were passed to the b2g client
    1.16 +  let args = window.arguments[0].QueryInterface(Ci.nsICommandLine);
    1.17 +  let appname;
    1.18 +
    1.19 +  // - Check if the argument is present before doing any work.
    1.20 +  try {
    1.21 +    // Returns null if the argument was not specified.  Throws
    1.22 +    // NS_ERROR_INVALID_ARG if there is no parameter specified (because
    1.23 +    // it was the last argument or the next argument starts with '-').
    1.24 +    // However, someone could still explicitly pass an empty argument!
    1.25 +    appname = args.handleFlagWithParam('runapp', false);
    1.26 +  } catch(e) {
    1.27 +    // treat a missing parameter like an empty parameter (=> show usage)
    1.28 +    appname = '';
    1.29 +  }
    1.30 +
    1.31 +  // not specified, bail.
    1.32 +  if (appname === null) {
    1.33 +    return;
    1.34 +  }
    1.35 +
    1.36 +  runAppObj = new AppRunner(appname);
    1.37 +  Services.obs.addObserver(runAppObj, 'browser-ui-startup-complete', false);
    1.38 +});
    1.39 +
    1.40 +window.addEventListener('unload', function() {
    1.41 +  if (runAppObj) {
    1.42 +    Services.obs.removeObserver(runAppObj, 'browser-ui-startup-complete');
    1.43 +  }
    1.44 +});
    1.45 +
    1.46 +function AppRunner(aName) {
    1.47 +  this._req = null;
    1.48 +  this._appName = aName;
    1.49 +}
    1.50 +AppRunner.prototype = {
    1.51 +  observe: function(aSubject, aTopic, aData) {
    1.52 +    if (aTopic == 'browser-ui-startup-complete') {
    1.53 +      this.doRunApp();
    1.54 +    }
    1.55 +  },
    1.56 +
    1.57 +  doRunApp: function() {
    1.58 +    // - Get the list of apps since the parameter was specified
    1.59 +    this._req = navigator.mozApps.mgmt.getAll();
    1.60 +    this._req.onsuccess = this.getAllSuccess.bind(this);
    1.61 +    this._req.onerror = this.getAllError.bind(this);
    1.62 +  },
    1.63 +
    1.64 +  getAllSuccess: function() {
    1.65 +    let apps = this._req.result;
    1.66 +
    1.67 +    function findAppWithName(name) {
    1.68 +      let normalizedSearchName = name.replace(/[- ]+/g, '').toLowerCase();
    1.69 +
    1.70 +      for (let i = 0; i < apps.length; i++) {
    1.71 +        let app = apps[i];
    1.72 +        let normalizedAppName =
    1.73 +          app.manifest.name.replace(/[- ]+/g, '').toLowerCase();
    1.74 +        if (normalizedSearchName === normalizedAppName) {
    1.75 +          return app;
    1.76 +        }
    1.77 +      }
    1.78 +      return null;
    1.79 +    }
    1.80 +
    1.81 +    function usageAndDie(justApps) {
    1.82 +      if (!justApps)
    1.83 +        dump(
    1.84 +          'The --runapp argument specifies an app to automatically run at\n'+
    1.85 +          'startup.  We match against app names per their manifest and \n' +
    1.86 +          'ignoring capitalization, dashes, and whitespace.\n' +
    1.87 +          '\nThe system will load as usual except the lock screen will be ' +
    1.88 +          'automatically be disabled.\n\n' +
    1.89 +          'Known apps:\n');
    1.90 +
    1.91 +      for (let i = 0; i < apps.length; i++) {
    1.92 +        dump('  ' + apps[i].manifest.name + '\n');
    1.93 +      }
    1.94 +
    1.95 +      // Exit the b2g client
    1.96 +      Services.startup.quit(Ci.nsIAppStartup.eAttemptQuit);
    1.97 +    }
    1.98 +
    1.99 +    if (this._appName === '') {
   1.100 +      usageAndDie();
   1.101 +      return;
   1.102 +    }
   1.103 +
   1.104 +    let app = findAppWithName(this._appName);
   1.105 +    if (!app) {
   1.106 +      dump('Could not find app: "' + this._appName + '". Maybe you meant one of:\n');
   1.107 +      usageAndDie(true);
   1.108 +      return;
   1.109 +    }
   1.110 +
   1.111 +    let setReq =
   1.112 +      navigator.mozSettings.createLock().set({'lockscreen.enabled': false});
   1.113 +    setReq.onsuccess = function() {
   1.114 +      // give the event loop 100ms to disable the lock screen
   1.115 +      window.setTimeout(function() {
   1.116 +        dump('--runapp launching app: ' + app.manifest.name + '\n');
   1.117 +        app.launch();
   1.118 +      }, 100);
   1.119 +    };
   1.120 +    setReq.onerror = function() {
   1.121 +      dump('--runapp failed to disable lock-screen.  Giving up.\n');
   1.122 +    };
   1.123 +
   1.124 +    dump('--runapp found app: ' + app.manifest.name +
   1.125 +         ', disabling lock screen...\n');
   1.126 +  },
   1.127 +
   1.128 +  getAllError: function() {
   1.129 +    dump('Problem getting the list of all apps!');
   1.130 +  }
   1.131 +};

mercurial