2012-06-21 14:06:24 -07: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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["WebappManager"];
|
2012-06-21 14:06:24 -07:00
|
|
|
|
|
|
|
let Cc = Components.classes;
|
|
|
|
let Ci = Components.interfaces;
|
|
|
|
let Cu = Components.utils;
|
|
|
|
|
2013-08-16 12:17:52 -07:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2012-06-21 14:06:24 -07:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Webapps.jsm");
|
2013-08-01 17:00:39 -07:00
|
|
|
Cu.import("resource://gre/modules/AppsUtils.jsm");
|
2014-03-15 14:37:37 -07:00
|
|
|
Cu.import("resource://gre/modules/NativeApp.jsm");
|
2012-07-16 11:22:16 -07:00
|
|
|
Cu.import("resource://gre/modules/WebappOSUtils.jsm");
|
2014-10-02 19:19:28 -07:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
2014-03-15 14:37:37 -07:00
|
|
|
Cu.import("resource://webapprt/modules/WebappRT.jsm");
|
2012-06-21 14:06:24 -07:00
|
|
|
|
2014-10-02 19:19:28 -07:00
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
this.WebappManager = {
|
2014-06-11 14:23:18 -07:00
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
let data = JSON.parse(aData);
|
|
|
|
data.mm = aSubject;
|
2012-06-21 14:06:24 -07:00
|
|
|
|
2014-06-11 14:23:18 -07:00
|
|
|
let chromeWin;
|
|
|
|
switch (aTopic) {
|
2012-06-21 14:06:24 -07:00
|
|
|
case "webapps-ask-install":
|
2014-06-11 14:23:18 -07:00
|
|
|
chromeWin = Services.wm.getOuterWindowWithId(data.oid);
|
2012-06-21 14:06:24 -07:00
|
|
|
if (chromeWin)
|
|
|
|
this.doInstall(data, chromeWin);
|
|
|
|
break;
|
2014-06-11 14:23:18 -07:00
|
|
|
case "webapps-ask-uninstall":
|
|
|
|
chromeWin = Services.wm.getOuterWindowWithId(data.windowId);
|
|
|
|
if (chromeWin) {
|
|
|
|
this.doUninstall(data, chromeWin);
|
|
|
|
}
|
|
|
|
break;
|
2012-06-21 14:06:24 -07:00
|
|
|
case "webapps-launch":
|
2012-07-16 11:22:16 -07:00
|
|
|
WebappOSUtils.launch(data);
|
2012-06-21 14:06:24 -07:00
|
|
|
break;
|
2012-08-08 18:04:48 -07:00
|
|
|
case "webapps-uninstall":
|
2014-12-30 20:04:00 -08:00
|
|
|
WebappOSUtils.uninstall(data).then(null, Cu.reportError);
|
2012-08-08 18:04:48 -07:00
|
|
|
break;
|
2012-06-21 14:06:24 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
update: function(aApp, aManifest, aZipPath) {
|
|
|
|
let nativeApp = new NativeApp(aApp, aManifest,
|
|
|
|
WebappRT.config.app.categories,
|
|
|
|
WebappRT.config.registryDir);
|
2014-07-04 06:23:16 -07:00
|
|
|
nativeApp.prepareUpdate(aApp, aManifest, aZipPath);
|
2014-03-15 14:37:37 -07:00
|
|
|
},
|
|
|
|
|
2012-06-21 14:06:24 -07:00
|
|
|
doInstall: function(data, window) {
|
2013-08-01 17:00:39 -07:00
|
|
|
let jsonManifest = data.isPackage ? data.app.updateManifest : data.app.manifest;
|
2014-07-30 14:00:15 -07:00
|
|
|
let manifest =
|
|
|
|
new ManifestHelper(jsonManifest, data.app.origin, data.app.manifestURL);
|
2013-08-01 17:00:39 -07:00
|
|
|
let name = manifest.name;
|
2012-06-21 14:06:24 -07:00
|
|
|
let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
|
|
|
|
|
|
|
|
let choice = Services.prompt.confirmEx(
|
|
|
|
window,
|
|
|
|
bundle.formatStringFromName("webapps.install.title", [name], 1),
|
|
|
|
bundle.formatStringFromName("webapps.install.description", [name], 1),
|
|
|
|
// Set both buttons to strings with the cancel button being default
|
|
|
|
Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
|
|
|
|
Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
|
|
|
|
Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
|
|
|
|
bundle.GetStringFromName("webapps.install.install"),
|
|
|
|
bundle.GetStringFromName("webapps.install.dontinstall"),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
{});
|
|
|
|
|
|
|
|
// Perform the install if the user allows it
|
2013-08-01 17:00:39 -07:00
|
|
|
if (choice == 0) {
|
2014-03-15 14:37:37 -07:00
|
|
|
let nativeApp = new NativeApp(data.app, jsonManifest,
|
2014-07-09 06:15:07 -07:00
|
|
|
data.app.categories,
|
2014-03-15 14:37:37 -07:00
|
|
|
WebappRT.config.registryDir);
|
|
|
|
let localDir;
|
|
|
|
try {
|
|
|
|
localDir = nativeApp.createProfile();
|
|
|
|
} catch (ex) {
|
2014-06-11 14:23:18 -07:00
|
|
|
DOMApplicationRegistry.denyInstall(data);
|
2014-03-15 14:37:37 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-08-01 17:00:39 -07:00
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
DOMApplicationRegistry.confirmInstall(data, localDir,
|
2014-07-11 04:25:46 -07:00
|
|
|
Task.async(function*(aApp, aManifest, aZipPath) {
|
|
|
|
yield nativeApp.install(aApp, aManifest, aZipPath);
|
|
|
|
})
|
2014-03-15 14:37:37 -07:00
|
|
|
);
|
2013-08-01 17:00:39 -07:00
|
|
|
} else {
|
2012-06-21 14:06:24 -07:00
|
|
|
DOMApplicationRegistry.denyInstall(data);
|
|
|
|
}
|
2013-08-16 12:17:52 -07:00
|
|
|
},
|
|
|
|
|
2014-06-11 14:23:18 -07:00
|
|
|
doUninstall: function(aData, aWindow) {
|
|
|
|
let jsonManifest = aData.isPackage ? aData.app.updateManifest : aData.app.manifest;
|
|
|
|
let manifest = new ManifestHelper(jsonManifest, aData.app.origin,
|
|
|
|
aData.app.manifestURL);
|
|
|
|
let name = manifest.name;
|
|
|
|
let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
|
|
|
|
|
|
|
|
let choice = Services.prompt.confirmEx(
|
|
|
|
aWindow,
|
|
|
|
bundle.formatStringFromName("webapps.uninstall.title", [name], 1),
|
|
|
|
bundle.formatStringFromName("webapps.uninstall.description", [name], 1),
|
|
|
|
// Set both buttons to strings with the cancel button being default
|
|
|
|
Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
|
|
|
|
Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
|
|
|
|
Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
|
|
|
|
bundle.GetStringFromName("webapps.uninstall.uninstall"),
|
|
|
|
bundle.GetStringFromName("webapps.uninstall.dontuninstall"),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
{});
|
|
|
|
|
|
|
|
// Perform the uninstall if the user allows it
|
|
|
|
if (choice == 0) {
|
|
|
|
DOMApplicationRegistry.confirmUninstall(aData).then((aApp) => {
|
2014-12-30 22:41:00 -08:00
|
|
|
WebappOSUtils.uninstall(aApp).then(null, Cu.reportError);
|
2014-06-11 14:23:18 -07:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
DOMApplicationRegistry.denyUninstall(aData);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-16 12:17:52 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference])
|
2012-06-21 14:06:24 -07:00
|
|
|
};
|
2013-08-16 12:17:52 -07:00
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
Services.obs.addObserver(WebappManager, "webapps-ask-install", false);
|
2014-06-11 14:23:18 -07:00
|
|
|
Services.obs.addObserver(WebappManager, "webapps-ask-uninstall", false);
|
2014-03-15 14:37:37 -07:00
|
|
|
Services.obs.addObserver(WebappManager, "webapps-launch", false);
|
|
|
|
Services.obs.addObserver(WebappManager, "webapps-uninstall", false);
|
|
|
|
Services.obs.addObserver(WebappManager, "webapps-update", false);
|
|
|
|
|
|
|
|
DOMApplicationRegistry.registerUpdateHandler(WebappManager.update);
|