2012-03-13 00:33:10 +00: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 21:37:37 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["WebappManager"];
|
2012-03-13 00:33:10 +00:00
|
|
|
|
2015-09-15 18:19:45 +00:00
|
|
|
var Ci = Components.interfaces;
|
|
|
|
var Cc = Components.classes;
|
|
|
|
var Cu = Components.utils;
|
2012-03-13 00:33:10 +00:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Webapps.jsm");
|
2012-10-03 05:38:03 +00:00
|
|
|
Cu.import("resource://gre/modules/AppsUtils.jsm");
|
2013-08-27 12:50:22 +00:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2014-11-28 19:08:29 +00:00
|
|
|
Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
|
2013-08-27 12:50:22 +00:00
|
|
|
|
2014-03-24 13:37:27 +00:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "NativeApp",
|
|
|
|
"resource://gre/modules/NativeApp.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "WebappOSUtils",
|
|
|
|
"resource://gre/modules/WebappOSUtils.jsm");
|
|
|
|
|
2013-08-27 12:50:22 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
2012-03-13 00:33:10 +00:00
|
|
|
|
2014-03-15 21:37:37 +00:00
|
|
|
this.WebappManager = {
|
2013-09-15 15:45:02 +00:00
|
|
|
// List of promises for in-progress installations
|
|
|
|
installations: {},
|
2013-08-27 12:50:22 +00:00
|
|
|
|
2014-03-15 21:37:37 +00:00
|
|
|
init: function() {
|
2012-03-13 00:33:10 +00:00
|
|
|
Services.obs.addObserver(this, "webapps-ask-install", false);
|
2014-06-11 21:23:18 +00:00
|
|
|
Services.obs.addObserver(this, "webapps-ask-uninstall", false);
|
2012-03-13 00:33:10 +00:00
|
|
|
Services.obs.addObserver(this, "webapps-launch", false);
|
2012-08-09 01:04:48 +00:00
|
|
|
Services.obs.addObserver(this, "webapps-uninstall", false);
|
2015-01-27 23:32:40 +00:00
|
|
|
cpmm.sendAsyncMessage("Webapps:RegisterForMessages",
|
|
|
|
{ messages: ["Webapps:Install:Return:OK",
|
|
|
|
"Webapps:Install:Return:KO",
|
|
|
|
"Webapps:UpdateState"]});
|
2013-09-15 15:45:02 +00:00
|
|
|
cpmm.addMessageListener("Webapps:Install:Return:OK", this);
|
|
|
|
cpmm.addMessageListener("Webapps:Install:Return:KO", this);
|
2013-10-17 12:47:58 +00:00
|
|
|
cpmm.addMessageListener("Webapps:UpdateState", this);
|
2012-03-13 00:33:10 +00:00
|
|
|
},
|
2012-10-03 05:38:03 +00:00
|
|
|
|
2014-03-15 21:37:37 +00:00
|
|
|
uninit: function() {
|
2012-03-13 00:33:10 +00:00
|
|
|
Services.obs.removeObserver(this, "webapps-ask-install");
|
2014-06-11 21:23:18 +00:00
|
|
|
Services.obs.removeObserver(this, "webapps-ask-uninstall");
|
2012-03-13 00:33:10 +00:00
|
|
|
Services.obs.removeObserver(this, "webapps-launch");
|
2012-08-09 01:04:48 +00:00
|
|
|
Services.obs.removeObserver(this, "webapps-uninstall");
|
2015-01-27 23:32:40 +00:00
|
|
|
cpmm.sendAsyncMessage("Webapps:UnregisterForMessages",
|
|
|
|
["Webapps:Install:Return:OK",
|
|
|
|
"Webapps:Install:Return:KO",
|
|
|
|
"Webapps:UpdateState"]);
|
2013-09-15 15:45:02 +00:00
|
|
|
cpmm.removeMessageListener("Webapps:Install:Return:OK", this);
|
|
|
|
cpmm.removeMessageListener("Webapps:Install:Return:KO", this);
|
2013-10-17 12:47:58 +00:00
|
|
|
cpmm.removeMessageListener("Webapps:UpdateState", this);
|
2013-08-27 12:50:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
let data = aMessage.data;
|
|
|
|
|
2013-09-15 15:45:02 +00:00
|
|
|
let manifestURL = data.manifestURL ||
|
|
|
|
(data.app && data.app.manifestURL) ||
|
|
|
|
data.manifest;
|
|
|
|
|
|
|
|
if (!this.installations[manifestURL]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-17 12:47:58 +00:00
|
|
|
if (aMessage.name == "Webapps:UpdateState") {
|
2013-09-15 15:45:02 +00:00
|
|
|
if (data.error) {
|
|
|
|
this.installations[manifestURL].reject(data.error);
|
2013-10-17 12:47:58 +00:00
|
|
|
} else if (data.app.installState == "installed") {
|
2013-09-15 15:45:02 +00:00
|
|
|
this.installations[manifestURL].resolve();
|
|
|
|
}
|
|
|
|
} else if (aMessage.name == "Webapps:Install:Return:OK" &&
|
|
|
|
!data.isPackage) {
|
2014-07-30 21:00:15 +00:00
|
|
|
let manifest = new ManifestHelper(data.app.manifest,
|
|
|
|
data.app.origin,
|
|
|
|
data.app.manifestURL);
|
2013-09-15 15:45:02 +00: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 12:50:22 +00:00
|
|
|
}
|
2012-03-13 00:33:10 +00:00
|
|
|
},
|
|
|
|
|
2014-03-15 21:37:37 +00:00
|
|
|
observe: function(aSubject, aTopic, aData) {
|
2012-03-13 00:33:10 +00:00
|
|
|
let data = JSON.parse(aData);
|
2012-09-18 17:34:55 +00:00
|
|
|
data.mm = aSubject;
|
2012-03-13 00:33:10 +00:00
|
|
|
|
2015-01-27 23:32:40 +00:00
|
|
|
let browser;
|
2012-03-13 00:33:10 +00:00
|
|
|
switch(aTopic) {
|
|
|
|
case "webapps-ask-install":
|
2015-01-27 23:32:40 +00:00
|
|
|
browser = this._getBrowserForId(data.topId);
|
|
|
|
if (browser) {
|
|
|
|
this.doInstall(data, browser);
|
2013-08-05 20:30:41 +00:00
|
|
|
}
|
2012-03-13 00:33:10 +00:00
|
|
|
break;
|
2014-06-11 21:23:18 +00:00
|
|
|
case "webapps-ask-uninstall":
|
2015-01-27 23:32:40 +00:00
|
|
|
browser = this._getBrowserForId(data.topId);
|
|
|
|
if (browser) {
|
|
|
|
this.doUninstall(data, browser);
|
2014-06-11 21:23:18 +00:00
|
|
|
}
|
|
|
|
break;
|
2012-03-13 00:33:10 +00:00
|
|
|
case "webapps-launch":
|
2012-07-16 18:22:16 +00:00
|
|
|
WebappOSUtils.launch(data);
|
2012-03-13 00:33:10 +00:00
|
|
|
break;
|
2012-08-09 01:04:48 +00:00
|
|
|
case "webapps-uninstall":
|
|
|
|
WebappOSUtils.uninstall(data);
|
|
|
|
break;
|
2012-03-13 00:33:10 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-01-27 23:32:40 +00:00
|
|
|
_getBrowserForId: function(aId) {
|
|
|
|
let windows = Services.wm.getEnumerator("navigator:browser");
|
|
|
|
while (windows.hasMoreElements()) {
|
|
|
|
let window = windows.getNext();
|
|
|
|
let tabbrowser = window.gBrowser;
|
|
|
|
let foundBrowser = tabbrowser.getBrowserForOuterWindowID(aId);
|
|
|
|
if (foundBrowser) {
|
|
|
|
return foundBrowser;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let foundWindow = Services.wm.getOuterWindowWithId(aId);
|
|
|
|
if (foundWindow) {
|
|
|
|
return foundWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShell)
|
|
|
|
.chromeEventHandler;
|
|
|
|
}
|
|
|
|
return null;
|
2013-08-05 20:30:41 +00:00
|
|
|
},
|
|
|
|
|
2015-01-27 23:32:40 +00:00
|
|
|
doInstall: function(aData, aBrowser) {
|
|
|
|
let chromeDoc = aBrowser.ownerDocument;
|
2013-09-09 12:57:37 +00:00
|
|
|
let chromeWin = chromeDoc.defaultView;
|
|
|
|
let popupProgressContent =
|
|
|
|
chromeDoc.getElementById("webapps-install-progress-content");
|
|
|
|
|
2013-08-05 20:30:41 +00:00
|
|
|
let bundle = chromeWin.gNavigatorBundle;
|
2012-03-13 00:33:10 +00:00
|
|
|
|
2014-03-15 21:37:37 +00:00
|
|
|
let jsonManifest = aData.isPackage ? aData.app.updateManifest : aData.app.manifest;
|
|
|
|
|
2013-09-09 12:57:37 +00:00
|
|
|
let notification;
|
|
|
|
|
2012-03-13 00:33:10 +00:00
|
|
|
let mainAction = {
|
|
|
|
label: bundle.getString("webapps.install"),
|
|
|
|
accessKey: bundle.getString("webapps.install.accesskey"),
|
2013-08-27 12:50:22 +00:00
|
|
|
callback: () => {
|
2013-09-09 12:57:37 +00:00
|
|
|
notification.remove();
|
|
|
|
|
|
|
|
notification = chromeWin.PopupNotifications.
|
2015-01-27 23:32:40 +00:00
|
|
|
show(aBrowser,
|
2013-09-09 12:57:37 +00:00
|
|
|
"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 12:50:22 +00:00
|
|
|
let manifestURL = aData.app.manifestURL;
|
2013-09-15 15:45:02 +00:00
|
|
|
|
2014-07-11 11:25:46 +00:00
|
|
|
let nativeApp = new NativeApp(aData.app, jsonManifest,
|
|
|
|
aData.app.categories);
|
|
|
|
|
|
|
|
this.installations[manifestURL] = Promise.defer();
|
|
|
|
this.installations[manifestURL].promise.then(() => {
|
2014-11-28 19:08:29 +00:00
|
|
|
notifyInstallSuccess(aData.app, nativeApp, bundle,
|
2015-01-27 23:32:40 +00:00
|
|
|
PrivateBrowsingUtils.isBrowserPrivate(aBrowser));
|
2014-07-11 11:25:46 +00:00
|
|
|
}, (error) => {
|
|
|
|
Cu.reportError("Error installing webapp: " + error);
|
|
|
|
}).then(() => {
|
2013-09-15 15:45:02 +00:00
|
|
|
popupProgressContent.removeChild(progressMeter);
|
|
|
|
delete this.installations[manifestURL];
|
|
|
|
if (Object.getOwnPropertyNames(this.installations).length == 0) {
|
|
|
|
notification.remove();
|
|
|
|
}
|
|
|
|
});
|
2013-08-27 12:50:22 +00:00
|
|
|
|
2014-03-15 21:37:37 +00:00
|
|
|
let localDir;
|
|
|
|
try {
|
|
|
|
localDir = nativeApp.createProfile();
|
|
|
|
} catch (ex) {
|
2012-04-17 14:17:23 +00:00
|
|
|
DOMApplicationRegistry.denyInstall(aData);
|
2014-03-15 21:37:37 +00:00
|
|
|
return;
|
2012-04-17 14:17:23 +00:00
|
|
|
}
|
2014-03-15 21:37:37 +00:00
|
|
|
|
|
|
|
DOMApplicationRegistry.confirmInstall(aData, localDir,
|
2014-07-11 11:25:46 +00:00
|
|
|
Task.async(function*(aApp, aManifest, aZipPath) {
|
2014-03-15 21:37:37 +00:00
|
|
|
try {
|
2014-07-04 13:23:16 +00:00
|
|
|
yield nativeApp.install(aApp, aManifest, aZipPath);
|
2014-03-15 21:37:37 +00:00
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError("Error installing webapp: " + ex);
|
2014-07-11 11:25:46 +00:00
|
|
|
throw ex;
|
2014-03-15 21:37:37 +00:00
|
|
|
}
|
2014-07-11 11:25:46 +00:00
|
|
|
})
|
2014-03-15 21:37:37 +00:00
|
|
|
);
|
2012-03-13 00:33:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-05 20:30:41 +00:00
|
|
|
let requestingURI = chromeWin.makeURI(aData.from);
|
2014-07-30 21:00:15 +00:00
|
|
|
let app = aData.app;
|
|
|
|
let manifest = new ManifestHelper(jsonManifest, app.origin, app.manifestURL);
|
2012-03-13 00:33:10 +00:00
|
|
|
|
2015-07-16 04:26:54 +00:00
|
|
|
let options = {
|
|
|
|
displayURI: requestingURI,
|
|
|
|
};
|
2012-03-13 00:33:10 +00:00
|
|
|
|
2015-05-27 10:45:49 +00:00
|
|
|
let message = bundle.getFormattedString("webapps.requestInstall2",
|
|
|
|
[manifest.name]);
|
2012-07-14 08:01:07 +00:00
|
|
|
|
2015-02-11 16:46:50 +00:00
|
|
|
let gBrowser = chromeWin.gBrowser;
|
|
|
|
if (gBrowser) {
|
|
|
|
let windowID = aData.oid;
|
|
|
|
|
|
|
|
let listener = {
|
|
|
|
onLocationChange(webProgress) {
|
|
|
|
if (webProgress.DOMWindowID == windowID) {
|
|
|
|
notification.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
gBrowser.addProgressListener(listener);
|
|
|
|
|
2015-05-27 10:45:49 +00:00
|
|
|
options.eventCallback = event => {
|
2015-02-11 16:46:50 +00:00
|
|
|
if (event != "removed") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The notification was removed, so we should
|
|
|
|
// remove our listener.
|
|
|
|
gBrowser.removeProgressListener(listener);
|
2015-05-27 10:45:49 +00:00
|
|
|
};
|
2015-02-11 16:46:50 +00:00
|
|
|
}
|
|
|
|
|
2015-01-27 23:32:40 +00:00
|
|
|
notification = chromeWin.PopupNotifications.show(aBrowser,
|
2013-09-09 12:57:37 +00:00
|
|
|
"webapps-install",
|
|
|
|
message,
|
|
|
|
"webapps-notification-icon",
|
2015-02-11 16:46:50 +00:00
|
|
|
mainAction, [],
|
2015-05-27 10:45:49 +00:00
|
|
|
options);
|
2014-06-11 21:23:18 +00:00
|
|
|
},
|
|
|
|
|
2015-01-27 23:32:40 +00:00
|
|
|
doUninstall: function(aData, aBrowser) {
|
|
|
|
let chromeDoc = aBrowser.ownerDocument;
|
2014-06-11 21:23:18 +00:00
|
|
|
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]);
|
|
|
|
|
2015-02-11 16:46:50 +00:00
|
|
|
|
2015-05-27 10:45:49 +00:00
|
|
|
let options = {};
|
2015-02-11 16:46:50 +00:00
|
|
|
let gBrowser = chromeWin.gBrowser;
|
|
|
|
if (gBrowser) {
|
|
|
|
let windowID = aData.oid;
|
|
|
|
|
|
|
|
let listener = {
|
|
|
|
onLocationChange(webProgress) {
|
|
|
|
if (webProgress.DOMWindowID == windowID) {
|
|
|
|
notification.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
gBrowser.addProgressListener(listener);
|
|
|
|
|
2015-05-27 10:45:49 +00:00
|
|
|
options.eventCallback = event => {
|
2015-02-11 16:46:50 +00:00
|
|
|
if (event != "removed") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The notification was removed, so we should
|
|
|
|
// remove our listener.
|
|
|
|
gBrowser.removeProgressListener(listener);
|
2015-05-27 10:45:49 +00:00
|
|
|
};
|
2015-02-11 16:46:50 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 21:23:18 +00:00
|
|
|
notification = chromeWin.PopupNotifications.show(
|
2015-01-27 23:32:40 +00:00
|
|
|
aBrowser, "webapps-uninstall", message,
|
2014-06-11 21:23:18 +00:00
|
|
|
"webapps-notification-icon",
|
2015-02-11 16:46:50 +00:00
|
|
|
mainAction, [secondaryAction],
|
2015-05-27 10:45:49 +00:00
|
|
|
options);
|
2012-07-14 08:01:07 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-14 08:01:07 +00:00
|
|
|
|
2014-11-28 19:08:29 +00:00
|
|
|
function notifyInstallSuccess(aApp, aNativeApp, aBundle, aInPrivateBrowsing) {
|
2013-07-31 01:37:00 +00:00
|
|
|
let launcher = {
|
|
|
|
observe: function(aSubject, aTopic) {
|
|
|
|
if (aTopic == "alertclickcallback") {
|
2014-03-15 21:37:37 +00:00
|
|
|
WebappOSUtils.launch(aApp);
|
2013-07-31 01:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-27 12:50:22 +00:00
|
|
|
try {
|
|
|
|
let notifier = Cc["@mozilla.org/alerts-service;1"].
|
|
|
|
getService(Ci.nsIAlertsService);
|
2012-07-14 08:01:07 +00:00
|
|
|
|
2014-03-15 21:37:37 +00:00
|
|
|
notifier.showAlertNotification(aNativeApp.iconURI.spec,
|
2013-08-27 12:50:22 +00:00
|
|
|
aBundle.getString("webapps.install.success"),
|
2014-03-15 21:37:37 +00:00
|
|
|
aNativeApp.appNameAsFilename,
|
2014-11-28 19:08:29 +00:00
|
|
|
true, null, launcher, "", "", "", "", null,
|
|
|
|
aInPrivateBrowsing);
|
2013-08-27 12:50:22 +00:00
|
|
|
} catch (ex) {}
|
2012-07-14 08:01:07 +00:00
|
|
|
}
|