diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /dom/quota/test/common/content.js | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/quota/test/common/content.js')
-rw-r--r-- | dom/quota/test/common/content.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/dom/quota/test/common/content.js b/dom/quota/test/common/content.js new file mode 100644 index 0000000000..d3927d649a --- /dev/null +++ b/dom/quota/test/common/content.js @@ -0,0 +1,62 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +const NS_ERROR_STORAGE_BUSY = SpecialPowers.Cr.NS_ERROR_STORAGE_BUSY; + +loadScript("dom/quota/test/common/global.js"); + +function clearAllDatabases(callback) { + let qms = SpecialPowers.Services.qms; + let principal = SpecialPowers.wrap(document).nodePrincipal; + let request = qms.clearStoragesForPrincipal(principal); + let cb = SpecialPowers.wrapCallback(callback); + request.callback = cb; + return request; +} + +// SimpleDB connections and SpecialPowers wrapping: +// +// SpecialPowers provides a SpecialPowersHandler Proxy mechanism that lets our +// content-privileged code borrow its chrome-privileged principal to access +// things we shouldn't be able to access. The proxies wrap their returned +// values, so once we have something wrapped we can rely on returned objects +// being wrapped as well. The proxy will also automatically unwrap wrapped +// arguments we pass in. However, we need to invoke wrapCallback on callback +// functions so that the arguments they receive will be wrapped because the +// proxy does not automatically wrap content-privileged functions. +// +// Our use of (wrapped) SpecialPowers.Cc results in getSimpleDatabase() +// producing a wrapped nsISDBConnection instance. The nsISDBResult instances +// exposed on the (wrapped) nsISDBRequest are also wrapped. +// In particular, the wrapper takes responsibility for automatically cloning +// the ArrayBuffer returned by nsISDBResult.getAsArrayBuffer into the content +// compartment (rather than wrapping it) so that constructing a Uint8Array +// from it will succeed. + +function getSimpleDatabase() { + let connection = SpecialPowers.Cc[ + "@mozilla.org/dom/sdb-connection;1" + ].createInstance(SpecialPowers.Ci.nsISDBConnection); + + let principal = SpecialPowers.wrap(document).nodePrincipal; + + connection.init(principal); + + return connection; +} + +async function requestFinished(request) { + await new Promise(function(resolve) { + request.callback = SpecialPowers.wrapCallback(function() { + resolve(); + }); + }); + + if (request.resultCode != SpecialPowers.Cr.NS_OK) { + throw new RequestError(request.resultCode, request.resultName); + } + + return request.result; +} |