|
1 /* ***** BEGIN LICENSE BLOCK ***** |
|
2 * |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. |
|
6 * |
|
7 * ***** END LICENSE BLOCK ***** */ |
|
8 |
|
9 let Ci = Components.interfaces, Cc = Components.classes, Cu = Components.utils; |
|
10 |
|
11 Cu.import("resource://gre/modules/Services.jsm") |
|
12 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
13 Cu.import("resource://gre/modules/AppsUtils.jsm"); |
|
14 |
|
15 #ifdef MOZ_ANDROID_SYNTHAPKS |
|
16 XPCOMUtils.defineLazyModuleGetter(this, "WebappManager", "resource://gre/modules/WebappManager.jsm"); |
|
17 #endif |
|
18 |
|
19 const DEFAULT_ICON = "chrome://browser/skin/images/default-app-icon.png"; |
|
20 |
|
21 let gStrings = Services.strings.createBundle("chrome://browser/locale/aboutApps.properties"); |
|
22 |
|
23 XPCOMUtils.defineLazyGetter(window, "gChromeWin", function() |
|
24 window.QueryInterface(Ci.nsIInterfaceRequestor) |
|
25 .getInterface(Ci.nsIWebNavigation) |
|
26 .QueryInterface(Ci.nsIDocShellTreeItem) |
|
27 .rootTreeItem |
|
28 .QueryInterface(Ci.nsIInterfaceRequestor) |
|
29 .getInterface(Ci.nsIDOMWindow) |
|
30 .QueryInterface(Ci.nsIDOMChromeWindow)); |
|
31 |
|
32 document.addEventListener("DOMContentLoaded", onLoad, false); |
|
33 |
|
34 var AppsUI = { |
|
35 uninstall: null, |
|
36 shortcut: null |
|
37 }; |
|
38 |
|
39 function openLink(aEvent) { |
|
40 try { |
|
41 let formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].getService(Ci.nsIURLFormatter); |
|
42 let url = formatter.formatURLPref(aEvent.currentTarget.getAttribute("pref")); |
|
43 let BrowserApp = gChromeWin.BrowserApp; |
|
44 BrowserApp.addTab(url, { selected: true, parentId: BrowserApp.selectedTab.id }); |
|
45 } catch (ex) {} |
|
46 } |
|
47 |
|
48 #ifdef MOZ_ANDROID_SYNTHAPKS |
|
49 function checkForUpdates(aEvent) { |
|
50 WebappManager.checkForUpdates(true); |
|
51 } |
|
52 #endif |
|
53 |
|
54 #ifndef MOZ_ANDROID_SYNTHAPKS |
|
55 var ContextMenus = { |
|
56 target: null, |
|
57 |
|
58 init: function() { |
|
59 document.addEventListener("contextmenu", this, false); |
|
60 document.getElementById("addToHomescreenLabel").addEventListener("click", this.addToHomescreen, false); |
|
61 document.getElementById("uninstallLabel").addEventListener("click", this.uninstall, false); |
|
62 }, |
|
63 |
|
64 handleEvent: function(event) { |
|
65 // store the target of context menu events so that we know which app to act on |
|
66 this.target = event.target; |
|
67 while (!this.target.hasAttribute("contextmenu")) { |
|
68 this.target = this.target.parentNode; |
|
69 } |
|
70 }, |
|
71 |
|
72 addToHomescreen: function() { |
|
73 let manifest = this.target.manifest; |
|
74 gChromeWin.WebappsUI.createShortcut(manifest.name, manifest.fullLaunchPath(), manifest.biggestIconURL || DEFAULT_ICON, "webapp"); |
|
75 this.target = null; |
|
76 }, |
|
77 |
|
78 uninstall: function() { |
|
79 navigator.mozApps.mgmt.uninstall(this.target.app); |
|
80 |
|
81 let manifest = this.target.manifest; |
|
82 gChromeWin.sendMessageToJava({ |
|
83 type: "Shortcut:Remove", |
|
84 title: manifest.name, |
|
85 url: manifest.fullLaunchPath(), |
|
86 origin: this.target.app.origin, |
|
87 shortcutType: "webapp" |
|
88 }); |
|
89 this.target = null; |
|
90 } |
|
91 } |
|
92 #endif |
|
93 |
|
94 function onLoad(aEvent) { |
|
95 let elmts = document.querySelectorAll("[pref]"); |
|
96 for (let i = 0; i < elmts.length; i++) { |
|
97 elmts[i].addEventListener("click", openLink, false); |
|
98 } |
|
99 |
|
100 #ifdef MOZ_ANDROID_SYNTHAPKS |
|
101 document.getElementById("update-item").addEventListener("click", checkForUpdates, false); |
|
102 #endif |
|
103 |
|
104 navigator.mozApps.mgmt.oninstall = onInstall; |
|
105 navigator.mozApps.mgmt.onuninstall = onUninstall; |
|
106 updateList(); |
|
107 |
|
108 #ifndef MOZ_ANDROID_SYNTHAPKS |
|
109 ContextMenus.init(); |
|
110 #endif |
|
111 } |
|
112 |
|
113 function updateList() { |
|
114 let grid = document.getElementById("appgrid"); |
|
115 while (grid.lastChild) { |
|
116 grid.removeChild(grid.lastChild); |
|
117 } |
|
118 |
|
119 let request = navigator.mozApps.mgmt.getAll(); |
|
120 request.onsuccess = function() { |
|
121 for (let i = 0; i < request.result.length; i++) |
|
122 addApplication(request.result[i]); |
|
123 if (request.result.length) |
|
124 document.getElementById("main-container").classList.remove("hidden"); |
|
125 } |
|
126 } |
|
127 |
|
128 function addApplication(aApp) { |
|
129 let list = document.getElementById("appgrid"); |
|
130 let manifest = new ManifestHelper(aApp.manifest, aApp.origin); |
|
131 |
|
132 let container = document.createElement("div"); |
|
133 container.className = "app list-item"; |
|
134 #ifndef MOZ_ANDROID_SYNTHAPKS |
|
135 container.setAttribute("contextmenu", "appmenu"); |
|
136 #endif |
|
137 container.setAttribute("id", "app-" + aApp.origin); |
|
138 container.setAttribute("mozApp", aApp.origin); |
|
139 container.setAttribute("title", manifest.name); |
|
140 |
|
141 let img = document.createElement("img"); |
|
142 img.src = manifest.biggestIconURL || DEFAULT_ICON; |
|
143 img.onerror = function() { |
|
144 // If the image failed to load, and it was not our default icon, attempt to |
|
145 // use our default as a fallback. |
|
146 if (img.src != DEFAULT_ICON) { |
|
147 img.src = DEFAULT_ICON; |
|
148 } |
|
149 } |
|
150 img.setAttribute("title", manifest.name); |
|
151 |
|
152 let title = document.createElement("div"); |
|
153 title.appendChild(document.createTextNode(manifest.name)); |
|
154 |
|
155 container.appendChild(img); |
|
156 container.appendChild(title); |
|
157 list.appendChild(container); |
|
158 |
|
159 container.addEventListener("click", function(aEvent) { |
|
160 aApp.launch(); |
|
161 }, false); |
|
162 container.app = aApp; |
|
163 container.manifest = manifest; |
|
164 } |
|
165 |
|
166 function onInstall(aEvent) { |
|
167 let node = document.getElementById("app-" + aEvent.application.origin); |
|
168 if (node) |
|
169 return; |
|
170 |
|
171 addApplication(aEvent.application); |
|
172 document.getElementById("main-container").classList.remove("hidden"); |
|
173 } |
|
174 |
|
175 function onUninstall(aEvent) { |
|
176 let node = document.getElementById("app-" + aEvent.application.origin); |
|
177 if (node) { |
|
178 let parent = node.parentNode; |
|
179 parent.removeChild(node); |
|
180 if (!parent.firstChild) |
|
181 document.getElementById("main-container").classList.add("hidden"); |
|
182 } |
|
183 } |
|
184 |