summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/localstorage/localStorageCommon.js
blob: 364e7088701bf99963c3d8b6d513d4fcdb8d35a7 (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
function localStorageFlush(cb) {
  if (SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled) {
    SimpleTest.executeSoon(function () {
      cb();
    });
    return;
  }

  var ob = {
    observe(sub, top, dat) {
      os().removeObserver(ob, "domstorage-test-flushed");
      cb();
    },
  };
  os().addObserver(ob, "domstorage-test-flushed");
  notify("domstorage-test-flush-force");
}

function localStorageReload(callback) {
  if (SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled) {
    localStorage.close();
    let qms = SpecialPowers.Services.qms;
    let principal = SpecialPowers.wrap(document).nodePrincipal;
    let request = qms.resetStoragesForPrincipal(principal, "default", "ls");
    request.callback = SpecialPowers.wrapCallback(function () {
      localStorage.open();
      callback();
    });
    return;
  }

  notify("domstorage-test-reload");
  SimpleTest.executeSoon(function () {
    callback();
  });
}

function localStorageFlushAndReload(callback) {
  if (SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled) {
    localStorage.close();
    let qms = SpecialPowers.Services.qms;
    let principal = SpecialPowers.wrap(document).nodePrincipal;
    let request = qms.resetStoragesForPrincipal(principal, "default", "ls");
    request.callback = SpecialPowers.wrapCallback(function () {
      localStorage.open();
      callback();
    });
    return;
  }

  localStorageFlush(function () {
    localStorageReload(callback);
  });
}

function localStorageClearAll(callback) {
  if (SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled) {
    let qms = SpecialPowers.Services.qms;
    let ssm = SpecialPowers.Services.scriptSecurityManager;

    qms.getUsage(
      SpecialPowers.wrapCallback(function (request) {
        if (request.resultCode != SpecialPowers.Cr.NS_OK) {
          callback();
          return;
        }

        let clearRequestCount = 0;
        for (let item of request.result) {
          let principal = ssm.createContentPrincipalFromOrigin(item.origin);
          let clearRequest = qms.clearStoragesForPrincipal(
            principal,
            "default",
            "ls"
          );
          clearRequestCount++;
          clearRequest.callback = SpecialPowers.wrapCallback(function () {
            if (--clearRequestCount == 0) {
              callback();
            }
          });
        }
      })
    );
    return;
  }

  os().notifyObservers(null, "cookie-changed", "cleared");
  SimpleTest.executeSoon(function () {
    callback();
  });
}

function localStorageClearDomain(domain, callback) {
  if (SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled) {
    let qms = SpecialPowers.Services.qms;
    let principal = SpecialPowers.wrap(document).effectiveStoragePrincipal;
    let request = qms.clearStoragesForPrincipal(principal, "default", "ls");
    let cb = SpecialPowers.wrapCallback(callback);
    request.callback = cb;
    return;
  }

  os().notifyObservers(null, "extension:purge-localStorage", domain);
  SimpleTest.executeSoon(function () {
    callback();
  });
}

function os() {
  return SpecialPowers.Services.obs;
}

function notify(top) {
  os().notifyObservers(null, top);
}

/**
 * Enable testing observer notifications in DOMStorageObserver.cpp.
 */
function localStorageEnableTestingMode(cb) {
  SpecialPowers.pushPrefEnv(
    {
      set: [
        ["dom.storage.testing", true],
        ["dom.quotaManager.testing", true],
      ],
    },
    cb
  );
}