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

http://github.com/zpao/v8monkey · JavaScript · 161 lines · 124 code · 29 blank · 8 comment · 13 complexity · 571a6a2241a451ad8f871ad6f579ee9b MD5 · raw file

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