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/. */
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! */
8 Cu.import("resource://webapprt/modules/WebappRT.jsm");
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) {
31 * Transmogrify the runtime session into one for the given webapp.
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.
44 function becomeWebapp(manifestURL, parameters, onBecome) {
45 function observeInstall(subj, topic, data) {
46 Services.obs.removeObserver(observeInstall, "webapps-ask-install");
48 // Step 2: Configure the runtime session to represent the app.
49 // We load DOMApplicationRegistry into a local scope to avoid appearing
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));
57 let installRecord = JSON.parse(data);
58 installRecord.mm = subj;
59 installRecord.registryDir = Services.dirsvc.get("ProfD", Ci.nsIFile).path;
60 WebappRT.config = installRecord;
62 let win = Services.wm.getMostRecentWindow("webapprt:webapp");
64 win = Services.ww.openWindow(null,
65 "chrome://webapprt/content/webapp.xul",
67 "chrome,dialog=no,resizable,scrollbars,centerscreen",
71 let promise = scope.startup(win);
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);
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);
83 promise.then(onBecome);
85 Services.obs.addObserver(observeInstall, "webapps-ask-install", false);
87 // Step 1: Install the app at the URL specified by the manifest.
88 let url = Services.io.newURI(manifestURL, null, null);
89 navigator.mozApps.install(url.spec, parameters);