2012-03-12 17:33:10 -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/. */
|
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["WebappManager"];
|
2012-03-12 17:33:10 -07:00
|
|
|
|
|
|
|
let Ci = Components.interfaces;
|
|
|
|
let Cc = Components.classes;
|
|
|
|
let Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Webapps.jsm");
|
2012-10-02 22:38:03 -07:00
|
|
|
Cu.import("resource://gre/modules/AppsUtils.jsm");
|
2013-08-27 05:50:22 -07:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
|
|
|
|
2014-03-24 06:37:27 -07:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "NativeApp",
|
|
|
|
"resource://gre/modules/NativeApp.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "WebappOSUtils",
|
|
|
|
"resource://gre/modules/WebappOSUtils.jsm");
|
|
|
|
|
2013-08-27 05:50:22 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
2012-03-12 17:33:10 -07:00
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
this.WebappManager = {
|
2013-09-15 08:45:02 -07:00
|
|
|
// List of promises for in-progress installations
|
|
|
|
installations: {},
|
2013-08-27 05:50:22 -07:00
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
init: function() {
|
2012-03-12 17:33:10 -07:00
|
|
|
Services.obs.addObserver(this, "webapps-ask-install", false);
|
2014-06-11 14:23:18 -07:00
|
|
|
Services.obs.addObserver(this, "webapps-ask-uninstall", false);
|
2012-03-12 17:33:10 -07:00
|
|
|
Services.obs.addObserver(this, "webapps-launch", false);
|
2012-08-08 18:04:48 -07:00
|
|
|
Services.obs.addObserver(this, "webapps-uninstall", false);
|
2013-09-15 08:45:02 -07:00
|
|
|
cpmm.addMessageListener("Webapps:Install:Return:OK", this);
|
|
|
|
cpmm.addMessageListener("Webapps:Install:Return:KO", this);
|
2013-10-17 05:47:58 -07:00
|
|
|
cpmm.addMessageListener("Webapps:UpdateState", this);
|
2012-03-12 17:33:10 -07:00
|
|
|
},
|
2012-10-02 22:38:03 -07:00
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
uninit: function() {
|
2012-03-12 17:33:10 -07:00
|
|
|
Services.obs.removeObserver(this, "webapps-ask-install");
|
2014-06-11 14:23:18 -07:00
|
|
|
Services.obs.removeObserver(this, "webapps-ask-uninstall");
|
2012-03-12 17:33:10 -07:00
|
|
|
Services.obs.removeObserver(this, "webapps-launch");
|
2012-08-08 18:04:48 -07:00
|
|
|
Services.obs.removeObserver(this, "webapps-uninstall");
|
2013-09-15 08:45:02 -07:00
|
|
|
cpmm.removeMessageListener("Webapps:Install:Return:OK", this);
|
|
|
|
cpmm.removeMessageListener("Webapps:Install:Return:KO", this);
|
2013-10-17 05:47:58 -07:00
|
|
|
cpmm.removeMessageListener("Webapps:UpdateState", this);
|
2013-08-27 05:50:22 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
let data = aMessage.data;
|
|
|
|
|
2013-09-15 08:45:02 -07:00
|
|
|
let manifestURL = data.manifestURL ||
|
|
|
|
(data.app && data.app.manifestURL) ||
|
|
|
|
data.manifest;
|
|
|
|
|
|
|
|
if (!this.installations[manifestURL]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-17 05:47:58 -07:00
|
|
|
if (aMessage.name == "Webapps:UpdateState") {
|
2013-09-15 08:45:02 -07:00
|
|
|
if (data.error) {
|
|
|
|
this.installations[manifestURL].reject(data.error);
|
2013-10-17 05:47:58 -07:00
|
|
|
} else if (data.app.installState == "installed") {
|
2013-09-15 08:45:02 -07:00
|
|
|
this.installations[manifestURL].resolve();
|
|
|
|
}
|
|
|
|
} else if (aMessage.name == "Webapps:Install:Return:OK" &&
|
|
|
|
!data.isPackage) {
|
2014-07-30 14:00:15 -07:00
|
|
|
let manifest = new ManifestHelper(data.app.manifest,
|
|
|
|
data.app.origin,
|
|
|
|
data.app.manifestURL);
|
2013-09-15 08:45:02 -07:00
|
|
|
if (!manifest.appcache_path) {
|
|
|
|
this.installations[manifestURL].resolve();
|
|
|
|
}
|
|
|
|
} else if (aMessage.name == "Webapps:Install:Return:KO") {
|
|
|
|
this.installations[manifestURL].reject(data.error);
|
2013-08-27 05:50:22 -07:00
|
|
|
}
|
2012-03-12 17:33:10 -07:00
|
|
|
},
|
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
observe: function(aSubject, aTopic, aData) {
|
2012-03-12 17:33:10 -07:00
|
|
|
let data = JSON.parse(aData);
|
2012-09-18 10:34:55 -07:00
|
|
|
data.mm = aSubject;
|
2012-03-12 17:33:10 -07:00
|
|
|
|
2014-06-11 14:23:18 -07:00
|
|
|
let win;
|
2012-03-12 17:33:10 -07:00
|
|
|
switch(aTopic) {
|
|
|
|
case "webapps-ask-install":
|
2014-06-11 14:23:18 -07:00
|
|
|
win = this._getWindowForId(data.oid);
|
2013-08-05 13:30:41 -07:00
|
|
|
if (win && win.location.href == data.from) {
|
|
|
|
this.doInstall(data, win);
|
|
|
|
}
|
2012-03-12 17:33:10 -07:00
|
|
|
break;
|
2014-06-11 14:23:18 -07:00
|
|
|
case "webapps-ask-uninstall":
|
|
|
|
win = this._getWindowForId(data.windowId);
|
|
|
|
if (win && win.location.href == data.from) {
|
|
|
|
this.doUninstall(data, win);
|
|
|
|
}
|
|
|
|
break;
|
2012-03-12 17:33:10 -07:00
|
|
|
case "webapps-launch":
|
2012-07-16 11:22:16 -07:00
|
|
|
WebappOSUtils.launch(data);
|
2012-03-12 17:33:10 -07:00
|
|
|
break;
|
2012-08-08 18:04:48 -07:00
|
|
|
case "webapps-uninstall":
|
|
|
|
WebappOSUtils.uninstall(data);
|
|
|
|
break;
|
2012-03-12 17:33:10 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-05 13:30:41 -07:00
|
|
|
_getWindowForId: function(aId) {
|
|
|
|
let someWindow = Services.wm.getMostRecentWindow(null);
|
2013-08-08 10:31:58 -07:00
|
|
|
return someWindow && Services.wm.getOuterWindowWithId(aId);
|
2013-08-05 13:30:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
doInstall: function(aData, aWindow) {
|
|
|
|
let browser = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShell)
|
|
|
|
.chromeEventHandler;
|
2013-09-09 05:57:37 -07:00
|
|
|
let chromeDoc = browser.ownerDocument;
|
|
|
|
let chromeWin = chromeDoc.defaultView;
|
|
|
|
let popupProgressContent =
|
|
|
|
chromeDoc.getElementById("webapps-install-progress-content");
|
|
|
|
|
2013-08-05 13:30:41 -07:00
|
|
|
let bundle = chromeWin.gNavigatorBundle;
|
2012-03-12 17:33:10 -07:00
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
let jsonManifest = aData.isPackage ? aData.app.updateManifest : aData.app.manifest;
|
|
|
|
|
2013-09-09 05:57:37 -07:00
|
|
|
let notification;
|
|
|
|
|
2012-03-12 17:33:10 -07:00
|
|
|
let mainAction = {
|
|
|
|
label: bundle.getString("webapps.install"),
|
|
|
|
accessKey: bundle.getString("webapps.install.accesskey"),
|
2013-08-27 05:50:22 -07:00
|
|
|
callback: () => {
|
2013-09-09 05:57:37 -07:00
|
|
|
notification.remove();
|
|
|
|
|
|
|
|
notification = chromeWin.PopupNotifications.
|
|
|
|
show(browser,
|
|
|
|
"webapps-install-progress",
|
|
|
|
bundle.getString("webapps.install.inprogress"),
|
|
|
|
"webapps-notification-icon");
|
|
|
|
|
|
|
|
let progressMeter = chromeDoc.createElement("progressmeter");
|
|
|
|
progressMeter.setAttribute("mode", "undetermined");
|
|
|
|
popupProgressContent.appendChild(progressMeter);
|
|
|
|
|
2013-08-27 05:50:22 -07:00
|
|
|
let manifestURL = aData.app.manifestURL;
|
2013-09-15 08:45:02 -07:00
|
|
|
|
2014-07-11 04:25:46 -07:00
|
|
|
let nativeApp = new NativeApp(aData.app, jsonManifest,
|
|
|
|
aData.app.categories);
|
|
|
|
|
|
|
|
this.installations[manifestURL] = Promise.defer();
|
|
|
|
this.installations[manifestURL].promise.then(() => {
|
|
|
|
notifyInstallSuccess(aData.app, nativeApp, bundle);
|
|
|
|
}, (error) => {
|
|
|
|
Cu.reportError("Error installing webapp: " + error);
|
|
|
|
}).then(() => {
|
2013-09-15 08:45:02 -07:00
|
|
|
popupProgressContent.removeChild(progressMeter);
|
|
|
|
delete this.installations[manifestURL];
|
|
|
|
if (Object.getOwnPropertyNames(this.installations).length == 0) {
|
|
|
|
notification.remove();
|
|
|
|
}
|
|
|
|
});
|
2013-08-27 05:50:22 -07:00
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
let localDir;
|
|
|
|
try {
|
|
|
|
localDir = nativeApp.createProfile();
|
|
|
|
} catch (ex) {
|
2012-04-17 07:17:23 -07:00
|
|
|
DOMApplicationRegistry.denyInstall(aData);
|
2014-03-15 14:37:37 -07:00
|
|
|
return;
|
2012-04-17 07:17:23 -07:00
|
|
|
}
|
2014-03-15 14:37:37 -07:00
|
|
|
|
|
|
|
DOMApplicationRegistry.confirmInstall(aData, localDir,
|
2014-07-11 04:25:46 -07:00
|
|
|
Task.async(function*(aApp, aManifest, aZipPath) {
|
2014-03-15 14:37:37 -07:00
|
|
|
try {
|
2014-07-04 06:23:16 -07:00
|
|
|
yield nativeApp.install(aApp, aManifest, aZipPath);
|
2014-03-15 14:37:37 -07:00
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError("Error installing webapp: " + ex);
|
2014-07-11 04:25:46 -07:00
|
|
|
throw ex;
|
2014-03-15 14:37:37 -07:00
|
|
|
}
|
2014-07-11 04:25:46 -07:00
|
|
|
})
|
2014-03-15 14:37:37 -07:00
|
|
|
);
|
2012-03-12 17:33:10 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-05 13:30:41 -07:00
|
|
|
let requestingURI = chromeWin.makeURI(aData.from);
|
2014-07-30 14:00:15 -07:00
|
|
|
let app = aData.app;
|
|
|
|
let manifest = new ManifestHelper(jsonManifest, app.origin, app.manifestURL);
|
2012-03-12 17:33:10 -07:00
|
|
|
|
|
|
|
let host;
|
|
|
|
try {
|
|
|
|
host = requestingURI.host;
|
|
|
|
} catch(e) {
|
|
|
|
host = requestingURI.spec;
|
|
|
|
}
|
|
|
|
|
|
|
|
let message = bundle.getFormattedString("webapps.requestInstall",
|
2012-08-03 03:14:08 -07:00
|
|
|
[manifest.name, host], 2);
|
2012-07-14 01:01:07 -07:00
|
|
|
|
2013-09-09 05:57:37 -07:00
|
|
|
notification = chromeWin.PopupNotifications.show(browser,
|
|
|
|
"webapps-install",
|
|
|
|
message,
|
|
|
|
"webapps-notification-icon",
|
|
|
|
mainAction);
|
2012-07-14 01:01:07 -07:00
|
|
|
|
2014-06-11 14:23:18 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
doUninstall: function(aData, aWindow) {
|
|
|
|
let browser = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShell)
|
|
|
|
.chromeEventHandler;
|
|
|
|
let chromeDoc = browser.ownerDocument;
|
|
|
|
let chromeWin = chromeDoc.defaultView;
|
|
|
|
|
|
|
|
let bundle = chromeWin.gNavigatorBundle;
|
|
|
|
let jsonManifest = aData.app.manifest;
|
|
|
|
|
|
|
|
let notification;
|
|
|
|
|
|
|
|
let mainAction = {
|
|
|
|
label: bundle.getString("webapps.uninstall"),
|
|
|
|
accessKey: bundle.getString("webapps.uninstall.accesskey"),
|
|
|
|
callback: () => {
|
|
|
|
notification.remove();
|
|
|
|
DOMApplicationRegistry.confirmUninstall(aData);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let secondaryAction = {
|
|
|
|
label: bundle.getString("webapps.doNotUninstall"),
|
|
|
|
accessKey: bundle.getString("webapps.doNotUninstall.accesskey"),
|
|
|
|
callback: () => {
|
|
|
|
notification.remove();
|
|
|
|
DOMApplicationRegistry.denyUninstall(aData, "USER_DECLINED");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let manifest = new ManifestHelper(jsonManifest, aData.app.origin,
|
|
|
|
aData.app.manifestURL);
|
|
|
|
|
|
|
|
let message = bundle.getFormattedString("webapps.requestUninstall",
|
|
|
|
[manifest.name]);
|
|
|
|
|
|
|
|
notification = chromeWin.PopupNotifications.show(
|
|
|
|
browser, "webapps-uninstall", message,
|
|
|
|
"webapps-notification-icon",
|
|
|
|
mainAction, [secondaryAction]);
|
2012-07-14 01:01:07 -07:00
|
|
|
}
|
|
|
|
}
|
2012-07-14 01:01:07 -07:00
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
function notifyInstallSuccess(aApp, aNativeApp, aBundle) {
|
2013-07-30 18:37:00 -07:00
|
|
|
let launcher = {
|
|
|
|
observe: function(aSubject, aTopic) {
|
|
|
|
if (aTopic == "alertclickcallback") {
|
2014-03-15 14:37:37 -07:00
|
|
|
WebappOSUtils.launch(aApp);
|
2013-07-30 18:37:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-27 05:50:22 -07:00
|
|
|
try {
|
|
|
|
let notifier = Cc["@mozilla.org/alerts-service;1"].
|
|
|
|
getService(Ci.nsIAlertsService);
|
2012-07-14 01:01:07 -07:00
|
|
|
|
2014-03-15 14:37:37 -07:00
|
|
|
notifier.showAlertNotification(aNativeApp.iconURI.spec,
|
2013-08-27 05:50:22 -07:00
|
|
|
aBundle.getString("webapps.install.success"),
|
2014-03-15 14:37:37 -07:00
|
|
|
aNativeApp.appNameAsFilename,
|
2013-08-27 05:50:22 -07:00
|
|
|
true, null, launcher);
|
|
|
|
} catch (ex) {}
|
2012-07-14 01:01:07 -07:00
|
|
|
}
|