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/. */
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["webappsUI"];
|
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");
|
2012-08-03 20:40:30 -07:00
|
|
|
Cu.import("resource://gre/modules/WebappsInstaller.jsm");
|
2012-07-16 11:22:16 -07:00
|
|
|
Cu.import("resource://gre/modules/WebappOSUtils.jsm");
|
2013-08-27 05:50:22 -07:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
2012-03-12 17:33:10 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.webappsUI = {
|
2013-08-27 05:50:22 -07:00
|
|
|
downloads: {},
|
|
|
|
|
2012-03-12 17:33:10 -07:00
|
|
|
init: function webappsUI_init() {
|
|
|
|
Services.obs.addObserver(this, "webapps-ask-install", false);
|
|
|
|
Services.obs.addObserver(this, "webapps-launch", false);
|
2012-08-08 18:04:48 -07:00
|
|
|
Services.obs.addObserver(this, "webapps-uninstall", false);
|
2013-08-27 05:50:22 -07:00
|
|
|
cpmm.addMessageListener("Webapps:OfflineCache", this);
|
2012-03-12 17:33:10 -07:00
|
|
|
},
|
2012-10-02 22:38:03 -07:00
|
|
|
|
2012-03-12 17:33:10 -07:00
|
|
|
uninit: function webappsUI_uninit() {
|
|
|
|
Services.obs.removeObserver(this, "webapps-ask-install");
|
|
|
|
Services.obs.removeObserver(this, "webapps-launch");
|
2012-08-08 18:04:48 -07:00
|
|
|
Services.obs.removeObserver(this, "webapps-uninstall");
|
2013-08-27 05:50:22 -07:00
|
|
|
cpmm.removeMessageListener("Webapps:OfflineCache", this);
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
let data = aMessage.data;
|
|
|
|
|
2013-08-28 12:35:35 -07:00
|
|
|
if (aMessage.name == "Webapps:OfflineCache" &&
|
|
|
|
data.installState == "installed" &&
|
|
|
|
this.downloads[data.manifest]) {
|
2013-08-27 05:50:22 -07:00
|
|
|
this.downloads[data.manifest].resolve();
|
|
|
|
}
|
2012-03-12 17:33:10 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function webappsUI_observe(aSubject, aTopic, aData) {
|
|
|
|
let data = JSON.parse(aData);
|
2012-09-18 10:34:55 -07:00
|
|
|
data.mm = aSubject;
|
2012-03-12 17:33:10 -07:00
|
|
|
|
|
|
|
switch(aTopic) {
|
|
|
|
case "webapps-ask-install":
|
2013-08-05 13:30:41 -07:00
|
|
|
let win = this._getWindowForId(data.oid);
|
|
|
|
if (win && win.location.href == data.from) {
|
|
|
|
this.doInstall(data, win);
|
|
|
|
}
|
2012-03-12 17:33:10 -07:00
|
|
|
break;
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2012-03-12 17:33:10 -07:00
|
|
|
openURL: function(aUrl, aOrigin) {
|
2012-10-02 22:38:03 -07:00
|
|
|
let browserEnumerator = Services.wm.getEnumerator("navigator:browser");
|
2012-03-12 17:33:10 -07:00
|
|
|
let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
|
|
|
|
|
|
|
|
// Check each browser instance for our URL
|
|
|
|
let found = false;
|
|
|
|
while (!found && browserEnumerator.hasMoreElements()) {
|
|
|
|
let browserWin = browserEnumerator.getNext();
|
|
|
|
let tabbrowser = browserWin.gBrowser;
|
|
|
|
|
|
|
|
// Check each tab of this browser instance
|
|
|
|
let numTabs = tabbrowser.tabs.length;
|
|
|
|
for (let index = 0; index < numTabs; index++) {
|
|
|
|
let tab = tabbrowser.tabs[index];
|
|
|
|
let appURL = ss.getTabValue(tab, "appOrigin");
|
|
|
|
if (appURL == aOrigin) {
|
|
|
|
// The URL is already opened. Select this tab.
|
|
|
|
tabbrowser.selectedTab = tab;
|
|
|
|
browserWin.focus();
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our URL isn't open. Open it now.
|
|
|
|
if (!found) {
|
|
|
|
let recentWindow = Services.wm.getMostRecentWindow("navigator:browser");
|
|
|
|
if (recentWindow) {
|
|
|
|
// Use an existing browser window
|
|
|
|
let browser = recentWindow.gBrowser;
|
|
|
|
let tab = browser.addTab(aUrl);
|
|
|
|
browser.pinTab(tab);
|
|
|
|
browser.selectedTab = tab;
|
|
|
|
ss.setTabValue(tab, "appOrigin", aOrigin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
if (aData.app.manifest && aData.app.manifest.appcache_path) {
|
|
|
|
this.downloads[manifestURL] = Promise.defer();
|
|
|
|
}
|
|
|
|
|
2013-08-01 17:00:39 -07:00
|
|
|
let app = WebappsInstaller.init(aData);
|
|
|
|
|
2012-06-22 15:19:23 -07:00
|
|
|
if (app) {
|
|
|
|
let localDir = null;
|
2012-08-14 23:19:36 -07:00
|
|
|
if (app.appProfile) {
|
2012-06-22 15:19:23 -07:00
|
|
|
localDir = app.appProfile.localDir;
|
|
|
|
}
|
|
|
|
|
2013-08-29 07:00:33 -07:00
|
|
|
DOMApplicationRegistry.confirmInstall(aData, localDir,
|
2013-08-27 05:50:22 -07:00
|
|
|
(aManifest) => {
|
|
|
|
Task.spawn(function() {
|
|
|
|
try {
|
|
|
|
yield WebappsInstaller.install(aData, aManifest);
|
|
|
|
if (this.downloads[manifestURL]) {
|
|
|
|
yield this.downloads[manifestURL].promise;
|
|
|
|
}
|
|
|
|
installationSuccessNotification(aData, app, bundle);
|
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError("Error installing webapp: " + ex);
|
2013-08-26 12:04:33 -07:00
|
|
|
// TODO: Notify user that the installation has failed
|
2013-08-27 05:50:22 -07:00
|
|
|
} finally {
|
2013-09-09 05:57:37 -07:00
|
|
|
popupProgressContent.removeChild(progressMeter);
|
|
|
|
notification.remove();
|
2013-08-27 05:50:22 -07:00
|
|
|
delete this.downloads[manifestURL];
|
2013-08-26 12:04:33 -07:00
|
|
|
}
|
2013-08-27 05:50:22 -07:00
|
|
|
}.bind(this));
|
|
|
|
});
|
2012-04-17 07:17:23 -07:00
|
|
|
} else {
|
|
|
|
DOMApplicationRegistry.denyInstall(aData);
|
|
|
|
}
|
2012-03-12 17:33:10 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-05 13:30:41 -07:00
|
|
|
let requestingURI = chromeWin.makeURI(aData.from);
|
2013-08-01 17:00:39 -07:00
|
|
|
let jsonManifest = aData.isPackage ? aData.app.updateManifest : aData.app.manifest;
|
|
|
|
let manifest = new ManifestHelper(jsonManifest, aData.app.origin);
|
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
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2012-07-14 01:01:07 -07:00
|
|
|
|
2013-08-27 05:50:22 -07:00
|
|
|
function installationSuccessNotification(aData, app, aBundle) {
|
2013-07-30 18:37:00 -07:00
|
|
|
let launcher = {
|
|
|
|
observe: function(aSubject, aTopic) {
|
|
|
|
if (aTopic == "alertclickcallback") {
|
|
|
|
WebappOSUtils.launch(aData.app);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2013-08-27 05:50:22 -07:00
|
|
|
notifier.showAlertNotification(app.iconURI.spec,
|
|
|
|
aBundle.getString("webapps.install.success"),
|
|
|
|
app.appNameAsFilename,
|
|
|
|
true, null, launcher);
|
|
|
|
} catch (ex) {}
|
2012-07-14 01:01:07 -07:00
|
|
|
}
|