nsCOMPtr<nsIFile> profileDir; PRBool exists;
NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv);  if (NS_FAILED(rv) || !locator) return NS_ERROR_FAILURE;  nsString data(charData);
nsProfile::CreateNewProfile(const PRUnichar* profileName,                             const PRUnichar* nativeProfileDir,                            const PRUnichar* langcode,                            PRBool useExistingDir){    NS_ENSURE_ARG_POINTER(profileName);       nsresult rv = NS_OK;#if defined(DEBUG_profile)    {      printf("ProfileManager : CreateNewProfile\n");      nsCAutoString temp1; temp1.AssignWithConversion(profileName);      printf("Profile Name: %s\n", NS_STATIC_CAST(const char*, temp1));      if (nativeProfileDir) {      nsCAutoString temp2; temp2.AssignWithConversion(nativeProfileDir);      printf("Profile Dir: %s\n", NS_STATIC_CAST(const char*, temp2));    }    }#endif    nsCOMPtr<nsIFile> profileDir;    PRBool exists;        if (!nativeProfileDir)    {        // They didn't specify a directory path...                        rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILES_ROOT_DIR, getter_AddRefs(profileDir));        if (NS_FAILED(rv)) return rv;        rv = profileDir->Exists(&exists);        if (NS_FAILED(rv)) return rv;                if (!exists)            profileDir->Create(nsIFile::DIRECTORY_TYPE, 0775);        // append profile name        profileDir->AppendUnicode(profileName);    }    else {            rv = NS_NewUnicodeLocalFile(nativeProfileDir, PR_TRUE, (nsILocalFile **)((nsIFile **)getter_AddRefs(profileDir)));        // this prevents people from choosing there profile directory        // or another directory, and remove it when they delete the profile.        // append profile name        profileDir->AppendUnicode(profileName);    }    // Make profile directory unique only when the user     // decides to not use an already existing profile directory    if (!useExistingDir) {        nsXPIDLCString  suggestedName;        profileDir->GetLeafName(getter_Copies(suggestedName));        rv = profileDir->CreateUnique(suggestedName, nsIFile::DIRECTORY_TYPE, 0775);        if (NS_FAILED(rv)) return rv;    }#if defined(DEBUG_profile_verbose)    printf("before SetProfileDir\n");#endif    rv = profileDir->Exists(&exists);    if (NS_FAILED(rv)) return rv;            if (!exists)    {        rv = profileDir->Create(nsIFile::DIRECTORY_TYPE, 0775);        if (NS_FAILED(rv)) return rv;        useExistingDir = PR_FALSE;    }    // since the directory didn't exist, add the indirection    rv = AddLevelOfIndirection(profileDir);    if (NS_FAILED(rv)) return rv;    // Set the directory value and add the entry to the registry tree.    // Creates required user directories.    rv = SetProfileDir(profileName, profileDir);#if defined(DEBUG_profile_verbose)    printf("after SetProfileDir\n");#endif    // Get profile defaults folder..    nsCOMPtr <nsIFile> profDefaultsDir;    rv = NS_GetSpecialDirectory(NS_APP_PROFILE_DEFAULTS_NLOC_50_DIR, getter_AddRefs(profDefaultsDir));    if (NS_FAILED(rv)) return rv;    if (langcode && nsCRT::strlen(langcode) != 0) {        // caller prefers locale subdir        nsCOMPtr<nsIFile> locProfDefaultsDir;        rv = profDefaultsDir->Clone(getter_AddRefs(locProfDefaultsDir));        if (NS_FAILED(rv)) return rv;                locProfDefaultsDir->AppendUnicode(langcode);        rv = locProfDefaultsDir->Exists(&exists);        if (NS_SUCCEEDED(rv) && exists) {            profDefaultsDir = locProfDefaultsDir; // transfers ownership        }        nsCOMPtr<nsIChromeRegistry> chromeRegistry = do_GetService(kChromeRegistryCID, &rv);        if (NS_SUCCEEDED(rv)) {            nsXPIDLCString  pathBuf;            rv = profileDir->GetPath(getter_Copies(pathBuf));            NS_ENSURE_SUCCESS(rv, rv);            nsFileSpec fileSpec((const char *)pathBuf);            nsFileURL  fileURL(fileSpec);            const char* fileStr = fileURL.GetURLString();            rv = chromeRegistry->SelectLocaleForProfile(langcode,                                                         NS_ConvertUTF8toUCS2(fileStr));        }    }    // Copy contents from defaults folder.    rv = profDefaultsDir->Exists(&exists);    if (NS_SUCCEEDED(rv) && exists && (!useExistingDir))    {        RecursiveCopy(profDefaultsDir, profileDir);    }    gProfileDataAccess->mNumProfiles++;    gProfileDataAccess->mProfileDataChanged = PR_TRUE;    gProfileDataAccess->UpdateRegistry(nsnull);        return NS_OK;}	