summaryrefslogtreecommitdiffstats
path: root/browser/components/contextualidentity/test/browser/browser_forgetAPI_quota_clearStoragesForPrincipal.js
blob: d4b57d08f66e3050eab0e908d9f945c61a65c2fd (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Bug 1278037 - A Test case for checking whether forgetting APIs are working for the quota manager.
 */

const CC = Components.Constructor;

const TEST_HOST = "example.com";
const TEST_URL =
  "http://" +
  TEST_HOST +
  "/browser/browser/components/contextualidentity/test/browser/";

const USER_CONTEXTS = ["default", "personal"];

//
// Support functions.
//

async function openTabInUserContext(uri, userContextId) {
  // Open the tab in the correct userContextId.
  let tab = BrowserTestUtils.addTab(gBrowser, uri, { userContextId });

  // Select tab and make sure its browser is focused.
  gBrowser.selectedTab = tab;
  tab.ownerGlobal.focus();

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);
  return { tab, browser };
}

// Setup an entry for the indexedDB.
async function setupIndexedDB(browser) {
  await SpecialPowers.spawn(
    browser,
    [{ input: "TestForgetAPIs" }],
    async function (arg) {
      let request = content.indexedDB.open("idb", 1);

      request.onerror = function () {
        throw new Error("error opening db connection");
      };

      request.onupgradeneeded = event => {
        let db = event.target.result;
        let store = db.createObjectStore("obj", { keyPath: "id" });
        store.createIndex("userContext", "userContext", { unique: false });
      };

      let db = await new Promise(resolve => {
        request.onsuccess = event => {
          resolve(event.target.result);
        };
      });

      // Add an entry into the indexedDB.
      let transaction = db.transaction(["obj"], "readwrite");
      let store = transaction.objectStore("obj");
      store.add({ id: 1, userContext: arg.input });

      await new Promise(resolve => {
        transaction.oncomplete = () => {
          resolve();
        };
      });

      // Check the indexedDB has been set properly.
      transaction = db.transaction(["obj"], "readonly");
      store = transaction.objectStore("obj");
      let getRequest = store.get(1);
      await new Promise(resolve => {
        getRequest.onsuccess = () => {
          let res = getRequest.result;
          is(res.userContext, arg.input, "Check the indexedDB value");
          resolve();
        };
      });
    }
  );
}

// Check whether the indexedDB has been cleared.
async function checkIndexedDB(browser) {
  await SpecialPowers.spawn(browser, [], async function () {
    let request = content.indexedDB.open("idb", 1);

    let db = await new Promise(done => {
      request.onsuccess = event => {
        done(event.target.result);
      };
    });

    try {
      db.transaction(["obj"], "readonly");
      ok(false, "The indexedDB should not exist");
    } catch (e) {
      is(e.name, "NotFoundError", "The indexedDB does not exist as expected");
    }

    db.close();

    content.indexedDB.deleteDatabase("idb");
  });
}

//
// Test functions.
//

add_setup(async function () {
  // Make sure userContext is enabled.
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.userContext.enabled", true]],
  });
});

add_task(async function test_quota_clearStoragesForPrincipal() {
  let tabs = [];

  for (let userContextId of Object.keys(USER_CONTEXTS)) {
    // Open our tab in the given user context.
    tabs[userContextId] = await openTabInUserContext(
      TEST_URL + "empty_file.html",
      userContextId
    );

    // Setup an entry for the indexedDB.
    await setupIndexedDB(tabs[userContextId].browser);

    // Close this tab.
    BrowserTestUtils.removeTab(tabs[userContextId].tab);
  }

  // Using quota manager to clear all indexed DB for a given domain.
  let caUtils = {};
  Services.scriptloader.loadSubScript(
    "chrome://global/content/contentAreaUtils.js",
    caUtils
  );
  let httpURI = caUtils.makeURI("http://" + TEST_HOST);
  let httpPrincipal = Services.scriptSecurityManager.createContentPrincipal(
    httpURI,
    {}
  );
  let clearRequest = Services.qms.clearStoragesForPrincipal(
    httpPrincipal,
    null,
    null,
    true
  );
  await new Promise(resolve => {
    clearRequest.callback = () => {
      resolve();
    };
  });

  for (let userContextId of Object.keys(USER_CONTEXTS)) {
    // Open our tab in the given user context.
    tabs[userContextId] = await openTabInUserContext(
      TEST_URL + "empty_file.html",
      userContextId
    );

    // Check whether indexed DB has been cleared.
    await checkIndexedDB(tabs[userContextId].browser);

    // Close this tab.
    BrowserTestUtils.removeTab(tabs[userContextId].tab);
  }
});