diff --git a/profile/macbuild/profile.mcp b/profile/macbuild/profile.mcp index 3fab7a781365..bac09d63d333 100644 Binary files a/profile/macbuild/profile.mcp and b/profile/macbuild/profile.mcp differ diff --git a/profile/src/Makefile.in b/profile/src/Makefile.in index 9e1a2b7b022f..79687974b632 100644 --- a/profile/src/Makefile.in +++ b/profile/src/Makefile.in @@ -31,6 +31,7 @@ IS_COMPONENT = 1 CPPSRCS = nsProfile.cpp \ nsProfileFactory.cpp \ + nsProfileAccess.cpp \ $(NULL) EXTRA_DSO_LDOPTS = \ diff --git a/profile/src/makefile.win b/profile/src/makefile.win index 7be742c329b4..3f57b2d099a5 100644 --- a/profile/src/makefile.win +++ b/profile/src/makefile.win @@ -89,6 +89,7 @@ LLIBS = \ OBJS = \ .\$(OBJDIR)\nsProfile.obj \ .\$(OBJDIR)\nsProfileFactory.obj \ + .\$(OBJDIR)\nsProfileAccess.obj \ $(NULL) #//------------------------------------------------------------------------ diff --git a/profile/src/nsProfile.cpp b/profile/src/nsProfile.cpp index 316979f58d65..960ea28ae763 100644 --- a/profile/src/nsProfile.cpp +++ b/profile/src/nsProfile.cpp @@ -60,10 +60,6 @@ #include "nsIGenericFactory.h" #if defined (XP_UNIX) -#define USER_ENVIRONMENT_VARIABLE "USER" -#define HOME_ENVIRONMENT_VARIABLE "HOME" -#define PROFILE_NAME_ENVIRONMENT_VARIABLE "PROFILE_NAME" -#define PROFILE_HOME_ENVIRONMENT_VARIABLE "PROFILE_HOME" #elif defined (XP_MAC) #define OLD_REGISTRY_FILE_NAME "Netscape Registry" #elif defined (XP_BEOS) @@ -74,31 +70,12 @@ // PREG information #define PREG_COOKIE "NS_REG2_PREG" -#define PREG_USERNAME "PREG_USER_NAME" +#define PREG_USERNAME "PREG_USER_NAME" #define PREG_DENIAL "PREG_USER_DENIAL" // kill me now. -#define REGISTRY_TRUE_STRING "true" -#define REGISTRY_FALSE_STRING "false" -#define REGISTRY_YES_STRING "yes" -#define REGISTRY_NO_STRING "no" - -// strings for items in the registry we'll be getting or setting -#define REGISTRY_PROFILE_SUBTREE_STRING "Profiles" -#define REGISTRY_CURRENT_PROFILE_STRING "CurrentProfile" -#define REGISTRY_NC_SERVICE_DENIAL_STRING "NCServiceDenial" -#define REGISTRY_NC_PROFILE_NAME_STRING "NCProfileName" -#define REGISTRY_HAVE_PREG_INFO_STRING "HavePregInfo" -#define REGISTRY_MIGRATED_STRING "migrated" -#define REGISTRY_DIRECTORY_STRING "directory" -#define REGISTRY_NEED_MIGRATION_STRING "NeedMigration" - -#define REGISTRY_VERSION_STRING "Version" -#define REGISTRY_VERSION_1_0 "1.0" - -#define MAX_PERSISTENT_DATA_SIZE 1000 -#define ISHEX(c) ( ((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F') ) -#define NUM_HEX_BYTES 8 +#define REGISTRY_YES_STRING "yes" +#define REGISTRY_NO_STRING "no" // this used to be cpwPreg.xul, that no longer exists. we need to fix this. #define PROFILE_PREG_URL "chrome://profile/content/createProfileWizard.xul" @@ -117,6 +94,11 @@ #define DEBUG_profile_ 1 #endif +// ProfileAccess varaible (gProfileDataAccess) to access registry operations +// gDataAccessInstCount is used to keep track of instance count to activate +// destructor at the right time (count == 0) +static nsProfileAccess* gProfileDataAccess = nsnull; +static PRInt32 gDataAccessInstCount = 0; // IID and CIDs of all the services needed static NS_DEFINE_CID(kIProfileIID, NS_IPROFILE_IID); @@ -158,16 +140,25 @@ nsresult GetStringFromSpec(nsFileSpec inSpec, char **string) */ nsProfile::nsProfile() { - m_reg = null_nsCOMPtr(); - mCount = 0; - mNumProfiles = 0; - mNumOldProfiles = 0; + if(!gProfileDataAccess) + gProfileDataAccess = new nsProfileAccess(); - NS_INIT_REFCNT(); + gDataAccessInstCount++; + + NS_INIT_REFCNT(); } nsProfile::~nsProfile() { +#if defined(DEBUG_profile) + printf("~nsProfile \n"); +#endif + + gProfileDataAccess->UpdateRegistry(); + + gDataAccessInstCount--; + if (gDataAccessInstCount == 0) + delete gProfileDataAccess; } /* @@ -181,102 +172,36 @@ NS_IMPL_QUERY_INTERFACE(nsProfile, kIProfileIID) /* * nsIProfile Implementation */ -// Creates an instance of the mozRegistry (m_reg) -// or any other registry that would be finalized for 5.0 -// Adds subtree Profiles to facilitate profile operations NS_IMETHODIMP nsProfile::Startup(const char *filename) { -#if defined(DEBUG_profile) - printf("ProfileManager (nsProfile) : Startup : Get Registry handle\n"); -#endif - nsresult rv = NS_OK; - - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - // Add the root Profiles node in the registry - nsRegistryKey key; - rv = m_reg->AddSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - - nsXPIDLCString versionKey; - rv = m_reg->GetString(key, REGISTRY_VERSION_STRING, getter_Copies(versionKey)); - - if (versionKey == nsnull) - { - FixRegEntries(); - } - - rv = m_reg->SetString(key, REGISTRY_VERSION_STRING, REGISTRY_VERSION_1_0); - return rv; -} - -nsresult -nsProfile::CloseRegistry() -{ - nsresult rv; - rv = m_reg->Close(); - return rv; -} - -nsresult -nsProfile::OpenRegistry() -{ - nsresult rv; - PRBool openalready; - - if (!m_reg) { - rv = nsComponentManager::CreateInstance(kRegistryCID, - nsnull, - nsIRegistry::GetIID(), - getter_AddRefs(m_reg)); - if (NS_FAILED(rv)) return rv; - if (!m_reg) return NS_ERROR_FAILURE; - } - - // Open the registry - rv = m_reg->IsOpen( &openalready); - if (NS_FAILED(rv)) return rv; - - if (!openalready) - rv = m_reg->OpenDefault(); - - return rv; + return NS_OK; } NS_IMETHODIMP nsProfile::StartupWithArgs(nsICmdLineService *cmdLineArgs) { - nsresult rv; - // initializations for profile manager - PRBool profileDirSet = PR_FALSE; - nsCString profileURLStr(""); - -#ifdef DEBUG_profile - printf("Profile Manager : Profile Wizard and Manager activites : Begin\n"); -#endif - - Startup(nsnull); - - if (cmdLineArgs) - rv = ProcessArgs(cmdLineArgs, &profileDirSet, profileURLStr); - - if (!profileDirSet) { - rv = LoadDefaultProfileDir(profileURLStr); - if (NS_FAILED(rv)) { - CloseRegistry(); - return rv; - } - } - - // Closing the registry that was opened in Startup() - rv = CloseRegistry(); - if (NS_FAILED(rv)) return rv; + nsresult rv; + // initializations for profile manager + PRBool profileDirSet = PR_FALSE; + nsCString profileURLStr(""); #ifdef DEBUG_profile - printf("Profile Manager : Profile Wizard and Manager activites : End\n"); + printf("Profile Manager : Profile Wizard and Manager activites : Begin\n"); #endif - return NS_OK; + if (cmdLineArgs) + rv = ProcessArgs(cmdLineArgs, &profileDirSet, profileURLStr); + + if (!profileDirSet) { + rv = LoadDefaultProfileDir(profileURLStr); + if (NS_FAILED(rv)) return rv; + } + +#ifdef DEBUG_profile + printf("Profile Manager : Profile Wizard and Manager activites : End\n"); +#endif + + return NS_OK; } @@ -308,50 +233,50 @@ nsProfile::LoadDefaultProfileDir(nsCString & profileURLStr) //if (NS_FAILED(rv)) return rv; if (profileURLStr.Length() == 0) + { + // This means that there was no command-line argument to force + // profile UI to come up. But we need the UI anyway if there + // are no profiles yet, or if there is more than one. + if (numProfiles == 0) { - // This means that there was no command-line argument to force - // profile UI to come up. But we need the UI anyway if there - // are no profiles yet, or if there is more than one. - if (numProfiles == 0) - { - if (pregPref) - profileURLStr = PROFILE_PREG_URL; - else - profileURLStr = PROFILE_WIZARD_URL; - } - else if (numProfiles > 1) - profileURLStr = PROFILE_SELECTION_URL; + if (pregPref) + profileURLStr = PROFILE_PREG_URL; + else + profileURLStr = PROFILE_WIZARD_URL; } + else if (numProfiles > 1) + profileURLStr = PROFILE_SELECTION_URL; + } // Provide Preg information - if (pregPref && (PL_strcmp(isPregInfoSet, REGISTRY_TRUE_STRING) != 0)) + if (pregPref && (PL_strcmp(isPregInfoSet, REGISTRY_YES_STRING) != 0)) profileURLStr = PROFILE_PREG_URL; if (profileURLStr.Length() != 0) - { - rv = NS_NewURI(getter_AddRefs(profileURL), (const char *)profileURLStr); - - if (NS_FAILED(rv)) { - return rv; - } + { + rv = NS_NewURI(getter_AddRefs(profileURL), (const char *)profileURLStr); - nsCOMPtr profWindow; - rv = profAppShell->CreateTopLevelWindow(nsnull, profileURL, - PR_TRUE, PR_TRUE, NS_CHROME_ALL_CHROME, - nsnull, - NS_SIZETOCONTENT, // width - NS_SIZETOCONTENT, // height - getter_AddRefs(profWindow)); + if (NS_FAILED(rv)) { + return rv; + } - if (NS_FAILED(rv)) return rv; + nsCOMPtr profWindow; + rv = profAppShell->CreateTopLevelWindow(nsnull, profileURL, + PR_TRUE, PR_TRUE, NS_CHROME_ALL_CHROME, + nsnull, + NS_SIZETOCONTENT, // width + NS_SIZETOCONTENT, // height + getter_AddRefs(profWindow)); - /* - * Start up the main event loop... - */ - rv = profAppShell->Run(); - } + if (NS_FAILED(rv)) return rv; + + /* + * Start up the main event loop... + */ + rv = profAppShell->Run(); + } // if we get here, and we don't have a current profile, // return a failure so we will exit @@ -363,9 +288,11 @@ nsProfile::LoadDefaultProfileDir(nsCString & profileURLStr) } PR_FREEIF(currentProfileStr); - if (pregPref && PL_strcmp(isPregInfoSet, REGISTRY_TRUE_STRING) != 0) + if (pregPref && PL_strcmp(isPregInfoSet, REGISTRY_YES_STRING) != 0) ProcessPRegCookie(); - + + CRTFREEIF(isPregInfoSet); + // Now we have the right profile, read the user-specific prefs. rv = prefs->ReadUserPrefs(); return rv; @@ -392,21 +319,21 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs, // remembers profile for next time rv = cmdLineArgs->GetCmdLineValue(PROFILE_CMD_LINE_ARG, &cmdResult); if (NS_SUCCEEDED(rv)) - { - if (cmdResult) { - char* currProfileName = cmdResult; + { + if (cmdResult) { + char* currProfileName = cmdResult; #ifdef DEBUG_profile - printf("ProfileName : %s\n", cmdResult); + printf("ProfileName : %s\n", cmdResult); #endif /* DEBUG_profile */ - - GetProfileDir(currProfileName, &currProfileDirSpec); - - if (NS_SUCCEEDED(rv)){ - *profileDirSet = PR_TRUE; - } + + GetProfileDir(currProfileName, &currProfileDirSpec); + + if (NS_SUCCEEDED(rv)){ + *profileDirSet = PR_TRUE; } } - + } + // -CreateProfile command line option works this way: // apprunner -CreateProfile profilename // creates a new profile named and sets the directory to your CWD @@ -420,95 +347,94 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs, rv = cmdLineArgs->GetCmdLineValue(CREATE_PROFILE_CMD_LINE_ARG, &cmdResult); if (NS_SUCCEEDED(rv)) - { - if (cmdResult) { - char* currProfileName = strtok(cmdResult, " "); - char* currProfileDirString = strtok(NULL, " "); - - if (currProfileDirString) - currProfileDirSpec = currProfileDirString; - else - { - // No directory name provided. Get File Locator - NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); - if (NS_FAILED(rv) || !locator) - return NS_ERROR_FAILURE; - - // Get current profile, make the new one a sibling... - nsCOMPtr spec; - rv = locator->GetFileLocation(nsSpecialFileSpec::App_UserProfileDirectory50, getter_AddRefs(spec)); - if (NS_FAILED(rv) || !spec) - return NS_ERROR_FAILURE; - spec->GetFileSpec(&currProfileDirSpec); - currProfileDirSpec.SetLeafName(currProfileName); + { + if (cmdResult) { + char* currProfileName = strtok(cmdResult, " "); + char* currProfileDirString = strtok(NULL, " "); + + if (currProfileDirString) + currProfileDirSpec = currProfileDirString; + else + { + // No directory name provided. Get File Locator + NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); + if (NS_FAILED(rv) || !locator) + return NS_ERROR_FAILURE; + + // Get current profile, make the new one a sibling... + nsCOMPtr spec; + rv = locator->GetFileLocation(nsSpecialFileSpec::App_DefaultUserProfileRoot50, getter_AddRefs(spec)); + if (NS_FAILED(rv) || !spec) + return NS_ERROR_FAILURE; + spec->GetFileSpec(&currProfileDirSpec); - rv = locator->ForgetProfileDir(); - } -#ifdef DEBUG_profile - printf("profileName & profileDir are: %s\n", cmdResult); -#endif - SetProfileDir(currProfileName, currProfileDirSpec); - *profileDirSet = PR_TRUE; - + rv = locator->ForgetProfileDir(); } +#ifdef DEBUG_profile + printf("profileName & profileDir are: %s\n", cmdResult); +#endif + CreateNewProfile(currProfileName, currProfileDirSpec.GetNativePathCString()); + *profileDirSet = PR_TRUE; } + } // Start Profile Manager rv = cmdLineArgs->GetCmdLineValue(PROFILE_MANAGER_CMD_LINE_ARG, &cmdResult); if (NS_SUCCEEDED(rv)) - { - if (cmdResult) { - profileURLStr = PROFILE_MANAGER_URL; - } + { + if (cmdResult) { + profileURLStr = PROFILE_MANAGER_URL; } + } // Start Profile Selection rv = cmdLineArgs->GetCmdLineValue(PROFILE_SELECTION_CMD_LINE_ARG, &cmdResult); if (NS_SUCCEEDED(rv)) - { - if (cmdResult) { - profileURLStr = PROFILE_SELECTION_URL; - } + { + if (cmdResult) { + profileURLStr = PROFILE_SELECTION_URL; } + } // Start Profile Wizard rv = cmdLineArgs->GetCmdLineValue(PROFILE_WIZARD_CMD_LINE_ARG, &cmdResult); if (NS_SUCCEEDED(rv)) - { - if (cmdResult) { - profileURLStr = PROFILE_WIZARD_URL; - } + { + if (cmdResult) { + profileURLStr = PROFILE_WIZARD_URL; } + } // Start Migaration activity rv = cmdLineArgs->GetCmdLineValue(INSTALLER_CMD_LINE_ARG, &cmdResult); if (NS_SUCCEEDED(rv)) - { - if (cmdResult) { - rv = MigrateProfileInfo(); - if (NS_FAILED(rv)) return rv; + { + if (cmdResult) { + rv = MigrateProfileInfo(); + if (NS_FAILED(rv)) return rv; - PRInt32 num4xProfiles = 0; - rv = Get4xProfileCount(&num4xProfiles); - if (NS_FAILED(rv)) return rv; - - PRInt32 numProfiles = 0; - GetProfileCount(&numProfiles); - if (num4xProfiles == 0 && numProfiles == 0) { - // show the create profile wizard - profileURLStr = PROFILE_WIZARD_URL; - } - else if (num4xProfiles == 1 && numProfiles == 0) { - // automatically migrate the one 4.x profile - MigrateAllProfiles(); - } - else { - // show the profile manager - profileURLStr = PROFILE_MANAGER_URL; - } + PRInt32 num4xProfiles = 0; + rv = Get4xProfileCount(&num4xProfiles); + if (NS_FAILED(rv)) return rv; + + PRInt32 numProfiles = 0; + GetProfileCount(&numProfiles); + if (num4xProfiles == 0 && numProfiles == 0) { + // show the create profile wizard + profileURLStr = PROFILE_WIZARD_URL; + } + else if (num4xProfiles == 1 && numProfiles == 0) { + // automatically migrate the one 4.x profile + MigrateAllProfiles(); + gProfileDataAccess->UpdateRegistry(); + } + else { + // show the profile manager + profileURLStr = PROFILE_MANAGER_URL; } } + } #ifdef DEBUG_profile printf("Profile Manager : Command Line Options : End\n"); #endif @@ -531,109 +457,54 @@ NS_IMETHODIMP nsProfile::GetProfileDir(const char *profileName, nsFileSpec* prof printf("ProfileManager : GetProfileDir\n"); #endif - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; + ProfileStruct *aProfile; - nsRegistryKey key; - - // Get handle to subtree REGISTRY_PROFILE_SUBTREE_STRING - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - - if (NS_SUCCEEDED(rv)) - { - nsRegistryKey newKey; - - // Get handle to passed - rv = m_reg->GetSubtree(key, profileName, &newKey); - - if (NS_SUCCEEDED(rv)) - { - nsXPIDLCString encodedProfileDir; - - // Get the value of entry REGISTRY_DIRECTORY_STRING - rv = m_reg->GetString( newKey, REGISTRY_DIRECTORY_STRING, getter_Copies(encodedProfileDir)); - - if (NS_SUCCEEDED(rv)) - { - nsXPIDLCString isMigrated; - - nsCOMPtrspec; - rv = NS_NewFileSpec(getter_AddRefs(spec)); - if (NS_FAILED(rv)) return rv; - rv = spec->SetPersistentDescriptorString(encodedProfileDir); - if (NS_FAILED(rv)) return rv; - rv = spec->GetFileSpec(profileDir); - if (NS_FAILED(rv)) return rv; - - // Get the value of entry REGISTRY_MIGRATED_STRING to check the nature of the profile - m_reg->GetString( newKey, REGISTRY_MIGRATED_STRING, - getter_Copies(isMigrated)); - - // Set this to be a current profile only if it is a 5.0 profile - if (PL_strcmp(isMigrated, REGISTRY_YES_STRING) == 0) - { - rv = m_reg->SetString(key, REGISTRY_CURRENT_PROFILE_STRING, profileName); - - if (NS_FAILED(rv)) - { -#if defined(DEBUG_profile) - printf("Profiles : Couldn't set CurrentProfile Name.\n"); -#endif - } - - nsFileSpec tmpFileSpec(*profileDir); - if (!tmpFileSpec.Exists()) { - - // Get profile defaults folder.. - NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); - - if (NS_FAILED(rv) || !locator) - return NS_ERROR_FAILURE; - - nsCOMPtr profDefaultsDir; - rv = locator->GetFileLocation(nsSpecialFileSpec::App_ProfileDefaultsFolder50, getter_AddRefs(profDefaultsDir)); - - if (NS_FAILED(rv) || !profDefaultsDir) - return NS_ERROR_FAILURE; - - nsFileSpec defaultsDirSpec; - - profDefaultsDir->GetFileSpec(&defaultsDirSpec); + gProfileDataAccess->GetValue(profileName, &aProfile); - // Need a separate hack for Mac. For now app folder is the fall back on Mac. - nsFilePath(tmpFileSpec.GetNativePathCString(), PR_TRUE); + if (aProfile == nsnull) + return NS_ERROR_FAILURE; + + nsCOMPtrspec; + rv = NS_NewFileSpec(getter_AddRefs(spec)); + if (NS_FAILED(rv)) return rv; + rv = spec->SetPersistentDescriptorString(aProfile->profileLocation); + if (NS_FAILED(rv)) return rv; + rv = spec->GetFileSpec(profileDir); + if (NS_FAILED(rv)) return rv; + + // Set this to be a current profile only if it is a 5.0 profile + if (PL_strcmp(aProfile->isMigrated, REGISTRY_YES_STRING) == 0) + { + gProfileDataAccess->SetCurrentProfile(profileName); + + nsFileSpec tmpFileSpec(*profileDir); + if (!tmpFileSpec.Exists()) + { + // Get profile defaults folder.. + NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); - // Copy contents from defaults folder. - if (defaultsDirSpec.Exists()) - { - defaultsDirSpec.RecursiveCopy(tmpFileSpec); - } + if (NS_FAILED(rv) || !locator) + return NS_ERROR_FAILURE; - } - - } - - } - else - { -#if defined(DEBUG_profile) - printf("Profiles : Couldn't get Profile directory name.\n"); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Profiles : Couldn't get profileName subtree.\n"); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Registry : Couldn't get Profiles subtree.\n"); -#endif - } + nsCOMPtr profDefaultsDir; + + rv = locator->GetFileLocation(nsSpecialFileSpec::App_ProfileDefaultsFolder50, getter_AddRefs(profDefaultsDir)); + if (NS_FAILED(rv) || !profDefaultsDir) + return NS_ERROR_FAILURE; + + nsFileSpec defaultsDirSpec; + profDefaultsDir->GetFileSpec(&defaultsDirSpec); + + // Need a separate hack for Mac. For now app folder is the fall back on Mac. + nsFilePath(tmpFileSpec.GetNativePathCString(), PR_TRUE); + + // Copy contents from defaults folder. + if (defaultsDirSpec.Exists()) + defaultsDirSpec.RecursiveCopy(tmpFileSpec); + } + } + + FreeProfileStruct(aProfile); return rv; } @@ -642,93 +513,10 @@ NS_IMETHODIMP nsProfile::GetProfileDir(const char *profileName, nsFileSpec* prof // Location: Common/Profiles NS_IMETHODIMP nsProfile::GetProfileCount(PRInt32 *numProfiles) { + *numProfiles = 0; - nsresult rv = NS_OK; - -#if defined(DEBUG_profile) - printf("ProfileManager : GetProfileCount\n"); -#endif - - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - // Enumerate all subkeys (immediately) under the given node. - nsCOMPtr enumKeys; - nsRegistryKey key; - - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->EnumerateSubtrees( key, getter_AddRefs(enumKeys)); - - if (NS_SUCCEEDED(rv)) - { - PRInt32 numKeys=0; - rv = enumKeys->First(); - - while( NS_SUCCEEDED( rv ) && (NS_OK != enumKeys->IsDone()) ) - { - nsCOMPtr base; - - rv = enumKeys->CurrentItem( getter_AddRefs(base) ); - - if (NS_SUCCEEDED(rv)) - { - // Get specific interface. - nsCOMPtr node; - nsIID nodeIID = NS_IREGISTRYNODE_IID; - - rv = base->QueryInterface( nodeIID, getter_AddRefs(node)); - - // Test that result. - if (NS_SUCCEEDED(rv)) - { - // Get node name. - nsXPIDLCString profile; - nsXPIDLCString isMigrated; - - rv = node->GetName( getter_Copies(profile) ); - - if (NS_SUCCEEDED(rv) && (profile)) - { - nsRegistryKey profKey; - - rv = m_reg->GetSubtree(key, profile, &profKey); - - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->GetString(profKey, REGISTRY_MIGRATED_STRING, getter_Copies(isMigrated)); - - if (NS_SUCCEEDED(rv) && (isMigrated)) - { - if (PL_strcmp(isMigrated, REGISTRY_YES_STRING) == 0) - { - numKeys++; - } - } - } - } - } - } - rv = enumKeys->Next(); - } - *numProfiles = numKeys; - } - else - { -#if defined(DEBUG_profile) - printf("Profiles : Can't enumerate subtrees.\n" ); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Registry : Couldn't get Profiles subtree.\n"); -#endif - } - return rv; + gProfileDataAccess->GetNumProfiles(numProfiles); + return NS_OK; } @@ -737,116 +525,10 @@ NS_IMETHODIMP nsProfile::GetProfileCount(PRInt32 *numProfiles) // Otherwise it return the name of the first valid profile. NS_IMETHODIMP nsProfile::GetFirstProfile(char **profileName) { - nsresult rv = NS_OK; + gProfileDataAccess->GetFirstProfile(profileName); + gProfileDataAccess->SetCurrentProfile(*profileName); -#if defined(DEBUG_profile) - printf("ProfileManager : GetFirstProfile\n"); -#endif - - // Enumerate all subkeys (immediately) under the given node. - nsCOMPtr enumKeys; - nsRegistryKey key; - - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->EnumerateSubtrees( key, getter_AddRefs(enumKeys)); - - if (NS_SUCCEEDED(rv)) - { - // Go to beginning. - rv = enumKeys->First(); - - while(NS_SUCCEEDED(rv)&& (NS_OK != enumKeys->IsDone()) ) - { - nsCOMPtr base; - rv = enumKeys->CurrentItem(getter_AddRefs(base)); - - if (NS_SUCCEEDED(rv)) - { - // Get specific interface. - nsCOMPtr node; - nsIID nodeIID = NS_IREGISTRYNODE_IID; - rv = base->QueryInterface( nodeIID, getter_AddRefs(node)); - - if (NS_SUCCEEDED(rv)) - { - nsXPIDLCString isMigrated; - nsRegistryKey profKey; - - // Get node name. - rv = node->GetName(profileName ); - - // Profiles need to be migrated are not considered as valid profiles - // On finding the valid profile, breaks out of the while loop. - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->GetSubtree(key, *profileName, &profKey); - - m_reg->GetString(profKey, REGISTRY_MIGRATED_STRING, getter_Copies(isMigrated)); - - if (PL_strcmp(isMigrated, REGISTRY_NO_STRING) == 0) - { - rv = enumKeys->Next(); - continue; - } - else - { - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->SetString(key, REGISTRY_CURRENT_PROFILE_STRING, *profileName); - - if (NS_SUCCEEDED(rv)) - { - break; - } - else - { -#if defined(DEBUG_profile) - printf("RegistryNode : Can't set Current Profile name.\n" ); -#endif - } - } - } - } - else - { -#if defined(DEBUG_profile) - printf("RegistryNode : Couldn't get the node name.\n"); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("RegistryNode : Couldn't get the interface.\n"); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Profiles : Couldn't get current enumerated item.\n"); -#endif - } - rv = enumKeys->Next(); - } //end while loop - } - else - { -#if defined(DEBUG_profile) - printf("Profiles : Couldn't enumerate subtrees.\n"); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Registry : Couldn't get Profiles subtree.\n"); -#endif - } - return rv; + return NS_OK; } @@ -854,44 +536,8 @@ NS_IMETHODIMP nsProfile::GetFirstProfile(char **profileName) NS_IMETHODIMP nsProfile::GetCurrentProfile(char **profileName) { - nsresult rv = NS_OK; - -#if defined(DEBUG_profile) - printf("ProfileManager : GetCurrentProfile\n"); -#endif - - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - nsRegistryKey key; - - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - - if (NS_SUCCEEDED(rv)) - { - - rv = m_reg->GetString( key, REGISTRY_CURRENT_PROFILE_STRING, profileName ); - - // if fails to get the current profile set the value to null - if (NS_FAILED(rv)) - { - *profileName = nsnull; - -#if defined(DEBUG_profile) - printf("Profiles:Can't get Current Profile value.\n" ); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Registry : Couldn't get Profiles subtree.\n"); -#endif - } -#if defined(DEBUG_profile) - printf("current profile = %s\n", *profileName?*profileName:"(null)"); -#endif - return rv; + gProfileDataAccess->GetCurrentProfile(profileName); + return NS_OK; } // Returns the name of the current profile directory @@ -899,31 +545,12 @@ NS_IMETHODIMP nsProfile::GetCurrentProfileDir(nsFileSpec* profileDir) { nsresult rv = NS_OK; -#if defined(DEBUG_profile) - printf("ProfileManager : GetCurrentProfileDir\n"); -#endif - char *profileName = nsnull; - rv = GetCurrentProfile(&profileName); - if (NS_SUCCEEDED(rv)) - { - rv = GetProfileDir(profileName, profileDir); + GetCurrentProfile(&profileName); - if (NS_FAILED(rv)) - { -#if defined(DEBUG_profile) - printf("Couldn't get the profile dir.\n" ); -#endif - } - - } - else - { -#if defined(DEBUG_profile) - printf("Couldn't get the current profile.\n" ); -#endif - } + rv = GetProfileDir(profileName, profileDir); + if (NS_FAILED(rv)) return rv; PR_FREEIF(profileName); return rv; @@ -939,82 +566,46 @@ NS_IMETHODIMP nsProfile::SetProfileDir(const char *profileName, nsFileSpec& prof { nsresult rv = NS_OK; - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - nsRegistryKey key; - - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - - if (NS_SUCCEEDED(rv)) - { - nsRegistryKey newKey; - - rv = m_reg->AddSubtree(key, profileName, &newKey); - - if (NS_SUCCEEDED(rv)) - { - // Create a tmp Filespec and create a directory if required - nsFileSpec tmpDir(profileDir); + // Create a tmp Filespec and create a directory if required + nsFileSpec tmpDir(profileDir); - if (!profileDir.Exists()) - { - // nsPersistentFileDescriptor requires an existing - // object. Make it first. - tmpDir.CreateDirectory(); - } - - - nsXPIDLCString profileDirString; - - nsCOMPtrspec; - rv = NS_NewFileSpecWithSpec(profileDir, getter_AddRefs(spec)); - if (NS_SUCCEEDED(rv)) { - rv = spec->GetPersistentDescriptorString(getter_Copies(profileDirString)); - } - - // Set the entry REGISTRY_DIRECTORY_STRING for this profile - if (NS_SUCCEEDED(rv) && profileDirString) - { - rv = m_reg->SetString(newKey, REGISTRY_DIRECTORY_STRING, profileDirString); - if (NS_FAILED(rv)) return rv; - - rv = CreateUserDirectories(tmpDir); - if (NS_FAILED(rv)) return rv; - - // A 5.0 profile needs no migration. - // Hence setting migrated flag to REGISTRY_YES_STRING - // Need to change this logic to "migrate" = true/false. - rv = m_reg->SetString( newKey, REGISTRY_MIGRATED_STRING, REGISTRY_YES_STRING); - } - else - { -#if defined(DEBUG_profile) - printf("NULL value received for directory name.\n" ); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Profiles : Could not add profile name subtree.\n" ); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Registry : Couldn't get Profiles subtree.\n"); -#endif - } - - rv = m_reg->SetString(key, REGISTRY_CURRENT_PROFILE_STRING, profileName); - -#if defined(DEBUG_profile) - if (NS_FAILED(rv)) { - printf("Couldn't set CurrentProfile name.\n" ); + if (!profileDir.Exists()) + { + // nsPersistentFileDescriptor requires an existing + // object. Make it first. + tmpDir.CreateDirectory(); } -#endif + + nsXPIDLCString profileDirString; + + nsCOMPtrspec; + rv = NS_NewFileSpecWithSpec(profileDir, getter_AddRefs(spec)); + if (NS_SUCCEEDED(rv)) { + rv = spec->GetPersistentDescriptorString(getter_Copies(profileDirString)); + } + if (NS_FAILED(rv)) return rv; + + ProfileStruct *aProfile; + + aProfile = (ProfileStruct *) PR_Malloc(sizeof(ProfileStruct)); + if (!aProfile) + return NS_ERROR_OUT_OF_MEMORY; + + aProfile->profileName = nsCRT::strdup(profileName); + aProfile->profileLocation = nsCRT::strdup(profileDirString); + aProfile->isMigrated = nsCRT::strdup(REGISTRY_YES_STRING); + aProfile->NCProfileName = nsnull; + aProfile->NCDeniedService = nsnull; + + rv = CreateUserDirectories(tmpDir); + if (NS_FAILED(rv)) return rv; + + gProfileDataAccess->SetValue(aProfile); + if (NS_FAILED(rv)) return rv; + + gProfileDataAccess->SetCurrentProfile(profileName); + if (NS_FAILED(rv)) return rv; + return rv; } @@ -1054,18 +645,18 @@ NS_IMETHODIMP nsProfile::CreateNewProfile(const char* profileName, const char* n // append profile name dirSpec += profileName; - dirSpec.MakeUnique(); + dirSpec.MakeUnique(); } else { dirSpec = nativeProfileDir; - // this prevents people from choosing there profile directory - // or another directory, and remove it when they delete the profile - // - // append profile name - dirSpec += profileName; - dirSpec.MakeUnique(); - } + // this prevents people from choosing there profile directory + // or another directory, and remove it when they delete the profile + // + // append profile name + dirSpec += profileName; + dirSpec.MakeUnique(); + } #if defined(DEBUG_profile) printf("before SetProfileDir\n"); @@ -1101,6 +692,9 @@ NS_IMETHODIMP nsProfile::CreateNewProfile(const char* profileName, const char* n defaultsDirSpec.RecursiveCopy(dirSpec); } + gProfileDataAccess->mNumProfiles++; + gProfileDataAccess->mProfileDataChanged = PR_TRUE; + return NS_OK; } @@ -1119,34 +713,25 @@ nsresult nsProfile::CreateUserDirectories(const nsFileSpec& profileDir) tmpDir += "News"; if (!tmpDir.Exists()) - { tmpDir.CreateDirectory(); - } tmpDir = profileDir; tmpDir += "ImapMail"; if (!tmpDir.Exists()) - { tmpDir.CreateDirectory(); - } - tmpDir = profileDir; tmpDir += "Mail"; if (!tmpDir.Exists()) - { tmpDir.CreateDirectory(); - } tmpDir = profileDir; tmpDir += "Cache"; if (!tmpDir.Exists()) - { tmpDir.CreateDirectory(); - } return rv; } @@ -1163,9 +748,7 @@ nsresult nsProfile::DeleteUserDirectories(const nsFileSpec& profileDir) #endif if (profileDir.Exists()) - { profileDir.Delete(PR_TRUE); - } return rv; } @@ -1200,6 +783,7 @@ NS_IMETHODIMP nsProfile::RenameProfile(const char* oldName, const char* newName) rv = RenameProfileDir(newName); if (NS_FAILED(rv)) { rv = DeleteProfile(newName, PR_FALSE /* don't delete files */); + gProfileDataAccess->mNumProfiles++; /* profile is just replaced. Keep the count same */ NS_ASSERTION(NS_SUCCEEDED(rv), "failed to delete the aborted profile in rename"); return NS_ERROR_FAILURE; } @@ -1208,9 +792,14 @@ NS_IMETHODIMP nsProfile::RenameProfile(const char* oldName, const char* newName) rv = DeleteProfile(oldName, PR_FALSE /* don't delete files */); if (NS_FAILED(rv)) return rv; + /* profile is just replaced. But Keep up the count */ + gProfileDataAccess->mNumProfiles++; + rv = ForgetCurrentProfile(); if (NS_FAILED(rv)) return rv; + gProfileDataAccess->mProfileDataChanged = PR_TRUE; + return NS_OK; } @@ -1218,83 +807,36 @@ NS_IMETHODIMP nsProfile::RenameProfile(const char* oldName, const char* newName) // In the process creates new profile subtree. nsresult nsProfile::CopyRegKey(const char *oldProfile, const char *newProfile) { - nsCOMPtr enumKeys; - nsRegistryKey sourceKey, destKey, profileRootKey; + nsresult rv = NS_OK; - nsresult rv; - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &profileRootKey); - if (NS_FAILED(rv)) return rv; - - rv = m_reg->GetSubtree(profileRootKey, (char *) oldProfile, &sourceKey); - if (NS_FAILED(rv)) return rv; - - rv = m_reg->AddSubtree(profileRootKey, (char *) newProfile, &destKey); - if (NS_FAILED(rv)) return rv; + ProfileStruct *aProfile; - rv = m_reg->EnumerateValues(sourceKey, getter_AddRefs(enumKeys)); - if (NS_FAILED(rv)) return rv; - - rv = enumKeys->First(); - if (NS_FAILED(rv)) return rv; - - // Enumerate subkeys till done. - while( NS_SUCCEEDED( rv ) && (NS_OK != enumKeys->IsDone()) ) - { - nsCOMPtr base; - rv = enumKeys->CurrentItem(getter_AddRefs(base)); - - // Test result. - if (NS_SUCCEEDED(rv)) - { - // Get specific interface. - nsCOMPtr value; - nsIID valueIID = NS_IREGISTRYVALUE_IID; - rv = base->QueryInterface( valueIID, getter_AddRefs(value)); - - // Test that result. - if (NS_SUCCEEDED(rv)) - { - // Get node name. - nsXPIDLCString entryName; - nsXPIDLCString entryValue; - - rv = value->GetName(getter_Copies(entryName)); - - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->GetString( sourceKey, entryName, getter_Copies(entryValue)); - - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->SetString(destKey, entryName, entryValue); - } - } - } - } - rv = enumKeys->Next(); - } + gProfileDataAccess->GetValue(oldProfile, &aProfile); + + if (aProfile == nsnull) + return NS_ERROR_FAILURE; + + aProfile->profileName = nsCRT::strdup(newProfile); + aProfile->NCProfileName = nsnull; + aProfile->NCDeniedService = nsnull; + + rv = gProfileDataAccess->SetValue(aProfile); + + FreeProfileStruct(aProfile); return rv; } NS_IMETHODIMP nsProfile::ForgetCurrentProfile() { - nsresult rv; - - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - nsRegistryKey profileRootKey; - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &profileRootKey); - if (NS_FAILED(rv)) return rv; + nsresult rv = NS_OK; // Remove the current profile subtree from the registry. - rv = m_reg->SetString(profileRootKey, REGISTRY_CURRENT_PROFILE_STRING, ""); + gProfileDataAccess->SetCurrentProfile(""); if (NS_FAILED(rv)) return rv; - NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); + gProfileDataAccess->mForgetProfileCalled = PR_TRUE; + + NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); if (NS_FAILED(rv)) return rv; if (!locator) return NS_ERROR_FAILURE; @@ -1302,7 +844,7 @@ NS_IMETHODIMP nsProfile::ForgetCurrentProfile() rv = locator->ForgetProfileDir(); if (NS_FAILED(rv)) return rv; - return NS_OK; + return rv; } // Delete a profile from the registry @@ -1312,22 +854,6 @@ NS_IMETHODIMP nsProfile::DeleteProfile(const char* profileName, PRBool canDelete { nsresult rv = NS_OK; -#if defined(DEBUG_profile) - printf("ProfileManager : DeleteProfile(%s,%s)\n", profileName, canDeleteFiles?"true":"false"); -#endif - - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - nsRegistryKey key; - - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - if (NS_FAILED(rv)) return rv; - -#if defined(DEBUG_profile) - printf("DeleteProfile : deleted profile -> %s <-\n", profileName); -#endif - rv = ForgetCurrentProfile(); if (NS_FAILED(rv)) return rv; @@ -1343,145 +869,21 @@ NS_IMETHODIMP nsProfile::DeleteProfile(const char* profileName, PRBool canDelete } // Remove the subtree from the registry. - rv = m_reg->RemoveSubtree(key, profileName); + gProfileDataAccess->RemoveSubTree(profileName); if (NS_FAILED(rv)) return rv; - + + gProfileDataAccess->mProfileDataChanged = PR_TRUE; + return rv; } - -// Get all profiles fro the registry's Profiles subtree -// All the profiles that need to be migrated will be appended -// with the text " - migrate" (e.g. - migrate). -// Need to change this lotic to have True/False to represent the -// migration state. -// Names are stored in the global array mProfiles[]. -nsresult nsProfile::GetAllProfiles() -{ - nsresult rv = NS_OK; - -#if defined(DEBUG_profile) - printf("ProfileManager : GetAllProfiles\n"); -#endif - - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - PRInt32 idx = 0; - - // Enumerate all subkeys (immediately) under the given node. - nsCOMPtr enumKeys; - nsRegistryKey key; - - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->EnumerateSubtrees( key, getter_AddRefs(enumKeys)); - - if (NS_SUCCEEDED(rv)) - { - - rv = enumKeys->First(); - - // Enumerate subkeys till done. - while( NS_SUCCEEDED( rv ) && (NS_OK != enumKeys->IsDone()) ) - { - nsCOMPtr base; - rv = enumKeys->CurrentItem(getter_AddRefs(base)); - - if (NS_SUCCEEDED(rv)) - { - // Get specific interface. - nsCOMPtr node; - nsIID nodeIID = NS_IREGISTRYNODE_IID; - rv = base->QueryInterface( nodeIID, getter_AddRefs(node)); - - // Test that result. - if (NS_SUCCEEDED(rv)) - { - // Get node name. - nsXPIDLCString profile; - nsXPIDLCString isMigrated; - - rv = node->GetName(getter_Copies(profile)); - - if (NS_SUCCEEDED(rv) && (profile)) - { - nsRegistryKey profKey; - - rv = m_reg->GetSubtree(key, profile, &profKey); - - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->GetString(profKey, REGISTRY_MIGRATED_STRING, getter_Copies(isMigrated)); - - if (NS_SUCCEEDED(rv) && (isMigrated)) - { - if (PL_strcmp(isMigrated, REGISTRY_NO_STRING) == 0) - { - PL_strcpy(mProfiles[idx], profile); - PL_strcat(mProfiles[idx], " - migrate"); - } - else - { - PL_strcpy(mProfiles[idx], profile); - } - -#if defined(DEBUG_profile) - printf("profile%d = %s\n", idx, mProfiles[idx]); -#endif - } - } - } - } - } - rv = enumKeys->Next(); - - if (NS_FAILED(rv)) - { -#if defined(DEBUG_profile) - printf( "Error advancing enumerator, rv=0x%08X\n", (int)rv ); -#endif - } - idx++; - } - mNumProfiles = idx; - } - } - - return NS_OK; -} - - - // Get the list of all profiles // Populate the input param. -// Essentially calls GetAllProfiles to fill mProfiles[]. // This method is written to support the core service // call to get the names all profiles. NS_IMETHODIMP nsProfile::GetProfileList(char **profileListStr) { - nsresult rv; - nsCAutoString profileList(""); - -#if defined(DEBUG_profile) - printf("Inside GetProfileList routine.\n" ); -#endif - - rv = GetAllProfiles(); - if (NS_FAILED(rv)) return rv; - - for (PRInt32 i=0; i < mNumProfiles; i++) - { - if (i != 0) - { - profileList += ","; - } - profileList += mProfiles[i]; - } - - *profileListStr = nsCRT::strdup((const char *)profileList); + gProfileDataAccess->GetProfileList(profileListStr); return NS_OK; } @@ -1499,17 +901,7 @@ NS_IMETHODIMP nsProfile::StartApprunner(const char* profileName) printf("ProfileManager : StartApprunner\n"); #endif - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - // First, set the profile to be the current profile. - // So that FileLocation services grabs right directory when it needs to. - nsRegistryKey profileRootKey; - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &profileRootKey); - if (NS_FAILED(rv)) return rv; - - rv = m_reg->SetString(profileRootKey, REGISTRY_CURRENT_PROFILE_STRING, profileName); - if (NS_FAILED(rv)) return rv; + gProfileDataAccess->SetCurrentProfile(profileName); NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); if (NS_FAILED(rv)) return rv; @@ -1532,26 +924,6 @@ NS_IMETHODIMP nsProfile::StartApprunner(const char* profileName) prefs->ResetPrefs(); prefs->ReadUserPrefs(); -/* - // rjc says: no longer do this! The profile manager now runs BEFORE - // hiddenwindow.xul (which causes bookmarks to be read in) is loaded - - - // we need to re-read the bookmarks here, other wise the user - // won't have there 4.x bookmarks until after they exit and come back - NS_WITH_SERVICE(nsIBookmarksService, bookmarks, kBookmarksCID, &rv); - if (NS_FAILED(rv)) return rv; - rv = bookmarks->ReadBookmarks(); - if (NS_FAILED(rv)) { - //temporary printf - printf("failed to read bookmarks\n"); - return rv; - } -*/ - - rv = CloseRegistry(); - if (NS_FAILED(rv)) return rv; - return NS_OK; } @@ -1588,211 +960,18 @@ NS_IMETHODIMP nsProfile::MigrateProfileInfo() PL_strcpy(oldRegFile, regLocation->GetNativePathCString()); #endif /* XP_PC */ - nsCOMPtr oldReg; - rv = nsComponentManager::CreateInstance(kRegistryCID, - nsnull, - nsIRegistry::GetIID(), - getter_AddRefs(oldReg)); - if (NS_FAILED(rv)) return rv; - - rv = oldReg->Open(oldRegFile); - if (NS_FAILED(rv)) return rv; - - // Enumerate 4x tree and create an array of that information. - // Enumerate all subkeys (immediately) under the given node. - nsCOMPtr enumKeys; - - rv = oldReg->EnumerateSubtrees(nsIRegistry::Users, - getter_AddRefs(enumKeys)); - - if (NS_SUCCEEDED(rv)) - { - - rv = enumKeys->First(); - - // Enumerate subkeys till done. - while( NS_SUCCEEDED( rv ) && (NS_OK != enumKeys->IsDone()) ) - { - nsCOMPtr base; - rv = enumKeys->CurrentItem(getter_AddRefs(base)); - - if (NS_SUCCEEDED(rv)) - { - // Get specific interface. - nsCOMPtr node; - nsIID nodeIID = NS_IREGISTRYNODE_IID; - rv = base->QueryInterface( nodeIID, getter_AddRefs(node)); - - // Get Node details. - if (NS_SUCCEEDED(rv)) - { - // Get node name. - char *profile = nsnull; - rv = node->GetName(&profile); - -#if defined(DEBUG_profile) - printf("oldProflie = %s\n", profile); -#endif - - nsRegistryKey key; - - rv = oldReg->GetSubtree(nsIRegistry::Users, profile, &key); - - if (NS_SUCCEEDED(rv)) - { - PL_strcpy(mOldProfiles[mNumOldProfiles], nsUnescape(profile)); - } - - nsXPIDLCString profLoc; - - rv = oldReg->GetString( key, "ProfileLocation", getter_Copies(profLoc)); - -#if defined(DEBUG_profile) - printf("oldProflie Location = %s\n", profLoc); -#endif - - if (NS_SUCCEEDED(rv)) - { - PL_strcpy(mOldProfLocations[mNumOldProfiles], profLoc); - mNumOldProfiles++; - } - - } - } - rv = enumKeys->Next(); - } - } + rv = gProfileDataAccess->Get4xProfileInfo(oldRegFile); - oldReg->Close(); #elif defined (XP_BEOS) #else /* XP_UNIX */ - char *unixProfileName = PR_GetEnv(PROFILE_NAME_ENVIRONMENT_VARIABLE); - char *unixProfileDirectory = PR_GetEnv(PROFILE_HOME_ENVIRONMENT_VARIABLE); - - if (!unixProfileName || !unixProfileDirectory || (PL_strlen(unixProfileName) == 0) || (PL_strlen(unixProfileDirectory) == 0)) { - unixProfileName = PR_GetEnv(USER_ENVIRONMENT_VARIABLE); - unixProfileDirectory = PR_GetEnv(HOME_ENVIRONMENT_VARIABLE); - } + rv = gProfileDataAccess->Get4xProfileInfo(nsnull); - if (unixProfileName && unixProfileDirectory) { - PL_strcpy(mOldProfiles[mNumOldProfiles], nsUnescape(unixProfileName)); - PL_strcpy(mOldProfLocations[mNumOldProfiles], unixProfileDirectory); - PL_strcat(mOldProfLocations[mNumOldProfiles], "/.netscape"); -#ifdef DEBUG_profile - printf("unix profile is %s, unix profile dir is %s\n",mOldProfiles[mNumOldProfiles],mOldProfLocations[mNumOldProfiles]); -#endif - mNumOldProfiles++; - } #endif /* XP_PC || XP_MAC */ - if (mNumOldProfiles > 0) { - rv = UpdateMozProfileRegistry(); - if (NS_FAILED(rv)) return rv; - } - - return rv; } -// Update the mozregistry with the 4x profile names -// and thier locations. Entry REGISTRY_MIGRATED_STRING is set to REGISTRY_NO_STRING -// to differentiate these profiles from 5x profiles. -// mOldProfiles, mOldProfLocations carried info about -// profile names and locations respectively. They are -// populated inthe routine MigrateProfileInfo() -nsresult nsProfile::UpdateMozProfileRegistry() -{ - - nsresult rv = NS_OK; - - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - -#if defined(DEBUG_profile) - printf("Entered UpdateMozProfileRegistry.\n"); -#endif - - for (PRInt32 idx = 0; idx < mNumOldProfiles; idx++) - { - nsRegistryKey key; - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - - if (NS_SUCCEEDED(rv)) - { - nsRegistryKey newKey; - - rv = m_reg->SetString(key, REGISTRY_NEED_MIGRATION_STRING, REGISTRY_TRUE_STRING); - - rv = m_reg->GetSubtree(key, mOldProfiles[idx], &newKey); - - if (NS_FAILED(rv)) - { - rv = m_reg->AddSubtree(key, mOldProfiles[idx], &newKey); - - if (NS_SUCCEEDED(rv)) - { - nsFileSpec profileDir(mOldProfLocations[idx]); - - nsXPIDLCString profileDirString; - nsCOMPtrspec; - rv = NS_NewFileSpecWithSpec(profileDir, getter_AddRefs(spec)); - if (NS_SUCCEEDED(rv)) { - rv = spec->GetPersistentDescriptorString(getter_Copies(profileDirString)); - } - - if (NS_SUCCEEDED(rv) && profileDirString) - { - - rv = m_reg->SetString(newKey, REGISTRY_DIRECTORY_STRING, profileDirString); - - if (NS_FAILED(rv)) - { -#if defined(DEBUG_profile) - printf("Couldn't set directory property.\n" ); -#endif - } - - rv = m_reg->SetString(newKey, REGISTRY_MIGRATED_STRING, REGISTRY_NO_STRING); - - if (NS_FAILED(rv)) - { -#if defined(DEBUG_profile) - printf("Couldn't add migrate flag.\n" ); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("NULL value received for directory name.\n" ); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Profiles : Could not add profile subtree.\n" ); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Profiles : Profile name subtree already exists.\n" ); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Registry : Couldn't get Profiles subtree.\n"); -#endif - } - } - - return rv; -} // Migrate a selected profile // Set the profile to the current profile....debatable. @@ -1853,49 +1032,14 @@ NS_IMETHODIMP nsProfile::MigrateProfile(const char* profileName, PRBool showProg rv = pPrefMigrator->ProcessPrefs(showProgressAsModalWindow); if (NS_FAILED(rv)) return rv; - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - //unmark migrate flag - nsRegistryKey key; - - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - - if (NS_SUCCEEDED(rv)) - { - nsRegistryKey newKey; - rv = m_reg->GetSubtree(key, profileName, &newKey); - - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->SetString( newKey, REGISTRY_MIGRATED_STRING, REGISTRY_YES_STRING); - - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->SetString(key, REGISTRY_CURRENT_PROFILE_STRING, profileName); - - if (NS_FAILED(rv)) - { -#if defined(DEBUG_profile) - printf("Profiles : Couldn't set CurrentProfile Name.\n"); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Profiles : Couldn't set migrate flag.\n"); -#endif - } - SetProfileDir(profileName, newProfDir); - } - else - { -#if defined(DEBUG_profile) - printf("Profiles : Couldn't get profileName subtree.\n"); -#endif - } - } + rv = SetProfileDir(profileName, newProfDir); + if (NS_FAILED(rv)) return rv; + + gProfileDataAccess->mNumProfiles++; + gProfileDataAccess->mNumOldProfiles--; + + gProfileDataAccess->mProfileDataChanged = PR_TRUE; + return rv; } @@ -1993,125 +1137,56 @@ NS_IMETHODIMP nsProfile::ProcessPREGInfo(const char* data) if (userServiceDenial.mLength > 0) userProfileName.SetString(""); - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - char *curProfile = nsnull; rv = GetCurrentProfile(&curProfile); + if (NS_FAILED(rv)) return rv; - if (NS_SUCCEEDED(rv)) + if (userProfileName.mLength > 0) { - if (userProfileName.mLength > 0) - rv = RenameProfile(curProfile, userProfileName.ToNewCString()); + rv = RenameProfile(curProfile, userProfileName.ToNewCString()); - if (NS_SUCCEEDED(rv)) - { - nsRegistryKey key; - - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - - if (NS_SUCCEEDED(rv)) - { - nsRegistryKey newKey; - - if (userProfileName.mLength > 0) - { - rv = m_reg->GetSubtree(key, userProfileName.ToNewCString(), &newKey); - - if (NS_SUCCEEDED(rv)) - { - // Register new info - rv = m_reg->SetString(newKey, REGISTRY_NC_PROFILE_NAME_STRING, userProfileName.ToNewCString()); - } - else - { -#if defined(DEBUG_profile) - printf("Profiles : Could not set NetCenter properties.\n" ); -#endif - } - - rv = m_reg->SetString(key, REGISTRY_CURRENT_PROFILE_STRING, userProfileName.ToNewCString()); - - if (NS_FAILED(rv)) - { -#if defined(DEBUG_profile) - printf("Couldn't set CurrentProfile name.\n" ); -#endif - } - } - } - else - { -#if defined(DEBUG_profile) - printf("Registry : Couldn't get Profiles subtree.\n"); -#endif - } - - if (userServiceDenial.mLength > 0) - rv = m_reg->SetString(key, REGISTRY_NC_SERVICE_DENIAL_STRING, userServiceDenial.ToNewCString()); - - rv = m_reg->SetString(key, REGISTRY_HAVE_PREG_INFO_STRING, REGISTRY_TRUE_STRING); - - if (NS_FAILED(rv)) - { -#if defined(DEBUG_profile) - printf("Couldn't set Preg info flag.\n" ); -#endif - } - } + ProfileStruct* aProfile; + + gProfileDataAccess->GetValue(userProfileName.ToNewCString(), &aProfile); + + PRInt32 length = PL_strlen(userProfileName.ToNewCString()); + aProfile->NCProfileName = (char *) PR_Realloc(aProfile->NCProfileName, length+1); + PL_strcpy(aProfile->NCProfileName, userProfileName.ToNewCString()); + + gProfileDataAccess->SetValue(aProfile); + gProfileDataAccess->SetCurrentProfile(userProfileName.ToNewCString()); + FreeProfileStruct(aProfile); } - PR_FREEIF(curProfile); + else if (userServiceDenial.mLength > 0) + { + ProfileStruct* aProfile; + + gProfileDataAccess->GetValue(curProfile, &aProfile); + + PRInt32 length = PL_strlen(userServiceDenial.ToNewCString()); + aProfile->NCDeniedService = (char *) PR_Realloc(aProfile->NCDeniedService, length+1); + PL_strcpy(aProfile->NCDeniedService, userServiceDenial.ToNewCString()); + + gProfileDataAccess->SetValue(aProfile); + FreeProfileStruct(aProfile); + } + + gProfileDataAccess->SetPREGInfo(REGISTRY_YES_STRING); + + PR_FREEIF(curProfile); return rv; } NS_IMETHODIMP nsProfile::IsPregCookieSet(char **pregSet) { - - nsresult rv = NS_OK; - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - nsRegistryKey key; - - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->GetString(key, REGISTRY_HAVE_PREG_INFO_STRING, pregSet); - } - else - { -#if defined(DEBUG_profile) - printf("Registry : Couldn't get Profiles subtree.\n"); -#endif - } - return rv; + gProfileDataAccess->GetPREGInfo(pregSet); + return NS_OK; } NS_IMETHODIMP nsProfile::ProfileExists(const char *profileName, PRBool *exists) { - nsresult rv = NS_OK; - - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - nsRegistryKey key; - - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - if (NS_FAILED(rv)) return rv; - - nsRegistryKey newKey; - - // Get handle to passed - rv = m_reg->GetSubtree(key, profileName, &newKey); - if (NS_FAILED(rv)) { - *exists = PR_FALSE; - } - else { - *exists = PR_TRUE; - } - + *exists = gProfileDataAccess->ProfileExists(profileName); return NS_OK; } @@ -2121,92 +1196,10 @@ NS_IMETHODIMP nsProfile::ProfileExists(const char *profileName, PRBool *exists) NS_IMETHODIMP nsProfile::Get4xProfileCount(PRInt32 *numProfiles) { - nsresult rv = NS_OK; + *numProfiles = 0; -#if defined(DEBUG_profile) - printf("ProfileManager : Get4xProfileCount\n"); -#endif - - rv = OpenRegistry(); - if (NS_FAILED(rv)) return rv; - - // Enumerate all subkeys (immediately) under the given node. - nsCOMPtr enumKeys; - nsRegistryKey key; - - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->EnumerateSubtrees( key, getter_AddRefs(enumKeys)); - - if (NS_SUCCEEDED(rv)) - { - PRInt32 numKeys=0; - rv = enumKeys->First(); - - while( NS_SUCCEEDED( rv ) && (NS_OK != enumKeys->IsDone()) ) - { - nsCOMPtr base; - - rv = enumKeys->CurrentItem(getter_AddRefs(base)); - - if (NS_SUCCEEDED(rv)) - { - // Get specific interface. - nsCOMPtr node; - nsIID nodeIID = NS_IREGISTRYNODE_IID; - - rv = base->QueryInterface( nodeIID, getter_AddRefs(node)); - - // Test that result. - if (NS_SUCCEEDED(rv)) - { - // Get node name. - nsXPIDLCString profile; - nsXPIDLCString isMigrated; - - rv = node->GetName(getter_Copies(profile)); - - if (NS_SUCCEEDED(rv) && (profile)) - { - nsRegistryKey profKey; - - rv = m_reg->GetSubtree(key, profile, &profKey); - - if (NS_SUCCEEDED(rv)) - { - rv = m_reg->GetString(profKey, REGISTRY_MIGRATED_STRING, getter_Copies(isMigrated)); - - if (NS_SUCCEEDED(rv) && (isMigrated)) - { - if (PL_strcmp(isMigrated, REGISTRY_NO_STRING) == 0) - { - numKeys++; - } - } - } - } - } - } - rv = enumKeys->Next(); - } - *numProfiles = numKeys; - } - else - { -#if defined(DEBUG_profile) - printf("Profiles : Can't enumerate subtrees.\n" ); -#endif - } - } - else - { -#if defined(DEBUG_profile) - printf("Registry : Couldn't get Profiles subtree.\n"); -#endif - } - return rv; + gProfileDataAccess->GetNum4xProfiles(numProfiles); + return NS_OK; } @@ -2214,13 +1207,13 @@ NS_IMETHODIMP nsProfile::Get4xProfileCount(PRInt32 *numProfiles) NS_IMETHODIMP nsProfile::MigrateAllProfiles() { nsresult rv = NS_OK; - for (PRInt32 i=0; i < mNumOldProfiles; i++) + for (PRInt32 i=0; i < gProfileDataAccess->mNumOldProfiles; i++) { - rv = MigrateProfile(mOldProfiles[i], PR_FALSE /* don't show progress as modal window */); + rv = MigrateProfile(gProfileDataAccess->m4xProfiles[i]->profileName, PR_FALSE /* don't show progress as modal window */); if (NS_FAILED(rv)) return rv; } - return NS_OK; + return rv; } @@ -2229,7 +1222,7 @@ nsresult nsProfile::RenameProfileDir(const char* newProfileName) nsresult rv = NS_OK; nsFileSpec dirSpec; - rv = GetProfileDir(newProfileName, &dirSpec); + rv = GetProfileDir(newProfileName, &dirSpec); if (NS_FAILED(rv)) return rv; nsFileSpec renamedDirSpec = dirSpec; @@ -2241,7 +1234,7 @@ nsresult nsProfile::RenameProfileDir(const char* newProfileName) if (NS_FAILED(rv)) return rv; // update the registry - rv = SetProfileDir(newProfileName, dirSpec); + rv = SetProfileDir(newProfileName, dirSpec); if (NS_FAILED(rv)) return rv; return NS_OK; @@ -2298,135 +1291,21 @@ NS_IMETHODIMP nsProfile::CloneProfile(const char* newProfile) printf("ProfileManager : Cloned CurrentProfile to new Profile ->%s<-\n", newProfile); #endif + gProfileDataAccess->mProfileDataChanged = PR_TRUE; return rv; } -nsresult nsProfile::FixRegEntries() +void +nsProfile::FreeProfileStruct(ProfileStruct* aProfile) { - nsresult rv = NS_OK; + if (aProfile) + { + CRTFREEIF(aProfile->profileName); + CRTFREEIF(aProfile->profileLocation); + CRTFREEIF(aProfile->isMigrated); + CRTFREEIF(aProfile->NCProfileName); + CRTFREEIF(aProfile->NCDeniedService); - // Enumerate all subkeys (immediately) under the given node. - nsCOMPtr enumKeys; - nsRegistryKey key; - - // Get the Reg key to Profile subtree - rv = m_reg->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &key); - if (NS_FAILED(rv)) - return NS_ERROR_FAILURE; - - // Get an Enumerator - rv = m_reg->EnumerateSubtrees( key, getter_AddRefs(enumKeys)); - if (NS_FAILED(rv)) - return NS_ERROR_FAILURE; - - // Get the first enum key - rv = enumKeys->First(); - if (NS_FAILED(rv)) - return NS_ERROR_FAILURE; - - - // Enumerate subkeys till done. - while( NS_SUCCEEDED( rv ) && (NS_OK != enumKeys->IsDone()) ) - { - nsCOMPtr base; - - // Get the Current item of the enum key - rv = enumKeys->CurrentItem(getter_AddRefs(base)); - if (NS_FAILED(rv)) - return NS_ERROR_FAILURE; - - // Get specific interface. - nsCOMPtr node; - nsIID nodeIID = NS_IREGISTRYNODE_IID; - - rv = base->QueryInterface( nodeIID, getter_AddRefs(node)); - if (NS_FAILED(rv)) - return NS_ERROR_FAILURE; - - // Get node name. - nsXPIDLCString profile; - nsXPIDLCString isMigrated; - nsXPIDLCString dirName; - - rv = node->GetName(getter_Copies(profile)); - if (NS_FAILED(rv) || (profile == nsnull)) - return NS_ERROR_FAILURE; - - nsRegistryKey profKey; - - // Open the subtree for a specific profile - rv = m_reg->GetSubtree(key, profile, &profKey); - if (NS_FAILED(rv)) - return NS_ERROR_FAILURE; - - // Obtain migration status - rv = m_reg->GetString(profKey, REGISTRY_MIGRATED_STRING, getter_Copies(isMigrated)); - if (NS_FAILED(rv) || (isMigrated == nsnull)) - return NS_ERROR_FAILURE; - - if (PL_strcmp(isMigrated, REGISTRY_YES_STRING) == 0) - { - nsSimpleCharString decodedDirName; - PRBool haveHexBytes = PR_TRUE; - - // Obtain the directory entry of the profile - rv = m_reg->GetString(profKey, REGISTRY_DIRECTORY_STRING, getter_Copies(dirName)); - if (NS_FAILED(rv)) - return NS_ERROR_FAILURE; - - // Decode the directory name to return the ordinary string - nsInputStringStream stream(dirName); - nsPersistentFileDescriptor descriptor; - - char bigBuffer[MAX_PERSISTENT_DATA_SIZE + 1]; - // The first 8 bytes of the data should be a hex version of the data size to follow. - PRInt32 bytesRead = NUM_HEX_BYTES; - bytesRead = stream.read(bigBuffer, bytesRead); - if (bytesRead != NUM_HEX_BYTES) - haveHexBytes = PR_FALSE; - - if (haveHexBytes) - { - bigBuffer[NUM_HEX_BYTES] = '\0'; - - for (int i = 0; i < NUM_HEX_BYTES; i++) - { - if (!(ISHEX(bigBuffer[i]))) - { - haveHexBytes = PR_FALSE; - break; - } - } - } - - if (haveHexBytes) - { - //stream(dirName); - PR_sscanf(bigBuffer, "%x", (PRUint32*)&bytesRead); - if (bytesRead > MAX_PERSISTENT_DATA_SIZE) - { - // Try to tolerate encoded values with no length header - bytesRead = NUM_HEX_BYTES + stream.read(bigBuffer + NUM_HEX_BYTES, MAX_PERSISTENT_DATA_SIZE - NUM_HEX_BYTES); - } - else - { - // Now we know how many bytes to read, do it. - bytesRead = stream.read(bigBuffer, bytesRead); - } - // Make sure we are null terminated - bigBuffer[bytesRead]='\0'; - descriptor.SetData(bigBuffer, bytesRead); - descriptor.GetData(decodedDirName); - - // Set back the simple, decoded directory entry - rv = m_reg->SetString(profKey, REGISTRY_DIRECTORY_STRING, decodedDirName); - if (NS_FAILED(rv)) - return NS_ERROR_FAILURE; - } - } - rv = enumKeys->Next(); - if (NS_FAILED(rv)) - return NS_ERROR_FAILURE; + PR_FREEIF(aProfile); } - return rv; } diff --git a/profile/src/nsProfile.h b/profile/src/nsProfile.h index 37feded0d18e..ff565c27641f 100644 --- a/profile/src/nsProfile.h +++ b/profile/src/nsProfile.h @@ -27,36 +27,21 @@ #include "nsFileSpec.h" #include "nsString.h" #include "nsICmdLineService.h" - - -/* XXX TODO fix this */ +#include "nsProfileAccess.h" #define _MAX_LENGTH 256 -#define _MAX_NUM_PROFILES 50 class nsProfile: public nsIProfile { - NS_DECL_ISUPPORTS - NS_DECL_NSIPROFILE + NS_DECL_ISUPPORTS + NS_DECL_NSIPROFILE private: - nsCOMPtr m_reg; + nsresult ProcessArgs(nsICmdLineService *service, + PRBool *profileDirSet, + nsCString & profileURLStr); + nsresult LoadDefaultProfileDir(nsCString & profileURLStr); - nsresult ProcessArgs(nsICmdLineService *service, - PRBool *profileDirSet, - nsCString & profileURLStr); - nsresult LoadDefaultProfileDir(nsCString & profileURLStr); - nsresult OpenRegistry(); - nsresult CloseRegistry(); - - char mNewProfileData[_MAX_NUM_PROFILES][_MAX_LENGTH]; - char mProfiles[_MAX_NUM_PROFILES][_MAX_LENGTH]; - PRInt32 mCount; - PRInt32 mNumProfiles; - PRInt32 mNumOldProfiles; - char mOldProfiles[_MAX_NUM_PROFILES][_MAX_LENGTH]; - char mOldProfLocations[_MAX_NUM_PROFILES][_MAX_LENGTH]; - public: nsProfile(); virtual ~nsProfile(); @@ -64,7 +49,7 @@ public: nsresult RenameProfileDir(const char *newProfileName); // Creates associated user directories on the creation of a new profile - nsresult CreateUserDirectories(const nsFileSpec& profileDir); + nsresult CreateUserDirectories(const nsFileSpec& profileDir); // Deletes associated user directories nsresult DeleteUserDirectories(const nsFileSpec& profileDir); @@ -72,12 +57,7 @@ public: // Copies all the registry keys from old profile to new profile nsresult CopyRegKey(const char *oldProfile, const char *newProfile); - // Fills the global array mProfiles by enumerating registry entries - nsresult GetAllProfiles(); - - nsresult UpdateMozProfileRegistry(); - - // Fix the version differences by modifying the dir name entries - nsresult FixRegEntries(); + // Routine that frees the memory allocated to temp profile struct + void FreeProfileStruct(ProfileStruct* aProfile); };