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: "use strict"; michael@0: michael@0: const Cu = Components.utils; michael@0: const Ci = Components.interfaces; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/Promise.jsm"); michael@0: michael@0: this.EXPORTED_SYMBOLS = ["ScriptPreloader"]; michael@0: michael@0: function debug(aMsg) { michael@0: //dump("--*-- ScriptPreloader: " + aMsg + "\n"); michael@0: } michael@0: michael@0: this.ScriptPreloader = { michael@0: #ifdef MOZ_B2G michael@0: _enabled: true, michael@0: #else michael@0: _enabled: false, michael@0: #endif michael@0: michael@0: preload: function(aApp, aManifest) { michael@0: debug("Preloading " + aApp.origin); michael@0: let deferred = Promise.defer(); michael@0: michael@0: if (!this._enabled) { michael@0: deferred.resolve(); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: if (aManifest.precompile && michael@0: Array.isArray(aManifest.precompile) && michael@0: aManifest.precompile.length > 0) { michael@0: let origin = Services.io.newURI(aApp.origin, null, null); michael@0: let toLoad = aManifest.precompile.length; michael@0: let principal = michael@0: Services.scriptSecurityManager michael@0: .getAppCodebasePrincipal(origin, aApp.localId, false); michael@0: michael@0: aManifest.precompile.forEach((aPath) => { michael@0: let uri = Services.io.newURI(aPath, null, origin); michael@0: debug("Script to compile: " + uri.spec); michael@0: try { michael@0: Services.scriptloader.precompileScript(uri, principal, michael@0: (aSubject, aTopic, aData) => { michael@0: let uri = aSubject.QueryInterface(Ci.nsIURI); michael@0: debug("Done compiling " + uri.spec); michael@0: michael@0: toLoad--; michael@0: if (toLoad == 0) { michael@0: deferred.resolve(); michael@0: } michael@0: }); michael@0: } catch (e) { michael@0: // Resolve the promise if precompileScript throws. michael@0: deferred.resolve(); michael@0: } michael@0: }); michael@0: } else { michael@0: // The precompile field is not an array, let the developer know. michael@0: // We don't want to have to enable debug for that to show up. michael@0: if (aManifest.precompile) { michael@0: Cu.reportError("ASM.JS compilation failed: the 'precompile' manifest " + michael@0: "property should be an array of script uris.\n"); michael@0: } michael@0: deferred.resolve(); michael@0: } michael@0: michael@0: return deferred.promise; michael@0: } michael@0: }