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-30 23:28:11 +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-30 23:28:11 +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) {
|
|
|
|
let someWindow = Services.wm.getMostRecentWindow(null);
|
|
|
|
|
|
|
|
if (someWindow) {
|
|
|
|
let windowUtils = someWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
let content = windowUtils.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-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() {
|
2012-06-23 02:19:23 +04:00
|
|
|
let app = WebappsInstaller.install(aData);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMApplicationRegistry.confirmInstall(aData, false, localDir);
|
2012-08-03 14:14:08 +04:00
|
|
|
installationSuccessNotification(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);
|
2012-10-03 09:38:03 +04:00
|
|
|
let manifest = new ManifestHelper(aData.app.manifest, 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
|
|
|
|
|
|
|
function installationSuccessNotification(app, aWindow) {
|
|
|
|
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,
|
|
|
|
false, null, null);
|
|
|
|
|
|
|
|
} catch (ex) {}
|
|
|
|
}
|
|
|
|
}
|