VacuumParticipant.sys.mjs (2956B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ 3 */ 4 5 // This testing component is used in test_vacuum* tests. 6 7 const CAT_NAME = "vacuum-participant"; 8 const CONTRACT_ID = "@unit.test.com/test-vacuum-participant;1"; 9 10 import { MockRegistrar } from "resource://testing-common/MockRegistrar.sys.mjs"; 11 import { TestUtils } from "resource://testing-common/TestUtils.sys.mjs"; 12 13 export class VacuumParticipant { 14 #dbConn; 15 #expectedPageSize = 0; 16 #useIncrementalVacuum = false; 17 #grant = false; 18 19 /** 20 * Build a VacuumParticipant instance. 21 * Note: After creation you must await instance.promiseRegistered() to ensure 22 * Category Caches have been updated. 23 * 24 * @param {mozIStorageAsyncConnection} databaseConnection 25 * The connection to be vacuumed. 26 * @param {number} [expectedPageSize] 27 * Used to change the database page size. 28 * @param {boolean} [useIncrementalVacuum] 29 * Whether to enable incremental vacuum on the database. 30 * @param {boolean} [grant] 31 * Whether the vacuum operation should be granted. 32 */ 33 constructor( 34 databaseConnection, 35 { expectedPageSize = 0, useIncrementalVacuum = false, grant = true } = {} 36 ) { 37 this.#dbConn = databaseConnection; 38 39 // Register as the only participant. 40 this.#unregisterAllParticipants(); 41 this.#registerAsParticipant(); 42 43 this.#expectedPageSize = expectedPageSize; 44 this.#useIncrementalVacuum = useIncrementalVacuum; 45 this.#grant = grant; 46 47 this.QueryInterface = ChromeUtils.generateQI([ 48 "mozIStorageVacuumParticipant", 49 ]); 50 } 51 52 promiseRegistered() { 53 // The category manager dispatches change notifications to the main thread, 54 // so we must wait one tick. 55 return TestUtils.waitForTick(); 56 } 57 58 #registerAsParticipant() { 59 MockRegistrar.register(CONTRACT_ID, this); 60 Services.catMan.addCategoryEntry( 61 CAT_NAME, 62 "vacuumParticipant", 63 CONTRACT_ID, 64 false, 65 false 66 ); 67 } 68 69 #unregisterAllParticipants() { 70 // First unregister other participants. 71 for (let { data: entry } of Services.catMan.enumerateCategory(CAT_NAME)) { 72 Services.catMan.deleteCategoryEntry("vacuum-participant", entry, false); 73 } 74 } 75 76 async dispose() { 77 this.#unregisterAllParticipants(); 78 MockRegistrar.unregister(CONTRACT_ID); 79 await new Promise(resolve => { 80 this.#dbConn.asyncClose(resolve); 81 }); 82 } 83 84 get expectedDatabasePageSize() { 85 return this.#expectedPageSize; 86 } 87 88 get useIncrementalVacuum() { 89 return this.#useIncrementalVacuum; 90 } 91 92 get databaseConnection() { 93 return this.#dbConn; 94 } 95 96 onBeginVacuum() { 97 if (!this.#grant) { 98 return false; 99 } 100 Services.obs.notifyObservers(null, "test-begin-vacuum"); 101 return true; 102 } 103 onEndVacuum(succeeded) { 104 Services.obs.notifyObservers( 105 null, 106 succeeded ? "test-end-vacuum-success" : "test-end-vacuum-failure" 107 ); 108 } 109 }