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: /* Note: this script is loaded by both mochitest.js and head.js, so make sure michael@0: * the code you put here can be evaluated by both! */ michael@0: michael@0: Cu.import("resource://webapprt/modules/WebappRT.jsm"); michael@0: michael@0: // When WebappsHandler opens an install confirmation dialog for apps we install, michael@0: // close it, which will be seen as the equivalent of cancelling the install. michael@0: // This doesn't prevent us from installing those apps, as we listen for the same michael@0: // notification as WebappsHandler and do the install ourselves. It just michael@0: // prevents the modal installation confirmation dialogs from hanging tests. michael@0: Services.ww.registerNotification({ michael@0: observe: function(win, topic) { michael@0: if (topic == "domwindowopened") { michael@0: // Wait for load because the window is not yet sufficiently initialized. michael@0: win.addEventListener("load", function onLoadWindow() { michael@0: win.removeEventListener("load", onLoadWindow, false); michael@0: if (win.location == "chrome://global/content/commonDialog.xul" && michael@0: win.opener == window) { michael@0: win.close(); michael@0: } michael@0: }, false); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: /** michael@0: * Transmogrify the runtime session into one for the given webapp. michael@0: * michael@0: * @param {String} manifestURL michael@0: * The URL of the webapp's manifest, relative to the base URL. michael@0: * Note that the base URL points to the *chrome* WebappRT mochitests, michael@0: * so you must supply an absolute URL to manifests elsewhere. michael@0: * @param {Object} parameters michael@0: * The value to pass as the "parameters" argument to michael@0: * mozIDOMApplicationRegistry.install, e.g., { receipts: ... }. michael@0: * Use undefined to pass nothing. michael@0: * @param {Function} onBecome michael@0: * The callback to call once the transmogrification is complete. michael@0: */ michael@0: function becomeWebapp(manifestURL, parameters, onBecome) { michael@0: function observeInstall(subj, topic, data) { michael@0: Services.obs.removeObserver(observeInstall, "webapps-ask-install"); michael@0: michael@0: // Step 2: Configure the runtime session to represent the app. michael@0: // We load DOMApplicationRegistry into a local scope to avoid appearing michael@0: // to leak it. michael@0: michael@0: let scope = {}; michael@0: Cu.import("resource://gre/modules/Webapps.jsm", scope); michael@0: Cu.import("resource://webapprt/modules/Startup.jsm", scope); michael@0: scope.DOMApplicationRegistry.confirmInstall(JSON.parse(data)); michael@0: michael@0: let installRecord = JSON.parse(data); michael@0: installRecord.mm = subj; michael@0: installRecord.registryDir = Services.dirsvc.get("ProfD", Ci.nsIFile).path; michael@0: WebappRT.config = installRecord; michael@0: michael@0: let win = Services.wm.getMostRecentWindow("webapprt:webapp"); michael@0: if (!win) { michael@0: win = Services.ww.openWindow(null, michael@0: "chrome://webapprt/content/webapp.xul", michael@0: "_blank", michael@0: "chrome,dialog=no,resizable,scrollbars,centerscreen", michael@0: null); michael@0: } michael@0: michael@0: let promise = scope.startup(win); michael@0: michael@0: // During chrome tests, we use the same window to load all the tests. We michael@0: // need to change the buildID so that the permissions for the currently michael@0: // tested application get installed. michael@0: Services.prefs.setCharPref("webapprt.buildID", WebappRT.config.app.manifestURL); michael@0: michael@0: // During tests, the webapps registry is already loaded. michael@0: // The Startup module needs to be notified when the webapps registry michael@0: // gets loaded, so we do that now. michael@0: Services.obs.notifyObservers(this, "webapps-registry-start", null); michael@0: michael@0: promise.then(onBecome); michael@0: } michael@0: Services.obs.addObserver(observeInstall, "webapps-ask-install", false); michael@0: michael@0: // Step 1: Install the app at the URL specified by the manifest. michael@0: navigator.mozApps.install(manifestURL, parameters); michael@0: }