summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/sanitize/browser_cookiePermission_aboutURL.js
blob: ff6badb53549c825c521bb8eaf52c9a61683607c (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
const { SiteDataTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/SiteDataTestUtils.sys.mjs"
);

// We will be removing the ["cookies","offlineApps"] option once we remove the
// old clear history dialog in Bug 1856418 - Remove all old clear data dialog boxes
let prefs = [["cookiesAndStorage"], ["cookies", "offlineApps"]];

function checkDataForAboutURL() {
  return new Promise(resolve => {
    let data = true;
    let uri = Services.io.newURI("about:newtab");
    let principal = Services.scriptSecurityManager.createContentPrincipal(
      uri,
      {}
    );
    let request = indexedDB.openForPrincipal(principal, "TestDatabase", 1);
    request.onupgradeneeded = function () {
      data = false;
    };
    request.onsuccess = function () {
      resolve(data);
    };
  });
}

for (let itemsToClear of prefs) {
  add_task(async function deleteStorageInAboutURL() {
    info("Test about:newtab");

    // Let's clean up all the data.
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, resolve);
    });

    await SpecialPowers.pushPrefEnv({
      set: [["browser.sanitizer.loglevel", "All"]],
    });

    // Let's create a tab with some data.
    await SiteDataTestUtils.addToIndexedDB("about:newtab", "foo", "bar", {});

    ok(await checkDataForAboutURL(), "We have data for about:newtab");

    // Cleaning up.
    await Sanitizer.runSanitizeOnShutdown();

    ok(await checkDataForAboutURL(), "about:newtab data is not deleted.");

    // Clean up.
    await Sanitizer.sanitize(itemsToClear);

    let principal =
      Services.scriptSecurityManager.createContentPrincipalFromOrigin(
        "about:newtab"
      );
    await new Promise(aResolve => {
      let req = Services.qms.clearStoragesForPrincipal(principal);
      req.callback = () => {
        aResolve();
      };
    });
  });

  add_task(async function deleteStorageOnlyCustomPermissionInAboutURL() {
    info("Test about:newtab + permissions");

    // Let's clean up all the data.
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, resolve);
    });

    await SpecialPowers.pushPrefEnv({
      set: [["browser.sanitizer.loglevel", "All"]],
    });

    // Custom permission without considering OriginAttributes
    let uri = Services.io.newURI("about:newtab");
    PermissionTestUtils.add(
      uri,
      "cookie",
      Ci.nsICookiePermission.ACCESS_SESSION
    );

    // Let's create a tab with some data.
    await SiteDataTestUtils.addToIndexedDB("about:newtab", "foo", "bar", {});

    ok(await checkDataForAboutURL(), "We have data for about:newtab");

    // Cleaning up.
    await Sanitizer.runSanitizeOnShutdown();

    ok(await checkDataForAboutURL(), "about:newtab data is not deleted.");

    // Clean up.
    await Sanitizer.sanitize(itemsToClear);

    let principal =
      Services.scriptSecurityManager.createContentPrincipalFromOrigin(
        "about:newtab"
      );
    await new Promise(aResolve => {
      let req = Services.qms.clearStoragesForPrincipal(principal);
      req.callback = () => {
        aResolve();
      };
    });

    PermissionTestUtils.remove(uri, "cookie");
  });
}