toolkit/mozapps/installer/precompile_cache.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/installer/precompile_cache.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// see http://mxr.mozilla.org/mozilla-central/source/services/sync/Weave.js#76
    1.11 +
    1.12 +const Cc = Components.classes;
    1.13 +const Ci = Components.interfaces;
    1.14 +const Cu = Components.utils;
    1.15 +
    1.16 +Cu.import("resource://gre/modules/Services.jsm");
    1.17 +
    1.18 +const rph = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
    1.19 +
    1.20 +function endsWith(str, end) {
    1.21 +  return str.slice(-end.length) == end;
    1.22 +}
    1.23 +
    1.24 +function jar_entries(jarReader, pattern) {
    1.25 +  var entries = [];
    1.26 +  var enumerator = jarReader.findEntries(pattern);
    1.27 +  while (enumerator.hasMore()) {
    1.28 +    entries.push(enumerator.getNext());
    1.29 +  }
    1.30 +  return entries;
    1.31 +}
    1.32 +
    1.33 +function dir_entries(baseDir, subpath, ext) {
    1.34 +  var dir = baseDir.clone();
    1.35 +  dir.append(subpath);
    1.36 +  var enumerator = dir.directoryEntries;
    1.37 +  var entries = [];
    1.38 +  while (enumerator.hasMoreElements()) {
    1.39 +    var file = enumerator.getNext().QueryInterface(Ci.nsIFile);
    1.40 +    if (file.isDirectory()) {
    1.41 +      entries = entries.concat(dir_entries(dir, file.leafName, ext).map(function(p) subpath + "/" + p));
    1.42 +    } else if (endsWith(file.leafName, ext)) {
    1.43 +      entries.push(subpath + "/" + file.leafName);
    1.44 +    }
    1.45 +  }
    1.46 +  return entries;
    1.47 +}
    1.48 +
    1.49 +function get_modules_under(uri) {
    1.50 +  if (uri instanceof Ci.nsIJARURI) {
    1.51 +    var jar = uri.QueryInterface(Ci.nsIJARURI);
    1.52 +    var jarReader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance(Ci.nsIZipReader);
    1.53 +    var file = jar.JARFile.QueryInterface(Ci.nsIFileURL);
    1.54 +    jarReader.open(file.file);
    1.55 +    var entries = jar_entries(jarReader, "components/*.js")
    1.56 +                  .concat(jar_entries(jarReader, "modules/*.js"))
    1.57 +                  .concat(jar_entries(jarReader, "modules/*.jsm"));
    1.58 +    jarReader.close();
    1.59 +    return entries;
    1.60 +  } else if (uri instanceof Ci.nsIFileURL){
    1.61 +    var file = uri.QueryInterface(Ci.nsIFileURL);
    1.62 +    return dir_entries(file.file, "components", ".js")
    1.63 +           .concat(dir_entries(file.file, "modules", ".js"))
    1.64 +           .concat(dir_entries(file.file, "modules", ".jsm"));
    1.65 +  } else {
    1.66 +    throw "Expected a nsIJARURI or nsIFileURL";
    1.67 +  }
    1.68 +}
    1.69 +
    1.70 +function load_modules_under(spec, uri) {
    1.71 +  var entries = get_modules_under(uri).sort();
    1.72 +  // The precompilation of JS here sometimes reports errors, which we don't
    1.73 +  // really care about. But if the errors are ever reported to xpcshell's
    1.74 +  // error reporter, it will cause it to return an error code, which will break
    1.75 +  // automation. Currently they won't be, because the component loader spins up
    1.76 +  // its JSContext before xpcshell has time to set its context callback (which
    1.77 +  // overrides the error reporter on all newly-created JSContexts). But as we
    1.78 +  // move towards a singled-cxed browser, we'll run into this. So let's be
    1.79 +  // forward-thinking and deal with it now.
    1.80 +  ignoreReportedErrors(true);
    1.81 +  for each (let entry in entries) {
    1.82 +    try {
    1.83 +      dump(spec + entry + "\n");
    1.84 +      Cu.import(spec + entry, null);
    1.85 +    } catch(e) {}
    1.86 +  }
    1.87 +  ignoreReportedErrors(false);
    1.88 +}
    1.89 +
    1.90 +function resolveResource(spec) {
    1.91 +  var uri = Services.io.newURI(spec, null, null);
    1.92 +  return Services.io.newURI(rph.resolveURI(uri), null, null);
    1.93 +}
    1.94 +
    1.95 +function precompile_startupcache(uri) {
    1.96 +  load_modules_under(uri, resolveResource(uri));
    1.97 +}

mercurial