Bug 457737 - Convert nsRegisterGREWin to Wide calls r=bsmedberg
authorBrad Lassey <blassey@mozilla.com>
Mon, 20 Oct 2008 16:15:32 -0400
changeset 20681 2fccc2d253e25508871fb95c6db9e5b706966dd4
parent 20680 482d2613d1e0084e33360ae8b77b6c371df9a7b6
child 20682 81e9c4b9525bdeee471f1c58c4c778ef82c58ce6
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs457737
milestone1.9.1b2pre
Bug 457737 - Convert nsRegisterGREWin to Wide calls r=bsmedberg
xulrunner/app/nsRegisterGREWin.cpp
--- a/xulrunner/app/nsRegisterGREWin.cpp
+++ b/xulrunner/app/nsRegisterGREWin.cpp
@@ -44,120 +44,128 @@
 #include "nsAppRunner.h" // for MAXPATHLEN
 #include "nsStringAPI.h"
 #include "nsXPCOMGlue.h"
 #include "nsCOMPtr.h"
 
 #include "prio.h"
 
 #include <windows.h>
+#include "malloc.h"
 
-static const char kRegKeyRoot[] = "Software\\mozilla.org\\GRE";
-static const char kRegFileGlobal[] = "global.reginfo";
-static const char kRegFileUser[] = "user.reginfo";
+static const wchar_t kRegKeyRoot[] = L"Software\\mozilla.org\\GRE";
+static const wchar_t kRegFileGlobal[] = L"global.reginfo";
+static const wchar_t kRegFileUser[] = L"user.reginfo";
 
 static nsresult
