summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/RegisterUnregisterChrome.js
blob: 26e4e80922f831e82df9ce10de37ea0939dbb73b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* This code is mostly copied from chrome/test/unit/head_crtestutils.js */

// This file assumes chrome-harness.js is loaded in the global scope.
/* import-globals-from ../../../../testing/mochitest/chrome-harness.js */

const NS_CHROME_MANIFESTS_FILE_LIST = "ChromeML";
const XUL_CACHE_PREF = "nglayout.debug.disable_xul_cache";

var gChromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(
  Ci.nsIXULChromeRegistry
);

// Create the temporary file in the profile, instead of in TmpD, because
// we know the mochitest harness kills off the profile when it's done.
function copyToTemporaryFile(f) {
  let tmpd = Services.dirsvc.get("ProfD", Ci.nsIFile);
  let tmpf = tmpd.clone();
  tmpf.append("temp.manifest");
  tmpf.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);
  tmpf.remove(false);
  f.copyTo(tmpd, tmpf.leafName);
  return tmpf;
}

function* dirIter(directory) {
  var testsDir = Services.io
    .newURI(directory)
    .QueryInterface(Ci.nsIFileURL).file;

  let en = testsDir.directoryEntries;
  while (en.hasMoreElements()) {
    yield en.nextFile;
  }
}

function getParent(path) {
  let lastSlash = path.lastIndexOf("/");
  if (lastSlash == -1) {
    lastSlash = path.lastIndexOf("\\");
    if (lastSlash == -1) {
      return "";
    }
    return "/" + path.substring(0, lastSlash).replace(/\\/g, "/");
  }
  return path.substring(0, lastSlash);
}

function copyDirToTempProfile(path, subdirname) {
  if (subdirname === undefined) {
    subdirname = "mochikit-tmp";
  }

  let tmpdir = Services.dirsvc.get("ProfD", Ci.nsIFile);
  tmpdir.append(subdirname);
  tmpdir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o777);

  let rootDir = getParent(path);
  if (rootDir == "") {
    return tmpdir;
  }

  // The SimpleTest directory is hidden
  var files = Array.from(dirIter("file://" + rootDir));
  for (let f in files) {
    files[f].copyTo(tmpdir, "");
  }
  return tmpdir;
}

function convertChromeURI(chromeURI) {
  let uri = Services.io.newURI(chromeURI);
  return gChromeReg.convertChromeURL(uri);
}

function chromeURIToFile(chromeURI) {
  var jar = getJar(chromeURI);
  if (jar) {
    var tmpDir = extractJarToTmp(jar);
    let parts = chromeURI.split("/");
    if (parts[parts.length - 1] != "") {
      tmpDir.append(parts[parts.length - 1]);
    }
    return tmpDir;
  }

  return convertChromeURI(chromeURI).QueryInterface(Ci.nsIFileURL).file;
}

// Register a chrome manifest temporarily and return a function which un-does
// the registrarion when no longer needed.
function createManifestTemporarily(tempDir, manifestText) {
  Services.prefs.setBoolPref(XUL_CACHE_PREF, true);

  tempDir.append("temp.manifest");

  let foStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(
    Ci.nsIFileOutputStream
  );
  foStream.init(tempDir, 0x02 | 0x08 | 0x20, 0o664, 0); // write, create, truncate
  foStream.write(manifestText, manifestText.length);
  foStream.close();
  let tempfile = copyToTemporaryFile(tempDir);

  Components.manager
    .QueryInterface(Ci.nsIComponentRegistrar)
    .autoRegister(tempfile);

  return function () {
    tempfile.fileSize = 0; // truncate the manifest
    gChromeReg.checkForNewChrome();
    Services.prefs.clearUserPref(XUL_CACHE_PREF);
  };
}

// Register a chrome manifest temporarily and return a function which un-does
// the registrarion when no longer needed.
function registerManifestTemporarily(manifestURI) {
  Services.prefs.setBoolPref(XUL_CACHE_PREF, true);

  let file = chromeURIToFile(manifestURI);

  let tempfile = copyToTemporaryFile(file);
  Components.manager
    .QueryInterface(Ci.nsIComponentRegistrar)
    .autoRegister(tempfile);

  return function () {
    tempfile.fileSize = 0; // truncate the manifest
    gChromeReg.checkForNewChrome();
    Services.prefs.clearUserPref(XUL_CACHE_PREF);
  };
}

function registerManifestPermanently(manifestURI) {
  var chromepath = chromeURIToFile(manifestURI);

  Components.manager
    .QueryInterface(Ci.nsIComponentRegistrar)
    .autoRegister(chromepath);
  return chromepath;
}