|
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 /* Note: this script is loaded by both mochitest.js and head.js, so make sure |
|
6 * the code you put here can be evaluated by both! */ |
|
7 |
|
8 Cu.import("resource://webapprt/modules/WebappRT.jsm"); |
|
9 |
|
10 // When WebappsHandler opens an install confirmation dialog for apps we install, |
|
11 // close it, which will be seen as the equivalent of cancelling the install. |
|
12 // This doesn't prevent us from installing those apps, as we listen for the same |
|
13 // notification as WebappsHandler and do the install ourselves. It just |
|
14 // prevents the modal installation confirmation dialogs from hanging tests. |
|
15 Services.ww.registerNotification({ |
|
16 observe: function(win, topic) { |
|
17 if (topic == "domwindowopened") { |
|
18 // Wait for load because the window is not yet sufficiently initialized. |
|
19 win.addEventListener("load", function onLoadWindow() { |
|
20 win.removeEventListener("load", onLoadWindow, false); |
|
21 if (win.location == "chrome://global/content/commonDialog.xul" && |
|
22 win.opener == window) { |
|
23 win.close(); |
|
24 } |
|
25 }, false); |
|
26 } |
|
27 } |
|
28 }); |
|
29 |
|
30 /** |
|
31 * Transmogrify the runtime session into one for the given webapp. |
|
32 * |
|
33 * @param {String} manifestURL |
|
34 * The URL of the webapp's manifest, relative to the base URL. |
|
35 * Note that the base URL points to the *chrome* WebappRT mochitests, |
|
36 * so you must supply an absolute URL to manifests elsewhere. |
|
37 * @param {Object} parameters |
|
38 * The value to pass as the "parameters" argument to |
|
39 * mozIDOMApplicationRegistry.install, e.g., { receipts: ... }. |
|
40 * Use undefined to pass nothing. |
|
41 * @param {Function} onBecome |
|
42 * The callback to call once the transmogrification is complete. |
|
43 */ |
|
44 function becomeWebapp(manifestURL, parameters, onBecome) { |
|
45 function observeInstall(subj, topic, data) { |
|
46 Services.obs.removeObserver(observeInstall, "webapps-ask-install"); |
|
47 |
|
48 // Step 2: Configure the runtime session to represent the app. |
|
49 // We load DOMApplicationRegistry into a local scope to avoid appearing |
|
50 // to leak it. |
|
51 |
|
52 let scope = {}; |
|
53 Cu.import("resource://gre/modules/Webapps.jsm", scope); |
|
54 Cu.import("resource://webapprt/modules/Startup.jsm", scope); |
|
55 scope.DOMApplicationRegistry.confirmInstall(JSON.parse(data)); |
|
56 |
|
57 let installRecord = JSON.parse(data); |
|
58 installRecord.mm = subj; |
|
59 installRecord.registryDir = Services.dirsvc.get("ProfD", Ci.nsIFile).path; |
|
60 WebappRT.config = installRecord; |
|
61 |
|
62 let win = Services.wm.getMostRecentWindow("webapprt:webapp"); |
|
63 if (!win) { |
|
64 win = Services.ww.openWindow(null, |
|
65 "chrome://webapprt/content/webapp.xul", |
|
66 "_blank", |
|
67 "chrome,dialog=no,resizable,scrollbars,centerscreen", |
|
68 null); |
|
69 } |
|
70 |
|
71 let promise = scope.startup(win); |
|
72 |
|
73 // During chrome tests, we use the same window to load all the tests. We |
|
74 // need to change the buildID so that the permissions for the currently |
|
75 // tested application get installed. |
|
76 Services.prefs.setCharPref("webapprt.buildID", WebappRT.config.app.manifestURL); |
|
77 |
|
78 // During tests, the webapps registry is already loaded. |
|
79 // The Startup module needs to be notified when the webapps registry |
|
80 // gets loaded, so we do that now. |
|
81 Services.obs.notifyObservers(this, "webapps-registry-start", null); |
|
82 |
|
83 promise.then(onBecome); |
|
84 } |
|
85 Services.obs.addObserver(observeInstall, "webapps-ask-install", false); |
|
86 |
|
87 // Step 1: Install the app at the URL specified by the manifest. |
|
88 navigator.mozApps.install(manifestURL, parameters); |
|
89 } |