Skip to content

Commit

Permalink
FancyZones: optimize elevation detection logic (#2103)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyoyuppe committed Apr 13, 2020
1 parent 6bb0f18 commit 86704ef
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
34 changes: 19 additions & 15 deletions src/common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,27 +364,31 @@ WindowState get_window_state(HWND hwnd)
return RESTORED;
}

bool is_process_elevated()
bool is_process_elevated(const bool use_cached_value)
{
HANDLE token = nullptr;
bool elevated = false;
auto detection_func = []() {
HANDLE token = nullptr;
bool elevated = false;

if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
{
TOKEN_ELEVATION elevation;
DWORD size;
if (GetTokenInformation(token, TokenElevation, &elevation, sizeof(elevation), &size))
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
{
elevated = (elevation.TokenIsElevated != 0);
TOKEN_ELEVATION elevation;
DWORD size;
if (GetTokenInformation(token, TokenElevation, &elevation, sizeof(elevation), &size))
{
elevated = (elevation.TokenIsElevated != 0);
}
}
}

if (token)
{
CloseHandle(token);
}
if (token)
{
CloseHandle(token);
}

return elevated;
return elevated;
};
static const bool cached_value = detection_func();
return use_cached_value ? cached_value : detection_func();
}

bool drop_elevated_privileges()
Expand Down
4 changes: 2 additions & 2 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ enum WindowState
WindowState get_window_state(HWND hwnd);

// Returns true if the current process is running with elevated privileges
bool is_process_elevated();
bool is_process_elevated(const bool use_cached_value = true);

// Drops the elevated privilages if present
bool drop_elevated_privileges();
Expand All @@ -78,7 +78,7 @@ bool run_same_elevation(const std::wstring& file, const std::wstring& params);
// Returns true if the current process is running from administrator account
bool check_user_is_admin();

//Returns true when one or more strings from vector found in string
// Returns true when one or more strings from vector found in string
bool find_app_name_in_path(const std::wstring& where, const std::vector<std::wstring>& what);

// Get the executable path or module name for modern apps
Expand Down
4 changes: 1 addition & 3 deletions src/modules/fancyzones/lib/FancyZones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,10 +867,8 @@ void FancyZones::UpdateDragState(HWND window, require_write_lock) noexcept
m_dragEnabled = !(shift | mouse);
}

const bool windowElevated = IsProcessOfWindowElevated(window);
static const bool meElevated = is_process_elevated();
static bool warning_shown = false;
if (windowElevated && !meElevated)
if (!is_process_elevated() && IsProcessOfWindowElevated(window))
{
m_dragEnabled = false;
if (!warning_shown && !is_cant_drag_elevated_warning_disabled())
Expand Down
2 changes: 1 addition & 1 deletion src/settings/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
Trace::RegisterProvider();
CoInitialize(nullptr);

const bool should_try_drop_privileges = !initialize_com_security_policy_for_webview() && is_process_elevated();
const bool should_try_drop_privileges = !initialize_com_security_policy_for_webview() && is_process_elevated(false);

if (should_try_drop_privileges)
{
Expand Down

0 comments on commit 86704ef

Please sign in to comment.