michael@0: /* This code is mostly copied from chrome/test/unit/head_crtestutils.js */ michael@0: michael@0: const NS_CHROME_MANIFESTS_FILE_LIST = "ChromeML"; michael@0: const XUL_CACHE_PREF = "nglayout.debug.disable_xul_cache"; michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: michael@0: let gDirSvc = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIDirectoryService).QueryInterface(Ci.nsIProperties); michael@0: let gChromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"]. michael@0: getService(Ci.nsIXULChromeRegistry); michael@0: let gPrefs = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefBranch); michael@0: michael@0: // Create the temporary file in the profile, instead of in TmpD, because michael@0: // we know the mochitest harness kills off the profile when it's done. michael@0: function copyToTemporaryFile(f) michael@0: { michael@0: let tmpd = gDirSvc.get("ProfD", Ci.nsIFile); michael@0: tmpf = tmpd.clone(); michael@0: tmpf.append("temp.manifest"); michael@0: tmpf.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600); michael@0: tmpf.remove(false); michael@0: f.copyTo(tmpd, tmpf.leafName); michael@0: return tmpf; michael@0: } michael@0: michael@0: function dirIter(directory) michael@0: { michael@0: var ioSvc = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: var testsDir = ioSvc.newURI(directory, null, null) michael@0: .QueryInterface(Ci.nsIFileURL).file; michael@0: michael@0: let en = testsDir.directoryEntries; michael@0: while (en.hasMoreElements()) { michael@0: let file = en.getNext(); michael@0: yield file.QueryInterface(Ci.nsIFile); michael@0: } michael@0: } michael@0: michael@0: function getParent(path) { michael@0: let lastSlash = path.lastIndexOf("/"); michael@0: if (lastSlash == -1) { michael@0: lastSlash = path.lastIndexOf("\\"); michael@0: if (lastSlash == -1) { michael@0: return ""; michael@0: } michael@0: return '/' + path.substring(0, lastSlash).replace(/\\/g, '/'); michael@0: } michael@0: return path.substring(0, lastSlash); michael@0: } michael@0: michael@0: function copyDirToTempProfile(path, subdirname) { michael@0: michael@0: if (subdirname === undefined) { michael@0: subdirname = "mochikit-tmp"; michael@0: } michael@0: michael@0: let tmpdir = gDirSvc.get("ProfD", Ci.nsIFile); michael@0: tmpdir.append(subdirname); michael@0: tmpdir.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777); michael@0: michael@0: let rootDir = getParent(path); michael@0: if (rootDir == "") { michael@0: return tmpdir; michael@0: } michael@0: michael@0: // The SimpleTest directory is hidden michael@0: var files = [file for (file in dirIter('file://' +rootDir))]; michael@0: for (f in files) { michael@0: files[f].copyTo(tmpdir, ""); michael@0: } michael@0: return tmpdir; michael@0: michael@0: } michael@0: michael@0: function convertChromeURI(chromeURI) michael@0: { michael@0: let uri = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService).newURI(chromeURI, null, null); michael@0: return gChromeReg.convertChromeURL(uri); michael@0: } michael@0: michael@0: function chromeURIToFile(chromeURI) michael@0: { michael@0: var jar = getJar(chromeURI); michael@0: if (jar) { michael@0: var tmpDir = extractJarToTmp(jar); michael@0: let parts = chromeURI.split('/'); michael@0: if (parts[parts.length - 1] != '') { michael@0: tmpDir.append(parts[parts.length - 1]); michael@0: } michael@0: return tmpDir; michael@0: } michael@0: michael@0: return convertChromeURI(chromeURI). michael@0: QueryInterface(Ci.nsIFileURL).file; michael@0: } michael@0: michael@0: // Register a chrome manifest temporarily and return a function which un-does michael@0: // the registrarion when no longer needed. michael@0: function createManifestTemporarily(tempDir, manifestText) michael@0: { michael@0: gPrefs.setBoolPref(XUL_CACHE_PREF, true); michael@0: michael@0: tempDir.append("temp.manifest"); michael@0: michael@0: let foStream = Cc["@mozilla.org/network/file-output-stream;1"] michael@0: .createInstance(Ci.nsIFileOutputStream); michael@0: foStream.init(tempDir, michael@0: 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate michael@0: foStream.write(manifestText, manifestText.length); michael@0: foStream.close(); michael@0: let tempfile = copyToTemporaryFile(tempDir); michael@0: michael@0: Components.manager.QueryInterface(Ci.nsIComponentRegistrar). michael@0: autoRegister(tempfile); michael@0: michael@0: gChromeReg.refreshSkins(); michael@0: michael@0: return function() { michael@0: tempfile.fileSize = 0; // truncate the manifest michael@0: gChromeReg.checkForNewChrome(); michael@0: gChromeReg.refreshSkins(); michael@0: gPrefs.clearUserPref(XUL_CACHE_PREF); michael@0: } michael@0: } michael@0: michael@0: // Register a chrome manifest temporarily and return a function which un-does michael@0: // the registrarion when no longer needed. michael@0: function registerManifestTemporarily(manifestURI) michael@0: { michael@0: gPrefs.setBoolPref(XUL_CACHE_PREF, true); michael@0: michael@0: let file = chromeURIToFile(manifestURI); michael@0: michael@0: let tempfile = copyToTemporaryFile(file); michael@0: Components.manager.QueryInterface(Ci.nsIComponentRegistrar). michael@0: autoRegister(tempfile); michael@0: michael@0: gChromeReg.refreshSkins(); michael@0: michael@0: return function() { michael@0: tempfile.fileSize = 0; // truncate the manifest michael@0: gChromeReg.checkForNewChrome(); michael@0: gChromeReg.refreshSkins(); michael@0: gPrefs.clearUserPref(XUL_CACHE_PREF); michael@0: } michael@0: } michael@0: michael@0: function registerManifestPermanently(manifestURI) michael@0: { michael@0: var chromepath = chromeURIToFile(manifestURI); michael@0: michael@0: Components.manager.QueryInterface(Ci.nsIComponentRegistrar). michael@0: autoRegister(chromepath); michael@0: return chromepath; michael@0: }