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/. */
7 const Cu = Components.utils;
8 const Ci = Components.interfaces;
10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
11 Cu.import("resource://gre/modules/Services.jsm");
12 Cu.import("resource://gre/modules/Promise.jsm");
14 this.EXPORTED_SYMBOLS = ["ScriptPreloader"];
16 function debug(aMsg) {
17 //dump("--*-- ScriptPreloader: " + aMsg + "\n");
20 this.ScriptPreloader = {
27 preload: function(aApp, aManifest) {
28 debug("Preloading " + aApp.origin);
29 let deferred = Promise.defer();
33 return deferred.promise;
36 if (aManifest.precompile &&
37 Array.isArray(aManifest.precompile) &&
38 aManifest.precompile.length > 0) {
39 let origin = Services.io.newURI(aApp.origin, null, null);
40 let toLoad = aManifest.precompile.length;
42 Services.scriptSecurityManager
43 .getAppCodebasePrincipal(origin, aApp.localId, false);
45 aManifest.precompile.forEach((aPath) => {
46 let uri = Services.io.newURI(aPath, null, origin);
47 debug("Script to compile: " + uri.spec);
49 Services.scriptloader.precompileScript(uri, principal,
50 (aSubject, aTopic, aData) => {
51 let uri = aSubject.QueryInterface(Ci.nsIURI);
52 debug("Done compiling " + uri.spec);
60 // Resolve the promise if precompileScript throws.
65 // The precompile field is not an array, let the developer know.
66 // We don't want to have to enable debug for that to show up.
67 if (aManifest.precompile) {
68 Cu.reportError("ASM.JS compilation failed: the 'precompile' manifest " +
69 "property should be an array of script uris.\n");
74 return deferred.promise;