|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 const Cc = Components.classes; |
|
6 const Ci = Components.interfaces; |
|
7 const Cu = Components.utils; |
|
8 |
|
9 Cu.import("resource://webapprt/modules/WebappRT.jsm"); |
|
10 Cu.import("resource://gre/modules/Services.jsm"); |
|
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
12 |
|
13 XPCOMUtils.defineLazyGetter(this, "gAppBrowser", |
|
14 function() document.getElementById("content")); |
|
15 |
|
16 #ifdef MOZ_CRASHREPORTER |
|
17 XPCOMUtils.defineLazyServiceGetter(this, "gCrashReporter", |
|
18 "@mozilla.org/toolkit/crash-reporter;1", |
|
19 "nsICrashReporter"); |
|
20 #endif |
|
21 |
|
22 function isSameOrigin(url) { |
|
23 let origin = Services.io.newURI(url, null, null).prePath; |
|
24 return (origin == WebappRT.config.app.origin); |
|
25 } |
|
26 |
|
27 let progressListener = { |
|
28 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, |
|
29 Ci.nsISupportsWeakReference]), |
|
30 onLocationChange: function onLocationChange(progress, request, location, |
|
31 flags) { |
|
32 |
|
33 // Close tooltip (code adapted from /browser/base/content/browser.js) |
|
34 let pageTooltip = document.getElementById("contentAreaTooltip"); |
|
35 let tooltipNode = pageTooltip.triggerNode; |
|
36 if (tooltipNode) { |
|
37 // Optimise for the common case |
|
38 if (progress.isTopLevel) { |
|
39 pageTooltip.hidePopup(); |
|
40 } |
|
41 else { |
|
42 for (let tooltipWindow = tooltipNode.ownerDocument.defaultView; |
|
43 tooltipWindow != tooltipWindow.parent; |
|
44 tooltipWindow = tooltipWindow.parent) { |
|
45 if (tooltipWindow == progress.DOMWindow) { |
|
46 pageTooltip.hidePopup(); |
|
47 break; |
|
48 } |
|
49 } |
|
50 } |
|
51 } |
|
52 |
|
53 // Set the title of the window to the name of the webapp, adding the origin |
|
54 // of the page being loaded if it's from a different origin than the app |
|
55 // (per security bug 741955, which specifies that other-origin pages loaded |
|
56 // in runtime windows must be identified in chrome). |
|
57 let title = WebappRT.config.app.manifest.name; |
|
58 if (!isSameOrigin(location.spec)) { |
|
59 title = location.prePath + " - " + title; |
|
60 } |
|
61 document.documentElement.setAttribute("title", title); |
|
62 }, |
|
63 |
|
64 onStateChange: function onStateChange(aProgress, aRequest, aFlags, aStatus) { |
|
65 if (aRequest instanceof Ci.nsIChannel && |
|
66 aFlags & Ci.nsIWebProgressListener.STATE_START && |
|
67 aFlags & Ci.nsIWebProgressListener.STATE_IS_DOCUMENT) { |
|
68 updateCrashReportURL(aRequest.URI); |
|
69 } |
|
70 } |
|
71 }; |
|
72 |
|
73 function onOpenWindow(event) { |
|
74 let name = event.detail.name; |
|
75 |
|
76 if (name == "_blank") { |
|
77 let uri = Services.io.newURI(event.detail.url, null, null); |
|
78 |
|
79 // Direct the URL to the browser. |
|
80 Cc["@mozilla.org/uriloader/external-protocol-service;1"]. |
|
81 getService(Ci.nsIExternalProtocolService). |
|
82 getProtocolHandlerInfo(uri.scheme). |
|
83 launchWithURI(uri); |
|
84 } else { |
|
85 let win = window.openDialog("chrome://webapprt/content/webapp.xul", |
|
86 name, |
|
87 "chrome,dialog=no,resizable," + event.detail.features); |
|
88 |
|
89 win.addEventListener("load", function onLoad() { |
|
90 win.removeEventListener("load", onLoad, false); |
|
91 |
|
92 #ifndef XP_WIN |
|
93 #ifndef XP_MACOSX |
|
94 if (isSameOrigin(event.detail.url)) { |
|
95 // On non-Windows platforms, we open new windows in fullscreen mode |
|
96 // if the opener window is in fullscreen mode, so we hide the menubar; |
|
97 // but on Mac we don't need to hide the menubar. |
|
98 if (document.mozFullScreenElement) { |
|
99 win.document.getElementById("main-menubar").style.display = "none"; |
|
100 } |
|
101 } |
|
102 #endif |
|
103 #endif |
|
104 |
|
105 win.document.getElementById("content").docShell.setIsApp(WebappRT.appID); |
|
106 win.document.getElementById("content").setAttribute("src", event.detail.url); |
|
107 }, false); |
|
108 } |
|
109 } |
|
110 |
|
111 function onLoad() { |
|
112 window.removeEventListener("load", onLoad, false); |
|
113 |
|
114 gAppBrowser.addProgressListener(progressListener, |
|
115 Ci.nsIWebProgress.NOTIFY_LOCATION | |
|
116 Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT); |
|
117 |
|
118 updateMenuItems(); |
|
119 |
|
120 gAppBrowser.addEventListener("mozbrowseropenwindow", onOpenWindow); |
|
121 } |
|
122 window.addEventListener("load", onLoad, false); |
|
123 |
|
124 function onUnload() { |
|
125 gAppBrowser.removeProgressListener(progressListener); |
|
126 gAppBrowser.removeEventListener("mozbrowseropenwindow", onOpenWindow); |
|
127 } |
|
128 window.addEventListener("unload", onUnload, false); |
|
129 |
|
130 // Fullscreen handling. |
|
131 |
|
132 #ifndef XP_MACOSX |
|
133 document.addEventListener('mozfullscreenchange', function() { |
|
134 if (document.mozFullScreenElement) { |
|
135 document.getElementById("main-menubar").style.display = "none"; |
|
136 } else { |
|
137 document.getElementById("main-menubar").style.display = ""; |
|
138 } |
|
139 }, false); |
|
140 #endif |
|
141 |
|
142 // On Mac, we dynamically create the label for the Quit menuitem, using |
|
143 // a string property to inject the name of the webapp into it. |
|
144 function updateMenuItems() { |
|
145 #ifdef XP_MACOSX |
|
146 let installRecord = WebappRT.config.app; |
|
147 let manifest = WebappRT.config.app.manifest; |
|
148 let bundle = |
|
149 Services.strings.createBundle("chrome://webapprt/locale/webapp.properties"); |
|
150 let quitLabel = bundle.formatStringFromName("quitApplicationCmdMac.label", |
|
151 [manifest.name], 1); |
|
152 let hideLabel = bundle.formatStringFromName("hideApplicationCmdMac.label", |
|
153 [manifest.name], 1); |
|
154 document.getElementById("menu_FileQuitItem").setAttribute("label", quitLabel); |
|
155 document.getElementById("menu_mac_hide_app").setAttribute("label", hideLabel); |
|
156 #endif |
|
157 } |
|
158 |
|
159 #ifndef XP_MACOSX |
|
160 let gEditUIVisible = true; |
|
161 #endif |
|
162 |
|
163 function updateEditUIVisibility() { |
|
164 #ifndef XP_MACOSX |
|
165 let editMenuPopupState = document.getElementById("menu_EditPopup").state; |
|
166 let contextMenuPopupState = document.getElementById("contentAreaContextMenu").state; |
|
167 |
|
168 // The UI is visible if the Edit menu is opening or open, if the context menu |
|
169 // is open, or if the toolbar has been customized to include the Cut, Copy, |
|
170 // or Paste toolbar buttons. |
|
171 gEditUIVisible = editMenuPopupState == "showing" || |
|
172 editMenuPopupState == "open" || |
|
173 contextMenuPopupState == "showing" || |
|
174 contextMenuPopupState == "open"; |
|
175 |
|
176 // If UI is visible, update the edit commands' enabled state to reflect |
|
177 // whether or not they are actually enabled for the current focus/selection. |
|
178 if (gEditUIVisible) { |
|
179 goUpdateGlobalEditMenuItems(); |
|
180 } |
|
181 |
|
182 // Otherwise, enable all commands, so that keyboard shortcuts still work, |
|
183 // then lazily determine their actual enabled state when the user presses |
|
184 // a keyboard shortcut. |
|
185 else { |
|
186 goSetCommandEnabled("cmd_undo", true); |
|
187 goSetCommandEnabled("cmd_redo", true); |
|
188 goSetCommandEnabled("cmd_cut", true); |
|
189 goSetCommandEnabled("cmd_copy", true); |
|
190 goSetCommandEnabled("cmd_paste", true); |
|
191 goSetCommandEnabled("cmd_selectAll", true); |
|
192 goSetCommandEnabled("cmd_delete", true); |
|
193 goSetCommandEnabled("cmd_switchTextDirection", true); |
|
194 } |
|
195 #endif |
|
196 } |
|
197 |
|
198 function updateCrashReportURL(aURI) { |
|
199 #ifdef MOZ_CRASHREPORTER |
|
200 if (!gCrashReporter.enabled) |
|
201 return; |
|
202 |
|
203 let uri = aURI.clone(); |
|
204 // uri.userPass throws on protocols without the concept of authentication, |
|
205 // like about:, which tests can load, so we catch and ignore an exception. |
|
206 try { |
|
207 if (uri.userPass != "") { |
|
208 uri.userPass = ""; |
|
209 } |
|
210 } catch (e) {} |
|
211 |
|
212 gCrashReporter.annotateCrashReport("URL", uri.spec); |
|
213 #endif |
|
214 } |
|
215 |
|
216 // Context menu handling code. |
|
217 // At the moment there isn't any built-in menu, we only support HTML5 custom |
|
218 // menus. |
|
219 |
|
220 let gContextMenu = null; |
|
221 |
|
222 XPCOMUtils.defineLazyGetter(this, "PageMenu", function() { |
|
223 let tmp = {}; |
|
224 Cu.import("resource://gre/modules/PageMenu.jsm", tmp); |
|
225 return new tmp.PageMenu(); |
|
226 }); |
|
227 |
|
228 function showContextMenu(aEvent, aXULMenu) { |
|
229 if (aEvent.target != aXULMenu) { |
|
230 return true; |
|
231 } |
|
232 |
|
233 gContextMenu = new nsContextMenu(aXULMenu); |
|
234 if (gContextMenu.shouldDisplay) { |
|
235 updateEditUIVisibility(); |
|
236 } |
|
237 |
|
238 return gContextMenu.shouldDisplay; |
|
239 } |
|
240 |
|
241 function hideContextMenu(aEvent, aXULMenu) { |
|
242 if (aEvent.target != aXULMenu) { |
|
243 return; |
|
244 } |
|
245 |
|
246 gContextMenu = null; |
|
247 |
|
248 updateEditUIVisibility(); |
|
249 } |
|
250 |
|
251 function nsContextMenu(aXULMenu) { |
|
252 this.initMenu(aXULMenu); |
|
253 } |
|
254 |
|
255 nsContextMenu.prototype = { |
|
256 initMenu: function(aXULMenu) { |
|
257 this.hasPageMenu = PageMenu.maybeBuildAndAttachMenu(document.popupNode, |
|
258 aXULMenu); |
|
259 this.shouldDisplay = this.hasPageMenu; |
|
260 }, |
|
261 }; |