diff --git a/mozilla/profile/src/nsProfile.cpp b/mozilla/profile/src/nsProfile.cpp index 0f0d88c7860..f8707974c31 100644 --- a/mozilla/profile/src/nsProfile.cpp +++ b/mozilla/profile/src/nsProfile.cpp @@ -75,49 +75,46 @@ #endif /* XP_UNIX */ // Activation cookie formats -#define NS_ACTIVATION_COOOKIE "NS_C5A_REG" -#define NS_ACTIVATION_USERNAME "NS_C5A_PN" -#define NS_ACTIVATION_USEREMAIL "NS_C5A_E" -#define NS_ACTIVATION_DENIAL "NS_C5A_DNY" +#define NS_ACTIVATION_COOOKIE "NS_C5A_REG" +#define NS_ACTIVATION_USERNAME "NS_C5A_PN" +#define NS_ACTIVATION_USEREMAIL "NS_C5A_E" +#define NS_ACTIVATION_DENIAL "NS_C5A_DNY" // JavaScript and Cookies prefs in the all.js -#define JAVASCRIPT_PREF "javascript.enabled" -#define COOKIES_PREF "network.accept_cookies" -#define NEVER_ACCEPT_COOKIES 2 +#define JAVASCRIPT_PREF "javascript.enabled" +#define COOKIES_PREF "network.accept_cookies" +#define NEVER_ACCEPT_COOKIES 2 // hack for copying panels.rdf into migrated profile dir -#define PANELS_RDF_FILE "panels.rdf" +#define PANELS_RDF_FILE "panels.rdf" // A default profile name, in case automigration 4x profile fails -#define DEFAULT_PROFILE_NAME "default" - -#define REGISTRY_YES_STRING "yes" -#define REGISTRY_NO_STRING "no" +#define DEFAULT_PROFILE_NAME "default" // Use PregUrlPref to extract the activation url -#define PREG_URL_PREF "browser.registration.url" -#define ACTIVATION_SERVER_URL "browser.registration.domain" -#define ACTIVATION_ACCEPT_DOMAIN "browser.registration.acceptdomain" -#define ACTIVATION_AIM_PREF "aim.session.screenname" -#define ACTIVATION_EMAIL_SERVER_NAME "browser.registration.mailservername" -#define ACTIVATION_EMAIL_SERVER_TYPE "browser.registration.mailservertype" +#define PREG_URL_PREF "browser.registration.url" +#define ACTIVATION_SERVER_URL "browser.registration.domain" +#define ACTIVATION_ACCEPT_DOMAIN "browser.registration.acceptdomain" +#define ACTIVATION_AIM_PREF "aim.session.screenname" +#define ACTIVATION_EMAIL_SERVER_NAME "browser.registration.mailservername" +#define ACTIVATION_EMAIL_SERVER_TYPE "browser.registration.mailservertype" -#define ACTIVATION_WINDOW_WIDTH 480 -#define ACTIVATION_WINDOW_HEIGHT 480 +#define ACTIVATION_WINDOW_WIDTH 480 +#define ACTIVATION_WINDOW_HEIGHT 480 -#define ACTIVATION_FRAME_URL "chrome://profile/content/activation.xul" -#define PROFILE_SELECTION_URL "chrome://profile/content/profileSelection.xul" +#define ACTIVATION_FRAME_URL "chrome://profile/content/activation.xul" +#define PROFILE_SELECTION_URL "chrome://profile/content/profileSelection.xul" #define PROFILE_SELECTION_CMD_LINE_ARG "-SelectProfile" -#define PROFILE_MANAGER_URL "chrome://profile/content/profileSelection.xul?manage=true" -#define PROFILE_MANAGER_CMD_LINE_ARG "-ProfileManager" -#define PROFILE_WIZARD_URL "chrome://profile/content/createProfileWizard.xul" -#define CONFIRM_AUTOMIGRATE_URL "chrome://profile/content/confirmMigration.xul" -#define PROFILE_WIZARD_CMD_LINE_ARG "-ProfileWizard" -#define INSTALLER_CMD_LINE_ARG "-installer" -#define CREATE_PROFILE_CMD_LINE_ARG "-CreateProfile" +#define PROFILE_MANAGER_URL "chrome://profile/content/profileSelection.xul?manage=true" +#define PROFILE_MANAGER_CMD_LINE_ARG "-ProfileManager" +#define PROFILE_WIZARD_URL "chrome://profile/content/createProfileWizard.xul" +#define CONFIRM_AUTOMIGRATE_URL "chrome://profile/content/confirmMigration.xul" +#define PROFILE_WIZARD_CMD_LINE_ARG "-ProfileWizard" +#define INSTALLER_CMD_LINE_ARG "-installer" +#define CREATE_PROFILE_CMD_LINE_ARG "-CreateProfile" #define PROFILE_CMD_LINE_ARG "-P" -#define PREF_CONFIRM_AUTOMIGRATION "profile.confirm_automigration" +#define PREF_CONFIRM_AUTOMIGRATION "profile.confirm_automigration" #if defined (XP_MAC) #define CHROME_STYLE nsIWebBrowserChrome::windowBordersOn | nsIWebBrowserChrome::windowCloseOn @@ -133,8 +130,8 @@ // 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; +static nsProfileAccess* gProfileDataAccess = nsnull; +static PRInt32 gDataAccessInstCount = 0; static PRBool mCurrentProfileAvailable = PR_FALSE; static PRBool sHaveRedundantDirectory = PR_FALSE; @@ -159,23 +156,24 @@ static NS_DEFINE_IID(kCookieServiceCID, NS_COOKIESERVICE_CID); static nsresult GetStringFromSpec(nsFileSpec inSpec, char **string) { - nsresult rv; - nsCOMPtr spec; - rv = NS_NewFileSpecWithSpec(inSpec, getter_AddRefs(spec)); - if (NS_SUCCEEDED(rv)) { + nsresult rv; + nsCOMPtr spec; + + rv = NS_NewFileSpecWithSpec(inSpec, getter_AddRefs(spec)); + if (NS_SUCCEEDED(rv)) { rv = spec->GetPersistentDescriptorString(string); - if (NS_SUCCEEDED(rv)) { - return NS_OK; + if (NS_SUCCEEDED(rv)) { + return NS_OK; } - else { + else { nsCRT::free(*string); - return rv; - } + return rv; + } } - else { - *string = nsnull; - return rv; - } + else { + *string = nsnull; + return rv; + } } /* @@ -183,28 +181,29 @@ nsresult GetStringFromSpec(nsFileSpec inSpec, char **string) */ nsProfile::nsProfile() { - mAutomigrate = PR_FALSE; + mAutomigrate = PR_FALSE; - if(!gProfileDataAccess) - gProfileDataAccess = new nsProfileAccess(); + if(!gProfileDataAccess) + gProfileDataAccess = new nsProfileAccess(); - gDataAccessInstCount++; + gDataAccessInstCount++; - NS_INIT_REFCNT(); + NS_INIT_REFCNT(); } nsProfile::~nsProfile() { #if defined(DEBUG_profile) - printf("~nsProfile \n"); + printf("~nsProfile \n"); #endif CleanUp(); - gProfileDataAccess->UpdateRegistry(); - - gDataAccessInstCount--; - if (gDataAccessInstCount == 0) - delete gProfileDataAccess; + gProfileDataAccess->mProfileDataChanged = PR_TRUE; + gProfileDataAccess->UpdateRegistry(); + + gDataAccessInstCount--; + if (gDataAccessInstCount == 0) + delete gProfileDataAccess; } /* @@ -215,59 +214,63 @@ NS_IMPL_ADDREF(nsProfile) NS_IMPL_RELEASE(nsProfile) NS_INTERFACE_MAP_BEGIN(nsProfile) - NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIProfile) - NS_INTERFACE_MAP_ENTRY(nsIProfile) - NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor) - NS_INTERFACE_MAP_ENTRY(nsIURIContentListener) + NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIProfile) + NS_INTERFACE_MAP_ENTRY(nsIProfile) + NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor) + NS_INTERFACE_MAP_ENTRY(nsIURIContentListener) NS_INTERFACE_MAP_END /* * nsIProfile Implementation */ -NS_IMETHODIMP nsProfile::Startup(const char *filename) +NS_IMETHODIMP nsProfile::Startup(const PRUnichar *filename) { - return NS_OK; + return NS_OK; } NS_IMETHODIMP nsProfile::GetAutomigrate(PRBool *aAutomigrate) { - if (!aAutomigrate) return NS_ERROR_NULL_POINTER; - *aAutomigrate = mAutomigrate; - return NS_OK; + NS_ENSURE_ARG_POINTER(aAutomigrate); + + *aAutomigrate = mAutomigrate; + return NS_OK; } + NS_IMETHODIMP nsProfile::SetAutomigrate(PRBool aAutomigrate) { - mAutomigrate = aAutomigrate; - return NS_OK; + mAutomigrate = aAutomigrate; + return NS_OK; } NS_IMETHODIMP nsProfile::StartupWithArgs(nsICmdLineService *cmdLineArgs) { - nsresult rv; - // initializations for profile manager - PRBool profileDirSet = PR_FALSE; - nsCString profileURLStr(""); + 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"); + printf("Profile Manager : Profile Wizard and Manager activites : Begin\n"); #endif - if (cmdLineArgs) - rv = ProcessArgs(cmdLineArgs, &profileDirSet, profileURLStr); + if (cmdLineArgs) + rv = ProcessArgs(cmdLineArgs, &profileDirSet, profileURLStr); - if (!profileDirSet) { - rv = LoadDefaultProfileDir(profileURLStr); - if (NS_FAILED(rv)) return rv; - } + if (!profileDirSet) { + rv = LoadDefaultProfileDir(profileURLStr); + + if (NS_FAILED(rv)) return rv; + } #ifdef DEBUG_profile - printf("Profile Manager : Profile Wizard and Manager activites : End\n"); + printf("Profile Manager : Profile Wizard and Manager activites : End\n"); #endif - return NS_OK; + return NS_OK; } @@ -282,11 +285,11 @@ nsProfile::LoadDefaultProfileDir(nsCString & profileURLStr) if (NS_FAILED(rv)) return rv; GetProfileCount(&numProfiles); - /* - * Create the Application Shell instance... - */ + /* + * Create the Application Shell instance... + */ NS_WITH_SERVICE(nsIAppShellService, profAppShell, - kAppShellServiceCID, &rv); + kAppShellServiceCID, &rv); if (NS_FAILED(rv)) return rv; @@ -301,17 +304,17 @@ nsProfile::LoadDefaultProfileDir(nsCString & profileURLStr) if (numProfiles == 0) { if (pregEnabled) - { - rv = CreateDefaultProfile(); - if (NS_FAILED(rv)) return rv; + { + rv = CreateDefaultProfile(); + if (NS_FAILED(rv)) return rv; - GetProfileCount(&numProfiles); - profileURLStr = ""; + GetProfileCount(&numProfiles); + profileURLStr = ""; mCurrentProfileAvailable = PR_TRUE; // Need to load new profile prefs. rv = LoadNewProfilePrefs(); - } + } else profileURLStr = PROFILE_WIZARD_URL; } @@ -330,54 +333,55 @@ nsProfile::LoadDefaultProfileDir(nsCString & profileURLStr) nsCOMPtr profWindow; rv = profAppShell->CreateTopLevelWindow(nsnull, profileURL, PR_TRUE, PR_TRUE, CHROME_STYLE, - NS_SIZETOCONTENT, // width - NS_SIZETOCONTENT, // height + NS_SIZETOCONTENT, // width + NS_SIZETOCONTENT, // height getter_AddRefs(profWindow)); if (NS_FAILED(rv)) return rv; /* * Start up the main event loop... - */ + */ rv = profAppShell->Run(); } PRBool confirmAutomigration = PR_FALSE; if (NS_SUCCEEDED(rv) && prefs) { - rv = prefs->GetBoolPref(PREF_CONFIRM_AUTOMIGRATION, &confirmAutomigration); - if (NS_FAILED(rv)) confirmAutomigration = PR_FALSE; + rv = prefs->GetBoolPref(PREF_CONFIRM_AUTOMIGRATION, &confirmAutomigration); + if (NS_FAILED(rv)) confirmAutomigration = PR_FALSE; } - + if (confirmAutomigration) { - if (profileURLStr == (const char *)(CONFIRM_AUTOMIGRATE_URL)) { - PRBool automigrate = PR_FALSE; - rv = GetAutomigrate(&automigrate); - if (NS_SUCCEEDED(rv) && automigrate) { - AutoMigrate(); - } - else { - // the user hit cancel. - // so they don't want to automatically migrate - // so call this again with the profile manager ui - nsCString profileManagerUrl(PROFILE_MANAGER_URL); - rv = LoadDefaultProfileDir(profileManagerUrl); - return rv; - } - } + if (profileURLStr == (const char*)CONFIRM_AUTOMIGRATE_URL) { + PRBool automigrate = PR_FALSE; + rv = GetAutomigrate(&automigrate); + if (NS_SUCCEEDED(rv) && automigrate) { + AutoMigrate(); + } + else { + // the user hit cancel. + // so they don't want to automatically migrate + // so call this again with the profile manager ui + nsCString profileManagerUrl(PROFILE_MANAGER_URL); + rv = LoadDefaultProfileDir(profileManagerUrl); + return rv; + } + } } - // if we get here, and we don't have a current profile, - // return a failure so we will exit - // this can happen, if the user hits Exit in the profile manager dialog - nsXPIDLCString currentProfileStr; - rv = GetCurrentProfile(getter_Copies(currentProfileStr)); - if (NS_FAILED(rv) || !((const char *)currentProfileStr) || (PL_strlen((const char *)currentProfileStr) == 0)) { - return NS_ERROR_FAILURE; - } + // if we get here, and we don't have a current profile, + // return a failure so we will exit + // this can happen, if the user hits Exit in the profile manager dialog + nsXPIDLString currentProfileStr; + rv = GetCurrentProfile(getter_Copies(currentProfileStr)); + + if (NS_FAILED(rv) || (*(const PRUnichar*)currentProfileStr == 0)) { + return NS_ERROR_FAILURE; + } mCurrentProfileAvailable = PR_TRUE; if (pregEnabled) - TriggerActivation((const char *)currentProfileStr); + TriggerActivation(currentProfileStr); // Now we have the right profile, read the user-specific prefs. rv = prefs->ReadUserPrefs(); @@ -389,28 +393,30 @@ nsProfile::LoadDefaultProfileDir(nsCString & profileURLStr) if (!pPrefConverter) return NS_ERROR_FAILURE; rv = pPrefConverter->ConvertPrefsToUTF8IfNecessary(); - return rv; + return rv; } nsresult nsProfile::AutoMigrate() { - nsresult rv = NS_OK; - // automatically migrate the one 4.x profile - rv = MigrateAllProfiles(); - if (NS_FAILED(rv)) - { + nsresult rv = NS_OK; + // automatically migrate the one 4.x profile + rv = MigrateAllProfiles(); + + if (NS_FAILED(rv)) + { #ifdef DEBUG_profile - printf("AutoMigration failed. Let's create a default 5.0 profile.\n"); + printf("AutoMigration failed. Let's create a default 5.0 profile.\n"); #endif - rv = CreateDefaultProfile(); - if (NS_FAILED(rv)) return rv; - } + rv = CreateDefaultProfile(); + if (NS_FAILED(rv)) return rv; + } - gProfileDataAccess->UpdateRegistry(); + gProfileDataAccess->mProfileDataChanged = PR_TRUE; + gProfileDataAccess->UpdateRegistry(); - return rv; + return rv; } nsresult @@ -418,16 +424,19 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs, PRBool* profileDirSet, nsCString & profileURLStr) { + NS_ASSERTION(cmdLineArgs, "Invalid cmdLineArgs"); + NS_ASSERTION(profileDirSet, "Invalid profileDirSet"); + nsresult rv; - char* cmdResult = nsnull; - nsFileSpec currProfileDirSpec; + char* cmdResult = nsnull; + nsFileSpec currProfileDirSpec; #ifdef DEBUG_profile printf("Profile Manager : Command Line Options : Begin\n"); #endif // check for command line arguments for profile manager - // + // // -P command line option works this way: // apprunner -P profilename // runs the app using the profile @@ -436,13 +445,14 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs, if (NS_SUCCEEDED(rv)) { if (cmdResult) { - char* currProfileName = cmdResult; + nsAutoString currProfileName(cmdResult); + #ifdef DEBUG_profile printf("ProfileName : %s\n", cmdResult); #endif /* DEBUG_profile */ PRBool exists; - rv = ProfileExists(currProfileName, &exists); - if (NS_FAILED(rv)) return rv; + rv = ProfileExists(currProfileName.GetUnicode(), &exists); + if (NS_FAILED(rv)) return rv; if (!exists) { PRInt32 num5xProfiles = 0; @@ -453,19 +463,19 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs, if (num5xProfiles == 0 && num4xProfiles == 0) { profileURLStr = PROFILE_WIZARD_URL; - } - else if (num5xProfiles > 0) { + } + else if (num5xProfiles > 0) { profileURLStr = PROFILE_SELECTION_URL; - } - else if (num4xProfiles > 0) { + } + else if (num4xProfiles > 0) { profileURLStr = PROFILE_MANAGER_URL; - } + } *profileDirSet = PR_FALSE; - } - else { - rv = GetProfileDir(currProfileName, &currProfileDirSpec); + } + else { + rv = GetProfileDir(currProfileName.GetUnicode(), &currProfileDirSpec); if (NS_SUCCEEDED(rv)){ - *profileDirSet = PR_TRUE; + *profileDirSet = PR_TRUE; mCurrentProfileAvailable = PR_TRUE; // Need to load new profile prefs. @@ -475,36 +485,39 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs, } } - // -CreateProfile command line option works this way: - // apprunner -CreateProfile profilename - // creates a new profile named and sets the directory to your CWD - // runs app using that profile - // remembers profile for next time - // - OR - - // apprunner -CreateProfile "profilename profiledir" - // creates a new profile named and sets the directory to - // runs app using that profile - // remembers profile for next time + // -CreateProfile command line option works this way: + // apprunner -CreateProfile profilename + // creates a new profile named and sets the directory to your CWD + // runs app using that profile + // remembers profile for next time + // - OR - + // apprunner -CreateProfile "profilename profiledir" + // creates a new profile named and sets the directory to + // runs app using that profile + // remembers profile for next time 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) { + nsAutoString currProfileName(strtok(cmdResult, " ")); + nsAutoString currProfileDirString(strtok(NULL, " ")); + + if (!currProfileDirString.IsEmpty()) { 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; - + 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)); + rv = locator->GetFileLocation( + nsSpecialFileSpec::App_DefaultUserProfileRoot50, + getter_AddRefs(spec)); + if (NS_FAILED(rv) || !spec) return NS_ERROR_FAILURE; spec->GetFileSpec(&currProfileDirSpec); @@ -514,22 +527,25 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs, #ifdef DEBUG_profile printf("profileName & profileDir are: %s\n", cmdResult); #endif /* DEBUG_profile */ - rv = CreateNewProfile(currProfileName, currProfileDirSpec.GetNativePathCString()); - if (NS_SUCCEEDED(rv)) { - *profileDirSet = PR_TRUE; + + nsAutoString currProfileDir(currProfileDirSpec.GetNativePathCString()); + rv = CreateNewProfile(currProfileName.GetUnicode(), currProfileDir.GetUnicode()); + if (NS_SUCCEEDED(rv)) { + *profileDirSet = PR_TRUE; mCurrentProfileAvailable = PR_TRUE; - // Need to load new profile prefs. - rv = LoadNewProfilePrefs(); - gProfileDataAccess->UpdateRegistry(); - } + // Need to load new profile prefs. + rv = LoadNewProfilePrefs(); + gProfileDataAccess->mProfileDataChanged = PR_TRUE; + gProfileDataAccess->UpdateRegistry(); + } } } // Start Profile Manager rv = cmdLineArgs->GetCmdLineValue(PROFILE_MANAGER_CMD_LINE_ARG, &cmdResult); if (NS_SUCCEEDED(rv)) - { + { if (cmdResult) { profileURLStr = PROFILE_MANAGER_URL; } @@ -538,7 +554,7 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs, // Start Profile Selection rv = cmdLineArgs->GetCmdLineValue(PROFILE_SELECTION_CMD_LINE_ARG, &cmdResult); if (NS_SUCCEEDED(rv)) - { + { if (cmdResult) { profileURLStr = PROFILE_SELECTION_URL; } @@ -548,18 +564,17 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs, // Start Profile Wizard rv = cmdLineArgs->GetCmdLineValue(PROFILE_WIZARD_CMD_LINE_ARG, &cmdResult); if (NS_SUCCEEDED(rv)) - { + { if (cmdResult) { profileURLStr = PROFILE_WIZARD_URL; } } - // Start Migaration activity + // 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; @@ -567,36 +582,37 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs, rv = Get4xProfileCount(&num4xProfiles); if (NS_FAILED(rv)) return rv; - PRInt32 numProfiles = 0; - GetProfileCount(&numProfiles); + PRInt32 numProfiles = 0; + GetProfileCount(&numProfiles); if (num4xProfiles == 0 && numProfiles == 0) { - // Let us create a default 5.0 profile - CreateDefaultProfile(); - if (NS_FAILED(rv)) return rv; + // Let us create a default 5.0 profile + CreateDefaultProfile(); + if (NS_FAILED(rv)) return rv; + } + else if (num4xProfiles == 0 && numProfiles == 1) { + profileURLStr = ""; } - else if (num4xProfiles == 0 && numProfiles == 1) { - profileURLStr = ""; - } else if (num4xProfiles == 1 && numProfiles == 0) { - PRBool confirmAutomigration = PR_FALSE; - NS_WITH_SERVICE(nsIPref, prefs, kPrefCID, &rv) - if (NS_SUCCEEDED(rv) && prefs) { - rv = prefs->GetBoolPref(PREF_CONFIRM_AUTOMIGRATION, &confirmAutomigration); - if (NS_FAILED(rv)) confirmAutomigration = PR_FALSE; - } - if (confirmAutomigration) { - profileURLStr = CONFIRM_AUTOMIGRATE_URL; - } - else { - AutoMigrate(); - } - } - else if (numProfiles > 1) - { - profileURLStr = PROFILE_SELECTION_URL; - } + PRBool confirmAutomigration = PR_FALSE; + NS_WITH_SERVICE(nsIPref, prefs, kPrefCID, &rv) + if (NS_SUCCEEDED(rv) && prefs) { + rv = prefs->GetBoolPref(PREF_CONFIRM_AUTOMIGRATION, + &confirmAutomigration); + if (NS_FAILED(rv)) confirmAutomigration = PR_FALSE; + } + if (confirmAutomigration) { + profileURLStr = CONFIRM_AUTOMIGRATE_URL; + } + else { + AutoMigrate(); + } + } + else if (numProfiles > 1) + { + profileURLStr = PROFILE_SELECTION_URL; + } else { - // show the profile manager + // show the profile manager profileURLStr = PROFILE_MANAGER_URL; } } @@ -626,17 +642,21 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs, // Gets the profiles directory for a given profile // Sets the given profile to be a current profile -NS_IMETHODIMP nsProfile::GetProfileDir(const char *profileName, nsFileSpec* profileDir) +NS_IMETHODIMP nsProfile::GetProfileDir(const PRUnichar *profileName, nsFileSpec* profileDir) { + NS_ENSURE_ARG_POINTER(profileName); + NS_ENSURE_ARG_POINTER(profileDir); + nsresult rv = NS_OK; #if defined(DEBUG_profile) printf("ProfileManager : GetProfileDir\n"); #endif - ProfileStruct *aProfile; + ProfileStruct *aProfile; - gProfileDataAccess->GetValue(profileName, &aProfile); + rv = gProfileDataAccess->GetValue(profileName, &aProfile); + if (NS_FAILED(rv)) return rv; if (aProfile == nsnull) return NS_ERROR_FAILURE; @@ -644,56 +664,66 @@ NS_IMETHODIMP nsProfile::GetProfileDir(const char *profileName, nsFileSpec* prof nsCOMPtrspec; rv = NS_NewFileSpec(getter_AddRefs(spec)); if (NS_FAILED(rv)) return rv; - rv = spec->SetPersistentDescriptorString(aProfile->profileLocation); + + nsCAutoString profileLocation(aProfile->profileLocation); + rv = spec->SetPersistentDescriptorString(profileLocation.GetBuffer()); 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); + if (aProfile->isMigrated == REGISTRY_YES_STRING) + { + gProfileDataAccess->SetCurrentProfile(profileName); nsFileSpec tmpFileSpec(*profileDir); if (!tmpFileSpec.Exists()) - { - // Get profile defaults folder.. - NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); - + { + // Get profile defaults folder.. + NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); + if (NS_FAILED(rv) || !locator) - return NS_ERROR_FAILURE; + return NS_ERROR_FAILURE; nsCOMPtr profDefaultsDir; - rv = locator->GetFileLocation(nsSpecialFileSpec::App_ProfileDefaultsFolder50, getter_AddRefs(profDefaultsDir)); + rv = locator->GetFileLocation( + nsSpecialFileSpec::App_ProfileDefaultsFolder50, + getter_AddRefs(profDefaultsDir)); if (NS_FAILED(rv) || !profDefaultsDir) - return NS_ERROR_FAILURE; + return NS_ERROR_FAILURE; - nsFileSpec defaultsDirSpec; + 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); + // 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. + // Copy contents from defaults folder. if (defaultsDirSpec.Exists()) - defaultsDirSpec.RecursiveCopy(tmpFileSpec); - } - } + defaultsDirSpec.RecursiveCopy(tmpFileSpec); + } + } - FreeProfileStruct(aProfile); + delete aProfile; return rv; } NS_IMETHODIMP nsProfile::GetDefaultProfileParentDir(nsIFileSpec **aDefaultProfileDir) { + NS_ENSURE_ARG_POINTER(aDefaultProfileDir); + nsresult rv; NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); if (NS_FAILED(rv) || !locator) return NS_ERROR_FAILURE; - rv = locator->GetFileLocation(nsSpecialFileSpec::App_DefaultUserProfileRoot50, aDefaultProfileDir); - if (NS_FAILED(rv) || !aDefaultProfileDir || !*aDefaultProfileDir) return NS_ERROR_FAILURE; + rv = locator->GetFileLocation( + nsSpecialFileSpec::App_DefaultUserProfileRoot50, + aDefaultProfileDir); + if (NS_FAILED(rv) || !aDefaultProfileDir || !*aDefaultProfileDir) + return NS_ERROR_FAILURE; return NS_OK; } @@ -702,20 +732,24 @@ NS_IMETHODIMP nsProfile::GetDefaultProfileParentDir(nsIFileSpec **aDefaultProfil // Location: Common/Profiles NS_IMETHODIMP nsProfile::GetProfileCount(PRInt32 *numProfiles) { + NS_ENSURE_ARG_POINTER(numProfiles); + *numProfiles = 0; - gProfileDataAccess->GetNumProfiles(numProfiles); - return NS_OK; + gProfileDataAccess->GetNumProfiles(numProfiles); + return NS_OK; } // If only a single profile exists // return the name of the single profile. // Otherwise it return the name of the first valid profile. -NS_IMETHODIMP nsProfile::GetFirstProfile(char **profileName) +NS_IMETHODIMP nsProfile::GetFirstProfile(PRUnichar **profileName) { - gProfileDataAccess->GetFirstProfile(profileName); - gProfileDataAccess->SetCurrentProfile(*profileName); + NS_ENSURE_ARG_POINTER(profileName); + + gProfileDataAccess->GetFirstProfile(profileName); + gProfileDataAccess->SetCurrentProfile(*profileName); return NS_OK; } @@ -723,25 +757,28 @@ NS_IMETHODIMP nsProfile::GetFirstProfile(char **profileName) // Returns the name of the current profile i.e., the last used profile NS_IMETHODIMP -nsProfile::GetCurrentProfile(char **profileName) +nsProfile::GetCurrentProfile(PRUnichar **profileName) { - gProfileDataAccess->GetCurrentProfile(profileName); + NS_ENSURE_ARG_POINTER(profileName); + + gProfileDataAccess->GetCurrentProfile(profileName); return NS_OK; } // Returns the name of the current profile directory NS_IMETHODIMP nsProfile::GetCurrentProfileDir(nsFileSpec* profileDir) { - nsresult rv = NS_OK; + NS_ENSURE_ARG_POINTER(profileDir); - char *profileName = nsnull; + nsresult rv = NS_OK; - GetCurrentProfile(&profileName); + nsXPIDLString profileName; - rv = GetProfileDir(profileName, profileDir); - if (NS_FAILED(rv)) return rv; + GetCurrentProfile(getter_Copies(profileName)); + + rv = GetProfileDir(profileName, profileDir); + if (NS_FAILED(rv)) return rv; - PR_FREEIF(profileName); return rv; } @@ -751,141 +788,149 @@ NS_IMETHODIMP nsProfile::GetCurrentProfileDir(nsFileSpec* profileDir) */ // Sets the current profile directory -NS_IMETHODIMP nsProfile::SetProfileDir(const char *profileName, nsFileSpec& profileDir) +NS_IMETHODIMP nsProfile::SetProfileDir(const PRUnichar *profileName, nsFileSpec& profileDir) { + NS_ENSURE_ARG_POINTER(profileName); + nsresult rv = NS_OK; - // 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()) + if (!profileDir.Exists()) { - // nsPersistentFileDescriptor requires an existing + // 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)); - } - if (NS_FAILED(rv)) return rv; + 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; + // Do I need to check for NS_ERROR_OUT_OF_MEMORY when I do a new on a class? + ProfileStruct* aProfile = new ProfileStruct(); - aProfile = (ProfileStruct *) PR_Malloc(sizeof(ProfileStruct)); - if (!aProfile) - return NS_ERROR_OUT_OF_MEMORY; + nsAutoString profileLocation(profileDirString); + + + aProfile->profileName = profileName; + aProfile->profileLocation = profileLocation; + aProfile->isMigrated = REGISTRY_YES_STRING; - aProfile->profileName = nsCRT::strdup(profileName); - aProfile->profileLocation = nsCRT::strdup(profileDirString); - aProfile->isMigrated = nsCRT::strdup(REGISTRY_YES_STRING); - aProfile->NCProfileName = nsnull; - aProfile->NCDeniedService = nsnull; - aProfile->NCEmailAddress = nsnull; - aProfile->NCHavePregInfo = nsnull; rv = CreateUserDirectories(tmpDir); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) { + delete aProfile; + return rv; + } gProfileDataAccess->SetValue(aProfile); - if (NS_FAILED(rv)) return rv; gProfileDataAccess->SetCurrentProfile(profileName); - if (NS_FAILED(rv)) return rv; + delete aProfile; + return rv; } // Creates a new profile -NS_IMETHODIMP nsProfile::CreateNewProfile(const char* profileName, const char* nativeProfileDir) +NS_IMETHODIMP +nsProfile::CreateNewProfile(const PRUnichar* profileName, const PRUnichar* nativeProfileDir) { - nsresult rv = NS_OK; + NS_ENSURE_ARG_POINTER(profileName); + + nsresult rv = NS_OK; #if defined(DEBUG_profile) printf("ProfileManager : CreateNewProfile\n"); - printf("Profile Name: %s\n", profileName); - printf("Profile Dir: %s\n", (!nativeProfileDir || !*nativeProfileDir)?"(use default)":nativeProfileDir); + printf("Profile Name: %s\n", (const char*)nsCAutoString(profileName)); + printf("Profile Dir: %s\n", (const char*)nsCAutoString(nativeProfileDir)); #endif NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); - if (NS_FAILED(rv) || !locator) - return NS_ERROR_FAILURE; + if (NS_FAILED(rv) || !locator) + return NS_ERROR_FAILURE; if (!profileName) return NS_ERROR_FAILURE; nsFileSpec dirSpec; - if (!nativeProfileDir || !*nativeProfileDir) + if (!nativeProfileDir || !*nativeProfileDir) { - // They didn't specify a directory path... - nsCOMPtr defaultRoot; - rv = locator->GetFileLocation(nsSpecialFileSpec::App_DefaultUserProfileRoot50, getter_AddRefs(defaultRoot)); + // They didn't specify a directory path... + nsCOMPtr defaultRoot; + rv = locator->GetFileLocation( + nsSpecialFileSpec::App_DefaultUserProfileRoot50, + getter_AddRefs(defaultRoot)); - if (NS_FAILED(rv) || !defaultRoot) - return NS_ERROR_FAILURE; + if (NS_FAILED(rv) || !defaultRoot) + return NS_ERROR_FAILURE; - defaultRoot->GetFileSpec(&dirSpec); + defaultRoot->GetFileSpec(&dirSpec); if (!dirSpec.Exists()) dirSpec.CreateDirectory(); // 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 + // 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"); + printf("before SetProfileDir\n"); #endif - if (!dirSpec.Exists()) - dirSpec.CreateDirectory(); + if (!dirSpec.Exists()) + dirSpec.CreateDirectory(); - // Set the directory value and add the entry to the registry tree. - // Creates required user directories. + // Set the directory value and add the entry to the registry tree. + // Creates required user directories. rv = SetProfileDir(profileName, dirSpec); #if defined(DEBUG_profile) - printf("after SetProfileDir\n"); + ("after SetProfileDir\n"); #endif // Get profile defaults folder.. nsCOMPtr profDefaultsDir; - rv = locator->GetFileLocation(nsSpecialFileSpec::App_ProfileDefaultsFolder50, getter_AddRefs(profDefaultsDir)); + rv = locator->GetFileLocation( + nsSpecialFileSpec::App_ProfileDefaultsFolder50, + getter_AddRefs(profDefaultsDir)); - if (NS_FAILED(rv) || !profDefaultsDir) - { - return NS_ERROR_FAILURE; - } + if (NS_FAILED(rv) || !profDefaultsDir) + { + return NS_ERROR_FAILURE; + } - nsFileSpec defaultsDirSpec; + nsFileSpec defaultsDirSpec; - profDefaultsDir->GetFileSpec(&defaultsDirSpec); + profDefaultsDir->GetFileSpec(&defaultsDirSpec); - // Copy contents from defaults folder. - if (defaultsDirSpec.Exists()) - { - defaultsDirSpec.RecursiveCopy(dirSpec); - } + // Copy contents from defaults folder. + if (defaultsDirSpec.Exists()) + { + defaultsDirSpec.RecursiveCopy(dirSpec); + } - gProfileDataAccess->mNumProfiles++; - gProfileDataAccess->mProfileDataChanged = PR_TRUE; - gProfileDataAccess->UpdateRegistry(); + gProfileDataAccess->mNumProfiles++; + gProfileDataAccess->mProfileDataChanged = PR_TRUE; + gProfileDataAccess->UpdateRegistry(); return NS_OK; } @@ -893,37 +938,37 @@ NS_IMETHODIMP nsProfile::CreateNewProfile(const char* profileName, const char* n // Create required user directories like ImapMail, Mail, News, Cache etc. nsresult nsProfile::CreateUserDirectories(const nsFileSpec& profileDir) { - nsresult rv = NS_OK; + nsresult rv = NS_OK; #if defined(DEBUG_profile) printf("ProfileManager : CreateUserDirectories\n"); #endif - nsFileSpec tmpDir; + nsFileSpec tmpDir; - tmpDir = profileDir; + tmpDir = profileDir; tmpDir += "News"; if (!tmpDir.Exists()) - tmpDir.CreateDirectory(); + tmpDir.CreateDirectory(); tmpDir = profileDir; tmpDir += "ImapMail"; if (!tmpDir.Exists()) - tmpDir.CreateDirectory(); + tmpDir.CreateDirectory(); - tmpDir = profileDir; + tmpDir = profileDir; tmpDir += "Mail"; if (!tmpDir.Exists()) - tmpDir.CreateDirectory(); + tmpDir.CreateDirectory(); - tmpDir = profileDir; + tmpDir = profileDir; tmpDir += "Cache"; if (!tmpDir.Exists()) - tmpDir.CreateDirectory(); + tmpDir.CreateDirectory(); return rv; } @@ -933,92 +978,88 @@ nsresult nsProfile::CreateUserDirectories(const nsFileSpec& profileDir) // A FileSpec of the profile's directory is taken as input param nsresult nsProfile::DeleteUserDirectories(const nsFileSpec& profileDir) { - nsresult rv = NS_OK; + nsresult rv = NS_OK; #if defined(DEBUG_profile) printf("ProfileManager : DeleteUserDirectories\n"); #endif - if (profileDir.Exists()) - profileDir.Delete(PR_TRUE); - - return rv; + if (profileDir.Exists()) + profileDir.Delete(PR_TRUE); + + return rv; } // Rename a old profile to new profile. // Copies all the keys from old profile to new profile. // Deletes the old profile from the registry -NS_IMETHODIMP nsProfile::RenameProfile(const char* oldName, const char* newName) +NS_IMETHODIMP +nsProfile::RenameProfile(const PRUnichar* oldName, const PRUnichar* newName) { - nsresult rv = NS_OK; + NS_ENSURE_ARG_POINTER(oldName); + NS_ENSURE_ARG_POINTER(newName); + + nsresult rv = NS_OK; #if defined(DEBUG_profile) - printf("ProfileManager : Renaming profile %s to %s \n", oldName, newName); + printf("ProfileManager : Renaming profile\n"); + printf("Old name: %s\n", (const char*)nsCAutoString(oldName)); + printf("New name: %s\n", (const char*)nsCAutoString(newName)); #endif - PRBool exists; - rv = ProfileExists(newName, &exists); - if (NS_FAILED(rv)) return rv; + PRBool exists; + rv = ProfileExists(newName, &exists); + if (NS_FAILED(rv)) return rv; - // That profile already exists... - if (exists) { + // That profile already exists... + if (exists) { #if defined(DEBUG_profile) - printf("ProfileManager : Rename Operation failed : Profile exists. Provide a different new name for profile.\n"); + printf("ProfileManager : Rename Operation failed : Profile exists. Provide a different new name for profile.\n"); #endif - return NS_ERROR_FAILURE; - } + return NS_ERROR_FAILURE; + } - // Copy reg keys - rv = CopyRegKey(oldName, newName); - if (NS_FAILED(rv)) return rv; - - 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; - } + // Copy reg keys + rv = CopyRegKey(oldName, newName); + if (NS_FAILED(rv)) return rv; + + // Delete old profile entry + 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++; - // Delete old profile entry - 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; - rv = ForgetCurrentProfile(); - if (NS_FAILED(rv)) return rv; + gProfileDataAccess->mProfileDataChanged = PR_TRUE; + gProfileDataAccess->UpdateRegistry(); - gProfileDataAccess->mProfileDataChanged = PR_TRUE; - gProfileDataAccess->UpdateRegistry(); - - return NS_OK; + return NS_OK; } // Copy old profile entries to the new profile // In the process creates new profile subtree. -nsresult nsProfile::CopyRegKey(const char *oldProfile, const char *newProfile) +nsresult nsProfile::CopyRegKey(const PRUnichar *oldProfile, const PRUnichar *newProfile) { - nsresult rv = NS_OK; + NS_ENSURE_ARG_POINTER(oldProfile); + NS_ENSURE_ARG_POINTER(newProfile); - ProfileStruct *aProfile; + nsresult rv = NS_OK; - gProfileDataAccess->GetValue(oldProfile, &aProfile); + ProfileStruct *aProfile; - if (aProfile == nsnull) - return NS_ERROR_FAILURE; + rv = gProfileDataAccess->GetValue(oldProfile, &aProfile); + if (NS_FAILED(rv)) return rv; - aProfile->profileName = nsCRT::strdup(newProfile); - aProfile->NCProfileName = nsnull; - aProfile->NCDeniedService = nsnull; - aProfile->NCEmailAddress = nsnull; - aProfile->NCHavePregInfo = nsnull; + aProfile->profileName = newProfile; - rv = gProfileDataAccess->SetValue(aProfile); + rv = gProfileDataAccess->SetValue(aProfile); - FreeProfileStruct(aProfile); - return rv; + delete aProfile; + + return rv; } NS_IMETHODIMP nsProfile::ForgetCurrentProfile() @@ -1026,12 +1067,14 @@ NS_IMETHODIMP nsProfile::ForgetCurrentProfile() nsresult rv = NS_OK; // Remove the current profile subtree from the registry. - gProfileDataAccess->SetCurrentProfile(""); + PRUnichar tmp[] = { '\0' }; + + gProfileDataAccess->SetCurrentProfile(tmp); if (NS_FAILED(rv)) return rv; - gProfileDataAccess->mForgetProfileCalled = PR_TRUE; + gProfileDataAccess->mForgetProfileCalled = PR_TRUE; - NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); + NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); if (NS_FAILED(rv)) return rv; if (!locator) return NS_ERROR_FAILURE; @@ -1045,8 +1088,10 @@ NS_IMETHODIMP nsProfile::ForgetCurrentProfile() // Delete a profile from the registry // Not deleting the directories on the harddisk yet. // 4.x kind of confirmation need to be implemented yet -NS_IMETHODIMP nsProfile::DeleteProfile(const char* profileName, PRBool canDeleteFiles) +NS_IMETHODIMP nsProfile::DeleteProfile(const PRUnichar* profileName, PRBool canDeleteFiles) { + NS_ENSURE_ARG_POINTER(profileName); + nsresult rv = NS_OK; rv = ForgetCurrentProfile(); @@ -1064,11 +1109,11 @@ NS_IMETHODIMP nsProfile::DeleteProfile(const char* profileName, PRBool canDelete } // Remove the subtree from the registry. - gProfileDataAccess->RemoveSubTree(profileName); + gProfileDataAccess->RemoveSubTree(profileName); if (NS_FAILED(rv)) return rv; - gProfileDataAccess->mProfileDataChanged = PR_TRUE; - gProfileDataAccess->UpdateRegistry(); + gProfileDataAccess->mProfileDataChanged = PR_TRUE; + gProfileDataAccess->UpdateRegistry(); return rv; } @@ -1077,9 +1122,11 @@ NS_IMETHODIMP nsProfile::DeleteProfile(const char* profileName, PRBool canDelete // Populate the input param. // This method is written to support the core service // call to get the names all profiles. -NS_IMETHODIMP nsProfile::GetProfileList(char **profileListStr) +NS_IMETHODIMP nsProfile::GetProfileList(PRUnichar **profileListStr) { - gProfileDataAccess->GetProfileList(profileListStr); + NS_ENSURE_ARG_POINTER(profileListStr); + + gProfileDataAccess->GetProfileList(profileListStr); return NS_OK; } @@ -1088,16 +1135,18 @@ NS_IMETHODIMP nsProfile::GetProfileList(char **profileListStr) // Prefs and FileLocation services are used here. // FileLocation service to make ir forget about the global profile dir it had. // Prefs service to kick off the startup to start the app with new profile's prefs. -NS_IMETHODIMP nsProfile::StartApprunner(const char* profileName) +NS_IMETHODIMP nsProfile::StartApprunner(const PRUnichar* profileName) { + NS_ENSURE_ARG_POINTER(profileName); nsresult rv = NS_OK; #if defined(DEBUG_profile) printf("ProfileManager : StartApprunner\n"); + printf("profileName passed in: %s", (const char*)nsCAutoString(profileName)); #endif - gProfileDataAccess->SetCurrentProfile(profileName); + gProfileDataAccess->SetCurrentProfile(profileName); mCurrentProfileAvailable = PR_TRUE; NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); @@ -1108,21 +1157,21 @@ NS_IMETHODIMP nsProfile::StartApprunner(const char* profileName) rv = locator->ForgetProfileDir(); if (NS_FAILED(rv)) { #ifdef DEBUG_profile - printf("failed to forget the profile dir\n"); + printf("failed to forget the profile dir\n"); #endif /* DEBUG_profile */ - return rv; + return rv; } - // Need to load new profile prefs. - rv = LoadNewProfilePrefs(); - return rv; + // Need to load new profile prefs. + rv = LoadNewProfilePrefs(); + return rv; } NS_IMETHODIMP nsProfile::LoadNewProfilePrefs() { - nsresult rv; + nsresult rv; NS_WITH_SERVICE(nsIPref, prefs, kPrefCID, &rv); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) return rv; prefs->ResetPrefs(); prefs->ReadUserPrefs(); @@ -1134,7 +1183,7 @@ NS_IMETHODIMP nsProfile::LoadNewProfilePrefs() // Migrate profile information from the 4x registry to 5x registry. NS_IMETHODIMP nsProfile::MigrateProfileInfo() { - nsresult rv = NS_OK; + nsresult rv = NS_OK; #if defined(XP_PC) || defined(XP_MAC) @@ -1142,7 +1191,7 @@ NS_IMETHODIMP nsProfile::MigrateProfileInfo() printf("Entered MigrateProfileInfo.\n"); #endif - char oldRegFile[_MAX_LENGTH] = {'\0'}; + char oldRegFile[_MAX_LENGTH] = {'\0'}; #ifdef XP_PC // Registry file has been traditionally stored in the windows directory (XP_PC). @@ -1154,24 +1203,28 @@ NS_IMETHODIMP nsProfile::MigrateProfileInfo() #else /* XP_MAC */ nsSpecialSystemDirectory *regLocation = NULL; - regLocation = new nsSpecialSystemDirectory(nsSpecialSystemDirectory::Mac_SystemDirectory); + regLocation = new nsSpecialSystemDirectory( + nsSpecialSystemDirectory::Mac_SystemDirectory); // Append the name of the old registry to the path obtained. *regLocation += "Preferences"; *regLocation += OLD_REGISTRY_FILE_NAME; PL_strcpy(oldRegFile, regLocation->GetNativePathCString()); + + delete regLocation; + #endif /* XP_PC */ - rv = gProfileDataAccess->Get4xProfileInfo(oldRegFile); + rv = gProfileDataAccess->Get4xProfileInfo(oldRegFile); #elif defined (XP_BEOS) #else /* XP_UNIX */ - rv = gProfileDataAccess->Get4xProfileInfo(nsnull); - + rv = gProfileDataAccess->Get4xProfileInfo(nsnull); #endif /* XP_PC || XP_MAC */ + gProfileDataAccess->mProfileDataChanged = PR_TRUE; gProfileDataAccess->UpdateRegistry(); return rv; @@ -1182,94 +1235,107 @@ NS_IMETHODIMP nsProfile::MigrateProfileInfo() // Set the profile to the current profile....debatable. // Calls PrefMigration service to do the Copy and Diverge // of 4x Profile information -NS_IMETHODIMP nsProfile::MigrateProfile(const char* profileName, PRBool showProgressAsModalWindow) +NS_IMETHODIMP +nsProfile::MigrateProfile(const PRUnichar* profileName, PRBool showProgressAsModalWindow) { - nsresult rv = NS_OK; + NS_ENSURE_ARG_POINTER(profileName); + + nsresult rv = NS_OK; #if defined(DEBUG_profile) - printf("Inside Migrate Profile routine.\n" ); + printf("Inside Migrate Profile routine.\n" ); #endif - nsFileSpec oldProfDir; - nsFileSpec newProfDir; + nsFileSpec oldProfDir; + nsFileSpec newProfDir; - rv = GetProfileDir(profileName, &oldProfDir); - if (NS_FAILED(rv)) return rv; - - // Create new profile dir path + rv = GetProfileDir(profileName, &oldProfDir); + if (NS_FAILED(rv)) return rv; + + // Create new profile dir path NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); - if (NS_FAILED(rv) || !locator) return NS_ERROR_FAILURE; - + if (NS_FAILED(rv) || !locator) return NS_ERROR_FAILURE; + // Get current profile, make the new one a sibling... - nsCOMPtr newSpec; - rv = locator->GetFileLocation(nsSpecialFileSpec::App_DefaultUserProfileRoot50, getter_AddRefs(newSpec)); - if (!newSpec) + nsCOMPtr newSpec; + rv = locator->GetFileLocation( + nsSpecialFileSpec::App_DefaultUserProfileRoot50, + getter_AddRefs(newSpec)); + if (!newSpec) return NS_ERROR_FAILURE; + newSpec->GetFileSpec(&newProfDir); - newProfDir += profileName; + newProfDir += profileName; - // Call migration service to do the work. + + // Call migration service to do the work. nsCOMPtr pPrefMigrator; - rv = nsComponentManager::CreateInstance(kPrefMigrationCID, - nsnull, - NS_GET_IID(nsIPrefMigration), - getter_AddRefs(pPrefMigrator)); + + rv = nsComponentManager::CreateInstance(kPrefMigrationCID, + nsnull, + NS_GET_IID(nsIPrefMigration), + getter_AddRefs(pPrefMigrator)); if (NS_FAILED(rv)) return rv; if (!pPrefMigrator) return NS_ERROR_FAILURE; nsXPIDLCString oldProfDirStr; nsXPIDLCString newProfDirStr; - + if (!newProfDir.Exists()) { - newProfDir.CreateDirectory(); + newProfDir.CreateDirectory(); } rv = GetStringFromSpec(newProfDir, getter_Copies(newProfDirStr)); if (NS_FAILED(rv)) return rv; if (!oldProfDir.Exists()) { - return NS_ERROR_FAILURE; + return NS_ERROR_FAILURE; } rv = GetStringFromSpec(oldProfDir, getter_Copies(oldProfDirStr)); if (NS_FAILED(rv)) return rv; - rv = pPrefMigrator->AddProfilePaths(oldProfDirStr, newProfDirStr); // you can do this a bunch of times. + + // you can do this a bunch of times. + rv = pPrefMigrator->AddProfilePaths(oldProfDirStr, newProfDirStr); + rv = pPrefMigrator->ProcessPrefs(showProgressAsModalWindow); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) return rv; // Copy the default 5.0 profile files into the migrated profile // Get profile defaults folder.. nsCOMPtr profDefaultsDir; - rv = locator->GetFileLocation(nsSpecialFileSpec::App_ProfileDefaultsFolder50, getter_AddRefs(profDefaultsDir)); + rv = locator->GetFileLocation( + nsSpecialFileSpec::App_ProfileDefaultsFolder50, + getter_AddRefs(profDefaultsDir)); - if (NS_FAILED(rv) || !profDefaultsDir) - { - return NS_ERROR_FAILURE; - } + if (NS_FAILED(rv) || !profDefaultsDir) + { + return NS_ERROR_FAILURE; + } - // Copy panels.rdf file - // This is a hack. Once the localFileSpec implementation - // is complete, this will be removed. - nsFileSpec defaultsDirSpecFile; + // Copy panels.rdf file + // This is a hack. Once the localFileSpec implementation + // is complete, this will be removed. + nsFileSpec defaultsDirSpecFile; - profDefaultsDir->GetFileSpec(&defaultsDirSpecFile); + profDefaultsDir->GetFileSpec(&defaultsDirSpecFile); - defaultsDirSpecFile += PANELS_RDF_FILE; + defaultsDirSpecFile += PANELS_RDF_FILE; - if (defaultsDirSpecFile.Exists()) - { - defaultsDirSpecFile.CopyToDir(newProfDir); - } - // hack finish. + if (defaultsDirSpecFile.Exists()) + { + defaultsDirSpecFile.CopyToDir(newProfDir); + } + // hack finish. rv = SetProfileDir(profileName, newProfDir); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) return rv; - gProfileDataAccess->mNumProfiles++; - gProfileDataAccess->mNumOldProfiles--; + gProfileDataAccess->mNumProfiles++; + gProfileDataAccess->mNumOldProfiles--; - gProfileDataAccess->mProfileDataChanged = PR_TRUE; - gProfileDataAccess->UpdateRegistry(); + gProfileDataAccess->mProfileDataChanged = PR_TRUE; + gProfileDataAccess->UpdateRegistry(); return rv; } @@ -1277,14 +1343,18 @@ NS_IMETHODIMP nsProfile::MigrateProfile(const char* profileName, PRBool showProg NS_IMETHODIMP nsProfile::GetCookie(char **cookie) { + NS_ENSURE_ARG_POINTER(cookie); + nsresult rv = NS_OK; - nsString aCookie; + nsAutoString aCookie; NS_WITH_SERVICE(nsIPref, prefs, kPrefCID, &rv); if (NS_FAILED(rv)) return rv; - char* pregURL; - rv = prefs->CopyCharPref(ACTIVATION_SERVER_URL, &pregURL); + nsXPIDLCString pregURL; + rv = prefs->CopyCharPref(ACTIVATION_SERVER_URL, + getter_Copies(pregURL)); + if (NS_FAILED(rv)) return rv; nsCOMPtr pregURI; rv = NS_NewURI(getter_AddRefs(pregURI), pregURL); @@ -1292,272 +1362,265 @@ NS_IMETHODIMP nsProfile::GetCookie(char **cookie) NS_WITH_SERVICE(nsICookieService, service, kCookieServiceCID, &rv); if ((NS_OK == rv) && (nsnull != service) && (nsnull != pregURI)) { rv = service->GetCookieString(pregURI, aCookie); - *cookie = nsCRT::strdup(aCookie.ToNewCString()); - } - CRTFREEIF(pregURL); + *cookie = nsCRT::strdup(nsCAutoString(aCookie).GetBuffer()); + } return rv; } NS_IMETHODIMP nsProfile::ProcessPRegCookie() { + nsresult rv = NS_OK; - nsresult rv = NS_OK; - - char *aCookie = nsnull; + char *aCookie = nsnull; GetCookie(&aCookie); - rv = ProcessPREGInfo(aCookie); - PR_FREEIF(aCookie); + rv = ProcessPREGInfo(aCookie); + CRTFREEIF(aCookie); - return rv; + return rv; } NS_IMETHODIMP nsProfile::ProcessPREGInfo(const char* data) { - nsresult rv = NS_OK; + NS_ENSURE_ARG_POINTER(data); + + nsresult rv = NS_OK; - nsString aCookie(data); + char *pregCookie = nsnull; + char *profileName = nsnull; + char *userEmail = nsnull; + char *service_denial = nsnull; - char *pregCookie = nsnull; - char *profileName = nsnull; - char *userEmail = nsnull; - char *service_denial = nsnull; + pregCookie = PL_strstr(data, NS_ACTIVATION_COOOKIE); - if ( (aCookie.ToNewCString()) != nsnull ) - { - pregCookie = PL_strstr(aCookie.ToNewCString(), NS_ACTIVATION_COOOKIE); - //In the original scenario you must UnEscape the string - //PL_strstr(nsUnescape(uregCookie),PREG_USERNAME); - if (pregCookie) - { - profileName = PL_strstr(nsUnescape(pregCookie), NS_ACTIVATION_USERNAME); - userEmail = PL_strstr(pregCookie, NS_ACTIVATION_USEREMAIL); - service_denial = PL_strstr(pregCookie, NS_ACTIVATION_DENIAL); - } - else - { - // cookie information is not available - return NS_ERROR_FAILURE; - } - } - else - { - // cookie information is not available - return NS_ERROR_FAILURE; - } + if (pregCookie) + { + profileName = PL_strstr(nsUnescape(pregCookie), + NS_ACTIVATION_USERNAME); + userEmail = PL_strstr(pregCookie, NS_ACTIVATION_USEREMAIL); + service_denial = PL_strstr(pregCookie, NS_ACTIVATION_DENIAL); + } + else + { + // cookie information is not available + return NS_ERROR_FAILURE; + } - nsAutoString pName; - nsAutoString emailAddress; - nsAutoString serviceState; + nsAutoString pName(profileName); + nsAutoString emailAddress(userEmail); + nsAutoString serviceState(service_denial); - if (profileName) - pName.Assign(profileName); - if (userEmail) - emailAddress.Assign(userEmail); - if (service_denial) - serviceState.Assign(service_denial); + PRInt32 profileNameIndex, serviceIndex, delimIndex; - PRInt32 profileNameIndex, delimIndex; - PRInt32 serviceIndex; + nsAutoString userProfileName, userServiceDenial, userEmailAddress; - nsString userProfileName, userServiceDenial, userEmailAddress; - - if (pName.Length()) - { - profileNameIndex = pName.Find("=", 0); - delimIndex = pName.Find("[-]", profileNameIndex-1); + if (pName.Length()) + { + profileNameIndex = pName.Find("=", 0); + delimIndex = pName.Find("[-]", profileNameIndex-1); - pName.Mid(userProfileName, profileNameIndex+1,delimIndex-(profileNameIndex+1)); + pName.Mid(userProfileName, profileNameIndex+1,delimIndex-(profileNameIndex+1)); #if defined(DEBUG_profile) - printf("\nProfiles : PREG Cookie user profile name = %s\n", userProfileName.ToNewCString()); + printf("\nProfiles : PREG Cookie user profile name = %s\n", nsCAutoString(userProfileName).GetBuffer()); #endif - } + } - if (emailAddress.Length()) - { - PRInt32 emailIndex; - emailIndex = emailAddress.Find("=", 0); - delimIndex = emailAddress.Find("[-]", emailIndex-1); + if (emailAddress.Length()) + { + PRInt32 emailIndex; + emailIndex = emailAddress.Find("=", 0); + delimIndex = emailAddress.Find("[-]", emailIndex-1); - emailAddress.Mid(userEmailAddress, emailIndex+1,delimIndex-(emailIndex+1)); + emailAddress.Mid(userEmailAddress, emailIndex+1,delimIndex-(emailIndex+1)); #if defined(DEBUG_profile) - printf("\nProfiles : PREG Cookie user email = %s\n", userEmailAddress.ToNewCString()); + printf("\nProfiles : PREG Cookie user email = %s\n", nsCAutoString(userEmailAddress).GetBuffer()); #endif - } + } - if (serviceState.Length()) - { - serviceIndex = serviceState.Find("=", 0); - delimIndex = serviceState.Find("[-]", serviceIndex-1); + if (serviceState.Length()) + { + serviceIndex = serviceState.Find("=", 0); + delimIndex = serviceState.Find("[-]", serviceIndex-1); - serviceState.Mid(userServiceDenial, serviceIndex+1,delimIndex-(serviceIndex+1)); + serviceState.Mid(userServiceDenial, serviceIndex+1,delimIndex-(serviceIndex+1)); #if defined(DEBUG_profile) - printf("\nProfiles : PREG Cookie netcenter service option = %s\n", userServiceDenial.ToNewCString()); + printf("\nProfiles : PREG Cookie netcenter service option = %s\n", nsCAutoString(userServiceDenial).GetBuffer()); #endif - } + } - // User didn't provide any information. - // No Netcenter info is available. - // User will hit the Preg info screens on the next run. - if ((userProfileName.Length() == 0) && (userServiceDenial.Length()== 0)) - return NS_ERROR_FAILURE; + // User didn't provide any information. + // No Netcenter info is available. + // User will hit the Preg info screens on the next run. + if ((userProfileName.mLength == 0) && (userServiceDenial.mLength == 0)) + return NS_ERROR_FAILURE; - // If user denies registration, ignore the information entered. - if (userServiceDenial.Length() > 0) - userProfileName.Assign(""); + // If user denies registration, ignore the information entered. + if (userServiceDenial.mLength > 0) + userProfileName.SetString(""); - char *curProfile = nsnull; - rv = GetCurrentProfile(&curProfile); + nsXPIDLString curProfile; + rv = GetCurrentProfile(getter_Copies(curProfile)); if (NS_FAILED(rv)) return rv; nsFileSpec dirSpec; rv = GetProfileDir(curProfile, &dirSpec); if (NS_FAILED(rv)) return rv; - if (userProfileName.Length() > 0) - { - rv = CloneProfile(userProfileName.ToNewCString()); + if (userProfileName.mLength > 0) + { + // BETA1 FIX for bug 31409 + //rv = CloneProfile(userProfileName.GetUnicode()); + //if (NS_FAILED(rv)) return rv; + + // Saving netcenter profile name for AIM and Mail settings + // This is required for the work around described below + nsAutoString netcenterProfileName(userProfileName); + + // XXX Setting the name to curProfile. Not renaming for BETA1. + userProfileName = curProfile; + + ProfileStruct* aProfile; + + rv = gProfileDataAccess->GetValue(userProfileName.GetUnicode(), &aProfile); if (NS_FAILED(rv)) return rv; - // Delete only if the new name is different from current profile - if (PL_strcmp(userProfileName.ToNewCString(), curProfile) != 0) - { - rv = DeleteProfile(curProfile, PR_FALSE); - if (NS_FAILED(rv)) return rv; - } - - // After cloning we have a new filespec. Remove the old one. - sRedundantDirectory = dirSpec; - sHaveRedundantDirectory = PR_TRUE; - - ProfileStruct* aProfile; - - gProfileDataAccess->GetValue(userProfileName.ToNewCString(), &aProfile); - - aProfile->NCProfileName = nsCRT::strdup(userProfileName.ToNewCString()); + aProfile->NCProfileName = netcenterProfileName; NS_WITH_SERVICE(nsIPref, prefs, kPrefCID, &rv); NS_ENSURE_SUCCESS(rv, rv); - prefs->SetCharPref(ACTIVATION_AIM_PREF, aProfile->NCProfileName); - - if (userEmailAddress.Length() > 0) - { - aProfile->NCEmailAddress = nsCRT::strdup(userEmailAddress.ToNewCString()); + prefs->SetCharPref(ACTIVATION_AIM_PREF, + nsCAutoString(netcenterProfileName).GetBuffer()); + + if ((userEmailAddress.Length()) > 0) + { + aProfile->NCEmailAddress = userEmailAddress; - PRBool validDomain = PR_FALSE; - char *domain = nsnull; - domain = PL_strstr(userEmailAddress.ToNewCString(), "@"); - - if (domain) - { - //strip @ symbol from domain string - domain++; + PRBool validDomain = PR_FALSE; + nsAutoString domain; + PRInt32 domainSep = userEmailAddress.FindChar('@'); + userEmailAddress.Mid(domain, domainSep+1, -1); // -1 means "the rest" + + if (domain.Length()) + { + //Check if it is a valid domain + CheckDomain(&validDomain, nsCAutoString(domain).GetBuffer()); + } - //Check if it is a valid domain - CheckDomain(&validDomain, domain); - } + if (validDomain) + { + nsXPIDLCString serverName; + nsXPIDLCString serverType; - if (validDomain) - { - char* serverName = nsnull; - char* serverType = nsnull; + rv = prefs->CopyCharPref(ACTIVATION_EMAIL_SERVER_NAME, + getter_Copies(serverName)); + if (NS_FAILED(rv)) return rv; + rv = prefs->CopyCharPref(ACTIVATION_EMAIL_SERVER_TYPE, + getter_Copies(serverType)); + if (NS_FAILED(rv)) return rv; - rv = prefs->CopyCharPref(ACTIVATION_EMAIL_SERVER_NAME, &serverName); - if (NS_FAILED(rv)) return rv; - rv = prefs->CopyCharPref(ACTIVATION_EMAIL_SERVER_TYPE, &serverType); - if (NS_FAILED(rv)) return rv; + if (!serverName || !serverType) + return NS_ERROR_FAILURE; + if ( (PL_strlen(serverName) == 0) || (PL_strlen(serverType) == 0) ) + return NS_ERROR_FAILURE; - if (!serverName || !serverType) - return NS_ERROR_FAILURE; - if ( (PL_strlen(serverName) == 0) || (PL_strlen(serverType) == 0) ) - return NS_ERROR_FAILURE; + NS_WITH_SERVICE(nsIMsgAccountManager, accountManager, + NS_MSGACCOUNTMANAGER_PROGID, &rv); + NS_ENSURE_SUCCESS(rv, rv); - NS_WITH_SERVICE(nsIMsgAccountManager, accountManager, NS_MSGACCOUNTMANAGER_PROGID, &rv); - NS_ENSURE_SUCCESS(rv, rv); + // create a fresh identity + nsCOMPtr identity; + rv = accountManager->CreateIdentity(getter_AddRefs(identity)); + if (NS_FAILED(rv)) return rv; - // create a fresh identity - nsCOMPtr identity; - rv = accountManager->CreateIdentity(getter_AddRefs(identity)); - if (NS_FAILED(rv)) return rv; + // Set Email address to the new identity + rv = identity->SetEmail(nsCAutoString(aProfile->NCEmailAddress).GetBuffer()); + if (NS_FAILED(rv)) return rv; + + // Set the identity's valid attribute to FALSE, so that account + // wizard picks up the imcomplete account + rv = identity->SetValid(PR_FALSE); + if (NS_FAILED(rv)) return rv; - // Set Email address to the new identity - rv = identity->SetEmail(aProfile->NCEmailAddress); - if (NS_FAILED(rv)) return rv; - - // Set the identity's valid attribute to FALSE, so that account - // wizard picks up the imcomplete account - rv = identity->SetValid(PR_FALSE); - if (NS_FAILED(rv)) return rv; + // create the server + nsCOMPtr server; + rv = accountManager->CreateIncomingServer( + nsCAutoString(aProfile->NCProfileName).GetBuffer(), + serverName, + serverType, + getter_AddRefs(server)); + if (NS_FAILED(rv)) return rv; - // create the server - nsCOMPtr server; - rv = accountManager->CreateIncomingServer(aProfile->NCProfileName, - serverName, - serverType, - getter_AddRefs(server)); - if (NS_FAILED(rv)) return rv; + // Set the IncomingServer's valid attribute to FALSE, so that account + // wizard picks up the imcomplete account + rv = server->SetValid(PR_FALSE); + if (NS_FAILED(rv)) return rv; - // Set the IncomingServer's valid attribute to FALSE, so that account - // wizard picks up the imcomplete account - rv = server->SetValid(PR_FALSE); - if (NS_FAILED(rv)) return rv; + // create the account + nsCOMPtr account; + rv = accountManager->CreateAccount(getter_AddRefs(account)); + if (NS_FAILED(rv)) return rv; - // create the account - nsCOMPtr account; - rv = accountManager->CreateAccount(getter_AddRefs(account)); - if (NS_FAILED(rv)) return rv; + // Assign server and add newly created identity to the account + rv = account->SetIncomingServer(server); + if (NS_FAILED(rv)) return rv; - // Assign server and add newly created identity to the account - rv = account->SetIncomingServer(server); - if (NS_FAILED(rv)) return rv; - rv = account->AddIdentity(identity); - if (NS_FAILED(rv)) return rv; + rv = account->AddIdentity(identity); + if (NS_FAILED(rv)) return rv; + } + } + aProfile->NCHavePregInfo = REGISTRY_YES_STRING; - CRTFREEIF(serverName); - CRTFREEIF(serverType); - } - } - aProfile->NCHavePregInfo = nsCRT::strdup(REGISTRY_YES_STRING); + gProfileDataAccess->SetValue(aProfile); + gProfileDataAccess->SetCurrentProfile(userProfileName.GetUnicode()); - gProfileDataAccess->SetValue(aProfile); - gProfileDataAccess->SetCurrentProfile(userProfileName.ToNewCString()); - FreeProfileStruct(aProfile); - } - else if (userServiceDenial.Length() > 0) - { - ProfileStruct* aProfile; + delete aProfile; + } + else if (userServiceDenial.mLength > 0) + { + ProfileStruct* aProfile; - gProfileDataAccess->GetValue(curProfile, &aProfile); + rv = gProfileDataAccess->GetValue(curProfile, &aProfile); + if (NS_FAILED(rv)) return rv; - aProfile->NCDeniedService = nsCRT::strdup(userServiceDenial.ToNewCString()); - aProfile->NCHavePregInfo = nsCRT::strdup(REGISTRY_YES_STRING); + aProfile->NCDeniedService = userServiceDenial; + aProfile->NCHavePregInfo = REGISTRY_YES_STRING; - gProfileDataAccess->SetValue(aProfile); - FreeProfileStruct(aProfile); - } + gProfileDataAccess->SetValue(aProfile); + + delete aProfile; + } - gProfileDataAccess->SetPREGInfo(REGISTRY_YES_STRING); + gProfileDataAccess->SetPREGInfo(REGISTRY_YES_STRING); - PR_FREEIF(curProfile); - return rv; + gProfileDataAccess->mProfileDataChanged=PR_TRUE; + gProfileDataAccess->UpdateRegistry(); + + return rv; } - -NS_IMETHODIMP nsProfile::IsPregCookieSet(const char *profileName, char **pregSet) +NS_IMETHODIMP nsProfile::IsPregCookieSet(const PRUnichar *profileName, char **pregSet) { - gProfileDataAccess->GetPREGInfo(profileName, pregSet); - return NS_OK; + NS_ENSURE_ARG_POINTER(profileName); + NS_ENSURE_ARG_POINTER(pregSet); + + gProfileDataAccess->GetPREGInfo(profileName, pregSet); + return NS_OK; } -NS_IMETHODIMP nsProfile::ProfileExists(const char *profileName, PRBool *exists) +NS_IMETHODIMP nsProfile::ProfileExists(const PRUnichar *profileName, PRBool *exists) { - *exists = gProfileDataAccess->ProfileExists(profileName); - return NS_OK; + NS_ENSURE_ARG_POINTER(profileName); + NS_ENSURE_ARG_POINTER(exists); + + *exists = gProfileDataAccess->ProfileExists(profileName); + return NS_OK; } NS_IMETHODIMP nsProfile::IsCurrentProfileAvailable(PRBool *avialable) { + NS_ENSURE_ARG_POINTER(avialable); + *avialable = mCurrentProfileAvailable; return NS_OK; } @@ -1566,10 +1629,11 @@ NS_IMETHODIMP nsProfile::IsCurrentProfileAvailable(PRBool *avialable) // Location: Common/Profiles NS_IMETHODIMP nsProfile::Get4xProfileCount(PRInt32 *numProfiles) { + NS_ENSURE_ARG_POINTER(numProfiles); *numProfiles = 0; - gProfileDataAccess->GetNum4xProfiles(numProfiles); + gProfileDataAccess->GetNum4xProfiles(numProfiles); return NS_OK; } @@ -1577,252 +1641,253 @@ NS_IMETHODIMP nsProfile::Get4xProfileCount(PRInt32 *numProfiles) // Migrates all unmigrated profiles NS_IMETHODIMP nsProfile::MigrateAllProfiles() { - nsresult rv = NS_OK; - for (PRInt32 i=0; i < gProfileDataAccess->mNumOldProfiles; i++) - { - ProfileStruct* profileItem = (ProfileStruct *) (gProfileDataAccess->m4xProfiles->ElementAt(i)); - rv = MigrateProfile(profileItem->profileName, PR_FALSE /* don't show progress as modal window */); - if (NS_FAILED(rv)) return rv; - } + nsresult rv = NS_OK; + for (PRInt32 i=0; i < gProfileDataAccess->mNumOldProfiles; i++) + { + ProfileStruct* profileItem = (ProfileStruct *) + (gProfileDataAccess->m4xProfiles->ElementAt(i)); + rv = MigrateProfile(profileItem->profileName.GetUnicode(), + PR_FALSE /* don't show progress as modal window */); + if (NS_FAILED(rv)) return rv; + } - return rv; + return rv; } -nsresult nsProfile::RenameProfileDir(const char* newProfileName) +nsresult nsProfile::RenameProfileDir(const PRUnichar* newProfileName) { - nsresult rv = NS_OK; - nsFileSpec dirSpec; + NS_ASSERTION(newProfileName, "Invalid new profile name"); + + nsresult rv = NS_OK; + nsFileSpec dirSpec; rv = GetProfileDir(newProfileName, &dirSpec); - if (NS_FAILED(rv)) return rv; - - nsFileSpec renamedDirSpec = dirSpec; - renamedDirSpec.SetLeafName(newProfileName); - renamedDirSpec.MakeUnique(); + if (NS_FAILED(rv)) return rv; + + nsFileSpec renamedDirSpec = dirSpec; + renamedDirSpec.SetLeafName(newProfileName); + renamedDirSpec.MakeUnique(); - // rename the directory - rv = dirSpec.Rename(renamedDirSpec.GetLeafName()); - if (NS_FAILED(rv)) return rv; + // rename the directory + rv = dirSpec.Rename(renamedDirSpec.GetLeafName()); + if (NS_FAILED(rv)) return rv; - // update the registry + // update the registry rv = SetProfileDir(newProfileName, dirSpec); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) return rv; - return NS_OK; + return NS_OK; } -NS_IMETHODIMP nsProfile::CloneProfile(const char* newProfile) +NS_IMETHODIMP nsProfile::CloneProfile(const PRUnichar* newProfile) { + NS_ENSURE_ARG_POINTER(newProfile); + nsresult rv = NS_OK; #if defined(DEBUG_profile) printf("ProfileManager : CloneProfile\n"); #endif - nsFileSpec currProfileDir; - nsFileSpec newProfileDir; + nsFileSpec currProfileDir; + nsFileSpec newProfileDir; NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); if (NS_FAILED(rv) || !locator) return NS_ERROR_FAILURE; - GetCurrentProfileDir(&currProfileDir); + GetCurrentProfileDir(&currProfileDir); - if (currProfileDir.Exists()) - { - nsCOMPtr dirSpec; - rv = locator->GetFileLocation(nsSpecialFileSpec::App_DefaultUserProfileRoot50, getter_AddRefs(dirSpec)); + if (currProfileDir.Exists()) + { + nsCOMPtr dirSpec; + rv = locator->GetFileLocation( + nsSpecialFileSpec::App_DefaultUserProfileRoot50, + getter_AddRefs(dirSpec)); - if (NS_FAILED(rv) || !dirSpec) - return NS_ERROR_FAILURE; + if (NS_FAILED(rv) || !dirSpec) + return NS_ERROR_FAILURE; - //Append profile name to form a directory name - dirSpec->GetFileSpec(&newProfileDir); + //Append profile name to form a directory name + dirSpec->GetFileSpec(&newProfileDir); - // TODO: - // hash profileName (will MakeUnique do that for us?) - // don't allow special characters (like ..) - // make acceptable length (will MakeUnique do that for us?) - newProfileDir += newProfile; - newProfileDir.MakeUnique(); + // TODO: + // hash profileName (will MakeUnique do that for us?) + // don't allow special characters (like ..) + // make acceptable length (will MakeUnique do that for us?) + newProfileDir += newProfile; + newProfileDir.MakeUnique(); - if (newProfileDir.Exists()) { + if (newProfileDir.Exists()) { #ifdef DEBUG_profile - printf("directory already exists\n"); + printf("directory already exists\n"); #endif - return NS_ERROR_FAILURE; - } + return NS_ERROR_FAILURE; + } - currProfileDir.RecursiveCopy(newProfileDir); - - rv = SetProfileDir(newProfile, newProfileDir); - } + currProfileDir.RecursiveCopy(newProfileDir); + + rv = SetProfileDir(newProfile, newProfileDir); + } #if defined(DEBUG_profile) - if (NS_SUCCEEDED(rv)) - printf("ProfileManager : Cloned CurrentProfile to new Profile ->%s<-\n", newProfile); + if (NS_SUCCEEDED(rv)) + printf("ProfileManager : Cloned CurrentProfile\n"); + printf("The new profile is ->%s<-\n", (const char*)nsCAutoString(newProfile)); #endif gProfileDataAccess->mNumProfiles++; - gProfileDataAccess->mProfileDataChanged = PR_TRUE; + gProfileDataAccess->mProfileDataChanged = PR_TRUE; rv = locator->ForgetProfileDir(); if (NS_FAILED(rv)) return rv; - return rv; -} - -void -nsProfile::FreeProfileStruct(ProfileStruct* aProfile) -{ - if (aProfile) - { - CRTFREEIF(aProfile->profileName); - CRTFREEIF(aProfile->profileLocation); - CRTFREEIF(aProfile->isMigrated); - CRTFREEIF(aProfile->NCProfileName); - CRTFREEIF(aProfile->NCDeniedService); - CRTFREEIF(aProfile->NCEmailAddress); - CRTFREEIF(aProfile->NCHavePregInfo); - - PR_FREEIF(aProfile); - } + return rv; } nsresult -nsProfile::TriggerActivation(const char *profileName) +nsProfile::TriggerActivation(const PRUnichar *profileName) { - nsresult rv = NS_OK; - char *isPregInfoSet = nsnull; - IsPregCookieSet(profileName, &isPregInfoSet); + NS_ASSERTION(profileName, "Invalid profileName"); - if (PL_strcmp(isPregInfoSet, REGISTRY_YES_STRING) != 0) - { - // fire up an instance of the cookie manager. - // I'm doing this using the serviceManager for convenience's sake. - // Presumably an application will init it's own cookie service a - // different way (this way works too though). - nsCOMPtr cookieService = do_GetService(NS_COOKIESERVICE_PROGID, &rv); - if (NS_FAILED(rv)) return rv; - // quiet the compiler - (void)cookieService; + nsresult rv = NS_OK; - PRBool acceptCookies = PR_TRUE; - cookieService->CookieEnabled(&acceptCookies); + nsXPIDLCString isPregInfoSet; + + IsPregCookieSet(profileName, getter_Copies(isPregInfoSet)); + + if (PL_strcmp(isPregInfoSet, REGISTRY_YES_STRING) != 0) + { + // fire up an instance of the cookie manager. + // I'm doing this using the serviceManager for convenience's sake. + // Presumably an application will init it's own cookie service a + // different way (this way works too though). + nsCOMPtr cookieService + = do_GetService(NS_COOKIESERVICE_PROGID, &rv); + if (NS_FAILED(rv)) return rv; + // quiet the compiler + (void)cookieService; + + PRBool acceptCookies = PR_TRUE; + cookieService->CookieEnabled(&acceptCookies); NS_WITH_SERVICE(nsIPref, prefs, kPrefCID, &rv); if (NS_FAILED(rv)) return rv; + // Check if the javascript is enabled.... + PRBool javascriptEnabled = PR_TRUE; + rv = prefs->GetBoolPref(JAVASCRIPT_PREF, &javascriptEnabled); - // Check if the javascript is enabled.... - PRBool javascriptEnabled = PR_TRUE; - rv = prefs->GetBoolPref(JAVASCRIPT_PREF, &javascriptEnabled); + // Check if cookies are accepted.... + // PRInt32 acceptCookies = 0; + // rv = prefs->GetIntPref(COOKIES_PREF, &acceptCookies); - // Check if cookies are accepted.... - // PRInt32 acceptCookies = 0; - // rv = prefs->GetIntPref(COOKIES_PREF, &acceptCookies); + // Set the boolean based on javascript and cookies prefs + PRBool requiredPrefsEnabled = PR_TRUE; - // Set the boolean based on javascript and cookies prefs - PRBool requiredPrefsEnabled = PR_TRUE; - - if ((!(javascriptEnabled)) || (!acceptCookies)) - requiredPrefsEnabled = PR_FALSE; + if ((!(javascriptEnabled)) || (!acceptCookies)) + requiredPrefsEnabled = PR_FALSE; - if (requiredPrefsEnabled) - { - /* - * Create the Application Shell instance... - */ - NS_WITH_SERVICE(nsIAppShellService, pregAppShell, kAppShellServiceCID, &rv); - if (NS_FAILED(rv)) return rv; + if (requiredPrefsEnabled) + { + /* + * Create the Application Shell instance... + */ + NS_WITH_SERVICE(nsIAppShellService, pregAppShell, kAppShellServiceCID, &rv); + if (NS_FAILED(rv)) return rv; - nsCOMPtr registrationURL; - rv = NS_NewURI(getter_AddRefs(registrationURL), ACTIVATION_FRAME_URL); + nsCOMPtr registrationURL; + rv = NS_NewURI(getter_AddRefs(registrationURL), ACTIVATION_FRAME_URL); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) return rv; - rv = pregAppShell->CreateTopLevelWindow(nsnull, registrationURL, - PR_TRUE, PR_TRUE, nsIWebBrowserChrome::allChrome, - ACTIVATION_WINDOW_WIDTH, // width - ACTIVATION_WINDOW_HEIGHT, // height - getter_AddRefs(mPregWindow)); + rv = pregAppShell->CreateTopLevelWindow(nsnull, registrationURL, + PR_TRUE, PR_TRUE, + nsIWebBrowserChrome::allChrome, + ACTIVATION_WINDOW_WIDTH, // width + ACTIVATION_WINDOW_HEIGHT, // height + getter_AddRefs(mPregWindow)); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) return rv; - // be sure to register ourself as the parent content listener on the - // webshell window we are creating. - nsCOMPtr docShellForWindow; - NS_ENSURE_SUCCESS(mPregWindow->GetDocShell(getter_AddRefs(docShellForWindow)), NS_ERROR_FAILURE); - nsCOMPtr ctnListener (do_GetInterface(docShellForWindow)); + // be sure to register ourself as the parent content listener on the + // webshell window we are creating. + nsCOMPtr docShellForWindow; + NS_ENSURE_SUCCESS(mPregWindow->GetDocShell( + getter_AddRefs(docShellForWindow)), + NS_ERROR_FAILURE); + nsCOMPtr ctnListener (do_GetInterface(docShellForWindow)); - ctnListener->SetParentContentListener(NS_STATIC_CAST(nsIURIContentListener *, this)); + ctnListener->SetParentContentListener(NS_STATIC_CAST(nsIURIContentListener *, this)); - /* - * Start up the main event loop... - */ - rv = pregAppShell->Run(); - } + /* + * Start up the main event loop... + */ + rv = pregAppShell->Run(); + } - ProcessPRegCookie(); - - } - CRTFREEIF(isPregInfoSet); - - return rv; + ProcessPRegCookie(); + } + return rv; } nsresult nsProfile::CleanUp() { - nsresult rv = NS_OK; + nsresult rv = NS_OK; - if (sHaveRedundantDirectory) - DeleteUserDirectories(sRedundantDirectory); + if (sHaveRedundantDirectory) + DeleteUserDirectories(sRedundantDirectory); - return rv; + return rv; } nsresult -nsProfile::CheckDomain(PRBool *valid, char* domain) +nsProfile::CheckDomain(PRBool *valid, const char* domain) { + NS_ASSERTION(valid, "Invalid valid pointer"); + NS_ASSERTION(domain, "Invalid domain"); + nsresult rv = NS_OK; NS_WITH_SERVICE(nsIPref, prefs, kPrefCID, &rv); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) return rv; - char* domainPref; - rv = prefs->CopyCharPref(ACTIVATION_ACCEPT_DOMAIN, &domainPref); - - if (PL_strcasecmp(domainPref, domain) == 0) - *valid = PR_TRUE; + nsXPIDLCString domainPref; + rv = prefs->CopyCharPref(ACTIVATION_ACCEPT_DOMAIN, getter_Copies(domainPref)); + + if (PL_strcasecmp(domainPref, domain) == 0) + *valid = PR_TRUE; - CRTFREEIF(domainPref); - - return rv; + return rv; } nsresult nsProfile::CreateDefaultProfile(void) { - nsresult rv = NS_OK; + nsresult rv = NS_OK; - nsFileSpec profileDirSpec; - - // Get the default user profiles folder + nsFileSpec profileDirSpec; + + // Get the default user profiles folder NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); - if (NS_FAILED(rv) || !locator) - return NS_ERROR_FAILURE; + if (NS_FAILED(rv) || !locator) + return NS_ERROR_FAILURE; nsCOMPtr spec; - rv = locator->GetFileLocation(nsSpecialFileSpec::App_DefaultUserProfileRoot50, getter_AddRefs(spec)); + rv = locator->GetFileLocation( + nsSpecialFileSpec::App_DefaultUserProfileRoot50, + getter_AddRefs(spec)); if (NS_FAILED(rv) || !spec) return NS_ERROR_FAILURE; spec->GetFileSpec(&profileDirSpec); rv = locator->ForgetProfileDir(); - rv = CreateNewProfile(DEFAULT_PROFILE_NAME, profileDirSpec); + nsAutoString dirSpecStr(profileDirSpec.GetNativePathCString()); - return rv; + rv = CreateNewProfile(nsAutoString(DEFAULT_PROFILE_NAME).GetUnicode(), dirSpecStr.GetUnicode()); + + return rv; } // The following implementations of nsIURIContentListener and nsIInterfaceRequestor are @@ -1832,8 +1897,8 @@ nsProfile::CreateDefaultProfile(void) NS_IMETHODIMP nsProfile::GetInterface(const nsIID & aIID, void * *aInstancePtr) { - NS_ENSURE_ARG_POINTER(aInstancePtr); - return QueryInterface(aIID, aInstancePtr); + NS_ENSURE_ARG_POINTER(aInstancePtr); + return QueryInterface(aIID, aInstancePtr); } // nsIURIContentListener support @@ -1846,90 +1911,111 @@ NS_IMETHODIMP nsProfile::OnStartURIOpen(nsIURI* aURI, NS_IMETHODIMP nsProfile::GetProtocolHandler(nsIURI *aURI, nsIProtocolHandler **aProtocolHandler) { - NS_ENSURE_ARG_POINTER(aProtocolHandler); - *aProtocolHandler = nsnull; - return NS_OK; + NS_ENSURE_ARG_POINTER(aURI); + NS_ENSURE_ARG_POINTER(aProtocolHandler); + + *aProtocolHandler = nsnull; + return NS_OK; } NS_IMETHODIMP nsProfile::GetParentContentListener(nsIURIContentListener** aParent) { - NS_ENSURE_ARG_POINTER(aParent); - *aParent = nsnull; - return NS_OK; + NS_ENSURE_ARG_POINTER(aParent); + *aParent = nsnull; + return NS_OK; } NS_IMETHODIMP nsProfile::SetParentContentListener(nsIURIContentListener* aParent) { - return NS_OK; + return NS_OK; } NS_IMETHODIMP nsProfile::GetLoadCookie(nsISupports ** aLoadCookie) { - NS_ENSURE_ARG_POINTER(aLoadCookie); - *aLoadCookie = nsnull; - return NS_OK; + NS_ENSURE_ARG_POINTER(aLoadCookie); + *aLoadCookie = nsnull; + return NS_OK; } NS_IMETHODIMP nsProfile::SetLoadCookie(nsISupports * aLoadCookie) { - return NS_OK; + return NS_OK; } NS_IMETHODIMP nsProfile::IsPreferred(const char * aContentType, - nsURILoadCommand aCommand, - const char * aWindowTarget, - char ** aDesiredContentType, - PRBool * aCanHandleContent) + nsURILoadCommand aCommand, + const char * aWindowTarget, + char ** aDesiredContentType, + PRBool * aCanHandleContent) { - return CanHandleContent(aContentType, aCommand, aWindowTarget, - aDesiredContentType, aCanHandleContent); + NS_ENSURE_ARG_POINTER(aContentType); + NS_ENSURE_ARG_POINTER(aWindowTarget); + NS_ENSURE_ARG_POINTER(aDesiredContentType); + NS_ENSURE_ARG_POINTER(aCanHandleContent); + + return CanHandleContent(aContentType, aCommand, aWindowTarget, + aDesiredContentType, aCanHandleContent); } NS_IMETHODIMP nsProfile::CanHandleContent(const char * aContentType, - nsURILoadCommand aCommand, - const char * aWindowTarget, - char ** aDesiredContentType, - PRBool * aCanHandleContent) + nsURILoadCommand aCommand, + const char * aWindowTarget, + char ** aDesiredContentType, + PRBool * aCanHandleContent) { - // the chrome window the profile manager is bringing up for registration - // needs to say that it handles ANY Content type because we haven't - // actually started to run any of the applications yet (which means - // that none of the applications are up and ready to handle any content). + // the chrome window the profile manager is bringing up for registration + // needs to say that it handles ANY Content type because we haven't + // actually started to run any of the applications yet (which means + // that none of the applications are up and ready to handle any content). - if (nsCRT::strcasecmp(aContentType, "message/rfc822") == 0) - *aDesiredContentType = nsCRT::strdup("text/xul"); - // since we explicilty loaded the url, we always want to handle it! - *aCanHandleContent = PR_TRUE; - return NS_OK; + if (nsCRT::strcasecmp(aContentType, "message/rfc822") == 0) + *aDesiredContentType = nsCRT::strdup("text/xul"); + + // since we explicilty loaded the url, we always want to handle it! + *aCanHandleContent = PR_TRUE; + return NS_OK; } NS_IMETHODIMP nsProfile::DoContent(const char * aContentType, - nsURILoadCommand aCommand, - const char * aWindowTarget, - nsIChannel * aOpenedChannel, - nsIStreamListener ** aContentHandler, - PRBool * aAbortProcess) + nsURILoadCommand aCommand, + const char * aWindowTarget, + nsIChannel * aOpenedChannel, + nsIStreamListener ** aContentHandler, + PRBool * aAbortProcess) { - // if we are currently showing a chromeless window for the registration stuff, - // then forward the call to the webshell window...then we are done... - if (mPregWindow) - { - nsCOMPtr docshell; - NS_ENSURE_SUCCESS(mPregWindow->GetDocShell(getter_AddRefs(docshell)), NS_ERROR_FAILURE); - nsCOMPtr ctnListener (do_GetInterface(docshell)); - NS_ENSURE_TRUE(ctnListener, NS_ERROR_FAILURE); + NS_ENSURE_ARG_POINTER(aContentType); + NS_ENSURE_ARG_POINTER(aWindowTarget); + NS_ENSURE_ARG_POINTER(aOpenedChannel); + NS_ENSURE_ARG_POINTER(aContentHandler); + NS_ENSURE_ARG_POINTER(aAbortProcess); - return ctnListener->DoContent(aContentType, aCommand, aWindowTarget, aOpenedChannel, aContentHandler, aAbortProcess); - } + nsresult rv = NS_OK; - return NS_OK; + // if we are currently showing a chromeless window for the registration stuff, + // then forward the call to the webshell window...then we are done... + if (mPregWindow) + { + nsCOMPtr docshell; + NS_ENSURE_SUCCESS(mPregWindow->GetDocShell( + getter_AddRefs(docshell)), NS_ERROR_FAILURE); + + nsCOMPtr ctnListener (do_GetInterface(docshell)); + NS_ENSURE_TRUE(ctnListener, NS_ERROR_FAILURE); + + return ctnListener->DoContent(aContentType, aCommand, + aWindowTarget, aOpenedChannel, + aContentHandler, aAbortProcess); + } + + return NS_OK; } + diff --git a/mozilla/profile/src/nsProfile.h b/mozilla/profile/src/nsProfile.h index df39612a995..be497ba1a23 100644 --- a/mozilla/profile/src/nsProfile.h +++ b/mozilla/profile/src/nsProfile.h @@ -34,49 +34,66 @@ #include "nsIInterfaceRequestor.h" #include "nsIURIContentListener.h" -#define _MAX_LENGTH 256 +#define _MAX_LENGTH 256 -class nsProfile: public nsIProfile, public nsIURIContentListener, public nsIInterfaceRequestor +#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_NC_USER_EMAIL_STRING "NCEmailAddress" +#define REGISTRY_NC_HAVE_PREG_INFO_STRING "NCHavePregInfo" +#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" + +class nsProfile: public nsIProfile, + public nsIURIContentListener, + public nsIInterfaceRequestor { - NS_DECL_ISUPPORTS - NS_DECL_NSIPROFILE + NS_DECL_ISUPPORTS + NS_DECL_NSIPROFILE - // these are necessary if the profile manager is going to load urls for - // the registration stuff.... - NS_DECL_NSIURICONTENTLISTENER - NS_DECL_NSIINTERFACEREQUESTOR + // these are necessary if the profile manager is going to load urls for + // the registration stuff.... + NS_DECL_NSIURICONTENTLISTENER + NS_DECL_NSIINTERFACEREQUESTOR private: - nsresult ProcessArgs(nsICmdLineService *service, - PRBool *profileDirSet, - nsCString & profileURLStr); - nsresult LoadDefaultProfileDir(nsCString & profileURLStr); - PRBool mAutomigrate; - nsCOMPtr mPregWindow; + nsresult ProcessArgs(nsICmdLineService *service, + PRBool *profileDirSet, + nsCString & profileURLStr); + nsresult LoadDefaultProfileDir(nsCString & profileURLStr); + PRBool mAutomigrate; + nsCOMPtr mPregWindow; public: - nsProfile(); - virtual ~nsProfile(); + nsProfile(); + virtual ~nsProfile(); - nsresult RenameProfileDir(const char *newProfileName); + nsresult RenameProfileDir(const PRUnichar *newProfileName); - // Creates associated user directories on the creation of a new profile + // Creates associated user directories on the creation of a new profile nsresult CreateUserDirectories(const nsFileSpec& profileDir); - // Deletes associated user directories - nsresult DeleteUserDirectories(const nsFileSpec& profileDir); + // Deletes associated user directories + nsresult DeleteUserDirectories(const nsFileSpec& profileDir); - // Copies all the registry keys from old profile to new profile - nsresult CopyRegKey(const char *oldProfile, const char *newProfile); + // Copies all the registry keys from old profile to new profile + nsresult CopyRegKey(const PRUnichar *oldProfile, const PRUnichar *newProfile); - // Routine that frees the memory allocated to temp profile struct - void FreeProfileStruct(ProfileStruct* aProfile); + nsresult AutoMigrate(); - nsresult AutoMigrate(); - - nsresult CreateDefaultProfile(void); - nsresult TriggerActivation(const char *profileName); - nsresult CleanUp(); - nsresult CheckDomain(PRBool *valid, char *domainName); + nsresult CreateDefaultProfile(void); + nsresult TriggerActivation(const PRUnichar *profileName); + nsresult CleanUp(); + nsresult CheckDomain(PRBool *valid, const char *domainName); }; diff --git a/mozilla/profile/src/nsProfileAccess.cpp b/mozilla/profile/src/nsProfileAccess.cpp index a0b0771488b..6a8d073dbcd 100644 --- a/mozilla/profile/src/nsProfileAccess.cpp +++ b/mozilla/profile/src/nsProfileAccess.cpp @@ -36,26 +36,8 @@ #include "nsFileStream.h" #include "nsEscape.h" -#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_NC_USER_EMAIL_STRING "NCEmailAddress" -#define REGISTRY_NC_HAVE_PREG_INFO_STRING "NCHavePregInfo" -#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 NUM_HEX_BYTES 8 +#define MAX_PERSISTENT_DATA_SIZE 1000 +#define NUM_HEX_BYTES 8 #define ISHEX(c) ( ((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F') ) #if defined (XP_UNIX) @@ -75,31 +57,25 @@ static NS_DEFINE_CID(kRegistryCID, NS_REGISTRY_CID); */ nsProfileAccess::nsProfileAccess() { - m_registry = null_nsCOMPtr(); - mCount = 0; - mNumProfiles = 0; - mNumOldProfiles = 0; - m4xCount = 0; - mCurrentProfile = nsnull; - mVersion = nsnull; - mHavePREGInfo = nsnull; - mFixRegEntries = PR_FALSE; - mProfileDataChanged = PR_FALSE; - mForgetProfileCalled = PR_FALSE; - mProfiles = new nsVoidArray(); - m4xProfiles = new nsVoidArray(); + m_registry = null_nsCOMPtr(); + mCount = 0; + mNumProfiles = 0; + mNumOldProfiles = 0; + m4xCount = 0; + mFixRegEntries = PR_FALSE; + mProfileDataChanged = PR_FALSE; + mForgetProfileCalled = PR_FALSE; + mProfiles = new nsVoidArray(); + m4xProfiles = new nsVoidArray(); - FillProfileInfo(); + FillProfileInfo(); } // On the way out, close the registry if it is // still opened and free up the resources. nsProfileAccess::~nsProfileAccess() { - // Release all resources. - CRTFREEIF(mCurrentProfile); - CRTFREEIF(mVersion); - CRTFREEIF(mHavePREGInfo); + // Release all resources. FreeProfileMembers(mProfiles, mCount); FreeProfileMembers(m4xProfiles, m4xCount); @@ -109,17 +85,21 @@ nsProfileAccess::~nsProfileAccess() void nsProfileAccess::FreeProfileMembers(nsVoidArray *profiles, PRInt32 numElems) { + NS_ASSERTION(profiles, "Invalid profiles"); - PRInt32 index = 0; + PRInt32 index = 0; - ProfileStruct* aProfile; - for (index = 0; index < numElems; index++) - { - aProfile = (ProfileStruct *) profiles->ElementAt(index); - delete aProfile; - } - if (profiles) - delete profiles; + ProfileStruct* aProfile; + if (profiles) { + for (index = 0; index < numElems; index++) + { + aProfile = (ProfileStruct *) profiles->ElementAt(index); + + delete aProfile; + } + + delete profiles; + } } // Close the registry. @@ -138,7 +118,6 @@ nsProfileAccess::OpenRegistry() nsresult rv; PRBool openalready = PR_FALSE; - if (!m_registry) { rv = nsComponentManager::CreateInstance(kRegistryCID, nsnull, @@ -162,45 +141,40 @@ nsProfileAccess::OpenRegistry() // contains the relavant profile information will be filled. // Caller must free up the profile struct. nsresult -nsProfileAccess::GetValue(const char* profileName, ProfileStruct** aProfile) +nsProfileAccess::GetValue(const PRUnichar* profileName, ProfileStruct** aProfile) { - PRInt32 index = 0; - - index = FindProfileIndex(profileName); + NS_ASSERTION(profileName, "Invalid profile name"); + NS_ASSERTION(aProfile, "Invalid profile pointer"); - if (index >= 0) - { - *aProfile = (ProfileStruct *) PR_Malloc(sizeof(ProfileStruct)); - if (!*aProfile) - return NS_ERROR_OUT_OF_MEMORY; + PRInt32 index = 0; + index = FindProfileIndex(profileName); - ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + if (index < 0) + return NS_ERROR_FAILURE; - (*aProfile)->profileName = nsnull; - (*aProfile)->profileLocation = nsnull; - (*aProfile)->isMigrated = nsnull; - (*aProfile)->NCProfileName = nsnull; - (*aProfile)->NCDeniedService = nsnull; - (*aProfile)->NCEmailAddress = nsnull; - (*aProfile)->NCHavePregInfo = nsnull; + *aProfile = new ProfileStruct(); + if (!*aProfile) + return NS_ERROR_OUT_OF_MEMORY; - (*aProfile)->profileName = nsCRT::strdup(profileItem->profileName); - (*aProfile)->profileLocation = nsCRT::strdup(profileItem->profileLocation); - (*aProfile)->isMigrated = nsCRT::strdup(profileItem->isMigrated); + ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); - if (profileItem->NCProfileName) - (*aProfile)->NCProfileName = nsCRT::strdup(profileItem->NCProfileName); - if (profileItem->NCDeniedService) - (*aProfile)->NCProfileName = nsCRT::strdup(profileItem->NCDeniedService); - if (profileItem->NCEmailAddress) - (*aProfile)->NCProfileName = nsCRT::strdup(profileItem->NCEmailAddress); - if (profileItem->NCHavePregInfo) - (*aProfile)->NCProfileName = nsCRT::strdup(profileItem->NCHavePregInfo); - } - else - *aProfile = nsnull; + (*aProfile)->profileName = profileItem->profileName; + (*aProfile)->profileLocation = profileItem->profileLocation; + (*aProfile)->isMigrated = profileItem->isMigrated; - return NS_OK; + if (!profileItem->NCProfileName.IsEmpty()) + (*aProfile)->NCProfileName = profileItem->NCProfileName; + + if (!profileItem->NCDeniedService.IsEmpty()) + (*aProfile)->NCDeniedService = profileItem->NCDeniedService; + + if (!profileItem->NCEmailAddress.IsEmpty()) + (*aProfile)->NCEmailAddress = profileItem->NCEmailAddress; + + if (!profileItem->NCHavePregInfo.IsEmpty()) + (*aProfile)->NCHavePregInfo = profileItem->NCHavePregInfo; + + return NS_OK; } // This method writes all changes to the array of the @@ -210,83 +184,57 @@ nsProfileAccess::GetValue(const char* profileName, ProfileStruct** aProfile) nsresult nsProfileAccess::SetValue(ProfileStruct* aProfile) { - PRInt32 index = 0; + NS_ASSERTION(aProfile, "Invalid profile"); - index = FindProfileIndex(aProfile->profileName); - - if (index >= 0) - { - ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + PRInt32 index = 0; + PRBool isNewProfile = PR_FALSE; + ProfileStruct* profileItem; - PRInt32 length = PL_strlen(aProfile->profileLocation); - profileItem->profileLocation = (char *) PR_Realloc(profileItem->profileLocation, length+1); - PL_strcpy(profileItem->profileLocation, aProfile->profileLocation); - - length = PL_strlen(aProfile->isMigrated); - profileItem->isMigrated = (char *) PR_Realloc(profileItem->isMigrated, length+1); - PL_strcpy(profileItem->isMigrated, aProfile->isMigrated); - - profileItem->updateProfileEntry = PR_TRUE; + index = FindProfileIndex(aProfile->profileName.GetUnicode()); - if (aProfile->NCProfileName) - { - length = PL_strlen(aProfile->NCProfileName); - profileItem->NCProfileName = (char *) PR_Realloc(profileItem->NCProfileName, length+1); - PL_strcpy(profileItem->NCProfileName, aProfile->NCProfileName); - } - if (aProfile->NCDeniedService) - { - length = PL_strlen(aProfile->NCDeniedService); - profileItem->NCDeniedService = (char *) PR_Realloc(profileItem->NCDeniedService, length+1); - PL_strcpy(profileItem->NCDeniedService, aProfile->NCDeniedService); - } - if (aProfile->NCEmailAddress) - { - length = PL_strlen(aProfile->NCEmailAddress); - profileItem->NCEmailAddress = (char *) PR_Realloc(profileItem->NCEmailAddress, length+1); - PL_strcpy(profileItem->NCEmailAddress, aProfile->NCEmailAddress); - } - if (aProfile->NCHavePregInfo) - { - length = PL_strlen(aProfile->NCHavePregInfo); - profileItem->NCHavePregInfo = (char *) PR_Realloc(profileItem->NCHavePregInfo, length+1); - PL_strcpy(profileItem->NCHavePregInfo, aProfile->NCHavePregInfo); - } - } - else - { - ProfileStruct* profileItem = new ProfileStruct(); - if (!profileItem) - return NS_ERROR_OUT_OF_MEMORY; + if (index >= 0) + { + profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + } + else + { + isNewProfile = PR_TRUE; - profileItem->profileName = nsnull; - profileItem->profileLocation = nsnull; - profileItem->isMigrated = nsnull; - profileItem->NCProfileName = nsnull; - profileItem->NCDeniedService = nsnull; - profileItem->NCEmailAddress = nsnull; - profileItem->NCHavePregInfo = nsnull; + profileItem = new ProfileStruct(); + if (!profileItem) + return NS_ERROR_OUT_OF_MEMORY; - profileItem->profileName = nsCRT::strdup(aProfile->profileName); - profileItem->profileLocation = nsCRT::strdup(aProfile->profileLocation); - profileItem->isMigrated = nsCRT::strdup(aProfile->isMigrated); - profileItem->updateProfileEntry = PR_TRUE; + profileItem->profileName = aProfile->profileName; + } - if (aProfile->NCProfileName) - profileItem->NCProfileName = nsCRT::strdup(aProfile->NCProfileName); - if (aProfile->NCDeniedService) - profileItem->NCProfileName = nsCRT::strdup(aProfile->NCDeniedService); - if (aProfile->NCEmailAddress) - profileItem->NCEmailAddress = nsCRT::strdup(aProfile->NCEmailAddress); - if (aProfile->NCHavePregInfo) - profileItem->NCHavePregInfo = nsCRT::strdup(aProfile->NCHavePregInfo); + profileItem->profileLocation = aProfile->profileLocation; - if (!mProfiles) - mProfiles = new nsVoidArray(); - mProfiles->AppendElement((void*)profileItem); - mCount++; - } - return NS_OK; + profileItem->isMigrated = aProfile->isMigrated; + + profileItem->updateProfileEntry = PR_TRUE; + + if (!aProfile->NCProfileName.IsEmpty()) + profileItem->NCProfileName = aProfile->NCProfileName; + + if (!aProfile->NCDeniedService.IsEmpty()) + profileItem->NCDeniedService = aProfile->NCDeniedService; + + if (!aProfile->NCEmailAddress.IsEmpty()) + profileItem->NCEmailAddress = aProfile->NCEmailAddress; + + if (!aProfile->NCHavePregInfo.IsEmpty()) + profileItem->NCHavePregInfo = aProfile->NCHavePregInfo; + + + if (isNewProfile) { + if (!mProfiles) + mProfiles = new nsVoidArray(); + + mProfiles->AppendElement((void*)profileItem); + mCount++; + } + + return NS_OK; } // Enumerates through the registry for profile @@ -301,146 +249,197 @@ nsProfileAccess::FillProfileInfo() { nsresult rv = NS_OK; - // Make the fail: thing work - mProfiles = nsnull; - rv = OpenRegistry(); if (NS_FAILED(rv)) return rv; // Enumerate all subkeys (immediately) under the given node. nsCOMPtr enumKeys; nsRegistryKey profilesTreeKey; - - rv = m_registry->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &profilesTreeKey); + + rv = m_registry->GetKey(nsIRegistry::Common, + nsAutoString(REGISTRY_PROFILE_SUBTREE_STRING).GetUnicode(), + &profilesTreeKey); if (NS_FAILED(rv)) - { - rv = m_registry->AddSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &profilesTreeKey); - if (NS_FAILED(rv)) return rv; - } - - // Get the current profile - rv = m_registry->GetStringUTF8(profilesTreeKey, REGISTRY_CURRENT_PROFILE_STRING, &mCurrentProfile); - - // Get the profile version - rv = m_registry->GetStringUTF8(profilesTreeKey, REGISTRY_VERSION_STRING, &mVersion); - - if (mVersion == nsnull) - { - mFixRegEntries = PR_TRUE; - mVersion = nsCRT::strdup(REGISTRY_VERSION_1_0); - mProfileDataChanged = PR_TRUE; - } - - // Get the preg info - rv = m_registry->GetStringUTF8(profilesTreeKey, REGISTRY_HAVE_PREG_INFO_STRING, &mHavePREGInfo); - - if (mHavePREGInfo == nsnull) - { - mHavePREGInfo = nsCRT::strdup(REGISTRY_NO_STRING); - mProfileDataChanged = PR_TRUE; - } - - rv = m_registry->EnumerateSubtrees( profilesTreeKey, getter_AddRefs(enumKeys)); - if (NS_FAILED(rv)) return rv; - - rv = enumKeys->First(); - if (NS_FAILED(rv)) return rv; - - mCount = 0; - mNumProfiles = 0; - mNumOldProfiles = 0; - - while( (NS_OK != enumKeys->IsDone()) ) { - nsCOMPtr base; + rv = m_registry->AddKey(nsIRegistry::Common, + nsAutoString(REGISTRY_PROFILE_SUBTREE_STRING).GetUnicode(), + &profilesTreeKey); + if (NS_FAILED(rv)) return rv; + } + + + // introducing these tmp variables as nsString variables cannot be passed to + // the resgitry methods + nsXPIDLString tmpCurrentProfile; + nsXPIDLString tmpVersion; + nsXPIDLString tmpPREGInfo; + + + // For the following variables, we do not check for the rv value + // but check for the variable instead, because it is valid to proceed + // without the variables having a value. That's why there are no returns + // for invalid rv values. + + // Get the current profile + rv = m_registry->GetString(profilesTreeKey, + nsAutoString(REGISTRY_CURRENT_PROFILE_STRING).GetUnicode(), + getter_Copies(tmpCurrentProfile)); + + if (tmpCurrentProfile) + { + // If current profile does not exist, mCurrentProfile will not be set + // This is not harmful, as GetCurrentProfile method needs to return this value + // And GetCurrentProfile returns: + // the current profile if set + // the first profile if profiles exist but no current profile is set + // an empty string if no profiles exist. + + mCurrentProfile = tmpCurrentProfile; + } + + // Get the profile version + rv = m_registry->GetString(profilesTreeKey, + nsAutoString(REGISTRY_VERSION_STRING).GetUnicode(), + getter_Copies(tmpVersion)); + + if (tmpVersion == nsnull) + { + mFixRegEntries = PR_TRUE; + mVersion = REGISTRY_VERSION_1_0; + + mProfileDataChanged = PR_TRUE; + } + + // Get the preg info + rv = m_registry->GetString(profilesTreeKey, + nsAutoString(REGISTRY_HAVE_PREG_INFO_STRING).GetUnicode(), + getter_Copies(tmpPREGInfo)); + + if (tmpPREGInfo == nsnull) + { + mHavePREGInfo = REGISTRY_NO_STRING; + + mProfileDataChanged = PR_TRUE; + } + + rv = m_registry->EnumerateSubtrees( profilesTreeKey, getter_AddRefs(enumKeys)); + if (NS_FAILED(rv)) return rv; + + rv = enumKeys->First(); + if (NS_FAILED(rv)) return rv; + + mCount = 0; + mNumProfiles = 0; + mNumOldProfiles = 0; + + while( (NS_OK != enumKeys->IsDone()) ) + { + nsCOMPtr base; + + rv = enumKeys->CurrentItem( getter_AddRefs(base) ); + if (NS_FAILED(rv)) return rv; - rv = enumKeys->CurrentItem( getter_AddRefs(base) ); - if (NS_FAILED(rv)) return rv; - // Get specific interface. nsCOMPtr node; nsIID nodeIID = NS_IREGISTRYNODE_IID; - + rv = base->QueryInterface( nodeIID, getter_AddRefs(node)); - if (NS_FAILED(rv)) return rv; - - // Get node name. - nsXPIDLCString profile; - nsXPIDLCString isMigrated; - nsXPIDLCString NCProfileName; - nsXPIDLCString NCDeniedService; - nsXPIDLCString NCEmailAddress; - nsXPIDLCString NCHavePregInfo; - char* directory = nsnull; - - rv = node->GetNameUTF8( getter_Copies(profile) ); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) return rv; - if (profile) - { - nsRegistryKey profKey; + // Get node name. + nsXPIDLString profile; + nsXPIDLString isMigrated; + nsXPIDLString NCProfileName; + nsXPIDLString NCDeniedService; + nsXPIDLString NCEmailAddress; + nsXPIDLString NCHavePregInfo; + nsXPIDLString directory; - rv = m_registry->GetSubtree(profilesTreeKey, profile, &profKey); - if (NS_FAILED(rv)) return rv; - - rv = m_registry->GetStringUTF8(profKey, REGISTRY_DIRECTORY_STRING, &directory); - if (NS_FAILED(rv)) return rv; + rv = node->GetName(getter_Copies(profile)); + if (NS_FAILED(rv)) return rv; - if (mFixRegEntries) - FixRegEntry(&directory); + nsRegistryKey profKey; + rv = node->GetKey(&profKey); + if (NS_FAILED(rv)) return rv; - rv = m_registry->GetStringUTF8(profKey, REGISTRY_MIGRATED_STRING, getter_Copies(isMigrated)); - if (NS_FAILED(rv)) return rv; + rv = m_registry->GetString(profKey, + nsAutoString(REGISTRY_DIRECTORY_STRING).GetUnicode(), + getter_Copies(directory)); + if (NS_FAILED(rv)) return rv; - rv = m_registry->GetStringUTF8(profKey, REGISTRY_NC_PROFILE_NAME_STRING, getter_Copies(NCProfileName)); - rv = m_registry->GetStringUTF8(profKey, REGISTRY_NC_SERVICE_DENIAL_STRING, getter_Copies(NCDeniedService)); - rv = m_registry->GetStringUTF8(profKey, REGISTRY_NC_USER_EMAIL_STRING, getter_Copies(NCEmailAddress)); - rv = m_registry->GetStringUTF8(profKey, REGISTRY_NC_HAVE_PREG_INFO_STRING, getter_Copies(NCHavePregInfo)); + if (mFixRegEntries) + FixRegEntry(getter_Copies(directory)); - ProfileStruct* profileItem = new ProfileStruct(); - if (!profileItem) - return NS_ERROR_OUT_OF_MEMORY; + rv = m_registry->GetString(profKey, + nsAutoString(REGISTRY_MIGRATED_STRING).GetUnicode(), + getter_Copies(isMigrated)); + if (NS_FAILED(rv)) return rv; - profileItem->profileName = nsnull; - profileItem->profileLocation = nsnull; - profileItem->isMigrated = nsnull; - profileItem->NCProfileName = nsnull; - profileItem->NCDeniedService = nsnull; - profileItem->NCEmailAddress = nsnull; - profileItem->NCHavePregInfo = nsnull; - profileItem->updateProfileEntry = PR_TRUE; - - profileItem->profileName = nsCRT::strdup(profile); - profileItem->profileLocation = nsCRT::strdup(directory); - profileItem->isMigrated = nsCRT::strdup(isMigrated); - if (NCProfileName) - profileItem->NCProfileName = nsCRT::strdup(NCProfileName); - if (NCDeniedService) - profileItem->NCDeniedService = nsCRT::strdup(NCDeniedService); - if (NCEmailAddress) - profileItem->NCEmailAddress = nsCRT::strdup(NCEmailAddress); - if (NCHavePregInfo) - profileItem->NCHavePregInfo = nsCRT::strdup(NCHavePregInfo); + // Not checking the return values of these variables as they + // are for activation, they are optional and their values + // do not call for a return + rv = m_registry->GetString(profKey, + nsAutoString(REGISTRY_NC_PROFILE_NAME_STRING).GetUnicode(), + getter_Copies(NCProfileName)); - if (PL_strcmp(isMigrated, REGISTRY_YES_STRING) == 0) - mNumProfiles++; - else if (PL_strcmp(isMigrated, REGISTRY_NO_STRING) == 0) - mNumOldProfiles++; + rv = m_registry->GetString(profKey, + nsAutoString(REGISTRY_NC_SERVICE_DENIAL_STRING).GetUnicode(), + getter_Copies(NCDeniedService)); + + rv = m_registry->GetString(profKey, + nsAutoString(REGISTRY_NC_USER_EMAIL_STRING).GetUnicode(), + getter_Copies(NCEmailAddress)); + + rv = m_registry->GetString(profKey, + nsAutoString(REGISTRY_NC_HAVE_PREG_INFO_STRING).GetUnicode(), + getter_Copies(NCHavePregInfo)); + + + ProfileStruct* profileItem = new ProfileStruct(); + if (!profileItem) + return NS_ERROR_OUT_OF_MEMORY; + + profileItem->updateProfileEntry = PR_TRUE; + + profileItem->profileName = profile; + profileItem->profileLocation = directory; + profileItem->isMigrated = isMigrated; + + if (NCProfileName) + profileItem->NCProfileName = NCProfileName; + + if (NCDeniedService) + profileItem->NCDeniedService = NCDeniedService; + + if (NCEmailAddress) + profileItem->NCEmailAddress = NCEmailAddress; + + if (NCHavePregInfo) + profileItem->NCHavePregInfo = NCHavePregInfo; + + if (nsAutoString(REGISTRY_YES_STRING).Equals(isMigrated)) + mNumProfiles++; + else if (nsAutoString(REGISTRY_NO_STRING).Equals(isMigrated)) + mNumOldProfiles++; + + if (!mProfiles) { + mProfiles = new nsVoidArray(); if (!mProfiles) - mProfiles = new nsVoidArray(); - mProfiles->AppendElement((void*)profileItem); - - mCount++; - CRTFREEIF(directory); - } - rv = enumKeys->Next(); - } + return NS_ERROR_OUT_OF_MEMORY; + } - mFixRegEntries = PR_FALSE; - rv = CloseRegistry(); - return rv; + mProfiles->AppendElement((void*)profileItem); + + mCount++; + + rv = enumKeys->Next(); + } + + mFixRegEntries = PR_FALSE; + rv = CloseRegistry(); + return rv; } // Return the number of 5x profiles. @@ -449,7 +448,9 @@ nsProfileAccess::FillProfileInfo() void nsProfileAccess::GetNumProfiles(PRInt32 *numProfiles) { - *numProfiles = mNumProfiles; + NS_ASSERTION(numProfiles, "Invalid numProfiles"); + + *numProfiles = mNumProfiles; } // Return the number of 4x (>=4.5 & < 5.0) profiles. @@ -458,7 +459,38 @@ nsProfileAccess::GetNumProfiles(PRInt32 *numProfiles) void nsProfileAccess::GetNum4xProfiles(PRInt32 *numProfiles) { - *numProfiles = mNumOldProfiles; + NS_ASSERTION(numProfiles, "Invalid numProfiles"); + + PRInt32 index = 0; + + *numProfiles = 0; + + for(index = 0; index < mCount; index++) + { + ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + + if (profileItem->isMigrated == REGISTRY_NO_STRING) + { + (*numProfiles)++; + } + } + + // ******** This is a HACK -- to be changed later ******** + // When we run mozilla -installer for the second time, mNumOldProfiles is set to 0 + // This happens because MigrateProfileInfo realizes that the old profiles info + // already exists in mozRegistry (from the first run of -installer) + // and does not fill m4xProfiles leaving it empty. + + // A default profile is created if there are 0 number of 4x and 5x profiles. + // Setting mNumOldProfiles to 0 can result in this side effect if there are no + // 5x profiles, although there are >0 number of 4x profiles. + // This side effect would happen in nsProfile::ProcessArgs -- INSTALLER option. + + // So we query the mProfiles array for the latest numOfOldProfiles + // This returns the right value and we set mNumOldProfiles to this value + // This results in the correct behaviour. + + mNumOldProfiles = *numProfiles; } // If the application can't find the current profile, @@ -466,22 +498,24 @@ nsProfileAccess::GetNum4xProfiles(PRInt32 *numProfiles) // This routine returns the first 5x profile. // Caller must free up the string (firstProfile). void -nsProfileAccess::GetFirstProfile(char **firstProfile) +nsProfileAccess::GetFirstProfile(PRUnichar **firstProfile) { - PRInt32 index = 0; + NS_ASSERTION(firstProfile, "Invalid firstProfile pointer"); - *firstProfile = nsnull; + PRInt32 index = 0; - for(index = 0; index < mCount; index++) - { - ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + *firstProfile = nsnull; - if (PL_strcmp(profileItem->isMigrated, REGISTRY_YES_STRING) == 0) - { - *firstProfile = nsCRT::strdup(profileItem->profileName); - break; - } - } + for(index = 0; index < mCount; index++) + { + ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + + if (profileItem->isMigrated == REGISTRY_YES_STRING) + { + *firstProfile = profileItem->profileName.ToNewUnicode(); + break; + } + } } // Set the current profile. Opearting directly on the tree. @@ -490,156 +524,166 @@ nsProfileAccess::GetFirstProfile(char **firstProfile) // now, we depend on registry operations. // Capture the current profile information into mCurrentProfile. void -nsProfileAccess::SetCurrentProfile(const char *profileName) +nsProfileAccess::SetCurrentProfile(const PRUnichar *profileName) { - CRTFREEIF(mCurrentProfile); - mCurrentProfile = nsCRT::strdup(profileName); - mProfileDataChanged = PR_TRUE; + NS_ASSERTION(profileName, "Invalid profile name"); + + mCurrentProfile = profileName; + mProfileDataChanged = PR_TRUE; } // Return the current profile value. // If mCurrent profile is already set, that value is returned. // If there is only one profile that value is set to CurrentProfile. void -nsProfileAccess::GetCurrentProfile(char **profileName) +nsProfileAccess::GetCurrentProfile(PRUnichar **profileName) { + *profileName = nsnull; - *profileName = nsnull; + if (!mCurrentProfile.IsEmpty() || mForgetProfileCalled) + { + *profileName = mCurrentProfile.ToNewUnicode(); + } - if (mCurrentProfile) - { - if ((PL_strcmp(mCurrentProfile,"") != 0) || mForgetProfileCalled) - { - *profileName = nsCRT::strdup(mCurrentProfile); - } - } - - // If there are profiles and profileName is not - // set yet. Get the first one and set it as Current Profile. - if (mNumProfiles > 0 && (*profileName == nsnull)) - { - GetFirstProfile(profileName); - SetCurrentProfile(*profileName); - } + // If there are profiles and profileName is not + // set yet. Get the first one and set it as Current Profile. + if (mNumProfiles > 0 && (*profileName == nsnull)) + { + GetFirstProfile(profileName); + SetCurrentProfile(*profileName); + } } // Delete a profile from profile structs void -nsProfileAccess::RemoveSubTree(const char* profileName) +nsProfileAccess::RemoveSubTree(const PRUnichar* profileName) { - // delete this entry from the mProfiles array - // by moving the pointers with something like memmove - // decrement mCount if it works. - PRInt32 index = 0; - PRBool isOldProfile = PR_FALSE; + NS_ASSERTION(profileName, "Invalid profile name"); - index = FindProfileIndex(profileName); + // delete this entry from the mProfiles array + // by moving the pointers with something like memmove + // decrement mCount if it works. + PRInt32 index = 0; + PRBool isOldProfile = PR_FALSE; - if (index >= 0) - { - ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + index = FindProfileIndex(profileName); - if (PL_strcmp(profileItem->isMigrated, REGISTRY_NO_STRING) == 0) - isOldProfile = PR_TRUE; + if (index >= 0) + { + ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); - mProfiles->RemoveElementAt(index); + if (profileItem->isMigrated == REGISTRY_NO_STRING) + isOldProfile = PR_TRUE; - mCount--; - if (isOldProfile) - mNumOldProfiles--; - else - mNumProfiles--; + mProfiles->RemoveElementAt(index); - if (PL_strcmp(profileName, mCurrentProfile) == 0) - PL_strcpy(mCurrentProfile,""); - } + mCount--; + + if (isOldProfile) + mNumOldProfiles--; + else + mNumProfiles--; + + if (mCurrentProfile.Equals(profileName)) + { + mCurrentProfile = ""; + } + } } // Fix registry incompatabilities with previous builds void -nsProfileAccess::FixRegEntry(char** dirName) +nsProfileAccess::FixRegEntry(PRUnichar** dirName) { - nsSimpleCharString decodedDirName; - PRBool haveHexBytes = PR_TRUE; + NS_ASSERTION(dirName, "Invalid dirName pointer"); - // 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; + nsSimpleCharString decodedDirName; + PRBool haveHexBytes = PR_TRUE; - if (haveHexBytes) - { - bigBuffer[NUM_HEX_BYTES] = '\0'; + // Decode the directory name to return the ordinary string + nsInputStringStream stream(*dirName); + nsPersistentFileDescriptor descriptor; - for (int i = 0; i < NUM_HEX_BYTES; i++) - { - if (!(ISHEX(bigBuffer[i]))) - { - haveHexBytes = PR_FALSE; - break; - } - } - } + 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 (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); + if (bytesRead != NUM_HEX_BYTES) + haveHexBytes = PR_FALSE; - *dirName = nsCRT::strdup(decodedDirName); - } -} + 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); + + *dirName = nsAutoString(decodedDirName).ToNewUnicode(); + } +} + // Return the index of a given profiel from the arraf of profile structs. PRInt32 -nsProfileAccess::FindProfileIndex(const char* profileName) -{ - PRInt32 retval = -1; - PRInt32 index = 0; +nsProfileAccess::FindProfileIndex(const PRUnichar* profileName) +{ + NS_ASSERTION(profileName, "Invalid profile name"); - for (index=0; index < mCount; index++) - { - ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + PRInt32 retval = -1; + PRInt32 index = 0; - if (PL_strcmp(profileName, profileItem->profileName) == 0) - { - retval = index; - break; - } - } - return retval; + for (index=0; index < mCount; index++) + { + ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + + if(profileItem->profileName.Equals(profileName)) + { + retval = index; + break; + } + } + return retval; } // Flush profile information from the data structure to the registry. nsresult nsProfileAccess::UpdateRegistry() { - nsresult rv; + nsresult rv; - if (!mProfileDataChanged) - { - return NS_OK; - } + if (!mProfileDataChanged) + { + return NS_OK; + } rv = OpenRegistry(); if (NS_FAILED(rv)) return rv; @@ -648,172 +692,216 @@ nsProfileAccess::UpdateRegistry() nsCOMPtr enumKeys; nsRegistryKey profilesTreeKey; - // Get the major subtree - rv = m_registry->GetSubtree(nsIRegistry::Common, REGISTRY_PROFILE_SUBTREE_STRING, &profilesTreeKey); - if (NS_FAILED(rv)) return rv; + // Get the major subtree + rv = m_registry->GetKey(nsIRegistry::Common, + nsAutoString(REGISTRY_PROFILE_SUBTREE_STRING).GetUnicode(), + &profilesTreeKey); + if (NS_FAILED(rv)) return rv; - // Set the current profile - if (mCurrentProfile) - { - rv = m_registry->SetStringUTF8(profilesTreeKey, REGISTRY_CURRENT_PROFILE_STRING, mCurrentProfile); + // Set the current profile + if (!mCurrentProfile.IsEmpty()) { + + rv = m_registry->SetString(profilesTreeKey, + nsAutoString(REGISTRY_CURRENT_PROFILE_STRING).GetUnicode(), + mCurrentProfile.GetUnicode()); if (NS_FAILED(rv)) return rv; - } + } - // Set the registry version - rv = m_registry->SetStringUTF8(profilesTreeKey, REGISTRY_VERSION_STRING, mVersion); - if (NS_FAILED(rv)) return rv; - - // Set preg info - rv = m_registry->SetStringUTF8(profilesTreeKey, REGISTRY_HAVE_PREG_INFO_STRING, mHavePREGInfo); - if (NS_FAILED(rv)) return rv; - - rv = m_registry->EnumerateSubtrees(profilesTreeKey, getter_AddRefs(enumKeys)); + // Set the registry version + rv = m_registry->SetString(profilesTreeKey, + nsAutoString(REGISTRY_VERSION_STRING).GetUnicode(), + mVersion.GetUnicode()); if (NS_FAILED(rv)) return rv; - - rv = enumKeys->First(); + + // Set preg info + rv = m_registry->SetString(profilesTreeKey, + nsAutoString(REGISTRY_HAVE_PREG_INFO_STRING).GetUnicode(), + mHavePREGInfo.GetUnicode()); if (NS_FAILED(rv)) return rv; - - while( (NS_OK != enumKeys->IsDone()) ) + + rv = m_registry->EnumerateSubtrees(profilesTreeKey, getter_AddRefs(enumKeys)); + if (NS_FAILED(rv)) return rv; + + rv = enumKeys->First(); + if (NS_FAILED(rv)) return rv; + + while( (NS_OK != enumKeys->IsDone()) ) { - nsCOMPtr base; + nsCOMPtr base; + + rv = enumKeys->CurrentItem( getter_AddRefs(base) ); + if (NS_FAILED(rv)) return rv; - rv = enumKeys->CurrentItem( getter_AddRefs(base) ); - if (NS_FAILED(rv)) return rv; - // Get specific interface. nsCOMPtr node; nsIID nodeIID = NS_IREGISTRYNODE_IID; - + rv = base->QueryInterface( nodeIID, getter_AddRefs(node)); - if (NS_FAILED(rv)) return rv; - - // Get node name. - nsXPIDLCString profile; - nsXPIDLCString isMigrated; - nsXPIDLCString directory; - - rv = node->GetNameUTF8( getter_Copies(profile) ); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) return rv; - PRInt32 index = 0; + // Get node name. + nsXPIDLString profile; + nsXPIDLString isMigrated; + nsXPIDLString directory; - index = FindProfileIndex(profile); + rv = node->GetName( getter_Copies(profile) ); + if (NS_FAILED(rv)) return rv; - if (index < 0) - { - // This profile is deleted. - rv = m_registry->RemoveSubtree(profilesTreeKey, profile); - if (NS_FAILED(rv)) return rv; - } - else + PRInt32 index = 0; + + index = FindProfileIndex(profile); + + if (index < 0) { - nsRegistryKey profKey; + // This profile is deleted. + rv = m_registry->RemoveKey(profilesTreeKey, profile); + if (NS_FAILED(rv)) return rv; + } + else + { + nsRegistryKey profKey; - ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); - rv = m_registry->GetSubtree(profilesTreeKey, profile, &profKey); - if (NS_FAILED(rv)) return rv; - - rv = m_registry->SetStringUTF8(profKey, REGISTRY_DIRECTORY_STRING, profileItem->profileLocation); - if (NS_FAILED(rv)) return rv; + rv = node->GetKey(&profKey); + if (NS_FAILED(rv)) return rv; - rv = m_registry->SetStringUTF8(profKey, REGISTRY_MIGRATED_STRING, profileItem->isMigrated); - if (NS_FAILED(rv)) return rv; + rv = m_registry->SetString(profKey, + nsAutoString(REGISTRY_DIRECTORY_STRING).GetUnicode(), + profileItem->profileLocation.GetUnicode()); + if (NS_FAILED(rv)) return rv; - rv = m_registry->SetStringUTF8(profKey, REGISTRY_NC_PROFILE_NAME_STRING, profileItem->NCProfileName); - rv = m_registry->SetStringUTF8(profKey, REGISTRY_NC_SERVICE_DENIAL_STRING, profileItem->NCDeniedService); - rv = m_registry->SetStringUTF8(profKey, REGISTRY_NC_USER_EMAIL_STRING, profileItem->NCEmailAddress); - rv = m_registry->SetStringUTF8(profKey, REGISTRY_NC_HAVE_PREG_INFO_STRING, profileItem->NCHavePregInfo); + rv = m_registry->SetString(profKey, + nsAutoString(REGISTRY_MIGRATED_STRING).GetUnicode(), + profileItem->isMigrated.GetUnicode()); + if (NS_FAILED(rv)) return rv; - profileItem->updateProfileEntry = PR_FALSE; - } - rv = enumKeys->Next(); - } + rv = m_registry->SetString(profKey, + nsAutoString(REGISTRY_NC_PROFILE_NAME_STRING).GetUnicode(), + profileItem->NCProfileName.GetUnicode()); - // Take care of new nodes - for (int i = 0; i < mCount; i++) - { - ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(i)); + rv = m_registry->SetString(profKey, + nsAutoString(REGISTRY_NC_SERVICE_DENIAL_STRING).GetUnicode(), + profileItem->NCDeniedService.GetUnicode()); - if (profileItem->updateProfileEntry) - { - nsRegistryKey profKey; + rv = m_registry->SetString(profKey, + nsAutoString(REGISTRY_NC_USER_EMAIL_STRING).GetUnicode(), + profileItem->NCEmailAddress.GetUnicode()); - rv = m_registry->AddSubtree(profilesTreeKey, profileItem->profileName, &profKey); - if (NS_FAILED(rv)) return rv; + rv = m_registry->SetString(profKey, + nsAutoString(REGISTRY_NC_HAVE_PREG_INFO_STRING).GetUnicode(), + profileItem->NCHavePregInfo.GetUnicode()); - rv = m_registry->SetStringUTF8(profKey, REGISTRY_DIRECTORY_STRING, profileItem->profileLocation); - if (NS_FAILED(rv)) return rv; + profileItem->updateProfileEntry = PR_FALSE; + } + rv = enumKeys->Next(); + } - rv = m_registry->SetStringUTF8(profKey, REGISTRY_MIGRATED_STRING, profileItem->isMigrated); - if (NS_FAILED(rv)) return rv; + // Take care of new nodes + for (int i = 0; i < mCount; i++) + { + ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(i)); - rv = m_registry->SetStringUTF8(profKey, REGISTRY_NC_PROFILE_NAME_STRING, profileItem->NCProfileName); - rv = m_registry->SetStringUTF8(profKey, REGISTRY_NC_SERVICE_DENIAL_STRING, profileItem->NCDeniedService); - rv = m_registry->SetStringUTF8(profKey, REGISTRY_NC_USER_EMAIL_STRING, profileItem->NCEmailAddress); - rv = m_registry->SetStringUTF8(profKey, REGISTRY_NC_HAVE_PREG_INFO_STRING, profileItem->NCHavePregInfo); + if (profileItem->updateProfileEntry) + { + nsRegistryKey profKey; - profileItem->updateProfileEntry = PR_FALSE; - } - } + rv = m_registry->AddKey(profilesTreeKey, + profileItem->profileName.GetUnicode(), + &profKey); + if (NS_FAILED(rv)) return rv; - rv = CloseRegistry(); - mProfileDataChanged = PR_FALSE; + rv = m_registry->SetString(profKey, + nsAutoString(REGISTRY_DIRECTORY_STRING).GetUnicode(), + profileItem->profileLocation.GetUnicode()); + if (NS_FAILED(rv)) return rv; - return rv; + rv = m_registry->SetString(profKey, + nsAutoString(REGISTRY_MIGRATED_STRING).GetUnicode(), + profileItem->isMigrated.GetUnicode()); + if (NS_FAILED(rv)) return rv; + + rv = m_registry->SetString(profKey, + nsAutoString(REGISTRY_NC_PROFILE_NAME_STRING).GetUnicode(), + profileItem->NCProfileName.GetUnicode()); + + rv = m_registry->SetString(profKey, + nsAutoString(REGISTRY_NC_SERVICE_DENIAL_STRING).GetUnicode(), + profileItem->NCDeniedService.GetUnicode()); + + rv = m_registry->SetString(profKey, + nsAutoString(REGISTRY_NC_USER_EMAIL_STRING).GetUnicode(), + profileItem->NCEmailAddress.GetUnicode()); + + rv = m_registry->SetString(profKey, + nsAutoString(REGISTRY_NC_HAVE_PREG_INFO_STRING).GetUnicode(), + profileItem->NCHavePregInfo.GetUnicode()); + + profileItem->updateProfileEntry = PR_FALSE; + } + } + + rv = CloseRegistry(); + mProfileDataChanged = PR_FALSE; + + return rv; } // Return the list of profiles, 4x and 5x. // For 4x profiles text "- migrate" is appended // to inform the JavaScript about the migration status. void -nsProfileAccess::GetProfileList(char **profileListStr) +nsProfileAccess::GetProfileList(PRUnichar **profileListStr) { - nsCAutoString profileList(""); + NS_ASSERTION(profileListStr, "Invalid profileListStr pointer"); - for (PRInt32 index=0; index < mCount; index++) - { - ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + nsAutoString profileList(""); - if (index != 0) - { - profileList += ","; - } - profileList += profileItem->profileName; + for (PRInt32 index=0; index < mCount; index++) + { + ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); - if (PL_strcmp(profileItem->isMigrated, REGISTRY_NO_STRING) == 0) - profileList += " - migrate"; - } + if (index != 0) + { + profileList += ","; + } + profileList += profileItem->profileName; - *profileListStr = nsCRT::strdup((const char *)profileList); + if (profileItem->isMigrated == REGISTRY_NO_STRING) + profileList += " - migrate"; + } + + *profileListStr = profileList.ToNewUnicode(); } // Return a boolean based on the profile existence. PRBool -nsProfileAccess::ProfileExists(const char *profileName) +nsProfileAccess::ProfileExists(const PRUnichar *profileName) { - PRBool exists = PR_FALSE; + NS_ASSERTION(profileName, "Invalid profile name"); - for (PRInt32 index=0; index < mCount; index++) - { - ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); - if (PL_strcmp(profileItem->profileName, profileName) == 0) - { - exists = PR_TRUE; - break; - } + PRBool exists = PR_FALSE; + + for (PRInt32 index=0; index < mCount; index++) + { + ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + if (profileItem->profileName.Equals(profileName)) + { + exists = PR_TRUE; + break; + } } - - return exists; + return exists; } // Capture the 4x profile information from the old registry (4x) nsresult nsProfileAccess::Get4xProfileInfo(const char *registryName) { + NS_ASSERTION(registryName, "Invalid registryName"); - nsresult rv = NS_OK; - mNumOldProfiles = 0; + nsresult rv = NS_OK; + mNumOldProfiles = 0; #if defined(XP_PC) || defined(XP_MAC) nsCOMPtr oldReg; @@ -822,165 +910,167 @@ nsProfileAccess::Get4xProfileInfo(const char *registryName) NS_GET_IID(nsIRegistry), getter_AddRefs(oldReg)); if (NS_FAILED(rv)) return rv; - + rv = oldReg->Open(registryName); 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_FAILED(rv)) return rv; rv = enumKeys->First(); if (NS_FAILED(rv)) return rv; - + // Enumerate subkeys till done. while( (NS_OK != enumKeys->IsDone())) - { - nsCOMPtr base; - rv = enumKeys->CurrentItem(getter_AddRefs(base)); - if (NS_FAILED(rv)) return rv; + { + nsCOMPtr base; + rv = enumKeys->CurrentItem(getter_AddRefs(base)); + if (NS_FAILED(rv)) return rv; // Get specific interface. nsCOMPtr node; nsIID nodeIID = NS_IREGISTRYNODE_IID; rv = base->QueryInterface( nodeIID, getter_AddRefs(node)); - if (NS_FAILED(rv)) return rv; - - char *profile = nsnull; - rv = node->GetNameUTF8(&profile); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) return rv; + + nsXPIDLString profile; + rv = node->GetName(getter_Copies(profile)); + if (NS_FAILED(rv)) return rv; + + PRBool exists = PR_FALSE;; + exists = ProfileExists(profile); + if (exists) + { + rv = enumKeys->Next(); + if (NS_FAILED(rv)) return rv; + + continue; + } - PRBool exists = PR_FALSE;; - exists = ProfileExists(profile); - if (exists) - { - rv = enumKeys->Next(); - if (NS_FAILED(rv)) return rv; - - continue; - } - nsRegistryKey key; - - rv = oldReg->GetSubtree(nsIRegistry::Users, profile, &key); - if (NS_FAILED(rv)) return rv; - + rv = node->GetKey(&key); + if (NS_FAILED(rv)) return rv; + nsXPIDLCString profLoc; - + rv = oldReg->GetStringUTF8( key, "ProfileLocation", getter_Copies(profLoc)); - if (NS_FAILED(rv)) return rv; - + if (NS_FAILED(rv)) return rv; + #if defined(DEBUG_profile) - printf("oldProflie Location = %s\n", profLoc); + printf("oldProflie Location = %s\n", profLoc); #endif - - ProfileStruct* profileItem = new ProfileStruct(); + + ProfileStruct* profileItem = new ProfileStruct(); if (!profileItem) - return NS_ERROR_OUT_OF_MEMORY; + return NS_ERROR_OUT_OF_MEMORY; - profileItem->profileName = nsnull; - profileItem->profileLocation = nsnull; - profileItem->isMigrated = nsnull; - profileItem->NCProfileName = nsnull; - profileItem->NCDeniedService = nsnull; - profileItem->NCEmailAddress = nsnull; - profileItem->NCHavePregInfo = nsnull; - profileItem->updateProfileEntry = PR_TRUE; + profileItem->updateProfileEntry = PR_TRUE; - profileItem->profileName = nsCRT::strdup(nsUnescape(profile)); - profileItem->profileLocation = nsCRT::strdup(profLoc); - profileItem->isMigrated = nsCRT::strdup(REGISTRY_NO_STRING); + // Unescape is done on the profileName to interpret special characters like %, _ etc. + // For example something like %20 would probably be interpreted as a space + // There is some problem I guess in sending a space as itself + + nsAutoString unescpPrfName(nsUnescape + (NS_CONST_CAST + (char*, nsCAutoString(profile).GetBuffer()))); + + profileItem->profileName = unescpPrfName; + + profileItem->profileLocation = nsAutoString(profLoc); + profileItem->isMigrated = REGISTRY_NO_STRING; if (!m4xProfiles) - m4xProfiles = new nsVoidArray(); + m4xProfiles = new nsVoidArray(); + m4xProfiles->AppendElement((void*)profileItem); mNumOldProfiles++; + //delete profileItem; + rv = enumKeys->Next(); if (NS_FAILED(rv)) return rv; } #elif defined (XP_BEOS) #else - /* XP_UNIX */ - char *unixProfileName = PR_GetEnv(PROFILE_NAME_ENVIRONMENT_VARIABLE); - char *unixProfileDirectory = PR_GetEnv(PROFILE_HOME_ENVIRONMENT_VARIABLE); +/* 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); - } + if (!unixProfileName || + !unixProfileDirectory || + (PL_strlen(unixProfileName) == 0) || + (PL_strlen(unixProfileDirectory) == 0)) + { + unixProfileName = PR_GetEnv(USER_ENVIRONMENT_VARIABLE); + unixProfileDirectory = PR_GetEnv(HOME_ENVIRONMENT_VARIABLE); + } - PRBool exists = PR_FALSE; - exists = ProfileExists(unixProfileName); - if (exists) - { - return NS_OK; - } + PRBool exists = PR_FALSE;; + exists = ProfileExists(nsAutoString(unixProfileName).GetUnicode()); + if (exists) + { + return NS_OK; + } - if (unixProfileName && unixProfileDirectory) { - nsCAutoString profileLocation(unixProfileDirectory); - profileLocation += "/.netscape"; - - nsCOMPtr users4xDotNetscapeDirectory; - rv = NS_NewFileSpec(getter_AddRefs(users4xDotNetscapeDirectory)); - if (NS_FAILED(rv)) return rv; - - rv = users4xDotNetscapeDirectory->SetNativePath((const char *)profileLocation); - if (NS_FAILED(rv)) return rv; + if (unixProfileName && unixProfileDirectory) { + nsCAutoString profileLocation(unixProfileDirectory); + profileLocation += "/.netscape"; + nsCOMPtr users4xDotNetscapeDirectory; - rv = users4xDotNetscapeDirectory->Exists(&exists); - if (NS_FAILED(rv)) return rv; + rv = NS_NewFileSpec(getter_AddRefs(users4xDotNetscapeDirectory)); + if (NS_FAILED(rv)) return rv; + + rv = users4xDotNetscapeDirectory->SetNativePath((const char *)profileLocation); + if (NS_FAILED(rv)) return rv; + rv = users4xDotNetscapeDirectory->Exists(&exists); + + if (NS_FAILED(rv)) return rv; #ifdef DEBUG - printf("%s exists: %d\n",(const char *)profileLocation, exists); + printf("%s exists: %d\n",profileLocation.GetBuffer(), exists); #endif - if (exists) { - ProfileStruct* profileItem = new ProfileStruct(); - if (!profileItem) - return NS_ERROR_OUT_OF_MEMORY; + if (exists) { + ProfileStruct* profileItem = new ProfileStruct(); + if (!profileItem) + return NS_ERROR_OUT_OF_MEMORY; + profileItem->updateProfileEntry = PR_TRUE; - profileItem->profileName = nsnull; - profileItem->profileLocation = nsnull; - profileItem->isMigrated = nsnull; - profileItem->NCProfileName = nsnull; - profileItem->NCDeniedService = nsnull; - profileItem->NCEmailAddress = nsnull; - profileItem->NCHavePregInfo = nsnull; - profileItem->updateProfileEntry = PR_TRUE; - - profileItem->profileName = nsCRT::strdup(nsUnescape(unixProfileName)); - profileItem->profileLocation = nsCRT::strdup((const char *)profileLocation); - - profileItem->isMigrated = nsCRT::strdup(REGISTRY_NO_STRING); - - if (!m4xProfiles) - m4xProfiles = new nsVoidArray(); - m4xProfiles->AppendElement((void*)profileItem); - - mNumOldProfiles++; - } - else { + profileItem->profileName = nsAutoString(nsUnescape(unixProfileName)).ToNewUnicode(); + profileItem->profileLocation = nsAutoString(profileLocation).ToNewUnicode(); + profileItem->isMigrated = nsAutoString(REGISTRY_NO_STRING).ToNewUnicode(); + + if (!m4xProfiles) + m4xProfiles = new nsVoidArray(); + + m4xProfiles->AppendElement((void*)profileItem); + + mNumOldProfiles++; + + //delete profileItem; + } + else { #ifdef DEBUG - printf("no 4.x profile\n"); + printf("no 4.x profile\n"); #endif + } } - } -#endif - - m4xCount = mNumOldProfiles; +#endif /* XP_UNIX */ + + m4xCount = mNumOldProfiles; if (m4xCount > 0) { UpdateProfileArray(); - } + } - return rv; + return rv; } // Update the mozregistry with the 4x profile names @@ -989,71 +1079,73 @@ nsProfileAccess::Get4xProfileInfo(const char *registryName) nsresult nsProfileAccess::UpdateProfileArray() { - nsresult rv = NS_OK; + nsresult rv = NS_OK; - for (PRInt32 idx = 0; idx < m4xCount; idx++) - { - ProfileStruct* profileItem = (ProfileStruct *) (m4xProfiles->ElementAt(idx)); - nsFileSpec profileDir(profileItem->profileLocation); + for (PRInt32 idx = 0; idx < m4xCount; idx++) + { + ProfileStruct* profileItem = (ProfileStruct *) (m4xProfiles->ElementAt(idx)); + nsFileSpec profileDir(profileItem->profileLocation); + + PRBool exists; + exists = ProfileExists(profileItem->profileName.GetUnicode()); + if (NS_FAILED(rv)) return rv; + + // That profile already exists... + // move on..... + if (exists) { + continue; + } - PRBool exists; - exists = ProfileExists(profileItem->profileName); - if (NS_FAILED(rv)) return rv; - - // That profile already exists... - // move on..... - if (exists) { - continue; - } - nsXPIDLCString profileDirString; nsCOMPtrspec; rv = NS_NewFileSpecWithSpec(profileDir, getter_AddRefs(spec)); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) return rv; + + rv = spec->GetPersistentDescriptorString(getter_Copies(profileDirString)); - rv = spec->GetPersistentDescriptorString(getter_Copies(profileDirString)); - if (NS_SUCCEEDED(rv) && profileDirString) { - - PRInt32 length = PL_strlen(profileDirString); - profileItem->profileLocation = (char *) PR_Realloc(profileItem->profileLocation, length+1); - - PL_strcpy(profileItem->profileLocation, profileDirString); - - SetValue(profileItem); - } - } - - mProfileDataChanged = PR_TRUE; - - return rv; + profileItem->profileLocation = profileDirString; + SetValue(profileItem); + } + } + mProfileDataChanged = PR_TRUE; + return rv; } // Set the PREG flag to indicate if that info exists void nsProfileAccess::SetPREGInfo(const char* pregInfo) { - mHavePREGInfo = nsCRT::strdup(pregInfo); + NS_ASSERTION(pregInfo, "Invalid pregInfo"); + + // This is always going to be just a yes/no string + mHavePREGInfo = pregInfo; } //Get the for PREG info. void -nsProfileAccess::GetPREGInfo(const char *profileName, char **info) +nsProfileAccess::GetPREGInfo(const PRUnichar *profileName, char **info) { - *info = nsnull; + NS_ASSERTION(profileName, "Invalid profile name"); + NS_ASSERTION(info, "Invalid info pointer"); + + *info = nsnull; PRInt32 index = 0; index = FindProfileIndex(profileName); - if (index >= 0 ) - { - ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); - - if (profileItem->NCHavePregInfo) - *info = nsCRT::strdup(profileItem->NCHavePregInfo); - else + if (index >= 0 ) + { + ProfileStruct* profileItem = (ProfileStruct *) (mProfiles->ElementAt(index)); + + if (!profileItem->NCHavePregInfo.IsEmpty()) { + *info = nsCRT::strdup(NS_CONST_CAST + (char*, nsCAutoString + (profileItem->NCHavePregInfo).GetBuffer())); + } + else *info = nsCRT::strdup(REGISTRY_NO_STRING); - } + } } diff --git a/mozilla/profile/src/nsProfileAccess.h b/mozilla/profile/src/nsProfileAccess.h index 1a574743795..6cd0a2a2d69 100644 --- a/mozilla/profile/src/nsProfileAccess.h +++ b/mozilla/profile/src/nsProfileAccess.h @@ -26,69 +26,96 @@ #include "nsXPIDLString.h" #include "nsVoidArray.h" -typedef struct _profile_struct { - char* profileName; - char* profileLocation; - char* isMigrated; - char* NCProfileName; - char* NCDeniedService; - char* NCEmailAddress; - char* NCHavePregInfo; - PRBool updateProfileEntry; -}ProfileStruct; +class ProfileStruct +{ +public: + nsString profileName; + nsString profileLocation; + nsString isMigrated; + nsString NCProfileName; + nsString NCDeniedService; + nsString NCEmailAddress; + nsString NCHavePregInfo; + PRBool updateProfileEntry; + +private: + // prevent inadvertent copies and assignments + //ProfileStruct& operator=(const ProfileStruct& rhs); + //ProfileStruct(const ProfileStruct& rhs); +}; class nsProfileAccess { private: - nsCOMPtr m_registry; + nsCOMPtr m_registry; - nsVoidArray *mProfiles; - PRInt32 mCount; - char* mCurrentProfile; - char* mVersion; - PRBool mFixRegEntries; - char* mHavePREGInfo; - PRInt32 m4xCount; + // This is an array that holds all the profile information--migrated/unmigrated + // unmigrated: if the profileinfo is migrated--i.e. -installer option is used + nsVoidArray* mProfiles; - nsresult OpenRegistry(); - nsresult CloseRegistry(); + // Represents the size of the mProfiles array + // This value keeps changing as profiles are created/deleted/migrated + PRInt32 mCount; + + nsString mCurrentProfile; + nsString mVersion; + PRBool mFixRegEntries; + nsString mHavePREGInfo; + + // Represents the size of the m4xProfiles array + // This value gets set after the profile information is migrated + // and does not change subsequently. + PRInt32 m4xCount; + + + nsresult OpenRegistry(); + nsresult CloseRegistry(); + + // It looks like mCount and m4xCount are not required. + // But retaining them for now to avoid some problems. + // Will re-evaluate them in future. public: - PRBool mProfileDataChanged; - PRBool mForgetProfileCalled; - PRInt32 mNumProfiles; - PRInt32 mNumOldProfiles; - nsVoidArray *m4xProfiles; + PRBool mProfileDataChanged; + PRBool mForgetProfileCalled; - nsProfileAccess(); - virtual ~nsProfileAccess(); + // This is the num of 5.x profiles at any given time + PRInt32 mNumProfiles; - nsresult SetValue(ProfileStruct* aProfile); - nsresult FillProfileInfo(); + // This is the num of 4.x profiles at any given time + PRInt32 mNumOldProfiles; - void GetNumProfiles(PRInt32 *numProfiles); - void GetNum4xProfiles(PRInt32 *numProfiles); - void GetFirstProfile(char **firstProfile); + nsVoidArray* m4xProfiles; - void SetCurrentProfile(const char *profileName); - void GetCurrentProfile(char **profileName); + nsProfileAccess(); + virtual ~nsProfileAccess(); - void RemoveSubTree(const char* profileName); - void FixRegEntry(char** dirName); + nsresult SetValue(ProfileStruct* aProfile); + nsresult FillProfileInfo(); - nsresult HavePregInfo(char **info); - nsresult GetValue(const char* profileName, ProfileStruct** aProfile); - PRInt32 FindProfileIndex(const char* profileName); + void GetNumProfiles(PRInt32 *numProfiles); + void GetNum4xProfiles(PRInt32 *numProfiles); + void GetFirstProfile(PRUnichar **firstProfile); - nsresult UpdateRegistry(); - void GetProfileList(char **profileListStr); - PRBool ProfileExists(const char *profileName); - nsresult Get4xProfileInfo(const char *registryName); - nsresult UpdateProfileArray(); - void SetPREGInfo(const char* pregInfo); - void GetPREGInfo(const char *profileName, char** pregInfo); - void FreeProfileMembers(nsVoidArray *aProfile, PRInt32 numElems); + void SetCurrentProfile(const PRUnichar *profileName); + void GetCurrentProfile(PRUnichar **profileName); + + void RemoveSubTree(const PRUnichar* profileName); + void FixRegEntry(PRUnichar** dirName); + + nsresult HavePregInfo(char **info); + nsresult GetValue(const PRUnichar* profileName, ProfileStruct** aProfile); + PRInt32 FindProfileIndex(const PRUnichar* profileName); + + nsresult UpdateRegistry(); + void GetProfileList(PRUnichar **profileListStr); + PRBool ProfileExists(const PRUnichar *profileName); + nsresult Get4xProfileInfo(const char *registryName); + nsresult UpdateProfileArray(); + void SetPREGInfo(const char* pregInfo); + void GetPREGInfo(const PRUnichar *profileName, char** pregInfo); + void FreeProfileMembers(nsVoidArray *aProfile, PRInt32 numElems); }; #endif // __nsProfileAccess_h___