QuotaUtils.sys.mjs (3523B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 import { PrefUtils } from "resource://testing-common/dom/quota/test/modules/PrefUtils.sys.mjs"; 7 import { RequestError } from "resource://testing-common/dom/quota/test/modules/RequestError.sys.mjs"; 8 9 export const QuotaUtils = { 10 /** 11 * Handles the completion of a request, awaiting the callback to be called 12 * before proceeding. 13 * 14 * This function is designed to handle requests of the types: 15 * - `nsIQuotaRequest` 16 * - `nsIQuotaUsageRequest` 17 * 18 * These requests are typically returned by the quota manager service. 19 * 20 * @param {object} request 21 * The request object, which must have a callback property and 22 * result-related properties (e.g., resultCode, resultName). 23 * @returns {Promise} 24 * Resolves with the request's result when the operation is successful. 25 * @throws {RequestError} 26 * If the request's resultCode is not `Cr.NS_OK`, indicating an error in 27 * the request. 28 */ 29 async requestFinished(request) { 30 await new Promise(function (resolve) { 31 request.callback = function () { 32 resolve(); 33 }; 34 }); 35 36 if (request.resultCode !== Cr.NS_OK) { 37 throw new RequestError(request.resultCode, request.resultName); 38 } 39 40 return request.result; 41 }, 42 43 /** 44 * Temporarily sets artificial failure preferences for testing, runs the 45 * callback, and then restores the original preferences. 46 * 47 * @param {number} categories - A bitwise combination of artificial failure 48 * categories to set. 49 * @param {number} probability - The probability (0-100) of triggering an 50 * artificial failure. A value of 0 means no failure, while 100 means 51 * failure is guaranteed. 52 * @param {number} errorCode - The error code to return when an artificial 53 * failure occurs. 54 * @param {Function} callback - The asynchronous function to execute with the 55 * artificial settings. 56 * @returns {*} - The result of the callback function after it has been 57 * awaited. 58 */ 59 async withArtificialFailures(categories, probability, errorCode, callback) { 60 const prefs = [ 61 ["dom.quotaManager.artificialFailure.categories", categories], 62 ["dom.quotaManager.artificialFailure.probability", probability], 63 ["dom.quotaManager.artificialFailure.errorCode", errorCode], 64 ]; 65 66 const originalPrefs = PrefUtils.getPrefs(prefs); 67 68 let result = null; 69 70 try { 71 PrefUtils.setPrefs(prefs); 72 73 result = await callback(); 74 } finally { 75 PrefUtils.setPrefs(originalPrefs); 76 } 77 78 return result; 79 }, 80 81 /** 82 * Simulates an orderly shutdown sequence by advancing through predefined 83 * application shutdown phases. This function mirrors the shutdown sequence 84 * triggered during the xpcshell test cleanup phase: 85 * https://searchfox.org/mozilla-central/rev/a2abcf7ff6b7ae0c2d8a04b9a35679f8c84634e7/testing/xpcshell/head.js#701-718 86 * 87 * Tests can use this function to prematurely trigger shutdown in order to 88 * test edge cases related to events occurring during Quota Manager shutdown. 89 */ 90 startShutdown() { 91 const phases = [ 92 Services.startup.SHUTDOWN_PHASE_APPSHUTDOWNNETTEARDOWN, 93 Services.startup.SHUTDOWN_PHASE_APPSHUTDOWNTEARDOWN, 94 Services.startup.SHUTDOWN_PHASE_APPSHUTDOWN, 95 Services.startup.SHUTDOWN_PHASE_APPSHUTDOWNQM, 96 ]; 97 98 for (const phase of phases) { 99 Services.startup.advanceShutdownPhase(phase); 100 } 101 }, 102 };