-MakeVersionKey(HKEY root, const char* keyname, const nsCString &grehome,
+MakeVersionKey(HKEY root, const wchar_t* keyname, const nsString &grehome,
                const GREProperty *aProperties, PRUint32 aPropertiesLen,
-               const char *aGREMilestone)
+               const wchar_t *aGREMilestone)
 {
   HKEY  subkey;
   DWORD disp;
-  if (::RegCreateKeyEx(root, keyname, NULL, NULL, 0, KEY_WRITE, NULL,
+  
+  if (::RegCreateKeyExW(root, keyname, NULL, NULL, 0, KEY_WRITE, NULL,
                        &subkey, &disp) != ERROR_SUCCESS)
     return NS_ERROR_FAILURE;
 
   if (disp != REG_CREATED_NEW_KEY) {
     ::RegCloseKey(subkey);
     return NS_ERROR_FAILURE;
   }
 
   PRBool failed = PR_FALSE;
-  failed |= ::RegSetValueEx(subkey, "Version", NULL, REG_SZ,
-                            (BYTE*) aGREMilestone,
-                            strlen(aGREMilestone)) != ERROR_SUCCESS;
-  failed |= ::RegSetValueEx(subkey, "GreHome", NULL, REG_SZ,
-                            (BYTE*) grehome.get(),
-                            grehome.Length()) != ERROR_SUCCESS;
-
+  failed |= ::RegSetValueExW(subkey, L"Version", NULL, REG_SZ,
+			     (BYTE*) aGREMilestone,
+			     sizeof(PRUnichar) * (wcslen(aGREMilestone) + 1)) 
+    != ERROR_SUCCESS;
+  failed |= ::RegSetValueExW(subkey, L"GreHome", NULL, REG_SZ,
+			     (BYTE*) grehome.get(),
+			     sizeof(PRUnichar) * (grehome.Length() + 1)) 
+    != ERROR_SUCCESS;
+  
   for (PRUint32 i = 0; i < aPropertiesLen; ++i) {
-    failed |= ::RegSetValueEx(subkey, aProperties[i].property, NULL, REG_SZ,
-                              (BYTE*) aProperties[i].value,
-                              strlen(aProperties[i].value)) != ERROR_SUCCESS;
+    // Properties should be ascii only 
+    NS_ConvertASCIItoUTF16 prop(aProperties[i].property);
+    NS_ConvertASCIItoUTF16 val(aProperties[i].value);
+    failed |= ::RegSetValueExW(subkey, prop.get(), NULL, REG_SZ, 
+			       (BYTE*) val.get(), 
+			       sizeof(wchar_t)*(val.Length()+1)
+			       ) != ERROR_SUCCESS;
   }
 
   ::RegCloseKey(subkey);
 
   if (failed) {
     // we created a key but couldn't fill it properly: delete it
-    ::RegDeleteKey(root, keyname);
+    ::RegDeleteKeyW(root, keyname);
     return NS_ERROR_FAILURE;
   }
 
   return NS_OK;
 }
 
 int
 RegisterXULRunner(PRBool aRegisterGlobally, nsIFile* aLocation,
                   const GREProperty *aProperties, PRUint32 aPropertiesLen,
-                  const char *aGREMilestone)
+                  const char *aGREMilestoneAscii)
 {
   // Register ourself in the windows registry, and record what key we created
   // for future unregistration.
 
   nsresult rv;
   PRBool irv;
   int i;
-
-  nsCString greHome;
-  rv = aLocation->GetNativePath(greHome);
+  NS_ConvertASCIItoUTF16 aGREMilestone(aGREMilestoneAscii);
+  nsString greHome;
+  rv = aLocation->GetPath(greHome);
   if (NS_FAILED(rv))
     return rv;
 
   nsCOMPtr<nsIFile> savedInfoFile;
   aLocation->Clone(getter_AddRefs(savedInfoFile));
   nsCOMPtr<nsILocalFile> localSaved(do_QueryInterface(savedInfoFile));
   if (!localSaved)
     return PR_FALSE;
 
-  const char *infoname = aRegisterGlobally ? kRegFileGlobal : kRegFileUser;
-  localSaved->AppendNative(nsDependentCString(infoname));
+  const wchar_t *infoname = aRegisterGlobally ? kRegFileGlobal : kRegFileUser;
+  localSaved->Append(nsDependentString(infoname));
 
   PRFileDesc* fd = nsnull;
   rv = localSaved->OpenNSPRFileDesc(PR_CREATE_FILE | PR_RDWR, 0664, &fd);
   if (NS_FAILED(rv)) {
     // XXX report error?
     return PR_FALSE;
   }
 
   HKEY rootKey = NULL;
-  char keyName[MAXPATHLEN];
+  wchar_t keyName[MAXPATHLEN];
   PRInt32 r;
 
-  if (::RegCreateKeyEx(aRegisterGlobally ? HKEY_LOCAL_MACHINE :
-                                           HKEY_CURRENT_USER,
-                       kRegKeyRoot, NULL, NULL, 0, KEY_WRITE,
-                       NULL, &rootKey, NULL) != ERROR_SUCCESS) {
+  if (::RegCreateKeyExW(aRegisterGlobally ? HKEY_LOCAL_MACHINE :
+			HKEY_CURRENT_USER,
+			kRegKeyRoot, NULL, NULL, 0, KEY_WRITE,
+			NULL, &rootKey, NULL) != ERROR_SUCCESS) {
     irv = PR_FALSE;
     goto reg_end;
   }
 
   r = PR_Read(fd, keyName, MAXPATHLEN);
   if (r < 0) {
     irv = PR_FALSE;
     goto reg_end;
   }
 
   if (r > 0) {
     keyName[r] = '\0';
 
     // There was already a .reginfo file, let's see if we are already
     // registered.
     HKEY existing = NULL;
-    if (::RegOpenKeyEx(rootKey, keyName, NULL, KEY_QUERY_VALUE, &existing) ==
+    if (::RegOpenKeyExW(rootKey, keyName, NULL, KEY_QUERY_VALUE, &existing) ==
         ERROR_SUCCESS) {
       fprintf(stderr, "Warning: Registry key Software\\mozilla.org\\GRE\\%s already exists.\n"
               "No action was performed.\n",
               keyName);
       irv = PR_FALSE;
       goto reg_end;
     }
 
@@ -167,32 +175,34 @@ RegisterXULRunner(PRBool aRegisterGlobal
     rv = localSaved->OpenNSPRFileDesc(PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE, 0664, &fd);
     if (NS_FAILED(rv)) {
       // XXX report error?
       irv = PR_FALSE;
       goto reg_end;
     }
   }
 
-  strcpy(keyName, aGREMilestone);
+  wcscpy(keyName, aGREMilestone.get());
   rv = MakeVersionKey(rootKey, keyName, greHome, aProperties, aPropertiesLen,
-                      aGREMilestone);
+                      aGREMilestone.get());
   if (NS_SUCCEEDED(rv)) {
-    PR_Write(fd, keyName, strlen(keyName));
+    NS_ConvertUTF16toUTF8 keyNameAscii(keyName);
+    PR_Write(fd, keyNameAscii.get(), sizeof(char)*keyNameAscii.Length());
     irv = PR_TRUE;
     goto reg_end;
   }
   
   for (i = 0; i < 1000; ++i) {
-    sprintf(keyName, "%s_%i", aGREMilestone,  i);
+    swprintf(keyName, L"%s_%i", aGREMilestone.get(),  i);
     rv = MakeVersionKey(rootKey, keyName, greHome,
                         aProperties, aPropertiesLen,
-                        aGREMilestone);
+                        aGREMilestone.get());
     if (NS_SUCCEEDED(rv)) {
-      PR_Write(fd, keyName, strlen(keyName));
+      NS_ConvertUTF16toUTF8 keyNameAscii(keyName);
+      PR_Write(fd, keyNameAscii.get(), sizeof(char)*keyNameAscii.Length());
       irv = PR_TRUE;
       goto reg_end;
     }
   }
 
   irv = PR_FALSE;
 
 reg_end:
@@ -210,49 +220,49 @@ UnregisterXULRunner(PRBool aGlobal, nsIF
                     const char *aGREMilestone)
 {
   nsCOMPtr<nsIFile> savedInfoFile;
   aLocation->Clone(getter_AddRefs(savedInfoFile));
   nsCOMPtr<nsILocalFile> localSaved (do_QueryInterface(savedInfoFile));
   if (!localSaved)
     return;
 
-  const char *infoname = aGlobal ? kRegFileGlobal : kRegFileUser;
-  localSaved->AppendNative(nsDependentCString(infoname));
+  const wchar_t *infoname = aGlobal ? kRegFileGlobal : kRegFileUser;
+  localSaved->Append(nsDependentString(infoname));
 
   PRFileDesc* fd = nsnull;
   nsresult rv = localSaved->OpenNSPRFileDesc(PR_RDONLY, 0, &fd);
   if (NS_FAILED(rv)) {
     // XXX report error?
     return;
   }
 
-  char keyName[MAXPATHLEN];
+  wchar_t keyName[MAXPATHLEN];
   PRInt32 r = PR_Read(fd, keyName, MAXPATHLEN);
   PR_Close(fd);
 
   localSaved->Remove(PR_FALSE);
 
   if (r <= 0)
     return;
 
   keyName[r] = '\0';
 
   HKEY rootKey = NULL;
-  if (::RegOpenKeyEx(aGlobal ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
-                     kRegKeyRoot, 0, KEY_READ, &rootKey) != ERROR_SUCCESS)
+  if (::RegOpenKeyExW(aGlobal ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
+		      kRegKeyRoot, 0, KEY_READ, &rootKey) != ERROR_SUCCESS)
     return;
 
   HKEY subKey = NULL;
-  if (::RegOpenKeyEx(rootKey, keyName, 0, KEY_READ, &subKey) == ERROR_SUCCESS) {
+  if (::RegOpenKeyExW(rootKey, keyName, 0, KEY_READ, &subKey) == ERROR_SUCCESS) {
 
     char regpath[MAXPATHLEN];
     DWORD reglen = MAXPATHLEN;
 
-    if (::RegQueryValueEx(subKey, "GreHome", NULL, NULL,
+    if (::RegQueryValueExW(subKey, L"GreHome", NULL, NULL,
                           (BYTE*) regpath, &reglen) == ERROR_SUCCESS) {
 
       nsCOMPtr<nsILocalFile> regpathfile;
       rv = NS_NewNativeLocalFile(nsDependentCString(regpath), PR_FALSE,
                                  getter_AddRefs(regpathfile));
 
       PRBool eq;
       if (NS_SUCCEEDED(rv) && 
@@ -268,11 +278,11 @@ UnregisterXULRunner(PRBool aGlobal, nsIF
 
         return;
       }
     }
 
     ::RegCloseKey(subKey);
   }
 
-  ::RegDeleteKey(rootKey, keyName);
+  ::RegDeleteKeyW(rootKey, keyName);
   ::RegCloseKey(rootKey);
 }