PageRenderTime 53ms CodeModel.GetById 19ms app.highlight 27ms RepoModel.GetById 2ms app.codeStats 0ms

/toolkit/content/tests/chrome/RegisterUnregisterChrome.js

http://github.com/zpao/v8monkey
JavaScript | 161 lines | 151 code | 7 blank | 3 comment | 3 complexity | 571a6a2241a451ad8f871ad6f579ee9b MD5 | raw file
  1/* This code is mostly copied from chrome/test/unit/head_crtestutils.js */
  2
  3const NS_CHROME_MANIFESTS_FILE_LIST = "ChromeML";
  4const XUL_CACHE_PREF = "nglayout.debug.disable_xul_cache";
  5
  6const Cc = Components.classes;
  7const Ci = Components.interfaces;
  8const Cr = Components.results;
  9
 10let gDirSvc    = Cc["@mozilla.org/file/directory_service;1"].
 11  getService(Ci.nsIDirectoryService).QueryInterface(Ci.nsIProperties);
 12let gChromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"].
 13                    getService(Ci.nsIXULChromeRegistry);
 14let gPrefs     = Cc["@mozilla.org/preferences-service;1"].
 15                    getService(Ci.nsIPrefBranch);
 16
 17// Create the temporary file in the profile, instead of in TmpD, because
 18// we know the mochitest harness kills off the profile when it's done.
 19function copyToTemporaryFile(f)
 20{
 21  let tmpd = gDirSvc.get("ProfD", Ci.nsIFile);
 22  tmpf = tmpd.clone();
 23  tmpf.append("temp.manifest");
 24  tmpf.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
 25  tmpf.remove(false);
 26  f.copyTo(tmpd, tmpf.leafName);
 27  return tmpf;
 28}
 29
 30function dirIter(directory)
 31{
 32  var ioSvc = Cc["@mozilla.org/network/io-service;1"].
 33              getService(Ci.nsIIOService);
 34  var testsDir = ioSvc.newURI(directory, null, null)
 35                  .QueryInterface(Ci.nsIFileURL).file;
 36
 37  let en = testsDir.directoryEntries;
 38  while (en.hasMoreElements()) {
 39    let file = en.getNext();
 40    yield file.QueryInterface(Ci.nsIFile);
 41  }
 42}
 43
 44function getParent(path) {
 45  let lastSlash = path.lastIndexOf("/");
 46  if (lastSlash == -1) {
 47    lastSlash = path.lastIndexOf("\\");
 48    if (lastSlash == -1) {
 49      return "";
 50    }
 51    return '/' + path.substring(0, lastSlash).replace(/\\/g, '/');
 52  }
 53  return path.substring(0, lastSlash);
 54}
 55
 56function copyDirToTempProfile(path, subdirname) {
 57
 58  if (subdirname === undefined) {
 59    subdirname = "mochikit-tmp";
 60  }
 61  
 62  let tmpdir = gDirSvc.get("ProfD", Ci.nsIFile);
 63  tmpdir.append(subdirname);
 64  tmpdir.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);
 65
 66  let rootDir = getParent(path);
 67  if (rootDir == "") {
 68    return tmpdir;
 69  }
 70
 71  // The SimpleTest directory is hidden
 72  var files = [file for (file in dirIter('file://' +rootDir))];
 73  for (f in files) {
 74    files[f].copyTo(tmpdir, "");
 75  }
 76  return tmpdir;
 77
 78}
 79
 80function convertChromeURI(chromeURI)
 81{
 82  let uri = Cc["@mozilla.org/network/io-service;1"].
 83    getService(Ci.nsIIOService).newURI(chromeURI, null, null);
 84  return gChromeReg.convertChromeURL(uri);
 85}
 86
 87function chromeURIToFile(chromeURI)
 88{
 89  var jar = getJar(chromeURI);
 90  if (jar) {
 91    var tmpDir = extractJarToTmp(jar);
 92    let parts = chromeURI.split('/');
 93    if (parts[parts.length - 1] != '') {
 94      tmpDir.append(parts[parts.length - 1]);
 95    }
 96    return tmpDir;
 97  }
 98
 99  return convertChromeURI(chromeURI).
100    QueryInterface(Ci.nsIFileURL).file;
101}  
102
103// Register a chrome manifest temporarily and return a function which un-does
104// the registrarion when no longer needed.
105function createManifestTemporarily(tempDir, manifestText)
106{
107  gPrefs.setBoolPref(XUL_CACHE_PREF, true);
108
109  tempDir.append("temp.manifest");
110
111  let foStream = Cc["@mozilla.org/network/file-output-stream;1"]
112                   .createInstance(Ci.nsIFileOutputStream);
113  foStream.init(tempDir,
114                0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
115  foStream.write(manifestText, manifestText.length);
116  foStream.close();
117  let tempfile = copyToTemporaryFile(tempDir);
118
119  Components.manager.QueryInterface(Ci.nsIComponentRegistrar).
120    autoRegister(tempfile);
121
122  gChromeReg.refreshSkins();
123
124  return function() {
125    tempfile.fileSize = 0; // truncate the manifest
126    gChromeReg.checkForNewChrome();
127    gChromeReg.refreshSkins();
128    gPrefs.clearUserPref(XUL_CACHE_PREF);
129  }
130}
131
132// Register a chrome manifest temporarily and return a function which un-does
133// the registrarion when no longer needed.
134function registerManifestTemporarily(manifestURI)
135{
136  gPrefs.setBoolPref(XUL_CACHE_PREF, true);
137
138  let file = chromeURIToFile(manifestURI);
139
140  let tempfile = copyToTemporaryFile(file);
141  Components.manager.QueryInterface(Ci.nsIComponentRegistrar).
142    autoRegister(tempfile);
143
144  gChromeReg.refreshSkins();
145
146  return function() {
147    tempfile.fileSize = 0; // truncate the manifest
148    gChromeReg.checkForNewChrome();
149    gChromeReg.refreshSkins();
150    gPrefs.clearUserPref(XUL_CACHE_PREF);
151  }
152}
153
154function registerManifestPermanently(manifestURI)
155{
156  var chromepath = chromeURIToFile(manifestURI);
157  
158  Components.manager.QueryInterface(Ci.nsIComponentRegistrar).
159    autoRegister(chromepath);
160  return chromepath;
161}