Bug 1572788 - Make nsXULAppInfo::GetUserCanElevate reuse GetElevationType. r=aklotz
authorToshihito Kikuchi <tkikuchi@mozilla.com>
Mon, 28 Oct 2019 14:53:42 +0000
changeset 499468 08f79829f9f22e52cc4a943ff7f7c7758369283b
parent 499467 17e43e854a153904d3fa7501c6049fb6866c2c80
child 499469 c84e0946e319eddcf27908ed62c1850287d430ac
push id98982
push userrmaries@mozilla.com
push dateMon, 28 Oct 2019 15:23:33 +0000
treeherderautoland@08f79829f9f2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaklotz
bugs1572788
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1572788 - Make nsXULAppInfo::GetUserCanElevate reuse GetElevationType. r=aklotz `nsXULAppInfo::GetUserCanElevate` can reuse `GetElevationType` and we can remove `VistaTokenElevationType` definition. Differential Revision: https://phabricator.services.mozilla.com/D49691
browser/app/winlauncher/LaunchUnelevated.cpp
toolkit/xre/nsAppRunner.cpp
widget/windows/WinHeaderOnlyUtils.h
--- a/browser/app/winlauncher/LaunchUnelevated.cpp
+++ b/browser/app/winlauncher/LaunchUnelevated.cpp
@@ -6,32 +6,21 @@
 
 #include "LaunchUnelevated.h"
 
 #include "mozilla/Assertions.h"
 #include "mozilla/CmdLineAndEnvUtils.h"
 #include "mozilla/mscom/ProcessRuntime.h"
 #include "mozilla/RefPtr.h"
 #include "mozilla/ShellHeaderOnlyUtils.h"
+#include "mozilla/WinHeaderOnlyUtils.h"
 #include "nsWindowsHelpers.h"
 
 #include <windows.h>
 
-static mozilla::LauncherResult<TOKEN_ELEVATION_TYPE> GetElevationType(
-    const nsAutoHandle& aToken) {
-  DWORD retLen;
-  TOKEN_ELEVATION_TYPE elevationType;
-  if (!::GetTokenInformation(aToken.get(), TokenElevationType, &elevationType,
-                             sizeof(elevationType), &retLen)) {
-    return LAUNCHER_ERROR_FROM_LAST();
-  }
-
-  return elevationType;
-}
-
 static mozilla::LauncherResult<bool> IsHighIntegrity(
     const nsAutoHandle& aToken) {
   DWORD reqdLen;
   if (!::GetTokenInformation(aToken.get(), TokenIntegrityLevel, nullptr, 0,
                              &reqdLen)) {
     DWORD err = ::GetLastError();
     if (err != ERROR_INSUFFICIENT_BUFFER) {
       return LAUNCHER_ERROR_FROM_WIN32(err);
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -899,54 +899,41 @@ nsXULAppInfo::GetLauncherProcessState(ui
   *aResult = static_cast<uint32_t>(gLauncherProcessState.value());
   return NS_OK;
 #else
   return NS_ERROR_NOT_AVAILABLE;
 #endif
 }
 
 #ifdef XP_WIN
-// Matches the enum in WinNT.h for the Vista SDK but renamed so that we can
-// safely build with the Vista SDK and without it.
-typedef enum {
-  VistaTokenElevationTypeDefault = 1,
-  VistaTokenElevationTypeFull,
-  VistaTokenElevationTypeLimited
-} VISTA_TOKEN_ELEVATION_TYPE;
-
-// avoid collision with TokeElevationType enum in WinNT.h
-// of the Vista SDK
-#  define VistaTokenElevationType static_cast<TOKEN_INFORMATION_CLASS>(18)
-
 NS_IMETHODIMP
 nsXULAppInfo::GetUserCanElevate(bool* aUserCanElevate) {
-  HANDLE hToken;
-
-  VISTA_TOKEN_ELEVATION_TYPE elevationType;
-  DWORD dwSize;
-
-  if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken) ||
-      !GetTokenInformation(hToken, VistaTokenElevationType, &elevationType,
-                           sizeof(elevationType), &dwSize)) {
+  HANDLE rawToken;
+  if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &rawToken)) {
+    *aUserCanElevate = false;
+    return NS_OK;
+  }
+
+  nsAutoHandle token(rawToken);
+  LauncherResult<TOKEN_ELEVATION_TYPE> elevationType = GetElevationType(token);
+  if (elevationType.isErr()) {
     *aUserCanElevate = false;
-  } else {
-    // The possible values returned for elevationType and their meanings are:
-    //   TokenElevationTypeDefault: The token does not have a linked token
-    //     (e.g. UAC disabled or a standard user, so they can't be elevated)
-    //   TokenElevationTypeFull: The token is linked to an elevated token
-    //     (e.g. UAC is enabled and the user is already elevated so they can't
-    //      be elevated again)
-    //   TokenElevationTypeLimited: The token is linked to a limited token
-    //     (e.g. UAC is enabled and the user is not elevated, so they can be
-    //      elevated)
-    *aUserCanElevate = (elevationType == VistaTokenElevationTypeLimited);
-  }
-
-  if (hToken) CloseHandle(hToken);
-
+    return NS_OK;
+  }
+
+  // The possible values returned for elevationType and their meanings are:
+  //   TokenElevationTypeDefault: The token does not have a linked token
+  //     (e.g. UAC disabled or a standard user, so they can't be elevated)
+  //   TokenElevationTypeFull: The token is linked to an elevated token
+  //     (e.g. UAC is enabled and the user is already elevated so they can't
+  //      be elevated again)
+  //   TokenElevationTypeLimited: The token is linked to a limited token
+  //     (e.g. UAC is enabled and the user is not elevated, so they can be
+  //      elevated)
+  *aUserCanElevate = (elevationType.inspect() == TokenElevationTypeLimited);
   return NS_OK;
 }
 #endif
 
 NS_IMETHODIMP
 nsXULAppInfo::GetEnabled(bool* aEnabled) {
   *aEnabled = CrashReporter::GetEnabled();
   return NS_OK;
--- a/widget/windows/WinHeaderOnlyUtils.h
+++ b/widget/windows/WinHeaderOnlyUtils.h
@@ -608,11 +608,23 @@ inline LauncherResult<ModuleVersion> Get
   return GetModuleVersion(fullPath.get());
 }
 #endif  // defined(MOZILLA_INTERNAL_API)
 
 struct CoTaskMemFreeDeleter {
   void operator()(void* aPtr) { ::CoTaskMemFree(aPtr); }
 };
 
+inline LauncherResult<TOKEN_ELEVATION_TYPE> GetElevationType(
+    const nsAutoHandle& aToken) {
+  DWORD retLen;
+  TOKEN_ELEVATION_TYPE elevationType;
+  if (!::GetTokenInformation(aToken.get(), TokenElevationType, &elevationType,
+                             sizeof(elevationType), &retLen)) {
+    return LAUNCHER_ERROR_FROM_LAST();
+  }
+
+  return elevationType;
+}
+
 }  // namespace mozilla
 
 #endif  // mozilla_WinHeaderOnlyUtils_h