commit 2c6f8ffd668e71503b6989d07a8c25748e4be29b
parent 8a5709ccf18d435c7fce7c9c7e1c3059cf314f87
Author: Nishu Sheth <nsheth@mozilla.com>
Date: Mon, 5 Jan 2026 11:25:27 +0000
Bug 2004950 - Expose nsIToolkitProfileService::getProfileDescriptorForRootDir and nsIToolkitProfileService::getProfileLocalDirFromRootDir to JS r=profiles-reviewers,mossop
Differential Revision: https://phabricator.services.mozilla.com/D275728
Diffstat:
5 files changed, 115 insertions(+), 23 deletions(-)
diff --git a/toolkit/profile/nsIToolkitProfileService.idl b/toolkit/profile/nsIToolkitProfileService.idl
@@ -10,7 +10,7 @@ interface nsIFile;
interface nsIToolkitProfile;
interface nsIProfileLock;
-[scriptable, builtinclass, uuid(1947899b-f369-48fa-89da-f7c37bb1e6bc)]
+[scriptable, builtinclass, uuid(fcb10bd5-3065-413f-a1ca-0c7818c5227b)]
interface nsIToolkitProfileService : nsISupports
{
/**
@@ -142,6 +142,24 @@ interface nsIToolkitProfileService : nsISupports
in AUTF8String aNamePrefix);
/**
+ * Gets the profile root directory descriptor for storing in profiles.ini or
+ * installs.ini.
+ * @param aRootDir
+ * The root directory of the profile.
+ * @param aIsRelative
+ * Set to true if the returned descriptor is relative to the
+ * application data directory.
+ */
+ AUTF8String getProfileDescriptor(in nsIFile aRootDir, out boolean aIsRelative);
+
+ /**
+ * Return the local directory for a given profile root directory.
+ * @param aRootDir
+ * The root directory of the profile
+ */
+ nsIFile getLocalDirFromRootDir(in nsIFile aRootDir);
+
+ /**
* Returns the number of profiles.
* @return the number of profiles.
*/
diff --git a/toolkit/profile/nsToolkitProfileService.cpp b/toolkit/profile/nsToolkitProfileService.cpp
@@ -269,8 +269,8 @@ nsToolkitProfile::nsToolkitProfile(const nsACString& aName, nsIFile* aRootDir,
bool isRelative = false;
nsCString descriptor;
- nsToolkitProfileService::gService->GetProfileDescriptor(this, descriptor,
- &isRelative);
+ nsToolkitProfileService::gService->GetProfileDescriptor(this, &isRelative,
+ descriptor);
db->SetString(mSection.get(), "IsRelative", isRelative ? "1" : "0");
db->SetString(mSection.get(), "Path", descriptor.get());
@@ -306,7 +306,7 @@ nsToolkitProfile::SetRootDir(nsIFile* aRootDir) {
nsCString newPath;
bool isRelative;
rv = nsToolkitProfileService::gService->GetProfileDescriptor(
- aRootDir, newPath, &isRelative);
+ aRootDir, &isRelative, newPath);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIFile> localDir;
@@ -830,7 +830,7 @@ nsresult nsToolkitProfileService::MaybeMakeDefaultDedicatedProfile(
}
nsCString descriptor;
- rv = GetProfileDescriptor(aProfile, descriptor, nullptr);
+ rv = GetProfileDescriptor(aProfile, nullptr, descriptor);
NS_ENSURE_SUCCESS(rv, rv);
// Get a list of all the installs.
@@ -1154,7 +1154,7 @@ nsresult nsToolkitProfileService::Init() {
// expected form again.
bool nowRelative;
nsCString descriptor;
- GetProfileDescriptor(currentProfile, descriptor, &nowRelative);
+ GetProfileDescriptor(currentProfile, &nowRelative, descriptor);
if (isRelative != nowRelative || !descriptor.Equals(filePath)) {
mProfileDB.SetString(profileID.get(), "IsRelative",
@@ -1305,7 +1305,7 @@ nsToolkitProfileService::SetDefaultProfile(nsIToolkitProfile* aProfile) {
mProfileDB.SetString(mInstallSection.get(), "Default", "");
} else {
nsCString profilePath;
- nsresult rv = GetProfileDescriptor(profile, profilePath, nullptr);
+ nsresult rv = GetProfileDescriptor(profile, nullptr, profilePath);
NS_ENSURE_SUCCESS(rv, rv);
mProfileDB.SetString(mInstallSection.get(), "Default",
@@ -1333,13 +1333,14 @@ nsToolkitProfileService::SetDefaultProfile(nsIToolkitProfile* aProfile) {
// Gets the profile root directory descriptor for storing in profiles.ini or
// installs.ini.
nsresult nsToolkitProfileService::GetProfileDescriptor(
- nsToolkitProfile* aProfile, nsACString& aDescriptor, bool* aIsRelative) {
- return GetProfileDescriptor(aProfile->mRootDir, aDescriptor, aIsRelative);
+ nsToolkitProfile* aProfile, bool* aIsRelative, nsACString& aDescriptor) {
+ return GetProfileDescriptor(aProfile->mRootDir, aIsRelative, aDescriptor);
}
-nsresult nsToolkitProfileService::GetProfileDescriptor(nsIFile* aRootDir,
- nsACString& aDescriptor,
- bool* aIsRelative) {
+NS_IMETHODIMP
+nsToolkitProfileService::GetProfileDescriptor(nsIFile* aRootDir,
+ bool* aIsRelative,
+ nsACString& aDescriptor) {
// if the profile dir is relative to appdir...
bool isRelative;
nsresult rv = mAppData->Contains(aRootDir, &isRelative);
@@ -1779,7 +1780,7 @@ nsresult nsToolkitProfileService::SelectStartupProfile(
nsCString descriptor;
for (RefPtr<nsToolkitProfile> profile : mProfiles) {
- GetProfileDescriptor(profile, descriptor, &isRelative);
+ GetProfileDescriptor(profile, &isRelative, descriptor);
if (descriptor.Equals(defaultDescriptor)) {
// Found the default profile. Copy the install section over to
@@ -2443,7 +2444,7 @@ nsToolkitProfileService::AsyncFlushCurrentProfile(JSContext* aCx,
profileData->mShowSelector = mCurrent->mShowProfileSelector;
bool isRelative;
- GetProfileDescriptor(mCurrent, profileData->mPath, &isRelative);
+ GetProfileDescriptor(mCurrent, &isRelative, profileData->mPath);
nsCOMPtr<nsIRemoteService> rs = GetRemoteService();
RefPtr<nsRemoteService> remoteService =
@@ -2736,13 +2737,14 @@ nsToolkitProfileService::Flush() {
return FlushData(profilesIniData, installsIniData);
}
-nsresult nsToolkitProfileService::GetLocalDirFromRootDir(nsIFile* aRootDir,
- nsIFile** aResult) {
+NS_IMETHODIMP
+nsToolkitProfileService::GetLocalDirFromRootDir(nsIFile* aRootDir,
+ nsIFile** aResult) {
NS_ASSERTION(nsToolkitProfileService::gService, "Where did my service go?");
nsCString path;
bool isRelative;
nsresult rv = nsToolkitProfileService::gService->GetProfileDescriptor(
- aRootDir, path, &isRelative);
+ aRootDir, &isRelative, path);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIFile> localDir;
diff --git a/toolkit/profile/nsToolkitProfileService.h b/toolkit/profile/nsToolkitProfileService.h
@@ -116,11 +116,8 @@ class nsToolkitProfileService final : public nsIToolkitProfileService {
nsToolkitProfile** aResult);
already_AddRefed<nsToolkitProfile> GetProfileByStoreID(
const nsACString& aStoreID);
-
- nsresult GetProfileDescriptor(nsIFile* aRootDir, nsACString& aDescriptor,
- bool* aIsRelative);
- nsresult GetProfileDescriptor(nsToolkitProfile* aProfile,
- nsACString& aDescriptor, bool* aIsRelative);
+ nsresult GetProfileDescriptor(nsToolkitProfile* aProfile, bool* aIsRelative,
+ nsACString& aDescriptor);
bool IsProfileForCurrentInstall(nsToolkitProfile* aProfile);
void ClearProfileFromOtherInstalls(nsToolkitProfile* aProfile);
nsresult MaybeMakeDefaultDedicatedProfile(nsToolkitProfile* aProfile,
@@ -135,7 +132,6 @@ class nsToolkitProfileService final : public nsIToolkitProfileService {
already_AddRefed<nsToolkitProfile> GetProfileByName(const nsACString& aName);
void SetNormalDefault(nsToolkitProfile* aProfile);
already_AddRefed<nsToolkitProfile> GetDefaultProfile();
- nsresult GetLocalDirFromRootDir(nsIFile* aRootDir, nsIFile** aResult);
void FlushProfileData(
const nsMainThreadPtrHandle<nsStartupLock>& aStartupLock,
const CurrentProfileData* aProfileInfo);
diff --git a/toolkit/profile/test/xpcshell/test_profile_descriptor_and_localdir.js b/toolkit/profile/test/xpcshell/test_profile_descriptor_and_localdir.js
@@ -0,0 +1,74 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+add_task(async () => {
+ let svc = Cc["@mozilla.org/toolkit/profile-service;1"].getService(
+ Ci.nsIToolkitProfileService
+ );
+
+ let nonRelativeRoot = gDataHome.parent.clone();
+ nonRelativeRoot.append("non-relative-profile-root");
+ nonRelativeRoot.createUnique(
+ Ci.nsIFile.DIRECTORY_TYPE,
+ FileUtils.PERMS_DIRECTORY
+ );
+
+ let isRelative = {};
+ let descriptor = svc.getProfileDescriptor(nonRelativeRoot, isRelative);
+
+ Assert.strictEqual(
+ descriptor,
+ nonRelativeRoot.persistentDescriptor,
+ "Non-relative descriptor should be the persistent descriptor"
+ );
+
+ Assert.strictEqual(
+ isRelative.value,
+ false,
+ "Non-relative root should report an absolute descriptor"
+ );
+
+ let localDir = svc.getLocalDirFromRootDir(nonRelativeRoot);
+
+ Assert.strictEqual(
+ localDir.path,
+ nonRelativeRoot.path,
+ "Non-relative local dir should match the root dir"
+ );
+
+ let relativeRoot = gDataHome.clone();
+ relativeRoot.append("relative-profile-root");
+ relativeRoot.createUnique(
+ Ci.nsIFile.DIRECTORY_TYPE,
+ FileUtils.PERMS_DIRECTORY
+ );
+
+ isRelative = {};
+ descriptor = svc.getProfileDescriptor(relativeRoot, isRelative);
+
+ let expectedDescriptor = "relative-profile-root";
+
+ Assert.strictEqual(
+ descriptor,
+ expectedDescriptor,
+ "Relative descriptor should be relative to gDataHome"
+ );
+
+ Assert.strictEqual(
+ isRelative.value,
+ true,
+ "Relative root should report a relative descriptor"
+ );
+
+ localDir = svc.getLocalDirFromRootDir(relativeRoot);
+
+ let expectedLocalDir = gDataHomeLocal.clone();
+ expectedLocalDir.append("relative-profile-root");
+
+ Assert.equal(
+ localDir.path,
+ expectedLocalDir.path,
+ "Relative local dir should resolve under gDataHomeLocal"
+ );
+});
diff --git a/toolkit/profile/test/xpcshell/xpcshell.toml b/toolkit/profile/test/xpcshell/xpcshell.toml
@@ -44,6 +44,8 @@ skip-if = [
["test_previous_dedicated.js"]
+["test_profile_descriptor_and_localdir.js"]
+
["test_profile_reset.js"]
["test_remove.js"]