FancyZones: optimize elevation detection logic (#2103)

This commit is contained in:
Andrey Nekrasov
2020-04-13 18:22:37 +03:00
committed by GitHub
parent 6bb0f18d53
commit 86704efcec
4 changed files with 23 additions and 21 deletions

View File

@@ -364,8 +364,9 @@ WindowState get_window_state(HWND hwnd)
return RESTORED; return RESTORED;
} }
bool is_process_elevated() bool is_process_elevated(const bool use_cached_value)
{ {
auto detection_func = []() {
HANDLE token = nullptr; HANDLE token = nullptr;
bool elevated = false; bool elevated = false;
@@ -385,6 +386,9 @@ bool is_process_elevated()
} }
return elevated; return elevated;
};
static const bool cached_value = detection_func();
return use_cached_value ? cached_value : detection_func();
} }
bool drop_elevated_privileges() bool drop_elevated_privileges()

View File

@@ -61,7 +61,7 @@ enum WindowState
WindowState get_window_state(HWND hwnd); WindowState get_window_state(HWND hwnd);
// Returns true if the current process is running with elevated privileges // 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 // Drops the elevated privilages if present
bool drop_elevated_privileges(); bool drop_elevated_privileges();
@@ -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 // Returns true if the current process is running from administrator account
bool check_user_is_admin(); 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); 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 // Get the executable path or module name for modern apps

View File

@@ -867,10 +867,8 @@ void FancyZones::UpdateDragState(HWND window, require_write_lock) noexcept
m_dragEnabled = !(shift | mouse); m_dragEnabled = !(shift | mouse);
} }
const bool windowElevated = IsProcessOfWindowElevated(window);
static const bool meElevated = is_process_elevated();
static bool warning_shown = false; static bool warning_shown = false;
if (windowElevated && !meElevated) if (!is_process_elevated() && IsProcessOfWindowElevated(window))
{ {
m_dragEnabled = false; m_dragEnabled = false;
if (!warning_shown && !is_cant_drag_elevated_warning_disabled()) if (!warning_shown && !is_cant_drag_elevated_warning_disabled())

View File

@@ -567,7 +567,7 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
Trace::RegisterProvider(); Trace::RegisterProvider();
CoInitialize(nullptr); 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) if (should_try_drop_privileges)
{ {