1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/apps/src/ScriptPreloader.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,76 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const Cu = Components.utils; 1.11 +const Ci = Components.interfaces; 1.12 + 1.13 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.14 +Cu.import("resource://gre/modules/Services.jsm"); 1.15 +Cu.import("resource://gre/modules/Promise.jsm"); 1.16 + 1.17 +this.EXPORTED_SYMBOLS = ["ScriptPreloader"]; 1.18 + 1.19 +function debug(aMsg) { 1.20 + //dump("--*-- ScriptPreloader: " + aMsg + "\n"); 1.21 +} 1.22 + 1.23 +this.ScriptPreloader = { 1.24 +#ifdef MOZ_B2G 1.25 + _enabled: true, 1.26 +#else 1.27 + _enabled: false, 1.28 +#endif 1.29 + 1.30 + preload: function(aApp, aManifest) { 1.31 + debug("Preloading " + aApp.origin); 1.32 + let deferred = Promise.defer(); 1.33 + 1.34 + if (!this._enabled) { 1.35 + deferred.resolve(); 1.36 + return deferred.promise; 1.37 + } 1.38 + 1.39 + if (aManifest.precompile && 1.40 + Array.isArray(aManifest.precompile) && 1.41 + aManifest.precompile.length > 0) { 1.42 + let origin = Services.io.newURI(aApp.origin, null, null); 1.43 + let toLoad = aManifest.precompile.length; 1.44 + let principal = 1.45 + Services.scriptSecurityManager 1.46 + .getAppCodebasePrincipal(origin, aApp.localId, false); 1.47 + 1.48 + aManifest.precompile.forEach((aPath) => { 1.49 + let uri = Services.io.newURI(aPath, null, origin); 1.50 + debug("Script to compile: " + uri.spec); 1.51 + try { 1.52 + Services.scriptloader.precompileScript(uri, principal, 1.53 + (aSubject, aTopic, aData) => { 1.54 + let uri = aSubject.QueryInterface(Ci.nsIURI); 1.55 + debug("Done compiling " + uri.spec); 1.56 + 1.57 + toLoad--; 1.58 + if (toLoad == 0) { 1.59 + deferred.resolve(); 1.60 + } 1.61 + }); 1.62 + } catch (e) { 1.63 + // Resolve the promise if precompileScript throws. 1.64 + deferred.resolve(); 1.65 + } 1.66 + }); 1.67 + } else { 1.68 + // The precompile field is not an array, let the developer know. 1.69 + // We don't want to have to enable debug for that to show up. 1.70 + if (aManifest.precompile) { 1.71 + Cu.reportError("ASM.JS compilation failed: the 'precompile' manifest " + 1.72 + "property should be an array of script uris.\n"); 1.73 + } 1.74 + deferred.resolve(); 1.75 + } 1.76 + 1.77 + return deferred.promise; 1.78 + } 1.79 +}