Log errors when invoking the rename dance by the browser.

This should be helpful when trying to understand why it sometimes fails.
Users can launch Chrome with something like:

"%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe" --enable-logging=stderr
  or perhaps:
"%LOCALAPPDATA%\Google\Chrome\Application\chrome.exe" --enable-logging=stderr

to see log messages on the console along the lines of:

[14512:3284:0130/153142.365:ERROR:upgrade_util_win.cc(70)] IProcessLauncher::LaunchCmdElevated failed; hr = 80040e01

BUG=926816

Change-Id: Ic3110e3c547c9e3ce51d0c37aba640ff563c6062
Reviewed-on: https://chromium-review.googlesource.com/c/1445895
Auto-Submit: Greg Thompson <[email protected]>
Reviewed-by: Gabriel Charette <[email protected]>
Commit-Queue: Gabriel Charette <[email protected]>
Cr-Commit-Position: refs/heads/master@{#628121}
diff --git a/chrome/browser/first_run/upgrade_util_win.cc b/chrome/browser/first_run/upgrade_util_win.cc
index 9c30b37..8d77b39 100644
--- a/chrome/browser/first_run/upgrade_util_win.cc
+++ b/chrome/browser/first_run/upgrade_util_win.cc
@@ -11,6 +11,7 @@
 #include <wrl/client.h>
 
 #include <algorithm>
+#include <ios>
 #include <string>
 
 #include "base/base_paths.h"
@@ -53,24 +54,41 @@
 bool InvokeGoogleUpdateForRename() {
 #if defined(GOOGLE_CHROME_BUILD)
   Microsoft::WRL::ComPtr<IProcessLauncher> ipl;
-  if (!FAILED(::CoCreateInstance(__uuidof(ProcessLauncherClass), nullptr,
-                                 CLSCTX_ALL, IID_PPV_ARGS(&ipl)))) {
-    ULONG_PTR phandle = NULL;
-    DWORD id = GetCurrentProcessId();
-    if (!FAILED(ipl->LaunchCmdElevated(install_static::GetAppGuid(),
-                                       google_update::kRegRenameCmdField, id,
-                                       &phandle))) {
-      HANDLE handle = HANDLE(phandle);
-      WaitForSingleObject(handle, INFINITE);
-      DWORD exit_code;
-      ::GetExitCodeProcess(handle, &exit_code);
-      ::CloseHandle(handle);
-      if (exit_code == installer::RENAME_SUCCESSFUL)
-        return true;
-    }
+  HRESULT hr = ::CoCreateInstance(__uuidof(ProcessLauncherClass), nullptr,
+                                  CLSCTX_ALL, IID_PPV_ARGS(&ipl));
+  if (FAILED(hr)) {
+    LOG(ERROR) << "CoCreate ProcessLauncherClass failed; hr = " << std::hex
+               << hr;
+    return false;
   }
-#endif  // GOOGLE_CHROME_BUILD
+
+  ULONG_PTR process_handle;
+  hr = ipl->LaunchCmdElevated(install_static::GetAppGuid(),
+                              google_update::kRegRenameCmdField,
+                              ::GetCurrentProcessId(), &process_handle);
+  if (FAILED(hr)) {
+    LOG(ERROR) << "IProcessLauncher::LaunchCmdElevated failed; hr = "
+               << std::hex << hr;
+    return false;
+  }
+
+  base::Process rename_process(
+      reinterpret_cast<base::ProcessHandle>(process_handle));
+  int exit_code;
+  if (!rename_process.WaitForExit(&exit_code)) {
+    PLOG(ERROR) << "WaitForExit of rename process failed";
+    return false;
+  }
+
+  if (exit_code != installer::RENAME_SUCCESSFUL) {
+    LOG(ERROR) << "Rename process failed with exit code " << exit_code;
+    return false;
+  }
+
+  return true;
+#else   // GOOGLE_CHROME_BUILD
   return false;
+#endif  // GOOGLE_CHROME_BUILD
 }
 
 }  // namespace
@@ -118,25 +136,45 @@
   // If this is a user-level install, directly launch a process to rename Chrome
   // executables. Obtain the command to launch the process from the registry.
   base::win::RegKey key;
-  if (key.Open(HKEY_CURRENT_USER, install_static::GetClientsKeyPath().c_str(),
-               KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
-    std::wstring rename_cmd;
-    if (key.ReadValue(google_update::kRegRenameCmdField,
-                      &rename_cmd) == ERROR_SUCCESS) {
-      base::LaunchOptions options;
-      options.wait = true;
-      options.start_hidden = true;
-      base::Process process = base::LaunchProcess(rename_cmd, options);
-      if (process.IsValid()) {
-        DWORD exit_code;
-        ::GetExitCodeProcess(process.Handle(), &exit_code);
-        if (exit_code == installer::RENAME_SUCCESSFUL)
-          return true;
-      }
-    }
+  auto result =
+      key.Open(HKEY_CURRENT_USER, install_static::GetClientsKeyPath().c_str(),
+               KEY_QUERY_VALUE | KEY_WOW64_32KEY);
+  if (result != ERROR_SUCCESS) {
+    ::SetLastError(result);
+    PLOG(ERROR) << "Open Clients key failed";
+    return false;
   }
 
-  return false;
+  std::wstring rename_cmd;
+  result = key.ReadValue(google_update::kRegRenameCmdField, &rename_cmd);
+  if (result != ERROR_SUCCESS) {
+    ::SetLastError(result);
+    PLOG(ERROR) << "Read rename command failed";
+    return false;
+  }
+
+  base::LaunchOptions options;
+  options.wait = true;
+  options.start_hidden = true;
+  ::SetLastError(ERROR_SUCCESS);
+  base::Process process = base::LaunchProcess(rename_cmd, options);
+  if (!process.IsValid()) {
+    PLOG(ERROR) << "Launch rename process failed";
+    return false;
+  }
+
+  DWORD exit_code;
+  if (!::GetExitCodeProcess(process.Handle(), &exit_code)) {
+    PLOG(ERROR) << "GetExitCodeProcess of rename process failed";
+    return false;
+  }
+
+  if (exit_code != installer::RENAME_SUCCESSFUL) {
+    LOG(ERROR) << "Rename process failed with exit code " << exit_code;
+    return false;
+  }
+
+  return true;
 }
 
 bool IsRunningOldChrome() {