Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const Cu = Components.utils; |
michael@0 | 8 | const Ci = Components.interfaces; |
michael@0 | 9 | |
michael@0 | 10 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 11 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 12 | Cu.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 13 | |
michael@0 | 14 | this.EXPORTED_SYMBOLS = ["ScriptPreloader"]; |
michael@0 | 15 | |
michael@0 | 16 | function debug(aMsg) { |
michael@0 | 17 | //dump("--*-- ScriptPreloader: " + aMsg + "\n"); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | this.ScriptPreloader = { |
michael@0 | 21 | #ifdef MOZ_B2G |
michael@0 | 22 | _enabled: true, |
michael@0 | 23 | #else |
michael@0 | 24 | _enabled: false, |
michael@0 | 25 | #endif |
michael@0 | 26 | |
michael@0 | 27 | preload: function(aApp, aManifest) { |
michael@0 | 28 | debug("Preloading " + aApp.origin); |
michael@0 | 29 | let deferred = Promise.defer(); |
michael@0 | 30 | |
michael@0 | 31 | if (!this._enabled) { |
michael@0 | 32 | deferred.resolve(); |
michael@0 | 33 | return deferred.promise; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | if (aManifest.precompile && |
michael@0 | 37 | Array.isArray(aManifest.precompile) && |
michael@0 | 38 | aManifest.precompile.length > 0) { |
michael@0 | 39 | let origin = Services.io.newURI(aApp.origin, null, null); |
michael@0 | 40 | let toLoad = aManifest.precompile.length; |
michael@0 | 41 | let principal = |
michael@0 | 42 | Services.scriptSecurityManager |
michael@0 | 43 | .getAppCodebasePrincipal(origin, aApp.localId, false); |
michael@0 | 44 | |
michael@0 | 45 | aManifest.precompile.forEach((aPath) => { |
michael@0 | 46 | let uri = Services.io.newURI(aPath, null, origin); |
michael@0 | 47 | debug("Script to compile: " + uri.spec); |
michael@0 | 48 | try { |
michael@0 | 49 | Services.scriptloader.precompileScript(uri, principal, |
michael@0 | 50 | (aSubject, aTopic, aData) => { |
michael@0 | 51 | let uri = aSubject.QueryInterface(Ci.nsIURI); |
michael@0 | 52 | debug("Done compiling " + uri.spec); |
michael@0 | 53 | |
michael@0 | 54 | toLoad--; |
michael@0 | 55 | if (toLoad == 0) { |
michael@0 | 56 | deferred.resolve(); |
michael@0 | 57 | } |
michael@0 | 58 | }); |
michael@0 | 59 | } catch (e) { |
michael@0 | 60 | // Resolve the promise if precompileScript throws. |
michael@0 | 61 | deferred.resolve(); |
michael@0 | 62 | } |
michael@0 | 63 | }); |
michael@0 | 64 | } else { |
michael@0 | 65 | // The precompile field is not an array, let the developer know. |
michael@0 | 66 | // We don't want to have to enable debug for that to show up. |
michael@0 | 67 | if (aManifest.precompile) { |
michael@0 | 68 | Cu.reportError("ASM.JS compilation failed: the 'precompile' manifest " + |
michael@0 | 69 | "property should be an array of script uris.\n"); |
michael@0 | 70 | } |
michael@0 | 71 | deferred.resolve(); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | return deferred.promise; |
michael@0 | 75 | } |
michael@0 | 76 | } |