bug 268266 : profile and appstartup code should use NS_CopyNativeToUnicode (r=cbie, sr=darin)

This commit is contained in:
jshin%mailaps.org 2004-11-16 18:07:23 +00:00
Родитель b8230b567b
Коммит 12ec7e7c7f
6 изменённых файлов: 39 добавлений и 190 удалений

Просмотреть файл

@ -62,7 +62,6 @@ REQUIRES = xpcom \
appshell \
appcomps \
chrome \
uconv \
windowwatcher \
profdirserviceprovider \
$(NULL)

Просмотреть файл

@ -54,6 +54,7 @@
#include "nsXPIDLString.h"
#include "nsEscape.h"
#include "nsIURL.h"
#include "nsNativeCharsetUtils.h"
#include "prprf.h"
@ -737,19 +738,8 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs,
if (cmdResult) {
foundProfileCommandArg = PR_TRUE;
nsAutoString currProfileName;
if (nsCRT::IsAscii(cmdResult)) {
currProfileName.AssignWithConversion(cmdResult);
}
else {
// get a platform charset
nsCAutoString charSet;
rv = GetPlatformCharset(charSet);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to get a platform charset");
// convert the profile name to Unicode
rv = ConvertStringToUnicode(charSet, cmdResult, currProfileName);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to convert ProfileName to unicode");
}
rv = NS_CopyNativeToUnicode(cmdResult, currProfileName);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to convert ProfileName to unicode");
#ifdef DEBUG_profile
printf("ProfileName : %s\n", (const char*)cmdResult);
@ -806,24 +796,16 @@ nsProfile::ProcessArgs(nsICmdLineService *cmdLineArgs,
foundProfileCommandArg = PR_TRUE;
nsAutoString currProfileName;
if (nsCRT::IsAscii(cmdResult)) {
currProfileName.AssignWithConversion(strtok(cmdResult.BeginWriting(), " "));
}
else {
// get a platform charset
nsCAutoString charSet;
rv = GetPlatformCharset(charSet);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to get a platform charset");
char *tmpStr;
rv = NS_CopyNativeToUnicode(
nsDependentCString(nsCRT::strtok(cmdResult.BeginWriting(), " ", &tmpStr)),
currProfileName);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to convert ProfileName to unicode");
// convert the profile name to Unicode
nsCAutoString profileName(strtok(cmdResult.BeginWriting(), " "));
rv = ConvertStringToUnicode(charSet, profileName.get(), currProfileName);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to convert ProfileName to unicode");
}
nsAutoString currProfileDirString; currProfileDirString.AssignWithConversion(strtok(NULL, " "));
if (!currProfileDirString.IsEmpty()) {
rv = NS_NewLocalFile(currProfileDirString, PR_TRUE, getter_AddRefs(currProfileDir));
char *currProfileDirString = nsCRT::strtok(tmpStr, " ", &tmpStr);
if (currProfileDirString && *currProfileDirString) {
rv = NS_NewNativeLocalFile(nsDependentCString(currProfileDirString),
PR_TRUE, getter_AddRefs(currProfileDir));
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
}
else {
@ -1678,8 +1660,9 @@ nsProfile::CreateNewProfileWithLocales(const PRUnichar* profileName,
do_GetService(NS_CHROMEREGISTRY_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv)) {
nsCAutoString uiLocale; uiLocale.AssignWithConversion(aUILocale);
nsCAutoString contentLocale; contentLocale.AssignWithConversion(aContentLocale);
nsCAutoString uiLocale, contentLocale;
LossyCopyUTF16toASCII(aUILocale, uiLocale);
LossyCopyUTF16toASCII(aContentLocale, contentLocale);
// When aUILocale == null or aContentLocale == null, set those
// from default values which are from default or from command
@ -1696,7 +1679,7 @@ nsProfile::CreateNewProfileWithLocales(const PRUnichar* profileName,
// is done in nsAppRunner.cpp::InstallGlobalLocale()
nsCOMPtr<nsIChromeRegistrySea> packageRegistry = do_QueryInterface(chromeRegistry);
if ((!aUILocale || !aUILocale[0]) && packageRegistry) {
if (uiLocale.IsEmpty() && packageRegistry) {
nsCAutoString currentUILocaleName;
rv = packageRegistry->GetSelectedLocale(NS_LITERAL_CSTRING("global"),
currentUILocaleName);
@ -1705,7 +1688,7 @@ nsProfile::CreateNewProfileWithLocales(const PRUnichar* profileName,
}
}
if (!aContentLocale || !aContentLocale[0]) {
if (contentLocale.IsEmpty()) {
nsCAutoString currentContentLocaleName;
rv = packageRegistry->GetSelectedLocale(NS_LITERAL_CSTRING("global-region"),
currentContentLocaleName);

Просмотреть файл

@ -117,6 +117,4 @@ public:
nsresult ShowProfileWizard(void);
};
extern nsresult ConvertStringToUnicode(nsCString& aCharset, const char* inString, nsAString& outString);
extern nsresult GetPlatformCharset(nsCString& aCharset);

Просмотреть файл

@ -54,6 +54,7 @@
#include "nsAppDirectoryServiceDefs.h"
#include "nsILocalFile.h"
#include "nsReadableUtils.h"
#include "nsNativeCharsetUtils.h"
#if defined(XP_MAC) || defined(XP_MACOSX)
#include <Processes.h>
@ -74,10 +75,6 @@
#include <rmsdef.h>
#endif
#include "nsICharsetConverterManager.h"
#include "nsIPlatformCharset.h"
#if defined (XP_UNIX)
#define USER_ENVIRONMENT_VARIABLE "USER"
#define LOGNAME_ENVIRONMENT_VARIABLE "LOGNAME"
@ -91,9 +88,6 @@
#elif defined (XP_BEOS)
#endif
// IID and CIDs of all the services needed
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
// Registry Keys
#define kRegistryYesString (NS_LITERAL_STRING("yes"))
@ -150,63 +144,6 @@ nsProfileAccess::~nsProfileAccess()
FreeProfileMembers(mProfiles);
}
// A wrapper function to call the interface to get a platform file charset.
nsresult
GetPlatformCharset(nsCString& aCharset)
{
nsresult rv;
// we may cache it since the platform charset will not change through application life
nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv) && platformCharset) {
rv = platformCharset->GetCharset(kPlatformCharsetSel_FileName, aCharset);
}
if (NS_FAILED(rv)) {
aCharset.AssignLiteral("ISO-8859-1"); // use ISO-8859-1 in case of any error
}
return rv;
}
// Apply a charset conversion from the given charset to Unicode for input C string.
nsresult
ConvertStringToUnicode(nsCString& aCharset, const char* inString, nsAString& outString)
{
nsresult rv;
// convert result to unicode
nsCOMPtr<nsICharsetConverterManager> ccm =
do_GetService(kCharsetConverterManagerCID, &rv);
if(NS_SUCCEEDED(rv)) {
nsCOMPtr <nsIUnicodeDecoder> decoder; // this may be cached
rv = ccm->GetUnicodeDecoderRaw(aCharset.get(), getter_AddRefs(decoder));
if(NS_SUCCEEDED(rv) && decoder) {
PRInt32 uniLength = 0;
PRInt32 srcLength = strlen(inString);
rv = decoder->GetMaxLength(inString, srcLength, &uniLength);
if (NS_SUCCEEDED(rv)) {
PRUnichar *unichars = new PRUnichar [uniLength];
if (nsnull != unichars) {
// convert to unicode
rv = decoder->Convert(inString, &srcLength, unichars, &uniLength);
if (NS_SUCCEEDED(rv)) {
// Pass back the unicode string
outString.Assign(unichars, uniLength);
}
delete [] unichars;
}
else {
rv = NS_ERROR_OUT_OF_MEMORY;
}
}
}
}
return rv;
}
// Free up the member profile structs
void
nsProfileAccess::FreeProfileMembers(nsVoidArray *profiles)
@ -1074,10 +1011,6 @@ nsProfileAccess::Get4xProfileInfo(nsIFile *registryFile, PRBool fromImport)
if (fromImport && m4xProfilesAdded)
return rv;
nsCAutoString charSet;
rv = GetPlatformCharset(charSet);
if (NS_FAILED(rv)) return rv;
#if defined(XP_WIN) || defined(XP_OS2) || defined(XP_MAC) || defined(XP_MACOSX)
NS_ENSURE_ARG(registryFile);
@ -1128,11 +1061,11 @@ nsProfileAccess::Get4xProfileInfo(nsIFile *registryFile, PRBool fromImport)
nsCAutoString profileName(nsUnescape(temp.BeginWriting()));
NS_ConvertUTF8toUTF16 convertedProfName(profileName);
#else
nsCAutoString temp; temp.AssignWithConversion(profile);
nsCAutoString temp;
NS_ConvertUnicodeToNative(profile, temp);
nsCAutoString profileName(nsUnescape(temp.BeginWriting()));
nsAutoString convertedProfName;
ConvertStringToUnicode(charSet, profileName.get(), convertedProfName);
NS_CopyNativeToUnicode(profileName, convertedProfName);
#endif
PRBool exists = PR_FALSE;
@ -1188,7 +1121,9 @@ nsProfileAccess::Get4xProfileInfo(nsIFile *registryFile, PRBool fromImport)
PRBool exists = PR_FALSE;
if (!fromImport) {
exists = ProfileExists(NS_ConvertASCIItoUCS2(unixProfileName).get());
nsAutoString profileNameUTF16;
NS_CopyNativeToUnicode(unixProfileName, profileNameUTF16);
exists = ProfileExists(profileNameUTF16.get());
if (exists)
{
return NS_OK;
@ -1215,7 +1150,7 @@ nsProfileAccess::Get4xProfileInfo(nsIFile *registryFile, PRBool fromImport)
profileItem->updateProfileEntry = PR_TRUE;
CopyASCIItoUTF16(unixProfileName, profileItem->profileName);
NS_CopyNativeToUnicode(unixProfileName, profileItem->profileName);
nsCOMPtr<nsILocalFile> localFile;
rv = NS_NewNativeLocalFile(profileLocation, PR_TRUE, getter_AddRefs(localFile));
@ -1418,21 +1353,8 @@ nsresult ProfileStruct::InternalizeLocation(nsIRegistry *aRegistry, nsRegistryKe
NS_ConvertUTF16toUTF8 tempLoc(profLoc);
nsCAutoString profileLocation(nsUnescape(tempLoc.BeginWriting()));
NS_ConvertUTF8toUTF16 convertedProfLoc(profileLocation);
#else
nsCAutoString charSet;
rv = GetPlatformCharset(charSet);
if (NS_FAILED(rv)) return rv;
// Unescape profile location and convert it to the right format
nsCAutoString tempLoc; tempLoc.AssignWithConversion(profLoc);
nsCAutoString profileLocation(nsUnescape(tempLoc.BeginWriting()));
nsAutoString convertedProfLoc;
ConvertStringToUnicode(charSet, profileLocation.get(), convertedProfLoc);
#endif
// Now we have a unicode path - make it into a file
#if defined(XP_MACOSX)
// This is an HFS style path, which can't be used with nsIFile, so convert it.
rv = NS_ERROR_FAILURE;
CFStringRef pathStrRef = ::CFStringCreateWithCharacters(NULL,
@ -1453,7 +1375,13 @@ nsresult ProfileStruct::InternalizeLocation(nsIRegistry *aRegistry, nsRegistryKe
::CFRelease(pathStrRef);
}
#else
rv = NS_NewLocalFile(convertedProfLoc, PR_TRUE, getter_AddRefs(tempLocal));
// Unescape profile location and convert it to the right format
nsCAutoString tempLoc;
rv = NS_CopyUnicodeToNative(profLoc, tempLoc);
NS_ASSERTION(NS_SUCCEEDED(rv),
"failed to convert profile location to native encoding");
nsCAutoString profileLocation(nsUnescape(tempLoc.BeginWriting()));
rv = NS_NewNativeLocalFile(profileLocation, PR_TRUE, getter_AddRefs(tempLocal));
#endif
}
else
@ -1552,7 +1480,7 @@ nsresult ProfileStruct::InternalizeMigratedFromLocation(nsIRegistry *aRegistry,
migratedFrom = tempLocal;
}
#else
rv = NS_NewLocalFile(NS_ConvertUTF8toUCS2(regData), PR_TRUE, getter_AddRefs(tempLocal));
rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(regData), PR_TRUE, getter_AddRefs(tempLocal));
if (NS_SUCCEEDED(rv))
migratedFrom = tempLocal;
#endif

Просмотреть файл

@ -63,7 +63,6 @@ REQUIRES = \
intl \
profile \
util \
uconv \
$(NULL)
CPPSRCS = \

Просмотреть файл

@ -42,7 +42,6 @@
#include "nsAppStartup.h"
#include "nsIAppShellService.h"
#include "nsICharsetConverterManager.h"
#include "nsICloseAllWindows.h"
#include "nsIDOMWindowInternal.h"
#include "nsIEventQueue.h"
@ -50,7 +49,6 @@
#include "nsIInterfaceRequestor.h"
#include "nsILocalFile.h"
#include "nsIObserverService.h"
#include "nsIPlatformCharset.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsIProfileChangeStatus.h"
@ -59,12 +57,12 @@
#include "nsIStringBundle.h"
#include "nsISupportsPrimitives.h"
#include "nsITimelineService.h"
#include "nsIUnicodeDecoder.h"
#include "nsIWebBrowserChrome.h"
#include "nsIWebShellWindow.h"
#include "nsIWindowMediator.h"
#include "nsIWindowWatcher.h"
#include "nsIXULWindow.h"
#include "nsNativeCharsetUtils.h"
#include "prprf.h"
#include "nsCRT.h"
@ -75,9 +73,6 @@
NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
// Static Function Prototypes
static nsresult ConvertToUnicode(nsCString& aCharset, const char* inString, nsAString& outString);
//
// nsAppStartup
//
@ -828,30 +823,11 @@ nsAppStartup::OpenBrowserWindow(PRInt32 height, PRInt32 width)
#endif /* DEBUG_CMD_LINE */
nsAutoString url;
if (nsCRT::IsAscii(urlToLoad)) {
CopyASCIItoUTF16(urlToLoad, url);
}
else {
// get a platform charset
nsCAutoString charSet;
nsCOMPtr <nsIPlatformCharset> platformCharset(do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv));
if (NS_FAILED(rv)) {
NS_ERROR("Failed to get a platform charset");
return rv;
}
rv = platformCharset->GetCharset(kPlatformCharsetSel_FileName, charSet);
if (NS_FAILED(rv)) {
NS_ERROR("Failed to get a charset");
return rv;
}
// convert the cmdLine URL to Unicode
rv = ConvertToUnicode(charSet, urlToLoad, url);
if (NS_FAILED(rv)) {
NS_ASSERTION(0, "Failed to convert commandline url to unicode");
return rv;
}
// convert the cmdLine URL to Unicode
rv = NS_CopyNativeToUnicode(nsDependentCString(urlToLoad), url);
if (NS_FAILED(rv)) {
NS_ERROR("Failed to convert commandline url to unicode");
return rv;
}
rv = OpenWindow(chromeUrlForTask, url, width, height);
@ -1018,37 +994,3 @@ nsAppStartup::Observe(nsISupports *aSubject,
return NS_OK;
}
static nsresult
ConvertToUnicode(nsCString& aCharset, const char* inString, nsAString& outString)
{
nsresult rv;
// convert result to unicode
nsCOMPtr<nsICharsetConverterManager> ccm(do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID , &rv));
if (NS_FAILED(rv))
return rv;
nsCOMPtr <nsIUnicodeDecoder> decoder;
rv = ccm->GetUnicodeDecoderRaw(aCharset.get(), getter_AddRefs(decoder));
if (NS_FAILED(rv))
return rv;
PRInt32 uniLength = 0;
PRInt32 srcLength = strlen(inString);
rv = decoder->GetMaxLength(inString, srcLength, &uniLength);
if (NS_FAILED(rv))
return rv;
outString.SetLength(uniLength);
nsWritingIterator<PRUnichar> unichars;
outString.BeginWriting(unichars);
// convert to unicode
rv = decoder->Convert(inString, &srcLength, unichars.get(), &uniLength);
if (NS_SUCCEEDED(rv)) {
// Pass back the unicode string
outString.Assign(unichars.get(), uniLength);
}
return rv;
}