From 9127e8bbdc2c71dc3fc8a55a75c3b9c6c49cd12a Mon Sep 17 00:00:00 2001 From: Dave Townsend Date: Fri, 28 Dec 2018 09:44:47 -0800 Subject: [PATCH] Bug 1322797: Replace selectedProfile with currentProfile and fix defaultProfile. r=froydnj, r=flod The current properties selectedProfile and defaultProfile are somewhat confusing selectedProfile actually returns the default profile for the build and defaultProfile returns the default profile for non-dev-edition builds. This confusion leads to callers doing the wrong thing in some places. What most code actually cares about is being able to set/get the default profile for this build and getting the current profile in use. So this patch replaces the previous properties with currentProfile and defaultProfile which do what makes more sense. This patch also switches from using the preprocessor to change behaviour for dev-edition builds to using a boolean flag since some code was incorrectly ignoring the setting to make dev-edition use the same profile as normal builds. In order to make currentProfile correct when resetting a profile I had to move CreateResetProfile into nsToolkitProfileService. Differential Revision: https://phabricator.services.mozilla.com/D16118 --HG-- extra : rebase_source : cefe252618cd3a1b0e0cd5a71b056dd2b557f1a3 extra : intermediate-source : 35af79575f54f75d22e213fdac7ddd704b40807a extra : source : 732d1ce192408d4f595f2fce16f45c7354ce3097 --- .../migration/FirefoxProfileMigrator.js | 2 +- devtools/shared/system.js | 9 +- toolkit/content/aboutProfiles.js | 74 +++--- toolkit/content/aboutProfiles.xhtml | 1 + .../en-US/toolkit/about/aboutProfiles.ftl | 3 + toolkit/profile/content/profileSelection.js | 12 +- toolkit/profile/nsIToolkitProfileService.idl | 22 +- toolkit/profile/nsToolkitProfileService.cpp | 215 +++++++++++------- toolkit/profile/nsToolkitProfileService.h | 19 +- toolkit/profile/xpcshell/head.js | 17 +- .../profile/xpcshell/test_create_default.js | 3 +- .../profile/xpcshell/test_select_default.js | 2 +- toolkit/xre/ProfileReset.cpp | 36 --- toolkit/xre/ProfileReset.h | 4 - toolkit/xre/nsAppRunner.cpp | 23 +- 15 files changed, 219 insertions(+), 223 deletions(-) diff --git a/browser/components/migration/FirefoxProfileMigrator.js b/browser/components/migration/FirefoxProfileMigrator.js index d3470049f85a..0591717e5d06 100644 --- a/browser/components/migration/FirefoxProfileMigrator.js +++ b/browser/components/migration/FirefoxProfileMigrator.js @@ -72,7 +72,7 @@ FirefoxProfileMigrator.prototype.getResources = function(aProfile) { let sourceProfileDir = aProfile ? this._getAllProfiles().get(aProfile.id) : Cc["@mozilla.org/toolkit/profile-service;1"] .getService(Ci.nsIToolkitProfileService) - .selectedProfile.rootDir; + .currentProfile.rootDir; if (!sourceProfileDir || !sourceProfileDir.exists() || !sourceProfileDir.isReadable()) return null; diff --git a/devtools/shared/system.js b/devtools/shared/system.js index 0069bcb902cf..c5e0279f5ff7 100644 --- a/devtools/shared/system.js +++ b/devtools/shared/system.js @@ -180,13 +180,14 @@ function getDeviceName() { function getProfileLocation() { // In child processes, we cannot access the profile location. try { + // For some reason this line must come first or in xpcshell tests + // nsXREDirProvider never gets initialised and so the profile service + // crashes on initialisation. const profd = Services.dirsvc.get("ProfD", Ci.nsIFile); const profservice = Cc["@mozilla.org/toolkit/profile-service;1"] .getService(Ci.nsIToolkitProfileService); - for (const profile of profservice.profiles) { - if (profile.rootDir.path == profd.path) { - return profile.name; - } + if (profservice.currentProfile) { + return profservice.currentProfile.name; } return profd.leafName; diff --git a/toolkit/content/aboutProfiles.js b/toolkit/content/aboutProfiles.js index d6f70d8b2a76..9abd61ea088c 100644 --- a/toolkit/content/aboutProfiles.js +++ b/toolkit/content/aboutProfiles.js @@ -14,33 +14,6 @@ XPCOMUtils.defineLazyServiceGetter( "nsIToolkitProfileService" ); -// nsIToolkitProfileService.selectProfile can be used only during the selection -// of the profile in the ProfileManager. If we are showing about:profiles in a -// tab, the selectedProfile returns the default profile. -// In this function we use the ProfD to find the current profile. -function findCurrentProfile() { - let cpd; - try { - cpd = Services.dirsvc.get("ProfD", Ci.nsIFile); - } catch (e) {} - - if (cpd) { - for (let profile of ProfileService.profiles) { - if (profile.rootDir.path == cpd.path) { - return profile; - } - } - } - - // selectedProfile can throw if nothing is selected or if the selected profile - // has been deleted. - try { - return ProfileService.selectedProfile; - } catch (e) { - return null; - } -} - function refreshUI() { let parent = document.getElementById("profiles"); while (parent.firstChild) { @@ -52,7 +25,7 @@ function refreshUI() { defaultProfile = ProfileService.defaultProfile; } catch (e) {} - let currentProfile = findCurrentProfile(); + let currentProfile = ProfileService.currentProfile; for (let profile of ProfileService.profiles) { let isCurrentProfile = profile == currentProfile; @@ -197,10 +170,10 @@ function display(profileData) { div.appendChild(sep); } +// This is called from the createProfileWizard.xul dialog. function CreateProfile(profile) { - ProfileService.selectedProfile = profile; - ProfileService.flush(); - refreshUI(); + // The wizard created a profile, just make it the default. + defaultProfile(profile); } function createProfileWizard() { @@ -269,30 +242,26 @@ async function removeProfile(profile) { } } - // If we are deleting the selected or the default profile we must choose a - // different one. - let isSelected = false; - try { - isSelected = ProfileService.selectedProfile == profile; - } catch (e) {} - + // If we are deleting the default profile we must choose a different one. let isDefault = false; try { isDefault = ProfileService.defaultProfile == profile; } catch (e) {} - if (isSelected || isDefault) { + if (isDefault) { for (let p of ProfileService.profiles) { if (profile == p) { continue; } - if (isSelected) { - ProfileService.selectedProfile = p; - } - if (isDefault) { - ProfileService.defaultProfile = p; + try { + ProfileService.defaultProfile = p; + } catch (e) { + // This can happen on dev-edition if a non-default profile is in use. + // In such a case the next time that dev-edition is started it will + // find no default profile and just create a new one. + } } break; @@ -315,10 +284,19 @@ async function removeProfile(profile) { refreshUI(); } -function defaultProfile(profile) { - ProfileService.defaultProfile = profile; - ProfileService.selectedProfile = profile; - ProfileService.flush(); +async function defaultProfile(profile) { + try { + ProfileService.defaultProfile = profile; + ProfileService.flush(); + } catch (e) { + // This can happen on dev-edition. + let [title, msg] = await document.l10n.formatValues([ + { id: "profiles-cannot-set-as-default-title" }, + { id: "profiles-cannot-set-as-default-message" }, + ]); + + Services.prompt.alert(window, title, msg); + } refreshUI(); } diff --git a/toolkit/content/aboutProfiles.xhtml b/toolkit/content/aboutProfiles.xhtml index c0de98c1a21c..ad2010e22c5c 100644 --- a/toolkit/content/aboutProfiles.xhtml +++ b/toolkit/content/aboutProfiles.xhtml @@ -12,6 +12,7 @@