author Nick Alexander <nalexander@mozilla.com>
Wed, 27 Jan 2016 15:28:31 -0800
changeset 321547 f66289e50d72401059c907080a3e56e52c2b390c
parent 256423 afbba6c11b19549da1ba20be0806e0c93ee9cccb
child 322683 55ecf767b62d74c1337d3209e9df13aed1205c92
permissions -rw-r--r--
Bug 1234629 - Part 1: Create bouncer APK for OTA distribution installs. r=margaret,gps This commit produces an "install bouncer" APK which is a "hollow shell" that looks like the main Fennec APK. In particular, both APKs have: * the same Android package name (application id); and * the same set of <permission>, <uses-permission>, and <uses-feature> blocks in their manifests. The bouncer APK must always have an android:versionCode smaller than the main Fennec APK; for now, we will just bump that manually mobile/android/bouncer/moz.build.

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

/* This module is imported at the startup of an application.  It takes care of
 * permissions installation, application url loading, security settings.  Put
 * stuff here that you want to happen once on startup before the webapp is
 * loaded.  */

this.EXPORTED_SYMBOLS = ["startup"];

const Ci = Components.interfaces;
const Cu = Components.utils;

/* We load here modules that are needed to perform the application startup.
 * We lazily load modules that aren't needed on every startup.
 * We load modules that aren't used here but that need to perform some
 * initialization steps later in the startup function. */



// Lazily load these modules because we don't need them at every
// startup, but only during first run or runtime update.

XPCOMUtils.defineLazyModuleGetter(this, "PermissionsInstaller",

const PROFILE_DIR = OS.Constants.Path.profileDir;

function isFirstRunOrUpdate() {
  let savedBuildID = null;
  try {
    savedBuildID = Services.prefs.getCharPref("webapprt.buildID");
  } catch (e) {}

  let ourBuildID = Services.appinfo.platformBuildID;

  if (ourBuildID != savedBuildID) {
    Services.prefs.setCharPref("webapprt.buildID", ourBuildID);
    return true;

  return false;

function writeFile(aPath, aData) {
  return Task.spawn(function() {
    let data = new TextEncoder().encode(aData);
    yield OS.File.writeAtomic(aPath, data, { tmpPath: aPath + ".tmp" });

function createBrandingFiles() {
  return Task.spawn(function() {
    let manifest = WebappRT.localeManifest;
    let name = WebappRT.localeManifest.name;
    let developer = " ";
    if (WebappRT.localeManifest.developer) {
      developer = WebappRT.localeManifest.developer.name;

    let brandDTDContent = '<!ENTITY brandShortName "' + name + '">\n\
  <!ENTITY brandFullName "' + name + '">\n\
  <!ENTITY vendorShortName "' + developer + '">\n\
  <!ENTITY trademarkInfo.part1 " ">';

    yield writeFile(OS.Path.join(PROFILE_DIR, "brand.dtd"), brandDTDContent);

    let brandPropertiesContent = 'brandShortName=' + name + '\n\
  brandFullName=' + name + '\n\
  vendorShortName=' + developer;

    yield writeFile(OS.Path.join(PROFILE_DIR, "brand.properties"),

// Observes all the events needed to actually launch an application.
// It waits for XUL window and webapps registry loading.
this.startup = function(window) {
  return Task.spawn(function () {
    // Observe XUL window loading.
    // For tests, it could be already loaded.
    let deferredWindowLoad = Promise.defer();
    if (window.document && window.document.getElementById("content")) {
    } else {
      window.addEventListener("DOMContentLoaded", function onLoad() {
        window.removeEventListener("DOMContentLoaded", onLoad, false);

    let appUpdated = false;
    let updatePending = yield WebappRT.isUpdatePending();
    if (updatePending) {
      appUpdated = yield WebappRT.applyUpdate();

    yield WebappRT.configPromise;

    let appData = WebappRT.config.app;

    // Initialize DOMApplicationRegistry by importing Webapps.jsm.
    // Initialize window-independent handling of webapps- notifications.

    // Wait for webapps registry loading.
    yield DOMApplicationRegistry.registryStarted;
    // Add the currently running app to the registry.
    yield DOMApplicationRegistry.addInstalledApp(appData, appData.manifest,

    let manifestURL = appData.manifestURL;
    if (manifestURL) {
      // On firstrun, set permissions to their default values.
      // When the webapp runtime is updated, update the permissions.
      if (isFirstRunOrUpdate(Services.prefs) || appUpdated) {
        PermissionsInstaller.installPermissions(appData, true);
        yield createBrandingFiles();

    // Branding substitution
    let aliasFile = Components.classes["@mozilla.org/file/local;1"]

    let aliasURI = Services.io.newFileURI(aliasFile);

               .setSubstitution("webappbranding", aliasURI);

    // Wait for XUL window loading
    yield deferredWindowLoad.promise;

    // Load these modules here because they aren't needed right at startup,
    // but they need to be loaded to perform some initialization steps.

    // Get the <browser> element in the webapp.xul window.
    let appBrowser = window.document.getElementById("content");

    // Set the principal to the correct appID and launch the application.
    appBrowser.setAttribute("src", WebappRT.launchURI);

    if (appData.manifest.fullscreen) {
      appBrowser.addEventListener("load", function onLoad() {
        appBrowser.removeEventListener("load", onLoad, true);
      }, true);