From ddda7b8c9c5d282fd394a0a5b7008fb4d4691579 Mon Sep 17 00:00:00 2001 From: Eric Chen Date: Fri, 24 May 2024 14:34:49 +0000 Subject: [PATCH] Bug 1872482 - recreate non-ephemeral background task profile directory if it's missing r=mossop Differential Revision: https://phabricator.services.mozilla.com/D210677 --- toolkit/profile/nsToolkitProfileService.cpp | 14 ++++++++++- ...ct_backgroundtasks_not_ephemeral_exists.js | 24 +++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/toolkit/profile/nsToolkitProfileService.cpp b/toolkit/profile/nsToolkitProfileService.cpp index aeab25c61f3b..63b01a874e5e 100644 --- a/toolkit/profile/nsToolkitProfileService.cpp +++ b/toolkit/profile/nsToolkitProfileService.cpp @@ -1539,6 +1539,8 @@ nsresult nsToolkitProfileService::SelectStartupProfile( nsAutoCString buffer; rv = mProfileDB.GetString("BackgroundTasksProfiles", profilePrefix.get(), buffer); + bool exists = false; + if (NS_SUCCEEDED(rv)) { // We have a record of one! Use it. rv = rootDir->Clone(getter_AddRefs(file)); @@ -1546,7 +1548,17 @@ nsresult nsToolkitProfileService::SelectStartupProfile( rv = file->AppendNative(buffer); NS_ENSURE_SUCCESS(rv, rv); - } else { + + rv = file->Exists(&exists); + NS_ENSURE_SUCCESS(rv, rv); + + if (!exists) { + printf_stderr( + "Profile directory does not exist, create a new directory"); + } + } + + if (!exists) { nsCString saltedProfilePrefix = profilePrefix; SaltProfileName(saltedProfilePrefix); diff --git a/toolkit/profile/xpcshell/test_select_backgroundtasks_not_ephemeral_exists.js b/toolkit/profile/xpcshell/test_select_backgroundtasks_not_ephemeral_exists.js index 6fa3ec5882e2..1ae7726e267b 100644 --- a/toolkit/profile/xpcshell/test_select_backgroundtasks_not_ephemeral_exists.js +++ b/toolkit/profile/xpcshell/test_select_backgroundtasks_not_ephemeral_exists.js @@ -17,10 +17,11 @@ add_task(condition, async () => { let hash = xreDirProvider.getInstallHash(); let saltedPath = `saltSALT.${vendor}BackgroundTask-${hash}-not_ephemeral_profile`; + let profileName = `${vendor}BackgroundTask-${hash}-not_ephemeral_profile`; // See note about ordering below. BACKGROUNDTASKS_PROFILE_DATA.backgroundTasksProfiles.splice(0, 0, { - name: `${vendor}BackgroundTask-${hash}-not_ephemeral_profile`, + name: profileName, path: saltedPath, }); @@ -38,19 +39,34 @@ add_task(condition, async () => { bts.overrideBackgroundTaskNameForTesting("not_ephemeral_profile"); let { didCreate, rootDir } = selectStartupProfile(); + checkStartupReason("backgroundtask-not-ephemeral"); - Assert.equal(didCreate, false, "Re-used existing non-ephemeral profile"); + //test directory will be created + Assert.equal( + didCreate, + true, + "Re-used existing non-ephemeral profile, but should create a new directory for it" + ); + + Assert.equal( + rootDir.exists(), + true, + "rootDir has a directory in the file system" + ); let profileData = readProfilesIni(); // Profile names are lexicographically ordered, and `not_ephemeral_profile` // sorts before `unrelated_task`. Assert.equal(profileData.backgroundTasksProfiles.length, 2); - Assert.deepEqual(profileData, BACKGROUNDTASKS_PROFILE_DATA); + //make sure the new salted path is recorded in profiles.ini + let createdProfile = profileData.backgroundTasksProfiles.find( + searchProfile => searchProfile.name == profileName + ); Assert.ok( - rootDir.path.endsWith(saltedPath), + rootDir.path.endsWith(createdProfile.path), `rootDir "${rootDir.path}" ends with salted path "${saltedPath}"` );