summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/tests/mochitest/head.js
blob: f105b1718dee1fea8950398df0efdcce3db9e16f (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
// calculate the fullhash and send it to gethash server
function addCompletionToServer(list, url, mochitestUrl) {
  return new Promise(function (resolve) {
    var listParam = "list=" + list;
    var fullhashParam = "fullhash=" + hash(url);

    var xhr = new XMLHttpRequest();
    xhr.open("PUT", mochitestUrl + "?" + listParam + "&" + fullhashParam, true);
    xhr.setRequestHeader("Content-Type", "text/plain");
    xhr.onreadystatechange = function () {
      if (this.readyState == this.DONE) {
        resolve();
      }
    };
    xhr.send();
  });
}

function hash(str) {
  var hasher = SpecialPowers.Cc["@mozilla.org/security/hash;1"].createInstance(
    SpecialPowers.Ci.nsICryptoHash
  );

  var data = new TextEncoder().encode(str);
  hasher.init(hasher.SHA256);
  hasher.update(data, data.length);

  return hasher.finish(true);
}

var pushPrefs = (...p) => SpecialPowers.pushPrefEnv({ set: p });

function whenDelayedStartupFinished(aWindow, aCallback) {
  Services.obs.addObserver(function observer(aSubject, aTopic) {
    if (aWindow == aSubject) {
      Services.obs.removeObserver(observer, aTopic);
      setTimeout(aCallback, 0);
    }
  }, "browser-delayed-startup-finished");
}