testing/web-platform/tests/IndexedDB/idbobjectstore-cross-realm-methods.html
author Dana Keeler <dkeeler@mozilla.com>
Sat, 12 Jul 2025 15:57:37 +0000 (13 hours ago)
changeset 796342 3a804f83c6b282c3c6b6ed9b67bacbc277bfb300
parent 768825 2ae11d3ec11d21eaf4f41136ad4160551d40127f
permissions -rw-r--r--
Bug 1976779 - rsclientcerts: make each backend responsible for rate-limiting calls to find_objects r=jschanck Before this patch, `rsclientcerts::manager` would rate-limit calls to `find_objects` to once every 3 seconds because the underlying operation can be time-consuming (in particular, on macOS and Windows, if there are many certificates/keys available). On Android, keys aren't available until the user selects one, which means that if a call to `find_objects` happened before the selection prompt was shown (which is what happens) and the user chose one in less than 3 seconds, the backend wouldn't search again, thus making it seem like no keys were available, which would cause Firefox to not send a client certificate. This patch makes each backend implementation responsible for this rate-limiting, because only they know if it's appropriate to do so (in particular, on Android, `find_objects` doesn't have the same performance concern as on macOS and Windows because rather than searching for certificates and keys, it asks `ClientAuthCertificateManager` for the cached list of certificates and keys that have already been approved for use by the user). Differential Revision: https://phabricator.services.mozilla.com/D257065
<!DOCTYPE html>
<meta charset=utf-8>
<title>Cross-realm IDBObjectStore methods from detached iframe work as expected</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=resources/support.js></script>

<body>
<script>
"use strict";
const KEY_EXISTING_LOWER = 1000;
const KEY_EXISTING_UPPER = 1001;
const KEY_EXISTING_RANGE = IDBKeyRange.bound(KEY_EXISTING_LOWER, KEY_EXISTING_UPPER);
const KEY_NEWLY_ADDED = 1002;

const VALUE_EXISTING_LOWER = "VALUE_EXISTING_LOWER";
const VALUE_EXISTING_UPPER = "VALUE_EXISTING_UPPER";
const VALUE_NEWLY_ADDED = "VALUE_NEWLY_ADDED";

const testCases = [
    {
        methodName: "put",
        arguments: [KEY_NEWLY_ADDED, KEY_EXISTING_LOWER],
        validateResult: (t, e) => {
            assert_equals(e.target.result, KEY_EXISTING_LOWER);
            const rq = e.target.source.getAll();
            rq.onsuccess = t.step_func_done(e => {
                assert_array_equals(e.target.result, [KEY_NEWLY_ADDED, VALUE_EXISTING_UPPER]);
            });
        },
    },
    {
        methodName: "add",
        arguments: [VALUE_NEWLY_ADDED, KEY_NEWLY_ADDED],
        validateResult: (t, e) => {
            assert_equals(e.target.result, KEY_NEWLY_ADDED);
            const rq = e.target.source.getAll();
            rq.onsuccess = t.step_func_done(e => {
                assert_array_equals(e.target.result, [VALUE_EXISTING_LOWER, VALUE_EXISTING_UPPER, VALUE_NEWLY_ADDED]);
            });
        },
    },
    {
        methodName: "delete",
        arguments: [KEY_EXISTING_LOWER],
        validateResult: (t, e) => {
            assert_equals(e.target.result, undefined);
            const rq = e.target.source.getAllKeys();
            rq.onsuccess = t.step_func_done(e => {
                assert_array_equals(e.target.result, [KEY_EXISTING_UPPER]);
            });
        },
    },
    {
        methodName: "clear",
        arguments: [],
        validateResult: (t, e) => {
            assert_equals(e.target.result, undefined);
            const rq = e.target.source.count();
            rq.onsuccess = t.step_func_done(e => {
                assert_equals(e.target.result, 0);
            });
        },
    },
    {
        methodName: "get",
        arguments: [KEY_EXISTING_UPPER],
        validateResult: (t, e) => {
            assert_equals(e.target.result, VALUE_EXISTING_UPPER);
            t.done();
        },
    },
    {
        methodName: "getKey",
        arguments: [KEY_EXISTING_LOWER],
        validateResult: (t, e) => {
            assert_equals(e.target.result, KEY_EXISTING_LOWER);
            t.done();
        },
    },
    {
        methodName: "getAll",
        arguments: [KEY_EXISTING_RANGE],
        validateResult: (t, e) => {
            assert_array_equals(e.target.result, [VALUE_EXISTING_LOWER, VALUE_EXISTING_UPPER]);
            t.done();
        },
    },
    {
        methodName: "getAllKeys",
        arguments: [KEY_EXISTING_RANGE],
        validateResult: (t, e) => {
            assert_array_equals(e.target.result, [KEY_EXISTING_LOWER, KEY_EXISTING_UPPER]);
            t.done();
        },
    },
    {
        methodName: "count",
        arguments: [],
        validateResult: (t, e) => {
            assert_equals(e.target.result, 2);
            t.done();
        },
    },
    {
        methodName: "openCursor",
        arguments: [],
        validateResult: (t, e) => {
            const cursor = e.target.result;
            assert_true(cursor instanceof IDBCursor);
            assert_equals(cursor.value, VALUE_EXISTING_LOWER);
            t.done();
        },
    },
    {
        methodName: "openKeyCursor",
        arguments: [],
        validateResult: (t, e) => {
            const cursor = e.target.result;
            assert_true(cursor instanceof IDBCursor);
            assert_equals(cursor.key, KEY_EXISTING_LOWER);
            t.done();
        },
    },
];

for (const testCase of testCases) {
    async_test(t => {
        const iframe = document.createElement("iframe");
        iframe.onload = t.step_func(() => {
            const method = iframe.contentWindow.IDBObjectStore.prototype[testCase.methodName];
            iframe.remove();

            let db;
            const open_rq = createdb(t);
            open_rq.onupgradeneeded = t.step_func(e => {
                db = e.target.result;
                const objectStore = db.createObjectStore("store");
                objectStore.add(VALUE_EXISTING_LOWER, KEY_EXISTING_LOWER);
                objectStore.add(VALUE_EXISTING_UPPER, KEY_EXISTING_UPPER);
            });

            open_rq.onsuccess = t.step_func(() => {
                const objectStore = db.transaction("store", "readwrite").objectStore("store");
                const rq = method.call(objectStore, ...testCase.arguments);
                rq.onsuccess = t.step_func(e => {
                    testCase.validateResult(t, e);
                });
            });
        });
        document.body.append(iframe);
    }, `Cross-realm IDBObjectStore::${testCase.methodName}() method from detached <iframe> works as expected`);
}
</script>