summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_ext_unlimitedStorage.html
blob: d4aeb04bb5b8fad90cef99136e00cc64eab580b7 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for simple WebExtension</title>
  <meta charset="utf-8">
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <script type="text/javascript" src="head_unlimitedStorage.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">

"use strict";

async function test_background_storagePersist(EXTENSION_ID) {
  await SpecialPowers.pushPrefEnv({
    "set": [
      ["dom.storageManager.prompt.testing", false],
      ["dom.storageManager.prompt.testing.allow", false],
    ],
  });

  const extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "permanent",

    manifest: {
      permissions: ["storage", "unlimitedStorage"],
      browser_specific_settings: {
        gecko: {
          id: EXTENSION_ID,
        },
      },
    },

    background: async function() {
      const PROMISE_RACE_TIMEOUT = 8000;

      browser.test.sendMessage("extension-uuid", window.location.host);

      await browser.storage.local.set({testkey: "testvalue"});
      await browser.test.sendMessage("storage-local-called");

      const requestStoragePersist = async () => {
        const persistAllowed = await navigator.storage.persist();
        if (!persistAllowed) {
          throw new Error("navigator.storage.persist() has been denied");
        }
      };

      await Promise.race([
        requestStoragePersist(),
        new Promise((resolve, reject) => {
          setTimeout(() => {
            reject(new Error("Timeout opening persistent db from background page"));
          }, PROMISE_RACE_TIMEOUT);
        }),
      ]).then(
        () => {
          browser.test.notifyPass("indexeddb-storagePersistent-unlimitedStorage-done");
        },
        (error) => {
          browser.test.fail(`error while testing persistent IndexedDB storage: ${error}`);
          browser.test.notifyFail("indexeddb-storagePersistent-unlimitedStorage-done");
        }
      );
    },
  });

  await extension.startup();

  const uuid = await extension.awaitMessage("extension-uuid");

  await extension.awaitMessage("storage-local-called");

  let chromeScript = SpecialPowers.loadChromeScript(function test_country_data() {
    /* eslint-env mozilla/chrome-script */
    const {addMessageListener, sendAsyncMessage} = this;

    addMessageListener("getPersistedStatus", (uuid) => {
      const {
        ExtensionStorageIDB,
      } = ChromeUtils.importESModule(
        "resource://gre/modules/ExtensionStorageIDB.sys.mjs"
      );

      const {WebExtensionPolicy} = Cu.getGlobalForObject(ExtensionStorageIDB);
      const policy = WebExtensionPolicy.getByHostname(uuid);
      const storagePrincipal = ExtensionStorageIDB.getStoragePrincipal(policy.extension);
      const request = Services.qms.persisted(storagePrincipal);
      request.callback = () => {
        // request.result will be undeinfed if the request failed (request.resultCode !== Cr.NS_OK).
        sendAsyncMessage("gotPersistedStatus", request.result);
      };
    });
  });

  const persistedPromise = chromeScript.promiseOneMessage("gotPersistedStatus");
  chromeScript.sendAsyncMessage("getPersistedStatus", uuid);
  is(await persistedPromise, true, "Got the expected persist status for the storagePrincipal");

  await extension.awaitFinish("indexeddb-storagePersistent-unlimitedStorage-done");
  await extension.unload();

  checkSitePermissions(uuid, Services.perms.UNKNOWN_ACTION, "has been cleared");
}

add_task(async function test_unlimitedStorage() {
  const EXTENSION_ID = "test-storagePersist@mozilla";
  await SpecialPowers.pushPrefEnv({
    "set": [
      ["extensions.webextensions.ExtensionStorageIDB.enabled", true],
    ],
  });

  // Verify persist mode enabled when the storage.local IDB database is opened from
  // the main process (from parent/ext-storage.js).
  info("Test unlimitedStorage on an extension migrating to the IndexedDB storage.local backend)");
  await test_background_storagePersist(EXTENSION_ID);

  await SpecialPowers.pushPrefEnv({
    "set": [
      [`extensions.webextensions.ExtensionStorageIDB.migrated.` + EXTENSION_ID, true],
    ],
  });

  // Verify persist mode enabled when the storage.local IDB database is opened from
  // the child process (from child/ext-storage.js).
  info("Test unlimitedStorage on an extension migrated to the IndexedDB storage.local backend");
  await test_background_storagePersist(EXTENSION_ID);
});

</script>

</body>
</html>