Skip to content

Instantly share code, notes, and snippets.

@lgarner
Created March 6, 2015 23:33

Revisions

  1. lgarner created this gist Mar 6, 2015.
    43 changes: 43 additions & 0 deletions get_certificate_fragment.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    getCertificate: function getCertificate(aManifestURL) {
    let manifestURL = "app://system.gaiamobile.org/manifest.webapp"; // change.
    Cu.import("resource://gre/modules/FileUtils.jsm");
    Cu.import("resource://gre/modules/NetUtil.jsm");

    XPCOMUtils.defineLazyServiceGetter(this, "appsService",
    "@mozilla.org/AppsService;1",
    "nsIAppsService");
    let app = appsService.getAppByManifestURL(manifestURL);
    if (!app) {
    debug("ZZZ: Missing app");
    return false;
    }

    let appDir = null;
    // Pre-installed apps (if Non-engr builds):
    appDir = FileUtils.getDir("coreAppsDir", ["webapps", app.id], false, true);
    if (!appDir.exists()) {
    // Privilaged apps:
    appDir = FileUtils.getDir("webappsDir", ["webapps", app.id], false, true);
    }
    // Since the app is supposed to be installed, this might work:
    // appDir = FileUtils.initWithPath(app.basePath);
    // appDir.append(app.id);
    let appPackage = appDir.clone();
    appPackage.append("application.zip");
    if (appPackage.exists()) {
    debug("ZZZ: object good.");
    } else {
    debug("ZZZ: object bad.");
    }

    let zipReader = Cc["@mozilla.org/libjar/zip-reader;1"]
    .createInstance(Ci.nsIZipReader);

    zipReader.open(appPackage);
    debug('has file: ' + zipReader.hasEntry("dev_cert.cer"));

    let devCertStream = zipReader.getInputStream("dev_cert.cer");
    let devCert = NetUtil.readInputStreamToString(devCertStream, devCertStream.available());
    devCertStream.close();
    debug("Dev cert: " + devCert);
    }