michael@0: /* ***** BEGIN LICENSE BLOCK ***** michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: * michael@0: * ***** END LICENSE BLOCK ***** */ michael@0: michael@0: let Ci = Components.interfaces, Cc = Components.classes, Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm") michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/AppsUtils.jsm"); michael@0: michael@0: #ifdef MOZ_ANDROID_SYNTHAPKS michael@0: XPCOMUtils.defineLazyModuleGetter(this, "WebappManager", "resource://gre/modules/WebappManager.jsm"); michael@0: #endif michael@0: michael@0: const DEFAULT_ICON = "chrome://browser/skin/images/default-app-icon.png"; michael@0: michael@0: let gStrings = Services.strings.createBundle("chrome://browser/locale/aboutApps.properties"); michael@0: michael@0: XPCOMUtils.defineLazyGetter(window, "gChromeWin", function() michael@0: window.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIWebNavigation) michael@0: .QueryInterface(Ci.nsIDocShellTreeItem) michael@0: .rootTreeItem michael@0: .QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindow) michael@0: .QueryInterface(Ci.nsIDOMChromeWindow)); michael@0: michael@0: document.addEventListener("DOMContentLoaded", onLoad, false); michael@0: michael@0: var AppsUI = { michael@0: uninstall: null, michael@0: shortcut: null michael@0: }; michael@0: michael@0: function openLink(aEvent) { michael@0: try { michael@0: let formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].getService(Ci.nsIURLFormatter); michael@0: let url = formatter.formatURLPref(aEvent.currentTarget.getAttribute("pref")); michael@0: let BrowserApp = gChromeWin.BrowserApp; michael@0: BrowserApp.addTab(url, { selected: true, parentId: BrowserApp.selectedTab.id }); michael@0: } catch (ex) {} michael@0: } michael@0: michael@0: #ifdef MOZ_ANDROID_SYNTHAPKS michael@0: function checkForUpdates(aEvent) { michael@0: WebappManager.checkForUpdates(true); michael@0: } michael@0: #endif michael@0: michael@0: #ifndef MOZ_ANDROID_SYNTHAPKS michael@0: var ContextMenus = { michael@0: target: null, michael@0: michael@0: init: function() { michael@0: document.addEventListener("contextmenu", this, false); michael@0: document.getElementById("addToHomescreenLabel").addEventListener("click", this.addToHomescreen, false); michael@0: document.getElementById("uninstallLabel").addEventListener("click", this.uninstall, false); michael@0: }, michael@0: michael@0: handleEvent: function(event) { michael@0: // store the target of context menu events so that we know which app to act on michael@0: this.target = event.target; michael@0: while (!this.target.hasAttribute("contextmenu")) { michael@0: this.target = this.target.parentNode; michael@0: } michael@0: }, michael@0: michael@0: addToHomescreen: function() { michael@0: let manifest = this.target.manifest; michael@0: gChromeWin.WebappsUI.createShortcut(manifest.name, manifest.fullLaunchPath(), manifest.biggestIconURL || DEFAULT_ICON, "webapp"); michael@0: this.target = null; michael@0: }, michael@0: michael@0: uninstall: function() { michael@0: navigator.mozApps.mgmt.uninstall(this.target.app); michael@0: michael@0: let manifest = this.target.manifest; michael@0: gChromeWin.sendMessageToJava({ michael@0: type: "Shortcut:Remove", michael@0: title: manifest.name, michael@0: url: manifest.fullLaunchPath(), michael@0: origin: this.target.app.origin, michael@0: shortcutType: "webapp" michael@0: }); michael@0: this.target = null; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: function onLoad(aEvent) { michael@0: let elmts = document.querySelectorAll("[pref]"); michael@0: for (let i = 0; i < elmts.length; i++) { michael@0: elmts[i].addEventListener("click", openLink, false); michael@0: } michael@0: michael@0: #ifdef MOZ_ANDROID_SYNTHAPKS michael@0: document.getElementById("update-item").addEventListener("click", checkForUpdates, false); michael@0: #endif michael@0: michael@0: navigator.mozApps.mgmt.oninstall = onInstall; michael@0: navigator.mozApps.mgmt.onuninstall = onUninstall; michael@0: updateList(); michael@0: michael@0: #ifndef MOZ_ANDROID_SYNTHAPKS michael@0: ContextMenus.init(); michael@0: #endif michael@0: } michael@0: michael@0: function updateList() { michael@0: let grid = document.getElementById("appgrid"); michael@0: while (grid.lastChild) { michael@0: grid.removeChild(grid.lastChild); michael@0: } michael@0: michael@0: let request = navigator.mozApps.mgmt.getAll(); michael@0: request.onsuccess = function() { michael@0: for (let i = 0; i < request.result.length; i++) michael@0: addApplication(request.result[i]); michael@0: if (request.result.length) michael@0: document.getElementById("main-container").classList.remove("hidden"); michael@0: } michael@0: } michael@0: michael@0: function addApplication(aApp) { michael@0: let list = document.getElementById("appgrid"); michael@0: let manifest = new ManifestHelper(aApp.manifest, aApp.origin); michael@0: michael@0: let container = document.createElement("div"); michael@0: container.className = "app list-item"; michael@0: #ifndef MOZ_ANDROID_SYNTHAPKS michael@0: container.setAttribute("contextmenu", "appmenu"); michael@0: #endif michael@0: container.setAttribute("id", "app-" + aApp.origin); michael@0: container.setAttribute("mozApp", aApp.origin); michael@0: container.setAttribute("title", manifest.name); michael@0: michael@0: let img = document.createElement("img"); michael@0: img.src = manifest.biggestIconURL || DEFAULT_ICON; michael@0: img.onerror = function() { michael@0: // If the image failed to load, and it was not our default icon, attempt to michael@0: // use our default as a fallback. michael@0: if (img.src != DEFAULT_ICON) { michael@0: img.src = DEFAULT_ICON; michael@0: } michael@0: } michael@0: img.setAttribute("title", manifest.name); michael@0: michael@0: let title = document.createElement("div"); michael@0: title.appendChild(document.createTextNode(manifest.name)); michael@0: michael@0: container.appendChild(img); michael@0: container.appendChild(title); michael@0: list.appendChild(container); michael@0: michael@0: container.addEventListener("click", function(aEvent) { michael@0: aApp.launch(); michael@0: }, false); michael@0: container.app = aApp; michael@0: container.manifest = manifest; michael@0: } michael@0: michael@0: function onInstall(aEvent) { michael@0: let node = document.getElementById("app-" + aEvent.application.origin); michael@0: if (node) michael@0: return; michael@0: michael@0: addApplication(aEvent.application); michael@0: document.getElementById("main-container").classList.remove("hidden"); michael@0: } michael@0: michael@0: function onUninstall(aEvent) { michael@0: let node = document.getElementById("app-" + aEvent.application.origin); michael@0: if (node) { michael@0: let parent = node.parentNode; michael@0: parent.removeChild(node); michael@0: if (!parent.firstChild) michael@0: document.getElementById("main-container").classList.add("hidden"); michael@0: } michael@0: } michael@0: