head.js (3311B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 /* import-globals-from /toolkit/profile/test/xpcshell/head.js */ 4 5 "use strict"; 6 7 const lazy = {}; 8 9 ChromeUtils.defineESModuleGetters(lazy, { 10 SelectableProfileService: 11 "resource:///modules/profiles/SelectableProfileService.sys.mjs", 12 Sqlite: "resource://gre/modules/Sqlite.sys.mjs", 13 }); 14 15 ChromeUtils.defineLazyGetter(lazy, "ProfilesDatastoreService", () => { 16 const { ProfilesDatastoreService } = ChromeUtils.importESModule( 17 "moz-src:///toolkit/profile/ProfilesDatastoreService.sys.mjs" 18 ); 19 20 ProfilesDatastoreService.overrideDirectoryService({ 21 ProfD: getProfileService().currentProfile.rootDir, 22 }); 23 24 return ProfilesDatastoreService; 25 }); 26 27 let gProfileServiceInitialised = false; 28 29 /** 30 * Starts up the toolkit profile services and initialises it with a new default profile. 31 */ 32 function startProfileService() { 33 if (gProfileServiceInitialised) { 34 return; 35 } 36 37 gProfileServiceInitialised = true; 38 selectStartupProfile(); 39 } 40 41 function getSelectableProfileService() { 42 return lazy.SelectableProfileService; 43 } 44 45 function getProfilesDatastoreService() { 46 return lazy.ProfilesDatastoreService; 47 } 48 49 /** 50 * Starts the selectable profile service and creates the group store for the 51 * current profile. 52 */ 53 async function initSelectableProfileService() { 54 startProfileService(); 55 56 const SelectableProfileService = getSelectableProfileService(); 57 const ProfilesDatastoreService = getProfilesDatastoreService(); 58 59 await ProfilesDatastoreService.init(); 60 61 await SelectableProfileService.init(); 62 63 await SelectableProfileService.maybeSetupDataStore(); 64 } 65 66 function getRelativeProfilePath(path) { 67 let relativePath = path.getRelativePath( 68 Services.dirsvc.get("UAppData", Ci.nsIFile) 69 ); 70 71 if (AppConstants.platform === "win") { 72 relativePath = relativePath.replace("/", "\\"); 73 } 74 75 return relativePath; 76 } 77 78 // Waits for the profile service to update about a change 79 async function updateNotified() { 80 let { resolve, promise } = Promise.withResolvers(); 81 let observer = (subject, topic, data) => { 82 Services.obs.removeObserver(observer, "sps-profiles-updated"); 83 resolve(data); 84 }; 85 86 Services.obs.addObserver(observer, "sps-profiles-updated"); 87 88 await promise; 89 } 90 91 async function openDatabase() { 92 let dbFile = Services.dirsvc.get("UAppData", Ci.nsIFile); 93 dbFile.append("Profile Groups"); 94 dbFile.append(`${getProfileService().currentProfile.storeID}.sqlite`); 95 return lazy.Sqlite.openConnection({ 96 path: dbFile.path, 97 openNotExclusive: true, 98 }); 99 } 100 101 async function createTestProfile(profileData = {}) { 102 const SelectableProfileService = getSelectableProfileService(); 103 104 let name = profileData.name ?? "Test"; 105 let path = profileData.path; 106 107 if (!path) { 108 path = await SelectableProfileService.createProfileDirs(name); 109 await SelectableProfileService.createProfileInitialFiles(path); 110 path = SelectableProfileService.getRelativeProfilePath(path); 111 } 112 113 return SelectableProfileService.insertProfile({ 114 avatar: profileData.avatar ?? "book", 115 name, 116 path, 117 themeBg: profileData.themeBg ?? "var(--background-color-box)", 118 themeFg: profileData.themeFg ?? "var(--text-color)", 119 themeId: profileData.themeId ?? "default", 120 }); 121 }