Bumping manifests a=b2g-bump
[gecko.git] / dom / apps / ScriptPreloader.jsm
blob1925a9b1d2db955bf58ed9e2b5b8d40bdf968772
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/. */
5 "use strict";
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 = {
21 #ifdef MOZ_B2G
22   _enabled: true,
23 #else
24   _enabled: false,
25 #endif
27   preload: function(aApp, aManifest) {
28     debug("Preloading " + aApp.origin);
29     let deferred = Promise.defer();
31     if (!this._enabled) {
32       deferred.resolve();
33       return deferred.promise;
34     }
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;
41       let principal =
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);
48         try {
49           Services.scriptloader.precompileScript(uri, principal,
50             (aSubject, aTopic, aData) => {
51               let uri = aSubject.QueryInterface(Ci.nsIURI);
52               debug("Done compiling " + uri.spec);
54               toLoad--;
55               if (toLoad == 0) {
56                 deferred.resolve();
57               }
58             });
59         } catch (e) {
60           // Resolve the promise if precompileScript throws.
61           deferred.resolve();
62         }
63       });
64     } else {
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");
70       }
71       deferred.resolve();
72     }
74     return deferred.promise;
75   }