2012-03-13 04:33:10 +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/. */
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ["webappsUI"];
|
2012-03-13 04:33:10 +04: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-03 09:38:03 +04:00
|
|
|
Cu.import("resource://gre/modules/AppsUtils.jsm");
|
2012-08-04 07:40:30 +04:00
|
|
|
Cu.import("resource://gre/modules/WebappsInstaller.jsm");
|
2012-07-16 22:22:16 +04:00
|
|
|
Cu.import("resource://gre/modules/WebappOSUtils.jsm");
|
2012-03-13 04:33:10 +04:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.webappsUI = {
|
2012-03-13 04:33:10 +04:00
|
|
|
init: function webappsUI_init() {
|
|
|
|
Services.obs.addObserver(this, "webapps-ask-install", false);
|
|
|
|
Services.obs.addObserver(this, "webapps-launch", false);
|
2012-08-09 05:04:48 +04:00
|
|
|
Services.obs.addObserver(this, "webapps-uninstall", false);
|
2012-03-13 04:33:10 +04:00
|
|
|
},
|
2012-10-03 09:38:03 +04:00
|
|
|
|
2012-03-13 04:33:10 +04:00
|
|
|
uninit: function webappsUI_uninit() {
|
|
|
|
Services.obs.removeObserver(this, "webapps-ask-install");
|
|
|
|
Services.obs.removeObserver(this, "webapps-launch");
|
2012-08-09 05:04:48 +04:00
|
|
|
Services.obs.removeObserver(this, "webapps-uninstall");
|
2012-03-13 04:33:10 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function webappsUI_observe(aSubject, aTopic, aData) {
|
|
|
|
let data = JSON.parse(aData);
|
2012-09-18 21:34:55 +04:00
|
|
|
data.mm = aSubject;
|
2012-03-13 04:33:10 +04:00
|
|
|
|
|
|
|
switch(aTopic) {
|
|
|
|
case "webapps-ask-install":
|
2012-08-03 14:14:08 +04:00
|
|
|
let [chromeWin, browser] = this._getBrowserForId(data.oid);
|
|
|
|
if (chromeWin)
|
|
|
|
this.doInstall(data, browser, chromeWin);
|
2012-03-13 04:33:10 +04:00
|
|
|
break;
|
|
|
|
case "webapps-launch":
|
2012-07-16 22:22:16 +04:00
|
|
|
WebappOSUtils.launch(data);
|
2012-03-13 04:33:10 +04:00
|
|
|
break;
|
2012-08-09 05:04:48 +04:00
|
|
|
case "webapps-uninstall":
|
|
|
|
WebappOSUtils.uninstall(data);
|
|
|
|
break;
|
2012-03-13 04:33:10 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
openURL: function(aUrl, aOrigin) {
|
2012-10-03 09:38:03 +04:00
|
|
|
let browserEnumerator = Services.wm.getEnumerator("navigator:browser");
|
2012-03-13 04:33:10 +04: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-03 14:14:08 +04:00
|
|
|
_getBrowserForId: function(aId) {
|
2013-05-07 20:34:21 +04:00
|
|
|
let content = Services.wm.getOuterWindowWithId(aId);
|
|
|
|
if (content) {
|
|
|
|
let browser = content.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocShell).chromeEventHandler;
|
|
|
|
let win = browser.ownerDocument.defaultView;
|
|
|
|
return [win, browser];
|
2012-08-03 14:14:08 +04:00
|
|
|
}
|
2012-03-13 04:33:10 +04:00
|
|
|
|
2012-08-03 14:14:08 +04:00
|
|
|
return [null, null];
|
|
|
|
},
|
|
|
|
|
|
|
|
doInstall: function(aData, aBrowser, aWindow) {
|
|
|
|
let bundle = aWindow.gNavigatorBundle;
|
2012-03-13 04:33:10 +04:00
|
|
|
|
|
|
|
let mainAction = {
|
|
|
|
label: bundle.getString("webapps.install"),
|
|
|
|
accessKey: bundle.getString("webapps.install.accesskey"),
|
2012-06-04 09:25:55 +04:00
|
|
|
callback: function() {
|
2013-08-02 04:00:39 +04:00
|
|
|
let app = WebappsInstaller.init(aData);
|
|
|
|
|
2012-06-23 02:19:23 +04:00
|
|
|
if (app) {
|
|
|
|
let localDir = null;
|
2012-08-15 10:19:36 +04:00
|
|
|
if (app.appProfile) {
|
2012-06-23 02:19:23 +04:00
|
|
|
localDir = app.appProfile.localDir;
|
|
|
|
}
|
|
|
|
|
2013-08-02 04:00:39 +04:00
|
|
|
DOMApplicationRegistry.confirmInstall(aData, false, localDir, null,
|
|
|
|
function (aManifest) {
|
|
|
|
if (WebappsInstaller.install(aData, aManifest)) {
|
|
|
|
installationSuccessNotification(aData, app, aWindow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2012-04-17 18:17:23 +04:00
|
|
|
} else {
|
|
|
|
DOMApplicationRegistry.denyInstall(aData);
|
|
|
|
}
|
2012-03-13 04:33:10 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-03 14:14:08 +04:00
|
|
|
let requestingURI = aWindow.makeURI(aData.from);
|
2013-08-02 04:00:39 +04:00
|
|
|
let jsonManifest = aData.isPackage ? aData.app.updateManifest : aData.app.manifest;
|
|
|
|
let manifest = new ManifestHelper(jsonManifest, aData.app.origin);
|
2012-03-13 04:33:10 +04:00
|
|
|
|
|
|
|
let host;
|
|
|
|
try {
|
|
|
|
host = requestingURI.host;
|
|
|
|
} catch(e) {
|
|
|
|
host = requestingURI.spec;
|
|
|
|
}
|
|
|
|
|
|
|
|
let message = bundle.getFormattedString("webapps.requestInstall",
|
2012-08-03 14:14:08 +04:00
|
|
|
[manifest.name, host], 2);
|
2012-07-14 12:01:07 +04:00
|
|
|
|
2012-08-03 14:14:08 +04:00
|
|
|
aWindow.PopupNotifications.show(aBrowser, "webapps-install", message,
|
|
|
|
"webapps-notification-icon", mainAction);
|
2012-07-14 12:01:07 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2012-07-14 12:01:07 +04:00
|
|
|
|
2013-07-31 05:37:00 +04:00
|
|
|
function installationSuccessNotification(aData, app, aWindow) {
|
|
|
|
let launcher = {
|
|
|
|
observe: function(aSubject, aTopic) {
|
|
|
|
if (aTopic == "alertclickcallback") {
|
|
|
|
WebappOSUtils.launch(aData.app);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-14 12:01:07 +04:00
|
|
|
let bundle = aWindow.gNavigatorBundle;
|
|
|
|
|
|
|
|
if (("@mozilla.org/alerts-service;1" in Cc)) {
|
|
|
|
let notifier;
|
|
|
|
try {
|
|
|
|
notifier = Cc["@mozilla.org/alerts-service;1"].
|
|
|
|
getService(Ci.nsIAlertsService);
|
|
|
|
|
|
|
|
notifier.showAlertNotification(app.iconURI.spec,
|
|
|
|
bundle.getString("webapps.install.success"),
|
|
|
|
app.appNameAsFilename,
|
2013-07-31 05:37:00 +04:00
|
|
|
true, null, launcher);
|
2012-07-14 12:01:07 +04:00
|
|
|
|
|
|
|
} catch (ex) {}
|
|
|
|
}
|
|
|
|
}
|