2012-08-15 02:27:26 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
2013-08-16 23:17:52 +04:00
|
|
|
/* 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. */
|
2012-08-15 02:27:26 +04:00
|
|
|
|
2013-08-16 23:17:52 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ["startup"];
|
2012-08-15 02:27:26 +04:00
|
|
|
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
// Initialize DOMApplicationRegistry by importing Webapps.jsm.
|
|
|
|
Cu.import("resource://gre/modules/Webapps.jsm");
|
2013-08-16 23:17:52 +04:00
|
|
|
Cu.import("resource://gre/modules/AppsUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/PermissionsInstaller.jsm");
|
2013-09-27 22:38:14 +04:00
|
|
|
Cu.import('resource://gre/modules/Payment.jsm');
|
2013-08-16 23:17:52 +04:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2013-10-09 17:16:18 +04:00
|
|
|
Cu.import("resource://gre/modules/osfile.jsm");
|
2012-08-15 02:27:26 +04:00
|
|
|
|
|
|
|
// Initialize window-independent handling of webapps- notifications.
|
|
|
|
Cu.import("resource://webapprt/modules/WebappsHandler.jsm");
|
2013-08-16 23:17:52 +04:00
|
|
|
Cu.import("resource://webapprt/modules/WebappRT.jsm");
|
2013-10-11 19:06:21 +04:00
|
|
|
Cu.import("resource://webapprt/modules/WebRTCHandler.jsm");
|
2013-08-16 23:17:52 +04:00
|
|
|
|
2013-10-09 17:16:18 +04:00
|
|
|
const PROFILE_DIR = OS.Constants.Path.profileDir;
|
|
|
|
|
2013-08-16 23:17:52 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:16:18 +04:00
|
|
|
function writeFile(aPath, aData) {
|
|
|
|
return Task.spawn(function() {
|
|
|
|
let data = 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"),
|
|
|
|
brandPropertiesContent);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-16 23:17:52 +04:00
|
|
|
// 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")) {
|
|
|
|
deferredWindowLoad.resolve();
|
|
|
|
} else {
|
|
|
|
window.addEventListener("load", function onLoad() {
|
|
|
|
window.removeEventListener("load", onLoad, false);
|
|
|
|
deferredWindowLoad.resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for webapps registry loading.
|
2014-02-21 00:35:09 +04:00
|
|
|
yield DOMApplicationRegistry.registryStarted;
|
2013-08-16 23:17:52 +04:00
|
|
|
|
|
|
|
// Install/update permissions and get the appID from the webapps registry.
|
|
|
|
let appID = Ci.nsIScriptSecurityManager.NO_APP_ID;
|
|
|
|
let manifestURL = WebappRT.config.app.manifestURL;
|
|
|
|
if (manifestURL) {
|
|
|
|
appID = DOMApplicationRegistry.getAppLocalIdByManifestURL(manifestURL);
|
|
|
|
|
|
|
|
// On firstrun, set permissions to their default values.
|
|
|
|
// When the webapp runtime is updated, update the permissions.
|
|
|
|
// TODO: Update the permissions when the application is updated.
|
|
|
|
if (isFirstRunOrUpdate(Services.prefs)) {
|
|
|
|
PermissionsInstaller.installPermissions(WebappRT.config.app, true);
|
2013-10-09 17:16:18 +04:00
|
|
|
yield createBrandingFiles();
|
2013-08-16 23:17:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:16:18 +04:00
|
|
|
// Branding substitution
|
|
|
|
let aliasFile = Components.classes["@mozilla.org/file/local;1"]
|
|
|
|
.createInstance(Ci.nsIFile);
|
|
|
|
aliasFile.initWithPath(PROFILE_DIR);
|
|
|
|
|
|
|
|
let aliasURI = Services.io.newFileURI(aliasFile);
|
|
|
|
|
|
|
|
Services.io.getProtocolHandler("resource")
|
|
|
|
.QueryInterface(Ci.nsIResProtocolHandler)
|
|
|
|
.setSubstitution("webappbranding", aliasURI);
|
|
|
|
|
2013-08-16 23:17:52 +04:00
|
|
|
// Wait for XUL window loading
|
|
|
|
yield deferredWindowLoad.promise;
|
2012-08-15 02:27:26 +04:00
|
|
|
|
2013-08-16 23:17:52 +04:00
|
|
|
// Get the <browser> element in the webapp.xul window.
|
|
|
|
let appBrowser = window.document.getElementById("content");
|
2012-08-15 02:27:26 +04:00
|
|
|
|
2013-08-16 23:17:52 +04:00
|
|
|
// Set the principal to the correct appID and launch the application.
|
|
|
|
appBrowser.docShell.setIsApp(appID);
|
|
|
|
appBrowser.setAttribute("src", WebappRT.launchURI);
|
|
|
|
}).then(null, Cu.reportError.bind(Cu));
|
2012-08-15 02:27:26 +04:00
|
|
|
}
|