1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/content/tests/chrome/RegisterUnregisterChrome.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,161 @@ 1.4 +/* This code is mostly copied from chrome/test/unit/head_crtestutils.js */ 1.5 + 1.6 +const NS_CHROME_MANIFESTS_FILE_LIST = "ChromeML"; 1.7 +const XUL_CACHE_PREF = "nglayout.debug.disable_xul_cache"; 1.8 + 1.9 +const Cc = Components.classes; 1.10 +const Ci = Components.interfaces; 1.11 +const Cr = Components.results; 1.12 + 1.13 +let gDirSvc = Cc["@mozilla.org/file/directory_service;1"]. 1.14 + getService(Ci.nsIDirectoryService).QueryInterface(Ci.nsIProperties); 1.15 +let gChromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"]. 1.16 + getService(Ci.nsIXULChromeRegistry); 1.17 +let gPrefs = Cc["@mozilla.org/preferences-service;1"]. 1.18 + getService(Ci.nsIPrefBranch); 1.19 + 1.20 +// Create the temporary file in the profile, instead of in TmpD, because 1.21 +// we know the mochitest harness kills off the profile when it's done. 1.22 +function copyToTemporaryFile(f) 1.23 +{ 1.24 + let tmpd = gDirSvc.get("ProfD", Ci.nsIFile); 1.25 + tmpf = tmpd.clone(); 1.26 + tmpf.append("temp.manifest"); 1.27 + tmpf.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600); 1.28 + tmpf.remove(false); 1.29 + f.copyTo(tmpd, tmpf.leafName); 1.30 + return tmpf; 1.31 +} 1.32 + 1.33 +function dirIter(directory) 1.34 +{ 1.35 + var ioSvc = Cc["@mozilla.org/network/io-service;1"]. 1.36 + getService(Ci.nsIIOService); 1.37 + var testsDir = ioSvc.newURI(directory, null, null) 1.38 + .QueryInterface(Ci.nsIFileURL).file; 1.39 + 1.40 + let en = testsDir.directoryEntries; 1.41 + while (en.hasMoreElements()) { 1.42 + let file = en.getNext(); 1.43 + yield file.QueryInterface(Ci.nsIFile); 1.44 + } 1.45 +} 1.46 + 1.47 +function getParent(path) { 1.48 + let lastSlash = path.lastIndexOf("/"); 1.49 + if (lastSlash == -1) { 1.50 + lastSlash = path.lastIndexOf("\\"); 1.51 + if (lastSlash == -1) { 1.52 + return ""; 1.53 + } 1.54 + return '/' + path.substring(0, lastSlash).replace(/\\/g, '/'); 1.55 + } 1.56 + return path.substring(0, lastSlash); 1.57 +} 1.58 + 1.59 +function copyDirToTempProfile(path, subdirname) { 1.60 + 1.61 + if (subdirname === undefined) { 1.62 + subdirname = "mochikit-tmp"; 1.63 + } 1.64 + 1.65 + let tmpdir = gDirSvc.get("ProfD", Ci.nsIFile); 1.66 + tmpdir.append(subdirname); 1.67 + tmpdir.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777); 1.68 + 1.69 + let rootDir = getParent(path); 1.70 + if (rootDir == "") { 1.71 + return tmpdir; 1.72 + } 1.73 + 1.74 + // The SimpleTest directory is hidden 1.75 + var files = [file for (file in dirIter('file://' +rootDir))]; 1.76 + for (f in files) { 1.77 + files[f].copyTo(tmpdir, ""); 1.78 + } 1.79 + return tmpdir; 1.80 + 1.81 +} 1.82 + 1.83 +function convertChromeURI(chromeURI) 1.84 +{ 1.85 + let uri = Cc["@mozilla.org/network/io-service;1"]. 1.86 + getService(Ci.nsIIOService).newURI(chromeURI, null, null); 1.87 + return gChromeReg.convertChromeURL(uri); 1.88 +} 1.89 + 1.90 +function chromeURIToFile(chromeURI) 1.91 +{ 1.92 + var jar = getJar(chromeURI); 1.93 + if (jar) { 1.94 + var tmpDir = extractJarToTmp(jar); 1.95 + let parts = chromeURI.split('/'); 1.96 + if (parts[parts.length - 1] != '') { 1.97 + tmpDir.append(parts[parts.length - 1]); 1.98 + } 1.99 + return tmpDir; 1.100 + } 1.101 + 1.102 + return convertChromeURI(chromeURI). 1.103 + QueryInterface(Ci.nsIFileURL).file; 1.104 +} 1.105 + 1.106 +// Register a chrome manifest temporarily and return a function which un-does 1.107 +// the registrarion when no longer needed. 1.108 +function createManifestTemporarily(tempDir, manifestText) 1.109 +{ 1.110 + gPrefs.setBoolPref(XUL_CACHE_PREF, true); 1.111 + 1.112 + tempDir.append("temp.manifest"); 1.113 + 1.114 + let foStream = Cc["@mozilla.org/network/file-output-stream;1"] 1.115 + .createInstance(Ci.nsIFileOutputStream); 1.116 + foStream.init(tempDir, 1.117 + 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate 1.118 + foStream.write(manifestText, manifestText.length); 1.119 + foStream.close(); 1.120 + let tempfile = copyToTemporaryFile(tempDir); 1.121 + 1.122 + Components.manager.QueryInterface(Ci.nsIComponentRegistrar). 1.123 + autoRegister(tempfile); 1.124 + 1.125 + gChromeReg.refreshSkins(); 1.126 + 1.127 + return function() { 1.128 + tempfile.fileSize = 0; // truncate the manifest 1.129 + gChromeReg.checkForNewChrome(); 1.130 + gChromeReg.refreshSkins(); 1.131 + gPrefs.clearUserPref(XUL_CACHE_PREF); 1.132 + } 1.133 +} 1.134 + 1.135 +// Register a chrome manifest temporarily and return a function which un-does 1.136 +// the registrarion when no longer needed. 1.137 +function registerManifestTemporarily(manifestURI) 1.138 +{ 1.139 + gPrefs.setBoolPref(XUL_CACHE_PREF, true); 1.140 + 1.141 + let file = chromeURIToFile(manifestURI); 1.142 + 1.143 + let tempfile = copyToTemporaryFile(file); 1.144 + Components.manager.QueryInterface(Ci.nsIComponentRegistrar). 1.145 + autoRegister(tempfile); 1.146 + 1.147 + gChromeReg.refreshSkins(); 1.148 + 1.149 + return function() { 1.150 + tempfile.fileSize = 0; // truncate the manifest 1.151 + gChromeReg.checkForNewChrome(); 1.152 + gChromeReg.refreshSkins(); 1.153 + gPrefs.clearUserPref(XUL_CACHE_PREF); 1.154 + } 1.155 +} 1.156 + 1.157 +function registerManifestPermanently(manifestURI) 1.158 +{ 1.159 + var chromepath = chromeURIToFile(manifestURI); 1.160 + 1.161 + Components.manager.QueryInterface(Ci.nsIComponentRegistrar). 1.162 + autoRegister(chromepath); 1.163 + return chromepath; 1.164 +}